Re: [Tutor] renumbering a sequence

2008-08-27 Thread Alan Gauld


"Christopher Spears" <[EMAIL PROTECTED]> wrote 


ordered_list = sorted(unordered_list)

test_frame_1 = ordered_list[0].split('.')[1]
test_frame_2 = ordered_list[1].split('.')[1]

if test_frame_1 == "":
   if test_frame_2 =! "0001":
   print "Sequence needs to be renumbered"
for frame in ordered_list:
new_frame = renumber_frame(frame)
print new_frame
elif test_frame_1 != "0001"
   print "Sequence needs to be renumbered"
   for frame in ordered_list:
new_frame = renumber_frame(frame)
print new_frame
else:
   print "Sequence is fine"


You can change the test sequence to avoid the 
code duplication


if tf1 == '' and tf2 == '0001':
   print 'sequence fine'
else
   print 'renumber sequence'
   for frame in etc

As you can see, the only part I haven't figured out is the 
actual renumbering.  Can't figure out how to do the following:

0017 convert to -> 0001


Instead of splitting and reconstructing you could use a 
regex to locate the number section and usere.sub() to replace 
the old number with the new sequence number


Not sure if that would be any faster but it might be more 
concise.



--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld

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


Re: [Tutor] renumbering a sequence

2008-08-27 Thread Paul McGuire
I am thinking along similar lines as Simone, but I suspect there is still a
missing step.  I think you need to know what the mapping is from old name to
new name, so that you can do something like rename or copy the files that
these names represent.

Python's built-in zip() method does the trick here (zip comes in handy in so
many ways, it is a good tool to add to your at-hand Python skill set).
Don't think of "zip" as in the common file compression scheme.  In Python,
zip() acts like a zipper, matching together the entries from 2 or more lists
the way a zipper matches the teeth from its two sides.  For instance, here
is a simple example of zip() from the command line:

>>> print zip("ABC","123")
[('A', '1'), ('B', '2'), ('C', '3')]

You can imagine how easy it would be to make a dict with zip:

>>> print dict(zip("ABC","123"))
{'A': '1', 'C': '3', 'B': '2'}


For your problem, here is how to zip your old and new file lists together,
and a hypothetical use of such a mapping (to rename the original files):

import os
original_list = sorted(unordered_list)

# use a list comprehension to construct list of new file names in order
new_list = [ "frame.%04d.dpx" % i for i in xrange(len(original_list)) ]

# map old names to new names - zip creates a list of tuples
# using an item from each list in order
file_mapping = zip(original_list, new_list)

# rename files to consecutive order
for oldname,newname in file_mapping:
if oldname != newname:
os.rename(oldname,newname)

-- Paul

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


Re: [Tutor] renumbering a sequence

2008-08-27 Thread Paul McGuire
One problem with my previous post.  I didn't look closely enough at the
original list of files, it turns out you have multiple entries for some of
them.  If you used the glob module to create this list (with something like
"original_list = glob.glob('frame.*.dpx')"), then there should be no problem
with duplicates.  But if the list was hand generated, you should probably
check for duplicates first.

The easiest way to do this is to use a Python set:

if len(set(original_names)) < len(original_names):
print "Watch out! There are duplicate file names in the list!"

Of course, having duplicates in the original list makes no matter in the
generation of a new list of consecutively-numbered file names.  The problems
will arise when you go to actually do something with the generated list,
such as the renaming example I posted earlier.

-- Paul


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


[Tutor] Working with IDLE in XP, Set Up for A New Program

2008-08-27 Thread Wayne Watson
Title: Signature.html




