Re: [Tutor] Decompile pyc file

2005-10-21 Thread Johan Geldenhuys




When I want to download the zip file from the source, I get the error
that the page does not exist. Where can I get it?

Thanx.

Bernard Lebel wrote:

  Sorry for the late reply.

Thanks a lot Kent, that was really helpful.


Bernard



On 10/14/05, Kent Johnson [EMAIL PROTECTED] wrote:
  
  
Bernard Lebel wrote:


  Hello,

Is there a way to "decompile" a pyc file? I have a big problem where
the py file was lost, but the pyc file is instact. I would need to
extract the source code from the pyc file.
  

Google 'python decompiler'

try http://www.freshports.org/devel/decompyle/
or the commercial service at http://www.crazy-compilers.com/decompyle/

Kent

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


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

  



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


Re: [Tutor] pytunes (Was __slots__, struct, etc.)

2005-10-21 Thread Liam Clarke
Hmmm, that's interesting.

Obviously, for a playlist with x songs, and n songs by a particular
artist, the optimum separation between songs would be x/n.

Which would be easy for one artist, but for all of them?

Hmm...

Let's see. First off, let's discount all the artists with only one
song, they can be the space filler.

Next, you'd have to work out optimal spacing for each artist's songs,
and try to insert them into a list. If that slot was already taken,
you'd want to look down and up the list for the next free space,
choosing whichever maximised the distance. And you'd have to treat
x[5] - 10 as x[96] for a list x where len(x) == 100.

Err, there's got to be an algorithm for this sorta stuff, but I can't
understand the big words when I google it... what's your solution?

On 10/21/05, John Fouhy [EMAIL PROTECTED] wrote:
 Hmm, neat. I don't have any feedback for you on your code, but since
 you're working with this sort of thing, I have a puzzle for you:

 Suppose you have a collection of MP3 files.  You want to build a
 random playlist, subject to the condition that tracks by the same
 artist are as far apart as possible.  You can assume you have the
 track / artist data in any structure you like.  How would you do this?

 (I have a solution, but I don't think it is optimal.  In fact, I'm not
 sure how to actually define optimal here... And so I am interested
 in how other people would solve this problem :-) )

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

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


Re: [Tutor] Dynamic Function Assignment

2005-10-21 Thread Alan Gauld
for id in ('A', 'B', 'C'):
   if segment.upper().startswith(id):

Think dictionary:

objects = {'A': a, 'B':b, 'C':c}

for id in ('A','B','C')
objects[id].method()

I have a section within my OOP topic that specifically talks about this 
in the context of a collection of bank account objects. You might find 
it worth skimming through.

HTH,

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld


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


Re: [Tutor] Time - sum/difference

2005-10-21 Thread Jonas Melian
I wanted to sum two time values:

-02:30
+01:00
--
-01:30

I found one solution:
  time_local = dt.time(2,30)
  time_str = str(time_local).split(':')

Now I'll can get the first value, convert to integer and sum it.

Kent Johnson wrote:

Jonas Melian wrote:
  

I would get the local time of a country, using UTC (Universal Time 
Coordinated) and DST (Daylight SavingTime) of that country.

An example, utc time -02:30 and dst +1 :

country_utc = datetime.time(2,30)
isUTCNegative = True
dst = datetime.time(1,0)

Now I would the difference of both times.
-02:30 + 01:00 - -01:30

Is possible get sum/difference of time values? How?



I'm not exactly sure what you are looking for, but you can subtract 
datetime.datetime instances. If you are trying to find the difference between 
local time and utc this is one way:

  import datetime as dt
  dt.datetime.now()
datetime.datetime(2005, 10, 20, 19, 41, 30, 393000)
  dt.datetime.utcnow()
datetime.datetime(2005, 10, 20, 23, 41, 52, 195000)
  dt.datetime.utcnow()-dt.datetime.now()
datetime.timedelta(0, 14400)

though there is a race condition here that might give you an error of a 
millisecond sometimes.

Kent

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

  


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


Re: [Tutor] How do I recursively remove the contents of a directory??

2005-10-21 Thread Alan Gauld
If I have a directory /u01/qa/logs and the logs has a number of children
 directories and I want to remove everything in logs and logs itself, is

os.system('rm -rf  %s ' % logpath)

Sometimes its easier to use the OS directly!

If you were selectively deleting files it would be different but for 
the sledgehammer case I'd pobably just go with rm...

Alan g


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


[Tutor] Fwd: Saving command line keyed input?

2005-10-21 Thread Ed Singleton
A quick tip for the Windows command line.

Ctrl-C, Ctrl-A etc don't work, neither does right-clicking
(menu-click).  However, right-clicking does work on the blue title
bar.

Right-click on the blue title bar and then go down to Edit and then
you can Select All.  Then right-click on the blue title bar again and
go down to Edit and then Copy.

If at any point you right click in the black part of the window or do
a Ctrl key sequence, it deselects all your text.  Very unintuitive, as
it is completely different to every other Windows app.  (I think they
do it on purpose to stop people wanting to use the Command Line).

Ed

On 19/10/05, CPIM Ronin [EMAIL PROTECTED] wrote:
 I know that one should use IDLE or a choosen editor for any substantial
 Python coding! However, if one  happens to have written some interesting
 doodlings on the regular command line interface (under Windows XP in my
 case), is there an easy way to save ALL input to date into a selected file?

 For example:
 class work_center:
  def __init__(self,x_name):
 self.name = x_name

 x = work_center(machine press)
 

   ---  What do I do to save the above work into a file named mywork.py

 Thanks.

 RC

 _
 On the road to retirement? Check out MSN Life Events for advice on how to
 get there! http://lifeevents.msn.com/category.aspx?cid=Retirement

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

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


Re: [Tutor] Help(!) with OOP/Composition from Learning Python

2005-10-21 Thread Kent Johnson
Andrew P wrote:
 I've been reading about composition vs inheritance, and went back to
 Learning Python to look at something I found very confusing a year
 ago.  Turns out I still find it very confusing :)
 
 The code at the bottom was taken from the OOP chapter, it's a solution
 to one of the end-of-chapter problems.
 
 Honestly, this toy program is enough to make my head spin.  Am I too
 stupid for OOP?  Here is me trying to decipher it:

You did a pretty good job!
 
 Which is just entirely too much for my brain to hold at once.  I'm
 sorry if this sounds like whining, but this entire program seems as
 bad as trying to decipher the GOTO-crazy scribblings of a lunatic. 
 There is no linearity, and the extra abstraction of the classes just
 adds another layer of complexity.  Instead of functions and arguments
 there are methods, classes, and instances being passed about
 willy-nilly and no (for me) easy way to follow the path or make sense
 of it.

I agree with the other posters that this is not a very good example of OOP. It 
shows some of the nuts and bolts of OOP - objects containing objects, invoking 
methods on objects and dividing responsibility between different classes - but 
it doesn't use them well and the motivation is lacking.

Fundamentally classes are a way to package state (variables) and behaviour 
(functions) into a single abstraction. Done well, the resulting class has a 
coherence and utility of its own.

For simple examples just look at Python's built in string, list and dict 
classes. In each case, the class hides considerable complexity and provides a 
useful abstraction. You can use a list without worrying about the details of 
how the list items are stored, how the storage is allocated, what happens when 
new storage is needed, etc., etc. You have at your disposal a sort() algorithm 
that has been tuned for high performance by top-notch programmers over many 
years, and much more.

You can also think of classes very pragmatically, as another tool available to 
organize your code, just like modules and functions. Classes add some useful 
capabilities to your toolkit. This essay gives some simple motivating examples 
of why a beginner might want to use classes:
http://personalpages.tds.net/~kent37/blog/stories/15.html

 
 Is this just an example of a technique with a constant complexity
 factor that looks ridiculous on a toy program, but very quickly
 becomes useful on larger ones?  Are there tools that make reading
 something like this more clear?

There is a very real cost to OOP that responsibility for some action can be 
distributed among multiple cooperating classes, so to trace through an 
operation you may have to follow a chain from one class to the next. When the 
design is well done, the benefit of useful abstractions outweighs this cost. 
Done poorly, you can write object-oriented spaghetti code. The key is to have a 
clear idea of the responsibility of each class. 

Stepping through the code in a debugger can be useful to understanding the flow.

Kent

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


Re: [Tutor] How do I recursively remove the contents of a directory??

2005-10-21 Thread Kent Johnson
Suri Chitti wrote:
 If I have a directory /u01/qa/logs and the logs has a number of children 
 directories and I want to remove everything in logs and logs itself, is 
 there a single method or command to do that?  I want to avoid 
 recursively removing the files in each child directory and so on.

shutil.rmtree() (which actually does recursively remove all the children)

Kent

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


Re: [Tutor] Fwd: Saving command line keyed input?

2005-10-21 Thread Kent Johnson
Ed Singleton wrote:
 A quick tip for the Windows command line.
 
 Ctrl-C, Ctrl-A etc don't work, neither does right-clicking
 (menu-click).  However, right-clicking does work on the blue title
 bar.
 
 Right-click on the blue title bar and then go down to Edit and then
 you can Select All.  Then right-click on the blue title bar again and
 go down to Edit and then Copy.

If you turn on Quick Edit (from Properties in the right-click menu) then you 
can select text and right-click to copy, then right-click again to paste.

Kent

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


Re: [Tutor] greetings...

2005-10-21 Thread Gabriel Farrell
Welcome to the list, Carl.  Feel free to browse the list archive at
http://mail.python.org/pipermail/tutor/ or
http://aspn.activestate.com/ASPN/Mail/Browse/Threaded/python-Tutor to
get some idea of the kinds of questions asked and answers given.
You'll find a lot of suggestions for beginners among the threads there
as well.

There's also http://wiki.python.org/moin/BeginnersGuide, which is
always a good place to start.

gsf


On Thu, Oct 20, 2005 at 05:09:07PM -0400, [EMAIL PROTECTED] wrote:
 Greetings to list,
 Tomorrow is my last day in class with Lutz. I am not only new to Python but
 new to programming in general, this is my first language. Looking forward to
 your help in the days and weeks to come.
 Any suggestions for self tutorial, whether on python.org
 http://python.orgor not, would be greatly appreciated. I am going to
 finish his Learning...
 book first then hack open some open code for a simple project in order to
 see how it works.
 peace
 Carl

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

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


[Tutor] perldoc - confused!

2005-10-21 Thread Dan Klose
Hello Python List,

When using perl I tend to use perldoc.

I spent ages today trying to list all items in a directory, simple I now
know (os.listdir) but I was trying to use os.walk simply because I had
no idea that listdir existed.  I only found listdir because I was
reading about walk

Is there a python version of perldoc?

Thanks for any guidance

Dan.

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


Re: [Tutor] Fwd: Saving command line keyed input?

2005-10-21 Thread w chun
On 10/21/05, Kent Johnson [EMAIL PROTECTED] wrote:
 Ed Singleton wrote:
  A quick tip for the Windows command line.
 
  Ctrl-C, Ctrl-A etc don't work, neither does right-clicking
  (menu-click).  However, right-clicking does work on the blue title
  bar.

 If you turn on Quick Edit (from Properties in the right-click menu) then you 
 can select text and right-click to copy, then right-click again to paste.


so yes, ed and kent are right.  you have to do those things if you're
running Python from the DOS window.   Ctrl-A, Ctrl-C, Ctrl-V work from
the IDLE window and most non-DOS win32 windows..

-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Core Python Programming, Prentice Hall, (c)2006,2001
http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] self knowledge

2005-10-21 Thread Ethan Ligon

I've devised a simple class; one of the class attributes is an identifier.

Thus,

class Foo:
  def __init__(self,name):
self.name=name

When using the class in practice, I've found it natural to
create instances of Foo so that the instance itself is called
name.  Thus, I find myself doing things like:

 a=Foo('a')
 b=Foo('b')

I want the name attribute to depend only on the name of the
original variable; thus

 c=a
 c.name
'a'

is the desired behavior. 

But this is plainly silly--I'm supplying information on identity
twice when I instantiate each instance---and gives rise to 
two questions. 

First, my immediate inclination is to try and think of 
a way for the object to know its own name (at the time
its instantiated) without being told, perhaps via the 
creation of a name method for Foo.  But the only ways 
I can think of learning this name are all incredibly 
awkward and kludgy.  What's the best way to do this?

Second, because this seems awkward, I strongly suspect 
that there's a pretty fundamental problem with the way
I'm approaching the problem.  When the name of a variable
is itself information which might be useful, what's the
right way convey this information?  

Thanks,
-Ethan

-- 
Ethan Ligon, Assoc. Professor   [EMAIL PROTECTED]
Dept. of Agricultural  Resource Economics
University of Californiahttp://are.berkeley.edu/~ligon
Berkeley, CA 94720-3310  (510)643-5411
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] self knowledge

2005-10-21 Thread Kent Johnson
Ethan Ligon wrote:
 I've devised a simple class; one of the class attributes is an identifier.
 
 Thus,
 
 class Foo:
   def __init__(self,name):
 self.name=name
 
 When using the class in practice, I've found it natural to
 create instances of Foo so that the instance itself is called
 name.  Thus, I find myself doing things like:
 
 
a=Foo('a')
b=Foo('b')

Why does this matter? Why do you need the name both places?

 First, my immediate inclination is to try and think of 
 a way for the object to know its own name (at the time
 its instantiated) without being told, perhaps via the 
 creation of a name method for Foo.  But the only ways 
 I can think of learning this name are all incredibly 
 awkward and kludgy.  What's the best way to do this?

The only ways to do this are awkward and kludgy - you have to inspect the stack 
and find out the name of the variable in the caller. Don't do it.

 
 Second, because this seems awkward, I strongly suspect 
 that there's a pretty fundamental problem with the way
 I'm approaching the problem.  When the name of a variable
 is itself information which might be useful, what's the
 right way convey this information?  

I think this is the third time this question has come up this week! Usually the 
answer is to put your data in a dictionary or list. Here is another version:
http://mail.python.org/pipermail/tutor/2005-October/042371.html


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


Re: [Tutor] : Threads?

2005-10-21 Thread Kent Johnson
Orri Ganel wrote:
 Hello all,
 
 I've been working on a program for a week or two now that will convert 
 all the wav files in a folder to mp3s, filling the id3 tags with the 
 correct information as collected from gracenote.com.  This part works 
 fine.  However, the actual conversion to mp3 takes between 30 and 50 
 seconds per song, so it's usually about 10 minutes per album.  With this 
 in mind, I thought, why not try to use threads so all the conversions 
 happen simultaneously?  That way, the whole album will take between 30 
 and 50 seconds.  Unfortunately, I can't seem to get a working threaded 
 version that significantly reduces the time involved . . . The 

The only part you are doing in a thread is the actual conversion. This is 
likely to be CPU-intensive so running it in multiple threads may not help - you 
still have only the one CPU to run on. To the extent that you can overlap disk 
I/O in one conversion with processing in another you may get a win; on the 
other hand you could just as well have contention for the disk as you try to 
read and write a bunch of files at the same time.

The fetch from gracenote.com seems like a better candidate for threading 
because there is some latency...but the total time is still probably small 
compared to the conversion time.

Maybe if you have multiple CPUs you can get a speedup by using as many threads 
as CPUs...I'm not sure how os.system() behaves in this case. You may have to 
explicitly fork to get a new process.

Hmm...come to think of it, os.system() may block other threads, I don't 
know...you could try subprocess.Popen() instead.

Kent

 unthreaded version is available at 
 http://rafb.net/paste/results/Y1DTnW54.html, and the current, only 
 slightly time-improved threaded version is available at 
 http://rafb.net/paste/results/Lvsjj495.html. Any and all suggestions as 
 to how to threadify this are welcome.  I realize the code isn't as 
 pretty as it could be, but first I want to make a threaded version that 
 works.  Once this has been accomplished, I'll finish commenting the code 
 and making it more user-friendly to read.
 
 Thanks in advance,
 Orri
 
 P.S. - In order to make my code work, it is necessary to download 
 several modules. Once the code works, I'll include above the code a 
 comment listing all the requirements. Right now, I believe the following 
 modules are needed:
 
 ClientForm - http://wwwsearch.sourceforge.net/ClientForm/
 id3writer - 
 http://www.comfortableshoe.co.uk/cgi-bin/blosxom.cgi/Home/Python/id3Writer.comments
  
 (click the download link link)
 id3reader - 
 http://www.comfortableshoe.co.uk/cgi-bin/blosxom.cgi/Home/Python/id3Writer.comments
  
 (click the Ned Batchelder's id3Reader link)
 PyID3 - http://sourceforge.net/projects/pyid3
 threadpool - http://chrisarndt.de/en/software/python/threadpool.html
 
 I also considered using the following, but decided not to for reasons of 
 difficulty of use or lack of desired operations:
 
 PyID3Tag - http://superduper.net/?page=pyid3tag
 ID3-Py - http://id3-py.sourceforge.net/
 
 The following non-Python program is also needed:
 
 Lame - http://lame.sourceforge.net/download/download.html
 

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


Re: [Tutor] Help(!) with OOP/Composition from Learning Python

2005-10-21 Thread Andrew P
On 10/21/05, Kent Johnson [EMAIL PROTECTED] wrote:

For simple examples just look at Python's built in string, list and dict
classes.

-Using- OOP isn't the problem :)  It's impossible to ignore it's usefulness when
programming in Python.  But getting from there to thinking non-procedurally is,
as has been pointed out, a tiny bit difficult.

My frustration/abject fear was with this particular example being very hard to
decipher, and not seeing any advantages to it being laid out that way.  I
assumed there were some, where this kind of thing had benefits.

You can also think of classes very pragmatically, as another tool available to
organize your code, just like modules and functions. Classes add some useful
capabilities to your toolkit. This essay gives some simple motivating examples
of why a beginner might want to use classes:
http://personalpages.tds.net/~kent37/blog/stories/15.html


I realize after all these reposnses that I should have mentioned that  I do use
classes to do exactly that.  Bundle logic and data, but then call the classes
I've made procedurally.  It's handy, having methods attached to objects, and to
be able to use any object that supports the same interface interchangeably.

But I have the nagging feeling I still have it all upside down and inside out.
Writing a bunch of objects that are supposed to interact in dynamic ways at
runtime with nobody leading the band, and all.  Maybe if I'm not writing a
a library or framework I shouldn't worry so much.


There is a very real cost to OOP that responsibility for some action can be
distributed among multiple cooperating classes, so to trace through an
operation you may have to follow a chain from one class to the next. When the
design is well done, the benefit of useful abstractions outweighs this cost.
Done poorly, you can write object-oriented spaghetti code. The key is to have a
clear idea of the responsibility of each class.


For all I know people are using IDEs that let them write unreadable code that
simultaneously allows loosest coupling between classes, and most reuse of code
at the expense of readability.  Giant jellyfish-like automatons that are easy
to maintain with appropriate tools.  I'm still not sure that impression is
wrong :)

I do my best learning by reading good examples.  I imagine most people do.
There is just no substitute for understanding what somebody else has done well,
tip to tail.  I am just mistrustful of something so hard to puzzle out.

Stepping through the code in a debugger can be useful to
understanding the flow.

