[Tutor] Having trouble with a component of the random module

2008-08-28 Thread Alan Gilfoy
One of my for-fun Python products is a booster-pack generator for  
trading card games (specifically, Magic the Gathering, the one game of  
the genre that I play heavily)
Somewhat like baseball cards, the cards in a pack are a  
somewhat-random selection of cards from the set in question, broken  
down by rarity levels.


There are anywhere from two to six rarity levels/groupings per set;  
this depends on the set in question.
I have a function for 2-list sets, another for 3, another for 4,  
another for 5, and another for 6.


By lists I mean the following: If a set has, say, 2 rarity levels in  
it, I make 2 lists, one per rarity level. Each list is a text file  
that has the names of each card of that rarity level in that set, one  
per line. (There are no blank lines.)


My function is set up kind of like this: Import the lists - via  
open(filename.txt).readlines()
This action creates a List, with strings as the list elements, one  
string/element for each line of the imported text file


I use random.sample to make the selection, random.sample(population, k)


for card in random.sample(Common, C):
print card


Common is the 1st list, with the names of the commons in the set as  
the items in that list. The C variable is an integer, the amount of  
commons in a booster pack of that set. Say, if there are 11 commons in  
a booster pack of that set, I feed 11 as the value for C, and the code  
snippet above proceeds to print 11 randomly-selected names from the  
Commons list


My program does the same thing for the second list (Uncommon), the  
third list (Rare), the fourth list (BasicLand), the fifth list  
(SpecialA), and the sixth list (SpecialB).


For card sets that use two, three, or four lists, the results are  
displayed just fine.


For card sets that use five or six lists, the results for the first  
four are displayed just fine, and then I get this:


The code that processes the 1st, 2nd, 3rd and 4th lists is very  
similar to the code that processes the 5th and 6th, so I don't see  
what's happening.


Traceback (most recent call last):
  File C:\Documents and Settings\Alan\My  
Documents\_Alan_Programming\trunk\BoosterPackMaker\BoosterPackMaker.py, line  
104, in module
SixList_PackChooser(C_list, UC_list, R_list, BL_list,  
SpecialA_list, SpecialB_list, C, UC, R, BL, SA, SB)
  File C:\Documents and Settings\Alan\My  
Documents\_Alan_Programming\trunk\BoosterPackMaker\PackGenerator.py,  
line 367, in SixList_PackChooser

for card in random.sample(SpecialA, SA):
  File C:\Python25\lib\random.py, line 303, in sample
raise ValueError, sample larger than population
ValueError: sample larger than population

Looking at the mentioned line in random.py, I don't see how my program  
is triggering the ValueError.


The number of items I want from a list is smaller than the population  
(number of items) in the list, so it should work.


In this specific case, I'm asking for one item from a five-item list.

Overall, more code is involved that I think can be represented in a  
few cut'n'paste snippets; how would I go about providing the files in  
question for interested parties to look at?
(There's the Python file for my front-end UI code, the Python module  
file for my processing function, and the relevant text files)


Apologizing for message length; I'm just trying to clearly explain the  
problem that I want assistance on.


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] __iter__ loops, partitioning list among children

2008-08-28 Thread Eric Abrahamsen
I finally got my iterator-based version working, only to discover that  
it's nearly four times slower than the brute-force multiple-loops  
version I started with! Then I tried just adding an incrementing index  
to the loop, so that each loop only ran through  
self.events[last_index:], but that was still twice as slow as without  
the index. I suppose it's the overhead of incrementing the variable,  
or maybe some optimization in Python's internals, but the take home  
lesson was definitely 'leave well enough alone'. Anyway, thanks again  
for the advice, it's been a learning experience...


E


On Aug 27, 2008, at 2:22 AM, Kent Johnson wrote:


On Tue, Aug 26, 2008 at 1:24 PM, Eric Abrahamsen
[EMAIL PROTECTED] wrote:

On Aug 26, 2008, at 7:20 PM, Kent Johnson wrote:



