Re: After migrating from debian to ubuntu, tkinter "hello world" doesn't work

2005-11-30 Thread Wade Leftwich

[EMAIL PROTECTED] wrote:
> Hi
>
> My tkinter apps worked fine in debian linux (woody and sarge)
> I moved to ubuntu 5.10
>
> I follow the 'hello world' test as seen in
> http://wiki.python.org/moin/TkInter
> 

Ubuntu uses X.org. Did your Debian distro use xfree86?

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


Re: Pythonic wrappers for SQL?

2006-01-16 Thread Wade Leftwich
Steve Holden wrote:
> >
> SQLObject is a very popuar object-relational mapper that works with a
> range of databases including SQLite.
>
> But SQL isn't actually that complicated to someone who's already learned
> Python! I maintain this page for students on the classes I teach, and it
> has some useful pointers:
>
>http://www.holdenweb.com/students/database.html
>
> regards
>   Steve
> --

With all the buzz about ORM these days, you don't hear much about good
old DBAPI. Maybe it's a matter of what's currently hip, or maybe DBAPI
has not achieved a consistent interface across adapters (e.g. ? vs %s
for placeholders). But it seems like to some extent the ORMs achieve
back end interchangeability by limiting what you can say.

Or maybe that's just my frustration because I am a beginner with
SQLObject, using it on a rewrite of an old (1999 or so) application
that has a lot of MSSQL-specific SQL expressed via mxODBC. I keep
catching myself working backward from what I want the SQL statement to
be, which is not very object-oriented on my part. I suppose it's a
matter of learning a new language, after a while you stop mentally
translating. However, I agree with the earlier comment that you'd
better be comfortable with SQL before you start trying to make sense of
an ORM, especially for data structures more complicated than you see in
a 20 minute screencast.

I am persevering with SQLObject, having faith that the extra work I'm
putting in to design my table classes will pay off in a more robust and
portable application. The module itself is elegant and well written,
and it uses metaclasses to boot.

-- Wade Leftwich
Ithaca, NY

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


Re: ElementTree and proper identation?

2006-09-27 Thread Wade Leftwich
John Salerno wrote:
> John Salerno wrote:
> > I've been doing a little studying of ElementTree and it doesn't seem
> > very satisfactory for writing XML files that are properly
> > formatted/indented. I saw on the website that there is an
> > indent/prettyprint function, but this isn't listed in the Python docs
> > and I didn't see it after doing a dir(), so I guess it isn't a part of
> > the Python version.
> >
> > Did I miss something somewhere else, or can you just not use ElementTree
> > to write formatted XML files as they'd look if done by hand?
> >
> > Thanks.

You can roll your own by starting at the root and recursing through
child elements, increasing the indentation as you go:

But I do it the lazy way:

$ xmllint --format ugly.xml >pretty.xml

-- Wade Leftwich
Ithaca, NY

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


Re: Iterate through list two items at a time

2007-01-03 Thread Wade Leftwich

Jeffrey Froman wrote:
> Dave Dean wrote:
>
> >  I'm looking for a way to iterate through a list, two (or more) items at a
> > time.
>
> Here's a solution, from the iterools documentation. It may not be the /most/
> beautiful, but it is short, and scales well for larger groupings:
>
> >>> from itertools import izip
> >>> def groupn(iterable, n):
> ... return izip(* [iter(iterable)] * n)
> ...
> >>> list(groupn(myList, 2))
> [(0, 1), (2, 3), (4, 5), (6, 7), (8, 9), (10, 11)]
> >>> list(groupn(myList, 3))
> [(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, 10, 11)]
> >>> list(groupn(myList, 4))
> [(0, 1, 2, 3), (4, 5, 6, 7), (8, 9, 10, 11)]
> >>> for a,b in groupn(myList, 2):
> ... print a, b
> ...
> 0 1
> 2 3
> 4 5
> 6 7
> 8 9
> 10 11
> >>>
>
> Jeffrey

This works great except you lose any 'remainder' from myList:

>>> list(groupn(range(10),3))
[(0, 1, 2), (3, 4, 5), (6, 7, 8)]  # did not include (9,)

The following might be more complex than necessary but it solves the
problem, and like groupn()
it works on infinite lists.

from itertools import groupby, imap
def chunk(it, n=0):
if n == 0:
return iter([it])
grouped = groupby(enumerate(it), lambda x: int(x[0]/n))
counted = imap(lambda x:x[1], grouped)
return imap(lambda x: imap(lambda y: y[1], x), counted)

>>> [list(x) for x in chunk(range(10), 3)]
[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]

Note the chunks are iterators, not tuples as in groupn():

>>> [x for x in chunk(range(10), 3)]
[,
 ,
 ,
 ]


-- Wade Leftwich
Ithaca, NY

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


Re: Iterate through list two items at a time

2007-01-04 Thread Wade Leftwich

Wade Leftwich wrote:
> Jeffrey Froman wrote:
> > Dave Dean wrote:
> >
> > >  I'm looking for a way to iterate through a list, two (or more) items at a
> > > time.
> >
> > Here's a solution, from the iterools documentation. It may not be the /most/
> > beautiful, but it is short, and scales well for larger groupings:
> >
> > >>> from itertools import izip
> > >>> def groupn(iterable, n):
> > ... return izip(* [iter(iterable)] * n)
> > ...
> > >>> list(groupn(myList, 2))
> > [(0, 1), (2, 3), (4, 5), (6, 7), (8, 9), (10, 11)]
> > >>> list(groupn(myList, 3))
> > [(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, 10, 11)]
> > >>> list(groupn(myList, 4))
> > [(0, 1, 2, 3), (4, 5, 6, 7), (8, 9, 10, 11)]
> > >>> for a,b in groupn(myList, 2):
> > ... print a, b
> > ...
> > 0 1
> > 2 3
> > 4 5
> > 6 7
> > 8 9
> > 10 11
> > >>>
> >
> > Jeffrey
>
> This works great except you lose any 'remainder' from myList:
>
> >>> list(groupn(range(10),3))
> [(0, 1, 2), (3, 4, 5), (6, 7, 8)]  # did not include (9,)
>
> The following might be more complex than necessary but it solves the
> problem, and like groupn()
> it works on infinite lists.
>
> from itertools import groupby, imap
> def chunk(it, n=0):
> if n == 0:
> return iter([it])
> grouped = groupby(enumerate(it), lambda x: int(x[0]/n))
>     counted = imap(lambda x:x[1], grouped)
> return imap(lambda x: imap(lambda y: y[1], x), counted)
>
> >>> [list(x) for x in chunk(range(10), 3)]
> [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]
>
> Note the chunks are iterators, not tuples as in groupn():
>
> >>> [x for x in chunk(range(10), 3)]
> [,
>  ,
>  ,
>  ]
>
>
> -- Wade Leftwich
> Ithaca, NY

Or, using generator expressions instead of imap and getting rid of the
lambdas --

from itertools import groupby

def chunk(it, n=0):
if n == 0:
return iter([it])
def groupfun((x,y)):
return int(x/n)
grouped = groupby(enumerate(it), groupfun)
counted = (y for (x,y) in grouped)
return ((z for (y,z) in x) for x in counted)

>>> [list(x) for x in chunk(range(10), 3)]
[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]

>>> [x for x in chunk(range(10), 3)]
[,
 ,
 ,
 ]

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


Re: Iterate through list two items at a time

