Re: [Tutor] Tutor Digest, Vol 100, Issue 58

2012-06-26 Thread Alan Gauld

On 26/06/12 02:22, Steven D'Aprano wrote:


I think you've messed up your quoting. It was Mike Nickey, not Emile,
who suggested using w[0] == 'x'.


Yes, but Emile's comment was in context of Mike's assertion about w[0].
However, reading it back I think that the This in Emile's comment was 
actually intended to refer back to the startswith discussed in his 
earlier(unquoted) comment...



--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Looping over histogram plots

2012-06-26 Thread Elaina Ann Hyde
Hello all,
   I have been making some big multiplots lately and found a nice little
way of writing out 30 plots as follows, this part works great and leads up
to my question, here I have 30 sets defined by the set=(), in this case I
get a nice arrangement of 30 plots for V(GSR) and Log(g) (2 variables):
-
fig, axes = plt.subplots(nrows=5, ncols=6, figsize=(12,6))

index=0
for b in axes:
for ax in b:
index=index+1
set=(dat['a'+str(index)] == 1.00)
#write the data
ax.plot(VGSR[set],logg[set],'.b')

#label the axis
if index==13.0:
ax.set_ylabel('counts')
if index = 25.0:
ax.set_xlabel('VGSR')
plt.show()

However, if I want a histogram plot instead, I get my histogram only on the
last i.e. set(dat['a30']==1) plot, so instead of 30 nice plots I get 29
empty ones and one crowded plot with some number of histograms in it.  The
set is the same, the data is the same, the only difference is the
histogram, the code also looks pretty much the same, it is:
#--
fig, axes = plt.subplots(nrows=5, ncols=6, figsize=(12,6))

index=0
for b in axes:
for ax in b:
index=index+1
set=(dat['a'+str(index)] == 1.00)
#write the data
P.hist(VGSR[set],bins=30, normed=True)

#label the axis
if index==13.0:
ax.set_ylabel('counts')
if index = 25.0:
ax.set_xlabel('VGSR')
plt.show()
#---
Here I use import pylab as P and import matplotlib.pyplot as plt

Any ideas would be appreciated, thanks in advance!
~Elaina

-- 
PhD Candidate
Department of Physics and Astronomy
Faculty of Science
Macquarie University
North Ryde, NSW 2109, Australia
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Looping over histogram plots

2012-06-26 Thread Don Jennings
 Message: 1
 Date: Tue, 26 Jun 2012 18:40:50 +1000
 From: Elaina Ann Hyde elainah...@gmail.com
 To: tutor@python.org
 Subject: [Tutor] Looping over histogram plots

snip

set=(dat['a'+str(index)] == 1.00)

You should not override the builtin set() type [1] as you've done here by 
assigning it.

 #write the data
P.hist(VGSR[set],bins=30, normed=True)

I am not familiar with matplotlib, etc. but given that the primary difference 
in your two code samples is where you write the data, I suspect you want 
something like:

ax.plot(P.hist(VGSR[set],bins=30, normed=True))

Take care,
Don

[1] http://docs.python.org/library/stdtypes.html#set
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Barcode decoder in Python

2012-06-26 Thread Sithembewena Lloyd Dube
Hi everyone,

Is there any decent barcode decoder software which one could use to read
image barcodes and return a result to a calling function/ app? I wish to
implement a Python server backend which would import and use such a module
(if it were that, for instance).

Thanks.

-- 
Regards,
Sithembewena Lloyd Dube
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Generating random alphanumeric codes

2012-06-26 Thread Sithembewena Lloyd Dube
HI,

Would anyone have tips on how to generate random 4-digit alphanumeric codes
in python? Also, how does one calculate the number of possible combinations?

Thanks in advance.

-- 
Regards,
Sithembewena Lloyd Dube
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Generating random alphanumeric codes

2012-06-26 Thread Hugo Arts
On Tue, Jun 26, 2012 at 4:52 PM, Sithembewena Lloyd Dube
zebr...@gmail.comwrote:

 HI,

 Would anyone have tips on how to generate random 4-digit alphanumeric
 codes in python? Also, how does one calculate the number of possible
 combinations?

 Thanks in advance.


Python's, random module is your friend. I'd suggest looking at the
random.choice() function. As for the number of possibilities, if your codes
are not case sensitive, you'll have 10 + 26 = 36 possibilities for each
character/digit. 4 digits, so 36**4 possible codes.

Hugo
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Generating random alphanumeric codes

2012-06-26 Thread taserian
First of all, determine your alphabet (the pool of characters you'll derive
your 4-character code from):

- For example, using an English alphabet with both lowercase and uppercase
letters and digits 0-9 makes for 62 characters (26 + 26 + 10).

Then, ask if you want to allow a character to repeat itself in the
4-character code.

- If so, then it's easy: take the length of your alphabet, and calculate
(length)^4. In my example above, it would be 62^4 = 14,776,336

- If not, then it's a little more complicated, calculate (length) *
(length-1) * (length-2) * (length-3), or in other words the factorial of
length minus the factorial of (length - 4). In my example, 62! - (62-4)! =
13,388,280

AR

On Tue, Jun 26, 2012 at 10:52 AM, Sithembewena Lloyd Dube zebr...@gmail.com
 wrote:

 HI,

 Would anyone have tips on how to generate random 4-digit alphanumeric
 codes in python? Also, how does one calculate the number of possible
 combinations?

 Thanks in advance.

 --
 Regards,
 Sithembewena Lloyd Dube

 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Generating random alphanumeric codes

2012-06-26 Thread taserian
Correcting what was said below.

- If not, then it's a little more complicated, calculate (length) *
(length-1) * (length-2) * (length-3), or in other words the factorial of
length divided by the factorial of (length - 4). In my example, 62! /
(62-4)! = 13,388,280

On Tue, Jun 26, 2012 at 11:08 AM, taserian taser...@gmail.com wrote:

 First of all, determine your alphabet (the pool of characters you'll
 derive your 4-character code from):

 - For example, using an English alphabet with both lowercase and uppercase
 letters and digits 0-9 makes for 62 characters (26 + 26 + 10).

 Then, ask if you want to allow a character to repeat itself in the
 4-character code.

 - If so, then it's easy: take the length of your alphabet, and calculate
 (length)^4. In my example above, it would be 62^4 = 14,776,336

 - If not, then it's a little more complicated, calculate (length) *
 (length-1) * (length-2) * (length-3), or in other words the factorial of
 length minus the factorial of (length - 4). In my example, 62! - (62-4)! =
 13,388,280

 AR

 On Tue, Jun 26, 2012 at 10:52 AM, Sithembewena Lloyd Dube 
 zebr...@gmail.com wrote:

 HI,

 Would anyone have tips on how to generate random 4-digit alphanumeric
 codes in python? Also, how does one calculate the number of possible
 combinations?

 Thanks in advance.

 --
 Regards,
 Sithembewena Lloyd Dube

 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 100, Issue 58

2012-06-26 Thread Emile van Sebille

On 6/26/2012 1:10 AM Alan Gauld said...

On 26/06/12 02:22, Steven D'Aprano wrote:


I think you've messed up your quoting. It was Mike Nickey, not Emile,
who suggested using w[0] == 'x'.


Yes, but Emile's comment was in context of Mike's assertion about w[0].
However, reading it back I think that the This in Emile's comment was
actually intended to refer back to the startswith discussed in his
earlier(unquoted) comment...


Actually, my first comment at that location was phrased with a negative 
as in something like 'and doesn't suffer the side effect of failing when 
the string is empty like this does' but as I edited before hitting send 
I rephrased my response and left the 'this' oinappropriately placed.


Emile



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Generating random alphanumeric codes

2012-06-26 Thread Martin A. Brown

Hello,

 : Would anyone have tips on how to generate random 4-digit 
 : alphanumeric codes in python? Also, how does one calculate the 
 : number of possible combinations?

Here are some thoughts that come to my mind:

  import string
  import random

  # -- technique #1, using random.choice
  print ''.join([random.choice(string.lowercase) for x in range(4)])

  # -- technique #2, using random.shuffle
  t = [ x for x in string.lowercase ]
  random.shuffle(t)
  ''.join(t[0:4])

  # -- technique #3, using random.sample
  ''.join(random.sample(string.lowercase,4))

I would be quite surprised if there were not more efficient ways of 
accomplishing this.

-Martin

-- 
Martin A. Brown
http://linux-ip.net/
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Generating random alphanumeric codes

2012-06-26 Thread Dave Angel
On 06/26/2012 03:47 PM, Martin A. Brown wrote:
 Hello,

  : Would anyone have tips on how to generate random 4-digit 
  : alphanumeric codes in python? Also, how does one calculate the 
  : number of possible combinations?

 Here are some thoughts that come to my mind:

   import string
   import random

   # -- technique #1, using random.choice
   print ''.join([random.choice(string.lowercase) for x in range(4)])

   # -- technique #2, using random.shuffle
   t = [ x for x in string.lowercase ]
   random.shuffle(t)
   ''.join(t[0:4])

   # -- technique #3, using random.sample
   ''.join(random.sample(string.lowercase,4))

 I would be quite surprised if there were not more efficient ways of 
 accomplishing this.

 -Martin

Two problems that I see:

string.lowercase includes the lowercase letters, but not the digits.

Your methods 2 and 3 produce different results than method1.  The OP
never specified which he wanted, but the distinction can well be important.

Likewise his query about how many possibilities there are depends on the
same question:
 Are duplicates permitted between the four characters in any given
answer.




-- 

DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Re.findall question

2012-06-26 Thread Alexander Quest
I'm a bit confused about extracting data using re.search or re.findall.

Say I have the following code: tuples =
re.findall(r'blahblah(\d+)yattayattayatta(\w+)moreblahblahblah(\w+)over',
text)

So I'm looking for that string in 'text', and I intend to extract the parts
which have parentheses around them. And it works: the variable tuples,
which I assigned to get the return of re.findall, returns a tuple list,
each 'element' therein being a tuple of 3 elements (which is what I wanted
since I had 3 sets of parentheses).

My question is how does Python know to return just the part in the
parentheses and not to return the blahblah and the yattayattayatta,
etc...? The 're.search' function returns the whole thing, and if I want
just the parentheses parts, I do tuples.group(1) or tuples.group(2) or
tuples.group(3), depending on which set of parentheses I want. Does the
re.findall command by default ignore anything outside of the parentheses
and only return the parentheses as a grouping withing one tuple (i.e., the
first element in tuples would be, as it is, a list comprised of 3
elements corresponding respectively to the 1st, 2nd, and 3rd parentheses)?
Thank you for reading.

-Alex
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Re.findall question

2012-06-26 Thread Alan Gauld

On 26/06/12 23:30, Alexander Quest wrote:


My question is how does Python know to return just the part in the
parentheses and not to return the blahblah and the yattayattayatta,
etc...?


If you want to know *how* Python does it you will have to read the 
module code (probably in C so download the source code)



... Does the
re.findall command by default ignore anything outside of the parentheses
and only return the parentheses as a grouping withing one tuple


The help() function returns:

--
findall(pattern, string, flags=0)
Return a list of all non-overlapping matches in the string.

If one or more groups are present in the pattern, return a
list of groups; this will be a list of tuples if the pattern
has more than one group.

Empty matches are included in the result.
--

So that's what its defined to do. How it does it is another matter.


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Looping over histogram plots

2012-06-26 Thread Elaina Ann Hyde
Dear Don,
Thanks for the comment, the set type is no problem for me, this is just
a variable that I call set... and it works great for my purposes, I do
suspect it is something in the way that matplotlib/pyplot deals with
histograms, but I have not so far been able to find the right syntax.
 Note, the first code example works great, it is only the second (with the
hist attempt) that does not do well.  It creates 29 blank plots and 1
histogram instead of 30 histograms... so probably it does need a different
phrasing, but the one you suggest gives invalid syntax.
~Elaina

On Tue, Jun 26, 2012 at 9:19 PM, Don Jennings dfjenni...@gmail.com wrote:

  Message: 1
  Date: Tue, 26 Jun 2012 18:40:50 +1000
  From: Elaina Ann Hyde elainah...@gmail.com
  To: tutor@python.org
  Subject: [Tutor] Looping over histogram plots

 snip

 set=(dat['a'+str(index)] == 1.00)

 You should not override the builtin set() type [1] as you've done here by
 assigning it.

  #write the data
 P.hist(VGSR[set],bins=30, normed=True)

 I am not familiar with matplotlib, etc. but given that the primary
 difference in your two code samples is where you write the data, I suspect
 you want something like:

ax.plot(P.hist(VGSR[set],bins=30, normed=True))

 Take care,
 Don

 [1] http://docs.python.org/library/stdtypes.html#set




-- 
PhD Candidate
Department of Physics and Astronomy
Faculty of Science
Macquarie University
North Ryde, NSW 2109, Australia
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Looping over histogram plots

2012-06-26 Thread Elaina Ann Hyde
Yay Python:

The solution was a syntax one, if anyone else ever feels like massively
multi-plotting histograms, here is the working code:

#--
fig, axes = plt.subplots(nrows=5, ncols=6, figsize=(12,6))

index=0
for b in axes:
for ax in b:
index=index+1
set=(dat['a'+str(index)] == 1.00)
#write the data
n, bins, patches = ax.hist(VGSR[set], 30, normed=1)

#label the axis
if index==13.0:
ax.set_ylabel('counts')
if index = 25.0:
ax.set_xlabel('VGSR')
plt.show()
#---

~Elaina Hyde
-- 
PhD Candidate
Department of Physics and Astronomy
Faculty of Science
Macquarie University
North Ryde, NSW 2109, Australia
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor