a dummy python question

2005-08-25 Thread Learning Python
A example in learning Python by Mark Lutz and David Ascher

about function scope

example like this:

>>def outer(x):
 def inner(i):
print i,
if i: inner(i-1)
 inner(x)
>>outer(3)

Here supposely, it should report error, because the function inner
cannot see itself since inner is only in local namespace of outer.

but I typed in this in python interface. It works!
it print out:
3 2 1 0


If you turn this into a module file and run this
it print out
3 2 1 0 none

Can anyone explain to me what's going on?

Thanks

BTW: I am using Python 2.3

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: a dummy python question

2005-08-25 Thread Learning Python
Thanks all for replying.
I finally know what's going on.

-- 
http://mail.python.org/mailman/listinfo/python-list


argument matching question

2005-08-25 Thread Learning Python
I know this is dummy, just never saw an example of this.


I want to use the special argument matching.

A code like this:

def adder(**varargs):
  sum=varargs[varargs.keys()[0]]
  for next in varargs.keys()[1:]:
sum=sum+varargs[next]
  return sum

print adder( "first","second",'third')



It pop up error like this:

Traceback (most recent call last):
  File "learn.py", line 7, in ?
print adder( "first","second",'third')
TypeError: adder() takes exactly 0 arguments (3 given)


How to pass arguments to a functions that use dictionary collection?


Thanks

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: argument matching question

2005-08-25 Thread Learning Python
thanks, got it.
I want to test the **name option for argument matching.

-- 
http://mail.python.org/mailman/listinfo/python-list


circular import problem

2005-09-09 Thread Learning Python
An example in the book I didn't understood well
two modules files recursively import/from each other

in recur1.py,we have:

x=1
import recur2
y=1


in recur2.py, we have

from recur1 import x
from recur1 import y


If we run interactively at python command line,

>>> import recur1
it has errors like this:
Traceback (most recent call last):
  File "", line 1, in ?
  File "recur1.py", line 2, in ?
import recur2
  File "recur2.py", line 2, in ?
from recur1 import y
ImportError: cannot import name y

I understood this because recur1 is not finished when recur2 is trying
to import y.

However, if you run interactive for recur2.py interactively, it is
fine.
when you run as script from command line, recur1.py is fine. but when
you
python recur2.py at commmand line, it has errors like this:
Traceback (most recent call last):
  File "recur2.py", line 1, in ?
from recur1 import x
  File "recur1.py", line 2, in ?
import recur2
  File "recur2.py", line 2, in ?
from recur1 import y
ImportError: cannot import name y

What's really the problem here?

Thanks

-- 
http://mail.python.org/mailman/listinfo/python-list


intermediate python csv reader/writer question from a beginner

2009-02-23 Thread Learning Python
anything related to csv, I usually use VB within excel to manipulate
the data, nonetheless, i finally got the courage to take a dive into
python.  i have viewed a lot of googled csv tutorials, but none of
them address everything i need.  Nonetheless, I was wondering if
someone can help me manipulate the sample csv (sample.csv) I have
generated:

,,
someinfo,,,
somotherinfo,,,
SEQ,Names,Test1,Test2,Date,Time,,
1,Adam,1,2,Monday,1:00 PM,,
2,Bob,3,4,Monday,1:00 PM,,
3,Charlie,5,6,Monday,1:00 PM,,
4,Adam,7,8,Monday,2:00 PM,,
5,Bob,9,10,Monday,2:00 PM,,
6,Charlie,11,12,Monday,2:00 PM,,
7,Adam,13,14,Tuesday,1:00 PM,,
8,Bob,15,16,Tuesday,1:00 PM,,
9,Charlie,17,18,Tuesday,1:00 PM,,

into (newfile.csv):

Adam-Test1,Adam-Test2,Bob-Test1,Bob-Test2,Charlie-Test1,Charlie-
Test2,Date,Time
1,2,3,4,5,6,Monday,1:00 PM
7,8,9,10,11,12,Monday,2:00 PM
13,14,15,16,17,18,Tuesday,1:00 PM

note:
1. the true header doesn't start line 4 (if this is the case would i
have to use "split"?)
2. if there were SEQ#10-12, or 13-15, it would still be Adam, Bob,
Charlie, but with different Test1/Test2/Date/Time
--
http://mail.python.org/mailman/listinfo/python-list