no more reload() in py3k

2007-09-18 Thread timaranz
Hi all, this is possibly a python-dev question but I'll ask here
first.

Situation:
I work on an application that takes 10-20 seconds to startup plus
opening a document.  To avoid having to restart the entire application
each edit-debug cycle we rely heavily on the reload command. A typical
method in our command dispatcher looks like this:

def edit_something(self, something):
   reload(meshDialogModule)
   dlg = meshDialogModule.MeshDialog(self.app, something)
   dlg.run()

when the application is built into an exe the reload function is
redefined to lambda x: x.
The advantage of this is that every time I open a dialog my latest
changes are applied and the debug cycle is: edit code -> open dialog -
>
test -> close dialog -> edit code, which is many times faster than
restarting the app and opening a project each time.

Question:
I am told to use the exec() statement instead.  I don't see how I can
use exec to reload a module.

Cheers
Tim

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


Re: execfile and function call

2007-09-24 Thread timaranz
I'm not sure why it doesn't work, but a better way would be to use the
import statement:

import app_applscrip
app_applscript.createOptionList(applOptions)

Cheers
Tim

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


Re: Missing documentation for ElementTree?

2007-09-25 Thread timaranz
On Sep 26, 4:00 pm, "Gabriel Genellina" <[EMAIL PROTECTED]>
wrote:
> En Tue, 25 Sep 2007 19:39:33 -0300, Robert Dailey <[EMAIL PROTECTED]>
> escribi?:
>
> > for the _ElementInterface class, there is no documentation for most of
> > the
> > members of this class such as .tail and .text. There's a brief
> > description
> > of these things on the front page of the ElementTree docs but nothing
> > helpful. I have no idea what Tail is vs Text and I wanted to figure this
> > out
> > myself, however I don't think I can now. Anyone know if I'm just missing
> > something in the documentation or if it really isn't in there?
>
> Start here:http://www.effbot.org/zone/element-index.htm
>
> --
> Gabriel Genellina

Check out the development version of the documentation
http://docs.python.org/dev/library/xml.etree.elementtree.html#module-xml.etree.ElementTree

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


Re: Using closures and partial functions to eliminate redundant code

2007-09-26 Thread timaranz
On Sep 27, 2:01 pm, Matthew Wilson <[EMAIL PROTECTED]> wrote:
> I wrote some code to create a user and update a user on a remote box by
> sending emails to that remote box.  When I was done, I realized that my
> create_user function and my update_user function were effectively
> identical except for different docstrings and a single different value
> inside:
>
> ### VERSION ONE
>
> def create_user(username, userpassword, useremail):
> "Send an email that will create a user in the remote system."
>
> # Build email
> email_body = """
> USERNAME = %s
> USERPASSWORD = %s
> USEREMAIL = %s
> """ % (username, userpassword, useremail)
>
> # send it.
> send_email(subject="CREATE", body=email_body)
>
> def update_user(username, userpassword, useremail):
> "Send an email that will update a user's password in the remote 
> system."
>
> # Build email
> email_body = """
> USERNAME = %s
> USERPASSWORD = %s
> USEREMAIL = %s
> """ % (username, userpassword, useremail)
>
> # send it.
> send_email(subject="UPDATE", body=email_body)
>
> ### END
>
> Then I came up with this approach to avoid all that redundant text:
>
> ### VERSION TWO
>
> def _f(mode):
>
> if mode not in ("create", "update"):
> raise ValueError("mode must be create or update!")
>
> def _g(username, userpassword, useremail):
>
> # Build email
> email_body = """
> USERNAME = %s
> USERPASSWORD = %s
> USEREMAIL = %s
> """ % (username, userpassword, useremail)
>
> # send it.
> send_email(subject=mode.upper(), body=email_body)
>
> # Seems goofy, but other ways are there?
>
> docstrings = {'create': "Send an email that will create a user in the 
> remote system.",
>   'update': "Send an email that will update a user's 
> password in the remote system."}
>
> _g.__doc__ = docstrings[mode]
>
> return _g
>
> # Then I created my functions like this:
>
> v2_create_user = _f("create")
> v2_update_user = _f("update")
>
> ### END
>
> Finally, I came up with this approach:
>
> ### VERSION THREE
>
> from functools import partial
>
> def _h(mode, username, userpassword, useremail):
>
> if mode not in ("create", "update"):
> raise ValueError("mode must be create or update!")
>
> # Build email
> email_body = """
> USERNAME = %s
> USERPASSWORD = %s
> USEREMAIL = %s
> """ % (username, userpassword, useremail)
>
> # send it.
> send_email(subject=mode.upper(), body=email_body)
>
> # I can't figure out how to set up the docstring on these.
>
> v3_create_user = partial(_h, mode="create")
> v3_update_user = partial(_h, mode="update")
>
> ### END
>
> I'm interested to hear how other people deal with really similar code.
> The similarity just bugs me.  However, I wonder if using stuff like
> closures or partial function application is needlessly showy.
>
> Also, I hope anyone here can help me figure out how to attach a
> meaningful docstring for my version three code.
>
> Thanks in advance!
>
> Matt

Without using partials, I would do something like

def _create_or_update_user(mode, username, userpassword, useremail)
   blah, blah, blah

def update_user(username, userpassword, useremail):
   'update doc string'
   return _create_or_update_user('update', username, userpassword,
useremail)

def create_user(username, userpassword, useremail):
   'create doc string'
   return _create_or_update_user('create', username, userpassword,
useremail)

Cheers
Tim

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


Re: developing an application

2007-09-27 Thread timaranz
On Sep 28, 10:06 am, yadin <[EMAIL PROTECTED]> wrote:
> hi!
> i was buiding an application using python...a program
> this was my first one...now that i got it working perfectly
> how can i put the bunch of files into one package?
> the user of the appliation will not know where to start? that is which
> file to click on in oder to start the program?
> thanks a lot

Check out py2exe and distutils

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


Re: GUI for viewing/editing python data structures?

2007-09-30 Thread timaranz
On Sep 27, 11:23 am, "Joshua J. Kugler" <[EMAIL PROTECTED]> wrote:
> A while back, I seem to remember coming across a small program that could
> view and edit python data structures via a nice expanding tree view.  I'm
> now in need of something like that (to verify data is imported correctly
> into a shelve file) and having a GUI would be much simpler than trying to
> wade through the output of str(d) or repr(d).
>
> I've tried googling with the obvious keywords (gui (view OR edit) python
> data structures) but t didn't get me anywhere.
>
> Pointers?
>
> j
>
> --
> Joshua Kugler
> Lead System Admin -- Senior Programmerhttp://www.eeinternet.com
> PGP Key:http://pgp.mit.edu/ ID 0xDB26D7CE

Try any of the good Python IDE's and use breakpoints and the watch
window. I like wing IDE from wingware.com - they have a free version
now.

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


Re: Convert on uppercase unaccentent unicode character

2007-10-03 Thread timaranz
On Oct 4, 7:35 am, JBJ  wrote:
> Hi,
> I'am very newbie in Python.
> For the moment I'am trying to convert an unicode character to his uppercase
> unaccented character.
> By example with locale fr_FR:
> a,A,à,À should return A
> o,O,ô,Ô should return O
> ½,¼ should return ¼
> i,I,î,Î should return I
>
> Have you some suggestions ?
>
> Thank.

Unicode strings have an upper() method - try that.  I'm think it
should work properly with your locale - it doesn't give the expected
result for me with an english locale.



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


Re: Boolean parser..

2007-10-04 Thread timaranz
On Oct 5, 7:29 am, Abandoned <[EMAIL PROTECTED]> wrote:
> Hi..
> I try a boolean parser in python since 1 weak.. But i can't do this
> because this is very complicated :(
> Do you know any blooean parser script in python or how do i write a
> boolean parser ?
> example query: ((google or yahoo) or (live msn)) not web
> I'm sorry my bad english.
> King Regards..

Try the pyparsing module (google it)
They have several examples.

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


Re: Mixing Python and C classes in a module

2007-10-09 Thread timaranz
On Oct 10, 3:32 pm, "Nicholas Bastin" <[EMAIL PROTECTED]> wrote:
> On 10/9/07, Chris Mellon <[EMAIL PROTECTED]> wrote:
>
>
>
> > On 09 Oct 2007 16:56:30 +0200, Stefan Arentz <[EMAIL PROTECTED]> wrote:
>
> > > Is it possible to mix classes defined in both Python and C in the same
> > > module? Ideally I would like to be able to do:
>
> > >  from some.module import MyPythonClass, MyCClass
>
> > > I guess that would mean that this would look like this on disk:
>
> > >  some/
> > >__init__.py
> > >module.py  (contains MyPythonClass)
> > >module.so  (contains MyCClass)
>
> > > But would this work?
>
> > No, you'll need to make module a package, and import from (differently
> > named) implementation packages.
>
> Nah, you can do it, just not in this way.  You can either futz with
> ihooks, or name module.py something like _module.py, import it into C,
> and then re-export as 'module', after attaching all your C types.
>
> --
> Nick

It is easier to do it the other way around.
Create module.py and _module.so and in module.py write:

from _module.so import *

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


decorating container types (Python 2.4)

2007-10-11 Thread timaranz
Hi,

I have a container class A and I want to add functionality to it by
using a decorator class B, as follows:

class A(object):
def __len__(self):
return 5

class B(object):
def __init__(self, a):
self._a = a

def __getattr__(self, attr):
return getattr(self._a, attr)

def other_methods(self):
blah blah blah

I was expecting len(B) to return 5 but I get
AttributeError: type object 'B' has no attribute '__len__'
instead.
I was expecting len() to call B.__len__() which would invoke
B.__getattr__ to call A.__len__ but __getattr__ is not being called.
I can work around this, but I am curious if anyone knows _why_
__getattr__ is not being called in this situation.

Thanks
Tim

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


Re: decorating container types (Python 2.4)

2007-10-11 Thread timaranz
On Oct 12, 12:19 pm, James Stroud <[EMAIL PROTECTED]> wrote:
> [EMAIL PROTECTED] wrote:
> > Hi,
>
> > I have a container class A and I want to add functionality to it by
> > using a decorator class B, as follows:
>
> > class A(object):
> > def __len__(self):
> > return 5
>
> > class B(object):
> > def __init__(self, a):
> > self._a = a
>
> > def __getattr__(self, attr):
> > return getattr(self._a, attr)
>
> > def other_methods(self):
> > blah blah blah
>
> > I was expecting len(B) to return 5 but I get
> > AttributeError: type object 'B' has no attribute '__len__'
> > instead.
> > I was expecting len() to call B.__len__() which would invoke
> > B.__getattr__ to call A.__len__ but __getattr__ is not being called.
> > I can work around this, but I am curious if anyone knows _why_
> > __getattr__ is not being called in this situation.
>
> > Thanks
> > Tim
>
> The why part is that __len__ is an unbound method of the class, not an
> unbound method of the class's metaclass. Also, your code above makes
> absolutely no association between classes A and B, which is the most
> fundamental reason.
>
> James
>
> --
> James Stroud
> UCLA-DOE Institute for Genomics and Proteomics
> Box 951570
> Los Angeles, CA  90095
>
> http://www.jamesstroud.com

Just so I'm clear on this: Are you saying that the problem with my
original code is that len() is equivalent to type(x).__len__(x) that
looks for a class attribute rather than an instance attribute as my
code requires?

I don't own class A and they are generated by function calls rather
than instantiated directly, that's why I'm decorating them instead of
inheriting.  The crux of the (academic) question is why does len()
expect '__len__' to be a class attribute instead of an instance
attribute, and why is this a good idea?

Cheers
Tim

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


Re: remove header line when reading/writing files

2007-10-11 Thread timaranz
On Oct 12, 12:23 pm, Tim Chase <[EMAIL PROTECTED]> wrote:
> Forgot the enumerate call of all things
>
> > for zipfile in filelist:
> >for i, line in enumerate(gzip.Gzipfile(zipfile,'r')):
> >if i: outfile.write(line)
>
> Some days, I'm braindead.
>
> -tkc

I would move the 'if' test outside the loop :

for zipfile in filelist:
zfiter = iter(gzip.Gzipfile(zipfile,'r'))
zfiter.next()  # ignore header line
for i, line in enumerate(fziter):
outfile.write(line)

I'm not sure if the iter(...) is required.  This will raise a
StopIteration exception if zipfile is empty.

Cheers
Tim

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


Re: Problem with format string / MySQL cursor

2007-10-18 Thread timaranz
On Oct 19, 7:32 am, Florian Lindner <[EMAIL PROTECTED]> wrote:
> Hello,
> I have a string:
>
> INSERT INTO mailboxes (`name`, `login`, `home`, `maildir`, `uid`,
> `gid`, `password`) VALUES (%s, %s, %s, %s, %i, %i, %s)
>
> that is passed to a MySQL cursor from MySQLdb:
>
> ret = cursor.execute(sql, paras)
>
> paras is:
>
> ('flindner', '[EMAIL PROTECTED]', '/home/flindner/', '/home/
> flindner/Mail/test', 1001, 1001, '123')
>
> But that gives me an error:
>
> Traceback (most recent call last):
>   File "account.py", line 188, in ?
> main()
>   File "account.py", line 33, in main
> execute(action, account_type, options)
>   File "account.py", line 129, in execute
> executeSQL(sql, options.username, options.login, options.home,
> options.directory, options.uid, options.gid, options.password)
>   File "/home/flindner/common.py", line 29, in executeSQL
> ret = cursor.execute(sql, paras)
>   File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line
> 148, in execute
> query = query % db.literal(args)
> TypeError: int argument required
>
> I don't see errors in the format string or some other problem
>
> What's wrong?
>
> Thanks,
>
> Florian

You should be using '?' for parameter bindings in your sql string not
python format specifiers (you are after all writing sql here not
python).

INSERT INTO mailboxes (`name`, `login`, `home`, `maildir`, `uid`,
`gid`, `password`) VALUES (?, ?, ?, ?, ?, ?, ?)

Cheers
Tim


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


Re: problem with Python class creating

2007-10-18 Thread timaranz
On Oct 19, 8:22 am, dmitrey <[EMAIL PROTECTED]> wrote:
> Hi all,
> I have the code like this one:
>
> from myMisc import ooIter
> class MyClass:
> def __init__(self): pass
> iterfcn = lambda *args: ooIter(self) # i.e pass the class instance
> to other func named ooIter
> field2 = val2
> field3 = val3 # etc
>
> So it yields "global name 'self' is not defined", that is true. How
> could I handle the situation?
>
> Currently I do (and it works, but give me some troubles - I should
> call MyClass.__init__ for each children class, and there are lots of
> those ones)
>
> class MyClass:
> def __init__(self):
> iterfcn = lambda *args: ooIter(self) # i.e pass the class
> instance to other func named ooIter
> field2 = val2
> field3 = val3 # etc
>
> I suspect it has better solution, is it?
> Thank you in advance, Dmitrey

without having tested - I think this should work for you:

from myMisc import ooIter

class MyClass:
 def __init__(self): pass
 iterfcn = lambda self: ooIter(self)

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