2007-01-04 Thread Wade Leftwich
Peter Otten wrote:
> Wade Leftwich wrote:
>
> > from itertools import groupby
> >
> > def chunk(it, n=0):
> > if n == 0:
> > return iter([it])
> > def groupfun((x,y)):
> > return int(x/n)
> > grouped = groupby(enumerate(it), groupfun)
> > counted = (y for (x,y) in grouped)
> > return ((z for (y,z) in x) for x in counted)
> >
> >>>> [list(x) for x in chunk(range(10), 3)]
> > [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]
> >
> >>>> [x for x in chunk(range(10), 3)]
> > [,
> >  ,
> >  ,
> >  ]
>
> Note that all but the last of these generators are useless:
>
> >>> chunks = [x for x in chunk(range(10), 3)]
> >>> [list(x) for x in chunks]
> [[], [], [], [9]] # did you expect that?
> In [48]: chunkgen = chunk(range(10), 3)

In [49]: for x in chunkgen:
   : print list(x)
   :
   :
[0, 1, 2]
[3, 4, 5]
[6, 7, 8]
[9]

> Peter

That's an interesting gotcha that I've never run into when using this
function, because in practice I never put the generator returned by
chunk() inside a list comprehension.

In [51]: chunkgen = chunk(range(10), 3)

In [52]: [list(x) for x in chunkgen]
Out[52]: [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]

But, as you pointed out --

In [57]: chunkgen = chunk(range(10), 3)

In [58]: chunks = list(chunkgen)

In [59]: [list(x) for x in chunks]
Out[59]: [[], [], [], [9]]

So apparently when we list(chunkgen), we are exhausting the generators
that are its members. And if we do it again, we get rid of the [9] --

In [60]: [list(x) for x in chunks]
Out[60]: [[], [], [], []]

I'll admit that chunk() is needlessly tricky for most purposes. I wrote
it to accept a really lengthy, possibly unbounded, iterator and write
out 10,000-line files from it, and also to play with Python's new
functional capabilities.

-- Wade

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


Excellent sci-fi novel featuring Python

2007-11-17 Thread Wade Leftwich
I'm about halfway through Charles Stross' excellent new novel,
"Halting State". It's set in Edinburgh in the year 2018, and one of
the main characters is a game programmer whose primary language is
something called "Python 3000".

The cover features blurbs from William Gibson, Vernor Vinge, John
Carnack, and Bruce Scheier.

What, they couldn't pop for an advance copy for Guido?

-- Wade Leftwich
Ithaca, NY



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


Re: Excellent sci-fi novel featuring Python

2007-11-17 Thread Wade Leftwich
On Nov 17, 10:37 am, Wade Leftwich <[EMAIL PROTECTED]> wrote:
> I'm about halfway through Charles Stross' excellent new novel,
> "Halting State". It's set in Edinburgh in the year 2018, and one of
> the main characters is a game programmer whose primary language is
> something called "Python 3000".
>
> The cover features blurbs from William Gibson, Vernor Vinge, John
> Carnack, and Bruce Scheier.
>
> What, they couldn't pop for an advance copy for Guido?
>
> -- Wade Leftwich
> Ithaca, NY

Damn keyboard. The last two endorsers are John Carmack and Bruce
Schneier.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Aggregate funuctions broken in MySQLdb?

2006-05-14 Thread Wade Leftwich
Works fine for me, and I certainly hope MySQLdb is ready for prime
time, because I use the heck out of it. Maybe you're getting fooled by
the fact that cursor.execute() returns the count of result rows. To
actually see the result rows, you have to say cursor.fetchone() or
fetchall() --

In [34]: cur.execute("select article_id from articles limit 10")
Out[34]: 10L

In [35]: cur.fetchall()
Out[35]: ((3L,), (4L,), (5L,), (6L,), (7L,), (8L,), (9L,), (10L,),
(11L,), (12L,))

In [36]: cur.execute("select count(article_id) from articles where
article_id < 13")
Out[36]: 1L

In [37]: cur.fetchall()
Out[37]: ((10L,),)

In [38]: cur.execute("select sum(article_id) from articles where
article_id < 13")
Out[38]: 1L

In [39]: cur.fetchone()
Out[39]: (75.0,)

In [40]: cur.execute("select avg(article_id) from articles where
article_id < 13")
Out[40]: 1L

In [41]: cur.fetchone()
Out[41]: (7.5,)

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