[Tutor] __init__.py not working?

2005-07-08 Thread Bernard Lebel
Hello,

In my script library, one directory is named rendering. When I try
to import a module from this directory, I always get this error:

#ImportError: dynamic module does not define init function (initrendering)

However, there is an __init__.py file in there, and also the pyc
version. All other directories of the library seem to be working fine.


Any suggestion?


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


Re: [Tutor] compiling python files

2005-07-01 Thread Bernard Lebel
That would be the compileall utility:
http://www.python.org/doc/2.4.1/lib/module-compileall.html


Cheers
Bernard


On 7/1/05, Johan Geldenhuys [EMAIL PROTECTED] wrote:
  Hi all,
  I have python .py files that I want to compile into .pyc files.
  Is there a easy way of doing this?
  
  Thanks,
  
  Johan
  
  
 ___
 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] repr()

2005-06-07 Thread Bernard Lebel
Hello,

Possibly I am missing something, but how do you use the repr() function?

I type this ultra-simple function:

def myFunc(): print 'hello'

Then run

repr( myFunc )

Wich returns

'function myFunc at 0x009C6630'


Okay then I run

s = repr( myFunc() )
print s

Wich returns

'None'


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


Re: [Tutor] repr()

2005-06-07 Thread Bernard Lebel
Ok thanks a lot.

The real question is, then, is there a way I can print the code of a
function as a string? Something like

'def myFunction: print hello'


Thanks
Bernard



On 6/7/05, Max Noel [EMAIL PROTECTED] wrote:
 
 On Jun 7, 2005, at 20:42, Bernard Lebel wrote:
 
  repr( myFunc )
 
  Wich returns
 
  'function myFunc at 0x009C6630'
 
 
  Okay then I run
 
  s = repr( myFunc() )
  print s
 
  Wich returns
 
  'None'
 
  That's perfectly normal. Your last assignment calls the
 function, then assigns to s the representation of the function's
 return value. A function that doesn't have a return statement returns
 None, and repr(None) == 'None'. Which is what you get.
 
 -- Max
 maxnoel_fr at yahoo dot fr -- ICQ #85274019
 Look at you hacker... A pathetic creature of meat and bone, panting
 and sweating as you run through my corridors... How can you challenge
 a perfect, immortal machine?
 

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


Re: [Tutor] repr()

2005-06-07 Thread Bernard Lebel
Well, to make a long story short, this is because I use Python through 
another software, Softimage|XSI, and by design a string is required to 
pass logic to on-the-fly GUIs. Since XSI implements 3 other languages 
(ActivePerl, VBScript and JScript), I suppose this was designed that way 
with JScript's .toString() in mind. The SDK documentation does indeed 
show only a JScript example.


Bernard


Alan G wrote

Curious as to why you would ever want to though?

  



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


Re: [Tutor] dictionary values in strings

2005-05-19 Thread Bernard Lebel
Indeed, dictionaries don't have a .key attribute.

Instead, use:

# Get list of values for 'key1'
aList = dol[ 'key1' ]


This would return the list of values you have assigned to 'key1' in
the dictionary. Once you got that list, you can look in the list to
find out the index of 'lil1' and return it:



# Iterate list of values assigned to 'key1'
for i in range( 0, len( dol[ 'key1' ] ) ):

# Get list element using current iteration value
sValue = dol[ 'key1' ][i]

# If list element is 'lil1', print its list index
if sValue == 'lil1': print 'lil1 has index : ' + str( i )


Cheers
Bernard


On 5/19/05, William O'Higgins [EMAIL PROTECTED] wrote:
 I am trying to discover the syntax for call on a dictionary of lists by
 key and index.
 
 The data structure looks like this:
 
 dol = {'key1':['li1','li2','li3'],'key2':['li1','li2','li3'],\
 'key3':['li1'li2,'li3','']}
 
 The keys are passed to a function as arguments, and I want the value of
 the specified list index.  This is what I *thought* it would look like:
 
 dol.key(argument)[0] # would return li1 when argument equals key1
 
 But that's wrong.  The error I get is this:
 AttributeError: 'dict' object has no attribute 'key'
 
 I don't know how to interpret that error (well, I know I screwed up, but
 ... :-)  Thanks.
 --
 
 yours,
 
 William
 
 
 
 BodyID:3456163.2.n.logpart (stored separately)
 
 ___
 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] map() and lambda to change class instance attribute (fwd)

2005-05-19 Thread Bernard Lebel
Well, that was a nice explanation. Thanks once again Kent!

Bernard


On 5/16/05, Kent Johnson [EMAIL PROTECTED] wrote:
 Bernard Lebel wrote:
  Hi Kent,
 
  So if I undestand you right, mapping a function with map() when it is
  a built-in function will/may be faster than a for loop, but if it's a
  custom function (ie. a def one), it will most likely be slower?
 
 I guess I didn't proofread that last mail...what I meant is
 
 - Write the code for clarity first
 - If you find a performance bottleneck then test different solutions to see 
 which is fastest for
 your actual data and usage.
 - Mapping a builtin function over a list is relatively fast in Python and it 
 is worth trying if it
 meets your needs.
 - Calling a function is relatively expensive in Python so if using map() 
 requires you to define a
 helper that may wipe out its advantage.
 - Testing is crucial! Guessing is only good for helping to come up with ideas 
 to test.
 
 Here is a program that tests six ways to apply a function to elements of a 
 list. The functions don't
 create new lists except as side effects since that was the original problem. 
 They use map(), list
 comprehension and an explicit for loop to apply str() or a Python function 
 returning str() to the
 elements of a list. (The list already contains strings so the actual function 
 call should be fast.)
 
 
 
 import timeit
 
 data = [str(n) for n in range(100)]
 
 def strByMap():
  map(str, data)
 
 def strByListComp():
  [str(n) for n in data]
 
 def strByLoop():
  for n in data:
  str(n)
 
 
 def func(x):
  return str(x)
 
 
 def funcByMap():
  map(func, data)
 
 def funcByListComp():
  [func(n) for n in data]
 
 def funcByLoop():
  for n in data:
  func(n)
 
 
 def timeOne(fn):
  setup = from __main__ import  + fn.__name__
  stmt = '%s()' % (fn.__name__)
 
  t = timeit.Timer(stmt, setup)
  secs = min(t.repeat(number=1000))
  print '%15s %s' % (fn.__name__, secs)
 
 for fn in [ strByMap, strByListComp, strByLoop, funcByMap, funcByListComp, 
 funcByLoop ]:
  timeOne(fn)
 
 ###
 
 Here are the results on my computer:
 
 strByMap 0.0359623918682
strByListComp 0.0581065470611
strByLoop 0.0481150537289
funcByMap 0.0810943849009
   funcByListComp 0.0891375859222
   funcByLoop 0.0806144356336
 
 So for this test, map is fastest when calling a builtin and explicit looping 
 is fastest when calling
 a function. With the explicit loop you might be able to inline the function, 
 in which case it would
 be much faster than either map or list comp.
 
 Modifying the program slightly so that each function actually creates a list 
 changes the results
 dramatically:
 strByMap 0.0365733633744
strByListComp 0.0579948010152
strByLoop 0.0764722890758
funcByMap 0.0811885309446
   funcByListComp 0.0883995032888
   funcByLoop 0.10586876265
 
 Now map() is fastest in both cases, though a for loop with inlined code beats 
 map() with an external
 function.
 
 Kent
 
 
  Thanks
  Bernard
 
 
  On 5/13/05, Kent Johnson [EMAIL PROTECTED] wrote:
 
 Bernard Lebel wrote:
 
 The authors even go as far as saysing, on page 228 (first paragraph)
 that map() used that way has a performance benefit and is faster than
 a for loop.
 
 That may well be correct, at least in the case where the function passed to 
 map is a builtin.
 Mapping a builtin to over a list is extremely fast. So write the code with 
 a for loop so it is
 clear. When you identify a performance bottleneck you can try rewriting 
 your loop using map or list
 comprehension, which is also fast. Until then it is premature optimization. 
 For typical loops over a
 small number of items I can't imagine you would notice the difference.
 
 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

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


Re: [Tutor] map() and lambda to change class instance attribute (fwd)

2005-05-19 Thread Bernard Lebel
That is very interesting John. Thanks!


Bernard




On 5/19/05, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:
 Quoting Bernard Lebel [EMAIL PROTECTED]:
 
  Well, that was a nice explanation. Thanks once again Kent!
 
 There is a nice (not too technical) essay on the running speeds of different
 looping constructs on python.org:
 
 http://www.python.org/doc/essays/list2str.html
 
 Just FYI :-)
 
 --
 John.

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


Re: [Tutor] Finding word in file

2005-05-18 Thread Bernard Lebel
Hi Joseph,

To answer your last question first, you should use the
os.path.exsits() method to see if the path is valid:
http://www.python.org/doc/2.4.1/lib/module-os.path.html


As for finding a word in a text, I would suggest to write a basic text
parser that would work on small files.



def parseText():

# oFile: text file to test
# myWord: word we are looking for

# Get all lines into list
aLines = oFile.readlines()

# Perform list comprehension on lines to test if the word is found
for sLine in aLines:

# Parse the line (remove spaces), returns list
aLine = sLine.split()

# Iterate words and test to see if they match our word
for sWord in aLines:
# if it matches, append it to our list
if sWord == myWord: aWords.append( sWord )



# Create empty list to store all instances of the word that we may find
aWords = []

# Prompt user to know what word to search
myWord = str( raw_input( 'what word to searh:' ) )



Note that I'm still new to Python, there might be more efficient ways
to do that.

For larger text files (in the order of thousands of lines), you may
not use readlines() to build a list of the lines and insteand read one
line after another with readline() or xreadline() and append to the
list as you find the word.



Cheers
Bernard



On 5/18/05, Joseph Quigley [EMAIL PROTECTED] wrote:
 I'm making a program that opens a file and tries to find the word you specify.
 I can't get  it to find the word! I also would like to know how I can get
 it to print 2 lines above and 2 lines below the line with the word specified.
 One more thing, the try: IOError won't work... I type the name of a
 nonexistent file and the except won't kick in and print my error message!
 Not to mention the program is killed. How can i fix that?
 Thanks,
  JQ
 
 Full Code:
 
 while True:
  file_name = raw_input(Enter the full file name: )
  f = file(file_name, 'r')
  try:
  IOError
  except:
  print File not found. Directories are not supported
 
  while True:
  line = f.readline()
  if len(line) == 0:
  break
  find_word = raw_input(What word do you want to find (quit() to
 quit)?\n )
  if find_word in file_name:
  print f.readline(find_word)
  elif find_word == quit():
  break
  print line,
  f.close()
 
  close = raw_input(\n\nQuit program? (Y/N) )
  if ((close == Y) or (y == close)):
  break
  else:
 print
 
 ___
 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] Finding word in file

2005-05-18 Thread Bernard Lebel
Ooops, a part of my code was missing, sorry about that. Here is it
again, complete.


def parseText():

# oFile: text file to test
# myWord: word we are looking for

# Get all lines into list
aLines = oFile.readlines()

# Perform list comprehension on lines to test if the word is found
for sLine in aLines:

# Parse the line (remove spaces), returns list
aLine = sLine.split()

# Iterate words and test to see if they match our word
for sWord in aLines:
# if it matches, append it to our list
if sWord == myWord: aWords.append( sWord )



# Create empty list to store all instances of the word that we may find
aWords = []

# Prompt user to know what word to search
myWord = str( raw_input( 'what word to searh:' ) )

# Call function
parseText()

# Check if list has at least one element
if len( aWords )  1: print 'Word not found in file'
else: print str( len( aWords ) ) + ' instances of our word found in file'


Sorry again
Bernard


On 5/18/05, Bernard Lebel [EMAIL PROTECTED] wrote:
 Hi Joseph,
 
 To answer your last question first, you should use the
 os.path.exsits() method to see if the path is valid:
 http://www.python.org/doc/2.4.1/lib/module-os.path.html
 
 As for finding a word in a text, I would suggest to write a basic text
 parser that would work on small files.
 
 def parseText():
 
 # oFile: text file to test
 # myWord: word we are looking for
 
 # Get all lines into list
 aLines = oFile.readlines()
 
 # Perform list comprehension on lines to test if the word is found
 for sLine in aLines:
 
 # Parse the line (remove spaces), returns list
 aLine = sLine.split()
 
 # Iterate words and test to see if they match our word
 for sWord in aLines:
 # if it matches, append it to our list
 if sWord == myWord: aWords.append( sWord )
 
 # Create empty list to store all instances of the word that we may find
 aWords = []
 
 # Prompt user to know what word to search
 myWord = str( raw_input( 'what word to searh:' ) )
 
 Note that I'm still new to Python, there might be more efficient ways
 to do that.
 
 For larger text files (in the order of thousands of lines), you may
 not use readlines() to build a list of the lines and insteand read one
 line after another with readline() or xreadline() and append to the
 list as you find the word.
 
 Cheers
 Bernard
 
 
 On 5/18/05, Joseph Quigley [EMAIL PROTECTED] wrote:
  I'm making a program that opens a file and tries to find the word you 
  specify.
  I can't get  it to find the word! I also would like to know how I can get
  it to print 2 lines above and 2 lines below the line with the word 
  specified.
  One more thing, the try: IOError won't work... I type the name of a
  nonexistent file and the except won't kick in and print my error message!
  Not to mention the program is killed. How can i fix that?
  Thanks,
   JQ
 
  Full Code:
 
  while True:
   file_name = raw_input(Enter the full file name: )
   f = file(file_name, 'r')
   try:
   IOError
   except:
   print File not found. Directories are not supported
 
   while True:
   line = f.readline()
   if len(line) == 0:
   break
   find_word = raw_input(What word do you want to find (quit() to
  quit)?\n )
   if find_word in file_name:
   print f.readline(find_word)
   elif find_word == quit():
   break
   print line,
   f.close()
 
   close = raw_input(\n\nQuit program? (Y/N) )
   if ((close == Y) or (y == close)):
   break
   else:
  print
 
  ___
  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] Lists of files

2005-05-14 Thread Bernard Lebel
Hi William,

First, check out the os and os.path modules. It has exactly what you
need to handle files and directories.
http://www.python.org/doc/2.4.1/lib/module-os.html
More specifically:
http://www.python.org/doc/2.4.1/lib/os-file-dir.html
http://www.python.org/doc/2.4.1/lib/module-os.path.html

import os

# Get list of files in directory
aFiles = os.listdir( path to files )

# Create empty list to store image files
aImgFiles = []

# Iterate list to collect image files
for sFile in aFiles:
 # Split extension to see if it is an image type
 # This returns a list of two elements, check the last one to get
the extension
 if os.path.splitext( sFile )[-1] == animageextension:
aImgFiles.append( sFile )



You could also do that more quickly with list comprehension:

aImgFiles = [ sFile for sFile in os.listdir( pathtolistfiles ) if
os.path.splitext( sFile )[-1] == imageextension ]



Cheers
Bernard



On 5/14/05, William O'Higgins [EMAIL PROTECTED] wrote:
 Here's the problem - I want a list (array) of the files in a directory,
 and then I want to iterate over the list testing for image-ness (with
 imghdr.what()) and put all the image filenames in a global list.
 
 What I've tried is this:
 
 files = glob.glob('*.*')
 
 for file in files:
 global pics
 pics = []
 if imghdr.what(file):
 # so far so good - file is a list of files in the directory
 pics.append(file)
 # I this this is the problem - my list only has the last
 # alphabetical entry in it
 
 So there's two questions - is there a better way to create a list of
 files in a directory?  And, how do I populate my list to get all of the
 filenames.  I've also tried pics + [file], but that gave me an empty
 list.
 --
 
 yours,
 
 William
 
 
 BodyID:4269787.2.n.logpart (stored separately)
 
 ___
 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] map() and lambda to change class instance attribute (fwd)

2005-05-14 Thread Bernard Lebel
Thanks Alan, that clears things up quite well.

Bernard


On 5/14/05, Alan Gauld [EMAIL PROTECTED] wrote:
  So if I undestand you right, mapping a function with map()
  when it is a built-in function will/may be faster than a for
  loop, but if it's a custom function (ie. a def one), it will
  most likely be slower?
 
 That's right, a builtin function, including map itself will
 be written in C and so be 'fast'. So we have the trade off
 between a Python for loop calling a function or a C loop
 which additionally has to build a list result. But in the
 builtin case we have C code calling C code which is
 much faster than Python code calling C code.
 
 So for a function written in Python there will be less
 difference and the bulk of the time is likely to be
 spent in the function calls but for builtins C to C
 could give a significant benefit.
 
 Which is faster will depend on several other factors including
 what the function returns and how easily that can be packaged
 into a list.
 
 From my personal experience I wouldn't expect map to be
 much faster than a for loop, if at all. But as Kent says
 you can always profile it and test it if you really need
 speed. The real issue is the map call is more obscure
 than an explicit loop since map() is intended to build
 a list not modify an existing list!
 
 Alan G.

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


Re: [Tutor] Why use apply()?

2005-05-13 Thread Bernard Lebel
All right, thank you.


Bernard



On 5/12/05, Bob Gailer [EMAIL PROTECTED] wrote:
  At 02:17 PM 5/12/2005, Bernard Lebel wrote:
  
 Just a generic question: why one would use apply()?
 
  In Learning Python, on page 357, there is an example of generating an
  instance using apply():
 
  class A:
def __init__( self, number ):
   self.number = number
 
  a = apply( A, 3 )
  What is the benefit of doing this over simply creating an instance the
 usual way:
  a = A( 3 )
  No benefit. See 2.2 Non-essential Built-in Functions in the Python Library
 Reference. 'Use of apply() is not necessary since the ``extended call
 syntax,'' as used in the last example, is completely equivalent. 
  
 
  Bob Gailer
  mailto:[EMAIL PROTECTED]
  510 558 3275 home
  720 938 2625 cell
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] map() and lambda to change class instance attribute (fwd)

2005-05-13 Thread Bernard Lebel
The authors even go as far as saysing, on page 228 (first paragraph)
that map() used that way has a performance benefit and is faster than
a for loop.


Cheers
Bernard



On 5/13/05, Alan Gauld [EMAIL PROTECTED] wrote:

 How bizarre. I'm astonished that Lutz/Ascher even show that as a means
 of changing an attribute. As Danny said, it's definitely an abuse of
 map,
 and there is no advantage whatsoever, as far as I can see. In fact I
 suspect that it's actually slower than an explicit loop (because of
 the extra list building).
 
  However I was looking into lambdas in hope to eliminate the need to
  define a function.
 
 And a loop also avoids the need for a function, and therefore for
 a function call, which is another reason the map should be slower.
 
 Its completely weird that a text book should even suggest that map
 be used for this!
 
 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

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


Re: [Tutor] map() and lambda to change class instance attribute (fwd)

2005-05-13 Thread Bernard Lebel
Hi Kent,

So if I undestand you right, mapping a function with map() when it is
a built-in function will/may be faster than a for loop, but if it's a
custom function (ie. a def one), it will most likely be slower?


Thanks
Bernard


On 5/13/05, Kent Johnson [EMAIL PROTECTED] wrote:
 Bernard Lebel wrote:
  The authors even go as far as saysing, on page 228 (first paragraph)
  that map() used that way has a performance benefit and is faster than
  a for loop.
 
 That may well be correct, at least in the case where the function passed to 
 map is a builtin.
 Mapping a builtin to over a list is extremely fast. So write the code with a 
 for loop so it is
 clear. When you identify a performance bottleneck you can try rewriting your 
 loop using map or list
 comprehension, which is also fast. Until then it is premature optimization. 
 For typical loops over a
 small number of items I can't imagine you would notice the difference.
 
 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] Why use apply()?

2005-05-12 Thread Bernard Lebel
Just a generic question: why one would use apply()?

In Learning Python, on page 357, there is an example of generating an
instance using apply():

class A:
  def __init__( self, number ):
 self.number = number

a = apply( A, 3 )



What is the benefit of doing this over simply creating an instance
the usual way:


a = A( 3 )



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


[Tutor] map() and lambda to change class instance attribute

2005-05-11 Thread Bernard Lebel
Hello,

Let say I have several class instances in a list, and these class
instances have an attribute named value, whose value is an integer.

I would like to know if it is possible to loop over the list of
instances to change their value attribute, using a map( (
lambda:...), ... ) type of loop. I'm very new to lambdas as you can
see :-)


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


Re: [Tutor] map() and lambda to change class instance attribute

2005-05-11 Thread Bernard Lebel
Hi Danny,

Thanks for the answer.

I have to confess that I already use map(), or should I say abuse, for
this, although it is the first time I consider using lambdas. Up until
now I always used map() to perform a looped call on a function that
would change the attribute value, as shown in Mark Lutz  David
Ascher's Learning Python:

# Perform attribute value change on a single instance
def iterateInstances( oInstance ):
 oInstance.value = myValue

# Loop over list of instances
map( iterateInstances, aListOfInstances )

However I was looking into lambdas in hope to eliminate the need to
define a function.

Cheers



On 5/11/05, Danny Yoo [EMAIL PROTECTED] wrote:
 
 
 On Wed, 11 May 2005, Bernard Lebel wrote:
 
  Let say I have several class instances in a list, and these class
  instances have an attribute named value, whose value is an integer.
 
  I would like to know if it is possible to loop over the list of
  instances to change their value attribute, using a map( ( lambda:...),
  ... ) type of loop.
 
 Hi Bernard,
 
 Hmmm... then map() is probably not what you want.  map() is meant to
 transform a list of things into another list of things, but isn't really
 meant to mutate its input.
 
 It is possible to abuse map() to do what you're trying to do, using the
 setattr() function:
 
 ## Pseudocode
 map(lambda instance: setattr(instance, 'value', 42))
 ##
 
 but this is definitely language abuse.  *grin*
 
 map() is not intended to be run just to affect changing assignments on its
 input.  It'll probably be clearest in Python just to write out the loop
 explicitely.
 
 Best of wishes to you!
 

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


[Tutor] Else in list comprehension

2005-05-07 Thread Bernard Lebel
Hello,

I just started using list comprehensions (loving them!)

I know you can add an if statement, but can you put in there an else?
I could not find an example of this.


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


[Tutor] Appending to many lists with list comprehension

2005-05-02 Thread Bernard Lebel
Hello,

I have an object, and this object has attributes. These attributes are 
objects in their own right, and each attribute also have its own attributes.

So I loop over the top object, and for each attribute encountered, I 
want to put in a list two of the attributes of this attribute. Hope it 
makes sense.

Right now, here is what I'm doing:



aList = [ [], [] ]


# Iterate attributes of the top object  
for oAttribute in oObj.attributes:

# Append to list 0 the attribute type of the current attribute
aList[0].append( str(oAttribute.type) )

# Append to list 1 the attribute name of the current attribute
aList[1].append( str(oAttribute.name) )




Instead of having two separate lists and a for loop, I'd like to perform 
a list comprehension that would do this all in one go, if this is doable.



Thanks
Bernard

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


Re: [Tutor] Dynamically composing a module name

2005-04-19 Thread Bernard Lebel
Hi,

The one thing I would try, if I understand what you're after
correctly, would be to run a exec command with the module name.


modulename = 'myModule'
exec 'import ' + modulename

Then you can access the module names as any imported module.


Cheers
Bernard


On 4/19/05, Tim Johnson [EMAIL PROTECTED] wrote:
 Hello Pythonmeisters:
 
 Is it possible to dynamically compose a module name
 for import?
 
 Pointers to documentation or other discussions would
 be sufficient at this time.
 
 thanks
 --
 Tim Johnson [EMAIL PROTECTED]
   http://www.alaska-internet-solutions.com
 ___
 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] Re: Class - superclass

2005-04-11 Thread Bernard Lebel
Thanks a lot, now it's clear.


Bernard


On Apr 8, 2005 3:48 PM, Andrei [EMAIL PROTECTED] wrote:
 Bernard Lebel wrote on Fri, 8 Apr 2005 15:05:13 -0400:
 
  I'm experimenting with basic inheritance concepts, and something that
  I would assume to work returns an error.
 
  class A:
  ... def __init__( self ):
  ... self.a = 13
  ...
  class B( A ): # create sub-class of class A
  ... def __init__( self ):
  ... self.b = 14
 
 Call the __init__ of the ancestor explicitly:
 
  class B(A):
 ... def __init__(self):
 ... A.__init__(self)
 ... self.b = 14
  b = B()
  b.a, b.b
 (13, 14)
 
 B inherits everything from A, but by defining B.__init__, the __init__
 inherited from A is replaced, so you'll need to call it explicitly. Python
 has no way of knowing that you still want to use the original __init__ too
 unless you tell it so. To demonstrate the fact that __init__ is indeed
 inherited:
 
  class C(A):
 ... pass
  c = C() # inherited __init__ (A.__init__) is called
  c.a
 13
 
 --
 Yours,
 
 Andrei
 
 =
 Real contact info (decode with rot13):
 [EMAIL PROTECTED] Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq
 gur yvfg, fb gurer'f ab arrq gb PP.
 
 ___
 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] Class - superclass

2005-04-08 Thread Bernard Lebel
Hello,

I'm experimenting with basic inheritance concepts, and something that
I would assume to work returns an error.


 class A:
... def __init__( self ):
... self.a = 13
...
 class B( A ): # create sub-class of class A
... def __init__( self ):
... self.b = 14
...



 b = B() # create instance of class B
 b.a # here I would expect the class A's a attibute to be accessible...
Traceback (most recent call last):
  File stdin, line 1, in ?
AttributeError: B instance has no attribute 'a'



Anyone can shed any light on this? Using Python 2.3.4.


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


Re: [Tutor] [HELP]how to test properties of a file

2005-04-05 Thread Bernard Lebel
As far as I'm aware, this is very normal. The file is being used by an
application, so there is a lock on it.


Cheers
Bernard


On Apr 4, 2005 7:34 PM, Shidai Liu [EMAIL PROTECTED] wrote:
 I found out in the following situation, it fails to work.
 Say, 'somefile.csv' is opened by EXCEL,
  os.access('somefile.csv', os.W_OK)
 True
  file('somefile.csv', 'w')
 IOError: [Errno 13] Permission denied: 'somefile.csv'
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] test

2005-03-31 Thread Bernard Lebel
test!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is the best book to start?

2005-03-30 Thread Bernard Lebel
Hoffmann wrote:
Hi All,
I am starting to studying Python. I have some previous
experience with C (beginner level). Could, anyone,
please, suggest a good Python book? I have both
Learning Python by Lutz  Ascher, and Python How to
Program by Deitel and others. Are those good books?
Thanks.
Hoffmann
I have only the former, and am totally happy with it. Very clear and 
well put book. A great investment.

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


Re: [Tutor] Basic terminology

2005-02-15 Thread Bernard Lebel
Well, thanks everyone who answered, much clearer now.
Bernard

Max Noel wrote:
In a slightly more generic fashion (everybody started dropping 
examples), the goal of an integer (euclidian) division (say, a / b) is 
to express an integer as such:

a = b * quotient + remainder
Where all the numbers used are integers, and 0 = remainder  b.
When you perform integer division in Python, a / b (a // b in 2.4+) 
gives you the quotient, and a % b gives you the remainder.

See the other posts for examples of use :p
-- Max
maxnoel_fr at yahoo dot fr -- ICQ #85274019
Look at you hacker... A pathetic creature of meat and bone, panting and 
sweating as you run through my corridors... How can you challenge a 
perfect, immortal machine?

___
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] Queued threads

2005-02-15 Thread Bernard Lebel
Hello,
I have already messed a little with simple thread programming, wich took 
this form:

from threading import Thread
def mainFunction():
pass
Thread( target=mainFunction ).start()
Now, I have a list of jobs, each job being a windows bat file that 
launches an executable and performs a rendering task. So I have this 
queue of jobs, and would like to launch one only when the previous one 
has finished, and in a separate window. So far I have not been having 
much success with simple stuff:

from threading import Thread
def mainFunction():
print 'function print'
return 1
for i in range( 0, 3 ):
oThread = Thread( target=mainFunction ).start()
if oThread == 1:
print 'sleeping 3 seconds'
time.sleep( 3 )
In this example, 'sleeping 3 seconds' not returned, and each thread is 
started without any waiting.

I'm looking at the various threading module details in the library but I 
have to admit that, well, I'm a bit at loss here.

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


Re: [Tutor] Queued threads

2005-02-15 Thread Bernard Lebel
That is an attempt to catch the death of the thread. I guess I'm not 
taking the right steps ;-)

Bernard
Liam Clarke wrote:
I'm sorry, but when does oThread get the value 1?
If you're testing for it's existence via a True/False thing, try 

if oThread:
But otherwise, I'm not sure what you're expecting to get. 

On Tue, 15 Feb 2005 20:58:15 -0500, Bernard Lebel
[EMAIL PROTECTED] wrote:
Hello,
I have already messed a little with simple thread programming, wich took
this form:
from threading import Thread
def mainFunction():
   pass
Thread( target=mainFunction ).start()
Now, I have a list of jobs, each job being a windows bat file that
launches an executable and performs a rendering task. So I have this
queue of jobs, and would like to launch one only when the previous one
has finished, and in a separate window. So far I have not been having
much success with simple stuff:
from threading import Thread
def mainFunction():
print 'function print'
return 1
for i in range( 0, 3 ):
oThread = Thread( target=mainFunction ).start()
if oThread == 1:
print 'sleeping 3 seconds'
time.sleep( 3 )
In this example, 'sleeping 3 seconds' not returned, and each thread is
started without any waiting.
I'm looking at the various threading module details in the library but I
have to admit that, well, I'm a bit at loss here.
Thanks in advance
Bernard

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


Re: [Tutor] calling an external program

2005-02-13 Thread Bernard Lebel
The os module is the answer. Use chdir() to make the target executable's 
directory the current directory, and then os.system( 'command' ) to run 
the actual command.

Cheers
Bernard
Lobster wrote:
- I am trying to call up an external program
with something like a Shell command - can not find a way of doing this
(in windows)
Any hints?
Ed Jason
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to run a script file

2005-01-05 Thread Bernard Lebel
Thanks everyone who answered, it's sorted now :-D



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


[Tutor] Basic question - How to use a class from a file?

2005-01-02 Thread Bernard Lebel
Hello,
An easy one to start the year.
Trying to write my first class, already running into problems after 3 
lines! :-(

So have this little class:
class rt:
def walk(self):
print 'yeah'
So if I write this in the Python shell, instantiate rt and call walk(), 
I get the proper result. yeah gets printed.

Now let say I put this into a file. I want to import that class and use 
its methods, but this is were I get lost.

import rt
rt.walk()
Returns:
Traceback (most recent call last):
  File interactive input, line 1, in ?
AttributeError: 'module' object has no attribute 'walk'
When I try to instantiate the class, I get an non-callable module object 
error.

So how to use a class from a file?
Thanks
Bernard
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


<    1   2