Re: py3k: converting int to bytes

2011-02-26 Thread J. Gerlach
Am 24.02.2011 17:19, schrieb s...@uce.gov:
> 
> Is there a better way to convert int to bytes then going through strings:
> 
> x=5
> str(x).encode()
> 
> 
> Thanks.
> 
>>> bytes([8])
b'\x08'
seems more straight forward...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: py3k: converting int to bytes

2011-02-26 Thread J. Gerlach
Am 26.02.2011 12:26, schrieb J. Gerlach:
> Am 24.02.2011 17:19, schrieb s...@uce.gov:
>>
>> Is there a better way to convert int to bytes then going through strings:
>>
>> x=5
>> str(x).encode()
>>
>>
>> Thanks.
>>
> >>> bytes([8])
> b'\x08'
> seems more straight forward...
... but it gives a different result. I should've tested before
answering, sorry.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: hashlib in one line

2010-11-29 Thread J. Gerlach
Am 29.11.2010 14:50, schrieb Thomas Guettler:
> Hi,
> 
> I think it would be nice if you could use the hashlib in one line:
> 
> hashlib.sha256().update('abc').hexdigest()
> 
> Unfortunately update() returns None.
> 
> Is there a way to convert a string to the hexdigest of sha256 in one line?
> 
>   Thomas
> 
> 
Yeah, something like this would be nice: ;)

hashlib.sha256('abc').hexdigest()

(tested in 2.6.6)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: group 0 in the re module

2010-12-08 Thread J. Gerlach
Am 08.12.2010 03:23, schrieb Yingjie Lan:
> Hi, 
> 
> According to the doc, group(0) is the entire match.
> 
 m = re.match(r"(\w+) (\w+)", "Isaac Newton, physicist") 
 m.group(0) # The entire match 'Isaac Newton' 
> 
> But if you do this:
 import re
 re.sub(r'(\d{3})(\d{3})', r'\0 to \1-\2', '757234')
> '\x00 to 757-234'
> 
> where I expected
> '757234 to 757-234'
> 
> Then I found that in python re '\0' is considered an octal number.
> So, is there anyway to refer to the entire match by an escaped
> notation?
> 
> Thanks,
> 
> Yingjie
> 
the documentation of the re module says:

>  \g uses the corresponding group number; \g<2> is
>  therefore equivalent to \2, but isn’t ambiguous in a replacement such
>  as \g<2>0. \20 would be interpreted as a reference to group 20, not a
>  reference to group 2 followed by the literal character '0'. The
>  backreference \g<0> substitutes in the entire substring matched by
>  the RE.

.. so you're looking for r"\g<0> to \1-\2"
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: "ImportError: No module named gobject"

2011-02-22 Thread J. Gerlach
Am 21.02.2011 16:04, schrieb Luther:
> I've tried installing pygtk, pygobject, and gobject-introspection from
> source, but none of them will compile, and nothing I install through
> synaptic has any effect.
> 
> I've tried too many things to post all the details here, but I'll post
> any details on request.
 Google says the package is called 'python-gobject' (Not that surprising
in itself, the name of most debian packages that are related to python
or that are written in python start with 'python-'.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PEP 249 (database api) -- executemany() with iterable?

2010-10-12 Thread J. Gerlach
Am 12.10.2010 17:10, schrieb Roy Smith:

> [A]re there any plans to update the api to allow an iterable instead of
> a sequence?

sqlite3 (standard library, python 2.6.6., Windows 32Bit) does that already::

import sqlite3 as sql

connection = sql.connect(":memory:")

cursor = connection.execute("""
CREATE TABLE test (
id INTEGER PRIMARY KEY AUTOINCREMENT,
text TEXT)
;""")
connection.commit()
cursor.executemany("""
INSERT INTO test (text) VALUES ( ? );
""",
# A generator expression - delivers one row at a time
( ("hello nr %03d!" % i,) for i in xrange(100)))
connection.commit()
cursor.execute("SELECT * FROM test")

for id_, text in cursor.fetchall():
print text, id_
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PEP 249 (database api) -- executemany() with iterable?

2010-10-13 Thread J. Gerlach
Am 13.10.2010 14:26, schrieb Jon Clements:
> On 12 Oct, 20:21, "J. Gerlach"  wrote:
>> Am 12.10.2010 17:10, schrieb Roy Smith:
>>
>>> [A]re there any plans to update the api to allow an iterable instead of
>>> a sequence?
>>
>> [sqlite3 example snipped]
> 
> What happens if you do itertools.repeat(0) instead of xrange(100) ?

And there I see why I don't want ``executemany()`` to take a generator
-- the never-ending ones will do some damage.

But on a similar note: would a ``execute_n_repeats(statement, iterable,
maximum_rows)`` be better?


Jörg

--
Have a nice day!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why "flat is better than nested"?

2010-11-05 Thread J. Gerlach
Am 28.10.2010 03:40, schrieb Steven D'Aprano:
> [ snip a lot of wise words ]

Can I put this (translated) in the german python wiki?
I guess it might help more people to understand some decisions taken
during python's development - and I'm to lazy to do something similar
myself ;)

Greetings from Berlin
Jörg Gerlach
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why "flat is better than nested"?

2010-11-06 Thread J. Gerlach
Am 06.11.2010 02:36, schrieb Steven D'Aprano:
> On Fri, 05 Nov 2010 14:19:47 +0100, J. Gerlach wrote:
> 
>> Am 28.10.2010 03:40, schrieb Steven D'Aprano:
>>> [ snip a lot of wise words ]
>>
>> Can I put this (translated) in the german python wiki? I guess it might
>> help more people to understand some decisions taken during python's
>> development - and I'm to lazy to do something similar myself ;)
> 
> 
> Sure, go ahead.
> 
> I would appreciate a link back to my post, and/or credit, but that's not 
> essential. (I understand it's a wiki and there's no guarantee that the 
> link will remain there forever.)
> 
> 
Thank you, and of course I gave you credit. I linked back to your post
via googlegroups.
-- 
http://mail.python.org/mailman/listinfo/python-list