I've never actually used a Python debugger. Or any debugger.  That was such a
commonsense answer I'm embarassed I didn't try it first.

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


Re: [Tutor] Help(!) with OOP/Composition from Learning Python

2005-10-21 Thread Kent Johnson
Andrew P wrote:
 On 10/21/05, Kent Johnson [EMAIL PROTECTED] wrote:
You can also think of classes very pragmatically, as another tool available to
organize your code, just like modules and functions.
 
 I realize after all these reposnses that I should have mentioned that  I do 
 use
 classes to do exactly that.  
 
 But I have the nagging feeling I still have it all upside down and inside out.
 Writing a bunch of objects that are supposed to interact in dynamic ways at
 runtime with nobody leading the band, and all.  Maybe if I'm not writing a
 a library or framework I shouldn't worry so much.

Hmm...there are probably some programs like that...maybe a large framework like 
Twisted. Most of my use of classes is fairly prosaic, just a way to organize 
code so it makes sense. Class instances are usually related by simple 
containment in a loose hierarchy of some sort.

 For all I know people are using IDEs that let them write unreadable code that
 simultaneously allows loosest coupling between classes, and most reuse of code
 at the expense of readability.  Giant jellyfish-like automatons that are easy
 to maintain with appropriate tools.  I'm still not sure that impression is
 wrong :)

Well, except for the easy to maintain with appropriate tools you are probably 
right. In fact I have the misfortune of working on a system like that right 
now. Without appropriate tools, whatever they may be.
 
 I do my best learning by reading good examples.  I imagine most people do.
 There is just no substitute for understanding what somebody else has done 
 well,
 tip to tail.  I am just mistrustful of something so hard to puzzle out.

This thread has a few suggestions:
http://groups.google.com/group/comp.lang.python/browse_frm/thread/da095d94c77fe2c7?q=code+exampleshl=en;

Kent

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


Re: [Tutor] perldoc - confused!

2005-10-21 Thread Danny Yoo

 I spent ages today trying to list all items in a directory, simple I now
 know (os.listdir) but I was trying to use os.walk simply because I had
 no idea that listdir existed.  I only found listdir because I was
 reading about walk

You should have a 'pydoc' utility.  It's also possible to get help from
the interactive prompt by doing 'help()'.

Good luck!

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


Re: [Tutor] self knowledge

2005-10-21 Thread Alan Gauld
Hi Ethan,

 When using the class in practice, I've found it natural to
 create instances of Foo so that the instance itself is called
 name.  Thus, I find myself doing things like:

 a=Foo('a')
 b=Foo('b')


This is very common at the interactive prompt.
However its nearly always the wrong thing to do in a real world
type program. For one thing how will the code elsewhere in your
programme know about these new object names? You would
need to find a way for every point in your code to gain awareness
of all the names currently in use! In fact Python does provide
ways of doing that because the mnames are hekld in system
dictionaries which you can access but its extre,mekly kludgy!

Much better is to create your instances in a dictionary with the
key being the name. Thus in your example:

 objects = {} # empty dictionary
 objects['a']=Foo('a')
 objects['b']=Foo('b')

You then access the objects with

objects[name].method()

slightly more typing but much easier to use because you can
iterate over all the objects in the dictionary with

for obj in objects:
   obj.dosomething()

Otherwise you would need to know all the object names
and call each in turn or resort to black magic to access
the Python internal dictionaries.

 I want the name attribute to depend only on the name of the
 original variable

Now that's a different proposition.
Do you want the name to depend on the prebviously created
variable or the variable name to depend on the object attribute?

If you want the object attribute to depend on the variable name
thats very strange. What happens when you pass the object to
amother namespace? Or if you save to a file or database and
then restore, will it always be associated with the same variable?

Can you explain why you believe that would be useful?

 c=a
 c.name
 'a'

 is the desired behavior.

OK, Thats different again.

That could be done as shoewn above with

 c = objects['a']
 c.name

 First, my immediate inclination is to try and think of
 a way for the object to know its own name (at the time
 its instantiated) without being told

variables in python are merely references to an object, the object is not 
actually called by the name of the variable. Consider:

a = C() # an instance of c
b = a# both a and b point at the instance

which is the name of the C instance? Is it a or b or neither?

 id(a)
 id(b)

shows that the objects real identity is a number which is
unrelated to either 'a' or 'b'

 Second, because this seems awkward, I strongly suspect
 that there's a pretty fundamental problem with the way
 I'm approaching the problem.  When the name of a variable
 is itself information which might be useful, what's the
 right way convey this information?

Yes you are trying tonsolve the wrong problem.
Instances created dynamically at runtime are best kept in
a container (list or dictionary) and managed as a group or
selected on demand. A dictionary is usually the best option.

After all thats why Python uses dictionaries under the hood
for its own purposes!

FWIW I give an example of this problem in the OOP topic
of my tutor, in the bank account example where we need to
store a collection of bank accounts by account ID.

HTH,

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld


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


Re: [Tutor] : Threads?

2005-10-21 Thread Alan Gauld
 in mind, I thought, why not try to use threads so all the conversions 
 happen simultaneously?  That way, the whole album will take between 30 
 and 50 seconds.  

One possible problem with this is that the activity is disk IO bound.
In fact using too many threads could even slow the thing down as 
the HD head spends all its time shunting between files.

If you keep the thread count down to two or three you might get 
a noticable improvement but one thread per track, unless you have 
a lot of separate hard disk spindles to distribute the work will 
not help much I suspect.


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


Re: [Tutor] is mxDateTime recommended?

2005-10-21 Thread Lance E Sloan
--On Thursday, October 20, 2005 4:44 PM -0400 Kent Johnson [EMAIL PROTECTED] 
wrote:
 Since 2.3 Python includes a datetime module which has some facility for
 date calculations. I think mxDateTime is more sophisticated but if your
 needs are simple take a look at datetime.

I think I would like to use the datetime module included with Python, 
just so I don't need to worry about an additional piece of code, the 
mxDateTime module.

In my code, I set up some mxDateTime RelativeDateTime objects that will be 
used later:

  secondLockBeginsDelta = DateTime.RelativeDateTime( day = 14, hour = 23,
  minute = 59, months = -4 )

When I add that to another mxDateTime object, the month, hour, and minute 
get set to those absolute values, but the month is reduced by four.  I know 
I won't be able to exactly the same thing with datetime.  I think I could 
do something like this (semi-pseudocode):

  firstTime = datetime.now()
  secondTime = firstTime - timedeltaFourMonthsObject
  secondTime.replace( day = 14, hour = 23, minute = 59 )

The problem is that I can't figure out how to create that 
timedeltaFourMonthsObject.  The datetime timedelta class constructor 
doesn't allow creating a delta with a month.  Any suggestions?

(I think it's a little too bad that the timedelta class represents all 
deltas as days and seconds.  That must be why they don't support months, 
since months have different lengths.  IMHO...)

Also, how can I make a copy of a datetime object?

  timeA = datetime.now()
  timeB = timeA # copy or reference to same object?

--
Lance E Sloan, Systems Research Programmer III
U-M WATS: Web Applications, Technologies, and Solutions
Full-service web and database design, development, and hosting.
http://www.itcs.umich.edu/wats/ - Putting U on the Web

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


Re: [Tutor] is mxDateTime recommended?

2005-10-21 Thread Kent Johnson
Lance E Sloan wrote:
 When I add that to another mxDateTime object, the month, hour, and 
 minute get set to those absolute values, but the month is reduced by 
 four.  I know I won't be able to exactly the same thing with datetime.  
 I think I could do something like this (semi-pseudocode):
 
  firstTime = datetime.now()
  secondTime = firstTime - timedeltaFourMonthsObject
  secondTime.replace( day = 14, hour = 23, minute = 59 )
 
 The problem is that I can't figure out how to create that 
 timedeltaFourMonthsObject.  The datetime timedelta class constructor 
 doesn't allow creating a delta with a month.  Any suggestions?
 
 (I think it's a little too bad that the timedelta class represents all 
 deltas as days and seconds.  That must be why they don't support months, 
 since months have different lengths.  IMHO...)

This thread may help:
http://groups.google.com/group/comp.lang.python/browse_frm/thread/6c751e2845932c20/08466fb5a150dd9e?hl=en#08466fb5a150dd9e


 Also, how can I make a copy of a datetime object?
 
  timeA = datetime.now()
  timeB = timeA # copy or reference to same object?

Reference to the same object.

one way is:
import copy
timeB = copy.copy(timeA)

Kent

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


Re: [Tutor] is mxDateTime recommended?

2005-10-21 Thread Tim Peters
[Lance E Sloan]
 ...
 (I think it's a little too bad that the timedelta class represents all
 deltas as days and seconds.

And microseconds.

 That must be why they don't support months, since months have
 different lengths.  IMHO...)

That's right.  It's hard to argue about what days, seconds and
microseconds mean (note that I didn't say it's impossible 0.5 wink).
 Exactly what a month means depends on who you ask, when you ask it,
and what they're doing at the time.  That is, there is no answer to
the question that doesn't assume some policy that, while it may help
some people some of the time, would at least as often get in the way
for other people.  datetime tries to be like, say, integers that way: 
a solid base with clear semantics, but if you want square roots you're
gonna have to define what the heck you mean by that and do it yourself
;-)

 Also, how can I make a copy of a datetime object?

It's almost certainly the case that you don't really want to, but are
suffering a confusion about something else that makes you _think_ you
want to.  It's hard to guess what that may be unless you can explain
clearly what you think making a copy would _accomplish_ for you.  It
will almost certainly turn out to be the case that it would not
accomplish what you're really after, or that there's no advantage in
accomplishing it.  datetime objects are (again like integers)
immutable:  there is nothing anyone can do to _change_ the value of a
datetime object.  In a nutshell, that's why making a copy is almost
certainly irrelevant.  For example, if you see that d.month is 7 at
some point, d.month will always be 7 thereafter, no matter what other
code you may run, just so long as `d` is bound to the same datetime
object for the duration.  There are no operations in the language that
can mutate a datetime object's value.

  timeA = datetime.now()
  timeB = timeA # copy or reference to same object?

Same object, and it makes no difference to that answer in Python no
matter what appears on the right side of the first statement.  You
happened to use a datetime.date in this example, but it would be the
same answer if you used an integer, string, list, tuple, function,
bound method object, class, array, module, file ..., any object
whatsoever.  No exceptions.

[Kent Johnson]
 import copy
 timeB = copy.copy(timeA)

That does happen to make a distinct physical copy today, but there's
no guarantee it will always do so.  In general, _any_ operation is
free to return a pre-existing instance of an object of an immutable
type, if it has the correct value.  Again there are no exceptions to
that rule.  Here's an example where copy.copy() doesn't happen to make
a distinct physical copy today:

 import copy
 two = 2
 another_2 = copy.copy(2)
 two is another_2
True

Again, if anyone thinks bad consequences may follow from this, it's
almost certainly the case that they misunderstand something else about
Python.  There is simply no way to tell whether two immutable objects
with the same _value_ are or aren't the same physical object without
looking at their memory addresses, and it's extremely rare for sane
wink Python code to give a hoot about exactly where an object
happens to reside in RAM.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] : Threads?

2005-10-21 Thread Orri Ganel
I'll try doing 3 or 4 tracks per thread, then. Thanks for the advice.If you keep the thread count down to two or three you might get
a noticable improvement but one thread per track, unless you havea lot of separate hard disk spindles to distribute the work willnot help much I suspect.Alan G.
-- Email: singingxduck AT gmail DOT comAIM: singingxduckProgramming Python for the fun of it.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor