Re: Is there a way to hook into module destruction?
On Jun 16, 11:03 pm, Gerald Kaszuba <[EMAIL PROTECTED]> wrote: > On Jun 17, 6:16 am, Neal Becker <[EMAIL PROTECTED]> wrote: > > > Code at global scope in a module is run at module construction (init). Is > > it possible to hook into module destruction (unloading)? > > Try the __del__ method. > > Seehttp://docs.python.org/ref/customization.htmlfor the docs. > > -- > Gerald Kaszubahttp://geraldkaszuba.com I doubt python calls __del__ when unloading module... and plus, I don't really think python does module unloading though. del module after import it got it removed from the global namespace but it does not clean reference I bet, so completely unloading a module should take more energy of that... Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: converting datetime object in UTC to local time
How about subclass datetime.tzinfo? That way you can use asttimezone to transfer utc to localtime. It requires an aware object though not naive. A bit more coding, but a lot less converting... Jim On Jul 3, 5:16 pm, Matt <[EMAIL PROTECTED]> wrote: > Hi all, > > So a lot of digging on doing this and still not a fabulous solution: > > import time > > # this takes the last_modified_date naivedatetime, converts it to a > # UTC timetuple, converts that to a timestamp (seconds since the > # epoch), subtracts the timezone offset (in seconds), and then > converts > # that back into a timetuple... Must be an easier way... > mytime = time.localtime(time.mktime(last_modified_date.utctimetuple()) > - time.timezone) > > lm_date_str = time.strftime("%m/%d/%Y %I:%M %p %Z", mytime) > > last_modified_date is a naivedatetime.datetimeobject > > A previous version gave me something like: > > mytime > =datetime.datetime.fromtimestamp(time.mktime(last_modified_date.utctimetuple()) > - time.timezone) > > lm_date_str = mytime.strftime("%m/%d/%Y %I:%M %p %Z") > > But this gave me no timezone since thedatetimeobject is still > naive. And I'm going from adatetimeto a timetuple to a timestamp > back to adatetime... > > All this seems like a lot of monkeying around to do something that > should be simple -- is there a simple way to do this without requiring > some other module? > > thx > > Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: what is wrong with that r"\"
Then you can use other chars as the delimiter, [EMAIL PROTECTED]@b@ or r!a!b!, etc... The import thing is so long as the interpreter doesn't get confused on the data and the delimiter. sed also allows for arbitrary delimiters too as long as you maintain the integrity of the original meaning... -Jim On Jul 6, 2007, at 5:20 AM, Tim Roberts wrote: > Matthieu TC <[EMAIL PROTECTED]> wrote: > >> May I suggest giving the possibility to use any delimiter for a >> raw string? just like in Vi or ruby. >> >> Vi: >> %s_a_b_g is valid and so is %s/a/b/g >> >> Ruby: >> %q{dj'\ks'a\'"} or %q-dj'\ks'a\'"- >> >> So as long as your regex does not use all the valid characters, >> readability is maintained. > > But what about my program that wants to use r_a_b_ as an identifier? > -- > Tim Roberts, [EMAIL PROTECTED] > Providenza & Boekelheide, Inc. > -- > http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Timing a python program run
simplest way is just put a timer on start and another on the end, then calc the elapse. You can also take a look timeit module too which provides similar but more powerful functions... -Jim On Jul 7, 2007, at 12:21 PM, David wrote: > Hi, > > In matlab, I'd calculate the time for a script named test.m to run > with: > >>> tic, run, toc > > Is there some way to do this in python on a mac os x from the terminal > window? Or whatever? > > -- > http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Execute commands from file
On May 16, 1:05 pm, Steve Holden <[EMAIL PROTECTED]> wrote: > Martin Blume wrote: > > "tmp123" schrieb > > >> We have very big files with python commands > >> (more or less, 50 commands each file). > > >> It is possible to execute them command by command, > > > inp = open(cmd_file) > > for line in inp: > > exec line > > > might help. You don't get quite the same feeling as > > "like if the commands was typed one after the other > > in a interactive session", but perhaps this helps. > > > Warning: the code above is without any error checks. > > You might also run into security problems, the example > > above assumes you trust your input. > > > HTH. YMMV. > > Martin > > The problem with this approach is that each line executes without any > connection to the environment created by previous lies. > > Try it on a file that reads something like > > xxx = 42 > print xxx > > and you will see NameError raised because the assignment hasn't affected > the environment for the print statement. > > regards > Steve > -- > Steve Holden+1 571 484 6266 +1 800 494 3119 > Holden Web LLC/Ltd http://www.holdenweb.com > Skype: holdenweb http://del.icio.us/steve.holden > -- Asciimercial - > Get on the web: Blog, lens and tag your way to fame!! > holdenweb.blogspot.comsquidoo.com/pythonology > tagged items: del.icio.us/steve.holden/python > All these services currently offer free registration! > -- Thank You for Reading cat file: x = 100 print x cat file.py: #!/usr/bin/python2.4 import os.path import sys file, ext = os.path.splitext(sys.argv[0]) f = open(file,'rb') for i in f: exec i >./file.py 100 Don't see the problem though. -- http://mail.python.org/mailman/listinfo/python-list
Re: Execute commands from file
On May 17, 3:02 am, Douglas Woodrow <[EMAIL PROTECTED]> wrote: > On Thu, 17 May 2007 00:30:23, i3dmaster <[EMAIL PROTECTED]> wrote > > >f = open(file,'rb') > >for i in f: > >exec i > > Why are you opening the file in binary mode? > > -- > Doug Woodrow 'b' is generally useful on systems that don't treat binary and text files differently. It will improve portability. -- http://mail.python.org/mailman/listinfo/python-list
unittest for threading function always failed...
I am having a little difficulty to figure out why this unittest for a Thread subclass always fails... # unittest code: class SPThreadUnitTest(unittest.TestCase): def testgetresult(self): from random import randint self.i = randint(1,10) def p(n): return n self.t = spthread.SPThread(target=p, args=(self.i)) self.t.start() #self.t._res = self.t._target(self.t._args) self.assertEquals(self.i,self.t.getresult()) #spthread.SPThread code: import threading class SPThread(threading.Thread): def __init__(self,target=None,args=None): threading.Thread.__init__(self) self._target = target self._args = args self._res = None def getresult(self): return self._res def run(self): self._res = self._target(self._args) A simple little test. But no matter what, the self._res didn't get any value but None, so the assertion of self.i and self.t.getresult() always fails. If I use the commented out code, it works. So the start() function has some tricky stuff? Can someone point me out where the problem is? Thanks, Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: unittest for threading function always failed...
On May 18, 4:13 pm, i3dmaster <[EMAIL PROTECTED]> wrote: > I am having a little difficulty to figure out why this unittest for a > Thread subclass always fails... > > # unittest code: > > class SPThreadUnitTest(unittest.TestCase): > > def testgetresult(self): > from random import randint > self.i = randint(1,10) > def p(n): return n > self.t = spthread.SPThread(target=p, args=(self.i)) > self.t.start() > #self.t._res = self.t._target(self.t._args) > self.assertEquals(self.i,self.t.getresult()) > > #spthread.SPThread code: > > import threading > class SPThread(threading.Thread): > > def __init__(self,target=None,args=None): > threading.Thread.__init__(self) > self._target = target > self._args = args > self._res = None > > def getresult(self): > return self._res > > def run(self): > self._res = self._target(self._args) > > A simple little test. But no matter what, the self._res didn't get any > value but None, so the assertion of self.i and self.t.getresult() > always fails. If I use the commented out code, it works. So the > start() function has some tricky stuff? Can someone point me out where > the problem is? > > Thanks, > Jim oh wft... I got it now. its the join() call... -- http://mail.python.org/mailman/listinfo/python-list
Re: Simple python iteration question
or use its builtin enumerate function: for i, j in enumerate(list): print i, j --Jim On Aug 14, 2007, at 9:26 AM, [EMAIL PROTECTED] wrote: > On Aug 14, 10:22 am, Bryan <[EMAIL PROTECTED]> wrote: >> Hi, >> >> I just started with python, and have a for loop question >> >> In c++ (or a number of other languages) I can do this: >> >> for (int i=0, j=0; i < i_len, j< j_len; ++i, ++j) {} >> >> If I have this in python: >> l = ['a', 'b', 'c'] >> >> I want to get the value and also an iterator: >> for i,v in len(l), l: >> print v >> print i >> >> Or something like this without declaring the iterator outside my >> loop... >> >> How do I do this? >> Thanks! > > this will get index and item at index, > > for i in range(0, len(l)): > print i > print l[i] > > > -- > http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Universal Feed Parser issue
I have a sample Atom feed like this: http://app.example.com/fjie4id939xdl3io23 foo bar [EMAIL PROTECTED] 2007-04-09T22:14:15.000Z After parsed by feedparser, the timezone element does not get the attribute "America/Mountain". Same thing on status element. This does not sound an expected result. I am wondering if it should be considered a bug... -- http://mail.python.org/mailman/listinfo/python-list
Re: Universal Feed Parser issue
On Apr 10, 6:45 pm, "Gabriel Genellina" <[EMAIL PROTECTED]> wrote: > En Tue, 10 Apr 2007 14:58:42 -0300, i3dmaster <[EMAIL PROTECTED]> > escribió: > > > I have a sample Atom feed like this: > > > > > ... > > > > > > > > > After parsed by feedparser, the timezone element does not get the > > attribute "America/Mountain". Same thing on status element. This does > > not sound an expected result. I am wondering if it should be > > considered a bug... > > Usually it's a good idea to read the documentation... > http://www.feedparser.org/docs/namespace-handling.html > > -- > Gabriel Genellina I did. Perhaps its because of not 100% atom compatible of my feed format? See if I use gnosis xml utility to parse it, it works fine though... -- http://mail.python.org/mailman/listinfo/python-list
Re: Universal Feed Parser issue
On Apr 11, 12:06 am, "Gabriel Genellina" <[EMAIL PROTECTED]> wrote: > En Wed, 11 Apr 2007 01:51:13 -0300, i3dmaster <[EMAIL PROTECTED]> > escribió: > > > > > On Apr 10, 6:45 pm, "Gabriel Genellina" <[EMAIL PROTECTED]> > > wrote: > >> En Tue, 10 Apr 2007 14:58:42 -0300, i3dmaster <[EMAIL PROTECTED]> > >> escribió: > > >> > > >> > ... > >> > > >> > > >> > > > >> > After parsed by feedparser, the timezone element does not get the > >> > attribute "America/Mountain". Same thing on status element. This does > >> > not sound an expected result. I am wondering if it should be > >> > considered a bug... > > >> Usually it's a good idea to read the documentation... > >>http://www.feedparser.org/docs/namespace-handling.html > > > I did. Perhaps its because of not 100% atom compatible of my feed > > format? See if I use gnosis xml utility to parse it, it works fine > > though... > > The link above answers *exactly* your question - have you actually read > the page? > > -- > Gabriel Genellina Hmm... well I don't know if I made my question clear then or you might have misread it?... Anyway, the namespace handling wasn't the issue of this question. feedparser handles it as expected but only when there is a text value associated with it. Look at the rdf file you will see the prism:issn element has a text value but in my case, the foo:timezone element DOES NOT have a text value but a Attribute or maybe attributes... What it looks like is feedparser simply ignores all the attributes when parsing it. This behavior wasn't seen from other xml parsers though. -- http://mail.python.org/mailman/listinfo/python-list
Re: Universal Feed Parser issue
On Apr 11, 12:06 am, "Gabriel Genellina" <[EMAIL PROTECTED]> wrote: > En Wed, 11 Apr 2007 01:51:13 -0300, i3dmaster <[EMAIL PROTECTED]> > escribió: > > > > > On Apr 10, 6:45 pm, "Gabriel Genellina" <[EMAIL PROTECTED]> > > wrote: > >> En Tue, 10 Apr 2007 14:58:42 -0300, i3dmaster <[EMAIL PROTECTED]> > >> escribió: > > >> > > >> > ... > >> > > >> > > >> > > > >> > After parsed by feedparser, the timezone element does not get the > >> > attribute "America/Mountain". Same thing on status element. This does > >> > not sound an expected result. I am wondering if it should be > >> > considered a bug... > > >> Usually it's a good idea to read the documentation... > >>http://www.feedparser.org/docs/namespace-handling.html > > > I did. Perhaps its because of not 100% atom compatible of my feed > > format? See if I use gnosis xml utility to parse it, it works fine > > though... > > The link above answers *exactly* your question - have you actually read > the page? > > -- > Gabriel Genellina Hmm... well I don't know if I made my question clear then or you might have misread it?... Anyway, the namespace handling wasn't the issue of this question. feedparser handles it as expected but only when there is a text value associated with it. Look at the rdf file you will see the prism:issn element has a text value but in my case, the foo:timezone element DOES NOT have a text value but a Attribute or maybe attributes... What it looks like is feedparser simply ignores all the attributes when parsing it. This behavior wasn't seen from other xml parsers though. -- http://mail.python.org/mailman/listinfo/python-list
Re: Universal Feed Parser issue
On Apr 11, 12:01 pm, "Gabriel Genellina" <[EMAIL PROTECTED]> wrote: > En Wed, 11 Apr 2007 14:07:15 -0300, i3dmaster <[EMAIL PROTECTED]> > escribió: > > > Hmm... well I don't know if I made my question clear then or you might > > have misread it?... Anyway, the namespace handling wasn't the issue > > of this question. feedparser handles it as expected but only when > > there is a text value associated with it. Look at the rdf file you > > will see the prism:issn element has a text value but in my case, the > > foo:timezone element DOES NOT have a text value but a Attribute or > > maybe attributes... What it looks like is feedparser simply ignores > > all the attributes when parsing it. This behavior wasn't seen from > > other xml parsers though. > > Oh, sorry! I thought you were complaining about the namespace. > You're out of luck with this parser - see my own reply on January for a > similar > issue:http://mail.python.org/pipermail/python-list/2007-January/422237.html > > -- > Gabriel Genellina Ok no problem. Back to my original question, should this be considered a bug (didn't see a bug reported though) ? Do you know if the developer is considering fixing it in the future? Thanks, Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: python safe scripting
On Nov 21, 12:44 pm, "Guilherme Polo" <[EMAIL PROTECTED]> wrote: > 2007/11/21, Vladimir Rusinov <[EMAIL PROTECTED]>: > > > Hello! > > > In one my project (it's logfile manager) I want to implement 'smart' > > configuration files, e.g. > > > logfile("/var/log/messages") > > if (size() > 10*1024*1024) and (lavg() < 5): > > execute("my_log_alerter") > >rotate(save=10, compress='bzip2') > > > how can I safely do this? > > > -- > > Vladimir Rusinov > > GreenMice Solutions: IT-ÒÅÛÅÎÉÑ ÎÁ ÂÁÚÅ Linux > > http://greenmice.info/ > > -- > >http://mail.python.org/mailman/listinfo/python-list > > logging already provides the rotating > > from logging.handlers import RotatingFileHandler > help(RotatingFileHandler) > > -- > -- Guilherme H. Polo Goncalves But the original RotatingFileHanlder does not offer compressing facility where the sample code that the author provides seems to indicate that he wants some sort of handling on that... Extends the class and override the doRollover method would probably do what you need. -- http://mail.python.org/mailman/listinfo/python-list
Re: Very basic, sorting a list ???
On Nov 28, 4:22 pm, stef mientki <[EMAIL PROTECTED]> wrote: > hello, > > I'm trying to sort a list, using the same list at the commandline works, > but in a program it doesn't. > > Here is the code > print 'xx1',type(ordered_list) > print 'xx2',ordered_list > print 'xx3',ordered_list.sort() > > And this is the result > xx1 > xx2 [14, 12, 10] > xx3 None > > What am I doing wrong ? > > thanks, > Stef Mientki sorted(alist) returns a copy of the original list with sorted order, that's probably what you really need. Yes, you should read documentations more. -- http://mail.python.org/mailman/listinfo/python-list
conditional install/distribution
Is there a feature in distutils or easy_install to specify what version of python that the target package can be installed? For example, if a package has a module that only needed if the python version < 2.6, is there a way to specifiy that in setup.py or easy_install cfg file so that when installing to python >= 2.6, this module wouldn't be installed?? Thanks, Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: conditional install/distribution
On Sep 11, 11:07 am, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote: > i3dmasterschrieb: > > > Is there a feature in distutils or easy_install to specify what > > version of python that the target package can be installed? For > > example, if a package has a module that only needed if the python > > version < 2.6, is there a way to specifiy that in setup.py or > > easy_install cfg file so that when installing to python >= 2.6, this > > module wouldn't be installed?? > > you can simply import sys and check sys.version in the setup-script, and > abort with an error-message if the expectations aren't matched. > > Diez I know I can precheck the version, but the real issue is how I can prevent from installing a specific module? Actually, the rest of the package should still be installed but just not a particular module because the feature is built in after 2.6. -- http://mail.python.org/mailman/listinfo/python-list
Re: httplib incredibly slow :-(
On Aug 12, 9:37 am, Chris Withers wrote: > David Stanek wrote: > > I tried to reproduce this, but I could not. Could you paste in the > > output of your script? > > Not sure how that'll help, but sure: > > 2009-08-11 21:27:59.153000 > request: 0:00:00.109000 > response: 0:00:00.109000 > read: 0:24:31.266000 > > > Also on the same box where you run this script > > > can you test with curl or wget? > > It's a Windows box, so no :-( > But it really does download in a few seconds with IE, and 20min+ using > the script I included... > > Chris > > -- > Simplistix - Content Management, Batch Processing & Python Consulting > -http://www.simplistix.co.uk Just wanted to check if you can try turning on the debug mode for httplib and see if you can read a bit more debug info on where the calls get hung. In your example, it would be conn.set_debuglevel(1) -- http://mail.python.org/mailman/listinfo/python-list