I use IDLE on XP, and would like to know if there's an easier way to
start it than what I use? I pick on a py file, and then use the pull
down menu, right-click, to select IDLE. That means I start off with
some program I may want to erase to get started on a new program. It's
easy to clear the text with a Ctrl-C and then delete. However, that
leaves the file as abc.py, where abc.py is the original program. Now I
need to be careful with my Save. I'd like to start IDLE on new programs
in such a way that when I perform a Save the program goes into a folder
of my py files, and not somewhere else. 
-- 


   Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

 (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
  Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet

“We are in a race between education and catastrophe.”
 – H.G. Wells

Web Page: 



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


Re: [Tutor] Working with IDLE in XP, Set Up for A New Program

2008-08-27 Thread Samir Parikh
On Wed, Aug 27, 2008 at 11:36 AM, Wayne Watson
<[EMAIL PROTECTED]> wrote:
> I use IDLE on XP, and would like to know if there's an easier way to start
> it than what I use? I pick on a py file, and then use the pull down menu,
> right-click, to select IDLE. That means I start off with some program I may
> want to erase to get started on a new program. It's easy to clear the text
> with a Ctrl-C and then delete. However, that leaves the file as abc.py,
> where abc.py is the original program. Now I need to be careful with my Save.
> I'd like to start IDLE on new programs in such a way that when I perform a
> Save the program goes into a folder of my py files, and not somewhere else.
> --

Hi Wayne,

Do you not see IDLE under Programs when you click the  button?
(I'm currently on my Linux machine so I don't recall the exact path,
but that's how I access it when I'm on my laptop running XP.).  Once
you launch IDLE, then you can click File > New File and save your new
Python program to any directory.

Thanks.

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


[Tutor] using sys.excepthook to handled unhandled exceptions

2008-08-27 Thread Tony Cappellini
The original authors left asserts in many places and I don't want the people
using the code to see the ugly tracebacks.
 want to add an unhandled exception handler to a large framework that I'm
maintaining, to make the applications behave better,

Users of a program shouldn't need to know anything about the language in
order to have an idea of what caused the error.

With that in mind, I thought I would add a handler which logs the tracebacks
to a file, display a 1-line message about the error on the screen, so the
user has some
idea of what happened, along with a simple message like this

"An unhandled exception has occured. This program will terminate. Please
email the "ErrorReport.txt" file to the developers"

I would like to hear from "people who have already assigned their own
function to sys.excepthook" as to how they approached this problem, and what
issues they encountered, if any.


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


[Tutor] using sys.excepthook to handle unhandled exception (corrected and reposted)

2008-08-27 Thread Tony Cappellini
I'm maintaining a large framework of python code where the original
authors left many assert statements.

When an unhandled exception occurs, the traceback is displayed on the
screen. Ideally, I don't want the users to see the tracebacks
from unhandled exceptions, but rather a short, useful message.

Users of a program shouldn't need to know anything about the language
the program was written in order to have an idea of what caused the
error.

I want to add an "unhandled exception handler" which logs the
tracebacks to a file, display a 1 line message as to where or why the
exception occurred.
Assigning my exception logger function to sys.excepthook is what I have in mind.

This exception logger will also display a message like this.
"An unhandled exception has occurred. This program will terminate.
Please email the "ErrorReport.txt" file to the developers"

Adding a function to log the tracebacks is easy, I'm more interested
in the side affects caused by assigning a function to sys.excepthook.
The framework I'm maintaining does NOT already use sys.excepthook.
That is a good beginning. ;-)

I would like to hear from the experiences of people "who have assigned
an exception handler function to sys.excepthook" as to how they
approached
this problem. More specifically, what issues they encountered, if any.



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


Re: [Tutor] using sys.excepthook to handle unhandled exception (corrected and reposted)

2008-08-27 Thread Kent Johnson
On Wed, Aug 27, 2008 at 3:18 PM, Tony Cappellini <[EMAIL PROTECTED]> wrote:

> I would like to hear from the experiences of people "who have assigned
> an exception handler function to sys.excepthook" as to how they
> approached
> this problem. More specifically, what issues they encountered, if any.

Have you searched comp.lang.python? There seems to be quite a bit of
discussion there.

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


Re: [Tutor] Working with IDLE in XP, Set Up for A New Program

2008-08-27 Thread Alan Gauld


"Wayne Watson" <[EMAIL PROTECTED]> wrote 

would like to know if there's an easier way to start it than 
what I use? I pick on a py file, and then use the pull down menu, 
right-click, to select IDLE. 


Yes, it should be on the Start menu possibly listed as Python GUI.

Or you can set up a shortcut to it by selecting idle.pyw in explorer 
and creating a shortcut and dragging it to your desktop.


Or you could do yourself a favour and get the Pythonwin 
extensions and use Pythonwin instead. IDLE is fine for a 
cross platform IDE but it is ugly and less feature rich than 
Pythonwin. And the pythonwin installer sets up the menus 
etc for you.


HTH

--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld

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


Re: [Tutor] using sys.excepthook to handled unhandled exceptions

2008-08-27 Thread Alan Gauld

"Tony Cappellini" <[EMAIL PROTECTED]> wrote

With that in mind, I thought I would add a handler which logs the 
tracebacks
to a file, display a 1-line message about the error on the screen, 
so the

user has some
idea of what happened, along with a simple message like this


I tend to do that after I get everything working by just wrapping
main() in a try/except clause that catches everything then mapping
any errors to friendly strings and logging the traceback if logging
is enabled.

In other words I don't mess with the excepthook value.

HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 



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


[Tutor] print the hole unicode list

2008-08-27 Thread Yang
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


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

2008-08-27 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 
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