Re: [Tutor] Encoding

2010-03-07 Thread Giorgio
2010/3/7 spir denis.s...@gmail.com


  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 into an image file:

filestream = file.read()
filetoput = filestream.encode('utf-8')

Do you think this is ok?

Oh, of course everything url-encoded then

Giorgio



 Denis
 --
 

 la vita e estrany

 spir.wikidot.com




-- 
--
AnotherNetFellow
Email: anothernetfel...@gmail.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[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,value) pairs. '''
print '*',
if self.holdsEntry:
yield (self.key,self.value)
for child in self.children:
print ,
child.__iter__()
print ,
raise StopIteration

With the debug prints in code above, for e in t: print e outputs:

* ('', 0)
   

print len(t.children) # -- 9

Why is no child.__iter__() executed at all? I imagine this can be caused by the 
special feature of a generator recording current execution point. (But then, is 
it at all possible to have a call in a generator? Or does the issue only appear 
whan the callee is a generator itself?) Else, the must be an obvious error in 
my code.


Denis
-- 


la vita e estrany

spir.wikidot.com

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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_generator(5)
 it.next()
5
 it.next()
4
 it.next()
3
 it.next()
2
 it.next()
1
 it.next()
Traceback (most recent call last):
  File stdin, line 1, in module
StopIteration


 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,value) pairs. '''
 print '*',
 if self.holdsEntry:
 yield (self.key,self.value)
 for child in self.children:
 print ,
 child.__iter__()
 print ,
 raise StopIteration


__iter__ should be an ordinary function, not a generator. Something like 
this should work:

# Untested.
def __iter__(self):
''' Iteration on (key,value) pairs. '''
def inner():
print '*',  # Side effects bad...
if self.holdsEntry:
yield (self.key,self.value)
for child in self.children:
print ,
child.__iter__()
print ,
raise StopIteration
return inner()


This means that your class won't *itself* be an iterator, but calling 
iter() on it will return a generator object, which of course is an 
iterator.

If you want to make your class an iterator itself, then you need to 
follow the iterator protocol. __iter__ must return the instance itself, 
and next must return (not yield) the next iteration.

class MyIterator(object):
def __init__(self, n):
self.value = n
def next(self):
if self.value == 0:
raise StopIteration
self.value //= 2
return self.value
def __iter__(self):
return self

See the discussion in the docs about the iterator protocol:

http://docs.python.org/library/stdtypes.html#iterator-types



 Why is no child.__iter__() executed at all? I imagine this can be
 caused by the special feature of a generator recording current
 execution point.

That's exactly what generators do: when they reach a yield, execution is 
halted, but the state of the generator is remembered. Then when you 
call the next() method, execution resumes.



 (But then, is it at all possible to have a call in a 
 generator? Or does the issue only appear whan the callee is a 
 generator itself?) Else, the must be an obvious error in my code.


I don't understand what you mean.


-- 
Steven D'Aprano
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Encoding

2010-03-07 Thread spir
On Sun, 7 Mar 2010 13:23:12 +0100
Giorgio anothernetfel...@gmail.com 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?

uContent = inFile.read().decode(***format***)
process, if any
outFile.write(uContent.encode('utf-8'))

input --decode-- process --encode-- output

This gives me an idea: when working with unicode, it would be cool to have an 
optional format parameter for file.read() and write. So, the above would be:

uContent = inFile.read(***format***)
process, if any
outFile.write(uContent, 'utf-8')

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?


denis
-- 


la vita e estrany

spir.wikidot.com

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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 puzzled why anyone would use the
regular open() for anything but binary operations.

Malcolm



- Original message -
From: spir denis.s...@gmail.com
To: Python tutor tutor@python.org
Date: Sun, 7 Mar 2010 14:29:11 +0100
Subject: Re: [Tutor] Encoding

On Sun, 7 Mar 2010 13:23:12 +0100
Giorgio anothernetfel...@gmail.com 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?

uContent = inFile.read().decode(***format***)
process, if any
outFile.write(uContent.encode('utf-8'))

input --decode-- process --encode-- output

This gives me an idea: when working with unicode, it would be cool to
have an optional format parameter for file.read() and write. So, the
above would be:

uContent = inFile.read(***format***)
process, if any
outFile.write(uContent, 'utf-8')

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?


denis
-- 