If all you want to do with the nested Month, etc is to iterate the
events in them, you could probably use a shared iterator. It would
have to be able to push-back items so that when you hit the
out-of-range item you could push it back on the iterator.


Is a 'shared iterator' something special, or do you mean all the  
instances
would draw their events from a single iterator? I'm not sure what  
this would

look like.


It's nothing special, I just mean that all instances would share an
iterator. You would pass the iterator to the iteration function.


Just for the sake of argument, here's the principle I'm working from:

#

lst = range(10)
iterlst = iter(lst)
iterlst.next()

0

for x in iterlst:

...   if x  5:
... print x
...   else:
... break
...
1
2
3
4

for x in iterlst:

...   print x
...
6
7
8
9
#

So that's why I'm creating the iterator outside of the while loop  
in the
original code, and then using a repeated for loop with a break to  
step
through all the events only once. Of course, the fact that 5 isn't  
in there

probably points to the source of my problems! The solution might be
assigning iterlist.next() to a variable, and advancing the variable.


The problem is that the first loop consumes the 5 from the iterator
but doesn't actually process it. That is why you need an iterator with
push-back - so you can put the 5 back in the iterator and get it out
again in the next loop.
http://code.activestate.com/recipes/502304/
Though working with indices directly might be simpler.

Kent


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Having trouble with a component of the random module

2008-08-28 Thread W W
On Thu, Aug 28, 2008 at 1:33 AM, Alan Gilfoy [EMAIL PROTECTED]wrote:


 The number of items I want from a list is smaller than the population
 (number of items) in the list, so it should work.

 In this specific case, I'm asking for one item from a five-item list.


Are you sure?

change this:
 for card in random.sample(SpecialA, SA):

to this:

print Special: , SpecialA, \nSA: , SA
for card in random.sample(SpecialA, SA):

then give us the output. I'll bet that your SA value is never reset to 1.


 Overall, more code is involved that I think can be represented in a few
 cut'n'paste snippets; how would I go about providing the files in question
 for interested parties to look at?


http://paste.pocoo.org I believe.



 (There's the Python file for my front-end UI code, the Python module file
 for my processing function, and the relevant text files)

 Apologizing for message length; I'm just trying to clearly explain the
 problem that I want assistance on.


You've obviously researched enough to know this:
 mylist = [1, 2, 3, 4, 5]
 from random import sample
 sample(mylist, 3)
[2, 1, 4]
 sample(mylist, 10)
Traceback (most recent call last):
  File stdin, line 1, in module
  File /usr/lib/python2.5/random.py, line 303, in sample
raise ValueError, sample larger than population
ValueError: sample larger than population


So give the print statement before your for loop a try and see what you come
up with.

HTH,
Wayne

-- 
To be considered stupid and to be told so is more painful than being called
gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness,
every vice, has found its defenders, its rhetoric, its ennoblement and
exaltation, but stupidity hasn't. - Primo Levi
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] print the hole unicode list

2008-08-28 Thread W W
Well, on my linux box (ubuntu) it had no problem with this:

for i in range(0, 65536):
uchar=unicode(\u%04X%i,unicode-escape)
print uchar

So my guess is you're missing some character in your set, but I'm not sure.

HTH
-Wayne

On Wed, Aug 27, 2008 at 8:59 PM, Yang [EMAIL PROTECTED] wrote:

 Hello,
I am trying to print out the hole unicode char list in window! form
 0-65535.
 I use the winxp in simple chinese LOCAL! the ascii form 0-127 and CJK chars
 form
 0X4E00-0X9FA4 can be print out! Other ucode chars case this error
 UnicodeEncodeError: 'gbk' codec can't encode character u'\u9fa6' in
 position 0

 my code is here:
  for i in range(0,65536 ):
uchar=unicode(\u%04X%i,unicode-escape)
print %x :%i,uchar

 how can I achive a hole unicode list? Or can it be done?

 Yang
 [EMAIL PROTECTED]
 2008-08-28




 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor




-- 
To be considered stupid and to be told so is more painful than being called
gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness,
every vice, has found its defenders, its rhetoric, its ennoblement and
exaltation, but stupidity hasn't. - Primo Levi
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] print the hole unicode list

2008-08-28 Thread Kent Johnson
On Wed, Aug 27, 2008 at 9:59 PM, Yang [EMAIL PROTECTED] wrote:
 Hello,
I am trying to print out the hole unicode char list in window! form 
 0-65535.
 I use the winxp in simple chinese LOCAL! the ascii form 0-127 and CJK chars 
 form
 0X4E00-0X9FA4 can be print out! Other ucode chars case this error
 UnicodeEncodeError: 'gbk' codec can't encode character u'\u9fa6' in position 
 0

 my code is here:
  for i in range(0,65536 ):
uchar=unicode(\u%04X%i,unicode-escape)

A simpler way to do this is
  uchar = unichr(i)

print %x :%i,uchar

 how can I achive a hole unicode list? Or can it be done?

I guess the error comes from the print statement. (Please include the
traceback with error reports.) If so, the problem is that your console
can't represent all the characters that you want to print. It looks
like your console encoding is 'gbk' and not every unicode character
can be represented in this encoding.

One way to fix this is to explicitly convert to gbk and tell Python to
substitute a ? character for the character that can't be converted. To
do this, use the str.encode() method and pass a second argument of
'replace'. Then the whole program becomes
for i in range(0, 65536):
  c = unichr(i).encode('gbk', 'replace')
  print c

Another solution might be to change your console to UTF-8 encoding, if
that is possible on your computer.

If your goal is to see the characters, the resources at Unicode.org
might be more help, for example:
http://www.unicode.org/charts/

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Calling an external program/daemon that doesn't exit/return

2008-08-28 Thread Michael Bernhard Arp Sørensen
Hi again.

Aparantly it was as simple as:

os.system(nohup /code/daemon.py )

I know it's more correct to write a real deamon startet from a runlevel, but
that will have to wait until later. :-)

Med venlig hilsen/Kind regards

Michael B. Arp Sørensen
Programmør / BOFH

Ride out and meet them.


On Thu, Aug 28, 2008 at 1:06 PM, Michael Bernhard Arp Sørensen 
[EMAIL PROTECTED] wrote:

 Hi there.

 I'm trying to find some info about starting a program from python. I just
 want to start it and move on. The program will not end until reboot.

 Any clues?

 Med venlig hilsen/Kind regards

 Michael B. Arp Sørensen
 Programmør / BOFH

 Ride out and meet them.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] __iter__ loops, partitioning list among children

2008-08-28 Thread Lie Ryan

 
  Just for the sake of argument, here's the principle I'm working
 from:
 
  #
  lst = range(10)
  iterlst = iter(lst)
  iterlst.next()
  0
  for x in iterlst:
  ...   if x  5:
  ... print x
  ...   else:
  ... break
  ...
  1
  2
  3
  4
  for x in iterlst:
  ...   print x
  ...
  6
  7
  8
  9
  #

If that contrived case is the case, you could change the code a bit to
make 5 appears:

for x in iterlst:
print x
if x = 5: break
for x in iterlst:
print x

 
  So that's why I'm creating the iterator outside of the while loop in
 the
  original code, and then using a repeated for loop with a break to
 step
  through all the events only once. Of course, the fact that 5 isn't
 in there
  probably points to the source of my problems! The solution might be
  assigning iterlist.next() to a variable, and advancing the variable.
 
 The problem is that the first loop consumes the 5 from the iterator
 but doesn't actually process it. That is why you need an iterator with
 push-back - so you can put the 5 back in the iterator and get it out
 again in the next loop.
 http://code.activestate.com/recipes/502304/
 Though working with indices directly might be simpler.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how do I create a lists of values associated with a key?

2008-08-28 Thread Lie Ryan
Message: 8
Date: Thu, 31 Jul 2008 20:16:54 -0700 (PDT)
From: Angela Yang [EMAIL PROTECTED]
Subject: [Tutor] how do I create a lists of values associated with a
   key?
To: tutor@python.org
Message-ID: [EMAIL PROTECTED]
Content-Type: text/plain; charset=iso-8859-1


 That did not work because list index is not numeric.

Dictionary is an unordered set (list is an ordered set) indexed by a
immutable value (e.g. string, numbers, tuple)

 But for dictionaries, it is key - value pairs.? But I need key - multiple
values.

That would mean, a dictionary of lists. Dictionary keys cannot be a mutable
(i.e. list cannot be a dictionary key) but dictionary's value may be
mutable.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Calling an external program/daemon that doesn't exit/return

2008-08-28 Thread Michael Bernhard Arp Sørensen
Hi there.

I'm trying to find some info about starting a program from python. I just
want to start it and move on. The program will not end until reboot.

Any clues?

Med venlig hilsen/Kind regards

Michael B. Arp Sørensen
Programmør / BOFH

Ride out and meet them.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] print the hole unicode list

2008-08-28 Thread Mark Tolonen


Yang [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]

Hello,
   I am trying to print out the hole unicode char list in window! form 
0-65535.
I use the winxp in simple chinese LOCAL! the ascii form 0-127 and CJK 
chars form

0X4E00-0X9FA4 can be print out! Other ucode chars case this error
UnicodeEncodeError: 'gbk' codec can't encode character u'\u9fa6' in 
position 0


my code is here:
for i in range(0,65536 ):
   uchar=unicode(\u%04X%i,unicode-escape)
   print %x :%i,uchar

how can I achive a hole unicode list? Or can it be done?


Your console encoding is 'gbk', which can't display all the Unicode 
characters.  The following code can be used to generate all the characters 
into a file using an encoding that supports all Unicode characters, and then 
that file can be viewed in a program that supports the encoding (like 
Notepad for this example).  Still, what characters you see will depend on 
the font used.  Fonts generally do not support display of every Unicode 
character.


import codecs
f=codecs.open('unicode.txt','wt',encoding='utf-8')
for i in xrange(32,0x1):   # skip control chars
   if i  0xD800 or i  0xDFFF:  # skip surrogate pair chars
   f.write(u'%04X: %s\t' % (i,unichr(i)))
f.close()


-Mark 



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Having trouble with a component of the random module

2008-08-28 Thread Alan Gilfoy

Quoting W W [EMAIL PROTECTED]:



The number of items I want from a list is smaller than the population
(number of items) in the list, so it should work.

In this specific case, I'm asking for one item from a five-item list.


Are you sure?

change this:
 for card in random.sample(SpecialA, SA):

to this:

print Special: , SpecialA, \nSA: , SA
for card in random.sample(SpecialA, SA):

then give us the output. I'll bet that your SA value is never reset to 1.

There is a part of my front-end code that declares the SA variable as  
equal to 1 for the card set in question, and feeds that into the  
processing function.

Changing the block of code, as you suggest, still gets me a traceback:

Special:  []
SA:  1

Traceback (most recent call last):
  File C:\Documents and Settings\Alan\My  
Documents\_Alan_Programming\trunk\BoosterPackMaker\BoosterPackMaker.py, line  
104, in module
SixList_PackChooser(C_list, UC_list, R_list, BL_list,  
SpecialA_list, SpecialB_list, C, UC, R, BL, SA, SB)
  File C:\Documents and Settings\Alan\My  
Documents\_Alan_Programming\trunk\BoosterPackMaker\PackGenerator.py,  
line 367, in SixList_PackChooser

for card in random.sample(SpecialA, SA):
  File C:\Python25\lib\random.py, line 303, in sample
raise ValueError, sample larger than population
ValueError: sample larger than population

So, I'm asking my program to pick 1 item from a 0-item list. How there  
are zero items in the list, I don't know. That must be the problem.




You've obviously researched enough to know this:

mylist = [1, 2, 3, 4, 5]
from random import sample
sample(mylist, 3)

[2, 1, 4]

sample(mylist, 10)

Traceback (most recent call last):
  File stdin, line 1, in module
  File /usr/lib/python2.5/random.py, line 303, in sample
raise ValueError, sample larger than population
ValueError: sample larger than population

Okay, asking for 3 items from a 5-item list works, and asking for 10  
items from a 5-item list doesn't work. It should be trying to pick 1  
item from the 5-item SpecialA list, but somehow the SpecialA list has  
zero items in it.


So give the print statement before your for loop a try and see what you come
up with.


Okay, I cut'n'pasted it before the traceback message.


HTH,
Wayne


Thank you.
---
SixList_PackChooser(C_list, UC_list, R_list, BL_list, SpecialA_list,  
SpecialB_list, C, UC, R, BL, SA, SB)


^ The error occurs in this function.
The program picks C number of items from C_list and displays them just fine.
The program picks UC number of items from UC_list and displays them just fine.
The program picks R number of items from R_list and displays them just fine.
The program picks BL number of items from BL_list and displays them just fine.

Looking at my user-interface/front-end, it looks like the function's  
being fed proper values.


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] It works! (my booster pack generator)

2008-08-28 Thread Alan Gilfoy
The booster pack generator (my use of random.sample as described in a  
previous message) is working as intended now.


There was a rather stupid typo in my code that was causing it to go wonky.

Y'all still helped, though. A few people replied, but a bit of advice  
from: W W [EMAIL PROTECTED] pointed me in the right direction.


for card in random.sample(SpecialA, SA):
 print card

he suggested changing to:

print Special: , SpecialA, \nSA: , SA
for card in random.sample(SpecialA, SA):
 print card

Basically, what my program was doing:

1. Take a textfile, import it/convert it to a list via  
open(filename.txt).readlines() This list was defined as the variable  
SpecialA_list.

2. Feed SpecialA_list, along with some other variables, into my function
3. SpecialA variable is set equal to a list consisting of items from  
SpecialA_list

4. Then sample from SpecialA  print the results.

Thanks to the print statement that WW suggested, I figured out that  
the SpecialA variable was coming out as an empty list. [error in  
Step 3 of the above process list, so Step 4 had nothing to sample  
*from*, thus triggering the traceback].
I found the typo that was causing this, and fixed it. So, the SpecialA  
variable was set equal to the expected list, so random.sample could  
sample properly.


Thank you. :)


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] It works! (my booster pack generator)

2008-08-28 Thread Kent Johnson
On Thu, Aug 28, 2008 at 11:49 AM, Alan Gilfoy [EMAIL PROTECTED] wrote:
 The booster pack generator (my use of random.sample as described in a
 previous message) is working as intended now.

 There was a rather stupid typo in my code that was causing it to go wonky.

 Y'all still helped, though. A few people replied, but a bit of advice from:
 W W [EMAIL PROTECTED] pointed me in the right direction.

As well as the lesson about using print statements to debug, I hope
you have learned to believe the interpreters error messages. It
usually knows what it is talking about :-)

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] It works! (my booster pack generator)

2008-08-28 Thread Alan Gilfoy
Yeah, I did a lot of python work for my high school's Senior Project  
(so, spring 2007); hadn't coded much since then, was rusty on the  
pritn stattements for debugging part.


And I was trying to look at the code mentioned in the traceback, but  
due to my own mistake, I hadn't detected the problems with the lines  
that the traceback was pointing to.


Quoting Kent Johnson [EMAIL PROTECTED]:

On Thu, Aug 28, 2008 at 11:49 AM, Alan Gilfoy   
[EMAIL PROTECTED] wrote:

The booster pack generator (my use of random.sample as described in a
previous message) is working as intended now.

There was a rather stupid typo in my code that was causing it to go wonky.

Y'all still helped, though. A few people replied, but a bit of advice from:
W W [EMAIL PROTECTED] pointed me in the right direction.


As well as the lesson about using print statements to debug, I hope
you have learned to believe the interpreters error messages. It
usually knows what it is talking about :-)

Kent





___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor