Re: [Tutor] Python best practices

2009-11-30 Thread Albert-Jan Roskam
I'm currently reading the book "Code Complete" (I don't know the author name), which gives a lot of useful best practices. It's not specifically about one programming language. The author stresses that the majority of those practices are a matter of consensus/consistency and not a matter of reli

Re: [Tutor] Python best practices

2009-11-30 Thread ALAN GAULD
Code Complete by Steve McConnell is an excellent book and I agree with almost everything in it. Including the advice about functions. But as I said earlier the concept of multiple return values in Python is similar to returning a struct in C. The key thing is that the function has a single purpo

[Tutor] python closures

2009-11-30 Thread spir
Hello, Below startup definitions: x = 1 def f(): n = 1 def g0(a): print (x + n + a) return g0 I'm surprised the snippet below works as expected (py 2.6) without any trick: g = f() g(1)# --> 3 This means a (real) closure is built for g0, or what? Thought I would need instead to

Re: [Tutor] python closures

2009-11-30 Thread Wayne Werner
On Mon, Nov 30, 2009 at 4:24 AM, spir wrote: > which seems to indicate python really embeds "symbolic references" (*) to > outer *variables*, when creating a closure for g0. Not "pointer references" > (**), otherwise the replacement of x would not be seen by the closure --like > in the case of de

Re: [Tutor] Python best practices

2009-11-30 Thread Wayne Werner
On Mon, Nov 30, 2009 at 4:06 AM, ALAN GAULD wrote: > > Thats what I think is meant by bad practice in returning > multiple values. The function returns two completely different > things depending on some input flag. > Now that's something I definitely agree with! I can't think of a single case wh

[Tutor] Equivalent exception of os.path.exists()

2009-11-30 Thread biboy mendz
http://pastebin.ca/1693849 This is end-of-chapter3 exercise of the book Core Python Programming. I'm reading/searching in the book and other materials but is unsuccessful. There are at least 50 exceptions listed but I can't find anything close. I commented out my modified script to just what

Re: [Tutor] python closures

2009-11-30 Thread Kent Johnson
On Mon, Nov 30, 2009 at 5:24 AM, spir wrote: > Hello, > > Below startup definitions: > > x = 1 > > def f(): >  n = 1 >  def g0(a): >    print (x + n + a) >  return g0 > > > I'm surprised the snippet below works as expected (py 2.6) without any trick: > > g = f() > g(1)    # --> 3 > > This means a

Re: [Tutor] python closures

2009-11-30 Thread Stefan Behnel
spir, 30.11.2009 11:24: > Below startup definitions: > > x = 1 > > def f(): > n = 1 > def g0(a): > print (x + n + a) > return g0 > > I'm surprised the snippet below works as expected (py 2.6) without any trick: > > g = f() > g(1) # --> 3 > > This means a (real) closure is built for

Re: [Tutor] Equivalent exception of os.path.exists()

2009-11-30 Thread Tim Golden
biboy mendz wrote: http://pastebin.ca/1693849 This is end-of-chapter3 exercise of the book Core Python Programming. I'm reading/searching in the book and other materials but is unsuccessful. There are at least 50 exceptions listed but I can't find anything close. I commented out my modified

Re: [Tutor] Equivalent exception of os.path.exists()

2009-11-30 Thread Lie Ryan
On 12/1/2009 12:00 AM, biboy mendz wrote: http://pastebin.ca/1693849 This is end-of-chapter3 exercise of the book Core Python Programming. I'm reading/searching in the book and other materials but is unsuccessful. There are at least 50 exceptions listed but I can't find anything close. I comme

Re: [Tutor] python closures

2009-11-30 Thread Hugo Arts
On Mon, Nov 30, 2009 at 11:24 AM, spir wrote: > Hello, > > Below startup definitions: > > x = 1 > > def f(): >  n = 1 >  def g0(a): >    print (x + n + a) >  return g0 > > > I'm surprised the snippet below works as expected (py 2.6) without any trick: > > g = f() > g(1)    # --> 3 > > This means a

Re: [Tutor] python closures

2009-11-30 Thread Hugo Arts
On Mon, Nov 30, 2009 at 4:16 PM, Kent Johnson wrote: > That has not been needed since 2.1 though it is still useful when > closures are created in a loop (because closures are kind of late > bound - I'm not sure the exact technical explanation): > In [13]: def f(): >   :     l = [] >   :

Re: [Tutor] Python best practices

2009-11-30 Thread spir
ALAN GAULD dixit: > But as I said earlier the concept of multiple return > values in Python is similar to returning a struct in C. > The key thing is that the function has a single purpose > so if returning multiple values or a struct the values > within that group should be related to the sin

Re: [Tutor] x is a global variable

2009-11-30 Thread Eike Welk
Hello Spir! On Monday 30 November 2009, spir wrote: > which seems to indicate python really embeds "symbolic references" > (*) to outer *variables*, when creating a closure for g0. Not > "pointer references" (**), otherwise the replacement of x would not > be seen by the closure --like in the case

Re: [Tutor] Equivalent exception of os.path.exists()

2009-11-30 Thread spir
biboy mendz dixit: > http://pastebin.ca/1693849 > > This is end-of-chapter3 exercise of the book Core Python Programming. > > I'm reading/searching in the book and other materials but is > unsuccessful. There are at least 50 exceptions listed but I can't find > anything close. > > I commente

Re: [Tutor] python closures

2009-11-30 Thread Eike Welk
On Monday 30 November 2009, Hugo Arts wrote: > Consider this python session: > >>> x = 0 > >>> def f(): > > x = x + 1 > > >>> f() > > Traceback (most recent call last): > File "", line 1, in > f() > File "", line 2, in f > x = x + 1 > UnboundLocalError: local variable

Re: [Tutor] python closures

2009-11-30 Thread Alan Gauld
"spir" wrote x = 1 def f(): n = 1 def g0(a): print (x + n + a) return g0 I'm surprised the snippet below works as expected (py 2.6) without any trick: I'm not sure how else it could work. x is a global name so the function must reference it. n is a local name so it musdt evaluate i

[Tutor] numerical simulation + SQLite

2009-11-30 Thread Faisal Moledina
Hey everyone, I have a general issue that I'd like to discuss. I'm using Python to run a numerical simulation where at each time step, I run a number of operations and store the results before moving to the next timestep. At first, I used a list to store a bunch of class instances, each of which c

Re: [Tutor] x is a global variable

2009-11-30 Thread spir
Hello Eike! Eike Welk dixit: > Hello Spir! > > On Monday 30 November 2009, spir wrote: > > which seems to indicate python really embeds "symbolic references" > > (*) to outer *variables*, when creating a closure for g0. Not > > "pointer references" (**), otherwise the replacement of x would not

Re: [Tutor] Equivalent exception of os.path.exists()

2009-11-30 Thread biboy mendz
spir wrote: What is your question? If it's about the type of exception raised when os.path.exists fails, well, sure it's hard to find: print os.path.exists("foo".bar) ==> False My question is: i'm looking for type of exception that more or less equivalent to os.path.exists attribute

Re: [Tutor] Equivalent exception of os.path.exists()

2009-11-30 Thread Tim Golden
biboy mendz wrote: Lie and Tim's input are true that raw_input doesnt do anything or you cant catch exception error from it. And i'm wrong in placing the try-except clause, it should be on the fobj-open line. But im still looking for the exception that will be raised when i input a filename an

Re: [Tutor] Equivalent exception of os.path.exists()

2009-11-30 Thread Dave Angel
spir wrote: biboy mendz dixit: http://pastebin.ca/1693849 This is end-of-chapter3 exercise of the book Core Python Programming. I'm reading/searching in the book and other materials but is unsuccessful. There are at least 50 exceptions listed but I can't find anything close. I comment

Re: [Tutor] python closures

2009-11-30 Thread spir
Eike Welk dixit: > On Monday 30 November 2009, Hugo Arts wrote: > > Consider this python session: > > >>> x = 0 > > >>> def f(): > > > > x = x + 1 > > > > >>> f() > > > > Traceback (most recent call last): > > File "", line 1, in > > f() > > File "", line 2, in f >

Re: [Tutor] Equivalent exception of os.path.exists()

2009-11-30 Thread Sander Sweers
2009/11/30 biboy mendz : > clause, it should be on the fobj-open line. But im still looking for the > exception that will be raised when i input a filename and that file already > exists. I hope you get what i mean :-) There is no exception to alert you a file already exists. Depending on how you

Re: [Tutor] Equivalent exception of os.path.exists()

2009-11-30 Thread spir
biboy mendz dixit: > > > spir wrote: > > > > What is your question? > > If it's about the type of exception raised when os.path.exists fails, well, > > sure it's hard to find: > > > > print os.path.exists("foo".bar) > > ==> False > > > > > My question is: i'm looking for type of excepti

Re: [Tutor] Equivalent exception of os.path.exists()

2009-11-30 Thread christopher . henk
biboy mendz wrote on 11/30/2009 03:04:52 PM: > > > spir wrote: > > > > What is your question? > > If it's about the type of exception raised when os.path.exists fails, well, sure it's hard to find: > > > > print os.path.exists("foo".bar) > > ==> False > > > > > My question is: i'm looking

Re: [Tutor] Equivalent exception of os.path.exists()

2009-11-30 Thread Alan Gauld
"Dave Angel" wrote My suspicion is that the author just wants you to try to do an open() (readonly), inside a try/except block, and to successfully handle the exception. And the critical thing here is that it must be an open in read mode. As others pointed out if you use write mode it wi

Re: [Tutor] numerical simulation + SQLite

2009-11-30 Thread Alan Gauld
"Faisal Moledina" wrote . At first, I used a list to store a bunch of class instances, each of which contained a bunch of data calculated at each time step. This resulted in whopping memory usage (2.75 GB RAM, 3.75 GB VM). So then I decided instead to use SQLite to store that information at eac

Re: [Tutor] numerical simulation + SQLite

2009-11-30 Thread Eike Welk
Hello Faisal! Just in case you don't know it, maybe Pytables is the right solution for you. It is a disk storage library specially for scientific applications: http://www.pytables.org/moin The makers claim, that it is fast. It has on the fly data compression which allegedly makes the library f

[Tutor] read in ascii and plot

2009-11-30 Thread questions anon
I would like to read in two columns of data from a *.txt file I type f=open("e:/testascii.txt") import pylab pylab.scatter(f) and then receive an error. How do I point it to each column and do I need to do anything about the space gap between the two columns? Thanks in advance. _

Re: [Tutor] python closures

2009-11-30 Thread Dave Angel
Alan Gauld wrote: "spir" wrote I did wonder if you would need n=n but I didn't think you would need x=x. Its an interesting example and I confess I don't fully understand how Python's naming/reference rules are working here. to let the inner func g0 "remember" outer values. Why is this id

Re: [Tutor] x is a global variable

2009-11-30 Thread Dave Angel
spir wrote: Hello Eike! Eike Welk dixit: Well, this is certainly not specific to closures. i = 0 def f(): i = i+1 print i f() ==> UnboundLocalError Imo, in this case, "i = i+1" is a kind of "paradoxal injonction" (lol! not sure of the exact idiom in english). You tell python both t

Re: [Tutor] read in ascii and plot

2009-11-30 Thread Wayne Werner
On Mon, Nov 30, 2009 at 5:55 PM, questions anon wrote: > I would like to read in two columns of data from a *.txt file > > I type > > f=open("e:/testascii.txt") > import pylab > pylab.scatter(f) > > and then receive an error. > How do I point it to each column and do I need to do anything about th

Re: [Tutor] numerical simulation + SQLite

2009-11-30 Thread bob gailer
Faisal Moledina wrote: Hey everyone, I have a general issue that I'd like to discuss. I'm using Python to run a numerical simulation where at each time step, I run a number of operations and store the results before moving to the next timestep. What do you do with the results after the simulat

Re: [Tutor] python closures

2009-11-30 Thread Kent Johnson
On Mon, Nov 30, 2009 at 7:50 PM, Dave Angel wrote: > And this > example could be made more complex if outer() is a generator, in which case > it may not have actually ended when inner gets called. Indeed. In [8]: def gen(): ...: for i in range(5): ...: def inner(): ...:

Re: [Tutor] read in ascii and plot

2009-11-30 Thread Kent Johnson
On Mon, Nov 30, 2009 at 8:26 PM, Wayne Werner wrote: > A sample of the data is always helpful, but I'll take a shot in the dark. > If you have data like this: > 2.31     72 > 98        23 > ...         > 34        7.32 > And those are x y pairs you could do something like this: > f = open('i

[Tutor] [Errno 9] Bad file descriptor

2009-11-30 Thread Khalid Al-Ghamdi
Hi everybody, I'm running python 2.6.1 on vista and I'm trying to use the csv module to write to a csv file and get the average of some numbers, but I keep getting the following error: Traceback (most recent call last): File "C:\Python31\MyCSVProjectFinal.py", line 83, in writer.writerow(h

[Tutor] does poplib have proxy support ?

2009-11-30 Thread Shashwat Anand
Does poplib/imaplib have proxy support like urllib? I was unable to get connected to my mail. Here is what i tried : >>> proxies = {'http': 'http://username:passw...@proxy:8080'} >>> host = 'pop.gmail.com' >>> me = 'usern...@gmail.com' >>> pass = '**' >>> pop = poplib.POP3_SSL(host) Traceback

Re: [Tutor] Equivalent exception of os.path.exists()

2009-11-30 Thread bibi midi
Thanks to all of you that replied. Your inputs are very helpful and informative. On Tue, Dec 1, 2009 at 12:14 AM, Sander Sweers wrote: > > There is no exception to alert you a file already exists. Depending on > how you open the file (mode) it will either read, write or append to > the file. If