la vita e estrany

spir.wikidot.com

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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:
 print ,
 child.__iter__()
 print ,
 raise StopIteration



__iter__ should be an ordinary function, not a generator. Something like
this should work:

# Untested.
 def __iter__(self):
 ''' Iteration on (key,value) pairs. '''
 def inner():
 print '*',  # Side effects bad...
 if self.holdsEntry:
 yield (self.key,self.value)
 for child in self.children:
 print ,
 child.__iter__()
 print ,
 raise StopIteration
 return inner()


That's just an unnecessarily redundant variation on the above. It's 
perfectly ok if __iter__() is a generator method.


Stefan

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[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. __iter__ returns self, its own iterator via next()
5. __iter__ returns an external iterator object
6. __iter__ returns iter() of a collection built just on time
   (this one is really contrived)
Also, one can always traverse the collection (already existing or built then) 
itself if it not quasi-infinite (no __iter__ at all).

There should be one-- and preferably only one --obvious way to do it
http://www.python.org/dev/peps/pep-0020/ 

I understand some ways fit given use cases better -- or worse. But it's 
difficult to find the best one, or even a proper one in a given case. Also, 
generation and iteration are rather abstract notions. And it's too much variety 
for me. I am lost in this field.

I would enjoy a commented and examplified overview.


Denis
-- 


la vita e estrany

spir.wikidot.com

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[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 denis.s...@gmail.com 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 __iter__(self):
        ''' Iteration on (key,value) pairs. '''
        print '*',
        if self.holdsEntry:
            yield (self.key,self.value)
        for child in self.children:
            print ,
            child.__iter__()
            print ,
        raise StopIteration

 With the debug prints in code above, for e in t: print e outputs:

 * ('', 0)


 print len(t.children) # -- 9

 Why is no child.__iter__() executed at all? I imagine this can be caused by 
 the special feature of a generator recording current execution point. (But 
 then, is it at all possible to have a call in a generator? Or does the issue 
 only appear whan the callee is a generator itself?) Else, the must be an 
 obvious error in my code.


 Denis

remember that child.__iter__ returns a generator object. Merely
calling the function won't do anything. To actually get elements from
a generator object, you need to call next() on the returned iterator,
or iterate over it in another way (e.g. a for loop). I would write
this method more or less like so:

from itertools import chain

def __iter__(self):
   this_entry = [(self.key, self.value)] is self.holds_entry else []
   return chain(this_entry, *[iter(c) for c in self.children])

(Warning! untested! I'm pretty sure chain will work like this, but you
might have to do a little tweaking)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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__(self):
 return (pair(n) for n in self.items)

Seems perfectly acceptable to me. That's syntactic sugar for the next 
one:


 2. __iter__ *is* a generator
 def __iter__(self):
 for n in self.items:
 yield pair(n)
 raise StopIteration

As Stefan pointed out, __iter__ can be a generator, so that's okay too. 
However, the StopIteration at the end is redundant: generators 
automatically raise StopIteration when they fall off the end at the 
end of the code. So this is equivalent to the above:

def __iter__(self):
for n in self.items:
yield pair(n)


 3. __iter__ returns a generator 
(this one is a bit weird, i guess)
 def __iter__(self):
 return self.pairs()

There's nothing weird about it. It's the difference between writing code 
directly inline, and calling it indirectly:

def f():
return [1, 2, 3]

versus:

def indirect():
return [1, 2, 3]

def f():
return indirect()


If you have a good reason for the indirection, it is perfectly fine. 
E.g. you might have a class with multiple iterators:

class X:
def iter_width(self):
Iterate left-to-right
pass
def iter_depth(self):
Iterate top-to-bottom
pass
def iter_spiral(self):
pass
def __iter__(self):  # Default.
return self.iter_width()


 4. __iter__ returns self, its own iterator via next()
 def __iter__(self):
 self.i=0
 return self

That's how you would make the class an iterator directly.


 5. __iter__ returns an external iterator object
 def __iter__(self):
 return Iter(self)

Built-ins such as lists, dicts, tuples and sets use that strategy:

 iter([1,2,3])
listiterator object at 0xb7d08a4c
 iter(dict(a=1,b=2))
dictionary-keyiterator object at 0xb7d08fe0



 6. __iter__ returns iter() of a collection built just on time
(this one is really contrived)
 def __iter__(self):
 return iter(tuple([pair(n) for n in self.items]))

Not contrived, but inefficient.

First you build a list, all at once, using a list comprehension. So much 
for lazy iteration, but sometimes you have good reason for this (see 
below).

Then you copy everything in the list into a tuple. Why?

Then you create an iterator from the tuple.

If you remove the intermediate tuple, it is a reasonable approach for 
ensuring that you can modify the original object without changing any 
iterators made from it. In other words, __iter__ returns a *copy* of 
the data in self. But the easiest way to do that:

def __iter__(self):
return iter([pair(n) for n in self.items])

No need to make a tuple first.



 Also, one can always traverse the collection (already existing or
 built then) itself if it not quasi-infinite (no __iter__ at all).

The point of __iter__ is to have a standard way to traverse data 
structures, so you can traverse them with for-loops. Otherwise, every 
data structure needs a different method:

for item in tree.traverse():

for item in mapping.next_key():

for item in sequence.get_next_item():

for item in obj.walker():



 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 do it.



-- 
Steven D'Aprano
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] recursive generator

2010-03-07 Thread Rich Lovely
On 7 March 2010 12:58, spir denis.s...@gmail.com 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 __iter__(self):
        ''' Iteration on (key,value) pairs. '''
        print '*',
        if self.holdsEntry:
            yield (self.key,self.value)
        for child in self.children:
            print ,
            child.__iter__()
            print ,
        raise StopIteration

 With the debug prints in code above, for e in t: print e outputs:

 * ('', 0)


 print len(t.children) # -- 9

 Why is no child.__iter__() executed at all? I imagine this can be caused by 
 the special feature of a generator recording current execution point. (But 
 then, is it at all possible to have a call in a generator? Or does the issue 
 only appear whan the callee is a generator itself?) Else, the must be an 
 obvious error in my code.


 Denis
 --
 

 la vita e estrany

 spir.wikidot.com

 ___
 Tutor maillist  -  tu...@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor


You are calling child.__iter__(), but it's return value is being thrown away.

What you want to be doing is something like

def __iter__(self):
if self.holdsEntry:
yield self.entry
for child in self.children:
print 
for val in child: #implicit call to child.__iter__()
yield val
print 

Then, when the child.__iter__() is called, the returned iterator is
iterated, and the values are passed up the call stack.  There's
probably a more terse way of doing this using itertools, but I think
this is probably more readable.

Hope this clears things up (a little, anyway...)
-- 
Rich Roadie Rich Lovely

Just because you CAN do something, doesn't necessarily mean you SHOULD.
In fact, more often than not, you probably SHOULDN'T.  Especially if I
suggested it.

10 re-discover BASIC
20 ???
30 PRINT Profit
40 GOTO 10
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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 st...@pearwood.info 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 do it.


And in this case, if you consider the thing to do to be traversal of
some collection, the one obvious way to do it is through iteration.
So, the iterator protocol is a perfect example of this part of the
zen; the flexibility you have in implementing iteration is necessary
to allow it to be the one way.

Hugo
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] recursive generator

