Re: [Tutor] Printing time without "if" statement

2010-03-07 Thread Steven D'Aprano
On Mon, 8 Mar 2010 03:38:49 pm Elisha Rosensweig wrote: > Hi, > > I have an event-based simulator written in Python (of course). It > takes a while to run, and I want to have messages printed every so > often to the screen, indicating the simulation time that has passed. > Currently, every event th

[Tutor] Passing nested structures to fcntl.ioctl

2010-03-07 Thread Noufal Ibrahim
Hello everyone, I have some code that's calling fcntl.ioctl. I need to pass a nested structure as the 3rd argument of the function. Something like this typedef struct coordinates{ int x; int y; } coordinates; typedef struct point{ int amp; coordinates* coord; } point; How would I

Re: [Tutor] Encoding

2010-03-07 Thread Stefan Behnel
Giorgio, 05.03.2010 14:56: What i don't understand is why: s = u"ciao è ciao" is converting a string to unicode, decoding it from the specified encoding but t = "ciao è ciao" t = unicode(t) That should do exactly the same instead of using the specified encoding always assume that if i'm not te

Re: [Tutor] Printing time without "if" statement

2010-03-07 Thread Steve Willoughby
On Sun, Mar 07, 2010 at 11:38:49PM -0500, Elisha Rosensweig wrote: > Hi, > > I have an event-based simulator written in Python (of course). It takes a > while to run, and I want to have messages printed every so often to the > screen, indicating the simulation time that has passed. Currently, ever

[Tutor] Printing time without "if" statement

2010-03-07 Thread Elisha Rosensweig
Hi, I have an event-based simulator written in Python (of course). It takes a while to run, and I want to have messages printed every so often to the screen, indicating the simulation time that has passed. Currently, every event that occurs follows the following code snippet: # initilize printste

Re: [Tutor] Encoding

2010-03-07 Thread Dave Angel
Giorgio wrote: 2010/3/7 Dave Angel Those two lines don't make any sense by themselves. Show us some context, and we can more sensibly comment on them. And try not to use names that hide built-in keywords, or Python stdlib names. Hi Dave, I'm considering Amazon SimpleDB as an alte

Re: [Tutor] pgdb and console output

2010-03-07 Thread Alan Gauld
"hithere there" wrote #! /usr/bin/env python import pgdb, sys db = pgdb.connect (dsn='192.168.0.1:familydata', user='postgres', password='') cursor = db.cursor () cursor.execute ("select * from names") rows = cursor.fetchall () for i in (rows): print i #viewtable (db) No idea what

[Tutor] pgdb and console output

2010-03-07 Thread hithere there
OS = RHEL 5.4 32Bit Python = 2.4.3 #! /usr/bin/env python import pgdb, sys db = pgdb.connect (dsn='192.168.0.1:familydata', user='postgres', password='') cursor = db.cursor () cursor.execute ("select * from names") rows = cursor.fetchall () for i in (rows): print i #viewtable (db) #

Re: [Tutor] Encoding

2010-03-07 Thread Giorgio
2010/3/7 Dave Angel > > Those two lines don't make any sense by themselves. Show us some context, > and we can more sensibly comment on them. And try not to use names that > hide built-in keywords, or Python stdlib names. > Hi Dave, I'm considering Amazon SimpleDB as an alternative to PGSQL,

Re: [Tutor] Encoding

2010-03-07 Thread Dave Angel
Giorgio wrote: 2010/3/7 spir One more question: Amazon SimpleDB only accepts UTF8. So, let's say i have to put into an image file: Do you mean a binary file with image data, such as a jpeg? In that case, an emphatic - NO. not even close. filestream = file.read() filetoput = filestr

Re: [Tutor] recursive generator

2010-03-07 Thread spir
On Sun, 7 Mar 2010 17:20:07 +0100 Hugo Arts wrote: > On Sun, Mar 7, 2010 at 1:58 PM, spir wrote: > > Hello, > > > > Is it possible at all to have a recursive generator? I think at a iterator > > for a recursive data structure (here, a trie). The following code does not > > work: it only yields

Re: [Tutor] __iter__: one obvious way to do it

2010-03-07 Thread Hugo Arts
On Sun, Mar 7, 2010 at 5:37 PM, Steven D'Aprano wrote: >> "There should be one-- and preferably only one --obvious way to do >> it" http://www.python.org/dev/peps/pep-0020/ > > This doesn't mean that there should be *only* one way to do something. > It means that the should be one OBVIOUS way to d

Re: [Tutor] recursive generator

2010-03-07 Thread Rich Lovely
On 7 March 2010 12:58, spir wrote: > Hello, > > Is it possible at all to have a recursive generator? I think at a iterator > for a recursive data structure (here, a trie). The following code does not > work: it only yields a single value. Like if child.__iter__() were never > called. > >    def

Re: [Tutor] __iter__: one obvious way to do it

2010-03-07 Thread Steven D'Aprano
On Mon, 8 Mar 2010 02:07:41 am spir wrote: > [sorry, forgot the code] > > Hello, > > Below 6 working way to implement __iter__ for a container here > simulated with a plain inner list. Sure, the example is a bit > artificial ;-) > 1. __iter__ returns a generator _expression_ > def __iter__(sel

Re: [Tutor] recursive generator

2010-03-07 Thread Chris Fuller
Here's a (more or less) useful example. It generates the "nth triangular series" (my term), n=2 is the triangular numbers, n=3 the tetrahedral numbers (n has a correspondence to dimension). Each series is the sum of the elements of the previous one. They are also the columns of Pascal's Tria

[Tutor] recursive generator

2010-03-07 Thread Hugo Arts
sorry, forgot to forward this to the list. On Sun, Mar 7, 2010 at 1:58 PM, spir wrote: > Hello, > > Is it possible at all to have a recursive generator? I think at a iterator > for a recursive data structure (here, a trie). The following code does not > work: it only yields a single value. Like

Re: [Tutor] recursive generator

2010-03-07 Thread Steven D'Aprano
On Mon, 8 Mar 2010 01:49:21 am Stefan Behnel wrote: > Steven D'Aprano, 07.03.2010 14:27: > > __iter__ should be an ordinary function, not a generator. Something > > like this should work: [...] > That's just an unnecessarily redundant variation on the above. It's > perfectly ok if __iter__() is a g

[Tutor] __iter__: one obvious way to do it

2010-03-07 Thread spir
[sorry, forgot the code] Hello, Below 6 working way to implement __iter__ for a container here simulated with a plain inner list. Sure, the example is a bit artificial ;-) 1. __iter__ returns a generator _expression_ 2. __iter__ *is* a generator 3. __iter__ returns a generator (this one is a

[Tutor] __iter__: one obvious way to do it

2010-03-07 Thread spir
Hello, Below 6 working way to implement __iter__ for a container here simulated with a plain inner list. Sure, the example is a bit artificial ;-) 1. __iter__ returns a generator _expression_ 2. __iter__ *is* a generator 3. __iter__ returns a generator (this one is a bit weird, i guess) 4. __

Re: [Tutor] recursive generator

2010-03-07 Thread Stefan Behnel
Steven D'Aprano, 07.03.2010 14:27: On Sun, 7 Mar 2010 11:58:05 pm spir wrote: def __iter__(self): ''' Iteration on (key,value) pairs. ''' print '*', if self.holdsEntry: yield (self.key,self.value) for child in self.children: prin

Re: [Tutor] Encoding

2010-03-07 Thread python
> Or, maybe even better, the format could be given as third parameter of file > open(); then any read or write operation would directly convert from/to the > said format. What do you all think? See the codecs.open() command as an alternative to open(). With all the hassles of encoding, I'm puzz

Re: [Tutor] Encoding

2010-03-07 Thread spir
On Sun, 7 Mar 2010 13:23:12 +0100 Giorgio wrote: > One more question: Amazon SimpleDB only accepts UTF8. [...] > filestream = file.read() > filetoput = filestream.encode('utf-8') No! What is the content of the file? Do you think it can be a pure python representation of a unicode text? uConten

Re: [Tutor] recursive generator

2010-03-07 Thread Steven D'Aprano
On Sun, 7 Mar 2010 11:58:05 pm spir wrote: > Hello, > > Is it possible at all to have a recursive generator? Yes. >>> def recursive_generator(n): ... if n == 0: ... return ... yield n ... for i in recursive_generator(n-1): ... yield i ... >>> it = recursive_generato

[Tutor] recursive generator

2010-03-07 Thread spir
Hello, Is it possible at all to have a recursive generator? I think at a iterator for a recursive data structure (here, a trie). The following code does not work: it only yields a single value. Like if child.__iter__() were never called. def __iter__(self): ''' Iteration on (key,val

Re: [Tutor] Encoding

2010-03-07 Thread Giorgio
2010/3/7 spir > > > Oh, right. And, if i'm not wrong B is an UTF8 string decoded to unicode > (due > > to the coding: statement at the top of the file) and re-encoded to latin1 > > Si! :-) > Ahah. Ok, Grazie! One more question: Amazon SimpleDB only accepts UTF8. So, let's say i have to put int