2010-03-07 Thread spir
On Sun, 7 Mar 2010 17:20:07 +0100
Hugo Arts hugo.yo...@gmail.com wrote:

 On Sun, Mar 7, 2010 at 1:58 PM, spir denis.s...@gmail.com 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 __iter__(self):
         ''' Iteration on (key,value) pairs. '''
         print '*',
         if self.holdsEntry:
             yield (self.key,self.value)
         for child in self.children:
             print ,
             child.__iter__()
             print ,
         raise StopIteration
 
  With the debug prints in code above, for e in t: print e outputs:
 
  * ('', 0)
 
 
  print len(t.children) # -- 9
 
  Why is no child.__iter__() executed at all? I imagine this can be caused by 
  the special feature of a generator recording current execution point. (But 
  then, is it at all possible to have a call in a generator? Or does the 
  issue only appear whan the callee is a generator itself?) Else, the must be 
  an obvious error in my code.
 
 
  Denis
 
 remember that child.__iter__ returns a generator object. Merely
 calling the function won't do anything. To actually get elements from
 a generator object, you need to call next() on the returned iterator,
 or iterate over it in another way (e.g. a for loop). I would write
 this method more or less like so:
 
 from itertools import chain
 
 def __iter__(self):
 this_entry = [(self.key, self.value)] is self.holds_entry else []
 return chain(this_entry, *[iter(c) for c in self.children])
 
 (Warning! untested! I'm pretty sure chain will work like this, but you
 might have to do a little tweaking)

@ Hugo  Steven
Thank you very much. I'll study your proposals as soon as I can, then tell you 
about the results.
In the meanwhile, I (recursively) constructed the collection of entries and 
returned iter(collection). [This works, indeed, but I also want do know how to 
properly build a recursive iterator.]

Denis
-- 


la vita e estrany

spir.wikidot.com

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Encoding

2010-03-07 Thread Dave Angel

Giorgio wrote:

2010/3/7 spir denis.s...@gmail.com

  
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 = filestream.encode('utf-8')

Do you think this is ok?

Oh, of course everything url-encoded then

Giorgio


  
Encoding binary data with utf-8 wouldn't make any sense, even if you did 
have the right semantics for a text file. 

Next problem, 'file' is a built-in keyword.  So if you write what you 
describe, you're trying to call a non-static function with a class 
object, which will error.



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.


If you're trying to store binary data in a repository that only permits 
text, it's not enough to pretend to convert it to UTF-8.  You need to do 
some other escaping, such as UUENCODE, that transforms the binary data 
into something resembling text.  Then you may or may not need to encode 
that text with utf-8, depending on its character set.



DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Encoding

2010-03-07 Thread Giorgio
2010/3/7 Dave Angel da...@ieee.org


 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, but i need to
store blobs.

Amazon's FAQs says that:

Q: What kind of data can I store?
You can store any UTF-8 string data in Amazon SimpleDB. Please refer
to the Amazon
Web Services Customer Agreement http://aws.amazon.com/agreement for
details.

This is the problem. Any idea?


 DaveA


Giorgio



-- 
--
AnotherNetFellow
Email: anothernetfel...@gmail.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[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)
#sys.stdout.write()

cursor.close ()
###END

The code as is will display the data to the console.  I have read the
db API 2.0 at python.org.  The problem is viewtable (db) will not work
or sys.stdout.write () to write the table data to the console screen.
What am I doing wrong here or what do I need to do different?

Can anyone point me to a book specificly for pgdb, python and postgre?
 Last thing is can I define the classes in a separate files and
reference them?  Kinda like code reuse in .Net?  I new to Python so
bear with me.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] pgdb and console output

2010-03-07 Thread Alan Gauld
hithere there pyli...@gmail.com 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 viewtable does, never seen it before...
The only case I found on Google was in a tutorial which defined 
viewtable() as a function, it wasn't in the pgdb module...



#sys.stdout.write()


This writes nothing to stdout. Apparently successfully from 
your comment below.


Try:

sys.stdout.write(str(i))


The code as is will display the data to the console.  I have read the
db API 2.0 at python.org.  The problem is viewtable (db) will not work
or sys.stdout.write () to write the table data to the console screen.
What am I doing wrong here or what do I need to do different?


The print is writing to the console I assume? That is the normal method.


Can anyone point me to a book specificly for pgdb, python and postgre?


It doesn't look lie you need a  postgres specific boook but just to 
go through the standard Python tutorial. It should make most things clear.



Last thing is can I define the classes in a separate files and
reference them?  Kinda like code reuse in .Net?  I new to Python so
bear with me.


Yes, just define them in a file then import that file as a module. 
The standard tutorial explains that too.


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/






___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Encoding

2010-03-07 Thread Dave Angel

Giorgio wrote:

2010/3/7 Dave Angel da...@ieee.org

  

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, but i need to
store blobs.

Amazon's FAQs says that:

Q: What kind of data can I store?
You can store any UTF-8 string data in Amazon SimpleDB. Please refer
to the Amazon
Web Services Customer Agreement http://aws.amazon.com/agreement for
details.

This is the problem. Any idea?


  

DaveA




Giorgio



  
You still didn't provide the full context.  Are you trying to do store 
binary data, or not?


Assuming you are, you could do the UUENCODE suggestion I made.  Or use 
base64:


base64.encodestring(/s/)   wlll turn binary data into (larger) binary 
data, also considered a string.  The latter is ASCII, so it's irrelevant 
whether it's considered utf-8 or otherwise.  You store the resulting 
string in your database, and use  base64.decodestring(s) to reconstruct 
your original.


There's 50 other ways, some more efficient, but this may be the simplest.

DaveA


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[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
printstep = 10
nextprint = 10

# for each event
if current_time  nextprint:
print 'time past: ' + str(nextprint)
nextprint += printstep

This seems stupid to me - why should I have to check each round if the time
has been passed? I was thinking that there might be a simpler way, using
threading, but I have zero experience with threading, so need your help. The
idea was to have a simple thread that waited X time (CPU cycles, say) and
then read the current_time variable and printed it.

Any simple (simpler?) solutions to this?

Thanks

Elisha
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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, every
 event that occurs follows the following code snippet:

It depends on how your system simulates the events and their
scheduling, but you might look at the sched module in the standard
library.  Maybe you would be able to use that to schedule your
simulated events and also schedule periodic time output too.


-- 
Steve Willoughby|  Using billion-dollar satellites
st...@alchemy.com   |  to hunt for Tupperware.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Encoding

2010-03-07 Thread Stefan Behnel

Giorgio, 05.03.2010 14:56:

What i don't understand is why:

s = uciao è 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 telling the function what the encoding is, i'm
using ASCII.

Is this a bug?


Did you read the Unicode tutorial at the link I posted? Here's the link again:

http://www.amk.ca/python/howto/unicode

Stefan

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[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 do this? I need to allocate a point structure and then a
coordinate structure and link them up together via the coord member of
point, fill them up with values and pass it to the fcntl.ioctl. The
actual IOCTL implementation will do the indirection to read out the
data.

   I can't use ctypes since I'm working on an embedded system with an ARM
core that doesn't have ctypes. The struct module doesn't seem to help
due to the indirection.

   Any ideas? I'm personally planning to write a small C extension that
creates the structure based on a specification in memory and then
passes that to the IOCTL call directly but that looks like some work. I
was also told to take a look at Cython which basically seems to be an
easy way of doing what I'm planning. Is there a quicker/easier way?

Thanks.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


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 that occurs follows the following code
 snippet:
[...]
 This seems stupid to me - why should I have to check each round if
 the time has been passed? I was thinking that there might be a
 simpler way, using threading, but I have zero experience with
 threading, so need your help. The idea was to have a simple thread
 that waited X time (CPU cycles, say) and then read the current_time
 variable and printed it.

 Any simple (simpler?) solutions to this?

That's a brilliant idea. I think I will steal it for my own code :)

(Why didn't I think of it myself? Probably because I almost never use 
threads...)

Anyway, here's something that should help:


import time
import threading

class Clock(threading.Thread):
def __init__(self, *args, **kwargs):
super(Clock, self).__init__(*args, **kwargs)
self.finished = False
def start(self):
print Clock %s started at %s % (self.getName(), time.ctime())
super(Clock, self).start()
def run(self):
while 1:
if self.finished:
break
print Clock %s still alive at %s % (
self.getName(), time.ctime())
time.sleep(2)
print Clock %s quit at %s % (self.getName(), time.ctime())
def quit(self):
print Clock %s asked to quit at %s % (
self.getName(), time.ctime())
self.finished = True

def do_work():
clock = Clock(name=clock-1)
clock.start()
# Start processing something hard.
for i in xrange(8):
print Processing %d... % i
# Simulate real work with a sleep.
time.sleep(0.75)
clock.quit()



And in action:

 do_work()
Clock clock-1 started at Mon Mar  8 17:40:42 2010
Processing 0...
Clock clock-1 still alive at Mon Mar  8 17:40:43 2010
Processing 1...
Processing 2...
Clock clock-1 still alive at Mon Mar  8 17:40:45 2010
Processing 3...
Processing 4...
Processing 5...
Clock clock-1 still alive at Mon Mar  8 17:40:47 2010
Processing 6...
Processing 7...
Clock clock-1 still alive at Mon Mar  8 17:40:49 2010
Clock clock-1 asked to quit at Mon Mar  8 17:40:49 2010
 Clock clock-1 quit at Mon Mar  8 17:40:51 2010




There's a bit of a display artifact in the interactive interpreter, when 
the final quit message is printed: the interpreter doesn't notice it 
needs to redraw the prompt. But other than that, it should be fine.


-- 
Steven D'Aprano
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor