From soumen08 at gmail.com Fri May 1 00:00:08 2009 From: soumen08 at gmail.com (Soumen banerjee) Date: Fri, 1 May 2009 09:30:08 +0530 Subject: wxPython having trouble with frame objects In-Reply-To: References: <5ddd5a7f-8c2b-4f39-a89e-376a3d646caf@t36g2000prt.googlegroups.com> Message-ID: Hello, Another thing, here i tried changing self.frame_1 to guithread.frame_1, so that that part of the code now reads:- import wx,gui,threading class guithread(threading.Thread): def run(self): app = wx.PySimpleApp(0) wx.InitAllImageHandlers() guithread.frame_1 = gui.MyFrame(None, -1, "") app.SetTopWindow(guithread.frame_1) guithread.frame_1.Show() app.MainLoop() gui1=guithread() gui1.start() class changer(threading.Thread): def run(self): gui1.frame_1.text_ctrl_1.Setvalue("hello") chang=changer() chang.start() I still get guithread object has no attribute frame_1. I must be doing something wrong On Fri, May 1, 2009 at 9:27 AM, Soumen banerjee wrote: > Hello, > you say that ?frame_1 is an attribute of the main class. The main > class here is guithread right? so any instance of guithread should > also have an attribute called frame_1 isnt it? Excuse me if im getting > this wrong, since i am somewhat new to python. > Regards > Soumen > > On Fri, May 1, 2009 at 9:05 AM, CM wrote: >> On Apr 30, 9:54?pm, Soumen banerjee wrote: >>> Hello, >>> I am using wxglade to design a gui which i am using in another script. >>> Here are the codes >>> >>> The main file: >>> import wx,gui,threading >>> class guithread(threading.Thread): >>> ? ?def run(self): >>> ? ? ? ?app = wx.PySimpleApp(0) >>> ? ? ? ?wx.InitAllImageHandlers() >>> ? ? ? ?self.frame_1 = gui.MyFrame(None, -1, "") >>> ? ? ? ?app.SetTopWindow(self.frame_1) >>> ? ? ? ?self.frame_1.Show() >>> ? ? ? ?app.MainLoop() >>> gui1=guithread() >>> gui1.start() >>> class changer(threading.Thread): >>> ? ?def run(self): >>> ? ? ? ?gui1.frame_1.text_ctrl_1.Setvalue("hello") >>> chang=changer() >>> chang.start() >>> >>> and The GUI file (gui.py, imported in the above) >>> import wx >>> >>> # begin wxGlade: extracode >>> # end wxGlade >>> >>> class MyFrame(wx.Frame): >>> ? ?def __init__(self, *args, **kwds): >>> ? ? ? ?# begin wxGlade: MyFrame.__init__ >>> ? ? ? ?kwds["style"] = wx.DEFAULT_FRAME_STYLE >>> ? ? ? ?wx.Frame.__init__(self, *args, **kwds) >>> ? ? ? ?self.text_ctrl_1 = wx.TextCtrl(self, -1, "") >>> ? ? ? ?self.slider_1 = wx.Slider(self, -1, 0, 0, 10) >>> ? ? ? ?self.Open = wx.Button(self, -1, "Open") >>> ? ? ? ?self.button_4 = wx.Button(self, -1, "Pause/Resume") >>> ? ? ? ?self.button_5 = wx.Button(self, -1, "Quit") >>> >>> ? ? ? ?self.__set_properties() >>> ? ? ? ?self.__do_layout() >>> >>> ? ? ? ?self.Bind(wx.EVT_COMMAND_SCROLL, self.slider, self.slider_1) >>> ? ? ? ?self.Bind(wx.EVT_BUTTON, self.open, self.Open) >>> ? ? ? ?self.Bind(wx.EVT_BUTTON, self.pause, self.button_4) >>> ? ? ? ?self.Bind(wx.EVT_BUTTON, self.quit, self.button_5) >>> ? ? ? ?# end wxGlade >>> >>> ? ?def __set_properties(self): >>> ? ? ? ?# begin wxGlade: MyFrame.__set_properties >>> ? ? ? ?self.SetTitle("frame_1") >>> ? ? ? ?self.SetSize((522, 457)) >>> ? ? ? ?# end wxGlade >>> >>> ? ?def __do_layout(self): >>> ? ? ? ?# begin wxGlade: MyFrame.__do_layout >>> ? ? ? ?sizer_1 = wx.BoxSizer(wx.VERTICAL) >>> ? ? ? ?sizer_2 = wx.BoxSizer(wx.VERTICAL) >>> ? ? ? ?sizer_3 = wx.BoxSizer(wx.HORIZONTAL) >>> ? ? ? ?sizer_2.Add(self.text_ctrl_1, 7, wx.EXPAND, 0) >>> ? ? ? ?sizer_2.Add(self.slider_1, 0, wx.EXPAND, 0) >>> ? ? ? ?sizer_3.Add(self.Open, 0, wx.LEFT, 70) >>> ? ? ? ?sizer_3.Add((52, 20), 0, 0, 0) >>> ? ? ? ?sizer_3.Add(self.button_4, 0, wx.ALIGN_CENTER_HORIZONTAL, 0) >>> ? ? ? ?sizer_3.Add((55, 23), 0, 0, 0) >>> ? ? ? ?sizer_3.Add(self.button_5, 0, 0, 0) >>> ? ? ? ?sizer_2.Add(sizer_3, 1, wx.EXPAND, 0) >>> ? ? ? ?sizer_1.Add(sizer_2, 1, wx.EXPAND, 0) >>> ? ? ? ?self.SetSizer(sizer_1) >>> ? ? ? ?self.Layout() >>> ? ? ? ?# end wxGlade >>> >>> ? ?def slider(self, event): # wxGlade: MyFrame. >>> ? ? ? ?print "Event handler `slider' not implemented!" >>> ? ? ? ?event.Skip() >>> >>> ? ?def open(self, event): # wxGlade: MyFrame. >>> ? ? ? ?print "Event handler `open' not implemented!" >>> ? ? ? ?event.Skip() >>> >>> ? ?def pause(self, event): # wxGlade: MyFrame. >>> ? ? ? ?print "Event handler `pause' not implemented!" >>> ? ? ? ?event.Skip() >>> >>> ? ?def quit(self, event): # wxGlade: MyFrame. >>> ? ? ? ?print "Event handler `quit' not implemented!" >>> ? ? ? ?event.Skip() >>> >>> # end of class MyFrame >>> >>> if __name__ == "__main__": >>> ? ?app = wx.PySimpleApp(0) >>> ? ?wx.InitAllImageHandlers() >>> ? ?frame_1 = MyFrame(None, -1, "") >>> ? ?app.SetTopWindow(frame_1) >>> ? ?frame_1.Show() >>> ? ?app.MainLoop() >>> >>> The problem here is that when i run the main file, i am told that >>> 'guithread' object has no attribute 'frame_1' whereas i seem to have >>> defined >>> self.frame_1=gui.MyFrame etc. >> >> Your statement above means that self--that is, the instance of >> your main class--has an attribute called frame_1, and that name >> refers to an instance of the MyFrame class from the gui module. >> >> It does not mean that the guithread object has an attribute named >> frame_1. ?In order to do that, you should have written: >> >> guithread.frame_1 = something >> >>> The idea here is to access a gui element running in a thread from a >>> separate thread. Please help >> >> I would post wxPython related questions on the wxPython mailing >> list, which is excellent. >> http://www.wxpython.org/maillist.php >> >> HTH, >> Che >> -- >> http://mail.python.org/mailman/listinfo/python-list >> > From ldo at geek-central.gen.new_zealand Fri May 1 00:30:19 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Fri, 01 May 2009 16:30:19 +1200 Subject: Why bool( object )? References: <54cb7f8a-fef4-4bf8-8054-16dc9b5c8830@d2g2000pra.googlegroups.com> <49f85861$0$29010$426a74cc@news.free.fr> <4t_Jl.37718$VA5.20343@tornado.fastwebnet.it> Message-ID: In message , Steven D'Aprano wrote: > The reason why Lawrence's insistence is so badly wrong becomes more > apparent if you look at what you can do with boolean contexts other than > simple `if` tests. Compare: > > > for x in a or b or c: > do_something_with(x) > > > versus: > > > if len(a) > 0: > temp = a > elif len(b) > 0: > temp = b > elif len(c) > 0: > temp = c > for x in temp: > do_something_with(x) I have never written anything so unbelievable in my life. And I hope I never will. From davea at ieee.org Fri May 1 00:52:52 2009 From: davea at ieee.org (Dave Angel) Date: Fri, 01 May 2009 00:52:52 -0400 Subject: wxPython having trouble with frame objects In-Reply-To: References: <5ddd5a7f-8c2b-4f39-a89e-376a3d646caf@t36g2000prt.googlegroups.com> Message-ID: <49FA8024.3060608@ieee.org> Soumen banerjee wrote: > Hello, > you say that frame_1 is an attribute of the main class. The main > class here is guithread right? so any instance of guithread should > also have an attribute called frame_1 isnt it? Excuse me if im getting > this wrong, since i am somewhat new to python. > Regards > Soumen > > On Fri, May 1, 2009 at 9:05 AM, CM wrote: > >> On Apr 30, 9:54 pm, Soumen banerjee wrote: >> >>> Hello, >>> I am using wxglade to design a gui which i am using in another script. >>> Here are the codes >>> >>> The main file: >>> import wx,gui,threading >>> class guithread(threading.Thread): >>> def run(self): >>> app =x.PySimpleApp(0) >>> wx.InitAllImageHandlers() >>> self.frame_1 =ui.MyFrame(None, -1, "") >>> app.SetTopWindow(self.frame_1) >>> self.frame_1.Show() >>> app.MainLoop() >>> gui1=ithread() >>> gui1.start() >>> class changer(threading.Thread): >>> def run(self): >>> gui1.frame_1.text_ctrl_1.Setvalue("hello") >>> chang=anger() >>> chang.start() >>> >>> and The GUI file (gui.py, imported in the above) >>> import wx >>> >>> # begin wxGlade: extracode >>> # end wxGlade >>> >>> class MyFrame(wx.Frame): >>> def __init__(self, *args, **kwds): >>> # begin wxGlade: MyFrame.__init__ >>> kwds["style"] =x.DEFAULT_FRAME_STYLE >>> wx.Frame.__init__(self, *args, **kwds) >>> self.text_ctrl_1 =x.TextCtrl(self, -1, "") >>> self.slider_1 =x.Slider(self, -1, 0, 0, 10) >>> self.Open =x.Button(self, -1, "Open") >>> self.button_4 =x.Button(self, -1, "Pause/Resume") >>> self.button_5 =x.Button(self, -1, "Quit") >>> >>> self.__set_properties() >>> self.__do_layout() >>> >>> self.Bind(wx.EVT_COMMAND_SCROLL, self.slider, self.slider_1) >>> self.Bind(wx.EVT_BUTTON, self.open, self.Open) >>> self.Bind(wx.EVT_BUTTON, self.pause, self.button_4) >>> self.Bind(wx.EVT_BUTTON, self.quit, self.button_5) >>> # end wxGlade >>> >>> def __set_properties(self): >>> # begin wxGlade: MyFrame.__set_properties >>> self.SetTitle("frame_1") >>> self.SetSize((522, 457)) >>> # end wxGlade >>> >>> def __do_layout(self): >>> # begin wxGlade: MyFrame.__do_layout >>> sizer_1 =x.BoxSizer(wx.VERTICAL) >>> sizer_2 =x.BoxSizer(wx.VERTICAL) >>> sizer_3 =x.BoxSizer(wx.HORIZONTAL) >>> sizer_2.Add(self.text_ctrl_1, 7, wx.EXPAND, 0) >>> sizer_2.Add(self.slider_1, 0, wx.EXPAND, 0) >>> sizer_3.Add(self.Open, 0, wx.LEFT, 70) >>> sizer_3.Add((52, 20), 0, 0, 0) >>> sizer_3.Add(self.button_4, 0, wx.ALIGN_CENTER_HORIZONTAL, 0) >>> sizer_3.Add((55, 23), 0, 0, 0) >>> sizer_3.Add(self.button_5, 0, 0, 0) >>> sizer_2.Add(sizer_3, 1, wx.EXPAND, 0) >>> sizer_1.Add(sizer_2, 1, wx.EXPAND, 0) >>> self.SetSizer(sizer_1) >>> self.Layout() >>> # end wxGlade >>> >>> def slider(self, event): # wxGlade: MyFrame. >>> print "Event handler `slider' not implemented!" >>> event.Skip() >>> >>> def open(self, event): # wxGlade: MyFrame. >>> print "Event handler `open' not implemented!" >>> event.Skip() >>> >>> def pause(self, event): # wxGlade: MyFrame. >>> print "Event handler `pause' not implemented!" >>> event.Skip() >>> >>> def quit(self, event): # wxGlade: MyFrame. >>> print "Event handler `quit' not implemented!" >>> event.Skip() >>> >>> # end of class MyFrame >>> >>> if __name__ ="__main__": >>> app =x.PySimpleApp(0) >>> wx.InitAllImageHandlers() >>> frame_1 =yFrame(None, -1, "") >>> app.SetTopWindow(frame_1) >>> frame_1.Show() >>> app.MainLoop() >>> >>> The problem here is that when i run the main file, i am told that >>> 'guithread' object has no attribute 'frame_1' whereas i seem to have >>> defined >>> self.frame_1=i.MyFrame etc. >>> >> Your statement above means that self--that is, the instance of >> your main class--has an attribute called frame_1, and that name >> refers to an instance of the MyFrame class from the gui module. >> >> It does not mean that the guithread object has an attribute named >> frame_1. In order to do that, you should have written: >> >> guithread.frame_1 =omething >> >> >>> The idea here is to access a gui element running in a thread from a >>> separate thread. Please help >>> >> I would post wxPython related questions on the wxPython mailing >> list, which is excellent. >> http://www.wxpython.org/maillist.php >> >> HTH, >> Che >> -- >> http://mail.python.org/mailman/listinfo/python-list >> >> Don't top-post. It puts things entirely out of order. Now the order of the parts of this message are 3, 1, 2, 4 Two things at least are wrong here, either of which is fatal. 1) you have two threads doing GUI stuff. Can't be done, at least not in wxPython. Certain things can be done in a second thread, but definitely not constructing Frames and such. 2) you have no synchronization between the two threads you start, so the changer thread may very well get to its usage of frame_1 long before guithread assigns it. This is probably the cause of your current symptom. Now, you could perhaps "fix" it by assigning it something in the guithread constructor (you don't define one), but although that'd get you further, problem #1 is fatal. From rcdailey at gmail.com Fri May 1 01:09:15 2009 From: rcdailey at gmail.com (Robert Dailey) Date: Thu, 30 Apr 2009 22:09:15 -0700 (PDT) Subject: subprocess & shared environments Message-ID: <8c79f749-e648-4a2e-afad-4053a8ec5cbd@f41g2000pra.googlegroups.com> I'm currently calling subprocess.call() on a batch file (in Windows) that sets a few environment variables that are needed by further processes started via subprocess.call(). How can I persist the environment modifications by the first call() function? I've done my own searching on this and I came up with nothing. Help is appreciated. From soumen08 at gmail.com Fri May 1 01:12:43 2009 From: soumen08 at gmail.com (Soumen banerjee) Date: Fri, 1 May 2009 10:42:43 +0530 Subject: wxPython having trouble with frame objects In-Reply-To: <49FA8024.3060608@ieee.org> References: <5ddd5a7f-8c2b-4f39-a89e-376a3d646caf@t36g2000prt.googlegroups.com> <49FA8024.3060608@ieee.org> Message-ID: Hello, Im not adding any GUI elements from the changer thread. Im just attempting to change the value of a preexisting widget made in thread one. The point is that i need to display text generated in thread 2 in a GUI generated in thread 1. As far as inter thread synchronization is concerned, i see your point. So i took this approach import wx,gui,threading fix=0 i=1 class guithread(threading.Thread): def run(self): global fix app = wx.PySimpleApp(0) wx.InitAllImageHandlers() guithread.frame_1 = gui.MyFrame(None, -1, "") app.SetTopWindow(guithread.frame_1) guithread.frame_1.Show() print "setting fix" fix=1 app.MainLoop() gui1=guithread() gui1.start() class changer(threading.Thread): def run(self): print fix gui1.frame_1.text_ctrl_1.SetValue("hello") print fix while i: if fix == 1: print "starting changer" chang=changer() chang.start() i=0 This works, but is there anything simpler? Regards Soumen From cs at zip.com.au Fri May 1 01:22:26 2009 From: cs at zip.com.au (Cameron Simpson) Date: Fri, 1 May 2009 15:22:26 +1000 Subject: Multiprocessing.Queue - I want to end. In-Reply-To: <20090430223744.GA26332@cskk.homeip.net> Message-ID: <20090501052226.GA6860@cskk.homeip.net> On 01May2009 08:37, I wrote: | On 30Apr2009 22:57, MRAB wrote: | > The producer could send just one None to indicate that it has finished | > producing. | > Each consumer could get the data from the queue, but if it's None then | > put it back in the queue for the other consumer, then clean up and | > finish. | > When all the consumers have finished, the queue will contain just the | > single None. | | And as it happens I have an IterableQueue class right here which does | _exact_ what was just described. You're welcome to it if you like. | Added bonus is that, as the name suggests, you can use the class as | an iterator: | for item in iterq: | ... | The producer calls iterq.close() when it's done. Someone asked, so code appended below. Cameron Simpson DoD#743 http://www.cskk.ezoshosting.com/cs/ class IterableQueue(Queue): ''' A Queue obeying the iterator protocol. Note: Iteration stops when a None comes off the Queue. ''' def __init__(self, *args, **kw): ''' Initialise the queue. ''' Queue.__init__(self, *args, **kw) self.__closed=False def put(self, item, *args, **kw): ''' Put an item on the queue. ''' assert not self.__closed, "put() on closed IterableQueue" assert item is not None, "put(None) on IterableQueue" return Queue.put(self, item, *args, **kw) def _closeAtExit(self): if not self.__closed: self.close() def close(self): ##logFnLine("%s.close()"%(self,),frame=sys._getframe(1)) if self.__closed: # this should be a real log message print >>sys.stderr, "close() on closed IterableQueue" else: self.__closed=True Queue.put(self,None) def __iter__(self): ''' Iterable interface for the queue. ''' return self def next(self): item=self.get() if item is None: Queue.put(self,None) # for another iterator raise StopIteration return item From st1999 at gmail.com Fri May 1 01:26:44 2009 From: st1999 at gmail.com (robean) Date: Thu, 30 Apr 2009 22:26:44 -0700 (PDT) Subject: urllib2 and threading Message-ID: I am writing a program that involves visiting several hundred webpages and extracting specific information from the contents. I've written a modest 'test' example here that uses a multi-threaded approach to reach the urls with urllib2. The actual program will involve fairly elaborate scraping and parsing (I'm using Beautiful Soup for that) but the example shown here is simplified and just confirms the url of the site visited. Here's the problem: the script simply crashes after getting a a couple of urls and takes a long time to run (slower that a non-threaded version that I wrote and ran). Can anyone figure out what I am doing wrong? I am new to both threading and urllib2, so its possible that the SNAFU is quite obvious. The urls are stored in a text file that I read from. The urls are all valid, so there's no problem there. Here's the code: #!/usr/bin/python import urllib2 import threading class MyThread(threading.Thread): """subclass threading.Thread to create Thread instances""" def __init__(self, func, args): threading.Thread.__init__(self) self.func = func self.args = args def run(self): apply(self.func, self.args) def get_info_from_url(url): """ A dummy version of the function simply visits urls and prints the url of the page. """ try: page = urllib2.urlopen(url) except urllib2.URLError, e: print "**** error ****", e.reason except urllib2.HTTPError, e: print "**** error ****", e.code else: ulock.acquire() print page.geturl() # obviously, do something more useful here, eventually page.close() ulock.release() ulock = threading.Lock() num_links = 10 threads = [] # store threads here urls = [] # store urls here fh = open("links.txt", "r") for line in fh: urls.append(line.strip()) fh.close() # collect threads for i in range(num_links): t = MyThread(get_info_from_url, (urls[i],) ) threads.append(t) # start the threads for i in range(num_links): threads[i].start() for i in range(num_links): threads[i].join() print "all done" From smichr at gmail.com Fri May 1 02:13:54 2009 From: smichr at gmail.com (smichr) Date: Thu, 30 Apr 2009 23:13:54 -0700 (PDT) Subject: Modifying the value of a float-like object References: <87iql6v9cr.fsf@benfinney.id.au> <142274a6-a8f5-49ae-8b99-46d4dbe31246@e21g2000yqb.googlegroups.com> Message-ID: On Apr 18, 4:21 pm, Eric.Le.Bi... at spectro.jussieu.fr wrote: > On Apr 15, 5:33 pm, Arnaud Delobelle wrote: > > I adjusted your code in a few ways, and put the result athttp://code.activestate.com/recipes/576721/(with due credit): > > 1) There was a strange behavior, which is fixed (by performing only > dynamic calculations of UExpr, with an optimization through a new > "constant" class): > Although normal arithmetic operations are correlated, the correlation is lost (AFAICT) as soon as you use a function: >>> a=Number_with_uncert(3,1);y=a+cos(a);y.error 1.0099083406911706 >>> #check it manually >>> def f(a): ... return a+cos(a) ... >>> h=1e-5;print (f(3+h)-f(3))/h 0.858884941879 >>> #check it analytically >>> 1-sin(3) 0.85887999194013276 It's effectively creating a new variable, tmp=cos(a) so y looks like a +tmp and the uncertainty in this is (a.error**2 + tmp.error**2)**.5: >>> a object with result 3.0 +- 1 >>> cos(a) object with result -0.9899924966 +- 0.14112000806 >>> (1+0.14112000806**2)**.5 1.009908340729422 That result we got when requesting the error in y We can also see that the a inside the cos() is invisible by taking the derivative wrt a >>> y.derivative_value(a) 0.99999999996214217 Also, this approach is limited in that only variables can be arguments to functions, not node expressions. If x and d are variables, x/d becomes an expression and you cannot compute sin(x/d). Or am I missing something? /c From arnodel at googlemail.com Fri May 1 02:24:42 2009 From: arnodel at googlemail.com (Arnaud Delobelle) Date: Fri, 01 May 2009 07:24:42 +0100 Subject: import and package confusion References: <20090429193621.GO32190@vnl.com> <49F8B4B1.4040705@Acm.Org> <20090429203403.GA4324@vnl.com> <49F8CF55.30705@Acm.Org> <_rOdnY_h8uTTQWXUnZ2dnUVZ_gKdnZ2d@pdx.net> <20090430001954.GC8373@vnl.com> <20090430063231.GI9749@nexus.in-nomine.org> <20090430140557.GA31415@vnl.com> Message-ID: Dale Amon writes: > Now I can move on to parsing those pesky Fortran card > images... There wouldn't happen to be a way to take n > continguous slices from a string (card image) where each > slice may be a different length would there? Fortran you > know. No spaces between input fields. :-) > > I know a way to do it, iterating over a list of slice sizes, > perhaps in a list comprehension, but some of the august python > personages here no doubt know better ways. It's a challenge to do it in a list comprehension, but here it is! Warning: unpythonic code follows(*). Sensitive readers may want to look away now. >>> data 'AAABBBBBCCCCCCCCCDD' >>> field_sizes [3, 5, 9, 2] >>> [data[i:j] for j in [0] for s in field_sizes for i, j in [(j, j+s)]] ['AAA', 'BBBBB', 'CCCCCCCCC', 'DD'] >>> Now you can look again. (*) Pythonicity is often much debated as it lies for a good part in the eye of the snake charmer. I don't think this snippet would generate much debate! -- Arnaud From arnodel at googlemail.com Fri May 1 02:28:51 2009 From: arnodel at googlemail.com (Arnaud Delobelle) Date: Fri, 01 May 2009 07:28:51 +0100 Subject: list comprehension question References: <6d502cfd-e252-4722-96a5-c52fc3579499@v1g2000prd.googlegroups.com> Message-ID: Ross writes: > If I have a list of tuples a = [(1,2), (3,4), (5,6)], and I want to > return a new list of each individual element in these tuples, I can do > it with a nested for loop but when I try to do it using the list > comprehension b = [j for j in i for i in a], my output is b = > [5,5,5,6,6,6] instead of the correct b = [1,2,3,4,5,6]. What am I > doing wrong? When writing nested list comprehension, the for loops are in the same order as you would write a normal nested for loop (which is not necessarily intuitive when you first find out but is very handy in the long run I think). So write: [j for i in a for j in i] -- Arnaud From wuwei23 at gmail.com Fri May 1 03:00:29 2009 From: wuwei23 at gmail.com (alex23) Date: Fri, 1 May 2009 00:00:29 -0700 (PDT) Subject: import and package confusion References: <20090429193621.GO32190@vnl.com> <49F8B4B1.4040705@Acm.Org> <20090429203403.GA4324@vnl.com> <49F8CF55.30705@Acm.Org> <_rOdnY_h8uTTQWXUnZ2dnUVZ_gKdnZ2d@pdx.net> <20090430001954.GC8373@vnl.com> <20090430063231.GI9749@nexus.in-nomine.org> <20090430140557.GA31415@vnl.com> Message-ID: <11bee2fa-5a39-4f17-98d3-a6c1d5780b5d@s38g2000prg.googlegroups.com> On May 1, 4:24?pm, Arnaud Delobelle wrote: > It's a challenge to do it in a list comprehension, but here it is! > >>> data > 'AAABBBBBCCCCCCCCCDD' > >>> field_sizes > [3, 5, 9, 2] > >>> [data[i:j] for j in [0] for s in field_sizes for i, j in [(j, j+s)]] > ['AAA', 'BBBBB', 'CCCCCCCCC', 'DD'] Ugh. I know using a listcomp was the point but I find using struct so much more elegant. >>> import struct >>> data = 'AAABBBBBCCCCCCCCCDD' >>> format = '3s5s9s2s' >>> struct.unpack(format, data) ('AAA', 'BBBBB', 'CCCCCCCCC', 'DD') Friends don't let friends abuse list comprehensions. I made sure the code works this time :) From smichr at gmail.com Fri May 1 03:19:43 2009 From: smichr at gmail.com (smichr) Date: Fri, 1 May 2009 00:19:43 -0700 (PDT) Subject: Modifying the value of a float-like object References: <87iql6v9cr.fsf@benfinney.id.au> <142274a6-a8f5-49ae-8b99-46d4dbe31246@e21g2000yqb.googlegroups.com> Message-ID: <269a0614-ffd4-4012-a9a0-e6ec0735348b@b7g2000pre.googlegroups.com> On May 1, 11:13 am, smichr wrote: > Also, this approach is limited in that only variables can be arguments > to functions, not node expressions. If x and d are variables, x/d > becomes an expression and you cannot compute sin(x/d). Or am I missing > something? > Note to self...it's best to restart with a fresh session if you've been making modifications. everything appears to work fine without any of the above observed problems: >>> a=Number_with_uncert(3,1);y=a+cos(a);y.error 0.85888494187891229 >>> b=Number_with_uncert(4,1) >>> cos(a/b) object with result 0.731688868874 +- 0.213012112511 Sorry for the false alarm, /c From http Fri May 1 03:22:22 2009 From: http (Paul Rubin) Date: 01 May 2009 00:22:22 -0700 Subject: Why bool( object )? References: <54cb7f8a-fef4-4bf8-8054-16dc9b5c8830@d2g2000pra.googlegroups.com> <49f85861$0$29010$426a74cc@news.free.fr> <4t_Jl.37718$VA5.20343@tornado.fastwebnet.it> Message-ID: <7xvdol1e4x.fsf@ruckus.brouhaha.com> Steven D'Aprano writes: > for x in a or b or c: > do_something_with(x) Ugh!!!! for x in [a,b,c]: if len(x) > 0: do_something_with(x) break From yaksavage at gmail.com Fri May 1 03:26:46 2009 From: yaksavage at gmail.com (thor) Date: Fri, 1 May 2009 00:26:46 -0700 (PDT) Subject: list comprehension question References: <6d502cfd-e252-4722-96a5-c52fc3579499@v1g2000prd.googlegroups.com> Message-ID: On May 1, 2:28?pm, Arnaud Delobelle wrote: > Ross writes: > > If I have a list of tuples a = [(1,2), (3,4), (5,6)], and I want to > > return a new list of each individual element in these tuples, I can do > > it with a nested for loop but when I try to do it using the list > > comprehension b = [j for j in i for i in a], my output is b = > > [5,5,5,6,6,6] instead of the correct b = [1,2,3,4,5,6]. What am I > > doing wrong? > > When writing nested list comprehension, the for loops are in the same > order as you would write a normal nested for loop (which is not > necessarily intuitive when you first find out but is very handy in the > long run I think). > > So write: > > ? ? [j for i in a for j in i] > > -- > Arnaud an trick >>> a [(1, 2), (3, 4), (5, 6)] >>> sum(a, ()) (1, 2, 3, 4, 5, 6) >>> you may search the maillist , somebody questioned before From http Fri May 1 03:27:01 2009 From: http (Paul Rubin) Date: 01 May 2009 00:27:01 -0700 Subject: urllib2 and threading References: Message-ID: <7xr5z91dx6.fsf@ruckus.brouhaha.com> robean writes: > reach the urls with urllib2. The actual program will involve fairly > elaborate scraping and parsing (I'm using Beautiful Soup for that) but > the example shown here is simplified and just confirms the url of the > site visited. Keep in mind Beautiful Soup is pretty slow, so if you're doing a lot of pages and have multiple cpu's, you probably want parallel processes rather than threads. > wrong? I am new to both threading and urllib2, so its possible that > the SNAFU is quite obvious.. > ... > ulock = threading.Lock() Without looking at the code for more than a few seconds, using an explicit lock like that is generally not a good sign. The usual Python style is to send all inter-thread communications through Queues. You'd dump all your url's into a queue and have a bunch of worker threads getting items off the queue and processing them. This really avoids a lot of lock-related headache. The price is that you sometimes use more threads than strictly necessary. Unless it's a LOT of extra threads, it's usually not worth the hassle of messing with locks. From piet at cs.uu.nl Fri May 1 03:45:29 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 01 May 2009 09:45:29 +0200 Subject: subprocess & shared environments References: <8c79f749-e648-4a2e-afad-4053a8ec5cbd@f41g2000pra.googlegroups.com> Message-ID: >>>>> Robert Dailey (RD) wrote: >RD> I'm currently calling subprocess.call() on a batch file (in Windows) >RD> that sets a few environment variables that are needed by further >RD> processes started via subprocess.call(). How can I persist the >RD> environment modifications by the first call() function? I've done my >RD> own searching on this and I came up with nothing. Environments are propagated down the subprocess chain, not up. (I don't know Windows good enough but I think this is also true on Windows.) What you could do is have the batch file print the environment, read it in your script and passing that environment to the other subprocesses. But then why would you do it in a batch file? You could as well copy the setting of the environment variables in your Python script. Or if the batch file is supplied by another source, parse it and extract the environment variables from it. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From li.wang.d at gmail.com Fri May 1 04:03:22 2009 From: li.wang.d at gmail.com (Li Wang) Date: Fri, 1 May 2009 18:03:22 +1000 Subject: How to measure the memory cost in Python? Message-ID: <11aabe3b0905010103r14b05fccg7a02c6ae010cd9ad@mail.gmail.com> Hi everyone: I want to measure the actual memory cost of a particular step in my program (Python program), does anyone know if there is some function in Python could help me to do this job? Or should I seek other tools to help me? Thank you very much! -- Li ------ Time is all we have and you may find one day you have less than you think -------------- next part -------------- An HTML attachment was scrubbed... URL: From mu at problemlos.ch Fri May 1 04:33:05 2009 From: mu at problemlos.ch (Kurt Mueller) Date: Fri, 01 May 2009 10:33:05 +0200 Subject: string processing question In-Reply-To: <3fbff25a-530e-4364-a7e1-3db6255b0d6b@k19g2000prh.googlegroups.com> References: <3fbff25a-530e-4364-a7e1-3db6255b0d6b@k19g2000prh.googlegroups.com> Message-ID: <49FAB3C1.70309@problemlos.ch> Paul McGuire schrieb: > -- > Weird. What happens if you change the second print statement to: > print b.center(6,u"-") Same behavior. I have an even more minimal example: :> python -c 'print unicode("?", "utf8")' ? :> python -c 'print unicode("?", "utf8")' | cat Traceback (most recent call last): File "", line 1, in UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128) Just the difference of having piped the output to another program or to a file. Maybe we leave the other issue with the different centering for the moment. My goal is to have my python programs unicode enabled. TIA -- Kurt Mueller -- Kurt M?ller, mu at problemlos.ch From steve at REMOVE-THIS-cybersource.com.au Fri May 1 04:33:08 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 01 May 2009 08:33:08 GMT Subject: Why bool( object )? References: <54cb7f8a-fef4-4bf8-8054-16dc9b5c8830@d2g2000pra.googlegroups.com> <49f85861$0$29010$426a74cc@news.free.fr> <4t_Jl.37718$VA5.20343@tornado.fastwebnet.it> <7xvdol1e4x.fsf@ruckus.brouhaha.com> Message-ID: <020aa73a$0$20667$c3e8da3@news.astraweb.com> On Fri, 01 May 2009 00:22:22 -0700, Paul Rubin wrote: > Steven D'Aprano writes: >> for x in a or b or c: >> do_something_with(x) > > Ugh!!!! > > for x in [a,b,c]: > if len(x) > 0: > do_something_with(x) > break What ugly, wasteful code. And it's *wrong* -- it doesn't do what my code does. (1) Why build a list [a, b, c] that you don't need? (2) Why assume that a, b and c are sequences with a fast __len__ method? They might be (say) linked lists that take O(N) to calculate the length, or binary trees that don't even have a length, but can be iterated over. (3) And your code is wrong. I pass each element of the first non-empty sequence to do_something_with(). You pass the entire sequence. To do what my code does, you would need a nested for-loop like this: for seq in [a,b,c]: if len(seq) > 0: for x in seq: do_something_with(x) break -- Steven From clp2 at rebertia.com Fri May 1 04:42:02 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 1 May 2009 01:42:02 -0700 Subject: How to measure the memory cost in Python? In-Reply-To: <11aabe3b0905010103r14b05fccg7a02c6ae010cd9ad@mail.gmail.com> References: <11aabe3b0905010103r14b05fccg7a02c6ae010cd9ad@mail.gmail.com> Message-ID: <50697b2c0905010142u628cb6f5t624b68e2860c8544@mail.gmail.com> On Fri, May 1, 2009 at 1:03 AM, Li Wang wrote: > Hi everyone: > > I want to measure the actual memory cost of a particular step in my program > (Python program), does anyone know if there is some function in Python could > help me to do this job? Or should I seek other tools to help me? See Gabriel's answer in your other thread "Measure the memory cost in Python". And please don't post duplicate threads asking essentially the same question, especially when you were given a perfectly acceptable answer and not that much time has past since you started the other thread. Cheers, Chris -- http://blog.rebertia.com From mail at microcorp.co.za Fri May 1 04:49:22 2009 From: mail at microcorp.co.za (Hendrik van Rooyen) Date: Fri, 1 May 2009 10:49:22 +0200 Subject: Multiprocessing.Queue - I want to end. References: <200904301649.10341.kyrie@uh.cu> Message-ID: <001201c9ca39$b9c4d140$0d00a8c0@Hendrik> "Luis Zarrabeitia" wrote: 8< -------explanation and example of one producer, -------- 8< -------more consumers and one queue -------------------- >As you can see, I'm sending one 'None' per consumer, and hoping that no >consumer will read more than one None. While this particular implementation You don't have to hope. You can write the consumers that way to guarantee it. >ensures that, it is very fragile. Is there any way to signal the consumers? Signalling is not easy - you can signal a process, but I doubt if it is possible to signal a thread in a process. >(or better yet, the queue itself, as it is shared by all consumers?) >Should "close" work for this? (raise the exception when the queue is >exhausted, not when it is closed by the producer). I haven't the foggiest if this will work, and it seems to me to be kind of involved compared to passing a sentinel or sentinels. And while we are on the subject - Passing None as a sentinel is IMO as good as or better than passing "XXZulu This is the End uluZXX", or any other imaginative string that is not likely to occur naturally in the input. I have always wondered why people do the one queue many getters thing. Given that the stuff you pass is homogenous in that it will require a similar amount of effort to process, is there not a case to be made to have as many queues as consumers, and to round robin the work? And if the stuff you pass around needs disparate effort to consume, it seems to me that you can more easily balance the load by having specialised consumers, instead of instances of one humungous "I can eat anything" consumer. I also think that having a queue per consumer thread makes it easier to replace the threads with processes and the queues with pipes or sockets if you need to do serious scaling later. In fact I happen to believe that anything that does any work needs one and only one input queue and nothing else, but I am peculiar that way. - Hendrik From phonethics at gmail.com Fri May 1 04:53:33 2009 From: phonethics at gmail.com (Saurabh) Date: Fri, 1 May 2009 14:23:33 +0530 Subject: returning values of a particular key from a dictionary Message-ID: arr = ({'x':'1', 'y':'a'}, {'x':'2', 'y':'b'}, {'x':'3', 'y':'c'}) print foo(arr, 'y') ['a','b','c'] I can write the function foo to return ['a','b','c']. Is there some 'automatic'/built-in way to get this in Python ? -- Phonethics From http Fri May 1 04:56:50 2009 From: http (Paul Rubin) Date: 01 May 2009 01:56:50 -0700 Subject: Why bool( object )? References: <54cb7f8a-fef4-4bf8-8054-16dc9b5c8830@d2g2000pra.googlegroups.com> <49f85861$0$29010$426a74cc@news.free.fr> <4t_Jl.37718$VA5.20343@tornado.fastwebnet.it> <7xvdol1e4x.fsf@ruckus.brouhaha.com> <020aa73a$0$20667$c3e8da3@news.astraweb.com> Message-ID: <7xvdoldwvh.fsf@ruckus.brouhaha.com> Steven D'Aprano writes: > (2) Why assume that a, b and c are sequences with a fast __len__ method? > They might be (say) linked lists that take O(N) to calculate the length, > or binary trees that don't even have a length, but can be iterated over. Why assume they have a bool method? Or a __or__ operator? > for seq in [a,b,c]: > if len(seq) > 0: > for x in seq: > do_something_with(x) > break Eh. for seq in [a,b,c]: if sum(1 for x in imap(do_something_with, seq)) > 0: break From http Fri May 1 05:00:42 2009 From: http (Paul Rubin) Date: 01 May 2009 02:00:42 -0700 Subject: returning values of a particular key from a dictionary References: Message-ID: <7xr5z9dwp1.fsf@ruckus.brouhaha.com> Saurabh writes: > arr = ({'x':'1', 'y':'a'}, {'x':'2', 'y':'b'}, {'x':'3', 'y':'c'}) > print foo(arr, 'y') > ['a','b','c'] print list(a['y'] for a in arr) From research at johnohagan.com Fri May 1 05:03:50 2009 From: research at johnohagan.com (John O'Hagan) Date: Fri, 1 May 2009 09:03:50 +0000 Subject: returning values of a particular key from a dictionary In-Reply-To: References: Message-ID: <200905010903.51595.research@johnohagan.com> On Fri, 1 May 2009, Saurabh wrote: > arr = ({'x':'1', 'y':'a'}, {'x':'2', 'y':'b'}, {'x':'3', 'y':'c'}) > print foo(arr, 'y') > ['a','b','c'] > > I can write the function foo to return ['a','b','c']. > Is there some 'automatic'/built-in way to get this in Python ? List comprehension: [i['y'] for i in arr] Regards, John From mail at microcorp.co.za Fri May 1 05:05:08 2009 From: mail at microcorp.co.za (Hendrik van Rooyen) Date: Fri, 1 May 2009 11:05:08 +0200 Subject: Why bool( object )? References: <54cb7f8a-fef4-4bf8-8054-16dc9b5c8830@d2g2000pra.googlegroups.com><49f85861$0$29010$426a74cc@news.free.fr><4t_Jl.37718$VA5.20343@tornado.fastwebnet.it> <7xvdol1e4x.fsf@ruckus.brouhaha.com> Message-ID: <001f01c9ca3b$ed7c5740$0d00a8c0@Hendrik> "Paul Rubin" wrote: > Steven D'Aprano writes: > > for x in a or b or c: > > do_something_with(x) > > Ugh!!!! > > for x in [a,b,c]: > if len(x) > 0: > do_something_with(x) > break > Ugh!!!! for x in [a,b,c]: if x: do_something_with(x) break :-) - Hendrik From clp2 at rebertia.com Fri May 1 05:05:58 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 1 May 2009 02:05:58 -0700 Subject: returning values of a particular key from a dictionary In-Reply-To: References: Message-ID: <50697b2c0905010205o63b1ab6coc84cf6a08e5e7372@mail.gmail.com> On Fri, May 1, 2009 at 1:53 AM, Saurabh wrote: > arr = ({'x':'1', 'y':'a'}, {'x':'2', 'y':'b'}, {'x':'3', 'y':'c'}) > print foo(arr, 'y') > ['a','b','c'] > > I can write the function foo to return ['a','b','c']. > Is there some 'automatic'/built-in way to get this in Python ? from operator import itemgetter print map(itemgetter("y"), arr) Cheers, Chris -- http://blog.rebertia.com From steve at REMOVE-THIS-cybersource.com.au Fri May 1 05:11:22 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 01 May 2009 09:11:22 GMT Subject: bug with os.rename in 2.4.1? References: <178473a3-906d-48e4-b84c-f9214d4fe708@w35g2000prg.googlegroups.com> Message-ID: <020ab030$0$20667$c3e8da3@news.astraweb.com> On Thu, 30 Apr 2009 03:30:04 -0500, Nick Craig-Wood wrote: >> The race condition is still there. The only difference is that in the >> first case it fails noisily, with an exception, and in the second it >> fails quietly and does nothing. > > I'd argue that since os.rename implements the syscall rename() and that > is defined to be atomic (give or take) then the above is atomic and > can't possibly be racy. Ah, you're right, at least for systems where rename is atomic. What I was trying to say is that the risk of the file *not* being renamed is still there, but in the first case the code failed noisily with an exception and in the second it just failed quietly. But that's wrong, the test is to see if the source file exists, not whether the destination file exists. If the destination file exists, and you have write-access to it, then it will be over-written no matter what. -- Steven From mu at problemlos.ch Fri May 1 05:28:12 2009 From: mu at problemlos.ch (Kurt Mueller) Date: Fri, 01 May 2009 11:28:12 +0200 Subject: string processing question In-Reply-To: References: Message-ID: <49FAC0AC.2070905@problemlos.ch> Scott David Daniels schrieb: > To discover what is happening, try something like: > python -c 'for a in "?", unicode("?"): print len(a), a' > > I suspect that in your encoding, "?" is two bytes long, and in > unicode it is converted to to a single character. :> python -c 'for a in "?", unicode("?", "utf8"): print len(a), a' 2 ? 1 ? :> Yes it is. That is one of the two problems I see. The solution for this is to unicode(, ) each string. I'd like to have my python programs unicode enabled. :> python -c 'for a in "?", unicode("?"): print len(a), a' Traceback (most recent call last): File "", line 1, in UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0: ordinal not in range(128) It seems that the default encoding is "ascii", so unicode() cannot cope with "?". If I specify "utf8" for the encoding, unicode() works. :> python -c 'for a in "?", unicode("?", "utf8"): print len(a), a' 2 ? 1 ? :> But the print statement yelds an UnicodeEncodeError if I pipe the output to a program or a file. :> python -c 'for a in "?", unicode("?", "utf8"): print len(a), a' | cat Traceback (most recent call last): File "", line 1, in UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in position 0: ordinal not in range(128) 2 ? 1 :> So it seems to me, that piping the output changes the behavior of the print statement: :> python -c 'for a in "?", unicode("?", "utf8", "ignore"): print a, len(a), type(a)' ? 2 ? 1 :> python -c 'for a in "?", unicode("?", "utf8", "ignore"): print a, len(a), type(a)' | cat Traceback (most recent call last): File "", line 1, in UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in position 0: ordinal not in range(128) ? 2 :> How can I achieve that my python programs are unicode enabled: - Input strings can have different encodings (mostly ascii, latin_1 or utf8) - My python programs should always output "utf8". Is that a good idea?? TIA -- Kurt M?ller, mu at problemlos.ch From steve at REMOVE-THIS-cybersource.com.au Fri May 1 05:30:21 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 01 May 2009 09:30:21 GMT Subject: Why bool( object )? References: <54cb7f8a-fef4-4bf8-8054-16dc9b5c8830@d2g2000pra.googlegroups.com> <49f85861$0$29010$426a74cc@news.free.fr> <4t_Jl.37718$VA5.20343@tornado.fastwebnet.it> Message-ID: <020ab4a3$0$20667$c3e8da3@news.astraweb.com> On Fri, 01 May 2009 16:30:19 +1200, Lawrence D'Oliveiro wrote: > I have never written anything so unbelievable in my life. And I hope I > never will. I didn't say you did. If anyone thought I was quoting Lawrence's code, I'd be surprised. It was not my intention to put words into your mouth. But seeing as you have replied, perhaps you could tell us something. Given so much you despise using non-bools in truth contexts, how would you re-write my example to avoid "a or b or c"? for x in a or b or c: do_something_with(x) -- Steven From ldo at geek-central.gen.new_zealand Fri May 1 05:40:41 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Fri, 01 May 2009 21:40:41 +1200 Subject: Light (general) Inter-Process Mutex/Wait/Notify Synchronization? References: <7x3abthbc2.fsf@ruckus.brouhaha.com> <4EEA4A39-1437-4AF1-93FF-F53B0EEB93AA@semanchuk.com> <49f729f5$0$1645$742ec2ed@news.sonic.net> Message-ID: In message <49f729f5$0$1645$742ec2ed at news.sonic.net>, John Nagle wrote: > Linux doesn't do interprocess communication very well. > ... and shared memory (unsafe). What about with a futex? From steve at REMOVE-THIS-cybersource.com.au Fri May 1 05:44:23 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 01 May 2009 09:44:23 GMT Subject: Why bool( object )? References: <54cb7f8a-fef4-4bf8-8054-16dc9b5c8830@d2g2000pra.googlegroups.com> <49f85861$0$29010$426a74cc@news.free.fr> <4t_Jl.37718$VA5.20343@tornado.fastwebnet.it> <7xvdol1e4x.fsf@ruckus.brouhaha.com> <020aa73a$0$20667$c3e8da3@news.astraweb.com> <7xvdoldwvh.fsf@ruckus.brouhaha.com> Message-ID: <020ab7ed$0$20667$c3e8da3@news.astraweb.com> On Fri, 01 May 2009 01:56:50 -0700, Paul Rubin wrote: > Steven D'Aprano writes: >> (2) Why assume that a, b and c are sequences with a fast __len__ >> method? They might be (say) linked lists that take O(N) to calculate >> the length, or binary trees that don't even have a length, but can be >> iterated over. > > Why assume they have a bool method? Or a __or__ operator? What programming language are you using? I'm using Python, where objects don't require either a bool or __or__ method (not operator) to work with the or operator. >>> hasattr(None, '__or__') or hasattr(None, '__bool__') or \ ... hasattr(None, '__nonzero__') False >>> >>> x = object() >>> hasattr(x, '__or__') or hasattr(x, '__bool__') or \ ... hasattr(x, '__nonzero__') False >>> >>> None or x Any object can be used as an operand to the boolean operators. > Eh. > > for seq in [a,b,c]: > if sum(1 for x in imap(do_something_with, seq)) > 0: > break Did I stumble into an Obfuscated Python competition? -- Steven From gdamjan at gmail.com Fri May 1 06:01:33 2009 From: gdamjan at gmail.com (=?UTF-8?B?0JTQsNC80ZjQsNC9INCT0LXQvtGA0LPQuNC10LLRgdC60Lg=?=) Date: Fri, 01 May 2009 12:01:33 +0200 Subject: Replacing files in a zip archive References: <6MmdnU-ISqVC3WfUnZ2dnUVZ_qCdnZ2d@pdx.net> Message-ID: > Which will produce the same output as the original, confounding > your user. You could just write the new values out, since .read > picks the last entry (as I believe it should). Alternatively, if > you want to replace it "in place", you'll need a bit more smarts > when there is more than one copy of a file in the archive (when > z.namelist.count(filename) > 1). I think I'll just hope that there will never be an ODF file like that, and if there is, then it's not my problem :) -- ?????? ( http://softver.org.mk/damjan/ ) Give me the knowledge to change the code I do not accept, the wisdom not to accept the code I cannot change, and the freedom to choose my preference. From sion at viridian.paintbox Fri May 1 06:46:34 2009 From: sion at viridian.paintbox (Sion Arrowsmith) Date: Fri, 01 May 2009 10:46:34 GMT Subject: string processing question References: <3fbff25a-530e-4364-a7e1-3db6255b0d6b@k19g2000prh.googlegroups.com> Message-ID: Kurt Mueller wrote: >:> python -c 'print unicode("?", "utf8")' >? > >:> python -c 'print unicode("?", "utf8")' | cat >Traceback (most recent call last): > File "", line 1, in >UnicodeEncodeError: 'ascii' codec can't encode characters in position >0-1: ordinal not in range(128) $ python -c 'import sys; print sys.stdout.encoding' UTF-8 $ python -c 'import sys; print sys.stdout.encoding' | cat None If print gets a Unicode string, it does an implicit .encode(sys.stdout.encoding or sys.getdefaultencoding()) on it. If you want your output to be guaranteed UTF-8, you'll need to explicitly .encode("utf8") it yourself. (I dare say this is slightly different in 3.x .) -- \S under construction From murali.au at gmail.com Fri May 1 06:56:37 2009 From: murali.au at gmail.com (Murali kumar) Date: Fri, 1 May 2009 16:26:37 +0530 Subject: problem in with keyword Message-ID: hi all.. my application runs fine in windows xp using python 2.6 and wxpython 2.8.9 but in ubuntu 8.10 following error appears.. using python 2.5.2 and wxpython 2.8.9 /home/murali/Desktop/mathdemo-configfinal/manageprofile.py:63: Warning: 'with' will become a reserved keyword in Python 2.6 Traceback (most recent call last): File "test.py", line 8, in import manageprofile as managepf File "/home/murali/Desktop/mathdemo-configfinal/manageprofile.py", line 63 with open(self.profile, 'wb') as configfile: ^ SyntaxError: invalid syntax help for this problem.. Advanced thanks.. -------------- next part -------------- An HTML attachment was scrubbed... URL: From clp2 at rebertia.com Fri May 1 07:12:37 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 1 May 2009 04:12:37 -0700 Subject: problem in with keyword In-Reply-To: References: Message-ID: <50697b2c0905010412m172fe171v1162b8d26e02401b@mail.gmail.com> On Fri, May 1, 2009 at 3:56 AM, Murali kumar wrote: > hi all.. > > my application runs fine in windows xp using python 2.6 and wxpython 2.8.9 > > but in ubuntu 8.10 following error appears..? using python 2.5.2 and > wxpython 2.8.9 > > /home/murali/Desktop/mathdemo-configfinal/manageprofile.py:63: Warning: > 'with' will become a reserved keyword in Python 2.6 > Traceback (most recent call last): > ? File "test.py", line 8, in > ??? import manageprofile as managepf > ? File "/home/murali/Desktop/mathdemo-configfinal/manageprofile.py", line 63 > ??? with open(self.profile, 'wb') as configfile: > ??????????? ^ > SyntaxError: invalid syntax According to PEP 343 (http://www.python.org/dev/peps/pep-0343/), the `with` keyword was only added in Python 2.6 To use it in Python 2.5, you'd need to add: from __future__ import with_statement to any files using the `with` syntax. Or you could just upgrade to Python 2.6 Cheers, Chris -- http://blog.rebertia.com From gabriel.rossetti at arimaz.com Fri May 1 07:18:06 2009 From: gabriel.rossetti at arimaz.com (Gabriel Rossetti) Date: Fri, 01 May 2009 13:18:06 +0200 Subject: Dynamically declared shared constant/variable imported twice problem Message-ID: <49FADA6E.2080607@arimaz.com> Hello everyone, I have three modules A, B, C; A declares this globally : UpdateEvent, UPDATE_EVENT_ID = wx.lib.newevent.NewEvent() Then they import stuff from each other: - A imports a constant from module B (in "__main__") - A imports a class and some constants from module C - B imports a constant from module A - C imports UPDATE_EVENT_ID from module A What happens is that since A imports stuff from B which then in turn loads something from A, UPDATE_EVENT_ID gets redefined and when A sends an event to C, C doesn't do anything since it doesn't have the same UPDATE_EVENT_ID. Does anyone have an idea on how to fix this? If it had been a statically defined variable then that wouldn't be "bad", but since it's dynamic it does pose problems. More globally, how cn I prevent this even with static constants? I don't think it's that great to redefine the stuff multiple times and I've already had this problem in the past with a constant dict. Thank you, Gabriel From __peter__ at web.de Fri May 1 07:37:39 2009 From: __peter__ at web.de (Peter Otten) Date: Fri, 01 May 2009 13:37:39 +0200 Subject: Dynamically declared shared constant/variable imported twice problem References: Message-ID: Gabriel Rossetti wrote: > I have three modules A, B, C; > > A declares this globally : > > UpdateEvent, UPDATE_EVENT_ID = wx.lib.newevent.NewEvent() > > Then they import stuff from each other: > > - A imports a constant from module B (in "__main__") > - A imports a class and some constants from module C > - B imports a constant from module A > - C imports UPDATE_EVENT_ID from module A > > What happens is that since A imports stuff from B which then in turn > loads something from A, UPDATE_EVENT_ID gets redefined and when A sends > an event to C, C doesn't do anything since it doesn't have the same > UPDATE_EVENT_ID. Does anyone have an idea on how to fix this? If it had > been a statically defined variable then that wouldn't be "bad", but > since it's dynamic it does pose problems. More globally, how cn I > prevent this even with static constants? I don't think it's that great > to redefine the stuff multiple times and I've already had this problem > in the past with a constant dict. Imported modules are cached, so this problem shouldn't occur unless you use A.py as your main script. In this case it will be cached as "__main__", and if you import it elsewhere a second copy will be executed and cached as "A". To fix it create a wrapper script that doesn't create any objects that will be used elsewhere, e. g. #!/usr/bin/env python import A A.main() assuming the function A.main() contains the code to start your application and invoke that instead of A.py Peter From venkatesh.th at gmail.com Fri May 1 07:37:49 2009 From: venkatesh.th at gmail.com (venky) Date: Fri, 1 May 2009 04:37:49 -0700 (PDT) Subject: where to start with Message-ID: <26ed4c2d-ac68-400c-9c5f-ceff0bfc6243@b7g2000pre.googlegroups.com> Hi, As iam very new to python i would like explore python. Can any body guide me as your guidance is more worth than googling and finding it. Thanks, Venkat http://www.prog2impress.com/ From clp2 at rebertia.com Fri May 1 07:44:25 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 1 May 2009 04:44:25 -0700 Subject: where to start with In-Reply-To: <26ed4c2d-ac68-400c-9c5f-ceff0bfc6243@b7g2000pre.googlegroups.com> References: <26ed4c2d-ac68-400c-9c5f-ceff0bfc6243@b7g2000pre.googlegroups.com> Message-ID: <50697b2c0905010444m775e4c16wf284e3209fd5352d@mail.gmail.com> On Fri, May 1, 2009 at 4:37 AM, venky wrote: > Hi, > > As iam very new to python i would like explore python. Can any body > guide me as your guidance is more worth than googling and finding it. Good tutorials: http://docs.python.org/tutorial/ http://diveintopython.org/toc/index.htm http://openbookproject.net/thinkCSpy/index.xhtmll To gain insight about Python's features and design: http://www.python.org/dev/peps/pep-0000/ Cheers, Chris -- http://blog.rebertia.com From opstad at batnet.com Fri May 1 07:58:16 2009 From: opstad at batnet.com (opstad at batnet.com) Date: Fri, 1 May 2009 04:58:16 -0700 (PDT) Subject: Generator oddity Message-ID: I'm a little baffled by the inconsistency here. Anyone have any explanations? >>> def gen(): ... yield 'a' ... yield 'b' ... yield 'c' ... >>> [c1 + c2 for c1 in gen() for c2 in gen()] ['aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc'] >>> list(c1 + c2 for c1 in gen() for c2 in gen()) ['aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc'] >>> it1 = gen() >>> it2 = gen() >>> list(c1 + c2 for c1 in it1 for c2 in it2) ['aa', 'ab', 'ac'] Why does this last list only have three elements instead of nine? From chambon.pascal at wanadoo.fr Fri May 1 08:34:32 2009 From: chambon.pascal at wanadoo.fr (Pascal Chambon) Date: Fri, 01 May 2009 14:34:32 +0200 Subject: Generator oddity In-Reply-To: References: Message-ID: <49FAEC58.2000201@wanadoo.fr> opstad at batnet.com a ?crit : > I'm a little baffled by the inconsistency here. Anyone have any > explanations? > > >>>> def gen(): >>>> > ... yield 'a' > ... yield 'b' > ... yield 'c' > ... > >>>> [c1 + c2 for c1 in gen() for c2 in gen()] >>>> > ['aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc'] > > >>>> list(c1 + c2 for c1 in gen() for c2 in gen()) >>>> > ['aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc'] > > >>>> it1 = gen() >>>> it2 = gen() >>>> list(c1 + c2 for c1 in it1 for c2 in it2) >>>> > ['aa', 'ab', 'ac'] > > Why does this last list only have three elements instead of nine? > > -- > http://mail.python.org/mailman/listinfo/python-list > > > When you use "for c2 in gen()]", at each loop of c1, a new gen is instanciated and looped-over to continue building the list. Whereas when you use "for c2 in it2)", it's always the same instance of gen which is used (it2), so after 3 loops on c2 this instance is exhausted, and the following iterations on c1 (and then attempts of looping on c2) don't give anything because the looping on c2 gives an exception "StopIteration". Regards, pascal -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at REMOVE-THIS-cybersource.com.au Fri May 1 08:48:18 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 01 May 2009 12:48:18 GMT Subject: Generator oddity References: Message-ID: <020ae308$0$20667$c3e8da3@news.astraweb.com> On Fri, 01 May 2009 04:58:16 -0700, opstad wrote: > I'm a little baffled by the inconsistency here. Anyone have any > explanations? > >>>> def gen(): > ... yield 'a' > ... yield 'b' > ... yield 'c' > ... >>>> [c1 + c2 for c1 in gen() for c2 in gen()] > ['aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc'] > >>>> list(c1 + c2 for c1 in gen() for c2 in gen()) > ['aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc'] > >>>> it1 = gen() >>>> it2 = gen() >>>> list(c1 + c2 for c1 in it1 for c2 in it2) > ['aa', 'ab', 'ac'] > > Why does this last list only have three elements instead of nine? Ah, good one! That had me puzzled for a while too. The answer is to write it out as a nested for-loop, using print in place of yield. Here's the first way: for c1 in gen(): for c2 in gen(): print c1 + c2 And the second: it1 = gen() it2 = gen() for c1 in it1: for c2 in it2: print c1 + c2 In the first example, the inner loop gets refreshed each time through the outer loop with a brand new instance of the generator. Expanding the loops in full: # First method: c1 = 'a' call gen() to make an iterable step through the fresh iterable, giving 'aa' 'ab' 'ac' c1 = 'b' call gen() to make an iterable step through the fresh iterable, giving 'ba' 'bb' 'bc' c1 = 'c' call gen() to make an iterable step through the fresh iterable, giving 'ca' 'cb' 'cc' # Second method: c1 = 'a' inner iterable already exists step through the iterable, giving 'aa' 'ab' 'ac' c1 = 'b' inner iterable is exhausted, so do nothing c1 = 'c' inner iterable is exhausted, so do nothing And there you have it. A nice Gotcha for the books. -- Steven From duncan.booth at invalid.invalid Fri May 1 08:53:01 2009 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 1 May 2009 12:53:01 GMT Subject: Generator oddity References: Message-ID: opstad at batnet.com wrote: >>>> it1 = gen() >>>> it2 = gen() >>>> list(c1 + c2 for c1 in it1 for c2 in it2) > ['aa', 'ab', 'ac'] > > Why does this last list only have three elements instead of nine? First time through the c1 in it1 loop you use up all of the it2 values. Second and third times through the c1 in it1 loop the it2 generator is still exhausted: you need to create a new generator each time if you want to repeat its values. -- Duncan Booth http://kupuguy.blogspot.com From opstad at batnet.com Fri May 1 09:21:52 2009 From: opstad at batnet.com (opstad at batnet.com) Date: Fri, 1 May 2009 06:21:52 -0700 (PDT) Subject: Generator oddity References: <020ae308$0$20667$c3e8da3@news.astraweb.com> Message-ID: D'oh! Many thanks for the explanation; I should have seen that myself. From gabriel.rossetti at arimaz.com Fri May 1 09:47:23 2009 From: gabriel.rossetti at arimaz.com (Gabriel Rossetti) Date: Fri, 01 May 2009 15:47:23 +0200 Subject: Dynamically declared shared constant/variable imported twice problem In-Reply-To: References: Message-ID: <49FAFD6B.6040906@arimaz.com> Peter Otten wrote: > Gabriel Rossetti wrote: > > >> I have three modules A, B, C; >> >> A declares this globally : >> >> UpdateEvent, UPDATE_EVENT_ID = wx.lib.newevent.NewEvent() >> >> Then they import stuff from each other: >> >> - A imports a constant from module B (in "__main__") >> - A imports a class and some constants from module C >> - B imports a constant from module A >> - C imports UPDATE_EVENT_ID from module A >> >> What happens is that since A imports stuff from B which then in turn >> loads something from A, UPDATE_EVENT_ID gets redefined and when A sends >> an event to C, C doesn't do anything since it doesn't have the same >> UPDATE_EVENT_ID. Does anyone have an idea on how to fix this? If it had >> been a statically defined variable then that wouldn't be "bad", but >> since it's dynamic it does pose problems. More globally, how cn I >> prevent this even with static constants? I don't think it's that great >> to redefine the stuff multiple times and I've already had this problem >> in the past with a constant dict. >> > > Imported modules are cached, so this problem shouldn't occur unless you use > A.py as your main script. In this case it will be cached as "__main__", and > if you import it elsewhere a second copy will be executed and cached as "A". > > To fix it create a wrapper script that doesn't create any objects that will > be used elsewhere, e. g. > > #!/usr/bin/env python > import A > A.main() > > assuming the function A.main() contains the code to start your application > and invoke that instead of A.py > > Peter > > Thank you Peter, I understand better now. I am indeed using A.py as my main script. I will create a launcher then, thank you. Gabriel From mu at problemlos.ch Fri May 1 10:02:37 2009 From: mu at problemlos.ch (Kurt Mueller) Date: Fri, 01 May 2009 16:02:37 +0200 Subject: string processing question In-Reply-To: References: <3fbff25a-530e-4364-a7e1-3db6255b0d6b@k19g2000prh.googlegroups.com> Message-ID: <49FB00FD.8080007@problemlos.ch> Sion Arrowsmith wrote: > Kurt Mueller wrote: >> :> python -c 'print unicode("?", "utf8")' >> ? >> :> python -c 'print unicode("?", "utf8")' | cat >> Traceback (most recent call last): >> File "", line 1, in >> UnicodeEncodeError: 'ascii' codec can't encode characters in position >> 0-1: ordinal not in range(128) > $ python -c 'import sys; print sys.stdout.encoding' > UTF-8 > $ python -c 'import sys; print sys.stdout.encoding' | cat > None > > If print gets a Unicode string, it does an implicit > .encode(sys.stdout.encoding or sys.getdefaultencoding()) on it. > If you want your output to be guaranteed UTF-8, you'll need to > explicitly .encode("utf8") it yourself. This works now correct with and without piping: python -c 'a=unicode("?", "utf8") ; print (a.encode("utf8"))' In my python source code I have these two lines first: #!/usr/bin/env python # vim: set fileencoding=utf-8 : So the source code itself and the strings in the source code are interpreted as utf-8. But from the command line python interprets the code as 'latin_1' I presume. That is why I have to convert the "?" with unicode(). Am I right? > (I dare say this is slightly different in 3.x .) I heard about it but I wait to go to 3.x until its time to... Thanks -- Kurt Mueller -- Kurt M?ller, mu at problemlos.ch From davea at ieee.org Fri May 1 10:25:23 2009 From: davea at ieee.org (Dave Angel) Date: Fri, 01 May 2009 10:25:23 -0400 Subject: Generator oddity In-Reply-To: References: Message-ID: <49FB0653.6030603@ieee.org> opstad at batnet.com wrote: > I'm a little baffled by the inconsistency here. Anyone have any > explanations? > > >>>> def gen(): >>>> > ... yield 'a' > ... yield 'b' > ... yield 'c' > ... > >>>> [c1 + c2 for c1 in gen() for c2 in gen()] >>>> > ['aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc'] > > >>>> list(c1 + c2 for c1 in gen() for c2 in gen()) >>>> > ['aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc'] > > >>>> it1 = gen() >>>> it2 = gen() >>>> list(c1 + c2 for c1 in it1 for c2 in it2) >>>> > ['aa', 'ab', 'ac'] > > Why does this last list only have three elements instead of nine? > > > The problem is in it2, which is initialized only once. Thus the second time you're going through the c2 loop, it doesn't have any more values. If you need the indirection provided by those it1 and it2, you need to postpone the function evaluation, at least for it2. I'd do it1 = gen it2 = gen list(c1 + c2 for c1 in it1() for c2 in it2()) or more simply: [c1 + c2 for c1 in it1() for c2 in it2()] From andreengels at gmail.com Fri May 1 10:31:44 2009 From: andreengels at gmail.com (Andre Engels) Date: Fri, 1 May 2009 16:31:44 +0200 Subject: Generator oddity In-Reply-To: <49FB0653.6030603@ieee.org> References: <49FB0653.6030603@ieee.org> Message-ID: <6faf39c90905010731v4405d51fu34df9d8e4adeeb83@mail.gmail.com> On Fri, May 1, 2009 at 4:25 PM, Dave Angel wrote: > The problem is in it2, which is initialized only once. ?Thus the second time > you're going through the c2 loop, it doesn't have any more values. > > If you need the indirection provided by those it1 and it2, you need to > postpone the function evaluation, at least for it2. > > I'd do > it1 = gen > it2 = gen > > list(c1 + c2 for c1 in it1() for c2 in it2()) > or more simply: > [c1 + c2 for c1 in it1() for c2 in it2()] Another possibility would be to use the outcome of looping through the generator rather than the generator itself: it1 = list(gen()) it2 = list(gen()) [c1 + c2 for c1 in it1() for c2 in it2()] -- Andr? Engels, andreengels at gmail.com From james at agentultra.com Fri May 1 10:31:45 2009 From: james at agentultra.com (J Kenneth King) Date: Fri, 01 May 2009 10:31:45 -0400 Subject: list comprehension question References: <6d502cfd-e252-4722-96a5-c52fc3579499@v1g2000prd.googlegroups.com> Message-ID: <85ocucnbce.fsf@agentultra.com> Chris Rebert writes: > On Thu, Apr 30, 2009 at 5:56 PM, Ross wrote: >> If I have a list of tuples a = [(1,2), (3,4), (5,6)], and I want to >> return a new list of each individual element in these tuples, I can do >> it with a nested for loop but when I try to do it using the list >> comprehension b = [j for j in i for i in a], my output is b = >> [5,5,5,6,6,6] instead of the correct b = [1,2,3,4,5,6]. What am I >> doing wrong? > > Your comprehension is the identity comprehension (i.e. it effectively > just copies the list as-is). > What you're trying to do is difficult if not impossible to do as a > comprehension. > > Here's another approach: > b = list(itertools.chain.from_iterable(a)) > > And without using a library function: > b = [] > for pair in a: > for item in pair: > b.append(item) This is much more clear than a nested comprehension. I love comprehensions, but abusing them can lead to really dense and difficult to read code. From zealalot at gmail.com Fri May 1 10:35:40 2009 From: zealalot at gmail.com (zealalot) Date: Fri, 1 May 2009 07:35:40 -0700 (PDT) Subject: Passing a function as an argument from within the same class? Message-ID: So, I'm trying to come up with a way to pass a method (from the same class) as the default argument for another method in the same class. Unfortunately though, I keep getting "self not defined" errors since the class hasn't been read completely before it references itself. Is there a better way of doing this? --- CODE --- class SomeClass(): def doNothing(self): pass def function1(self): print "Running function 1." def function2(self, passedFunction=self.doNothing): print "Running passed function." passedFunction() someObject = SomeClass() someObject.function2(someobject.function1) --- CODE --- Thanks, - Zealalot From debatem1 at gmail.com Fri May 1 10:38:49 2009 From: debatem1 at gmail.com (CTO) Date: Fri, 1 May 2009 07:38:49 -0700 (PDT) Subject: where to start with References: <26ed4c2d-ac68-400c-9c5f-ceff0bfc6243@b7g2000pre.googlegroups.com> Message-ID: I'd add http://diveintopython.org/ to that list as you gain more experience with Python, or if you already know at least one language. From linuxguy123 at gmail.com Fri May 1 10:41:09 2009 From: linuxguy123 at gmail.com (Linuxguy123) Date: Fri, 01 May 2009 08:41:09 -0600 Subject: Python servlet for Java applet ? In-Reply-To: References: Message-ID: <1241188869.3773.28.camel@localhost.localdomain> On Thu, 2009-04-30 at 14:06 +0200, Piet van Oostrum wrote: > Well, for a start, in the Python world the word 'servlet' isn't used > much. So I assume it comes from your Java legacy and you actually mean > 'any server-side python script' (in webware they use the term > 'servlet', however). I agree that servlet isn't a Python term. I should have put it in quotes. Your reply was very instructive. Thanks for taking the time to post it. I am probably going to build my "servlet" as a simple Python socket service. LG From bearophileHUGS at lycos.com Fri May 1 10:44:43 2009 From: bearophileHUGS at lycos.com (bearophileHUGS at lycos.com) Date: Fri, 1 May 2009 07:44:43 -0700 (PDT) Subject: Passing a function as an argument from within the same class? References: Message-ID: <4a39ccba-56c0-400b-bd0b-60bbe042ba0d@d7g2000prl.googlegroups.com> Zealalot, probably there are some ways to do that, but a simple one is the following (not tested): def function2(self, passed_function=None): if passed_function is None: passed_function = self.doNothing ... Bye, bearophile From ppearson at nowhere.invalid Fri May 1 10:48:51 2009 From: ppearson at nowhere.invalid (Peter Pearson) Date: 1 May 2009 14:48:51 GMT Subject: What do you think of ShowMeDo References: <87tz48s7hm.fsf@benfinney.id.au> <52d28fba-ebf7-4bfb-b568-64304da650d5@w31g2000prd.googlegroups.com> <17bacf12-037f-4dda-8ee3-1b51d83ac654@z23g2000prd.googlegroups.com> Message-ID: <760gejF1af7reU1@mid.individual.net> On Fri, 1 May 2009 07:53:35 +1000, Paul Hemans wrote: [snip] > them as they have been recorded in the anals of the web, however I .........................................^^^^^ That's the second time in this thread. The first might have been deliberate gross wordplay, but now it's time for somebody to point out that maybe this word doesn't mean what you think it means. -- To email me, substitute nowhere->spamcop, invalid->net. From debatem1 at gmail.com Fri May 1 10:50:06 2009 From: debatem1 at gmail.com (CTO) Date: Fri, 1 May 2009 07:50:06 -0700 (PDT) Subject: Passing a function as an argument from within the same class? References: Message-ID: <39845303-4a6a-4762-8be2-0b23c9ca6f8f@d2g2000pra.googlegroups.com> Make doNothing a classmethod. class SomeClass: @classmethod def doNothing(cls): pass def function1(self): print "Running function 1" def function2(self, passedFunction=SomeClass.doNothing): print "Running passed function" passedFunction() someObject = SomeClass() someObject.function2() someObject.function2(someObject.function1) From debatem1 at gmail.com Fri May 1 10:54:39 2009 From: debatem1 at gmail.com (CTO) Date: Fri, 1 May 2009 07:54:39 -0700 (PDT) Subject: How to measure the memory cost in Python? References: <11aabe3b0905010103r14b05fccg7a02c6ae010cd9ad@mail.gmail.com> Message-ID: <7e908367-0604-4400-951e-9e172befc7e7@w31g2000prd.googlegroups.com> Not OP, but I'd actually like to know if there's an answer to this one that doesn't involve platform-specific tools. From invalid at invalid Fri May 1 10:54:41 2009 From: invalid at invalid (Grant Edwards) Date: Fri, 01 May 2009 09:54:41 -0500 Subject: What do you think of ShowMeDo References: <87tz48s7hm.fsf@benfinney.id.au> <52d28fba-ebf7-4bfb-b568-64304da650d5@w31g2000prd.googlegroups.com> <17bacf12-037f-4dda-8ee3-1b51d83ac654@z23g2000prd.googlegroups.com> <760gejF1af7reU1@mid.individual.net> Message-ID: <69ydne2K6eiskGbUnZ2dnUVZ_radnZ2d@posted.visi> On 2009-05-01, Peter Pearson wrote: > On Fri, 1 May 2009 07:53:35 +1000, Paul Hemans wrote: > [snip] >> them as they have been recorded in the anals of the web, however I >.........................................^^^^^ > > That's the second time in this thread. The first might have been > deliberate gross wordplay, but now it's time for somebody to point > out that maybe this word doesn't mean what you think it means. You've got to admit it's rather funny. :) -- Grant Edwards grante Yow! I just heard the at SEVENTIES were over!! And visi.com I was just getting in touch with my LEISURE SUIT!! From clp2 at rebertia.com Fri May 1 11:04:16 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Fri, 1 May 2009 08:04:16 -0700 Subject: How to measure the memory cost in Python? In-Reply-To: <7e908367-0604-4400-951e-9e172befc7e7@w31g2000prd.googlegroups.com> References: <11aabe3b0905010103r14b05fccg7a02c6ae010cd9ad@mail.gmail.com> <7e908367-0604-4400-951e-9e172befc7e7@w31g2000prd.googlegroups.com> Message-ID: <50697b2c0905010804k7ad376eapd43c9f6bc24bf7df@mail.gmail.com> On Fri, May 1, 2009 at 7:54 AM, CTO wrote: > Not OP, but I'd actually like to know if there's an answer to this one > that doesn't involve platform-specific tools. sys.getsizeof() [a suggested solution] isn't platform-specific. Cheers, Chris -- http://blog.rebertia.com From st1999 at gmail.com Fri May 1 11:09:06 2009 From: st1999 at gmail.com (robean) Date: Fri, 1 May 2009 08:09:06 -0700 (PDT) Subject: urllib2 and threading References: <7xr5z91dx6.fsf@ruckus.brouhaha.com> Message-ID: Thanks for your reply. Obviously you make several good points about Beautiful Soup and Queue. But here's the problem: even if I do nothing whatsoever with the threads beyond just visiting the urls with urllib2, the program chokes. If I replace else: ulock.acquire() print page.geturl() # obviously, do something more useful here,eventually page.close() ulock.release() with else: pass the urllib2 starts raising URLErrros after the first 3 - 5 urls have been visited. Do you have any sense what in the threads is corrupting urllib2's behavior? Many thanks, Robean On May 1, 12:27?am, Paul Rubin wrote: > robean writes: > > reach the urls with urllib2. The actual program will involve fairly > > elaborate scraping and parsing (I'm using Beautiful Soup for that) but > > the example shown here is simplified and just confirms the url of the > > site visited. > > Keep in mind Beautiful Soup is pretty slow, so if you're doing a lot > of pages and have multiple cpu's, you probably want parallel processes > rather than threads. > > > wrong? I am new to both threading and urllib2, so its possible that > > the SNAFU is quite obvious.. > > ... > > ulock = threading.Lock() > > Without looking at the code for more than a few seconds, using an > explicit lock like that is generally not a good sign. ?The usual > Python style is to send all inter-thread communications through > Queues. ?You'd dump all your url's into a queue and have a bunch of > worker threads getting items off the queue and processing them. ?This > really avoids a lot of lock-related headache. ?The price is that you > sometimes use more threads than strictly necessary. ?Unless it's a LOT > of extra threads, it's usually not worth the hassle of messing with > locks. From zealalot at gmail.com Fri May 1 11:11:01 2009 From: zealalot at gmail.com (zealalot) Date: Fri, 1 May 2009 08:11:01 -0700 (PDT) Subject: Passing a function as an argument from within the same class? References: <39845303-4a6a-4762-8be2-0b23c9ca6f8f@d2g2000pra.googlegroups.com> Message-ID: On May 1, 10:50?am, CTO wrote: > Make doNothing a classmethod. > > class SomeClass: > > ? ? @classmethod > ? ? def doNothing(cls): > ? ? ? ? pass > > ? ? def function1(self): > ? ? ? ? print "Running function 1" > > ? ? def function2(self, passedFunction=SomeClass.doNothing): > ? ? ? ? print "Running passed function" > ? ? ? ? passedFunction() > > someObject = SomeClass() > someObject.function2() > someObject.function2(someObject.function1) It's not surprising, but I've never heard of a classmethod before. Basically, I read that it basically removes the need for the 'self' argument. Very cool! And thanks for the quick response, - Zealalot From kyle at kyleterry.com Fri May 1 11:20:34 2009 From: kyle at kyleterry.com (Kyle Terry) Date: Fri, 1 May 2009 08:20:34 -0700 Subject: where to start with In-Reply-To: References: <26ed4c2d-ac68-400c-9c5f-ceff0bfc6243@b7g2000pre.googlegroups.com> Message-ID: <65a7ac1b0905010820u2e4309ib894fbcc0caa7713@mail.gmail.com> On Fri, May 1, 2009 at 7:38 AM, CTO wrote: > I'd add http://diveintopython.org/ to that list as you gain more > experience with Python, or if you already know at least one language. Wasn't diveintopython in his list? > > > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Fri May 1 11:41:27 2009 From: __peter__ at web.de (Peter Otten) Date: Fri, 01 May 2009 17:41:27 +0200 Subject: Passing a function as an argument from within the same class? References: <39845303-4a6a-4762-8be2-0b23c9ca6f8f@d2g2000pra.googlegroups.com> Message-ID: CTO wrote: > Make doNothing a classmethod. > > class SomeClass: > > @classmethod > def doNothing(cls): > pass > > def function1(self): > print "Running function 1" > > def function2(self, passedFunction=SomeClass.doNothing): > print "Running passed function" > passedFunction() > > someObject = SomeClass() > someObject.function2() > someObject.function2(someObject.function1) To make that run without error you have to jump through a lot of hoops: class SomeClass(object): @classmethod def doNothing(cls): pass def function1(self): print "Running function 1" def function2(self, passedFunction=SomeClass.doNothing): print "Running passed function" passedFunction() SomeClass.function2 = function2 someObject = SomeClass() someObject.function2() someObject.function2(someObject.function1) And if you don't need access to the instance you may not need access to the class either. In this case you can simplify: def doNothing(): pass class SomeClass(object): def function1(self): print "Running function 1" def function2(self, passedFunction=doNothing): print "Running passed function" passedFunction() If you do need information about the state of the instance you can either pass it explicitly class SomeClass(object): def doNothing(self): pass def function1(self): print "Running function 1" def function2(self, passedFunction=doNothing): print "Running passed function" passedFunction.__get__(self)() or (better, I think) use a sentinel as shown by Bearophile. Peter From emile at fenx.com Fri May 1 12:01:05 2009 From: emile at fenx.com (Emile van Sebille) Date: Fri, 01 May 2009 09:01:05 -0700 Subject: list comprehension question In-Reply-To: <85ocucnbce.fsf@agentultra.com> References: <6d502cfd-e252-4722-96a5-c52fc3579499@v1g2000prd.googlegroups.com> <85ocucnbce.fsf@agentultra.com> Message-ID: On 5/1/2009 7:31 AM J Kenneth King said... > Chris Rebert writes: >> b = [] >> for pair in a: >> for item in pair: >> b.append(item) > > This is much more clear than a nested comprehension. > > I love comprehensions, but abusing them can lead to really dense and > difficult to read code. I disagree on dense and difficult, although I'll leave open the question of abuse. b = [ item for pair in a for item in pair ] This is exactly the code above expressed in comprehension form. It's worth knowing that a list comprehension is structured identically to the equivalent for loop. So it really is neither more dense nor more difficult to read. Further, you can tell immediately from the start of the list comprehension what you've got -- in this case a list of item(s). Here with some slight changes... >>> a = [(1, 2), (3, 4, 7), (5, 6)] >>> [ item for j in a if len(j)==2 for item in j if item % 2 ] [1, 5] ...opposed to... >>> for j in a: ... if len(j)==2: ... for item in j: ... if item % 2: ... b.append(item) ... >>> b [1, 5] >>> YMMV, Emile From Scott.Daniels at Acm.Org Fri May 1 12:06:28 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Fri, 01 May 2009 09:06:28 -0700 Subject: string processing question In-Reply-To: References: Message-ID: Kurt Mueller wrote: > Scott David Daniels schrieb: >> To discover what is happening, try something like: >> python -c 'for a in "?", unicode("?"): print len(a), a' >> >> I suspect that in your encoding, "?" is two bytes long, and in >> unicode it is converted to to a single character. > > :> python -c 'for a in "?", unicode("?", "utf8"): print len(a), a' > 2 ? > 1 ? > :> > > Yes it is. That is one of the two problems I see. > The solution for this is to unicode(, ) each string. > > > I'd like to have my python programs unicode enabled. > > > > > :> python -c 'for a in "?", unicode("?"): print len(a), a' > Traceback (most recent call last): > File "", line 1, in > UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0: > ordinal not in range(128) > > It seems that the default encoding is "ascii", so unicode() cannot cope > with "?". > If I specify "utf8" for the encoding, unicode() works. > > :> python -c 'for a in "?", unicode("?", "utf8"): print len(a), a' > 2 ? > 1 ? > :> > > > But the print statement yelds an UnicodeEncodeError > if I pipe the output to a program or a file. > > :> python -c 'for a in "?", unicode("?", "utf8"): print len(a), a' | cat > Traceback (most recent call last): > File "", line 1, in > UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in > position 0: ordinal not in range(128) > 2 ? > 1 :> > > > So it seems to me, that piping the output changes the behavior of the > print statement: > > :> python -c 'for a in "?", unicode("?", "utf8", "ignore"): print a, > len(a), type(a)' > ? 2 > ? 1 > > :> python -c 'for a in "?", unicode("?", "utf8", "ignore"): print a, > len(a), type(a)' | cat > Traceback (most recent call last): > File "", line 1, in > UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in > position 0: ordinal not in range(128) > ? 2 > :> > > > > > How can I achieve that my python programs are unicode enabled: > - Input strings can have different encodings (mostly ascii, latin_1 or utf8) > - My python programs should always output "utf8". > > Is that a good idea?? OK, the issue here is your use of -c, rather than an actual source file. I don't know how to make -c take the magic initial encoding line. If you rely on ascii source, you are safe, but have to write things like ms = u'That would be na\u00EFve' or ms = u'That would be na\xEFve.' or ms = u'That would be na\N{LATIN SMALL LETTER I WITH DIAERESIS}ve.' If you do put an encoding line in your source (first or second line): # -*- coding: utf-8 -*- or # -*- coding: iso-8859-1 -*- or # -*- coding: latin-1 -*- you can (later in that file) simply use: ms = u'That would be na?ve.' That is, I would avoid non-ascii source for plain strings in 2.X unless you have a _very_ good reason; use it, instead, for unicode strings. --Scott David Daniels Scott.Daniels at Acm.Org From arnodel at googlemail.com Fri May 1 12:19:21 2009 From: arnodel at googlemail.com (Arnaud Delobelle) Date: Fri, 01 May 2009 17:19:21 +0100 Subject: list comprehension question References: <6d502cfd-e252-4722-96a5-c52fc3579499@v1g2000prd.googlegroups.com> <85ocucnbce.fsf@agentultra.com> Message-ID: Emile van Sebille writes: > On 5/1/2009 7:31 AM J Kenneth King said... >> Chris Rebert writes: >>> b = [] >>> for pair in a: >>> for item in pair: >>> b.append(item) >> >> This is much more clear than a nested comprehension. >> >> I love comprehensions, but abusing them can lead to really dense and >> difficult to read code. > > I disagree on dense and difficult, although I'll leave open the > question of abuse. > > b = [ item for pair in a for item in pair ] > > This is exactly the code above expressed in comprehension form. If the comprehension above is an abuse, then every nested list comprehension is an abuse of comprehensions so they might as well not be in the language... -- Arnaud From sgeiger at councilforeconed.org Fri May 1 12:24:05 2009 From: sgeiger at councilforeconed.org (Shane Geiger) Date: Fri, 01 May 2009 12:24:05 -0400 Subject: list comprehension question In-Reply-To: <50697b2c0904301805p7158e167n7f84b18afd0f55f3@mail.gmail.com> References: <6d502cfd-e252-4722-96a5-c52fc3579499@v1g2000prd.googlegroups.com> <50697b2c0904301805p7158e167n7f84b18afd0f55f3@mail.gmail.com> Message-ID: <49FB2225.4030601@councilforeconed.org> from goopy.functional import flatten # http://sourceforge.net/projects/goog-goopy/ b = [(1,2), (3,4), (5,6)] print flatten(b) #from goopy.functional import flatten # http://sourceforge.net/projects/goog-goopy/ def flatten(seq): """ Returns a list of the contents of seq with sublists and tuples "exploded". The resulting list does not contain any sequences, and all inner sequences are exploded. For example: >>> flatten([7,(6,[5,4],3),2,1]) [7,6,5,4,3,2,1] """ lst = [] for el in seq: if type(el) == list or type(el) is tuple: lst.extend(flatten(el)) else: lst.append(el) return lst Chris Rebert wrote: > On Thu, Apr 30, 2009 at 5:56 PM, Ross wrote: > >> If I have a list of tuples a = [(1,2), (3,4), (5,6)], and I want to >> return a new list of each individual element in these tuples, I can do >> it with a nested for loop but when I try to do it using the list >> comprehension b = [j for j in i for i in a], my output is b = >> [5,5,5,6,6,6] instead of the correct b = [1,2,3,4,5,6]. What am I >> doing wrong? >> > > Your comprehension is the identity comprehension (i.e. it effectively > just copies the list as-is). > What you're trying to do is difficult if not impossible to do as a > comprehension. > > Here's another approach: > b = list(itertools.chain.from_iterable(a)) > > And without using a library function: > b = [] > for pair in a: > for item in pair: > b.append(item) > > Cheers, > Chris > -- Shane Geiger, IT Director Council For Economic Education / www.councilforeconed.org sgeiger at councilforeconed.org / 402-438-8958 Teaching Opportunity From warpcat at sbcglobal.net Fri May 1 12:24:10 2009 From: warpcat at sbcglobal.net (warpcat) Date: Fri, 1 May 2009 09:24:10 -0700 (PDT) Subject: object query assigned variable name? Message-ID: I've passed this around some other groups, and I'm being told "probably not possible". But I thought I'd try here as well :) I *did* search first, and found several similar threads, but they quickly tangented into other specifics of the language that were a bit over my head :) At any rate, here's a simple example, I'd love to know if as shown, is somehow possible: Given an object: class Spam(object): def __init__(self): # stuff.... I'd like it to print, when instanced, something like this: >>> s = Spam() I?m assigned to s! But it seems prohibitively hard (based on my web and forum searches) for an object to know what variable name is has been assigned to when created. Querying 'self' in __init__ returns a memory location, not the variable name passed in. If you're wondering why I'm trying to figure this out, this is just part of my continued learning of the language and pushing the bounds, to see what is possible ;) Any thoughts? From chris at simplistix.co.uk Fri May 1 12:26:29 2009 From: chris at simplistix.co.uk (Chris Withers) Date: Fri, 01 May 2009 17:26:29 +0100 Subject: [Python-Dev] .pth files are evil In-Reply-To: <49E60832.8030806@egenix.com> References: <49D4DA72.60401@v.loewis.de> <49D52115.6020001@egenix.com> <49D66C6E.3090602@v.loewis.de> <49DB475B.8060504@egenix.com> <20090407140317.EBD383A4063@sparrow.telecommunity.com> <49DB6A1F.50801@egenix.com> <20090407174355.B62983A4063@sparrow.telecommunity.com> <49E4A58F.70309@egenix.com> <20090414162603.70C843A4100@sparrow.telecommunity.com> <49E4F93B.6010802@egenix.com> <20090415003026.B0A783A4114@sparrow.telecommunity.com> <49E59202.6050809@egenix.com> <20090415144147.6845F3A4100@sparrow.telecommunity.com> <49E60832.8030806@egenix.com> Message-ID: <49FB22B5.3040507@simplistix.co.uk> M.-A. Lemburg wrote: > """ > If the package really requires adding one or more directories on sys.path (e.g. > because it has not yet been structured to support dotted-name import), a "path > configuration file" named package.pth can be placed in either the site-python or > site-packages directory. > ... > A typical installation should have no or very few .pth files or something is > wrong, and if you need to play with the search order, something is very wrong. > """ I'll say! I think .pth files are absolute evil and I wish they could just be banned. +1 on anything that makes them closer to going away or reduces the possibility of yet another similar feature from hurting the comprehensibility of a python setup. Chris -- Simplistix - Content Management, Zope & Python Consulting - http://www.simplistix.co.uk From stefan_ml at behnel.de Fri May 1 12:29:22 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Fri, 01 May 2009 18:29:22 +0200 Subject: urllib2 and threading In-Reply-To: References: Message-ID: <49fb2363$0$31865$9b4e6d93@newsspool3.arcor-online.net> robean wrote: > I am writing a program that involves visiting several hundred webpages > and extracting specific information from the contents. I've written a > modest 'test' example here that uses a multi-threaded approach to > reach the urls with urllib2. The actual program will involve fairly > elaborate scraping and parsing (I'm using Beautiful Soup for that) Try lxml.html instead. It often parses HTML pages better than BS, can parse directly from HTTP/FTP URLs, frees the GIL doing so, and is generally a lot faster and more memory friendly than the combination of urllib2 and BS, especially when threading is involved. It also supports CSS selectors for finding page content, so your "elaborate scraping" might actually turn out to be a lot simpler than you think. http://codespeak.net/lxml/ These might be worth reading: http://blog.ianbicking.org/2008/12/10/lxml-an-underappreciated-web-scraping-library/ http://blog.ianbicking.org/2008/03/30/python-html-parser-performance/ Stefan From chris at simplistix.co.uk Fri May 1 12:30:16 2009 From: chris at simplistix.co.uk (Chris Withers) Date: Fri, 01 May 2009 17:30:16 +0100 Subject: [Python-Dev] PEP 382: little help for stupid people? In-Reply-To: <49E60832.8030806@egenix.com> References: <49D4DA72.60401@v.loewis.de> <49D52115.6020001@egenix.com> <49D66C6E.3090602@v.loewis.de> <49DB475B.8060504@egenix.com> <20090407140317.EBD383A4063@sparrow.telecommunity.com> <49DB6A1F.50801@egenix.com> <20090407174355.B62983A4063@sparrow.telecommunity.com> <49E4A58F.70309@egenix.com> <20090414162603.70C843A4100@sparrow.telecommunity.com> <49E4F93B.6010802@egenix.com> <20090415003026.B0A783A4114@sparrow.telecommunity.com> <49E59202.6050809@egenix.com> <20090415144147.6845F3A4100@sparrow.telecommunity.com> <49E60832.8030806@egenix.com> Message-ID: <49FB2398.5000708@simplistix.co.uk> M.-A. Lemburg wrote: > The much more common use case is that of wanting to have a base package > installation which optional add-ons that live in the same logical > package namespace. > > The PEP provides a way to solve this use case by giving both developers > and users a standard at hand which they can follow without having to > rely on some non-standard helpers and across Python implementations. > > My proposal tries to solve this without adding yet another .pth > file like mechanism - hopefully in the spirit of the original Python > package idea. Okay, I need to issue a plea for a little help. I think I kinda get what this PEP is about now, and as someone who wants to ship a base package with several add-ons that live in the same logical package namespace, I'm very interested. However, despite trying to follow this thread *and* having tried to read the PEP a couple of times, I still don't know how I'd go about doing this. I did give some examples from what I'd be looking to do much earlier. I'll ask again in the vague hope of you or someone else explaining things to me like I'm a 5 year old - something I'm mentally equipped to be well ;-) In either of the proposals on the table, what code would I write and where to have a base package with a set of add-on packages? Simple examples would be greatly appreciated, and might bring things into focus for some of the less mentally able bystanders - like myself! cheers, Chris -- Simplistix - Content Management, Zope & Python Consulting - http://www.simplistix.co.uk From chris at simplistix.co.uk Fri May 1 12:32:14 2009 From: chris at simplistix.co.uk (Chris Withers) Date: Fri, 01 May 2009 17:32:14 +0100 Subject: [Python-Dev] PEP 382: Namespace Packages In-Reply-To: <20090415175704.966B13A4100@sparrow.telecommunity.com> References: <49D4DA72.60401@v.loewis.de> <49D52115.6020001@egenix.com> <49D66C6E.3090602@v.loewis.de> <49DB475B.8060504@egenix.com> <20090407140317.EBD383A4063@sparrow.telecommunity.com> <49DB6A1F.50801@egenix.com> <20090407174355.B62983A4063@sparrow.telecommunity.com> <49E4A58F.70309@egenix.com> <20090414162603.70C843A4100@sparrow.telecommunity.com> <49E4F93B.6010802@egenix.com> <20090415003026.B0A783A4114@sparrow.telecommunity.com> <49E59202.6050809@egenix.com> <20090415144147.6845F3A4100@sparrow.telecommunity.com> <49E60832.8030806@egenix.com> <20090415175704.966B13A4100@sparrow.telecommunity.com> Message-ID: <49FB240E.8030905@simplistix.co.uk> P.J. Eby wrote: > At 06:15 PM 4/15/2009 +0200, M.-A. Lemburg wrote: >> The much more common use case is that of wanting to have a base package >> installation which optional add-ons that live in the same logical >> package namespace. > > Please see the large number of Zope and PEAK distributions on PyPI as > minimal examples that disprove this being the common use case. If you mean "the common use case as opposed to having code in the __init__.py of the namespace package", I think you'll find that's because people (especially me!) don't know how to do this, not because we don't want to! Chris - who would actually like to know how to do this, with or without the PEP, and how to indicate interdependencies in situations like this to setuptools... -- Simplistix - Content Management, Zope & Python Consulting - http://www.simplistix.co.uk From Scott.Daniels at Acm.Org Fri May 1 12:33:19 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Fri, 01 May 2009 09:33:19 -0700 Subject: Passing a function as an argument from within the same class? In-Reply-To: References: <39845303-4a6a-4762-8be2-0b23c9ca6f8f@d2g2000pra.googlegroups.com> Message-ID: zealalot wrote: > On May 1, 10:50 am, CTO wrote: >> Make doNothing a classmethod. >> >> class SomeClass: >> @classmethod >> def doNothing(cls): >> ... >> def function2(self, passedFunction=SomeClass.doNothing): >> print "Running passed function" >> passedFunction() >> ... > It's not surprising, but I've never heard of a classmethod before. > Basically, I read that it basically removes the need for the 'self' > argument. Very cool! Careful, bearophiles' answer remains the best one. The only reason your example worked is that you had already had SomeClass defined (probably from a previous experiment). At the time you are executing the "def" statements for the methods of SomeClass, SomeClass is not yet defined. the default arguments are evaluated at "def" time, but the method body is evaluated at methd invokation time (well past the time SomeClass is fully built. Typically, classmethod is used to provide alternate constructors for a class, though it does have other uses. --Scott David Daniels Scott.Daniels at Acm.Org From MrJean1 at gmail.com Fri May 1 12:37:49 2009 From: MrJean1 at gmail.com (Jean) Date: Fri, 1 May 2009 09:37:49 -0700 (PDT) Subject: How to measure the memory cost in Python? References: <11aabe3b0905010103r14b05fccg7a02c6ae010cd9ad@mail.gmail.com> <7e908367-0604-4400-951e-9e172befc7e7@w31g2000prd.googlegroups.com> Message-ID: On May 1, 7:54?am, CTO wrote: > Not OP, but I'd actually like to know if there's an answer to this one > that doesn't involve platform-specific tools. Depending on what you need and the O/S you are using, this recipe may help That recipe also appeared in the 2nd edition of the Python Cookbook, see /Jean Brouwers From Scott.Daniels at Acm.Org Fri May 1 12:40:13 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Fri, 01 May 2009 09:40:13 -0700 Subject: object query assigned variable name? In-Reply-To: References: Message-ID: warpcat wrote: > I've passed this around some other groups, and I'm being told > "probably not possible". But I thought I'd try here as well :) ... > Given an object: > class Spam(object): > def __init__(self): > # stuff.... > I'd like it to print, when instanced, something like this: >>>> s = Spam() > I?m assigned to s! > > But it seems prohibitively hard (based on my web and forum searches) > for an object to know what variable name is has been assigned to when > created. Here's an example of why it is impossibly hard. What would you want the following code to print?: print 'NoneUnique:', Spam() is Spam() Objects are created and then used somehow (including being put somewhere), not created int a "variable." --Scott David Daniels Scott.Daniels at Acm.Org From martin at v.loewis.de Fri May 1 12:41:03 2009 From: martin at v.loewis.de (=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Fri, 01 May 2009 18:41:03 +0200 Subject: [Python-Dev] PEP 382: little help for stupid people? In-Reply-To: <49FB2398.5000708@simplistix.co.uk> References: <49D4DA72.60401@v.loewis.de> <49D52115.6020001@egenix.com> <49D66C6E.3090602@v.loewis.de> <49DB475B.8060504@egenix.com> <20090407140317.EBD383A4063@sparrow.telecommunity.com> <49DB6A1F.50801@egenix.com> <20090407174355.B62983A4063@sparrow.telecommunity.com> <49E4A58F.70309@egenix.com> <20090414162603.70C843A4100@sparrow.telecommunity.com> <49E4F93B.6010802@egenix.com> <20090415003026.B0A783A4114@sparrow.telecommunity.com> <49E59202.6050809@egenix.com> <20090415144147.6845F3A4100@sparrow.telecommunity.com> <49E60832.8030806@egenix.com> <49FB2398.5000708@simplistix.co.uk> Message-ID: <49FB261F.9080306@v.loewis.de> > In either of the proposals on the table, what code would I write and > where to have a base package with a set of add-on packages? I don't quite understand the question. Why would you want to write code (except for the code that actually is in the packages)? PEP 382 is completely declarative - no need to write code. Regards, Martin From piet at cs.uu.nl Fri May 1 12:46:18 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 01 May 2009 18:46:18 +0200 Subject: Multiprocessing Pool and functions with many arguments References: <880bfeb5-cc71-4ff5-b7a6-ffd6404597f0@v23g2000pro.googlegroups.com> Message-ID: >>>>> "psaffrey at googlemail.com" (P) wrote: >P> I'm trying to get to grips with the multiprocessing module, having >P> only used ParallelPython before. >P> based on this example: >P> http://docs.python.org/library/multiprocessing.html#using-a-pool-of-workers >P> what happens if I want my "f" to take more than one argument? I want >P> to have a list of tuples of arguments and have these correspond the >P> arguments in f, but it keeps complaining that I only have one argument >P> (the tuple). Do I have to pass in a tuple and break it up inside f? I >P> can't use multiple input lists, as I would with regular map. You give the tuple of the arguments for the function: def f(a, b, c): return a + b * c pool = Pool(processes=4) # start 4 worker processes result = pool.apply(f, (2, 3, 4)) # evaluate "f(2, 3, 4)" Or if you have a list: args = [ (2, 3, 4), # arguments for call 1 (5, 6, 7) # arguments for call 2 ] print [pool.apply(f, a) for a in args] However, as each call to apply wait for its results, this will execute sequentially instead of parallel. You can't use map directly as it works only with single argument functions. >>> print pool.map(f, args) Traceback (most recent call last): File "", line 1, in File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/multiprocessing/pool.py", line 148, in map return self.map_async(func, iterable, chunksize).get() File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/multiprocessing/pool.py", line 422, in get raise self._value TypeError: f() takes exactly 3 arguments (1 given) Is that what you mean? But you can use a wrapper function: def wrapf(abc): return f(*abc) [later...] print pool.map(wrapf, args) This is covered in the examples section of multiprocessing (see calculate and calculatestar for example). Or you can use apply_async and later wait for the results: results = [pool.apply_async(f, a) for a in args] print [r.get() for r in results] Now the calls to f are done in parallel, which you can check by putting a sleep inside f. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From steve at REMOVE-THIS-cybersource.com.au Fri May 1 12:48:32 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 01 May 2009 16:48:32 GMT Subject: object query assigned variable name? References: Message-ID: <020b1b54$0$20667$c3e8da3@news.astraweb.com> On Fri, 01 May 2009 09:24:10 -0700, warpcat wrote: > I'd like it to print, when instanced, something like this: > >>>> s = Spam() > I?m assigned to s! > > But it seems prohibitively hard (based on my web and forum searches) for > an object to know what variable name is has been assigned to when > created. Can't be done. Objects don't know what names they are bound to. Furthermore, your question is based on an invalid assumption. Objects aren't necessarily bound to a single name, they can be bound to zero, one, or more names. What name should Spam print in these examples? alist = [1, 2, Spam(), 4] alist.append(Spam()) x = y = z = Spam() -- Steven From Scott.Daniels at Acm.Org Fri May 1 12:48:54 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Fri, 01 May 2009 09:48:54 -0700 Subject: [Python-Dev] .pth files are evil In-Reply-To: References: <49D4DA72.60401@v.loewis.de> <49D52115.6020001@egenix.com> <49D66C6E.3090602@v.loewis.de> <49DB475B.8060504@egenix.com> <20090407140317.EBD383A4063@sparrow.telecommunity.com> <49DB6A1F.50801@egenix.com> <20090407174355.B62983A4063@sparrow.telecommunity.com> <49E4A58F.70309@egenix.com> <20090414162603.70C843A4100@sparrow.telecommunity.com> <49E4F93B.6010802@egenix.com> <20090415003026.B0A783A4114@sparrow.telecommunity.com> <49E59202.6050809@egenix.com> <20090415144147.6845F3A4100@sparrow.telecommunity.com> <49E60832.8030806@egenix.com> Message-ID: Chris Withers wrote: > M.-A. Lemburg wrote: >> """ >> If the package really requires adding one or more directories on >> sys.path (e.g. because it has not yet been structured to support >> dotted-name import), a "path configuration file" named package.pth >> can be placed in either the site-python or site-packages directory.... > I'll say! I think .pth files are absolute evil and I wish they could > just be banned. > +1 on anything that makes them closer to going away or reduces the > possibility of yet another similar feature from hurting the > comprehensibility of a python setup. ".pth" files are great when used in moderation. Especially when used with the new per-user location searched when setting up sys.path. I can move my current project's directory name into a .pth file that I change as I switch projects. I can also throw in a reference to my convenience tools (dinky little functions and classes that I would otherwise waste time scratching together each time I needed them). They obviate the need to fiddle with site.py. --Scott David Daniels Scott.Daniels at Acm.Org From chris at simplistix.co.uk Fri May 1 12:58:18 2009 From: chris at simplistix.co.uk (Chris Withers) Date: Fri, 01 May 2009 17:58:18 +0100 Subject: [Python-Dev] PEP 382: little help for stupid people? In-Reply-To: <49FB261F.9080306@v.loewis.de> References: <49D4DA72.60401@v.loewis.de> <49D52115.6020001@egenix.com> <49D66C6E.3090602@v.loewis.de> <49DB475B.8060504@egenix.com> <20090407140317.EBD383A4063@sparrow.telecommunity.com> <49DB6A1F.50801@egenix.com> <20090407174355.B62983A4063@sparrow.telecommunity.com> <49E4A58F.70309@egenix.com> <20090414162603.70C843A4100@sparrow.telecommunity.com> <49E4F93B.6010802@egenix.com> <20090415003026.B0A783A4114@sparrow.telecommunity.com> <49E59202.6050809@egenix.com> <20090415144147.6845F3A4100@sparrow.telecommunity.com> <49E60832.8030806@egenix.com> <49FB2398.5000708@simplistix.co.uk> <49FB261F.9080306@v.loewis.de> Message-ID: <49FB2A2A.4090606@simplistix.co.uk> Martin v. L?wis wrote: >> In either of the proposals on the table, what code would I write and >> where to have a base package with a set of add-on packages? > > I don't quite understand the question. Why would you want to write code > (except for the code that actually is in the packages)? > > PEP 382 is completely declarative - no need to write code. "code" is anything I need to write to make this work... So, what do I need to do? Chris -- Simplistix - Content Management, Zope & Python Consulting - http://www.simplistix.co.uk From jjposner at snet.net Fri May 1 13:00:23 2009 From: jjposner at snet.net (John Posner) Date: Fri, 01 May 2009 13:00:23 -0400 Subject: list comprehension question In-Reply-To: <49FB2225.4030601@councilforeconed.org> References: <6d502cfd-e252-4722-96a5-c52fc3579499@v1g2000prd.googlegroups.com> <50697b2c0904301805p7158e167n7f84b18afd0f55f3@mail.gmail.com> <49FB2225.4030601@councilforeconed.org> Message-ID: <49FB2AA7.6020403@snet.net> Shane Geiger wrote: > if type(el) == list or type(el) is tuple: A tiny improvement: if type(el) in (list, tuple): From steve at REMOVE-THIS-cybersource.com.au Fri May 1 13:00:25 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 01 May 2009 17:00:25 GMT Subject: Passing a function as an argument from within the same class? References: <39845303-4a6a-4762-8be2-0b23c9ca6f8f@d2g2000pra.googlegroups.com> Message-ID: <020b1e1d$0$20667$c3e8da3@news.astraweb.com> On Fri, 01 May 2009 08:11:01 -0700, zealalot wrote: > On May 1, 10:50?am, CTO wrote: >> Make doNothing a classmethod. >> >> class SomeClass: >> >> ? ? @classmethod >> ? ? def doNothing(cls): >> ? ? ? ? pass >> >> ? ? def function1(self): >> ? ? ? ? print "Running function 1" >> >> ? ? def function2(self, passedFunction=SomeClass.doNothing): >> ? ? ? ? print "Running passed function" >> ? ? ? ? passedFunction() >> >> someObject = SomeClass() >> someObject.function2() >> someObject.function2(someObject.function1) > > It's not surprising, but I've never heard of a classmethod before. > Basically, I read that it basically removes the need for the 'self' > argument. Very cool! Not so. When you call an ordinary method (an "instance method"), Python automatically inserts the object itself as the first argument. So you need to define the method with one extra parameter, self. (Using the name self is just a convention. You can call it anything you like.) For classmethods, Python automatically inserts not the object itself, but the object's *class* as the first argument. So you still need to define the method with an extra parameter, only now the convention is to call is cls rather than self. Here's an example: class Test(object): x = 0 # class attribute def __init__(self, x): self.x = x # instance attribute def spam(self): print self, self.x @classmethod def ham(cls): print cls, cls.x And in use: >>> t = Test(42) >>> t.spam() <__main__.Test object at 0xb7cccc6c> 42 >>> t.ham() 0 There is one other related built-in decorator, staticmethod(). staticmethod() tells Python not to pass any extra argument to the method. That means the inside a static method, you can't refer to either the instance (self) or the class! Needless to say, there aren't very many uses for staticmethod(). -- Steven From drobinow at gmail.com Fri May 1 13:03:56 2009 From: drobinow at gmail.com (David Robinow) Date: Fri, 1 May 2009 13:03:56 -0400 Subject: object query assigned variable name? In-Reply-To: References: Message-ID: <4eb0089f0905011003i49237d89w7e81f23b704d5aa9@mail.gmail.com> On Fri, May 1, 2009 at 12:24 PM, warpcat wrote: > I've passed this around some other groups, and I'm being told > "probably not possible". ?But I thought I'd try here as well :) ? I > *did* search first, and found several similar threads, but they > quickly tangented into other specifics of the language that were a bit > over my head :) ?At any rate, here's a simple example, I'd love to > know if as shown, is somehow possible: > > Given an object: > > class Spam(object): > ? ?def __init__(self): > ? ? ? ?# stuff.... > > I'd like it to print, when instanced, something like this: > >>>> s = Spam() > I?m assigned to s! > > But it seems prohibitively hard (based on my web and forum searches) > for an object to know what variable name is has been assigned to when > created. ?Querying 'self' in __init__ returns a memory location, not > the variable name passed in. > > If you're wondering why I'm trying to figure this out, this is just > part of my continued learning of the language and pushing the bounds, > to see what is possible ;) > > Any thoughts? > -- > http://mail.python.org/mailman/listinfo/python-list > Others have explained to you that this is not possible. I'll just point out that your method for learning the language is not optimal. If you had gotten a recipe to do what you asked, how would it help you write better programs? I suggest that you start programming. If you get stumped, feel free to ask here. From warpcat at sbcglobal.net Fri May 1 13:08:55 2009 From: warpcat at sbcglobal.net (AKEric) Date: Fri, 1 May 2009 10:08:55 -0700 (PDT) Subject: object query assigned variable name? References: <020b1b54$0$20667$c3e8da3@news.astraweb.com> Message-ID: <4ad92d8b-7c06-47ad-b3f4-a0da02498d81@s38g2000prg.googlegroups.com> On May 1, 9:48?am, Steven D'Aprano wrote: > On Fri, 01 May 2009 09:24:10 -0700, warpcat wrote: > > I'd like it to print, when instanced, something like this: > > >>>> s = Spam() > > I?m assigned to s! > > > But it seems prohibitively hard (based on my web and forum searches) for > > an object to know what variable name is has been assigned to when > > created. > > Can't be done. Objects don't know what names they are bound to. > > Furthermore, your question is based on an invalid assumption. Objects > aren't necessarily bound to a single name, they can be bound to zero, > one, or more names. > > What name should Spam print in these examples? > > alist = [1, 2, Spam(), 4] > alist.append(Spam()) > x = y = z = Spam() > > -- > Steven And getting back all the pointers is fine with me. But this sounds like a dead end, and I was just looking for confirmation of that. Thanks From steve at REMOVE-THIS-cybersource.com.au Fri May 1 13:09:21 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 01 May 2009 17:09:21 GMT Subject: Passing a function as an argument from within the same class? References: Message-ID: <020b2035$0$20667$c3e8da3@news.astraweb.com> On Fri, 01 May 2009 07:35:40 -0700, zealalot wrote: > So, I'm trying to come up with a way to pass a method (from the same > class) as the default argument for another method in the same class. > Unfortunately though, I keep getting "self not defined" errors since the > class hasn't been read completely before it references itself. > > Is there a better way of doing this? My first instinct is to say "Don't do that!", but let's see if there's a way to get what you want. It's actually very easy: just put the definition of the passed method before the method you want to use it in, then refer to it by name *without* self. However, there is a catch: you need to manually pass in the instance, instead of letting Python do it for you. class Spam(object): def ham(self): return "ham" def spam(self, func=ham): return "spam is a tasty %s-like food product" % func(self) And in use: >>> obj = Spam() >>> obj.ham() 'ham' >>> obj.spam() 'spam is a tasty ham-like food product' -- Steven From warpcat at sbcglobal.net Fri May 1 13:16:26 2009 From: warpcat at sbcglobal.net (AKEric) Date: Fri, 1 May 2009 10:16:26 -0700 (PDT) Subject: object query assigned variable name? References: Message-ID: <030ba85c-7b3a-47e3-a38d-338e04104579@x1g2000prh.googlegroups.com> On May 1, 10:03?am, David Robinow wrote: > On Fri, May 1, 2009 at 12:24 PM, warpcat wrote: > > I've passed this around some other groups, and I'm being told > > "probably not possible". ?But I thought I'd try here as well :) ? I > > *did* search first, and found several similar threads, but they > > quickly tangented into other specifics of the language that were a bit > > over my head :) ?At any rate, here's a simple example, I'd love to > > know if as shown, is somehow possible: > > > Given an object: > > > class Spam(object): > > ? ?def __init__(self): > > ? ? ? ?# stuff.... > > > I'd like it to print, when instanced, something like this: > > >>>> s = Spam() > > I?m assigned to s! > > > But it seems prohibitively hard (based on my web and forum searches) > > for an object to know what variable name is has been assigned to when > > created. ?Querying 'self' in __init__ returns a memory location, not > > the variable name passed in. > > > If you're wondering why I'm trying to figure this out, this is just > > part of my continued learning of the language and pushing the bounds, > > to see what is possible ;) > > > Any thoughts? > > -- > >http://mail.python.org/mailman/listinfo/python-list > > Others have explained to you that this is not possible. > I'll just point out that your method for learning the language is not optimal. > If you had gotten a recipe to do what you asked, how would it help you > write better programs? > I suggest that you start programming. If you get stumped, feel free to ask here. wow No one has yet told me it isn't possible (until other posts in this thread), they just alluded to it. Common replies are "well, maybe this way, or maybe that way". I myself help on other language forums (that I know far better than Python), and sometimes it's helpful for someone to just say "no, you can't do it" (if you truly know you can't), which is what I was looking for here. But to argue the way I'm learning the language isn't optimal is a bit short-sited, based on a single-post query. It's valuable to both know what you can, can't, should and shouldn't do, and one shouldn't be debased for asking such questions. From norseman at hughes.net Fri May 1 13:19:15 2009 From: norseman at hughes.net (norseman) Date: Fri, 01 May 2009 10:19:15 -0700 Subject: Importing modules In-Reply-To: References: <87tz48s7hm.fsf@benfinney.id.au> <52d28fba-ebf7-4bfb-b568-64304da650d5@w31g2000prd.googlegroups.com> <17bacf12-037f-4dda-8ee3-1b51d83ac654@z23g2000prd.googlegroups.com> <4eb0089f0904291713m3859c90yd0298a740967b149@mail.gmail.com> <49f9e0f8$0$7893$9a6e19ea@unlimited.newshosting.com> Message-ID: <49FB2F13.4040906@hughes.net> Gabriel Genellina wrote: > En Thu, 30 Apr 2009 14:33:38 -0300, Jim Carlock escribi?: > >> I'm messing around with a program right at the moment. It >> ends up as two applications, one runs as a server and one >> as a client which presents a Window. It almost works, so I >> need to work through it to work out it's bugs, and I'll be >> rewriting it in a couple other languages. >> >> Python looks fairly simple. It's a lot of "import" commands >> and then "from" statements to do more importing. So I need >> to figure out what the difference is between the two, as >> the "from" seems to provide a way to identify what it wants >> to import from the library/module. > > Start by reading http://docs.python.org/tutorial > In particular, section 6: Modules > >> I find it odd that no one includes the FQFN and employs a >> short name. > > fully.qualified.module.name is long to type, error prone, and slow -- > Python has to resolve each dotted name at runtime, every time it's used. > > So writting this is common: > from fully.qualified.module import name > and then, just use `name` in the code. By looking at the `import` lines, > usually located at the top, you know where a certain name comes from. > > ...unless there are statements like this: > from somemodule import * > which are considered bad practice anyway. > >> Nothing seems to get SET in the Environment to >> identify where the library gets configured. I have to run >> off to find help on the differences between "import" and >> "from". > > No need for that, usually. There is a default library search path that > is built relative to the interpreter location. That is, if the Python > interpreter used is /usr/some/fancy/directory/python, then the standard > library is at /usr/some/fancy/directory/lib/python2.6, additional > packages are at /usr/some/fancy/directory/lib/python2.6/site-packages, etc. > > You *can* alter the search path by setting some environment variables, > but I don't like that. > ------------------------------ I would like to make a small comment here on Python locations. Windows seems to have to have a slight difference. ...\python\Lib #py libs go here ...\pythonVer\Lib\site-packages #addons usually go here same as Linux ...\pythonVer\libs #seems to hold MicroSoft specific py libs Like Gabriel - my Linux is same as he describes. except I don't have a /usr/...ory/python in my case it's all in /usr/local/lib/python2.5 to keep it all in one place /usr/local/bin has the soft links to run it (reduces PATH size. /usr/local/bin already there) Which shows how flexible Python is. OH - something you mentioned that didn't seem to be addressed. import - load a complete library from - obtain specific 'function'(s) from a library (from mylib import fopen, fclose or from mylib import f* one or more gets fopen, fclose, etc from mylib import fopen as myfopen can be useful myfopen(...) won't be confused with the system fopen(...) which is great for not having to MyVeryLongNameLib.fopen() :) ) The cautionary note here is, IF two libs have a function with the same name and 'from' is used to get it from each, you wind up the last one requested. To alleviate the problem you can open(...) or mylib.open(...) and thus access both (or even more) in the same program. Check the built-ins and the libs for name problems and use the techniques noted to get what you want. Not sure what is in a given library? import somelib help(somelib) (and read) When it come to showing off, Python has its moments! HTH Steve From debatem1 at gmail.com Fri May 1 13:26:12 2009 From: debatem1 at gmail.com (CTO) Date: Fri, 1 May 2009 10:26:12 -0700 (PDT) Subject: Passing a function as an argument from within the same class? References: <39845303-4a6a-4762-8be2-0b23c9ca6f8f@d2g2000pra.googlegroups.com> Message-ID: <8ac8111d-4f02-4de0-9c66-28b4efa004c3@k38g2000yqh.googlegroups.com> > Careful, bearophiles' answer remains the best one. > > The only reason your example worked is that you had already had > SomeClass defined (probably from a previous experiment). Scott is correct, and if bearophile and I ever give you conflicting advice, take bearophile's. A (corrected) bit of code that might serve your purpose would be as follows: class SomeClass: def doNothing(): pass def function1(self): print "running function 1" def function2(self, passedFunction=doNothing): print "running passed function" passedFunction() again, though- bearophile's is the best, and most standard, answer here From steve at REMOVE-THIS-cybersource.com.au Fri May 1 13:33:03 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 01 May 2009 17:33:03 GMT Subject: object query assigned variable name? References: Message-ID: <020b25c3$0$20667$c3e8da3@news.astraweb.com> On Fri, 01 May 2009 13:03:56 -0400, David Robinow wrote: > Others have explained to you that this is not possible. I'll just point > out that your method for learning the language is not optimal. If you > had gotten a recipe to do what you asked, how would it help you write > better programs? Exactly the same way that knowing that lists have an append() method, but dicts do not, will help you to write better programs. If you don't know what tools the language offers, how are you supposed to write *any* programs, let alone "better" ones? > I suggest that you start programming. If you get stumped, feel free to > ask here. There's nothing wrong with experimenting with your tools, getting a feel for what they can do, pushing the boundaries with them. You might not get any useful code out of such games, but when you start writing code, you will already know what tools are available. Warpcat, don't be ashamed about experimenting with Python and learning the language before getting into "serious" programming. There's absolutely nothing wrong with such curiosity. -- Steven From emile at fenx.com Fri May 1 13:36:25 2009 From: emile at fenx.com (Emile van Sebille) Date: Fri, 01 May 2009 10:36:25 -0700 Subject: list comprehension question In-Reply-To: References: <6d502cfd-e252-4722-96a5-c52fc3579499@v1g2000prd.googlegroups.com> <85ocucnbce.fsf@agentultra.com> Message-ID: On 5/1/2009 9:19 AM Arnaud Delobelle said... > Emile van Sebille writes: >> On 5/1/2009 7:31 AM J Kenneth King said... >>> I love comprehensions, but abusing them can lead to really dense and >>> difficult to read code. >> I disagree on dense and difficult, although I'll leave open the >> question of abuse. >> >> b = [ item for pair in a for item in pair ] >> >> This is exactly the code above expressed in comprehension form. > > If the comprehension above is an abuse, then every nested list > comprehension is an abuse of comprehensions so they might as well not be > in the language... > I hope you didn't understand me to say that this usage is abuse. That's certainly not what I mean. I have, however, examples in my code base that _would_ qualify as abuse (mostly from pre dictionary comprehension vintage where I would build a dict from within a list comp), and I have seen enough from this group to know that abuse abounds, with only slight provocation. :) Fondly-remembering-byte-code-hacks-and-introspection-ly y'rs, Emile From martin at v.loewis.de Fri May 1 13:38:12 2009 From: martin at v.loewis.de (=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Fri, 01 May 2009 19:38:12 +0200 Subject: [Python-Dev] PEP 382: little help for stupid people? In-Reply-To: <49FB2A2A.4090606@simplistix.co.uk> References: <49D4DA72.60401@v.loewis.de> <49D52115.6020001@egenix.com> <49D66C6E.3090602@v.loewis.de> <49DB475B.8060504@egenix.com> <20090407140317.EBD383A4063@sparrow.telecommunity.com> <49DB6A1F.50801@egenix.com> <20090407174355.B62983A4063@sparrow.telecommunity.com> <49E4A58F.70309@egenix.com> <20090414162603.70C843A4100@sparrow.telecommunity.com> <49E4F93B.6010802@egenix.com> <20090415003026.B0A783A4114@sparrow.telecommunity.com> <49E59202.6050809@egenix.com> <20090415144147.6845F3A4100@sparrow.telecommunity.com> <49E60832.8030806@egenix.com> <49FB2398.5000708@simplistix.co.uk> <49FB261F.9080306@v.loewis.de> <49FB2A2A.4090606@simplistix.co.uk> Message-ID: <49FB3384.1030106@v.loewis.de> >>> In either of the proposals on the table, what code would I write and >>> where to have a base package with a set of add-on packages? >> >> I don't quite understand the question. Why would you want to write code >> (except for the code that actually is in the packages)? >> >> PEP 382 is completely declarative - no need to write code. > > "code" is anything I need to write to make this work... > > So, what do I need to do? Ok, so create three tar files: 1. base.tar, containing simplistix/ simplistix/__init__.py 2. addon1.tar, containing simplistix/addon1.pth (containing a single "*") simplistix/feature1.py 3. addon2.tar, containing simplistix/addon2.pth simplistix/feature2.py Unpack each of them anywhere on sys.path, in any order. Regards, Martin From mabdelkader at gmail.com Fri May 1 13:39:39 2009 From: mabdelkader at gmail.com (ma) Date: Fri, 1 May 2009 13:39:39 -0400 Subject: ctypes: reference of a struct member? Message-ID: <148918f0905011039t1a9a8561nf36c6fe4534e3694@mail.gmail.com> If I have this struct in C: struct spam { int ham; char foo; }; if I have this declaration: struct spam s_; If I wanted to pass a reference to a function of s_'s foo character, I can do something like this: somefunc(&s_.foo) How do I do the same thing in ctypes? ctypes.addressof(s_) + ctypes.sizeof(spam.foo)*(1) From arnodel at googlemail.com Fri May 1 13:52:30 2009 From: arnodel at googlemail.com (Arnaud Delobelle) Date: Fri, 01 May 2009 18:52:30 +0100 Subject: list comprehension question References: <6d502cfd-e252-4722-96a5-c52fc3579499@v1g2000prd.googlegroups.com> <85ocucnbce.fsf@agentultra.com> Message-ID: Emile van Sebille writes: > On 5/1/2009 9:19 AM Arnaud Delobelle said... >> Emile van Sebille writes: >>> On 5/1/2009 7:31 AM J Kenneth King said... >>>> I love comprehensions, but abusing them can lead to really dense and >>>> difficult to read code. >>> I disagree on dense and difficult, although I'll leave open the >>> question of abuse. >>> >>> b = [ item for pair in a for item in pair ] >>> >>> This is exactly the code above expressed in comprehension form. >> >> If the comprehension above is an abuse, then every nested list >> comprehension is an abuse of comprehensions so they might as well not be >> in the language... >> > > I hope you didn't understand me to say that this usage is abuse. Emile: sorry, the way I was quoting earlier posts was unclear. I was refering to the post you quoted in your message, which describe the nested list comprehension as abuse. As supporting evidence for my reply I included your example of nested list comprehension, which is one of the simplest possible ones one can think of. -- Arnaud From debatem1 at gmail.com Fri May 1 13:56:06 2009 From: debatem1 at gmail.com (CTO) Date: Fri, 1 May 2009 10:56:06 -0700 (PDT) Subject: How to measure the memory cost in Python? References: <11aabe3b0905010103r14b05fccg7a02c6ae010cd9ad@mail.gmail.com> <7e908367-0604-4400-951e-9e172befc7e7@w31g2000prd.googlegroups.com> Message-ID: > sys.getsizeof() [a suggested solution] isn't platform-specific. So, to answer the OP's question, you'd just do something like def get_totalsize(obj): total_size = sys.getsizeof(obj) for value in vars(obj).values(): try: total_size += get_total_size(value) except: total_size += sys.getsizeof(value) return totalSize def get_current_size(env): size = 0 for value in env.values(): try: size += get_total_size(value) except: pass return size get_current_size(vars()) and discount the weight of the interpreter? From shailen.tuli at gmail.com Fri May 1 14:18:10 2009 From: shailen.tuli at gmail.com (shailen.tuli at gmail.com) Date: Fri, 1 May 2009 11:18:10 -0700 (PDT) Subject: urllib2 and threading References: <49fb2363$0$31865$9b4e6d93@newsspool3.arcor-online.net> Message-ID: <2a5530c7-4442-45f1-bd36-4e708553e665@c18g2000prh.googlegroups.com> For better performance, lxml easily outperforms Beautiful Soup. For what its worth, the code runs fine if you switch from urllib2 to urllib (different exceptions are raised, obviously). I have no experience using urllib2 in a threaded environment, so I'm not sure why it breaks; urllib does OK, though. - Shailen On May 1, 9:29?am, Stefan Behnel wrote: > robean wrote: > > I am writing a program that involves visiting several hundred webpages > > and extracting specific information from the contents. I've written a > > modest 'test' example here that uses a multi-threaded approach to > > reach the urls with urllib2. The actual program will involve fairly > > elaborate scraping and parsing (I'm using Beautiful Soup for that) > > Try lxml.html instead. It often parses HTML pages better than BS, can parse > directly from HTTP/FTP URLs, frees the GIL doing so, and is generally a lot > faster and more memory friendly than the combination of urllib2 and BS, > especially when threading is involved. It also supports CSS selectors for > finding page content, so your "elaborate scraping" might actually turn out > to be a lot simpler than you think. > > http://codespeak.net/lxml/ > > These might be worth reading: > > http://blog.ianbicking.org/2008/12/10/lxml-an-underappreciated-web-sc...http://blog.ianbicking.org/2008/03/30/python-html-parser-performance/ > > Stefan From debatem1 at gmail.com Fri May 1 14:28:06 2009 From: debatem1 at gmail.com (CTO) Date: Fri, 1 May 2009 11:28:06 -0700 (PDT) Subject: ctypes: reference of a struct member? References: Message-ID: ctypes.byref() From nick at craig-wood.com Fri May 1 14:30:05 2009 From: nick at craig-wood.com (Nick Craig-Wood) Date: Fri, 01 May 2009 13:30:05 -0500 Subject: object query assigned variable name? References: Message-ID: warpcat wrote: > I've passed this around some other groups, and I'm being told > "probably not possible". But I thought I'd try here as well :) I > *did* search first, and found several similar threads, but they > quickly tangented into other specifics of the language that were a bit > over my head :) At any rate, here's a simple example, I'd love to > know if as shown, is somehow possible: > > Given an object: > > class Spam(object): > def __init__(self): > # stuff.... > > I'd like it to print, when instanced, something like this: > > >>> s = Spam() > I?m assigned to s! > > But it seems prohibitively hard (based on my web and forum searches) > for an object to know what variable name is has been assigned to when > created. Querying 'self' in __init__ returns a memory location, not > the variable name passed in. > > If you're wondering why I'm trying to figure this out, this is just > part of my continued learning of the language and pushing the bounds, > to see what is possible ;) > > Any thoughts? Read up on introspection and learn how to look up through the stack frames. When you've mastered that look for an object matching self in all the locals in those stack frames. That will give some kind of answer. I have no idea whether this will work - the keyboard of my phone is too small to produce a proof ;-) -- Nick Craig-Wood -- http://www.craig-wood.com/nick From davea at ieee.org Fri May 1 14:47:30 2009 From: davea at ieee.org (Dave Angel) Date: Fri, 01 May 2009 14:47:30 -0400 Subject: Passing a function as an argument from within the same class? In-Reply-To: References: <39845303-4a6a-4762-8be2-0b23c9ca6f8f@d2g2000pra.googlegroups.com> Message-ID: <49FB43C2.6040801@ieee.org> zealalot wrote: > On May 1, 10:50 am, CTO wrote: > >> Make doNothing a classmethod. >> >> class SomeClass: >> >> @classmethod >> def doNothing(cls): >> pass >> >> def function1(self): >> print "Running function 1" >> >> def function2(self, passedFunction=meClass.doNothing): >> print "Running passed function" >> passedFunction() >> >> someObject =omeClass() >> someObject.function2() >> someObject.function2(someObject.function1) >> > > It's not surprising, but I've never heard of a classmethod before. > Basically, I read that it basically removes the need for the 'self' > argument. Very cool! > > And thanks for the quick response, > > - Zealalot > > As you've probably figured out by now, that also gets an error, since you can't refer to the SomeClass until the definition is complete. Better is either the sentinel approach, or defining the function outside the class. Note that since the signature must match the passed function, you presumably need neither a self nor a cls. So keep it simple and define doNothing as a top-level function. But it's worth expanding on the notion of a classmethod. This type of function is callable with any object, or just with the class name itself. But it doesn't have an access to instance attributes. That's good, but not very common. Class attributes are more common, and they're attributes which are shared among all the objects of the class. From mabdelkader at gmail.com Fri May 1 14:57:03 2009 From: mabdelkader at gmail.com (ma) Date: Fri, 1 May 2009 14:57:03 -0400 Subject: ctypes: reference of a struct member? In-Reply-To: References: Message-ID: <148918f0905011157p19f49d8aha4afda704ab2824b@mail.gmail.com> ctypes.byref() does not work for struct members. Try it out. class s(ctypes.Structure): _fields_ = [('x',ctypes.c_int)] a = s() ctypes.byref(a.x) //this won't work. On Fri, May 1, 2009 at 2:28 PM, CTO wrote: > ctypes.byref() > > -- > http://mail.python.org/mailman/listinfo/python-list > From Scott.Daniels at Acm.Org Fri May 1 15:03:39 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Fri, 01 May 2009 12:03:39 -0700 Subject: list comprehension question In-Reply-To: References: <6d502cfd-e252-4722-96a5-c52fc3579499@v1g2000prd.googlegroups.com> <50697b2c0904301805p7158e167n7f84b18afd0f55f3@mail.gmail.com> <49FB2225.4030601@councilforeconed.org> Message-ID: <0_qdnYwTacv122bUnZ2dnUVZ_rqdnZ2d@pdx.net> John Posner wrote: > Shane Geiger wrote: >> if type(el) == list or type(el) is tuple: > A tiny improvement: > > if type(el) in (list, tuple): or (even better) if isinstance(el, (list, tuple)) However, it is my contention that you shouldn't be flattening by type -- you should know where, explicitly to flatten. If I have 3-D points as triples, a tree of points would, by this code, get flattened into a a list of numbers. If, however, I create a class with the same elements, but a method or two, suddenly flatten will produce a list of points. It is my contention that the tree abstraction should be responsible for producing its leaves, rather than a blunderbus that doesn't know where one container abstraction ends, and its contents begin. In other words, I'd like to see thigs like "flatten one layer." --Scott David Daniels Scott.Daniels at Acm.Org From norseman at hughes.net Fri May 1 15:04:48 2009 From: norseman at hughes.net (norseman) Date: Fri, 01 May 2009 12:04:48 -0700 Subject: don't understand namespaces... In-Reply-To: References: Message-ID: <49FB47D0.4040600@hughes.net> Simon Forman wrote: > On Apr 30, 10:11 am, Lawrence Hanser wrote: >> Dear Pythoners, >> >> I think I do not yet have a good understanding of namespaces. Here is >> what I have in broad outline form: >> >> ------------------------------------ >> import Tkinter >> >> Class App(Frame) >> define two frames, buttons in one and Listbox in the other >> >> Class App2(Frame) >> define one frame with a Text widget in it >> >> root = Tk() >> app = App(root) >> win2 = Toplevel(root) >> app2 = App2(win2) >> root.mainloop() >> ------------------------------------ >> >> My understanding of the above goes like this: >> 1) create a root window >> 2) instantiate a class that defines a Frame in the root window >> 3) create another Toplevel window >> 4) instantiate another class that defines a frame in the Toplevel window (win2) >> >> What I cannot figure out is how to reference a widget in app2 from app... >> >> I hope this is sort of clear. >> >> Any assistance appreciated. >> >> Thanks, >> >> Larry > ...(snip) # you already have Simon's approach and I'm just shorting the overall reply. --------------------------------- IF you think each widget is supposed to be a program, I would have to say NO! Each widget is just a pretty interface to the human. build your program! use the GUI to communicate with the human IN LIEU OF A COMMAND LINE. !! That is all the GUI is for.!!!!(teacher stomping foot, shaking finger :) instead of print() do msgbox() instead of getch() do textbox() instead of (command line thingy) do (GUI thingy) and so on. Your program is a program. It just communicates differently. Remember: Designing for the clicker is designing for the brain dead. :) (True - not in all cases, but mostly) import Tkinter help(Tkinter) (read!) in short import Tkinter # load lib root= Tk # use template, go live main= Frame(root,... # the mockup board frame1= Frame(main,... # frame1 will be found inside main frame2= Frame(main,.... # frame2 will be found inside main button1= Button(frame1,... # button1 will be found inside frame1 frame3= Frame(root,... # frame3 will float about on its own msgbox1= MsgBox(where,... # and so it goes . . root.mainloop() # run it Those widgets with a callback= allowed can send their answers to a function that has has been defined previously *above* in the code. MyYNVal= StrVal() # here they are before example below MyYNVal.set("*") # to be used with above example they # # def somehandler(): # need to be in same order and a_global= MyYNVal.get() # before the root= line root.quit() # rbframe= Frame(Tk,... rb1= Radiobutton(rbframe,....., variable=MyYNVal, value= "N", command=somehandler, ... ) rb2= Radiobutton(rbframe,....., variable=MyYNVal, value= "Y", command=somehandler, ... ) program calls GUI # how? encapsulate widgets in a def name() # name() human pushes button of choice which sets MyYNVal and calls somehandler which returns to program human_said= a_global if human_said == "Y": program process accordingly else: program process accordingly . . Does that help? And Yes - you can pre-edit/validate the GUI entries while still in the GUI. It gets a bit complicated because anything Tkinter is to use is already defined by time of use. Read that sentence again. (One pass interpreter. The pre-scan is for syntax only. It does not build address or set variables until actual run. ) Today: 20090430 snippets are generalized for concept HTH Steve From mabdelkader at gmail.com Fri May 1 15:08:43 2009 From: mabdelkader at gmail.com (ma) Date: Fri, 1 May 2009 15:08:43 -0400 Subject: fcntl and siginfo_t in python In-Reply-To: <148918f0904301637t5e63537atd85e2a475f4e069f@mail.gmail.com> References: <148918f0904281356v534680dcmd03084fe255cb2d3@mail.gmail.com> <148918f0904291012j4c1ada35jcee6030b75b952d0@mail.gmail.com> <148918f0904301637t5e63537atd85e2a475f4e069f@mail.gmail.com> Message-ID: <148918f0905011208u4946c377uce7603bb2e456370@mail.gmail.com> According to man signal, "The default action for an unhandled real-time signal is to terminate the receiving process." This means that my registered callback and sigaction does not work. I think the only solution would be to try this with a C-extension. Has anyone had any experience with this before? I attached my latest copy. Any insight is appreciated. On Thu, Apr 30, 2009 at 7:37 PM, ma wrote: > I attached a clean copy of the .py file in case others couldn't read > it in their emails. > I'll try that and let you know how?SIGRTMIN+1 goes! > What about this part? > > #sigemptyset(&act.sa_mask); > #python2.6 has byref(act, offset),how can i port this over? > #maybe addressof(act)+sizeof(sigaction.sa_mask)*(position_in_sigaction) > rc = __clib.sigemptyset(byref(act)) > > Thanks! > Mahmoud > > > > On Thu, Apr 30, 2009 at 7:33 PM, Philip wrote: >> >> ma gmail.com> writes: >> >> > >> > >> > >> > >> > Here's something that I came up with so far, I'm having some issues with >> segfaulting, if I want to pass a struct member by ref in ctypes(see below), if >> not, I just get a >> > "Real-time signal 0" sent back to me. >> > >> > >> > Any ideas? >> >> Try "SIGRTMIN+1", per http://souptonuts.sourceforge.net/code/dnotify.c.html >> >> Philip >> >> >> >> -- >> http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- A non-text attachment was scrubbed... Name: ctypes_fcntl.py Type: application/octet-stream Size: 3803 bytes Desc: not available URL: From mail at timgolden.me.uk Fri May 1 15:13:13 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Fri, 01 May 2009 20:13:13 +0100 Subject: [Python-Dev] .pth files are evil In-Reply-To: <49FB22B5.3040507@simplistix.co.uk> References: <49D4DA72.60401@v.loewis.de> <49D52115.6020001@egenix.com> <49D66C6E.3090602@v.loewis.de> <49DB475B.8060504@egenix.com> <20090407140317.EBD383A4063@sparrow.telecommunity.com> <49DB6A1F.50801@egenix.com> <20090407174355.B62983A4063@sparrow.telecommunity.com> <49E4A58F.70309@egenix.com> <20090414162603.70C843A4100@sparrow.telecommunity.com> <49E4F93B.6010802@egenix.com> <20090415003026.B0A783A4114@sparrow.telecommunity.com> <49E59202.6050809@egenix.com> <20090415144147.6845F3A4100@sparrow.telecommunity.com> <49E60832.8030806@egenix.com> <49FB22B5.3040507@simplistix.co.uk> Message-ID: <49FB49C9.9060901@timgolden.me.uk> Chris Withers wrote: > I'll say! I think .pth files are absolute evil and I wish they could > just be banned. > > +1 on anything that makes them closer to going away or reduces the > possibility of yet another similar feature from hurting the > comprehensibility of a python setup. I've seen this view expressed by you (and others) a number of times, but I use them myself in my admittedly simple circumstances without any problem. Would you mind sharing what's so bad about them? TJG From Scott.Daniels at Acm.Org Fri May 1 15:13:58 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Fri, 01 May 2009 12:13:58 -0700 Subject: Passing a function as an argument from within the same class? In-Reply-To: <020b2035$0$20667$c3e8da3@news.astraweb.com> References: <020b2035$0$20667$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > On Fri, 01 May 2009 07:35:40 -0700, zealalot wrote: >> So, I'm trying to come up with a way to pass a method (from the same >> class) as the default argument for another method in the same class.... > > My first instinct is to say "Don't do that!", but let's see if there's a > way to get what you want. It's actually very easy: just put the > definition of the passed method before the method you want to use it in, > then refer to it by name *without* self. > > However, there is a catch: you need to manually pass in the instance, > instead of letting Python do it for you. > > class Spam(object): > def ham(self): > return "ham" > def spam(self, func=ham): > return "spam is a tasty %s-like food product" % func(self) But you don't quite get the behavior the OP wanted, which was to allow (text-producing in this case) methods as the argument: >>> obj = Spam() >>> obj.spam() 'spam is a tasty ham-like food product' >>> obj.spam(obj.ham) Traceback (most recent call last): File "", line 1, in obj.spam(obj.ham) File "", line 5, in spam return "spam is a tasty %s-like food product" % func(self) TypeError: ham() takes exactly 1 argument (2 given) Now it is tough to use obj. --Scott David Daniels Scott.Daniels at Acm.Org From davea at ieee.org Fri May 1 15:45:30 2009 From: davea at ieee.org (Dave Angel) Date: Fri, 01 May 2009 15:45:30 -0400 Subject: wxPython having trouble with frame objects In-Reply-To: References: <5ddd5a7f-8c2b-4f39-a89e-376a3d646caf@t36g2000prt.googlegroups.com> <49FA8024.3060608@ieee.org> <49FAFBCF.2010105@dejaviewphoto.com> Message-ID: <49FB515A.7070302@ieee.org> Soumen banerjee wrote: > Hello, > The code works almost perfectly. I was able to correct some small > things, and get it to work brilliantly. I would like to thank you for > your dedicated help. My understanding of the matter here has improved > by a lot due to your kind help. I will further work on your > suggestions. By the way why should the general rule in 5 apply with > respect to private and global variables? > Regards > Soumen > > On Fri, May 1, 2009 at 7:10 PM, Dave Angel wrote: > >> Soumen banerjee wrote: >> > You accidentally top-posted, so your response is at the beginning, rather than in sequence. You also forgot to CC the PythonList, so nobody else will see it. One typo I thought of after hitting "send" was I omitted the 'self' parameter when calling the parent __init__() methods. function variables are preferred over globals for several reasons. One is that there's less likelihood of accidentally getting the wrong value. If two different functions use the same name for something, there's no confusion unless one of them decides to make it global. This is referred to as namespace pollution. Generally, the fewer names that are visible at any moment, the better. Much better to get an error than use the worng one. Other namespace cases is my preference of simple import over from xxx import yyy. It means that a reference to something from the import takes more typing, but is clearer to the reader. Second problem with globals is that if you write a successful program to do something once, it very well might not work if you just repeat it, because you might forget to re-initialize the globals. If this were the only problem, I might solve it by making a "init_globals()" function that uses the global keyword to export the names it's initializing. Then make sure you run init_globals() before running main(). Unfortunately, there are more problems, so forget this "solution." Third problem with globals is involved in refactoring. When you first write a glob of code, you tend to put lots in one module. Then when you better understand it, you refactor it into separate modules. If it was coded with globals, this refactoring is much more painful. Fourth problem with globals is in multithreading. Any reference to mutable shared information has to be considered carefully, and each global is a potential hazard. So, are there uses for globals? Certainly. First use, simple utility programs that fit in one file, run non-interactively, and for whom development pretty much stops when they "work." Second, any immutable variable, whether or not it's literally protected. There are many of these already in the system. For example, all your function names are global variables. Third, the system already defines some, such as in the sys module. Some of these are constant, such as the path separator. Others are changeable, but not usually while the application is running, like sys.path. If you have to define new ones, either put them in a separate module (as sys does), or put them in a single class, as instance attributes. Then, when refactoring, at least you only have one thing that's got to beshared between modules. Any any code that refers to them will be using the same name for them. From MrJean1 at gmail.com Fri May 1 15:50:55 2009 From: MrJean1 at gmail.com (Jean) Date: Fri, 1 May 2009 12:50:55 -0700 (PDT) Subject: How to measure the memory cost in Python? References: <11aabe3b0905010103r14b05fccg7a02c6ae010cd9ad@mail.gmail.com> <7e908367-0604-4400-951e-9e172befc7e7@w31g2000prd.googlegroups.com> Message-ID: On May 1, 10:56?am, CTO wrote: > > sys.getsizeof() [a suggested solution] isn't platform-specific. > > So, to answer the OP's question, you'd just do something like > > def get_totalsize(obj): > ? ? ? ? total_size = sys.getsizeof(obj) > ? ? ? ? for value in vars(obj).values(): > ? ? ? ? ? ? ? ? try: total_size += get_total_size(value) > ? ? ? ? ? ? ? ? except: total_size += sys.getsizeof(value) > ? ? ? ? return totalSize > > def get_current_size(env): > ? ? ? ? size = 0 > ? ? ? ? for value in env.values(): > ? ? ? ? ? ? ? ? try: size += get_total_size(value) > ? ? ? ? ? ? ? ? except: pass > ? ? ? ? return size > > get_current_size(vars()) > > and discount the weight of the interpreter? Keep in mind, sys.getsizeof(obj) returns only the size of the given object. Any referenced objects are not included. You can get the latter from gc.get_referents(obj). /Jean Brouwers PS) The asizeof(obj) function from this recipe does size the object plus its references, recursively. From cdalten at gmail.com Fri May 1 16:02:59 2009 From: cdalten at gmail.com (grocery_stocker) Date: Fri, 1 May 2009 13:02:59 -0700 (PDT) Subject: Question about the wording in the python documents. Message-ID: At the following url... http://docs.python.org/library/urllib2.html They have the following... "urllib2.urlopen(url[, data][, timeout]) Open the URL url, which can be either a string or a Request object" I don't get how urllib2.urlopen() can take a Request object. When I do the following.... [cdalten at localhost ~]$ python Python 2.4.3 (#1, Oct 1 2006, 18:00:19) [GCC 4.1.1 20060928 (Red Hat 4.1.1-28)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import urllib2 >>> >>> url = 'http://www.google.com' >>> req = urllib2.Request(url) >>> response = urllib2.urlopen(req) >>> req is clearly an instance of urllib2.Request and not a Request object. From lenglish5 at cox.net Fri May 1 16:10:26 2009 From: lenglish5 at cox.net (Lawson English) Date: Fri, 01 May 2009 13:10:26 -0700 Subject: [ANN] Python testing client for Second Life virtual world In-Reply-To: <20090428122837.24697.1129485049.divmod.quotient.12755@henry.divmod.com> References: <20090428122837.24697.1129485049.divmod.quotient.12755@henry.divmod.com> Message-ID: <49FB5732.8030807@cox.net> If anyone is curious, I'm working on a GUI shell for the python second life client, pyogp. Temp home for pyogp intro: https://wiki.secondlife.com/wiki/Pyogp/Client_Lib Basically the [first] plan is to have a GUI wrapper around the sample code with a batch file option, and to allow multiple avatar execution and control in the same process. It's been tested in the command line with up to 20 avatars at once, and the goal is to handle 200 at least, for QA purposes, so the GUI has to handle that as well. Its a pretty interesting project, IMHO. Gives complete source for non-graphical aspects of interface with the Second Life virtual world. Apache V2 licensed. Lawson From rpmuller at gmail.com Fri May 1 16:54:02 2009 From: rpmuller at gmail.com (Rick Muller) Date: Fri, 1 May 2009 13:54:02 -0700 (PDT) Subject: Profiling gives very different predictions of best algorithm Message-ID: I'm the main programmer for the PyQuante package, a quantum chemistry package in Python. I'm trying to speed up one of my rate determining steps. Essentially, I have to decide between two algorithms: 1. Packed means that I compute N**4/8 integrals, and then do a bunch of indexing operations to unpack; 2. Unpacked means that I compute all N**4 integrals, but don't have to do any indexing. Raw timing the two options show that packed is clearly faster (12.5 sec vs 20.6 sec). However, the profilings show very different results. I have the results below. Clearly I'm going to use the packed scheme. My question to the mailing list is what am I doing wrong with my profiling that it shows such poor predictions? I rely on profiling a great deal to tune my algorithms, and I'm used to seeing differences, but nothing close to this magnitude. Here is packed: ncalls tottime percall cumtime percall filename:lineno(function) 11021725 84.493 0.000 84.493 0.000 :0(ijkl2intindex) 18 62.064 3.448 119.865 6.659 Ints.py:150(getK) 18 32.063 1.781 61.186 3.399 Ints.py:131(getJ) 52975 9.404 0.000 19.658 0.000 CGBF.py:189(coulomb) 313643 2.542 0.000 2.542 0.000 :0(range) 52975 2.260 0.000 2.260 0.000 :0(contr_coulomb) 218200 1.377 0.000 1.377 0.000 CGBF.py:51(norm) 211900 1.337 0.000 1.337 0.000 CGBF.py:53(powers) 211900 1.336 0.000 1.336 0.000 CGBF.py:56(exps) 211900 1.329 0.000 1.329 0.000 CGBF.py:58(pnorms) 211900 1.328 0.000 1.328 0.000 CGBF.py:52(origin) 211900 1.328 0.000 1.328 0.000 CGBF.py:57(coefs) 1 0.979 0.979 21.108 21.108 Ints.py:112(get2ints) 11790 0.197 0.000 0.197 0.000 :0(dot) 11828 0.166 0.000 0.166 0.000 :0(zeros) Here is unpacked: ncalls tottime percall cumtime percall filename:lineno(function) 18 16.158 0.898 17.544 0.975 Ints.py:167(getK) 52975 9.301 0.000 19.515 0.000 CGBF.py:189(coulomb) 18 4.584 0.255 5.904 0.328 Ints.py:146(getJ) 313643 2.630 0.000 2.630 0.000 :0(range) 52975 2.254 0.000 2.254 0.000 :0(contr_coulomb) 218200 1.375 0.000 1.375 0.000 CGBF.py:51(norm) 211900 1.330 0.000 1.330 0.000 CGBF.py:58(pnorms) 211900 1.325 0.000 1.325 0.000 CGBF.py:53(powers) 211900 1.325 0.000 1.325 0.000 CGBF.py:57(coefs) 211900 1.323 0.000 1.323 0.000 CGBF.py:56(exps) 211900 1.321 0.000 1.321 0.000 CGBF.py:52(origin) 1 0.782 0.782 20.373 20.373 Ints.py:114(get2ints) 1875 0.156 0.000 0.384 0.000 CGBF.py:106(nuclear) 11790 0.147 0.000 0.147 0.000 :0(dot) 17856 0.112 0.000 0.112 0.000 PGBF.py:63(coef) From Brian.Mingus at colorado.edu Fri May 1 16:57:27 2009 From: Brian.Mingus at colorado.edu (Brian) Date: Fri, 1 May 2009 14:57:27 -0600 Subject: Profiling gives very different predictions of best algorithm In-Reply-To: References: Message-ID: <9839a05c0905011357h3b604bf3k12e630e76717df34@mail.gmail.com> quantum chemistry sounds complicated. that means any advice i can give you makes me a genius! just kidding. i've heard through the grapevine that reentrant functions mess up profilers. On Fri, May 1, 2009 at 2:54 PM, Rick Muller wrote: > I'm the main programmer for the PyQuante package, a quantum chemistry > package in Python. I'm trying to speed up one of my rate determining > steps. Essentially, I have to decide between two algorithms: > > 1. Packed means that I compute N**4/8 integrals, and then do a bunch > of indexing operations to unpack; > 2. Unpacked means that I compute all N**4 integrals, but don't have to > do any indexing. > > Raw timing the two options show that packed is clearly faster (12.5 > sec vs 20.6 sec). However, the profilings show very different results. > I have the results below. Clearly I'm going to use the packed scheme. > My question to the mailing list is what am I doing wrong with my > profiling that it shows such poor predictions? I rely on profiling a > great deal to tune my algorithms, and I'm used to seeing differences, > but nothing close to this magnitude. > > Here is packed: > ncalls tottime percall cumtime percall filename:lineno(function) > 11021725 84.493 0.000 84.493 0.000 :0(ijkl2intindex) > 18 62.064 3.448 119.865 6.659 Ints.py:150(getK) > 18 32.063 1.781 61.186 3.399 Ints.py:131(getJ) > 52975 9.404 0.000 19.658 0.000 CGBF.py:189(coulomb) > 313643 2.542 0.000 2.542 0.000 :0(range) > 52975 2.260 0.000 2.260 0.000 :0(contr_coulomb) > 218200 1.377 0.000 1.377 0.000 CGBF.py:51(norm) > 211900 1.337 0.000 1.337 0.000 CGBF.py:53(powers) > 211900 1.336 0.000 1.336 0.000 CGBF.py:56(exps) > 211900 1.329 0.000 1.329 0.000 CGBF.py:58(pnorms) > 211900 1.328 0.000 1.328 0.000 CGBF.py:52(origin) > 211900 1.328 0.000 1.328 0.000 CGBF.py:57(coefs) > 1 0.979 0.979 21.108 21.108 Ints.py:112(get2ints) > 11790 0.197 0.000 0.197 0.000 :0(dot) > 11828 0.166 0.000 0.166 0.000 :0(zeros) > > Here is unpacked: > ncalls tottime percall cumtime percall filename:lineno(function) > 18 16.158 0.898 17.544 0.975 Ints.py:167(getK) > 52975 9.301 0.000 19.515 0.000 CGBF.py:189(coulomb) > 18 4.584 0.255 5.904 0.328 Ints.py:146(getJ) > 313643 2.630 0.000 2.630 0.000 :0(range) > 52975 2.254 0.000 2.254 0.000 :0(contr_coulomb) > 218200 1.375 0.000 1.375 0.000 CGBF.py:51(norm) > 211900 1.330 0.000 1.330 0.000 CGBF.py:58(pnorms) > 211900 1.325 0.000 1.325 0.000 CGBF.py:53(powers) > 211900 1.325 0.000 1.325 0.000 CGBF.py:57(coefs) > 211900 1.323 0.000 1.323 0.000 CGBF.py:56(exps) > 211900 1.321 0.000 1.321 0.000 CGBF.py:52(origin) > 1 0.782 0.782 20.373 20.373 Ints.py:114(get2ints) > 1875 0.156 0.000 0.384 0.000 CGBF.py:106(nuclear) > 11790 0.147 0.000 0.147 0.000 :0(dot) > 17856 0.112 0.000 0.112 0.000 PGBF.py:63(coef) > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From emile at fenx.com Fri May 1 17:06:23 2009 From: emile at fenx.com (Emile van Sebille) Date: Fri, 01 May 2009 14:06:23 -0700 Subject: Question about the wording in the python documents. In-Reply-To: References: Message-ID: On 5/1/2009 1:02 PM grocery_stocker said... > At the following url... > > http://docs.python.org/library/urllib2.html > > They have the following... > > "urllib2.urlopen(url[, data][, timeout]) > > Open the URL url, which can be either a string or a Request > object" > > I don't get how urllib2.urlopen() can take a Request object. When I do > the following.... > > [cdalten at localhost ~]$ python > Python 2.4.3 (#1, Oct 1 2006, 18:00:19) > [GCC 4.1.1 20060928 (Red Hat 4.1.1-28)] on linux2 > Type "help", "copyright", "credits" or "license" for more information. >>>> import urllib2 >>>> >>>> url = 'http://www.google.com' >>>> req = urllib2.Request(url) >>>> response = urllib2.urlopen(req) >>>> > > req is clearly an instance of urllib2.Request and not a Request object. Yes -- it seems both the 'Request Object' and 'Request Instance' terms are used. If you feel it's a bug you can report this at http://bugs.python.org, but I'm not sure what else an object would be if not an instance of a class... Emile ----- >>> help (urllib2) Help on module urllib2: NAME urllib2 - An extensible library for opening URLs using a variety of protocols FILE c:\python24\lib\urllib2.py DESCRIPTION The simplest way to use this module is to call the urlopen function, which accepts a string containing a URL or a Request object (described below). ---------------^^^^^^^^^^^^^^ urlopen(url, data=None) -- basic usage is that same as original urllib. pass the url and optionally data to post to an HTTP URL, and get a file-like object back. One difference is that you can also pass a Request instance instead of URL. ----^^^^^^^^^^^^^^^^ From castironpi at gmail.com Fri May 1 17:18:05 2009 From: castironpi at gmail.com (Aaron Brady) Date: Fri, 1 May 2009 14:18:05 -0700 (PDT) Subject: subprocess & shared environments References: <8c79f749-e648-4a2e-afad-4053a8ec5cbd@f41g2000pra.googlegroups.com> Message-ID: <98aa4906-5006-437a-8fc0-0eaedf433be9@3g2000yqk.googlegroups.com> On May 1, 12:09?am, Robert Dailey wrote: > I'm currently calling subprocess.call() on a batch file (in Windows) > that sets a few environment variables that are needed by further > processes started via subprocess.call(). How can I persist the > environment modifications by the first call() function? I've done my > own searching on this and I came up with nothing. > > Help is appreciated. I don't know if this will work, but you can try spawning a batch file that calls the first program to set the variables, then executes the second program. You might need to create that batch file by hand, as well as the command line for the second process. You can also try setting the env. variables in the parent process, and restoring them after. From kyosohma at gmail.com Fri May 1 17:21:26 2009 From: kyosohma at gmail.com (Mike Driscoll) Date: Fri, 1 May 2009 14:21:26 -0700 (PDT) Subject: wxPython having trouble with frame objects References: <5ddd5a7f-8c2b-4f39-a89e-376a3d646caf@t36g2000prt.googlegroups.com> <49FA8024.3060608@ieee.org> Message-ID: <7a695810-56b6-428e-98cb-2205407eb5c6@g19g2000yql.googlegroups.com> On May 1, 12:12?am, Soumen banerjee wrote: > Hello, > Im not adding any GUI elements from the changer thread. Im just > attempting to change the value of a preexisting widget made in thread > one. The point is that i need to display text generated in thread 2 in > a GUI generated in thread 1. > As far as inter thread synchronization is concerned, i see your point. > So i took this approach > > import wx,gui,threading > fix=0 > i=1 > class guithread(threading.Thread): > ? ? def run(self): > ? ? ? ? global fix > ? ? ? ? app = wx.PySimpleApp(0) > ? ? ? ? wx.InitAllImageHandlers() > ? ? ? ? guithread.frame_1 = gui.MyFrame(None, -1, "") > ? ? ? ? app.SetTopWindow(guithread.frame_1) > ? ? ? ? guithread.frame_1.Show() > ? ? ? ? print "setting fix" > ? ? ? ? fix=1 > ? ? ? ? app.MainLoop() > gui1=guithread() > gui1.start() > class changer(threading.Thread): > ? ? def run(self): > ? ? ? ? print fix > ? ? ? ? gui1.frame_1.text_ctrl_1.SetValue("hello") > print fix > while i: > ? ? if fix == 1: > ? ? ? ? print "starting changer" > ? ? ? ? chang=changer() > ? ? ? ? chang.start() > ? ? ? ? i=0 > This works, but is there anything simpler? > Regards > Soumen For additional ideas on using threads in wxPython, see the wiki: http://wiki.wxpython.org/LongRunningTasks - Mike From darwin at nowhere.com Fri May 1 17:23:58 2009 From: darwin at nowhere.com (Paul Hemans) Date: Sat, 2 May 2009 07:23:58 +1000 Subject: What do you think of ShowMeDo References: <87tz48s7hm.fsf@benfinney.id.au> <52d28fba-ebf7-4bfb-b568-64304da650d5@w31g2000prd.googlegroups.com> <17bacf12-037f-4dda-8ee3-1b51d83ac654@z23g2000prd.googlegroups.com> <760gejF1af7reU1@mid.individual.net> Message-ID: I was fishing for a response, by posting multiple replies Inadvertantly, I have offended one of the Python rabbis For this misdemeanor I am most contrite But I still got a fish to bite. "Peter Pearson" wrote in message news:760gejF1af7reU1 at mid.individual.net... > On Fri, 1 May 2009 07:53:35 +1000, Paul Hemans wrote: > [snip] >> them as they have been recorded in the anals of the web, however I > .........................................^^^^^ > > That's the second time in this thread. The first might have been > deliberate gross wordplay, but now it's time for somebody to point > out that maybe this word doesn't mean what you think it means. > > -- > To email me, substitute nowhere->spamcop, invalid->net. From kyosohma at gmail.com Fri May 1 17:24:41 2009 From: kyosohma at gmail.com (Mike Driscoll) Date: Fri, 1 May 2009 14:24:41 -0700 (PDT) Subject: wxPython having trouble with frame objects References: <5ddd5a7f-8c2b-4f39-a89e-376a3d646caf@t36g2000prt.googlegroups.com> Message-ID: <38f911cf-654e-4b7e-ba73-2cb63f18c424@q16g2000yqg.googlegroups.com> On Apr 30, 11:52?pm, Dave Angel wrote: > Soumen banerjee wrote: > > Hello, > > you say that ?frame_1 is an attribute of the main class. The main > > class here is guithread right? so any instance of guithread should > > also have an attribute called frame_1 isnt it? Excuse me if im getting > > this wrong, since i am somewhat new to python. > > Regards > > Soumen > > > On Fri, May 1, 2009 at 9:05 AM, CM wrote: > > >> On Apr 30, 9:54 pm, Soumen banerjee wrote: > > >>> Hello, > >>> I am using wxglade to design a gui which i am using in another script. > >>> Here are the codes > > >>> The main file: > >>> import wx,gui,threading > >>> class guithread(threading.Thread): > >>> ? ?def run(self): > >>> ? ? ? ?app =x.PySimpleApp(0) > >>> ? ? ? ?wx.InitAllImageHandlers() > >>> ? ? ? ?self.frame_1 =ui.MyFrame(None, -1, "") > >>> ? ? ? ?app.SetTopWindow(self.frame_1) > >>> ? ? ? ?self.frame_1.Show() > >>> ? ? ? ?app.MainLoop() > >>> gui1=ithread() > >>> gui1.start() > >>> class changer(threading.Thread): > >>> ? ?def run(self): > >>> ? ? ? ?gui1.frame_1.text_ctrl_1.Setvalue("hello") > >>> chang=anger() > >>> chang.start() > > >>> and The GUI file (gui.py, imported in the above) > >>> import wx > > >>> # begin wxGlade: extracode > >>> # end wxGlade > > >>> class MyFrame(wx.Frame): > >>> ? ?def __init__(self, *args, **kwds): > >>> ? ? ? ?# begin wxGlade: MyFrame.__init__ > >>> ? ? ? ?kwds["style"] =x.DEFAULT_FRAME_STYLE > >>> ? ? ? ?wx.Frame.__init__(self, *args, **kwds) > >>> ? ? ? ?self.text_ctrl_1 =x.TextCtrl(self, -1, "") > >>> ? ? ? ?self.slider_1 =x.Slider(self, -1, 0, 0, 10) > >>> ? ? ? ?self.Open =x.Button(self, -1, "Open") > >>> ? ? ? ?self.button_4 =x.Button(self, -1, "Pause/Resume") > >>> ? ? ? ?self.button_5 =x.Button(self, -1, "Quit") > > >>> ? ? ? ?self.__set_properties() > >>> ? ? ? ?self.__do_layout() > > >>> ? ? ? ?self.Bind(wx.EVT_COMMAND_SCROLL, self.slider, self.slider_1) > >>> ? ? ? ?self.Bind(wx.EVT_BUTTON, self.open, self.Open) > >>> ? ? ? ?self.Bind(wx.EVT_BUTTON, self.pause, self.button_4) > >>> ? ? ? ?self.Bind(wx.EVT_BUTTON, self.quit, self.button_5) > >>> ? ? ? ?# end wxGlade > > >>> ? ?def __set_properties(self): > >>> ? ? ? ?# begin wxGlade: MyFrame.__set_properties > >>> ? ? ? ?self.SetTitle("frame_1") > >>> ? ? ? ?self.SetSize((522, 457)) > >>> ? ? ? ?# end wxGlade > > >>> ? ?def __do_layout(self): > >>> ? ? ? ?# begin wxGlade: MyFrame.__do_layout > >>> ? ? ? ?sizer_1 =x.BoxSizer(wx.VERTICAL) > >>> ? ? ? ?sizer_2 =x.BoxSizer(wx.VERTICAL) > >>> ? ? ? ?sizer_3 =x.BoxSizer(wx.HORIZONTAL) > >>> ? ? ? ?sizer_2.Add(self.text_ctrl_1, 7, wx.EXPAND, 0) > >>> ? ? ? ?sizer_2.Add(self.slider_1, 0, wx.EXPAND, 0) > >>> ? ? ? ?sizer_3.Add(self.Open, 0, wx.LEFT, 70) > >>> ? ? ? ?sizer_3.Add((52, 20), 0, 0, 0) > >>> ? ? ? ?sizer_3.Add(self.button_4, 0, wx.ALIGN_CENTER_HORIZONTAL, 0) > >>> ? ? ? ?sizer_3.Add((55, 23), 0, 0, 0) > >>> ? ? ? ?sizer_3.Add(self.button_5, 0, 0, 0) > >>> ? ? ? ?sizer_2.Add(sizer_3, 1, wx.EXPAND, 0) > >>> ? ? ? ?sizer_1.Add(sizer_2, 1, wx.EXPAND, 0) > >>> ? ? ? ?self.SetSizer(sizer_1) > >>> ? ? ? ?self.Layout() > >>> ? ? ? ?# end wxGlade > > >>> ? ?def slider(self, event): # wxGlade: MyFrame. > >>> ? ? ? ?print "Event handler `slider' not implemented!" > >>> ? ? ? ?event.Skip() > > >>> ? ?def open(self, event): # wxGlade: MyFrame. > >>> ? ? ? ?print "Event handler `open' not implemented!" > >>> ? ? ? ?event.Skip() > > >>> ? ?def pause(self, event): # wxGlade: MyFrame. > >>> ? ? ? ?print "Event handler `pause' not implemented!" > >>> ? ? ? ?event.Skip() > > >>> ? ?def quit(self, event): # wxGlade: MyFrame. > >>> ? ? ? ?print "Event handler `quit' not implemented!" > >>> ? ? ? ?event.Skip() > > >>> # end of class MyFrame > > >>> if __name__ ="__main__": > >>> ? ?app =x.PySimpleApp(0) > >>> ? ?wx.InitAllImageHandlers() > >>> ? ?frame_1 =yFrame(None, -1, "") > >>> ? ?app.SetTopWindow(frame_1) > >>> ? ?frame_1.Show() > >>> ? ?app.MainLoop() > > >>> The problem here is that when i run the main file, i am told that > >>> 'guithread' object has no attribute 'frame_1' whereas i seem to have > >>> defined > >>> self.frame_1=i.MyFrame etc. > > >> Your statement above means that self--that is, the instance of > >> your main class--has an attribute called frame_1, and that name > >> refers to an instance of the MyFrame class from the gui module. > > >> It does not mean that the guithread object has an attribute named > >> frame_1. ?In order to do that, you should have written: > > >> guithread.frame_1 =omething > > >>> The idea here is to access a gui element running in a thread from a > >>> separate thread. Please help > > >> I would post wxPython related questions on the wxPython mailing > >> list, which is excellent. > >>http://www.wxpython.org/maillist.php > > >> HTH, > >> Che > >> -- > >>http://mail.python.org/mailman/listinfo/python-list > > Don't top-post. It puts things entirely out of order. Now the order of > the parts of this message are 3, 1, 2, 4 > > Two things at least are wrong here, either of which is fatal. > ? ?1) you have two threads doing GUI stuff. ?Can't be done, at least not in wxPython. ?Certain things can be done in a second thread, but definitely not constructing Frames and such. You are correct...but I think that's pretty common across GUI toolkits. The GUI has it's own main loop that will get blocked if another thread tries to do something or you'll end up with strange behavior. As far as I know, each toolkit has its own methods for working around this. In wxPython's case, you can use wx.CallAfter, wx.CallLater and several others to manipulate the GUI from a thread. There's some other ways of going about this on the wxPython wiki: http://wiki.wxpython.org/LongRunningTasks - Mike From seanm.py at gmail.com Fri May 1 17:34:03 2009 From: seanm.py at gmail.com (seanm.py at gmail.com) Date: Fri, 1 May 2009 14:34:03 -0700 (PDT) Subject: Help! Can't get program to run. Message-ID: <77a2fc3c-638d-4aef-bae9-3b1ea9d4e988@c36g2000yqn.googlegroups.com> I think this is maybe the most basic problem possible, but I can't get even the most basic Python to run on OS X using Terminal or IDLE. I used the IDLE editor to create a file with one line of code print 'text string' and I saved the file as module1.py. When using terminal I entered "python" to load the interpreter or whatever and then tried python module1.py but I got an error message. I've tried using the file path instead of module1.py and that didn't work either. I've tried moving the file to different places, my desktop, hard drive, etc., and none of that worked either. In IDLE I tried similar things and only got error messages there too. Needless to say this is a frustrating start to learning this langauge. I'd really appreciate any help getting this to work, so I can move on the actual language. Thanks so much. From tjreedy at udel.edu Fri May 1 17:37:48 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 01 May 2009 17:37:48 -0400 Subject: Passing a function as an argument from within the same class? In-Reply-To: References: Message-ID: zealalot wrote: > So, I'm trying to come up with a way to pass a method (from the same > class) as the default argument for another method in the same class. > Unfortunately though, I keep getting "self not defined" errors since > the class hasn't been read completely before it references itself. > > Is there a better way of doing this? > > --- CODE --- > > class SomeClass(): > def doNothing(self): > pass > def function1(self): > print "Running function 1." > def function2(self, passedFunction=self.doNothing): > print "Running passed function." > passedFunction() > > someObject = SomeClass() > someObject.function2(someobject.function1) As Stephen D'Aprano indicated, this is very easy class SomeClass(): def doNothing(self): print("Doing nothing") def function1(self): print ("Running function 1.") def function2(self, passedFunction=doNothing): print ("Running passed function.") passedFunction(self) someObject = SomeClass() someObject.function2() someObject.function2(SomeClass.function1) produces (with 3.0.1) Running passed function. Doing nothing Running passed function. Running function 1. Key 1: a class statement introduces a new local namespace. The body of the class statement is executed in that namespace. Default arguments are evaluated, when a def statement is executed, in the local namespace of that def statement. For methods, that is the local namespace of the class. Hence, 'passedFunction = doNothing' works fine. Key 2: When a parameter is a function, the signature of the default and passed args are effectively part of the required type for the args. In this case, passedFunction is a function with one parameter. When captured, doNothing is not yet a method of the yet-to-become Someclass. So non-defaults passed to .function2 do not have to be methods either. In Python 3, unbound methods are simply functions anyway. Terry Jan Reedy From tjreedy at udel.edu Fri May 1 17:42:47 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 01 May 2009 17:42:47 -0400 Subject: Importing modules In-Reply-To: <49FB2F13.4040906@hughes.net> References: <87tz48s7hm.fsf@benfinney.id.au> <52d28fba-ebf7-4bfb-b568-64304da650d5@w31g2000prd.googlegroups.com> <17bacf12-037f-4dda-8ee3-1b51d83ac654@z23g2000prd.googlegroups.com> <4eb0089f0904291713m3859c90yd0298a740967b149@mail.gmail.com> <49f9e0f8$0$7893$9a6e19ea@unlimited.newshosting.com> <49FB2F13.4040906@hughes.net> Message-ID: norseman wrote: > OH - something you mentioned that didn't seem to be addressed. > import - load a complete library > from - obtain specific 'function'(s) from a library from can also be used to get a specific module from a package If you 'import pack.mod', then you have to write 'pack.mod.ob' to access anything within mod. But if you do 'from pack import mod', then just 'mod.ob' works. From deets at nospam.web.de Fri May 1 17:45:23 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Fri, 01 May 2009 23:45:23 +0200 Subject: Help! Can't get program to run. In-Reply-To: <77a2fc3c-638d-4aef-bae9-3b1ea9d4e988@c36g2000yqn.googlegroups.com> References: <77a2fc3c-638d-4aef-bae9-3b1ea9d4e988@c36g2000yqn.googlegroups.com> Message-ID: <7618rjF1a3t8jU1@mid.uni-berlin.de> seanm.py at gmail.com schrieb: > I think this is maybe the most basic problem possible, but I can't get > even the most basic Python to run on OS X using Terminal or IDLE. I > used the IDLE editor to create a file with one line of code > > print 'text string' > > and I saved the file as module1.py. When using terminal I entered > "python" to load the interpreter or whatever and then tried > > python module1.py > > but I got an error message. I've tried using the file path instead of > module1.py and that didn't work either. I've tried moving the file to > different places, my desktop, hard drive, etc., and none of that > worked either. In IDLE I tried similar things and only got error > messages there too. > > Needless to say this is a frustrating start to learning this langauge. > I'd really appreciate any help getting this to work, so I can move on > the actual language. Thanks so much. It would have helped if you had given us the *actual* error-message. From what little information you give, it appears as if you do something very basic wrong - instead of doing $ python >>> python mymodule.py (where $ is the shell/terminal and >>> the python-prompt) you either do $ python >>> print "hello" or $ python mymodule.py to execute mymodule directly. Diez From tjreedy at udel.edu Fri May 1 17:50:00 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 01 May 2009 17:50:00 -0400 Subject: Profiling gives very different predictions of best algorithm In-Reply-To: References: Message-ID: Rick Muller wrote: > I'm the main programmer for the PyQuante package, a quantum chemistry > package in Python. I'm trying to speed up one of my rate determining > steps. Essentially, I have to decide between two algorithms: > > 1. Packed means that I compute N**4/8 integrals, and then do a bunch > of indexing operations to unpack; > 2. Unpacked means that I compute all N**4 integrals, but don't have to > do any indexing. > > Raw timing the two options show that packed is clearly faster (12.5 > sec vs 20.6 sec). However, the profilings show very different results. > I have the results below. Clearly I'm going to use the packed scheme. > My question to the mailing list is what am I doing wrong with my > profiling that it shows such poor predictions? That *might* be easier to answer if you were to show exactly what you did to get the odd-looking results ;-) From castironpi at gmail.com Fri May 1 17:51:04 2009 From: castironpi at gmail.com (Aaron Brady) Date: Fri, 1 May 2009 14:51:04 -0700 (PDT) Subject: Multiprocessing.Queue - I want to end. References: Message-ID: <2759676b-952a-46d0-a766-334782794984@o18g2000yqi.googlegroups.com> On Apr 30, 3:49?pm, Luis Zarrabeitia wrote: > Hi. I'm building a script that closely follows a producer-consumer model. In > this case, the producer is disk-bound and the consumer is cpu-bound, so I'm > using the multiprocessing module (python2.5 with the multiprocessing backport > from google.code) to speed up the processing (two consumers, one per core, > and one producer). The consumers are two multiprocessing.Process instances, > the producer is the main script, and the data is sent using a > multiprocessing.Queue instance (with bounded capacity). > > The problem: when there is no more data to process, how can I signal the > consumers to consume until the queue is empty and then stop consuming? I need > them to do some clean-up work after they finish (and then I need the main > script to summarize the results) snip > ? ? for data in iter(queue.get, None): > ? ? ? ? process_data(data, outfile) # stores the result in the outfile snip > ? ? queue.put(None); queue.put(None) snip > As you can see, I'm sending one 'None' per consumer, and hoping that no > consumer will read more than one None. While this particular implementation > ensures that, it is very fragile. Is there any way to signal the consumers? > (or better yet, the queue itself, as it is shared by all consumers?) > Should "close" work for this? (raise the exception when the queue is > exhausted, not when it is closed by the producer). You may have to write the consumer loop by hand, rather than using 'for'. In the same-process case, you can do this. producer: sentinel= object( ) consumer: while True: item= queue.get( ) if item is sentinel: break etc. Then, each consumer is guaranteed to consume no more than one sentinel, and thus producing one sentinel per consumer will halt them all. However, with multiple processes, the comparison to 'sentinel' will fail, since each subprocess gets a copy, not the original, of the sentinel. A sample program which sent the same object multiple times produced this output: Theoretically, you could send a shared object, which would satisfy the identity test in the subprocess. That failed with this exception: File "c:\programs\python30\lib\multiprocessing\queues.py", line 51, in __getstate__ assert_spawning(self) ... RuntimeError: Queue objects should only be shared between processes th rough inheritance As a result, your options are more complicated. I think the best option is to send a tuple with the data. Instead of sending 'item', send '( True, item )'. Then when the producer is finished, send '( False, )'. The consumer will break when it encounters a 'False' first value. An alternative is to spawn a watchman thread in each subprocess, which merely blocks for a shared Event object, then sets a per-process variable, then adds a dummy object to the queue. The dummy is guaranteed to be added after the last of the data. Each process is guaranteed to consume no more than one dummy, so they will all wake up. If you don't like those, you could just use a time-out, which checks the contents of a shared variable, like a one-element array, then checks the queue to be empty. If the shared variable is True, and the queue is empty, there is no more data. I'm curious how these work and what you decide. From castironpi at gmail.com Fri May 1 18:03:30 2009 From: castironpi at gmail.com (Aaron Brady) Date: Fri, 1 May 2009 15:03:30 -0700 (PDT) Subject: Why bool( object )? References: <54cb7f8a-fef4-4bf8-8054-16dc9b5c8830@d2g2000pra.googlegroups.com> <49f85861$0$29010$426a74cc@news.free.fr> <4t_Jl.37718$VA5.20343@tornado.fastwebnet.it> <020ab4a3$0$20667$c3e8da3@news.astraweb.com> Message-ID: On May 1, 4:30?am, Steven D'Aprano wrote: > On Fri, 01 May 2009 16:30:19 +1200, Lawrence D'Oliveiro wrote: > > I have never written anything so unbelievable in my life. And I hope I > > never will. > > I didn't say you did. If anyone thought I was quoting Lawrence's code, > I'd be surprised. It was not my intention to put words into your mouth. > > But seeing as you have replied, perhaps you could tell us something. > Given so much you despise using non-bools in truth contexts, how would > you re-write my example to avoid "a or b or c"? > > for x in a or b or c: > ? ? do_something_with(x) I think Hendrik's is the closest so far, but still doesn't iterate over x: for t in [a,b,c]: if t: for x in t: do_something_with(x) break This is still not right, since Steven's code will raise an exception if 'a' and 'b' test False, and 'c' is non-iterable. >>> for x in 0 or 0 or 0: ... print( x ) ... Traceback (most recent call last): File "", line 1, in TypeError: 'int' object is not iterable >>> for x in 0 or 0 or []: ... print( x ) ... >>> for x in 0 or 0 or 1: ... print( x ) ... Traceback (most recent call last): File "", line 1, in TypeError: 'int' object is not iterable >>> for x in ['abc'] or 0 or 0: ... print( x ) ... abc To mimic it exactly, you'd have to actually convert the first one, execute 'or' on the other two, since they may have side-effects or return other values than themselves, and try iterating over the last one. I think you are looking at an 'ireduce' function, which doesn't exist in 'itertools' yet. I don't think it would be very common to write Steven's construction for arbitrary values of 'a', 'b', and 'c'. From rschroev_nospam_ml at fastmail.fm Fri May 1 18:04:45 2009 From: rschroev_nospam_ml at fastmail.fm (Roel Schroeven) Date: Sat, 02 May 2009 00:04:45 +0200 Subject: Multiprocessing.Queue - I want to end. In-Reply-To: References: <200904301649.10341.kyrie@uh.cu> Message-ID: <2mKKl.16353$B17.1674@newsfe26.ams2> Hendrik van Rooyen schreef: > I have always wondered why people do the one queue many getters thing. Because IMO it's the simplest and most elegant solution. > > Given that the stuff you pass is homogenous in that it will require a > similar amount of effort to process, is there not a case to be made > to have as many queues as consumers, and to round robin the work? Could work if the processing time for each work unit is exactly the same (otherwise one or more consumers will be idle part of the time), but in most cases that is not guaranteed. A simple example is fetching data over the network: even if the data size is always the same, there will be differences because of network load variations. If you use one queue, each consumer fetches a new work unit as soon it has consumed the previous one. All consumers will be working as long as there is work to do, without having to write any code to do the load balancing. With one queue for each consumer, you either have to assume that the average processing time is the same (otherwise some consumers will be idle at the end, while others are still busy processing work units), or you need some clever code in the producer(s) or the driving code to balance the loads. That's extra complexity for little or no benefit. I like the simplicity of having one queue: the producer(s) put work units on the queue with no concern which consumer will process them or how many consumers there even are; likewise the consumer(s) don't know and don't need to know where their work units come from. And the work gets automatically distributed to whichever consumer has first finished its previous work unit. > And if the stuff you pass around needs disparate effort to consume, > it seems to me that you can more easily balance the load by having > specialised consumers, instead of instances of one humungous > "I can eat anything" consumer. If there is a semantic difference, maybe yes; but I think it makes no sense to differentiate purely on the expected execution times. > I also think that having a queue per consumer thread makes it easier > to replace the threads with processes and the queues with pipes or > sockets if you need to do serious scaling later. Perhaps, but isn't that a case of YAGNI and/or premature optimization? -- The saddest aspect of life right now is that science gathers knowledge faster than society gathers wisdom. -- Isaac Asimov Roel Schroeven From piet at cs.uu.nl Fri May 1 18:08:55 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sat, 02 May 2009 00:08:55 +0200 Subject: string processing question References: <3fbff25a-530e-4364-a7e1-3db6255b0d6b@k19g2000prh.googlegroups.com> Message-ID: >>>>> Kurt Mueller (KM) wrote: >KM> But from the command line python interprets the code >KM> as 'latin_1' I presume. That is why I have to convert >KM> the "?" with unicode(). >KM> Am I right? There are a couple of stages: 1. Your terminal emulator interprets your keystrokes, encodes them in a sequence of bytes and passes them to the shell. How the characters are encodes depends on the encoding used in the terminal emulator. So for example when the terminal is set to utf-8, your "?" is converted to two bytes: \xc3 and \xa4. 2. The shell passes these bytes to the python command. 3. The python interpreter must interpret these bytes with some decoding. If you use them in a bytes string they are copied as such, so in the example above the string "?" will consist of the 2 bytes '\xc3\xa4'. If your terminal encoding would have been iso-8859-1, the string would have had a single byte '\xe4'. If you use it in a unicode string the Python parser has to convert it to unicode. If there is an encoding declaration in the source than that is used. Of course it should be the same as the actual encoding used by the shell (or the editor when you have a script saved in a file) otherwise you have a problem. If there is no encoding declaration in the source Python has to guess. It appears that in Python 2.x the default is iso-8859-1 but in Python 3.x it will be utf-8. You should avoid making any assumptions about this default. 4. During runtime unicode characters that have to be printed, written to a file, passed as file names or arguments to other processes etc. have to be encoded again to a sequence of bytes. In this case Python refuses to guess. Also you can't use the same encoding as in step 3, because the program can run on a completely different system than were it was compiled to byte code. So if the (unicode) string isn't ASCII and no encoding is given you get an error. The encoding can be given explicitely, or depending on the context, by sys.stdout.encoding, sys.getdefaultencoding or PYTHONIOENCODING (from 2.6 on). Unfortunately there is no equivalent to PYTHONIOENCODING for the interpretation of the source text, it only works on run-time. Example: python -c 'print len(u"?")' prints 2 on my system, because my terminal is utf-8 so the ? is passed as 2 bytes (\xc3\xa4), but these are interpreted by Python 2.6.2 as two iso-8859-1 bytes. If I do python -c 'print u"?"' in my terminal I therefore get two characters: ?? but if I do this in Emacs I get: UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128) because my Emacs doesn't pass the encoding of its terminal emulation. However: python -c '# -*- coding:utf-8 -*- print len(u"?")' will correctly print 1. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From nad at acm.org Fri May 1 18:10:26 2009 From: nad at acm.org (Ned Deily) Date: Fri, 01 May 2009 15:10:26 -0700 Subject: Help! Can't get program to run. References: <77a2fc3c-638d-4aef-bae9-3b1ea9d4e988@c36g2000yqn.googlegroups.com> <7618rjF1a3t8jU1@mid.uni-berlin.de> Message-ID: In article <7618rjF1a3t8jU1 at mid.uni-berlin.de>, "Diez B. Roggisch" wrote: > seanm.py at gmail.com schrieb: > > I think this is maybe the most basic problem possible, but I can't get > > even the most basic Python to run on OS X using Terminal or IDLE. I > > used the IDLE editor to create a file with one line of code > > > > print 'text string' > > > > and I saved the file as module1.py. When using terminal I entered > > "python" to load the interpreter or whatever and then tried > > > > python module1.py > > > > but I got an error message. I've tried using the file path instead of > > module1.py and that didn't work either. I've tried moving the file to > > different places, my desktop, hard drive, etc., and none of that > > worked either. In IDLE I tried similar things and only got error > > messages there too. > > > > Needless to say this is a frustrating start to learning this langauge. > > I'd really appreciate any help getting this to work, so I can move on > > the actual language. Thanks so much. > > It would have helped if you had given us the *actual* error-message. > From what little information you give, it appears as if you do > something very basic wrong - instead of doing > $ python > >>> python mymodule.py > (where $ is the shell/terminal and >>> the python-prompt) > you either do > $ python > >>> print "hello" > or > $ python mymodule.py > to execute mymodule directly. And from within OS X IDLE itself, if you create or open the file mymodule.py, it will be in a separate window and, as long as that window is selected, you can run the file directly within IDLE by selecting "Run Module" from the "Run" menu. So, no need to use the Terminal if you don't want to. -- Ned Deily, nad at acm.org From castironpi at gmail.com Fri May 1 18:13:34 2009 From: castironpi at gmail.com (Aaron Brady) Date: Fri, 1 May 2009 15:13:34 -0700 (PDT) Subject: Multiprocessing Pool and functions with many arguments References: <880bfeb5-cc71-4ff5-b7a6-ffd6404597f0@v23g2000pro.googlegroups.com> Message-ID: <678d4c8b-63e0-4b63-8623-bd730b451d35@w40g2000yqd.googlegroups.com> On Apr 29, 1:01?pm, "psaff... at googlemail.com" wrote: > I'm trying to get to grips with the multiprocessing module, having > only used ParallelPython before. > > based on this example: > > http://docs.python.org/library/multiprocessing.html#using-a-pool-of-w... > > what happens if I want my "f" to take more than one argument? I want > to have a list of tuples of arguments and have these correspond the > arguments in f, but it keeps complaining that I only have one argument > (the tuple). Do I have to pass in a tuple and break it up inside f? I > can't use multiple input lists, as I would with regular map. > > Thanks, > > Peter The basic constructor should allow it. http://docs.python.org/library/multiprocessing.html#the-process-class p = Process(target=f, args=('bob',)) I think you are imagining: result = pool.apply_async(f, [ *(1,2,3), *(4,5,6) ] ) But I don't know of a way. From rpmuller at gmail.com Fri May 1 18:21:11 2009 From: rpmuller at gmail.com (Rick) Date: Fri, 1 May 2009 15:21:11 -0700 (PDT) Subject: Profiling gives very different predictions of best algorithm References: Message-ID: Sorry, I'm running the function: def debugsuite(): import profile,pstats profile.run('runsuite()','prof') prof = pstats.Stats('prof') prof.strip_dirs().sort_stats('time').print_stats(15) where runsuite() runs the Hartree-Fock energy of a water molecule, and is given by: import unittest,logging from PyQuante.CI import CIS from PyQuante.Molecule import Molecule from PyQuante.MP import MP2 from PyQuante.OEP import oep_hf,oep_hf_an from PyQuante.PyQuante2 import SCF,SubspaceSolver,DmatSolver class UnitTests(unittest.TestCase): def setUp(self): from PyQuante.Molecule import Molecule self.h2o = Molecule('h2o',[(8,(0,0,0)),(1,(1.,0,0)),(1, (0,1.,0))], units="Angstrom") def testH2OHF(self): h2o_hf = SCF(self.h2o,method='HF') h2o_hf.iterate() self.assertAlmostEqual(h2o_hf.energy,-76.011755864850628,4) def runsuite(verbose=True): # To use psyco, uncomment this line: #import psyco; psyco.full() if verbose: verbosity=2 else: verbosity=1 # If you want more output, uncomment this line: #logging.basicConfig(format="%(message)s",level=logging.DEBUG) suite = unittest.TestLoader().loadTestsFromTestCase(UnitTests) unittest.TextTestRunner(verbosity=verbosity).run(suite) # Running without verbosity is equivalent to replacing the above # two lines with the following: #unittest.main() return On May 1, 3:50?pm, Terry Reedy wrote: > Rick Muller wrote: > > I'm the main programmer for the PyQuante package, a quantum chemistry > > package in Python. I'm trying to speed up one of my rate determining > > steps. Essentially, I have to decide between two algorithms: > > > 1. Packed means that I compute N**4/8 integrals, and then do a bunch > > of indexing operations to unpack; > > 2. Unpacked means that I compute all N**4 integrals, but don't have to > > do any indexing. > > > Raw timing the two options show that packed is clearly faster (12.5 > > sec vs 20.6 sec). However, the profilings show very different results. > > I have the results below. Clearly I'm going to use the packed scheme. > > My question to the mailing list is what am I doing wrong with my > > profiling that it shows such poor predictions? > > That *might* be easier to answer if you were to show exactly what you > did to get the odd-looking results ;-) From guenther.dietrich at spamfence.net Fri May 1 18:28:45 2009 From: guenther.dietrich at spamfence.net (=?UTF-8?Q?G=C3=BCnther?= Dietrich) Date: Sat, 02 May 2009 00:28:45 +0200 Subject: ctypes: reference of a struct member? References: Message-ID: ma wrote: >If I have this struct in C: > >struct spam { > int ham; > char foo; >}; > > >if I have this declaration: >struct spam s_; > >If I wanted to pass a reference to a function of s_'s foo character, I >can do something like this: > >somefunc(&s_.foo) > >How do I do the same thing in ctypes? >ctypes.addressof(s_) + ctypes.sizeof(spam.foo)*(1) See ctypes.create_string_buffer(). To unpack the struct elements received from the C function, use struct.unpack(). Here some example: def rssiam_volt_Q(self): '''Output Voltage Query ''' query_buffer = ctypes.create_string_buffer(8) self.rssiam_32_dll.rssiam_volt_Q.argtypes = [ctypes.c_long, ctypes.c_char_p] result = self.rssiam_32_dll.rssiam_volt_Q(self.session, query_buffer) if result != VI_SUCCESS: raise IOError, 'rssiam_volt_Q: result = %d' % result return(struct.unpack('d', query_buffer.raw)[0]) In this case, it is a 64 bits float (= double) value that is returned by reference (query_buffer). Attention, struct.unpack() returns a tuple, even when there is only a single element to extract from the buffer. Regards, G?nther From http Fri May 1 18:47:49 2009 From: http (Paul Rubin) Date: 01 May 2009 15:47:49 -0700 Subject: Why bool( object )? References: <54cb7f8a-fef4-4bf8-8054-16dc9b5c8830@d2g2000pra.googlegroups.com> <49f85861$0$29010$426a74cc@news.free.fr> <4t_Jl.37718$VA5.20343@tornado.fastwebnet.it> <020ab4a3$0$20667$c3e8da3@news.astraweb.com> Message-ID: <7xmy9w1luy.fsf@ruckus.brouhaha.com> Aaron Brady writes: > I think you are looking at an 'ireduce' function, which doesn't > exist in 'itertools' yet. Nothing is being done with the return value. sum(1 for x in imap(func, seq)) is enough to force evaluation of func on each element of seq. From piet at cs.uu.nl Fri May 1 18:57:09 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sat, 02 May 2009 00:57:09 +0200 Subject: urllib2 and threading References: Message-ID: >>>>> robean (R) wrote: >R> def get_info_from_url(url): >R> """ A dummy version of the function simply visits urls and prints >R> the url of the page. """ >R> try: >R> page = urllib2.urlopen(url) >R> except urllib2.URLError, e: >R> print "**** error ****", e.reason >R> except urllib2.HTTPError, e: >R> print "**** error ****", e.code There's a problem here. HTTPError is a subclass of URLError so it should be first. Otherwise when you have an HTTPError (like a 404 File not found) it will be caught by the "except URLError", but it will not have a reason attribute, and then you get an exception in the except clause and the thread will crash. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From malkocb at gmail.com Fri May 1 19:04:04 2009 From: malkocb at gmail.com (Berkin Malkoc) Date: Sat, 2 May 2009 02:04:04 +0300 Subject: is it possible to traverse two lists simulatenously using python In-Reply-To: References: Message-ID: <3e22924c0905011604v5a8b08dcj464959f12b267988@mail.gmail.com> You should be looking for "zip".. On Thu, Apr 30, 2009 at 9:31 AM, wrote: > Python > for i,j in topgirls, richgirls: > print i,j > > Cheetah > #for $i,$j in $topgirls, $richgirls > $i, $j > #end for > This doesnt work > > -- > Bidegg worlds best auction site > http://bidegg.com > > -- > http://mail.python.org/mailman/listinfo/python-list > > From fetchinson at googlemail.com Fri May 1 19:18:09 2009 From: fetchinson at googlemail.com (Daniel Fetchinson) Date: Fri, 1 May 2009 16:18:09 -0700 Subject: is it possible to traverse two lists simulatenously using python In-Reply-To: References: Message-ID: > Python > for i,j in topgirls, richgirls: > print i,j for i, j in zip( topgirls, richgirls ): print i,j HTH, Daniel > Cheetah > #for $i,$j in $topgirls, $richgirls$i, $j > #end for > This doesnt work -- Psss, psss, put it down! - http://www.cafepress.com/putitdown From mark.seagoe at gmail.com Fri May 1 19:31:38 2009 From: mark.seagoe at gmail.com (mark.seagoe at gmail.com) Date: Fri, 1 May 2009 16:31:38 -0700 (PDT) Subject: web access through vpn client Message-ID: <0aa2beb9-b2b1-4bba-b130-e88821b914e8@d19g2000prh.googlegroups.com> Hi; I am trying to retrieve financial data off website for stock market analysis. Just hobby not for pay. I actually am impressed that urllib2 and BeautifulSoup work pretty well to do what I want, and the first little routine actually gets the data from the web page... except if my VPN client is turned on. I've looked around for a solution but can't find... perhaps I'm not entering the right search words. I don't even know where to start. Can anyone suggest some good reading, or basic idea how to approach? Thanks, Mark From norseman at hughes.net Fri May 1 19:58:50 2009 From: norseman at hughes.net (norseman) Date: Fri, 01 May 2009 16:58:50 -0700 Subject: string processing question In-Reply-To: References: <3fbff25a-530e-4364-a7e1-3db6255b0d6b@k19g2000prh.googlegroups.com> Message-ID: <49FB8CBA.5090803@hughes.net> Piet van Oostrum wrote: >>>>>> Kurt Mueller (KM) wrote: > >> KM> But from the command line python interprets the code >> KM> as 'latin_1' I presume. That is why I have to convert >> KM> the "?" with unicode(). >> KM> Am I right? > > There are a couple of stages: > 1. Your terminal emulator interprets your keystrokes, encodes them in a > sequence of bytes and passes them to the shell. How the characters > are encodes depends on the encoding used in the terminal emulator. So > for example when the terminal is set to utf-8, your "?" is converted > to two bytes: \xc3 and \xa4. > 2. The shell passes these bytes to the python command. > 3. The python interpreter must interpret these bytes with some decoding. > If you use them in a bytes string they are copied as such, so in the > example above the string "?" will consist of the 2 bytes '\xc3\xa4'. > If your terminal encoding would have been iso-8859-1, the string > would have had a single byte '\xe4'. If you use it in a unicode > string the Python parser has to convert it to unicode. If there is an > encoding declaration in the source than that is used. Of course it > should be the same as the actual encoding used by the shell (or the > editor when you have a script saved in a file) otherwise you have a > problem. If there is no encoding declaration in the source Python has > to guess. It appears that in Python 2.x the default is iso-8859-1 but > in Python 3.x it will be utf-8. You should avoid making any > assumptions about this default. > 4. During runtime unicode characters that have to be printed, written to > a file, passed as file names or arguments to other processes etc. > have to be encoded again to a sequence of bytes. In this case Python > refuses to guess. Also you can't use the same encoding as in step 3, > because the program can run on a completely different system than > were it was compiled to byte code. So if the (unicode) string isn't > ASCII and no encoding is given you get an error. The encoding can be > given explicitely, or depending on the context, by sys.stdout.encoding, > sys.getdefaultencoding or PYTHONIOENCODING (from 2.6 on). > > Unfortunately there is no equivalent to PYTHONIOENCODING for the > interpretation of the source text, it only works on run-time. > > Example: > python -c 'print len(u"?")' > prints 2 on my system, because my terminal is utf-8 so the ? is passed > as 2 bytes (\xc3\xa4), but these are interpreted by Python 2.6.2 as two > iso-8859-1 bytes. > > If I do > python -c 'print u"?"' in my terminal I therefore get two characters: ?? > but if I do this in Emacs I get: > UnicodeEncodeError: 'ascii' codec can't encode characters in position > 0-1: ordinal not in range(128) > because my Emacs doesn't pass the encoding of its terminal emulation. > > However: > python -c '# -*- coding:utf-8 -*- > print len(u"?")' > will correctly print 1. =============================== Thank you. I knew there had to be something simpler than brute force. I have missed seeing the explanations for: python -c '# -*- coding:utf-8 -*- in the 2.5 docs. Where can I find these? (the python -c is for config, I presume?) By the way - the however: python...\nprint... snippet bombs in 2.5.2 1st bomb: looking for closing ' #so I add one and remove one below 2nd bomb: bad syntax # I play awhile and join EMACS 3rd bomb: Non-ASCII character '\xe4' in file....no encoding declared.. Python flatly states it's not ASCII and quits. Python print refuses to handle high bit set bytes in 2.5.2.... The thank you is for pointing out how it works. I can use sed to fix for file listing purposes. (Python won't like them, but a second pass thru sed can give me something python can use and the two names can go on a line on the cheat sheet.) Barry, Kurt - do understand using sed to change the incoming names? Put the python in a box and use the Linux mc, ls, sed and echo routines to get the names into a form python can use while making the cheat sheet at the same time. Substitutions like a for ? will generally be acceptable. Yes or No? The cheat sheet can show the ? in the original name because the OS functions allow it. I have no doubt there will be some exceptions. :( Once the names are "ASCII" you can get the python out & put it to work. Just to head off the comments that it's not .... whatever ls -1 | cheater.scr | python_program.py IS PURE UNIX Unix is designed for this. Files from different parts of the world? If you can see the name as something besides ????? make a cheeter for each 'Page'. mc /path/to/dir/of/choice ls -1 >dummy highlight dummy F3 F4 and read the hex takes me longer to type it in here than to do it. (leading spaces) :) Today: 20090430 Steve ps. Piet - thanks for including the version specifics. It makes a huge difference in expectations and allowances. From apt.shansen at gmail.com Fri May 1 20:41:03 2009 From: apt.shansen at gmail.com (Stephen Hansen) Date: Fri, 1 May 2009 17:41:03 -0700 Subject: web access through vpn client In-Reply-To: <0aa2beb9-b2b1-4bba-b130-e88821b914e8@d19g2000prh.googlegroups.com> References: <0aa2beb9-b2b1-4bba-b130-e88821b914e8@d19g2000prh.googlegroups.com> Message-ID: <7a9c25c20905011741m6b5d4360yf32acdf99238a685@mail.gmail.com> > > I am trying to retrieve financial data off website for stock market > analysis. Just hobby not for pay. I actually am impressed that > urllib2 and BeautifulSoup work pretty well to do what I want, and the > first little routine actually gets the data from the web page... > except if my VPN client is turned on. > Usually when something like that happens with a VPN its because the VPN has been configured to do so. Its the default usually and is a security precaution. The exact wording of the option varies from VPN client to VPN client; and its usually hidden deep in 'advanced'. E.g., on the mac its "Send all traffic over VPN connection", on Windows I /believe/ its something like "Set as default gateway" but its been awhile since I had to tweak my windows install. No idea what it looks like on the Cisco client. --S -------------- next part -------------- An HTML attachment was scrubbed... URL: From MrJean1 at gmail.com Fri May 1 20:50:37 2009 From: MrJean1 at gmail.com (Jean) Date: Fri, 1 May 2009 17:50:37 -0700 (PDT) Subject: How to measure the memory cost in Python? References: <11aabe3b0905010103r14b05fccg7a02c6ae010cd9ad@mail.gmail.com> <7e908367-0604-4400-951e-9e172befc7e7@w31g2000prd.googlegroups.com> Message-ID: <75e47cc6-72bc-4941-802e-767f2187445f@d7g2000prl.googlegroups.com> On May 1, 12:50?pm, Jean wrote: > On May 1, 10:56?am, CTO wrote: > > > > > > sys.getsizeof() [a suggested solution] isn't platform-specific. > > > So, to answer the OP's question, you'd just do something like > > > def get_totalsize(obj): > > ? ? ? ? total_size = sys.getsizeof(obj) > > ? ? ? ? for value in vars(obj).values(): > > ? ? ? ? ? ? ? ? try: total_size += get_total_size(value) > > ? ? ? ? ? ? ? ? except: total_size += sys.getsizeof(value) > > ? ? ? ? return totalSize > > > def get_current_size(env): > > ? ? ? ? size = 0 > > ? ? ? ? for value in env.values(): > > ? ? ? ? ? ? ? ? try: size += get_total_size(value) > > ? ? ? ? ? ? ? ? except: pass > > ? ? ? ? return size > > > get_current_size(vars()) > > > and discount the weight of the interpreter? > > Keep in mind, sys.getsizeof(obj) returns only the size of the given > object. ?Any referenced objects are not included. ?You can get the > latter from gc.get_referents(obj). > > /Jean Brouwers > > PS) The asizeof(obj) function from this recipe code.activestate.com/recipes/546530> does size the object plus its > references, recursively. Correction, the last sentence should be: The asizeof(obj) ... plus its referents, recursively. From gert.cuykens at gmail.com Fri May 1 20:56:24 2009 From: gert.cuykens at gmail.com (gert) Date: Fri, 1 May 2009 17:56:24 -0700 (PDT) Subject: eval(WsgiApplication) Message-ID: <8357032b-5fc0-4d4c-843a-a1ea27c62e5c@o14g2000vbo.googlegroups.com> I would like to read the following from a text file from json import loads from gert.db import Db def application(environ, response): v = loads(environ['wsgi.input'].read(int(environ ['CONTENT_LENGTH'])).decode('utf-8')) db = Db() db.execute('UPDATE votes SET count=count+1 WHERE vid=?',(v ['vid'],)) db.execute('SELECT * FROM votes') j = '{"rec":'+db.json()+',\n' j+= ' "des":'+db.jdes()+'}' j = j.encode('utf-8') response('200 OK', [('Content-type', 'text/ javascript;charset=utf-8'), ('Content-Length', str(len(j)))]) return [j] execute it, and wrap a new function name around it for example def wrapper(environ, response): exec(file) return application(environ, response) How do I do this in python3? From martin.hellwig at dcuktec.org Fri May 1 20:57:32 2009 From: martin.hellwig at dcuktec.org (Martin P. Hellwig) Date: Sat, 02 May 2009 01:57:32 +0100 Subject: web access through vpn client In-Reply-To: <0aa2beb9-b2b1-4bba-b130-e88821b914e8@d19g2000prh.googlegroups.com> References: <0aa2beb9-b2b1-4bba-b130-e88821b914e8@d19g2000prh.googlegroups.com> Message-ID: mark.seagoe at gmail.com wrote: > Hi; > > I am trying to retrieve financial data off website for stock market > analysis. Just hobby not for pay. I actually am impressed that > urllib2 and BeautifulSoup work pretty well to do what I want, and the > first little routine actually gets the data from the web page... > except if my VPN client is turned on. > > I've looked around for a solution but can't find... perhaps I'm not > entering the right search words. I don't even know where to start. > Can anyone suggest some good reading, or basic idea how to approach? > > Thanks, > Mark When the VPN client is on, can you actually still browse the 'classical' way to the website in question? I ask this because many cooperate VPN clients have a way of shutting down all other access besides to the VPN server (which is very annoying but I can see the logic behind it). If that is the case your VPN network might have a webproxy which needs to be configured to continue browsing when connected to the VPN. For python you can automate this by checking for access to the proxy and use it if it is there. -- MPH http://blog.dcuktec.com From tjreedy at udel.edu Fri May 1 21:29:26 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 01 May 2009 21:29:26 -0400 Subject: is it possible to traverse two lists simulatenously using python In-Reply-To: References: Message-ID: mobiledreamers at gmail.com wrote: > Python > for i,j in topgirls, richgirls: > print i,j for i,y in zip(a,b): ... From tjreedy at udel.edu Fri May 1 21:38:12 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 01 May 2009 21:38:12 -0400 Subject: Profiling gives very different predictions of best algorithm In-Reply-To: References: Message-ID: Rick wrote: [Rearrangee to put answer after question. Unless your name is Guido and you are making a short pronouncement that you want to be sure everyone sees, please do not toppost] >>> My question to the mailing list is what am I doing wrong with my >>> profiling that it shows such poor predictions? [i wrote] >> That *might* be easier to answer if you were to show exactly what you >> did to get the odd-looking results ;-) > Sorry, I'm running the function: > > def debugsuite(): > import profile,pstats > profile.run('runsuite()','prof') > prof = pstats.Stats('prof') > prof.strip_dirs().sort_stats('time').print_stats(15) > > where runsuite() runs the Hartree-Fock energy of a water molecule, and > is given by: > > > import unittest,logging > from PyQuante.CI import CIS > from PyQuante.Molecule import Molecule > from PyQuante.MP import MP2 > from PyQuante.OEP import oep_hf,oep_hf_an > from PyQuante.PyQuante2 import SCF,SubspaceSolver,DmatSolver As far as I could see, you only actually use Molecule and SCF > > class UnitTests(unittest.TestCase): > def setUp(self): > from PyQuante.Molecule import Molecule and you repeat the Molecule import here. > self.h2o = Molecule('h2o',[(8,(0,0,0)),(1,(1.,0,0)),(1, > (0,1.,0))], > units="Angstrom") > > def testH2OHF(self): > h2o_hf = SCF(self.h2o,method='HF') > h2o_hf.iterate() I presume that this is where the time goes. > self.assertAlmostEqual(h2o_hf.energy,-76.011755864850628,4) > > def runsuite(verbose=True): > # To use psyco, uncomment this line: > #import psyco; psyco.full() > if verbose: verbosity=2 > else: verbosity=1 > # If you want more output, uncomment this line: > #logging.basicConfig(format="%(message)s",level=logging.DEBUG) > suite = unittest.TestLoader().loadTestsFromTestCase(UnitTests) > unittest.TextTestRunner(verbosity=verbosity).run(suite) > # Running without verbosity is equivalent to replacing the above > # two lines with the following: > #unittest.main() > return I presume in your overall time text, you ran the two versions of the algorith 'naked'. But, for some reason, you are profiling them embedded inside a test suite and runner. It does not seem that this should affect relative timing, but I have seen some pretty strange behaviors. At best, it will add noise. Let me expand my question: what did you do differently between the two profile runs? tjr From steve at REMOVE-THIS-cybersource.com.au Fri May 1 21:47:32 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 02 May 2009 01:47:32 GMT Subject: Why bool( object )? References: <54cb7f8a-fef4-4bf8-8054-16dc9b5c8830@d2g2000pra.googlegroups.com> <49f85861$0$29010$426a74cc@news.free.fr> <4t_Jl.37718$VA5.20343@tornado.fastwebnet.it> <020ab4a3$0$20667$c3e8da3@news.astraweb.com> Message-ID: <020b99a5$0$20667$c3e8da3@news.astraweb.com> On Fri, 01 May 2009 15:03:30 -0700, Aaron Brady wrote: > On May 1, 4:30?am, Steven D'Aprano cybersource.com.au> wrote: >> On Fri, 01 May 2009 16:30:19 +1200, Lawrence D'Oliveiro wrote: >> > I have never written anything so unbelievable in my life. And I hope >> > I never will. >> >> I didn't say you did. If anyone thought I was quoting Lawrence's code, >> I'd be surprised. It was not my intention to put words into your mouth. >> >> But seeing as you have replied, perhaps you could tell us something. >> Given so much you despise using non-bools in truth contexts, how would >> you re-write my example to avoid "a or b or c"? >> >> for x in a or b or c: >> ? ? do_something_with(x) [...] > I don't think it would be very common to write Steven's construction for > arbitrary values of 'a', 'b', and 'c'. I don't care about "arbitrary values" for a, b and c. I don't expect a solution that works for (say) a=None, b=5, c=[]. I'm happy to restrict the arguments to all be arbitrary sequence-like objects. I'm even happy for somebody to give a solution with further restrictions, like "if I know before hand that all three are lists, then I do blah...". But state your restrictions up front. -- Steven From steve at REMOVE-THIS-cybersource.com.au Fri May 1 21:52:27 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 02 May 2009 01:52:27 GMT Subject: Question about the wording in the python documents. References: Message-ID: <020b9acb$0$20667$c3e8da3@news.astraweb.com> On Fri, 01 May 2009 13:02:59 -0700, grocery_stocker wrote: > I don't get how urllib2.urlopen() can take a Request object. When I do > the following.... > > [cdalten at localhost ~]$ python > Python 2.4.3 (#1, Oct 1 2006, 18:00:19) [GCC 4.1.1 20060928 (Red Hat > 4.1.1-28)] on linux2 Type "help", "copyright", "credits" or "license" > for more information. >>>> import urllib2 >>>> >>>> url = 'http://www.google.com' >>>> req = urllib2.Request(url) >>>> response = urllib2.urlopen(req) >>>> >>>> > req is clearly an instance of urllib2.Request and not a Request object. It looks like a Request object to me. You create it by calling urllib2.Request(url), and it's an object. I don't understand your objection. Is it that the documentation calls it Request instead of urllib2.Request? Or that it calls it an object instead of an instance? In either case, I think you're picking a nit so small that it isn't actually there. All objects are instances (in Python), and all instances are objects. And it should be clear from context that Request refers to urllib2.Request, and not some mythical built-in Request object. I don't believe that needs to be spelled out. -- Steven From dayscondor at ameritech.net Fri May 1 22:10:16 2009 From: dayscondor at ameritech.net (watermod) Date: Sat, 02 May 2009 02:10:16 GMT Subject: debian apt somehow created python hell! - help Message-ID: I was doing one of those auto apt-get gui things with update manager in Debian and not watching the error log so I don't know where it started. It involves only Python stuff. I don't know Python but apps use it. The current apt state is that python apps are failing install in: /usr/sbin/update-python-modules typical error list looks like: dpkg: warning - old pre-removal script returned error exit status 1 dpkg - trying script from the new package instead ... WARNING: python-gtk2-doc.private does not exist. Some bytecompiled files may be left behind. Traceback (most recent call last): File "/usr/sbin/update-python-modules", line 437, in public_packages[package].install(need_postinstall) File "/usr/sbin/update-python-modules", line 232, in __getitem__ self[name] = SharedFileList (path) File "/usr/sbin/update-python-modules", line 146, in __init__ for line in file(path): IOError: [Errno 21] Is a directory dpkg: error processing /var/cache/apt/archives/python-gtk2- doc_2.14.1-2_all.deb (--unpack): subprocess new pre-removal script returned error exit status 1 Traceback (most recent call last): File "/usr/sbin/update-python-modules", line 437, in public_packages[package].install(need_postinstall) File "/usr/sbin/update-python-modules", line 232, in __getitem__ self[name] = SharedFileList (path) File "/usr/sbin/update-python-modules", line 146, in __init__ for line in file(path): IOError: [Errno 21] Is a directory dpkg: error while cleaning up: on and on and on.... The following: dpkg -C gives: The following packages are in a mess due to serious problems during installation. They must be reinstalled for them (and any packages that depend on them) to function properly: python-reportbug Python modules for interacting with bug tracking systems lsb-release Linux Standard Base version reporting utility python-ogg Python interface to the Ogg library system-config-lvm A utility for graphically configuring Logical Volumes bitpim utility to communicate with many CDMA phones bitpim-lib architecture-dependent helper files for BitPim python-glade2 GTK+ bindings: Glade support python-gtk2 Python bindings for the GTK+ widget set python-feedparser Universal Feed Parser for Python python-gtk2-doc Python bindings for the GTK+ widget set - documentation k3d 3D modeling and animation system The following packages have been unpacked but not yet configured. They must be configured using dpkg --configure or the configure menu option in dselect for them to work: acroread-plugins Plugins for Adobe Acrobat(R) Reader python-roman module for generating/analyzing Roman numerals python-soappy SOAP Support for Python python-glpk Python bindings to the GNU Linear Programming Kit python-dialog Python module for making simple Text/Console-mode user in python-biggles Scientific plotting package for Python python-gd Python module wrapper for libgd python-clutter Open GL based interactive canvas library - Python binding python-decoratortools version-agnostic decorators support for Python python-fpconst Utilities for handling IEEE 754 floating point special va acroread-data data files for acroread python-galago-gtk GTK+ widgets for the Galago presence library (Python inte python-utmp python module for working with utmp python-debianbts Python interface to Debian's Bug Tracking System python-gobject-dev Development headers for the GObject Python bindings acroread-escript Adobe EScript Plug-In python-dhm collection of Python utilities / helper python-unit unit test framework for Python acpid Utilities for using ACPI power management python-sqlalchemy SQL toolkit and Object Relational Mapper for Python python-gnutls Python wrapper for the GNUTLS library python-xlib Interface for Python to the X11 Protocol acroread Adobe Acrobat Reader: Portable Document Format file viewe python-pyinotify simple Linux inotify Python bindings python-facebook Python wrappers for the Facebook API reportbug reports bugs in the Debian distribution python-ll-core Python modules for colors, make, cron, daemons, URLs, tem python-beaker Simple WSGI middleware that uses the Myghty Container API python-xcbgen X C Binding - protocol binding generator libroot-dev Header files for ROOT python-dcop DCOP bindings for Python python-docutils utilities for the documentation of Python modules libroot-python-dev Python extension for ROOT - development files python-extractor extracts meta-data from files of arbitrary type (Python b python-debian Python modules to work with Debian-related data formats python-cheetah text-based template engine and Python code generator python-galago Galago presence library (Python interface) The following packages are only half configured, probably due to problems configuring them the first time. The configuration should be retried using dpkg --configure or the configure menu option in dselect: python-support automated rebuilding support for Python modules The last one appears to be a key problem.... reconfigure gives the same dam error. Chicken egg problem of some sort... I can't add or remove anything python with the debian package tools... Maybe I could install by hand by a tarball if I knew where to stick stuff I unpacked a tarball to: /usr/src/python-support-1.0.2 but have no idea what to do... I am not a python guy... c, c++ perl but brain dead for this... /usr/src/python-support-1.0.2# ls -CF COPYING debian/ movemodules* pysupport.py README update-python-modules* debhelper/ hold parseversions* python-support.private runtime/ update-python-modules.8 SO!!! HELP ... SUGGESTIONS? From mark.seagoe at gmail.com Fri May 1 22:50:58 2009 From: mark.seagoe at gmail.com (mark.seagoe at gmail.com) Date: Fri, 1 May 2009 19:50:58 -0700 (PDT) Subject: web access through vpn client References: <0aa2beb9-b2b1-4bba-b130-e88821b914e8@d19g2000prh.googlegroups.com> Message-ID: <3b82989a-7e54-461a-b18f-dc90d803c0f9@x29g2000prf.googlegroups.com> On May 1, 5:57?pm, "Martin P. Hellwig" wrote: > mark.sea... at gmail.com wrote: > > Hi; > > > I am trying to retrieve financial data off website for stock market > > analysis. ?Just hobby not for pay. ?I actually am impressed that > > urllib2 and BeautifulSoup work pretty well to do what I want, and the > > first little routine actually gets the data from the web page... > > except if my VPN client is turned on. > > > I've looked around for a solution but can't find... perhaps I'm not > > entering the right search words. ?I don't even know where to start. > > Can anyone suggest some good reading, or basic idea how to approach? > > > Thanks, > > Mark > > When the VPN client is on, can you actually still browse the 'classical' > way to the website in question? > I ask this because many cooperate VPN clients have a way of shutting > down all other access besides to the VPN server (which is very annoying > but I can see the logic behind it). > > If that is the case your VPN network might have a webproxy which needs > to be configured to continue browsing when connected to the VPN. For > python you can automate this by checking for access to the proxy and use > it if it is there. > > -- > MPHhttp://blog.dcuktec.com Yes when Cisco AnyConnect VPN client is connected, I can browse w/ IE. I think the packets get encrypted and possibly go to my company's server and out from there, because then also Outlook connects to my company, Communicator works, etc. With VPN off I can browse and Python can get URL, but no connection through my company's server & no encryption. Where can I find out more about how to configure the webproxy, if the info is handy? Thanks! Mark From gagsl-py2 at yahoo.com.ar Fri May 1 23:51:29 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Sat, 02 May 2009 00:51:29 -0300 Subject: ctypes: reference of a struct member? References: <148918f0905011039t1a9a8561nf36c6fe4534e3694@mail.gmail.com> Message-ID: En Fri, 01 May 2009 14:39:39 -0300, ma escribi?: > If I have this struct in C: > > struct spam { > int ham; > char foo; > }; > > > if I have this declaration: > struct spam s_; > > If I wanted to pass a reference to a function of s_'s foo character, I > can do something like this: > > somefunc(&s_.foo) > > How do I do the same thing in ctypes? > ctypes.addressof(s_) + ctypes.sizeof(spam.foo)*(1) slightly less ugly: ctypes.addressof(s_) + spam.foo.offset -- Gabriel Genellina From gagsl-py2 at yahoo.com.ar Fri May 1 23:56:50 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Sat, 02 May 2009 00:56:50 -0300 Subject: ctypes: reference of a struct member? References: <148918f0905011039t1a9a8561nf36c6fe4534e3694@mail.gmail.com> Message-ID: En Sat, 02 May 2009 00:51:29 -0300, Gabriel Genellina escribi?: > En Fri, 01 May 2009 14:39:39 -0300, ma escribi?: > >> If I have this struct in C: >> >> struct spam { >> int ham; >> char foo; >> }; >> >> >> if I have this declaration: >> struct spam s_; >> >> If I wanted to pass a reference to a function of s_'s foo character, I >> can do something like this: >> >> somefunc(&s_.foo) >> >> How do I do the same thing in ctypes? >> ctypes.addressof(s_) + ctypes.sizeof(spam.foo)*(1) > > ctypes.addressof(s_) + spam.foo.offset Another way: ctypes from Python 2.6 lets you say: byref(s_, spam.foo.offset) -- Gabriel Genellina From aahz at pythoncraft.com Sat May 2 00:16:38 2009 From: aahz at pythoncraft.com (Aahz) Date: 1 May 2009 21:16:38 -0700 Subject: urllib2 and threading References: Message-ID: In article , robean wrote: > >Here's the problem: the script simply crashes after getting a a couple >of urls and takes a long time to run (slower that a non-threaded >version that I wrote and ran). Can anyone figure out what I am doing >wrong? I am new to both threading and urllib2, so its possible that >the SNAFU is quite obvious. For an example, see http://www.pythoncraft.com/OSCON2001/index.html -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "Typing is cheap. Thinking is expensive." --Roy Smith From research at johnohagan.com Sat May 2 00:22:20 2009 From: research at johnohagan.com (John O'Hagan) Date: Sat, 2 May 2009 04:22:20 +0000 Subject: object query assigned variable name? In-Reply-To: References: Message-ID: <200905020422.20895.research@johnohagan.com> On Fri, 1 May 2009, warpcat wrote: > I've passed this around some other groups, and I'm being told > "probably not possible". But I thought I'd try here as well :) I > *did* search first, and found several similar threads, but they > quickly tangented into other specifics of the language that were a bit > over my head :) At any rate, here's a simple example, I'd love to > know if as shown, is somehow possible: > > Given an object: > > class Spam(object): > def __init__(self): > # stuff.... > > I'd like it to print, when instanced, something like this: > >>> s = Spam() > > I?m assigned to s! If you just want the names an instance has in a given namespace, you could give your class a method like: class KnowNames(object): def get_names(self, namespace): id_str = str(hex(id(self))[:-1]) return [i for i in namespace if id_str in str(namespace[i])] which will give you a list of names when called on an instance. But if you try moving that method inside __init__(), it returns an empty list because any assignment is not yet in the namespace. I can see that it's tantalizing, though, because _somebody_ must know about the assignment; after all, we just executed it! Regards, John From research at johnohagan.com Sat May 2 00:48:47 2009 From: research at johnohagan.com (John O'Hagan) Date: Sat, 2 May 2009 04:48:47 +0000 Subject: debian apt somehow created python hell! - help In-Reply-To: References: Message-ID: <200905020448.48038.research@johnohagan.com> On Sat, 2 May 2009, watermod wrote: > I was doing one of those auto apt-get gui things with update manager in > Debian and not watching the error log so I don't know where it started. > > It involves only Python stuff. I don't know Python but apps use it. > > > The current apt state is that python apps are failing install in: > /usr/sbin/update-python-modules > > typical error list looks like: > [snip Debian package-management errors] I sympathise, but those errors are typical of a borked Debian upgrade (usually best fixed by waiting or downgrading if the problem is in the repository itself, else by manually reinstalling the messed-up package if the problem is local), not a python problem, so you'll get better answers on, say, debian-users. John From ben+python at benfinney.id.au Sat May 2 01:40:50 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Sat, 02 May 2009 15:40:50 +1000 Subject: debian apt somehow created python hell! - help References: Message-ID: <87r5z8m599.fsf@benfinney.id.au> John O'Hagan writes: > On Sat, 2 May 2009, watermod wrote: > > The current apt state is that python apps are failing install in: > > /usr/sbin/update-python-modules > > > [snip Debian package-management errors] > ? you'll get better answerson, say, debian-users. John refers to the Debian user discussion forum, a mailing list at . -- \ ?Compulsory unification of opinion achieves only the unanimity | `\ of the graveyard.? ?Justice Roberts in 319 U.S. 624 (1943) | _o__) | Ben Finney From krmane at gmail.com Sat May 2 03:45:22 2009 From: krmane at gmail.com (Krishnakant) Date: Sat, 02 May 2009 13:15:22 +0530 Subject: problem with money datatype based calculations in python Message-ID: <1241250322.4669.13.camel@kk-laptop> hello all, I am using postgresql as a database server for my db application. The database is related to accounts and point of sales and many calculations involve money datatype. The application logic is totally done in python. Now the problem is that there is a situation where we calculate tax on % of the total amount on the invoice. The percentage is in terms of float but the eventual amount has to be in a money datatype. The product cost comes in money datatype from postgresql and the tax % comes in float. So I would like to know if python supports some thing similar to money datatype so that I can cast my total amount (in money ) divided by tax% (in float ) so money divided by float should be the result and that result should be in money datatype. For example a product x has the cost Rs. 100 which is stored in the tabel as money type. and in the tax table for that product the VAT is 5% and this 5% is stored in float datatype. So after using both these values the resulting total 105Rs. should be in money type and not float. I awaite some reply, Thanks in advice. happy hacking. Krishnakant. From hackingkk at gmail.com Sat May 2 04:12:58 2009 From: hackingkk at gmail.com (Krishnakant) Date: Sat, 02 May 2009 13:42:58 +0530 Subject: interesting float to money conversion problem Message-ID: <1241251978.4669.16.camel@kk-laptop> hello all, > > I am using postgresql as a database server for my db application. > > > > The database is related to accounts and point of sales and many > > calculations involve money datatype. > > > > The application logic is totally done in python. Now the problem is > > that there is a situation where we calculate tax on % of the total > > amount on the invoice. The percentage is in terms of float but the > > eventual amount has to be in a money datatype. > > > > The product cost comes in money datatype from postgresql and the tax % > > comes in float. So I would like to know if python supports some thing > > similar to money datatype so that I can cast my total amount (in money ) > > divided by tax% (in float ) so money divided by float should be the > > result and that result should be in money datatype. > > For example a product x has the cost Rs. 100 which is stored in the > > tabel as money type. and in the tax table for that product the VAT is > > 5% and this 5% is stored in float datatype. So after using both these > > values the resulting total 105Rs. should be in money type and not float. > > I awaite some reply, > > Thanks in advice. > > happy hacking. > > Krishnakant. > > > > -- > > http://mail.python.org/mailman/listinfo/python-list From arnodel at googlemail.com Sat May 2 04:23:58 2009 From: arnodel at googlemail.com (Arnaud Delobelle) Date: Sat, 02 May 2009 09:23:58 +0100 Subject: Why bool( object )? References: <54cb7f8a-fef4-4bf8-8054-16dc9b5c8830@d2g2000pra.googlegroups.com> <49f85861$0$29010$426a74cc@news.free.fr> <4t_Jl.37718$VA5.20343@tornado.fastwebnet.it> <020ab4a3$0$20667$c3e8da3@news.astraweb.com> <020b99a5$0$20667$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano writes: > On Fri, 01 May 2009 15:03:30 -0700, Aaron Brady wrote: > >> On May 1, 4:30?am, Steven D'Aprano > cybersource.com.au> wrote: >>> On Fri, 01 May 2009 16:30:19 +1200, Lawrence D'Oliveiro wrote: >>> > I have never written anything so unbelievable in my life. And I hope >>> > I never will. >>> >>> I didn't say you did. If anyone thought I was quoting Lawrence's code, >>> I'd be surprised. It was not my intention to put words into your mouth. >>> >>> But seeing as you have replied, perhaps you could tell us something. >>> Given so much you despise using non-bools in truth contexts, how would >>> you re-write my example to avoid "a or b or c"? >>> >>> for x in a or b or c: >>> ? ? do_something_with(x) > > [...] >> I don't think it would be very common to write Steven's construction for >> arbitrary values of 'a', 'b', and 'c'. > > I don't care about "arbitrary values" for a, b and c. I don't expect a > solution that works for (say) a=None, b=5, c=[]. I'm happy to restrict > the arguments to all be arbitrary sequence-like objects. > > I'm even happy for somebody to give a solution with further restrictions, > like "if I know before hand that all three are lists, then I do blah...". > But state your restrictions up front. If a, b, c are names or literals, then I guess you could do this: for seq in a, b, c: if seq: break for x in seq: do_something_with(x) I have never been in a situation where I needed something like this, though. -- Arnaud From arnodel at googlemail.com Sat May 2 04:25:47 2009 From: arnodel at googlemail.com (Arnaud Delobelle) Date: Sat, 02 May 2009 09:25:47 +0100 Subject: eval(WsgiApplication) References: <8357032b-5fc0-4d4c-843a-a1ea27c62e5c@o14g2000vbo.googlegroups.com> Message-ID: gert writes: > I would like to read the following from a text file > > from json import loads > from gert.db import Db > def application(environ, response): > v = loads(environ['wsgi.input'].read(int(environ > ['CONTENT_LENGTH'])).decode('utf-8')) > db = Db() > db.execute('UPDATE votes SET count=count+1 WHERE vid=?',(v > ['vid'],)) > db.execute('SELECT * FROM votes') > j = '{"rec":'+db.json()+',\n' > j+= ' "des":'+db.jdes()+'}' > j = j.encode('utf-8') > response('200 OK', [('Content-type', 'text/ > javascript;charset=utf-8'), ('Content-Length', str(len(j)))]) > return [j] > > execute it, and wrap a new function name around it for example > > def wrapper(environ, response): > exec(file) > return application(environ, response) > > How do I do this in python3? What's wrong with importing it? -- Arnaud From subhakolkata1234 at gmail.com Sat May 2 04:26:14 2009 From: subhakolkata1234 at gmail.com (subhakolkata1234 at gmail.com) Date: Sat, 2 May 2009 01:26:14 -0700 (PDT) Subject: File handling problem. Message-ID: Dear Group, I am using Python2.6 and has created a file where I like to write some statistical values I am generating. The statistical values are generating in a nice way, but as I am going to write it, it is not taking it, the file is opening or closing properly but the values are not getting stored. It is picking up arbitrary values from the generated set of values and storing it. Is there any solution for it? Best Regards, SBanerjee. From chambon.pascal at wanadoo.fr Sat May 2 04:46:28 2009 From: chambon.pascal at wanadoo.fr (Pascal Chambon) Date: Sat, 02 May 2009 10:46:28 +0200 Subject: File handling problem. In-Reply-To: References: Message-ID: <49FC0864.5080309@wanadoo.fr> subhakolkata1234 at gmail.com a ?crit : > Dear Group, > > I am using Python2.6 and has created a file where I like to write some > statistical values I am generating. The statistical values are > generating in a nice way, but as I am going to write it, it is not > taking it, the file is opening or closing properly but the values are > not getting stored. It is picking up arbitrary values from the > generated set of values and storing it. Is there any solution for it? > > Best Regards, > SBanerjee. > -- > http://mail.python.org/mailman/listinfo/python-list > > > Hello Could you post excerpt of your file-handling code ? It might be a buffering problem (although when the file closes, I think buffers get flushed), else it's really weird... Regards, pascal From paul at subsignal.org Sat May 2 05:06:44 2009 From: paul at subsignal.org (paul) Date: Sat, 02 May 2009 11:06:44 +0200 Subject: eval(WsgiApplication) In-Reply-To: <8357032b-5fc0-4d4c-843a-a1ea27c62e5c@o14g2000vbo.googlegroups.com> References: <8357032b-5fc0-4d4c-843a-a1ea27c62e5c@o14g2000vbo.googlegroups.com> Message-ID: gert schrieb: > I would like to read the following from a text file Hi gert, I'm puzzled, what is wrong with using wsgi as advertised? Just import your code and insert it in the wsgi chain no? cheers Paul > > from json import loads > from gert.db import Db > def application(environ, response): > v = loads(environ['wsgi.input'].read(int(environ > ['CONTENT_LENGTH'])).decode('utf-8')) > db = Db() > db.execute('UPDATE votes SET count=count+1 WHERE vid=?',(v > ['vid'],)) > db.execute('SELECT * FROM votes') > j = '{"rec":'+db.json()+',\n' > j+= ' "des":'+db.jdes()+'}' > j = j.encode('utf-8') > response('200 OK', [('Content-type', 'text/ > javascript;charset=utf-8'), ('Content-Length', str(len(j)))]) > return [j] > > execute it, and wrap a new function name around it for example > > def wrapper(environ, response): > exec(file) > return application(environ, response) > > How do I do this in python3? > -- > http://mail.python.org/mailman/listinfo/python-list > From gert.cuykens at gmail.com Sat May 2 05:49:20 2009 From: gert.cuykens at gmail.com (gert) Date: Sat, 2 May 2009 02:49:20 -0700 (PDT) Subject: eval(WsgiApplication) References: <8357032b-5fc0-4d4c-843a-a1ea27c62e5c@o14g2000vbo.googlegroups.com> Message-ID: <39057d64-921e-4150-a382-ec1d443a383f@j12g2000vbl.googlegroups.com> On May 2, 10:25?am, Arnaud Delobelle wrote: > gert writes: > > I would like to read the following from a text file > > > from json import loads > > from gert.db import Db > > def application(environ, response): > > ? ? v = loads(environ['wsgi.input'].read(int(environ > > ['CONTENT_LENGTH'])).decode('utf-8')) > > ? ? db = Db() > > ? ? db.execute('UPDATE votes SET count=count+1 WHERE vid=?',(v > > ['vid'],)) > > ? ? db.execute('SELECT * FROM votes') > > ? ? j = '{"rec":'+db.json()+',\n' > > ? ? j+= ' "des":'+db.jdes()+'}' > > ? ? j = j.encode('utf-8') > > ? ? response('200 OK', [('Content-type', 'text/ > > javascript;charset=utf-8'), ('Content-Length', str(len(j)))]) > > ? ? return [j] > > > execute it, and wrap a new function name around it for example > > > def wrapper(environ, response): > > ? ? ?exec(file) > > ? ? ?return application(environ, response) > > > How do I do this in python3? > > What's wrong with importing it? The problem is that my wsgi files have a wsgi extention for mod_wsgi use package - __init__.py - session.py - db.py - sqlite - - sql.db - www - - test.htm - - test.css - - test.js - - test.wsgi i would like to make this package work both in mod_wsgi and cherrypy server mod_wsgi has a .wsgi handler because it is recommended to rename the wsgi file with wsgi extensions to avoid double imports cherrypy server has a dispatcher class From chambon.pascal at wanadoo.fr Sat May 2 05:56:28 2009 From: chambon.pascal at wanadoo.fr (Pascal Chambon) Date: Sat, 02 May 2009 11:56:28 +0200 Subject: Warning of missing side effects In-Reply-To: References: Message-ID: <49FC18CC.4040807@wanadoo.fr> Tobias Weber a ?crit : > Hi, > being new to Python I find remarkable that I don't see any side effects. > That's especially true for binding. First, it is a statement, so this > won't work: > > if x = q.pop(): > print x # output only true values > > Second, methods in the standard library either return a value OR modify > the reciever, so even if assignment was an expression the above wouldn't > work. > > Only it still wouldn't, because IF is a statement as well. So no ternary: > > x = if True: 5 else: 7; > > However there is one bit of magic, functions implicitly return None. So > while the following will both run without error, only one actually works: > > x = 'foo'.upper() > y = ['f', 'b'].reverse() > > Now I mentioned that the mutable types don't have functions that mutate > and return something, so I only have to remember that... > > But I'm used to exploiting side effect, and sometimes forget this rule > in my own classes. IS THERE A WAY to have the following produce a > runtime error? > > def f(): > x = 5 > # no return > > y = f() > > Maybe use strict ;) > > Hello Just to note that if "['f', 'b'].reverse()" doesn't return the new value, there is a corersponding function : "reversed(mylist)" which does it (idem, sorted(list) <-> list.sort()) B-) Concerning your question on warnings, well I guess that after a little time in python, you won't make mistakes on "side effects" anymore ; But if you want to add checks to your methods, you should see towards decorators or metaclasses : they allow you to wrap your methods inside other methods, of which the only point couldbe, for example, to check what your methods returtn and raise a warning if it returns "None". But the problem is, sometimes you WANT them to return None.... If you want to detect methods that don't have explicit "return" statements, then you'll have to play with abstract syntax trees it seems... much trouble for not much gain. I guess you'll quickly get the pythonic habits without needing all that ^^ Regards, pascal From martin.hellwig at dcuktec.org Sat May 2 06:01:32 2009 From: martin.hellwig at dcuktec.org (Martin P. Hellwig) Date: Sat, 02 May 2009 11:01:32 +0100 Subject: web access through vpn client In-Reply-To: <3b82989a-7e54-461a-b18f-dc90d803c0f9@x29g2000prf.googlegroups.com> References: <0aa2beb9-b2b1-4bba-b130-e88821b914e8@d19g2000prh.googlegroups.com> <3b82989a-7e54-461a-b18f-dc90d803c0f9@x29g2000prf.googlegroups.com> Message-ID: mark.seagoe at gmail.com wrote: > On May 1, 5:57 pm, "Martin P. Hellwig" > wrote: >> mark.sea... at gmail.com wrote: >>> Hi; >>> I am trying to retrieve financial data off website for stock market >>> analysis. Just hobby not for pay. I actually am impressed that >>> urllib2 and BeautifulSoup work pretty well to do what I want, and the >>> first little routine actually gets the data from the web page... >>> except if my VPN client is turned on. >>> I've looked around for a solution but can't find... perhaps I'm not >>> entering the right search words. I don't even know where to start. >>> Can anyone suggest some good reading, or basic idea how to approach? >>> Thanks, >>> Mark >> When the VPN client is on, can you actually still browse the 'classical' >> way to the website in question? >> I ask this because many cooperate VPN clients have a way of shutting >> down all other access besides to the VPN server (which is very annoying >> but I can see the logic behind it). >> >> If that is the case your VPN network might have a webproxy which needs >> to be configured to continue browsing when connected to the VPN. For >> python you can automate this by checking for access to the proxy and use >> it if it is there. >> >> -- >> MPHhttp://blog.dcuktec.com > > Yes when Cisco AnyConnect VPN client is connected, I can browse w/ > IE. I think the packets get encrypted and possibly go to my company's > server and out from there, because then also Outlook connects to my > company, Communicator works, etc. With VPN off I can browse and > Python can get URL, but no connection through my company's server & no > encryption. > > Where can I find out more about how to configure the webproxy, if the > info is handy? Thanks! > > Mark Most likely your IE gets automatically configured for using a proxy when the VPN is made, just check your IE settings ounce you are connected to the VPN particular the Connection / LAN-connection settings. Or query what is set in the registry at : 'HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings' And use the content (if set) as the proxy address in the urllib module. You might find this module useful for that: http://docs.python.org/library/_winreg.html -- MPH http://blog.dcuktec.com From martin at v.loewis.de Sat May 2 06:02:55 2009 From: martin at v.loewis.de (=?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?=) Date: Sat, 02 May 2009 12:02:55 +0200 Subject: Warning of missing side effects In-Reply-To: References: Message-ID: <49FC1A4F.6040502@v.loewis.de> > But I'm used to exploiting side effect, and sometimes forget this rule > in my own classes. IS THERE A WAY to have the following produce a > runtime error? > > def f(): > x = 5 > # no return > > y = f() Typically, this will produce a runtime error fairly quickly, namely when you *use* the (presumed) return value of f(). You would normally try to perform some computation with y, or invoke methods on it - and then you see that it is None. So while it is not possible to get an exception on the assignment, you will usually get a runtime error sooner or later (most of the time, sooner). FWIW, pylint -e reports on your code E: 5: Assigning to function call which doesn't return Regards, Martin From mail at microcorp.co.za Sat May 2 06:38:01 2009 From: mail at microcorp.co.za (Hendrik van Rooyen) Date: Sat, 2 May 2009 12:38:01 +0200 Subject: Multiprocessing.Queue - I want to end. References: <200904301649.10341.kyrie@uh.cu> <2mKKl.16353$B17.1674@newsfe26.ams2> Message-ID: <00a601c9cb12$152d25e0$0d00a8c0@Hendrik> : "Roel Schroeven" wrote: > Hendrik van Rooyen schreef: > > I have always wondered why people do the one queue many getters thing. > > Because IMO it's the simplest and most elegant solution. That is fair enough... > > > > Given that the stuff you pass is homogenous in that it will require a > > similar amount of effort to process, is there not a case to be made > > to have as many queues as consumers, and to round robin the work? > > Could work if the processing time for each work unit is exactly the same > (otherwise one or more consumers will be idle part of the time), but in > most cases that is not guaranteed. A simple example is fetching data > over the network: even if the data size is always the same, there will > be differences because of network load variations. > > If you use one queue, each consumer fetches a new work unit as soon it > has consumed the previous one. All consumers will be working as long as > there is work to do, without having to write any code to do the load > balancing. > > With one queue for each consumer, you either have to assume that the > average processing time is the same (otherwise some consumers will be > idle at the end, while others are still busy processing work units), or > you need some clever code in the producer(s) or the driving code to > balance the loads. That's extra complexity for little or no benefit. > > I like the simplicity of having one queue: the producer(s) put work > units on the queue with no concern which consumer will process them or > how many consumers there even are; likewise the consumer(s) don't know > and don't need to know where their work units come from. And the work > gets automatically distributed to whichever consumer has first finished > its previous work unit. This is all true in the case of a job that starts, runs and finishes. I am not so sure it applies to something that has a long life. > > > And if the stuff you pass around needs disparate effort to consume, > > it seems to me that you can more easily balance the load by having > > specialised consumers, instead of instances of one humungous > > "I can eat anything" consumer. > > If there is a semantic difference, maybe yes; but I think it makes no > sense to differentiate purely on the expected execution times. The idea is basically that you have the code that classifies in one place only, instead of running in all the instances of the consumer. Feels better to me, somehow. > > > I also think that having a queue per consumer thread makes it easier > > to replace the threads with processes and the queues with pipes or > > sockets if you need to do serious scaling later. > > Perhaps, but isn't that a case of YAGNI and/or premature optimization? Yes and no: Yes - You Are Gonna Need It. and No it is never premature to use a decent structure. :-) - Hendrik From basti.wiesner at gmx.net Sat May 2 07:14:30 2009 From: basti.wiesner at gmx.net (Sebastian Wiesner) Date: Sat, 02 May 2009 13:14:30 +0200 Subject: Question about the wording in the python documents. References: <020b9acb$0$20667$c3e8da3@news.astraweb.com> Message-ID: > I don't understand your objection. Is it that the documentation calls it > Request instead of urllib2.Request? Or that it calls it an object instead > of an instance? I guess the latter ... > In either case, I think you're picking a nit so small that it isn't > actually there. All objects are instances (in Python), and all instances > are objects. Exactly, so strictly seen, "Request object" could possibly refer to the urllib2.Request class itself. I guess, the OP would prefer the term "Request instance", emphasizing, that an instance of the request class has to be passed, not he class itself. -- Freedom is always the freedom of dissenters. (Rosa Luxemburg) From ldo at geek-central.gen.new_zealand Sat May 2 07:25:24 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Sat, 02 May 2009 23:25:24 +1200 Subject: Question about the wording in the python documents. References: Message-ID: In message , grocery_stocker wrote: > req is clearly an instance of urllib2.Request and not a Request object. "Object" is a term commonly used to mean "instance of a class". In Python, classes are also objects, but if classes were meant rather than instances, I imagine it would say so. From gdamjan at gmail.com Sat May 2 08:15:38 2009 From: gdamjan at gmail.com (=?UTF-8?B?0JTQsNC80ZjQsNC9INCT0LXQvtGA0LPQuNC10LLRgdC60Lg=?=) Date: Sat, 02 May 2009 14:15:38 +0200 Subject: eval(WsgiApplication) References: <8357032b-5fc0-4d4c-843a-a1ea27c62e5c@o14g2000vbo.googlegroups.com> <39057d64-921e-4150-a382-ec1d443a383f@j12g2000vbl.googlegroups.com> Message-ID: >> > How do I do this in python3? >> >> What's wrong with importing it? > > The problem is that my wsgi files have a wsgi extention for mod_wsgi > use .. > mod_wsgi has a .wsgi handler because it is recommended to rename the > wsgi file with wsgi extensions to avoid double imports > cherrypy server has a dispatcher class You can either use .py extension for the wsgi files OR use a custom importer that can import your .wsgi files http://docs.python.org/library/modules.html -- ?????? ( http://softver.org.mk/damjan/ ) Scarlett Johansson: You always see the glass half-empty. Woody Allen: No. I see the glass half-full, but of poison. From aleksandar27 at brisiovonet.hr Sat May 2 08:25:29 2009 From: aleksandar27 at brisiovonet.hr (alejandro) Date: Sat, 2 May 2009 14:25:29 +0200 Subject: stuck with PyOBEX References: <75opebF19fc22U1@mid.uni-berlin.de> Message-ID: I am having problems with connect() it says that it doesn't have sendall atribute. Error: Traceback (most recent call last): File "novi_pokusaj.py", line 25, in client.connect() File "C:\Python25\lib\PyOBEX\client.py", line 356, in connect return Client.connect(self, header_list = [headers.Target(uuid)]) File "C:\Python25\lib\PyOBEX\client.py", line 127, in connect response = self._send_headers(request, header_list, max_length) File "C:\Python25\lib\PyOBEX\client.py", line 81, in _send_headers self.socket.sendall(request.encode()) AttributeError: BluetoothSocket instance has no attribute 'sendall'>>> Exit Code: 1 Did you manage to run it under windows? From steve at REMOVE-THIS-cybersource.com.au Sat May 2 09:24:47 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 02 May 2009 13:24:47 GMT Subject: File handling problem. References: Message-ID: <020c3d0c$0$20667$c3e8da3@news.astraweb.com> On Sat, 02 May 2009 01:26:14 -0700, subhakolkata1234 wrote: > Dear Group, > > I am using Python2.6 and has created a file where I like to write some > statistical values I am generating. The statistical values are > generating in a nice way, but as I am going to write it, it is not > taking it, the file is opening or closing properly but the values are > not getting stored. It is picking up arbitrary values from the generated > set of values and storing it. Is there any solution for it? Yes. Find the bug in your program and fix it. If you'd like some help finding the bug, you'll need to give us a little bit more information. This website might help you: http://www.catb.org/~esr/faqs/smart-questions.html -- Steven From dstanek at dstanek.com Sat May 2 09:28:31 2009 From: dstanek at dstanek.com (David Stanek) Date: Sat, 2 May 2009 09:28:31 -0400 Subject: object query assigned variable name? In-Reply-To: <020b1b54$0$20667$c3e8da3@news.astraweb.com> References: <020b1b54$0$20667$c3e8da3@news.astraweb.com> Message-ID: On Fri, May 1, 2009 at 12:48 PM, Steven D'Aprano wrote: > On Fri, 01 May 2009 09:24:10 -0700, warpcat wrote: > >> I'd like it to print, when instanced, something like this: >> >>>>> s = Spam() >> I?m assigned to s! >> >> But it seems prohibitively hard (based on my web and forum searches) for >> an object to know what variable name is has been assigned to when >> created. > > Can't be done. Objects don't know what names they are bound to. > While objects don't know what they are assigned to, they can be made to find out. Unless you have a good use case I don't think that you really want to be doing it. DecoratorTools allows you to do this. I have code that allows you to use a a function like: class C: attr = inject(Customer) The inject function does know that it is being assigned to attr. There is tracing/frame introspection black magic involved. So much so that I have been debating removing that feature. The code is on Bitbucket[1]. It has a little extra complication because inject can also be used as a decorator. The key is the decorate_assignment call in the inject function. Again I don't think you really want to do this. [1] http://bitbucket.org/dstanek/snake-guice/src/tip/snakeguice/decorators.py -- David blog: http://www.traceback.org twitter: http://twitter.com/dstanek From davea at ieee.org Sat May 2 09:35:50 2009 From: davea at ieee.org (Dave Angel) Date: Sat, 02 May 2009 09:35:50 -0400 Subject: Warning of missing side effects In-Reply-To: References: Message-ID: <49FC4C36.2000401@ieee.org> Tobias Weber wrote: > Hi, > being new to Python I find remarkable that I don't see any side effects. > That's especially true for binding. First, it is a statement, so this > won't work: > > if x = q.pop(): > print x # output only true values > > Second, methods in the standard library either return a value OR modify > the reciever, so even if assignment was an expression the above wouldn't > work. > > Only it still wouldn't, because IF is a statement as well. So no ternary: > > x = if True: 5 else: 7; > > However there is one bit of magic, functions implicitly return None. So > while the following will both run without error, only one actually works: > > x = 'foo'.upper() > y = ['f', 'b'].reverse() > > Now I mentioned that the mutable types don't have functions that mutate > and return something, so I only have to remember that... > > But I'm used to exploiting side effect, and sometimes forget this rule > in my own classes. IS THERE A WAY to have the following produce a > runtime error? > > def f(): > x = 5 > # no return > > y = f() > > Maybe use strict ;) > > There's a ternary operator. Check out the following sequence: a = 42 b = 12 if a == 42 else 9 print a, b Then try it again with a different value of a. As for using = in an ordinary expression, I don't really miss it. Python allows multiple assignments in the same statement, but they're all to the same object. For the kinds of things where I would have done an assignment inside an if or while (in C++), I usually can use a list comprehension or somesuch. From guenther.dietrich at spamfence.net Sat May 2 09:43:23 2009 From: guenther.dietrich at spamfence.net (=?UTF-8?Q?G=C3=BCnther?= Dietrich) Date: Sat, 02 May 2009 15:43:23 +0200 Subject: problem with money datatype based calculations in python References: Message-ID: Krishnakant wrote: ['money datatype' for database stored values] What ist the real problem? Do you have problems converting results of float calculations to the 'money datatype' before storing them in the database, or do you want to avoid the precision issues which come with the conversion od non-integer values between binary and decimal? In the latter case use the Decimal data type from module decimal (comes with the python distribution -- it is mentioned in the tutorial). Regards, G?nther From arnodel at googlemail.com Sat May 2 10:07:23 2009 From: arnodel at googlemail.com (Arnaud Delobelle) Date: Sat, 02 May 2009 15:07:23 +0100 Subject: Warning of missing side effects References: Message-ID: Dave Angel writes: > Python allows multiple assignments in the same statement, but they're > all to the same object. Unless they are to different objects: a, b = 1, 2 -- Arnaud From anuhacks at gmail.com Sat May 2 10:08:28 2009 From: anuhacks at gmail.com (anusha k) Date: Sat, 2 May 2009 19:38:28 +0530 Subject: problem with money datatype based calculations in python In-Reply-To: <1241250322.4669.13.camel@kk-laptop> References: <1241250322.4669.13.camel@kk-laptop> Message-ID: <61f9a5fb0905020708g5e8f5a7exabd38c9eada3e2f2@mail.gmail.com> Do calculations in postgresql and then call that calculated value from the python.So no need to calculate in python at all Njoy the share of Freedom, Anusha Kadambala On Sat, May 2, 2009 at 1:15 PM, Krishnakant wrote: > hello all, > I am using postgresql as a database server for my db application. > > The database is related to accounts and point of sales and many > calculations involve money datatype. > > The application logic is totally done in python. Now the problem is > that there is a situation where we calculate tax on % of the total > amount on the invoice. The percentage is in terms of float but the > eventual amount has to be in a money datatype. > > The product cost comes in money datatype from postgresql and the tax % > comes in float. So I would like to know if python supports some thing > similar to money datatype so that I can cast my total amount (in money ) > divided by tax% (in float ) so money divided by float should be the > result and that result should be in money datatype. > For example a product x has the cost Rs. 100 which is stored in the > tabel as money type. and in the tax table for that product the VAT is > 5% and this 5% is stored in float datatype. So after using both these > values the resulting total 105Rs. should be in money type and not float. > I awaite some reply, > Thanks in advice. > happy hacking. > Krishnakant. > > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at ieee.org Sat May 2 10:09:55 2009 From: davea at ieee.org (Dave Angel) Date: Sat, 02 May 2009 10:09:55 -0400 Subject: problem with money datatype based calculations in python In-Reply-To: <1241250322.4669.13.camel@kk-laptop> References: <1241250322.4669.13.camel@kk-laptop> Message-ID: <49FC5433.2070404@ieee.org> Krishnakant wrote: > hello all, > I am using postgresql as a database server for my db application. > > The database is related to accounts and point of sales and many > calculations involve money datatype. > > The application logic is totally done in python. Now the problem is > that there is a situation where we calculate tax on % of the total > amount on the invoice. The percentage is in terms of float but the > eventual amount has to be in a money datatype. > > The product cost comes in money datatype from postgresql and the tax % > comes in float. So I would like to know if python supports some thing > similar to money datatype so that I can cast my total amount (in money ) > divided by tax% (in float ) so money divided by float should be the > result and that result should be in money datatype. > For example a product x has the cost Rs. 100 which is stored in the > tabel as money type. and in the tax table for that product the VAT is > 5% and this 5% is stored in float datatype. So after using both these > values the resulting total 105Rs. should be in money type and not float. > I awaite some reply, > Thanks in advice. > happy hacking. > Krishnakant. > > > Tax rate isn't divided, it's multiplied. But probably that's just a typo in your message. Short answer is use your money class to convert the float into money. And then test, to see that's really what you wanted. Be sure and check the edge cases, because float is a binary system, and you can get both quantization and rounding problems converting between them. Is the money you're dealing with decimal-based? So maybe money values are integers plus two more digits of fractional precision? It's going to be tough for somebody with no access to that "money type" to guess what your problem is. What operations does it currently support? If it supports multiply, then just change the tax rate to be a money type. Unless of course you might have a tax rate of 5.221% Does your money class support accurate conversion to class decimal? In that case, convert to decimal, multiply, then convert back. Or if money is really a fixed-point class, with assumed two digits, multiply the cost by 100 and convert to int. Multiply by the tax, and convert back. You also need to specify the government rules for the multiply. Is rounding permitted, or must all fractions of a penny (assumption there) be rounded up? Or switch everything to "class decimal" in the standard python library, You still have most of the questions to answer, but at least you'd be asking on the right forum. From pxrepreza at gmail.com Sat May 2 10:40:40 2009 From: pxrepreza at gmail.com (Paulo Repreza) Date: Sat, 2 May 2009 07:40:40 -0700 Subject: debian apt somehow created python hell! - help In-Reply-To: References: Message-ID: Hi, Try this. # aptitude update # aptitude update # aptitude upgrade you can also use # aptitude safe-upgrade Hope it helps. Paulo Repreza On May 1, 2009 7:20 PM, "watermod" wrote: I was doing one of those auto apt-get gui things with update manager in Debian and not watching the error log so I don't know where it started. It involves only Python stuff. I don't know Python but apps use it. The current apt state is that python apps are failing install in: /usr/sbin/update-python-modules typical error list looks like: dpkg: warning - old pre-removal script returned error exit status 1 dpkg - trying script from the new package instead ... WARNING: python-gtk2-doc.private does not exist. Some bytecompiled files may be left behind. Traceback (most recent call last): File "/usr/sbin/update-python-modules", line 437, in public_packages[package].install(need_postinstall) File "/usr/sbin/update-python-modules", line 232, in __getitem__ self[name] = SharedFileList (path) File "/usr/sbin/update-python-modules", line 146, in __init__ for line in file(path): IOError: [Errno 21] Is a directory dpkg: error processing /var/cache/apt/archives/python-gtk2- doc_2.14.1-2_all.deb (--unpack): subprocess new pre-removal script returned error exit status 1 Traceback (most recent call last): File "/usr/sbin/update-python-modules", line 437, in public_packages[package].install(need_postinstall) File "/usr/sbin/update-python-modules", line 232, in __getitem__ self[name] = SharedFileList (path) File "/usr/sbin/update-python-modules", line 146, in __init__ for line in file(path): IOError: [Errno 21] Is a directory dpkg: error while cleaning up: on and on and on.... The following: dpkg -C gives: The following packages are in a mess due to serious problems during installation. They must be reinstalled for them (and any packages that depend on them) to function properly: python-reportbug Python modules for interacting with bug tracking systems lsb-release Linux Standard Base version reporting utility python-ogg Python interface to the Ogg library system-config-lvm A utility for graphically configuring Logical Volumes bitpim utility to communicate with many CDMA phones bitpim-lib architecture-dependent helper files for BitPim python-glade2 GTK+ bindings: Glade support python-gtk2 Python bindings for the GTK+ widget set python-feedparser Universal Feed Parser for Python python-gtk2-doc Python bindings for the GTK+ widget set - documentation k3d 3D modeling and animation system The following packages have been unpacked but not yet configured. They must be configured using dpkg --configure or the configure menu option in dselect for them to work: acroread-plugins Plugins for Adobe Acrobat(R) Reader python-roman module for generating/analyzing Roman numerals python-soappy SOAP Support for Python python-glpk Python bindings to the GNU Linear Programming Kit python-dialog Python module for making simple Text/Console-mode user in python-biggles Scientific plotting package for Python python-gd Python module wrapper for libgd python-clutter Open GL based interactive canvas library - Python binding python-decoratortools version-agnostic decorators support for Python python-fpconst Utilities for handling IEEE 754 floating point special va acroread-data data files for acroread python-galago-gtk GTK+ widgets for the Galago presence library (Python inte python-utmp python module for working with utmp python-debianbts Python interface to Debian's Bug Tracking System python-gobject-dev Development headers for the GObject Python bindings acroread-escript Adobe EScript Plug-In python-dhm collection of Python utilities / helper python-unit unit test framework for Python acpid Utilities for using ACPI power management python-sqlalchemy SQL toolkit and Object Relational Mapper for Python python-gnutls Python wrapper for the GNUTLS library python-xlib Interface for Python to the X11 Protocol acroread Adobe Acrobat Reader: Portable Document Format file viewe python-pyinotify simple Linux inotify Python bindings python-facebook Python wrappers for the Facebook API reportbug reports bugs in the Debian distribution python-ll-core Python modules for colors, make, cron, daemons, URLs, tem python-beaker Simple WSGI middleware that uses the Myghty Container API python-xcbgen X C Binding - protocol binding generator libroot-dev Header files for ROOT python-dcop DCOP bindings for Python python-docutils utilities for the documentation of Python modules libroot-python-dev Python extension for ROOT - development files python-extractor extracts meta-data from files of arbitrary type (Python b python-debian Python modules to work with Debian-related data formats python-cheetah text-based template engine and Python code generator python-galago Galago presence library (Python interface) The following packages are only half configured, probably due to problems configuring them the first time. The configuration should be retried using dpkg --configure or the configure menu option in dselect: python-support automated rebuilding support for Python modules The last one appears to be a key problem.... reconfigure gives the same dam error. Chicken egg problem of some sort... I can't add or remove anything python with the debian package tools... Maybe I could install by hand by a tarball if I knew where to stick stuff I unpacked a tarball to: /usr/src/python-support-1.0.2 but have no idea what to do... I am not a python guy... c, c++ perl but brain dead for this... /usr/src/python-support-1.0.2# ls -CF COPYING debian/ movemodules* pysupport.py README update-python-modules* debhelper/ hold parseversions* python-support.private runtime/ update-python-modules.8 SO!!! HELP ... SUGGESTIONS? -- http://mail.python.org/mailman/listinfo/python-list -------------- next part -------------- An HTML attachment was scrubbed... URL: From david at boddie.org.uk Sat May 2 10:41:26 2009 From: david at boddie.org.uk (David Boddie) Date: Sat, 02 May 2009 16:41:26 +0200 Subject: stuck with PyOBEX References: <75opebF19fc22U1@mid.uni-berlin.de> Message-ID: On Saturday 02 May 2009 14:25, alejandro wrote: > I am having problems with connect() it says that it doesn't have sendall > atribute. > > Error: > > Traceback (most recent call last): > File "novi_pokusaj.py", line 25, in > client.connect() > File "C:\Python25\lib\PyOBEX\client.py", line 356, in connect > return Client.connect(self, header_list = [headers.Target(uuid)]) > File "C:\Python25\lib\PyOBEX\client.py", line 127, in connect > response = self._send_headers(request, header_list, max_length) > File "C:\Python25\lib\PyOBEX\client.py", line 81, in _send_headers > self.socket.sendall(request.encode()) > AttributeError: BluetoothSocket instance has no attribute 'sendall'>>> > Exit Code: 1 > > Did you manage to run it under windows? I haven't tried to run it under Windows. I just assumed that BluetoothSocket would have the same API on both Linux and Windows. Looking around, it seems that this is something we can work around: http://svn.navi.cx/misc/trunk/laserprop/client/BluetoothConduit.py I'll send you an updated version to try if you would like to test it. David From rpmuller at gmail.com Sat May 2 11:10:22 2009 From: rpmuller at gmail.com (Rick) Date: Sat, 2 May 2009 08:10:22 -0700 (PDT) Subject: Profiling gives very different predictions of best algorithm References: Message-ID: <74e32884-95b2-4e1d-9d3d-21f300bd8715@k19g2000prh.googlegroups.com> On May 1, 7:38?pm, Terry Reedy wrote: > I presume in your overall time text, you ran the two versions of the > algorith 'naked'. ?But, for some reason, you are profiling them embedded > inside a test suite and runner. ?It does not seem that this should > affect relative timing, but I have seen some pretty strange behaviors. > At best, it will add noise. > > Let me expand my question: what did you do differently between the two > profile runs? When I compute the electron repulsion integrals, the two different methods are: if packed: ijkl = intindex(i,j,k,l) Ints[ijkl] = coulomb(bfs[i],bfs[j],bfs [k],bfs[l]) else: val = coulomb(bfs[i],bfs[j],bfs[k],bfs[l]) Ints[i,j,k,l] = val Ints[j,i,k,l] = val Ints[i,j,l,k] = val Ints[j,i,l,k] = val Ints[k,l,i,j] = val Ints[k,l,j,i] = val Ints[l,k,i,j] = val Ints[l,k,j,i] = val and when I access the integrals the differences are: if packed: index = intindex(i,j,k,l) temp[kl] = Ints[index] else: temp[kl] = Ints[i,j,k,l] If you look at the profiling, I'm making something like 11M calls to intindex, which is a routine I've written in C. I thought that by storing all N**4 integrals (rather than the N**4/8 that packed stores) would remove the need for the intindex calls and speed things up, but it's 50% slower. I can't really figure out why it's slower, though, since profiling makes it look like the slower version is faster. Something like 30% of the time for the full PyQuante test suite is taken up with calls to intindex, and I'd like to remove this if possible. I also wrote a version of the code where I stored all of the intindex values in a Python dictionary rather than calling a C function. That version appeared to be roughly the same speed when I profiled the code, although I didn't test it without profiling (stupidly) because at that point I didn't recognize the magnitude of the discrepancy between the profiling/nonprofiling timings. I was posting to the list mostly because I would like to know whether I can do something different when I profile my code so that it give results that correspond more closely to those that the nonprofiling results give. From ebonak at hotmail.com Sat May 2 11:12:30 2009 From: ebonak at hotmail.com (Esmail) Date: Sat, 02 May 2009 11:12:30 -0400 Subject: for with decimal values? Message-ID: Hello all, Is there a Python construct to allow me to do something like this: for i in range(-10.5, 10.5, 0.1): ... If there is such a thing already available, I'd like to use it, otherwise I can write a function to mimic this, but I thought I'd check (my search yielded nothing). Thanks, Esmail From aahz at pythoncraft.com Sat May 2 11:17:27 2009 From: aahz at pythoncraft.com (Aahz) Date: 2 May 2009 08:17:27 -0700 Subject: for with decimal values? References: Message-ID: In article , Esmail wrote: > >Is there a Python construct to allow me to do something like this: > > for i in range(-10.5, 10.5, 0.1): > ... > >If there is such a thing already available, I'd like >to use it, otherwise I can write a function to mimic this, >but I thought I'd check (my search yielded nothing). Write a function -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "Typing is cheap. Thinking is expensive." --Roy Smith From cdalten at gmail.com Sat May 2 11:30:25 2009 From: cdalten at gmail.com (grocery_stocker) Date: Sat, 2 May 2009 08:30:25 -0700 (PDT) Subject: Question about the wording in the python documents. References: <020b9acb$0$20667$c3e8da3@news.astraweb.com> Message-ID: <4a751872-d92b-4b66-826e-f58a4c33bf16@v1g2000prd.googlegroups.com> On May 2, 4:14 am, Sebastian Wiesner wrote: > > I don't understand your objection. Is it that the documentation calls it > > Request instead of urllib2.Request? Or that it calls it an object instead > > of an instance? > > I guess the latter ... > > > In either case, I think you're picking a nit so small that it isn't > > actually there. All objects are instances (in Python), and all instances > > are objects. > > Exactly, so strictly seen, "Request object" could possibly refer to the > urllib2.Request class itself. I guess, the OP would prefer the term > "Request instance", emphasizing, that an instance of the request class has > to be passed, not he class itself. > Yes, I personally think that the term "Request instance" would have made it much clearer to us people that have never taken a computer science class in our lives. From davea at ieee.org Sat May 2 11:32:02 2009 From: davea at ieee.org (Dave Angel) Date: Sat, 02 May 2009 11:32:02 -0400 Subject: Warning of missing side effects In-Reply-To: References: Message-ID: <49FC6772.8050700@ieee.org> Arnaud Delobelle wrote: > Dave Angel writes: > > >> Python allows multiple assignments in the same statement, but they're >> all to the same object. >> > > Unless they are to different objects: > > a, b = 1, 2 > > You're right, of course. I was referring to the multiple '=' form, and my statement was too general to be correct. a = b = 42 is legal a = (b=42) + 1 is not From chambon.pascal at wanadoo.fr Sat May 2 11:50:42 2009 From: chambon.pascal at wanadoo.fr (Pascal Chambon) Date: Sat, 02 May 2009 17:50:42 +0200 Subject: for with decimal values? In-Reply-To: References: Message-ID: <49FC6BD2.8030304@wanadoo.fr> Aahz a ?crit : > In article , > Esmail wrote: > >> Is there a Python construct to allow me to do something like this: >> >> for i in range(-10.5, 10.5, 0.1): >> ... >> >> If there is such a thing already available, I'd like >> to use it, otherwise I can write a function to mimic this, >> but I thought I'd check (my search yielded nothing). >> > > Write a function > Else you can work on integers - for i in range (-105, 105) - and divide by ten just below... but concerning performances I don't know if it's a good idea ^^ Regards, Pascal -------------- next part -------------- An HTML attachment was scrubbed... URL: From darcy at druid.net Sat May 2 11:57:34 2009 From: darcy at druid.net (D'Arcy J.M. Cain) Date: Sat, 2 May 2009 11:57:34 -0400 Subject: problem with money datatype based calculations in python In-Reply-To: References: Message-ID: <20090502115734.b280bf35.darcy@druid.net> On Sat, 02 May 2009 15:43:23 +0200 "G?nther Dietrich" wrote: > Krishnakant wrote: > > ['money datatype' for database stored values] > > What ist the real problem? Do you have problems converting results of > float calculations to the 'money datatype' before storing them in the > database, or do you want to avoid the precision issues which come with > the conversion od non-integer values between binary and decimal? > In the latter case use the Decimal data type from module decimal (comes > with the python distribution -- it is mentioned in the tutorial). And if you use PyGreSQL (http://www.PyGreSQL.org/) to connect to the database then the money type is already converted to Decimal. -- D'Arcy J.M. Cain | Democracy is three wolves http://www.druid.net/darcy/ | and a sheep voting on +1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner. From ppearson at nowhere.invalid Sat May 2 12:01:35 2009 From: ppearson at nowhere.invalid (Peter Pearson) Date: 2 May 2009 16:01:35 GMT Subject: problem with money datatype based calculations in python References: Message-ID: <76392uF1aduopU1@mid.individual.net> On Sat, 02 May 2009 13:15:22 +0530, Krishnakant wrote: > hello all, > I am using postgresql as a database server for my db application. > > The database is related to accounts and point of sales and many > calculations involve money datatype. [snip] As far as I can tell, Python has no type "money". Is money a class defined by some postgresql module you're using? A very small amount of specific information (e.g., a five-line sample program that demonstrates the problem) would greatly improve your chances of getting useful help. -- To email me, substitute nowhere->spamcop, invalid->net. From jjkk73 at gmail.com Sat May 2 12:16:48 2009 From: jjkk73 at gmail.com (jorma kala) Date: Sat, 2 May 2009 18:16:48 +0200 Subject: Accessing files in a directory which is a shortcut link (Windows) Message-ID: Hi, I'd like to process files in a directory which is in fact a short cut link to another directory (under windows XP). If the path to the directory is for instance called c:\test. I have tried both following code snipets for printing all names of files in the directory: ++ snippet 1++ for filename in glob.glob( os.path.join(r'c:\test', '*') ): print filename ++ snippet 2++ for filename in glob.glob( os.path.join(r'c:\test.lnk', '*') ): print filename Neither solution works. Thanks very much for any help. -------------- next part -------------- An HTML attachment was scrubbed... URL: From chambon.pascal at wanadoo.fr Sat May 2 12:33:15 2009 From: chambon.pascal at wanadoo.fr (Pascal Chambon) Date: Sat, 02 May 2009 18:33:15 +0200 Subject: Daemonic processes in multiprocessing - solved In-Reply-To: <49F33FBA.1050101@gmail.com> References: <49F33FBA.1050101@gmail.com> Message-ID: <49FC75CB.6030802@wanadoo.fr> Pascal Chambon a ?crit : > > Hello everyone > > > I've just read the doc of the (awesome) "multiprocessing" module, and > there are some little things I don't understand, concerning daemon > processes (see quotes below). > > When a python process exits, the page says it attempts to join all its > children. Is this just a design choice, or are there constraints > behind this ? Because normally, when a parent process exits, its child > gets adopted by init, and that can be useful for creating daemons, > can't it ? > > Concerning daemons processes, precisely, the manual states that they > are all terminated when their parent process exits. But isn't it > contrary to the concept of dameons, which are supposed to have become > independent from their parent ? > > And I don't understand how "the initial value (of the "daemonic" > attribute) is inherited from the creating process", since "daemonic > processes are not allowed to create child processes". Isn't it the > same to say that "daemonic" is always false by default, then ? > And finally, why can't daemonic processes have children ? If these > children get "orphaned" when the daemonic process gets terminated (by > its parent), they'll simply get adpoted by init, won't they ? > > Thanks a lot for helping me get rid of my confusion, > regards, > Pascal > > > =========QUOTES========== > daemon? > > > The process's daemon flag, a Boolean value. This must be set > before start() > > is called. > > The initial value is inherited from the creating process. > > When a process exits, it attempts to terminate all of its daemonic > child processes. > > Note that a daemonic process is not allowed to create child > processes. Otherwise a daemonic process would leave its children > orphaned if it gets terminated when its parent process exits. > > ---------------------- > > Similarly, if the child process is non-daemonic then the parent > process may hang on exit when it tries to join all its non-daemonic children. > -------------- > Remember also that non-daemonic > processes will be automatically be joined. > > > ------------------------------------------------------------------------ > > -- > http://mail.python.org/mailman/listinfo/python-list > Allright, I guess I hadn't understand much about "daemonic processes" in python's multiprocessing module. So, for those interested, here is a clarification of the concepts, as far as I've understood - please poke me if I'm wrong somewhere. Usually, in Unix, daemon processes are processes which got disconnected from their parent process and from terminals, and work in the background, often under a different user identity. The python multiprocessing module has the concept of "daemon" too, but this time in reference to the "threading" module, in which dameons are just threads that wont prevent the application termination, even if they are still running. Thus, daemonic processes launched through multiprocessing API are normal processes that will be terminated (and not joined) if non-dameonic processes are all over. Thus, not much in common between "traditionnal" *nix daemon processes, and python multiprocessing daemon processes. Regards, pascal -------------- next part -------------- An HTML attachment was scrubbed... URL: From arnodel at googlemail.com Sat May 2 12:35:16 2009 From: arnodel at googlemail.com (Arnaud Delobelle) Date: Sat, 02 May 2009 17:35:16 +0100 Subject: for with decimal values? References: Message-ID: Esmail writes: > Hello all, > > Is there a Python construct to allow me to do something like > this: > > for i in range(-10.5, 10.5, 0.1): > ... > > If there is such a thing already available, I'd like > to use it, otherwise I can write a function to mimic this, > but I thought I'd check (my search yielded nothing). > > Thanks, > Esmail In this particular example, you could do this: for i in range(-105, 105): i = 0.1*i ... In general, you'd have to write your own (generator) function. -- Arnaud From mail at timgolden.me.uk Sat May 2 12:35:35 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Sat, 02 May 2009 17:35:35 +0100 Subject: Accessing files in a directory which is a shortcut link (Windows) In-Reply-To: References: Message-ID: <49FC7657.1040104@timgolden.me.uk> jorma kala wrote: > Hi, > I'd like to process files in a directory which is in fact a short cut link > to another directory (under windows XP). > If the path to the directory is for instance called c:\test. I have tried > both following code snipets for printing all names of files in the > directory: > > ++ snippet 1++ > > for filename in glob.glob( os.path.join(r'c:\test', '*') ): > print filename > > ++ snippet 2++ > > for filename in glob.glob( os.path.join(r'c:\test.lnk', '*') ): > print filename Windows shortcuts are Shell (ie GUI Desktop) objects rather than filesystem objects. The filesystem doesn't treat them specially; just returns the .lnk file (or whatever it's called). You need to invoke the Shell functionality specifically. import os, sys import glob import pythoncom from win32com.shell import shell, shellcon def shortcut_target (filename): shell_link = pythoncom.CoCreateInstance ( shell.CLSID_ShellLink, None, pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IShellLink ) ipersist = shell_link.QueryInterface (pythoncom.IID_IPersistFile) ipersist.Load (filename) name, _ = shell_link.GetPath (1) return name def shell_glob (pattern): for filename in glob.glob (pattern): if filename.endswith (".lnk"): yield "%s => %s" % (filename, shortcut_target (filename)) else: yield filename for filename in shell_glob ("c:/temp/*"): print filename TJG From dickinsm at gmail.com Sat May 2 12:45:47 2009 From: dickinsm at gmail.com (Mark Dickinson) Date: Sat, 2 May 2009 09:45:47 -0700 (PDT) Subject: for with decimal values? References: Message-ID: <7832d72d-8e17-4662-8f24-4252c6783f46@x6g2000vbg.googlegroups.com> On May 2, 4:12?pm, Esmail wrote: > Is there a Python construct to allow me to do something like > this: > > ? ? for i in range(-10.5, 10.5, 0.1): > ? ? ? ... I'd recommend using integer arithmetic: for ten_times_i in range(-105, 106): i = ten_times_i / 10.0 ... This has the advantage that it's clear that you want to include the endpoint 10.5. (*Did* you want to include the endpoint?) Presumably you'd want the float version to be interpreted as: i = -10.5 while i < 10.5: ... i += 0.1 (which isn't *quite* right, because the value of i that you end up with *after* the while loop has run to completion is 0.1 more than the last value used within the loop). The big problem with this is that 0.1 is not exactly representable as a float, and so the successive additions in the assignment i += 0.1 are subject to (small) rounding errors. As a result, it's not clear whether the value of i for the last loop is going to be 10.4+small_error or 10.5-small_error. Mark From mail at timgolden.me.uk Sat May 2 12:46:56 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Sat, 02 May 2009 17:46:56 +0100 Subject: Accessing files in a directory which is a shortcut link (Windows) In-Reply-To: <49FC7657.1040104@timgolden.me.uk> References: <49FC7657.1040104@timgolden.me.uk> Message-ID: <49FC7900.60603@timgolden.me.uk> Tim Golden wrote: > Windows shortcuts are Shell (ie GUI Desktop) objects rather > than filesystem objects. The filesystem doesn't treat them > specially; just returns the .lnk file (or whatever it's > called). As a caveat: they don't actually *have* to be called .lnk (altho' they almost universally are). That said, I don't know any straightforward way of otherwise distinguishing shortcut from non-shortcut files. I think you'd just have to call .Load on the IPersist instance and catch the E_FAIL exception. TJG From ebonak at hotmail.com Sat May 2 13:00:03 2009 From: ebonak at hotmail.com (Esmail) Date: Sat, 02 May 2009 13:00:03 -0400 Subject: for with decimal values? In-Reply-To: References: Message-ID: Thanks all, I appreciate the various suggestions and caveats. Just wanted to make sure I'm not re-inventing the wheel as Python seems to have already so much. Cheers, Esmail From Justin at EmbeddedMicro.com Sat May 2 13:12:19 2009 From: Justin at EmbeddedMicro.com (Justin Rajewski) Date: Sat, 02 May 2009 10:12:19 -0700 Subject: Convert variable directly into a string (no ASCII) Message-ID: <1241284339.3478.4.camel@justin-desktop> Hello, I need to print variables out over serial, however I need them to not be in ASCII, ie if the variable is 5 then print 5 not "5". The function that writes to the serial port requires a string and I can send non-variables out with the string "/x05" for 5. Is this even possible? Thanks, Justin From basti.wiesner at gmx.net Sat May 2 13:35:25 2009 From: basti.wiesner at gmx.net (Sebastian Wiesner) Date: Sat, 02 May 2009 19:35:25 +0200 Subject: Question about the wording in the python documents. References: <020b9acb$0$20667$c3e8da3@news.astraweb.com> <4a751872-d92b-4b66-826e-f58a4c33bf16@v1g2000prd.googlegroups.com> Message-ID: > On May 2, 4:14 am, Sebastian Wiesner wrote: >> > >> > In either case, I think you're picking a nit so small that it isn't >> > actually there. All objects are instances (in Python), and all >> > instances are objects. >> >> Exactly, so strictly seen, "Request object" could possibly refer to the >> urllib2.Request class itself. I guess, the OP would prefer the term >> "Request instance", emphasizing, that an instance of the request class >> has to be passed, not he class itself. > > Yes, I personally think that the term "Request instance" would have > made it much clearer to us people that have never taken a computer > science class in our lives. Btw, I should have said, that I don't share your objection: Imho you don't need computer science lessons, but only common sense to understand, that passing the class object itself doesn't really make sense in this context ;) -- Freedom is always the freedom of dissenters. (Rosa Luxemburg) From bearophileHUGS at lycos.com Sat May 2 14:01:39 2009 From: bearophileHUGS at lycos.com (bearophileHUGS at lycos.com) Date: Sat, 2 May 2009 11:01:39 -0700 (PDT) Subject: for with decimal values? References: Message-ID: Esmail: > Is there a Python construct to allow me to do something like > this: > ? ? for i in range(-10.5, 10.5, 0.1): Sometimes I use an improved version of this: http://code.activestate.com/recipes/66472/ Bye, bearophile From ebonak at hotmail.com Sat May 2 14:06:51 2009 From: ebonak at hotmail.com (Esmail) Date: Sat, 02 May 2009 14:06:51 -0400 Subject: for with decimal values? In-Reply-To: References: Message-ID: bearophileHUGS at lycos.com wrote: > Esmail: >> Is there a Python construct to allow me to do something like >> this: >> for i in range(-10.5, 10.5, 0.1): > > Sometimes I use an improved version of this: > http://code.activestate.com/recipes/66472/ neat .. lots of code to study there. Thanks, Esmail From nagle at animats.com Sat May 2 14:10:32 2009 From: nagle at animats.com (John Nagle) Date: Sat, 02 May 2009 11:10:32 -0700 Subject: Is there any way this queue read can possibly block? In-Reply-To: References: <49f9e9c0$0$1604$742ec2ed@news.sonic.net> Message-ID: <49fc83c4$0$1639$742ec2ed@news.sonic.net> That wasn't the problem. An incoherent bug report from a user was the problem. John Nagle Carl Banks wrote: > On Apr 30, 11:48 am, John Nagle wrote: >> def draininput(self) : # consume any queued input >> try: >> while True : >> ch = self.inqueue.get_nowait() # get input, if any >> except Queue.Empty: # if empty >> return # done >> >> "self.inqueue" is a Queue object. The intent here is to drain the >> queue, then return. Is there any way this can possibly block or hang? > > Yes, but it'll be waiting to acquire the semaphore which Queues > normally don't hold onto for long. > > > Carl Banks > >> I wouldn't think so. Another thread is doing "put" operations, >> but slowly, never more than 5 per second. >> >> (It's working for me, but a user of a program of mine is having a problem.) >> >> John Nagle > From rschroev_nospam_ml at fastmail.fm Sat May 2 14:19:29 2009 From: rschroev_nospam_ml at fastmail.fm (Roel Schroeven) Date: Sat, 02 May 2009 20:19:29 +0200 Subject: Multiprocessing.Queue - I want to end. In-Reply-To: References: <200904301649.10341.kyrie@uh.cu> <2mKKl.16353$B17.1674@newsfe26.ams2> Message-ID: Hendrik van Rooyen schreef: > : "Roel Schroeven" wrote: >> ... > This is all true in the case of a job that starts, runs and finishes. > I am not so sure it applies to something that has a long life. It's true that I'm talking about work units with relatively short lifetimes, mostly a few seconds but perhaps maximum about ten minutes. I assumed that queues are mostly used for that kind of stuff. I've never really thought about cases where that assumption doesn't hold, so it's very well possible that all I've said is invalid in other cases. >>> And if the stuff you pass around needs disparate effort to consume, >>> it seems to me that you can more easily balance the load by having >>> specialised consumers, instead of instances of one humungous >>> "I can eat anything" consumer. >> If there is a semantic difference, maybe yes; but I think it makes no >> sense to differentiate purely on the expected execution times. > > The idea is basically that you have the code that classifies in one > place only, instead of running in all the instances of the consumer. > Feels better to me, somehow. I most cases that I can imagine (and certainly in all cases I've used), no classification whatsoever is even needed. -- The saddest aspect of life right now is that science gathers knowledge faster than society gathers wisdom. -- Isaac Asimov Roel Schroeven From apt.shansen at gmail.com Sat May 2 14:24:15 2009 From: apt.shansen at gmail.com (Stephen Hansen) Date: Sat, 2 May 2009 11:24:15 -0700 Subject: Convert variable directly into a string (no ASCII) In-Reply-To: <1241284339.3478.4.camel@justin-desktop> References: <1241284339.3478.4.camel@justin-desktop> Message-ID: <7a9c25c20905021124k5be73ecp3350f10be07081f8@mail.gmail.com> > I need to print variables out over serial, however I need them to not be > in ASCII, ie if the variable is 5 then print 5 not "5". > > The function that writes to the serial port requires a string and I can > send non-variables out with the string "/x05" for 5. > > Is this even possible? > Check out the struct module. >>> a = 5 >>> struct.pack("b", a) '\x05' --S -------------- next part -------------- An HTML attachment was scrubbed... URL: From guenther.dietrich at spamfence.net Sat May 2 14:33:25 2009 From: guenther.dietrich at spamfence.net (=?UTF-8?Q?G=C3=BCnther?= Dietrich) Date: Sat, 02 May 2009 20:33:25 +0200 Subject: Convert variable directly into a string (no ASCII) References: Message-ID: Justin Rajewski wrote: >I need to print variables out over serial, however I need them to not be >in ASCII, ie if the variable is 5 then print 5 not "5". > >The function that writes to the serial port requires a string and I can >send non-variables out with the string "/x05" for 5. See module struct: >>> struct.pack('b', 5) '\x05' >>> struct.pack('BBBB', 5, 150, 3, 224) '\x05\x96\x03\xe0' Regards, G?nther From krmane at gmail.com Sat May 2 14:37:17 2009 From: krmane at gmail.com (Krishnakant) Date: Sun, 03 May 2009 00:07:17 +0530 Subject: problem with money datatype based calculations in python In-Reply-To: <20090502115734.b280bf35.darcy@druid.net> References: <20090502115734.b280bf35.darcy@druid.net> Message-ID: <1241289437.3842.6.camel@kk-laptop> On Sat, 2009-05-02 at 11:57 -0400, D'Arcy J.M. Cain wrote: > And if you use PyGreSQL (http://www.PyGreSQL.org/) to connect to > the database then the money type is already converted to Decimal. > d'arcy, I visited the home page for pygresql and discovered that you wrote the library. I am happy that you also included a non-dbapi (postgresql classic interface ) along with the standard DBAPI module. I would like to know if it has been tested with postgresql 8.3 and are there any known bottlenecks while using this driver module on large scale database opperations? Is this actively maintained? Perhaps this might be off topic but may I ask, how is pypgsql, > Has any one used pgsql for any postgresql based software? and between pygresql and pypgsql which one is more maintained? the system I am developing is a mission critical financial application and I must be pritty sure about any module I am using. happy hacking. Krishnakant. From martin at v.loewis.de Sat May 2 14:41:38 2009 From: martin at v.loewis.de (=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Sat, 02 May 2009 20:41:38 +0200 Subject: Convert variable directly into a string (no ASCII) In-Reply-To: References: Message-ID: <49fc93e3$0$3652$9b622d9e@news.freenet.de> > I need to print variables out over serial, however I need them to not be > in ASCII, ie if the variable is 5 then print 5 not "5". > > The function that writes to the serial port requires a string and I can > send non-variables out with the string "/x05" for 5. > > Is this even possible? So you have a number N given, between 0 and 255, and you want to send the byte whose ordinal is N. Right? Send chr(N). HTH, Martin From doomster at knuut.de Sat May 2 14:47:51 2009 From: doomster at knuut.de (Ulrich Eckhardt) Date: Sat, 02 May 2009 20:47:51 +0200 Subject: Convert variable directly into a string (no ASCII) References: Message-ID: <763iqtF1b3p9cU1@mid.uni-berlin.de> Justin Rajewski wrote: > I need to print variables out over serial, however I need them to not be > in ASCII, ie if the variable is 5 then print 5 not "5". > > The function that writes to the serial port requires a string and I can > send non-variables out with the string "/x05" for 5. Take a look at the chr() function. For completeness, the complementary one is ord(). However, also take a look at the 'struct' module, which is the tool of choice if you want to do other formatting operations. I seem to remember there was a third tool that can be used for similar tasks, but I forgot which it was... Uli From jhmsmits at gmail.com Sat May 2 14:53:17 2009 From: jhmsmits at gmail.com (SanPy) Date: Sat, 2 May 2009 11:53:17 -0700 (PDT) Subject: writing consecutive data to subprocess command 'more' Message-ID: I have this method that prints a given text via a subprocess command 'more' . It is like this: def print_by_page(text): if hasattr(sys.stdout, 'isatty') and sys.stdout.isatty(): viewer = 'more -EMR' proc = subprocess.Popen([viewer], shell=True, stdin=subprocess.PIPE, stderr=subprocess.PIPE) try: stdout, stderr = proc.communicate(text) except OSError: pass else: if stderr: # probably no 'more' available on this system sys.stdout.write(text) return sys.stdout.write(text) It works fine, but what I really want to do is first print a header through the 'more' command, then fetch some data from the web, and show it through the same 'more' command/process. And even more data, another header, other data from the web, all through the same 'more' command/process. Can somebody help me out on this? Regards, Sander Smits From timr at probo.com Sat May 2 15:10:27 2009 From: timr at probo.com (Tim Roberts) Date: Sat, 02 May 2009 12:10:27 -0700 Subject: Convert variable directly into a string (no ASCII) References: Message-ID: <5h6pv45424h4le90mjaoigq1cpror7tu0c@4ax.com> Justin Rajewski wrote: > >I need to print variables out over serial, however I need them to not be >in ASCII, ie if the variable is 5 then print 5 not "5". > >The function that writes to the serial port requires a string and I can >send non-variables out with the string "/x05" for 5. > >Is this even possible? Of course. The struct and array modules can help you with this. Note that you have to know how big each variable should be. For example, if you want to sent 1, 2, and 3 out as bytes, you can do: a = 1 b = 2 c = 3 serial = struct.pack( 'BBB', a, b, c ) If you need them as 16-bit ints, you can use H instead of B. -- Tim Roberts, timr at probo.com Providenza & Boekelheide, Inc. From debatem1 at gmail.com Sat May 2 15:30:42 2009 From: debatem1 at gmail.com (CTO) Date: Sat, 2 May 2009 12:30:42 -0700 (PDT) Subject: How to measure the memory cost in Python? References: <11aabe3b0905010103r14b05fccg7a02c6ae010cd9ad@mail.gmail.com> <7e908367-0604-4400-951e-9e172befc7e7@w31g2000prd.googlegroups.com> <75e47cc6-72bc-4941-802e-767f2187445f@d7g2000prl.googlegroups.com> Message-ID: <7744f434-dd43-4010-ba25-90096f59d1d9@n7g2000prc.googlegroups.com> > > PS) The asizeof(obj) function from this recipe > code.activestate.com/recipes/546530> does size the object plus its > > references, recursively. > > Correction, the last sentence should be: The asizeof(obj) ... plus its > referents, recursively. I will admit, I have *no idea* what that code is doing, but in looking through the gc module documentation, I'm seeing the gc.get_objects function. Would it be equivalent to what the OP is asking to track the size of every element returned by that? From rpmuller at gmail.com Sat May 2 16:11:56 2009 From: rpmuller at gmail.com (Rick) Date: Sat, 2 May 2009 13:11:56 -0700 (PDT) Subject: Profiling gives very different predictions of best algorithm References: <74e32884-95b2-4e1d-9d3d-21f300bd8715@k19g2000prh.googlegroups.com> Message-ID: On May 2, 9:10?am, Rick wrote: > I was posting to the list mostly because I would like to know whether > I can do something different when I profile my code so that it give > results that correspond more closely to those that the nonprofiling > results give. A good comment that was emailed to me rather than being posted to the list was to use cProfile rather than profile. Doing this gave much more realistic results. From seanm.py at gmail.com Sat May 2 16:24:00 2009 From: seanm.py at gmail.com (seanm.py at gmail.com) Date: Sat, 2 May 2009 13:24:00 -0700 (PDT) Subject: Help! Can't get program to run. References: <77a2fc3c-638d-4aef-bae9-3b1ea9d4e988@c36g2000yqn.googlegroups.com> <7618rjF1a3t8jU1@mid.uni-berlin.de> Message-ID: <8f6634a2-c977-430a-b9f2-90ef9356d336@x6g2000vbg.googlegroups.com> On May 1, 6:10?pm, Ned Deily wrote: > In article <7618rjF1a3t8... at mid.uni-berlin.de>, > ?"Diez B. Roggisch" wrote: > > > > > seanm... at gmail.com schrieb: > > > I think this is maybe the most basic problem possible, but I can't get > > > even the most basic Python to run on OS X using Terminal or IDLE. I > > > used the IDLE editor to create a file with one line of code > > > > print 'text string' > > > > and I saved the file as module1.py. When using terminal I entered > > > "python" to load the interpreter or whatever and then tried > > > > python module1.py > > > > but I got an error message. I've tried using the file path instead of > > > module1.py and that didn't work either. I've tried moving the file to > > > different places, my desktop, hard drive, etc., and none of that > > > worked either. In IDLE I tried similar things and only got error > > > messages there too. > > > > Needless to say this is a frustrating start to learning this langauge. > > > I'd really appreciate any help getting this to work, so I can move on > > > the actual language. Thanks so much. > > > It would have helped if you had given us the *actual* error-message. > > ?From what little information you give, it appears as if you do > > something very basic wrong - instead of doing > > $ python > > ?>>> python mymodule.py > > (where $ is the shell/terminal and >>> the python-prompt) > > you either do > > $ python > > ?>>> print "hello" > > or > > $ python mymodule.py > > to execute mymodule directly. > > And from within OS X IDLE itself, if you create or open the file > mymodule.py, it will be in a separate window and, as long as that window > is selected, you can run the file directly within IDLE by selecting "Run > Module" from the "Run" menu. ? So, no need to use the Terminal if you > don't want to. > > -- > ?Ned Deily, > ?n... at acm.org Thank you for both for the help. I still cannot get the program to run though. Below I've copied my commands and the error messages for you (in IDLE then Terminal): IDLE 2.6.2 >>> python module1.py SyntaxError: invalid syntax and sean-marimpietris-computer:~ seanmarimpietri$ python Python 2.6.2 (r262:71600, Apr 16 2009, 09:17:39) [GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> python module1.py File "", line 1 python module1.py ^ SyntaxError: invalid syntax Again, any help would be appreciated. Thanks. From arnodel at googlemail.com Sat May 2 16:30:16 2009 From: arnodel at googlemail.com (Arnaud Delobelle) Date: Sat, 02 May 2009 21:30:16 +0100 Subject: Help! Can't get program to run. References: <77a2fc3c-638d-4aef-bae9-3b1ea9d4e988@c36g2000yqn.googlegroups.com> <7618rjF1a3t8jU1@mid.uni-berlin.de> <8f6634a2-c977-430a-b9f2-90ef9356d336@x6g2000vbg.googlegroups.com> Message-ID: seanm.py at gmail.com writes: > sean-marimpietris-computer:~ seanmarimpietri$ python > Python 2.6.2 (r262:71600, Apr 16 2009, 09:17:39) > [GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin > Type "help", "copyright", "credits" or "license" for more information. >>>> python module1.py > File "", line 1 > python module1.py > ^ > SyntaxError: invalid syntax > > > > Again, any help would be appreciated. Thanks. >From Terminal.app, this should work: sean-marimpietris-computer:~ seanmarimpietri$ python module1.py Assuming that your module1.py file is in /Users/seanmarimpietri/ -- Arnaud From gagsl-py2 at yahoo.com.ar Sat May 2 16:35:19 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Sat, 02 May 2009 17:35:19 -0300 Subject: writing consecutive data to subprocess command 'more' References: Message-ID: En Sat, 02 May 2009 15:53:17 -0300, SanPy escribi?: > I have this method that prints a given text via a subprocess command > 'more' . It is like this: > > def print_by_page(text): > if hasattr(sys.stdout, 'isatty') and sys.stdout.isatty(): > viewer = 'more -EMR' > proc = subprocess.Popen([viewer], shell=True, > stdin=subprocess.PIPE, > stderr=subprocess.PIPE) > try: > stdout, stderr = proc.communicate(text) > except OSError: > pass > else: > if stderr: # probably no 'more' available on this system > sys.stdout.write(text) > return > sys.stdout.write(text) > > It works fine, but what I really want to do is first print a header > through the 'more' command, then fetch some data from the web, and > show it through the same 'more' command/process. And even more data, > another header, other data from the web, all through the same 'more' > command/process. > Can somebody help me out on this? communicate writes to the child's stdin and waits for it to finish. If you want to keep writing, don't use communicate. And you'll need to keep state from one call to another, so use a class. Based on the code above, create a class Pager with __init__, write and close methods: class Pager: def __init__(self): # copy the logic above self.proc = subprocess.Popen(...) self.file = self.proc.stdin # if something goes wrong, set self.proc=None and self.file=sys.stdout def write(self, text): self.file.write(text) def close(self): if self.proc: self.file.close() self.proc.wait() Also, take a look at the pager function in the pydoc module (see the source) - it handles several cases. -- Gabriel Genellina From seanm.py at gmail.com Sat May 2 16:36:00 2009 From: seanm.py at gmail.com (seanm.py at gmail.com) Date: Sat, 2 May 2009 13:36:00 -0700 (PDT) Subject: Most Basic Question Ever - please help Message-ID: <65be3c66-5940-41ab-8a46-da424918b197@z5g2000vba.googlegroups.com> I am going to try posting here again with more detail to see if I can finally get my first program to work. I am working on a MacBook Pro with OS X 10.4.11. I opened a new window in IDLE to create a file. The file had only one line of code and was saved as module1.py. I saved it to Macintosh HD. The one line of code in the file is copied below: print 'Hello module world!' I closed the file and tried to run it in IDLE and Terminal, but I have had no success. I'll paste my commands and the error messages below (for IDLE, then Terminal). Any help would be very much appreciated. I feel like the marathon just started and I've fallen flat on my face. Thanks. IDLE 2.6.2 >>> python module1.py SyntaxError: invalid syntax sean-m-computer:~ seanm$ python Python 2.6.2 (r262:71600, Apr 16 2009, 09:17:39) [GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> python module1.py File "", line 1 python module1.py ^ SyntaxError: invalid syntax From piet at cs.uu.nl Sat May 2 16:37:24 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sat, 02 May 2009 22:37:24 +0200 Subject: writing consecutive data to subprocess command 'more' References: Message-ID: >>>>> SanPy (S) wrote: >S> I have this method that prints a given text via a subprocess command >S> 'more' . It is like this: >S> def print_by_page(text): >S> if hasattr(sys.stdout, 'isatty') and sys.stdout.isatty(): >S> viewer = 'more -EMR' >S> proc = subprocess.Popen([viewer], shell=True, >S> stdin=subprocess.PIPE, >S> stderr=subprocess.PIPE) >S> try: >S> stdout, stderr = proc.communicate(text) >S> except OSError: >S> pass >S> else: >S> if stderr: # probably no 'more' available on this system >S> sys.stdout.write(text) >S> return >S> sys.stdout.write(text) >S> It works fine, but what I really want to do is first print a header >S> through the 'more' command, then fetch some data from the web, and >S> show it through the same 'more' command/process. And even more data, >S> another header, other data from the web, all through the same 'more' >S> command/process. >S> Can somebody help me out on this? You have to split this. Do the startup (creating the subprocess) once, and then for each piece of text that you want to pass through 'more' do stdout, stderr = proc.communicate(text) By the way, in this last line, stdout should come out empty because the output of more doesn't come back to the parent process, but goes to the original stdout of the calling script. Also I would use the following: viewer = ['more', '-EMR'] proc = subprocess.Popen(viewer, stdin=subprocess.PIPE, stderr=subprocess.PIPE) No reason to use a shell. And if there is no 'more' available you get an exception on the Popen already. You don't have to wait until the communicate call. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From seanm.py at gmail.com Sat May 2 16:42:03 2009 From: seanm.py at gmail.com (seanm.py at gmail.com) Date: Sat, 2 May 2009 13:42:03 -0700 (PDT) Subject: Help! Can't get program to run. References: <77a2fc3c-638d-4aef-bae9-3b1ea9d4e988@c36g2000yqn.googlegroups.com> <7618rjF1a3t8jU1@mid.uni-berlin.de> <8f6634a2-c977-430a-b9f2-90ef9356d336@x6g2000vbg.googlegroups.com> Message-ID: <02786fbc-c881-4f68-9877-c662d6362b8e@s31g2000vbp.googlegroups.com> On May 2, 4:30?pm, Arnaud Delobelle wrote: > seanm... at gmail.com writes: > > sean-marimpietris-computer:~ seanmarimpietri$ python > > Python 2.6.2 (r262:71600, Apr 16 2009, 09:17:39) > > [GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin > > Type "help", "copyright", "credits" or "license" for more information. > >>>> python module1.py > > ? File "", line 1 > > ? ? python module1.py > > ? ? ? ? ? ? ? ? ?^ > > SyntaxError: invalid syntax > > > Again, any help would be appreciated. Thanks. > > From Terminal.app, this should work: > > sean-marimpietris-computer:~ seanmarimpietri$ python module1.py > > Assuming that your module1.py file is in /Users/seanmarimpietri/ > > -- > Arnaud Awesome. Thank you, Arnaud. I moved the file to that folder, and I got it to work in Terminal. However, I still can't get it to run using IDLE. Any idea why that might be? Thanks again. -Sean From bvidinli at gmail.com Sat May 2 16:42:31 2009 From: bvidinli at gmail.com (bvidinli) Date: Sat, 2 May 2009 23:42:31 +0300 Subject: eric not working on ubuntu 9.04 Message-ID: <36e8a7020905021342g64efbe30l36230bb0df407ffe@mail.gmail.com> any idea ? here is output: bvidinli at bvidinli-yenihdd:~$ eric Warning: translation file 'qt_tr_TR'could not be loaded. Using default. Warning: translation file 'qscintilla_tr_TR'could not be loaded. Using default. Exception RuntimeError: 'maximum recursion depth exceeded while calling a Python object' in ignored Exception RuntimeError: 'maximum recursion depth exceeded in __subclasscheck__' in ignored An unhandled exception occurred. Please report the problem using the error reporting dialog or via email to . A log has been written to "/home/bvidinli/.eric4/eric4_error.log". Error information: -------------------------------------------------------------------------------- 2009-05-02, 23:42:05 -------------------------------------------------------------------------------- : cannot import name walk -------------------------------------------------------------------------------- File "/usr/share/eric/modules/eric4.py", line 250, in main() File "/usr/share/eric/modules/eric4.py", line 229, in main from UI.UserInterface import UserInterface File "/usr/share/eric/modules/UI/UserInterface.py", line 66, in from EmailDialog import EmailDialog File "/usr/share/eric/modules/UI/EmailDialog.py", line 28, in from email.MIMEBase import MIMEBase File "/usr/lib/python2.6/email/__init__.py", line 79, in __getattr__ __import__(self.__name__) File "/usr/lib/python2.6/email/mime/base.py", line 9, in from email import message File "/usr/lib/python2.6/email/message.py", line 90, in class Message: File "/usr/lib/python2.6/email/message.py", line 790, in Message from email.Iterators import walk -------------------------------------------------------------------------------- Version Numbers: Python 2.6.2 KDE 4.2.2 PyKDE 4.0.2 Qt 4.5.0 PyQt4 4.4.4 sip sip version not available QScintilla 2.3.2 eric4 4.3.0 (r2852) Platform: linux2 2.6.2 (release26-maint, Apr 19 2009, 01:56:41) [GCC 4.3.3] -------------------------------------------------------------------------------- Distribution Info: /etc/lsb-release DISTRIB_ID=Ubuntu DISTRIB_RELEASE=9.04 DISTRIB_CODENAME=jaunty DISTRIB_DESCRIPTION="Ubuntu 9.04" bvidinli at bvidinli-yenihdd:~$ From gert.cuykens at gmail.com Sat May 2 16:44:58 2009 From: gert.cuykens at gmail.com (gert) Date: Sat, 2 May 2009 13:44:58 -0700 (PDT) Subject: return functions Message-ID: Aldo i like the cerrypy wsgi server very much, i do not like the tools that go with it so i am stuck with a configuration file that looks like this http://pastebin.com/m4d8184bc After 152 line I finally arrived to a point where i was thinkig "thats it, this is like going to work on a uni cycle and is just plain ridicules" So how can i generate some functions something like def generate(url,type): # where type could be htm js css or wsgi From davea at ieee.org Sat May 2 16:46:16 2009 From: davea at ieee.org (Dave Angel) Date: Sat, 02 May 2009 16:46:16 -0400 Subject: Multiprocessing.Queue - I want to end. In-Reply-To: <49FCAFAC.40800@ieee.org> References: <200904301649.10341.kyrie@uh.cu> <2mKKl.16353$B17.1674@newsfe26.ams2> <00a601c9cb12$152d25e0$0d00a8c0@Hendrik> <49FCAFAC.40800@ieee.org> Message-ID: <49FCB118.3010908@ieee.org> Hendrik van Rooyen wrote: > : "Roel Schroeven" wrote: > > > >> Hendrik van Rooyen schreef: >> >>> I have always wondered why people do the one queue many getters thing. >>> >> Because IMO it's the simplest and most elegant solution. >> > > That is fair enough... > > >>> Given that the stuff you pass is homogenous in that it will require a >>> similar amount of effort to process, is there not a case to be made >>> to have as many queues as consumers, and to round robin the work? >>> >> Could work if the processing time for each work unit is exactly the same >> (otherwise one or more consumers will be idle part of the time), but in >> most cases that is not guaranteed. A simple example is fetching data >> over the network: even if the data size is always the same, there will >> be differences because of network load variations. >> >> If you use one queue, each consumer fetches a new work unit as soon it >> has consumed the previous one. All consumers will be working as long as >> there is work to do, without having to write any code to do the load >> balancing. >> >> With one queue for each consumer, you either have to assume that the >> average processing time is the same (otherwise some consumers will be >> idle at the end, while others are still busy processing work units), or >> you need some clever code in the producer(s) or the driving code to >> balance the loads. That's extra complexity for little or no benefit. >> >> I like the simplicity of having one queue: the producer(s) put work >> units on the queue with no concern which consumer will process them or >> how many consumers there even are; likewise the consumer(s) don't know >> and don't need to know where their work units come from. And the work >> gets automatically distributed to whichever consumer has first finished >> its previous work unit. >> > > This is all true in the case of a job that starts, runs and finishes. > I am not so sure it applies to something that has a long life. > > >>> And if the stuff you pass around needs disparate effort to consume, >>> it seems to me that you can more easily balance the load by having >>> specialised consumers, instead of instances of one humungous "I can >>> eat anything" consumer. >>> >> If there is a semantic difference, maybe yes; but I think it makes no >> sense to differentiate purely on the expected execution times. >> > > The idea is basically that you have the code that classifies in one > place only, instead of running in all the instances of the consumer. > Feels better to me, somehow. > If the classifying you're doing is just based on expected time to consume the item, then I think your plan to use separate queues is misguided. If the consumers are interchangeable in their abilities, then feeding them from a single queue is more efficient, both on average wait time, worst-case wait time, and on consumer utilization, in nearly all non-pathological scenarios. Think the line at the bank. There's a good reason they now have a single line for multiple tellers. If you have five tellers, and one of your transactions is really slow, the rest of the line is only slowed down by 20%, rather than a few people being slowed down by a substantial amount because they happen to be behind the slowpoke. 30 years ago, they'd have individual lines, and I tried in vain to explain queuing theory to the bank manager. Having said that, notice that sometimes the consumers in a computer are not independent. If you're running 20 threads with this model, on a processor with only two cores, and if the tasks are CPU bound, you're wasting lots of thread-management time without gaining anything. Similarly, if there are other shared resources that all the threads trip over, it may not pay to do as many of them in parallel. But for homogeneous consumers, do them with a single queue, and do benchmarks to determine the optimum number of consumers to start. From gert.cuykens at gmail.com Sat May 2 16:58:10 2009 From: gert.cuykens at gmail.com (gert) Date: Sat, 2 May 2009 13:58:10 -0700 (PDT) Subject: return functions References: Message-ID: <2b9deb7a-327d-40b2-8df9-d6db916a6bab@s28g2000vbp.googlegroups.com> On May 2, 10:44?pm, gert wrote: > Aldo i like the cerrypy wsgi server very much, i do not like the tools > that go with it > so i am stuck with a configuration file that looks like this > > http://pastebin.com/m4d8184bc > > After 152 line I finally arrived to a point where i was thinkig "thats > it, this is like going to work on a uni cycle and is just plain > ridicules" > > So how can i generate some functions something like > > def generate(url,type): # where type could be htm js css or wsgi I was thinking something like this ? def static(url, mime): def application(environ, response): f=open(os.path.join(os.path.dirname(__file__), url),'rb') l=os.fstat(f.fileno()).st_size response('200 OK', [('Content-type', mime+';charset=utf-8'), ('Content-Length', str(l))]) return FileWrapper(f, 8192) return application From jhmsmits at gmail.com Sat May 2 17:00:44 2009 From: jhmsmits at gmail.com (SanPy) Date: Sat, 2 May 2009 14:00:44 -0700 (PDT) Subject: writing consecutive data to subprocess command 'more' References: Message-ID: Thanks, that works beautifully! Regards, Sander On 2 mei, 22:35, "Gabriel Genellina" wrote: > En Sat, 02 May 2009 15:53:17 -0300, SanPy escribi?: > > > > > > > I have this method that prints a given text via a subprocess command > > 'more' . It is like this: > > > def print_by_page(text): > > ? ? if hasattr(sys.stdout, 'isatty') and sys.stdout.isatty(): > > ? ? ? ? viewer = 'more -EMR' > > ? ? ? ? proc = subprocess.Popen([viewer], shell=True, > > stdin=subprocess.PIPE, > > ? ? ? ? ? ? stderr=subprocess.PIPE) > > ? ? ? ? try: > > ? ? ? ? ? ? stdout, stderr = proc.communicate(text) > > ? ? ? ? except OSError: > > ? ? ? ? ? ? pass > > ? ? ? ? else: > > ? ? ? ? ? ? if stderr: # probably no 'more' available on this system > > ? ? ? ? ? ? ? ? sys.stdout.write(text) > > ? ? ? ? ? ? return > > ? ? sys.stdout.write(text) > > > It works fine, but what I really want to do is first print a header > > through the 'more' command, then fetch some data from the web, and > > show it through the same 'more' command/process. And even more data, > > another header, other data from the web, all through the same 'more' > > command/process. > > Can somebody help me out on this? > > communicate writes to the child's stdin and waits for it to finish. If you ? > want to keep writing, don't use communicate. And you'll need to keep state ? > ?from one call to another, so use a class. Based on the code above, create ? > a class Pager with __init__, write and close methods: > > class Pager: > ? def __init__(self): > ? ?# copy the logic above > ? ?self.proc = subprocess.Popen(...) > ? ?self.file = self.proc.stdin > ? ?# if something goes wrong, set self.proc=None and self.file=sys.stdout > > ? def write(self, text): > ? ?self.file.write(text) > > ? def close(self): > ? ?if self.proc: > ? ? self.file.close() > ? ? self.proc.wait() > > Also, take a look at the pager function in the pydoc module (see the ? > source) - it handles several cases. > > -- > Gabriel Genellina From rhodri at wildebst.demon.co.uk Sat May 2 17:21:15 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Sat, 02 May 2009 22:21:15 +0100 Subject: Help! Can't get program to run. In-Reply-To: <02786fbc-c881-4f68-9877-c662d6362b8e@s31g2000vbp.googlegroups.com> References: <77a2fc3c-638d-4aef-bae9-3b1ea9d4e988@c36g2000yqn.googlegroups.com> <7618rjF1a3t8jU1@mid.uni-berlin.de> <8f6634a2-c977-430a-b9f2-90ef9356d336@x6g2000vbg.googlegroups.com> <02786fbc-c881-4f68-9877-c662d6362b8e@s31g2000vbp.googlegroups.com> Message-ID: On Sat, 02 May 2009 21:42:03 +0100, wrote: > Awesome. Thank you, Arnaud. I moved the file to that folder, and I got > it to work in Terminal. However, I still can't get it to run using > IDLE. Any idea why that might be? Thanks again. -Sean To repeat what you were told earlier, IDLE is an interactive Python shell, not a command line. It executes Python commands directly, like >>> print "Hello, world" "python mymodule.py" is *not* a Python command. To run your script from within IDLE, you have two choices: either open the script to edit with IDLE (menu File->Open or Ctrl-O) and use menu Run->Run Module or F5 to run it; or (assuming mymodule.py is in your home directory) type >>> import mymodule The former has a few gotchas that you probably won't hit, and the latter has several that you almost certainly will hit. Personally I'd stick with the command line that you've already succeeded with! -- Rhodri James *-* Wildebeeste Herder to the Masses From nad at acm.org Sat May 2 17:30:40 2009 From: nad at acm.org (Ned Deily) Date: Sat, 02 May 2009 14:30:40 -0700 Subject: Help! Can't get program to run. References: <77a2fc3c-638d-4aef-bae9-3b1ea9d4e988@c36g2000yqn.googlegroups.com> <7618rjF1a3t8jU1@mid.uni-berlin.de> <8f6634a2-c977-430a-b9f2-90ef9356d336@x6g2000vbg.googlegroups.com> Message-ID: In article <8f6634a2-c977-430a-b9f2-90ef9356d336 at x6g2000vbg.googlegroups.com>, seanm.py at gmail.com wrote: > Thank you for both for the help. I still cannot get the program to run > though. Below I've copied my commands and the error messages for you > (in IDLE then Terminal): > > IDLE 2.6.2 > >>> python module1.py > SyntaxError: invalid syntax The default IDLE window is a python shell window. That is, you are already "inside" the python interpreter, the same as if you typed just "python", with no file name, in the Terminal window. You can use that to execute python statements and introspect objects, among other things. Try it. But, back in IDLE, ignore that window for the moment and use the Open command in the File Menu to select and open your file module1.py from whatever directory it is in. A new window should open with the contents of that file. With that window selected (click on it, if necessary), there should be a "Run" option in the Menu bar and under it will be a "Run Script" option that will cause the script to run and the output to appear in the shell window. You can then explore the other options available in IDLE for debugging, etc. -- Ned Deily, nad at acm.org From mike.blonder at gmail.com Sat May 2 17:40:34 2009 From: mike.blonder at gmail.com (mikefromvt) Date: Sat, 2 May 2009 14:40:34 -0700 (PDT) Subject: Need Help with str.replace & tuples Message-ID: <90983bf0-c21d-44f5-91c1-873c484678b9@s16g2000vbp.googlegroups.com> Hello. I am very very unfamiliar with Python and need to update a Python script. What I need to do is to replace three variables (already defined in the script) within a string. The present script correctly replaces two of the three variables. I am unable to add a third variable. Specifically, I would like to add "panoble",panoble to the following code: idxItemStr+=string.replace(string.replace (idxItemTplStr,"stylenumber",stylenumber),"formalname",formalname) and, specifically as regards idxItemTplStr Is this possible? Thanks Mike From apt.shansen at gmail.com Sat May 2 17:44:43 2009 From: apt.shansen at gmail.com (Stephen Hansen) Date: Sat, 2 May 2009 14:44:43 -0700 Subject: Most Basic Question Ever - please help In-Reply-To: <65be3c66-5940-41ab-8a46-da424918b197@z5g2000vba.googlegroups.com> References: <65be3c66-5940-41ab-8a46-da424918b197@z5g2000vba.googlegroups.com> Message-ID: <7a9c25c20905021444x620ed83cmf95d0381df185712@mail.gmail.com> > > IDLE 2.6.2 > >>> python module1.py > SyntaxError: invalid syntax > The ">>>" prompt is Python's interactive interpreter. Once you are here, you already are in python-- so typing "python" again is redundant and invalid. >From this prompt you type in python code. I have never used IDLE so can't comment on how to get it to run modules and collect output-- but I suspect when your file is open in IDLE there's a menu option that says something about Run. But.... > sean-m-computer:~ seanm$ python > Python 2.6.2 (r262:71600, Apr 16 2009, 09:17:39) > [GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin > Type "help", "copyright", "credits" or "license" for more information. > >>> python module1.py > File "", line 1 > python module1.py > ^ > SyntaxError: invalid syntax > Same thing. You're starting python: then doing 'python module1.py' within Python, which doesn't make sense. If you want Python to run the module, just do "python module1.py" instead of "python" from your command prompt. --S -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at ieee.org Sat May 2 17:46:15 2009 From: davea at ieee.org (Dave Angel) Date: Sat, 02 May 2009 17:46:15 -0400 Subject: Most Basic Question Ever - please help In-Reply-To: <65be3c66-5940-41ab-8a46-da424918b197@z5g2000vba.googlegroups.com> References: <65be3c66-5940-41ab-8a46-da424918b197@z5g2000vba.googlegroups.com> Message-ID: <49FCBF27.5060103@ieee.org> seanm.py at gmail.com wrote: > I am going to try posting here again with more detail to see if I can > finally get my first program to work. > > I am working on a MacBook Pro with OS X 10.4.11. I opened a new window > in IDLE to create a file. The file had only one line of code and was > saved as module1.py. I saved it to Macintosh HD. The one line of code > in the file is copied below: > > print 'Hello module world!' > > I closed the file and tried to run it in IDLE and Terminal, but I have > had no success. I'll paste my commands and the error messages below > (for IDLE, then Terminal). Any help would be very much appreciated. I > feel like the marathon just started and I've fallen flat on my face. > Thanks. > > IDLE 2.6.2 > >>>> python module1.py >>>> > SyntaxError: invalid syntax > > > sean-m-computer:~ seanm$ python > Python 2.6.2 (r262:71600, Apr 16 2009, 09:17:39) > [GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin > Type "help", "copyright", "credits" or "license" for more information. > >>>> python module1.py >>>> > File "", line 1 > python module1.py > ^ > SyntaxError: invalid syntax > > > In both cases, you're already running python. Why would you expect to have to run python inside python? Once you're at a python prompt (in either of your cases), you use the command "import" to load a module. And you do not put the ".py" extension on the parameter. Specifically, it should look like this, and very similar for IDLE. M:\Programming\Python\sources\temp>c:\ProgFiles\Python26\python.exe Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import module1 Hello module world! >>> > > From bearophileHUGS at lycos.com Sat May 2 18:12:36 2009 From: bearophileHUGS at lycos.com (bearophileHUGS at lycos.com) Date: Sat, 2 May 2009 15:12:36 -0700 (PDT) Subject: Dictionary, integer compression References: <368d63c2-0467-4c3e-a623-2a39ef3d92c7@r31g2000prh.googlegroups.com> <20dee642-fbfa-4c9f-a182-7319077298e5@t36g2000prt.googlegroups.com> Message-ID: <5fe34cf2-e56c-4ed2-a2fd-7c2fc7d5efcc@o27g2000vbd.googlegroups.com> dineshv: > Thanks for that about Python3. ?My integers range from 0 to 9,999,999 > and I have loads of them. ?Do you think Python3 will help? Nope. Bye, bearophile From gert.cuykens at gmail.com Sat May 2 18:17:54 2009 From: gert.cuykens at gmail.com (gert) Date: Sat, 2 May 2009 15:17:54 -0700 (PDT) Subject: return functions References: <2b9deb7a-327d-40b2-8df9-d6db916a6bab@s28g2000vbp.googlegroups.com> Message-ID: <0b04c45d-0152-4498-a331-a19452c29b2f@l28g2000vba.googlegroups.com> On May 2, 10:58?pm, gert wrote: > On May 2, 10:44?pm, gert wrote: > > > Aldo i like the cerrypy wsgi server very much, i do not like the tools > > that go with it > > so i am stuck with a configuration file that looks like this > > >http://pastebin.com/m4d8184bc > > > After 152 line I finally arrived to a point where i was thinkig "thats > > it, this is like going to work on a uni cycle and is just plain > > ridicules" > > > So how can i generate some functions something like > > > def generate(url,type): # where type could be htm js css or wsgi > > I was thinking something like this ? > > def static(url, mime): > ? ? def application(environ, response): > ? ? ? ? f=open(os.path.join(os.path.dirname(__file__), url),'rb') > ? ? ? ? l=os.fstat(f.fileno()).st_size > ? ? ? ? response('200 OK', [('Content-type', mime+';charset=utf-8'), > ('Content-Length', str(l))]) > ? ? ? ? return FileWrapper(f, 8192) > ? ? return application it works, i reduced it to this :-) http://code.google.com/p/appwsgi/source/browse/trunk/server.py any other suggestions to make it smaller and more automatic ? From bearophileHUGS at lycos.com Sat May 2 18:22:34 2009 From: bearophileHUGS at lycos.com (bearophileHUGS at lycos.com) Date: Sat, 2 May 2009 15:22:34 -0700 (PDT) Subject: Need Help with str.replace & tuples References: <90983bf0-c21d-44f5-91c1-873c484678b9@s16g2000vbp.googlegroups.com> Message-ID: mikefromvt: > I am very very unfamiliar with Python and need to update a Python > script. ?What I need to do is to replace three variables (already > defined in the script) within a string. ?The present script correctly > replaces two of the three variables. ?I am unable to add a third > variable. ?Specifically, I would like to add > > "panoble",panoble > to the following code: > idxItemStr+=string.replace(string.replace (idxItemTplStr,"stylenumber",stylenumber),"formalname",formalname) This isn't going to win some contest of coding style quality, but you may try this (for other people: I have not added any new variable name on purpose): idxItemStr += idxItemTplStr.replace("stylenumber", stylenumber).replace("formalname", formalname).replace("panoble", panoble) Note that in Python names are usually written like this: idx_item_tpl Instead of: idxItemTplStr Bye, bearophile From sjmachin at lexicon.net Sat May 2 18:25:26 2009 From: sjmachin at lexicon.net (John Machin) Date: Sat, 2 May 2009 15:25:26 -0700 (PDT) Subject: Most Basic Question Ever - please help References: <65be3c66-5940-41ab-8a46-da424918b197@z5g2000vba.googlegroups.com> Message-ID: <0b10f7fd-9b08-40d4-8395-83bdfe860f90@v23g2000pro.googlegroups.com> On May 3, 7:46?am, Dave Angel wrote: > seanm... at gmail.com wrote: > > I am going to try posting here again with more detail to see if I can > > finally get my first program to work. > > > I am working on a MacBook Pro with OS X 10.4.11. I opened a new window > > in IDLE to create a file. The file had only one line of code and was > > saved as module1.py. I saved it to Macintosh HD. The one line of code > > in the file is copied below: > > > print 'Hello module world!' > > > I closed the file and tried to run it in IDLE and Terminal, but I have > > had no success. I'll paste my commands and the error messages below > > (for IDLE, then Terminal). Any help would be very much appreciated. I > > feel like the marathon just started and I've fallen flat on my face. > > Thanks. > > > IDLE 2.6.2 > > >>>> python module1.py > > > SyntaxError: invalid syntax > > > sean-m-computer:~ seanm$ python > > Python 2.6.2 (r262:71600, Apr 16 2009, 09:17:39) > > [GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin > > Type "help", "copyright", "credits" or "license" for more information. > > >>>> python module1.py > > > ? File "", line 1 > > ? ? python module1.py > > ? ? ? ? ? ? ? ? ?^ > > SyntaxError: invalid syntax > > In both cases, you're already running python. ?Why would you expect to > have to run python inside python? > > Once you're at a python prompt (in either of your cases), you use the > command "import" to load a module. ?And you do not put the ".py" > extension on the parameter. ?Specifically, it should look like this, and > very similar for IDLE. > > M:\Programming\Python\sources\temp>c:\ProgFiles\Python26\python.exe > Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit > (Intel)] on > win32 > Type "help", "copyright", "credits" or "license" for more information. > ?>>> import module1 > Hello module world! > ?>>> Dave, importing modules which have side effects like printing is NOT a good habit to which a beginner should be introduced. He needs to know how to run a script. Sean, in Terminal, instead of typing python type python module1.py and I suggest that you give your script (not module) a more meaningful name. HTH, John From sjmachin at lexicon.net Sat May 2 18:45:31 2009 From: sjmachin at lexicon.net (John Machin) Date: Sat, 2 May 2009 15:45:31 -0700 (PDT) Subject: problem with money datatype based calculations in python References: <1241250322.4669.13.camel@kk-laptop> Message-ID: On May 3, 12:09?am, Dave Angel wrote: > Krishnakant wrote: > > hello all, > > I am using postgresql as a database server for my db application. > > > The database is related to accounts and point of sales and many > > calculations involve money datatype. > > > The application logic is totally done in python. ?Now the problem is > > that there is a situation where we calculate tax on % of the total > > amount on the invoice. ?The ?percentage is in terms of float but the > > eventual amount has to be in a money datatype. > > > The product cost comes in money datatype from postgresql and the tax % > > comes in float. ?So I would like to know if python supports some thing > > similar to money datatype so that I can cast my total amount (in money ) > > divided by tax% (in float ) so money divided by float should be the > > result and that result should be in money datatype. > > For example a product x has the cost Rs. 100 which is stored in the > > tabel as money type. ?and in the tax table for that product the VAT is > > 5% and this 5% is stored in float datatype. ?So after using both these > > values the resulting total 105Rs. should be in money type and not float. > > I awaite some reply, > > Thanks in advice. > > happy hacking. > > Krishnakant. > > Tax rate isn't divided, it's multiplied. ?But probably that's just a > typo in your message. > > Short answer is use your money class to convert the float into money. ? > And then test, to see that's really what you wanted. ?Be sure and check > the edge cases, because float is a binary system, and you can get both > quantization and rounding problems converting between them. > > Is the money you're dealing with decimal-based? ?So maybe money values > are integers plus two more digits of fractional precision? > > It's going to be tough for somebody with no access to that "money type" > to guess what your problem is. ?What operations does it currently > support? ?If it supports multiply, then just change the tax rate to be a > money type. Aarrgghh!! If the "money type" supports multiplying money by money, the author should be put in the stocks and pelted with rotten vegetables. (IMHO) From ben+python at benfinney.id.au Sat May 2 19:48:34 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Sun, 03 May 2009 09:48:34 +1000 Subject: for with decimal values? References: Message-ID: <8763gjm5gt.fsf@benfinney.id.au> Esmail writes: > Hello all, > > Is there a Python construct to allow me to do something like > this: > > for i in range(-10.5, 10.5, 0.1): > ... Note that those values are of type ?float?, which is a distinct type from ?Decimal? with very different behaviour. > If there is such a thing already available, I'd like to use it, > otherwise I can write a function to mimic this, but I thought I'd > check (my search yielded nothing). You can write a function to do it. Alternatively, this case seems simple enough that you can write a generator expression. Assuming you want ?Decimal? values: >>> from decimal import Decimal >>> amounts = (Decimal(mantissa)/10 for mantissa in range(-105, 105)) >>> for amount in amounts: ... print amount ... -10.5 -10.4 -10.3 -10.2 -10.1 -10 -9.9 ? 9.6 9.7 9.8 9.9 10 10.1 10.2 10.3 10.4 -- \ ?I have a large seashell collection, which I keep scattered on | `\ the beaches all over the world. Maybe you've seen it.? ?Steven | _o__) Wright | Ben Finney From seanm.py at gmail.com Sat May 2 20:08:07 2009 From: seanm.py at gmail.com (seanm.py at gmail.com) Date: Sat, 2 May 2009 17:08:07 -0700 (PDT) Subject: Help! Can't get program to run. References: <77a2fc3c-638d-4aef-bae9-3b1ea9d4e988@c36g2000yqn.googlegroups.com> <7618rjF1a3t8jU1@mid.uni-berlin.de> <8f6634a2-c977-430a-b9f2-90ef9356d336@x6g2000vbg.googlegroups.com> Message-ID: <85d158e7-6d31-46ca-8c61-85d422a16087@21g2000vbk.googlegroups.com> On May 2, 5:30?pm, Ned Deily wrote: > In article > <8f6634a2-c977-430a-b9f2-90ef9356d... at x6g2000vbg.googlegroups.com>, > > ?seanm... at gmail.com wrote: > > Thank you for both for the help. I still cannot get the program to run > > though. Below I've copied my commands and the error messages for you > > (in IDLE then Terminal): > > > IDLE 2.6.2 > > >>> python module1.py > > SyntaxError: invalid syntax > > The default IDLE window is a python shell window. ?That is, you are > already "inside" the python interpreter, the same as if you typed just > "python", with no file name, in the Terminal window. ?You can use that > to execute python statements and introspect objects, among other things. ? > Try it. > > But, back in IDLE, ignore that window for the moment and use the Open > command in the File Menu to select and open your file module1.py from > whatever directory it is in. ?A new window should open with the contents > of that file. ?With that window selected (click on it, if necessary), > there should be a "Run" option in the Menu bar and under it will be a > "Run Script" option that will cause the script to run and the output to > appear in the shell window. ?You can then explore the other options > available in IDLE for debugging, etc. > > -- > ?Ned Deily, > ?n... at acm.org Awesome. I think that clears it up (at least enough for now). Thank you both very much. From seanm.py at gmail.com Sat May 2 20:10:14 2009 From: seanm.py at gmail.com (seanm.py at gmail.com) Date: Sat, 2 May 2009 17:10:14 -0700 (PDT) Subject: Most Basic Question Ever - please help References: <65be3c66-5940-41ab-8a46-da424918b197@z5g2000vba.googlegroups.com> <0b10f7fd-9b08-40d4-8395-83bdfe860f90@v23g2000pro.googlegroups.com> Message-ID: On May 2, 6:25?pm, John Machin wrote: > On May 3, 7:46?am, Dave Angel wrote: > > > > > seanm... at gmail.com wrote: > > > I am going to try posting here again with more detail to see if I can > > > finally get my first program to work. > > > > I am working on a MacBook Pro with OS X 10.4.11. I opened a new window > > > in IDLE to create a file. The file had only one line of code and was > > > saved as module1.py. I saved it to Macintosh HD. The one line of code > > > in the file is copied below: > > > > print 'Hello module world!' > > > > I closed the file and tried to run it in IDLE and Terminal, but I have > > > had no success. I'll paste my commands and the error messages below > > > (for IDLE, then Terminal). Any help would be very much appreciated. I > > > feel like the marathon just started and I've fallen flat on my face. > > > Thanks. > > > > IDLE 2.6.2 > > > >>>> python module1.py > > > > SyntaxError: invalid syntax > > > > sean-m-computer:~ seanm$ python > > > Python 2.6.2 (r262:71600, Apr 16 2009, 09:17:39) > > > [GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin > > > Type "help", "copyright", "credits" or "license" for more information. > > > >>>> python module1.py > > > > ? File "", line 1 > > > ? ? python module1.py > > > ? ? ? ? ? ? ? ? ?^ > > > SyntaxError: invalid syntax > > > In both cases, you're already running python. ?Why would you expect to > > have to run python inside python? > > > Once you're at a python prompt (in either of your cases), you use the > > command "import" to load a module. ?And you do not put the ".py" > > extension on the parameter. ?Specifically, it should look like this, and > > very similar for IDLE. > > > M:\Programming\Python\sources\temp>c:\ProgFiles\Python26\python.exe > > Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit > > (Intel)] on > > win32 > > Type "help", "copyright", "credits" or "license" for more information. > > ?>>> import module1 > > Hello module world! > > ?>>> > > Dave, importing modules which have side effects like printing is NOT a > good habit to which a beginner should be introduced. He needs to know > how to run a script. > > Sean, in Terminal, instead of typing > ? ? python > type > ? ? python module1.py > > and I suggest that you give your script (not module) a more meaningful > name. > > HTH, > John Great. Thank you both very much. I appreciate the help. From mark.seagoe at gmail.com Sat May 2 20:45:29 2009 From: mark.seagoe at gmail.com (mark.seagoe at gmail.com) Date: Sat, 2 May 2009 17:45:29 -0700 (PDT) Subject: web access through vpn client References: <0aa2beb9-b2b1-4bba-b130-e88821b914e8@d19g2000prh.googlegroups.com> <3b82989a-7e54-461a-b18f-dc90d803c0f9@x29g2000prf.googlegroups.com> Message-ID: On May 2, 3:01?am, "Martin P. Hellwig" wrote: > mark.sea... at gmail.com wrote: > > On May 1, 5:57 pm, "Martin P. Hellwig" > > wrote: > >> mark.sea... at gmail.com wrote: > >>> Hi; > >>> I am trying to retrieve financial data off website for stock market > >>> analysis. ?Just hobby not for pay. ?I actually am impressed that > >>> urllib2 and BeautifulSoup work pretty well to do what I want, and the > >>> first little routine actually gets the data from the web page... > >>> except if my VPN client is turned on. > >>> I've looked around for a solution but can't find... perhaps I'm not > >>> entering the right search words. ?I don't even know where to start. > >>> Can anyone suggest some good reading, or basic idea how to approach? > >>> Thanks, > >>> Mark > >> When the VPN client is on, can you actually still browse the 'classical' > >> way to the website in question? > >> I ask this because many cooperate VPN clients have a way of shutting > >> down all other access besides to the VPN server (which is very annoying > >> but I can see the logic behind it). > > >> If that is the case your VPN network might have a webproxy which needs > >> to be configured to continue browsing when connected to the VPN. For > >> python you can automate this by checking for access to the proxy and use > >> it if it is there. > > >> -- > >> MPHhttp://blog.dcuktec.com > > > Yes when Cisco AnyConnect VPN client is connected, I can browse w/ > > IE. ?I think the packets get encrypted and possibly go to my company's > > server and out from there, because then also Outlook connects to my > > company, Communicator works, etc. ?With VPN off I can browse and > > Python can get URL, but no connection through my company's server & no > > encryption. > > > Where can I find out more about how to configure the webproxy, if the > > info is handy? ?Thanks! > > > Mark > > Most likely your IE gets automatically configured for using a proxy when > the VPN is made, just check your IE settings ounce you are connected to > the VPN particular the Connection / LAN-connection settings. > Or query what is set in the registry at : > 'HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet > Settings' > > And use the content (if set) as the proxy address in the urllib module. > > You might find this module useful for that:http://docs.python.org/library/_winreg.html > > -- > MPHhttp://blog.dcuktec.com- Hide quoted text - > > - Show quoted text - Thanks! I'll check it out. From ebonak at hotmail.com Sat May 2 21:31:38 2009 From: ebonak at hotmail.com (Esmail) Date: Sat, 02 May 2009 21:31:38 -0400 Subject: for with decimal values? In-Reply-To: <8763gjm5gt.fsf@benfinney.id.au> References: <8763gjm5gt.fsf@benfinney.id.au> Message-ID: <49FCF3FA.3020401@hotmail.com> Ben Finney wrote: > > Note that those values are of type ?float?, which is a distinct type > from ?Decimal? with very different behaviour. > >> If there is such a thing already available, I'd like to use it, >> otherwise I can write a function to mimic this, but I thought I'd >> check (my search yielded nothing). > > You can write a function to do it. > > Alternatively, this case seems simple enough that you can write a > generator expression. Assuming you want ?Decimal? values: > > >>> from decimal import Decimal > >>> amounts = (Decimal(mantissa)/10 for mantissa in range(-105, 105)) > >>> for amount in amounts: > ... print amount > ... Another nice solution stored away for use! Thanks, Esmail From soumen08 at gmail.com Sat May 2 21:41:09 2009 From: soumen08 at gmail.com (Soumen banerjee) Date: Sun, 3 May 2009 07:11:09 +0530 Subject: Can someone please explain to me this code? Message-ID: Hello, I was trying to find a method to make global hotkeys with python in linux. I found this one which uses a library called python-xlib. The point is that since i dont have much experience with this, i cant understand some of the code. Can someone please explain to me how this code works? According to the author, it is meant to reduce and increase volume. Im not interested in that specifically. All i want is to be ale to bind a hotkey to a function in my python program. from Xlib.display import Display from Xlib import X import oss # custom keys from my dell D400 Laptop vol_plus = 176 vol_moins = 174 keys = [vol_plus,vol_moins] def changeVolume(aValue): mixer = oss.open_mixer() symbol = oss.SOUND_DEVICE_LABELS.index('Vol ') left,right = mixer.read_channel(symbol) avg = (left + right) / 2 if (avg + aValue) >= 0: mixer.write_channel(symbol,(left + aValue,right + aValue)) mixer.close() def handle_event(aEvent): keycode = aEvent.detail if aEvent.type == X.KeyPress: if keycode == vol_moins: changeVolume(-2) elif keycode == vol_plus: changeVolume(+2) def main(): # current display disp = Display() root = disp.screen().root # we tell the X server we want to catch keyPress event root.change_attributes(event_mask = X.KeyPressMask) for keycode in keys: root.grab_key(keycode, X.AnyModifier, 1,X.GrabModeAsync, X.GrabModeAsync) while 1: event = root.display.next_event() handle_event(event) if __name__ == '__main__': main() Regards Soumen From gert.cuykens at gmail.com Sat May 2 21:53:37 2009 From: gert.cuykens at gmail.com (gert) Date: Sat, 2 May 2009 18:53:37 -0700 (PDT) Subject: eval(WsgiApplication) References: <8357032b-5fc0-4d4c-843a-a1ea27c62e5c@o14g2000vbo.googlegroups.com> <39057d64-921e-4150-a382-ec1d443a383f@j12g2000vbl.googlegroups.com> Message-ID: On May 2, 2:15?pm, ?????? ??????????? wrote: > >> > How do I do this in python3? > > >> What's wrong with importing it? > > > The problem is that my wsgi files have a wsgi extention for mod_wsgi > > use > .. > > mod_wsgi has a .wsgi handler because it is recommended to rename the > > wsgi file with wsgi extensions to avoid double imports > > cherrypy server has a dispatcher class > > You can either use .py extension for the wsgi files OR use a custom > importer that can import your .wsgi fileshttp://docs.python.org/library/modules.html > > -- > ?????? (http://softver.org.mk/damjan/) > > Scarlett Johansson: You always see the glass half-empty. > Woody Allen: No. I see the glass half-full, but of poison. I stick with the .py files thank you :) From gert.cuykens at gmail.com Sat May 2 21:56:12 2009 From: gert.cuykens at gmail.com (gert) Date: Sat, 2 May 2009 18:56:12 -0700 (PDT) Subject: return functions References: <2b9deb7a-327d-40b2-8df9-d6db916a6bab@s28g2000vbp.googlegroups.com> <0b04c45d-0152-4498-a331-a19452c29b2f@l28g2000vba.googlegroups.com> Message-ID: <901b5269-fd69-4664-aae7-4cce91ed1dd9@o14g2000vbo.googlegroups.com> On May 3, 12:17?am, gert wrote: > On May 2, 10:58?pm, gert wrote: > > > > > On May 2, 10:44?pm, gert wrote: > > > > Aldo i like the cerrypy wsgi server very much, i do not like the tools > > > that go with it > > > so i am stuck with a configuration file that looks like this > > > >http://pastebin.com/m4d8184bc > > > > After 152 line I finally arrived to a point where i was thinkig "thats > > > it, this is like going to work on a uni cycle and is just plain > > > ridicules" > > > > So how can i generate some functions something like > > > > def generate(url,type): # where type could be htm js css or wsgi > > > I was thinking something like this ? > > > def static(url, mime): > > ? ? def application(environ, response): > > ? ? ? ? f=open(os.path.join(os.path.dirname(__file__), url),'rb') > > ? ? ? ? l=os.fstat(f.fileno()).st_size > > ? ? ? ? response('200 OK', [('Content-type', mime+';charset=utf-8'), > > ('Content-Length', str(l))]) > > ? ? ? ? return FileWrapper(f, 8192) > > ? ? return application > > it works, i reduced it to this :-) > > http://code.google.com/p/appwsgi/source/browse/trunk/server.py > > any other suggestions to make it smaller and more automatic ? Wait a minute why am i not using this ? http://docs.python.org/3.0/library/wsgiref.html#wsgiref.util.request_uri And just make my own server ? From ross.jett at gmail.com Sat May 2 22:13:51 2009 From: ross.jett at gmail.com (Ross) Date: Sat, 2 May 2009 19:13:51 -0700 (PDT) Subject: yet another list comprehension question Message-ID: <594765c1-e5ed-492d-b398-a8ad00c6d6ad@x6g2000vbg.googlegroups.com> I'm trying to set up a simple filter using a list comprehension. If I have a list of tuples, a = [(1,2), (3,4), (5,None), (6,7), (8, None)] and I wanted to filter out all tuples containing None, I would like to get the new list b = [(1,2), (3,4),(6,7)]. I tried b = [i for i in a if t for t in i is not None] but I get the error that "t is not defined". What am I doing wrong? From clp2 at rebertia.com Sat May 2 22:21:47 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Sat, 2 May 2009 19:21:47 -0700 Subject: yet another list comprehension question In-Reply-To: <594765c1-e5ed-492d-b398-a8ad00c6d6ad@x6g2000vbg.googlegroups.com> References: <594765c1-e5ed-492d-b398-a8ad00c6d6ad@x6g2000vbg.googlegroups.com> Message-ID: <50697b2c0905021921u4189e2c0n205bdc6a43f1b18d@mail.gmail.com> On Sat, May 2, 2009 at 7:13 PM, Ross wrote: > I'm trying to set up a simple filter using a list comprehension. If I > have a list of tuples, a = [(1,2), (3,4), (5,None), (6,7), (8, None)] > and I wanted to filter out all tuples containing None, I would like to > get the new list b = [(1,2), (3,4),(6,7)]. b = [tup for tup in a if None not in tup] Cheers, Chris -- http://blog.rebertia.com From ross.jett at gmail.com Sat May 2 22:23:38 2009 From: ross.jett at gmail.com (Ross) Date: Sat, 2 May 2009 19:23:38 -0700 (PDT) Subject: yet another list comprehension question References: <594765c1-e5ed-492d-b398-a8ad00c6d6ad@x6g2000vbg.googlegroups.com> Message-ID: <126c59d6-4f6a-4d88-a4a3-d6d8ee03f353@h23g2000vbc.googlegroups.com> On May 2, 7:21?pm, Chris Rebert wrote: > On Sat, May 2, 2009 at 7:13 PM, Ross wrote: > > I'm trying to set up a simple filter using a list comprehension. If I > > have a list of tuples, a = [(1,2), (3,4), (5,None), (6,7), (8, None)] > > and I wanted to filter out all tuples containing None, I would like to > > get the new list b = [(1,2), (3,4),(6,7)]. > > b = [tup for tup in a if None not in tup] > > Cheers, > Chris > --http://blog.rebertia.com Thanks I feel retarded sometimes. From debatem1 at gmail.com Sat May 2 22:25:03 2009 From: debatem1 at gmail.com (CTO) Date: Sat, 2 May 2009 19:25:03 -0700 (PDT) Subject: yet another list comprehension question References: <594765c1-e5ed-492d-b398-a8ad00c6d6ad@x6g2000vbg.googlegroups.com> Message-ID: <086ef9ec-da1f-4275-9d48-a62c3a7122af@s1g2000prd.googlegroups.com> On May 2, 10:13?pm, Ross wrote: > I'm trying to set up a simple filter using a list comprehension. If I > have a list of tuples, a = [(1,2), (3,4), (5,None), (6,7), (8, None)] > and I wanted to filter out all tuples containing None, I would like to > get the new list b = [(1,2), (3,4),(6,7)]. try this: b = [i for i in a if None not in i] > I tried b = [i for i in a if t for t in i is not None] ? but I get the > error that "t is not defined". What am I doing wrong? You've got a "for" and an "if" backwards. t isn't defined when the if tries to evaluate it. From debatem1 at gmail.com Sat May 2 22:39:33 2009 From: debatem1 at gmail.com (CTO) Date: Sat, 2 May 2009 19:39:33 -0700 (PDT) Subject: Can someone please explain to me this code? References: Message-ID: On Sat, May 2, 2009 at 9:41 PM, Soumen banerjee wrote: Hello, I was trying to find a method to make global hotkeys with python in linux. I found this one which uses a library called python-xlib. The point is that since i dont have much experience with this, i cant understand some of the code. Can someone please explain to me how this code works? According to the author, it is meant to reduce and increase volume. Im not interested in that specifically. All i want is to be ale to bind a hotkey to a function in my python program. It's not so hard. Just a little confusing at first. The thing is that X is a client-server protocol, so it doesn't call a function or anything to let you know that the user has pressed a key- it just sends all the events you've asked for to your application, and lets it handle them how it will. To get a better idea about how that happens, fire up xev (the X Event Viewer) on your console. from Xlib.display import Display from Xlib import X You still need these def handle_event(aEvent): keycode = aEvent.detail if aEvent.type == X.KeyPress: if keycode == vol_moins: changeVolume(-2) elif keycode == vol_plus: changeVolume(+2) (Spacing mine) You don't really care about this, but the important thing is how it's structured. It takes an event, then gets its detail attribute, which is where the key that is pressed will go if it is a keypress, then sees if its type is KeyPress, then performs the appropriate action based on that keycode. Your app will do pretty much the same thing, just with different actions. def main(): # current display disp = Display() root = disp.screen().root # we tell the X server we want to catch keyPress event root.change_attributes(event_mask = X.KeyPressMask) for keycode in keys: root.grab_key(keycode, X.AnyModifier, 1,X.GrabModeAsync, X.GrabModeAsync) while 1: event = root.display.next_event() handle_event(event) if __name__ == '__main__': main() (spacing mine) While I'd do things a little differently, this is pretty much boilerplate. You can use this as a template for what you want to do, assuming that you've appropriately defined your keycodes and handle_event function. Geremy Condra From apt.shansen at gmail.com Sat May 2 23:01:55 2009 From: apt.shansen at gmail.com (Stephen Hansen) Date: Sat, 2 May 2009 20:01:55 -0700 Subject: Multiprocessing + Frozen bug? Message-ID: <7a9c25c20905022001g1cdc7440gfcbe071d007f06b4@mail.gmail.com> In the multiprocessing.forking module, there's: def get_command_line(): ... if getattr(sys, 'frozen', False): return [sys.executable, '--multiprocessing-fork'] else: prog = 'from multiprocessing.forking import main; main()' return [_python_exe, '-c', prog, '--multiprocessing-fork'] I think that the test there should be "if not getattr(sys, 'frozen', False):", because if its not frozen then you want to use the sys.executable -- but if it is, you want to use the one which has been explicitly set. I think, at least. I'm not quite sure if I want to report it as a bug yet because I'm very new to multiprocessing and am doing something very complicated (a windows service using multiprocessing to start various processes, each of which start arbitrary thirdparty-non-python subprocesses themselves) so maybe my understanding is wrong. Or iiis it not? --S -------------- next part -------------- An HTML attachment was scrubbed... URL: From darcy at druid.net Sat May 2 23:20:25 2009 From: darcy at druid.net (D'Arcy J.M. Cain) Date: Sun, 3 May 2009 03:20:25 +0000 Subject: problem with money datatype based calculations in python In-Reply-To: <1241289437.3842.6.camel@kk-laptop> References: <20090502115734.b280bf35.darcy@druid.net> <1241289437.3842.6.camel@kk-laptop> Message-ID: <20090503032025.06428836.darcy@druid.net> On Sun, 03 May 2009 00:07:17 +0530 Krishnakant wrote: > On Sat, 2009-05-02 at 11:57 -0400, D'Arcy J.M. Cain wrote: > > And if you use PyGreSQL (http://www.PyGreSQL.org/) to connect to > > the database then the money type is already converted to Decimal. > > d'arcy, I visited the home page for pygresql and discovered that you wrote the library. Sorry, I should have mentioned my bias. :-) > I am happy that you also included a non-dbapi (postgresql classic > interface ) along with the standard DBAPI module. That interface existed before there was a DB-API spec. We added the DB-API interface later and decided to support both. > I would like to know if it has been tested with postgresql 8.3 and are The current version of PyGreSQL has been tested with Python 2.5 and PostGreSQL 8.3. Older version should work as well, but you will need at least Python 2.3 and PostgreSQL 7.4. > there any known bottlenecks while using this driver module on large > scale database opperations? None that I know of. It is used on some pretty big databases that I know of. > Is this actively maintained? Yes. I'll leave the rest of the questions for someone less biased. :-) -- D'Arcy J.M. Cain | Democracy is three wolves http://www.druid.net/darcy/ | and a sheep voting on +1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner. From soumen08 at gmail.com Sat May 2 23:43:32 2009 From: soumen08 at gmail.com (Soumen banerjee) Date: Sun, 3 May 2009 09:13:32 +0530 Subject: Can someone please explain to me this code? In-Reply-To: References: Message-ID: Hello, I'd like a little more help with the following lines: root.change_attributes(event_mask = X.KeyPressMask) for keycode in keys: root.grab_key(keycode, X.AnyModifier, 1,X.GrabModeAsync, X.GrabModeAsync) what exactly do they do? regards Soumen From debatem1 at gmail.com Sun May 3 00:03:48 2009 From: debatem1 at gmail.com (CTO) Date: Sat, 2 May 2009 21:03:48 -0700 (PDT) Subject: Can someone please explain to me this code? References: Message-ID: > root.change_attributes(event_mask = X.KeyPressMask) This asks X to send this application keypress events > root.grab_key(keycode, X.AnyModifier, 1,X.GrabModeAsync, X.GrabModeAsync) This tells X to grab the keyboard if the given keycode is generated and any modifier is pressed, not to stop processing keyboard events, and not to stop processing pointer events. That's pretty safe, although I'm not sure if you'd need the modifier for your application. From cmpython at gmail.com Sun May 3 00:22:09 2009 From: cmpython at gmail.com (CM) Date: Sat, 2 May 2009 21:22:09 -0700 (PDT) Subject: Most Basic Question Ever - please help References: <65be3c66-5940-41ab-8a46-da424918b197@z5g2000vba.googlegroups.com> Message-ID: <7c328de7-ed20-450c-819f-7689e73f6a6c@g19g2000vbi.googlegroups.com> On May 2, 4:36?pm, seanm... at gmail.com wrote: > I am going to try posting here again with more detail to see if I can > finally get my first program to work. > > I am working on a MacBook Pro with OS X 10.4.11. I opened a new window > in IDLE to create a file. The file had only one line of code and was > saved as module1.py. I saved it to Macintosh HD. The one line of code > in the file is copied below: > > print 'Hello module world!' > > I closed the file and tried to run it in IDLE and Terminal, but I have > had no success. I'll paste my commands and the error messages below > (for IDLE, then Terminal). Any help would be very much appreciated. I > feel like the marathon just started and I've fallen flat on my face. > Thanks. > > IDLE 2.6.2>>> python module1.py > > SyntaxError: invalid syntax > > sean-m-computer:~ seanm$ python > Python 2.6.2 (r262:71600, Apr 16 2009, 09:17:39) > [GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin > Type "help", "copyright", "credits" or "license" for more information.>>> python module1.py > > ? File "", line 1 > ? ? python module1.py > ? ? ? ? ? ? ? ? ?^ > SyntaxError: invalid syntax Sean, also, keep in mind you can use IDLE to run your scripts. After you have saved a script/program, there is an option for Run in the menu, and then under that, Run Module. The output of the script will be sent to IDLE window indicated as the Python shell. You can also just test code directly from within that shell, though for multi-line programs, it is easier within the composing window. I suggest you sign up for the Python tutor list: http://mail.python.org/mailman/listinfo/tutor And you can search through their big archive of questions and answers here: http://www.nabble.com/Python---tutor-f2981.html The tutors there are great and super-helpful, and will field any level of question but are particularly good for absolute beginners. Here is a tutorial on using IDLE: http://hkn.eecs.berkeley.edu/~dyoo/python/idle_intro/index.html And here is a very good general programming tutorial online book, focusing on Python: http://www.freenetpages.co.uk/hp/alan.gauld/ Good luck! Che From Kevin.Smith at sas.com Sun May 3 00:36:26 2009 From: Kevin.Smith at sas.com (Kevin D. Smith) Date: Sat, 2 May 2009 23:36:26 -0500 Subject: Doc strings in descriptors Message-ID: I have a simple descriptor to create a cached property as shown below. class cproperty(object): """ Property whose value is only calculated once and cached """ def __init__(self, func): self._func = func self.__doc__ = func.__doc__ def __get__(self, obj, type=None): if obj is None: return self try: return getattr(obj, '@%s' % self._func.func_name) except AttributeError: result = self._func(obj) setattr(obj, '@%s' % self._func.func_name, result) return result The problem is that when I use the help() function on them, I don't get the doc string from the function that is being wrapped. Instead, I get the following: hasEmployees = What do I need to do to get the doc string of the wrapped function to apper when using help()? -- Kevin D. Smith From Graham.Dumpleton at gmail.com Sun May 3 00:58:21 2009 From: Graham.Dumpleton at gmail.com (Graham Dumpleton) Date: Sat, 2 May 2009 21:58:21 -0700 (PDT) Subject: eval(WsgiApplication) References: <8357032b-5fc0-4d4c-843a-a1ea27c62e5c@o14g2000vbo.googlegroups.com> <39057d64-921e-4150-a382-ec1d443a383f@j12g2000vbl.googlegroups.com> Message-ID: <8dfeb7b5-1a06-44c6-b9be-581093d89d8e@d19g2000prh.googlegroups.com> On May 2, 10:15?pm, ?????? ??????????? wrote: > >> > How do I do this in python3? > > >> What's wrong with importing it? > > > The problem is that my wsgi files have a wsgi extention for mod_wsgi > > use > .. > > mod_wsgi has a .wsgi handler because it is recommended to rename the > > wsgi file with wsgi extensions to avoid double imports > > cherrypy server has a dispatcher class > > You can either use .py extension for the wsgi files OR use a custom > importer that can import your .wsgi fileshttp://docs.python.org/library/modules.html You don't have to go to such an extreme if it is only for one file to be used as root WSGI application. Can use something like: def load_script(filename, label='__wsgi__'): module = imp.new_module(label) module.__file__ = filename execfile(filename, module.__dict__) return module module = load_script('/some/path/file.wsgi') application = module.application Graham From Graham.Dumpleton at gmail.com Sun May 3 01:33:23 2009 From: Graham.Dumpleton at gmail.com (Graham Dumpleton) Date: Sat, 2 May 2009 22:33:23 -0700 (PDT) Subject: return functions References: Message-ID: <5dde50ab-c47d-471a-a8cb-3824387f42c5@x1g2000prh.googlegroups.com> On May 3, 6:44?am, gert wrote: > Aldo i like the cerrypywsgiserver very much, i do not like the tools > that go with it > so i am stuck with a configuration file that looks like this > > http://pastebin.com/m4d8184bc > > After 152 line I finally arrived to a point where i was thinkig "thats > it, this is like going to work on a uni cycle and is just plain > ridicules" > > So how can i generate some functions something like > > def generate(url,type): # where type could be htm js css orwsgi You seem to have finally discovered that when using Apache/mod_wsgi, Apache does a level of URL matching to filesystem based resources. This isn't automatic in normal WSGI servers unless you use a WSGI middleware that does the mapping for you. :-) Graham From stefan_ml at behnel.de Sun May 3 02:25:33 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sun, 03 May 2009 08:25:33 +0200 Subject: eric not working on ubuntu 9.04 In-Reply-To: References: Message-ID: <49fd38dd$0$31333$9b4e6d93@newsspool4.arcor-online.net> bvidinli wrote: > An unhandled exception occurred. Please report the problem > using the error reporting dialog or via email to . > A log has been written to "/home/bvidinli/.eric4/eric4_error.log". Did you try that? Stefan From jhmsmits at gmail.com Sun May 3 03:06:37 2009 From: jhmsmits at gmail.com (SanPy) Date: Sun, 3 May 2009 00:06:37 -0700 (PDT) Subject: writing consecutive data to subprocess command 'more' References: Message-ID: <427b5e4e-4754-48d1-985a-809ff2328ca0@b1g2000vbc.googlegroups.com> Hmm, it works as long as the pager command is available on the system. I created a test file to explain what I mean. You can find it here: http://gist.github.com/105880 The 'pager' command is on purpose a command that is not available on the system. It should fall back to sys.stdout in the write method. However, it does not show any output at all. Strangely enough, when a put a time.sleep(1) between the first and second printer.write statements, the second and third statement do appear. Btw, the normal pager command works fine on my system. How can I solve this, so that the fallback sys.stdout does show output? --- Sander On 2 mei, 23:00, SanPy wrote: > Thanks, that works beautifully! > > Regards, > Sander > > On 2 mei, 22:35, "Gabriel Genellina" wrote: > > > communicate writes to the child's stdin and waits for it to finish. If you ? > > want to keep writing, don't use communicate. And you'll need to keep state ? > > ?from one call to another, so use a class. Based on the code above, create ? > > a class Pager with __init__, write and close methods: > > > class Pager: > > ? def __init__(self): > > ? ?# copy the logic above > > ? ?self.proc = subprocess.Popen(...) > > ? ?self.file = self.proc.stdin > > ? ?# if something goes wrong, set self.proc=None and self.file=sys.stdout > > > ? def write(self, text): > > ? ?self.file.write(text) > > > ? def close(self): > > ? ?if self.proc: > > ? ? self.file.close() > > ? ? self.proc.wait() > > > Also, take a look at the pager function in the pydoc module (see the ? > > source) - it handles several cases. > > > -- > > Gabriel Genellina From mabdelkader at gmail.com Sun May 3 03:07:41 2009 From: mabdelkader at gmail.com (ma) Date: Sun, 3 May 2009 03:07:41 -0400 Subject: yet another list comprehension question In-Reply-To: <086ef9ec-da1f-4275-9d48-a62c3a7122af@s1g2000prd.googlegroups.com> References: <594765c1-e5ed-492d-b398-a8ad00c6d6ad@x6g2000vbg.googlegroups.com> <086ef9ec-da1f-4275-9d48-a62c3a7122af@s1g2000prd.googlegroups.com> Message-ID: <148918f0905030007l3fc7280co8bc4c46a56c7e52f@mail.gmail.com> This isn't list comprehension, but it's something to keep in mind: b = filter(lambda x: None not in x, input_list) On Sat, May 2, 2009 at 10:25 PM, CTO wrote: > On May 2, 10:13 pm, Ross wrote: > > I'm trying to set up a simple filter using a list comprehension. If I > > have a list of tuples, a = [(1,2), (3,4), (5,None), (6,7), (8, None)] > > and I wanted to filter out all tuples containing None, I would like to > > get the new list b = [(1,2), (3,4),(6,7)]. > > try this: > > b = [i for i in a if None not in i] > > > I tried b = [i for i in a if t for t in i is not None] but I get the > > error that "t is not defined". What am I doing wrong? > > You've got a "for" and an "if" backwards. t isn't defined when the if > tries to evaluate it. > > > > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at REMOVE-THIS-cybersource.com.au Sun May 3 03:13:40 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 03 May 2009 07:13:40 GMT Subject: for with decimal values? References: Message-ID: <00191e5c$0$9761$c3e8da3@news.astraweb.com> On Sat, 02 May 2009 11:01:39 -0700, bearophileHUGS wrote: > Esmail: >> Is there a Python construct to allow me to do something like this: >> ? ? for i in range(-10.5, 10.5, 0.1): > > Sometimes I use an improved version of this: > http://code.activestate.com/recipes/66472/ Care to contribute your improvements back to the recipe? Or at least here? I for one am curious to see how you do it. -- Steven From steve at REMOVE-THIS-cybersource.com.au Sun May 3 03:21:06 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 03 May 2009 07:21:06 GMT Subject: Doc strings in descriptors References: Message-ID: <00192019$0$9761$c3e8da3@news.astraweb.com> On Sat, 02 May 2009 23:36:26 -0500, Kevin D. Smith wrote: > I have a simple descriptor to create a cached property as shown below. ... > What do I need to do to get the doc string of the wrapped function to > apper when using help()? Call it on the class, not the instance: >>> class Test(object): ... def getx(self): return "spam" ... def setx(self, value): return None # and do nothing ... x = property(getx, setx, None, "This is my docstring") ... >>> t = Test() >>> t.x.__doc__ # like "spam".__doc__ 'str(object) -> string\n\nReturn a nice string representation of the object.\nIf the argument is a string, the return value is the same object.' >>> Test.x.__doc__ 'This is my docstring' -- Steven From dusan.smitran at gmail.com Sun May 3 03:25:32 2009 From: dusan.smitran at gmail.com (dusans) Date: Sun, 3 May 2009 00:25:32 -0700 (PDT) Subject: for with decimal values? References: <00191e5c$0$9761$c3e8da3@news.astraweb.com> Message-ID: <2400dc46-f1f6-4621-b478-762557effa9e@z19g2000vbz.googlegroups.com> >>> import numpy as np >>> for i in np.arange(-10.5, 10.5, 0.1): ... print i ... -10.5 -10.4 -10.3 -10.2 From debatem1 at gmail.com Sun May 3 03:34:08 2009 From: debatem1 at gmail.com (CTO) Date: Sun, 3 May 2009 00:34:08 -0700 (PDT) Subject: eric not working on ubuntu 9.04 References: Message-ID: <6cf276e7-70c8-4653-8dde-aa6cad85c538@q14g2000vbn.googlegroups.com> > ? File "/usr/lib/python2.6/email/message.py", line 790, in Message > ? ? from email.Iterators import walk Well, the module is called email.iterators (rather than email.Iterators), for starters. It looks like __all__ exports both names (which seems a little dodgy to me, but hey, y'all are the experts) but Ubuntu messes with Python a lot before it makes it to a package. I don't have a jaunty box handy, but my guess is that some heinous confluence of email.iterators.walk moving to Message.walk() in python 3, the module email.Iterators not existing anymore, and Ubuntu messing with Python has caused the whole house of cards to come crashing down. If I were you, I'd probably do the following: 1) file a ubuntu bug report 2) see if you can fire up the interpreter and see if the following works: >>> from email.Iterators import walk If not, try it with the 'i' lowercased. If that works, you've got your culprit. Otherwise, let me know and I'll take a look at it when I get back in front of my jaunty box. Geremy Condra From __peter__ at web.de Sun May 3 03:54:44 2009 From: __peter__ at web.de (Peter Otten) Date: Sun, 03 May 2009 09:54:44 +0200 Subject: eric not working on ubuntu 9.04 References: Message-ID: bvidinli wrote: > any idea ? It works on my Kubuntu 9.04, but I can provoke a similar error with $ export PYTHONPATH=`pwd` $ touch email.py $ eric As a first step I recommend that you make sure that eric doesn't accidentally import modules written by you. Peter From aleksandar27 at brisiovonet.hr Sun May 3 04:33:43 2009 From: aleksandar27 at brisiovonet.hr (alejandro) Date: Sun, 3 May 2009 10:33:43 +0200 Subject: stuck with PyOBEX References: <75opebF19fc22U1@mid.uni-berlin.de> Message-ID: Yes! > I haven't tried to run it under Windows. I just assumed that > BluetoothSocket > would have the same API on both Linux and Windows. > > Looking around, it seems that this is something we can work around: > > http://svn.navi.cx/misc/trunk/laserprop/client/BluetoothConduit.py > > I'll send you an updated version to try if you would like to test it. > > David From bruce306 at rogers.com Sun May 3 06:43:27 2009 From: bruce306 at rogers.com (Uncle Bruce) Date: Sun, 3 May 2009 03:43:27 -0700 (PDT) Subject: Use of Unicode in Python 2.5 source code literals Message-ID: <0658da84-6bcf-4689-8a5a-4b934202af08@t10g2000vbg.googlegroups.com> I'm working with Python 2.5.4 and the NLTK (Natural Language Toolkit). I'm an experienced programmer, but new to Python. This question arose when I tried to create a literal in my source code for a Unicode codepoint greater than 255. (I also posted this question in the NLTK discussion group). The Python HELP (at least for version 2.5.4) states: +++++++ Python supports writing Unicode literals in any encoding, but you have to declare the encoding being used. This is done by including a special comment as either the first or second line of the source file: #!/usr/bin/env python # -*- coding: latin-1 -*- ++++++++++++ Based on some experimenting I've done, I suspect that the support for Unicode literals in ANY encoding isn't really accurate. What seems to happen is that there must be an 8-bit mapping between the set of Unicode literals and what can be used as literals. Even when I set Options / General / Default Source Encoding to UTF-8, IDLE won't allow Unicode literals (e.g. characters copied and pasted from the Windows Character Map program) to be used, even in a quoted string, if they represent an ord value greater than 255. I noticed, in researching this question, that Marc Andre Lemburg stated, back in 2001, "Since Python source code is defined to be ASCII..." I'm writing code for linguistics (other than English), so I need access to lots more characters. Most of the time, the characters come from files, so no problem. But for some processing tasks, I simply must be able to use "real" Unicode literals in the source code. (Writing hex escape sequences in a complex regex would be a nightmare). Was this taken care of in the switch from Python 2.X to 3.X? Is there a way to use more than 255 Unicode characters in source code literals in Python 2.5.4? Also, in the Windows version of Python, how can I tell if it was compiled to support 16 bits of Unicode or 32 bits of Unicode? Bruce in Toronto From steve at REMOVE-THIS-cybersource.com.au Sun May 3 07:32:39 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 03 May 2009 11:32:39 GMT Subject: Use of Unicode in Python 2.5 source code literals References: <0658da84-6bcf-4689-8a5a-4b934202af08@t10g2000vbg.googlegroups.com> Message-ID: <00195b0e$0$9761$c3e8da3@news.astraweb.com> On Sun, 03 May 2009 03:43:27 -0700, Uncle Bruce wrote: > Based on some experimenting I've done, I suspect that the support for > Unicode literals in ANY encoding isn't really accurate. What seems to > happen is that there must be an 8-bit mapping between the set of Unicode > literals and what can be used as literals. > > Even when I set Options / General / Default Source Encoding to UTF-8, > IDLE won't allow Unicode literals (e.g. characters copied and pasted > from the Windows Character Map program) to be used, even in a quoted > string, if they represent an ord value greater than 255. When you say it "won't allow", what do you mean? That you can't paste them into the document? Does it give an error? An exception at compile time or runtime? I assume you have included the coding line at the top of the file. Make sure it says utf-8 and not latin-1. # -*- coding: uft-8 -*- This is especially important if you use a Windows text editor that puts a Unicode BOM at the start of the file. What happens if you use a different editor to insert the characters in the file, and then open it in IDLE? How are you writing the literals? As byte strings or unicode strings? E.g. # filename = nonascii.py theta = '?' # byte string, probably will lead to problems sigma = u'?' # unicode, this is the Right Way > Is there a way to use more than 255 Unicode characters in source code > literals in Python 2.5.4? It works for me in Python 2.4 and 2.5, although I'm not using IDLE. >>> import nonascii >>> nonascii.sigma >>> print nonascii.sigma ? >>> print nonascii.theta ? Perhaps it is a problem with IDLE? -- Steven From mnordhoff at mattnordhoff.com Sun May 3 07:37:35 2009 From: mnordhoff at mattnordhoff.com (Matt Nordhoff) Date: Sun, 03 May 2009 11:37:35 +0000 Subject: Use of Unicode in Python 2.5 source code literals In-Reply-To: <0658da84-6bcf-4689-8a5a-4b934202af08@t10g2000vbg.googlegroups.com> References: <0658da84-6bcf-4689-8a5a-4b934202af08@t10g2000vbg.googlegroups.com> Message-ID: <49FD81FF.1060207@mattnordhoff.com> Uncle Bruce wrote: > I'm working with Python 2.5.4 and the NLTK (Natural Language > Toolkit). I'm an experienced programmer, but new to Python. > > This question arose when I tried to create a literal in my source code > for a Unicode codepoint greater than 255. (I also posted this > question in the NLTK discussion group). > > The Python HELP (at least for version 2.5.4) states: > > +++++++ > Python supports writing Unicode literals in any encoding, but you have > to declare the encoding being used. This is done by including a > special comment as either the first or second line of the source file: > > #!/usr/bin/env python > # -*- coding: latin-1 -*- > ++++++++++++ > > Based on some experimenting I've done, I suspect that the support for > Unicode literals in ANY encoding isn't really accurate. What seems to > happen is that there must be an 8-bit mapping between the set of > Unicode literals and what can be used as literals. > > Even when I set Options / General / Default Source Encoding to UTF-8, > IDLE won't allow Unicode literals (e.g. characters copied and pasted > from the Windows Character Map program) to be used, even in a quoted > string, if they represent an ord value greater than 255. > > I noticed, in researching this question, that Marc Andre Lemburg > stated, back in 2001, "Since Python source code is defined to be > ASCII..." > > I'm writing code for linguistics (other than English), so I need > access to lots more characters. Most of the time, the characters come > from files, so no problem. But for some processing tasks, I simply > must be able to use "real" Unicode literals in the source code. > (Writing hex escape sequences in a complex regex would be a > nightmare). > > Was this taken care of in the switch from Python 2.X to 3.X? > > Is there a way to use more than 255 Unicode characters in source code > literals in Python 2.5.4? > > Also, in the Windows version of Python, how can I tell if it was > compiled to support 16 bits of Unicode or 32 bits of Unicode? > > Bruce in Toronto Works for me: --- snip --- $ cat snowman.py #!/usr/bin/env python # -*- coding: utf-8 -*- import unicodedata snowman = u'?' print len(snowman) print unicodedata.name(snowman) $ python2.6 snowman.py 1 SNOWMAN --- snip --- What did you set the encoding to in the declaration at the top of the file? The help text you quoted uses latin-1 as an example, an encoding which, of course, only supports 256 code points. Did you try utf-8 instead? The regular expression engine's Unicode support is a different question, and I do not know the answer. By the way, Python 2.x only supports using non-ASCII characters in source code in string literals. Python 3 adds support for Unicode identifiers (e.g. variable names, function argument names, etc.). -- From runarte at start.no Sun May 3 07:53:46 2009 From: runarte at start.no (Runar Tenfjord) Date: Sun, 3 May 2009 11:53:46 +0000 (UTC) Subject: ANN: pyscite Message-ID: ANN: pyscite released Download it from: http://code.google.com/p/pyscite/ What is pyscite? Pyscite is a python module for accessing the SciTE editors Director Interface on windows. Example are included with code to integrate the aspell spell checker engine with SciTE. From flamz3d at gmail.com Sun May 3 09:08:02 2009 From: flamz3d at gmail.com (flamz3d at gmail.com) Date: Sun, 3 May 2009 06:08:02 -0700 (PDT) Subject: AutoComplete in C++ Editor for Python Message-ID: Hello, I am embedding python support in my C++ application and was looking at adding "Intellisense" or "AutoComplete" support. I found a way to do it using the "dir" function, but this creates a problem. Here's why. Let's say I have the following code in my editor: import sys x = sys Now, I would like to get all attributes of the object called 'x'. I can "instrument" the code and add "print dir(x)" at the end, temporarily redirect the python output to a string and execute the code. But this is not safe: I do NOT want to execute the code while the user is typing! Is there a way to "compile" the python code and get access to the symbol table from that compiled block? Did anybody ever implement AutoComplete in a editor for Python? cheers. From mike.blonder at gmail.com Sun May 3 09:21:29 2009 From: mike.blonder at gmail.com (mikefromvt) Date: Sun, 3 May 2009 06:21:29 -0700 (PDT) Subject: Need Help with str.replace & tuples References: <90983bf0-c21d-44f5-91c1-873c484678b9@s16g2000vbp.googlegroups.com> Message-ID: <05b72ff3-305d-4c96-bfb6-51f65ab26c43@q2g2000vbr.googlegroups.com> On May 2, 6:22?pm, bearophileH... at lycos.com wrote: > mikefromvt:> I am very very unfamiliar with Python and need to update a Python > > script. ?What I need to do is to replace three variables (already > > defined in the script) within a string. ?The present script correctly > > replaces two of the three variables. ?I am unable to add a third > > variable. ?Specifically, I would like to add > > > "panoble",panoble > > to the following code: > > idxItemStr+=string.replace(string.replace > > (idxItemTplStr,"stylenumber",stylenumber),"formalname",formalname) > > This isn't going to win some contest of coding style quality, but you > may try this (for other people: I have not added any new variable name > on purpose): > > idxItemStr += idxItemTplStr.replace("stylenumber", > ? ? ? ? ? ? ? stylenumber).replace("formalname", > ? ? ? ? ? ? ? formalname).replace("panoble", panoble) > > Note that in Python names are usually written like this: > idx_item_tpl > Instead of: > idxItemTplStr > > Bye, > bearophile Thank you SO MUCH. It works perfectly. Mike From riccardo.f at email.it Sun May 3 10:05:42 2009 From: riccardo.f at email.it (Riccardo Fadiga) Date: Sun, 3 May 2009 16:05:42 +0200 Subject: New Module Proposal Message-ID: <001c01c9cbf8$3fc39500$bf4abf00$@f@email.it> Greetings, I'm a basic python coder, and I wrote for myself a module wich has many function regarding encryption/decryption with Caesar, Vigenere, RSA and many more ciphers. People told me that it would be useful that it would be included in stardard Python distribution. I'll send you a copy of the file, just in case you decide to really do that. Thanks for reading. Please write back. Riccardo -- Caselle da 1GB, trasmetti allegati fino a 3GB e in piu' IMAP, POP3 e SMTP autenticato? GRATIS solo con Email.it http://www.email.it/f Sponsor: Posta Elettronica Certificata con valore legale e notifica via SMS: scopri legal.email.it Clicca qui: http://adv.email.it/cgi-bin/foclick.cgi?mid=8977&d=3-5 -------------- next part -------------- An HTML attachment was scrubbed... URL: From bruce306 at rogers.com Sun May 3 10:26:59 2009 From: bruce306 at rogers.com (Uncle Bruce) Date: Sun, 3 May 2009 07:26:59 -0700 (PDT) Subject: Use of Unicode in Python 2.5 source code literals References: <0658da84-6bcf-4689-8a5a-4b934202af08@t10g2000vbg.googlegroups.com> Message-ID: <197f510f-2b7b-42b9-b5b1-14e5a71700cb@o14g2000vbo.googlegroups.com> On May 3, 7:37?am, Matt Nordhoff wrote: > Uncle Bruce wrote: > -- I think I've figured it out! What I was trying to do was to enter the literal strings directly into the IDLE interpreter. The IDLE interpreter will not accept high codepoints directly. However, when I put a defined function in a separate file with high codepoints, IDLE processes them just fine! display produced the expected Hex strings, and Print displayed the correct characters. Success! From steve at REMOVE-THIS-cybersource.com.au Sun May 3 10:42:04 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 03 May 2009 14:42:04 GMT Subject: Strange interaction between timeit and recursion Message-ID: <00198774$0$9761$c3e8da3@news.astraweb.com> I'm seeing a strange interaction between timeit and recursion. >>> sys.getrecursionlimit() 1000 >>> from timeit import Timer >>> setup = """def test(n=1): ... if n < 999: return test(n+1) ... return None ... """ >>> exec setup >>> test() is None True >>> >>> Timer('test()', setup).repeat(number=1) Traceback (most recent call last): File "", line 1, in File "/usr/lib/python2.5/timeit.py", line 188, in repeat t = self.timeit(number) File "/usr/lib/python2.5/timeit.py", line 161, in timeit timing = self.inner(it, self.timer) File "", line 9, in inner File "", line 4, in test File "", line 4, in test File "", line 4, in test ... File "", line 4, in test I don't understand why my recursive function hits the recursion limit inside the timeit.Timer when it works outside of it. Is there any way to see the current recursion depth at a particular moment? -- Steven From rose.0978 at gmail.com Sun May 3 10:49:49 2009 From: rose.0978 at gmail.com (rose) Date: Sun, 3 May 2009 07:49:49 -0700 (PDT) Subject: Using Help inside Python Message-ID: Hi, I have an idea of the basics of programming language in general. How to access help in python i.e. help followed by something or to get to know about some inbuilt module or method etc. how do I access help from within the IDLE using the help command. Thank You. From krmane at gmail.com Sun May 3 11:14:29 2009 From: krmane at gmail.com (Krishnakant) Date: Sun, 03 May 2009 20:44:29 +0530 Subject: problem with money datatype based calculations in python In-Reply-To: <20090503032025.06428836.darcy@druid.net> References: <20090502115734.b280bf35.darcy@druid.net> <1241289437.3842.6.camel@kk-laptop> <20090503032025.06428836.darcy@druid.net> Message-ID: <1241363669.3724.10.camel@kk-laptop> On Sun, 2009-05-03 at 03:20 +0000, D'Arcy J.M. Cain wrote: > Sorry, I should have mentioned my bias. :-) > No problems at all. It is a very good library. I get an impression that it is realy a mature module and a very powerful set of API. > > I would like to know if it has been tested with postgresql 8.3 and are > > The current version of PyGreSQL has been tested with Python 2.5 and > PostGreSQL 8.3. Older version should work as well, but you will need > at least Python 2.3 and PostgreSQL 7.4. > Ah, I am using python 2.6 and postgresql 8.3 I am really impressed with the library. I want some manual or tutorial for the same and if possible can you write me off the list with a small tutorial for things like connecting to the database, sending queries, and working with the resultset at the client site. I will be really happy if this library also supports calling a stored procedurs directly. Psycopg2 does not have such a feature and I had to create one small module which does that work. What it does is that you just give the name of the procuedure procedure as the first parameter and a list of arguements as the second parameter. That's all, you will get the result because the query for the call is created inside the module and the programmer need not worry about what the sored procedure looks like. Never the less I am expecting that pygresql has some infrastructure for making calls to the stored procedures. Please send me some tutorial off the list. happy hacking. Krishnakant. From steve at REMOVE-THIS-cybersource.com.au Sun May 3 11:15:25 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 03 May 2009 15:15:25 GMT Subject: Using Help inside Python References: Message-ID: <00198f45$0$9761$c3e8da3@news.astraweb.com> On Sun, 03 May 2009 07:49:49 -0700, rose wrote: > Hi, > I have an idea of the basics of programming language in > general. How to access help in python i.e. help followed by something or > to get to know about some inbuilt module or method etc. how do I access > help from within the IDLE using the help command. > > Thank You. At the prompt, type any of: help() help(module) help(function) help(any_object) help("keyword") # note the quotes around the keyword then hit Enter to get help about that object or keyword. -- Steven From icebergwtf at gmail.com Sun May 3 11:18:33 2009 From: icebergwtf at gmail.com (tiefeng wu) Date: Sun, 3 May 2009 23:18:33 +0800 Subject: smtplib send email by using gmail smtp server Message-ID: <4314c1f70905030818y73809e16s45e04f8edb5ce714@mail.gmail.com> Hi all I'm tring send email using smtp.gmail.com here's the code: *s = SMTP('**smtp.gmail.com* *', 25) s.set_debuglevel(1)* *s.ehlo() s.starttls() s.ehlo() s.login(mygmailaccount, mygmailpassword) s.sendmail(from_addr, to_addr_list, message) s.close()* I got the following: *send: 'ehlo [192.168.1.104]\r\n' reply: b'**250-mx.google.com* * at your service, [58.39.112.163]\r\n' reply: b'250-SIZE 35651584\r\n' reply: b'250-8BITMIME\r\n' reply: b'250-STARTTLS\r\n' reply: b'250-ENHANCEDSTATUSCODES\r\n' reply: b'250 PIPELINING\r\n' reply: retcode (250); Msg: b'**mx.google.com* * at your service, [58.39.112.163]\nSIZE 35651584\n8BITMIME\nSTARTTLS\nENHANCED STATUSCODES\nPIPELINING' send: 'STARTTLS\r\n' reply: b'220 2.0.0 Ready to start TLS\r\n' reply: retcode (220); Msg: b'2.0.0 Ready to start TLS'* After a long time waiting I got: *Traceback (most recent call last): ... File "sendmail.py", line 56, in send_mail s.starttls() File "C:\usr\bin\python30\lib\smtplib.py", line 619, in starttls self.sock = ssl.wrap_socket(self.sock, keyfile, certfile) File "C:\usr\bin\python30\lib\ssl.py", line 381, in wrap_socket suppress_ragged_eofs=suppress_ragged_eofs) File "C:\usr\bin\python30\lib\ssl.py", line 135, in __init__ raise x File "C:\usr\bin\python30\lib\ssl.py", line 131, in __init__ self.do_handshake() File "C:\usr\bin\python30\lib\ssl.py", line 327, in do_handshake self._sslobj.do_handshake() ssl.SSLError: [Errno 8] _ssl.c:486: EOF occurred in violation of protocol* After this I tried port 465, I got no debug messages like "send... reply...", and after a long time waiting the following exception occured: *Traceback (most recent call last): ...* * File "sendmail.py", line 49, in send_mail s = SMTP('smtp.gmail.com', 465) File "C:\usr\bin\python30\lib\smtplib.py", line 239, in __init__ (code, msg) = self.connect(host, port) File "C:\usr\bin\python30\lib\smtplib.py", line 296, in connect (code, msg) = self.getreply() File "C:\usr\bin\python30\lib\smtplib.py", line 342, in getreply raise SMTPServerDisconnected("Connection unexpectedly closed") smtplib.SMTPServerDisconnected: Connection unexpectedly closed* Am I doing something wrong? I'm using python30 and python31a2 under Windows XP thanks! Tiefeng Wu 2009-05-03 -------------- next part -------------- An HTML attachment was scrubbed... URL: From deets at nospam.web.de Sun May 3 11:59:41 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Sun, 03 May 2009 17:59:41 +0200 Subject: Strange interaction between timeit and recursion In-Reply-To: <00198774$0$9761$c3e8da3@news.astraweb.com> References: <00198774$0$9761$c3e8da3@news.astraweb.com> Message-ID: <765tbdF1be7kpU1@mid.uni-berlin.de> Steven D'Aprano schrieb: > I'm seeing a strange interaction between timeit and recursion. > > >>>> sys.getrecursionlimit() > 1000 >>>> from timeit import Timer >>>> setup = """def test(n=1): > ... if n < 999: return test(n+1) > ... return None > ... """ >>>> exec setup >>>> test() is None > True >>>> Timer('test()', setup).repeat(number=1) > Traceback (most recent call last): > File "", line 1, in > File "/usr/lib/python2.5/timeit.py", line 188, in repeat > t = self.timeit(number) > File "/usr/lib/python2.5/timeit.py", line 161, in timeit > timing = self.inner(it, self.timer) > File "", line 9, in inner > File "", line 4, in test > File "", line 4, in test > File "", line 4, in test > ... > File "", line 4, in test > > > > I don't understand why my recursive function hits the recursion limit > inside the timeit.Timer when it works outside of it. > > Is there any way to see the current recursion depth at a particular > moment? import inspect def rec(count=100): if not count: return print len(inspect.getouterframes(inspect.currentframe())) rec(count-1) rec() Diez From david at boddie.org.uk Sun May 3 12:40:27 2009 From: david at boddie.org.uk (David Boddie) Date: Sun, 03 May 2009 18:40:27 +0200 Subject: stuck with PyOBEX References: <75opebF19fc22U1@mid.uni-berlin.de> Message-ID: On Sunday 03 May 2009 10:33, alejandro wrote: > Yes! > >> I'll send you an updated version to try if you would like to test it. My mails to you keep getting returned, so I've put it here: http://www.boddie.org.uk/david/Projects/Python/PyOBEX/Software/PyOBEX-0.21.zip Please let me know if it works on Windows, and feel free to get in touch if you have any problems. David From subhakolkata1234 at gmail.com Sun May 3 12:51:13 2009 From: subhakolkata1234 at gmail.com (SUBHABRATA BANERJEE) Date: Sun, 3 May 2009 22:21:13 +0530 Subject: File handling problem. In-Reply-To: <49FC0864.5080309@wanadoo.fr> References: <49FC0864.5080309@wanadoo.fr> Message-ID: <97c638a50905030951w61639bc9g18f0d378382bd8@mail.gmail.com> Dear Group, I am working on a code like the following: from decimal import* #SAMPLE TEST PROGRAM FOR FILE def sample_file_test(n): #FILE FOR STORING PROBABILITY VALUES open_file=open("/python26/Newfile1.txt","r+") #OPENING OF ENGLISH CORPUS open_corp_eng=open("/python26/TOTALENGLISHCORPUS1.txt","r") #READING THE ENGLISH CORPUS corp_read=open_corp_eng.read() #CONVERTING THE CORPUS FILE IN WORDS corp_word=corp_read.split() #EXTRACTING WORDS FROM CORPUS FILE OF WORDS for word in corp_word: #COUNTING THE WORD count1=corp_word.count(word) #COUNTING TOTAL NUMBER OF WORDS count2=len(corp_word) #COUNTING PROBABILITY OF WORD count1_dec=Decimal(count1) count2_dec=Decimal(count2) getcontext().prec = 6 prob_count=count1_dec/count2_dec print prob_count string_of_prob_count=str(prob_count) file_input_val=open_file.write(string_of_prob_count) open_file.close() The problems I am getting: (i) The probability values are not being stored properly in file. (ii) ?Newfile1.txt? is storing not the series of values but an arbitrary value from series 0.00000143096 (iii) As I was testing it again it gave me another error Traceback (most recent call last): File "", line 1, in sample_file_test(1) File "C:\Python26\testprogramforfiles1.py", line 25, in sample_file_test file_input_val=open_file.write(string_of_prob_count) ValueError: I/O operation on closed file If you can kindly let me know what is the wrong I am doing here. As I took out the code pasted it in MS-Word before posting, there may be slight indentation problem. Best Regards, Subhabrata. On Sat, May 2, 2009 at 2:16 PM, Pascal Chambon wrote: > subhakolkata1234 at gmail.com a ?crit : > >> Dear Group, >> >> I am using Python2.6 and has created a file where I like to write some >> statistical values I am generating. The statistical values are >> generating in a nice way, but as I am going to write it, it is not >> taking it, the file is opening or closing properly but the values are >> not getting stored. It is picking up arbitrary values from the >> generated set of values and storing it. Is there any solution for it? >> >> Best Regards, >> SBanerjee. >> -- >> http://mail.python.org/mailman/listinfo/python-list >> >> >> >> > Hello > > Could you post excerpt of your file-handling code ? > It might be a buffering problem (although when the file closes, I think > buffers get flushed), else it's really weird... > > Regards, > pascal > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From aahz at pythoncraft.com Sun May 3 12:51:13 2009 From: aahz at pythoncraft.com (Aahz) Date: 3 May 2009 09:51:13 -0700 Subject: How to measure the memory cost in Python? References: <11aabe3b0905010103r14b05fccg7a02c6ae010cd9ad@mail.gmail.com> <75e47cc6-72bc-4941-802e-767f2187445f@d7g2000prl.googlegroups.com> <7744f434-dd43-4010-ba25-90096f59d1d9@n7g2000prc.googlegroups.com> Message-ID: In article <7744f434-dd43-4010-ba25-90096f59d1d9 at n7g2000prc.googlegroups.com>, CTO wrote: > >I will admit, I have *no idea* what that code is doing, but in looking >through the gc module documentation, I'm seeing the gc.get_objects >function. Would it be equivalent to what the OP is asking to track the >size of every element returned by that? Perhaps. gc.get_objects() only returns objects tracked by GC (i.e. containers). You would need to also check all the object references held by the containers. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "Typing is cheap. Thinking is expensive." --Roy Smith From koranthala at gmail.com Sun May 3 12:54:11 2009 From: koranthala at gmail.com (koranthala) Date: Sun, 3 May 2009 09:54:11 -0700 (PDT) Subject: Reg: MFC71.DLL Message-ID: Hi, I had written a python 2.4 program. When I made it to an executable with py2exe, it told that the mfc71.dll is not added - so I may have to ship it separately. It also showed many other dll's which are all in windows/system32 - which again it said is not added. Now, I have two questions - (1) Should I include mfc71.dll separately? - I found in the following dicsussion - http://groups.google.com/group/comp.lang.python/browse_thread/thread/96933f0b9a1a0f12/887675601f5c5c63 - that mfc71 might not be needed. Should I ship it separately? (2) The dlls in windows/system32 - do I need to ship it separately? Koran From clp2 at rebertia.com Sun May 3 13:17:22 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Sun, 3 May 2009 10:17:22 -0700 Subject: File handling problem. In-Reply-To: <97c638a50905030951w61639bc9g18f0d378382bd8@mail.gmail.com> References: <49FC0864.5080309@wanadoo.fr> <97c638a50905030951w61639bc9g18f0d378382bd8@mail.gmail.com> Message-ID: <50697b2c0905031017k10cc564dj6e575eaa150916f@mail.gmail.com> On Sun, May 3, 2009 at 9:51 AM, SUBHABRATA BANERJEE wrote: > Dear Group, > > > > I am working on a code like the following: > > > > from decimal import* > > #SAMPLE TEST PROGRAM FOR FILE > > def sample_file_test(n): > > ??? #FILE FOR STORING PROBABILITY VALUES > > ??? open_file=open("/python26/Newfile1.txt","r+") Is there a reason you must output the results to the same file the input came from? It's possible this is part of your problems. > > ??? #OPENING OF ENGLISH CORPUS > > ??? open_corp_eng=open("/python26/TOTALENGLISHCORPUS1.txt","r") > > ??? #READING THE ENGLISH CORPUS > > ??? corp_read=open_corp_eng.read() > > ??? #CONVERTING THE CORPUS FILE IN WORDS > > ??? corp_word=corp_read.split() > > ??? #EXTRACTING WORDS FROM CORPUS FILE OF WORDS > > ??? for word in corp_word: > > ??????? #COUNTING THE WORD > > ??????? count1=corp_word.count(word) Note: Your program is currently O(N^2) rather than O(N) because you re-count the number of occurrences of each word /on every occurrence of the word/. > ??????? #COUNTING TOTAL NUMBER OF WORDS > > ??????? count2=len(corp_word) > > ??????? #COUNTING PROBABILITY OF WORD > > ??????? count1_dec=Decimal(count1) > > ??????? count2_dec=Decimal(count2) > > ??????? getcontext().prec = 6 > > ??????? prob_count=count1_dec/count2_dec > > ??????? print prob_count > > ??????? string_of_prob_count=str(prob_count) > > ??????? file_input_val=open_file.write(string_of_prob_count) > > ??????? open_file.close() You shouldn't be closing the file until the /entire loop/ has finished writing to the file. So the previous line should be dedented. > > > > The problems I am getting: > > (i)????????????????? The probability values are not being stored properly in > file. Also, you're currently not putting any separator between consecutive entires, so it's all going to run together as one long line. Have you considered using one of the std lib modules to output the file in a well-defined human-readable format such as JSON or CSV? > (ii)??????????????? ?Newfile1.txt? is storing not the series of values but > an arbitrary value from series 0.00000143096 > > (iii)?????????????? As I was testing it again it gave me another error > > Traceback (most recent call last): > > ? File "", line 1, in > > ??? sample_file_test(1) > > ? File "C:\Python26\testprogramforfiles1.py", line 25, in sample_file_test > > ??? file_input_val=open_file.write(string_of_prob_count) > > ValueError: I/O operation on closed file Cheers, Chris -- http://blog.rebertia.com From debatem1 at gmail.com Sun May 3 13:29:36 2009 From: debatem1 at gmail.com (CTO) Date: Sun, 3 May 2009 10:29:36 -0700 (PDT) Subject: How to measure the memory cost in Python? References: <11aabe3b0905010103r14b05fccg7a02c6ae010cd9ad@mail.gmail.com> <75e47cc6-72bc-4941-802e-767f2187445f@d7g2000prl.googlegroups.com> <7744f434-dd43-4010-ba25-90096f59d1d9@n7g2000prc.googlegroups.com> Message-ID: Alright, it's pretty obvious that I have a lot to learn before I'll be able to intelligently address this problem, but if somebody could point me at something that would help me figure out the terminology at least I'd really appreciate it. From what you're saying, it sounds like a combination of the above approaches would do what I'm asking- ie, get all the containers, then get the contents of each container- but I don't see why that would work unless gc tracks some kind of global container for small values, which, as I (poorly) understand it, are tracked separately? Thanks again, Geremy Condra From subhakolkata1234 at gmail.com Sun May 3 13:33:52 2009 From: subhakolkata1234 at gmail.com (SUBHABRATA BANERJEE) Date: Sun, 3 May 2009 23:03:52 +0530 Subject: File handling problem. In-Reply-To: <50697b2c0905031017k10cc564dj6e575eaa150916f@mail.gmail.com> References: <49FC0864.5080309@wanadoo.fr> <97c638a50905030951w61639bc9g18f0d378382bd8@mail.gmail.com> <50697b2c0905031017k10cc564dj6e575eaa150916f@mail.gmail.com> Message-ID: <97c638a50905031033x5ce3b500i381b5eb98b0d114d@mail.gmail.com> Dear Sir, Thanx for your prompt reply, I would be trying to work on your suggestion and get back to you as soon as possible. Best Regards, Subhabrata. On Sun, May 3, 2009 at 10:47 PM, Chris Rebert wrote: > On Sun, May 3, 2009 at 9:51 AM, SUBHABRATA BANERJEE > wrote: > > Dear Group, > > > > > > > > I am working on a code like the following: > > > > > > > > from decimal import* > > > > #SAMPLE TEST PROGRAM FOR FILE > > > > def sample_file_test(n): > > > > #FILE FOR STORING PROBABILITY VALUES > > > > open_file=open("/python26/Newfile1.txt","r+") > > Is there a reason you must output the results to the same file the > input came from? It's possible this is part of your problems. > > > > > #OPENING OF ENGLISH CORPUS > > > > open_corp_eng=open("/python26/TOTALENGLISHCORPUS1.txt","r") > > > > #READING THE ENGLISH CORPUS > > > > corp_read=open_corp_eng.read() > > > > #CONVERTING THE CORPUS FILE IN WORDS > > > > corp_word=corp_read.split() > > > > #EXTRACTING WORDS FROM CORPUS FILE OF WORDS > > > > for word in corp_word: > > > > #COUNTING THE WORD > > > > count1=corp_word.count(word) > > Note: Your program is currently O(N^2) rather than O(N) because you > re-count the number of occurrences of each word /on every occurrence > of the word/. > > > #COUNTING TOTAL NUMBER OF WORDS > > > > count2=len(corp_word) > > > > #COUNTING PROBABILITY OF WORD > > > > count1_dec=Decimal(count1) > > > > count2_dec=Decimal(count2) > > > > getcontext().prec = 6 > > > > prob_count=count1_dec/count2_dec > > > > print prob_count > > > > string_of_prob_count=str(prob_count) > > > > file_input_val=open_file.write(string_of_prob_count) > > > > open_file.close() > > You shouldn't be closing the file until the /entire loop/ has finished > writing to the file. So the previous line should be dedented. > > > > > > > > > The problems I am getting: > > > > (i) The probability values are not being stored properly > in > > file. > > Also, you're currently not putting any separator between consecutive > entires, so it's all going to run together as one long line. > Have you considered using one of the std lib modules to output the > file in a well-defined human-readable format such as JSON or CSV? > > > (ii) ?Newfile1.txt? is storing not the series of values > but > > an arbitrary value from series 0.00000143096 > > > > (iii) As I was testing it again it gave me another error > > > > Traceback (most recent call last): > > > > File "", line 1, in > > > > sample_file_test(1) > > > > File "C:\Python26\testprogramforfiles1.py", line 25, in > sample_file_test > > > > file_input_val=open_file.write(string_of_prob_count) > > > > ValueError: I/O operation on closed file > > > Cheers, > Chris > -- > http://blog.rebertia.com > -------------- next part -------------- An HTML attachment was scrubbed... URL: From gagsl-py2 at yahoo.com.ar Sun May 3 14:08:55 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Sun, 03 May 2009 15:08:55 -0300 Subject: smtplib send email by using gmail smtp server References: <4314c1f70905030818y73809e16s45e04f8edb5ce714@mail.gmail.com> Message-ID: En Sun, 03 May 2009 12:18:33 -0300, tiefeng wu escribi?: > I'm tring send email using smtp.gmail.com > > here's the code: > > *s = SMTP('**smtp.gmail.com* *', 25) > s.set_debuglevel(1)* > *s.ehlo() > s.starttls() > s.ehlo() > s.login(mygmailaccount, mygmailpassword) > s.sendmail(from_addr, to_addr_list, message) > s.close()* I've tested with 3.0.1 on Windows XP and worked fine. Seems to be a problem in the SSL support, but that's all I could say. -- Gabriel Genellina From davea at ieee.org Sun May 3 15:14:13 2009 From: davea at ieee.org (Dave Angel) Date: Sun, 03 May 2009 15:14:13 -0400 Subject: AutoComplete in C++ Editor for Python In-Reply-To: References: Message-ID: <49FDED05.5080802@ieee.org> flamz3d at gmail.com wrote: > Hello, > I am embedding python support in my C++ application and was looking at > adding "Intellisense" or "AutoComplete" support. > > I found a way to do it using the "dir" function, but this creates a > problem. Here's why. Let's say I have the following code in my editor: > > import sys > x = sys > > > Now, I would like to get all attributes of the object called 'x'. I > can "instrument" the code and add "print dir(x)" at the end, > temporarily redirect the python output to a string and execute the > code. > > But this is not safe: I do NOT want to execute the code while the user > is typing! > > Is there a way to "compile" the python code and get access to the > symbol table from that compiled block? > > Did anybody ever implement AutoComplete in a editor for Python? > > cheers. > > Several editors for Python support auto-complete, to one extent or another. The only one I have experience with is Komodo. Komodo runs in a separate process, so it doesn't suffer from the problems of having two gui event-loops in the same process, and other similar problems. It also won't be executing code that might have side effects in the child process. The downside is that in order to do auto-complete, it has to figure it out from other clues. From the docs, and from reading, and from experiementing, I believe that it uses two approaches. One approach is a set of language files which try to describe all the symbols in the standard language and library. They have one for each release (2.4, 2.5, ...) Theoretically, you could add your own for other libraries. Second approach is to parse the code that's visible to it. That parsing is well done, and surprisingly quick, but there are lots of tricks a developer might use that can fool it. For example, wxPython lets you import just one symbol, and lots more appear magically. It's no big deal, they have code structured one way, but the interface is very different. Catch is that code completion frequently gets fooled by these. I'd point out that if you do walk the dictionaries at run time, you'll get different results when you do it with nothing running than if you do a strictly static analysis. So some things might only show up if you've stepped into the function with the magic going on. Simplest example I can come up with of something a static analysis won't spot: An instance object obj may have some number of attributes assigned the the __init__() method. But it could also have new fields added by anyone with a referent into it. There is a Python parser in module ast. Perhaps that'd be some help. I've not used it, so can't give you any specifics. DaveA From cdalten at gmail.com Sun May 3 15:41:28 2009 From: cdalten at gmail.com (grocery_stocker) Date: Sun, 3 May 2009 12:41:28 -0700 (PDT) Subject: Is it better to use threads or fork in the following case Message-ID: <4e76cd62-e0ba-41dc-8259-103b42730e56@x31g2000prc.googlegroups.com> Let's say there is a new zip file with updated information every 30 minutes on a remote website. Now, I wanna connect to this website every 30 minutes, download the file, extract the information, and then have the program search the file search for certain items. Would it be better to use threads to break this up? I have one thread download the data and then have another to actually process the data . Or would it be better to use fork? From deets at nospam.web.de Sun May 3 16:16:49 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Sun, 03 May 2009 22:16:49 +0200 Subject: Is it better to use threads or fork in the following case In-Reply-To: <4e76cd62-e0ba-41dc-8259-103b42730e56@x31g2000prc.googlegroups.com> References: <4e76cd62-e0ba-41dc-8259-103b42730e56@x31g2000prc.googlegroups.com> Message-ID: <766cdhF1bk2ipU1@mid.uni-berlin.de> grocery_stocker schrieb: > Let's say there is a new zip file with updated information every 30 > minutes on a remote website. Now, I wanna connect to this website > every 30 minutes, download the file, extract the information, and then > have the program search the file search for certain items. > > Would it be better to use threads to break this up? I have one thread > download the data and then have another to actually process the data . > Or would it be better to use fork? Neither. Why do you think you need concurrency at all? Diez From rose.0978 at gmail.com Sun May 3 16:21:49 2009 From: rose.0978 at gmail.com (rose) Date: Sun, 3 May 2009 13:21:49 -0700 (PDT) Subject: Using Help inside Python References: <00198f45$0$9761$c3e8da3@news.astraweb.com> Message-ID: <465fe83f-3269-4fa3-8d6a-75799254e1ba@z8g2000prd.googlegroups.com> On May 3, 8:15?pm, Steven D'Aprano wrote: > On Sun, 03 May 2009 07:49:49 -0700, rose wrote: > > Hi, > > ? ? ? ? ? ? I have an idea of the basics of programming language in > > general. How to access help in python i.e. help followed by something or > > to get to know about some inbuilt module or method etc. how do I access > > help from within the IDLE using the help command. > > > Thank You. > > At the prompt, type any of: > > help() > > help(module) > > help(function) > > help(any_object) > > help("keyword") ?# note the quotes around the keyword > > then hit Enter to get help about that object or keyword. > > -- > Steven Many Thanks to you Steven, for such a concise explanation of using the help. May I request some examples to make it a bit more explicit. From cdalten at gmail.com Sun May 3 16:29:38 2009 From: cdalten at gmail.com (grocery_stocker) Date: Sun, 3 May 2009 13:29:38 -0700 (PDT) Subject: Is it better to use threads or fork in the following case References: <4e76cd62-e0ba-41dc-8259-103b42730e56@x31g2000prc.googlegroups.com> <766cdhF1bk2ipU1@mid.uni-berlin.de> Message-ID: <5212a09e-37be-4e5f-b040-515913ccf2e8@p6g2000pre.googlegroups.com> On May 3, 1:16 pm, "Diez B. Roggisch" wrote: > grocery_stocker schrieb: > > > Let's say there is a new zip file with updated information every 30 > > minutes on a remote website. Now, I wanna connect to this website > > every 30 minutes, download the file, extract the information, and then > > have the program search the file search for certain items. > > > Would it be better to use threads to break this up? I have one thread > > download the data and then have another to actually process the data . > > Or would it be better to use fork? > > Neither. Why do you think you need concurrency at all? > Okay, here is what was going through my mind. I'm a 56k dialup modem. What happens it takes me 15 minutes to download the file? Now let's say during those 15 minutes, the program needs to parse the data in the existing file. From deets at nospam.web.de Sun May 3 16:40:27 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Sun, 03 May 2009 22:40:27 +0200 Subject: Is it better to use threads or fork in the following case In-Reply-To: <5212a09e-37be-4e5f-b040-515913ccf2e8@p6g2000pre.googlegroups.com> References: <4e76cd62-e0ba-41dc-8259-103b42730e56@x31g2000prc.googlegroups.com> <766cdhF1bk2ipU1@mid.uni-berlin.de> <5212a09e-37be-4e5f-b040-515913ccf2e8@p6g2000pre.googlegroups.com> Message-ID: <766dprF1bc1asU1@mid.uni-berlin.de> grocery_stocker schrieb: > On May 3, 1:16 pm, "Diez B. Roggisch" wrote: >> grocery_stocker schrieb: >> >>> Let's say there is a new zip file with updated information every 30 >>> minutes on a remote website. Now, I wanna connect to this website >>> every 30 minutes, download the file, extract the information, and then >>> have the program search the file search for certain items. >>> Would it be better to use threads to break this up? I have one thread >>> download the data and then have another to actually process the data . >>> Or would it be better to use fork? >> Neither. Why do you think you need concurrency at all? >> > > Okay, here is what was going through my mind. I'm a 56k dialup modem. > What happens it takes me 15 minutes to download the file? Now let's > say during those 15 minutes, the program needs to parse the data in > the existing file. Is this an exercise in asking 20 hypothetical questions? Getting concurrency right isn't trivial, so if you absolute don't need this, don't do it. Diez From zentraders at gmail.com Sun May 3 16:41:49 2009 From: zentraders at gmail.com (Zentrader) Date: Sun, 3 May 2009 13:41:49 -0700 (PDT) Subject: for with decimal values? References: Message-ID: <930f396f-a15e-4dd5-9d11-f6a074d44ffc@z16g2000prd.googlegroups.com> There is no need for a function or a generator. A for() loop is a unique case of a while loop ## for i in range(-10.5, 10.5, 0.1): ctr = -10.5 while ctr < 10.5: print ctr ctr += 0.1 From debatem1 at gmail.com Sun May 3 16:44:32 2009 From: debatem1 at gmail.com (CTO) Date: Sun, 3 May 2009 13:44:32 -0700 (PDT) Subject: Is it better to use threads or fork in the following case References: <4e76cd62-e0ba-41dc-8259-103b42730e56@x31g2000prc.googlegroups.com> <766cdhF1bk2ipU1@mid.uni-berlin.de> <5212a09e-37be-4e5f-b040-515913ccf2e8@p6g2000pre.googlegroups.com> Message-ID: <477fce8b-98b6-46c8-a839-496a17cb5032@s28g2000vbp.googlegroups.com> Probably better just to check HEAD and see if its updated within the time you're looking at before any unpack. Even on a 56k that's going to be pretty fast, and you don't risk unpacking an old file while a new version is on the way. If you still want to be able to unpack the old file if there's an update then you're probably right about needing to run it concurrently, and personally I'd just fork it for ease of use- it doesn't sound like you're trying to run 100,000 of these at the same time, and you're saving the file anyway. Geremy Condra From paul.hankin at gmail.com Sun May 3 16:45:36 2009 From: paul.hankin at gmail.com (Paul Hankin) Date: Sun, 3 May 2009 13:45:36 -0700 (PDT) Subject: Is it better to use threads or fork in the following case References: <4e76cd62-e0ba-41dc-8259-103b42730e56@x31g2000prc.googlegroups.com> <766cdhF1bk2ipU1@mid.uni-berlin.de> <5212a09e-37be-4e5f-b040-515913ccf2e8@p6g2000pre.googlegroups.com> Message-ID: On May 3, 10:29?pm, grocery_stocker wrote: > On May 3, 1:16 pm, "Diez B. Roggisch" wrote: > > > grocery_stocker schrieb: > > > > Let's say there is a new zip file with updated information every 30 > > > minutes on a remote website. Now, I wanna connect to this website > > > every 30 minutes, download the file, extract the information, and then > > > have the program search the file search for certain items. > > > > Would it be better to use threads to break this up? I have one thread > > > download the data and then have another to actually process the data . > > > Or would it be better to use fork? > > > Neither. Why do you think you need concurrency at all? > > Okay, here is what was going through my mind. I'm a 56k dialup modem. > What happens it takes me 15 minutes to download the file? Now let's > say during those 15 minutes, the program needs to parse the data in > the existing file. If your modem is going at full speed for those 15 minutes, you'll have around 6.3Mb of data. Even after decompressing, and unless the data is in some quite difficult to parse format, it'll take seconds to process. -- Paul Hankin From google at mrabarnett.plus.com Sun May 3 16:52:26 2009 From: google at mrabarnett.plus.com (MRAB) Date: Sun, 03 May 2009 21:52:26 +0100 Subject: for with decimal values? In-Reply-To: <930f396f-a15e-4dd5-9d11-f6a074d44ffc@z16g2000prd.googlegroups.com> References: <930f396f-a15e-4dd5-9d11-f6a074d44ffc@z16g2000prd.googlegroups.com> Message-ID: <49FE040A.8010600@mrabarnett.plus.com> Zentrader wrote: > There is no need for a function or a generator. A for() loop is a > unique case of a while loop > ## for i in range(-10.5, 10.5, 0.1): > ctr = -10.5 > while ctr < 10.5: > print ctr > ctr += 0.1 > Python stores floats in binary, and 0.1 can't be held exactly as a fractional binary number. Therefore it might be better to produce the values as integers divided by 10: for i in range(-105, 105): i = i / 10.0 # do stuff here using i From cdalten at gmail.com Sun May 3 16:59:11 2009 From: cdalten at gmail.com (grocery_stocker) Date: Sun, 3 May 2009 13:59:11 -0700 (PDT) Subject: Is it better to use threads or fork in the following case References: <4e76cd62-e0ba-41dc-8259-103b42730e56@x31g2000prc.googlegroups.com> <766cdhF1bk2ipU1@mid.uni-berlin.de> <5212a09e-37be-4e5f-b040-515913ccf2e8@p6g2000pre.googlegroups.com> <766dprF1bc1asU1@mid.uni-berlin.de> Message-ID: <58b1d8d5-aee0-4ae0-8310-4ae8d430dddd@d38g2000prn.googlegroups.com> On May 3, 1:40 pm, "Diez B. Roggisch" wrote: > grocery_stocker schrieb: > > > > > On May 3, 1:16 pm, "Diez B. Roggisch" wrote: > >> grocery_stocker schrieb: > > >>> Let's say there is a new zip file with updated information every 30 > >>> minutes on a remote website. Now, I wanna connect to this website > >>> every 30 minutes, download the file, extract the information, and then > >>> have the program search the file search for certain items. > >>> Would it be better to use threads to break this up? I have one thread > >>> download the data and then have another to actually process the data . > >>> Or would it be better to use fork? > >> Neither. Why do you think you need concurrency at all? > > > Okay, here is what was going through my mind. I'm a 56k dialup modem. > > What happens it takes me 15 minutes to download the file? Now let's > > say during those 15 minutes, the program needs to parse the data in > > the existing file. > > Is this an exercise in asking 20 hypothetical questions? > No. This the prelude to me writing a real life python program. From gagsl-py2 at yahoo.com.ar Sun May 3 17:38:56 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Sun, 03 May 2009 18:38:56 -0300 Subject: Is it better to use threads or fork in the following case References: <4e76cd62-e0ba-41dc-8259-103b42730e56@x31g2000prc.googlegroups.com> <766cdhF1bk2ipU1@mid.uni-berlin.de> <5212a09e-37be-4e5f-b040-515913ccf2e8@p6g2000pre.googlegroups.com> Message-ID: En Sun, 03 May 2009 17:45:36 -0300, Paul Hankin escribi?: > On May 3, 10:29?pm, grocery_stocker wrote: >> On May 3, 1:16 pm, "Diez B. Roggisch" wrote: >> > grocery_stocker schrieb: >> > > Would it be better to use threads to break this up? I have one >> thread >> > > download the data and then have another to actually process the >> data . >> > > Or would it be better to use fork? >> >> > Neither. Why do you think you need concurrency at all? >> >> Okay, here is what was going through my mind. I'm a 56k dialup modem. >> What happens it takes me 15 minutes to download the file? Now let's >> say during those 15 minutes, the program needs to parse the data in >> the existing file. > > If your modem is going at full speed for those 15 minutes, you'll have > around 6.3Mb of data. Even after decompressing, and unless the data is > in some quite difficult to parse format, it'll take seconds to > process. In addition, the zip file format stores the directory at the end of the file. So you can't process it until it's completely downloaded. Concurrency doesn't help here. -- Gabriel Genellina From debatem1 at gmail.com Sun May 3 17:47:27 2009 From: debatem1 at gmail.com (CTO) Date: Sun, 3 May 2009 14:47:27 -0700 (PDT) Subject: Is it better to use threads or fork in the following case References: <4e76cd62-e0ba-41dc-8259-103b42730e56@x31g2000prc.googlegroups.com> <766cdhF1bk2ipU1@mid.uni-berlin.de> <5212a09e-37be-4e5f-b040-515913ccf2e8@p6g2000pre.googlegroups.com> Message-ID: <4b87a84a-eef0-42cb-91f7-6ade662bdd4d@t10g2000vbg.googlegroups.com> > In addition, the zip file format stores the directory at the end of the ? > file. So you can't process it until it's completely downloaded. ? > Concurrency doesn't help here. Don't think that's relevant, if I'm understanding the OP correctly. Lets say you've downloaded the file once and you're doing whatever the app does with it. Now, while that's happening the half an hour time limit comes up. Now you want to start another download, but you also want to continue to work with the old version. Voila, concurrency. From martin.hellwig at dcuktec.org Sun May 3 18:14:19 2009 From: martin.hellwig at dcuktec.org (Martin P. Hellwig) Date: Sun, 03 May 2009 23:14:19 +0100 Subject: Is it better to use threads or fork in the following case In-Reply-To: <4e76cd62-e0ba-41dc-8259-103b42730e56@x31g2000prc.googlegroups.com> References: <4e76cd62-e0ba-41dc-8259-103b42730e56@x31g2000prc.googlegroups.com> Message-ID: grocery_stocker wrote: > Let's say there is a new zip file with updated information every 30 > minutes on a remote website. Now, I wanna connect to this website > every 30 minutes, download the file, extract the information, and then > have the program search the file search for certain items. > > Would it be better to use threads to break this up? I have one thread > download the data and then have another to actually process the data . > Or would it be better to use fork? > I concur with Diez that I don't think threading/forking will bring significant advantages for this particular case. That said, if you are thinking from a responsiveness perspective, I would definitely say threading. If you ask from a performance perspective I would need to know what OS you are running (that is is if forking is even supported) and if you have multiple CPU's and if you are actually planning on spawning that sub-process on a (possibly) a different CPU as the parent process. So the workflow would be something like this: Downloading block If block has enough data to process, spawn a new process (using multiprocessing module) and let it write the result back to x (requiring lock and release). Things to keep in mind, is the overhead of: - Multiple interpreters running on the multiple CPU's - IPC - Locking/Releasing Still less then if you would have no threading at all? About forking, this usually means that the child process starts out as an exact copy of the parent process and runs ideally mostly independent of the parent meaning that the best case would be that the child process can run fine without the presents of the parent process, is this really what you want to do? -- MPH http://blog.dcuktec.com 'If consumed, best digested with added seasoning to own preference.' From bearophileHUGS at lycos.com Sun May 3 18:39:47 2009 From: bearophileHUGS at lycos.com (bearophileHUGS at lycos.com) Date: Sun, 3 May 2009 15:39:47 -0700 (PDT) Subject: Self function Message-ID: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> Sometimes I rename recursive functions, or I duplicate&modify them, and they stop working because inside them there's one or more copy of their old name. This happens to me more than one time every year. So I have written this: from inspect import getframeinfo, currentframe def SOMEVERYUGLYNAME(n): if n <= 1: return 1 else: self_name = getframeinfo(currentframe()).function #self_name = getframeinfo(currentframe())[2] # older python # only if it's a global function #return n * globals()[self_name](n - 1) return n * eval(self_name + "(%d)" % (n - 1)) assert SOMEVERYUGLYNAME(6) == 2*3*4*5*6 Are there nicer ways to do that? I don't know. If there aren't, then a way may be added. An idea-syntax: def fact(n): return 1 if n <= 1 else n * inspect.self(n - 1) Or even a lambda, because you don't need the name anymore to call the function: fact = lambda n: 1 if n <= 1 else n * self(n - 1) (If you have two or more recursive functions that call each other this idea can't be used, but I think such situations are uncommon enough to not deserve help from the language). Bye, bearophile From rhodri at wildebst.demon.co.uk Sun May 3 19:00:42 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Mon, 04 May 2009 00:00:42 +0100 Subject: Using Help inside Python In-Reply-To: <465fe83f-3269-4fa3-8d6a-75799254e1ba@z8g2000prd.googlegroups.com> References: <00198f45$0$9761$c3e8da3@news.astraweb.com> <465fe83f-3269-4fa3-8d6a-75799254e1ba@z8g2000prd.googlegroups.com> Message-ID: On Sun, 03 May 2009 21:21:49 +0100, rose wrote: > On May 3, 8:15?pm, Steven D'Aprano cybersource.com.au> wrote: >> On Sun, 03 May 2009 07:49:49 -0700, rose wrote: >> > Hi, >> > ? ? ? ? ? ? I have an idea of the basics of programming language in >> > general. How to access help in python i.e. help followed by something >> or >> > to get to know about some inbuilt module or method etc. how do I >> access >> > help from within the IDLE using the help command. >> >> > Thank You. >> >> At the prompt, type any of: >> >> help() >> >> help(module) >> >> help(function) >> >> help(any_object) >> >> help("keyword") ?# note the quotes around the keyword >> >> then hit Enter to get help about that object or keyword. > > Many Thanks to you Steven, for such a concise explanation of using the > help. May I request some examples to make it a bit more explicit. I'll try and expand on Steven's examples a bit, but really >>> help() gives you quite a lot of the information anyway. If you want help on the "for" keyword, for example, type: >>> help("for") (As Steven said, note the quote marks.) If you want help on anything else in Python at all, be it a function, module or object, then stick that object in the brackets. Thus: >>> help(123) tells you all about Python's integers, while >>> l = [1, 2, 3] >>> help(l) tells you about lists. The only gotcha is that if you try getting help on a string object, it thinks that you're trying to look up a keyword. In other words: >>> s = "for" >>> help(s) and >>> help("for") get the same message. -- Rhodri James *-* Wildebeeste Herder to the Masses From norseman at hughes.net Sun May 3 19:13:32 2009 From: norseman at hughes.net (norseman) Date: Sun, 03 May 2009 16:13:32 -0700 Subject: Tkinter, Trouble with Message,Label widget Message-ID: <49FE251C.40103@hughes.net> Intended action: Two Tkinter windows. W1 is complex, user interacts with program from here ONLY W2 is for display only, NO user interactions I can get info to W2. But to get it to update I first have to manually kill it. Program does create both. Both terminate when programs says. I want to post 'verbal' (text) progress reports to W2 and I want it to self update at posting. (Or at least appear to do so to the user.) If a control sequence is changed, the posting is to reflect it. As long as I manually kill it, everything works fine, but I get: 1) finger cramps 2) quite uhhh... 'unhappy' at the stupidity requiring that... There has to be some way of using a Message or Label (or some) widget as a simple posting board. I have been using the python print statement (via what Windows calls a Command Window) quite nicely in Linux. Under Linux only I would be content to go that route. Unfortunately the program also has to work in MicroSoft. And Microsoft has much to much of the old dinosaur age still in it. Why the posting board? 1) It posts for visual inspection the current controls chosen. A tally, a synopsis of the current choice pattern. 2) It remains visible while the control panel vanishes to give the user the full screen to conduct the rest of the human interface with vendor's product. It reminds the user of what is to happen next. 3) On re-display of main control screen the posting again functions as in 1) above. Thus serving as a reminder of "What I did last", because it does not change until a button is pressed, as well as "What I'm about to do." This can't be the first time someone has needed this. But I have not found (probably just plain missed) it in all Tkinter references I have been able to find. I have tried: ... (overhead) a= StringVar() p="string with formatting, variables and new lines" a.set(p) ... Label(root, textvar= '%s%s...' % p[:], and a number of other things and either it fails completely, looks horrible or generates lots of horrible messages telling me it don't like me because I'm not allowed to do that. :) print '%s%s%...' % p[:] works in any "Command Window", Linux or MS The ...Label(... text= '%s%s%...' % p[:], .... works if I manually destroy the posting window to get each update. I think I need a way to generate the W2 and exit leaving it visible. Question is HOW? Or what is needed to effect the same? Then the update can destroy old one and put up new one. ANY help is appreciated. Using: Python 2.5.2, Linux Slackware 10.2 Today: 20090503 Snippets: non functional, for clarifying only Steve From steve at REMOVE-THIS-cybersource.com.au Sun May 3 19:19:34 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 03 May 2009 23:19:34 GMT Subject: Using Help inside Python References: <00198f45$0$9761$c3e8da3@news.astraweb.com> <465fe83f-3269-4fa3-8d6a-75799254e1ba@z8g2000prd.googlegroups.com> Message-ID: <001a00be$0$9761$c3e8da3@news.astraweb.com> On Sun, 03 May 2009 13:21:49 -0700, rose wrote: > Many Thanks to you Steven, for such a concise explanation of using the > help. May I request some examples to make it a bit more explicit. Suppose you want to get help about the max() function. Inside IDLE, at the command prompt, type: help(max) where means "press the ENTER key". If you want help about the min() function instead, type: help(min) If you have an object called "parrot", and you want to get help about that object, do this: help(parrot) If you just want general help about everything, type: help() and follow the instructions on screen. -- Steven From gdamjan at gmail.com Sun May 3 19:49:25 2009 From: gdamjan at gmail.com (=?UTF-8?B?0JTQsNC80ZjQsNC9INCT0LXQvtGA0LPQuNC10LLRgdC60Lg=?=) Date: Mon, 04 May 2009 01:49:25 +0200 Subject: return functions References: <5dde50ab-c47d-471a-a8cb-3824387f42c5@x1g2000prh.googlegroups.com> Message-ID: <55n1d6-4km.ln1@archaeopteryx.softver.org.mk> > You seem to have finally discovered that when using Apache/mod_wsgi, > Apache does a level of URL matching to filesystem based resources. didn't Paste include something like that ... > This isn't automatic in normal WSGI servers unless you use a WSGI > middleware that does the mapping for you. :-) finally found it http://pythonpaste.org/modules/urlparser.html -- ?????? ( http://softver.org.mk/damjan/ ) Begin...the rest is easy. From steve at REMOVE-THIS-cybersource.com.au Sun May 3 19:52:29 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 03 May 2009 23:52:29 GMT Subject: for with decimal values? References: <930f396f-a15e-4dd5-9d11-f6a074d44ffc@z16g2000prd.googlegroups.com> Message-ID: <001a0876$0$9761$c3e8da3@news.astraweb.com> On Sun, 03 May 2009 13:41:49 -0700, Zentrader wrote: > There is no need for a function or a generator. A for() loop is a > unique case of a while loop > ## for i in range(-10.5, 10.5, 0.1): > ctr = -10.5 > while ctr < 10.5: > print ctr > ctr += 0.1 To match the semantics of range(), the final value to ctr must be less than but not equal to 10.5. >>> print ctr 10.6 Your solution does not do this -- it goes one steps too many, and then misleadingly fails to print the value. This is a bug waiting to hit, any time somebody runs your loop then goes to use ctr outside the loop. -- Steven From gdamjan at gmail.com Sun May 3 19:58:19 2009 From: gdamjan at gmail.com (=?UTF-8?B?0JTQsNC80ZjQsNC9INCT0LXQvtGA0LPQuNC10LLRgdC60Lg=?=) Date: Mon, 04 May 2009 01:58:19 +0200 Subject: Doc strings in descriptors References: Message-ID: > I have a simple descriptor to create a cached property as shown below. ... > The problem is that when I use the help() function on them, I don't > get the doc string from the function that is being wrapped. ... > What do I need to do to get the doc string of the wrapped function to > apper when using help()? take a look at http://pypi.python.org/pypi/decorator and possibly http://docs.python.org/library/functools.html#functools.update_wrapper -- ?????? ( http://softver.org.mk/damjan/ ) Real men don't use backups, they post their stuff on a public ftp server and let the rest of the world make copies. -- Linus Torvalds From gagsl-py2 at yahoo.com.ar Sun May 3 20:05:33 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Sun, 03 May 2009 21:05:33 -0300 Subject: for with decimal values? References: <930f396f-a15e-4dd5-9d11-f6a074d44ffc@z16g2000prd.googlegroups.com> Message-ID: En Sun, 03 May 2009 17:41:49 -0300, Zentrader escribi?: > There is no need for a function or a generator. A for() loop is a > unique case of a while loop > ## for i in range(-10.5, 10.5, 0.1): > ctr = -10.5 > while ctr < 10.5: > print ctr > ctr += 0.1 Isn't so easy. You have representation errors and rounding errors here, and they accumulate. The last number printed should be 10.4 but I get 10.5: ... 10.3 10.4 10.5 (or more precisely, 10.499999999999959) Also, after exiting a for loop, the *last* value used is retained. In your while loop, the ctr variable contains the *next* value. -- Gabriel Genellina From emile at fenx.com Sun May 3 20:21:53 2009 From: emile at fenx.com (Emile van Sebille) Date: Sun, 03 May 2009 17:21:53 -0700 Subject: Self function In-Reply-To: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> Message-ID: On 5/3/2009 3:39 PM bearophileHUGS at lycos.com said... > Sometimes I rename recursive functions, or I duplicate&modify them, > and they stop working because inside them there's one or more copy of > their old name. > This happens to me more than one time every year. > So I have written this: > > from inspect import getframeinfo, currentframe > > def SOMEVERYUGLYNAME(n): > if n <= 1: > return 1 > else: > self_name = getframeinfo(currentframe()).function > #self_name = getframeinfo(currentframe())[2] # older python > > # only if it's a global function > #return n * globals()[self_name](n - 1) > return n * eval(self_name + "(%d)" % (n - 1)) > assert SOMEVERYUGLYNAME(6) == 2*3*4*5*6 > > Are there nicer ways to do that? I've sometimes used classes like: class SOMEVERYUGLYNAME: def __call__(self,n): if n<=1: return 1 else: return n*self.__class__()(n-1) assert SOMEVERYUGLYNAME()(6) == 2*3*4*5*6 It's probably nicer (for some definition of nice), but I wouldn't say it's nice. Emile From gagsl-py2 at yahoo.com.ar Sun May 3 20:22:21 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Sun, 03 May 2009 21:22:21 -0300 Subject: Doc strings in descriptors References: Message-ID: En Sun, 03 May 2009 20:58:19 -0300, ?????? ??????????? escribi?: >> I have a simple descriptor to create a cached property as shown below. > ... >> The problem is that when I use the help() function on them, I don't >> get the doc string from the function that is being wrapped. > ... >> What do I need to do to get the doc string of the wrapped function to >> apper when using help()? > > take a look at > http://pypi.python.org/pypi/decorator > and possibly > http://docs.python.org/library/functools.html#functools.update_wrapper That doesn't help. The descriptor *does* have its docstring copied from the function. But pydoc (the module behind the help system) doesn't handle the case - "normal" properties are handled specially, but a generic descriptor like that is not understood. (and it's not so easy to fix - pydoc does a lot of things, all intermixed: it tries to discover all methods and attributes, determine which are relevant, extract documentation from them, organize and clasify such documentation, generate HTML files/text files/present it to the user/run a webserver/invoke the pager... Too much for my taste, but I disgress...) -- Gabriel Genellina From doctoresam at gmail.com Sun May 3 20:41:47 2009 From: doctoresam at gmail.com (Deep_Feelings) Date: Sun, 3 May 2009 17:41:47 -0700 (PDT) Subject: python docs for beginner programmer? Message-ID: Starting to learn python and because i don't want to read something twice (i get bored quickly) i need a source to learn python that is as comprehensive as possible. Do you think python online docs are good starting point for me? ( i experience with other programming languages ) or should i get giant book or something ? thankx From ldo at geek-central.gen.new_zealand Sun May 3 20:44:59 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Mon, 04 May 2009 12:44:59 +1200 Subject: python docs for beginner programmer? References: Message-ID: In message , Deep_Feelings wrote: > Do you think python online docs are good starting point for me? Why not try them and see? From doctoresam at gmail.com Sun May 3 20:49:03 2009 From: doctoresam at gmail.com (Deep_Feelings) Date: Sun, 3 May 2009 17:49:03 -0700 (PDT) Subject: python docs for beginner programmer? References: Message-ID: <597f14c1-981c-4b94-a64b-a0a96f35f574@e14g2000vbe.googlegroups.com> On May 4, 3:44?am, Lawrence D'Oliveiro wrote: > In message > , > > Deep_Feelings wrote: > > Do you think python online docs are good starting point for me? > > Why not try them and see? thank you should i try them then after a month of reading i discover that they are - for example - not suitable for beginners OR should i ask here first ? :) From matt at tplus1.com Sun May 3 21:04:31 2009 From: matt at tplus1.com (Matthew Wilson) Date: Mon, 04 May 2009 01:04:31 GMT Subject: How to walk up parent directories? Message-ID: Is there already a tool in the standard library to let me walk up from a subdirectory to the top of my file system? In other words, I'm looking for something like: >>> for x in walkup('/home/matt/projects'): ... print(x) /home/matt/projects /home/matt /home / I know I could build something like this with various os.path components, but I'm hoping I don't have to. TIA Matt From ben+python at benfinney.id.au Sun May 3 21:05:14 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Mon, 04 May 2009 11:05:14 +1000 Subject: python docs for beginner programmer? References: <597f14c1-981c-4b94-a64b-a0a96f35f574@e14g2000vbe.googlegroups.com> Message-ID: <87vdohk791.fsf@benfinney.id.au> Deep_Feelings writes: > should i try them then after a month of reading i discover that they > are - for example - not suitable for beginners OR should i ask here > first ? :) You should take on the task of educating yourself. The official Python docs are an excellent resource. If you're self-admittedly someone who gets bored quickly, though, that's not a problem anyone but you can solve. -- \ ?It seems intuitively obvious to me, which means that it might | `\ be wrong.? ?Chris Torek | _o__) | Ben Finney From gokhansever at gmail.com Sun May 3 21:21:27 2009 From: gokhansever at gmail.com (=?UTF-8?Q?G=C3=B6khan_SEVER?=) Date: Sun, 3 May 2009 20:21:27 -0500 Subject: python docs for beginner programmer? In-Reply-To: References: Message-ID: <49d6b3500905031821w557a8d08m3bcb65b5ae74068a@mail.gmail.com> Hey, I am a person who gets bored too quickly, too especially reading technical materials, and if the author(s) have the habit of wondering off the subject often. So far, I like Wesley Chun's Core Python book the most as a Python beginner like you. He also has video training material on the same subject. Easy to read, very well explanations and good chosen examples... That said, after getting a few months of familiarity, I want to get into the roots of the Python. So where else could be the best choice but the actual language user guide and reference manual :) Enjoy your Python programming adventures... G?khan On Sun, May 3, 2009 at 7:41 PM, Deep_Feelings wrote: > Starting to learn python and because i don't want to read something > twice (i get bored quickly) i need a source to learn python that is as > comprehensive as possible. > > > Do you think python online docs are good starting point for me? ( i > experience with other programming languages ) or should i get giant > book or something ? > > thankx > > -- > http://mail.python.org/mailman/listinfo/python-list > From ben+python at benfinney.id.au Sun May 3 21:24:59 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Mon, 04 May 2009 11:24:59 +1000 Subject: How to walk up parent directories? References: Message-ID: <87ocu9k6c4.fsf@benfinney.id.au> Matthew Wilson writes: > Is there already a tool in the standard library to let me walk up from > a subdirectory to the top of my file system? Sounds like a pretty seldom-needed task, with an implementation simple using the existing standard library parts. > In other words, I'm looking for something like: > > >>> for x in walkup('/home/matt/projects'): > ... print(x) > /home/matt/projects > /home/matt > /home > / >>> import os.path >>> def walkup(path): ... at_top = False ... while not at_top: ... yield path ... parent_path = os.path.dirname(path) ... if parent_path == path: ... at_top = True ... else: ... path = parent_path ... >>> for dir in walkup('/home/matt/projects'): ... print dir ... /home/matt/projects /home/matt /home / > I know I could build something like this with various os.path > components, but I'm hoping I don't have to. Not every simple function belongs in the standard library :-) -- \ ?Ubi dubium, ibi libertas.? (?Where there is doubt, there is | `\ freedom.?) | _o__) | Ben Finney From kirby.urner at gmail.com Sun May 3 21:30:35 2009 From: kirby.urner at gmail.com (kirby.urner at gmail.com) Date: Sun, 3 May 2009 18:30:35 -0700 (PDT) Subject: pythonic chew toyz (continued) Message-ID: <7f96ae2f-a51c-4760-a135-38d18945b42e@d38g2000prn.googlegroups.com> On Sun, May 3, 2009 at 5:52 PM, kirby wrote: > From: "kirby.ur... at gmail.com" > Date: Jan 14, 2:18 pm > Subject: pythonic chew toyz (a column, by K. Urner) v.1 n.1 > To: comp.lang.python > > > Selena spotted me right away as "the O'Reilly spy", me agreeing she > had an exceptionally good memory, maybe we could recruit her? Our > banter traces to the first meeting on OS Bridge and how I introduced > myself [0], not yet realizing Allison Randal was in the audience (way > more an insider, as to how many Ls in Randall).[1] > Selena kindly welcomed us into her home for an OS Bridge meeting awhile back, where we tackled the daunting task of sorting through a great many interesting proposals, mine barely squeaking by (yes, helps to have a self interested party at the table, I don't deny it). Audrey was a strong advocate for Allison Randal's getting to speak, but of course that's a shoe in. I'm more the maverick, not having invented any virtual machines yet, just a lot of verbiage mostly, with source code sprinkled in (syntactic sugar we call it). Selena is a Postgres jock many of you may already know. http://www.chesnok.com/daily/ Take a look for news on the new Sun - Oracle merger (yes we know, MySQL). Audrey is with our Legion of Tech especially in the Rails community (which Kaplan-Moss said some nice things about). Together these two are anchoring OS Bridge, our new Open Source conference in Oregon, the Pacific Northwest always in the mood for some big geeky festival, with lots of fun themes, like FOSS on the Farm. http://opensourcebridge.org/sessions/240 > Last night in the Roman room (CubeSpace) I proposed she could emcee > the panel we're planning (maybe?), after the new Mayor (Sam Adams) > kicks it off with a keynote -- all in the proposal stage, looking over > her shoulder. > Sam got rather famous after I posted this or was it before I forget, but a Wild West town like ours is supposed to go overboard into various forms of burlesque, player piano optional, maybe a few head turning cross-dressers (truly, PDX is proud of its heritage, think our airport is pretty spiffy, plus Ikea, other destinations, take the Max, Go By Train, very green). > I was also there for the __metaclasses__ discussion by John Melesky, > and for work, most of that same day (this is PDX CubeSpace, a lot of > us rent office space). Jason talked about the __missing__ rib (in > dict).[2] > > As we were getting settled, they talked about OSCON, perking my > interest. Whereas some take the view we're somehow competing with San > Jose (where some of my friends work, or worked in the case of Maxtor > and IBM), my model is more Cirque du Soleil, given where "geeks" come > from (something about chickens).[3] > I haven't decided about OSCON yet, would love to go, love the show, but this retarded economy has me showing my bank every move, no suitcases of cash like before (just kidding). Seriously, that O'Reilly School of Technology guy was like "we're gonna miss the free beer" referring to sometimes generous Alaska airlines people, maybe Frontier I don't know, giving passengers an up front taste for our offerings, this being not only a FOSS capital, per CSM 2005, but a Beer Capital as well (funny how these go together hand in glove -- no tap at Free Geek though, whereas at Cubespace it's like 50% foam). Anyway, I've heard many wonderful things about San Jose and will say this first hand: the Silicon Valley is not our enemy, nor is the Silicon Hills (Austin), so there, you have it in writing. > Five OSCONs running in parallel in any given summer would make more > sense than just one, but then O'Reilly isn't quite as big as McGraw- > Hill (BYTE), yet. Anyway, Portland is happy to share the glory. OS > Bridge is yet another bulb on the breadboard (aka grid), getting > brighter by the day. > > Speaking of BYTE, I also learned last night that Eliza, our geek > therapist, is alive and well in Python, as a part of the Natural > Language Toolkit or NLTK. And she's got company. Some of you may > recall Hugh Kenner's hilarious investigation of chat bots, then very > new, in a way back issue (1980s).[4] > I tried to get Nat Bobbitt interested in this but he said "no more comms" perhaps being strategic, time will tell. I admit to not following through either, making Sun Tzu and Eliza have conversational intercourse, a great high school project, and now we have a Watcher between Pycons -- Vern Ceder, tasked with scoping out talent, Pythonistas too young for a Pycon maybe (Hyatts pretty racy, lots of alcoholic beverages), but still able to exhibit in a poster session, perhaps with a teacher as stand in, proud for the whole school (Python Teacher of the Year?) -- we'll have someone for high schools to contact (soon). Check edu-sig archives for more data. edu-sig @ Python.org web page was transitioned to our university president in residence, Dr. Crunchy (yes, an alias), should have some contact info by summer. Ramping up slowly, as Pycon is traditionally business oriented, into planning and doing things right (thx to CTE this year, also on the job for DjangoConf per this guy's sources -- make a space for September). > Hugh, by the way, was a leading 20th century James Joyce scholar and > fan of R. Buckminster Fuller (see 'The Pound Era'), Bucky a champion > for me as well (hence my rbf.py -- named after the guy, for use with > VPython, POV-Ray, x3d etc., more on edu-sig). > > I'll catch up again later, maybe from Chicago, maybe before.[5] > So yeah, Chicago was great. Too much for one Chew Toyz (the name of this column), so I'll say some more later, haven't finished watching all the Blip TVs yet. Loved 'Designing Django' by Kaplan-Moss, forwarded to __CHR__ as exemplary of professional (and fun).[0] My talk cost extra so don't expect to see the whole thing on Blip, co- presented with Holden (holdenweb.com), with "Dr. Ian" contributing a short video re Diffie-Hellman-Merkle (I mostly did RSA because that's how we do it in Portland so far (wave to Lionel [1])). Kirby Cc: http://osgarden.appspot.com/motd.html [0] http://pycon.blip.tv/file/1957605/ [1] http://worldgame.blogspot.com/2009/03/spring-equinox-2009.html > [0]http://mail.python.org/pipermail/portland/2008-November/000512.html > [1]http://radar.oreilly.com/allison/ > [2]http://mybizmo.blogspot.com/2009/01/ppug-2009113.html > [3]http://worldgame.blogspot.com/2007/06/pro-python-propaganda.html > [4]http://mathforum.org/kb/message.jspa?messageID=5256703&tstart=0 > [5]http://showmedo.com/videos/video?name=1010050&fromSeriesID=101 > > [ 2nd edition: reformatted, fixed endnotes ] > > << with thanks to verizon wireless >> From ebonak at hotmail.com Sun May 3 21:41:23 2009 From: ebonak at hotmail.com (Esmail) Date: Sun, 03 May 2009 21:41:23 -0400 Subject: for with decimal values? In-Reply-To: References: <930f396f-a15e-4dd5-9d11-f6a074d44ffc@z16g2000prd.googlegroups.com> Message-ID: Gabriel Genellina wrote: > > Isn't so easy. You have representation errors and rounding errors here, > and they accumulate. The last number printed should be 10.4 but I get 10.5: > ... > 10.3 > 10.4 > 10.5 > (or more precisely, 10.499999999999959) > > Also, after exiting a for loop, the *last* value used is retained. In > your while loop, the ctr variable contains the *next* value. All this discussion makes me wonder if it would be a good idea for Python to have this feature (batteries included and all) - it would have its uses, no? From ben+python at benfinney.id.au Sun May 3 21:58:12 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Mon, 04 May 2009 11:58:12 +1000 Subject: for with decimal values? References: <930f396f-a15e-4dd5-9d11-f6a074d44ffc@z16g2000prd.googlegroups.com> Message-ID: <87fxflk4sr.fsf@benfinney.id.au> Esmail writes: > All this discussion makes me wonder if it would be a good idea > for Python to have this feature (batteries included and all) - it > would have its uses, no? What feature are you referring to? Python already has fixed-point decimal values in the ?decimal.Decimal? type, if that's what you mean. -- \ ?I spent all my money on a FAX machine. Now I can only FAX | `\ collect.? ?Steven Wright | _o__) | Ben Finney From ross.jett at gmail.com Sun May 3 22:36:35 2009 From: ross.jett at gmail.com (Ross) Date: Sun, 3 May 2009 19:36:35 -0700 (PDT) Subject: Code works fine except... Message-ID: <8d4ec1df-dddb-469a-99a1-695152db7c04@n4g2000vba.googlegroups.com> For the past couple weeks, I've been working on an algorithm to schedule tennis leagues given court constraints and league considerations (i.e. whether it's a singles or a doubles league). Here were my requirements when I was designing this algorithm: -Each player plays against a unique opponent each week. -Similarly, in a doubles league, each player plays with a unique partner each week. -Each player gets a fair number of bye weeks (i.e. the player with the most bye weeks will have no more than one bye week than the player with the least number of bye weeks) I'm very close to arriving at my desired solution, but I have one glaring flaw. When I have an even number of players sign up for my league and there are court constraints, my current algorithm gives the first player in my league a bye week every single week. I'll post my code below and see how you guys think I should add to/ amend my code. def round_robin(players, rounds): if len(players)%2: players.insert(0, None) mid = len(players)//2 for i in range(rounds): yield zip(players[:mid], players[mid:]) players = players[0:1] + players[mid:mid+1] + players[1:mid-1] + players[mid+1:] + players[mid-1:mid] def test_round_robin(players, rounds, courts, doubles = False): players = range(players) for week in round_robin(players,rounds,courts): if doubles == True: doubles_week = len(week)/2.0 byes = doubles_week - courts if byes == 0: bye_list = [] else: bye_list = week[::int(round(1.072*(courts/byes)+1.08))] playing = [u for u in week if u not in bye_list] midd = len(playing)//2 doub_sched = zip(playing[:midd], playing[midd:]) print doub_sched, bye_list else: byes = len(week)- courts if byes == 0: bye_list = [] else: bye_list = week[::int(round(1.072*(courts/byes)+1.08))] playing = [u for u in week if u not in bye_list] print playing, bye_list From wuwei23 at gmail.com Sun May 3 22:48:20 2009 From: wuwei23 at gmail.com (alex23) Date: Sun, 3 May 2009 19:48:20 -0700 (PDT) Subject: for with decimal values? References: <930f396f-a15e-4dd5-9d11-f6a074d44ffc@z16g2000prd.googlegroups.com> Message-ID: On May 4, 11:41?am, Esmail wrote: > All this discussion makes me wonder if it would be a good idea > for Python to have this feature (batteries included and all) - it > would have its uses, no? Well, sometimes more discussion == less consensus :) But it's really easy to roll your own: from decimal import Decimal def args2dec(fn): '''*args to Decimal decorator''' float2dec = lambda f: Decimal(str(f)) def _args2dec(*args): args = map(float2dec, args) return fn(*args) return _args2dec @args2dec def drange(start, stop, step): while start < stop: yield start start += step From clp2 at rebertia.com Sun May 3 23:29:22 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Sun, 3 May 2009 20:29:22 -0700 Subject: Code works fine except... In-Reply-To: <8d4ec1df-dddb-469a-99a1-695152db7c04@n4g2000vba.googlegroups.com> References: <8d4ec1df-dddb-469a-99a1-695152db7c04@n4g2000vba.googlegroups.com> Message-ID: <50697b2c0905032029g7b0009a6ia191be312119b5d3@mail.gmail.com> On Sun, May 3, 2009 at 7:36 PM, Ross wrote: > For the past couple weeks, I've been working on an algorithm to > schedule tennis leagues given court constraints and league > considerations (i.e. whether it's a singles or a doubles league). Here > were my requirements when I was designing this algorithm: > > -Each player plays against a unique opponent each week. > -Similarly, in a doubles league, each player plays with a unique > partner each week. > -Each player gets a fair number of bye weeks (i.e. the player with the > most bye weeks will have no more than one bye week than the player > with the least number of bye weeks) > > I'm very close to arriving at my desired solution, but I have one > glaring flaw. When I have an even number of players sign up for my > league and there are court constraints, my current algorithm gives the > first player in my league a bye week every single week. I'll post my > code below and see how you guys think I should add to/ amend my code. > > def round_robin(players, rounds): > ? ?if len(players)%2: > ? ? ? ?players.insert(0, None) > ? ?mid = len(players)//2 > ? ?for i in range(rounds): > ? ? ? ?yield zip(players[:mid], players[mid:]) > ? ? ? ?players = players[0:1] + players[mid:mid+1] + players[1:mid-1] + > players[mid+1:] + players[mid-1:mid] > > > def test_round_robin(players, rounds, courts, doubles = False): > ? ?players = range(players) > ? ?for week in round_robin(players,rounds,courts): > ? ? ? ? ? ?if doubles == True: > ? ? ? ? ? ? ? ? ? ?doubles_week = len(week)/2.0 > ? ? ? ? ? ? ? ? ? ?byes = doubles_week - courts > ? ? ? ? ? ? ? ? ? ?if byes == 0: > ? ? ? ? ? ? ? ? ? ? ? ? ? ?bye_list = [] > ? ? ? ? ? ? ? ? ? ?else: > ? ? ? ? ? ? ? ? ? ? ? ? ? ?bye_list = week[::int(round(1.072*(courts/byes)+1.08))] > ? ? ? ? ? ? ? ? ? ?playing = [u for u in week if u not in bye_list] > ? ? ? ? ? ? ? ? ? ?midd = len(playing)//2 > ? ? ? ? ? ? ? ? ? ?doub_sched = zip(playing[:midd], playing[midd:]) > ? ? ? ? ? ? ? ? ? ?print doub_sched, bye_list > ? ? ? ? ? ?else: > ? ? ? ? ? ? ? ? ? ?byes = len(week)- courts > ? ? ? ? ? ? ? ? ? ?if byes == 0: > ? ? ? ? ? ? ? ? ? ? ? ? ? ?bye_list = [] > ? ? ? ? ? ? ? ? ? ?else: > ? ? ? ? ? ? ? ? ? ? ? ? ? ?bye_list = week[::int(round(1.072*(courts/byes)+1.08))] > ? ? ? ? ? ? ? ? ? ?playing = [u for u in week if u not in bye_list] > ? ? ? ? ? ? ? ? ? ?print playing, bye_list Probably not the cause of the problem, but where did the magic numbers 1.072 and 1.08 come from? Cheers, Chris -- http://blog.rebertia.com From sjmachin at lexicon.net Sun May 3 23:29:46 2009 From: sjmachin at lexicon.net (John Machin) Date: Sun, 3 May 2009 20:29:46 -0700 (PDT) Subject: Code works fine except... References: <8d4ec1df-dddb-469a-99a1-695152db7c04@n4g2000vba.googlegroups.com> Message-ID: On May 4, 12:36?pm, Ross wrote: > For the past couple weeks, I've been working on an algorithm to > schedule tennis leagues given court constraints and league > considerations (i.e. whether it's a singles or a doubles league). Here > were my requirements when I was designing this algorithm: > > -Each player plays against a unique opponent each week. > -Similarly, in a doubles league, each player plays with a unique > partner each week. > -Each player gets a fair number of bye weeks (i.e. the player with the > most bye weeks will have no more than one bye week than the player > with the least number of bye weeks) > > I'm very close to arriving at my desired solution, but I have one > glaring flaw. When I have an even number of players sign up for my > league and there are court constraints, my current algorithm gives the > first player in my league a bye week every single week. I'll post my > code below and see how you guys think I should add to/ amend my code. > > def round_robin(players, rounds): > ? ? if len(players)%2: > ? ? ? ? players.insert(0, None) > ? ? mid = len(players)//2 > ? ? for i in range(rounds): > ? ? ? ? yield zip(players[:mid], players[mid:]) > ? ? ? ? players = players[0:1] + players[mid:mid+1] + players[1:mid-1] + > players[mid+1:] + players[mid-1:mid] > > def test_round_robin(players, rounds, courts, doubles = False): > ? ? players = range(players) DON'T change the type/contents/meaning of a variable name like that. E.g. use "nthings" for a number of things and "things" for a collection of things. > ? ? for week in round_robin(players,rounds,courts): The round_robin() function has only TWO arguments. This code won't even run. When you document neither your data structures nor what your functions are intended to do, the last hope for somebody trying to make sense of your code is to give meaningful names to your variables. "week" and "doubles_week" are NOT meaningful. > ? ? ? ? ? ? if doubles == True: Bletch. s/ == True// > ? ? ? ? ? ? ? ? ? ? doubles_week = len(week)/2.0 I doubt very much that using floating point is a good idea here. > ? ? ? ? ? ? ? ? ? ? byes = doubles_week - courts > ? ? ? ? ? ? ? ? ? ? if byes == 0: > ? ? ? ? ? ? ? ? ? ? ? ? ? ? bye_list = [] > ? ? ? ? ? ? ? ? ? ? else: > ? ? ? ? ? ? ? ? ? ? ? ? ? ? bye_list = week[::int(round(1.072*(courts/byes)+1.08))] The derivation of the constants 1.072 and 1.08 is .... what? > ? ? ? ? ? ? ? ? ? ? playing = [u for u in week if u not in bye_list] > ? ? ? ? ? ? ? ? ? ? midd = len(playing)//2 > ? ? ? ? ? ? ? ? ? ? doub_sched = zip(playing[:midd], playing[midd:]) > ? ? ? ? ? ? ? ? ? ? print doub_sched, bye_list > ? ? ? ? ? ? else: > ? ? ? ? ? ? ? ? ? ? byes = len(week)- courts > ? ? ? ? ? ? ? ? ? ? if byes == 0: > ? ? ? ? ? ? ? ? ? ? ? ? ? ? bye_list = [] > ? ? ? ? ? ? ? ? ? ? else: > ? ? ? ? ? ? ? ? ? ? ? ? ? ? bye_list = week[::int(round(1.072*(courts/byes)+1.08))] > ? ? ? ? ? ? ? ? ? ? playing = [u for u in week if u not in bye_list] > ? ? ? ? ? ? ? ? ? ? print playing, bye_list From gallium.arsenide at gmail.com Sun May 3 23:32:02 2009 From: gallium.arsenide at gmail.com (John Yeung) Date: Sun, 3 May 2009 20:32:02 -0700 (PDT) Subject: Code works fine except... References: <8d4ec1df-dddb-469a-99a1-695152db7c04@n4g2000vba.googlegroups.com> Message-ID: On May 3, 10:36?pm, Ross wrote: > def round_robin(players, rounds): [snip] > > def test_round_robin(players, rounds, courts, doubles = False): > ? ? players = range(players) > ? ? for week in round_robin(players,rounds,courts): [snip] First things first: I take it the call to round_robin is only supposed to take two parameters? Or do you have a version that takes 3? John From isgrey at gmail.com Sun May 3 23:47:20 2009 From: isgrey at gmail.com (Ioannis Lalopoulos) Date: Sun, 3 May 2009 20:47:20 -0700 (PDT) Subject: Tkinter, Trouble with Message,Label widget References: Message-ID: <4f61c431-1c78-4de7-9b82-08eee09ae7d6@s20g2000vbp.googlegroups.com> I assume that you create the two windows through two different calls to Tkinter.Tk() but you cannot enter two mainloops (at least not in a normal way). If you want a second window use the Toplevel widget. Try the following, it does what you want: import Tkinter root = Tkinter.Tk() my_text = Tkinter.StringVar(root) another_window = Tkinter.Toplevel() entry = Tkinter.Entry(root, textvar=my_text) entry.pack() label = Tkinter.Label(another_window, textvar=my_text) label.pack() root.mainloop() In the above example, whatever you type in the entry widget in the root window gets reflected in the label widget which is inside the second window, the one that was created with Tkinter.Toplevel(). Hope it helps, John From research at johnohagan.com Mon May 4 00:26:15 2009 From: research at johnohagan.com (John O'Hagan) Date: Mon, 4 May 2009 04:26:15 +0000 Subject: object query assigned variable name? In-Reply-To: <200905020422.20895.research@johnohagan.com> References: <200905020422.20895.research@johnohagan.com> Message-ID: <200905040426.15958.research@johnohagan.com> On Sat, 2 May 2009, John O'Hagan wrote: > On Fri, 1 May 2009, warpcat wrote: [...] > > Given an object: > > > > class Spam(object): > > def __init__(self): > > # stuff.... > > > > I'd like it to print, when instanced, something like this: > > >>> s = Spam() > > > > I?m assigned to s! > > If you just want the names an instance has in a given namespace, you could > give your class a method like: > > class KnowNames(object): > def get_names(self, namespace): > id_str = str(hex(id(self))[:-1]) > return [i for i in namespace if id_str in str(namespace[i])] > > which will give you a list of names when called on an instance. And which is a silly way of saying: class KnowName(object): def get_names(self, namespace): return [i for i in namespace if namespace[i] is self] removing four function calls, an assignment and a slicing operation from a mere two lines; certainly a personal best for insanely over-wrought code! Oops. :) , John From kyrie at uh.cu Mon May 4 00:38:42 2009 From: kyrie at uh.cu (Luis Alberto Zarrabeitia Gomez) Date: Mon, 04 May 2009 00:38:42 -0400 Subject: Multiprocessing.Queue - I want to end. In-Reply-To: <20090501052226.GA6860@cskk.homeip.net> References: <20090501052226.GA6860@cskk.homeip.net> Message-ID: <1241411922.49fe715207236@mail.uh.cu> Quoting Cameron Simpson : > | And as it happens I have an IterableQueue class right here which does > | _exact_ what was just described. You're welcome to it if you like. > | Added bonus is that, as the name suggests, you can use the class as > | an iterator: > | for item in iterq: > | ... > | The producer calls iterq.close() when it's done. > > Someone asked, so code appended below. > [...] Thank you!. I tested it, and it seems to work... and having the queue be an iterable is a plus. Thank you, Cameron & MRAB! -- Luis Zarrabeitia Facultad de Matem?tica y Computaci?n, UH http://profesores.matcom.uh.cu/~kyrie -- Participe en Universidad 2010, del 8 al 12 de febrero de 2010 La Habana, Cuba http://www.universidad2010.cu From kyrie at uh.cu Mon May 4 00:56:26 2009 From: kyrie at uh.cu (Luis Alberto Zarrabeitia Gomez) Date: Mon, 04 May 2009 00:56:26 -0400 Subject: Multiprocessing.Queue - I want to end. In-Reply-To: <001201c9ca39$b9c4d140$0d00a8c0@Hendrik> References: <200904301649.10341.kyrie@uh.cu> <001201c9ca39$b9c4d140$0d00a8c0@Hendrik> Message-ID: <1241412986.49fe757a94f93@mail.uh.cu> Quoting Hendrik van Rooyen : > "Luis Zarrabeitia" wrote: > > 8< -------explanation and example of one producer, -------- > 8< -------more consumers and one queue -------------------- > > >As you can see, I'm sending one 'None' per consumer, and hoping that no > >consumer will read more than one None. While this particular implementation > > > You don't have to hope. You can write the consumers that way to guarantee > it. I did. But that solution is not very reusable (I _must_ remember that implementation detail every time) and most important, i'll have to remember it in a few months with I'm updating the code. > >ensures that, it is very fragile. Is there any way to signal the consumers? > > Signalling is not easy - you can signal a process, but I doubt if it is > possible to signal a thread in a process. > > >(or better yet, the queue itself, as it is shared by all consumers?) > >Should "close" work for this? (raise the exception when the queue is > >exhausted, not when it is closed by the producer). > > I haven't the foggiest if this will work, and it seems to me to be kind > of involved compared to passing a sentinel or sentinels. Well, that would be a vaild signal. Too bad I have to pass it by hand, instead of the queue class taking care of it for me. > I have always wondered why people do the one queue many getters thing. > > Given that the stuff you pass is homogenous in that it will require a > similar amount of effort to process, is there not a case to be made > to have as many queues as consumers, and to round robin the work? Abstraction. This problem is modeled nicely as a producer-consumer (it would be in fact a classic producer-consumer). I could take care of the scheduling myself, but there are already good scheduling algorithms written for my OS, that take into account both the available CPU and IO. A solution may not be a queue (in my case, I don't care the order in which the elements are processed, only that they are), but ideally I would just be 'yielding' results on my producer(s), and receiving them on my consumer(s), leaving the IPC mechanism to deal with how to move the data from producers to consumers (and to which consumers). > And if the stuff you pass around needs disparate effort to consume, > it seems to me that you can more easily balance the load by having > specialised consumers, instead of instances of one humungous > "I can eat anything" consumer. Not necessarily. The load may depend on the size of the data that was sent. The consumers are receiving the same kind of data, only the sizes are different (non-predictable different). Again, I could try to implement some heuristics to try and guess what processor has lower load, but I'd rather delegate that to the OS. > I also think that having a queue per consumer thread makes it easier > to replace the threads with processes and the queues with pipes or > sockets if you need to do serious scaling later. This is already multiprocess. It could be nice to extend it to multi-computers later, but the complexity is not worth it right now. > In fact I happen to believe that anything that does any work needs > one and only one input queue and nothing else, but I am peculiar > that way. Well, I also need some output. In my case, the outputs are files with the result of the processing, that can be summarized later (hence the need to 'join' the processes, to know when I can summarize them). Thank you. -- Luis Zarrabeitia Facultad de Matem?tica y Computaci?n, UH http://profesores.matcom.uh.cu/~kyrie -- Participe en Universidad 2010, del 8 al 12 de febrero de 2010 La Habana, Cuba http://www.universidad2010.cu From kyrie at uh.cu Mon May 4 01:02:36 2009 From: kyrie at uh.cu (Luis Alberto Zarrabeitia Gomez) Date: Mon, 04 May 2009 01:02:36 -0400 Subject: Multiprocessing.Queue - I want to end. In-Reply-To: References: Message-ID: <1241413356.49fe76ec17c83@mail.uh.cu> Quoting Dennis Lee Bieber : > I'm not familiar with the multiprocessing module and its queues but, > presuming it behaves similar to the threading module AND that you have > design control over the consumers (as you did in the sample) make a > minor change. > > queue.put(None) ONCE in the producer > > Then, in the consumer, after it sees the None and begins shutdown > processing, have the consumer ALSO do > > queue.put(None) > Thank you. I went with this idea, only that instead of modifying the consumer, I modified the queue itself... Well, Cameron Simpson did :D. It's working nicely now. -- Luis Zarrabeitia Facultad de Matem?tica y Computaci?n, UH http://profesores.matcom.uh.cu/~kyrie -- Participe en Universidad 2010, del 8 al 12 de febrero de 2010 La Habana, Cuba http://www.universidad2010.cu From gallium.arsenide at gmail.com Mon May 4 01:16:20 2009 From: gallium.arsenide at gmail.com (John Yeung) Date: Sun, 3 May 2009 22:16:20 -0700 (PDT) Subject: Code works fine except... References: <8d4ec1df-dddb-469a-99a1-695152db7c04@n4g2000vba.googlegroups.com> Message-ID: <7f3493ca-efcb-48cc-aa29-487bb11fcd09@p4g2000vba.googlegroups.com> On May 3, 11:29 pm, Chris Rebert wrote: > Probably not the cause of the problem, but where > did the magic numbers 1.072 and 1.08 come from? It is perhaps not the most direct cause of the problem, in the sense that the magic numbers could take various values and the problem would still be there. But the magic numbers appear to be used for "spreading out" bye selection, and that's broken. The extended slice as written will always pick the first element, since the step is guaranteed to be positive. Since the first player (or None, when there are an odd number of players) stays put in the first position during the round_robin shuffle, that player will always be selected for a bye. Further, as written, the calculated number of byes has no bearing on the actual number of byes selected. I think what I would do is adjust the shuffling algorithm in such a way that everyone moves through the various positions in the list (would it be as simple as adding a shift at the end of round_robin???). Then I could simply select the byes from one end of the list (with a regular slice instead of an extended slice). John From rylesny at gmail.com Mon May 4 01:21:31 2009 From: rylesny at gmail.com (rylesny at gmail.com) Date: Sun, 3 May 2009 22:21:31 -0700 (PDT) Subject: Multiprocessing.Queue - I want to end. References: <2759676b-952a-46d0-a766-334782794984@o18g2000yqi.googlegroups.com> Message-ID: > You may have to write the consumer loop by hand, rather than using > 'for'. ?In the same-process case, you can do this. > > producer: > sentinel= object( ) > > consumer: > while True: > ? item= queue.get( ) > ? if item is sentinel: > ? ? break > ? etc. > > Then, each consumer is guaranteed to consume no more than one > sentinel, and thus producing one sentinel per consumer will halt them > all. > > However, with multiple processes, the comparison to 'sentinel' will > fail, since each subprocess gets a copy, not the original, of the > sentinel. Rather than use object() you can create a type whose instances are equal. class Stopper(object): def __eq__(self, other): return type(other) == type(self) producer's stop(): queue.put(Stopper()) consumers main loop: for item in iter(queue.get, Stopper()): ... From icebergwtf at gmail.com Mon May 4 01:21:59 2009 From: icebergwtf at gmail.com (tiefeng wu) Date: Mon, 4 May 2009 13:21:59 +0800 Subject: smtplib send email by using gmail smtp server In-Reply-To: References: <4314c1f70905030818y73809e16s45e04f8edb5ce714@mail.gmail.com> Message-ID: <4314c1f70905032221j61ed1e3bka6222edf1d1ab1@mail.gmail.com> > > I've tested with 3.0.1 on Windows XP and worked fine. Seems to be a problem > in the SSL support, but that's all I could say. > > -- > Gabriel Genellina > thanks, I'll check SSL support on my system Tiefeng Wu 2009-05-04 -------------- next part -------------- An HTML attachment was scrubbed... URL: From ur.liam at iksmlapan.reverse Mon May 4 01:28:28 2009 From: ur.liam at iksmlapan.reverse (Napalmski) Date: Mon, 4 May 2009 06:28:28 +0100 Subject: python docs for beginner programmer? References: <597f14c1-981c-4b94-a64b-a0a96f35f574@e14g2000vbe.googlegroups.com> <87vdohk791.fsf@benfinney.id.au> Message-ID: In article <87vdohk791.fsf at benfinney.id.au>, ben+python at benfinney.id.au says... > > Deep_Feelings writes: > > > should i try them then after a month of reading i discover that they > > are - for example - not suitable for beginners OR should i ask here > > first ? :) > > You should take on the task of educating yourself. The official Python > docs are an excellent resource. If you're self-admittedly someone who > gets bored quickly, though, that's not a problem anyone but you can > solve. Been doing Python for a week or so now and I must agree that the official python docs are a fantastic resource. I'm not sure how good they would be for someone with no previous programming language experience but they seem to be pretty thourough, at least for one beginning the language. http://docs.python.org/tutorial/index.html From alex at bluebottle.net.au Mon May 4 01:46:06 2009 From: alex at bluebottle.net.au (Alex Jurkiewicz) Date: Mon, 04 May 2009 13:46:06 +0800 Subject: Threaded alternatives to smtplib? Message-ID: <49FE811E.8060400@bluebottle.net.au> Hi all, I'm writing a Python script to do a "mail merge" style email distribution. I create a few python threads and in each one I call `smtpserver = smtplib.SMTP(our.smtpserver.com)`. However, during the sending process, there seems to be only one connection open to our mail server at any one time. In other words, all these threads gain me no speed benefit at all! I seem to be running into the issue where smtplib is not fully thread-safe, as mentioned in this thread: http://mail.python.org/pipermail/python-list/2007-March/429067.html http://mail.python.org/pipermail/python-list/2007-March/429172.html Does anyone have suggestions as to a suitable workaround for this issue? I was looking at the Twisted library, which seems possible but significantly more complex. Thanks, Alex Jurkiewicz From showell30 at yahoo.com Mon May 4 02:21:10 2009 From: showell30 at yahoo.com (Steve Howell) Date: Sun, 3 May 2009 23:21:10 -0700 (PDT) Subject: Self function References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> Message-ID: <8629a726-aca4-43c4-a223-ae150944fd58@o27g2000vbd.googlegroups.com> On May 3, 5:21?pm, Emile van Sebille wrote: > On 5/3/2009 3:39 PM bearophileH... at lycos.com said... > > > > > Sometimes I rename recursive functions, or I duplicate&modify them, > > and they stop working because inside them there's one or more copy of > > their old name. > > This happens to me more than one time every year. > > So I have written this: > > > from inspect import getframeinfo, currentframe > > > def SOMEVERYUGLYNAME(n): > > ? ? if n <= 1: > > ? ? ? ? return 1 > > ? ? else: > > ? ? ? ? self_name = getframeinfo(currentframe()).function > > ? ? ? ? #self_name = getframeinfo(currentframe())[2] # older python > > > ? ? ? ? # only if it's a global function > > ? ? ? ? #return n * globals()[self_name](n - 1) > > ? ? ? ? return n * eval(self_name + "(%d)" % (n - 1)) > > assert SOMEVERYUGLYNAME(6) == 2*3*4*5*6 > > > Are there nicer ways to do that? > > I've sometimes used classes like: > > class SOMEVERYUGLYNAME: > ? ? ?def __call__(self,n): > ? ? ? ? ?if n<=1: > ? ? ? ? ? ? ?return 1 > ? ? ? ? ?else: > ? ? ? ? ? ? ?return n*self.__class__()(n-1) > > assert SOMEVERYUGLYNAME()(6) == 2*3*4*5*6 > > It's probably nicer (for some definition of nice), but I wouldn't say > it's nice. > Some of that could probably abstracted into a decorator maybe? From clp2 at rebertia.com Mon May 4 02:24:40 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Sun, 3 May 2009 23:24:40 -0700 Subject: Strange interaction between timeit and recursion In-Reply-To: References: <00198774$0$9761$c3e8da3@news.astraweb.com> Message-ID: <50697b2c0905032324m27fcaa1k90d0407ef4c74adb@mail.gmail.com> On Sun, May 3, 2009 at 11:27 PM, namekuseijin wrote: > Recursion is unpythonic. ?Do not use it. That's a tad extreme. I think the accepted practice is merely not to use recursion gratuitously. Cheers, Chris -- http://blog.rebertia.com From namekuseijin.nospam at gmail.com Mon May 4 02:26:18 2009 From: namekuseijin.nospam at gmail.com (namekuseijin) Date: Mon, 04 May 2009 03:26:18 -0300 Subject: Using Help inside Python In-Reply-To: References: Message-ID: I'm from the time when I inspected python objects themselves, say: print obj.__doc__ or dir( obj ) to know the goodies... From namekuseijin.nospam at gmail.com Mon May 4 02:27:46 2009 From: namekuseijin.nospam at gmail.com (namekuseijin) Date: Mon, 04 May 2009 03:27:46 -0300 Subject: Strange interaction between timeit and recursion In-Reply-To: <00198774$0$9761$c3e8da3@news.astraweb.com> References: <00198774$0$9761$c3e8da3@news.astraweb.com> Message-ID: Recursion is unpythonic. Do not use it. From arnodel at googlemail.com Mon May 4 02:29:29 2009 From: arnodel at googlemail.com (Arnaud Delobelle) Date: Mon, 04 May 2009 07:29:29 +0100 Subject: Self function References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> Message-ID: bearophileHUGS at lycos.com writes: > Sometimes I rename recursive functions, or I duplicate&modify them, > and they stop working because inside them there's one or more copy of > their old name. > This happens to me more than one time every year. > So I have written this: > > from inspect import getframeinfo, currentframe > > def SOMEVERYUGLYNAME(n): > if n <= 1: > return 1 > else: > self_name = getframeinfo(currentframe()).function > #self_name = getframeinfo(currentframe())[2] # older python > > # only if it's a global function > #return n * globals()[self_name](n - 1) > return n * eval(self_name + "(%d)" % (n - 1)) > assert SOMEVERYUGLYNAME(6) == 2*3*4*5*6 > > Are there nicer ways to do that? I don't know. > If there aren't, then a way may be added. > An idea-syntax: > > def fact(n): > return 1 if n <= 1 else n * inspect.self(n - 1) > > Or even a lambda, because you don't need the name anymore to call the > function: > > fact = lambda n: 1 if n <= 1 else n * self(n - 1) > > (If you have two or more recursive functions that call each other this > idea can't be used, but I think such situations are uncommon enough to > not deserve help from the language). > > Bye, > bearophile Here's an idea: >>> def bindfunc(f): ... def boundf(*args, **kwargs): ... return f(boundf, *args, **kwargs) ... return boundf ... >>> @bindfunc ... def fac(self, n): ... return 1 if n <= 1 else n * self(n - 1) ... >>> fac(5) 120 >>> fac = bindfunc(lambda f, n: 1 if n <= 1 else n*f(n - 1)) >>> fac(5) 120 -- Arnaud From clp2 at rebertia.com Mon May 4 02:31:46 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Sun, 3 May 2009 23:31:46 -0700 Subject: Self function In-Reply-To: References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> Message-ID: <50697b2c0905032331s27af9e63t2fcc0a365d24e4ab@mail.gmail.com> On Sun, May 3, 2009 at 11:29 PM, Arnaud Delobelle wrote: > bearophileHUGS at lycos.com writes: > >> Sometimes I rename recursive functions, or I duplicate&modify them, >> and they stop working because inside them there's one or more copy of >> their old name. >> This happens to me more than one time every year. >> So I have written this: >> >> from inspect import getframeinfo, currentframe >> >> def SOMEVERYUGLYNAME(n): >> ? ? if n <= 1: >> ? ? ? ? return 1 >> ? ? else: >> ? ? ? ? self_name = getframeinfo(currentframe()).function >> ? ? ? ? #self_name = getframeinfo(currentframe())[2] # older python >> >> ? ? ? ? # only if it's a global function >> ? ? ? ? #return n * globals()[self_name](n - 1) >> ? ? ? ? return n * eval(self_name + "(%d)" % (n - 1)) >> assert SOMEVERYUGLYNAME(6) == 2*3*4*5*6 >> >> Are there nicer ways to do that? I don't know. >> If there aren't, then a way may be added. >> An idea-syntax: >> >> def fact(n): >> ? ? return 1 if n <= 1 else n * inspect.self(n - 1) >> >> Or even a lambda, because you don't need the name anymore to call the >> function: >> >> fact = lambda n: 1 if n <= 1 else n * self(n - 1) >> >> (If you have two or more recursive functions that call each other this >> idea can't be used, but I think such situations are uncommon enough to >> not deserve help from the language). >> >> Bye, >> bearophile > > Here's an idea: > >>>> def bindfunc(f): > ... ? ? def boundf(*args, **kwargs): > ... ? ? ? ? return f(boundf, *args, **kwargs) > ... ? ? return boundf > ... >>>> @bindfunc > ... def fac(self, n): > ... ? ? return 1 if n <= 1 else n * self(n - 1) > ... >>>> fac(5) > 120 >>>> fac = bindfunc(lambda f, n: 1 if n <= 1 else n*f(n - 1)) >>>> fac(5) > 120 Why am I reminded of the Y-Combinator...? Cheers, Chris -- http://blog.rebertia.com From namekuseijin.nospam at gmail.com Mon May 4 02:33:25 2009 From: namekuseijin.nospam at gmail.com (namekuseijin) Date: Mon, 04 May 2009 03:33:25 -0300 Subject: yet another list comprehension question In-Reply-To: <594765c1-e5ed-492d-b398-a8ad00c6d6ad@x6g2000vbg.googlegroups.com> References: <594765c1-e5ed-492d-b398-a8ad00c6d6ad@x6g2000vbg.googlegroups.com> Message-ID: >>> ls = [(1,2), (3,4), (5, None), (6,7), (8, None)] >>> [(x,y) for (x,y) in ls if y] [(1, 2), (3, 4), (6, 7)] From deets at nospam.web.de Mon May 4 02:45:17 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Mon, 04 May 2009 08:45:17 +0200 Subject: Threaded alternatives to smtplib? In-Reply-To: References: Message-ID: <767h7tF1b8q8gU1@mid.uni-berlin.de> Alex Jurkiewicz schrieb: > Hi all, > I'm writing a Python script to do a "mail merge" style email > distribution. I create a few python threads and in each one I call > `smtpserver = smtplib.SMTP(our.smtpserver.com)`. However, during the > sending process, there seems to be only one connection open to our mail > server at any one time. In other words, all these threads gain me no > speed benefit at all! > > I seem to be running into the issue where smtplib is not fully > thread-safe, as mentioned in this thread: > http://mail.python.org/pipermail/python-list/2007-March/429067.html > http://mail.python.org/pipermail/python-list/2007-March/429172.html > > Does anyone have suggestions as to a suitable workaround for this issue? > I was looking at the Twisted library, which seems possible but > significantly more complex. I doubt that there is such issue. The above discussion is not concluding that there actually *are* threading-issues within smtplib. And looking at the source of smtplib, it certainly doesn't use any locking-primitives or (directly) any c-extension that might explain such behavior. Without more code, it's impossible to tell if there is anything peculiar in your usage of the lib. Maybe you close your connections to fast to see several open? Diez From deets at nospam.web.de Mon May 4 02:46:39 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Mon, 04 May 2009 08:46:39 +0200 Subject: Strange interaction between timeit and recursion In-Reply-To: References: <00198774$0$9761$c3e8da3@news.astraweb.com> Message-ID: <767hafF1b8q8gU2@mid.uni-berlin.de> namekuseijin schrieb: > Recursion is unpythonic. Do not use it. Since when? Says who? Lacking tail-recursion, it's not the choice for loops, but whatever algorithm is recursive can be written as such. Diez From gagsl-py2 at yahoo.com.ar Mon May 4 03:05:35 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Mon, 04 May 2009 04:05:35 -0300 Subject: python docs for beginner programmer? References: Message-ID: En Sun, 03 May 2009 21:41:47 -0300, Deep_Feelings escribi?: > Do you think python online docs are good starting point for me? ( i > experience with other programming languages ) or should i get giant > book or something ? If you have some previous experience with other languages, I think that "Dive into Python" would be a good resource. -- Gabriel Genellina From jussij at zeusedit.com Mon May 4 03:05:51 2009 From: jussij at zeusedit.com (JussiJ) Date: Mon, 4 May 2009 00:05:51 -0700 (PDT) Subject: What IDE support python 3.0.1 ? References: <48f7aa16-ea53-4373-817d-a95fdba6e355@3g2000yqk.googlegroups.com> Message-ID: <4199fa56-4228-45bb-b400-6535bdbaaa64@y34g2000prb.googlegroups.com> On Apr 16, 1:26 pm, Brendon Wickham wrote: > I agree, no IDE needed. Just don't use Notepad! I'm on Mac, so > spoiled for choice of text editors, but I'm sure there's one > or 2 good uns if you're on Windows. The Zeus for Windows IDE is Python aware: http://www.zeusedit.com/python.html It does syntax highlighting, smart indenting etc. You can even write Zeus scripts in Python. Jussi Jumppanen Author: Zeus for Windows IDE From alex at bluebottle.net.au Mon May 4 03:19:21 2009 From: alex at bluebottle.net.au (Alex Jurkiewicz) Date: Mon, 04 May 2009 15:19:21 +0800 Subject: Threaded alternatives to smtplib? In-Reply-To: <767h7tF1b8q8gU1@mid.uni-berlin.de> References: <767h7tF1b8q8gU1@mid.uni-berlin.de> Message-ID: <49FE96F9.3000108@bluebottle.net.au> Diez B. Roggisch wrote: > Without more code, it's impossible to tell if there is anything > peculiar in your usage of the lib. Maybe you close your connections to > fast to see several open? Here's the relevant stuff from my (python2.6) code: CONCURRENCY = 3 def threadProcessRecipient(): # Each thread has its own SMTP connection smtpserver = smtplib.SMTP(SMTPSERVER) # Each thread pulls a recipient entry from the queue to process and loops until the queue is empty. try: recipientData = recipientQueue.get_nowait() except Queue.Empty: recipientData = None while recipientData: message = prepareMessage() sendMail(senderEmail, recipientEmail, message, smtpserver) recipientQueue.task_done() try: recipientData = recipientQueue.get_nowait() except Queue.Empty: recipientData = None smtpserver.quit() if __name__ == '__main__': THREADS = [] for i in range(CONCURRENCY): THREADS.append(threading.Thread(target=threadProcessRecipient)) for thread in THREADS: thread.run() for thread in THREADS: thread.join() From mail at microcorp.co.za Mon May 4 03:25:16 2009 From: mail at microcorp.co.za (Hendrik van Rooyen) Date: Mon, 4 May 2009 09:25:16 +0200 Subject: Tkinter, Trouble with Message,Label widget References: <49FE251C.40103@hughes.net> Message-ID: <006d01c9cc89$7e3295c0$0d00a8c0@Hendrik> "norseman" wrote: > There has to be some way of using a Message or Label (or some) widget as > a simple posting board. There is - look at textvariable - an instance of StringVar that is associated with the widget. If all else fails, you can always use configure to change the text... hth - Hendrik From gagsl-py2 at yahoo.com.ar Mon May 4 03:40:34 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Mon, 04 May 2009 04:40:34 -0300 Subject: Threaded alternatives to smtplib? References: <767h7tF1b8q8gU1@mid.uni-berlin.de> <49FE96F9.3000108@bluebottle.net.au> Message-ID: En Mon, 04 May 2009 04:19:21 -0300, Alex Jurkiewicz escribi?: > Diez B. Roggisch wrote: >> Without more code, it's impossible to tell if there is anything >> peculiar in your usage of the lib. Maybe you close your connections to >> fast to see several open? > > Here's the relevant stuff from my (python2.6) code: > > > CONCURRENCY = 3 > > def threadProcessRecipient(): > # Each thread has its own SMTP connection > smtpserver = smtplib.SMTP(SMTPSERVER) > # Each thread pulls a recipient entry from the queue to process > and loops until the queue is empty. > try: > recipientData = recipientQueue.get_nowait() > except Queue.Empty: > recipientData = None > while recipientData: > message = prepareMessage() > sendMail(senderEmail, recipientEmail, message, smtpserver) > recipientQueue.task_done() > try: > recipientData = recipientQueue.get_nowait() > except Queue.Empty: > recipientData = None > smtpserver.quit() Try logging the start/stop of your threads. It may be that your threads stop before you think. The above code works correctly only if you fill the queue before starting any thread - because as soon as a thread sees the queue empty, it finishes. You could use the sample code in the Queue documentation at the end of http://docs.python.org/library/queue.html -- Gabriel Genellina From hniksic at xemacs.org Mon May 4 03:53:50 2009 From: hniksic at xemacs.org (Hrvoje Niksic) Date: Mon, 04 May 2009 09:53:50 +0200 Subject: Strange interaction between timeit and recursion References: <00198774$0$9761$c3e8da3@news.astraweb.com> Message-ID: <87ws8xe229.fsf@busola.homelinux.net> Steven D'Aprano writes: > I don't understand why my recursive function hits the recursion > limit inside the timeit.Timer when it works outside of it. Probably because your test function is at the very edge of the recursion limit, and timeit.Timer triggers it because it calls it at a deeper stack level than is the case with the interactive prompt. AFAIK the "recursion limit" simply limits the number of nested function calls. > Is there any way to see the current recursion depth at a particular > moment? I don't think that's possible from within Python, although maybe it should be. The current recursion depth is a simple member of the thread state structure that could be exposed as a low-level function that begins with an underscore, similar to sys._getframe. The "inspect" module and various debuggers might put it to good use. From mail at microcorp.co.za Mon May 4 04:01:23 2009 From: mail at microcorp.co.za (Hendrik van Rooyen) Date: Mon, 4 May 2009 10:01:23 +0200 Subject: Multiprocessing.Queue - I want to end. References: <200904301649.10341.kyrie@uh.cu><001201c9ca39$b9c4d140$0d00a8c0@Hendrik> <1241412986.49fe757a94f93@mail.uh.cu> Message-ID: <00a201c9cc8e$89420f40$0d00a8c0@Hendrik> "Luis Alberto Zarrabeitia Gomez" wrote: >Quoting Hendrik van Rooyen : >> In fact I happen to believe that anything that does any work needs >> one and only one input queue and nothing else, but I am peculiar >> that way. > >Well, I also need some output. In my case, the outputs are files with the result >of the processing, that can be summarized later (hence the need to 'join' the >processes, to know when I can summarize them). When you know what you want to do with the output (which I suspect is your case) then the trick is to put the individual outputs onto the input queue of the summary process. This will form a virtual (or real if you have different machines) systolic array with producers feeding consumers that feed the summary process, all running concurrently. Ponder on it that in the description only the word "feed" is used... And if you don't know where your output must go, then it must be put onto the input queue of your default router, who must know what to do with stuff. - this is in a sense what happens when you pipe stdout to stdin in a *nix environment - you use the OS to set up a temporary systolic array. You only need to keep the output of the consumers in files if you need access to it later for some reason. In your case it sounds as if you are only interested in the output of the summary. - Hendrik From alex at bluebottle.net.au Mon May 4 04:31:46 2009 From: alex at bluebottle.net.au (Alex Jurkiewicz) Date: Mon, 04 May 2009 16:31:46 +0800 Subject: Threaded alternatives to smtplib? In-Reply-To: References: <767h7tF1b8q8gU1@mid.uni-berlin.de> <49FE96F9.3000108@bluebottle.net.au> Message-ID: <49FEA7F2.9040605@bluebottle.net.au> Gabriel Genellina wrote: > Try logging the start/stop of your threads. It may be that your > threads stop before you think. The above code works correctly only if > you fill the queue before starting any thread - because as soon as a > thread sees the queue empty, it finishes. > You could use the sample code in the Queue documentation at the end of > http://docs.python.org/library/queue.htm The queue is actually filled before the threads are started, data is only removed after they start. Logging thread state (at least in debug mode) would be good though. How would I go about it in this example? The docs suggest using thread.name, but in my current setup I don't have any idea which thread is running. Something like this: for i in range(CONCURRENCY): THREADS.append( threading.Thread(target=threadProcessRecipient, args=i, name=i) ) And: def threadProcessRecipient(name): print "Name is %s" % name AJ From deets at nospam.web.de Mon May 4 05:14:38 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Mon, 04 May 2009 11:14:38 +0200 Subject: Is it better to use threads or fork in the following case References: <4e76cd62-e0ba-41dc-8259-103b42730e56@x31g2000prc.googlegroups.com> <766cdhF1bk2ipU1@mid.uni-berlin.de> <5212a09e-37be-4e5f-b040-515913ccf2e8@p6g2000pre.googlegroups.com> <4b87a84a-eef0-42cb-91f7-6ade662bdd4d@t10g2000vbg.googlegroups.com> Message-ID: <767pt4F1ai5lsU1@mid.uni-berlin.de> CTO wrote: >> In addition, the zip file format stores the directory at the end of the >> file. So you can't process it until it's completely downloaded. >> Concurrency doesn't help here. > > Don't think that's relevant, if I'm understanding the OP correctly. > Lets say you've downloaded the file once and you're doing whatever > the app does with it. Now, while that's happening the half an hour > time limit comes up. Now you want to start another download, but > you also want to continue to work with the old version. Voila, > concurrency. Which brings us backs to the "20 questions"-part of my earlier post. It could be, but it could also be that processing takes seconds. Or it takes so long that even concurrency won't help. Who knows? Diez From bearophileHUGS at lycos.com Mon May 4 05:39:40 2009 From: bearophileHUGS at lycos.com (bearophileHUGS at lycos.com) Date: Mon, 4 May 2009 02:39:40 -0700 (PDT) Subject: Self function References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> Message-ID: Arnaud Delobelle: > >>> def bindfunc(f): > ... ? ? def boundf(*args, **kwargs): > ... ? ? ? ? return f(boundf, *args, **kwargs) > ... ? ? return boundf > ...>>> @bindfunc > ... def fac(self, n): > ... ? ? return 1 if n <= 1 else n * self(n - 1) > ...>>> fac(5) > 120 This is cute, now I have two names to take care of. Thank you to all the people that have answered. Another possible syntax: def fact(n): return 1 if n <= 1 else n * return(n - 1) But I guess most people don't see this problem as important&common enough to justify changing the language. Bye, bearophile From deets at nospam.web.de Mon May 4 06:52:05 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Mon, 04 May 2009 12:52:05 +0200 Subject: Threaded alternatives to smtplib? References: <767h7tF1b8q8gU1@mid.uni-berlin.de> <49FE96F9.3000108@bluebottle.net.au> Message-ID: <767vjrF1bier8U1@mid.uni-berlin.de> Alex Jurkiewicz wrote: > Gabriel Genellina wrote: >> Try logging the start/stop of your threads. It may be that your >> threads stop before you think. The above code works correctly only if >> you fill the queue before starting any thread - because as soon as a >> thread sees the queue empty, it finishes. >> You could use the sample code in the Queue documentation at the end of >> http://docs.python.org/library/queue.htm > The queue is actually filled before the threads are started, data is > only removed after they start. > > Logging thread state (at least in debug mode) would be good though. How > would I go about it in this example? The docs suggest using thread.name, > but in my current setup I don't have any idea which thread is running. > Something like this: > for i in range(CONCURRENCY): > THREADS.append( threading.Thread(target=threadProcessRecipient, > args=i, name=i) ) > > And: > > def threadProcessRecipient(name): > print "Name is %s" % name Each thread gets an unique name assigned anyway. Alternatively, you can use partial to bind parameters to functions. And last but not least to subclass Thread is also an option. Diez From deets at nospam.web.de Mon May 4 06:55:34 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Mon, 04 May 2009 12:55:34 +0200 Subject: Threaded alternatives to smtplib? References: <767h7tF1b8q8gU1@mid.uni-berlin.de> <49FE96F9.3000108@bluebottle.net.au> <767vjrF1bier8U1@mid.uni-berlin.de> Message-ID: <767vqcF1bd5b4U1@mid.uni-berlin.de> Diez B. Roggisch wrote: > Alex Jurkiewicz wrote: > >> Gabriel Genellina wrote: >>> Try logging the start/stop of your threads. It may be that your >>> threads stop before you think. The above code works correctly only if >>> you fill the queue before starting any thread - because as soon as a >>> thread sees the queue empty, it finishes. >>> You could use the sample code in the Queue documentation at the end of >>> http://docs.python.org/library/queue.htm >> The queue is actually filled before the threads are started, data is >> only removed after they start. >> >> Logging thread state (at least in debug mode) would be good though. How >> would I go about it in this example? The docs suggest using thread.name, >> but in my current setup I don't have any idea which thread is running. >> Something like this: >> for i in range(CONCURRENCY): >> THREADS.append( threading.Thread(target=threadProcessRecipient, >> args=i, name=i) ) >> >> And: >> >> def threadProcessRecipient(name): >> print "Name is %s" % name > Oh, and I forgot - threading.currentThread() gets you the actively running thread of course. Dize From a.a.ovtchinnikov at gmail.com Mon May 4 07:32:38 2009 From: a.a.ovtchinnikov at gmail.com (Snorri H) Date: Mon, 4 May 2009 04:32:38 -0700 (PDT) Subject: yet another list comprehension question References: <594765c1-e5ed-492d-b398-a8ad00c6d6ad@x6g2000vbg.googlegroups.com> Message-ID: On May 3, 6:13 am, Ross wrote: > I'm trying to set up a simple filter using a list comprehension. If I > have a list of tuples, a = [(1,2), (3,4), (5,None), (6,7), (8, None)] > and I wanted to filter out all tuples containing None, I would like to > get the new list b = [(1,2), (3,4),(6,7)]. > > I tried b = [i for i in a if t for t in i is not None] but I get the > error that "t is not defined". What am I doing wrong? Works as well: filter(lambda x:not None in x, your_list) From arnodel at googlemail.com Mon May 4 07:54:15 2009 From: arnodel at googlemail.com (Arnaud Delobelle) Date: Mon, 04 May 2009 12:54:15 +0100 Subject: yet another list comprehension question References: <594765c1-e5ed-492d-b398-a8ad00c6d6ad@x6g2000vbg.googlegroups.com> Message-ID: Snorri H writes: > On May 3, 6:13 am, Ross wrote: >> I'm trying to set up a simple filter using a list comprehension. If I >> have a list of tuples, a = [(1,2), (3,4), (5,None), (6,7), (8, None)] >> and I wanted to filter out all tuples containing None, I would like to >> get the new list b = [(1,2), (3,4),(6,7)]. >> >> I tried b = [i for i in a if t for t in i is not None] but I get the >> error that "t is not defined". What am I doing wrong? > > > Works as well: > filter(lambda x:not None in x, your_list) ^^^^^^^^^^^^^ This is usually spelt 'None not in x'. -- Arnaud From mgalle at irisa.fr Mon May 4 08:08:51 2009 From: mgalle at irisa.fr (=?ISO-8859-1?Q?Matthias_Gall=E9?=) Date: Mon, 04 May 2009 14:08:51 +0200 Subject: find sublist inside list Message-ID: Hi. My problem is to replace all occurrences of a sublist with a new element. Example: Given ['a','c','a','c','c','g','a','c'] I want to replace all occurrences of ['a','c'] by 6 (result [6,6,'c','g',6]). If I do this with string ('acaccgac') I have the advantage of all the 'find' functions, but perfomance is bad and some extra care must be taken if one element consist of more then one character (case of 11 for example) So I really would like to work with lists straightforward, but I could not found anything to search a sublist inside a list. Any propositions for a simple solution? Thanks in advance, -- Matthias Gall? Project Symbiose Centre de Recherche INRIA Rennes - Bretagne Atlantique, Campus de Beaulieu, 35042 Rennes cedex, France tel: (33|0) 2 9984 7523 http://www.irisa.fr/symbiose/matthias_galle From drobinow at gmail.com Mon May 4 08:15:47 2009 From: drobinow at gmail.com (David Robinow) Date: Mon, 4 May 2009 08:15:47 -0400 Subject: yet another list comprehension question In-Reply-To: References: <594765c1-e5ed-492d-b398-a8ad00c6d6ad@x6g2000vbg.googlegroups.com> Message-ID: <4eb0089f0905040515s7937dd6ck404282a8a52a105d@mail.gmail.com> On Mon, May 4, 2009 at 2:33 AM, namekuseijin wrote: >>>> ls = [(1,2), (3,4), (5, None), (6,7), (8, None)] >>>> [(x,y) for (x,y) in ls if y] > [(1, 2), (3, 4), (6, 7)] Nope. That filters out 0 as well as None. Not what the OP asked for. From a.a.ovtchinnikov at gmail.com Mon May 4 08:26:23 2009 From: a.a.ovtchinnikov at gmail.com (Snorri H) Date: Mon, 4 May 2009 05:26:23 -0700 (PDT) Subject: How to walk up parent directories? References: Message-ID: <5deab389-38f2-4c54-bfe3-3ce7cefa8764@v17g2000vbb.googlegroups.com> On May 4, 5:04 am, Matthew Wilson wrote: > Is there already a tool in the standard library to let me walk up from a > subdirectory to the top of my file system? Never seen such a standard tool, yet it can be implemented in a way like this def walkup(path): aux = path.rsplit('/') while aux != ['']: yield reduce(lambda x,y:x+'/'+y,aux) aux.pop() yield '/' Maybe this is not the best way to ascend directories, but it seems to work. From mark at mailinator.com Mon May 4 08:29:54 2009 From: mark at mailinator.com (Mark) Date: 04 May 2009 12:29:54 GMT Subject: What IDE support python 3.0.1 ? References: Message-ID: On Wed, 15 Apr 2009 22:45:35 +0000, Benjamin Peterson wrote: > Why do you think you're wasting time with 2.x? I'm a relative newbie to python as well but I'd agree that there is at least a small degree of time "wasted" learning python 2.x simply because the standard library of python 3.x has been cleaned up and consolidated which makes learning it slightly easier. From zasaconsulting at gmail.com Mon May 4 08:38:10 2009 From: zasaconsulting at gmail.com (Alexzive) Date: Mon, 4 May 2009 05:38:10 -0700 (PDT) Subject: replacing numbers in LARGE MATRIX with criterium in 2 columns (a--> b) Message-ID: <68d22002-fc0a-4590-9395-c78b6ee412b2@r34g2000vba.googlegroups.com> Hello, I have this matrix [20*4 - but it could be n*4 , with n~100,000] in file "EL_list" like this: 1, 1, 2, 3 2, 4, 1, 5 3, 5, 1, 6 4, 7, 5, 6 5, 8, 7, 9 6, 8, 5, 7 7, 10, 9, 7 8, 10, 11, 12 9, 7, 13, 10 10, 14, 15, 16 11, 14, 17, 15 12, 17, 14, 18 13, 13, 16, 10 14, 16, 15, 11 15, 16, 11, 10 16, 19, 20, 21 17, 22, 15, 20 18, 17, 20, 15 19, 23, 20, 24 20, 25, 24, 20 I would like to replace some numbers in "EL_list" but only in columns 2,3,4 using the criterium by line in file "criterium" (column 1 are the new numbers, column 2 the old ones). 1 1 2 3 3 5 4 12 5 13 6 14 7 15 8 16 9 17 10 18 11 19 12 10 13 21 14 22 15 23 16 24 17 25 For example all the 7 have to replaced by 15 and so on.. -- How to implement it in a fast end efficient code? many thanks, Alex From bjourne at gmail.com Mon May 4 08:39:08 2009 From: bjourne at gmail.com (=?ISO-8859-1?Q?BJ=F6rn_Lindqvist?=) Date: Mon, 4 May 2009 14:39:08 +0200 Subject: Self function In-Reply-To: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> Message-ID: <740c3aec0905040539y78d1332k71a911a7007aa49b@mail.gmail.com> 2009/5/4 : > An idea-syntax: > > def fact(n): > ? ?return 1 if n <= 1 else n * inspect.self(n - 1) > > Or even a lambda, because you don't need the name anymore to call the > function: > > fact = lambda n: 1 if n <= 1 else n * self(n - 1) How would it work with methods? class Foo: def fac(self, n): return 1 if n <= 1 else n * self.self(n-1) ?? -- mvh Bj?rn From zasaconsulting at gmail.com Mon May 4 08:41:04 2009 From: zasaconsulting at gmail.com (Alessandro) Date: Mon, 4 May 2009 05:41:04 -0700 (PDT) Subject: replacing numbers in LARGE MATRIX with criterium in 2 columns (a--> b) References: <68d22002-fc0a-4590-9395-c78b6ee412b2@r34g2000vba.googlegroups.com> Message-ID: <16a90ce7-7634-4f22-9f53-47b1e30ab234@r34g2000vba.googlegroups.com> On May 4, 2:38 pm, Alexzive wrote: > Hello, > I have this matrix [20*4 - but it could be n*4 , with n~100,000] in > file "EL_list" like this: > > 1, 1, 2, 3 > 2, 4, 1, 5 > 3, 5, 1, 6 > 4, 7, 5, 6 > 5, 8, 7, 9 > 6, 8, 5, 7 > 7, 10, 9, 7 > 8, 10, 11, 12 > 9, 7, 13, 10 > 10, 14, 15, 16 > 11, 14, 17, 15 > 12, 17, 14, 18 > 13, 13, 16, 10 > 14, 16, 15, 11 > 15, 16, 11, 10 > 16, 19, 20, 21 > 17, 22, 15, 20 > 18, 17, 20, 15 > 19, 23, 20, 24 > 20, 25, 24, 20 > > I would like to replace some numbers in "EL_list" but only in columns > 2,3,4 using the criterium by line in file "criterium" (column 1 are > the new numbers, column 2 the old ones). > > 1 1 > 2 3 > 3 5 > 4 12 > 5 13 > 6 14 > 7 15 > 8 16 > 9 17 > 10 18 > 11 19 > 12 10 > 13 21 > 14 22 > 15 23 > 16 24 > 17 25 > > For example all the 7 have to replaced by 15 and so on.. > ERRATA: for example, all the 15 have to replaced by 7 and so on.. From bearophileHUGS at lycos.com Mon May 4 08:46:32 2009 From: bearophileHUGS at lycos.com (bearophileHUGS at lycos.com) Date: Mon, 4 May 2009 05:46:32 -0700 (PDT) Subject: find sublist inside list References: Message-ID: <25535ba8-80d9-4eb0-8f31-e453f31e0f26@s20g2000vbp.googlegroups.com> Matthias Gall?: > My problem is to replace all occurrences of a sublist with a new element. > Example: > Given ['a','c','a','c','c','g','a','c'] I want to replace all > occurrences of ['a','c'] by 6 (result [6,6,'c','g',6]). There are several ways to solve this problem. Representing a string as a list of "chars" (1-strings) is fine if you work with small strings, but once they get longer you need too much memory and time. This is a starting point: >>> mapper = {"a":6, "c":6} >>> data = 'acaccgac' >>> [mapper.get(c, c) for c in data] [6, 6, 6, 6, 6, 'g', 6, 6] If you need higher performance, to represent few small numbers into a well defined string like genomic data, you can use ASCII values of 6 and 11: >>> from string import maketrans >>> tab = maketrans("acgt", "".join([chr(6), chr(6), "gt"])) >>> s.translate(tab) '\x06\x06\x06\x06\x06g\x06\x06' Later in processing it's easy to tell apart normal genome bases from those small numbers. Note that there is the array.array function too in the standard lib, and elsewhere there is Numpy too. There are several other possible solutions, but I stop here so you can explain your purposes and constraints better. Bye, bearophile From mail at johnohagan.com Mon May 4 09:01:36 2009 From: mail at johnohagan.com (John O'Hagan) Date: Mon, 4 May 2009 13:01:36 +0000 Subject: find sublist inside list In-Reply-To: References: Message-ID: <200905041301.36962.mail@johnohagan.com> On Mon, 4 May 2009, Matthias Gall? wrote: > Hi. > > My problem is to replace all occurrences of a sublist with a new element. > > Example: > Given ['a','c','a','c','c','g','a','c'] I want to replace all > occurrences of ['a','c'] by 6 (result [6,6,'c','g',6]). > li=['a', 'c', 'a', 'c', 'c', 'g', 'a', 'c'] for i in range(len(li)): if li[i:i + 2] == ['a', 'c']: li[i:i + 2] = ['6'] HTH, John From f.guerrieri at gmail.com Mon May 4 09:13:27 2009 From: f.guerrieri at gmail.com (Francesco Guerrieri) Date: Mon, 4 May 2009 15:13:27 +0200 Subject: find sublist inside list In-Reply-To: <200905041301.36962.mail@johnohagan.com> References: <200905041301.36962.mail@johnohagan.com> Message-ID: <79b79e730905040613y5ba8e36wb27e66260f06247@mail.gmail.com> On Mon, May 4, 2009 at 3:01 PM, John O'Hagan wrote: > On Mon, 4 May 2009, Matthias Gall? wrote: > > Hi. > > > > My problem is to replace all occurrences of a sublist with a new element. > > > > Example: > > Given ['a','c','a','c','c','g','a','c'] I want to replace all > > occurrences of ['a','c'] by 6 (result [6,6,'c','g',6]). > > > > li=['a', 'c', 'a', 'c', 'c', 'g', 'a', 'c'] > for i in range(len(li)): > if li[i:i + 2] == ['a', 'c']: > li[i:i + 2] = ['6'] > > HTH, > > John > Beware that you are mutating the list you are iterating over. That could lead to some strange bugs (for instance if you replaced the deleted items with a longer sequence, the range(len(li)) would still go up to the original lenght). It is better to modify a new list instead. Eg you could append to a new list. Francesco -------------- next part -------------- An HTML attachment was scrubbed... URL: From bearophileHUGS at lycos.com Mon May 4 09:20:34 2009 From: bearophileHUGS at lycos.com (bearophileHUGS at lycos.com) Date: Mon, 4 May 2009 06:20:34 -0700 (PDT) Subject: find sublist inside list References: Message-ID: John O'Hagan: > li=['a', 'c', 'a', 'c', 'c', 'g', 'a', 'c'] > for i ?in range(len(li)): > ? ? if li[i:i + 2] == ['a', 'c']: > ? ? ? ? li[i:i + 2] = ['6'] Oh well, I have done a mistake, it seems. Another solution then: >>> 'acaccgac'.replace("ac", chr(6)) '\x06\x06cg\x06' Bye, bearophile From amrbekhit at gmail.com Mon May 4 09:27:49 2009 From: amrbekhit at gmail.com (Amr) Date: Mon, 4 May 2009 06:27:49 -0700 (PDT) Subject: Default padding for Tkinter grid Message-ID: Hello all, I've been spending the last few days experimenting with Tkinter. The grid manager is nice and easy to use, but I have found that I am often having to specify padx and pady options to every widget I add to my grid. The way I am doing it is to create a dictionary: paddding = {'padx': '1m', 'pady': '1m'} and then apply it to the grid method using **padding. However, I was wondering if there was a way of setting default padx and pady controls for the grid, so that I can just call grid without having to specify any extra parameters. Thanks, --Amr From tomanishkb at gmail.com Mon May 4 09:30:28 2009 From: tomanishkb at gmail.com (M Kumar) Date: Mon, 4 May 2009 19:00:28 +0530 Subject: Help in getting the IP address of client machines Message-ID: <783b47270905040630y2342bb61xe741ba84bbb86707@mail.gmail.com> Hi, I am using pylons web framework for my server. I need to get the ip address of the client machine which made a request to the server in my python code. How can I do that, I am new to pylons and the existing server has so many applications. looking forward to get some solutions from the kind friendly users thanks & regards Maneesh KB -------------- next part -------------- An HTML attachment was scrubbed... URL: From ganesh.gbg at gmail.com Mon May 4 09:44:41 2009 From: ganesh.gbg at gmail.com (gganesh) Date: Mon, 4 May 2009 06:44:41 -0700 (PDT) Subject: Is there is any way to send messages to chunk of emails ID's concurrently using smptlib Message-ID: <17ea2ba6-3f5d-4e7e-b05b-6d91dfbc5b36@k19g2000prh.googlegroups.com> Hi friends, I suppose sendmail() can send mails one by one ,how to send mails concurrently , It would be very grateful,if someone could point out a solution. Thanks Ganesh From deets at nospam.web.de Mon May 4 09:45:16 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Mon, 04 May 2009 15:45:16 +0200 Subject: How to walk up parent directories? References: <5deab389-38f2-4c54-bfe3-3ce7cefa8764@v17g2000vbb.googlegroups.com> Message-ID: <7689oiF1bjavlU1@mid.uni-berlin.de> Snorri H wrote: > On May 4, 5:04 am, Matthew Wilson wrote: >> Is there already a tool in the standard library to let me walk up from a >> subdirectory to the top of my file system? > > > Never seen such a standard tool, yet it can be implemented in a way > like this > > def walkup(path): > aux = path.rsplit('/') > while aux != ['']: > yield reduce(lambda x,y:x+'/'+y,aux) > aux.pop() > yield '/' > > Maybe this is not the best way to ascend directories, but it seems to > work. It's not working for windows. And using reduce to create a path instead of os.sep.join(aux) seems to be showing off mad functional coding skillz than anything else... Diez From kyrie at uh.cu Mon May 4 09:52:14 2009 From: kyrie at uh.cu (Luis Zarrabeitia) Date: Mon, 4 May 2009 09:52:14 -0400 Subject: Multiprocessing.Queue - I want to end. In-Reply-To: <00a201c9cc8e$89420f40$0d00a8c0@Hendrik> References: <200904301649.10341.kyrie@uh.cu> <1241412986.49fe757a94f93@mail.uh.cu> <00a201c9cc8e$89420f40$0d00a8c0@Hendrik> Message-ID: <200905040952.14804.kyrie@uh.cu> On Monday 04 May 2009 04:01:23 am Hendrik van Rooyen wrote: > This will form a virtual (or real if you have different machines) > systolic array with producers feeding consumers that feed > the summary process, all running concurrently. Nah, I can't do that. The summary process is expensive, but not nearly as expensive as the consuming (10 minutes vs. a few hours), and can't be started anyway before the consumers are done. > You only need to keep the output of the consumers in files if > you need access to it later for some reason. In your case it sounds > as if you are only interested in the output of the summary. Or if the summarizing process requires that it is stored on files. Or if the consumers naturally store the data on files. The consumers "produce" several gigabytes of data, not enough to make it intractable, but enough to not want to load them into RAM or transmit it back. In case you are wondering what the job is: i'm indexing a lot of documents with Xapian. The producer reads the [compressed] documents from the hard disk, the consumers process it and index it on they own xapian database. When they are finished, I merge the databases (the summarizing) and delete the partial DBs. I don't need the DBs to be in memory at any time, and xapian works with files anyway. Even if I were to use different machines (not worth it for a process that will not run very frequently, except at developing time), it would be still cheaper to scp the files. Now, if I only had a third core available to consume a bit faster ... Regards, -- Luis Zarrabeitia (aka Kyrie) Fac. de Matem?tica y Computaci?n, UH. http://profesores.matcom.uh.cu/~kyrie From davea at ieee.org Mon May 4 09:52:38 2009 From: davea at ieee.org (Dave Angel) Date: Mon, 04 May 2009 09:52:38 -0400 Subject: replacing numbers in LARGE MATRIX with criterium in 2 columns (a--> b) In-Reply-To: <16a90ce7-7634-4f22-9f53-47b1e30ab234@r34g2000vba.googlegroups.com> References: <68d22002-fc0a-4590-9395-c78b6ee412b2@r34g2000vba.googlegroups.com> <16a90ce7-7634-4f22-9f53-47b1e30ab234@r34g2000vba.googlegroups.com> Message-ID: <49FEF326.7080501@ieee.org> Alessandro wrote: > On May 4, 2:38 pm, Alexzive wrote: > >> Hello, >> I have this matrix [20*4 - but it could be n*4 , with n~100,000] in >> file "EL_list" like this: >> >> 1, 1, 2, 3 >> 2, 4, 1, 5 >> 3, 5, 1, 6 >> 4, 7, 5, 6 >> 5, 8, 7, 9 >> 6, 8, 5, 7 >> 7, 10, 9, 7 >> 8, 10, 11, 12 >> 9, 7, 13, 10 >> 10, 14, 15, 16 >> 11, 14, 17, 15 >> 12, 17, 14, 18 >> 13, 13, 16, 10 >> 14, 16, 15, 11 >> 15, 16, 11, 10 >> 16, 19, 20, 21 >> 17, 22, 15, 20 >> 18, 17, 20, 15 >> 19, 23, 20, 24 >> 20, 25, 24, 20 >> >> I would like to replace some numbers in "EL_list" but only in columns >> 2,3,4 using the criterium by line in file "criterium" (column 1 are >> the new numbers, column 2 the old ones). >> >> 1 1 >> 2 3 >> 3 5 >> 4 12 >> 5 13 >> 6 14 >> 7 15 >> 8 16 >> 9 17 >> 10 18 >> 11 19 >> 12 10 >> 13 21 >> 14 22 >> 15 23 >> 16 24 >> 17 25 >> > > > > >> For example all the 7 have to replaced by 15 and so on.. >> >> > > ERRATA: for example, all the 15 have to replaced by 7 and so on.. > > What have you come up with so far? Did the instructor give you any sample code to solve similar problems? Did he really describe it as a matrix? Sounds to me like you have two text files. One contains a replacement table (which you can read into a dictionary), and the other (large) file is just text. After building the dictionary, you'd want a loop that goes through the text file, splitting each line into a list of four. You then use the dictionary to replace list items 1, 2, and 3, and write the result someplace. Try writing the code, see if anything goes wrong, and then ask for help. From google at mrabarnett.plus.com Mon May 4 09:54:33 2009 From: google at mrabarnett.plus.com (MRAB) Date: Mon, 04 May 2009 14:54:33 +0100 Subject: replacing numbers in LARGE MATRIX with criterium in 2 columns (a--> b) In-Reply-To: <68d22002-fc0a-4590-9395-c78b6ee412b2@r34g2000vba.googlegroups.com> References: <68d22002-fc0a-4590-9395-c78b6ee412b2@r34g2000vba.googlegroups.com> Message-ID: <49FEF399.5070806@mrabarnett.plus.com> Alexzive wrote: > Hello, > I have this matrix [20*4 - but it could be n*4 , with n~100,000] in > file "EL_list" like this: > > 1, 1, 2, 3 > 2, 4, 1, 5 > 3, 5, 1, 6 > 4, 7, 5, 6 > 5, 8, 7, 9 > 6, 8, 5, 7 > 7, 10, 9, 7 > 8, 10, 11, 12 > 9, 7, 13, 10 > 10, 14, 15, 16 > 11, 14, 17, 15 > 12, 17, 14, 18 > 13, 13, 16, 10 > 14, 16, 15, 11 > 15, 16, 11, 10 > 16, 19, 20, 21 > 17, 22, 15, 20 > 18, 17, 20, 15 > 19, 23, 20, 24 > 20, 25, 24, 20 > > I would like to replace some numbers in "EL_list" but only in columns > 2,3,4 using the criterium by line in file "criterium" (column 1 are > the new numbers, column 2 the old ones). > > 1 1 > 2 3 > 3 5 > 4 12 > 5 13 > 6 14 > 7 15 > 8 16 > 9 17 > 10 18 > 11 19 > 12 10 > 13 21 > 14 22 > 15 23 > 16 24 > 17 25 > > For example all the 7 have to replaced by 15 and so on.. > > -- How to implement it in a fast end efficient code? > Read the contents of "criterium" into a dict with the old value as the key and the new value as the value and then iterate through the matrix replacing the values in columns 2 to 4 with criterium.get(current_value, current_value). Incidentally, the singular of "criteria" is "criterion" because it's Greek, not Latin. From google at mrabarnett.plus.com Mon May 4 09:57:51 2009 From: google at mrabarnett.plus.com (MRAB) Date: Mon, 04 May 2009 14:57:51 +0100 Subject: [SPAM] Re: Threaded alternatives to smtplib? In-Reply-To: <767h7tF1b8q8gU1@mid.uni-berlin.de> References: <767h7tF1b8q8gU1@mid.uni-berlin.de> Message-ID: <49FEF45F.7070700@mrabarnett.plus.com> Diez B. Roggisch wrote: > Alex Jurkiewicz schrieb: >> Hi all, >> I'm writing a Python script to do a "mail merge" style email >> distribution. I create a few python threads and in each one I call >> `smtpserver = smtplib.SMTP(our.smtpserver.com)`. However, during the >> sending process, there seems to be only one connection open to our >> mail server at any one time. In other words, all these threads gain me >> no speed benefit at all! >> >> I seem to be running into the issue where smtplib is not fully >> thread-safe, as mentioned in this thread: >> http://mail.python.org/pipermail/python-list/2007-March/429067.html >> http://mail.python.org/pipermail/python-list/2007-March/429172.html >> >> Does anyone have suggestions as to a suitable workaround for this >> issue? I was looking at the Twisted library, which seems possible but >> significantly more complex. > > I doubt that there is such issue. The above discussion is not concluding > that there actually *are* threading-issues within smtplib. And looking > at the source of smtplib, it certainly doesn't use any > locking-primitives or (directly) any c-extension that might explain such > behavior. > > Without more code, it's impossible to tell if there is anything peculiar > in your usage of the lib. Maybe you close your connections to fast to > see several open? > If all the threads are using the same server, couldn't the SMTP part be put into its own thread and then fed the pre-composed emails via a queue? From dns4 at cornell.edu Mon May 4 09:58:40 2009 From: dns4 at cornell.edu (David Smith) Date: Mon, 04 May 2009 09:58:40 -0400 Subject: How to walk up parent directories? In-Reply-To: References: Message-ID: Matthew Wilson wrote: > Is there already a tool in the standard library to let me walk up from a > subdirectory to the top of my file system? > > In other words, I'm looking for something like: > > >>> for x in walkup('/home/matt/projects'): > ... print(x) > /home/matt/projects > /home/matt > /home > / > > I know I could build something like this with various os.path > components, but I'm hoping I don't have to. > > TIA > > > Matt You should take a look at the os.path module. Seems like you might find something in that toolbox for this. --David From mgalle at irisa.fr Mon May 4 10:01:02 2009 From: mgalle at irisa.fr (=?ISO-8859-1?Q?Matthias_Gall=E9?=) Date: Mon, 04 May 2009 16:01:02 +0200 Subject: find sublist inside list In-Reply-To: References: Message-ID: bearophileHUGS at lycos.com wrote: > John O'Hagan: >> li=['a', 'c', 'a', 'c', 'c', 'g', 'a', 'c'] >> for i in range(len(li)): >> if li[i:i + 2] == ['a', 'c']: >> li[i:i + 2] = ['6'] > > Oh well, I have done a mistake, it seems. > Another solution then: > >>>> 'acaccgac'.replace("ac", chr(6)) > '\x06\x06cg\x06' > > Bye, > bearophile Thanks bearophile and John for your quick answers. Unfortunately, the int that can replace a sublist can be > 255, but John's answer looks simple and good enough for me. I will use it as a starting point. Thank's again. -- Matthias Gall? Project Symbiose Centre de Recherche INRIA Rennes - Bretagne Atlantique, Campus de Beaulieu, 35042 Rennes cedex, France tel: (33|0) 2 9984 7523 http://www.irisa.fr/symbiose/matthias_galle From ross.jett at gmail.com Mon May 4 10:01:07 2009 From: ross.jett at gmail.com (Ross) Date: Mon, 4 May 2009 07:01:07 -0700 (PDT) Subject: Code works fine except... References: <8d4ec1df-dddb-469a-99a1-695152db7c04@n4g2000vba.googlegroups.com> <7f3493ca-efcb-48cc-aa29-487bb11fcd09@p4g2000vba.googlegroups.com> Message-ID: <16d47b2f-14d4-4266-84d2-c26501beb0c1@r34g2000vbi.googlegroups.com> On May 3, 10:16?pm, John Yeung wrote: > On May 3, 11:29 pm, Chris Rebert wrote: > > > Probably not the cause of the problem, but where > > did the magic numbers 1.072 and 1.08 come from? > > It is perhaps not the most direct cause of the problem, in the sense > that the magic numbers could take various values and the problem would > still be there. ?But the magic numbers appear to be used for > "spreading out" bye selection, and that's broken. > > The extended slice as written will always pick the first element, > since the step is guaranteed to be positive. ?Since the first player > (or None, when there are an odd number of players) stays put in the > first position during the round_robin shuffle, that player will always > be selected for a bye. > > Further, as written, the calculated number of byes has no bearing on > the actual number of byes selected. > > I think what I would do is adjust the shuffling algorithm in such a > way that everyone moves through the various positions in the list > (would it be as simple as adding a shift at the end of > round_robin???). ?Then I could simply select the byes from one end of > the list (with a regular slice instead of an extended slice). > > John The "magic numbers" that everyone is wondering about are indeed used for spreading out the bye selection and I got them by simply calculating a line of best fit when plotting several courts: byes ratios. The "byes = #whatever" in my code calculate the number of tuples that need to be placed in the bye_list. At first glance, the suggestion of adding a simple shift at the end of round_robin doesn't seem to work since round_robin already shifts everything except for the first position already. Please correct me if I'm wrong because you might have been suggesting something different. From ross.jett at gmail.com Mon May 4 10:07:38 2009 From: ross.jett at gmail.com (Ross) Date: Mon, 4 May 2009 07:07:38 -0700 (PDT) Subject: Code works fine except... References: <8d4ec1df-dddb-469a-99a1-695152db7c04@n4g2000vba.googlegroups.com> <7f3493ca-efcb-48cc-aa29-487bb11fcd09@p4g2000vba.googlegroups.com> <16d47b2f-14d4-4266-84d2-c26501beb0c1@r34g2000vbi.googlegroups.com> Message-ID: On May 4, 7:01?am, Ross wrote: > On May 3, 10:16?pm, John Yeung wrote: > > > > > On May 3, 11:29 pm, Chris Rebert wrote: > > > > Probably not the cause of the problem, but where > > > did the magic numbers 1.072 and 1.08 come from? > > > It is perhaps not the most direct cause of the problem, in the sense > > that the magic numbers could take various values and the problem would > > still be there. ?But the magic numbers appear to be used for > > "spreading out" bye selection, and that's broken. > > > The extended slice as written will always pick the first element, > > since the step is guaranteed to be positive. ?Since the first player > > (or None, when there are an odd number of players) stays put in the > > first position during the round_robin shuffle, that player will always > > be selected for a bye. > > > Further, as written, the calculated number of byes has no bearing on > > the actual number of byes selected. > > > I think what I would do is adjust the shuffling algorithm in such a > > way that everyone moves through the various positions in the list > > (would it be as simple as adding a shift at the end of > > round_robin???). ?Then I could simply select the byes from one end of > > the list (with a regular slice instead of an extended slice). > > > John > > The "magic numbers" that everyone is wondering about are indeed used > for spreading out the bye selection and I got them by simply > calculating a line of best fit when plotting several courts: byes > ratios. > > The "byes = #whatever" in my code calculate the number of tuples that > need to be placed in the bye_list. > > At first glance, the suggestion of adding a simple shift at the end of > round_robin doesn't seem to work since round_robin already shifts > everything except for the first position already. Please correct me if > I'm wrong because you might have been suggesting something different. And also, as you all have pointed out, my inclusion of > def test_round_robin(players, rounds, courts, doubles = False): > players = range(players) > for week in round_robin(players,rounds,courts): should have been > def test_round_robin(players, rounds, courts, doubles = False): > players = range(players) > for week in round_robin(players,rounds): I forgot to erase that extra parameter when I was playing around with my code. From flamz3d at gmail.com Mon May 4 10:09:20 2009 From: flamz3d at gmail.com (flamz3d at gmail.com) Date: Mon, 4 May 2009 07:09:20 -0700 (PDT) Subject: AutoComplete in C++ Editor for Python References: Message-ID: <10fef506-2c39-418c-82e8-5c0c05a19e5c@s31g2000vbp.googlegroups.com> On May 3, 3:14?pm, Dave Angel wrote: > flam... at gmail.com wrote: > > Hello, > > I am embedding python support in my C++ application and was looking at > > adding "Intellisense" or "AutoComplete" support. > > > I found a way to do it using the "dir" function, but this creates a > > problem. Here's why. Let's say I have the following code in my editor: > > > import sys > > x = sys > > > Now, I would like to get all attributes of the object called 'x'. I > > can "instrument" the code and add "print dir(x)" at the end, > > temporarily redirect the python output to a string and execute the > > code. > > > But this is not safe: I do NOT want to execute the code while the user > > is typing! > > > Is there a way to "compile" the python code and get access to the > > symbol table from that compiled block? > > > Did anybody ever implement AutoComplete in a editor for Python? > > > cheers. > > Several editors for Python support auto-complete, to one extent or > another. ?The only one I have experience with is Komodo. ?Komodo runs in > a separate process, so it doesn't suffer from the problems of having two > gui event-loops in the same process, and other similar problems. ? It > also won't be executing code that might have side effects in the child > process. > > The downside is that in order to do auto-complete, it has to figure it > out from other clues. ?From the docs, and from reading, and from > experiementing, I believe that it uses two approaches. ?One approach is > a set of language files which try to describe all the symbols in the > standard language and library. ?They have one for each release (2.4, > 2.5, ...) ?Theoretically, you could add your own for other libraries. ? > Second approach is to parse the code that's visible to it. ?That parsing > is well done, and surprisingly quick, but there are lots of tricks a > developer might use that can fool it. ?For example, wxPython lets you > import just one symbol, and lots more appear magically. ?It's no big > deal, they have code structured one way, but the interface is very > different. ?Catch is that code completion frequently gets fooled by these. > > I'd point out that if you do walk the dictionaries at run time, you'll > get different results when you do it with nothing running than if you do > a strictly static analysis. ?So some things might only show up if you've > stepped into the function with the magic going on. > > Simplest example I can come up with of something a static analysis won't > spot: ? An instance object ? ?obj ? may have some number of attributes > assigned the the __init__() method. ?But it could also have new fields > added by anyone with a referent into it. > > There is a Python parser in module ast. ?Perhaps that'd be some help. ? > I've not used it, so can't give you any specifics. > > DaveA I think it might be possible for me to figure out how to do AutoCompletion using the symtable object. But I am having difficulty making sense of it. In my tests, I was use the following Python code: [code] import sys x=sys [/code] The idea is to get the type of the variable x To attempt this, I use the following C code (my editor is written in C) symtable* st = Py_SymtableString ( "import sys \nx=sys,"",Py_single_input); PyObject *name, *value; Py_ssize_t pos = 0; while (PyDict_Next(st->st_symbols, &pos, &name, &value)) { ... } Using this code, I can get information like the name of the symbol (x), but I can't figure out how to get the type. If I knew how to get this it would solve 99% of my problems :) Any ideas? From apardon at forel.vub.ac.be Mon May 4 10:25:44 2009 From: apardon at forel.vub.ac.be (Antoon Pardon) Date: 4 May 2009 14:25:44 GMT Subject: if statement, with function inside it: if (t = Test()) == True: References: <020183f4$0$20619$c3e8da3@news.astraweb.com> Message-ID: On 2009-04-24, Steven D'Aprano wrote: > On Fri, 24 Apr 2009 03:00:26 -0700, GC-Martijn wrote: > >> Hello, >> >> I'm trying to do a if statement with a function inside it. I want to use >> that variable inside that if loop , without defining it. >> >> def Test(): >> return 'Vla' >> >> I searching something like this: >> >> if (t = Test()) == 'Vla': >> print t # Vla >> >> or >> >> if (t = Test()): >> print t # Vla > > Fortunately, there is no way of doing that with Python. This is one > source of hard-to-debug bugs that Python doesn't have. I think this is an unfortunate consequence of choosing '=' for the assignment. They could have chosen an other token to indicate an assignment one that would have made the difference between an assignment and a comparison more visually different and thus bugs by using one while needing the other less hard to track down. So when a certain kind of bug is hard to track down because of the similarity of the tokens and not because of the specific functionality I find it unfortunate they dropped the functionality instead of making the tokens less similar. -- Antoon Pardon From bearophileHUGS at lycos.com Mon May 4 10:53:03 2009 From: bearophileHUGS at lycos.com (bearophileHUGS at lycos.com) Date: Mon, 4 May 2009 07:53:03 -0700 (PDT) Subject: find sublist inside list References: Message-ID: Matthias Gall?: >the int that can replace a sublist can be > 255,< You didn't specify your integer ranges. Probably there are many other solutions for your problem, but you have to give more information. Like the typical array size, typical range of the numbers, how much important is total memory used, how much important is running speed, what kind of processing (or serialization/ output) you later have to do with such arrays, and so on. Other solutions include using an array('H', []), and using 0-255 to represent ASCII and numbers >255 <2^16 to represent the other numbers, etc. If speed is critical you can even think about creating a little function with PyInline or D+Pyd, etc. Bye, bearophile From gagsl-py2 at yahoo.com.ar Mon May 4 10:57:41 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Mon, 04 May 2009 11:57:41 -0300 Subject: Default padding for Tkinter grid References: Message-ID: En Mon, 04 May 2009 10:27:49 -0300, Amr escribi?: > I've been spending the last few days experimenting with Tkinter. The > grid manager is nice and easy to use, but I have found that I am often > having to specify padx and pady options to every widget I add to my > grid. The way I am doing it is to create a dictionary: > > paddding = {'padx': '1m', 'pady': '1m'} > > and then apply it to the grid method using **padding. > > However, I was wondering if there was a way of setting default padx > and pady controls for the grid, so that I can just call grid without > having to specify any extra parameters. You have to call grid() once on every widget, so adding **padding at the end doesn't appear too bad to me: label = Label(master, text="Hello world!") widget.grid(row=3, col=1, **padding) What about a function: def grid_with_padding(widget, padx='1m', pady='1m', **kw): widget.grid(padx=padx, pady=pady, **kw) If you want an uniform padding: def add_padding(container, padx='1m', pady='1m'): nc, nr = container.grid_size() for i in range(nc): container.grid_columnconfigure(i, pad=padx) for i in range(nr): container.grid_rowconfigure(i, pad=pady) -- Gabriel Genellina From google at mrabarnett.plus.com Mon May 4 11:09:16 2009 From: google at mrabarnett.plus.com (MRAB) Date: Mon, 04 May 2009 16:09:16 +0100 Subject: find sublist inside list In-Reply-To: References: Message-ID: <49FF051C.9080209@mrabarnett.plus.com> Matthias Gall? wrote: > bearophileHUGS at lycos.com wrote: >> John O'Hagan: >>> li=['a', 'c', 'a', 'c', 'c', 'g', 'a', 'c'] >>> for i in range(len(li)): >>> if li[i:i + 2] == ['a', 'c']: >>> li[i:i + 2] = ['6'] >> >> Oh well, I have done a mistake, it seems. >> Another solution then: >> >>>>> 'acaccgac'.replace("ac", chr(6)) >> '\x06\x06cg\x06' >> >> Bye, >> bearophile > > Thanks bearophile and John for your quick answers. > Unfortunately, the int that can replace a sublist can be > 255, but > John's answer looks simple and good enough for me. I will use it as a > starting point. > John's solution changes the length of the list over which it's iterating. I'd suggest something more like: li = ['a', 'c', 'a', 'c', 'c', 'g', 'a', 'c'] pos = 0 try: while True: pos = li.index('a', pos) if li[pos : pos + 2] == ['a', 'c']: li[pos : pos + 2] = [6] pos += 1 except ValueError: pass From amrbekhit at gmail.com Mon May 4 11:10:38 2009 From: amrbekhit at gmail.com (Amr) Date: Mon, 4 May 2009 08:10:38 -0700 (PDT) Subject: Default padding for Tkinter grid References: Message-ID: <2c838b86-4eb3-4ca9-9ea5-6b0d23064d9b@v17g2000vbb.googlegroups.com> On May 4, 3:57?pm, "Gabriel Genellina" wrote: > En Mon, 04 May 2009 10:27:49 -0300, Amr escribi?: > > > I've been spending the last few days experimenting with Tkinter. The > > grid manager is nice and easy to use, but I have found that I am often > > having to specify padx and pady options to every widget I add to my > > grid. The way I am doing it is to create a dictionary: > > > paddding = {'padx': '1m', 'pady': '1m'} > > > and then apply it to the grid method using **padding. > > > However, I was wondering if there was a way of setting default padx > > and pady controls for the grid, so that I can just call grid without > > having to specify any extra parameters. > > You have to call grid() once on every widget, so adding **padding at the ? > end doesn't appear too bad to me: > > ? ?label = Label(master, text="Hello world!") > ? ?widget.grid(row=3, col=1, **padding) > > What about a function: > > def grid_with_padding(widget, padx='1m', pady='1m', **kw): > ? ?widget.grid(padx=padx, pady=pady, **kw) > > If you want an uniform padding: > > def add_padding(container, padx='1m', pady='1m'): > ? ?nc, nr = container.grid_size() > ? ?for i in range(nc): > ? ? ?container.grid_columnconfigure(i, pad=padx) > ? ?for i in range(nr): > ? ? ?container.grid_rowconfigure(i, pad=pady) > > -- > Gabriel Genellina Hi Gabriel, Thanks for your reply - I like the method you mention at the end, using grid_columnconfigure and grid_rowconfigure as I can use that to apply setting to the whole grid at the end. I'll have a mess around with it. --Amr From soumen08 at gmail.com Mon May 4 11:13:43 2009 From: soumen08 at gmail.com (Soumen banerjee) Date: Mon, 4 May 2009 20:43:43 +0530 Subject: IDE for python 2.6 (auto complete) Message-ID: Hello, I have just installed and run python 2.6.2 from the sources available on the website. I notice that SPE (the editor which i used with python 2.5) is not displaying some of the functions new in 2.6 as autocomplete options. Is there any IDE with support for autocomplete in python 2.6 with all the newer functions included? From showell30 at yahoo.com Mon May 4 11:27:06 2009 From: showell30 at yahoo.com (Steve Howell) Date: Mon, 4 May 2009 08:27:06 -0700 (PDT) Subject: Self function References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> Message-ID: <40fbffe5-a9dc-4a8c-a239-fa546c5ff2ef@z19g2000vbz.googlegroups.com> On May 3, 3:39?pm, bearophileH... at lycos.com wrote: > Sometimes I rename recursive functions, or I duplicate&modify them, > and they stop working because inside them there's one or more copy of > their old name. > This happens to me more than one time every year. > So I have written this: > > from inspect import getframeinfo, currentframe > > def SOMEVERYUGLYNAME(n): > ? ? if n <= 1: > ? ? ? ? return 1 > ? ? else: > ? ? ? ? self_name = getframeinfo(currentframe()).function > ? ? ? ? #self_name = getframeinfo(currentframe())[2] # older python > > ? ? ? ? # only if it's a global function > ? ? ? ? #return n * globals()[self_name](n - 1) > ? ? ? ? return n * eval(self_name + "(%d)" % (n - 1)) > assert SOMEVERYUGLYNAME(6) == 2*3*4*5*6 > > Are there nicer ways to do that? I don't know. > If there aren't, then a way may be added. > An idea-syntax: > > def fact(n): > ? ? return 1 if n <= 1 else n * inspect.self(n - 1) > > Or even a lambda, because you don't need the name anymore to call the > function: > > fact = lambda n: 1 if n <= 1 else n * self(n - 1) > > (If you have two or more recursive functions that call each other this > idea can't be used, but I think such situations are uncommon enough to > not deserve help from the language). > One very simple thing that you can do is assign the current function name to a local var at the very top to make it a little more obvious. I agree that there are lots of recursive patterns where python itself does not provide much sugar. A pattern that comes up for me occasionally is two methods with almost identical names, where one function is the public interface and then another method that does most of the recursion. From kyosohma at gmail.com Mon May 4 11:46:03 2009 From: kyosohma at gmail.com (Mike Driscoll) Date: Mon, 4 May 2009 08:46:03 -0700 (PDT) Subject: IDE for python 2.6 (auto complete) References: Message-ID: <6e840938-6180-44a8-8a1e-7f1660cfa2af@n8g2000vbb.googlegroups.com> On May 4, 10:13?am, Soumen banerjee wrote: > Hello, > I have just installed and run python 2.6.2 from the sources available > on the website. I notice that SPE (the editor which i used with python > 2.5) is not displaying some of the functions new in 2.6 as > autocomplete options. Is there any IDE with support for autocomplete > in python 2.6 with all the newer functions included? Wingware probably does. You should just submit a bug report to Stani though. He can probably fix SPE for you. Mike From ross.jett at gmail.com Mon May 4 12:01:05 2009 From: ross.jett at gmail.com (Ross) Date: Mon, 4 May 2009 09:01:05 -0700 (PDT) Subject: Code works fine except... References: <8d4ec1df-dddb-469a-99a1-695152db7c04@n4g2000vba.googlegroups.com> Message-ID: <4e49873c-9e74-4a3f-9070-2f245e6467c2@e20g2000vbc.googlegroups.com> On May 3, 8:29?pm, John Machin wrote: > On May 4, 12:36?pm, Ross wrote: > > > > > For the past couple weeks, I've been working on an algorithm to > > schedule tennis leagues given court constraints and league > > considerations (i.e. whether it's a singles or a doubles league). Here > > were my requirements when I was designing this algorithm: > > > -Each player plays against a unique opponent each week. > > -Similarly, in a doubles league, each player plays with a unique > > partner each week. > > -Each player gets a fair number of bye weeks (i.e. the player with the > > most bye weeks will have no more than one bye week than the player > > with the least number of bye weeks) > > > I'm very close to arriving at my desired solution, but I have one > > glaring flaw. When I have an even number of players sign up for my > > league and there are court constraints, my current algorithm gives the > > first player in my league a bye week every single week. I'll post my > > code below and see how you guys think I should add to/ amend my code. > > > def round_robin(players, rounds): > > ? ? if len(players)%2: > > ? ? ? ? players.insert(0, None) > > ? ? mid = len(players)//2 > > ? ? for i in range(rounds): > > ? ? ? ? yield zip(players[:mid], players[mid:]) > > ? ? ? ? players = players[0:1] + players[mid:mid+1] + players[1:mid-1] + > > players[mid+1:] + players[mid-1:mid] > > > def test_round_robin(players, rounds, courts, doubles = False): > > ? ? players = range(players) > > DON'T change the type/contents/meaning of a variable name like that. > E.g. use "nthings" for a number of things and "things" for a > collection of things. > > > ? ? for week in round_robin(players,rounds,courts): > > The round_robin() function has only TWO arguments. This code won't > even run. > > When you document neither your data structures nor what your functions > are intended to do, the last hope for somebody trying to make sense of > your code is to give meaningful names to your variables. "week" and > "doubles_week" are NOT meaningful. > > > ? ? ? ? ? ? if doubles == True: > > Bletch. s/ == True// > > > ? ? ? ? ? ? ? ? ? ? doubles_week = len(week)/2.0 > > I doubt very much that using floating point is a good idea here. > > > ? ? ? ? ? ? ? ? ? ? byes = doubles_week - courts > > ? ? ? ? ? ? ? ? ? ? if byes == 0: > > ? ? ? ? ? ? ? ? ? ? ? ? ? ? bye_list = [] > > ? ? ? ? ? ? ? ? ? ? else: > > ? ? ? ? ? ? ? ? ? ? ? ? ? ? bye_list = week[::int(round(1.072*(courts/byes)+1.08))] > > The derivation of the constants 1.072 and 1.08 is .... what? > > > ? ? ? ? ? ? ? ? ? ? playing = [u for u in week if u not in bye_list] > > ? ? ? ? ? ? ? ? ? ? midd = len(playing)//2 > > ? ? ? ? ? ? ? ? ? ? doub_sched = zip(playing[:midd], playing[midd:]) > > ? ? ? ? ? ? ? ? ? ? print doub_sched, bye_list > > ? ? ? ? ? ? else: > > ? ? ? ? ? ? ? ? ? ? byes = len(week)- courts > > ? ? ? ? ? ? ? ? ? ? if byes == 0: > > ? ? ? ? ? ? ? ? ? ? ? ? ? ? bye_list = [] > > ? ? ? ? ? ? ? ? ? ? else: > > ? ? ? ? ? ? ? ? ? ? ? ? ? ? bye_list = week[::int(round(1.072*(courts/byes)+1.08))] > > ? ? ? ? ? ? ? ? ? ? playing = [u for u in week if u not in bye_list] > > ? ? ? ? ? ? ? ? ? ? print playing, bye_list For everybody's enlightenment, I have gone through and commented my code so you can better understand what I'm doing. Here it is: def round_robin(players, rounds): # if number of players odd, insert None at first position if len(players)%2: players.insert(0, None) mid = len(players)//2 for i in range(rounds): yield zip(players[:mid], players[mid:]) players = players[0:1] + players[mid:mid+1] + players[1:mid-1] + players[mid+1:] + players[mid-1:mid] """ rotates players like this: 1 2 -> 3 -> 4 / | 5 <- 6 <-7 <- 8 """ def test_round_robin(players, rounds, courts, doubles = False): players = range(players) for week in round_robin(players,rounds): if doubles == True: #for doubles pairings doubles_week = len(week)/2.0 byes = doubles_week - courts #number of tuples to be put into bye_list if byes == 0: bye_list = [] else: """ following formula equally spaces out tuples selected for bye_list and selects appropriate number according to length of the league""" bye_list = week[::int(round(1.072*(courts/byes)+1.08))] playing = [u for u in week if u not in bye_list] midd = len(playing)//2 doub_sched = zip(playing[:midd], playing[midd:])#matches the remaining tuples into doubles matches print doub_sched, bye_list else: byes = len(week)- courts if byes == 0: bye_list = [] else: bye_list = week[::int(round(1.072*(courts/byes)+1.08))] playing = [u for u in week if u not in bye_list] print playing, bye_list From mabdelkader at gmail.com Mon May 4 12:03:42 2009 From: mabdelkader at gmail.com (ma) Date: Mon, 4 May 2009 12:03:42 -0400 Subject: fcntl and siginfo_t in python In-Reply-To: <148918f0905011208u4946c377uce7603bb2e456370@mail.gmail.com> References: <148918f0904281356v534680dcmd03084fe255cb2d3@mail.gmail.com> <148918f0904291012j4c1ada35jcee6030b75b952d0@mail.gmail.com> <148918f0904301637t5e63537atd85e2a475f4e069f@mail.gmail.com> <148918f0905011208u4946c377uce7603bb2e456370@mail.gmail.com> Message-ID: <148918f0905040903s7a5c7e5exc2968d0e65540d81@mail.gmail.com> Ok! So, I decided to write a C-extension instead of using ctypes. So far, I create a module called dnotifier and the handler callback receives two arguments, the signal number and the respective file descriptor that was modified. This works beautifully. Now, I want to release this to the public, so I'm thinking of making a bit of code cleanup. Should I just pack the entire siginfo_t struct, right now I just use the fd, into a dictionary and pass it to the python callback handler function? Maybe there might be some more suggestions to what data structures to use, so I'm open right now to any of them. The siginfo_t structure is defined as follows in C: union sigval { int sival_int; void *sival_ptr; }; typedef struct { int si_signo; int si_code; union sigval si_value; int si_errno; pid_t si_pid; uid_t si_uid; void *si_addr; int si_status; int si_band; } siginfo_t; Thanks, Mahmoud On Fri, May 1, 2009 at 3:08 PM, ma wrote: > > According to man signal, > "The default action for an unhandled real-time signal is to terminate > the receiving process." > > This means that my registered callback and sigaction does not work. I > think the only solution would be to try this with a C-extension. Has > anyone had any experience with this before? > > I attached my latest copy. Any insight is appreciated. > > On Thu, Apr 30, 2009 at 7:37 PM, ma wrote: > > I attached a clean copy of the .py file in case others couldn't read > > it in their emails. > > I'll try that and let you know how?SIGRTMIN+1 goes! > > What about this part? > > > > #sigemptyset(&act.sa_mask); > > #python2.6 has byref(act, offset),how can i port this over? > > #maybe addressof(act)+sizeof(sigaction.sa_mask)*(position_in_sigaction) > > rc = __clib.sigemptyset(byref(act)) > > > > Thanks! > > Mahmoud > > > > > > > > On Thu, Apr 30, 2009 at 7:33 PM, Philip wrote: > >> > >> ma gmail.com> writes: > >> > >> > > >> > > >> > > >> > > >> > Here's something that I came up with so far, I'm having some issues with > >> segfaulting, if I want to pass a struct member by ref in ctypes(see below), if > >> not, I just get a > >> > "Real-time signal 0" sent back to me. > >> > > >> > > >> > Any ideas? > >> > >> Try "SIGRTMIN+1", per http://souptonuts.sourceforge.net/code/dnotify.c.html > >> > >> Philip > >> > >> > >> > >> -- > >> http://mail.python.org/mailman/listinfo/python-list > > From matt at tplus1.com Mon May 4 12:07:21 2009 From: matt at tplus1.com (Matthew Wilson) Date: Mon, 04 May 2009 16:07:21 GMT Subject: How to walk up parent directories? References: <87ocu9k6c4.fsf@benfinney.id.au> Message-ID: On Sun 03 May 2009 09:24:59 PM EDT, Ben Finney wrote: > Not every simple function belongs in the standard library :-) Thanks for the help with this! Maybe I'm overestimating how often people need this walkup function. Matt From velvetyuan at gmail.com Mon May 4 12:14:28 2009 From: velvetyuan at gmail.com (Allan Yuan) Date: Tue, 5 May 2009 00:14:28 +0800 Subject: Your confirmation is required to join the Python-list mailing list In-Reply-To: References: Message-ID: <7be934440905040914t699a0e70s62671f299961ac6@mail.gmail.com> Hi, I just wanna know how to set SYSTEM variables and USER variables of windows, but got no way. Firstly I thought "os.environ + os.system" may work well, but found no way to let "os.environ" run to retrive USER variables. Then I tried win32api, finding the GetEnvironmentVariables() mixing SYSTEM and USER variables up, and SetEnvironmentVariables() failing to add variables. Could you help me, please? Thanks a lot. -------------- next part -------------- An HTML attachment was scrubbed... URL: From aahz at pythoncraft.com Mon May 4 12:15:18 2009 From: aahz at pythoncraft.com (Aahz) Date: 4 May 2009 09:15:18 -0700 Subject: Code works fine except... References: <8d4ec1df-dddb-469a-99a1-695152db7c04@n4g2000vba.googlegroups.com> Message-ID: In article <8d4ec1df-dddb-469a-99a1-695152db7c04 at n4g2000vba.googlegroups.com>, Ross wrote: > >def test_round_robin(players, rounds, courts, doubles = False): > players = range(players) > for week in round_robin(players,rounds,courts): > if doubles == True: > doubles_week = len(week)/2.0 > byes = doubles_week - courts Side note: thou shalt indent four spaces, no more, no fewer For more info, see PEP 8. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "It is easier to optimize correct code than to correct optimized code." --Bill Harlan From aahz at pythoncraft.com Mon May 4 12:25:17 2009 From: aahz at pythoncraft.com (Aahz) Date: 4 May 2009 09:25:17 -0700 Subject: find sublist inside list References: Message-ID: In article , =?ISO-8859-1?Q?Matthias_Gall=E9?= wrote: > >My problem is to replace all occurrences of a sublist with a new element. > >Example: >Given ['a','c','a','c','c','g','a','c'] I want to replace all >occurrences of ['a','c'] by 6 (result [6,6,'c','g',6]). What's your goal? After you do this once, you cannot repeat the operation with a different sublist because you are not tracking the source of the numbers. You might look into standard compression algorithms for information about how to accomplish this. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "It is easier to optimize correct code than to correct optimized code." --Bill Harlan From oshecho at gmail.com Mon May 4 12:58:57 2009 From: oshecho at gmail.com (Echo) Date: Mon, 4 May 2009 12:58:57 -0400 Subject: How is the start of my server monitoring code? Message-ID: <910885da0905040958n1b62361ei4633d2b86fb3adae@mail.gmail.com> I just started a project to monitor servers(load, memory, processes, etc) via ssh(using?paramiko). And I was hoping to get some input on the design of my project, how pythonic it is, etc. It is quite basic right now. But it is currently able to get load and memory stats from any number of servers. I haven't started the logging to a DB or the GUI for it yet. I also haven't touched on error handling at all either. My reasons for making my own server monitoring project: -It's fun -Low requirements (only python, paramiko, and pycrypto 1.9+ is required) -Nothing to install on the servers -Easy to customize You can view the code at: http://github.com/brandonsinger/pywatchlmn/blob/43afb593a891a3d25f3cd9910c5829867bbe229d/monitor.py The project page is at: http://github.com/brandonsinger/pywatchlmn/tree/master -- -Brandon Singer From v+python at g.nevcal.com Mon May 4 13:09:42 2009 From: v+python at g.nevcal.com (Glenn Linderman) Date: Mon, 04 May 2009 10:09:42 -0700 Subject: [ANN] regobj - Pythonic object-based access to the Windows Registry In-Reply-To: <1241361308.4359.9.camel@mango> References: <1241361308.4359.9.camel@mango> Message-ID: <49FF2156.6010909@g.nevcal.com> On approximately 5/3/2009 7:35 AM, came the following characters from the keyboard of Ryan Kelly: > Hi All, > > I've just released the results of a nice Sunday's coding, inspired by > one too many turns at futzing around with the _winreg module. The > "regobj" module brings a convenient and clean object-based API for > accessing the Windows Registry. Sounds very interesting, Ryan. Just a couple questions about the dictionary interface. > If a subkey is assigned a dictionary, the structure of that dictionary > is copied into the subkey. Scalar values become key values, while > nested dictionaries create subkeys: > > >>> HKCU.Software.MyTests = {"val1":7, "stuff":{"a":1,"c":2,"e":3}} > >>> [v.name for v in HKCU.Software.MyTests.values()] > ['val1'] > >>> [k.name for k in HKCU.Software.MyTests.subkeys()] > ['stuff'] > >>> len(HKCU.Software.MyTests.stuff) > 3 > > Any other value assigned to a subkey will become the default value for > that key (i.e. the value with name ""): > > >>> HKCU.Software.MyTests = "dead parrot" > >>> HKCU.Software.MyTests[""].data > u'dead parrot' I could see how you could map numbers to DWORD, 2.x str/3.x bytes to binary, and 2.x unicode/3.x str to REG_SZ. But you don't document such a translation, so it is unclear exactly what you actually do. This would be somewhat weird in 2.x, though, where str commonly would want to map to String rather than Binary. It seems almost necessary to add an explanation of whatever mapping is presently done, to have complete documentation. Thinking more, I wonder if it is possible to create objects of type Value to put in the dictionary to allow specification of multistring and expandablestring Registry types, but then the dictionary name would have to be redundant with the name attribute in the Value object. Maybe a nameless value type could be invented, with just type and data attributes? Then the dictionary could be populated with numbers (mapped to DWORD) str or unicode items (mapped to String), [3.x only] bytes (mapped to Binary), or nameless value objects (which map to their internal types). This would allow a bidirectional conversion between dictionaries and registry keys... when asking for the value of a key, one could return a dictionary of nameless value types... and allow iterations over that. Subkeys could be a tuple with the type of key, and the value of a key object. Well, these are just thoughts. I don't know if they would increase or decrease the Pythonicity of your design, which certainly sounds better than using _winreg, to me. -- Glenn -- http://nevcal.com/ =========================== A protocol is complete when there is nothing left to remove. -- Stuart Cheshire, Apple Computer, regarding Zero Configuration Networking From aahz at pythoncraft.com Mon May 4 13:17:05 2009 From: aahz at pythoncraft.com (Aahz) Date: 4 May 2009 10:17:05 -0700 Subject: replacing numbers in LARGE MATRIX with criterium in 2 columns (a--> b) References: <68d22002-fc0a-4590-9395-c78b6ee412b2@r34g2000vba.googlegroups.com> Message-ID: In article <68d22002-fc0a-4590-9395-c78b6ee412b2 at r34g2000vba.googlegroups.com>, Alexzive wrote: > >I have this matrix [20*4 - but it could be n*4 , with n~100,000] in >file "EL_list" like this: Take a look at NumPy -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "It is easier to optimize correct code than to correct optimized code." --Bill Harlan From detlev at die-offenbachs.de Mon May 4 13:29:36 2009 From: detlev at die-offenbachs.de (Detlev Offenbach) Date: Mon, 04 May 2009 19:29:36 +0200 Subject: IDE for python 2.6 (auto complete) References: <6e840938-6180-44a8-8a1e-7f1660cfa2af@n8g2000vbb.googlegroups.com> Message-ID: Mike Driscoll wrote: > On May 4, 10:13?am, Soumen banerjee wrote: >> Hello, >> I have just installed and run python 2.6.2 from the sources available >> on the website. I notice that SPE (the editor which i used with python >> 2.5) is not displaying some of the functions new in 2.6 as >> autocomplete options. Is there any IDE with support for autocomplete >> in python 2.6 with all the newer functions included? > > Wingware probably does. You should just submit a bug report to Stani > though. He can probably fix SPE for you. And eric4 does this as well. Detlev -- Detlev Offenbach detlev at die-offenbachs.de From Florian.Wollenschein at FernUni-Hagen.de Mon May 4 13:31:38 2009 From: Florian.Wollenschein at FernUni-Hagen.de (Florian Wollenschein) Date: Mon, 04 May 2009 19:31:38 +0200 Subject: Open a dialog from MainWindow - pyQT4 - Beginner :-) Message-ID: Dear folks, I'm just working on a pyQT4 version of my txt to html converter thc (see listick.org for details). I created the MainWindow with QT Designer and then converted it to .py with pyuic4. It works well so far. Then I created a new UI for my abtDialog (about dialog for my application). I did the same as with the MainWindow, so I have two UIs and two *_ui.py files now. How can I open the dialog from my main application class? Here's part of the code I'm using: ... self.connect(self.actionAbout, QtCore.SIGNAL("triggered()"), self.OpenAbout) ... def OpenAbout(self): pass ... The OpenAbout function should open the dialog. I've already tried a abtDialog.show() after creating it via abtDialog = ThcAboutDialog() (ThcAboutDialog is the class for this dlg). Any ideas? Thanks, Listick Lorch http://www.listick.org PS: Keep in mind that I'm quite a beginner in the field of python and qt... From mail at johnohagan.com Mon May 4 13:33:40 2009 From: mail at johnohagan.com (John O'Hagan) Date: Mon, 4 May 2009 17:33:40 +0000 Subject: find sublist inside list In-Reply-To: <79b79e730905040613y5ba8e36wb27e66260f06247@mail.gmail.com> References: <200905041301.36962.mail@johnohagan.com> <79b79e730905040613y5ba8e36wb27e66260f06247@mail.gmail.com> Message-ID: <200905041733.41172.mail@johnohagan.com> On Mon, 4 May 2009, Francesco Guerrieri wrote: > On Mon, May 4, 2009 at 3:01 PM, John O'Hagan wrote: > > On Mon, 4 May 2009, Matthias Gall? wrote: > > > Hi. > > > > > > My problem is to replace all occurrences of a sublist with a new > > > element. > > > > > > Example: > > > Given ['a','c','a','c','c','g','a','c'] I want to replace all > > > occurrences of ['a','c'] by 6 (result [6,6,'c','g',6]). > > > > li=['a', 'c', 'a', 'c', 'c', 'g', 'a', 'c'] > > for i in range(len(li)): > > if li[i:i + 2] == ['a', 'c']: > > li[i:i + 2] = ['6'] > > > > HTH, > > > > John > > Beware that you are mutating the list you are iterating over. That could > lead to some strange bugs (for instance if you replaced the deleted items > with a longer sequence, the range(len(li)) would still go up to the > original lenght). > It is better to modify a new list instead. Eg you could append to a new > list. [...] Quite right, while it happens to work in this particular example, as you and MRAB point out, it's generally dangerous (and in fact this one silently and uselessly iterates over the last couple of indexes which no longer exist); a new list could be created like this: index=0 newli=[] while index <766cdhF1bk2ipU1@mid.uni-berlin.de> <5212a09e-37be-4e5f-b040-515913ccf2e8@p6g2000pre.googlegroups.com> <4b87a84a-eef0-42cb-91f7-6ade662bdd4d@t10g2000vbg.googlegroups.com> <767pt4F1ai5lsU1@mid.uni-berlin.de> Message-ID: > Which brings us backs to the "20 questions"-part of my earlier post. It > could be, but it could also be that processing takes seconds. Or it takes > so long that even concurrency won't help. Who knows? Probably the OP ;) Geremy Condra From grettke at gmail.com Mon May 4 14:06:06 2009 From: grettke at gmail.com (Grant Rettke) Date: Mon, 4 May 2009 11:06:06 -0700 (PDT) Subject: Personal recommendations for Python and Django on-boarding materials Message-ID: <61ac6511-0f9e-458d-b3f1-5b153939293f@r34g2000vba.googlegroups.com> Hi folks, >From one developer to another, I am looking for some personal recommendations on what are the best materials for fast tracking an on- boarding to Python and Django. I know how to program, get web apps and OO; this is the audience. I have found some good recommendations and stuff on Amazon and various websites, but if I could get some humans vouching for things directly that would be worth a lot more. Best wishes, Grant From jsanga at cox.net Mon May 4 14:12:41 2009 From: jsanga at cox.net (mzdude) Date: Mon, 4 May 2009 11:12:41 -0700 (PDT) Subject: find sublist inside list References: Message-ID: On May 4, 9:01?am, Matthias Gall? wrote: > bearophileH... at lycos.com wrote: > > John O'Hagan: > >> li=['a', 'c', 'a', 'c', 'c', 'g', 'a', 'c'] > >> for i ?in range(len(li)): > >> ? ? if li[i:i + 2] == ['a', 'c']: > >> ? ? ? ? li[i:i + 2] = ['6'] > > > Oh well, I have done a mistake, it seems. > > Another solution then: > > >>>> 'acaccgac'.replace("ac", chr(6)) > > '\x06\x06cg\x06' > > > Bye, > > bearophile > > Thanks bearophile and John for your quick answers. > Unfortunately, the int that can replace a sublist can be > 255, but > John's answer looks simple and good enough for me. I will use it as a > starting point. > substring isn't limited to 0..255 >>> substring = "\0x%d\0x%d" % (257,257) >>> 'acaccgac'.replace("ac", substring) '\x00x257\x00x257\x00x257\x00x257cg\x00x257\x00x257' From grahamdick77 at gmail.com Mon May 4 14:15:13 2009 From: grahamdick77 at gmail.com (grahamdick77 at gmail.com) Date: Mon, 4 May 2009 11:15:13 -0700 (PDT) Subject: improved search algorithm Message-ID: Hi I have an excel file that is read into python (8000 rows) from csv import reader, writer incsv = reader(open(MY_FILE), dialect='excel') keys = incsv.next() There are mixed datatypes. the last column contains a cumulative frequency running in order 0.0000 to 1.0000 for the 8000 rows for a loop of 100,000 times I want to take a new random number each time and find the row with the next biggest cumulative frequency value Here's my current (pseudo)code: for 1 to 100000 myRand = random.random() for line in incsv: if float(line[-1]) > myRand: resline = [] for item in line: try: i = int(item) except ValueError: try: i = float(item) except ValueError: i = item resline.append(i) #Here we construct a dict of pair values: #{'ID':18,... res = dict(zip(keys,resline)) break else: continue #do some stuff with res I'm scanning over each line of the csv and deciding which row to select (100k times) this is just not very efficient How can i improve this code. for line in incsv: if float(line[-1]) > random.random(): I can use numpy etc. whatever From bearophileHUGS at lycos.com Mon May 4 14:26:25 2009 From: bearophileHUGS at lycos.com (bearophileHUGS at lycos.com) Date: Mon, 4 May 2009 11:26:25 -0700 (PDT) Subject: Self function References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> <40fbffe5-a9dc-4a8c-a239-fa546c5ff2ef@z19g2000vbz.googlegroups.com> Message-ID: <202a05cd-3ce4-41fb-943b-5fd3f18d9bbb@z5g2000vba.googlegroups.com> Steve Howell: >two methods with almost identical names, where one function is the public interface and then another method that does most of the recursion.< Thanks Guido & Walter both Python and D support nested functions, so in such situations I put the recursive function inside the "public interface" function/method. Recursion is a high-level way to represent certain kinds of algorithms in a short and readable way (when data structures are nested, the most natural algorithms that work on them are recursive). But in Python function calls are slow, the maximum level of nested calls is limited (and it can't grow too much), so this has sometimes forced me to manually convert recursive functions into iterative ones with a stack. This is silly for a supposed high-level language. The bad support for recursivity is one of the few faults of Python. Bye, bearophile From qhfgva at gmail.com Mon May 4 14:27:13 2009 From: qhfgva at gmail.com (qhfgva) Date: Mon, 4 May 2009 11:27:13 -0700 (PDT) Subject: pychecker vs pychecker2 Message-ID: For my edification I was looking through the source code of pychecker. I noticed that there was also a pychecker2 directory (ubuntu). The pychecker command line tool points to pychecker (w/out the 2). Does anyone know off the top of their head what this second directory is about? thanks qhfgva From umut.tabak at gmail.com Mon May 4 14:42:04 2009 From: umut.tabak at gmail.com (utab) Date: Mon, 4 May 2009 11:42:04 -0700 (PDT) Subject: change some lines from a read file Message-ID: <0e624af8-d84c-4117-ac93-0cc4a4df4d02@s20g2000vbp.googlegroups.com> Dear all, I have to change some lines from a template file, which is rather long to paste here, but I would like to make some parts of some lines optional with my command line arguments but I could not see this directly, I can count the line numbers and decide on this basis to decide the lines to be configured to my will. More specifically, say, I have a that file includes this is an example python file to play around . . . some lines . . . . -> an option line for example. . . . -> another option line so on. and execute the script ./myScript option1 option2 so that the options at correct locations will be written. Any other options for this simple job that i can improve my Python a bit as well. Best, Umut From google at mrabarnett.plus.com Mon May 4 14:45:23 2009 From: google at mrabarnett.plus.com (MRAB) Date: Mon, 04 May 2009 19:45:23 +0100 Subject: improved search algorithm In-Reply-To: References: Message-ID: <49FF37C3.2040809@mrabarnett.plus.com> grahamdick77 at gmail.com wrote: > Hi > > I have an excel file that is read into python (8000 rows) > > from csv import reader, writer > incsv = reader(open(MY_FILE), dialect='excel') > keys = incsv.next() > > There are mixed datatypes. > > the last column contains a cumulative frequency running in order > 0.0000 to 1.0000 for the 8000 rows > > for a loop of 100,000 times I want to take a new random number each > time and find the row with the next biggest cumulative frequency value > > Here's my current (pseudo)code: > > for 1 to 100000 > > myRand = random.random() > for line in incsv: > if float(line[-1]) > myRand: > resline = [] > for item in line: > try: > i = int(item) > except ValueError: > try: > i = float(item) > except ValueError: > i = item > resline.append(i) > #Here we construct a dict of pair values: > #{'ID':18,... > res = dict(zip(keys,resline)) > break > else: > continue > > #do some stuff with res > > > > > I'm scanning over each line of the csv and deciding which row to > select (100k times) this is just not very efficient > > How can i improve this code. > for line in incsv: > if float(line[-1]) > random.random(): > > I can use numpy etc. whatever > Here's a suggestion: Construct the dicts for all the rows, stored in a list. Construct a list of just the cumulative frequencies. For each random value, use the bisect module to search for the value in the cumulative frequencies list. This will return an index which you can then use on your list of dicts. From arnodel at googlemail.com Mon May 4 14:51:03 2009 From: arnodel at googlemail.com (Arnaud Delobelle) Date: Mon, 04 May 2009 19:51:03 +0100 Subject: Self function References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> <40fbffe5-a9dc-4a8c-a239-fa546c5ff2ef@z19g2000vbz.googlegroups.com> <202a05cd-3ce4-41fb-943b-5fd3f18d9bbb@z5g2000vba.googlegroups.com> Message-ID: bearophileHUGS at lycos.com writes: > Steve Howell: >>two methods with almost identical names, where one function is the >>public interface and then another method that does most of the >>recursion.< > > Thanks Guido & Walter both Python and D support nested functions, so > in such situations I put the recursive function inside the "public > interface" function/method. Yes, this is a common idiom for me: def public_function(a, b): def rec(): ... return rec() E.g, to continue with the factorial toy example (of course for this particular example and in this particular language, a loop would be more appropriate): def fac(n): def rec(n, acc): if n <= 1: return acc else: return rec(n - 1, n*acc) return rec(n, 1) This is tail recursive, but Python does not optimise tail-calls so there is not much point. > Recursion is a high-level way to represent certain kinds of algorithms > in a short and readable way (when data structures are nested, the most > natural algorithms that work on them are recursive). But in Python > function calls are slow, the maximum level of nested calls is limited > (and it can't grow too much), so this has sometimes forced me to > manually convert recursive functions into iterative ones with a stack. > This is silly for a supposed high-level language. The bad support for > recursivity is one of the few faults of Python. Bearophile, there is a thread on python-ideas about tail-call optimization at the moment. -- Arnaud From google at mrabarnett.plus.com Mon May 4 14:59:37 2009 From: google at mrabarnett.plus.com (MRAB) Date: Mon, 04 May 2009 19:59:37 +0100 Subject: change some lines from a read file In-Reply-To: <0e624af8-d84c-4117-ac93-0cc4a4df4d02@s20g2000vbp.googlegroups.com> References: <0e624af8-d84c-4117-ac93-0cc4a4df4d02@s20g2000vbp.googlegroups.com> Message-ID: <49FF3B19.7070907@mrabarnett.plus.com> utab wrote: > Dear all, > > I have to change some lines from a template file, which is rather long > to paste here, but I would like to make some parts of some lines > optional with my command line arguments but I could not see this > directly, I can count the line numbers and decide on this basis to > decide the lines to be configured to my will. > > More specifically, say, I have a that file includes > > this is an example python file to play around > . > . > . > some lines > . > . > . > . -> an option line for example. > . > . > . > -> another option line so on. > > and execute the script > ./myScript option1 option2 > > so that the options at correct locations will be written. > > Any other options for this simple job that i can improve my Python a > bit as well. > You could put special markers around the optional parts in the template file. Your script would remove the markers from around the optional parts it wants to keep and then remove any remaining unwanted optional parts and their markers. From bearophileHUGS at lycos.com Mon May 4 15:12:19 2009 From: bearophileHUGS at lycos.com (bearophileHUGS at lycos.com) Date: Mon, 4 May 2009 12:12:19 -0700 (PDT) Subject: Self function References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> <40fbffe5-a9dc-4a8c-a239-fa546c5ff2ef@z19g2000vbz.googlegroups.com> <202a05cd-3ce4-41fb-943b-5fd3f18d9bbb@z5g2000vba.googlegroups.com> Message-ID: Arnaud Delobelle: > def fac(n): > ? ? def rec(n, acc): > ? ? ? ? if n <= 1: > ? ? ? ? ? ? return acc > ? ? ? ? else: > ? ? ? ? ? ? return rec(n - 1, n*acc) > ? ? return rec(n, 1) Right, that's another way to partially solve the problem I was talking about. (Unfortunately the performance in Python is significantly lower. I can use that in D). > Bearophile, there is a thread on python-ideas about tail-call > optimization at the moment. Someday I'll have to start following those mailing lists... But I am not interested in such optimization. It's not going to help me significantly. Most times I use recursion, I think it can't be optimized away by simple means (think about a visit to a binary tree). Bye, bearophile From aahz at pythoncraft.com Mon May 4 15:50:18 2009 From: aahz at pythoncraft.com (Aahz) Date: 4 May 2009 12:50:18 -0700 Subject: Self function References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> <202a05cd-3ce4-41fb-943b-5fd3f18d9bbb@z5g2000vba.googlegroups.com> Message-ID: In article , wrote: >Arnaud Delobelle: >> >> Bearophile, there is a thread on python-ideas about tail-call >> optimization at the moment. > >Someday I'll have to start following those mailing lists... >But I am not interested in such optimization. It's not going to help >me significantly. Most times I use recursion, I think it can't be >optimized away by simple means (think about a visit to a binary tree). When have you ever had a binary tree a thousand levels deep? Consider how big 2**1000 is... -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "It is easier to optimize correct code than to correct optimized code." --Bill Harlan From arnodel at googlemail.com Mon May 4 15:51:45 2009 From: arnodel at googlemail.com (Arnaud Delobelle) Date: Mon, 04 May 2009 20:51:45 +0100 Subject: Personal recommendations for Python and Django on-boarding materials References: <61ac6511-0f9e-458d-b3f1-5b153939293f@r34g2000vba.googlegroups.com> Message-ID: Grant Rettke writes: > Hi folks, > > From one developer to another, I am looking for some personal > recommendations on what are the best materials for fast tracking an on- > boarding to Python and Django. > > I know how to program, get web apps and OO; this is the audience. > > I have found some good recommendations and stuff on Amazon and various > websites, but if I could get some humans vouching for things directly > that would be worth a lot more. > > Best wishes, > > Grant I've learnt both Python and Django using the tutorials, the docs and lots of experimentation. Django now have a book [1] which might be useful to read. [1] http://djangobook.com/ -- Arnaud From anthra.norell at bluewin.ch Mon May 4 16:06:51 2009 From: anthra.norell at bluewin.ch (Anthra Norell) Date: Mon, 04 May 2009 22:06:51 +0200 Subject: change some lines from a read file In-Reply-To: <0e624af8-d84c-4117-ac93-0cc4a4df4d02@s20g2000vbp.googlegroups.com> References: <0e624af8-d84c-4117-ac93-0cc4a4df4d02@s20g2000vbp.googlegroups.com> Message-ID: <49FF4ADB.5020101@bluewin.ch> utab wrote: > Dear all, > > I have to change some lines from a template file, which is rather long > to paste here, but I would like to make some parts of some lines > optional with my command line arguments but I could not see this > directly, I can count the line numbers and decide on this basis to > decide the lines to be configured to my will. > > More specifically, say, I have a that file includes > > this is an example python file to play around > . > . > . > some lines > . > . > . > . -> an option line for example. > . > . > . > -> another option line so on. > > and execute the script > ./myScript option1 option2 > > so that the options at correct locations will be written. > > Any other options for this simple job that i can improve my Python a > bit as well. > > Best, > Umut > -- > http://mail.python.org/mailman/listinfo/python-list > > Your description is not explicit enough to convey your intention. If your template file is too long to post, post a short representative section, an input data sample and a hand-edited sample of the output data you want to generate. You will get more and better advice. . Frederic From namekuseijin at gmail.com Mon May 4 16:18:10 2009 From: namekuseijin at gmail.com (namekuseijin) Date: Mon, 4 May 2009 13:18:10 -0700 (PDT) Subject: yet another list comprehension question References: <594765c1-e5ed-492d-b398-a8ad00c6d6ad@x6g2000vbg.googlegroups.com> Message-ID: On May 4, 9:15?am, David Robinow wrote: > On Mon, May 4, 2009 at 2:33 AM, namekuseijin > > wrote: > >>>> ls = [(1,2), (3,4), (5, None), (6,7), (8, None)] > >>>> [(x,y) for (x,y) in ls if y] > > [(1, 2), (3, 4), (6, 7)] > > Nope. That filters out 0 as well as None. Not what the OP asked for. True. I'm still a C programmer at heart I guess. ah, the flexibility of 0... ;) From tim.wintle at teamrubber.com Mon May 4 16:24:36 2009 From: tim.wintle at teamrubber.com (Tim Wintle) Date: Mon, 04 May 2009 21:24:36 +0100 Subject: Self function In-Reply-To: References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> <40fbffe5-a9dc-4a8c-a239-fa546c5ff2ef@z19g2000vbz.googlegroups.com> <202a05cd-3ce4-41fb-943b-5fd3f18d9bbb@z5g2000vba.googlegroups.com> Message-ID: <1241468676.7670.11.camel@tim-laptop> On Mon, 2009-05-04 at 19:51 +0100, Arnaud Delobelle wrote: > > Bearophile, there is a thread on python-ideas about tail-call > optimization at the moment. Oooh - haven't noticed that (and don't have time to follow it), but has anyone seen the results I got a week or so ago from briefly playing with a couple of simple optimisations: I was amazed how much you could improve performance by not jumping all over the stack -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 196 bytes Desc: This is a digitally signed message part URL: From bearophileHUGS at lycos.com Mon May 4 16:25:59 2009 From: bearophileHUGS at lycos.com (bearophileHUGS at lycos.com) Date: Mon, 4 May 2009 13:25:59 -0700 (PDT) Subject: Self function References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> <202a05cd-3ce4-41fb-943b-5fd3f18d9bbb@z5g2000vba.googlegroups.com> Message-ID: <8c18a63a-8ca5-44bd-9b47-c1247a8f1a97@v4g2000vba.googlegroups.com> Aahz: > When have you ever had a binary tree a thousand levels deep? Yesterday. >Consider how big 2**1000 is...< You are thinking just about complete binary trees. But consider that a topology like a single linked list (every node has 1 child, and they are chained) is a true binary tree still. Bye, bearophile From ross.jett at gmail.com Mon May 4 16:27:27 2009 From: ross.jett at gmail.com (Ross) Date: Mon, 4 May 2009 13:27:27 -0700 (PDT) Subject: Code works fine except... References: <8d4ec1df-dddb-469a-99a1-695152db7c04@n4g2000vba.googlegroups.com> Message-ID: <58fea998-acf1-46b5-9328-bc3cbd74f115@n4g2000vba.googlegroups.com> On May 4, 12:15?pm, a... at pythoncraft.com (Aahz) wrote: > In article <8d4ec1df-dddb-469a-99a1-695152db7... at n4g2000vba.googlegroups.com>, > > Ross ? wrote: > > >def test_round_robin(players, rounds, courts, doubles = False): > > ? ?players = range(players) > > ? ?for week in round_robin(players,rounds,courts): > > ? ? ? ?if doubles == True: > > ? ? ? ? ? ? ? ?doubles_week = len(week)/2.0 > > ? ? ? ? ? ? ? ?byes = doubles_week - courts > > Side note: thou shalt indent four spaces, no more, no fewer > > For more info, see PEP 8. > -- > Aahz (a... at pythoncraft.com) ? ? ? ? ? <*> ? ? ? ?http://www.pythoncraft.com/ > > "It is easier to optimize correct code than to correct optimized code." > --Bill Harlan Yes... I know this. Unfortunately, copy/pasting my code from the IDE into the google group messes with indentations. From davea at ieee.org Mon May 4 16:38:41 2009 From: davea at ieee.org (Dave Angel) Date: Mon, 04 May 2009 16:38:41 -0400 Subject: AutoComplete in C++ Editor for Python In-Reply-To: <10fef506-2c39-418c-82e8-5c0c05a19e5c@s31g2000vbp.googlegroups.com> References: <10fef506-2c39-418c-82e8-5c0c05a19e5c@s31g2000vbp.googlegroups.com> Message-ID: <49FF5251.2010303@ieee.org> flamz3d at gmail.com wrote: > On May 3, 3:14 pm, Dave Angel wrote: > >> flam... at gmail.com wrote: >> >>> Hello, >>> I am embedding python support in my C++ application and was looking at >>> adding "Intellisense" or "AutoComplete" support. >>> >>> I found a way to do it using the "dir" function, but this creates a >>> problem. Here's why. Let's say I have the following code in my editor: >>> >>> import sys >>> x =ys >>> >>> Now, I would like to get all attributes of the object called 'x'. I >>> can "instrument" the code and add "print dir(x)" at the end, >>> temporarily redirect the python output to a string and execute the >>> code. >>> >>> But this is not safe: I do NOT want to execute the code while the user >>> is typing! >>> >>> Is there a way to "compile" the python code and get access to the >>> symbol table from that compiled block? >>> >>> Did anybody ever implement AutoComplete in a editor for Python? >>> >>> cheers. >>> >> Several editors for Python support auto-complete, to one extent or >> another. The only one I have experience with is Komodo. Komodo runs in >> a separate process, so it doesn't suffer from the problems of having two >> gui event-loops in the same process, and other similar problems. It >> also won't be executing code that might have side effects in the child >> process. >> >> The downside is that in order to do auto-complete, it has to figure it >> out from other clues. From the docs, and from reading, and from >> experiementing, I believe that it uses two approaches. One approach is >> a set of language files which try to describe all the symbols in the >> standard language and library. They have one for each release (2.4, >> 2.5, ...) Theoretically, you could add your own for other libraries. >> Second approach is to parse the code that's visible to it. That parsing >> is well done, and surprisingly quick, but there are lots of tricks a >> developer might use that can fool it. For example, wxPython lets you >> import just one symbol, and lots more appear magically. It's no big >> deal, they have code structured one way, but the interface is very >> different. Catch is that code completion frequently gets fooled by these. >> >> I'd point out that if you do walk the dictionaries at run time, you'll >> get different results when you do it with nothing running than if you do >> a strictly static analysis. So some things might only show up if you've >> stepped into the function with the magic going on. >> >> Simplest example I can come up with of something a static analysis won't >> spot: An instance object obj may have some number of attributes >> assigned the the __init__() method. But it could also have new fields >> added by anyone with a referent into it. >> >> There is a Python parser in module ast. Perhaps that'd be some help. >> I've not used it, so can't give you any specifics. >> >> DaveA >> > > > I think it might be possible for me to figure out how to do > AutoCompletion using the symtable object. But I am having difficulty > making sense of it. > > In my tests, I was use the following Python code: > > [code] > import sys > x=s > [/code] > > The idea is to get the type of the variable x > > To attempt this, I use the following C code (my editor is written in > C) > > symtable* st =y_SymtableString ( "import sys > \nx=s,"",Py_single_input); > > PyObject *name, *value; > Py_ssize_t pos =; > while (PyDict_Next(st->st_symbols, &pos, &name, &value)) > { > ... > } > > > > Using this code, I can get information like the name of the symbol > (x), but I can't figure out how to get the type. If I knew how to get > this it would solve 99% of my problems :) > > Any ideas? > > There's a type() function as a builtin. It returns a string showing the type of any object you pass it. I have my doubts about the 99%; I'd say more like 1%. Objects have to be instantiated to have a type, there's no declarations. And a function has a type, but in order to see the result type through introspection, you'd have to run it. And before you could do that, you'd have to guess its parameter types. And hope it didn't have any serious side effects. I can't help with the C++ code, since I've never written any that use the Python runtime, but perhaps you can experiment with a Python version, till you see what is and is-not possible. Look at the following in Python: import sys dict = sys.__dict__ for sym in dict: print "**", sym, type(dict[sym]), str(dict[sym]) Now, for many of these sym values, sym.__dict__ could similarly be analyzed. So you might write a recursive generator that gave you a list of symbols. From davea at ieee.org Mon May 4 16:42:42 2009 From: davea at ieee.org (Dave Angel) Date: Mon, 04 May 2009 16:42:42 -0400 Subject: IDE for python 2.6 (auto complete) In-Reply-To: References: Message-ID: <49FF5342.8060901@ieee.org> Soumen banerjee wrote: > Hello, > I have just installed and run python 2.6.2 from the sources available > on the website. I notice that SPE (the editor which i used with python > 2.5) is not displaying some of the functions new in 2.6 as > autocomplete options. Is there any IDE with support for autocomplete > in python 2.6 with all the newer functions included? > > No idea if it's "all", but Komodo 5.1.1 has been updated for 2.6. From piet at cs.uu.nl Mon May 4 16:44:55 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Mon, 04 May 2009 22:44:55 +0200 Subject: Threaded alternatives to smtplib? References: <767h7tF1b8q8gU1@mid.uni-berlin.de> Message-ID: >>>>> Alex Jurkiewicz (AJ) wrote: >AJ> def threadProcessRecipient(): [snip] >AJ> if __name__ == '__main__': >AJ> THREADS = [] >AJ> for i in range(CONCURRENCY): >AJ> THREADS.append(threading.Thread(target=threadProcessRecipient)) >AJ> for thread in THREADS: >AJ> thread.run() You should use thread.start(), not thread.run(). When you use run(), it will be sequential execution, as you experience. With start() you get concurrency -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From davea at ieee.org Mon May 4 16:47:18 2009 From: davea at ieee.org (Dave Angel) Date: Mon, 04 May 2009 16:47:18 -0400 Subject: How to walk up parent directories? In-Reply-To: References: <87ocu9k6c4.fsf@benfinney.id.au> Message-ID: <49FF5456.9050808@ieee.org> Matthew Wilson wrote: > On Sun 03 May 2009 09:24:59 PM EDT, Ben Finney wrote: > >> Not every simple function belongs in the standard library :-) >> > > Thanks for the help with this! Maybe I'm overestimating how often > people need this walkup function. > > Matt > > Look at os.path.normpath(os.path.join( name, "..")) From norseman at hughes.net Mon May 4 16:54:26 2009 From: norseman at hughes.net (norseman) Date: Mon, 04 May 2009 13:54:26 -0700 Subject: change some lines from a read file In-Reply-To: <49FF4ADB.5020101@bluewin.ch> References: <0e624af8-d84c-4117-ac93-0cc4a4df4d02@s20g2000vbp.googlegroups.com> <49FF4ADB.5020101@bluewin.ch> Message-ID: <49FF5602.4060702@hughes.net> Anthra Norell wrote: > utab wrote: >> Dear all, >> >> I have to change some lines from a template file, which is rather long >> to paste here, but I would like to make some parts of some lines >> optional with my command line arguments but I could not see this >> directly, I can count the line numbers and decide on this basis to >> decide the lines to be configured to my will. >> >> More specifically, say, I have a that file includes >> >> this is an example python file to play around >> . >> . >> . >> some lines >> . >> . >> . >> . -> an option line for example. >> . >> . >> . >> -> another option line so on. >> >> and execute the script >> ./myScript option1 option2 >> >> so that the options at correct locations will be written. >> >> Any other options for this simple job that i can improve my Python a >> bit as well. >> >> Best, >> Umut >> -- >> http://mail.python.org/mailman/listinfo/python-list >> >> > Your description is not explicit enough to convey your intention. If > your template file is too long to post, post a short representative > section, an input data sample and a hand-edited sample of the output > data you want to generate. You will get more and better advice. . > > Frederic > > -- > http://mail.python.org/mailman/listinfo/python-list > =================================================== Assuming the total number of changes is not large, put OPTION1 where you want it to go in the text put OPTION2 (same) import sys if len(sys.argv) >1: OPTION1 = sys.argv[1] else: OPTION1 = "some default stuff" similar for OPTION2 if len(sys.argv) >2: like above and any others print section1+OPTION1+section2+OPTION2.... or just using the line count (in place of "print sect..") line_number= 0 for i in template: if line_number matches line number to place Opt1 print OPTION1 elif line_number matches v2 print OPTION2 and so forth else: print i (line from template NOT being changed) line_number += Line_number you can group line number and replacement on command line runprogram (43,"use this instead") (56,.... and parse inside program before opening template one thing to watch out for: for i in file... often returns just one character at a time, meaning you will have to track EOL's yourself OR import the readline and use it to stand a better chance of getting what you expect from a read file function. Today: 20090504 Logic outline, No particular version HTH From norseman at hughes.net Mon May 4 16:54:46 2009 From: norseman at hughes.net (norseman) Date: Mon, 04 May 2009 13:54:46 -0700 Subject: change some lines from a read file In-Reply-To: <49FF4ADB.5020101@bluewin.ch> References: <0e624af8-d84c-4117-ac93-0cc4a4df4d02@s20g2000vbp.googlegroups.com> <49FF4ADB.5020101@bluewin.ch> Message-ID: <49FF5616.9070909@hughes.net> Anthra Norell wrote: > utab wrote: >> Dear all, >> >> I have to change some lines from a template file, which is rather long >> to paste here, but I would like to make some parts of some lines >> optional with my command line arguments but I could not see this >> directly, I can count the line numbers and decide on this basis to >> decide the lines to be configured to my will. >> >> More specifically, say, I have a that file includes >> >> this is an example python file to play around >> . >> . >> . >> some lines >> . >> . >> . >> . -> an option line for example. >> . >> . >> . >> -> another option line so on. >> >> and execute the script >> ./myScript option1 option2 >> >> so that the options at correct locations will be written. >> >> Any other options for this simple job that i can improve my Python a >> bit as well. >> >> Best, >> Umut >> -- >> http://mail.python.org/mailman/listinfo/python-list >> >> > Your description is not explicit enough to convey your intention. If > your template file is too long to post, post a short representative > section, an input data sample and a hand-edited sample of the output > data you want to generate. You will get more and better advice. . > > Frederic > > -- > http://mail.python.org/mailman/listinfo/python-list > =================================================== Assuming the total number of changes is not large, put OPTION1 where you want it to go in the text put OPTION2 (same) import sys if len(sys.argv) >1: OPTION1 = sys.argv[1] else: OPTION1 = "some default stuff" similar for OPTION2 if len(sys.argv) >2: like above and any others print section1+OPTION1+section2+OPTION2.... or just using the line count (in place of "print sect..") line_number= 0 for i in template: if line_number matches line number to place Opt1 print OPTION1 elif line_number matches v2 print OPTION2 and so forth else: print i (line from template NOT being changed) line_number += Line_number you can group line number and replacement on command line runprogram (43,"use this instead") (56,.... and parse inside program before opening template one thing to watch out for: for i in file... often returns just one character at a time, meaning you will have to track EOL's yourself OR import the readline and use it to stand a better chance of getting what you expect from a read file function. Today: 20090504 Logic outline, No particular version HTH Steve From clp2 at rebertia.com Mon May 4 16:58:04 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Mon, 4 May 2009 13:58:04 -0700 Subject: Self function In-Reply-To: <8c18a63a-8ca5-44bd-9b47-c1247a8f1a97@v4g2000vba.googlegroups.com> References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> <202a05cd-3ce4-41fb-943b-5fd3f18d9bbb@z5g2000vba.googlegroups.com> <8c18a63a-8ca5-44bd-9b47-c1247a8f1a97@v4g2000vba.googlegroups.com> Message-ID: <50697b2c0905041358i5ff7e3c4nf304174631c5f611@mail.gmail.com> On Mon, May 4, 2009 at 1:25 PM, wrote: > Aahz: >> When have you ever had a binary tree a thousand levels deep? > > Yesterday. > > >>Consider how big 2**1000 is...< > > You are thinking just about complete binary trees. > But consider that a topology like a single linked list (every node has > 1 child, and they are chained) is a true binary tree still. And who in their right mind would not use a *self-balancing* binary tree in that situation? Cheers, Chris -- http://blog.rebertia.com From piet at cs.uu.nl Mon May 4 17:01:12 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Mon, 04 May 2009 23:01:12 +0200 Subject: Is there is any way to send messages to chunk of emails ID's concurrently using smptlib References: <17ea2ba6-3f5d-4e7e-b05b-6d91dfbc5b36@k19g2000prh.googlegroups.com> Message-ID: >>>>> gganesh (g) wrote: >g> Hi friends, >g> I suppose sendmail() can send mails one by one ,how to send mails >g> concurrently , >g> It would be very grateful,if someone could point out a solution. There is a discussion about this in the thread `Threaded alternatives to smtplib?' -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From davea at ieee.org Mon May 4 17:10:11 2009 From: davea at ieee.org (Dave Angel) Date: Mon, 04 May 2009 17:10:11 -0400 Subject: SYSTEM and USER environment (was Your confirmation is required to join the Python-list mailing list) In-Reply-To: <7be934440905040914t699a0e70s62671f299961ac6@mail.gmail.com> References: <7be934440905040914t699a0e70s62671f299961ac6@mail.gmail.com> Message-ID: <49FF59B3.3030300@ieee.org> Allan Yuan wrote: > Hi, > I just wanna know how to set SYSTEM variables and USER variables of windows, > but got no way. > > Firstly I thought "os.environ + os.system" may work well, but found no way > to let "os.environ" run to retrive USER variables. > > Then I tried win32api, finding the GetEnvironmentVariables() mixing SYSTEM > and USER variables up, and SetEnvironmentVariables() failing to add > variables. > > Could you help me, please? > Thanks a lot. > > First, you need to put a meaningful subject line on a query to the list. Hopefully you also did the confirmation, so that you'll actually be getting the list emails. Next, your problem. SYSTEM and USER variables are a uniquely Windows concept, and as far as I know, have no direct counterpart in Python. These are really just names used in the Control Panel applet to refer to two sections of the registry which are used to define which environment variables a task will start with, if the task is started directly from Explorer. Tasks that are started by other tasks (eg. the command line) get environment variables as defined by the parent. So, there are at least three obvious ways a process gets started. One is by explorer, in which case it gets the environment described above. Two is by a DOS box, in which case it gets the environment variables according to the rules of the CMD shell. And third is from an arbitrary 3rd party process, in which case it's up to that developer. So, which are you trying to change? I'm suspecting you're interested in the Explorer version, for example that launches a program from a shortcut on the desktop, or via an association with a particular file extension. i don't know which registry variables are involved, but there are two registry keys, Try: hklm/SYSTEm/CurrentControlSet/Control/Session Manager/Environment for the SYSTEM environment variables and hkcu/Environment for the USER environment variables Experiment first with REGEDIT, then when you get the behavior you want, look up module _winreg (or winreg in Python 3.0) to do it programmatically. Notice that existing DOS boxes won't see these new variables, only things launched by Explorer or the equivalent. It's also possible you just want to set environment variables for a particular DOS box session. In that case, use a .BAT or .CMD file, in which a SET statement tells the shell what values to use next time it launches. From arnodel at googlemail.com Mon May 4 17:31:29 2009 From: arnodel at googlemail.com (Arnaud Delobelle) Date: Mon, 04 May 2009 22:31:29 +0100 Subject: Self function References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> <202a05cd-3ce4-41fb-943b-5fd3f18d9bbb@z5g2000vba.googlegroups.com> <8c18a63a-8ca5-44bd-9b47-c1247a8f1a97@v4g2000vba.googlegroups.com> Message-ID: bearophileHUGS at lycos.com writes: > Aahz: >> When have you ever had a binary tree a thousand levels deep? > > Yesterday. > > >>Consider how big 2**1000 is...< > > You are thinking just about complete binary trees. > But consider that a topology like a single linked list (every node has > 1 child, and they are chained) is a true binary tree still. In that case the following would not grow the stack, given tail-call optimization: def visit(node): print 'visiting', node if node.right is None: return visit(node.left) if node.left is not None: visit(node.left) return visit(node.right) -- Arnaud From rhodri at wildebst.demon.co.uk Mon May 4 17:54:08 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Mon, 04 May 2009 22:54:08 +0100 Subject: if statement, with function inside it: if (t = Test()) == True: In-Reply-To: References: <020183f4$0$20619$c3e8da3@news.astraweb.com> Message-ID: On Mon, 04 May 2009 15:25:44 +0100, Antoon Pardon wrote: > On 2009-04-24, Steven D'Aprano > wrote: >> On Fri, 24 Apr 2009 03:00:26 -0700, GC-Martijn wrote: >> >>> Hello, >>> >>> I'm trying to do a if statement with a function inside it. I want to >>> use >>> that variable inside that if loop , without defining it. >>> >>> def Test(): >>> return 'Vla' >>> >>> I searching something like this: >>> >>> if (t = Test()) == 'Vla': >>> print t # Vla >>> >>> or >>> >>> if (t = Test()): >>> print t # Vla >> >> Fortunately, there is no way of doing that with Python. This is one >> source of hard-to-debug bugs that Python doesn't have. > > I think this is an unfortunate consequence of choosing '=' for the > assignment. They could have chosen an other token to indicate an > assignment one that would have made the difference between an > assignment and a comparison more visually different and thus > bugs by using one while needing the other less hard to track > down. What token could be used and still be meaningful, though? Algol used ":=", which has most of the same problems as "=" (more, in my opinion, since it fools the eye more easily if you're scanning printed code quickly). Easily constructed arrows like "<=" or "<-" collide with different comparators. About all that's left that even vaguely implies assignment is "~", and it's no better. -- Rhodri James *-* Wildebeeste Herder to the Masses From gagsl-py2 at yahoo.com.ar Mon May 4 17:54:22 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Mon, 04 May 2009 18:54:22 -0300 Subject: find sublist inside list References: Message-ID: En Mon, 04 May 2009 15:12:41 -0300, mzdude escribi?: > substring isn't limited to 0..255 >>>> substring = "\0x%d\0x%d" % (257,257) >>>> 'acaccgac'.replace("ac", substring) > '\x00x257\x00x257\x00x257\x00x257cg\x00x257\x00x257' This isn't what you think it is. Look carefully: py> substring = "\0x%d\0x%d" % (257,257) py> len(substring) 10 py> list(substring) ['\x00', 'x', '2', '5', '7', '\x00', 'x', '2', '5', '7'] -- Gabriel Genellina From tjreedy at udel.edu Mon May 4 17:54:50 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 04 May 2009 17:54:50 -0400 Subject: Self function In-Reply-To: References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> Message-ID: bearophileHUGS at lycos.com wrote: > Another possible syntax: > > def fact(n): > return 1 if n <= 1 else n * return(n - 1) > > But I guess most people don't see this problem as important&common > enough to justify changing the language. Actually, I would like a way to refer to the current function from inside a function. but I would like support in the language, so that the compiler patched the code after the function object is created to directly refer to the function object (or can the code object call the code object?) without any name lookup at all. [An possible objection to doing this is that the code might no longer be valid if used to make another function object, but I think this is so rare at to hardly worry about.] Your initial post seemed to be about a hackish work around that looked to me to make as much trouble as it saves. So I did not respond. From Robert.T.Lynch at seagate.com Mon May 4 18:02:13 2009 From: Robert.T.Lynch at seagate.com (Robert.T.Lynch at seagate.com) Date: Mon, 4 May 2009 15:02:13 -0700 Subject: exit a program gracefully Message-ID: In a python program I ask if the user wants to continue. If they answer 'no', what options do I have to halt execution? I can put the rest of the code inside an "if bContinue:" block, but that seems awkward. I have looked at raising an exception, and perhaps this is the preferred method, but it seems daunting to my non-OOP eyes. Thanks -- Rob -------------- next part -------------- An HTML attachment was scrubbed... URL: From tjreedy at udel.edu Mon May 4 18:02:59 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 04 May 2009 18:02:59 -0400 Subject: find sublist inside list In-Reply-To: References: Message-ID: Matthias Gall? wrote: > Hi. > > My problem is to replace all occurrences of a sublist with a new element. > > Example: > Given ['a','c','a','c','c','g','a','c'] I want to replace all > occurrences of ['a','c'] by 6 (result [6,6,'c','g',6]). > > If I do this with string ('acaccgac') I have the advantage of all the > 'find' functions, but perfomance is bad and some extra care must be > taken if one element consist of more then one character (case of 11 for > example) > > So I really would like to work with lists straightforward, but I could > not found anything to search a sublist inside a list. > Any propositions for a simple solution? For a mutable homogenous array, consider the array module. Any algorithm that applies to a sequence of chars can be adjusted to other sequences. For the above case, remember than you can easily filter None out of a sequence. IE, replace 'a','c' with 6, None and then filter when done. From clp2 at rebertia.com Mon May 4 18:14:53 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Mon, 4 May 2009 15:14:53 -0700 Subject: exit a program gracefully In-Reply-To: References: Message-ID: <50697b2c0905041514t22ac9436td0dbe51e0f22dcfa@mail.gmail.com> On Mon, May 4, 2009 at 3:02 PM, wrote: > > In a python program I ask if the user wants to continue. ?If they answer > 'no', what options do I have to halt execution? ?I can put the rest of the > code inside an "if bContinue:" block, but that seems awkward. ?I have looked > at raising an exception, and perhaps this is the preferred method, but it > seems daunting to my non-OOP eyes. ?Thanks -- Rob There's the C-like: from sys import exit exit(return_code) Or if you prefer exceptions, the standard way is: raise SystemExit Cheers, Chris -- http://blog.rebertia.com From google at mrabarnett.plus.com Mon May 4 18:16:28 2009 From: google at mrabarnett.plus.com (MRAB) Date: Mon, 04 May 2009 23:16:28 +0100 Subject: if statement, with function inside it: if (t = Test()) == True: In-Reply-To: References: <020183f4$0$20619$c3e8da3@news.astraweb.com> Message-ID: <49FF693C.9000506@mrabarnett.plus.com> Rhodri James wrote: > On Mon, 04 May 2009 15:25:44 +0100, Antoon Pardon > wrote: > >> On 2009-04-24, Steven D'Aprano >> wrote: >>> On Fri, 24 Apr 2009 03:00:26 -0700, GC-Martijn wrote: >>> >>>> Hello, >>>> >>>> I'm trying to do a if statement with a function inside it. I want to >>>> use >>>> that variable inside that if loop , without defining it. >>>> >>>> def Test(): >>>> return 'Vla' >>>> >>>> I searching something like this: >>>> >>>> if (t = Test()) == 'Vla': >>>> print t # Vla >>>> >>>> or >>>> >>>> if (t = Test()): >>>> print t # Vla >>> >>> Fortunately, there is no way of doing that with Python. This is one >>> source of hard-to-debug bugs that Python doesn't have. >> >> I think this is an unfortunate consequence of choosing '=' for the >> assignment. They could have chosen an other token to indicate an >> assignment one that would have made the difference between an >> assignment and a comparison more visually different and thus >> bugs by using one while needing the other less hard to track >> down. > > What token could be used and still be meaningful, though? Algol > used ":=", which has most of the same problems as "=" (more, in my > opinion, since it fools the eye more easily if you're scanning > printed code quickly). Easily constructed arrows like "<=" or > "<-" collide with different comparators. About all that's left > that even vaguely implies assignment is "~", and it's no better. > If you're not limited to ASCII then there's '?' (U+2190, 'LEFTWARDS ARROW'). It's a little too late now, though. From ryan at rfk.id.au Mon May 4 18:17:38 2009 From: ryan at rfk.id.au (Ryan Kelly) Date: Tue, 05 May 2009 08:17:38 +1000 Subject: [ANN] regobj - Pythonic object-based access to the Windows Registry In-Reply-To: <49FF2156.6010909@g.nevcal.com> References: <1241361308.4359.9.camel@mango> <49FF2156.6010909@g.nevcal.com> Message-ID: <1241475458.4427.14.camel@mango> > > I've just released the results of a nice Sunday's coding, inspired by > > one too many turns at futzing around with the _winreg module. The > > "regobj" module brings a convenient and clean object-based API for > > accessing the Windows Registry. > > Sounds very interesting, Ryan. Just a couple questions about the > dictionary interface. Thanks for you interest and suggestions, a couple of quick points below. > I could see how you could map numbers to DWORD, 2.x str/3.x bytes to > binary, and 2.x unicode/3.x str to REG_SZ. But you don't document such > a translation, so it is unclear exactly what you actually do. This > would be somewhat weird in 2.x, though, where str commonly would want to > map to String rather than Binary. > > It seems almost necessary to add an explanation of whatever mapping is > presently done, to have complete documentation. I guess I just ran out of steam on the documentation front :-) Currently it maps integers to DWORD and anything else to REG_SZ. The idea of having a distinct Value class is to allow a more nuanced mapping to be developed, but I haven't got down to the details of that yet - my current application only requires REG_SZ. > Thinking more, I wonder if it is possible to create objects of type > Value to put in the dictionary to allow specification of multistring and > expandablestring Registry types, but then the dictionary name would have > to be redundant with the name attribute in the Value object. Maybe a > nameless value type could be invented, with just type and data attributes? It certainly is, using syntax like: HKCU.foo.bar["key"] = Value("%SYSTEMROOT%/mydir",type=REG_EXPAND_SZ)} Putting a nameless Value() instance in the initialisation dictionary should also work (but as you note, there's no documentation or tests for this yet...) Cheers, Ryan -- Ryan Kelly http://www.rfk.id.au | This message is digitally signed. Please visit ryan at rfk.id.au | http://www.rfk.id.au/ramblings/gpg/ for details -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 204 bytes Desc: This is a digitally signed message part URL: From google at mrabarnett.plus.com Mon May 4 18:21:06 2009 From: google at mrabarnett.plus.com (MRAB) Date: Mon, 04 May 2009 23:21:06 +0100 Subject: exit a program gracefully In-Reply-To: References: Message-ID: <49FF6A52.5070502@mrabarnett.plus.com> Robert.T.Lynch at seagate.com wrote: > > In a python program I ask if the user wants to continue. If they answer > 'no', what options do I have to halt execution? I can put the rest of > the code inside an "if bContinue:" block, but that seems awkward. I > have looked at raising an exception, and perhaps this is the preferred > method, but it seems daunting to my non-OOP eyes. Thanks -- Rob > What is the overall structure of the program? What happens if the user wants to continue? If you're in a loop then you can just use 'break' to leave the loop. From Scott.Daniels at Acm.Org Mon May 4 18:21:18 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Mon, 04 May 2009 15:21:18 -0700 Subject: AutoComplete in C++ Editor for Python In-Reply-To: <10fef506-2c39-418c-82e8-5c0c05a19e5c@s31g2000vbp.googlegroups.com> References: <10fef506-2c39-418c-82e8-5c0c05a19e5c@s31g2000vbp.googlegroups.com> Message-ID: flamz3d at gmail.com wrote: > ... Using this code, I can get information like the name of the symbol > (x), but I can't figure out how to get the type. If I knew how to get > this it would solve 99% of my problems :) If Python were statically typed, you might be correct. A _value_ in python has a type, not a variable. so, you can do: some_list = [] for a in None, True, 1, 1.0, 3.1j, 'text', u'text': some_list.append(a) print some_list and you will see something like: [None, True, 1, 1.0, 3.1000000000000001j, 'text', u'text'] That is, a has the type of its current value. any kind of object can be stored in any variable. --Scott David Daniels Scott.Daniels at Acm.Org From seanm.py at gmail.com Mon May 4 18:22:56 2009 From: seanm.py at gmail.com (seanm.py at gmail.com) Date: Mon, 4 May 2009 15:22:56 -0700 (PDT) Subject: Most Basic Question Ever - please help References: <65be3c66-5940-41ab-8a46-da424918b197@z5g2000vba.googlegroups.com> <7c328de7-ed20-450c-819f-7689e73f6a6c@g19g2000vbi.googlegroups.com> Message-ID: On May 3, 12:22?am, CM wrote: > On May 2, 4:36?pm, seanm... at gmail.com wrote: > > > > > I am going to try posting here again with more detail to see if I can > > finally get my first program to work. > > > I am working on a MacBook Pro with OS X 10.4.11. I opened a new window > > in IDLE to create a file. The file had only one line of code and was > > saved as module1.py. I saved it to Macintosh HD. The one line of code > > in the file is copied below: > > > print 'Hello module world!' > > > I closed the file and tried to run it in IDLE and Terminal, but I have > > had no success. I'll paste my commands and the error messages below > > (for IDLE, then Terminal). Any help would be very much appreciated. I > > feel like the marathon just started and I've fallen flat on my face. > > Thanks. > > > IDLE 2.6.2>>> python module1.py > > > SyntaxError: invalid syntax > > > sean-m-computer:~ seanm$ python > > Python 2.6.2 (r262:71600, Apr 16 2009, 09:17:39) > > [GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin > > Type "help", "copyright", "credits" or "license" for more information.>>> python module1.py > > > ? File "", line 1 > > ? ? python module1.py > > ? ? ? ? ? ? ? ? ?^ > > SyntaxError: invalid syntax > > Sean, also, keep in mind you can use IDLE to run > your scripts. ?After you have saved a script/program, > there is an option for Run in the menu, and then under > that, Run Module. ?The output of the script will be > sent to IDLE window indicated as the Python shell. > You can also just test code directly from within > that shell, though for multi-line programs, it is > easier within the composing window. > > I suggest you sign up for the Python tutor list:http://mail.python.org/mailman/listinfo/tutor > > And you can search through their big archive of > questions and answers here:http://www.nabble.com/Python---tutor-f2981.html > > The tutors there are great and super-helpful, and > will field any level of question but are particularly > good for absolute beginners. > > Here is a tutorial on using IDLE:http://hkn.eecs.berkeley.edu/~dyoo/python/idle_intro/index.html > > And here is a very good general programming tutorial online > book, focusing on Python:http://www.freenetpages.co.uk/hp/alan.gauld/ > > Good luck! > Che Thanks Che! The explaination and links are much appreciated. -Sean From Scott.Daniels at Acm.Org Mon May 4 18:31:01 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Mon, 04 May 2009 15:31:01 -0700 Subject: Self function In-Reply-To: References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> <202a05cd-3ce4-41fb-943b-5fd3f18d9bbb@z5g2000vba.googlegroups.com> <8c18a63a-8ca5-44bd-9b47-c1247a8f1a97@v4g2000vba.googlegroups.com> Message-ID: <37idnXafVJvm9mLUnZ2dnUVZ_v-dnZ2d@pdx.net> Arnaud Delobelle wrote: > In that case the following would not grow the stack, given tail-call > optimization: > > def visit(node): > print 'visiting', node > if node.right is None: > return visit(node.left) > if node.left is not None: > visit(node.left) > return visit(node.right) > Or (without TCO): def visit(node): while node is not None: print 'visiting', node if node.right is None: node = node.left else: if node.left is not None: visit(node.left) node = node.right Not so hard, really. --Scott David Daniels Scott.Daniels at Acm.Org From gagsl-py2 at yahoo.com.ar Mon May 4 18:33:51 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Mon, 04 May 2009 19:33:51 -0300 Subject: exit a program gracefully References: <50697b2c0905041514t22ac9436td0dbe51e0f22dcfa@mail.gmail.com> Message-ID: En Mon, 04 May 2009 19:14:53 -0300, Chris Rebert escribi?: > On Mon, May 4, 2009 at 3:02 PM, wrote: >> In a python program I ask if the user wants to continue. ?If they answer >> 'no', what options do I have to halt execution? ?I can put the rest of >> the >> code inside an "if bContinue:" block, but that seems awkward. ?I have >> looked >> at raising an exception, and perhaps this is the preferred method, but >> it >> seems daunting to my non-OOP eyes. ?Thanks -- Rob > > There's the C-like: > from sys import exit > exit(return_code) > > Or if you prefer exceptions, the standard way is: > raise SystemExit I prefer to put the code inside a function, and just `return` earlier. Having a sys.exit() in the middle of a function means that it's not reusable; I usually have a single exit() at the end. def do_work(): do some work ask the user if not continue: return 0 do more work return 0 if __name__=='__main__': try: sts = do_work() except: sts = 1, log exception sys.exit(sts) -- Gabriel Genellina From tjreedy at udel.edu Mon May 4 18:36:55 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 04 May 2009 18:36:55 -0400 Subject: if statement, with function inside it: if (t = Test()) == True: In-Reply-To: References: <020183f4$0$20619$c3e8da3@news.astraweb.com> Message-ID: Antoon Pardon wrote: > On 2009-04-24, Steven D'Aprano wrote: >> On Fri, 24 Apr 2009 03:00:26 -0700, GC-Martijn wrote: >> >>> Hello, >>> >>> I'm trying to do a if statement with a function inside it. I want to use >>> that variable inside that if loop , without defining it. >>> >>> def Test(): >>> return 'Vla' >>> >>> I searching something like this: >>> >>> if (t = Test()) == 'Vla': >>> print t # Vla >>> >>> or >>> >>> if (t = Test()): >>> print t # Vla >> Fortunately, there is no way of doing that with Python. There is a way to do something close. def x(v): x.val = v return v if x(3)==3: print('x.val is ', x.val) # prints x.val is 3 In OP's case, condition is "x(Test()) == 'Vla'" >> This is one source of hard-to-debug bugs that Python doesn't have. > > I think this is an unfortunate consequence of choosing '=' for the > assignment. Actually, is is a consequence of making assignment a statement rather than an expression. And that is because if assignment were an expression, the new name would have to be quoted. Or there would have to be a special exception to the normal expression evaulation rule. In other words, the problems one sees in a pure expression language. Note that C, for instance, which has such a special exception, does not, last I knew, allow a,b = 1,2. The solution for this use case is to encapsulate the binding within a function. The above is one possible example. One could use a pocket class instead of a pocket function. Or def set_echo(name, val): globals()[name] = val return val if set_echo('t', Test()) == 'Vla': print t ... Terry Jan Reedy From Scott.Daniels at Acm.Org Mon May 4 18:49:46 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Mon, 04 May 2009 15:49:46 -0700 Subject: if statement, with function inside it: if (t = Test()) == True: In-Reply-To: References: <020183f4$0$20619$c3e8da3@news.astraweb.com> Message-ID: MRAB wrote: > If you're not limited to ASCII then there's '?' (U+2190, 'LEFTWARDS > ARROW'). It's a little too late now, though. Well, if you are old enough, that was the ASCII graphic for the character now printed as '_' (ASCII), and SAIL used it for assignment statements, causing much consternation when our source code displayed funny on new-fangled terminals. So, just an unfortunate setting on the time machine is the basic flaw. --Scott David Daniels @Acm.Org From pavlovevidence at gmail.com Mon May 4 18:51:15 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Mon, 4 May 2009 15:51:15 -0700 (PDT) Subject: Self function References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> <202a05cd-3ce4-41fb-943b-5fd3f18d9bbb@z5g2000vba.googlegroups.com> <8c18a63a-8ca5-44bd-9b47-c1247a8f1a97@v4g2000vba.googlegroups.com> Message-ID: On May 4, 1:25?pm, bearophileH... at lycos.com wrote: > Aahz: > > > When have you ever had a binary tree a thousand levels deep? > > Yesterday. > > >Consider how big 2**1000 is...< > > You are thinking just about complete binary trees. > But consider that a topology like a single linked list (every node has > 1 child, and they are chained) is a true binary tree still. 1. Singly-linked lists can and should be handled with iteration. All recursion does it make what you're doing a lot less readable for almost all programmers. 2. You should be using a Python list anyway. Carl Banks From ben+python at benfinney.id.au Mon May 4 18:55:31 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Tue, 05 May 2009 08:55:31 +1000 Subject: IDE for python 2.6 (auto complete) References: Message-ID: <87zldsiil8.fsf@benfinney.id.au> Soumen banerjee writes: > Is there any IDE with support for autocomplete in python 2.6 with all > the newer functions included? Emacs, with the right library, is an excellent Python IDE with auto-completion and many other features . -- \ ?People are very open-minded about new things, as long as | `\ they're exactly like the old ones.? ?Charles F. Kettering | _o__) | Ben Finney From bearophileHUGS at lycos.com Mon May 4 19:06:28 2009 From: bearophileHUGS at lycos.com (bearophileHUGS at lycos.com) Date: Mon, 4 May 2009 16:06:28 -0700 (PDT) Subject: Self function References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> <202a05cd-3ce4-41fb-943b-5fd3f18d9bbb@z5g2000vba.googlegroups.com> <8c18a63a-8ca5-44bd-9b47-c1247a8f1a97@v4g2000vba.googlegroups.com> Message-ID: Carl Banks: >1. Singly-linked lists can and should be handled with iteration.< I was talking about a binary tree with list-like topology, of course. >All recursion does it make what you're doing a lot less readable for almost all programmers.< I can't agree. If the data structure is recursive (like a tree, or even sometimes graphs, etc) a recursive algorithm is shorter, more readable and more natural. An example, a postorder recursive visit: def postorder_scan(root): if root is not None: if root.left is not None: for n in postorder_scan(root.left): yield n if root.right is not None: for n in postorder_scan(root.right): yield n yield root Try to write that in an iterative way, not adding any new field to the nodes, and you will see. And even if you add a "visited" new boolean field to nodes, the resulting code isn't as nice as the recursive one yet. > 2. You should be using a Python list anyway. I should use D when I have to use such algorithms, I know. Bye, bearophile From amber.of.luxor at gmail.com Mon May 4 19:30:32 2009 From: amber.of.luxor at gmail.com (Amber) Date: Mon, 4 May 2009 17:30:32 -0600 Subject: Database help needed Message-ID: My PHB is insane. Today he drops 50,000 databases in MS Access format on my desk, and tells me that by Friday I need to: * Remove all of the "junk content" in the record fields; * Remove all records with blank fields in them; * Correct all fields in which the markup is "wrong"; * Correct all fields in which the data is "wrong"; * Ensure that all database include some specific tables; ** Add appropriate content to the records in the "new" tables; And finally, said databases are to be in: * MS Access 97 format; * MS Access 2000 format; * MS Access 2003 format; ** Is there any documentation anywhere on what the differences between those is? Won't a database created for Access 97 be openable in MS Access 2003? * SQLite format; * MySQL format; * PDB format, for use on his PalmPilot; * Commas separated values; * dBase 3; * Excell spreadsheets; * ODF spreadsheets; I'm a programming novice, but I think that I can write a script that reads the MSAccess databases, and cleans up the content in the individual fields of the database. What I need is: * A pointer to good. current, downloadable documentation on creating, and editing databases using Python; ** Good current books on python and databases will also be useful; * Pointers to material that describe how to create MS Access databases using Python, on Linux; * Pointers to material that describe how to create, and edit SQL CE 2.0 databases in Python, on Linux. I'm assuming that this is possible, but since that database engine is available for Windows Mobile only --- at least according to the Google search I did --- I have some doubts; I need stuff I can download. I'll be burning it to a DVD, and taking that to work. No internet access at work. The PHB considers that to be a time waster, and an expensive luxury that can not be justified in the current economic climate. Amber -- Wind under Thy Wings -------------- next part -------------- An HTML attachment was scrubbed... URL: From pavlovevidence at gmail.com Mon May 4 19:33:13 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Mon, 4 May 2009 16:33:13 -0700 (PDT) Subject: Self function References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> <202a05cd-3ce4-41fb-943b-5fd3f18d9bbb@z5g2000vba.googlegroups.com> <8c18a63a-8ca5-44bd-9b47-c1247a8f1a97@v4g2000vba.googlegroups.com> Message-ID: On May 4, 4:06?pm, bearophileH... at lycos.com wrote: > Carl Banks: > > >1. Singly-linked lists can and should be handled with iteration.< > > I was talking about a binary tree with list-like topology, of course. "(every node has 1 child, and they are chained)" That's a singly-linked list, not a tree. It's a degenerate tree at best. > >All recursion does it make what you're doing a lot less readable for almost all programmers.< > > I can't agree. If every child has one node you don't need recursion. > If the data structure is recursive (like a tree, or > even sometimes graphs, etc) a recursive algorithm is shorter, more > readable and more natural. Because that's a tree, not a linked-list. Which is germane because Python's recursion limit is the thing you're complaining about here, and you don't normally hit that limit with real trees because they rarely go 1000 deep. Singly-linked lists don't count because you don't need recursion for them. [snip strawman example] > > 2. You should be using a Python list anyway. > > I should use D when I have to use such algorithms, I know. That's another idea. Carl Banks From emile at fenx.com Mon May 4 19:51:10 2009 From: emile at fenx.com (Emile van Sebille) Date: Mon, 04 May 2009 16:51:10 -0700 Subject: Database help needed In-Reply-To: References: Message-ID: On 5/4/2009 4:30 PM Amber said... > > My PHB is insane. > > Today he drops 50,000 databases in MS Access format on my desk, and > tells me that by Friday I need to: > * Remove all of the "junk content" in the record fields; > * Remove all records with blank fields in them; > * Correct all fields in which the markup is "wrong"; > * Correct all fields in which the data is "wrong"; > * Ensure that all database include some specific tables; > ** Add appropriate content to the records in the "new" tables; > > And finally, said databases are to be in: > > * MS Access 97 format; > * MS Access 2000 format; > * MS Access 2003 format; > ** Is there any documentation anywhere on what the differences between > those is? Won't a database created for Access 97 be openable in MS > Access 2003? > * SQLite format; > * MySQL format; > * PDB format, for use on his PalmPilot; > * Commas separated values; > * dBase 3; > * Excell spreadsheets; > * ODF spreadsheets; It wouldn't surprise me that you couldn't do all this with open office using command line options which is where I'd start. There's also python modules available, but I haven't yet gotten into them. I found most of what I needed on groups.google.com though, so not having internet access could be trouble -- some of the conversion structures and requirements weren't quite so obvious nor documentation easily available. Emile From debatem1 at gmail.com Mon May 4 19:53:05 2009 From: debatem1 at gmail.com (CTO) Date: Mon, 4 May 2009 16:53:05 -0700 (PDT) Subject: Database help needed References: Message-ID: On May 4, 7:51?pm, Emile van Sebille wrote: > On 5/4/2009 4:30 PM Amber said... > > > > > > > My PHB is insane. > > > Today he drops 50,000 databases in MS Access format on my desk, and > > tells me that by Friday I need to: > > * Remove all of the "junk content" in the record fields; > > * Remove all records with blank fields in them; > > * Correct all fields in which the markup is "wrong"; > > * Correct all fields in which the data is "wrong"; > > * Ensure that all database include some specific tables; > > ** Add appropriate content to the records in the "new" tables; > > > And finally, said databases are to be in: > > > * MS Access 97 format; > > * MS Access 2000 format; > > * MS Access 2003 format; > > ** Is there any documentation anywhere on what the differences between > > those is? ? Won't a database created for Access 97 be openable in MS > > Access 2003? > > * SQLite format; > > * MySQL format; > > * PDB format, for use on his PalmPilot; > > * Commas separated values; > > * dBase 3; > > * Excell spreadsheets; > > * ODF spreadsheets; > > It wouldn't surprise me that you couldn't do all this with open office > using command line options which is where I'd start. ?There's also > python modules available, but I haven't yet gotten into them. ?I found > most of what I needed on groups.google.com though, so not having > internet access could be trouble -- some of the conversion structures > and requirements weren't quite so obvious nor documentation easily > available. > > Emile I was just about to recommend OO. PyUNO is pretty easy to use, IMHO, and while I obviously have no idea what your boss wants in specific, it shouldn't be too bad. Geremy Condra From gallium.arsenide at gmail.com Mon May 4 19:59:15 2009 From: gallium.arsenide at gmail.com (John Yeung) Date: Mon, 4 May 2009 16:59:15 -0700 (PDT) Subject: Code works fine except... References: <8d4ec1df-dddb-469a-99a1-695152db7c04@n4g2000vba.googlegroups.com> <7f3493ca-efcb-48cc-aa29-487bb11fcd09@p4g2000vba.googlegroups.com> <16d47b2f-14d4-4266-84d2-c26501beb0c1@r34g2000vbi.googlegroups.com> Message-ID: <1691f042-bc14-46b9-b7b3-5a44348e9965@b1g2000vbc.googlegroups.com> On May 4, 10:01?am, Ross wrote: > The "magic numbers" that everyone is wondering about are > indeed used for spreading out the bye selection and I got > them by simply calculating a line of best fit when plotting > several courts: byes ratios. But that doesn't really help you. When you do seq[::step], step is evaluated once and used for the whole extended slice. So in almost all cases that you are likely to encounter, step is 2, so you'll get seq[0], seq[2], seq[4], etc. Even if step is some other positive number, seq[0] will always be chosen. > The "byes = #whatever" in my code calculate the number of > tuples that need to be placed in the bye_list. Fine, but that's not the number of byes that you put into bye_list. > At first glance, the suggestion of adding a simple shift > at the end of round_robin doesn't seem to work since > round_robin already shifts everything except for the first > position already. Please correct me if I'm wrong because > you might have been suggesting something different. If you read my post carefully, you would see that you HAVE to do something about the first player always being stuck in the first spot. Either move him around, or select your byes differently. That said, I've played around with the shuffling a bit, and it does seem to be pretty tricky to get it to work for the general case where there is no prior knowledge of how many players and courts you will have. I can't shake the feeling that someone good at math will be able to figure out something elegant; but if left to my own devices, I am starting to lean toward just generating all the possible matches and somehow picking from them as needed to fill the courts, trying to keep the number of matches played by each player as close to equal as possible, and trying to keep the waiting times approximately equal as well. John From norseman at hughes.net Mon May 4 20:15:55 2009 From: norseman at hughes.net (norseman) Date: Mon, 04 May 2009 17:15:55 -0700 Subject: Tkinter, Trouble with Message,Label widget In-Reply-To: <4f61c431-1c78-4de7-9b82-08eee09ae7d6@s20g2000vbp.googlegroups.com> References: <4f61c431-1c78-4de7-9b82-08eee09ae7d6@s20g2000vbp.googlegroups.com> Message-ID: <49FF853B.7040404@hughes.net> Ioannis Lalopoulos wrote: > I assume that you create the two windows through two different calls > to Tkinter.Tk() but you cannot enter two mainloops (at least not in a > normal way). > > If you want a second window use the Toplevel widget. > > Try the following, it does what you want: > > import Tkinter > > root = Tkinter.Tk() > > my_text = Tkinter.StringVar(root) > > another_window = Tkinter.Toplevel() > > entry = Tkinter.Entry(root, textvar=my_text) > entry.pack() > > label = Tkinter.Label(another_window, textvar=my_text) > label.pack() > > root.mainloop() > > In the above example, whatever you type in the entry widget in the > root window gets reflected in the label widget which is inside the > second window, the one that was created with Tkinter.Toplevel(). > > Hope it helps, > > John > -- > http://mail.python.org/mailman/listinfo/python-list > ================================================ Hendrik van Rooyen mentioned the textvar too. Thanks Hendrik John (Ioannis); It took me awhile to get your template to work for me. People reading this! THE ABOVE CODE WORKS JUST FINE - AS IS!!! My needs are slightly different and it took me a bit to get python and I on the same track. I still have reflexes that demand dictionary compliance. Global is supposed to be Global, really, not just sort of. Once I get past the children I seem to do OK in python. like: mVar= '1234' according to docs, this is a global p='%6s\n' % mVar same ...some code to do something mVar= '4321' updates a supposed global these two don't think so print p prints from both of these show the 1234, print '%s' % p[:] they do not reflect the updated 'global' >>> >>> mVar= '1234' >>> mVar '1234' >>> p= '%s\n' % mVar >>> p '1234\n' >>> >>> mVar= '4321' >>> mVar '4321' >>> print p 1234 >>> print '%s' % p[:] 1234 >>> The definitive on Toplevel was the biggest help. All I have read have never clearly stated its purpose. (A non-root root) per Tkinter help: "class Toplevel(BaseWidget, Wm) Toplevel widget, e.g. for dialogs. ... " Since I'm not doing dialogs, I quite reading and move on. John: Thank you again. Today: 20090504 copy/paste from Python 2.5.2 on Linux Slackware 10.2 Steve norseman at hughes.net From aydavis at purdue.edu Mon May 4 20:45:25 2009 From: aydavis at purdue.edu (Davis, Amelie Y) Date: Mon, 4 May 2009 20:45:25 -0400 Subject: sorting items in a table problematic because of scientific notation In-Reply-To: <1ff35eef-23bf-4e0a-94e2-73cd3c4e7ba8@y6g2000prf.googlegroups.com> References: <47B887AC6641DD4DA8949367606399390207BB12@EXCH06.purdue.lcl> <1ff35eef-23bf-4e0a-94e2-73cd3c4e7ba8@y6g2000prf.googlegroups.com> Message-ID: <47B887AC6641DD4DA8949367606399390207BF1F@EXCH06.purdue.lcl> Thank you _ that solved it. ? Am?lie ? ??Please consider the environment before printing this e-mail or any of its attachments (if applicable) ? -----Original Message----- From: python-list-bounces+aydavis=purdue.edu at python.org [mailto:python-list-bounces+aydavis=purdue.edu at python.org] On Behalf Of Raymond Hettinger Sent: Wednesday, April 29, 2009 10:31 AM To: python-list at python.org Subject: Re: sorting items in a table problematic because of scientific notation [John Machin] > > 'NEAR_DIST'], [('N', 9, 0), ('N', 9, 0), ('F', 19, 11)], [53, 55, ' > > The data type code for the offending column is "F" which is not in the > bog-standard dBase III set of C, N, D, and L. The code that you have used merely > returns unchanged the character string that finds in the data base. > > > > > 1.05646365517e+005'], [53, 6, ' 9.32599134016e+004'], [53, 0, ' > > 8.97477154418e+004'], [53, 2, ' 8.96449127749e+004'], [53, 1, ' > > 7.88170078501e+004'], [53, 5, ' 8.29281503631e+004'], [53, 4, ' FWIW, I just updated the recipe to handle an F typecode by casting it to a float. Hope this helps. Raymond -- http://mail.python.org/mailman/listinfo/python-list From ross.jett at gmail.com Mon May 4 20:56:57 2009 From: ross.jett at gmail.com (Ross) Date: Mon, 4 May 2009 17:56:57 -0700 (PDT) Subject: Code works fine except... References: <8d4ec1df-dddb-469a-99a1-695152db7c04@n4g2000vba.googlegroups.com> <7f3493ca-efcb-48cc-aa29-487bb11fcd09@p4g2000vba.googlegroups.com> <16d47b2f-14d4-4266-84d2-c26501beb0c1@r34g2000vbi.googlegroups.com> <1691f042-bc14-46b9-b7b3-5a44348e9965@b1g2000vbc.googlegroups.com> Message-ID: <1a497471-498a-43d8-90ba-d676e8acf68b@r36g2000vbr.googlegroups.com> On May 4, 7:59?pm, John Yeung wrote: > On May 4, 10:01?am, Ross wrote: > > > The "magic numbers" that everyone is wondering about are > > indeed used for spreading out the bye selection and I got > > them by simply calculating a line of best fit when plotting > > several courts: byes ratios. > > But that doesn't really help you. ?When you do seq[::step], step is > evaluated once and used for the whole extended slice. ?So in almost > all cases that you are likely to encounter, step is 2, so you'll get > seq[0], seq[2], seq[4], etc. ?Even if step is some other positive > number, seq[0] will always be chosen. > > > The "byes = #whatever" in my code calculate the number of > > tuples that need to be placed in the bye_list. > > Fine, but that's not the number of byes that you put into bye_list. > > > At first glance, the suggestion of adding a simple shift > > at the end of round_robin doesn't seem to work since > > round_robin already shifts everything except for the first > > position already. Please correct me if I'm wrong because > > you might have been suggesting something different. > > If you read my post carefully, you would see that you HAVE to do > something about the first player always being stuck in the first > spot. ?Either move him around, or select your byes differently. > > That said, I've played around with the shuffling a bit, and it does > seem to be pretty tricky to get it to work for the general case where > there is no prior knowledge of how many players and courts you will > have. > > I can't shake the feeling that someone good at math will be able to > figure out something elegant; but if left to my own devices, I am > starting to lean toward just generating all the possible matches and > somehow picking from them as needed to fill the courts, trying to keep > the number of matches played by each player as close to equal as > possible, and trying to keep the waiting times approximately equal as > well. > > John "But that doesn't really help you. When you do seq[::step], step is evaluated once and used for the whole extended slice. So in almost all cases that you are likely to encounter, step is 2, so you'll get seq[0], seq[2], seq[4], etc. Even if step is some other positive number, seq[0] will always be chosen." It's not true that in almost all cases the step is 2. How that is evaluated directly depends on the number of available courts. Anyways, you're right that seq[0] is always evaluated. That's why my algorithm works fine when there are odd numbers of players in a league. But if you will notice, my original question was how I could ADD TO or AMMEND my current code to account for even number of players. I have used an algorithm that comes up with all possible pairings and then randomly puts people together each week and places players in a bye list according to how many times they've previously been in the bye list but since I was dealing with random permutations, the algorithm took minutes to evaluate when there were more than 10 players in the league. From rcdailey at gmail.com Mon May 4 22:25:42 2009 From: rcdailey at gmail.com (Robert Dailey) Date: Mon, 4 May 2009 19:25:42 -0700 (PDT) Subject: subprocess & shared environments References: <8c79f749-e648-4a2e-afad-4053a8ec5cbd@f41g2000pra.googlegroups.com> <98aa4906-5006-437a-8fc0-0eaedf433be9@3g2000yqk.googlegroups.com> Message-ID: On May 1, 4:18?pm, Aaron Brady wrote: > On May 1, 12:09?am, Robert Dailey wrote: > > > I'm currently calling subprocess.call() on a batch file (in Windows) > > that sets a few environment variables that are needed by further > > processes started via subprocess.call(). How can I persist the > > environment modifications by the first call() function? I've done my > > own searching on this and I came up with nothing. > > > Help is appreciated. > > I don't know if this will work, but you can try spawning a batch file > that calls the first program to set the variables, then executes the > second program. ?You might need to create that batch file by hand, as > well as the command line for the second process. > > You can also try setting the env. variables in the parent process, and > restoring them after. Thanks for your help guys. Unfortunately both ideas will not work. I guess I should have mentioned that the batch file in question is vsvars32.bat, from the Visual Studio installation directory. I should not modify this file, nor can I create a "wrapper" batch file as you suggested, since I call several other batch files at different times. There's about 6 other nmake calls I make that each is preceeded with a call to vsvars32.bat. For now I'm using the && DOS operator, this seems to work, however it is not ideal. From soumen08 at gmail.com Mon May 4 22:30:13 2009 From: soumen08 at gmail.com (Soumen banerjee) Date: Tue, 5 May 2009 08:00:13 +0530 Subject: local module-docs server on Linux? Message-ID: Hello, I had used python on windows and one of the features i liked best was that you could start a module-docs server and then use firefox to access it. This would show module-docs for all modules you had installed(including any 3rd party installs) . How do i do this on linux? regards Soumen From gallium.arsenide at gmail.com Mon May 4 22:33:29 2009 From: gallium.arsenide at gmail.com (John Yeung) Date: Mon, 4 May 2009 19:33:29 -0700 (PDT) Subject: Code works fine except... References: <8d4ec1df-dddb-469a-99a1-695152db7c04@n4g2000vba.googlegroups.com> <7f3493ca-efcb-48cc-aa29-487bb11fcd09@p4g2000vba.googlegroups.com> <16d47b2f-14d4-4266-84d2-c26501beb0c1@r34g2000vbi.googlegroups.com> <1691f042-bc14-46b9-b7b3-5a44348e9965@b1g2000vbc.googlegroups.com> <1a497471-498a-43d8-90ba-d676e8acf68b@r36g2000vbr.googlegroups.com> Message-ID: <8b49b775-66a7-42d5-9b00-cf23d9b044cc@e23g2000vbe.googlegroups.com> On May 4, 8:56?pm, Ross wrote: > Anyways, you're right that seq[0] is always evaluated. > That's why my algorithm works fine when there are odd > numbers of players in a league. It doesn't work fine for all odd numbers of players. For example, 15 players on 3 courts should result in 5 byes. But what actually happens with your code is that you get 4 byes or 8 byes, depending on whether you've got floating-point division enabled. So the way you are selecting byes does not even guarantee that you'll allocate the correct number of active matches for the number of courts, and this is due to the fact that the extended slice is too "coarse" a tool for the task. Now, it may be that for you, the number of players and courts is always within a confined range such that extended slicing and your magic constants will work. That's fine for you, but we weren't given that information. I haven't even begun to look at what happens for doubles. John From david.lyon at preisshare.net Mon May 4 22:52:38 2009 From: david.lyon at preisshare.net (David Lyon) Date: Mon, 04 May 2009 22:52:38 -0400 Subject: local module-docs server on =?UTF-8?Q?Linux=3F?= In-Reply-To: References: Message-ID: <153af629ebccc7f8a28813ae89d2acc4@preisshare.net> Hi Soumen, You could try running the Python Package Manager that we are developing on sourceforge. There isn't a release yet but we have implemented two buttons inside the program for 'Examples' and 'Documentation'. What they do is go off and find any documentation files or example directories for third party modules that are installed on the system. Then, if found, they will open a web browser on any documentation or examples directories. In the spirit of open source you could give it a try.... svn co https://pythonpkgmgr.svn.sourceforge.net/svnroot/pythonpkgmgr pythonpkgmgr Any feedback is welcome.. David On Tue, 5 May 2009 08:00:13 +0530, Soumen banerjee wrote: > Hello, > I had used python on windows and one of the features i liked best was > that you could start a module-docs server and then use firefox to > access it. This would show module-docs for all modules you had > installed(including any 3rd party installs) . How do i do this on > linux? > regards > Soumen > -- > http://mail.python.org/mailman/listinfo/python-list From ross.jett at gmail.com Mon May 4 23:01:34 2009 From: ross.jett at gmail.com (Ross) Date: Mon, 4 May 2009 20:01:34 -0700 (PDT) Subject: Code works fine except... References: <8d4ec1df-dddb-469a-99a1-695152db7c04@n4g2000vba.googlegroups.com> <7f3493ca-efcb-48cc-aa29-487bb11fcd09@p4g2000vba.googlegroups.com> <16d47b2f-14d4-4266-84d2-c26501beb0c1@r34g2000vbi.googlegroups.com> <1691f042-bc14-46b9-b7b3-5a44348e9965@b1g2000vbc.googlegroups.com> <1a497471-498a-43d8-90ba-d676e8acf68b@r36g2000vbr.googlegroups.com> <8b49b775-66a7-42d5-9b00-cf23d9b044cc@e23g2000vbe.googlegroups.com> Message-ID: <54d76f11-c255-487d-9c3a-aaa380d7827a@z7g2000vbh.googlegroups.com> On May 4, 7:33?pm, John Yeung wrote: > On May 4, 8:56?pm, Ross wrote: > > > Anyways, you're right that seq[0] is always evaluated. > > That's why my algorithm works fine when there are odd > > numbers of players in a league. > > It doesn't work fine for all odd numbers of players. ?For example, 15 > players on 3 courts should result in 5 byes. ?But what actually > happens with your code is that you get 4 byes or 8 byes, depending on > whether you've got floating-point division enabled. > > So the way you are selecting byes does not even guarantee that you'll > allocate the correct number of active matches for the number of > courts, and this is due to the fact that the extended slice is too > "coarse" a tool for the task. ?Now, it may be that for you, the number > of players and courts is always within a confined range such that > extended slicing and your magic constants will work. ?That's fine for > you, but we weren't given that information. > > I haven't even begun to look at what happens for doubles. > > John You're right... I only tested cases when number of people playing outnumbered the number of byes that week. Anyways, I'm new to programming and this has been a good learning experience. Next time around, I'll be sure to thoroughly comment my code before I ask for help on it. I really appreciate all the help that you've offered so far. Right now, I'm debating whether I should try to reinvent the round_robin generator part of the code or whether there still might be a way to shuffle the results of the generated output so that I can slice it effectively. From steven at REMOVE.THIS.cybersource.com.au Mon May 4 23:22:34 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 05 May 2009 03:22:34 GMT Subject: Self function References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> <202a05cd-3ce4-41fb-943b-5fd3f18d9bbb@z5g2000vba.googlegroups.com> <8c18a63a-8ca5-44bd-9b47-c1247a8f1a97@v4g2000vba.googlegroups.com> Message-ID: On Mon, 04 May 2009 15:51:15 -0700, Carl Banks wrote: > All > recursion does it make what you're doing a lot less readable for almost > all programmers. What nonsense. There are many algorithms that are more understandable written recursively than iteratively -- consult any good text book for examples. There are algorithms that are naturally iterative, and algorithms which are naturally recursive (and a few which are equally simple either way), and forcing either one into the other form leads to complicated and confusing code. This especially holds for mutually- recursive functions, where re-writing them to be iterative is usually very difficult. Sometimes it's worth rewriting a recursive algorithm to be iterative for the performance optimization, but that comes at the price of reduced readability. -- Steven From damienlmoore at gmail.com Mon May 4 23:24:40 2009 From: damienlmoore at gmail.com (dmoore) Date: Mon, 4 May 2009 20:24:40 -0700 (PDT) Subject: Problem with case insensitive volumes Message-ID: Does anyone know of a way to get the unique pathname for files stored on FAT32 or other case insensitive drives? For example: os.path.samefile('/media/usbkey/file1.jpg','/media/usbkey/FILE1.jpg') returns True but, is there a simple way to determine whether '/media/usbkey/ file1.jpg' or '/media/usbkey/FILE1.jpg' is the actual representation on the device. it matters because operations like this have meaning: os.rename('/media/usbkey/file1.jpg','/media/usbkey/tmp') os.rename('/media/usbkey/tmp','/media/usbkey/FILE1.jpg') If I have a program that scans a directory for files and compares against a stored list of pathnames, I want to be able to determine not only that the file still exists but whether or not it has changed case (i.e. '/media/usbkey/file1.jpg' has become '/media/usbkey/FILE1.jpg'). Any takers? From steven at REMOVE.THIS.cybersource.com.au Mon May 4 23:26:15 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 05 May 2009 03:26:15 GMT Subject: Self function References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> <202a05cd-3ce4-41fb-943b-5fd3f18d9bbb@z5g2000vba.googlegroups.com> <8c18a63a-8ca5-44bd-9b47-c1247a8f1a97@v4g2000vba.googlegroups.com> Message-ID: On Mon, 04 May 2009 16:33:13 -0700, Carl Banks wrote: > On May 4, 4:06?pm, bearophileH... at lycos.com wrote: >> Carl Banks: >> >> >1. Singly-linked lists can and should be handled with iteration.< >> >> I was talking about a binary tree with list-like topology, of course. > > "(every node has 1 child, and they are chained)" > > That's a singly-linked list, not a tree. It's a degenerate tree at > best. A singly-linked list is a degenerate tree. Not "at best", but "is". >> >All recursion does it make what you're doing a lot less readable for >> >almost all programmers.< >> >> I can't agree. > > If every child has one node you don't need recursion. And if one single node in the entire tree has two children, you do. What are you suggesting, that Bearophile should write his tree-processing code to only deal with the degenerate case? >> If the data structure is recursive (like a tree, or even sometimes >> graphs, etc) a recursive algorithm is shorter, more readable and more >> natural. > > Because that's a tree, not a linked-list. And a linked list is a degenerate tree. If you have a non-self-balancing tree, sometimes it will be degenerate, and your code needs to deal with it. > Which is germane because Python's recursion limit is the thing you're > complaining about here, and you don't normally hit that limit with real > trees because they rarely go 1000 deep. And if just ONE branch of the tree goes 1000 deep, even if the rest of the tree is shallow, then you hit the default recursion limit. > Singly-linked lists don't count because you don't need recursion for > them. If each node has two (or more) child fields, even if only one of those children is non-empty, then your data structure is a degenerate tree and does count. > [snip strawman example] It's not a strawman just because you don't like it. Dealing with degenerate trees is a genuine issue, and avoiding degenerate trees is the reason why people have developed more complicated structures like red- black trees. -- Steven From alex at bluebottle.net.au Tue May 5 00:12:56 2009 From: alex at bluebottle.net.au (Alex Jurkiewicz) Date: Tue, 05 May 2009 12:12:56 +0800 Subject: Threaded alternatives to smtplib? In-Reply-To: References: <767h7tF1b8q8gU1@mid.uni-berlin.de> Message-ID: <49FFBCC8.8080905@bluebottle.net.au> Piet van Oostrum wrote: >> AJ> if __name__ == '__main__': >> AJ> THREADS = [] >> AJ> for i in range(CONCURRENCY): >> AJ> THREADS.append(threading.Thread(target=threadProcessRecipient)) >> AJ> for thread in THREADS: >> AJ> thread.run() >> > > You should use thread.start(), not thread.run(). When you use run(), it > will be sequential execution, as you experience. With start() you get > concurrency > Thanks! Changing this method call fixes the original problem we saw. In the process I've run into another though, seemingly a race condition / deadlock. Our email script DKIM signs all messages with pydkim. Because this is so CPU intensive we run the signing in an external process, using dkimsign.py (which basically accepts the message on stdin, calls dkim.sign() on it and passes the result back on stdout). We call the DKIM signing while preparing each message in each thread like this: cmd = subprocess.Popen( ['/usr/bin/nice', PYTHONBIN, 'dkimsign.py', DKIMSELECTOR, DKIMDOMAIN, DKIMPRIVATEKEYFILE], stdin = subprocess.PIPE, stdout = subprocess.PIPE) message = cmd.communicate(message)[0] The majority of the time (80%) our mail merge works fine -- messages are sent out in a multi-threaded manner. The rest of the time, some of the threads deadlock when calling dkimsign.py. Specifically, they stop during the communicate() call above. dkimsign.py never receives any input (hangs on message=sys.stdin.read()), and sits there waiting forever, which stops the thread from doing anything. Is this a bug with subprocess? Is there some way to set a timeout on the communicate() call so I can detect these locks? Cheers, AJ -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at ieee.org Tue May 5 00:33:22 2009 From: davea at ieee.org (Dave Angel) Date: Tue, 05 May 2009 00:33:22 -0400 Subject: Problem with case insensitive volumes In-Reply-To: References: Message-ID: <49FFC192.40704@ieee.org> dmoore wrote: > Does anyone know of a way to get the unique pathname for files stored > on FAT32 or other case insensitive drives? > > For example: > > os.path.samefile('/media/usbkey/file1.jpg','/media/usbkey/FILE1.jpg') > > returns True > > but, is there a simple way to determine whether '/media/usbkey/ > file1.jpg' or '/media/usbkey/FILE1.jpg' is the actual representation > on the device. > > it matters because operations like this have meaning: > > os.rename('/media/usbkey/file1.jpg','/media/usbkey/tmp') > os.rename('/media/usbkey/tmp','/media/usbkey/FILE1.jpg') > > If I have a program that scans a directory for files and compares > against a stored list of pathnames, I want to be able to determine not > only that the file still exists but whether or not it has changed case > (i.e. '/media/usbkey/file1.jpg' has become '/media/usbkey/FILE1.jpg'). > > Any takers? > > os;walk will tell you the correct case for each node. So if it gives you a different case than the stored form, you have your answer. From debatem1 at gmail.com Tue May 5 00:45:44 2009 From: debatem1 at gmail.com (CTO) Date: Mon, 4 May 2009 21:45:44 -0700 (PDT) Subject: local module-docs server on Linux? References: Message-ID: <3792323b-c44c-47d0-ad90-21df767a4623@o20g2000vbh.googlegroups.com> On May 4, 10:30?pm, Soumen banerjee wrote: > Hello, > I had used python on windows and one of the features i liked best was > that you could start a module-docs server and then use firefox to > access it. pydoc -p From ur.liam at iksmlapan.reverse Tue May 5 00:59:18 2009 From: ur.liam at iksmlapan.reverse (Napalmski) Date: Tue, 5 May 2009 05:59:18 +0100 Subject: Question of UTF16BE encoding / decoding Message-ID: Hello, I have an encoded string in the form "004e006100700061006c006d", if you split on every 4 characters it decodes to a single character. I have come up with this: name = '004e006100700061006c006d' name2 = "" for x in range(0, len(name), 4): name2 = name2 + chr(int(name[x:x+4], 16)) Is there a better way to do this using name.decode() that would then also work with .encode()? I have tried .decode("utf-16-be"), which I beleive is the format here, but I am getting the error: UnicodeEncodeError: 'ascii' codec can't encode characters in position 0- 11: ordinal not in range(128) And my debugger seems to show a load of unicode when it breaks. Thank you! From gallium.arsenide at gmail.com Tue May 5 01:12:03 2009 From: gallium.arsenide at gmail.com (John Yeung) Date: Mon, 4 May 2009 22:12:03 -0700 (PDT) Subject: Code works fine except... References: <8d4ec1df-dddb-469a-99a1-695152db7c04@n4g2000vba.googlegroups.com> <7f3493ca-efcb-48cc-aa29-487bb11fcd09@p4g2000vba.googlegroups.com> <16d47b2f-14d4-4266-84d2-c26501beb0c1@r34g2000vbi.googlegroups.com> <1691f042-bc14-46b9-b7b3-5a44348e9965@b1g2000vbc.googlegroups.com> <1a497471-498a-43d8-90ba-d676e8acf68b@r36g2000vbr.googlegroups.com> <8b49b775-66a7-42d5-9b00-cf23d9b044cc@e23g2000vbe.googlegroups.com> <54d76f11-c255-487d-9c3a-aaa380d7827a@z7g2000vbh.googlegroups.com> Message-ID: On May 4, 11:01 pm, Ross wrote: > Anyways, I'm new to > programming and this has been a good learning experience. I'm glad that you've been trying, and seem to be sticking it out despite sometimes getting negative feedback here. > Next time around, I'll be sure to thoroughly comment > my code before I ask for help on it. The best documentation, especially for a language like Python, is simply logical variable names and sensible conventions. I bring this up because I'm wary of the word "thoroughly". When you're dealing with experts, it's actually best to use inline comments sparingly; they can figure out what code does. The overall problem description at the top is important, as well as explanation of "magic numbers" and such. > I really appreciate all the help that you've offered so far. You're welcome. I'm not sure how much I've helped, especially as I'm frankly not one of the stronger programmers here. > Right now, I'm debating whether I should try to reinvent > the round_robin generator part of the code or whether > there still might be a way to shuffle the results of the > generated output so that I can slice it effectively. If you are going to typically have roughly enough courts for your players (as implied by your test data), then maybe there's still a chance for using extended slicing. Just make sure you don't always pick the first element. If the number of players and the number of courts can vary wildly, and won't always match up at least sort of nicely, then the problem may require bigger guns (either much better math or much more sophisticated programming). John From metolone+gmane at gmail.com Tue May 5 01:21:29 2009 From: metolone+gmane at gmail.com (Mark Tolonen) Date: Mon, 4 May 2009 22:21:29 -0700 Subject: Question of UTF16BE encoding / decoding References: Message-ID: "Napalmski" wrote in message news:MPG.2469d7edf8bbcd0a989683 at eu.news.astraweb.com... > Hello, > > I have an encoded string in the form "004e006100700061006c006d", if you > split on every 4 characters it decodes to a single character. > I have come up with this: > > name = '004e006100700061006c006d' > name2 = "" > for x in range(0, len(name), 4): > name2 = name2 + chr(int(name[x:x+4], 16)) > > Is there a better way to do this using name.decode() that would then > also work with .encode()? > > I have tried .decode("utf-16-be"), which I beleive is the format here, > but I am getting the error: > UnicodeEncodeError: 'ascii' codec can't encode characters in position 0- > 11: ordinal not in range(128) > > And my debugger seems to show a load of unicode when it breaks. import binascii s = '004e006100700061006c006d' h = binascii.unhexlify(s) print h.decode('utf-16-be') -Mark From gagsl-py2 at yahoo.com.ar Tue May 5 01:47:31 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Tue, 05 May 2009 02:47:31 -0300 Subject: subprocess & shared environments References: <8c79f749-e648-4a2e-afad-4053a8ec5cbd@f41g2000pra.googlegroups.com> <98aa4906-5006-437a-8fc0-0eaedf433be9@3g2000yqk.googlegroups.com> Message-ID: En Mon, 04 May 2009 23:25:42 -0300, Robert Dailey escribi?: > Thanks for your help guys. Unfortunately both ideas will not work. I > guess I should have mentioned that the batch file in question is > vsvars32.bat, from the Visual Studio installation directory. I should > not modify this file, nor can I create a "wrapper" batch file as you > suggested, since I call several other batch files at different times. > There's about 6 other nmake calls I make that each is preceeded with a > call to vsvars32.bat. For now I'm using the && DOS operator, this > seems to work, however it is not ideal. There is nothing special about vsvars32.bat. If you execute it before your first program (maybe inside a .cmd that calls vsvars32 and then your program) then all the spawned programs will already see those settings. No need to invoke it over and over. Also, if you enter the same settings in the registry (so they become persistent) you don't have to worry again. -- Gabriel Genellina From joelriv at gmail.com Tue May 5 02:00:05 2009 From: joelriv at gmail.com (Joel Juvenal Rivera Rivera) Date: Tue, 05 May 2009 01:00:05 -0500 Subject: Any idea to emulate tail -f Message-ID: <1241503205.5374.25.camel@keblar.lapjoe> I want to make something very similar to the command tail -f (follow a file), i have been trying with some while True and some microsleeps (about .1 s); did someone has already done something like this? And about the file is the apache acceslog of a site with a lot of traffic. Regards joe / Joel Rivera From steven at REMOVE.THIS.cybersource.com.au Tue May 5 02:08:24 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 05 May 2009 06:08:24 GMT Subject: Self function References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> Message-ID: On Mon, 04 May 2009 17:54:50 -0400, Terry Reedy wrote: > bearophileHUGS at lycos.com wrote: > >> Another possible syntax: >> >> def fact(n): >> return 1 if n <= 1 else n * return(n - 1) >> >> But I guess most people don't see this problem as important&common >> enough to justify changing the language. > > Actually, I would like a way to refer to the current function from > inside a function. but I would like support in the language, so that > the compiler patched the code after the function object is created to > directly refer to the function object (or can the code object call the > code object?) without any name lookup at all. I don't know about avoiding name lookup, that smacks of deepest black magic, and Python doesn't usually do that. It does however do parlour tricks like `__name__` and `self`, suggests a solution. I propose a small piece of sugar. When a function is entered, Python creates an ordinary local name in the function's local namespace, and binds the function itself to that name. Two possibilities for the name are `this` or `__this__`, analogous to `self` in methods and `__name__` in modules. If there is any support for this, I propose to send the following (long) post to python-ideas. Feedback, corrections and suggestions welcome. * * * Summary: Most objects in Python don't need to know what name, or names (if any) they are bound to. Functions are a conspicuous exception to that rule: there are good reasons for a function to refer to itself, and the only straightforward way to do so is by name. I propose that on calling a function, Python should create a local name which refers to the function itself, giving the programmer the ability to have a function refer to itself without using its name. There are (at least) four reasons for a function to refer to itself. In no particular order: (1) User-friendly messages (for logging, errors or other): def parrot(): print "Error feeding cracker to function %r" % parrot (2) Recursion: def spam(n): if n < 0: return spam(-n).upper() return "spam "*n (3) Introspection: def spanish_inquisition(): print inspect.getmembers(spanish_inquisition) (4) Storing data between function calls: def cheeseshop(): if hasattr(cheeseshop, 'cheeses'): print "We have many fine cheeses" else: print "I'm sorry, I have been wasting your time" I call these self-reflective functions. In some cases there are alternatives e.g. cheeseshop could be written as a functor object, or some recursive functions can be re-written as iteration. Nevertheless such self-reflective functions are acceptable to many people, and consequently in moderately common use. How to have a function refer to itself is a common question, e.g. for newbies: http://www.mail-archive.com/tutor%40python.org/msg35114.html and even more experienced programmers: http://article.gmane.org/gmane.comp.python.general/622020 (An earlier draft of this proposal was sent to that thread.) However, none of these functions are robust in the face of the function being renamed, either at runtime or in the source code. In general, this will fail for any of the above functions: func = spam del spam func() Self-reflective functions like these are (almost?) unique in Python in that they require a known name to work correctly. You can rename a class, instance or module and expect it to continue to work, but not so for such functions. When editing source code, it is very easy to forget to change all the references to the function name within the body of itself function, even for small functions, and refactoring tools are overkill. My proposal is for Python to automatically generate a local variable named either `this` or `__this__` when entering a function. This will allow any of the above functions to be re-written using the special name, and become robust against name changes. def spanish_inquisition(): print inspect.getmembers(__this__) fang = spanish_inquisition del spanish_inquisition fang() It will also allow lambda to use recursion: lambda n: 0 if n <= 1 else __this__(n-1) (This may count as a point against it, for those who dislike lambda.) It is often useful to create two similar functions, or a variant of a function, without duplicating the source code. E.g. you might want a function that uses a cache, while still keeping around the version without a cache: cached_function = memoize(function) Currently, this does not give the expected results for recursive functions, nor does it give an error. It simply fails to behave as expected. Re-writing function() to use __this__ for the recursive call will solve that problem. Q: Will `__this__` or `this` clash with existing variables? I prefer `this` over `__this__`, for analogy with `self` inside methods. However it is more likely that existing code already uses the name `this`, since double-leading-and-trailing-underscore names are reserved for use by Python. Because `this` is an ordinary local variable, if a function assigns to it the function will work as expected. The only meaningful clash I can think of will occur when a function refers to a non-local name `this` without declaring it first. Another obvious clash may be: def function(): import this ... Both of these are likely to be very rare. Q: Will there be a performance penalty if every function defines the name `__this__` on entry? I don't expect so, but if it is found to be a problem, a slightly more sophisticated strategy would be to only define `__this__` inside the function if the body of the function refers to that variable. That will allow the majority of functions which are not self-reflective to avoid paying that (probably minuscule) cost. Q: Is this really necessary? This is sugar, a convenience for the programmer. None of the problems it solves cannot be solved, or worked around, by other methods. Nevertheless, that the question "how do I have my function refer to itself?" keeps being asked over and over again suggests that the current solutions are (1) not obvious to newbies, and (2) not entirely satisfactory to more experienced programmers. Here is a proof-of-concept pure Python implementation, using a decorator, and abusing a global variable. This is NOT to imply that `__this__` should be a global if this proposal goes ahead. from functools import wraps def make_this(func): global __this__ __this__ = func @wraps(func) def f(*args, **kwargs): return func(*args, **kwargs) return f >>> @make_this ... def spam(n): ... if n < 0: return __this__(-n).upper() ... return ' '.join([__this__.__name__] * n) ... >>> tasty_meat_like_product = spam >>> del spam >>> tasty_meat_like_product(-3) 'SPAM SPAM SPAM' -- Steven From pavlovevidence at gmail.com Tue May 5 02:09:25 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Mon, 4 May 2009 23:09:25 -0700 (PDT) Subject: Self function References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> <202a05cd-3ce4-41fb-943b-5fd3f18d9bbb@z5g2000vba.googlegroups.com> <8c18a63a-8ca5-44bd-9b47-c1247a8f1a97@v4g2000vba.googlegroups.com> Message-ID: <37e0021f-b50c-4f0c-8ba5-0beed01da029@z16g2000prd.googlegroups.com> On May 4, 8:22?pm, Steven D'Aprano wrote: > On Mon, 04 May 2009 15:51:15 -0700, Carl Banks wrote: > > All > > recursion does it make what you're doing a lot less readable for almost > > all programmers. > > What nonsense. It's not nonsense for a singly-linked list. I don't need to be taught the benefit of recursion, but whenever interation is suitable for an algorithm/data structure, as it is with singly-linked lists, then it is the more readable and more preferrable choice, especially in Python. In Python the One Obvious Way is iteration when possible, recursion when necessary. Carl Banks From ur.liam at iksmlapan.reverse Tue May 5 02:20:14 2009 From: ur.liam at iksmlapan.reverse (Napalmski) Date: Tue, 5 May 2009 07:20:14 +0100 Subject: Question of UTF16BE encoding / decoding References: Message-ID: In article , metolone+gmane at gmail.com says... > > > import binascii > s = '004e006100700061006c006d' > h = binascii.unhexlify(s) > print h.decode('utf-16-be') > > -Mark And: name2 = name2.encode("utf-16-be") print binascii.b2a_hex(name2) to re-encode, Thank you, much appreciated! From sri_annauni at yahoo.co.in Tue May 5 02:41:06 2009 From: sri_annauni at yahoo.co.in (srinivasan srinivas) Date: Tue, 5 May 2009 12:11:06 +0530 (IST) Subject: Which one is best Python or Java for developing GUI applications? Message-ID: <793408.86105.qm@web7903.mail.in.yahoo.com> Could you tell me does Python have any advantages over Java for the development of GUI applications? Thanks, Srini Now surf faster and smarter ! Check out the new Firefox 3 - Yahoo! Edition http://downloads.yahoo.com/in/firefox/?fr=om_email_firefox From clp2 at rebertia.com Tue May 5 02:46:58 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Mon, 4 May 2009 23:46:58 -0700 Subject: Self function In-Reply-To: References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> Message-ID: <50697b2c0905042346u482eabf9j1bb9f95853f38864@mail.gmail.com> On Mon, May 4, 2009 at 11:08 PM, Steven D'Aprano wrote: > On Mon, 04 May 2009 17:54:50 -0400, Terry Reedy wrote: > >> bearophileHUGS at lycos.com wrote: >> >>> Another possible syntax: >>> >>> def fact(n): >>> ? ? return 1 if n <= 1 else n * return(n - 1) >>> >>> But I guess most people don't see this problem as important&common >>> enough to justify changing the language. >> >> Actually, I would like a way to refer to the current function from >> inside a function. ?but I would like support in the language, so that >> the compiler patched the code after the function object is created to >> directly refer to the function object (or can the code object call the >> code object?) without any name lookup at all. > > I don't know about avoiding name lookup, that smacks of deepest black > magic, and Python doesn't usually do that. It does however do parlour > tricks like `__name__` and `self`, suggests a solution. > > I propose a small piece of sugar. When a function is entered, Python > creates an ordinary local name in the function's local namespace, and > binds the function itself to that name. Two possibilities for the name > are `this` or `__this__`, analogous to `self` in methods and `__name__` > in modules. > > If there is any support for this, I propose to send the following (long) > post to python-ideas. Feedback, corrections and suggestions welcome. While I'm +0 on the proto-PEP, it doesn't currently address (or at least rebut hard enough) the inevitable obvious naysayer argument which will go along the lines of: Adding syntax is EVIL(tm) for it angers the Gods of Backwards Compatibility, and this proposal is completely unnecessary because you could instead just write: def recursive(func): """We caved enough to allow this into functools.""" def wrapper(*args, **kwds): return func(func, *args, **kwds) return wrapper ##### from functools import recursive @recursive def spam(this, number_of_thy_counting): print " ".join([this.__name__.capitalize() + "!"]*number_of_thy_counting) Which is not significantly more complex/longer/painful than learning about __this__'s existence or typing the extra underscores and it doesn't require any new syntax. And the performance penalty for the proposed feature would actually be horrible! And there would be much clashing with existing variable names, for keywords are the Devil's work! And your mother wears combat boots! (etc...) You touch on this in "Is this really necessary?", but I think you're going to prove more persuasively how drastically sweeter this syntax sugar would be. Cheers, Chris -- The Devil's Advocate-General http://blog.rebertia.com From debatem1 at gmail.com Tue May 5 02:50:52 2009 From: debatem1 at gmail.com (CTO) Date: Mon, 4 May 2009 23:50:52 -0700 (PDT) Subject: Any idea to emulate tail -f References: Message-ID: <99114d6c-9d12-4b74-9b7c-ce32377a8b61@21g2000vbk.googlegroups.com> On May 5, 2:00?am, Joel Juvenal Rivera Rivera wrote: > I want to make something very similar to ?the command tail -f (follow a > file), i have been trying ?with some while True and some microsleeps > (about .1 s); did someone has already done something like this? > > And about the file is the apache acceslog ?of a site with a lot of > traffic. > > Regards ? ? > > joe / Joel Rivera You might want to try http://pyinotify.sourceforge.net/. Works well on Linux systems. Otherwise, I'd watch the mtime on the file and fork to handle the change. Geremy Condra From pavlovevidence at gmail.com Tue May 5 02:55:41 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Mon, 4 May 2009 23:55:41 -0700 (PDT) Subject: Self function References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> <202a05cd-3ce4-41fb-943b-5fd3f18d9bbb@z5g2000vba.googlegroups.com> <8c18a63a-8ca5-44bd-9b47-c1247a8f1a97@v4g2000vba.googlegroups.com> Message-ID: <04ed259e-01cf-459b-8664-a1dd6a23d60e@d19g2000prh.googlegroups.com> On May 4, 8:26?pm, Steven D'Aprano wrote: > On Mon, 04 May 2009 16:33:13 -0700, Carl Banks wrote: > > On May 4, 4:06?pm, bearophileH... at lycos.com wrote: > >> Carl Banks: > > >> >1. Singly-linked lists can and should be handled with iteration.< > > >> I was talking about a binary tree with list-like topology, of course. > > > "(every node has 1 child, and they are chained)" > > > That's a singly-linked list, not a tree. ?It's a degenerate tree at > > best. > > A singly-linked list is a degenerate tree. Not "at best", but "is". No, many implemenations of singly-linked lists aren't trees by any definition. You are probably thinking of the bastardized list-tree spawn they use in Lisp. But many implementations don't even bother with a cons cell and just have the object itself refer to the next object. That is not a tree. But even singly-linked lists implemented with cons cells can be handled, and are better handled, with interation. > >> >All recursion does it make what you're doing a lot less readable for > >> >almost all programmers.< > > >> I can't agree. > > > If every child has one node you don't need recursion. > > And if one single node in the entire tree has two children, you do. Then it't not a singly-linked list. It's a tree. > What > are you suggesting, that Bearophile should write his tree-processing code > to only deal with the degenerate case? I'm suggesting that Bearophile should write recursive code for his trees and iterative code for his lists. > >> If the data structure is recursive (like a tree, or even sometimes > >> graphs, etc) a recursive algorithm is shorter, more readable and more > >> natural. > > > Because that's a tree, not a linked-list. > > And a linked list is a degenerate tree. If you have a non-self-balancing > tree, sometimes it will be degenerate, and your code needs to deal with > it. If you have a tree, then you use recursive code. If you have a list you use iterative code. > > Which is germane because Python's recursion limit is the thing you're > > complaining about here, and you don't normally hit that limit with real > > trees because they rarely go 1000 deep. > > And if just ONE branch of the tree goes 1000 deep, even if the rest of > the tree is shallow, then you hit the default recursion limit. Yeah, well, see that's just the point here. Bearophile was asked if any of his trees go 1000 deep, he said yes, his singly-linked lists do. Well, I'm sorry, that's not going to convince me, because bearophile should be using iteration for the singly-linked lists. Bearophile might very well have real trees that are 1000 deep, but that's not going to convince me either, because IMO real trees rarely do that, and Python's recursion limit can be increased in the rare cases they do. Bearophile and you are welcome to try to convince me that there are lots of real trees out there that can't be handled with iteration and that do go 1000 deep, and that this is a common enough problem that Python should drastically improve support for recursion. Until then I will opine that it is adequate now for almost all purposes. > > Singly-linked lists don't count because you don't need recursion for > > them. > > If each node has two (or more) child fields, even if only one of those > children is non-empty, then your data structure is a degenerate tree and > does count. If one of the fields is always empty, you don't need recursion to deal with it. > > [snip strawman example] > > It's not a strawman just because you don't like it. No, it's a strawman because I said singly-linked lists don't need recursion, and his counterexample was showing that recursion was useful a tree. Which was true, but it wasn't answering my argument. > Dealing with > degenerate trees is a genuine issue, and avoiding degenerate trees is the > reason why people have developed more complicated structures like red- > black trees. I'm not talking about degenerate trees. I'm talking about singly- linked lists, which you DO NOT NEED, and SHOULD NOT USE, recursion to deal with. Carl Banks From arnodel at googlemail.com Tue May 5 03:09:27 2009 From: arnodel at googlemail.com (Arnaud Delobelle) Date: Tue, 5 May 2009 00:09:27 -0700 (PDT) Subject: Self function References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> Message-ID: <4b6c0d49-5a05-42ff-8990-9a483c0f62b9@g20g2000vba.googlegroups.com> On 5 May, 07:08, Steven D'Aprano wrote: > On Mon, 04 May 2009 17:54:50 -0400, Terry Reedy wrote: > > bearophileH... at lycos.com wrote: > > >> Another possible syntax: > > >> def fact(n): > >> ? ? return 1 if n <= 1 else n * return(n - 1) > > >> But I guess most people don't see this problem as important&common > >> enough to justify changing the language. > > > Actually, I would like a way to refer to the current function from > > inside a function. ?but I would like support in the language, so that > > the compiler patched the code after the function object is created to > > directly refer to the function object (or can the code object call the > > code object?) without any name lookup at all. > > I don't know about avoiding name lookup, that smacks of deepest black > magic, and Python doesn't usually do that. It does however do parlour > tricks like `__name__` and `self`, suggests a solution. > > I propose a small piece of sugar. When a function is entered, Python > creates an ordinary local name in the function's local namespace, and > binds the function itself to that name. Two possibilities for the name > are `this` or `__this__`, analogous to `self` in methods and `__name__` > in modules. > > If there is any support for this, I propose to send the following (long) > post to python-ideas. Feedback, corrections and suggestions welcome. > > * * * > > Summary: > > Most objects in Python don't need to know what name, or names (if any) > they are bound to. Functions are a conspicuous exception to that rule: > there are good reasons for a function to refer to itself, and the only > straightforward way to do so is by name. I propose that on calling a > function, Python should create a local name which refers to the function > itself, giving the programmer the ability to have a function refer to > itself without using its name. > > There are (at least) four reasons for a function to refer to itself. In > no particular order: > > (1) User-friendly messages (for logging, errors or other): > > def parrot(): > ? ? print "Error feeding cracker to function %r" % parrot > > (2) Recursion: > > def spam(n): > ? ? if n < 0: return spam(-n).upper() > ? ? return "spam "*n > > (3) Introspection: > > def spanish_inquisition(): > ? ? print inspect.getmembers(spanish_inquisition) > > (4) Storing data between function calls: > > def cheeseshop(): > ? ? if hasattr(cheeseshop, 'cheeses'): > ? ? ? ? print "We have many fine cheeses" > ? ? else: > ? ? ? ? print "I'm sorry, I have been wasting your time" > > I call these self-reflective functions. > > In some cases there are alternatives e.g. cheeseshop could be written as > a functor object, or some recursive functions can be re-written as > iteration. Nevertheless such self-reflective functions are acceptable to > many people, and consequently in moderately common use. How to have a > function refer to itself is a common question, e.g. for newbies: > > http://www.mail-archive.com/tutor%40python.org/msg35114.html > > and even more experienced programmers: > > http://article.gmane.org/gmane.comp.python.general/622020 > > (An earlier draft of this proposal was sent to that thread.) > > However, none of these functions are robust in the face of the function > being renamed, either at runtime or in the source code. In general, this > will fail for any of the above functions: > > func = spam > del spam > func() > > Self-reflective functions like these are (almost?) unique in Python in > that they require a known name to work correctly. You can rename a class, > instance or module and expect it to continue to work, but not so for such > functions. When editing source code, it is very easy to forget to change > all the references to the function name within the body of itself > function, even for small functions, and refactoring tools are overkill. > > My proposal is for Python to automatically generate a local variable > named either `this` or `__this__` when entering a function. This will > allow any of the above functions to be re-written using the special name, > and become robust against name changes. > > def spanish_inquisition(): > ? ? print inspect.getmembers(__this__) > > fang = spanish_inquisition > del spanish_inquisition > fang() > > It will also allow lambda to use recursion: > > lambda n: 0 if n <= 1 else __this__(n-1) > > (This may count as a point against it, for those who dislike lambda.) > > It is often useful to create two similar functions, or a variant of a > function, without duplicating the source code. E.g. you might want a > function that uses a cache, while still keeping around the version > without a cache: > > cached_function = memoize(function) > > Currently, this does not give the expected results for recursive > functions, nor does it give an error. It simply fails to behave as > expected. Re-writing function() to use __this__ for the recursive call > will solve that problem. > > Q: Will `__this__` or `this` clash with existing variables? > > I prefer `this` over `__this__`, for analogy with `self` inside methods. > However it is more likely that existing code already uses the name > `this`, since double-leading-and-trailing-underscore names are reserved > for use by Python. Because `this` is an ordinary local variable, if a > function assigns to it the function will work as expected. The only > meaningful clash I can think of will occur when a function refers to a > non-local name `this` without declaring it first. Another obvious clash > may be: > > def function(): > ? ? import this > ? ? ... > > Both of these are likely to be very rare. > > Q: Will there be a performance penalty if every function defines the name > `__this__` on entry? > > I don't expect so, but if it is found to be a problem, a slightly more > sophisticated strategy would be to only define `__this__` inside the > function if the body of the function refers to that variable. That will > allow the majority of functions which are not self-reflective to avoid > paying that (probably minuscule) cost. > > Q: Is this really necessary? > > This is sugar, a convenience for the programmer. None of the problems it > solves cannot be solved, or worked around, by other methods. > Nevertheless, that the question "how do I have my function refer to > itself?" keeps being asked over and over again suggests that the current > solutions are (1) not obvious to newbies, and (2) not entirely > satisfactory to more experienced programmers. > > Here is a proof-of-concept pure Python implementation, using a decorator, > and abusing a global variable. This is NOT to imply that `__this__` > should be a global if this proposal goes ahead. > > from functools import wraps > def make_this(func): > ? ? global __this__ > ? ? __this__ = func > ? ? @wraps(func) > ? ? def f(*args, **kwargs): > ? ? ? ? return func(*args, **kwargs) > ? ? return f > > >>> @make_this > > ... def spam(n): > ... ? ? if n < 0: return __this__(-n).upper() > ... ? ? return ' '.join([__this__.__name__] * n) > ...>>> tasty_meat_like_product = spam > >>> del spam > >>> tasty_meat_like_product(-3) > > 'SPAM SPAM SPAM' > > -- > Steven- Hide quoted text - > > - Show quoted text - Are you aware of PEP 3130? http://www.python.org/dev/peps/pep-3130/ (BTW, it seems to me that your implementation breaks as soon as two functions are decorated - not tested!) -- Arnaud From debatem1 at gmail.com Tue May 5 03:12:40 2009 From: debatem1 at gmail.com (CTO) Date: Tue, 5 May 2009 00:12:40 -0700 (PDT) Subject: Self function References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> Message-ID: <611eb164-5eff-4021-8b1c-b45c0d85462f@l5g2000vbc.googlegroups.com> On May 5, 2:08?am, Steven D'Aprano wrote: > On Mon, 04 May 2009 17:54:50 -0400, Terry Reedy wrote: > > bearophileH... at lycos.com wrote: > > >> Another possible syntax: > > >> def fact(n): > >> ? ? return 1 if n <= 1 else n * return(n - 1) > > >> But I guess most people don't see this problem as important&common > >> enough to justify changing the language. > > > Actually, I would like a way to refer to the current function from > > inside a function. ?but I would like support in the language, so that > > the compiler patched the code after the function object is created to > > directly refer to the function object (or can the code object call the > > code object?) without any name lookup at all. > > I don't know about avoiding name lookup, that smacks of deepest black > magic, and Python doesn't usually do that. It does however do parlour > tricks like `__name__` and `self`, suggests a solution. > > I propose a small piece of sugar. When a function is entered, Python > creates an ordinary local name in the function's local namespace, and > binds the function itself to that name. Two possibilities for the name > are `this` or `__this__`, analogous to `self` in methods and `__name__` > in modules. > > If there is any support for this, I propose to send the following (long) > post to python-ideas. Feedback, corrections and suggestions welcome. [snip proposal] I'm not all that in favor of this, but I think I've got another use case for you at http://code.activestate.com/recipes/576731/. The functions written to use it would be a lot more standard looking at least. Geremy Condra From http Tue May 5 03:22:48 2009 From: http (Paul Rubin) Date: 05 May 2009 00:22:48 -0700 Subject: Self function References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> Message-ID: <7xk54wvws7.fsf@ruckus.brouhaha.com> bearophileHUGS at lycos.com writes: > This happens to me more than one time every year. > So I have written this: > ... > self_name = getframeinfo(currentframe()).function ZOMG, you've got to be kidding. I'd expect Pylint to catch that sort of error statically. If not, the best approach is probably to extend Pylint to handle those cases. The frame crawling stuff just comes across as madness to me. From http Tue May 5 03:23:21 2009 From: http (Paul Rubin) Date: 05 May 2009 00:23:21 -0700 Subject: Which one is best Python or Java for developing GUI applications? References: Message-ID: <7xfxfkvwra.fsf@ruckus.brouhaha.com> srinivasan srinivas writes: > Could you tell me does Python have any advantages over Java for the development of GUI applications? Yes. From paul.nospam at rudin.co.uk Tue May 5 03:26:56 2009 From: paul.nospam at rudin.co.uk (Paul Rudin) Date: Tue, 05 May 2009 08:26:56 +0100 Subject: Which one is best Python or Java for developing GUI applications? References: <7xfxfkvwra.fsf@ruckus.brouhaha.com> Message-ID: <87d4ao9fi7.fsf@rudin.co.uk> Paul Rubin writes: > srinivasan srinivas writes: >> Could you tell me does Python have any advantages over Java for the >> development of GUI applications? > > Yes. Clearly c.l.p needs to adopt the SNB convention :) From gallium.arsenide at gmail.com Tue May 5 03:32:03 2009 From: gallium.arsenide at gmail.com (John Yeung) Date: Tue, 5 May 2009 00:32:03 -0700 (PDT) Subject: Code works fine except... References: <8d4ec1df-dddb-469a-99a1-695152db7c04@n4g2000vba.googlegroups.com> <7f3493ca-efcb-48cc-aa29-487bb11fcd09@p4g2000vba.googlegroups.com> <16d47b2f-14d4-4266-84d2-c26501beb0c1@r34g2000vbi.googlegroups.com> <1691f042-bc14-46b9-b7b3-5a44348e9965@b1g2000vbc.googlegroups.com> <1a497471-498a-43d8-90ba-d676e8acf68b@r36g2000vbr.googlegroups.com> <8b49b775-66a7-42d5-9b00-cf23d9b044cc@e23g2000vbe.googlegroups.com> <54d76f11-c255-487d-9c3a-aaa380d7827a@z7g2000vbh.googlegroups.com> Message-ID: <991ccae3-f3e2-4d73-bd32-bc6f560e6f5f@n8g2000vbb.googlegroups.com> On May 5, 1:12?am, John Yeung wrote: > [...] the problem may require bigger guns (either much better > math or much more sophisticated programming). Yes, I'm responding to myself. Well, I went ahead with the approach I mentioned earlier, generating all possible matches and then selecting among them as needed to fill up the courts, trying to keep the number of matches played by each player as fair as possible. (I should mention that this, or something similar, was suggested earlier by someone else in a different thread, in response to the same question by the same OP.) I did use "bigger guns" (mainly a class for player objects, with custom __cmp__ method), but still didn't do anything with doubles. I haven't tested it much, but I'll post it if anyone's interested. (That way people can pick on me instead of the OP. ;) John From bruno.42.desthuilliers at websiteburo.invalid Tue May 5 03:37:01 2009 From: bruno.42.desthuilliers at websiteburo.invalid (Bruno Desthuilliers) Date: Tue, 05 May 2009 09:37:01 +0200 Subject: Personal recommendations for Python and Django on-boarding materials In-Reply-To: References: <61ac6511-0f9e-458d-b3f1-5b153939293f@r34g2000vba.googlegroups.com> Message-ID: <49ffec97$0$27576$426a74cc@news.free.fr> Arnaud Delobelle a ?crit : > Grant Rettke writes: > >> Hi folks, >> >> From one developer to another, I am looking for some personal >> recommendations on what are the best materials for fast tracking an on- >> boarding to Python and Django. >> >> I know how to program, get web apps and OO; this is the audience. >> >> I have found some good recommendations and stuff on Amazon and various >> websites, but if I could get some humans vouching for things directly >> that would be worth a lot more. >> >> Best wishes, >> >> Grant > > I've learnt both Python and Django using the tutorials, the docs and > lots of experimentation. > Django now have a book [1] which might be > useful to read. > > [1] http://djangobook.com/ > You may also want to join the django-users group, browse django-snippets, and read James Bennet's blog. From bruno.42.desthuilliers at websiteburo.invalid Tue May 5 03:37:41 2009 From: bruno.42.desthuilliers at websiteburo.invalid (Bruno Desthuilliers) Date: Tue, 05 May 2009 09:37:41 +0200 Subject: Which one is best Python or Java for developing GUI applications? In-Reply-To: <7xfxfkvwra.fsf@ruckus.brouhaha.com> References: <7xfxfkvwra.fsf@ruckus.brouhaha.com> Message-ID: <49ffecbf$0$27576$426a74cc@news.free.fr> Paul Rubin a ?crit : > srinivasan srinivas writes: >> Could you tell me does Python have any advantages over Java for the development of GUI applications? > > Yes. Indeed. From mail at microcorp.co.za Tue May 5 03:41:37 2009 From: mail at microcorp.co.za (Hendrik van Rooyen) Date: Tue, 5 May 2009 09:41:37 +0200 Subject: Tkinter, Trouble with Message,Label widget References: <4f61c431-1c78-4de7-9b82-08eee09ae7d6@s20g2000vbp.googlegroups.com> <49FF853B.7040404@hughes.net> Message-ID: <009201c9cd54$f1ac07e0$0d00a8c0@Hendrik> "norseman" wrote: > Hendrik van Rooyen mentioned the textvar too. Thanks Hendrik Yeah - and also omitted to mention the set method. With friends like that, who needs enemies? - Hendrik From QJing.Li at gmail.com Tue May 5 03:50:44 2009 From: QJing.Li at gmail.com (Leon) Date: Tue, 5 May 2009 00:50:44 -0700 (PDT) Subject: Which one is best Python or Java for developing GUI applications? References: Message-ID: <4da39bb0-7bfe-48bf-a843-0e79e567a56d@r3g2000vbp.googlegroups.com> I think there are two advantages over java for GUI application First, python is more productive and has very rich third modules support, you can check the demo of wxPython. Second, you can develop native-looking GUI BTW: I'm developing GUI application using python and wxPython. Second, On May 4, 11:41?pm, srinivasan srinivas wrote: > Could you tell me does Python have any advantages over Java for the development of GUI applications? > > Thanks, > Srini > > ? ? ? Now surf faster and smarter ! Check out the new Firefox 3 - Yahoo! Editionhttp://downloads.yahoo.com/in/firefox/?fr=om_email_firefox From steven at REMOVE.THIS.cybersource.com.au Tue May 5 03:51:19 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 05 May 2009 07:51:19 GMT Subject: Self function References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> <202a05cd-3ce4-41fb-943b-5fd3f18d9bbb@z5g2000vba.googlegroups.com> <8c18a63a-8ca5-44bd-9b47-c1247a8f1a97@v4g2000vba.googlegroups.com> <37e0021f-b50c-4f0c-8ba5-0beed01da029@z16g2000prd.googlegroups.com> Message-ID: On Mon, 04 May 2009 23:09:25 -0700, Carl Banks wrote: > On May 4, 8:22?pm, Steven D'Aprano > wrote: >> On Mon, 04 May 2009 15:51:15 -0700, Carl Banks wrote: >> > All >> > recursion does it make what you're doing a lot less readable for >> > almost all programmers. >> >> What nonsense. > > It's not nonsense for a singly-linked list. def ivisit(node): print node while node and node.link is not None: node = node.link print node def rvisit(node): print node if node and node.link is not None: rvisit(node.link) If there are programmers who find rvisit "a lot less readable" than ivisit, then in my arrogant opinion they should consider a change of profession. > I don't need to be taught > the benefit of recursion, but whenever interation is suitable for an > algorithm/data structure, as it is with singly-linked lists, then it is > the more readable and more preferrable choice, especially in Python. Most (all?) recursive algorithms can be re-written as iteration. For many recursive algorithms (but not all) the cost for doing so is to simulate recursion yourself by managing your own stack. By making such an absolutist claim, you're claiming that it is "more readable" and "more preferable" to manage your own stack than to let the compiler do so. That's clearly nonsense. Whenever iteration gives a simpler and more readable algorithm than recursion, then it should be preferred on account of being simpler and more readable. That's not just a no-brainer, it's a tautology. "Whenever iteration is simpler, it's simpler." But that's a far cry from what you said: that recursion, as a general technique, is "a lot less readable" for "almost all" programmers. > In Python the One Obvious Way is iteration when possible, recursion when > necessary. There's nothing "obvious" about solving the 8 Queens problem using iteration. Or walking a binary tree. Or generating all the permutations of a list. But don't just tell me that recursion isn't Pythonic. Tell Guido: http://www.python.org/doc/essays/graphs.html I quote: "These [recursive] functions are about as simple as they get. Yet, they are nearly optimal (for code written in Python)." -- Steven From steven at REMOVE.THIS.cybersource.com.au Tue May 5 03:58:15 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 05 May 2009 07:58:15 GMT Subject: Self function References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> <4b6c0d49-5a05-42ff-8990-9a483c0f62b9@g20g2000vba.googlegroups.com> Message-ID: On Tue, 05 May 2009 00:09:27 -0700, Arnaud Delobelle wrote: > Are you aware of PEP 3130? http://www.python.org/dev/peps/pep-3130/ I am now. Very disappointing. > (BTW, it seems to me that your implementation breaks as soon as two > functions are decorated - not tested!) Of course it does! That's why I warned that I was "abusing a global variable", and that it should not be read as meaning that I wanted __this__ to be global. I want it to be local to the function. If somebody can tell me how to inject a new local name into a function, that would be far preferable to using global in the decorator. -- Steven From pavlovevidence at gmail.com Tue May 5 04:25:49 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Tue, 5 May 2009 01:25:49 -0700 (PDT) Subject: Self function References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> <202a05cd-3ce4-41fb-943b-5fd3f18d9bbb@z5g2000vba.googlegroups.com> <8c18a63a-8ca5-44bd-9b47-c1247a8f1a97@v4g2000vba.googlegroups.com> <37e0021f-b50c-4f0c-8ba5-0beed01da029@z16g2000prd.googlegroups.com> Message-ID: <5a8c7e8b-8b3d-4a47-9ac6-23bc85b512f4@d7g2000prl.googlegroups.com> On May 5, 12:51?am, Steven D'Aprano wrote: > On Mon, 04 May 2009 23:09:25 -0700, Carl Banks wrote: > > In Python the One Obvious Way is iteration when possible, recursion when > > necessary. > > There's nothing "obvious" about solving the 8 Queens problem using > iteration. Or walking a binary tree. Or generating all the permutations > of a list. *Sigh* Well, I'm out of this debate. Apparently it's not possible to argue that recursivie algorithms should be avoided *sometimes* without everyone citing cases that obviously aren't from those times (as if I had been arguing that recursion should be avoided all the time). Here's a parting thought for you to cite "counterexamples" of: Iteration should be used instead of recursion anywhere a tail- recursive algorithm is possible. Recursion should be used only when tail-recursion is not possible. Carl Banks From pscott at uwc.ac.za Tue May 5 04:32:38 2009 From: pscott at uwc.ac.za (Paul Scott) Date: Tue, 05 May 2009 10:32:38 +0200 Subject: Any idea to emulate tail -f In-Reply-To: <99114d6c-9d12-4b74-9b7c-ce32377a8b61@21g2000vbk.googlegroups.com> References: <99114d6c-9d12-4b74-9b7c-ce32377a8b61@21g2000vbk.googlegroups.com> Message-ID: <1241512358.6541.16.camel@paul-laptop> On Mon, 2009-05-04 at 23:50 -0700, CTO wrote: > You might want to try http://pyinotify.sourceforge.net/. Works well on > Linux systems. Otherwise, I'd watch the mtime on the file and fork to > handle the change. > pyinotify works really well. If you need to watch a file, simply use the IN_MODIFY watch to fire off a notification when the file changes. Then you can use that event in anything you may need. I put up some basic pyinotify usage here: http://www.paulscott.za.net/index.php?module=jabberblog&postid=ps123_2560_1240747637&action=viewsingle but if you need some more help with it, read the docs and then ask me also -- Paul http://www.paulscott.za.net http://twitter.com/paulscott56 http://avoir.uwc.ac.za -- From wolfram.hinderer at googlemail.com Tue May 5 04:35:17 2009 From: wolfram.hinderer at googlemail.com (wolfram.hinderer at googlemail.com) Date: Tue, 5 May 2009 01:35:17 -0700 (PDT) Subject: Self function References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> Message-ID: <1292fea0-96fb-4a21-b39d-b6ce610ea6b2@t10g2000vbg.googlegroups.com> On 5 Mai, 08:08, Steven D'Aprano wrote: > Self-reflective functions like these are (almost?) unique in Python in > that they require a known name to work correctly. You can rename a class, > instance or module and expect it to continue to work, but not so for such > functions. When editing source code, it is very easy to forget to change > all the references to the function name within the body of itself > function, even for small functions, and refactoring tools are overkill. It is easy to change all references of the function name, except for those in the function body itself? That needs some explantation. > It is often useful to create two similar functions, or a variant of a > function, without duplicating the source code. E.g. you might want a > function that uses a cache, while still keeping around the version > without a cache: > > cached_function = memoize(function) > > Currently, this does not give the expected results for recursive > functions, nor does it give an error. It simply fails to behave as > expected. Re-writing function() to use __this__ for the recursive call > will solve that problem. Won't __this__ (inside function) still refer to function, not cached_function? > Here is a proof-of-concept pure Python implementation, using a decorator, > and abusing a global variable. This is NOT to imply that `__this__` > should be a global if this proposal goes ahead. > > from functools import wraps > def make_this(func): > ? ? global __this__ > ? ? __this__ = func > ? ? @wraps(func) > ? ? def f(*args, **kwargs): > ? ? ? ? return func(*args, **kwargs) > ? ? return f This still has the memoizing problem. From steven at REMOVE.THIS.cybersource.com.au Tue May 5 04:37:28 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 05 May 2009 08:37:28 GMT Subject: Self function References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> <202a05cd-3ce4-41fb-943b-5fd3f18d9bbb@z5g2000vba.googlegroups.com> <8c18a63a-8ca5-44bd-9b47-c1247a8f1a97@v4g2000vba.googlegroups.com> <04ed259e-01cf-459b-8664-a1dd6a23d60e@d19g2000prh.googlegroups.com> Message-ID: On Mon, 04 May 2009 23:55:41 -0700, Carl Banks wrote: > On May 4, 8:26?pm, Steven D'Aprano > wrote: >> On Mon, 04 May 2009 16:33:13 -0700, Carl Banks wrote: >> > On May 4, 4:06?pm, bearophileH... at lycos.com wrote: >> >> Carl Banks: >> >> >> >1. Singly-linked lists can and should be handled with iteration.< >> >> >> I was talking about a binary tree with list-like topology, of >> >> course. >> >> > "(every node has 1 child, and they are chained)" >> >> > That's a singly-linked list, not a tree. ?It's a degenerate tree at >> > best. >> >> A singly-linked list is a degenerate tree. Not "at best", but "is". > > No, many implemenations of singly-linked lists aren't trees by any > definition. I would say not. Nodes with a single link field can't form a tree, because you can't give it a left and right child. However trees can degenerate into a singly-linked list, where each node has at least one unused child. That is what both Bearophile and I are trying to tell you. It's not that anyone sane thinks "I know, I'll use a binary tree to implement a linked list" -- that would be stupid, and shame on you for thinking that Bearophile is that stupid. But if you insert data into a non-self-balancing tree, sometimes the tree will degenerate into a linked list with extra, unused child links. [...] > But even singly-linked lists implemented with cons cells can be handled, > and are better handled, with interation. Can be, certainly. Better? Maybe. >> >> >All recursion does it make what you're doing a lot less readable >> >> >for almost all programmers.< >> >> >> I can't agree. >> >> > If every child has one node you don't need recursion. >> >> And if one single node in the entire tree has two children, you do. > > Then it't not a singly-linked list. It's a tree. It was already a tree. It's just that the entire tree happened to form one long chain. >> What >> are you suggesting, that Bearophile should write his tree-processing >> code to only deal with the degenerate case? > > I'm suggesting that Bearophile should write recursive code for his trees > and iterative code for his lists. But his tree-handling recursive code MUST and WILL operate on degenerate trees that form chains (linked lists), unless he writes more complicated code to avoid it (e.g. red-black trees). For simple trees, you don't have the luxury of saying "Oh, my trees will never be degenerate, they'll always be optimal, with the minimum depth possible." You get the trees you're given, and sometimes they'll be one long branch with no, or very fewer, off-shoots, and you'll have O(N) performance instead of O(log N). And the clever thing? Just write the recursive code, as normal, and it will magically handle the degenerate case too. >> >> If the data structure is recursive (like a tree, or even sometimes >> >> graphs, etc) a recursive algorithm is shorter, more readable and >> >> more natural. >> >> > Because that's a tree, not a linked-list. >> >> And a linked list is a degenerate tree. If you have a >> non-self-balancing tree, sometimes it will be degenerate, and your code >> needs to deal with it. > > If you have a tree, then you use recursive code. If you have a list you > use iterative code. I wish to retract my poorly worded comment "And a linked list is a degenerate tree". That is incorrect. What I should have said is that a degenerate tree behaves equivalently to a linked list, rather than "is". >> > Which is germane because Python's recursion limit is the thing you're >> > complaining about here, and you don't normally hit that limit with >> > real trees because they rarely go 1000 deep. >> >> And if just ONE branch of the tree goes 1000 deep, even if the rest of >> the tree is shallow, then you hit the default recursion limit. > > Yeah, well, see that's just the point here. Bearophile was asked if any > of his trees go 1000 deep, he said yes, his singly-linked lists do. He certainly did not say anything of the sort. > Well, I'm sorry, that's not going to convince me, because bearophile > should be using iteration for the singly-linked lists. What Bearophile actually wrote was: "You are thinking just about complete binary trees. But consider that a topology LIKE a single linked list (every node has 1 child, and they are chained) is a true binary tree still." [Emphasis added.] It should be obvious what he means. But if, by some chance, you misunderstood what he said, he replied to your earlier post and explained further: "I was talking about a binary tree with list-like topology, of course." If you don't understand what a binary-tree with a list-like topology is, then I respectfully suggest that you are unqualified to have an opinion in this discussion and you should come back when you've learned what a binary-tree with a list-like topology actually is, and why your suggestion that he use iteration on linked lists is, to quote Wolfgang Pauli, Not Even Wrong. > Bearophile might very well have real trees that are 1000 deep, but > that's not going to convince me either, because IMO real trees rarely do > that, and Python's recursion limit can be increased in the rare cases > they do. Are you saying that Bearophile's trees are imaginary? That's he's making them up? Personally, I don't think it's good to have a function fail just because it has to recurse over a mere 1000 items. I think that's as poor as some hypothetical language where you can't iterate over more than 1000 items, and if you try, you have to catch the exception, increase the iteration limit, and try again. People wouldn't stand for it. Oh, I understand *why* Python has that limitation. It's just a black mark on an otherwise wonderful language. [...] >> > Singly-linked lists don't count because you don't need recursion for >> > them. >> >> If each node has two (or more) child fields, even if only one of those >> children is non-empty, then your data structure is a degenerate tree >> and does count. > > If one of the fields is always empty, you don't need recursion to deal > with it. That's just silly. How do you know that one of the fields is always empty until you've walked the tree? [...] > I'm not talking about degenerate trees. I'm talking about singly- > linked lists, which you DO NOT NEED, and SHOULD NOT USE, recursion to > deal with. Ah, then you are the one introducing the straw man, because Bearophile is talking about trees. -- Steven From steven at REMOVE.THIS.cybersource.com.au Tue May 5 04:46:41 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 05 May 2009 08:46:41 GMT Subject: Self function References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> <202a05cd-3ce4-41fb-943b-5fd3f18d9bbb@z5g2000vba.googlegroups.com> <8c18a63a-8ca5-44bd-9b47-c1247a8f1a97@v4g2000vba.googlegroups.com> <37e0021f-b50c-4f0c-8ba5-0beed01da029@z16g2000prd.googlegroups.com> <5a8c7e8b-8b3d-4a47-9ac6-23bc85b512f4@d7g2000prl.googlegroups.com> Message-ID: On Tue, 05 May 2009 01:25:49 -0700, Carl Banks wrote: > *Sigh* Well, I'm out of this debate. Apparently it's not possible to > argue that recursivie algorithms should be avoided *sometimes* without > everyone citing cases that obviously aren't from those times (as if I > had been arguing that recursion should be avoided all the time). You overstated your position, and then instead of gracefully admitting that you overstated it, you're trying to weasel out of it by acting the victim. If all you had said was that "sometimes" recursion should be avoided, then who could argue against that? Scheme purists? Pah, this is Python, we don't need no stinkin' mathematical purity! Practicality beats purity is *precisely* why sometimes you need recursion instead of forcing a complicated iterative solution onto a simple recursive problem. > Here's a parting thought for you to cite "counterexamples" of: > > Iteration should be used instead of recursion anywhere a tail- recursive > algorithm is possible. Recursion should be used only when > tail-recursion is not possible. Or when the recursive algorithm is simpler than the iterative algorithm, and you don't care about squeezing out every last tiny micro-optimization into the code. -- Steven From sri_annauni at yahoo.co.in Tue May 5 05:11:28 2009 From: sri_annauni at yahoo.co.in (srinivasan srinivas) Date: Tue, 5 May 2009 14:41:28 +0530 (IST) Subject: Which one is best Python or Java for developing GUI applications? In-Reply-To: <49ffecbf$0$27576$426a74cc@news.free.fr> References: <7xfxfkvwra.fsf@ruckus.brouhaha.com> <49ffecbf$0$27576$426a74cc@news.free.fr> Message-ID: <629374.1103.qm@web7904.mail.in.yahoo.com> Could you list down those advantages?? ----- Original Message ---- From: Bruno Desthuilliers To: python-list at python.org Sent: Tuesday, 5 May, 2009 1:07:41 PM Subject: Re: Which one is best Python or Java for developing GUI applications? Paul Rubin a ?crit : > srinivasan srinivas writes: >> Could you tell me does Python have any advantages over Java for the development of GUI applications? > > Yes. Indeed. -- http://mail.python.org/mailman/listinfo/python-list Cricket on your mind? Visit the ultimate cricket website. Enter http://beta.cricket.yahoo.com From umut.tabak at gmail.com Tue May 5 05:21:27 2009 From: umut.tabak at gmail.com (utab) Date: Tue, 5 May 2009 02:21:27 -0700 (PDT) Subject: change some lines from a read file References: <0e624af8-d84c-4117-ac93-0cc4a4df4d02@s20g2000vbp.googlegroups.com> Message-ID: <9cf75556-a58c-4079-b457-da88b0370a1d@s16g2000vbp.googlegroups.com> On May 4, 10:06?pm, Anthra Norell wrote: > utab wrote: > > Dear all, > > > I have to change some lines from a template file, which is rather long > > to paste here, but I would like to make some parts of some lines > > optional with my command line arguments but I could not see this > > directly, I can count the line numbers and decide on this basis to > > decide the lines to be configured to my will. > > > More specifically, say, I have a that file includes > > > this is an example python file to play around > > . > > . > > . > > some lines > > . > > . > > . > > . -> an option line for example. > > . > > . > > . > > -> another option line so on. > > > and execute the script > > ./myScript option1 option2 > > > so that the options at correct locations will be written. > > > Any other options for this simple job that i can improve my Python a > > bit as well. > > > Best, > > Umut > > -- > >http://mail.python.org/mailman/listinfo/python-list > > Your description is not explicit enough to convey your intention. If > your template file is too long to post, post a short representative > section, an input data sample and a hand-edited sample of the output > data you want to generate. You will get more and better advice. . > > Frederic Thanks here is a sample Session file from a commercial finite element pre-post processor PATRAN, the file longer but the ideas are the same so that let me give an example, say I would like to make write line 10 composed of some command line arguments: path+database_name, Also there are some lines similar to this in the below sections of the file. I am doing it with a if-elif-else clause checking the line numbers with which I thought of there should be a better way and ended up asking... 1 $# Session file patran.ses.02 started recording at 05-May-09 11:12:25 2 $# Build: 15.0.038 Mon Aug 20 21:31:45 PDT 2007 3 $# Recorded by: Patran 2007 r1b 4 $# FLEXlm initialization complete. Acquiring license(s)... 5 $# License File: /opt/patran-2007r1b/license.dat 6 $# Patran 2007 r1b has obtained 1 concurrent license(s) from FLEXlm per a 7 $# request to execute on Linux node hpc06(001e4f20c494) (Linux 8 $# 2.6.23.15-80.fc7) at 05-May-09 11:12:25. 9 uil_file_new.go( "/opt/patran-2007r1b/template.db", @ 10 "/home/utabak/PATRAN/cavityModels/fromGmsh.db" ) 11 $# Question from application FILE 12 $# Database /home/utabak/PATRAN/cavityModels/fromGmsh.db already exists. 13 $# Do you wish to delete the existing database and create a new one ? 14 $? YES 36000002 15 $# Copying /opt/patran-2007r1b/template.db to 16 $# /home/utabak/PATRAN/cavityModels/fromGmsh.db 17 $# Template copy complete. 18 $# Database version 3.8 created by Patran 2007 r1b successfully opened. 19 ga_viewport_size_set( "default_viewport", 14.571214, 7.509466, 1 ) 20 ga_viewport_location_set( "default_viewport", 0.000000, 1.088771, 1 ) 21 $# Creating journal file /home/utabak/PATRAN/cavityModels/ fromGmsh.db.jou at 22 $# 05-May-09 11:12:43 23 nastran_input_import( "/home/utabak/PATRAN/cavityModels/ toPatran.bdf", @ 24 "default_group", 10, [TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, @ 25 TRUE], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [ @ 26 -2000000000, -2000000000, -2000000000, -2000000000, -2000000000, -2000000000, @ 27 -2000000000, -2000000000, 0, 0] ) 28 $# Reading of MSC.Nastran input file completed. 29 ui_exec_function( "mesh_seed_display_mgr", "init" ) Best, From clp2 at rebertia.com Tue May 5 05:28:15 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Tue, 5 May 2009 02:28:15 -0700 Subject: Which one is best Python or Java for developing GUI applications? In-Reply-To: <87d4ao9fi7.fsf@rudin.co.uk> References: <7xfxfkvwra.fsf@ruckus.brouhaha.com> <87d4ao9fi7.fsf@rudin.co.uk> Message-ID: <50697b2c0905050228l5fee0351re35f61f0fe9d35ab@mail.gmail.com> On Tue, May 5, 2009 at 12:26 AM, Paul Rudin wrote: > Paul Rubin writes: > >> srinivasan srinivas writes: >>> Could you tell me does Python have any advantages over Java for the >>> development of GUI applications? >> >> Yes. > > Clearly c.l.p needs to adopt the SNB > convention :) Surely you have forgotten the comedy style of the language's namesake, which makes that rule completely inadmissible! ;P Cheers, Chris -- http://blog.rebertia.com From ldo at geek-central.gen.new_zealand Tue May 5 05:46:38 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Tue, 05 May 2009 21:46:38 +1200 Subject: Any idea to emulate tail -f References: Message-ID: In message , Joel Juvenal Rivera Rivera wrote: > I want to make something very similar to the command tail -f (follow a > file) ... From QJing.Li at gmail.com Tue May 5 06:05:51 2009 From: QJing.Li at gmail.com (Leon) Date: Tue, 5 May 2009 03:05:51 -0700 (PDT) Subject: what's the best way to call a method of object without a guarantee of its existence Message-ID: <22690572-e338-483a-a99a-85ccf6c5fae6@x6g2000vbg.googlegroups.com> One way, define the object before it is used, like this: object = None ..... ..... if object is not None: object.method() The other way, using try ... catch try: object.method() catch NameError: pass for big programs, which is better, or any other way? Miles From grflanagan at gmail.com Tue May 5 06:22:43 2009 From: grflanagan at gmail.com (Gerard Flanagan) Date: Tue, 05 May 2009 11:22:43 +0100 Subject: find sublist inside list In-Reply-To: References: Message-ID: Matthias Gall? wrote: > Hi. > > My problem is to replace all occurrences of a sublist with a new element. > > Example: > Given ['a','c','a','c','c','g','a','c'] I want to replace all > occurrences of ['a','c'] by 6 (result [6,6,'c','g',6]). > For novelty value: from itertools import izip def replace2(data, pattern): assert len(pattern) == 2 pattern = tuple(pattern) icopy = iter(data) icopy.next() gen = izip(data, icopy) while True: item = gen.next() if item == pattern: yield '6' gen.next() else: yield item[0] # works if list ends with ['a', 'c'] data = ['g', 'a', 'c', 'a', 'c', 'a', 'a', 'a', 'g', 'a', 'c'] want = 'g66aaag6' assert ''.join(replace2(data, ['a', 'c'])) == want # otherwise you lose the last element of the tail data = ['g', 'a', 'c', 'a', 'c', 'a', 'a', 'a', 'g', 'a', 'c', 'c', 'g'] want = 'g66aaag6cg' get = 'g66aaag6c' assert not ''.join(replace2(data, ['a', 'c'])) == want assert ''.join(replace2(data, ['a', 'c'])) == get # fix by adding the pattern to the end of the data as a sentinel def replace2(data, pattern): assert len(pattern) == 2 def _replace2(data, pattern): pattern = tuple(pattern) icopy = iter(data) icopy.next() gen = izip(data, icopy) while True: item = gen.next() if item == pattern: yield '6' gen.next() else: yield item[0] data = data + pattern return list(_replace2(data, pattern))[:-1] data = ['g', 'a', 'c', 'a', 'c', 'a', 'a', 'a', 'g', 'a', 'c'] want = 'g66aaag6' assert ''.join(replace2(data, ['a', 'c'])) == want data = ['g', 'a', 'c', 'a', 'c', 'a', 'a', 'a', 'g', 'a', 'c', 'c', 'g'] want = 'g66aaag6cg' assert ''.join(replace2(data, ['a', 'c'])) == want print 'done' From iainking at gmail.com Tue May 5 06:24:29 2009 From: iainking at gmail.com (Iain King) Date: Tue, 5 May 2009 03:24:29 -0700 (PDT) Subject: Any idea to emulate tail -f References: Message-ID: <55f6d122-187f-4796-8162-78b066875c4d@q14g2000vbn.googlegroups.com> On May 5, 7:00?am, Joel Juvenal Rivera Rivera wrote: > I want to make something very similar to ?the command tail -f (follow a > file), i have been trying ?with some while True and some microsleeps > (about .1 s); did someone has already done something like this? > > And about the file is the apache acceslog ?of a site with a lot of > traffic. > > Regards ? ? > > joe / Joel Rivera This is very interesting, about using Generator Expressions: http://209.85.229.132/search?q=cache:ZHrV4E0eTI8J:www.dabeaz.com/generators/Generators.pdf Relevant stuff about 'tail -f' is on page 39, but I'd read the whole thing if you can. Iain From marco at sferacarta.com Tue May 5 06:25:08 2009 From: marco at sferacarta.com (Marco Mariani) Date: Tue, 05 May 2009 12:25:08 +0200 Subject: what's the best way to call a method of object without a guarantee of its existence In-Reply-To: <22690572-e338-483a-a99a-85ccf6c5fae6@x6g2000vbg.googlegroups.com> References: <22690572-e338-483a-a99a-85ccf6c5fae6@x6g2000vbg.googlegroups.com> Message-ID: <9uULl.4199$Ux.2825@tornado.fastwebnet.it> Leon wrote: > One way, define the object before it is used, > like this: > object = None This is a good practice anyway. Conditional existance of objects is quite evil. Resorting to if defined('foo') is double-plus-ugly. > The other way, using try ... catch > try: > object.method() > catch NameError: > pass Except you should trap AttributeError because you defined the thing as None before. NameErrors should be fixed as bugs, not trapped (IMHO -- but in python there is always a use case for everything). Keep in mind that AttributeError might come from inside the method(), which could be confusing By using the if stuff: stuff.run() idiom, you avoid the last issue and keep it simple enough. > for big programs, which is better, or any other way? Define "big", as in scope, LOCs, or number of committers? From jsanga at cox.net Tue May 5 06:26:45 2009 From: jsanga at cox.net (mzdude) Date: Tue, 5 May 2009 03:26:45 -0700 (PDT) Subject: find sublist inside list References: Message-ID: <87859e88-315f-47e3-98b9-338709d4c044@s28g2000vbp.googlegroups.com> On May 4, 4:54?pm, "Gabriel Genellina" wrote: > En Mon, 04 May 2009 15:12:41 -0300, mzdude escribi?: > > > substring isn't limited to 0..255 > >>>> substring = "\0x%d\0x%d" % (257,257) > >>>> 'acaccgac'.replace("ac", substring) > > '\x00x257\x00x257\x00x257\x00x257cg\x00x257\x00x257' > > This isn't what you think it is. Look carefully: > > py> substring = "\0x%d\0x%d" % (257,257) > py> len(substring) > 10 > py> list(substring) > ['\x00', 'x', '2', '5', '7', '\x00', 'x', '2', '5', '7'] > > -- > Gabriel Genellina OOPS. My bad. But I'm not going to give up. l = ['a','b','c','a','c'] us = unicode("".join(l)) substr = unichr(257) + unichr(257) us = us.replace(u'ac',substr) print len(us) print list(us) output is >>> 5 [u'a', u'b', u'c', u'\u0101', u'\u0101'] From aleksandar27 at BRISIOVOnet.hr Tue May 5 07:21:10 2009 From: aleksandar27 at BRISIOVOnet.hr (alejandro) Date: Tue, 5 May 2009 13:21:10 +0200 Subject: stuck with PyOBEX References: <75opebF19fc22U1@mid.uni-berlin.de> Message-ID: Ejla! I have sent you a mail (in case you check it often like me :-)) "David Boddie" wrote in message news:gtkhdr$obj$1 at get-news01.get.basefarm.net... > On Sunday 03 May 2009 10:33, alejandro wrote: > >> Yes! >> >>> I'll send you an updated version to try if you would like to test it. > > My mails to you keep getting returned, so I've put it here: > > http://www.boddie.org.uk/david/Projects/Python/PyOBEX/Software/PyOBEX-0.21.zip > > Please let me know if it works on Windows, and feel free to get in touch > if > you have any problems. > > David From ldo at geek-central.gen.new_zealand Tue May 5 07:28:16 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Tue, 05 May 2009 23:28:16 +1200 Subject: exit a program gracefully References: <50697b2c0905041514t22ac9436td0dbe51e0f22dcfa@mail.gmail.com> Message-ID: In message , Gabriel Genellina wrote: > I prefer to put the code inside a function, and just `return` earlier. It would be nice if Python offered a straightforward equivalent to ... initialization goes here ... do /*once*/ { ... do stuff ... if (check1_failed) break; ... do more stuff ... if (check2_failed) break; ... do even more stuff ... } while (false); ... cleanup goes here ... From cs at zip.com.au Tue May 5 07:42:00 2009 From: cs at zip.com.au (Cameron Simpson) Date: Tue, 5 May 2009 21:42:00 +1000 Subject: exit a program gracefully In-Reply-To: Message-ID: <20090505114200.GA11906@cskk.homeip.net> On 05May2009 23:28, Lawrence D'Oliveiro wrote: | In message , Gabriel | Genellina wrote: | | > I prefer to put the code inside a function, and just `return` earlier. | | It would be nice if Python offered a straightforward equivalent to | | ... initialization goes here ... | do /*once*/ | { | ... do stuff ... | if (check1_failed) | break; | ... do more stuff ... | if (check2_failed) | break; | ... do even more stuff ... | } | while (false); | ... cleanup goes here ... while True: ... do stuff ... if check1_failed: break; ... do more stuff ... if check2_failed: break; ... do even more stuff ... break ... cleanup goes here ... Seems more straightforward to me! And there's always try/except and context managers. Cheers, -- Cameron Simpson DoD#743 http://www.cskk.ezoshosting.com/cs/ So why can't we celebrate November 5th the way Guy Fawkes would've wanted? - Mike Holmes From tomanishkb at gmail.com Tue May 5 08:10:13 2009 From: tomanishkb at gmail.com (M Kumar) Date: Tue, 5 May 2009 17:40:13 +0530 Subject: help required with pylons Message-ID: <783b47270905050510y7f899a70s1b79bb3965d44f32@mail.gmail.com> Hi, I am having an application server in pylons, which was giving error sometimes and sometimes it gives the result. I will copy paste the error below. I am new to pylons and not getting any clue of this kind of behavior. Please help me what should I do to avoid this kind of errors. ---------------------------------------- Exception happened during processing of request from ('127.0.0.1', 33021) Traceback (most recent call last): File "/usr/lib/python2.5/site-packages/Paste-1.4.2-py2.5.egg/paste/httpserver.py", line 1046, in process_request_in_thread self.finish_request(request, client_address) File "/usr/lib/python2.5/SocketServer.py", line 254, in finish_request self.RequestHandlerClass(request, client_address, self) File "/usr/lib/python2.5/SocketServer.py", line 522, in __init__ self.handle() File "/usr/lib/python2.5/site-packages/Paste-1.4.2-py2.5.egg/paste/httpserver.py", line 425, in handle BaseHTTPRequestHandler.handle(self) File "/usr/lib/python2.5/BaseHTTPServer.py", line 316, in handle self.handle_one_request() File "/usr/lib/python2.5/site-packages/Paste-1.4.2-py2.5.egg/paste/httpserver.py", line 420, in handle_one_request self.wsgi_execute() File "/usr/lib/python2.5/site-packages/Paste-1.4.2-py2.5.egg/paste/httpserver.py", line 287, in wsgi_execute self.wsgi_start_response) File "/usr/lib/python2.5/site-packages/Paste-1.4.2-py2.5.egg/paste/cascade.py", line 92, in __call__ return self.apps[-1](environ, start_response) File "/usr/lib/python2.5/site-packages/Paste-1.4.2-py2.5.egg/paste/registry.py", line 340, in __call__ app_iter = self.application(environ, start_response) File "/usr/lib/python2.5/site-packages/AuthKit-0.4.0-py2.5.egg/authkit/authenticate/__init__.py", line 290, in __call__ return self.app(environ, start_response) File "/usr/lib/python2.5/site-packages/AuthKit-0.4.0-py2.5.egg/authkit/authenticate/cookie.py", line 354, in __call__ return self.app(environ, cookie_setting_start_response) File "/usr/lib/python2.5/site-packages/AuthKit-0.4.0-py2.5.egg/authkit/authenticate/multi.py", line 64, in __call__ raise Exception('WSGI start_response was not called before a result' Exception: WSGI start_response was not called before a result was returned ---------------------------------------- thanks in advance Maneesh KB -------------- next part -------------- An HTML attachment was scrubbed... URL: From damienlmoore at gmail.com Tue May 5 08:18:57 2009 From: damienlmoore at gmail.com (spillz) Date: Tue, 5 May 2009 05:18:57 -0700 (PDT) Subject: Problem with case insensitive volumes References: Message-ID: <26c57bf5-5ad2-4838-8097-c8d51cf1ccae@n8g2000vbb.googlegroups.com> > os;walk will tell you the correct case for each node. ? So if it gives > you a different case than the stored form, you have your answer. Thanks, although I was hoping that wouldn't be the answer (I have to compare a LOT of files). From ganesh.gbg at gmail.com Tue May 5 08:29:13 2009 From: ganesh.gbg at gmail.com (gganesh) Date: Tue, 5 May 2009 05:29:13 -0700 (PDT) Subject: problem in using sendmail in multi thread Message-ID: <343747e9-549f-4336-9b15-522411a785a3@x1g2000prh.googlegroups.com> hi, I'm a beginner in using Python script I'm trying to send mails using multi-thread I wrote FROM = 'ganeshxxxx at xxxx.com' # for more mail add';' the another mail id listTo = ['g.xxx at gmail.com', 'xxjango.py at gmail.com', 'xxxxx at yahoo.co.in'] SUBJECT = 'This is the subject' MSGBODY = 'This the body of the message ' MAILSERVER = 'mail.xxxx.com' port = 25 username = 'xxxxx' password = 'xxxxx' # trim the strings of any leading or trailing spaces FROM = FROM.strip() SUBJECT = SUBJECT.strip() MSGBODY = MSGBODY.strip() MAILSERVER = MAILSERVER.strip() username = username.strip() password = password.strip() #Connect to server print 'Connecting to mail server ', MAILSERVER try: s = smtplib.SMTP(MAILSERVER,port) print 'connected' #s.set_debuglevel(1) except: print 'ERROR: Unable to connect to mail server', sys.exc_info ()[0] sys.exit(1) #login to server if password <> '': print 'Logging into mail server' try: s.login(username,password) except: print 'ERROR: Unable to login to mail server', MAILSERVER print 'Please recheck your password' sys.exit(1) #-------------------------------------------------- print "Starting Multi Thread Method" class MyThread(Thread): def __init__(self, site, s, FROM, MSGBODY): Thread.__init__(self) self.site = site self.s=s self.FROM=FROM self.MSGBODY=MSGBODY def run(self): print "running for %s " %self.site s.sendmail(self.FROM, self.site, self.MSGBODY) print "Emailed for site %s" %self.site a= time.time() threads = [] for site in listTo: T = MyThread(site,s,FROM,MSGBODY) threads.append(T) T.start() for i in threads: i.join() s.quit() s.close() print "Took %s seconds" %str(time.time()-a) #----------------------------------------------------- Error: There is no problem with mail ids I'm using python2.5 ,Ubuntu I got an error like Traceback (most recent call last): File "/usr/lib/python2.5/threading.py", line 486, in __bootstrap_inner self.run() File "threadmailmul.py", line 85, in run s.sendmail(self.FROM, self.site, self.MSGBODY) File "/usr/lib/python2.5/smtplib.py", line 703, in sendmail raise SMTPRecipientsRefused(senderrs) SMTPRecipientsRefused: {'g.xxx at gmail.com': (503, '5.5.1 Error: nested MAIL command')} Exception in thread Thread-2: Traceback (most recent call last): File "/usr/lib/python2.5/threading.py", line 486, in __bootstrap_inner self.run() File "threadmailmul.py", line 85, in run s.sendmail(self.FROM, self.site, self.MSGBODY) File "/usr/lib/python2.5/smtplib.py", line 704, in sendmail (code,resp) = self.data(msg) File "/usr/lib/python2.5/smtplib.py", line 487, in data raise SMTPDataError(code,repl) SMTPDataError: (503, '5.5.1 Error: need MAIL command') Exception in thread Thread-3: Traceback (most recent call last): File "/usr/lib/python2.5/threading.py", line 486, in __bootstrap_inner self.run() File "threadmailmul.py", line 85, in run s.sendmail(self.FROM, self.site, self.MSGBODY) File "/usr/lib/python2.5/smtplib.py", line 692, in sendmail raise SMTPSenderRefused(code, resp, from_addr) SMTPSenderRefused: (503, '5.5.1 Error: need RCPT command', 'ganeshxxxx at xxx.com') Exception in thread Thread-4: Traceback (most recent call last): File "/usr/lib/python2.5/threading.py", line 486, in __bootstrap_inner self.run() File "threadmailmul.py", line 85, in run s.sendmail(self.FROM, self.site, self.MSGBODY) File "/usr/lib/python2.5/smtplib.py", line 702, in sendmail self.rset() File "/usr/lib/python2.5/smtplib.py", line 453, in rset return self.docmd("rset") File "/usr/lib/python2.5/smtplib.py", line 378, in docmd return self.getreply() File "/usr/lib/python2.5/smtplib.py", line 355, in getreply raise SMTPServerDisconnected("Connection unexpectedly closed") SMTPServerDisconnected: Connection unexpectedly closed Traceback (most recent call last): File "threadmailmul.py", line 102, in i.join() File "/usr/lib/python2.5/threading.py", line 594, in join self.__block.wait() File "/usr/lib/python2.5/threading.py", line 216, in wait waiter.acquire() If someone could point out the (probably silly) mistake I'm making , I would be very grateful. Thanks in advance From kyrie at uh.cu Tue May 5 08:33:01 2009 From: kyrie at uh.cu (Luis Zarrabeitia) Date: Tue, 5 May 2009 08:33:01 -0400 Subject: Self function In-Reply-To: <50697b2c0905042346u482eabf9j1bb9f95853f38864@mail.gmail.com> References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> <50697b2c0905042346u482eabf9j1bb9f95853f38864@mail.gmail.com> Message-ID: <200905050833.01211.kyrie@uh.cu> On Tuesday 05 May 2009 02:46:58 am Chris Rebert wrote: > > Adding syntax is EVIL(tm) for it angers the Gods of Backwards > Compatibility, and this proposal is completely unnecessary because you > could instead just write: [...] > And there would be much clashing with existing variable names, > for keywords are the Devil's work! > Heh. I liked the proposal (though I'm not 100% sold on the name __this__), and one of the reasons I liked it was... it preempted the name-clashing argument. Not a new keyword, just a variable that is injected on the local namespace, so it would only clash with code that uses __this__ as a global (or that expects to use an unbound __this__). Btw, is there any way to inject a name into a function's namespace? Following the python tradition, maybe we should try to create a more general solution! K. (P.S: there is one advantage on having it as a keyword, though: it would make static analisis easier) -- Luis Zarrabeitia (aka Kyrie) Fac. de Matem?tica y Computaci?n, UH. http://profesores.matcom.uh.cu/~kyrie From kyrie at uh.cu Tue May 5 08:44:24 2009 From: kyrie at uh.cu (Luis Zarrabeitia) Date: Tue, 5 May 2009 08:44:24 -0400 Subject: Self function In-Reply-To: References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> <37e0021f-b50c-4f0c-8ba5-0beed01da029@z16g2000prd.googlegroups.com> Message-ID: <200905050844.24207.kyrie@uh.cu> On Tuesday 05 May 2009 03:51:19 am Steven D'Aprano wrote: > def ivisit(node): > ? ? print node > ? ? while node and node.link is not None: > ? ? ? ? node = node.link > ? ? ? ? print node > > def rvisit(node): > ? ? print node > ? ? if node and node.link is not None: > ? ? ? ? rvisit(node.link) > > > If there are programmers who find rvisit "a lot less readable" than > ivisit, then in my arrogant opinion they should consider a change of > profession. /me smiles. What if I happen to find rvisit _more_ readable than ivisit? /me ducks. [I'm not a lisp user, but I tend to think recursively anyway...] -- Luis Zarrabeitia (aka Kyrie) Fac. de Matem?tica y Computaci?n, UH. http://profesores.matcom.uh.cu/~kyrie From kyrie at uh.cu Tue May 5 08:57:17 2009 From: kyrie at uh.cu (Luis Zarrabeitia) Date: Tue, 5 May 2009 08:57:17 -0400 Subject: Self function In-Reply-To: <5a8c7e8b-8b3d-4a47-9ac6-23bc85b512f4@d7g2000prl.googlegroups.com> References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> <5a8c7e8b-8b3d-4a47-9ac6-23bc85b512f4@d7g2000prl.googlegroups.com> Message-ID: <200905050857.17904.kyrie@uh.cu> On Tuesday 05 May 2009 04:25:49 am Carl Banks wrote: > Iteration should be used instead of recursion anywhere a tail- > recursive algorithm is possible. ?Recursion should be used only when > tail-recursion is not possible. Why? Is it because most languages suck at recursion (specially python, as this thread shows)? If that's the reason, I think you have it backwards... Turning a tail-recursion into an iteration should be the compiler's job, not mine. An algorithm, any algorithm, should be written in the way that is easier to read, unless the language wont allow that easier implementation to be efficient enough. Programming languages suck, but that shouldn't mean that we can't hope to improve them. -- Luis Zarrabeitia (aka Kyrie) Fac. de Matem?tica y Computaci?n, UH. http://profesores.matcom.uh.cu/~kyrie From henning.vonbargen at arcor.de Tue May 5 09:16:38 2009 From: henning.vonbargen at arcor.de (henning.vonbargen at arcor.de) Date: Tue, 5 May 2009 06:16:38 -0700 (PDT) Subject: How to build Python 2.6.2 on HP-UX Itanium with thread support? Message-ID: <3dcdc298-ff77-443d-a7df-b1635f3372b2@z7g2000vbh.googlegroups.com> Our program that makes use of cx_Oracle and multi-threading (and works fine on Windows, Linux and other platforms, including HP-UX PA-RISC), fails to run on HP-UX Itanium. When trying to start the first daemon thread, the program raises an exception: ... File "/usr/local/lib/python2.6/threading.py", line 471, in start _start_new_thread(self.__bootstrap, ()) error: can't start new thread I guess there's something wrong with the way we built Python, but I have absolutely no clue what (I'm not too familiar with HP-UX). >From what I found in the internet, it's probably something about the threading libraries at the C level. So the question is: Which steps are necessary to build Python 2.6.2 (32bit or 64bit) on HP- UX Itanium 11.31 with working multi-threading support? Note: For whatever reasons, in order to get cx_Oracle 5.0.1 to work on HP- UX, I have to build it as a built-in module. That's why I have to build Python myself (and unfortunately there's no Python 2.6.2 binary distribution for HP-UX itanium available). Everything works fine except multi-threading. Before running configure and make, I set up the environment like this: export PATH=$PATH:/usr/local/bin export CPPFLAGS=-I/opt/openssl/0.9.8/include export LDFLAGS="-L/usr/lib/hpux32 -L/usr/local/lib/hpux32 -L/opt/ openssl/0.9.8/lib" I have the files generated by configure and make available, but it's too much to post it all, as as I don't know what is relevant and what not. So, here's just the first few lines of the config.log output: This file contains any messages produced by compilers while running configure, to aid debugging if configure makes a mistake. It was created by python configure 2.6, which was generated by GNU Autoconf 2.61. Invocation command line was $ ./configure --with-zlib --with-openssl ## --------- ## ## Platform. ## ## --------- ## hostname = polref4 uname -m = ia64 uname -r = B.11.31 uname -s = HP-UX uname -v = U From anthra.norell at bluewin.ch Tue May 5 09:21:48 2009 From: anthra.norell at bluewin.ch (Anthra Norell) Date: Tue, 05 May 2009 15:21:48 +0200 Subject: change some lines from a read file In-Reply-To: <9cf75556-a58c-4079-b457-da88b0370a1d@s16g2000vbp.googlegroups.com> References: <0e624af8-d84c-4117-ac93-0cc4a4df4d02@s20g2000vbp.googlegroups.com> <9cf75556-a58c-4079-b457-da88b0370a1d@s16g2000vbp.googlegroups.com> Message-ID: <4A003D6C.1090104@bluewin.ch> utab wrote: > On May 4, 10:06 pm, Anthra Norell wrote: > >> utab wrote: >> >>> Dear all, >>> >>> I have to change some lines from a template file, which is rather long >>> to paste here, but I would like to make some parts of some lines >>> optional with my command line arguments but I could not see this >>> directly, I can count the line numbers and decide on this basis to >>> decide the lines to be configured to my will. >>> >>> More specifically, say, I have a that file includes >>> >>> this is an example python file to play around >>> . >>> . >>> . >>> some lines >>> . >>> . >>> . >>> . -> an option line for example. >>> . >>> . >>> . >>> -> another option line so on. >>> >>> and execute the script >>> ./myScript option1 option2 >>> >>> so that the options at correct locations will be written. >>> >>> Any other options for this simple job that i can improve my Python a >>> bit as well. >>> >>> Best, >>> Umut >>> -- >>> http://mail.python.org/mailman/listinfo/python-list >>> >> Your description is not explicit enough to convey your intention. If >> your template file is too long to post, post a short representative >> section, an input data sample and a hand-edited sample of the output >> data you want to generate. You will get more and better advice. . >> >> Frederic >> > > Thanks here is a sample Session file from a commercial finite element > pre-post processor PATRAN, the file longer but the ideas are the same > so that let me give an example, say I would like to make write line 10 > composed of some command line arguments: path+database_name, Also > there are some lines similar to this in the below sections of the > file. I am doing it with a if-elif-else clause checking the line > numbers with which I thought of there should be a better way and ended > up asking... > > 1 $# Session file patran.ses.02 started recording at 05-May-09 > 11:12:25 > 2 $# Build: 15.0.038 Mon Aug 20 21:31:45 PDT 2007 > 3 $# Recorded by: Patran 2007 r1b > 4 $# FLEXlm initialization complete. Acquiring license(s)... > 5 $# License File: /opt/patran-2007r1b/license.dat > 6 $# Patran 2007 r1b has obtained 1 concurrent license(s) from > FLEXlm per a > 7 $# request to execute on Linux node hpc06(001e4f20c494) (Linux > 8 $# 2.6.23.15-80.fc7) at 05-May-09 11:12:25. > 9 uil_file_new.go( "/opt/patran-2007r1b/template.db", @ > 10 "/home/utabak/PATRAN/cavityModels/fromGmsh.db" ) > Okay! Please clarify: Question 1: Is it always line 10 or is it always a path-file-name, which could be on another line? Question 2: Are there other lines you want to translate? If yes, how do you identify them? By line number or by content? Question 3: If you identify your target by content, what ist the distinctive feature which it doesn't share with non-target lines? (e.g. Path-file name format and nothing else) Question 4: Do you want to substitute the path name with another path name and the file name with another file name, both of which you supply on the command line? Question 5: If your input has a number of different names, do you want to translate them all to the same name, in which case you'd only have to supply a single pair of names on the command line? Question 6: If your input has a number of different names and your translation also consists of a number of path-file-name pairs, how do the targets and substitutes correlate? Sequentially? By pattern? Question 7: Can you estimate how many different path-file-names your complete session file contains? And how many different substitutes you may have in extreme cases? > 11 $# Question from application FILE > 12 $# Database /home/utabak/PATRAN/cavityModels/fromGmsh.db > already exists. > 13 $# Do you wish to delete the existing database and create a new > one ? > 14 $? YES 36000002 > 15 $# Copying /opt/patran-2007r1b/template.db to > Question 8: Here you have a file name. What do you do with it? > 16 $# /home/utabak/PATRAN/cavityModels/fromGmsh.db > Question 9: Here line 10 repeats. What do you do with it? > 17 $# Template copy complete. > 18 $# Database version 3.8 created by Patran 2007 r1b successfully > opened. > 19 ga_viewport_size_set( "default_viewport", 14.571214, 7.509466, > 1 ) > 20 ga_viewport_location_set( "default_viewport", 0.000000, > 1.088771, 1 ) > 21 $# Creating journal file /home/utabak/PATRAN/cavityModels/ > fromGmsh.db.jou at > Question 10: Here the file name of line 10 repeats. What do you do? > 22 $# 05-May-09 11:12:43 > 23 nastran_input_import( "/home/utabak/PATRAN/cavityModels/ > toPatran.bdf", @ > Question 11: Here the file name of line 10 repeats. What do you do? > 24 "default_group", 10, [TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, > TRUE, FALSE, @ > 25 TRUE], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, > 0, 0], [ @ > 26 -2000000000, -2000000000, -2000000000, -2000000000, > -2000000000, -2000000000, @ > 27 -2000000000, -2000000000, 0, 0] ) > 28 $# Reading of MSC.Nastran input file completed. > 29 ui_exec_function( "mesh_seed_display_mgr", "init" ) > > Best, > -- > http://mail.python.org/mailman/listinfo/python-list > > Frederic From arnodel at googlemail.com Tue May 5 09:30:01 2009 From: arnodel at googlemail.com (Arnaud Delobelle) Date: Tue, 5 May 2009 06:30:01 -0700 (PDT) Subject: Self function References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> <50697b2c0905042346u482eabf9j1bb9f95853f38864@mail.gmail.com> Message-ID: <565698f7-d95b-4b99-95a5-5de68d983400@q2g2000vbr.googlegroups.com> On 5 May, 13:33, Luis Zarrabeitia wrote: > On Tuesday 05 May 2009 02:46:58 am Chris Rebert wrote: > > > > > Adding syntax is EVIL(tm) for it angers the Gods of Backwards > > Compatibility, and this proposal is completely unnecessary because you > > could instead just write: > [...] > > And there would be much clashing with existing variable names, > > for keywords are the Devil's work! > > > > Heh. I liked the proposal (though I'm not 100% sold on the name __this__), and > one of the reasons I liked it was... it preempted the name-clashing argument. > Not a new keyword, just a variable that is injected on the local namespace, > so it would only clash with code that uses __this__ as a global (or that > expects to use an unbound __this__). One issue with automatically binding a local variable to the current function is with nested functions: def foo() def bar(): # How do I call foo() from here? One solution would be def foo() def bar(foo=__this__): foo() I don't know, it does not convince me ATM. -- Arnaud From nickgaens at gmail.com Tue May 5 09:45:25 2009 From: nickgaens at gmail.com (nickgaens at gmail.com) Date: Tue, 5 May 2009 06:45:25 -0700 (PDT) Subject: Open a dialog from MainWindow - pyQT4 - Beginner :-) References: Message-ID: <55a5a393-f204-4bcb-a010-424bd4ddf063@o30g2000vbc.googlegroups.com> On May 4, 7:31?pm, Florian Wollenschein wrote: > Dear folks, > > I'm just working on a pyQT4 version of my txt to html converter thc (see > listick.org for details). > > I created the MainWindow with QT Designer and then converted it to .py > with pyuic4. It works well so far. Then I created a new UI for my > abtDialog (about dialog for my application). I did the same as with the > MainWindow, so I have two UIs and two *_ui.py files now. How can I open > the dialog from my main application class? > > Here's part of the code I'm using: > > ... > self.connect(self.actionAbout, > ? ? ? ? ? ? ? ? ?QtCore.SIGNAL("triggered()"), self.OpenAbout) > ... > ? def OpenAbout(self): > ? ? ? ? ?pass > ... > > The OpenAbout function should open the dialog. I've already tried a > abtDialog.show() after creating it via abtDialog = ThcAboutDialog() > (ThcAboutDialog is the class for this dlg). > > Any ideas? > > Thanks, > Listick Lorchhttp://www.listick.org > > PS: Keep in mind that I'm quite a beginner in the field of python and qt... def OpenAbout(self): # don't forget to connect the "Ok"-button of this about-dialog to QDialog::accept() ;-) abtDialog.setAttribute(QtCore.Qt.WA_DeleteOnClose) abtDialog.exec_() R3 ps: you could take a look at a QMessageBox::information or so to make this more "simple" From Scott.Daniels at Acm.Org Tue May 5 09:47:03 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Tue, 05 May 2009 06:47:03 -0700 Subject: exit a program gracefully In-Reply-To: References: <50697b2c0905041514t22ac9436td0dbe51e0f22dcfa@mail.gmail.com> Message-ID: Lawrence D'Oliveiro wrote: > It would be nice if Python offered a straightforward equivalent to > ... > do /*once*/ > { > ... do stuff ... > if (check1_failed) > break; > ... do even more stuff ... > } > while (false); > ... cleanup goes here ... What's wrong with: for _ in (None,): ... do stuff ... if (check1_failed) break; ... do even more stuff ... ... cleanup ... --Scott David Daniels Scott.Daniels at Acm.Org From Scott.Daniels at Acm.Org Tue May 5 10:02:32 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Tue, 05 May 2009 07:02:32 -0700 Subject: Problem with case insensitive volumes In-Reply-To: <26c57bf5-5ad2-4838-8097-c8d51cf1ccae@n8g2000vbb.googlegroups.com> References: <26c57bf5-5ad2-4838-8097-c8d51cf1ccae@n8g2000vbb.googlegroups.com> Message-ID: spillz wrote: >> os;walk will tell you the correct case for each node. So if it gives >> you a different case than the stored form, you have your answer. > > Thanks, although I was hoping that wouldn't be the answer (I have to > compare a LOT of files). What is so tough about something like: base, dirs, files = next(os.walk(dirn)) # older: os.walk(dirn).next() current = dict((name.upper() for name in dirs + files) ... changed = some_name == current[some_name.upper()] ... --Scott David Daniels Scott.Daniels at Acm.Org From python-url at phaseit.net Tue May 5 10:15:21 2009 From: python-url at phaseit.net (Gabriel Genellina) Date: Tue, 5 May 2009 14:15:21 +0000 (UTC) Subject: Python-URL! - weekly Python news and links (May 5) Message-ID: QOTW: "... [S]omebody's gotta put up some resistance to cute shortcuts, or we'll find ourselves back with Perl." - Peter Pearson http://groups.google.com/group/comp.lang.python/msg/2ce1b43e4d40528f How much memory occupies an object? http://groups.google.com/group/comp.lang.python/t/e0581c6c80e813aa/ Get the item actually contained in a set: http://groups.google.com/group/comp.lang.python/t/a2ab7d3ffc28e1b1/ Manipulating individual bits in a long bit string http://groups.google.com/group/comp.lang.python/t/471162166df52100/ A "float" version of range() -like range(-10.5,10.5,0.1)- isn't trivial to implement: http://groups.google.com/group/comp.lang.python/t/70bdf77282b63944/ How can a function refer to itself? http://groups.google.com/group/comp.lang.python/t/d265da85d4b70eaf/ Why do all objects have an implicit boolean value? http://groups.google.com/group/comp.lang.python/t/1167f4f275a9a64c/ multiprocessing: How to notify other processes that there is no more work to do: http://groups.google.com/group/comp.lang.python/t/8c57b2796cae95ff/ bearophile kindly shares some of his itertools enhancements: http://groups.google.com/group/comp.lang.python/t/642e3d4dc3e04a46/ A package/module/class confusion -- also: how to organize a package containing hundreds of classes http://groups.google.com/group/comp.lang.python/t/c216908a72d4fb8c/ Python lists aren't linked lists, as Lisp programmers are used to: http://groups.google.com/group/comp.lang.python/t/a0ce55f3d1fbe25/ And the Lisp mentality is very different :) http://groups.google.com/group/comp.lang.python/t/68bf9c5bae807545/ Passing a method as an argument from within the same class: http://groups.google.com/group/comp.lang.python/t/dabfc9f5443b7892/ Handling large arrays that barely fit in the address space: http://groups.google.com/group/comp.lang.python/t/7f065dac609436ea/ ======================================================================== Everything Python-related you want is probably one or two clicks away in these pages: Python.org's Python Language Website is the traditional center of Pythonia http://www.python.org Notice especially the master FAQ http://www.python.org/doc/FAQ.html PythonWare complements the digest you're reading with the marvelous daily python url http://www.pythonware.com/daily Just beginning with Python? This page is a great place to start: http://wiki.python.org/moin/BeginnersGuide/Programmers The Python Papers aims to publish "the efforts of Python enthusiats": http://pythonpapers.org/ The Python Magazine is a technical monthly devoted to Python: http://pythonmagazine.com Readers have recommended the "Planet" sites: http://planetpython.org http://planet.python.org comp.lang.python.announce announces new Python software. Be sure to scan this newsgroup weekly. http://groups.google.com/group/comp.lang.python.announce/topics Python411 indexes "podcasts ... to help people learn Python ..." Updates appear more-than-weekly: http://www.awaretek.com/python/index.html The Python Package Index catalogues packages. http://www.python.org/pypi/ The somewhat older Vaults of Parnassus ambitiously collects references to all sorts of Python resources. http://www.vex.net/~x/parnassus/ Much of Python's real work takes place on Special-Interest Group mailing lists http://www.python.org/sigs/ Python Success Stories--from air-traffic control to on-line match-making--can inspire you or decision-makers to whom you're subject with a vision of what the language makes practical. http://www.pythonology.com/success The Python Software Foundation (PSF) has replaced the Python Consortium as an independent nexus of activity. It has official responsibility for Python's development and maintenance. http://www.python.org/psf/ Among the ways you can support PSF is with a donation. http://www.python.org/psf/donations/ The Summary of Python Tracker Issues is an automatically generated report summarizing new bugs, closed ones, and patch submissions. http://search.gmane.org/?author=status%40bugs.python.org&group=gmane.comp.python.devel&sort=date Although unmaintained since 2002, the Cetus collection of Python hyperlinks retains a few gems. http://www.cetus-links.org/oo_python.html Python FAQTS http://python.faqts.com/ The Cookbook is a collaborative effort to capture useful and interesting recipes. http://code.activestate.com/recipes/langs/python/ Many Python conferences around the world are in preparation. Watch this space for links to them. Among several Python-oriented RSS/RDF feeds available, see: http://www.python.org/channews.rdf For more, see: http://www.syndic8.com/feedlist.php?ShowMatch=python&ShowStatus=all The old Python "To-Do List" now lives principally in a SourceForge reincarnation. http://sourceforge.net/tracker/?atid=355470&group_id=5470&func=browse http://www.python.org/dev/peps/pep-0042/ del.icio.us presents an intriguing approach to reference commentary. It already aggregates quite a bit of Python intelligence. http://del.icio.us/tag/python Enjoy the *Python Magazine*. http://pymag.phparch.com/ *Py: the Journal of the Python Language* http://www.pyzine.com Dr.Dobb's Portal is another source of Python news and articles: http://www.ddj.com/TechSearch/searchResults.jhtml?queryText=python and Python articles regularly appear at IBM DeveloperWorks: http://www.ibm.com/developerworks/search/searchResults.jsp?searchSite=dW&searchScope=dW&encodedQuery=python&rankprofile=8 Previous - (U)se the (R)esource, (L)uke! - messages are listed here: http://search.gmane.org/?query=python+URL+weekly+news+links&group=gmane.comp.python.general&sort=date http://groups.google.com/groups/search?q=Python-URL!+group%3Acomp.lang.python&start=0&scoring=d& http://lwn.net/Search/DoSearch?words=python-url&ctype3=yes&cat_25=yes There is *not* an RSS for "Python-URL!"--at least not yet. Arguments for and against are occasionally entertained. Suggestions/corrections for next week's posting are always welcome. E-mail to should get through. To receive a new issue of this posting in e-mail each Monday morning (approximately), ask to subscribe. Mention "Python-URL!". Write to the same address to unsubscribe. -- The Python-URL! Team-- Phaseit, Inc. (http://phaseit.net) is pleased to participate in and sponsor the "Python-URL!" project. Watch this space for upcoming news about posting archives. From sion at viridian.paintbox Tue May 5 10:43:07 2009 From: sion at viridian.paintbox (Sion Arrowsmith) Date: Tue, 05 May 2009 14:43:07 GMT Subject: object query assigned variable name? References: Message-ID: <%fYLl.34150$Sp3.23473@newsfe25.ams2> John O'Hagan wrote: >I can see that it's tantalizing, though, because _somebody_ must know about >the assignment; after all, we just executed it! Except we haven't, if we're talking about reporting from the object's __init__: >>> class Brian: ... def __init__(self): ... print "I'm Brian!" ... >>> l = [] >>> l[1] = Brian() I'm Brian! Traceback (most recent call last): File "", line 1, in IndexError: list assignment index out of range (Yeah, I know that's a setitem call not an assignment. Point stands. It also demonstrates why the whole idea of "what name is a newly- created object assigned to" is broken.) -- \S under construction From ross.jett at gmail.com Tue May 5 10:49:24 2009 From: ross.jett at gmail.com (Ross) Date: Tue, 5 May 2009 07:49:24 -0700 (PDT) Subject: Code works fine except... References: <8d4ec1df-dddb-469a-99a1-695152db7c04@n4g2000vba.googlegroups.com> <7f3493ca-efcb-48cc-aa29-487bb11fcd09@p4g2000vba.googlegroups.com> <16d47b2f-14d4-4266-84d2-c26501beb0c1@r34g2000vbi.googlegroups.com> <1691f042-bc14-46b9-b7b3-5a44348e9965@b1g2000vbc.googlegroups.com> <1a497471-498a-43d8-90ba-d676e8acf68b@r36g2000vbr.googlegroups.com> <8b49b775-66a7-42d5-9b00-cf23d9b044cc@e23g2000vbe.googlegroups.com> <54d76f11-c255-487d-9c3a-aaa380d7827a@z7g2000vbh.googlegroups.com> <991ccae3-f3e2-4d73-bd32-bc6f560e6f5f@n8g2000vbb.googlegroups.com> Message-ID: On May 5, 12:32?am, John Yeung wrote: > On May 5, 1:12?am, John Yeung wrote: > > > [...] the problem may require bigger guns (either much better > > math or much more sophisticated programming). > > Yes, I'm responding to myself. > > Well, I went ahead with the approach I mentioned earlier, generating > all possible matches and then selecting among them as needed to fill > up the courts, trying to keep the number of matches played by each > player as fair as possible. ?(I should mention that this, or something > similar, was suggested earlier by someone else in a different thread, > in response to the same question by the same OP.) > > I did use "bigger guns" (mainly a class for player objects, with > custom __cmp__ method), but still didn't do anything with doubles. > > I haven't tested it much, but I'll post it if anyone's interested. > (That way people can pick on me instead of the OP. ;) > > John I'm interested to see what you did. From your description, it sounds like I've tried what you've done, but when I implemented my version, it took minutes to evaluate for bigger numbers. If that isn't the case with yours, I'd be interested in seeing your implementation. From sion at viridian.paintbox Tue May 5 11:09:48 2009 From: sion at viridian.paintbox (Sion Arrowsmith) Date: Tue, 05 May 2009 15:09:48 GMT Subject: for with decimal values? References: <930f396f-a15e-4dd5-9d11-f6a074d44ffc@z16g2000prd.googlegroups.com> Message-ID: <0FYLl.34151$Sp3.6719@newsfe25.ams2> Gabriel Genellina wrote: >En Sun, 03 May 2009 17:41:49 -0300, Zentrader >escribi?: >> There is no need for a function or a generator. A for() loop is a >> unique case of a while loop >> ## for i in range(-10.5, 10.5, 0.1): >> ctr = -10.5 >> while ctr < 10.5: >> print ctr >> ctr += 0.1 > >Isn't so easy. You have representation errors and rounding errors here, >and they accumulate. [ ... ] And remarkably quickly at that: >>> ctr = -10.5 >>> for i in range(-105, 106): ... i *= 0.1 ... if i != ctr: print repr(i), repr(ctr) ... ctr += 0.1 ... -10.0 -10.000000000000002 etc. -- \S under construction From ricaraoz at gmail.com Tue May 5 11:12:23 2009 From: ricaraoz at gmail.com (=?ISO-8859-1?Q?Ricardo_Ar=E1oz?=) Date: Tue, 05 May 2009 12:12:23 -0300 Subject: yet another list comprehension question In-Reply-To: References: <594765c1-e5ed-492d-b398-a8ad00c6d6ad@x6g2000vbg.googlegroups.com> Message-ID: <4A005757.7050405@gmail.com> namekuseijin wrote: > On May 4, 9:15 am, David Robinow wrote: > >> On Mon, May 4, 2009 at 2:33 AM, namekuseijin >> >> wrote: >> >>>>>> ls = [(1,2), (3,4), (5, None), (6,7), (8, None)] >>>>>> [(x,y) for (x,y) in ls if y] >>>>>> >>> [(1, 2), (3, 4), (6, 7)] >>> >> Nope. That filters out 0 as well as None. Not what the OP asked for. >> > > True. I'm still a C programmer at heart I guess. ah, the flexibility > of 0... ;) > -- > http://mail.python.org/mailman/listinfo/python-list > > This seems to work for any length tuples : >>> a = [(1,2), (3,4, 'goes'), (5,None), (6,7, 8, 'as', None), (8, None), (9, 0)] >>> [tup for tup in a if not [e for e in tup if e == None]] [(1, 2), (3, 4, 'goes'), (9, 0)] -------------- next part -------------- An HTML attachment was scrubbed... URL: From Florian.Wollenschein at FernUni-Hagen.de Tue May 5 11:43:09 2009 From: Florian.Wollenschein at FernUni-Hagen.de (Florian Wollenschein) Date: Tue, 05 May 2009 17:43:09 +0200 Subject: Open a dialog from MainWindow - pyQT4 - Beginner :-) In-Reply-To: <55a5a393-f204-4bcb-a010-424bd4ddf063@o30g2000vbc.googlegroups.com> References: <55a5a393-f204-4bcb-a010-424bd4ddf063@o30g2000vbc.googlegroups.com> Message-ID: nickgaens at gmail.com wrote: > On May 4, 7:31 pm, Florian Wollenschein Hagen.de> wrote: >> Dear folks, >> >> I'm just working on a pyQT4 version of my txt to html converter thc (see >> listick.org for details). >> >> I created the MainWindow with QT Designer and then converted it to .py >> with pyuic4. It works well so far. Then I created a new UI for my >> abtDialog (about dialog for my application). I did the same as with the >> MainWindow, so I have two UIs and two *_ui.py files now. How can I open >> the dialog from my main application class? >> >> Here's part of the code I'm using: >> >> ... >> self.connect(self.actionAbout, >> QtCore.SIGNAL("triggered()"), self.OpenAbout) >> ... >> def OpenAbout(self): >> pass >> ... >> >> The OpenAbout function should open the dialog. I've already tried a >> abtDialog.show() after creating it via abtDialog = ThcAboutDialog() >> (ThcAboutDialog is the class for this dlg). >> >> Any ideas? >> >> Thanks, >> Listick Lorchhttp://www.listick.org >> >> PS: Keep in mind that I'm quite a beginner in the field of python and qt... > > def OpenAbout(self): > # don't forget to connect the "Ok"-button of this about-dialog to > QDialog::accept() ;-) > abtDialog.setAttribute(QtCore.Qt.WA_DeleteOnClose) > abtDialog.exec_() > > R3 > > ps: you could take a look at a QMessageBox::information or so to make > this more "simple" Wow, thanks for your help. Now it's working :-) Great. Regards, Listick From georgeoliverGO at gmail.com Tue May 5 11:52:39 2009 From: georgeoliverGO at gmail.com (George Oliver) Date: Tue, 5 May 2009 08:52:39 -0700 (PDT) Subject: call function of class instance with no assigned name? Message-ID: <447f2963-07b1-40bb-b084-ddf6c9cc880b@c18g2000prh.googlegroups.com> hi, I'm a Python beginner with a basic question. I'm writing a game where I have keyboard input handling defined in one class, and command execution defined in another class. The keyboard handler class contains a dictionary that maps a key to a command string (like 'h': 'left') and the command handler class contains functions that do the commands (like def do_right(self):), I create instances of these classes in a list attached to a third, 'brain' class. What I'd like to have happen is when the player presses a key, the command string is passed to the command handler, which runs the function. However I can't figure out a good way to make this happen. I've tried a dictionary mapping command strings to functions in the command handler class, but those dictionary values are evaluated just once when the class is instantiated. I can create a dictionary in a separate function in the command handler (like a do_command function) but creating what could be a big dictionary for each input seems kind of silly (unless I'm misunderstanding something there). What would be a good way to make this happen, or is there a different kind of architecture I should be thinking of? From damienlmoore at gmail.com Tue May 5 11:55:13 2009 From: damienlmoore at gmail.com (spillz) Date: Tue, 5 May 2009 08:55:13 -0700 (PDT) Subject: Problem with case insensitive volumes References: <26c57bf5-5ad2-4838-8097-c8d51cf1ccae@n8g2000vbb.googlegroups.com> Message-ID: On May 5, 10:02?am, Scott David Daniels wrote: > What is so tough about something like: > > base, dirs, files = next(os.walk(dirn)) ?# older: os.walk(dirn).next() > current = dict((name.upper() for name in dirs + files) > ... > changed = some_name == current[some_name.upper()] > ... not so fast. :) Consider: /my/path/to/usbkey/and/file everything up to "/my/path/to/usbkey" is case sensitive. only the "and/ file" is case insensitive. but my list of stored paths might include all images under "/my". thus /my/path/to/usbkey/and/file1==/my/path/to/usbkey/and/FILE1 /my/path/to/usbkey/and/file1==/my/path/to/usbkey/AND/FILE1 but /my/path/to/usbkey/and/file1!=/my/path/TO/usbkey/AND/FILE1 so to do this right I'd need to establish which parts of the path are case insensitive. being able to retrieve the actual path from an equivalent representation would be so much simpler. From clp2 at rebertia.com Tue May 5 12:01:52 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Tue, 5 May 2009 09:01:52 -0700 Subject: call function of class instance with no assigned name? In-Reply-To: <447f2963-07b1-40bb-b084-ddf6c9cc880b@c18g2000prh.googlegroups.com> References: <447f2963-07b1-40bb-b084-ddf6c9cc880b@c18g2000prh.googlegroups.com> Message-ID: <50697b2c0905050901v616d4f6axcdb283eaa2890cc1@mail.gmail.com> On Tue, May 5, 2009 at 8:52 AM, George Oliver wrote: > hi, I'm a Python beginner with a basic question. I'm writing a game > where I have keyboard input handling defined in one class, and command > execution defined in another class. The keyboard handler class > contains a dictionary that maps a key to a command string (like 'h': > 'left') and the command handler class contains functions that do the > commands (like def do_right(self):), > > I create instances of these classes in a list attached to a third, > 'brain' class. What I'd like to have happen is when the player presses > a key, the command string is passed to the command handler, which runs > the function. However I can't figure out a good way to make this > happen. > > I've tried a dictionary mapping command strings to functions in the > command handler class, but those dictionary values are evaluated just > once when the class is instantiated. I can create a dictionary in a > separate function in the command handler (like a do_command function) > but creating what could be a big dictionary for each input seems kind > of silly (unless I'm misunderstanding something there). > > What would be a good way to make this happen, or is there a different > kind of architecture I should be thinking of? You could exploit Python's dynamism by using the getattr() function: key2cmd = {'h':'left'} cmd_name = key2cmd[keystroke] getattr("do_"+cmd_name, cmd_handler)() #same as cmd_handler.do_left() Cheers, Chris -- http://blog.rebertia.com From aahz at pythoncraft.com Tue May 5 12:06:59 2009 From: aahz at pythoncraft.com (Aahz) Date: 5 May 2009 09:06:59 -0700 Subject: problem in using sendmail in multi thread References: <343747e9-549f-4336-9b15-522411a785a3@x1g2000prh.googlegroups.com> Message-ID: In article <343747e9-549f-4336-9b15-522411a785a3 at x1g2000prh.googlegroups.com>, gganesh wrote: > >I'm a beginner in using Python script >I'm trying to send mails using multi-thread You need a separate SMTP connection for each thread. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "It is easier to optimize correct code than to correct optimized code." --Bill Harlan From nickle at gmail.com Tue May 5 12:07:29 2009 From: nickle at gmail.com (Nick) Date: Tue, 5 May 2009 09:07:29 -0700 (PDT) Subject: SQL and CSV Message-ID: I have a requirement to read a CSV file. Normally, no problem, just import CSV and slurp the file up. However, in this case I want to filter out lines that have fields set to particular values. It would be neat to be able to do something like this. select * from test.csv where status <> "Canceled" Using adodb I can do this, so long as I don't have the where clause. :- ( Is there a reasonable lightweight way of doing this in Python? I could write some python code that is used to filter rows, and inport that from config, but it's not quite as elegant as an SQL route. Thanks Nick From james at agentultra.com Tue May 5 12:15:15 2009 From: james at agentultra.com (J Kenneth King) Date: Tue, 05 May 2009 12:15:15 -0400 Subject: list comprehension question References: <6d502cfd-e252-4722-96a5-c52fc3579499@v1g2000prd.googlegroups.com> <85ocucnbce.fsf@agentultra.com> Message-ID: <85ljpb8r1o.fsf@agentultra.com> Emile van Sebille writes: > On 5/1/2009 7:31 AM J Kenneth King said... >> Chris Rebert writes: >>> b = [] >>> for pair in a: >>> for item in pair: >>> b.append(item) >> >> This is much more clear than a nested comprehension. >> >> I love comprehensions, but abusing them can lead to really dense and >> difficult to read code. > > I disagree on dense and difficult, although I'll leave open the > question of abuse. Dense and difficult may be subjective to people like you or I, but you left out the "to read" part of that sentence. I was referring to the negative effect nested or complex list comprehensions can have on readability. > b = [ item for pair in a for item in pair ] It's a clever statement, but use it once and its gone. Why not use the expanded form in a function definition and get an even shorter, but clearer statement? >>> b = flatten(a) Boom, done. > This is exactly the code above expressed in comprehension form. > > It's worth knowing that a list comprehension is structured identically > to the equivalent for loop. So it really is neither more dense nor > more difficult to read. Further, you can tell immediately from the > start of the list comprehension what you've got -- in this case a list > of item(s). > > Here with some slight changes... > >>>> a = [(1, 2), (3, 4, 7), (5, 6)] >>>> [ item for j in a if len(j)==2 for item in j if item % 2 ] > [1, 5] > > ...opposed to... > >>>> for j in a: > ... if len(j)==2: > ... for item in j: > ... if item % 2: > ... b.append(item) > ... >>>> b > [1, 5] >>>> Thanks for the lesson in list comprehensions, but I'm well aware of how they work. List comprehensions can make a reader of your code apprehensive because it can read like a run-on sentence and thus be difficult to parse. The Python documentation discourages their use and I believe for good reason. It's much easier to explain complex processes with a function rather than a single nested statement. > > YMMV, > > Emile It will apparently vary greatly. Depending on how much coffee I've had. ;) J From arnodel at googlemail.com Tue May 5 12:17:10 2009 From: arnodel at googlemail.com (Arnaud Delobelle) Date: Tue, 05 May 2009 17:17:10 +0100 Subject: call function of class instance with no assigned name? References: <447f2963-07b1-40bb-b084-ddf6c9cc880b@c18g2000prh.googlegroups.com> Message-ID: Chris Rebert writes: > On Tue, May 5, 2009 at 8:52 AM, George Oliver wrote: >> hi, I'm a Python beginner with a basic question. I'm writing a game >> where I have keyboard input handling defined in one class, and >> command execution defined in another class. The keyboard handler >> class contains a dictionary that maps a key to a command string (like >> 'h': 'left') and the command handler class contains functions that do >> the commands (like def do_right(self):), >> >> I create instances of these classes in a list attached to a third, >> 'brain' class. What I'd like to have happen is when the player >> presses a key, the command string is passed to the command handler, >> which runs the function. However I can't figure out a good way to >> make this happen. >> >> I've tried a dictionary mapping command strings to functions in the >> command handler class, but those dictionary values are evaluated just >> once when the class is instantiated. I can create a dictionary in a >> separate function in the command handler (like a do_command function) >> but creating what could be a big dictionary for each input seems kind >> of silly (unless I'm misunderstanding something there). >> >> What would be a good way to make this happen, or is there a different >> kind of architecture I should be thinking of? > > You could exploit Python's dynamism by using the getattr() function: > > key2cmd = {'h':'left'} > cmd_name = key2cmd[keystroke] > getattr("do_"+cmd_name, cmd_handler)() #same as cmd_handler.do_left() getattr(cmd_handler, "do_" + cmd_name)() will work even better! -- Arnaud From james at agentultra.com Tue May 5 12:18:00 2009 From: james at agentultra.com (J Kenneth King) Date: Tue, 05 May 2009 12:18:00 -0400 Subject: Any idea to emulate tail -f References: <55f6d122-187f-4796-8162-78b066875c4d@q14g2000vbn.googlegroups.com> Message-ID: <85hbzz8qx3.fsf@agentultra.com> Iain King writes: > On May 5, 7:00?am, Joel Juvenal Rivera Rivera > wrote: >> I want to make something very similar to ?the command tail -f (follow a >> file), i have been trying ?with some while True and some microsleeps >> (about .1 s); did someone has already done something like this? >> >> And about the file is the apache acceslog ?of a site with a lot of >> traffic. >> >> Regards ? ? >> >> joe / Joel Rivera > > This is very interesting, about using Generator Expressions: > http://209.85.229.132/search?q=cache:ZHrV4E0eTI8J:www.dabeaz.com/generators/Generators.pdf > > Relevant stuff about 'tail -f' is on page 39, but I'd read the whole > thing if you can. > > Iain +1 I second this suggestion. I've used this method in many scripts of late and it is quite handy. He even takes it a little further and shows you some neat things you can do with it later on. From mail at timgolden.me.uk Tue May 5 12:19:32 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Tue, 05 May 2009 17:19:32 +0100 Subject: SQL and CSV In-Reply-To: References: Message-ID: <4A006714.3090601@timgolden.me.uk> Nick wrote: > I have a requirement to read a CSV file. Normally, no problem, just > import CSV and slurp the file up. > > However, in this case I want to filter out lines that have fields set > to particular values. > > It would be neat to be able to do something like this. > > select * from test.csv where status <> "Canceled" > > Using adodb I can do this, so long as I don't have the where clause. :- > ( > > Is there a reasonable lightweight way of doing this in Python? > > I could write some python code that is used to filter rows, and inport > that from config, but it's not quite as elegant as an SQL route. Not entirely clear what you are and aren't prepared to try here, but... the most obvious Python-based way to do this is treating the csv reader as an iterator and filtering there. Your last line suggests that's not what you want but just in case I've misunderstood: id,code,status 1,"ONE","Active" 2,"TWO","Cancelled" 3,"THREE","Active" import csv for row in csv.DictReader (open ("c:/temp/test.csv", "rb")): if row['status'] != 'Cancelled': print row Doesn't seem too onerous, and could obviously be wrapped in some useful class/module. But if you really want to go the SQL route, I believe there are ODBC adapters for CSV which, combined with PyODBC or CeODBC, would probably take you where you want to go. TJG From piet at cs.uu.nl Tue May 5 12:25:06 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Tue, 05 May 2009 18:25:06 +0200 Subject: problem in using sendmail in multi thread References: <343747e9-549f-4336-9b15-522411a785a3@x1g2000prh.googlegroups.com> Message-ID: >>>>> gganesh (g) wrote: >g> hi, >g> I'm a beginner in using Python script >g> I'm trying to send mails using multi-thread >g> I wrote >g> FROM = 'ganeshxxxx at xxxx.com' >g> # for more mail add';' the another mail id >g> listTo = ['g.xxx at gmail.com', 'xxjango.py at gmail.com', >g> 'xxxxx at yahoo.co.in'] >g> SUBJECT = 'This is the subject' >g> MSGBODY = 'This the body of the message ' >g> MAILSERVER = 'mail.xxxx.com' >g> port = 25 >g> username = 'xxxxx' >g> password = 'xxxxx' >g> # trim the strings of any leading or trailing spaces >g> FROM = FROM.strip() >g> SUBJECT = SUBJECT.strip() >g> MSGBODY = MSGBODY.strip() >g> MAILSERVER = MAILSERVER.strip() >g> username = username.strip() >g> password = password.strip() >g> #Connect to server >g> print 'Connecting to mail server ', MAILSERVER >g> try: >g> s = smtplib.SMTP(MAILSERVER,port) You can't have a single SMTP connection that's used in multiple threads. That is what causes the error. Each thread should have its own SMTP connection. So move this code (above and below) into the run() method. >g> print 'connected' >g> #s.set_debuglevel(1) >g> except: >g> print 'ERROR: Unable to connect to mail server', sys.exc_info ()[0] >g> sys.exit(1) >g> #login to server >g> if password <> '': >g> print 'Logging into mail server' I think this try except block should be inside the if statement, i.e. indented 4 spaces. >g> try: >g> s.login(username,password) >g> except: >g> print 'ERROR: Unable to login to mail server', MAILSERVER >g> print 'Please recheck your password' >g> sys.exit(1) >g> #-------------------------------------------------- >g> print "Starting Multi Thread Method" >g> class MyThread(Thread): >g> def __init__(self, site, s, FROM, MSGBODY): >g> Thread.__init__(self) >g> self.site = site >g> self.s=s >g> self.FROM=FROM >g> self.MSGBODY=MSGBODY You give the s (connection) here as a parameter, store it in self.s and never use that attribute. >g> def run(self): >g> print "running for %s " %self.site >g> s.sendmail(self.FROM, self.site, self.MSGBODY) Here you use the global s, not self.s As I said above you should do the SMTP connection setup, including the login, here, and store the connection either in self.s or in a local variable s in the method. As you don't use the s in another method, I think a local variable is better. >g> print "Emailed for site %s" %self.site >g> a= time.time() >g> threads = [] >g> for site in listTo: >g> T = MyThread(site,s,FROM,MSGBODY) >g> threads.append(T) >g> T.start() >g> for i in threads: >g> i.join() Of course the next 2 lines should also be moved to run(). >g> s.quit() >g> s.close() >g> print "Took %s seconds" %str(time.time()-a) >g> #----------------------------------------------------- -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From nickle at gmail.com Tue May 5 12:25:42 2009 From: nickle at gmail.com (Nick) Date: Tue, 5 May 2009 09:25:42 -0700 (PDT) Subject: SQL and CSV References: Message-ID: <1d2bb50f-2e3f-429b-aa98-7e1fe1f011b0@t10g2000vbg.googlegroups.com> On May 5, 5:19?pm, Tim Golden wrote: > Nick wrote: > > I have a requirement to read a CSV file. Normally, no problem, just > > import CSV and slurp the file up. > > > However, in this case I want to filter out lines that have fields set > > to particular values. > > > It would be neat to be able to do something like this. > > > select * from test.csv where status <> "Canceled" > > > Using adodb I can do this, so long as I don't have the where clause. :- > > ( > > > Is there a reasonable lightweight way of doing this in Python? > > > I could write some python code that is used to filter rows, and inport > > that from config, but it's not quite as elegant as an SQL route. > > Not entirely clear what you are and aren't prepared to try here, but... > the most obvious Python-based way to do this is treating the csv reader > as an iterator and filtering there. Your last line suggests that's not > what you want but just in case I've misunderstood: > > > id,code,status > 1,"ONE","Active" > 2,"TWO","Cancelled" > 3,"THREE","Active" > > > > import csv > > for row in csv.DictReader (open ("c:/temp/test.csv", "rb")): > ? if row['status'] != 'Cancelled': > ? ? print row > > > > Doesn't seem too onerous, and could obviously be wrapped in > some useful class/module. > > But if you really want to go the SQL route, I believe there are > ODBC adapters for CSV which, combined with PyODBC or CeODBC, > would probably take you where you want to go. > > TJG Part of the problem is that the 'selection' needs to be in a config file. I can put the if row['status'] != 'Cancelled': return True into a config, read it and eval it, but its not quite as clean as an sql route. Nick From Florian.Wollenschein at FernUni-Hagen.de Tue May 5 12:34:17 2009 From: Florian.Wollenschein at FernUni-Hagen.de (Florian Wollenschein) Date: Tue, 05 May 2009 18:34:17 +0200 Subject: thc v0.3 - txt to html converter - better code? Message-ID: Hi all, here's some code of thc, my txt to html converter programmed with Python and pyQT4: ------------------------------------------------------------------------------- if self.rdioBtnTransitional.isChecked(): if self.cmboBoxLang.currentText() == "English": file_open.write('' + '\n') elif self.cmboBoxLang.currentText() == "German": file_open.write('' + '\n') else: file_open.write('' + '\n') if self.cmboBoxLang.currentText() == "English": file_open.write('' + '\n') elif self.cmboBoxLang.currentText() == "German": file_open.write('' + '\n') -------------------------------------------------------------------------------- Do you have any ideas for a better code than that? Could this be done smarter, shorter, whatever!? Thanks in advance, Listick http://www.listick.org From silverburgh.meryl at gmail.com Tue May 5 12:41:42 2009 From: silverburgh.meryl at gmail.com (silverburgh) Date: Tue, 5 May 2009 09:41:42 -0700 (PDT) Subject: Error in running python -v on Mac ox 10.5. Message-ID: Hi, I run 'python -v' on Macos 10.5 but I get this error : # can't create /System/Library/Frameworks/Python.framework/Versions/ 2.5/lib/python2.5/encodings/utf_8.pyc Can you please tell me how to fix it? And why I am seeing a lot of 'install' message' when i run 'python - v'? I don't think I see that many install message on linux. Here is the full message. Thanks for any help. $ python -v # installing zipimport hook import zipimport # builtin # installed zipimport hook # /System/Library/Frameworks/Python.framework/Versions/2.5/lib/ python2.5/site.pyc matches /System/Library/Frameworks/Python.framework/ Versions/2.5/lib/python2.5/site.py import site # precompiled from /System/Library/Frameworks/ Python.framework/Versions/2.5/lib/python2.5/site.pyc # /System/Library/Frameworks/Python.framework/Versions/2.5/lib/ python2.5/os.pyc matches /System/Library/Frameworks/Python.framework/ Versions/2.5/lib/python2.5/os.py import os # precompiled from /System/Library/Frameworks/ Python.framework/Versions/2.5/lib/python2.5/os.pyc import posix # builtin # /System/Library/Frameworks/Python.framework/Versions/2.5/lib/ python2.5/posixpath.pyc matches /System/Library/Frameworks/ Python.framework/Versions/2.5/lib/python2.5/posixpath.py import posixpath # precompiled from /System/Library/Frameworks/ Python.framework/Versions/2.5/lib/python2.5/posixpath.pyc # /System/Library/Frameworks/Python.framework/Versions/2.5/lib/ python2.5/stat.pyc matches /System/Library/Frameworks/Python.framework/ Versions/2.5/lib/python2.5/stat.py import stat # precompiled from /System/Library/Frameworks/ Python.framework/Versions/2.5/lib/python2.5/stat.pyc # /System/Library/Frameworks/Python.framework/Versions/2.5/lib/ python2.5/UserDict.pyc matches /System/Library/Frameworks/ Python.framework/Versions/2.5/lib/python2.5/UserDict.py import UserDict # precompiled from /System/Library/Frameworks/ Python.framework/Versions/2.5/lib/python2.5/UserDict.pyc # /System/Library/Frameworks/Python.framework/Versions/2.5/lib/ python2.5/copy_reg.pyc matches /System/Library/Frameworks/ Python.framework/Versions/2.5/lib/python2.5/copy_reg.py import copy_reg # precompiled from /System/Library/Frameworks/ Python.framework/Versions/2.5/lib/python2.5/copy_reg.pyc # /System/Library/Frameworks/Python.framework/Versions/2.5/lib/ python2.5/types.pyc matches /System/Library/Frameworks/ Python.framework/Versions/2.5/lib/python2.5/types.py import types # precompiled from /System/Library/Frameworks/ Python.framework/Versions/2.5/lib/python2.5/types.pyc import _types # builtin # /System/Library/Frameworks/Python.framework/Versions/2.5/lib/ python2.5/warnings.pyc matches /System/Library/Frameworks/ Python.framework/Versions/2.5/lib/python2.5/warnings.py import warnings # precompiled from /System/Library/Frameworks/ Python.framework/Versions/2.5/lib/python2.5/warnings.pyc # /System/Library/Frameworks/Python.framework/Versions/2.5/lib/ python2.5/linecache.pyc matches /System/Library/Frameworks/ Python.framework/Versions/2.5/lib/python2.5/linecache.py import linecache # precompiled from /System/Library/Frameworks/ Python.framework/Versions/2.5/lib/python2.5/linecache.pyc import encodings # directory /System/Library/Frameworks/ Python.framework/Versions/2.5/lib/python2.5/encodings # /System/Library/Frameworks/Python.framework/Versions/2.5/lib/ python2.5/encodings/__init__.pyc matches /System/Library/Frameworks/ Python.framework/Versions/2.5/lib/python2.5/encodings/__init__.py import encodings # precompiled from /System/Library/Frameworks/ Python.framework/Versions/2.5/lib/python2.5/encodings/__init__.pyc # /System/Library/Frameworks/Python.framework/Versions/2.5/lib/ python2.5/codecs.pyc matches /System/Library/Frameworks/ Python.framework/Versions/2.5/lib/python2.5/codecs.py import codecs # precompiled from /System/Library/Frameworks/ Python.framework/Versions/2.5/lib/python2.5/codecs.pyc import _codecs # builtin # /System/Library/Frameworks/Python.framework/Versions/2.5/lib/ python2.5/encodings/aliases.pyc matches /System/Library/Frameworks/ Python.framework/Versions/2.5/lib/python2.5/encodings/aliases.py import encodings.aliases # precompiled from /System/Library/Frameworks/ Python.framework/Versions/2.5/lib/python2.5/encodings/aliases.pyc # /System/Library/Frameworks/Python.framework/Versions/2.5/lib/ python2.5/encodings/utf_8.pyc has bad mtime import encodings.utf_8 # from /System/Library/Frameworks/ Python.framework/Versions/2.5/lib/python2.5/encodings/utf_8.py # can't create /System/Library/Frameworks/Python.framework/Versions/ 2.5/lib/python2.5/encodings/utf_8.pyc Python 2.5.1 (r251:54863, Jan 13 2009, 10:26:13) [GCC 4.0.1 (Apple Inc. build 5465)] on darwin Type "help", "copyright", "credits" or "license" for more information. dlopen("/System/Library/Frameworks/Python.framework/Versions/2.5/lib/ python2.5/lib-dynload/readline.so", 2); import readline # dynamically loaded from /System/Library/Frameworks/ Python.framework/Versions/2.5/lib/python2.5/lib-dynload/readline.so >>> quit From paul.sijben at xs4all.nl Tue May 5 12:43:57 2009 From: paul.sijben at xs4all.nl (Paul Sijben) Date: Tue, 05 May 2009 18:43:57 +0200 Subject: desperately looking for a howto on running my wxPython app on Vista In-Reply-To: References: <49f6d607$0$199$e4fe514c@news.xs4all.nl> Message-ID: <4A006CCD.5060901@xs4all.nl> Mike Driscoll wrote: > On Apr 29, 4:17 am, Paul Sijben wrote: >> Is there any way to check which is the offending pyd/dll? (normally >> Vista does not give out much data on what went wrong) >> >> Paul > > You might be able to find it using the Dependency Walker utility: > > http://www.dependencywalker.com/ Mike, thanks! That spotted indeed some issues with one specific file: SHLWAPI.DLL, now let's see what is the issue. The report from Dependency Walker:"Warning: At least one module has an unresolved import due to a missing export function in a delay-load dependent module." is not telling me much at this moment. The FAQ from Dependency Walker sais this is not a major issue, but apparently on Vista it is ?!? Does anyone have a clue what I am to do about this? Interestingly On WinXP it flags DWMAPI.DLL as missing. I did not spot that on the Vista box. Paul > > - Mike From emile at fenx.com Tue May 5 13:11:08 2009 From: emile at fenx.com (Emile van Sebille) Date: Tue, 05 May 2009 10:11:08 -0700 Subject: list comprehension question In-Reply-To: <85ljpb8r1o.fsf@agentultra.com> References: <6d502cfd-e252-4722-96a5-c52fc3579499@v1g2000prd.googlegroups.com> <85ocucnbce.fsf@agentultra.com> <85ljpb8r1o.fsf@agentultra.com> Message-ID: On 5/5/2009 9:15 AM J Kenneth King said... > List comprehensions can make a reader of your code apprehensive > because it can read like a run-on sentence and thus be difficult to > parse. The Python documentation discourages their use and I believe > for good reason. Can you provide a link for this? I'd like to see specifically what's being discouraged, as I'd be surprised to find routine usage frowned upon. Emile From jcd at sdf.lonestar.org Tue May 5 13:11:43 2009 From: jcd at sdf.lonestar.org (J. Cliff Dyer) Date: Tue, 05 May 2009 13:11:43 -0400 Subject: for with decimal values? In-Reply-To: References: <930f396f-a15e-4dd5-9d11-f6a074d44ffc@z16g2000prd.googlegroups.com> Message-ID: <1241543503.6816.9.camel@aalcdl07> On Sun, 2009-05-03 at 19:48 -0700, alex23 wrote: > On May 4, 11:41 am, Esmail wrote: > > All this discussion makes me wonder if it would be a good idea > > for Python to have this feature (batteries included and all) - it > > would have its uses, no? > > Well, sometimes more discussion == less consensus :) > > But it's really easy to roll your own: > > from decimal import Decimal > > def args2dec(fn): > '''*args to Decimal decorator''' > float2dec = lambda f: Decimal(str(f)) > def _args2dec(*args): > args = map(float2dec, args) > return fn(*args) > return _args2dec > > @args2dec > def drange(start, stop, step): > while start < stop: > yield start > start += step I'd prefer not to force my users into using the Decimal type. Maybe float fits their needs better. So here's a generalized xrange replacement, which fixes an error in the drange implementation above (try it with a negative step). It's also highly *not* optimized. Calculating i * step every time slows it down. Speeding it back up is an exercise for the reader. def general_xrange(start, stop, step=1): target = stop * step if start * step > target: raise ValueError i = start while i * step < target: yield i i += step Cheers, Cliff From georgeoliverGO at gmail.com Tue May 5 13:28:43 2009 From: georgeoliverGO at gmail.com (George Oliver) Date: Tue, 5 May 2009 10:28:43 -0700 (PDT) Subject: call function of class instance with no assigned name? References: <447f2963-07b1-40bb-b084-ddf6c9cc880b@c18g2000prh.googlegroups.com> Message-ID: <2af0b836-b3a0-46d5-bf6c-9629614bbccd@d2g2000pra.googlegroups.com> On May 5, 9:01 am, Chris Rebert wrote: > On Tue, May 5, 2009 at 8:52 AM, George Oliver wrote: > > I create instances of these classes in a list attached to a third, > > 'brain' class. > > You could exploit Python's dynamism by using the getattr() function: Thanks for the responses -- I should have mentioned that I looked at the getattr() function, but as I instantiate the key handler and command handler classes in a list (like handlers = [command_handler(), keyboard_handler()], I'm unsure how to reference that class instance in the getattr in a straightforward way (short of keeping track of each instance's position in the handlers list). Is there a typical way of doing that? From google at mrabarnett.plus.com Tue May 5 13:33:58 2009 From: google at mrabarnett.plus.com (MRAB) Date: Tue, 05 May 2009 18:33:58 +0100 Subject: Code works fine except... In-Reply-To: References: <8d4ec1df-dddb-469a-99a1-695152db7c04@n4g2000vba.googlegroups.com> <7f3493ca-efcb-48cc-aa29-487bb11fcd09@p4g2000vba.googlegroups.com> <16d47b2f-14d4-4266-84d2-c26501beb0c1@r34g2000vbi.googlegroups.com> <1691f042-bc14-46b9-b7b3-5a44348e9965@b1g2000vbc.googlegroups.com> <1a497471-498a-43d8-90ba-d676e8acf68b@r36g2000vbr.googlegroups.com> <8b49b775-66a7-42d5-9b00-cf23d9b044cc@e23g2000vbe.googlegroups.com> <54d76f11-c255-487d-9c3a-aaa380d7827a@z7g2000vbh.googlegroups.com> <991ccae3-f3e2-4d73-bd32-bc6f560e6f5f@n8g2000vbb.googlegroups.com> Message-ID: <4A007886.5010703@mrabarnett.plus.com> Ross wrote: > On May 5, 12:32 am, John Yeung wrote: >> On May 5, 1:12 am, John Yeung wrote: >> >>> [...] the problem may require bigger guns (either much better >>> math or much more sophisticated programming). >> Yes, I'm responding to myself. >> >> Well, I went ahead with the approach I mentioned earlier, generating >> all possible matches and then selecting among them as needed to fill >> up the courts, trying to keep the number of matches played by each >> player as fair as possible. (I should mention that this, or something >> similar, was suggested earlier by someone else in a different thread, >> in response to the same question by the same OP.) >> >> I did use "bigger guns" (mainly a class for player objects, with >> custom __cmp__ method), but still didn't do anything with doubles. >> >> I haven't tested it much, but I'll post it if anyone's interested. >> (That way people can pick on me instead of the OP. ;) >> >> John > > I'm interested to see what you did. From your description, it sounds > like I've tried what you've done, but when I implemented my version, > it took minutes to evaluate for bigger numbers. If that isn't the case > with yours, I'd be interested in seeing your implementation. > Here's my approach (incomplete): def get_pair(player_list, played): for first in range(len(player_list)): player_1 = player_list[first] for second in range(first + 1, len(player_list)): player_2 = player_list[second] pair = player_1, player_2 sorted_pair = tuple(sorted(pair)) if sorted_pair not in played: played.add(sorted_pair) del player_list[second] del player_list[first] return pair return None def round_robin(player_list, courts, played): playing = [] for c in range(courts): pair = get_pair(player_list, played) if pair is None: break playing.append(pair) byes = player_list[:] player_list[:] = byes + [player for pair in playing for player in pair] yield playing, byes def test_round_robin(players, rounds, courts, doubles=False): player_list = range(players) played = set() for r in range(rounds): for playing, byes in round_robin(player_list, courts, played): print playing, byes From minesh at gmail.com Tue May 5 13:39:59 2009 From: minesh at gmail.com (Minesh Patel) Date: Tue, 5 May 2009 10:39:59 -0700 Subject: Is there is any way to send messages to chunk of emails ID's concurrently using smptlib In-Reply-To: References: <17ea2ba6-3f5d-4e7e-b05b-6d91dfbc5b36@k19g2000prh.googlegroups.com> Message-ID: On Mon, May 4, 2009 at 2:01 PM, Piet van Oostrum wrote: >>>>>> gganesh (g) wrote: > >>g> Hi friends, >>g> I suppose sendmail() can send mails one by one ,how to send mails >>g> concurrently , >>g> It would be very grateful,if someone could point out a solution. > You can always use twisted which has an smtp library which is used for concurrency. Not a complete working example but an understanding of deferreds is required. Ex: from twisted.mail import smtp from twisted.internet import reactor, defer def sendEmail(to, message, subject="Testing", _from='foo at bar.com'): """Used to send emails asyncronously to multiple recipients""" msg = MIMEText(message) msg['Subject'] = subject msg['From'] = _from msg['To'] = ", ".join(to) sending = smtp.sendmail('localhost', _from, to, msg) sending.addCallback(sendComplete, to).addErrback(sendFailed) def sendFailed(error): print "Email failed: %r" % (error.getTraceback(),) def sendComplete(result, recipients): numDelivered, addrInfo = result print addrInfo if (numDelivered != len(recipients)): log.msg(SmtpError, "Not all recipients received email %r" % addrInfo) buf = 'TESTING' sendEmail(to=['to at spam.com'], message=buf) -- Thanks, ./Minesh From jcd at sdf.lonestar.org Tue May 5 13:43:32 2009 From: jcd at sdf.lonestar.org (J. Cliff Dyer) Date: Tue, 05 May 2009 13:43:32 -0400 Subject: list comprehension question In-Reply-To: <49FB2AA7.6020403@snet.net> References: <6d502cfd-e252-4722-96a5-c52fc3579499@v1g2000prd.googlegroups.com> <50697b2c0904301805p7158e167n7f84b18afd0f55f3@mail.gmail.com> <49FB2225.4030601@councilforeconed.org> <49FB2AA7.6020403@snet.net> Message-ID: <1241545412.12966.3.camel@aalcdl07> On Fri, 2009-05-01 at 13:00 -0400, John Posner wrote: > Shane Geiger wrote: > > if type(el) == list or type(el) is tuple: > A tiny improvement: > > if type(el) in (list, tuple): > Another alternative, which might be useful in some cases: if hasattr(el, '__iter__'): This covers all iterables, not just lists and tuples. So: >>> flatten([1,2, xrange(3,15), 15, 16]) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] The downside, of course, is that some iterables might be infinite (such as count), which would cause hidden breakage. But if you have one of those in your data structure, why are you trying to flatten it anyway? Cheers, Cliff From murali.au at gmail.com Tue May 5 13:43:58 2009 From: murali.au at gmail.com (Murali kumar) Date: Tue, 5 May 2009 23:13:58 +0530 Subject: help for documentation and license Message-ID: hi all.. I finished my application using python 2.6 and wxpython 2.8.9 >> I want to generate documentation for my application.. please suggest me and provide links to generate documents in easy way.. >> I want to host my product as open source.. I'dont know about licensing.. help me for this also.. Advanced Thanks .. -------------- next part -------------- An HTML attachment was scrubbed... URL: From jcd at sdf.lonestar.org Tue May 5 14:01:02 2009 From: jcd at sdf.lonestar.org (J. Cliff Dyer) Date: Tue, 05 May 2009 14:01:02 -0400 Subject: list comprehension question In-Reply-To: <85ljpb8r1o.fsf@agentultra.com> References: <6d502cfd-e252-4722-96a5-c52fc3579499@v1g2000prd.googlegroups.com> <85ocucnbce.fsf@agentultra.com> <85ljpb8r1o.fsf@agentultra.com> Message-ID: <1241546462.12966.20.camel@aalcdl07> On Tue, 2009-05-05 at 12:15 -0400, J Kenneth King wrote: > Emile van Sebille writes: > > > On 5/1/2009 7:31 AM J Kenneth King said... > >> Chris Rebert writes: > >>> b = [] > >>> for pair in a: > >>> for item in pair: > >>> b.append(item) > >> > >> This is much more clear than a nested comprehension. > >> > >> I love comprehensions, but abusing them can lead to really dense and > >> difficult to read code. > > > > I disagree on dense and difficult, although I'll leave open the > > question of abuse. > > Dense and difficult may be subjective to people like you or I, but you > left out the "to read" part of that sentence. I was referring to the > negative effect nested or complex list comprehensions can have on > readability. > > > b = [ item for pair in a for item in pair ] > > It's a clever statement, but use it once and its gone. Why not use the > expanded form in a function definition and get an even shorter, but > clearer statement? > It's also not obvious what it means. I would have thought the proper incantation would be: [item for item in pair for pair in a] but that returns [5,5,5,6,6,6]. I still haven't figured out why. The way you have to bounce your eyes back and forth in the comprehension makes it hard to read the logic. With the loop, on the other hand it is blatantly obvious which way the nesting occurs. > > >>> b = flatten(a) > > Boom, done. > > > This is exactly the code above expressed in comprehension form. > > > > It's worth knowing that a list comprehension is structured identically > > to the equivalent for loop. So it really is neither more dense nor > > more difficult to read. Further, you can tell immediately from the > > start of the list comprehension what you've got -- in this case a list > > of item(s). Empirically, I'd have to disagree with you. The structure is changed by the fact that "item" gets put at the beginning of the comprehension. With the loop, each variable gets introduced in a for statement, and then used. With the list comprehension, "item" gets used at the beginning, and then assigned to in the for expression at the end. Meanwhile, pair gets introduced as the loop variable in the first for expression, and iterated over in the second for expression. So do you read it left to right or right to left? Neither. You have to hold the whole expression in your mind at once. Good for short and simple comprehensions, bad as they get more complicated. > > > > Here with some slight changes... > > > >>>> a = [(1, 2), (3, 4, 7), (5, 6)] > >>>> [ item for j in a if len(j)==2 for item in j if item % 2 ] > > [1, 5] > > This, to me, looks like line noise, approaching perl in its unreadability. > > ...opposed to... > > > >>>> for j in a: > > ... if len(j)==2: > > ... for item in j: > > ... if item % 2: > > ... b.append(item) > > ... > >>>> b > > [1, 5] > >>>> > Much nicer. Thank you. > Thanks for the lesson in list comprehensions, but I'm well aware of > how they work. > > List comprehensions can make a reader of your code apprehensive > because it can read like a run-on sentence and thus be difficult to > parse. The Python documentation discourages their use and I believe > for good reason. It's much easier to explain complex processes with a > function rather than a single nested statement. > > > > > YMMV, > > > > Emile > > It will apparently vary greatly. Depending on how much coffee I've > had. > > ;) > > J > -- > http://mail.python.org/mailman/listinfo/python-list > From mccredie at gmail.com Tue May 5 14:04:00 2009 From: mccredie at gmail.com (Matimus) Date: Tue, 5 May 2009 11:04:00 -0700 (PDT) Subject: SQL and CSV References: <1d2bb50f-2e3f-429b-aa98-7e1fe1f011b0@t10g2000vbg.googlegroups.com> Message-ID: <95543497-5eea-4804-9b41-e6a7e842961f@u9g2000pre.googlegroups.com> On May 5, 9:25?am, Nick wrote: > On May 5, 5:19?pm, Tim Golden wrote: > > > > > Nick wrote: > > > I have a requirement to read a CSV file. Normally, no problem, just > > > import CSV and slurp the file up. > > > > However, in this case I want to filter out lines that have fields set > > > to particular values. > > > > It would be neat to be able to do something like this. > > > > select * from test.csv where status <> "Canceled" > > > > Using adodb I can do this, so long as I don't have the where clause. :- > > > ( > > > > Is there a reasonable lightweight way of doing this in Python? > > > > I could write some python code that is used to filter rows, and inport > > > that from config, but it's not quite as elegant as an SQL route. > > > Not entirely clear what you are and aren't prepared to try here, but... > > the most obvious Python-based way to do this is treating the csv reader > > as an iterator and filtering there. Your last line suggests that's not > > what you want but just in case I've misunderstood: > > > > > id,code,status > > 1,"ONE","Active" > > 2,"TWO","Cancelled" > > 3,"THREE","Active" > > > > > > > import csv > > > for row in csv.DictReader (open ("c:/temp/test.csv", "rb")): > > ? if row['status'] != 'Cancelled': > > ? ? print row > > > > > > Doesn't seem too onerous, and could obviously be wrapped in > > some useful class/module. > > > But if you really want to go the SQL route, I believe there are > > ODBC adapters for CSV which, combined with PyODBC or CeODBC, > > would probably take you where you want to go. > > > TJG > > Part of the problem is that the 'selection' needs to be in a config > file. I can put the if row['status'] != 'Cancelled': return True into > a config, read it and eval it, but its not quite as clean as an sql > route. > > Nick Well, if you are using 2.5.x you could always stuff it into a sqlite in-memory database, and then execute a SQL query. Heck, you don't even _need_ 2.5, but in 2.5 sqlite is part of the distribution. Matt From gokhansever at gmail.com Tue May 5 14:30:22 2009 From: gokhansever at gmail.com (=?UTF-8?Q?G=C3=B6khan_SEVER?=) Date: Tue, 5 May 2009 13:30:22 -0500 Subject: help for documentation and license In-Reply-To: References: Message-ID: <49d6b3500905051130r1e8406fg470c6d584710558a@mail.gmail.com> Hi, Even though I don't know what your project does, you will need to use "Sphinx" to create semi-automatic documentation out of your project. I would recommend you to take a look a quality "free" Python module: Matplotlib (http://matplotlib.sourceforge.net/index.html) Go ahead, and check out the main svn trunk. There you will see all what you need about documentation creation with Sphinx. Good luck... G?khan On Tue, May 5, 2009 at 12:43 PM, Murali kumar wrote: > hi all.. > > I finished my application using python 2.6 and wxpython 2.8.9 > >>> I want to generate documentation for my application.. > ????? please suggest me and provide links to generate documents in easy > way.. > >>> I want to host my product as open source.. I'dont know about licensing.. > ????? help me for this also.. > > > Advanced Thanks .. > > > > > -- > http://mail.python.org/mailman/listinfo/python-list > > From debatem1 at gmail.com Tue May 5 14:45:34 2009 From: debatem1 at gmail.com (CTO) Date: Tue, 5 May 2009 11:45:34 -0700 (PDT) Subject: help for documentation and license References: Message-ID: <7b470dd7-ff66-46ba-859f-2a81603ae745@r34g2000vbi.googlegroups.com> On May 5, 2:30?pm, G?khan SEVER wrote: > Hi, > > Even though I don't know what your project does, you will need to use > "Sphinx" to create semi-automatic documentation out of your project. > > I would recommend you to take a look a quality "free" Python module: > Matplotlib (http://matplotlib.sourceforge.net/index.html) > Go ahead, and check out the main svn trunk. There you will see all > what you need about documentation creation with Sphinx. > > Good luck... > > G?khan > > On Tue, May 5, 2009 at 12:43 PM, Murali kumar wrote: > > hi all.. > > > I finished my application using python 2.6 and wxpython 2.8.9 > > >>> I want to generate documentation for my application.. > > ????? please suggest me and provide links to generate documents in easy > > way.. > > >>> I want to host my product as open source.. I'dont know about licensing.. > > ????? help me for this also.. You can autogenerate API docs using pydoc (builtin, usually ugly) or epydoc (external, not so ugly), or you can use a project like sphinx for hand written docs. As far as hosting goes, sourceforge is usually the way to go, although I'm a huge fan of gitorious (in fact, wrote a recipe to autogenerate API docs for markdown ) but you have to be using git for it to work. Github is another choice. If that's the case. As far as licensing goes, you have a *LOT* of options, so you may want to check out Geremy Condra From stefan_ml at behnel.de Tue May 5 14:46:45 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 05 May 2009 20:46:45 +0200 Subject: thc v0.3 - txt to html converter - better code? In-Reply-To: References: Message-ID: <4a008996$0$31862$9b4e6d93@newsspool3.arcor-online.net> Florian Wollenschein wrote: > here's some code of thc, my txt to html converter programmed with Python > and pyQT4: > ------------------------------------------------------------------------------- > > if self.rdioBtnTransitional.isChecked(): > if self.cmboBoxLang.currentText() == "English": > file_open.write(' 4.0 Transitional//EN">' + '\n') > elif self.cmboBoxLang.currentText() == "German": > file_open.write(' 4.0 Transitional//DE">' + '\n') > else: > file_open.write(' Strict//EN">' + '\n') > if self.cmboBoxLang.currentText() == "English": > file_open.write(' 4.0 Strict//EN">' + '\n') > elif self.cmboBoxLang.currentText() == "German": > file_open.write(' 4.0 Strict/DE">' + '\n') > -------------------------------------------------------------------------------- > > > Do you have any ideas for a better code than that? Could this be done > smarter, shorter, whatever!? I assume you are referring to the code duplication above. You can build the string incrementally instead, e.g. language_map = {'English': 'EN', 'Deutsch': 'DE'} strict_or_transitional = {True: 'Transitional', False: 'Strict'} # this will raise a KeyError for unknown languages language = language_map[ self.cmboBoxLang.currentText() ] # this assumes that isChecked() returns True or False spec = strict_or_transitional[self.rdioBtnTransitional.isChecked()] doctype = '\n' % ( spec, language) Stefan From kyosohma at gmail.com Tue May 5 14:57:12 2009 From: kyosohma at gmail.com (Mike Driscoll) Date: Tue, 5 May 2009 11:57:12 -0700 (PDT) Subject: desperately looking for a howto on running my wxPython app on Vista References: <49f6d607$0$199$e4fe514c@news.xs4all.nl> <4A006CCD.5060901@xs4all.nl> Message-ID: <1f97707a-9fd8-45d0-8aa6-142ce16b1951@e24g2000vbe.googlegroups.com> On May 5, 11:43?am, Paul Sijben wrote: > Mike Driscoll wrote: > > On Apr 29, 4:17 am, Paul Sijben wrote: > >> Is there any way to check which is the offending pyd/dll? ?(normally > >> Vista does not give out much data on what went wrong) > > >> Paul > > > You might be able to find it using the Dependency Walker utility: > > >http://www.dependencywalker.com/ > > Mike, thanks! That spotted indeed some issues with one specific file: > SHLWAPI.DLL, now let's see what is the issue. The report from Dependency > ?Walker:"Warning: At least one module has an unresolved import due to a > missing export function in a delay-load dependent module." is not > telling me much at this moment. The FAQ from Dependency Walker sais this > ? is not a major issue, but apparently on Vista it is ?!? > > Does anyone have a clue what I am to do about this? > > Interestingly On WinXP it flags DWMAPI.DLL as missing. I did not spot > that on the Vista box. > > Paul > > > > > - Mike Hmmm...I'm not familiar with that DLL, but a little googling seems to indicate that you may be able to get it off your installation CD: http://www.techimo.com/forum/applications-operating-systems/76550-winxp-msgina-dll-shlwapi-dll-problem.html The link above gives the steps for XP, but Vista will probably be similar. I don't know for sure, but you may have to register the dll once you've got it copied back onto your machine. Mike From davea at ieee.org Tue May 5 14:59:21 2009 From: davea at ieee.org (Dave Angel) Date: Tue, 05 May 2009 14:59:21 -0400 Subject: call function of class instance with no assigned name? In-Reply-To: <447f2963-07b1-40bb-b084-ddf6c9cc880b@c18g2000prh.googlegroups.com> References: <447f2963-07b1-40bb-b084-ddf6c9cc880b@c18g2000prh.googlegroups.com> Message-ID: <4A008C89.8090503@ieee.org> George Oliver wrote: > hi, I'm a Python beginner with a basic question. I'm writing a game > where I have keyboard input handling defined in one class, and command > execution defined in another class. The keyboard handler class > contains a dictionary that maps a key to a command string (like 'h': > 'left') and the command handler class contains functions that do the > commands (like def do_right(self):), > > I create instances of these classes in a list attached to a third, > 'brain' class. What I'd like to have happen is when the player presses > a key, the command string is passed to the command handler, which runs > the function. However I can't figure out a good way to make this > happen. > > I've tried a dictionary mapping command strings to functions in the > command handler class, but those dictionary values are evaluated just > once when the class is instantiated. I can create a dictionary in a > separate function in the command handler (like a do_command function) > but creating what could be a big dictionary for each input seems kind > of silly (unless I'm misunderstanding something there). > > What would be a good way to make this happen, or is there a different > kind of architecture I should be thinking of? > > You have lots of words, but no code, and no explanation of why you're choosing this "architecture." So it's unclear just what to propose to you, without sticking to generalities. 1) forget about getattr() unless you have hundreds of methods in your map. The real question is why you need two maps. What good is the "command string" doing you? Why not just map the keyvalues directly into function objects? 2) Look at the following simple example, where I'm assuming that a "keycode" is simply an ASCII character. class Command(object): def __init__(self): pass def doEnter(self): print "Enter is done" def doA(self): print "A is done" cmd = Command() table = { 13: cmd.doEnter, 65: cmd.doA } print "trying 13" key = 13 table[key]() print "trying 65" key = 65 table[key]() 3) As for rebuilding the dictionary, that just depends on where you build it, and what it's lifetime is. If it's got enough lifetime, and appropriate scope, you wouldn't have to rebuild anything. 4) Code up a first try, and when you get stuck, ask a specific question. You said you're a beginner. So keep it as simple as possible.l From http Tue May 5 15:01:39 2009 From: http (Paul Rubin) Date: 05 May 2009 12:01:39 -0700 Subject: Self function References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> <202a05cd-3ce4-41fb-943b-5fd3f18d9bbb@z5g2000vba.googlegroups.com> <8c18a63a-8ca5-44bd-9b47-c1247a8f1a97@v4g2000vba.googlegroups.com> <37e0021f-b50c-4f0c-8ba5-0beed01da029@z16g2000prd.googlegroups.com> Message-ID: <7xhbzzz84s.fsf@ruckus.brouhaha.com> Steven D'Aprano writes: > def rvisit(node): > print node > if node and node.link is not None: > rvisit(node.link) Less redundant (remember the extra "recursion" doesn't cost anything if the compiler is sane enough to turn it into a jump): def rvisit(node): print node if node: rvisit(node.link) From chambon.pascal at wanadoo.fr Tue May 5 15:05:15 2009 From: chambon.pascal at wanadoo.fr (Pascal Chambon) Date: Tue, 05 May 2009 21:05:15 +0200 Subject: Which one is best Python or Java for developing GUI applications? In-Reply-To: <50697b2c0905050228l5fee0351re35f61f0fe9d35ab@mail.gmail.com> References: <7xfxfkvwra.fsf@ruckus.brouhaha.com> <87d4ao9fi7.fsf@rudin.co.uk> <50697b2c0905050228l5fee0351re35f61f0fe9d35ab@mail.gmail.com> Message-ID: <4A008DEB.80607@wanadoo.fr> Chris Rebert a ?crit : > On Tue, May 5, 2009 at 12:26 AM, Paul Rudin wrote: > >> Paul Rubin writes: >> >> >>> srinivasan srinivas writes: >>> >>>> Could you tell me does Python have any advantages over Java for the >>>> development of GUI applications? >>>> >>> Yes. >>> >> Clearly c.l.p needs to adopt the SNB >> convention :) >> > > Surely you have forgotten the comedy style of the language's namesake, > which makes that rule completely inadmissible! ;P > > Cheers, > Chris > The fact that Python is a dynamic language offers, in my opinion, a huge advantage to quickly setup a GUI, without caring about the infinite details of the variable types and function signatures. Its good handling of "function as first-class objects" is also precious when comes the time of setting callbacks (I can't bear anymore the way Swing does it, with interfaces etc.) But much depends on the framework used, too. I've used wxPython for a multimedia project, and actually it lacked a lot of necessary features (transparency, event loop tuning, multithreading support...), but that was 1 year ago, maybe things have changed. Anyway, I'd advocate the use of PyQt, which really offers tremendous possibilities - if your application isn't a simple office application, its' really worth turning towards pyqt. Regards, Pascal -------------- next part -------------- An HTML attachment was scrubbed... URL: From a.cavallo at cavallinux.eu Tue May 5 15:06:17 2009 From: a.cavallo at cavallinux.eu (A. Cavallo) Date: Tue, 5 May 2009 20:06:17 +0100 Subject: Numpy on python 2.7a Message-ID: <200905052006.17708.a.cavallo@cavallinux.eu> Hi, I'm trying to compile numpy using my own pet project based on the python svn code (that's the reason for the 2.7a tag). It is essentially a /opt python installation and it tailored for linux: its main goal is to be installed in parallel so it won't collide with a system installed python (this won't impact the sys admin work). The rpms built so far can be found under: http://download.opensuse.org/repositories/home:/cavallo71:/python-opt/ It includes several patches both stolen from other sources, some made by myself (at some point I plan to do the same for debian based distributions when I'll have some spare time) and it contains a smoke test to verify the build is ok. The user willing to activate the "opt-python" has to just source the file /opt/opt-python-2.7a0/opt-python-env.sh and he/she will have a fully working python (in this way there could be several different python version at the same time). So far so good, I've been able to compile the python imaging library, but it fails to compile numpy. The message I'm receiving is: ============ /opt/opt-python-2.7a0/include/python2.7/pyconfig.h:127:1: warning: this is the location of the previous definition ar: adding 1 object files to build/temp.linux-i686-2.7/libnpymath.a ar: illegal option -- / Usage: ar [emulation options] [-]{dmpqrstx}[abcfilNoPsSuvV] [member-name] [count] archive-file file... ar -M [ - read options from emulation options: No emulation specific options ar: supported targets: elf32-i386 a.out-i386-linux efi-app-ia32 efi-bsdrv-ia32 efi-rtdrv-ia32 elf32-little elf32-big elf64-alpha ecoff-litt lealpha elf64-little elf64-big elf32-littlearm elf32-bigarm elf32-hppa-linux elf32-hppa elf64-hppa-linux elf64-hppa elf64-x86-64 elf64-ia64 -little elf64-ia64-big efi-app-ia64 efi-bsdrv-ia64 efi-rtdrv-ia64 elf32-m68k a.out-m68k-linux elf32-tradbigmips elf32-tradlittlemips ecoff- bigmips ecoff-littlemips elf32-ntradbigmips elf64-tradbigmips elf32-ntradlittlemips elf64-tradlittlemips elf32-powerpc aixcoff-rs6000 elf32 -powerpcle ppcboot elf64-powerpc elf64-powerpcle aixcoff64-rs6000 elf32-s390 elf64-s390 elf32-sh-linux elf32-shbig-linux elf32-sparc a.out- sparc-linux elf64-sparc a.out-sunos-big efi-app-x86_64 efi-bsdrv-x86_64 efi-rtdrv-x86_64 mach-o-le mach-o-be mach-o-fat pef pef-xlib sym sr ec symbolsrec tekhex binary ihex trad-core ar: illegal option -- / Usage: ar [emulation options] [-]{dmpqrstx}[abcfilNoPsSuvV] [member-name] [count] archive-file file... ============ Is there any way I can increase the output to discover what went wrong? Best regards, Antonio Cavallo From georgeoliverGO at gmail.com Tue May 5 15:17:23 2009 From: georgeoliverGO at gmail.com (George Oliver) Date: Tue, 5 May 2009 12:17:23 -0700 (PDT) Subject: call function of class instance with no assigned name? References: <447f2963-07b1-40bb-b084-ddf6c9cc880b@c18g2000prh.googlegroups.com> Message-ID: On May 5, 11:59 am, Dave Angel wrote: > 1) forget about getattr() unless you have hundreds of methods in your > map. The real question is why you need two maps. What good is the > "command string" doing you? Why not just map the keyvalues directly > into function objects? Thanks for the reply Dave. I understand your example and it's what I originally used. Here is more detail on what I'm doing now, I hope this will explain my question better. In the game I'm writing the player, monsters, items and so on are instances of class Thing, like: class Thing(object): def __init__(self, x, y, name): self.x, self.y = x, y self.name = name self.brain = None Some Things have an instance of class Brain attached. The Brain instance has a list of handlers, like: class Brain(object): def __init__(self): self.handlers = [] A handler class defines some functionality for the Brain. Each Brain has an update method like this: def update(self, arguments): for handler in self.handlers: handler.update(arguments) So on each pass through the main game loop, it calls the update method of all the brains in the game, which run their handler update methods to change the state of the game. A handler would be something like a key input handler. The key input handler is defined separately from the command handler in the case I want to use a different method of input, for example a mouse or joystick. In the dictionary of key inputs I could map each input directly to a function. However there is no instance name I can call the function on, as I create a thing, add a brain, and add handlers to the brain like this: player = Thing(26, 16, 'player') player.brain = Brain() player.brain.add_handlers( commandHandler(player.brain, player), keyboardHandler(player.brain), fovHandler(player.brain)) So what I'm wondering is how to reference the instance in each brain's list of handlers when I want to map something like a key input to a command, or what a better way might be to structure the code. From mail at timgolden.me.uk Tue May 5 15:27:37 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Tue, 05 May 2009 20:27:37 +0100 Subject: SQL and CSV In-Reply-To: <1d2bb50f-2e3f-429b-aa98-7e1fe1f011b0@t10g2000vbg.googlegroups.com> References: <1d2bb50f-2e3f-429b-aa98-7e1fe1f011b0@t10g2000vbg.googlegroups.com> Message-ID: <4A009329.70608@timgolden.me.uk> Nick wrote: > Part of the problem is that the 'selection' needs to be in a config > file. I can put the if row['status'] != 'Cancelled': return True into > a config, read it and eval it, but its not quite as clean as an sql > route. Still not clear what the restriction is. If you were writing SQL you'd have to read *something* from your config file, unless you're suggesting that the "config file" is in fact a SQL file. Which is one way of doing it, but then you might just as well have your config file as a Python file and import it. Have I missed the point somewhere here? Can you give an example -- even a fictional one -- of what you couldn't do using, say, the example I gave earlier? TJG From showell30 at yahoo.com Tue May 5 15:50:41 2009 From: showell30 at yahoo.com (Steve Howell) Date: Tue, 5 May 2009 12:50:41 -0700 (PDT) Subject: Self function References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> Message-ID: <6c6fffd7-2f43-454d-8e63-056d0c1c05d3@w31g2000prd.googlegroups.com> On May 4, 11:08?pm, Steven D'Aprano wrote: > > I propose a small piece of sugar. When a function is entered, Python > creates an ordinary local name in the function's local namespace, and > binds the function itself to that name. Two possibilities for the name > are `this` or `__this__`, analogous to `self` in methods and `__name__` > in modules. > > If there is any support for this, I propose to send the following (long) > post to python-ideas. Feedback, corrections and suggestions welcome. > I totally support this proposal. I've definitely wasted time in the past trying to invent my own workarounds for the use cases you describe. Obviously, there will be some bikeshed debates on __this__ vs. __name__ vs. __func__, etc. I don't have an opinion there, just want *something* handy for introspection, DRYness, etc. A more interesting question is whether __this__ would just always be there (that's my vote), or if you should have to apply a special decorator to get it (which I oppose, but I can see some merits). From kushal.kumaran at gmail.com Tue May 5 15:51:04 2009 From: kushal.kumaran at gmail.com (Kushal Kumaran) Date: Wed, 6 May 2009 01:21:04 +0530 Subject: Way to use proxies & login to site? In-Reply-To: <629c75b1-9e67-44fb-9de0-0a23ff6c306b@v23g2000pro.googlegroups.com> References: <36bb20c2-2199-42bb-a123-171bf380182c@y10g2000prc.googlegroups.com> <629c75b1-9e67-44fb-9de0-0a23ff6c306b@v23g2000pro.googlegroups.com> Message-ID: <1e364c4e0905051251g7f98c61ds941c9574f589aa65@mail.gmail.com> On Wed, Apr 29, 2009 at 8:21 AM, inVINCable wrote: > On Apr 27, 7:40?pm, inVINCable wrote: >> Hello, >> >> I have been using ClientForm to log in to sites & ClientCookie so I >> can automatically log into my site to do some penetration testing, >> although, I cannot figure out a solution to use proxies with this >> logging in automatically. Does anyone have any solutions? >> >> Thanks :) >> >> Vince > > Any ideas? If, like the example at http://wwwsearch.sourceforge.net/ClientForm/, you are using urllib2, you can read the documentation for that module. It also has examples for working with proxies. -- kushal From castironpi at gmail.com Tue May 5 16:02:47 2009 From: castironpi at gmail.com (Aaron Brady) Date: Tue, 5 May 2009 13:02:47 -0700 (PDT) Subject: Self function References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> <6c6fffd7-2f43-454d-8e63-056d0c1c05d3@w31g2000prd.googlegroups.com> Message-ID: <4a69c949-ace7-47d9-8bb7-d05bc6c895f3@s31g2000vbp.googlegroups.com> On May 5, 2:50?pm, Steve Howell wrote: > On May 4, 11:08?pm, Steven D'Aprano > > wrote: > > > I propose a small piece of sugar. When a function is entered, Python > > creates an ordinary local name in the function's local namespace, and > > binds the function itself to that name. Two possibilities for the name > > are `this` or `__this__`, analogous to `self` in methods and `__name__` > > in modules. > > > If there is any support for this, I propose to send the following (long) > > post to python-ideas. Feedback, corrections and suggestions welcome. > > I totally support this proposal. ? I've definitely wasted time in the > past trying to invent my own workarounds for the use cases you > describe. > > Obviously, there will be some bikeshed debates on __this__ vs. > __name__ vs. __func__, etc. ?I don't have an opinion there, just want > *something* handy for introspection, DRYness, etc. > > A more interesting question is whether __this__ would just always be > there (that's my vote), or if you should have to apply a special > decorator to get it (which I oppose, but I can see some merits). Here is how to get the function into the function as an argument, and still permit recursive calls onto it. >>> def auto( f ): ... def _inner( *ar, **kw ): ... return f( g, *ar, **kw ) ... g= _inner ... return g ... >>> @auto ... def f( self, n ): ... print( self, n ) ... return 1 if n== 1 else n* self( n- 1 ) ... >>> f(7) 7 6 5 4 3 2 1 5040 The function that is passed into the function is the decorated function, not the original. The decorator function is also returned from the decorator calls, so both the external caller and the internal caller are calling the same function, '_inner' in the sample, which then back-calls the original. Please stay tuned for the post-graduate lecture. There will be time for questions at the cookies-and-creme seminar afterward. From garylinux at gmail.com Tue May 5 16:12:17 2009 From: garylinux at gmail.com (justme) Date: Tue, 5 May 2009 13:12:17 -0700 (PDT) Subject: AutoComplete in C++ Editor for Python References: Message-ID: <3746e883-79bc-444c-8b09-641675339954@s20g2000vbp.googlegroups.com> I the rope project http://rope.sourceforge.net/ has an autocomplete lib. (I have not used it just remember reading about it) From castironpi at gmail.com Tue May 5 16:16:39 2009 From: castironpi at gmail.com (Aaron Brady) Date: Tue, 5 May 2009 13:16:39 -0700 (PDT) Subject: call function of class instance with no assigned name? References: <447f2963-07b1-40bb-b084-ddf6c9cc880b@c18g2000prh.googlegroups.com> Message-ID: <0c2f0973-1829-46fa-a28c-c38e7906e750@l5g2000vbc.googlegroups.com> On May 5, 2:17?pm, George Oliver wrote: > On May 5, 11:59 am, Dave Angel wrote: > > > 1) forget about getattr() unless you have hundreds of methods in your > > map. ?The real question is why you need two maps. What good is the > > "command string" doing you? ? Why not just map the keyvalues directly > > into function objects? > > Thanks for the reply Dave. I understand your example and it's what I > originally used. Here is more detail on what I'm doing now, I hope > this will explain my question better. > > In the game I'm writing the player, monsters, items and so on are > instances of class Thing, like: > > class Thing(object): > ? ? def __init__(self, x, y, name): > ? ? ? ? self.x, self.y = x, y > ? ? ? ? self.name = name > ? ? ? ? self.brain = None > > Some Things have an instance of class Brain attached. The Brain > instance has a list of handlers, like: > > class Brain(object): > ? ? def __init__(self): > ? ? ? ? self.handlers = [] > > A handler class defines some functionality for the Brain. Each Brain > has an update method like this: > > def update(self, arguments): > ? ? for handler in self.handlers: > ? ? ? ? handler.update(arguments) > > So on each pass through the main game loop, it calls the update method > of all the brains in the game, which run their handler update methods > to change the state of the game. > > A handler would be something like a key input handler. The key input > handler is defined separately from the command handler in the case I > want to use a different method of input, for example a mouse or > joystick. > > In the dictionary of key inputs I could map each input directly to a > function. However there is no instance name I can call the function > on, as I create a thing, add a brain, and add handlers to the brain > like this: > > player = Thing(26, 16, 'player') > player.brain = Brain() > player.brain.add_handlers( > ? ? ? ? ? ? ? ? ? ? ? ? ? ? commandHandler(player.brain, player), > ? ? ? ? ? ? ? ? ? ? ? ? ? ? keyboardHandler(player.brain), > ? ? ? ? ? ? ? ? ? ? ? ? ? ? fovHandler(player.brain)) > > So what I'm wondering is how to reference the instance in each brain's > list of handlers when I want to map something like a key input to a > command, or what a better way might be to structure the code. I'm not entirely sure I follow, but you may want to try keeping a set (or list) of handlers that you pass in to the 'add_handlers' function, and trying the 'getattr( handlerX, keyY )' on each of them, possible only until you have a success. You could also cache these results to avoid performing the same search subsequently. Otherwise, query each of the passed-in handlers when they are passed in for a list of events they can handle; this may be by a brute 'getattr( x ) for x in ALL_EVENTS', or by maintaining a list of handlers on a per-object basis, and merely combining the dictionaries; then pick one or all of the handlers you've accumulated for an event when the event occurs. If you're really trying to be clever, you might use a metaclass or class decorator to accumulate what events are handled in a class; or just try the '__dict__' member of the /class/. Will any of your handlers be handling overlapping or duplicate events? Do they all get a crack at it, or do you follow a pre- deterimend order of precedence, or follow the order in which they were passed in? I'd probably try to discourage you from circularly linking the object and handlers, not only for practical reasons. There is also the possibility you could use dynamic mix-ins to create a specialized once-in-a-lifetime brain class: def create_brain( handlers ): class new_brain( Brain ): pass for x in handlers: setattr( new_brain, x.__name__, x ) return new_brain brain= create_brain( handlers )( creation_args ) Forgive, just Thing-king aloud. From duncan.booth at invalid.invalid Tue May 5 16:19:54 2009 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 5 May 2009 20:19:54 GMT Subject: Self function References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> <6c6fffd7-2f43-454d-8e63-056d0c1c05d3@w31g2000prd.googlegroups.com> <4a69c949-ace7-47d9-8bb7-d05bc6c895f3@s31g2000vbp.googlegroups.com> Message-ID: Aaron Brady wrote: > Here is how to get the function into the function as an argument, and > still permit recursive calls onto it. > >>>> def auto( f ): > ... def _inner( *ar, **kw ): > ... return f( g, *ar, **kw ) > ... g= _inner > ... return g > ... >>>> @auto > ... def f( self, n ): > ... print( self, n ) > ... return 1 if n== 1 else n* self( n- 1 ) > ... >>>> f(7) > 7 > 6 > 5 > 4 > 3 > 2 > 1 > 5040 > It might be better to use some convention other than 'self' for the first argument otherwise it will cause confusion when writing recursive methods: >>> class C(object): @auto def meth(this, self, n): print this, self, n return 1 if n==1 else n*this(self, n-1) >>> inst = C() >>> inst.meth(5) <__main__.C object at 0x000000000366C4E0> 5 <__main__.C object at 0x000000000366C4E0> 4 <__main__.C object at 0x000000000366C4E0> 3 <__main__.C object at 0x000000000366C4E0> 2 <__main__.C object at 0x000000000366C4E0> 1 120 From ross.jett at gmail.com Tue May 5 16:25:24 2009 From: ross.jett at gmail.com (Ross) Date: Tue, 5 May 2009 13:25:24 -0700 (PDT) Subject: Code works fine except... References: <8d4ec1df-dddb-469a-99a1-695152db7c04@n4g2000vba.googlegroups.com> <7f3493ca-efcb-48cc-aa29-487bb11fcd09@p4g2000vba.googlegroups.com> <16d47b2f-14d4-4266-84d2-c26501beb0c1@r34g2000vbi.googlegroups.com> <1691f042-bc14-46b9-b7b3-5a44348e9965@b1g2000vbc.googlegroups.com> <1a497471-498a-43d8-90ba-d676e8acf68b@r36g2000vbr.googlegroups.com> <8b49b775-66a7-42d5-9b00-cf23d9b044cc@e23g2000vbe.googlegroups.com> <54d76f11-c255-487d-9c3a-aaa380d7827a@z7g2000vbh.googlegroups.com> <991ccae3-f3e2-4d73-bd32-bc6f560e6f5f@n8g2000vbb.googlegroups.com> Message-ID: <3a4e6e31-76fd-4a85-8270-a7efbaf2ff1d@o27g2000vbd.googlegroups.com> On May 5, 1:33?pm, MRAB wrote: > Ross wrote: > > On May 5, 12:32 am, John Yeung wrote: > >> On May 5, 1:12 am, John Yeung wrote: > > >>> [...] the problem may require bigger guns (either much better > >>> math or much more sophisticated programming). > >> Yes, I'm responding to myself. > > >> Well, I went ahead with the approach I mentioned earlier, generating > >> all possible matches and then selecting among them as needed to fill > >> up the courts, trying to keep the number of matches played by each > >> player as fair as possible. ?(I should mention that this, or something > >> similar, was suggested earlier by someone else in a different thread, > >> in response to the same question by the same OP.) > > >> I did use "bigger guns" (mainly a class for player objects, with > >> custom __cmp__ method), but still didn't do anything with doubles. > > >> I haven't tested it much, but I'll post it if anyone's interested. > >> (That way people can pick on me instead of the OP. ;) > > >> John > > > I'm interested to see what you did. From your description, it sounds > > like I've tried what you've done, but when I implemented my version, > > it took minutes to evaluate for bigger numbers. If that isn't the case > > with yours, I'd be interested in seeing your implementation. > > Here's my approach (incomplete): > > def get_pair(player_list, played): > ? ? ?for first in range(len(player_list)): > ? ? ? ? ?player_1 = player_list[first] > ? ? ? ? ?for second in range(first + 1, len(player_list)): > ? ? ? ? ? ? ?player_2 = player_list[second] > ? ? ? ? ? ? ?pair = player_1, player_2 > ? ? ? ? ? ? ?sorted_pair = tuple(sorted(pair)) > ? ? ? ? ? ? ?if sorted_pair not in played: > ? ? ? ? ? ? ? ? ?played.add(sorted_pair) > ? ? ? ? ? ? ? ? ?del player_list[second] > ? ? ? ? ? ? ? ? ?del player_list[first] > ? ? ? ? ? ? ? ? ?return pair > ? ? ?return None > > def round_robin(player_list, courts, played): > ? ? ?playing = [] > ? ? ?for c in range(courts): > ? ? ? ? ?pair = get_pair(player_list, played) > ? ? ? ? ?if pair is None: > ? ? ? ? ? ? ?break > ? ? ? ? ?playing.append(pair) > ? ? ?byes = player_list[:] > ? ? ?player_list[:] = byes + [player for pair in playing for player in pair] > ? ? ?yield playing, byes > > def test_round_robin(players, rounds, courts, doubles=False): > ? ? ?player_list = range(players) > ? ? ?played = set() > ? ? ?for r in range(rounds): > ? ? ? ? ?for playing, byes in round_robin(player_list, courts, played): > ? ? ? ? ? ? ?print playing, byes- Hide quoted text - > > - Show quoted text - Looks like somewhat of an improvement, although the bye distribution is still slightly lopsided. For example, in a singles league with 12 players, 12 rounds, and 4 courts, The first player had at least 2 less byes than every other player and 3 less byes than the players with the most number of byes. Thanks for your input though...I'll look into how I can improve upon it. From doctoresam at gmail.com Tue May 5 16:37:53 2009 From: doctoresam at gmail.com (Deep_Feelings) Date: Tue, 5 May 2009 13:37:53 -0700 (PDT) Subject: python docs for beginner programmer? References: Message-ID: <81f82d8c-80fa-4ba0-a402-3a8b5757a967@s16g2000vbp.googlegroups.com> that is good ,thank you anyone did that ? learning from python docs straight away ? From bearophileHUGS at lycos.com Tue May 5 16:43:16 2009 From: bearophileHUGS at lycos.com (bearophileHUGS at lycos.com) Date: Tue, 5 May 2009 13:43:16 -0700 (PDT) Subject: Self function References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> <1292fea0-96fb-4a21-b39d-b6ce610ea6b2@t10g2000vbg.googlegroups.com> Message-ID: <4f13a110-b3e5-4de5-b6d6-0d64da20b61e@r34g2000vbi.googlegroups.com> wolfram.hinde...: > It is easy to change all references of the function name, except for > those in the function body itself? That needs some explantation. I can answer this. If I have a recursive function, I may want to create a similar function, so I copy and paste it, to later modify the copied version. Then to change its name I usually don't use a search & rename of its name into its block of code because it's usually useless. In non-recursive functions the name of the function is stated only once, at the top. Recursive functions violate the DRY principle, that's the root of the problem. The auto(f) decorator shown later by Aaron Brady seems the best solution found so far, it's so nice that it even seems practically usable. Bye, bearophile From bearophileHUGS at lycos.com Tue May 5 16:54:44 2009 From: bearophileHUGS at lycos.com (bearophileHUGS at lycos.com) Date: Tue, 5 May 2009 13:54:44 -0700 (PDT) Subject: Self function References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> <6c6fffd7-2f43-454d-8e63-056d0c1c05d3@w31g2000prd.googlegroups.com> <4a69c949-ace7-47d9-8bb7-d05bc6c895f3@s31g2000vbp.googlegroups.com> Message-ID: <50222a39-88d0-49da-9105-2d3a47af5940@r34g2000vba.googlegroups.com> Aaron Brady: > >>> def auto( f ): > > ... ? ? def _inner( *ar, **kw ): > ... ? ? ? ? ? ? return f( g, *ar, **kw ) > ... ? ? g= _inner > ... ? ? return g Looks nice, I'll try to the following variant to see if it's usable: def thisfunc(fun): """Decorator to inject a default name of a function inside a function""" def _inner(*args, **kwds): return fun(g, *args, **kwds) g = _inner g.__name__ = fun.__name__ g.__dict__.update(fun.__dict__) g.__doc__ = fun.__doc__ g.__module__ = fun.__module__ return g @thisfunc def SOMEVERYUGLYNAME(this, n): return 1 if n <= 1 else n * this(n - 1) assert SOMEVERYUGLYNAME(6) == 2*3*4*5*6 Bye, bearophile From piet at cs.uu.nl Tue May 5 17:03:28 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Tue, 05 May 2009 23:03:28 +0200 Subject: Error in running python -v on Mac ox 10.5. References: Message-ID: >>>>> silverburgh (s) wrote: >s> Hi, >s> I run 'python -v' on Macos 10.5 but I get this error : >s> # can't create /System/Library/Frameworks/Python.framework/Versions/ >s> 2.5/lib/python2.5/encodings/utf_8.pyc >s> Can you please tell me how to fix it? This looks like the utf_8.py file has not been compiled in the installation. Looks like a (not serious) bug to me. Probably sudo python -v would solve it (even without -v, but it will tell you that it wrote the .pyc file). >s> And why I am seeing a lot of 'install' message' when i run 'python - >s> v'? I don't think I see that many >s> install message on linux. I don't see 'install' messages in the included text. >s> Here is the full message. Thanks for any help. [deleted] -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From castironpi at gmail.com Tue May 5 17:20:15 2009 From: castironpi at gmail.com (Aaron Brady) Date: Tue, 5 May 2009 14:20:15 -0700 (PDT) Subject: Self function References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> <6c6fffd7-2f43-454d-8e63-056d0c1c05d3@w31g2000prd.googlegroups.com> <4a69c949-ace7-47d9-8bb7-d05bc6c895f3@s31g2000vbp.googlegroups.com> <50222a39-88d0-49da-9105-2d3a47af5940@r34g2000vba.googlegroups.com> Message-ID: <5b81cccc-c174-42bd-b4a0-638755ca9e03@l5g2000vbc.googlegroups.com> On May 5, 3:54?pm, bearophileH... at lycos.com wrote: > Aaron Brady: > > > >>> def auto( f ): > > > ... ? ? def _inner( *ar, **kw ): > > ... ? ? ? ? ? ? return f( g, *ar, **kw ) > > ... ? ? g= _inner > > ... ? ? return g > > Looks nice, I'll try to the following variant to see if it's usable: > > def thisfunc(fun): > ? ? """Decorator to inject a default name of a > ? ? function inside a function""" > ? ? def _inner(*args, **kwds): > ? ? ? ? return fun(g, *args, **kwds) > ? ? g = _inner > ? ? g.__name__ = fun.__name__ > ? ? g.__dict__.update(fun.__dict__) > ? ? g.__doc__ = fun.__doc__ > ? ? g.__module__ = fun.__module__ > ? ? return g > > @thisfunc > def SOMEVERYUGLYNAME(this, n): > ? ? return 1 if n <= 1 else n * this(n - 1) > > assert SOMEVERYUGLYNAME(6) == 2*3*4*5*6 > > Bye, > bearophile /Actually/, the 'g' variable was spurious. FWIW. def auto( f ): def _inner( *ar, **kw ): return f( _inner, *ar, **kw ) return _inner From robert.kern at gmail.com Tue May 5 17:30:02 2009 From: robert.kern at gmail.com (Robert Kern) Date: Tue, 05 May 2009 17:30:02 -0400 Subject: Numpy on python 2.7a In-Reply-To: <200905052006.17708.a.cavallo@cavallinux.eu> References: <200905052006.17708.a.cavallo@cavallinux.eu> Message-ID: On 2009-05-05 15:06, A. Cavallo wrote: > Hi, > > I'm trying to compile numpy using my own pet project based on the python svn > code (that's the reason for the 2.7a tag). Ask on the numpy-discussion mailing list: http://www.scipy.org/Mailing_Lists -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco From QJing.Li at gmail.com Tue May 5 17:42:15 2009 From: QJing.Li at gmail.com (Leon) Date: Tue, 5 May 2009 14:42:15 -0700 (PDT) Subject: what's the best way to call a method of object without a guarantee of its existence References: <22690572-e338-483a-a99a-85ccf6c5fae6@x6g2000vbg.googlegroups.com> <9uULl.4199$Ux.2825@tornado.fastwebnet.it> Message-ID: <0348bb8e-3c1a-410d-a886-352b9b0ae391@e23g2000vbe.googlegroups.com> On May 5, 3:25?am, Marco Mariani wrote: > Leon wrote: > > One way, ?define the object before it is used, > > like this: > > object = None > > This is a good practice anyway. Conditional existance of objects is > quite evil. Resorting to if defined('foo') is double-plus-ugly. This was why I asked this question. In my program, I use the pattern of object.method() a lot, but every time I need to check if it exists or not, which makes me feel uncomfortable. > > The other way, using try ... catch > > try: > > ? ? ?object.method() > > catch NameError: > > ? ? ?pass > > Except you should trap AttributeError because you defined the thing as > None before. NameErrors should be fixed as bugs, not trapped (IMHO -- > but in python there is always a use case for everything). > > Keep in mind that AttributeError might come from inside the method(), > which could be confusing > > By using the > > if stuff: > ? ? stuff.run() > > idiom, you avoid the last issue and keep it simple enough. > > > for ?big programs, which is better, or any other way? > > Define "big", as in scope, LOCs, or number of committers? "big", I mean, is not in the same scope For instance, class frame(wx.Frame): def __init__(self, parent, ....): .... self.parent = parent # parent from another module ..... def eventHandler_1(self,event): if self.parent.object_1 is not None: self.parent.object_1.method() def eventHandler_2(self, event): if self.parent.object_2 is not None: self.parent.object_2.method() So I need to go back to the module including "parent" class to define the objects that I maybe use in future as None, actually, the module including "parent' class works very well without those definitions, from parent class's point of view, those definitions are kind of noisy. Thanks Marco From gunterhenriksen at gmail.com Tue May 5 18:05:46 2009 From: gunterhenriksen at gmail.com (Gunter Henriksen) Date: Tue, 5 May 2009 15:05:46 -0700 Subject: How to walk up parent directories? In-Reply-To: <87ocu9k6c4.fsf@benfinney.id.au> References: <87ocu9k6c4.fsf@benfinney.id.au> Message-ID: Be careful not to presume "/a/b/" is the same directory as "/a/b/c/../" I would consider the latter the parent of /a/b/c/, not the former. From tjreedy at udel.edu Tue May 5 18:14:19 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 05 May 2009 18:14:19 -0400 Subject: Google selects Mercurial for DVCS support Message-ID: Given the debate over Python's choice of Mercurial, some might fine the following interesting. http://google-code-updates.blogspot.com/2009/04/mercurial-support-for-project-hosting.html Currently, Mercurial support will be in addition to continued Subversion support. It was also reported on http://arstechnica.com/open-source/news/2009/04/google-code-adds-mercurial-version-control-system.ars with comments. From georgeoliverGO at gmail.com Tue May 5 18:15:37 2009 From: georgeoliverGO at gmail.com (George Oliver) Date: Tue, 5 May 2009 15:15:37 -0700 (PDT) Subject: call function of class instance with no assigned name? References: <447f2963-07b1-40bb-b084-ddf6c9cc880b@c18g2000prh.googlegroups.com> <0c2f0973-1829-46fa-a28c-c38e7906e750@l5g2000vbc.googlegroups.com> Message-ID: <045e683e-5791-4af0-a400-b759064a8046@v23g2000pro.googlegroups.com> Thanks for the suggestions so far. I've taken the advice to keep things simple so currently I'm just creating one instance of the commandHandler and assigning it a name with command = commandHandler (). This makes it easy to call it from any of the other handlers, and I think this will work for what I want the game to do. From deets at nospam.web.de Tue May 5 18:26:30 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Wed, 06 May 2009 00:26:30 +0200 Subject: Error in running python -v on Mac ox 10.5. In-Reply-To: References: Message-ID: <76bsomF1bmc0oU1@mid.uni-berlin.de> silverburgh schrieb: > Hi, > > I run 'python -v' on Macos 10.5 but I get this error : > # can't create /System/Library/Frameworks/Python.framework/Versions/ > 2.5/lib/python2.5/encodings/utf_8.pyc > > Can you please tell me how to fix it? This looks like a rights-problem to me, and a missing pyc-file. If you do sudo python -v does it work? Then, utf_8.pyc might have been created, and subsequent sudo-less inovcations might work. Diez From google at mrabarnett.plus.com Tue May 5 18:33:34 2009 From: google at mrabarnett.plus.com (MRAB) Date: Tue, 05 May 2009 23:33:34 +0100 Subject: Self function In-Reply-To: <6c6fffd7-2f43-454d-8e63-056d0c1c05d3@w31g2000prd.googlegroups.com> References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> <6c6fffd7-2f43-454d-8e63-056d0c1c05d3@w31g2000prd.googlegroups.com> Message-ID: <4A00BEBE.90608@mrabarnett.plus.com> Steve Howell wrote: > On May 4, 11:08 pm, Steven D'Aprano > wrote: >> I propose a small piece of sugar. When a function is entered, Python >> creates an ordinary local name in the function's local namespace, and >> binds the function itself to that name. Two possibilities for the name >> are `this` or `__this__`, analogous to `self` in methods and `__name__` >> in modules. >> >> If there is any support for this, I propose to send the following (long) >> post to python-ideas. Feedback, corrections and suggestions welcome. >> > > I totally support this proposal. I've definitely wasted time in the > past trying to invent my own workarounds for the use cases you > describe. > > Obviously, there will be some bikeshed debates on __this__ vs. > __name__ vs. __func__, etc. I don't have an opinion there, just want > *something* handy for introspection, DRYness, etc. > > A more interesting question is whether __this__ would just always be > there (that's my vote), or if you should have to apply a special > decorator to get it (which I oppose, but I can see some merits). > I'd say that __this__ is a little unclear, so I'd choose __func__. From norseman at hughes.net Tue May 5 19:28:41 2009 From: norseman at hughes.net (norseman) Date: Tue, 05 May 2009 16:28:41 -0700 Subject: Any idea to emulate tail -f In-Reply-To: <1241503205.5374.25.camel@keblar.lapjoe> References: <1241503205.5374.25.camel@keblar.lapjoe> Message-ID: <4A00CBA9.6030402@hughes.net> Joel Juvenal Rivera Rivera wrote: > I want to make something very similar to the command tail -f (follow a > file), i have been trying with some while True and some microsleeps > (about .1 s); did someone has already done something like this? > > And about the file is the apache acceslog of a site with a lot of > traffic. > > Regards > > joe / Joel Rivera > > -- > http://mail.python.org/mailman/listinfo/python-list > ============================================== I read the 4 replies available at this point in time. I agree with them in the general sense. Question 1: Why not use tail -f | my_program.py ?? (The obvious answer is that you are not in a Unix OS) Question 2: Have you looked at Popen2.popen3? (No, you are not in ..) In the Unix environment one can output to log files that are actually named pipes. Program 'up and running' leaves the keyboard (or in this case stdin) open. Kinda like typing something into the letter, go to lunch, come back and type in some more. Self regulating. Might be able to dummy up something that emulates the concept. Question 3: How much CPU time do you need dedicated to this? Running a program again every 0.1seconds uses resources. CRON at x-minute intervals uses less. (bigger the x, less the resources.) Question 4: Is the program looking for things that, if found, need to sound the Claxton (nasty ear ponding alarm noise)? Or is it just to update the monitor over there on the side? (ie.. How time critical is it?) This effects Q3's answer. Question 5: Does the monitor machine just have to be NOT Linux? Does the log file just have to be NOT on a Linux machine? If the Claxton is the point of your program, consider Q5 carefully. If for some reason the log can not reside on a Linux/Unix drive, see if samba type linkage from a Linux to the logfile OS is doable. (It is if the IT types don't get in the way.) From Linux you then mount the samba and use the soft link and run your program accordingly and it works as hinted in questions 1 and 2. Soft link is created by: ln -s /path/real_file /different_path/same_or_different_f-name 2nd is treated as original and reacts same as if it were. I would make one request. Please tell people the OS you want the program on when you ask the question originally. To use yours: "I want to make something very similar to the command tail -f ..." FOR USE IN MS WINDOWS XP. or whatever. XP and VISTA have frustrating differences and neither are UNIX or MAC. I know I haven't given anything like the answers the others provided, but then I really am not sure of the intended use or OS. Fire alarms are one thing, gleaning is another. One is extremely time critical while for the other "tomorrow" is OK. Steve From rhodri at wildebst.demon.co.uk Tue May 5 20:06:35 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Wed, 06 May 2009 01:06:35 +0100 Subject: call function of class instance with no assigned name? In-Reply-To: <447f2963-07b1-40bb-b084-ddf6c9cc880b@c18g2000prh.googlegroups.com> References: <447f2963-07b1-40bb-b084-ddf6c9cc880b@c18g2000prh.googlegroups.com> Message-ID: On Tue, 05 May 2009 16:52:39 +0100, George Oliver wrote: > hi, I'm a Python beginner with a basic question. I'm writing a game > where I have keyboard input handling defined in one class, and command > execution defined in another class. The keyboard handler class > contains a dictionary that maps a key to a command string (like 'h': > 'left') and the command handler class contains functions that do the > commands (like def do_right(self):), > > I create instances of these classes in a list attached to a third, > 'brain' class. What I'd like to have happen is when the player presses > a key, the command string is passed to the command handler, which runs > the function. However I can't figure out a good way to make this > happen. > > I've tried a dictionary mapping command strings to functions in the > command handler class, but those dictionary values are evaluated just > once when the class is instantiated. I can create a dictionary in a > separate function in the command handler (like a do_command function) > but creating what could be a big dictionary for each input seems kind > of silly (unless I'm misunderstanding something there). Taking a wild guess when you were creating that dictionary mapping command strings to functions, did you map to function calls or to the function objects themselves? I'd make a small wager you did the former, and the latter would have done what you want. Something like this, maybe? >>>> CODE FOLLOWS<<<< class DoSomething(object): def do_this(self): print "Doing this" def do_that(self): print "Doing that" def do_the_other(self): print "Doing the other" _CMD_DICT = { 'this' : do_this, 'that' : do_that, 'the other' : do_the_other } def update(self, cmd): try: DoSomething._CMD_DICT[cmd](self) except KeyError: print "Foul! Foul, I say!" >>>> END CODE<<<< To be honest, the getattr approach is probably as easy to follow and less prone to forgetting to update the dictionary. -- Rhodri James *-* Wildebeeste Herder to the Masses From cournape at gmail.com Tue May 5 20:37:21 2009 From: cournape at gmail.com (David Cournapeau) Date: Wed, 6 May 2009 09:37:21 +0900 Subject: Numpy on python 2.7a In-Reply-To: <200905052006.17708.a.cavallo@cavallinux.eu> References: <200905052006.17708.a.cavallo@cavallinux.eu> Message-ID: <5b8d13220905051737g222bdb62y2d7c6c132b9e82bc@mail.gmail.com> On Wed, May 6, 2009 at 4:06 AM, A. Cavallo wrote: > Hi, > > I'm trying to compile numpy using my own pet project based on the python svn > code (that's the reason for the 2.7a tag). It is a python bug, see bugs 5940 and 5941. cheers, David From silverburgh.meryl at gmail.com Tue May 5 20:57:40 2009 From: silverburgh.meryl at gmail.com (silverburgh) Date: Tue, 5 May 2009 17:57:40 -0700 (PDT) Subject: Install Python 3.0 dmg to macos 10.5 Message-ID: <627526f7-ac27-4874-b234-13d1a13accf5@y33g2000prg.googlegroups.com> Hi, If I install python 3.0 dmg, will it wipe out the existing python installation on macos 10.5 (i don't know how the original python was installed ( i think it is version 2.5). Thank you. From steven at REMOVE.THIS.cybersource.com.au Tue May 5 20:58:03 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 06 May 2009 00:58:03 GMT Subject: Self function References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> <50697b2c0905042346u482eabf9j1bb9f95853f38864@mail.gmail.com> <565698f7-d95b-4b99-95a5-5de68d983400@q2g2000vbr.googlegroups.com> Message-ID: On Tue, 05 May 2009 06:30:01 -0700, Arnaud Delobelle wrote: > One issue with automatically binding a local variable to the current > function is with nested functions: > > def foo() > def bar(): > # How do I call foo() from here? As foo(), just like you would call foo() from outside of foo(). __this__ would refer to bar() inside bar(). My proposal isn't meant to solve the general case "how do I call any function anywhere without knowing its name?". I don't think there is a general solution to that problem (although obviously you can do it in special cases). My proposal is sugar for a fairly general problem "how should a function refer to itself?", not an alternative mechanism for referring to functions outside of the usual namespace lookup mechanism. > One solution would be > > def foo() > def bar(foo=__this__): > foo() That would be an option, if the programmer had some reason for wanting to do this. -- Steven From rhodri at wildebst.demon.co.uk Tue May 5 21:35:08 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Wed, 06 May 2009 02:35:08 +0100 Subject: Self function In-Reply-To: <4f13a110-b3e5-4de5-b6d6-0d64da20b61e@r34g2000vbi.googlegroups.com> References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> <1292fea0-96fb-4a21-b39d-b6ce610ea6b2@t10g2000vbg.googlegroups.com> <4f13a110-b3e5-4de5-b6d6-0d64da20b61e@r34g2000vbi.googlegroups.com> Message-ID: On Tue, 05 May 2009 21:43:16 +0100, wrote: > wolfram.hinde...: >> It is easy to change all references of the function name, except for >> those in the function body itself? That needs some explantation. > > I can answer this. If I have a recursive function, I may want to > create a similar function, so I copy and paste it, to later modify the > copied version. Then to change its name I usually don't use a search & > rename of its name into its block of code because it's usually > useless. In non-recursive functions the name of the function is stated > only once, at the top. I'm sorry, but while I'm mildly positive towards the proposal (and more so towards Aaron's decorator), I don't buy this argument at all. What is broken about your editor's global search-and-replace function that makes it "usually useless" for making these name changes? -- Rhodri James *-* Wildebeeste Herder to the Masses From revathi5587 at gmail.com Tue May 5 21:36:56 2009 From: revathi5587 at gmail.com (revathy) Date: Tue, 5 May 2009 18:36:56 -0700 (PDT) Subject: work at online earn 50$ daily Message-ID: click the following link ========================================== http://www.s1.webstarts.com/physicsengines/ =========================================== From steven at REMOVE.THIS.cybersource.com.au Tue May 5 21:52:24 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 06 May 2009 01:52:24 GMT Subject: list comprehension question References: <6d502cfd-e252-4722-96a5-c52fc3579499@v1g2000prd.googlegroups.com> <50697b2c0904301805p7158e167n7f84b18afd0f55f3@mail.gmail.com> <49FB2225.4030601@councilforeconed.org> <49FB2AA7.6020403@snet.net> Message-ID: On Tue, 05 May 2009 13:43:32 -0400, J. Cliff Dyer wrote: > On Fri, 2009-05-01 at 13:00 -0400, John Posner wrote: >> Shane Geiger wrote: >> > if type(el) == list or type(el) is tuple: >> A tiny improvement: >> >> if type(el) in (list, tuple): >> >> > Another alternative, which might be useful in some cases: > > if hasattr(el, '__iter__'): > > This covers all iterables, not just lists and tuples. Except for the ones that it doesn't cover, like strings: >>> hasattr('abcd', '__iter__') False >>> list(iter('abcd')) ['a', 'b', 'c', 'd'] And classes that follow the sequence protocol: >>> class Spam: ... def __getitem__(self, index): ... if 0 <= index < 5: ... return "spam" ... raise IndexError ... >>> hasattr(Spam(), '__iter__') False >>> list(iter(Spam())) ['spam', 'spam', 'spam', 'spam', 'spam'] -- Steven From gagsl-py2 at yahoo.com.ar Tue May 5 22:24:32 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Tue, 05 May 2009 23:24:32 -0300 Subject: Problem with case insensitive volumes References: <26c57bf5-5ad2-4838-8097-c8d51cf1ccae@n8g2000vbb.googlegroups.com> Message-ID: En Tue, 05 May 2009 12:55:13 -0300, spillz escribi?: > > /my/path/to/usbkey/and/file > > everything up to "/my/path/to/usbkey" is case sensitive. only the "and/ > file" is case insensitive. but my list of stored paths might include > all images under "/my". thus > > /my/path/to/usbkey/and/file1==/my/path/to/usbkey/and/FILE1 > /my/path/to/usbkey/and/file1==/my/path/to/usbkey/AND/FILE1 > but > /my/path/to/usbkey/and/file1!=/my/path/TO/usbkey/AND/FILE1 > > so to do this right I'd need to establish which parts of the path are > case insensitive. being able to retrieve the actual path from an > equivalent representation would be so much simpler. I use this function on Windows to obtain the true case name of a file: from win32api import FindFiles from win32file import FILE_ATTRIBUTE_DIRECTORY from os.path import normpath, split, join def truecase(fullpath, _cache={}): """ Normalize fullpath, returning names using the same case as actually used in the filesystem. """ fullpath = normpath(fullpath) dirname, filename = split(fullpath) dirname = dirname.lower() if not filename: # drive letter result = _cache[dirname] = dirname.upper() return result if dirname in _cache: dirname = _cache[dirname] elif dirname: dirname = truecase(dirname) finddata = FindFiles(fullpath) if not finddata: import errno, os raise IOError(errno.ENOENT, os.strerror(errno.ENOENT), fullpath) if len(finddata)!=1: raise ValueError("Too many results for %r" % fullpath) attr = finddata[0][0] filename = finddata[0][8] result = join(dirname, filename) if attr & FILE_ATTRIBUTE_DIRECTORY: _cache[result.lower()] = result return result truecase("c:")=="C:" truecase("C:\\")=="C:\\" truecase("c:\\")=="C:\\" truecase("C:\\TeMp")=="C:\\TEMP" truecase("C:\\TeMp\\")=="C:\\TEMP\\" truecase(r"C:\Documents and Settings\gabriel")==r"C:\Documents and Settings\gabriel" truecase(r"c:\dOcUmEnTs aNd SeTtInGs\GaBrIeL\mIS DOCUMENTOS\105_3 - nist - sPECIFICATIONS vOLUMETRIC fIELD sTANDARD.PDF")==r"C:\Documents and Settings\gabriel\Mis documentos\105_3 - NIST - Specifications Volumetric Field Standard.pdf" truecase(r"E:\prog\test\compface\Debug\No existe.txt") -- Gabriel Genellina From zac256 at gmail.com Tue May 5 22:52:25 2009 From: zac256 at gmail.com (Zac Burns) Date: Tue, 5 May 2009 19:52:25 -0700 Subject: Cannot start a thread in atexit callback Message-ID: <333edbe80905051952v56ced17cl5aaa4b04bb8d3120@mail.gmail.com> It seems that one cannot start a thread in an atexit callback. My use case is that I have a IO heavy callback that I want to run in a thread so that other callbacks can finish while it's doing it's thing to save on exit time. Example code (py3k) ############################################ import threading import time import atexit def print_(): ??? print(1) ??? time.sleep(1) ??? print(2) def test(): ??? threading.Thread(target=print_).start() atexit.register(test) ############################################ -- Zachary Burns (407)590-4814 Aim - Zac256FL Production Engineer (Digital Overlord) Zindagi Games From davea at ieee.org Tue May 5 22:55:53 2009 From: davea at ieee.org (Dave Angel) Date: Tue, 05 May 2009 22:55:53 -0400 Subject: call function of class instance with no assigned name? In-Reply-To: References: <447f2963-07b1-40bb-b084-ddf6c9cc880b@c18g2000prh.googlegroups.com> Message-ID: <4A00FC39.4010107@ieee.org> George Oliver wrote: > On May 5, 11:59 am, Dave Angel wrote: > > >> 1) forget about getattr() unless you have hundreds of methods in your >> map. The real question is why you need two maps. What good is the >> "command string" doing you? Why not just map the keyvalues directly >> into function objects? >> > > > Thanks for the reply Dave. I understand your example and it's what I > originally used. Here is more detail on what I'm doing now, I hope > this will explain my question better. > > In the game I'm writing the player, monsters, items and so on are > instances of class Thing, like: > > class Thing(object): > def __init__(self, x, y, name): > self.x, self.y = x, y > self.name = name > self.brain = None > > Some Things have an instance of class Brain attached. The Brain > instance has a list of handlers, like: > > class Brain(object): > def __init__(self): > self.handlers = [] > > A handler class defines some functionality for the Brain. Each Brain > has an update method like this: > > def update(self, arguments): > for handler in self.handlers: > handler.update(arguments) > > So on each pass through the main game loop, it calls the update method > of all the brains in the game, which run their handler update methods > to change the state of the game. > > A handler would be something like a key input handler. The key input > handler is defined separately from the command handler in the case I > want to use a different method of input, for example a mouse or > joystick. > > In the dictionary of key inputs I could map each input directly to a > function. However there is no instance name I can call the function > on, as I create a thing, add a brain, and add handlers to the brain > like this: > > player = Thing(26, 16, 'player') > player.brain = Brain() > player.brain.add_handlers( > commandHandler(player.brain, player), > keyboardHandler(player.brain), > fovHandler(player.brain)) > > > So what I'm wondering is how to reference the instance in each brain's > list of handlers when I want to map something like a key input to a > command, or what a better way might be to structure the code. > > > >> >>player.brain.add_handlers( >> commandHandler(player.brain, player), >> keyboardHandler(player.brain), >> fovHandler(player.brain)) You're executing commandHandler, and passing its return value into the add_handlers() method. So commandHandler is a function, not a method, and what is its relationship to anything that actually processes commands? Sorry, your pseudo-code is so far from real code that I can't figure out what you're doing. So I guess I can't be any help till something else turns up to make it clearer. Maybe it's just me. From musicguy at alphaios.net Tue May 5 23:14:06 2009 From: musicguy at alphaios.net (The Music Guy) Date: Tue, 5 May 2009 22:14:06 -0500 Subject: FLV download script works, but I want to enhance it Message-ID: I've written a script that can scan any number of web pages for urls to .flv (Flash Video) files and then download them using urllib2. It's kind of like a YouTube download utility, but more general-purpose (and it doesn't actually work with YouTube. :P). The script works fine; it even has support for outputting URLs of files it's found to stdout for chaining to tools like wget. After I download the files, I usually want to convert them to another video format using command line tools, and I usually convert each one in a separate terminal since that way they can all be converted at the same time rather than one-by-one. Oddly enough, converting tends to take longer than downloading, but converting multiple files at once is faster than converting them one-by-one. What I want to do is add an option to my script that will make it begin converting each video using my command line tool after each is downloaded, while at the same time continuing to download other files in the background. I know this is possible using threading or subprocesses, but I need help figuring out how to do it. Another thing I want to do is allow the script to use wget for downloading instead of urllib2. The script is below. I've already added the options for the new features, but they don't do anything yet. #!/usr/bin/env python import urllib import re import sys import optparse HELP = """ %prog [-p] url1, url2, url3, ... Flash Video Snagger (FLVSnag) Tool for downloading files referenced in the source code of some video-displaying websites. Simply pass a website URL as the first argument and the HTML will be scanned for URLs to .flv files and they will be downloaded. Note that all given pages are scanned before the first video is downloaded. FIXME: Downloads may not occur in the order that they are found. - The Music Guy, 5/5/09 musicguy at alphaios.net """ def main(): # Create the option parser and parse the options. parser = optparse.OptionParser(usage=HELP) parser.add_option( '-p', '--print', action = 'store_true', default = False, help = "Causes the URLs to be printed to stdout (one URL per line) " "without actually downloading them. Good for piping." ) parser.add_option( '-d', '--dpg', action = 'store_true', default = False, help = "TODO: Converts each downloaded file to DPG if dpgconv.py is available " "from the PATH." ) parser.add_option( '-w', '--wget', action = 'store_true', default = False, help = "TODO: Downloads each file using wget instead of using internal " "downloader. Only works if wget is available from the PATH." ) ops,args = parser.parse_args() if not args: parser.print_usage() return # Create a pattern that matches embedded .flv file URLs. urlre = re.compile("http\:\/\/(? <7f3493ca-efcb-48cc-aa29-487bb11fcd09@p4g2000vba.googlegroups.com> <16d47b2f-14d4-4266-84d2-c26501beb0c1@r34g2000vbi.googlegroups.com> <1691f042-bc14-46b9-b7b3-5a44348e9965@b1g2000vbc.googlegroups.com> <1a497471-498a-43d8-90ba-d676e8acf68b@r36g2000vbr.googlegroups.com> <8b49b775-66a7-42d5-9b00-cf23d9b044cc@e23g2000vbe.googlegroups.com> <54d76f11-c255-487d-9c3a-aaa380d7827a@z7g2000vbh.googlegroups.com> <991ccae3-f3e2-4d73-bd32-bc6f560e6f5f@n8g2000vbb.googlegroups.com> Message-ID: <3d167fe3-da37-4d1e-a43a-832819a0e405@h23g2000vbc.googlegroups.com> On May 5, 10:49?am, Ross wrote: > I'm interested to see what you did. From your description, > it sounds like I've tried what you've done, but when I > implemented my version, it took minutes to evaluate for > bigger numbers. If that isn't the case with yours, I'd be > interested in seeing your implementation. I don't know how big your "bigger numbers" are, but this should run in a reasonable time. It helps if you are using Python 2.6. If not, you should go on-line to the itertools docs to get the pure-Python equivalent for itertools.combinations. Since no matches are "thrown away", the byes are expressed simply as a list of players that have to sit out for the week. A couple of the lines are on the longish side, so beware of wrapping from the browser/newsreader. # Let's just try choosing from the possible combinations, each week # scheduling those who have played the least or waited the longest. import itertools class Player(object): def __init__(self, id): self.id = id self.played = [] self.latency = 0 def __str__(self): str(self.id) def __cmp__(self, other): if len(self.played) != len(other.played): return len(self.played) - len(other.played) return other.latency - self.latency # select longer latency first def flattened(listOfLists): return list(itertools.chain(*listOfLists)) def pairings(players): idlist = range(players) playerlist = [Player(i) for i in idlist] matchlist = list(itertools.combinations(idlist, 2)) while matchlist: found = False playerlist.sort() for i in range(players - 1): for j in range(i + 1, players): candidate = tuple(sorted((playerlist[i].id, playerlist [j].id))) if candidate in matchlist: yield matchlist.pop(matchlist.index(candidate)) for p in playerlist: if p.id == candidate[0]: p.played.append(candidate[1]) p.latency = 0 elif p.id == candidate[1]: p.played.append(candidate[0]) p.latency = 0 else: p.latency += 1 found = True break if found: break def schedule(players, weeks, courts): playerlist = range(players) matchlist = list(pairings(players)) cap = min(courts, players // 2) start = 0 for week in range(weeks): matches = matchlist[start:start + cap] byes = set(playerlist) - set(flattened(matches)) print matches, list(byes) start += cap From namekuseijin at gmail.com Tue May 5 23:17:28 2009 From: namekuseijin at gmail.com (namekuseijin) Date: Wed, 6 May 2009 00:17:28 -0300 Subject: yet another list comprehension question In-Reply-To: <4A005757.7050405@gmail.com> References: <594765c1-e5ed-492d-b398-a8ad00c6d6ad@x6g2000vbg.googlegroups.com> <4A005757.7050405@gmail.com> Message-ID: <67d0527e0905052017v7ede4c6akd87e41d77bd8ebcd@mail.gmail.com> 2009/5/5 Ricardo Ar?oz : > This seems to work for any length tuples : > >>>> a = [(1,2), (3,4, 'goes'), (5,None), (6,7, 8, 'as', None), (8, None), >>>> (9, 0)] >>>> [tup for tup in a if not [e for e in tup if e == None]] > [(1, 2), (3, 4, 'goes'), (9, 0)] Why that extra "for"? KISS >>> a = [(1,2), (3,4, 'goes'), (5,None), (6,7, 8, 'as', None), (8, None), (9, 0)] [(1, 2), (3, 4, 'goes'), (5, None), (6, 7, 8, 'as', None), (8, None), (9, 0)] >>> [t for t in a if None not in t] [(1, 2), (3, 4, 'goes'), (9, 0)] "in" works perfectly well for any sequence, including strings. From wuwei23 at gmail.com Tue May 5 23:36:37 2009 From: wuwei23 at gmail.com (alex23) Date: Tue, 5 May 2009 20:36:37 -0700 (PDT) Subject: list comprehension question References: <6d502cfd-e252-4722-96a5-c52fc3579499@v1g2000prd.googlegroups.com> <85ocucnbce.fsf@agentultra.com> <85ljpb8r1o.fsf@agentultra.com> Message-ID: On May 6, 4:01?am, "J. Cliff Dyer" wrote: > The way you have to bounce your eyes back and forth in the comprehension > makes it hard to read the logic. ?With the loop, on the other hand it is > blatantly obvious which way the nesting occurs. > > >>>> [ item for j in a if len(j)==2 for item in j if item % 2 ] > > > ...opposed to... > > >>>> for j in a: > > > ... ? ? if len(j)==2: > > > ... ? ? ? ? for item in j: > > > ... ? ? ? ? ? ? if item % 2: > > > ... ? ? ? ? ? ? ? ? b.append(item) > > > ... > Much nicer. ?Thank you. Apart from the presence of 'item' at the beginning of the list comprehension as opposed to 'b.append(item)' at the end of the for- loop, how exactly does the listcomp force you to "bounce [..] back and forth" to follow the logic? The only other difference between the two is in the layout - the indentation & colons - otherwise they're structurally identical. From ross.jett at gmail.com Tue May 5 23:37:37 2009 From: ross.jett at gmail.com (Ross) Date: Tue, 5 May 2009 20:37:37 -0700 (PDT) Subject: Code works fine except... References: <8d4ec1df-dddb-469a-99a1-695152db7c04@n4g2000vba.googlegroups.com> <7f3493ca-efcb-48cc-aa29-487bb11fcd09@p4g2000vba.googlegroups.com> <16d47b2f-14d4-4266-84d2-c26501beb0c1@r34g2000vbi.googlegroups.com> <1691f042-bc14-46b9-b7b3-5a44348e9965@b1g2000vbc.googlegroups.com> <1a497471-498a-43d8-90ba-d676e8acf68b@r36g2000vbr.googlegroups.com> <8b49b775-66a7-42d5-9b00-cf23d9b044cc@e23g2000vbe.googlegroups.com> <54d76f11-c255-487d-9c3a-aaa380d7827a@z7g2000vbh.googlegroups.com> <991ccae3-f3e2-4d73-bd32-bc6f560e6f5f@n8g2000vbb.googlegroups.com> Message-ID: On May 5, 10:33?am, MRAB wrote: > Ross wrote: > > On May 5, 12:32 am, John Yeung wrote: > >> On May 5, 1:12 am, John Yeung wrote: > > >>> [...] the problem may require bigger guns (either much better > >>> math or much more sophisticated programming). > >> Yes, I'm responding to myself. > > >> Well, I went ahead with the approach I mentioned earlier, generating > >> all possible matches and then selecting among them as needed to fill > >> up the courts, trying to keep the number of matches played by each > >> player as fair as possible. ?(I should mention that this, or something > >> similar, was suggested earlier by someone else in a different thread, > >> in response to the same question by the same OP.) > > >> I did use "bigger guns" (mainly a class for player objects, with > >> custom __cmp__ method), but still didn't do anything with doubles. > > >> I haven't tested it much, but I'll post it if anyone's interested. > >> (That way people can pick on me instead of the OP. ;) > > >> John > > > I'm interested to see what you did. From your description, it sounds > > like I've tried what you've done, but when I implemented my version, > > it took minutes to evaluate for bigger numbers. If that isn't the case > > with yours, I'd be interested in seeing your implementation. > > Here's my approach (incomplete): > > def get_pair(player_list, played): > ? ? ?for first in range(len(player_list)): > ? ? ? ? ?player_1 = player_list[first] > ? ? ? ? ?for second in range(first + 1, len(player_list)): > ? ? ? ? ? ? ?player_2 = player_list[second] > ? ? ? ? ? ? ?pair = player_1, player_2 > ? ? ? ? ? ? ?sorted_pair = tuple(sorted(pair)) > ? ? ? ? ? ? ?if sorted_pair not in played: > ? ? ? ? ? ? ? ? ?played.add(sorted_pair) > ? ? ? ? ? ? ? ? ?del player_list[second] > ? ? ? ? ? ? ? ? ?del player_list[first] > ? ? ? ? ? ? ? ? ?return pair > ? ? ?return None > > def round_robin(player_list, courts, played): > ? ? ?playing = [] > ? ? ?for c in range(courts): > ? ? ? ? ?pair = get_pair(player_list, played) > ? ? ? ? ?if pair is None: > ? ? ? ? ? ? ?break > ? ? ? ? ?playing.append(pair) > ? ? ?byes = player_list[:] > ? ? ?player_list[:] = byes + [player for pair in playing for player in pair] > ? ? ?yield playing, byes > > def test_round_robin(players, rounds, courts, doubles=False): > ? ? ?player_list = range(players) > ? ? ?played = set() > ? ? ?for r in range(rounds): > ? ? ? ? ?for playing, byes in round_robin(player_list, courts, played): > ? ? ? ? ? ? ?print playing, byes FYI... I was testing your code further and discovered a strange outcome... when there are 16 people for 7 courts, every 7th round your code produces 4 byes instead of the correct 2 byes. From lrhazi at gmail.com Tue May 5 23:43:25 2009 From: lrhazi at gmail.com (Mohamed Lrhazi) Date: Tue, 5 May 2009 23:43:25 -0400 Subject: pyc files problem Message-ID: Hello, My code sends a pointer to a Python function to a C library, using ctypes module. When my program restarts, after a crash or normal exit... it cannot start again without sigfaulting What is the proper way around this problem? other than adding code to delete the cache files? Thanks a lot, Mohamed. From campanha1 at dnsreverso.com Tue May 5 23:46:12 2009 From: campanha1 at dnsreverso.com (Cursos) Date: Wed, 6 May 2009 03:46:12 GMT Subject: Curso de Excel Message-ID: <20090506034824.5D8B91FF21C5@out19.dnsreverso.com> An HTML attachment was scrubbed... URL: From gagsl-py2 at yahoo.com.ar Tue May 5 23:59:59 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Wed, 06 May 2009 00:59:59 -0300 Subject: Self function References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> <1292fea0-96fb-4a21-b39d-b6ce610ea6b2@t10g2000vbg.googlegroups.com> <4f13a110-b3e5-4de5-b6d6-0d64da20b61e@r34g2000vbi.googlegroups.com> Message-ID: En Tue, 05 May 2009 22:35:08 -0300, Rhodri James escribi?: > On Tue, 05 May 2009 21:43:16 +0100, wrote: >> wolfram.hinde...: >>> It is easy to change all references of the function name, except for >>> those in the function body itself? That needs some explantation. >> >> I can answer this. If I have a recursive function, I may want to >> create a similar function, so I copy and paste it, to later modify the >> copied version. Then to change its name I usually don't use a search & >> rename of its name into its block of code because it's usually >> useless. In non-recursive functions the name of the function is stated >> only once, at the top. > > I'm sorry, but while I'm mildly positive towards the proposal (and more > so towards Aaron's decorator), I don't buy this argument at all. What > is broken about your editor's global search-and-replace function that > makes it "usually useless" for making these name changes? It happened to me sometimes. If a module defines some functions, and it doesn't *use* them, why should I use a global search-and-replace to rename something? Modifying the "def" line should be enough - unless the function happens to be recursive. It's the DRY principle in action, the same argument as when decorators where introduced: there should be no need to repeat the function name again and again. -- Gabriel Genellina From steven at REMOVE.THIS.cybersource.com.au Wed May 6 00:10:32 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 06 May 2009 04:10:32 GMT Subject: list comprehension question References: <6d502cfd-e252-4722-96a5-c52fc3579499@v1g2000prd.googlegroups.com> <85ocucnbce.fsf@agentultra.com> <85ljpb8r1o.fsf@agentultra.com> Message-ID: On Tue, 05 May 2009 20:36:37 -0700, alex23 wrote: > On May 6, 4:01?am, "J. Cliff Dyer" wrote: >> The way you have to bounce your eyes back and forth in the >> comprehension makes it hard to read the logic. ?With the loop, on the >> other hand it is blatantly obvious which way the nesting occurs. > >> > >>>> [ item for j in a if len(j)==2 for item in j if item % 2 ] >> > > ...opposed to... >> > >>>> for j in a: >> > > ... ? ? if len(j)==2: >> > > ... ? ? ? ? for item in j: >> > > ... ? ? ? ? ? ? if item % 2: >> > > ... ? ? ? ? ? ? ? ? b.append(item) ... > >> Much nicer. ?Thank you. > > Apart from the presence of 'item' at the beginning of the list > comprehension as opposed to 'b.append(item)' at the end of the for- > loop, how exactly does the listcomp force you to "bounce [..] back and > forth" to follow the logic? The only other difference between the two is > in the layout - the indentation & colons - otherwise they're > structurally identical. It's precisely the indentation and colons (plus newlines) that makes nested for-loops easier to read than list-comps with multiple fors. for a in alist: for b in blist: for c in clist: if c: parrot(a, b, c) is much easier to read than the equivalent one-liner: [parrot(a, b, c) for a in alist for b in blist for c in clist if c] You can get back *nearly* all the readability by splitting the list comp into multiple lines: [parrot(a, b, c) for a in alist for b in blist for c in clist if c ] -- Steven From steven at REMOVE.THIS.cybersource.com.au Wed May 6 00:18:03 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 06 May 2009 04:18:03 GMT Subject: pyc files problem References: Message-ID: On Tue, 05 May 2009 23:43:25 -0400, Mohamed Lrhazi wrote: > Hello, > > My code sends a pointer to a Python function to a C library, using > ctypes module. When my program restarts, after a crash or normal exit... > it cannot start again without sigfaulting > > What is the proper way around this problem? other than adding code to > delete the cache files? Surely the proper way to solve this is to find the cause of the segfault, and fix that bug. -- Steven From gallium.arsenide at gmail.com Wed May 6 00:36:12 2009 From: gallium.arsenide at gmail.com (John Yeung) Date: Tue, 5 May 2009 21:36:12 -0700 (PDT) Subject: Code works fine except... References: <8d4ec1df-dddb-469a-99a1-695152db7c04@n4g2000vba.googlegroups.com> <7f3493ca-efcb-48cc-aa29-487bb11fcd09@p4g2000vba.googlegroups.com> <16d47b2f-14d4-4266-84d2-c26501beb0c1@r34g2000vbi.googlegroups.com> <1691f042-bc14-46b9-b7b3-5a44348e9965@b1g2000vbc.googlegroups.com> <1a497471-498a-43d8-90ba-d676e8acf68b@r36g2000vbr.googlegroups.com> <8b49b775-66a7-42d5-9b00-cf23d9b044cc@e23g2000vbe.googlegroups.com> <54d76f11-c255-487d-9c3a-aaa380d7827a@z7g2000vbh.googlegroups.com> <991ccae3-f3e2-4d73-bd32-bc6f560e6f5f@n8g2000vbb.googlegroups.com> Message-ID: On May 5, 11:37?pm, Ross wrote: > On May 5, 10:33?am, MRAB wrote: > > > Here's my approach (incomplete): > > FYI... I was testing your code further and discovered a strange > outcome... when there are 16 people for 7 courts, every 7th > round your code produces 4 byes instead of the correct 2 byes. Well, MRAB did say his was incomplete, and it's significantly shorter and cleaner than mine. Mine has even weirder-looking behavior at 16 players on 7 courts, but I think it's because mine tries harder to keep things balanced. After a few rounds, the inequalities start to build up, and my routine picks some people more often to "catch up", but it doesn't prevent the same person from being scheduled for multiple matches the same week. There may also be other, more subtle ways mine is broken. It also may be that the problem is inherently unsolvable for some values. (I'm still waiting for someone with sufficient math ability to swoop in and either provide a way that works for all values, or confirm that there are just some unavoidable pathological cases.) John From tjreedy at udel.edu Wed May 6 00:42:34 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 06 May 2009 00:42:34 -0400 Subject: list comprehension question In-Reply-To: References: <6d502cfd-e252-4722-96a5-c52fc3579499@v1g2000prd.googlegroups.com> <50697b2c0904301805p7158e167n7f84b18afd0f55f3@mail.gmail.com> <49FB2225.4030601@councilforeconed.org> <49FB2AA7.6020403@snet.net> Message-ID: Steven D'Aprano wrote: > On Tue, 05 May 2009 13:43:32 -0400, J. Cliff Dyer wrote: > Except for the ones that it doesn't cover, like strings: > >>>> hasattr('abcd', '__iter__') > False True in Python3 From gagsl-py2 at yahoo.com.ar Wed May 6 00:58:08 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Wed, 06 May 2009 01:58:08 -0300 Subject: pyc files problem References: Message-ID: En Wed, 06 May 2009 00:43:25 -0300, Mohamed Lrhazi escribi?: > My code sends a pointer to a Python function to a C library, using > ctypes module. When my program restarts, after a crash or normal > exit... it cannot start again without sigfaulting Do you mean that, if you delete the .pyc files your program runs properly, but if you keep the .pyc files your program crashes? That's very strange... > What is the proper way around this problem? other than adding code to > delete the cache files? There is no "proper way around" the problem; it should not exist in the first place! Have you read the note at the end of the "Callback" section in the ctypes documentation? """Important note for callback functions: Make sure you keep references to CFUNCTYPE objects as long as they are used from C code. ctypes doesn't, and if you don't, they may be garbage collected, crashing your program when a callback is made.""" There is a ctypes-specific list: https://lists.sourceforge.net/lists/listinfo/ctypes-users -- Gabriel Genellina From ganesh.gbg at gmail.com Wed May 6 01:17:35 2009 From: ganesh.gbg at gmail.com (gganesh) Date: Tue, 5 May 2009 22:17:35 -0700 (PDT) Subject: problem in using sendmail in multi thread References: <343747e9-549f-4336-9b15-522411a785a3@x1g2000prh.googlegroups.com> Message-ID: On May 5, 9:25?pm, Piet van Oostrum wrote: > >>>>> gganesh (g) wrote: > >g> hi, > >g> I'm a beginner in using Python script > >g> I'm trying to send mails using multi-thread > >g> I wrote > >g> FROM = 'ganeshx... at xxxx.com' > >g> # for more mail add';' the another mail id > >g> listTo = ['g.... at gmail.com', 'xxjango... at gmail.com', > >g> 'xx... at yahoo.co.in'] > >g> SUBJECT = 'This is the subject' > >g> MSGBODY = 'This the body of the message ' > >g> MAILSERVER = 'mail.xxxx.com' > >g> port = 25 > >g> username = 'xxxxx' > >g> password = 'xxxxx' > >g> # trim the strings of any leading or trailing spaces > >g> FROM = FROM.strip() > >g> SUBJECT = SUBJECT.strip() > >g> MSGBODY = MSGBODY.strip() > >g> MAILSERVER = MAILSERVER.strip() > >g> username = username.strip() > >g> password = password.strip() > >g> #Connect to server > >g> print 'Connecting to mail server ', MAILSERVER > >g> try: > >g> ? ? ? s = smtplib.SMTP(MAILSERVER,port) > > You can't have a single SMTP connection that's used in multiple threads. > That is what causes the error. Each thread should have its own SMTP > connection. So move this code (above and below) into the run() method. > > >g> ? ? ? print 'connected' > >g> #s.set_debuglevel(1) > >g> except: > >g> ? ? ? ?print 'ERROR: Unable to connect to mail server', sys.exc_info ?()[0] > >g> ? ? ? ?sys.exit(1) > >g> #login to server > >g> if password <> '': > >g> ? ? ? print 'Logging into mail server' > > I think this try except block should be inside the if statement, i.e. > indented 4 spaces. > > >g> try: > >g> ? ? ? s.login(username,password) > >g> except: > >g> ? ? ? print 'ERROR: Unable to login to mail server', MAILSERVER > >g> ? ? ? print 'Please recheck your password' > >g> ? ? ? sys.exit(1) > >g> #-------------------------------------------------- > >g> print "Starting Multi Thread Method" > >g> class MyThread(Thread): > >g> ? ? def __init__(self, site, s, FROM, MSGBODY): > >g> ? ? ? ? Thread.__init__(self) > >g> ? ? ? ? self.site = site > >g> ? ? ? self.s=s > >g> ? ? ? self.FROM=FROM > >g> ? ? ? self.MSGBODY=MSGBODY > > You give the s (connection) here as a parameter, store it in self.s and > never use that attribute. > > >g> ? ? def run(self): > >g> ? ? ? print "running for %s " %self.site > >g> ? ? ? s.sendmail(self.FROM, self.site, self.MSGBODY) > > Here you use the global s, not self.s > > As I said above you should do the SMTP connection setup, including the > login, here, and store the connection either in self.s or in a local > variable s in the method. As you don't use the s in another method, I > think a local variable is better. > > >g> ? ? ? print "Emailed for ?site %s" %self.site > >g> a= time.time() > >g> threads = [] > >g> for site in listTo: > >g> ? ? T = MyThread(site,s,FROM,MSGBODY) > >g> ? ? threads.append(T) > >g> ? ? T.start() > >g> for i in threads: > >g> ? ? i.join() > > Of course the next 2 lines should also be moved to run(). > > >g> s.quit() > >g> s.close() > >g> print "Took %s seconds" %str(time.time()-a) > >g> #----------------------------------------------------- > > -- > Piet van Oostrum > URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4] > Private email: p... at vanoostrum.org Thanks Everyone By your guidance the code worked fine I can send mails in multi threaded environment. Is this only way to send mails concurrently or any other method aviable? regards Ganesh From gagsl-py2 at yahoo.com.ar Wed May 6 01:18:41 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Wed, 06 May 2009 02:18:41 -0300 Subject: Cannot start a thread in atexit callback References: <333edbe80905051952v56ced17cl5aaa4b04bb8d3120@mail.gmail.com> Message-ID: En Tue, 05 May 2009 23:52:25 -0300, Zac Burns escribi?: > It seems that one cannot start a thread in an atexit callback. > > My use case is that I have a IO heavy callback that I want to run in a > thread so that other callbacks can finish while it's doing it's thing > to save on exit time. Try creating the thread when the program begins, but locked. And release the lock when your programs is about to finish. -- Gabriel Genellina From research at johnohagan.com Wed May 6 01:56:35 2009 From: research at johnohagan.com (John O'Hagan) Date: Wed, 6 May 2009 05:56:35 +0000 Subject: object query assigned variable name? Message-ID: <200905060556.35190.research@johnohagan.com> On Tue, 5 May 2009, Sion Arrowsmith wrote: > John O'Hagan wrote: > >I can see that it's tantalizing, though, because _somebody_ must know > > about the assignment; after all, we just executed it! > > Except we haven't, if we're talking about reporting from the > > object's __init__: > >>> class Brian: > > ... def __init__(self): > ... print "I'm Brian!" > ... > > >>> l = [] > >>> l[1] = Brian() > > I'm Brian! > Traceback (most recent call last): > File "", line 1, in > IndexError: list assignment index out of range > > (Yeah, I know that's a setitem call not an assignment. Point stands. > It also demonstrates why the whole idea of "what name is a newly- > created object assigned to" is broken.) I guess what I meant was that if I type: brian = Brian() in the python shell and then hit return, it seems to me that _somewhere_ (in the interpreter? I have no idea how it's done) it must be written that the new Brian object will later be assigned the name "brian", even as the process of creating the instance begins. As you've just demonstrated, the actual assignment occurs afterwards. But even if this is true I'm guessing it'd be black magic to get to it. Regards, John From gallium.arsenide at gmail.com Wed May 6 02:10:34 2009 From: gallium.arsenide at gmail.com (John Yeung) Date: Tue, 5 May 2009 23:10:34 -0700 (PDT) Subject: list comprehension question References: <6d502cfd-e252-4722-96a5-c52fc3579499@v1g2000prd.googlegroups.com> <85ocucnbce.fsf@agentultra.com> <85ljpb8r1o.fsf@agentultra.com> Message-ID: <9086e914-3875-491c-8c88-ae697157b40f@o27g2000vbd.googlegroups.com> On May 5, 11:36?pm, alex23 wrote: > Apart from the presence of 'item' at the beginning of the > list comprehension as opposed to 'b.append(item)' at the > end of the for-loop, how exactly does the listcomp force > you to "bounce [..] back and forth" to follow the logic? It's precisely the fact that the item is at the beginning which is the main instigator of the bounce. To tranlate [f(x) for x in a] into a loop, you start in the middle, work your way toward the right, then bounce back to the left. I think some of us, perhaps subconsciously, get used to reading that small chunk on the right, then bouncing left. See enough small, flat LCs, and we may be conditioned to think that the general rule is to start with that small chunk on the right and bounce left. When confronted with a long, nested LC then, some of us have the instinct to read the rightmost chunk, then bounce left to the next- rightmost chunk, etc. Essentially, if you see [B A] over and over and over again, when finally confronted with LCs of more "elements", it's not immediately clear that the pattern of increasing nestedness is [B A] => [C A B] => [D A B C] => etc. rather than [B A] => [C B A] => [D C B A] => etc. Maybe we're wired wrong, but there are many of us who find the second pattern more logical, which is why it is so easy for us to mess up the order of the nesting in a more complex LC. John From wuwei23 at gmail.com Wed May 6 03:20:29 2009 From: wuwei23 at gmail.com (alex23) Date: Wed, 6 May 2009 00:20:29 -0700 (PDT) Subject: list comprehension question References: <6d502cfd-e252-4722-96a5-c52fc3579499@v1g2000prd.googlegroups.com> <85ocucnbce.fsf@agentultra.com> <85ljpb8r1o.fsf@agentultra.com> Message-ID: <75f5e3c1-1c0b-4278-bf43-172676e7903b@s1g2000prd.googlegroups.com> On May 6, 2:10?pm, Steven D'Aprano wrote: > It's precisely the indentation and colons (plus newlines) that makes > nested for-loops easier to read than list-comps with multiple fors. > > You can get back *nearly* all the readability by splitting the list comp > into multiple lines: It was less the overall readability I was commenting on, and more the claim that the listcomp required a 'back-and-forth' parsing to understand. As you've reinforced, the listcomp can be readily converted back to the multiple-line form by the inclusion of colons & EOL markers, which means you can make as much sequential sense from a listcomp as you can a for-loop. From arnodel at googlemail.com Wed May 6 03:23:42 2009 From: arnodel at googlemail.com (Arnaud Delobelle) Date: Wed, 6 May 2009 00:23:42 -0700 (PDT) Subject: Self function References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> <6c6fffd7-2f43-454d-8e63-056d0c1c05d3@w31g2000prd.googlegroups.com> <4a69c949-ace7-47d9-8bb7-d05bc6c895f3@s31g2000vbp.googlegroups.com> <50222a39-88d0-49da-9105-2d3a47af5940@r34g2000vba.googlegroups.com> <5b81cccc-c174-42bd-b4a0-638755ca9e03@l5g2000vbc.googlegroups.com> Message-ID: <57b22b7c-1130-4f28-a7b7-bd119445de41@z19g2000vbz.googlegroups.com> On May 5, 10:20?pm, Aaron Brady wrote: > def auto( f ): > ? ? def _inner( *ar, **kw ): > ? ? ? ? return f( _inner, *ar, **kw ) > ? ? return _inner Quoting myself near the start of this thread: Here's an idea: >>> def bindfunc(f): ... def boundf(*args, **kwargs): ... return f(boundf, *args, **kwargs) ... return boundf -- Arnaud From Graham.Dumpleton at gmail.com Wed May 6 03:26:07 2009 From: Graham.Dumpleton at gmail.com (Graham Dumpleton) Date: Wed, 6 May 2009 00:26:07 -0700 (PDT) Subject: Cannot start a thread in atexit callback References: <333edbe80905051952v56ced17cl5aaa4b04bb8d3120@mail.gmail.com> Message-ID: <2acc47dd-35c0-40fa-8d56-bc1a52c87cc6@s1g2000prd.googlegroups.com> On May 6, 3:18?pm, "Gabriel Genellina" wrote: > En Tue, 05 May 2009 23:52:25 -0300, Zac Burns escribi?: > > > It seems that one cannot start a thread in an atexit callback. > > > My use case is that I have a IO heavy callback that I want to run in a > > thread so that other callbacks can finish while it's doing it's thing > > to save on exit time. > > Try creating the thread when the program begins, but locked. And release ? > the lock when your programs is about to finish. FWIW, from Python 2.5 (??) onwards, a shutdown of non daemonized threads is performed as a separate step before atexit callbacks. Not sure if other aspects of threading may be undone at that point and so prevent startup of new ones. The code in threading module which is called is: class _MainThread(Thread): ... def _exitfunc(self): self._Thread__stop() t = _pickSomeNonDaemonThread() if t: if __debug__: self._note("%s: waiting for other threads", self) while t: t.join() t = _pickSomeNonDaemonThread() if __debug__: self._note("%s: exiting", self) self._Thread__delete() _shutdown = _MainThread()._exitfunc The call to this is done from WaitForThreadShutdown() in main.c. Which is in turn called from Py_Main() just prior to calling Py_Finalize(). WaitForThreadShutdown(); Py_Finalize(); This is all different to older versions of Python which shutdown such threads from an actual atexit handler. This caused various ordering issues. Anyway, the point of this is that it would need to be a daemonized thread to begin with. :-) Graham From google at mrabarnett.plus.com Wed May 6 03:29:41 2009 From: google at mrabarnett.plus.com (MRAB) Date: Wed, 06 May 2009 08:29:41 +0100 Subject: Code works fine except... In-Reply-To: References: <8d4ec1df-dddb-469a-99a1-695152db7c04@n4g2000vba.googlegroups.com> <7f3493ca-efcb-48cc-aa29-487bb11fcd09@p4g2000vba.googlegroups.com> <16d47b2f-14d4-4266-84d2-c26501beb0c1@r34g2000vbi.googlegroups.com> <1691f042-bc14-46b9-b7b3-5a44348e9965@b1g2000vbc.googlegroups.com> <1a497471-498a-43d8-90ba-d676e8acf68b@r36g2000vbr.googlegroups.com> <8b49b775-66a7-42d5-9b00-cf23d9b044cc@e23g2000vbe.googlegroups.com> <54d76f11-c255-487d-9c3a-aaa380d7827a@z7g2000vbh.googlegroups.com> <991ccae3-f3e2-4d73-bd32-bc6f560e6f5f@n8g2000vbb.googlegroups.com> Message-ID: <4A013C65.4020104@mrabarnett.plus.com> John Yeung wrote: > On May 5, 11:37 pm, Ross wrote: > >> On May 5, 10:33 am, MRAB wrote: >> >>> Here's my approach (incomplete): >> FYI... I was testing your code further and discovered a strange >> outcome... when there are 16 people for 7 courts, every 7th >> round your code produces 4 byes instead of the correct 2 byes. > > Well, MRAB did say his was incomplete, and it's significantly shorter > and cleaner than mine. > > Mine has even weirder-looking behavior at 16 players on 7 courts, but > I think it's because mine tries harder to keep things balanced. After > a few rounds, the inequalities start to build up, and my routine picks > some people more often to "catch up", but it doesn't prevent the same > person from being scheduled for multiple matches the same week. There > may also be other, more subtle ways mine is broken. > > It also may be that the problem is inherently unsolvable for some > values. (I'm still waiting for someone with sufficient math ability > to swoop in and either provide a way that works for all values, or > confirm that there are just some unavoidable pathological cases.) > I'm not sure that all the requirements can be met. I have the feeling that if the number of rounds is restricted then the difference between the minimum and maximum number of byes could be 2 because of the requirement that players shouldn't play each other more than once, meaning that the players have to be shuffled around a bit, so a player might play a week earlier or later than would otherwise be the case. If every possible combination is done (everyone plays everyone else) then the number of byes would be the same for all, otherwise the difference could be 2, not 1. I think! :-) From CKurtM at gmail.com Wed May 6 04:38:16 2009 From: CKurtM at gmail.com (CkurtM) Date: Wed, 6 May 2009 01:38:16 -0700 (PDT) Subject: Pybuez Client sending messages to j2me Server Message-ID: <5858a389-7b9f-4f29-848a-1ef80861c8ad@m24g2000vbp.googlegroups.com> I have a problem with recieving requests on j2me based bluetooth server using RFCOMM. I can send messages from the j2me client to python server, but cant receive from the python bluetooth client to j2me server, it only connects but doesnt receive??.I can send copy of code on request. I'm using pybluez on python 2.6 and standard javax.bluetooth for the mobile interface. Any help wud be welcome. Thanks in advance. From R.Brodie at rl.ac.uk Wed May 6 04:45:24 2009 From: R.Brodie at rl.ac.uk (Richard Brodie) Date: Wed, 6 May 2009 09:45:24 +0100 Subject: thc v0.3 - txt to html converter - better code? References: <4a008996$0$31862$9b4e6d93@newsspool3.arcor-online.net> Message-ID: "Stefan Behnel" wrote in message news:4a008996$0$31862$9b4e6d93 at newsspool3.arcor-online.net... > language_map = {'English': 'EN', 'Deutsch': 'DE'} > strict_or_transitional = {True: 'Transitional', False: 'Strict'} > > # this will raise a KeyError for unknown languages > language = language_map[ self.cmboBoxLang.currentText() ] > > # this assumes that isChecked() returns True or False > spec = strict_or_transitional[self.rdioBtnTransitional.isChecked()] > > doctype = '\n' % ( > spec, language) Incidentally, the language in an HTML DOCTYPE refers to the language of the DTD, not the document. It's never correct to use //DE in an HTML page, unless you have a custom (German) DTD. So the code can be improved further by cutting that part out. strict_or_transitional = {True: 'Transitional', False: 'Strict'} # this assumes that isChecked() returns True or False spec = strict_or_transitional[self.rdioBtnTransitional.isChecked()] doctype = '\n' % spec From digitig at gmail.com Wed May 6 04:46:43 2009 From: digitig at gmail.com (Tim Rowe) Date: Wed, 6 May 2009 09:46:43 +0100 Subject: Which one is best Python or Java for developing GUI applications? In-Reply-To: References: <793408.86105.qm@web7903.mail.in.yahoo.com> Message-ID: 2009/5/6 Dennis Lee Bieber : > (the "near" is because I feel Ada is > stricter than any other language) Try SPARK -- it's Ada based, but /much/ stricter. It's just right for some really critical stuff, but is no sort of an answer to "Which one is best Python or Java for developing GUI"! -- Tim Rowe From paul.sijben at xs4all.nl Wed May 6 04:51:34 2009 From: paul.sijben at xs4all.nl (Paul Sijben) Date: Wed, 06 May 2009 10:51:34 +0200 Subject: desperately looking for a howto on running my wxPython app on Vista In-Reply-To: <1f97707a-9fd8-45d0-8aa6-142ce16b1951@e24g2000vbe.googlegroups.com> References: <49f6d607$0$199$e4fe514c@news.xs4all.nl> <4A006CCD.5060901@xs4all.nl> <1f97707a-9fd8-45d0-8aa6-142ce16b1951@e24g2000vbe.googlegroups.com> Message-ID: <4A014F96.5090004@xs4all.nl> Mike Driscoll wrote: > > > Hmmm...I'm not familiar with that DLL, but a little googling seems to > indicate that you may be able to get it off your installation CD: it actually is there on my system. So this may be the red herring the Dependency Walker FAQ is warning for.... maybe I should leave this mess and revert to python 2.5.... > > http://www.techimo.com/forum/applications-operating-systems/76550-winxp-msgina-dll-shlwapi-dll-problem.html > > The link above gives the steps for XP, but Vista will probably be > similar. I don't know for sure, but you may have to register the dll > once you've got it copied back onto your machine. > > Mike From davea at ieee.org Wed May 6 04:57:00 2009 From: davea at ieee.org (Dave Angel) Date: Wed, 06 May 2009 04:57:00 -0400 Subject: pyc files problem In-Reply-To: References: Message-ID: <4A0150DC.5050909@ieee.org> Gabriel Genellina wrote: >
En Wed, > 06 May 2009 00:43:25 -0300, Mohamed Lrhazi escribi?: > >> My code sends a pointer to a Python function to a C library, using >> ctypes module. When my program restarts, after a crash or normal >> exit... it cannot start again without sigfaulting > > Do you mean that, if you delete the .pyc files your program runs > properly, but if you keep the .pyc files your program crashes? > That's very strange... > >> What is the proper way around this problem? other than adding code to >> delete the cache files? > > There is no "proper way around" the problem; it should not exist in > the first place! > > Have you read the note at the end of the "Callback" section in the > ctypes documentation? > """Important note for callback functions: > > Make sure you keep references to CFUNCTYPE objects as long as they are > used from C code. ctypes doesn't, and if you don't, they may be > garbage collected, crashing your program when a callback is made.""" > > There is a ctypes-specific list: > https://lists.sourceforge.net/lists/listinfo/ctypes-users > 1) Is it true (as Gabriel asks) that deleting the .pyc files solves the problem? 2) If so, have you investigated to see which one of them gets corrupted? This isn't necessarily the problem, it could also be timing, or related to the order of the memory allocation pool. 3) When you get the segment fault, what does you C debugger show? What's happening at the time of the crash? 4) Is either the C code or the Python code multithreaded? 5) Are you running it under a Python debugger? From rschroev_nospam_ml at fastmail.fm Wed May 6 05:26:25 2009 From: rschroev_nospam_ml at fastmail.fm (Roel Schroeven) Date: Wed, 06 May 2009 11:26:25 +0200 Subject: list comprehension question In-Reply-To: <9086e914-3875-491c-8c88-ae697157b40f@o27g2000vbd.googlegroups.com> References: <6d502cfd-e252-4722-96a5-c52fc3579499@v1g2000prd.googlegroups.com> <85ocucnbce.fsf@agentultra.com> <85ljpb8r1o.fsf@agentultra.com> <9086e914-3875-491c-8c88-ae697157b40f@o27g2000vbd.googlegroups.com> Message-ID: <5JcMl.11662$5S3.7978@newsfe05.ams2> John Yeung schreef: > Essentially, if you see [B A] over and over and over again, when > finally confronted with LCs of more "elements", it's not immediately > clear that the pattern of increasing nestedness is > > [B A] => [C A B] => [D A B C] => etc. > > rather than > > [B A] => [C B A] => [D C B A] => etc. > > Maybe we're wired wrong, but there are many of us who find the second > pattern more logical, which is why it is so easy for us to mess up the > order of the nesting in a more complex LC. Indeed, that is exactly the reason why I find it difficult to read nested loop comprehensions, and why I almost never use them. -- The saddest aspect of life right now is that science gathers knowledge faster than society gathers wisdom. -- Isaac Asimov Roel Schroeven From sasurve at gmail.com Wed May 6 05:53:21 2009 From: sasurve at gmail.com (shruti surve) Date: Wed, 6 May 2009 15:23:21 +0530 Subject: About ODFPY links Message-ID: <862e00220905060253j290672acu86f18fc1bcd6bb2a@mail.gmail.com> hey all, For my project, i am using ODFpy open office spreadsheets. I am creating ledgers in python ie.ledger.py. So when i run ledger.py, spreadsheet will be opened in open office. since ledger is made of number of accounts, i am creating multiple tables for all accounts in single spreadsheet. So, there are number of tables in one spreadsheet. Now i want create links in each table of spreadsheet so that i can connect multiple tables. SO can anyone please tell me how to create links in odfpy.. -------------- next part -------------- An HTML attachment was scrubbed... URL: From Lacrima.Maxim at gmail.com Wed May 6 06:08:08 2009 From: Lacrima.Maxim at gmail.com (Lacrima) Date: Wed, 6 May 2009 03:08:08 -0700 (PDT) Subject: Checking for required arguments when instantiating class. Message-ID: Hello! For example I have two classes: >>> class First: def __init__(self, *args, **kwargs): pass >>> class Second: def __init__(self, somearg, *args, **kwargs): self.somearg = somearg How can I test that First class takes 1 required argument and Second class takes no required arguments? So that I could instantiate them in a for loop. >>> a = [First, Second] >>> for cls in a: instance = cls() Traceback (most recent call last): File "", line 2, in instance = cls() TypeError: __init__() takes at least 2 arguments (1 given) Of course, I can do like this: >>> for cls in a: try: instance = cls() except TypeError: instance = cls('hello') >>> print instance.somearg hello But what if I have to instantiate any class with 3 or 4 required arguments? How can I do it? With regards, Max From Lacrima.Maxim at gmail.com Wed May 6 06:11:58 2009 From: Lacrima.Maxim at gmail.com (Lacrima) Date: Wed, 6 May 2009 03:11:58 -0700 (PDT) Subject: Checking for required arguments when instantiating class. References: Message-ID: <7c16b282-298a-4593-9872-bc63d6edff15@t11g2000vbc.googlegroups.com> > >>> class First: > > ? ? ? ? def __init__(self, *args, **kwargs): > ? ? ? ? ? ? ? ? pass > > >>> class Second: > > ? ? ? ? def __init__(self, somearg, *args, **kwargs): > ? ? ? ? ? ? ? ? self.somearg = somearg > > How can I test that First class takes 1 required argument and Second > class takes no required arguments? Sorry, I have made a mistake. Of course, Second class takes 1 required argument, not First. From marco at sferacarta.com Wed May 6 06:21:45 2009 From: marco at sferacarta.com (Marco Mariani) Date: Wed, 06 May 2009 12:21:45 +0200 Subject: what's the best way to call a method of object without a guarantee of its existence In-Reply-To: <0348bb8e-3c1a-410d-a886-352b9b0ae391@e23g2000vbe.googlegroups.com> References: <22690572-e338-483a-a99a-85ccf6c5fae6@x6g2000vbg.googlegroups.com> <9uULl.4199$Ux.2825@tornado.fastwebnet.it> <0348bb8e-3c1a-410d-a886-352b9b0ae391@e23g2000vbe.googlegroups.com> Message-ID: <1xdMl.5391$Ux.3483@tornado.fastwebnet.it> Leon wrote: > So I need to go back to the module including "parent" class > to define the objects that I maybe use in future as None, You can assign them to a placeholder, with a method that always exists but does nothing. class NullObject(object): def method(self, *args, **kw): pass > actually, the module including "parent' class works very well > without those definitions, from parent class's point of view, > those definitions are kind of noisy. If you don't feel the need to define the instances there, maybe they don't belong to that class after all. From clp2 at rebertia.com Wed May 6 06:28:53 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Wed, 6 May 2009 03:28:53 -0700 Subject: Checking for required arguments when instantiating class. In-Reply-To: References: Message-ID: <50697b2c0905060328g3c3557cbw3fecc2b8b65b8f8e@mail.gmail.com> On Wed, May 6, 2009 at 3:08 AM, Lacrima wrote: > Hello! > > For example I have two classes: > >>>> class First: > ? ? ? ?def __init__(self, *args, **kwargs): > ? ? ? ? ? ? ? ?pass > >>>> class Second: > ? ? ? ?def __init__(self, somearg, *args, **kwargs): > ? ? ? ? ? ? ? ?self.somearg = somearg > > How can I test that First class takes 1 required argument and Second > class takes no required arguments? import inspect args, varargs, varkw, defaults = inspect.getargspec(klass) num_args_reqd = len(args) - (0 if defaults is None else len(defaults)) However, it sounds like a code smell that you're instanciating unknown classes that don't share a common constructor signature. Cheers, Chris -- http://blog.rebertia.com From sion at viridian.paintbox Wed May 6 06:32:40 2009 From: sion at viridian.paintbox (Sion Arrowsmith) Date: Wed, 06 May 2009 10:32:40 GMT Subject: object query assigned variable name? References: Message-ID: John O'Hagan wrote: >I guess what I meant was that if I type: > >brian = Brian() > >in the python shell and then hit return, it seems to me that _somewhere_ (in >the interpreter? I have no idea how it's done) it must be written that the >new Brian object will later be assigned the name "brian", even as the process >of creating the instance begins. As you've just demonstrated, the actual >assignment occurs afterwards. As far as CPython goes, the parser (presumably) knows, but the bytecode interpreter doesn't (without looking ahead), and you can't assume you've got anything more than the bytecode. This is what I should've done in the first place: >>> def f(): ... messiah = Brian() ... >>> import dis >>> dis.dis(f) 2 0 LOAD_GLOBAL 0 (Brian) 3 CALL_FUNCTION 0 6 STORE_FAST 0 (messiah) 9 LOAD_CONST 0 (None) 12 RETURN_VALUE >>> So while the information might be there, I'm not sure how you could make use of it without going into bytecodehacks territory. -- \S under construction From lie.1296 at gmail.com Wed May 6 06:51:47 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Wed, 06 May 2009 10:51:47 GMT Subject: Checking for required arguments when instantiating class. In-Reply-To: <7c16b282-298a-4593-9872-bc63d6edff15@t11g2000vbc.googlegroups.com> References: <7c16b282-298a-4593-9872-bc63d6edff15@t11g2000vbc.googlegroups.com> Message-ID: <7ZdMl.9024$y61.7026@news-server.bigpond.net.au> Lacrima wrote: >>>>> class First: >> def __init__(self, *args, **kwargs): >> pass >> >>>>> class Second: >> def __init__(self, somearg, *args, **kwargs): >> self.somearg = somearg >> >> How can I test that First class takes 1 required argument and Second >> class takes no required arguments? > > > Sorry, I have made a mistake. Of course, Second class takes 1 required > argument, not First. Of course, it is possible to just try it one-by-one with a try block and a while loop (as long as your class/function doesn't rely/change on some global state (e.g. files, global variable, GUI, etc) while initializing/called), but if you really need that, there is something seriously wrong with the class/function design. You should check the help and determine how many and what are all the arguments meant. In case you're wondering, this is how it would look like: >>> def f0(): ... print ... >>> def f1(a): ... print a ... >>> def f2(a, b): ... print a, b ... >>> def f2a(a, b, *args, **kargs): ... print a, b, args, kargs ... >>> import itertools >>> funclist = [f0, f1, f2, f2a] >>> for f in funclist: ... for i in itertools.count(): ... try: ... f(*range(i)) ... except TypeError: ... pass ... else: ... print 'Function %s takes %s required arguments' % (f.__name__, i) ... break ... Function f0 takes 0 required arguments 0 Function f1 takes 1 required arguments 0 1 Function f2 takes 2 required arguments 0 1 () {} Function f2a takes 2 required arguments >>> From lie.1296 at gmail.com Wed May 6 07:49:15 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Wed, 06 May 2009 11:49:15 GMT Subject: Self function In-Reply-To: References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> <50697b2c0905042346u482eabf9j1bb9f95853f38864@mail.gmail.com> Message-ID: <%OeMl.9036$y61.4364@news-server.bigpond.net.au> Luis Zarrabeitia wrote: > Btw, is there any way to inject a name into a function's namespace? Following > the python tradition, maybe we should try to create a more general solution! How about a mechanism to pass arguments that are optional to accept? So (in the general case) the caller can call a function with certain arguments, but unless the function explicitly accept it, the argument will be discarded. With this, the injecting a local would simply be a case of adding an "I accept the argument" statement. Sort of like this: def func(): ''' I reject ''' pass func(@somearg='Feel free to accept this arg') def func(@somearg): ''' I accept ''' print(somearg) func(@somearg='Feel free to accept this arg') Then on the case of injecting locals is simply by wrapping the function with a function that will inject various argument to the function and the function selectively choose which arguments they want to accept. def needinfo(f): # let's assume there is an inspect code here func(@__name__=yournameis, @__call__=yourfunction, @__code__=yourcode, @__yoursub__=youryo, @__yourlim__=yoururso, @__yourina__=yournisk, @__yourlmes__=youridna, @__yoursage__=yourpped ) @needinfo def func(@__name__, @__call__): print 'Hi, my name is %s and to call me press %s' % (__name__, __call__) Does it sounds like a feature smell? From piet at cs.uu.nl Wed May 6 07:57:26 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Wed, 06 May 2009 13:57:26 +0200 Subject: object query assigned variable name? References: Message-ID: >>>>> John O'Hagan (JO) wrote: >JO> I guess what I meant was that if I type: >JO> brian = Brian() >JO> in the python shell and then hit return, it seems to me that >JO> _somewhere_ (in the interpreter? I have no idea how it's done) it >JO> must be written that the new Brian object will later be assigned >JO> the name "brian", even as the process of creating the instance >JO> begins. As you've just demonstrated, the actual assignment occurs >JO> afterwards. No, that's not how it works. There is no `preparation' for the assignment and there is no reason for it. Look at the generated byte code for this Python code: def test(): class Brian: def __init__(self): print "I'm Brian!" brian = Brian() 2 0 LOAD_CONST 1 ('Brian') 3 LOAD_CONST 3 (()) 6 LOAD_CONST 2 (", line 2>) 9 MAKE_FUNCTION 0 12 CALL_FUNCTION 0 15 BUILD_CLASS 16 STORE_FAST 0 (Brian) 5 19 LOAD_FAST 0 (Brian) 22 CALL_FUNCTION 0 25 STORE_FAST 1 (brian) 28 LOAD_CONST 0 (None) 31 RETURN_VALUE You can see that the assignment (STORE_FAST) is done after the method call (CALL_FUNCTION) and there is no reference to the variable name before the call. If you do a parallel assignment the relationship becomes even less: brian, test = Brian(), 1 5 19 LOAD_FAST 0 (Brian) 22 CALL_FUNCTION 0 25 LOAD_CONST 3 (1) 28 ROT_TWO 29 STORE_FAST 1 (brian) 32 STORE_FAST 2 (test) -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From deets at nospam.web.de Wed May 6 08:07:58 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Wed, 06 May 2009 14:07:58 +0200 Subject: Install Python 3.0 dmg to macos 10.5 References: <627526f7-ac27-4874-b234-13d1a13accf5@y33g2000prg.googlegroups.com> Message-ID: <76dcpuF1cbhk2U1@mid.uni-berlin.de> silverburgh wrote: > Hi, > > If I install python 3.0 dmg, will it wipe out the existing python > installation on macos 10.5 (i don't know how the original python was > installed ( i think it is version 2.5). No. The original is under /System/Library/Frameworks/Python.framework (and should be kept there & not touched), and other installs are under /Library/Frameworks/Python.framework Diez From phd at phd.pp.ru Wed May 6 08:09:13 2009 From: phd at phd.pp.ru (Oleg Broytmann) Date: Wed, 6 May 2009 16:09:13 +0400 Subject: SQLObject 0.9.10 Message-ID: <20090506120913.GB15968@phd.pp.ru> Hello! I'm pleased to announce version 0.9.10, a minor bugfix release of 0.9 branch of SQLObject. What is SQLObject ================= SQLObject is an object-relational mapper. Your database tables are described as classes, and rows are instances of those classes. SQLObject is meant to be easy to use and quick to get started with. SQLObject supports a number of backends: MySQL, PostgreSQL, SQLite, Firebird, Sybase, MSSQL and MaxDB (also known as SAPDB). Where is SQLObject ================== Site: http://sqlobject.org Development: http://sqlobject.org/devel/ Mailing list: https://lists.sourceforge.net/mailman/listinfo/sqlobject-discuss Archives: http://news.gmane.org/gmane.comp.python.sqlobject Download: http://cheeseshop.python.org/pypi/SQLObject/0.9.10 News and changes: http://sqlobject.org/News.html What's New ========== News since 0.9.9 ---------------- * Another unicode-related patch for MySQL; required because different versions of MySQLdb require different handling:: - MySQLdb < 1.2.1: only ascii - MySQLdb = 1.2.1: only unicode - MySQLdb > 1.2.1: both ascii and unicode * Setup requires FormEncode version 1.1.1+. * Fixed a minor bug - pass name to DecimalValidator. * Fixed a bug in InheritableIteration - pass connection to child klass.select(). * A bug was fixed in PostgresConnection.columnsFromSchema() - foreign keys are now recognized and created as proper ForeignKey with correct column name and table name. * Bugs in PostgresConnection and MSSQLConnection related to properties was fixed. A note for developers: from now on properties in DBConnection classes are forbidden as they don't work with Transaction - Transaction.__getattr__() cannot properly wrap 'self' so a property is called with wrong 'self'. * Transaction instances now explicitly raises TypeError on close() - without this calling Transaction.close() calls connection.close() which is wrong. * A bug in SQLiteConnection.columnsFromSchema() that led to an infinite loop was fixed. For a more complete list, please see the news: http://sqlobject.org/News.html Oleg. -- Oleg Broytmann http://phd.pp.ru/ phd at phd.pp.ru Programmers don't die, they just GOSUB without RETURN. From phd at phd.pp.ru Wed May 6 08:19:34 2009 From: phd at phd.pp.ru (Oleg Broytmann) Date: Wed, 6 May 2009 16:19:34 +0400 Subject: SQLObject 0.10.5 Message-ID: <20090506121934.GF15968@phd.pp.ru> Hello! I'm pleased to announce version 0.10.5, a minor bugfix release of 0.10 branch of SQLObject. What is SQLObject ================= SQLObject is an object-relational mapper. Your database tables are described as classes, and rows are instances of those classes. SQLObject is meant to be easy to use and quick to get started with. SQLObject supports a number of backends: MySQL, PostgreSQL, SQLite, Firebird, Sybase, MSSQL and MaxDB (also known as SAPDB). Where is SQLObject ================== Site: http://sqlobject.org Development: http://sqlobject.org/devel/ Mailing list: https://lists.sourceforge.net/mailman/listinfo/sqlobject-discuss Archives: http://news.gmane.org/gmane.comp.python.sqlobject Download: http://cheeseshop.python.org/pypi/SQLObject/0.10.5 News and changes: http://sqlobject.org/News.html What's New ========== News since 0.10.4 ----------------- * Another unicode-related patch for MySQL; required because different versions of MySQLdb require different handling:: - MySQLdb < 1.2.1: only ascii - MySQLdb = 1.2.1: only unicode - MySQLdb > 1.2.1: both ascii and unicode * Setup requires FormEncode version 1.1.1+. * Fixed a minor bug - pass name to DecimalValidator. * Fixed a bug in InheritableIteration - pass connection to child klass.select(). * sqlmeta.getColumns() becomes classmethod. * A bug was fixed in PostgresConnection.columnsFromSchema() - foreign keys are now recognized and created as proper ForeignKey with correct column name and table name. * Bugs in PostgresConnection and MSSQLConnection related to properties was fixed. A note for developers: from now on properties in DBConnection classes are forbidden as they don't work with Transaction - Transaction.__getattr__() cannot properly wrap 'self' so a property is called with wrong 'self'. * Transaction instances now explicitly raises TypeError on close() - without this calling Transaction.close() calls connection.close() which is wrong. * A bug in SQLiteConnection.columnsFromSchema() that led to an infinite loop was fixed. For a more complete list, please see the news: http://sqlobject.org/News.html Oleg. -- Oleg Broytmann http://phd.pp.ru/ phd at phd.pp.ru Programmers don't die, they just GOSUB without RETURN. From piet at cs.uu.nl Wed May 6 08:24:59 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Wed, 06 May 2009 14:24:59 +0200 Subject: Checking for required arguments when instantiating class. References: Message-ID: >>>>> Lacrima (L) wrote: >L> Hello! >L> For example I have two classes: >>>>> class First: >L> def __init__(self, *args, **kwargs): >L> pass >>>>> class Second: >L> def __init__(self, somearg, *args, **kwargs): >L> self.somearg = somearg >L> How can I test that First class takes 1 required argument and Second >L> class takes no required arguments? >L> So that I could instantiate them in a for loop. >>>>> a = [First, Second] >>>>> for cls in a: >L> instance = cls() >L> Traceback (most recent call last): >L> File "", line 2, in >L> instance = cls() >L> TypeError: __init__() takes at least 2 arguments (1 given) >L> Of course, I can do like this: >>>>> for cls in a: >L> try: >L> instance = cls() >L> except TypeError: >L> instance = cls('hello') >>>>> print instance.somearg >L> hello >L> But what if I have to instantiate any class with 3 or 4 required >L> arguments? How can I do it? cls.__init__.im_func.__code__.co_argcount This will include self, so it will be 1 in First and 2 in Second. However this is very dirty trickery and should not be recommended. It may also change in future versions and other implementations of Python. I think it would be cleaner to put a class attribute in the classes that defines how they should be initialized (e.g. just the number of required arguments or more specific information) or have a special factory method for this use case. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From clp2 at rebertia.com Wed May 6 08:36:48 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Wed, 6 May 2009 05:36:48 -0700 Subject: Checking for required arguments when instantiating class. In-Reply-To: References: Message-ID: <50697b2c0905060536u4063f9c9g63c5d832d86da017@mail.gmail.com> On Wed, May 6, 2009 at 5:24 AM, Piet van Oostrum wrote: >>>>>> Lacrima (L) wrote: > >>L> Hello! >>L> For example I have two classes: > >>>>>> class First: >>L> ? ? def __init__(self, *args, **kwargs): >>L> ? ? ? ? ? ? pass > >>>>>> class Second: >>L> ? ? def __init__(self, somearg, *args, **kwargs): >>L> ? ? ? ? ? ? self.somearg = somearg > >>L> How can I test that First class takes 1 required argument and Second >>L> class takes no required arguments? >>L> So that I could instantiate them in a for loop. > >>>>>> a = [First, Second] >>>>>> for cls in a: >>L> ? ? instance = cls() > >>L> Traceback (most recent call last): >>L> ? File "", line 2, in >>L> ? ? instance = cls() >>L> TypeError: __init__() takes at least 2 arguments (1 given) > >>L> Of course, I can do like this: >>>>>> for cls in a: >>L> ? ? try: >>L> ? ? ? ? ? ? instance = cls() >>L> ? ? except TypeError: >>L> ? ? ? ? ? ? instance = cls('hello') > >>>>>> print instance.somearg >>L> hello > >>L> But what if I have to instantiate any class with 3 or 4 required >>L> arguments? How can I do it? > > cls.__init__.im_func.__code__.co_argcount > > This will include self, so it will be 1 in First and 2 in Second. AFAICT, that would count non-required arguments too, which isn't strictly what the OP requested. > However this is very dirty trickery and should not be recommended. It > may also change in future versions and other implementations of Python. Very much agreed. > I think it would be cleaner to put a class attribute in the classes that > defines how they should be initialized (e.g. just the number of required > arguments or more specific information) or have a special factory method > for this use case. Seconded. I'd recommend the latter personally, though it's impossible to give a definitive answer without more context. Cheers, Chris -- http://blog.rebertia.com From castironpi at gmail.com Wed May 6 08:56:17 2009 From: castironpi at gmail.com (Aaron Brady) Date: Wed, 6 May 2009 05:56:17 -0700 (PDT) Subject: Self function References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> <6c6fffd7-2f43-454d-8e63-056d0c1c05d3@w31g2000prd.googlegroups.com> <4a69c949-ace7-47d9-8bb7-d05bc6c895f3@s31g2000vbp.googlegroups.com> <50222a39-88d0-49da-9105-2d3a47af5940@r34g2000vba.googlegroups.com> <5b81cccc-c174-42bd-b4a0-638755ca9e03@l5g2000vbc.googlegroups.com> <57b22b7c-1130-4f28-a7b7-bd119445de41@z19g2000vbz.googlegroups.com> Message-ID: <507794a3-acc3-49f7-a418-4b1bf27c6a9e@s16g2000vbp.googlegroups.com> On May 6, 2:23?am, Arnaud Delobelle wrote: > On May 5, 10:20?pm, Aaron Brady wrote: > > > def auto( f ): > > ? ? def _inner( *ar, **kw ): > > ? ? ? ? return f( _inner, *ar, **kw ) > > ? ? return _inner > > Quoting myself near the start of this thread: > > Here's an idea: > > >>> def bindfunc(f): > > ... ? ? def boundf(*args, **kwargs): > ... ? ? ? ? return f(boundf, *args, **kwargs) > ... ? ? return boundf > > -- > Arnaud Yeah, but that was two days ago. From castironpi at gmail.com Wed May 6 08:57:34 2009 From: castironpi at gmail.com (Aaron Brady) Date: Wed, 6 May 2009 05:57:34 -0700 (PDT) Subject: Self function References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> <50697b2c0905042346u482eabf9j1bb9f95853f38864@mail.gmail.com> <%OeMl.9036$y61.4364@news-server.bigpond.net.au> Message-ID: On May 6, 6:49?am, Lie Ryan wrote: > Luis Zarrabeitia wrote: > > Btw, is there any way to inject a name into a function's namespace? Following > > the python tradition, maybe we should try to create a more general solution! > > How about a mechanism to pass arguments that are optional to accept? > > So (in the general case) the caller can call a function with certain > arguments, but unless the function explicitly accept it, the argument > will be discarded. > > With this, the injecting a local would simply be a case of adding an "I > accept the argument" statement. snip You could write a decorator to do that with the inspect module, or you could just accept a keyword dict for extra as-desired args. From castironpi at gmail.com Wed May 6 09:04:20 2009 From: castironpi at gmail.com (Aaron Brady) Date: Wed, 6 May 2009 06:04:20 -0700 (PDT) Subject: object query assigned variable name? References: Message-ID: On May 6, 12:56?am, John O'Hagan wrote: > On Tue, 5 May 2009, Sion Arrowsmith wrote: > > John O'Hagan ? wrote: > > >I can see that it's tantalizing, though, because _somebody_ must know > > > about the assignment; after all, we just executed it! > > > Except we haven't, if we're talking about reporting from the > > > object's __init__: > > >>> class Brian: > > > ... ? ? def __init__(self): > > ... ? ? ? ? print "I'm Brian!" > > ... > > > >>> l = [] > > >>> l[1] = Brian() > > > I'm Brian! > > Traceback (most recent call last): > > ? File "", line 1, in > > IndexError: list assignment index out of range > > > (Yeah, I know that's a setitem call not an assignment. Point stands. > > It also demonstrates why the whole idea of "what name is a newly- > > created object assigned to" is broken.) > > I guess what I meant was that if I type: > > brian = Brian() > > in the python shell and then hit return, it seems to me that _somewhere_ (in > the interpreter? I have no idea how it's done) it must be written that the > new Brian object will later be assigned the name "brian", even as the process > of creating the instance begins. As you've just demonstrated, the actual > assignment occurs afterwards. > > But even if this is true I'm guessing it'd be black magic to get to it. > > Regards, > > John In principle, you can't get all the names an object is bound to, since it might be a temporary value or member of a container at some time. If it was in a dictionary, would you want its key? Or sequence and index? Furthermore, we don't have a data structure for two-way lookup. Sometimes you would want to lookup name by object, sometimes object by name. Lastly, it would make code significantly less readable . From OldGrantonian at googlemail.com Wed May 6 09:37:33 2009 From: OldGrantonian at googlemail.com (OldGrantonian) Date: Wed, 6 May 2009 06:37:33 -0700 (PDT) Subject: Separate Windows versions of Python Message-ID: <52b28a62-d0de-4d26-b2bd-dc4bf2affb71@p4g2000vba.googlegroups.com> I use Windows Vista Home Premium. I have Python 2.6 currently installed. I'm not a techy. I want to use "virtualenv". When I try to install the latest version of virtualenv for Windows, I get the message that Python 2.5 is not found. How do I install Python 2.5 (or anything else except 2.6) so that I use the correct version of Python at the correct time. For example, do I need to edit the "path" variable each time that I want to change versions from 2.5 to 2.6? From tornadomaker at gmx.net Wed May 6 09:41:12 2009 From: tornadomaker at gmx.net (Mike) Date: Wed, 6 May 2009 06:41:12 -0700 (PDT) Subject: pyinstaller and logging Message-ID: Pyinstaller seems to have a problem with logging ... I installed pyinstaller 1.3 - using it together with Python 2.6. I used pyinstaller for a small project, the created .exe worked fine. After some additional changes to my project I got strange run time errors when running the .exe (but no problems when running the .py file directly with the python interpreter). I stripped down my program and tracked the problem down to the first log message being written. So this is a stripped down version of the python script: import logging logging.basicConfig(level=logging.INFO, format='%(asctime)-15s %(levelname)-8s %(message) s', datefmt='%Y-%m-%d %H:%M:%S', filename="test1.log", filemode='w') print "written via print" logging.info("written via logging") when I start this from the python shell I am getting IDLE 2.6.2 ==== No Subprocess ==== >>> written via print >>> and test1.log contains 2009-05-05 19:39:52 INFO written via logging as expected. When I start test1.exe I am getting written via print Traceback (most recent call last): File "", line 8, in File "c:\dokumente und einstellungen\dieter\eigene dateien\collector \v0r0m2\buildtest1\out1.pyz/logging", line 1451, in info File "c:\dokumente und einstellungen\dieter\eigene dateien\collector \v0r0m2\buildtest1\out1.pyz/logging", line 1030, in info File "c:\dokumente und einstellungen\dieter\eigene dateien\collector \v0r0m2\buildtest1\out1.pyz/logging", line 1142, in _log File "c:\dokumente und einstellungen\dieter\eigene dateien\collector \v0r0m2\buildtest1\out1.pyz/logging", line 1117, in makeRecord File "c:\dokumente und einstellungen\dieter\eigene dateien\collector \v0r0m2\buildtest1\out1.pyz/logging", line 272, in __init__ File "C:\Programme\pyinstaller-1.3\iu.py", line 312, in importHook mod = _self_doimport(nm, ctx, fqname) File "C:\Programme\pyinstaller-1.3\iu.py", line 398, in doimport exec co in mod.__dict__ File "c:\dokumente und einstellungen\dieter\eigene dateien\collector \v0r0m2\buildtest1\out1.pyz/multiprocessing", line 83, in File "C:\Programme\pyinstaller-1.3\iu.py", line 312, in importHook mod = _self_doimport(nm, ctx, fqname) File "C:\Programme\pyinstaller-1.3\iu.py", line 382, in doimport mod = director.getmod(nm) File "C:\Programme\pyinstaller-1.3\iu.py", line 215, in getmod mod = owner.getmod(nm) File "C:\Programme\pyinstaller-1.3\iu.py", line 77, in getmod mod = imp.load_module(nm, fp, attempt, (ext, mode, typ)) TypeError: importHook() takes at most 5 arguments (6 given) What am I doing wrong? From james at agentultra.com Wed May 6 09:48:51 2009 From: james at agentultra.com (J Kenneth King) Date: Wed, 06 May 2009 09:48:51 -0400 Subject: list comprehension question References: <6d502cfd-e252-4722-96a5-c52fc3579499@v1g2000prd.googlegroups.com> <85ocucnbce.fsf@agentultra.com> <85ljpb8r1o.fsf@agentultra.com> Message-ID: <85d4am8hq4.fsf@agentultra.com> Emile van Sebille writes: > On 5/5/2009 9:15 AM J Kenneth King said... > >> List comprehensions can make a reader of your code apprehensive >> because it can read like a run-on sentence and thus be difficult to >> parse. The Python documentation discourages their use and I believe >> for good reason. > > Can you provide a link for this? I'd like to see specifically what's > being discouraged, as I'd be surprised to find routine usage frowned > upon. > > Emile http://docs.python.org/tutorial/datastructures.html#nested-list-comprehensions "If you?ve got the stomach for it, list comprehensions can be nested. They are a powerful tool but ? like all powerful tools ? they need to be used carefully, if at all." and "In real world, you should prefer builtin functions to complex flow statements." From mauriceling at gmail.com Wed May 6 10:08:24 2009 From: mauriceling at gmail.com (mauriceling@acm.org) Date: Wed, 6 May 2009 07:08:24 -0700 (PDT) Subject: [ANN] Completion of The Python Papers Volume 4 Issue 1 Message-ID: <8b10356e-1a40-424b-b288-bbc3fa6f7d39@x29g2000prf.googlegroups.com> Welcome to Issue 4 Volume 1 of The Python Papers. This marks our 4th year in business. It is my pleasure to announce 4 improvements made to The Python Papers Anthology. Firstly, we had created a class of editorial members in our team ? the Editorial Reviewers (ER). This is in addition to the existing Editors- in-Chief (EIC) and Associate Editors (AE). In the words of our revised editorial policy, ?ER is a tier of editors deemed to have in-depth expertise knowledge in specialized areas. As members of the editorial board, ERs are accorded editorial status but are generally not involved in the strategic and routine operations of the periodicals although their expert opinions may be sought at the discretion of EIC.? To this aspect, we are glad to have Jacob Kaplan-Moss (President, Django Software Foundation; Partner, Revolution Systems, LLC; Co-BDFL, Django) as our first ER. Welcome aboard. Secondly, we had adopted a fast publication scheme. In the past, accepted manuscripts will only be published on the release date in the month of April, August and December each year. With the use of Open Journal System, we are able to publish manuscripts as they are being accepted. This means that the release date in the month of April, August and December is now effectively our closing date for the issue. For example, manuscripts accepted after April 2009 will be routed to immediate publication in Volume 4 Issue 2. This is to being information faster to our readers, rather than waiting for a few months. Thirdly, we announce a set of manuscript templates for our authors and contributors for their manuscript preparation. As such, we had revamped our website (http://www.pythonpapers.org) to reflect these changes. Lastly, we are glad to announce the launch of our 2nd publication under The Python Papers Anthology ? The Python Papers Source Codes (ISSN 1836-621X). TPPSC, for short, is a collection of software and source codes, usually associated with papers published in The Python Papers and The Python Papers Monograph. These codes are refereed for originality, accuracy, completeness, and lasting value. TPPSC will act as a developers codebook of high-quality, peer-reviewed codes. TPPSC is an annual publication using the same fast publication scheme. We thank all our readers and contributors for their continued support. Happy reading. Maurice Ling Co-EIC, TPPA From exarkun at divmod.com Wed May 6 10:10:40 2009 From: exarkun at divmod.com (Jean-Paul Calderone) Date: Wed, 6 May 2009 10:10:40 -0400 Subject: problem in using sendmail in multi thread In-Reply-To: Message-ID: <20090506141040.21531.104267133.divmod.quotient.836@henry.divmod.com> On Tue, 5 May 2009 22:17:35 -0700 (PDT), gganesh wrote: >On May 5, 9:25?pm, Piet van Oostrum wrote: >> >>>>> gganesh (g) wrote: >> >g> hi, >> >g> I'm a beginner in using Python script >> >g> I'm trying to send mails using multi-thread >> >g> I wrote >> >g> FROM = 'ganeshx... at xxxx.com' >> >g> # for more mail add';' the another mail id >> >g> listTo = ['g.... at gmail.com', 'xxjango... at gmail.com', >> >g> 'xx... at yahoo.co.in'] >> >g> SUBJECT = 'This is the subject' >> >g> MSGBODY = 'This the body of the message ' >> >g> MAILSERVER = 'mail.xxxx.com' >> >g> port = 25 >> >g> username = 'xxxxx' >> >g> password = 'xxxxx' >> >g> # trim the strings of any leading or trailing spaces >> >g> FROM = FROM.strip() >> >g> SUBJECT = SUBJECT.strip() >> >g> MSGBODY = MSGBODY.strip() >> >g> MAILSERVER = MAILSERVER.strip() >> >g> username = username.strip() >> >g> password = password.strip() >> >g> #Connect to server >> >g> print 'Connecting to mail server ', MAILSERVER >> >g> try: >> >g> ? ? ? s = smtplib.SMTP(MAILSERVER,port) >> >> You can't have a single SMTP connection that's used in multiple threads. >> That is what causes the error. Each thread should have its own SMTP >> connection. So move this code (above and below) into the run() method. >> >> >g> ? ? ? print 'connected' >> >g> #s.set_debuglevel(1) >> >g> except: >> >g> ? ? ? ?print 'ERROR: Unable to connect to mail server', sys.exc_info ?()[0] >> >g> ? ? ? ?sys.exit(1) >> >g> #login to server >> >g> if password <> '': >> >g> ? ? ? print 'Logging into mail server' >> >> I think this try except block should be inside the if statement, i.e. >> indented 4 spaces. >> >> >g> try: >> >g> ? ? ? s.login(username,password) >> >g> except: >> >g> ? ? ? print 'ERROR: Unable to login to mail server', MAILSERVER >> >g> ? ? ? print 'Please recheck your password' >> >g> ? ? ? sys.exit(1) >> >g> #-------------------------------------------------- >> >g> print "Starting Multi Thread Method" >> >g> class MyThread(Thread): >> >g> ? ? def __init__(self, site, s, FROM, MSGBODY): >> >g> ? ? ? ? Thread.__init__(self) >> >g> ? ? ? ? self.site = site >> >g> ? ? ? self.s=s >> >g> ? ? ? self.FROM=FROM >> >g> ? ? ? self.MSGBODY=MSGBODY >> >> You give the s (connection) here as a parameter, store it in self.s and >> never use that attribute. >> >> >g> ? ? def run(self): >> >g> ? ? ? print "running for %s " %self.site >> >g> ? ? ? s.sendmail(self.FROM, self.site, self.MSGBODY) >> >> Here you use the global s, not self.s >> >> As I said above you should do the SMTP connection setup, including the >> login, here, and store the connection either in self.s or in a local >> variable s in the method. As you don't use the s in another method, I >> think a local variable is better. >> >> >g> ? ? ? print "Emailed for ?site %s" %self.site >> >g> a= time.time() >> >g> threads = [] >> >g> for site in listTo: >> >g> ? ? T = MyThread(site,s,FROM,MSGBODY) >> >g> ? ? threads.append(T) >> >g> ? ? T.start() >> >g> for i in threads: >> >g> ? ? i.join() >> >> Of course the next 2 lines should also be moved to run(). >> >> >g> s.quit() >> >g> s.close() >> >g> print "Took %s seconds" %str(time.time()-a) >> >g> #----------------------------------------------------- >> >> -- >> Piet van Oostrum >> URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4] >> Private email: p... at vanoostrum.org > > >Thanks Everyone By your guidance the code worked fine >I can send mails in multi threaded environment. >Is this only way to send mails concurrently or any other method >aviable? Here's another way: from twisted.mail.smtp import sendmail from twisted.internet import reactor from twisted.python.log import err MAILSERVER = ... listTo = [...] FROM = ... MSGBODY = ... done = sendmail(MAILSERVER, FROM, listTo, MSGBODY) done.addErrback(err) done.addCallback(lambda ignored: reactor.stop()) reactor.run() Jean-Paul From piotrek at nonexistent.domain Wed May 6 10:20:01 2009 From: piotrek at nonexistent.domain (Piotrek G.) Date: Wed, 06 May 2009 16:20:01 +0200 Subject: Control a process interactively (pexpect) and save data Message-ID: Hi, I'm trying to use something like pexpect.interact() but I want to save all my inputs and save all outputs from the process (/bin/sh Linux). The main goal is to record all sequence of commands and responses in order to automatically generate pexpect script. My script is like below so far, but I'm unable to print responses to stdout and save it to variables/files/whatever. p = pexpect.spawn('/bin/sh') print "PID: " + str(p.pid) p.logfile = sys.stdout while True: if not p.isalive(): print "Not alive" break else: print "Alive!" p.flush() bb = sys.stdin.readline() p.sendline(bb) sys.exit(0) pexpect.interact() doesn't allow to save input and output. I tried pipes but I've found that "Don't use a pipe to control another application..." - http://edgysoftware.com/doc/python-pexpect/doc/ I tried subprocess module but it didn't allow me to control /bin/sh as communicate() method do as follows "Interact with process: Send data to stdin. Read data from stdout and stderr, until end-of-file is reached. Wait for process to terminate. The optional input argument should be a string to be sent to the child process, or None, if no data should be sent to the child." So it "waits for process to terminate" and I'm unable to interact... Any suggestions? Oh, and by the way my script works with /bin/sh but doesn't work with /bin/bash. If I enter 'exit' sh exits, bash does not... Why? Thanks! -- Regards Piotrek From aahz at pythoncraft.com Wed May 6 10:39:50 2009 From: aahz at pythoncraft.com (Aahz) Date: 6 May 2009 07:39:50 -0700 Subject: python docs for beginner programmer? References: <81f82d8c-80fa-4ba0-a402-3a8b5757a967@s16g2000vbp.googlegroups.com> Message-ID: In article <81f82d8c-80fa-4ba0-a402-3a8b5757a967 at s16g2000vbp.googlegroups.com>, Deep_Feelings wrote: > >anyone did that ? learning from python docs straight away ? Yes. Admittedly that was ten years ago and the docs have improved some since then.... -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "It is easier to optimize correct code than to correct optimized code." --Bill Harlan From bborcic at gmail.com Wed May 6 10:49:28 2009 From: bborcic at gmail.com (Boris Borcic) Date: Wed, 06 May 2009 16:49:28 +0200 Subject: list comprehension question In-Reply-To: <6d502cfd-e252-4722-96a5-c52fc3579499@v1g2000prd.googlegroups.com> References: <6d502cfd-e252-4722-96a5-c52fc3579499@v1g2000prd.googlegroups.com> Message-ID: Ross wrote: > If I have a list of tuples a = [(1,2), (3,4), (5,6)], and I want to > return a new list of each individual element in these tuples, I can do > it with a nested for loop but when I try to do it using the list > comprehension b = [j for j in i for i in a], my output is b = > [5,5,5,6,6,6] instead of the correct b = [1,2,3,4,5,6]. What am I > doing wrong? > -- > http://mail.python.org/mailman/listinfo/python-list > just fyi, in python 2.6 list(itertools.chain.from_iterable(a)) would do it in python 2.5 list(itertools.chain(*a)) would do it too, but I wouldn't try it with arbitrarily long a From Scott.Daniels at Acm.Org Wed May 6 10:57:00 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Wed, 06 May 2009 07:57:00 -0700 Subject: pyc files problem In-Reply-To: References: Message-ID: Dave Angel wrote: > Gabriel Genellina wrote: ... >> 06 May 2009 00:43:25 -0300, Mohamed Lrhazi escribi?: >>> My code sends a pointer to a Python function to a C library, using >>> ctypes module. When my program restarts, after a crash or normal >>> exit... it cannot start again without sigfaulting >> >> Do you mean that, if you delete the .pyc files your program runs >> properly, but if you keep the .pyc files your program crashes? >> That's very strange... > > 1) Is it true (as Gabriel asks) that deleting the .pyc files solves the > problem? > 2) If so, have you investigated to see which one of them gets > corrupted? This isn't necessarily the problem, it could also be timing, > or related to the order of the memory allocation pool. > 3) When you get the segment fault, what does you C debugger show? > What's happening at the time of the crash? > 4) Is either the C code or the Python code multithreaded? > 5) Are you running it under a Python debugger? > Could this have to do with a manifest floating point constant being a "Not A Number" or "Infinity"? That is the last .pyc- related problem I know about. To the original poster: Please give a _lot_ more detail about the full Python version, OS type and version, and so on. Otherwise, it becomes tempting to say, "that is too bad, sorry you have troubles." One way to isolate the problem (if it is, indeed .pyc-loading related) is to delete individual .pyc files until you find the suspect, then cut that one in pieces and re-isolate until you have a small bit of code with the issue. This is why we point people to smart-questions(*) so often. (*) http://www.catb.org/~esr/faqs/smart-questions.html --Scott David Daniels Scott.Daniels at Acm.Org From michele.simionato at gmail.com Wed May 6 11:14:16 2009 From: michele.simionato at gmail.com (Michele Simionato) Date: Wed, 6 May 2009 08:14:16 -0700 (PDT) Subject: python docs for beginner programmer? References: <81f82d8c-80fa-4ba0-a402-3a8b5757a967@s16g2000vbp.googlegroups.com> Message-ID: <404c22b7-041f-4c85-9c7d-11e9945d0758@v4g2000vba.googlegroups.com> On May 5, 10:37?pm, Deep_Feelings wrote: > that is good ,thank you > > anyone did that ? learning from python docs straight away ? A lot of people did. I did, but I usually prefer reading user manuals over books, and YMMV. From keshet1 at umbc.edu Wed May 6 11:31:58 2009 From: keshet1 at umbc.edu (Ben Keshet) Date: Wed, 06 May 2009 11:31:58 -0400 Subject: Pyhton script to call another program Message-ID: <4A01AD6E.4010807@umbc.edu> Hi, I am trying to write a simple python script to manipulate files and call other programs. I have a program installed (rocs) which I run using cygwin on my XP (but is not in python). Can I run the pyhton script and then call the other program in the same script? For example: Python Code # this line opens a file Python Code # this line splits the file into 4 different files rocs # this line calls the program 'rocs' to run on the newly formed files Python Code # this line renames rocs output files and saves them in the right place. Otherwise, I would have to write two scripts (before and after 'rocs'), and run the 3 scripts/programs separately. I am relatively new to python and don't know a lot about cygwin. Please remember that if you try to answer me :) Thanks! From Scott.Daniels at Acm.Org Wed May 6 11:35:51 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Wed, 06 May 2009 08:35:51 -0700 Subject: Separate Windows versions of Python In-Reply-To: <52b28a62-d0de-4d26-b2bd-dc4bf2affb71@p4g2000vba.googlegroups.com> References: <52b28a62-d0de-4d26-b2bd-dc4bf2affb71@p4g2000vba.googlegroups.com> Message-ID: <1fWdnY3Iu8y0MJzXnZ2dnUVZ_uqdnZ2d@pdx.net> OldGrantonian wrote: > I use Windows Vista Home Premium. I have Python 2.6 currently > installed. I'm not a techy. > > I want to use "virtualenv". When I try to install the latest version > of virtualenv for Windows, I get the message that Python 2.5 is not > found. > > How do I install Python 2.5 (or anything else except 2.6) so that I > use the correct version of Python at the correct time. > > For example, do I need to edit the "path" variable each time that I > want to change versions from 2.5 to 2.6? On windows, major versions (..., 2.5, 2.6, 2.7, ...., 3.0, 3.1, ...) can coexist quite successfully. Find and run an installer for the latest version of 2.5 (2.5.4, I believe) on the Python download page. That will make 2.5 your default Python. To switch the easiest way (for you) is uninstall the version of python you want to be the default, then install it again (making it the last one installed). This suggests keeping installer binaries for all version you are using. Note that the this uninstall-reinstall should not affect you loaded Python packages that you have installed separately. To simply run a python program with a different python (but not change the default), open a command window (run program / cmd), sometimes called a "terminal window" and use command: C:\> C:\Python24\python program.py # command line version or C:\> C:\Python24\pythonw program.pyw # gui verson You can also change shortcuts to refer to specific Python versions. Of course, the different Python versions should also be available on your start menu. However, Starting with Python 2.6, if you want user-specific installation, you can also create and put things in (for example), C:\Documents and Settings\USER\Application Data\ Python\Python26\site-packages where you replace USER with the user name in question. If you are not planning to do a lot of switching, that might be enough for you. The base directory is also called %APPDATA% on a command line or via os.path.expandvars. Once you have created this site-packages directory, Python 2.6 and later will use it to find .pth, .py, .pyw, and .pyd files (as well as .pyc and .pyo files). You may find changing your personal site-packages directory (and/or a .pth therein that you manipulate) will address the issues that you are planning to solve with virtualenv. Sorry for the long-winded answer. --Scott David Daniels Scott.Daniels at Acm.Org in addition to the common site-packages directory on From deets at nospam.web.de Wed May 6 11:57:46 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Wed, 06 May 2009 17:57:46 +0200 Subject: Pyhton script to call another program References: Message-ID: <76dq8pF1bisltU1@mid.uni-berlin.de> Ben Keshet wrote: > Hi, > > I am trying to write a simple python script to manipulate files and call > other programs. I have a program installed (rocs) which I run using > cygwin on my XP (but is not in python). Can I run the pyhton script and > then call the other program in the same script? > > For example: > Python Code # this line opens a file > Python Code # this line splits the file into 4 different files > rocs # this line calls the program 'rocs' to run on the newly formed > files Python Code # this line renames rocs output files and saves them in > the right place. > > Otherwise, I would have to write two scripts (before and after 'rocs'), > and run the 3 scripts/programs separately. > > I am relatively new to python and don't know a lot about cygwin. Please > remember that if you try to answer me :) Thanks! Check out the module "subprocess" in the standard library documentation. This will allow you to call other programs. Diez From Florian.Wollenschein at FernUni-Hagen.de Wed May 6 12:06:17 2009 From: Florian.Wollenschein at FernUni-Hagen.de (Florian Wollenschein) Date: Wed, 06 May 2009 18:06:17 +0200 Subject: thc v0.3 - txt to html converter - better code? In-Reply-To: References: <4a008996$0$31862$9b4e6d93@newsspool3.arcor-online.net> Message-ID: Richard Brodie wrote: > "Stefan Behnel" wrote in message > news:4a008996$0$31862$9b4e6d93 at newsspool3.arcor-online.net... > >> language_map = {'English': 'EN', 'Deutsch': 'DE'} >> strict_or_transitional = {True: 'Transitional', False: 'Strict'} >> >> # this will raise a KeyError for unknown languages >> language = language_map[ self.cmboBoxLang.currentText() ] >> >> # this assumes that isChecked() returns True or False >> spec = strict_or_transitional[self.rdioBtnTransitional.isChecked()] >> >> doctype = '\n' % ( >> spec, language) > > Incidentally, the language in an HTML DOCTYPE refers to the language of the DTD, not > the document. It's never correct to use //DE in an HTML page, unless you have a custom > (German) DTD. So the code can be improved further by cutting that part out. > > strict_or_transitional = {True: 'Transitional', False: 'Strict'} > > # this assumes that isChecked() returns True or False > spec = strict_or_transitional[self.rdioBtnTransitional.isChecked()] > > doctype = '\n' % spec > > Yes, that's true. I missed that. Thanks for the information. And many thanks to Stefan. From mauriceling at gmail.com Wed May 6 12:12:35 2009 From: mauriceling at gmail.com (Maurice Ling) Date: Wed, 6 May 2009 09:12:35 -0700 (PDT) Subject: Completion of The Python Papers Volume 4 Issue 1 References: <8b10356e-1a40-424b-b288-bbc3fa6f7d39@x29g2000prf.googlegroups.com> Message-ID: My apologies. It should be Volume 4 Issue 1 instead of Issue 4 Volume 1. Thank you Scott for pointing this out. Regards Maurice On May 6, 10:08 pm, "mauricel... at acm.org" wrote: > Welcome to Issue 4 Volume 1 of The Python Papers. This marks our 4th > year in business. It is my pleasure to announce 4 improvements made to > The Python Papers Anthology. > > Firstly, we had created a class of editorial members in our team ? the > Editorial Reviewers (ER). This is in addition to the existing Editors- > in-Chief (EIC) and Associate Editors (AE). In the words of our revised > editorial policy, ?ER is a tier of editors deemed to have in-depth > expertise knowledge in specialized areas. As members of the editorial > board, ERs are accorded editorial status but are generally not > involved in the strategic and routine operations of the periodicals > although their > expert opinions may be sought at the discretion of EIC.? To this > aspect, we are glad to have Jacob Kaplan-Moss (President, Django > Software Foundation; Partner, Revolution Systems, LLC; Co-BDFL, > Django) as our first ER. Welcome aboard. > > Secondly, we had adopted a fast publication scheme. In the past, > accepted manuscripts will only be published on the release date in the > month of April, August and December each year. With the use of Open > Journal System, we are able to publish manuscripts as they are being > accepted. This means that the release date in the month of April, > August and December is now effectively our closing date for the issue. > For example, manuscripts accepted after April 2009 will be routed to > immediate publication in Volume 4 Issue 2. This is to being > information faster to our readers, rather than waiting for a few > months. > > Thirdly, we announce a set of manuscript templates for our authors and > contributors for their manuscript preparation. As such, we had > revamped our website (http://www.pythonpapers.org) to reflect these > changes. > > Lastly, we are glad to announce the launch of our 2nd publication > under The Python Papers Anthology ? The Python Papers Source Codes > (ISSN 1836-621X). TPPSC, for short, is a collection of software and > source codes, usually associated with papers published in The Python > Papers and The Python Papers Monograph. These codes are refereed for > originality, accuracy, completeness, and lasting value. TPPSC will > act as a developers codebook of high-quality, peer-reviewed codes. > TPPSC is an annual publication using the same fast publication scheme. > > We thank all our readers and contributors for their continued support. > > Happy reading. > > Maurice Ling > Co-EIC, TPPA From vinay_sajip at yahoo.co.uk Wed May 6 12:14:41 2009 From: vinay_sajip at yahoo.co.uk (Vinay Sajip) Date: Wed, 6 May 2009 09:14:41 -0700 (PDT) Subject: pyinstaller and logging References: Message-ID: <93736f55-0177-4be7-a333-8eb850d4c321@x6g2000vbg.googlegroups.com> On May 6, 2:41 pm, Mike wrote: > Pyinstaller seems to have a problem withlogging... > > I installed pyinstaller 1.3 - using it together with Python 2.6. I > used pyinstaller for a small project, the created .exe worked fine. > After some additional changes to my project I got strange run time > errors when running the .exe (but no problems when running the .py > file directly with the python interpreter). I stripped down my program > and tracked the problem down to the first log message being written. > > So this is a stripped down version of the python script: > > importlogginglogging.basicConfig(level=logging.INFO, > format='%(asctime)-15s %(levelname)-8s %(message) > s', > datefmt='%Y-%m-%d %H:%M:%S', > filename="test1.log", > filemode='w') > print "written via print"logging.info("written vialogging") > > when I start this from the python shell I am getting > IDLE 2.6.2 ==== No Subprocess ==== > >>> > written via print > >>> > and test1.log contains > 2009-05-05 19:39:52 INFO written vialogging > as expected. > > When I start test1.exe I am getting > > written via print > Traceback (most recent call last): > File "", line 8, in > File "c:\dokumente und einstellungen\dieter\eigene dateien\collector > \v0r0m2\buildtest1\out1.pyz/logging", line 1451, in info > File "c:\dokumente und einstellungen\dieter\eigene dateien\collector > \v0r0m2\buildtest1\out1.pyz/logging", line 1030, in info > File "c:\dokumente und einstellungen\dieter\eigene dateien\collector > \v0r0m2\buildtest1\out1.pyz/logging", line 1142, in _log > File "c:\dokumente und einstellungen\dieter\eigene dateien\collector > \v0r0m2\buildtest1\out1.pyz/logging", line 1117, in makeRecord > File "c:\dokumente und einstellungen\dieter\eigene dateien\collector > \v0r0m2\buildtest1\out1.pyz/logging", line 272, in __init__ > File "C:\Programme\pyinstaller-1.3\iu.py", line 312, in importHook > mod = _self_doimport(nm, ctx, fqname) > File "C:\Programme\pyinstaller-1.3\iu.py", line 398, in doimport > exec co in mod.__dict__ > File "c:\dokumente und einstellungen\dieter\eigene dateien\collector > \v0r0m2\buildtest1\out1.pyz/multiprocessing", line 83, in > File "C:\Programme\pyinstaller-1.3\iu.py", line 312, in importHook > mod = _self_doimport(nm, ctx, fqname) > File "C:\Programme\pyinstaller-1.3\iu.py", line 382, in doimport > mod = director.getmod(nm) > File "C:\Programme\pyinstaller-1.3\iu.py", line 215, in getmod > mod = owner.getmod(nm) > File "C:\Programme\pyinstaller-1.3\iu.py", line 77, in getmod > mod = imp.load_module(nm, fp, attempt, (ext, mode, typ)) > TypeError: importHook() takes at most 5 arguments (6 given) > > What am I doing wrong? Apparently, PyInstaller only supports versions of Python between 1.5 and 2.4: http://www.pyinstaller.org/#Requirements So, you are using it in an unsupported environment. I see you've cross- posted this to the PyInstaller mailing list: http://groups.google.com/group/PyInstaller I hope someone on that list can help. An additional bit of information from your traceback is that line 272 in 2.6's logging/__init__.py is one that says "from multiprocessing import current_process". As this line is only executed if logging.logMultiProcessing is set, you may be able to work around by setting logging.logMultiProcessing to False in your app initialisation. Of course, this might just result in a failure somewhere else... Regards, Vinay Sajip From OldGrantonian at googlemail.com Wed May 6 12:24:56 2009 From: OldGrantonian at googlemail.com (OldGrantonian) Date: Wed, 6 May 2009 09:24:56 -0700 (PDT) Subject: Separate Windows versions of Python References: <52b28a62-d0de-4d26-b2bd-dc4bf2affb71@p4g2000vba.googlegroups.com> <1fWdnY3Iu8y0MJzXnZ2dnUVZ_uqdnZ2d@pdx.net> Message-ID: Many thanks for the very detailed answer :) I will go ahead right now and implement all your suggestions. On May 6, 4:35?pm, Scott David Daniels wrote: > OldGrantonian wrote: > > I use Windows Vista Home Premium. I have Python 2.6 currently > > installed. I'm not a techy. > > > I want to use "virtualenv". When I try to install the latest version > > of virtualenv for Windows, I get the message that Python 2.5 is not > > found. > > > How do I install Python 2.5 (or anything else except 2.6) so that I > > use the correct version of Python at the correct time. > > > For example, do I need to edit the "path" variable each time that I > > want to change versions from 2.5 to 2.6? > > On windows, major versions (..., 2.5, 2.6, 2.7, ...., 3.0, 3.1, ?...) > can coexist quite successfully. ?Find and run an installer for the > latest version ?of 2.5 (2.5.4, I believe) on the Python download page. > That will make 2.5 your default Python. ?To switch the easiest way > (for you) is uninstall the version of python you want to be the > default, then install it again (making it the last one installed). > This suggests keeping installer binaries for all version you are > using. ?Note that the this uninstall-reinstall should not affect > you loaded Python packages that you have installed separately. > To simply run a python program with a different python (but not > change the default), open a command window (run program / cmd), > sometimes called a "terminal window" and use command: > ? ? ?C:\> C:\Python24\python program.py ?# command line version > or > ? ? ?C:\> C:\Python24\pythonw program.pyw ?# gui verson > You can also change shortcuts to refer to specific Python versions. > > Of course, the different Python versions should also be available > on your start menu. > > However, Starting with Python 2.6, if you want user-specific > installation, you can also create and put things in (for example), > ? ? ?C:\Documents and Settings\USER\Application Data\ > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Python\Python26\site-packages > where you replace USER with the user name in question. > > If you are not planning to do a lot of switching, that might be > enough for you. ?The base directory is also called %APPDATA% on > a command line or via os.path.expandvars. ?Once you have created > this site-packages directory, Python 2.6 and later will use it to > find .pth, .py, .pyw, and .pyd files (as well as .pyc and .pyo > files). ?You may find changing your personal site-packages directory > (and/or a .pth therein that you manipulate) will address the issues > that you are planning to solve with virtualenv. > > Sorry for the long-winded answer. > --Scott David Daniels > Scott.Dani... at Acm.Org > > in addition to the common site-packages directory on From damienlmoore at gmail.com Wed May 6 12:51:57 2009 From: damienlmoore at gmail.com (spillz) Date: Wed, 6 May 2009 09:51:57 -0700 (PDT) Subject: Problem with case insensitive volumes References: <26c57bf5-5ad2-4838-8097-c8d51cf1ccae@n8g2000vbb.googlegroups.com> Message-ID: <7761aa9c-2d0f-4429-9b02-766b315c0c80@o30g2000vbc.googlegroups.com> On May 5, 10:24?pm, "Gabriel Genellina" wrote: > I use this function on Windows to obtain the true case name of a file: very nice. I think the python bindings to the gnome GIO library *might* provide what I need on the linux side. From tornadomaker at gmx.net Wed May 6 13:22:32 2009 From: tornadomaker at gmx.net (Mike) Date: Wed, 6 May 2009 10:22:32 -0700 (PDT) Subject: pyinstaller and logging References: <93736f55-0177-4be7-a333-8eb850d4c321@x6g2000vbg.googlegroups.com> Message-ID: On 6 Mai, 18:14, Vinay Sajip wrote: > On May 6, 2:41 pm, Mike wrote: > > > > > Pyinstaller seems to have a problem withlogging... > > > ?I installed pyinstaller 1.3 - using it together with Python 2.6. I > > used pyinstaller for a small project, the created .exe worked fine. > > After some additional changes to my project I got strange run time > > errors when running the .exe (but no problems when running the .py > > file directly with the python interpreter). I stripped down my program > > and tracked the problem down to the first log message being written. > > > So this is a stripped down version of the python script: > > > importlogginglogging.basicConfig(level=logging.INFO, > > ? ? ? ? ? ? ? ? ? ? format='%(asctime)-15s %(levelname)-8s %(message) > > s', > > ? ? ? ? ? ? ? ? ? ? datefmt='%Y-%m-%d %H:%M:%S', > > ? ? ? ? ? ? ? ? ? ? filename="test1.log", > > ? ? ? ? ? ? ? ? ? ? filemode='w') > > print "written via print"logging.info("written vialogging") > > > when I start this from the python shell I am getting > > ? ? ? ? ? ?IDLE 2.6.2 ? ? ?==== No Subprocess ==== > > > ? ? ? ? ? ?written via print > > > and test1.log contains > > ? ? ? ? ? ?2009-05-05 19:39:52 INFO ? ? written vialogging > > as expected. > > > When I start test1.exe I am getting > > > written via print > > Traceback (most recent call last): > > ? File "", line 8, in > > ? File "c:\dokumente und einstellungen\dieter\eigene dateien\collector > > \v0r0m2\buildtest1\out1.pyz/logging", line 1451, in info > > ? File "c:\dokumente und einstellungen\dieter\eigene dateien\collector > > \v0r0m2\buildtest1\out1.pyz/logging", line 1030, in info > > ? File "c:\dokumente und einstellungen\dieter\eigene dateien\collector > > \v0r0m2\buildtest1\out1.pyz/logging", line 1142, in _log > > ? File "c:\dokumente und einstellungen\dieter\eigene dateien\collector > > \v0r0m2\buildtest1\out1.pyz/logging", line 1117, in makeRecord > > ? File "c:\dokumente und einstellungen\dieter\eigene dateien\collector > > \v0r0m2\buildtest1\out1.pyz/logging", line 272, in __init__ > > ? File "C:\Programme\pyinstaller-1.3\iu.py", line 312, in importHook > > ? ? mod = _self_doimport(nm, ctx, fqname) > > ? File "C:\Programme\pyinstaller-1.3\iu.py", line 398, in doimport > > ? ? exec co in mod.__dict__ > > ? File "c:\dokumente und einstellungen\dieter\eigene dateien\collector > > \v0r0m2\buildtest1\out1.pyz/multiprocessing", line 83, in > > ? File "C:\Programme\pyinstaller-1.3\iu.py", line 312, in importHook > > ? ? mod = _self_doimport(nm, ctx, fqname) > > ? File "C:\Programme\pyinstaller-1.3\iu.py", line 382, in doimport > > ? ? mod = director.getmod(nm) > > ? File "C:\Programme\pyinstaller-1.3\iu.py", line 215, in getmod > > ? ? mod = owner.getmod(nm) > > ? File "C:\Programme\pyinstaller-1.3\iu.py", line 77, in getmod > > ? ? mod = imp.load_module(nm, fp, attempt, (ext, mode, typ)) > > TypeError: importHook() takes at most 5 arguments (6 given) > > > What am I doing wrong? > > Apparently, PyInstaller only supports versions of Python between 1.5 > and 2.4: > > http://www.pyinstaller.org/#Requirements > > So, you are using it in an unsupported environment. I see you've cross- > posted this to the PyInstaller mailing list: > > http://groups.google.com/group/PyInstaller > > I hope someone on that list can help. An additional bit of information > from your traceback is that line 272 in 2.6's logging/__init__.py is > one that says "from multiprocessing import current_process". As this > line is only executed if logging.logMultiProcessing is set, you may be > able to work around by setting logging.logMultiProcessing to False in > your app initialisation. Of course, this might just result in a > failure somewhere else... > > Regards, > > Vinay Sajip Shame on me - I somehow skipped the requirement from pyinstaller just supporting python between 1.5 and 2.4 ... - thanks for your help - I was aware it is not supporting python 3.0 (I searched a tool creating .exes for Python 3.0 in the first place). I googled around and found some recommendations for pyinstaller, but with the restriction "it is just supporting the latest version of 2.x but not 3.0 of python". I never double checked on the formal requirements on the pyinstaller site... . Your hint regarding logging.logMultiProcessing=False did not change the problem. As I do not want to switch to python 2.4 I will have to look for something else. I tried py2exe today, which seems to work fine for my project (and officially supports python 2.6 ;-) ) - it is able to create an exe file within a dist directory, but not a single executable like pyinstaller is doing it. But this is fine enough for me just now. From norseman at hughes.net Wed May 6 13:47:21 2009 From: norseman at hughes.net (norseman) Date: Wed, 06 May 2009 10:47:21 -0700 Subject: Pyhton script to call another program In-Reply-To: <4A01AD6E.4010807@umbc.edu> References: <4A01AD6E.4010807@umbc.edu> Message-ID: <4A01CD29.3010507@hughes.net> Ben Keshet wrote: > Hi, > > I am trying to write a simple python script to manipulate files and call > other programs. I have a program installed (rocs) which I run using > cygwin on my XP (but is not in python). Can I run the pyhton script and > then call the other program in the same script? > > For example: > Python Code # this line opens a file > Python Code # this line splits the file into 4 different files > rocs # this line calls the program 'rocs' to run on the newly formed files > Python Code # this line renames rocs output files and saves them in the > right place. > > Otherwise, I would have to write two scripts (before and after 'rocs'), > and run the 3 scripts/programs separately. > I am relatively new to python and don't know a lot about cygwin. Please > remember that if you try to answer me :) Thanks! > -- > http://mail.python.org/mailman/listinfo/python-list > ========================================== If you don't like a lot of typing that obscures the process, take a look at the spawn family. processPython 2.5.2 (r252:60911, Mar 4 2008, 10:40:55) [GCC 3.3.6] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import os >>> AbWd = os.spawnlp( os.P_WAIT,"abiword","abiword","") The P_WAIT stops python until the program (abiword in this case) completes. The "" at the end are for tokens to be given to the program and yes - contrary to manual, the program MUST be there TWICE (complete with any path needed). for windows this works: (can't cut and paste from a dos box!###%*&!!!) Python 2.5.1 ... on win32 >>> import os >>> result = os.spawnl( os.P_WAIT, "d:\\winmcad\\mcad","") Runs the program mcad. Returns to python when mcad exits. Today: 20090506 Python ver as snippets show. OS is Linux Slackware 10.2 OS is Windows XP Pro Steve From Florian.Wollenschein at FernUni-Hagen.de Wed May 6 13:48:11 2009 From: Florian.Wollenschein at FernUni-Hagen.de (Florian Wollenschein) Date: Wed, 06 May 2009 19:48:11 +0200 Subject: hex(dummy)[2:] - issue... Message-ID: Hi there, I need some advice :-) I'm using hex(dummy)[2:] to represent a color in hexadecimal format for the bgcolor in an html file. dummy is the color value in RGB of course... Now, if there's an R, G or B value of zero, this command only prints one single 0 instead of two. What's wrong with the code? Thanks, Listick http://www.listick.org From 71david at libero.it Wed May 6 13:49:57 2009 From: 71david at libero.it (David) Date: Wed, 6 May 2009 19:49:57 +0200 Subject: Pyhton script to call another program References: Message-ID: <1crn8u0lutm6h$.1qt709r9zm842.dlg@40tude.net> Il Wed, 06 May 2009 11:31:58 -0400, Ben Keshet ha scritto: > Hi, > > I am trying to write a simple python script to manipulate files and call > other programs. I have a program installed (rocs) which I run using > cygwin on my XP (but is not in python). Can I run the pyhton script and > then call the other program in the same script? Look at the os.system function call. D. From python.list at tim.thechases.com Wed May 6 13:54:13 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Wed, 06 May 2009 12:54:13 -0500 Subject: Copy & Paste in a Dos box (was: Pyhton script to call another program) In-Reply-To: <4A01CD29.3010507@hughes.net> References: <4A01AD6E.4010807@umbc.edu> <4A01CD29.3010507@hughes.net> Message-ID: <4A01CEC5.4090008@tim.thechases.com> > for windows this works: > (can't cut and paste from a dos box!###%*&!!!) Depending on how it was spawned, you can either right-click in the window and choose Mark/Paste (when marking, use to terminate the selection; and selections are blockwise rectangular rather than linewise or characterwise). Alternatively, if you click on the system menu (the icon in the title-bar with resize/minimize/maximize/close/help), there should be an Edit submenu with the same options within. A pain, but at least feasible. HTH, -tkc From norseman at hughes.net Wed May 6 14:02:06 2009 From: norseman at hughes.net (norseman) Date: Wed, 06 May 2009 11:02:06 -0700 Subject: Which one is best Python or Java for developing GUI applications? In-Reply-To: References: <793408.86105.qm@web7903.mail.in.yahoo.com> Message-ID: <4A01D09E.3020304@hughes.net> Tim Rowe wrote: > 2009/5/6 Dennis Lee Bieber : > >> (the "near" is because I feel Ada is >> stricter than any other language) > > Try SPARK -- it's Ada based, but /much/ stricter. It's just right for > some really critical stuff, but is no sort of an answer to "Which one > is best Python or Java for developing GUI"! > ========================== "...really critical stuff..." Then the only real choice is assembly! And correct - that's nothing to do with Python vs Java. Back to the question: Python has one advantage that has not been mentioned. It allows the programmer to start with prior coding practices and 'ease' into OOP because Python happily allows a mix of traditional and OOP coding. Beyond that - "There is no accounting for taste." You are on your own. You see - with daughter #1 I need pencil and paper to explain things. with daughter #2 I sit on my hands and narrate correctly the first time. Many things have no "one is better" option beyond individual quirks. Or shall we say preferences? The old - "This works for me" is right one. Steve From wzab01 at gmail.com Wed May 6 14:03:57 2009 From: wzab01 at gmail.com (wzab) Date: Wed, 6 May 2009 11:03:57 -0700 (PDT) Subject: IIR filter conversion routines for Python? Message-ID: Hi, I'm looking for procedures converting the IIR filters into cascade and/ or parallel forms. Something like dir2cas.m or dir2par.m known in the Matlab/Octave world. Unfortunately SciPy does not contain such functions. If they are not available, I would be grateful for any hints helping me to implement them myself. -- TIA, BR, WZab From Shawn at Milochik.com Wed May 6 14:12:53 2009 From: Shawn at Milochik.com (Shawn Milochik) Date: Wed, 6 May 2009 14:12:53 -0400 Subject: Copy & Paste in a Dos box (was: Pyhton script to call another program) In-Reply-To: <4A01CEC5.4090008@tim.thechases.com> References: <4A01AD6E.4010807@umbc.edu> <4A01CD29.3010507@hughes.net> <4A01CEC5.4090008@tim.thechases.com> Message-ID: <2dc0c81b0905061112l32426cb8rafa3a86dca12810d@mail.gmail.com> On Wed, May 6, 2009 at 1:54 PM, Tim Chase wrote: >> for windows this works: >> (can't cut and paste from a dos box!###%*&!!!) > > Depending on how it was spawned, you can either right-click in the window > and choose Mark/Paste (when marking, use to terminate the selection; > and selections are blockwise rectangular rather than linewise or > characterwise). ?Alternatively, if you click on the system menu (the icon in > the title-bar with resize/minimize/maximize/close/help), there should be an > Edit submenu with the same options within. > > A pain, but at least feasible. I know I'm coming to the conversation late, but here's what I do*: 1. Use Cygwin. (http://www.cygwin.com/) 2. Use PuttyCYG (http://code.google.com/p/puttycyg/) That way, you can basically use PuTTY to shell into your Windows box. Note: If you are familiar with the Linux command line, you will be in love with this solution. If you're a Windows-only type, then be forewarned that doing this will require you to type Unix/Linux commands (for the most part) instead of DOS commands. *Disclaimer: I use a Mac and Linux. This is my workaround when forced to use Windows. Your environment preferences may vary. From python.list at tim.thechases.com Wed May 6 14:13:23 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Wed, 06 May 2009 13:13:23 -0500 Subject: hex(dummy)[2:] - issue... In-Reply-To: References: Message-ID: <4A01D343.8000800@tim.thechases.com> > I need some advice :-) > I'm using hex(dummy)[2:] to represent a color in hexadecimal format for > the bgcolor in an html file. dummy is the color value in RGB of course... > > Now, if there's an R, G or B value of zero, this command only prints one > single 0 instead of two. What's wrong with the code? You can try PLACES = 2 # 6? hex(dummy)[2:].zfill(PLACES) Alternatively, you can output decimal numbers in HTML/CSS with rgb(r, g, b) such as style="rgb(255,0,0)" However, I recommend doing this via CSS unless you have a strong reason to sully your HTML with style information. -tkc From stermen975 at gmail.com Wed May 6 14:27:18 2009 From: stermen975 at gmail.com (stermen) Date: Wed, 06 May 2009 18:27:18 GMT Subject: Iphone Going 3G! In-Reply-To: <49081152_1@news.tm.net.my> References: <486cab43$1_2@news.tm.net.my> <20fqou.nq0.19.1@news.alt.net> <48df149c_1@news.tm.net.my> <49081152_1@news.tm.net.my> Message-ID: Notebooks stocks priced http://rover.ebay.com/rover/1/711-53200-19255-0/1?type=3&campid=5336229480&toolid=10001&customid=&ext=notebook&satitle=notebook "Shafiq" ha scritto nel messaggio news:49081152_1 at news.tm.net.my... > apalah kau ni tak belajar ke bahasa menunjukkan bangsa > "news.tm.net.my" wrote in message > news:48df149c_1 at news.tm.net.my... >> relic wrote: >>> fizzi wrote: >>>> For the Iphone lovers out there, >>> >>> Wrong group. No weirdos here. >> testtt > > From google at mrabarnett.plus.com Wed May 6 14:28:02 2009 From: google at mrabarnett.plus.com (MRAB) Date: Wed, 06 May 2009 19:28:02 +0100 Subject: hex(dummy)[2:] - issue... In-Reply-To: References: Message-ID: <4A01D6B2.8060805@mrabarnett.plus.com> Florian Wollenschein wrote: > Hi there, > > I need some advice :-) > I'm using hex(dummy)[2:] to represent a color in hexadecimal format for > the bgcolor in an html file. dummy is the color value in RGB of course... > > Now, if there's an R, G or B value of zero, this command only prints one > single 0 instead of two. What's wrong with the code? > hex() returns '0x' followed by no more digits than are necessary: >>> hex(0xFF) '0xff' >>> hex(0xF) '0xf' Try "%02X" instead (it'll pad with leading zeros up to a width of 2): >>> "%02X" % 0xFF 'FF' >>> "%02X" % 0xF '0F' From iainemsley at googlemail.com Wed May 6 14:32:28 2009 From: iainemsley at googlemail.com (iainemsley) Date: Wed, 6 May 2009 11:32:28 -0700 (PDT) Subject: Parsing text Message-ID: <37255869-6cc8-44c4-8b13-6376604df842@h23g2000vbc.googlegroups.com> Hi, I'm trying to write a fairly basic text parser to split up scenes and acts in plays to put them into XML. I've managed to get the text split into the blocks of scenes and acts and returned correctly but I'm trying to refine this and get the relevant scene number when the split is made but I keep getting an NoneType error trying to read the block inside the for loop and nothing is being returned. I'd be grateful for some suggestions as to how to get this working. for scene in text.split('Scene'): num = re.compile("^\s\[0-9, i{1,4}, v]", re.I) textNum = num.match(scene) if textNum: print textNum else: print "No scene number" m = '
References: <4A01AD6E.4010807@umbc.edu> <4A01CD29.3010507@hughes.net> <4A01CEC5.4090008@tim.thechases.com> <-ZOdnagfEPjuRJzXnZ2dnUVZ_omdnZ2d@posted.visi> Message-ID: <2dc0c81b0905061147v55027b08t3a5fafb0ebea81f9@mail.gmail.com> On Wed, May 6, 2009 at 2:39 PM, Grant Edwards wrote: > On 2009-05-06, Shawn Milochik wrote: > >> I know I'm coming to the conversation late, but here's what I do*: >> >> 1. Use Cygwin. (http://www.cygwin.com/) >> 2. Use PuttyCYG (http://code.google.com/p/puttycyg/) >> >> That way, you can basically use PuTTY to shell into your >> Windows box. > > Better yet, set up sshd in your Cygwin install, and then use > whatever terminal you normally use on your Linux/MacOS box to > ssh into the Cygwin box. ?When run that way, windows is almost > usable... > > -- > Grant Edwards True, but when I'm using Cygwin, that means I'm at work and don't have a non-MS OS available. Of course I can open an ssh session to my home machine for sanity (or kicking off a torrent download at home), but I don't have a *nix-based OS at the day job. Lanching DSL embedded to use the terminal seems a bit much. ^_^ Shawn From Shawn at Milochik.com Wed May 6 14:49:49 2009 From: Shawn at Milochik.com (Shawn Milochik) Date: Wed, 6 May 2009 14:49:49 -0400 Subject: Parsing text In-Reply-To: <37255869-6cc8-44c4-8b13-6376604df842@h23g2000vbc.googlegroups.com> References: <37255869-6cc8-44c4-8b13-6376604df842@h23g2000vbc.googlegroups.com> Message-ID: <2dc0c81b0905061149p4e2f4f07y7ad6168c491f6244@mail.gmail.com> On Wed, May 6, 2009 at 2:32 PM, iainemsley wrote: > Hi, > I'm trying to write a fairly basic text parser to split up scenes and > acts in plays to put them into XML. I've managed to get the text split > into the blocks of scenes and acts and returned correctly but I'm > trying to refine this and get the relevant scene number when the split > is made but I keep getting an NoneType error trying to read the block > inside the for loop and nothing is being returned. I'd be grateful for > some suggestions as to how to get this working. > > for scene in text.split('Scene'): > ? ?num = re.compile("^\s\[0-9, i{1,4}, v]", re.I) > ? ?textNum = num.match(scene) > ? ?if textNum: > ? ? ? ?print textNum > ? ?else: > ? ? ? ?print "No scene number" > ? ?m = '
%s
' % (scene,) From Scott.Daniels at Acm.Org Wed May 6 14:50:39 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Wed, 06 May 2009 11:50:39 -0700 Subject: Parsing text In-Reply-To: <37255869-6cc8-44c4-8b13-6376604df842@h23g2000vbc.googlegroups.com> References: <37255869-6cc8-44c4-8b13-6376604df842@h23g2000vbc.googlegroups.com> Message-ID: iainemsley wrote: > Hi, > I'm trying to write a fairly basic text parser to split up scenes and > acts in plays to put them into XML. I've managed to get the text split > into the blocks of scenes and acts and returned correctly but I'm > trying to refine this and get the relevant scene number when the split > is made but I keep getting an NoneType error trying to read the block > inside the for loop and nothing is being returned. I'd be grateful for > some suggestions as to how to get this working. > > ...(some code)... You'll get a lot better help if you: (1) Include enough code to run and encounter the problem. Edit this down to something small (in the process, you may discover what was wrong). (2) Include actual sample data demonstrating the problem. and (3) Cut and paste the _actual_ error message and traceback from your output when running the sample code with the sample data. For extra points, identify the Python version you are using. --Scott David Daniels Scott.Daniels at Acm.Org From google at mrabarnett.plus.com Wed May 6 14:55:21 2009 From: google at mrabarnett.plus.com (MRAB) Date: Wed, 06 May 2009 19:55:21 +0100 Subject: Parsing text In-Reply-To: <37255869-6cc8-44c4-8b13-6376604df842@h23g2000vbc.googlegroups.com> References: <37255869-6cc8-44c4-8b13-6376604df842@h23g2000vbc.googlegroups.com> Message-ID: <4A01DD19.3020106@mrabarnett.plus.com> iainemsley wrote: > Hi, > I'm trying to write a fairly basic text parser to split up scenes and > acts in plays to put them into XML. I've managed to get the text split > into the blocks of scenes and acts and returned correctly but I'm > trying to refine this and get the relevant scene number when the split > is made but I keep getting an NoneType error trying to read the block > inside the for loop and nothing is being returned. I'd be grateful for > some suggestions as to how to get this working. > > for scene in text.split('Scene'): > num = re.compile("^\s\[0-9, i{1,4}, v]", re.I) > textNum = num.match(scene) > if textNum: > print textNum > else: > print "No scene number" > m = '
Of course I can open an ssh session to my home machine for > sanity (or kicking off a torrent download at home), but I > don't have a *nix-based OS at the day job. Lanching DSL > embedded to use the terminal seems a bit much. ^_^ Probably so. -- Grant Edwards grante Yow! I want a VEGETARIAN at BURRITO to go ... with visi.com EXTRA MSG!! From python.list at tim.thechases.com Wed May 6 14:59:19 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Wed, 06 May 2009 13:59:19 -0500 Subject: Parsing text In-Reply-To: <37255869-6cc8-44c4-8b13-6376604df842@h23g2000vbc.googlegroups.com> References: <37255869-6cc8-44c4-8b13-6376604df842@h23g2000vbc.googlegroups.com> Message-ID: <4A01DE07.1060309@tim.thechases.com> > I'm trying to write a fairly basic text parser to split up scenes and > acts in plays to put them into XML. I've managed to get the text split > into the blocks of scenes and acts and returned correctly but I'm > trying to refine this and get the relevant scene number when the split > is made but I keep getting an NoneType error trying to read the block > inside the for loop and nothing is being returned. I'd be grateful for > some suggestions as to how to get this working. > > for scene in text.split('Scene'): > num = re.compile("^\s\[0-9, i{1,4}, v]", re.I) The first thing that occurs to me is that this should likely be a raw string to get those backslashes into the regexp. Compare: print "^\s\[0-9, i{1,4}, v]" print r"^\s\[0-9, i{1,4}, v]" Without an excerpt of the actual text (or at least the lead-in for each scene), it's hard to tell whether this regex finds what you expect. It doesn't look like your regexp finds what you may think it does (it looks like you're using commas . Just so you're aware, your split is a bit fragile too, in case any lines contain "Scene". However, with a proper regexp, you can even use it to split the scenes *and* tag the scene-number. Something like >>> import re >>> s = """Scene [42] ... this is stuff in the 42nd scene ... Scene [IIV] ... stuff in the other scene ... """ >>> r = re.compile(r"Scene\s+\[(\d+|[ivx]+)]", re.I) >>> r.split(s)[1:] ['42', '\nthis is stuff in the 42nd scene\n', 'IIV', '\nstuff in the other scene\n'] >>> def grouper(iterable, groupby): ... iterable = iter(iterable) ... while True: ... yield [iterable.next() for _ in range(groupby)] ... >>> for scene, content in grouper(r.split(s)[1:], 2): ... print "

%s

%s

" % (scene, content) ...

42

this is stuff in the 42nd scene

IIV

stuff in the other scene

Play accordingly. -tkc From stefan_ml at behnel.de Wed May 6 15:05:04 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Wed, 06 May 2009 21:05:04 +0200 Subject: Parsing text In-Reply-To: <37255869-6cc8-44c4-8b13-6376604df842@h23g2000vbc.googlegroups.com> References: <37255869-6cc8-44c4-8b13-6376604df842@h23g2000vbc.googlegroups.com> Message-ID: <4a01df61$0$30226$9b4e6d93@newsspool1.arcor-online.net> iainemsley wrote: > for scene in text.split('Scene'): > num = re.compile("^\s\[0-9, i{1,4}, v]", re.I) > textNum = num.match(scene) Not related to your problem, but to your code - I'd write this as follows: match_scene_num = re.compile("^\s\[0-9, i{1,4}, v]", re.I).match for scene_section in text.split('Scene'): text_num = match_scene_num(scene_section) This makes the code more readable and avoids unnecessary work inside the loop. Stefan From tjreedy at udel.edu Wed May 6 15:28:04 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 06 May 2009 15:28:04 -0400 Subject: Separate Windows versions of Python In-Reply-To: <1fWdnY3Iu8y0MJzXnZ2dnUVZ_uqdnZ2d@pdx.net> References: <52b28a62-d0de-4d26-b2bd-dc4bf2affb71@p4g2000vba.googlegroups.com> <1fWdnY3Iu8y0MJzXnZ2dnUVZ_uqdnZ2d@pdx.net> Message-ID: Scott David Daniels wrote: > On windows, major versions (..., 2.5, 2.6, 2.7, ...., 3.0, 3.1, ...) > can coexist quite successfully. Find and run an installer for the > latest version of 2.5 (2.5.4, I believe) on the Python download page. > That will make 2.5 your default Python. believe the installer gives one the choice as to whether or not to make the new installation the default -- which is to say, the one registered with .py, etc, extensions. From philip at subaru.naoj.org Wed May 6 16:03:09 2009 From: philip at subaru.naoj.org (Philip) Date: Wed, 6 May 2009 20:03:09 +0000 (UTC) Subject: fcntl and =?utf-8?b?c2lnaW5mb190?= in python References: <148918f0904281356v534680dcmd03084fe255cb2d3@mail.gmail.com> <148918f0904291012j4c1ada35jcee6030b75b952d0@mail.gmail.com> <148918f0904301637t5e63537atd85e2a475f4e069f@mail.gmail.com> <148918f0905011208u4946c377uce7603bb2e456370@mail.gmail.com> <148918f0905040903s7a5c7e5exc2968d0e65540d81@mail.gmail.com> Message-ID: ma gmail.com> writes: > > Ok! So, I decided to write a C-extension instead of using ctypes... > > This works beautifully. Now, I want to release this to the public, so > I'm thinking of making a bit of code cleanup. Should I just pack the > entire siginfo_t struct, right now I just use the fd, into a > dictionary and pass it to the python callback handler function? Maybe > there might be some more suggestions to what data structures to use, > so I'm open right now to any of them. Could we have a look at your working prototype? Philip J. Tait http://subarutelescope.org From mensanator at aol.com Wed May 6 16:13:55 2009 From: mensanator at aol.com (Mensanator) Date: Wed, 6 May 2009 13:13:55 -0700 (PDT) Subject: Copy & Paste in a Dos box (was: Pyhton script to call another program) References: <4A01AD6E.4010807@umbc.edu> <4A01CD29.3010507@hughes.net> Message-ID: On May 6, 12:54 pm, Tim Chase wrote: > > for windows this works: > > (can't cut and paste from a dos box!###%*&!!!) > > Depending on how it was spawned, you can either right-click in > the window and choose Mark/Paste (when marking, use to > terminate the selection; and selections are blockwise rectangular > rather than linewise or characterwise). Alternatively, if you > click on the system menu (the icon in the title-bar with > resize/minimize/maximize/close/help), there should be an Edit > submenu with the same options within. > > A pain, but at least feasible. Less painful is to set the properties of the dos box: Edit Options: [x] Quick Edit mode And when prompted, do "(.) modify shortcut that started this window" After which, you can dispense with the menus (except when pasting), just select the text and hit . Makes a world of difference. > > HTH, > > -tkc From davea at ieee.org Wed May 6 16:29:23 2009 From: davea at ieee.org (Dave Angel) Date: Wed, 06 May 2009 16:29:23 -0400 Subject: Copy & Paste in a Dos box (was: Pyhton script to call another program) In-Reply-To: <4A01CEC5.4090008@tim.thechases.com> References: <4A01AD6E.4010807@umbc.edu> <4A01CD29.3010507@hughes.net> <4A01CEC5.4090008@tim.thechases.com> Message-ID: <4A01F323.4040808@ieee.org> Tim Chase wrote: >
> for > windows this works: >> (can't cut and paste from a dos box!###%*&!!!) > > Depending on how it was spawned, you can either right-click in the > window and choose Mark/Paste (when marking, use to terminate > the selection; and selections are blockwise rectangular rather than > linewise or characterwise). Alternatively, if you click on the system > menu (the icon in the title-bar with > resize/minimize/maximize/close/help), there should be an Edit submenu > with the same options within. > > A pain, but at least feasible. > > HTH, > > -tkc > > Note that if you like copy/paste, you can turn on "Quick Edit" mode in your cmd.exe windows. From that same System menu on your DOS box, choose "Defaults". From the first tab that box, enable "Quick Edit" checkbox. This will change the default for subsequent DOS boxes. You can change it just for the current one by using Properties in the same system menu. From davea at ieee.org Wed May 6 16:46:12 2009 From: davea at ieee.org (Dave Angel) Date: Wed, 06 May 2009 16:46:12 -0400 Subject: Copy & Paste in a Dos box (was: Pyhton script to call another program) In-Reply-To: References: <4A01AD6E.4010807@umbc.edu> <4A01CD29.3010507@hughes.net> Message-ID: <4A01F714.4020602@ieee.org> Mensanator wrote: > > > And when prompted, do "(.) modify shortcut that started this window" > > After which, you can dispense with the menus (except when pasting), > just select the text and hit . > > To paste into a DOS box, once Quick Edit is enabled, use Right-Click. They keystrokes will be sent to the command editor. Note that the interpretation is rather literal, so be careful if copy/pasting more than one line, or a line that was wrapped. From emile at fenx.com Wed May 6 17:04:44 2009 From: emile at fenx.com (Emile van Sebille) Date: Wed, 06 May 2009 14:04:44 -0700 Subject: list comprehension question In-Reply-To: <85d4am8hq4.fsf@agentultra.com> References: <6d502cfd-e252-4722-96a5-c52fc3579499@v1g2000prd.googlegroups.com> <85ocucnbce.fsf@agentultra.com> <85ljpb8r1o.fsf@agentultra.com> <85d4am8hq4.fsf@agentultra.com> Message-ID: On 5/6/2009 6:48 AM J Kenneth King said... > Emile van Sebille writes: >> On 5/5/2009 9:15 AM J Kenneth King said... >>> The Python documentation discourages their use and I believe >>> for good reason. >> Can you provide a link for this? I'd like to see specifically what's >> being discouraged, as I'd be surprised to find routine usage frowned >> upon. > http://docs.python.org/tutorial/datastructures.html#nested-list-comprehensions > Thanks. > > "If you?ve got the stomach for it, list comprehensions can be > nested. They are a powerful tool but ? like all powerful tools ? they > need to be used carefully, if at all." Yes, although this is in reference to nested list comprehensions. Eg, [xx for xx in [yy for yy in [zz for zz in iterable if z] if y] if x] The section above on list comprehensions advocates their use saying: "List comprehensions provide a concise way to create lists without resorting to use of map(), filter() and/or lambda. The resulting list definition tends often to be clearer than lists built using those constructs." Emile From rhodri at wildebst.demon.co.uk Wed May 6 17:15:03 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Wed, 06 May 2009 22:15:03 +0100 Subject: Parsing text In-Reply-To: <37255869-6cc8-44c4-8b13-6376604df842@h23g2000vbc.googlegroups.com> References: <37255869-6cc8-44c4-8b13-6376604df842@h23g2000vbc.googlegroups.com> Message-ID: On Wed, 06 May 2009 19:32:28 +0100, iainemsley wrote: > Hi, > I'm trying to write a fairly basic text parser to split up scenes and > acts in plays to put them into XML. I've managed to get the text split > into the blocks of scenes and acts and returned correctly but I'm > trying to refine this and get the relevant scene number when the split > is made but I keep getting an NoneType error trying to read the block > inside the for loop and nothing is being returned. I'd be grateful for > some suggestions as to how to get this working. With neither a sample of your data nor the traceback you get, this is going to require some crystal ball work. Assuming that all you've got is running text, I should warn you now that getting this right is a hard task. Getting it apparently right and having it fall over in a heap or badly mangle the text is, unfortunately, very easy. > for scene in text.split('Scene'): Not a safe start. This will split on the word "Scenery" as well, for example, and doesn't guarantee you the start of a scene by a long way. > num = re.compile("^\s\[0-9, i{1,4}, v]", re.I) This is almost certainly not going to do what you expect, because all those backslashes in the string are going to get processed as escape characters before the string is ever passed to re.compile. Even if you fix that (by doubling the backslashes or making it a raw string), I sincerely doubt that this is the regular expression you want. As escaped, it matches in sequence: * the start of the string * a space, tab, newline or other whitespace character. Just the one. * the literal string "[0-9, " * either "i" or "I" repeated between 1 and four times * the literal string ", " * either "v" or "V" * the literal string "]" Assuming you didn't mean to escape the open square bracket doesn't help: * the start of the string * one whitespace character * one of the following characters: 0123456789,iI{}vV Also, what the heck is this doing *inside* the for loop? > textNum = num.match(scene) If you're using re.match(), the "^" on the regular expression is redundant. > if textNum: > print textNum textNum is the match object, so printing it won't tell you much. In particular, it isn't going to produce well-formed XML. > else: > print "No scene number" Nor will this. > m = '
> It happened to me sometimes. If a module defines some functions, and it > doesn't *use* them, why should I use a global search-and-replace to > rename something? Modifying the "def" line should be enough - unless the > function happens to be recursive. So the answer to my question would be "nothing"? > It's the DRY principle in action, the same argument as when decorators > where introduced: there should be no need to repeat the function name > again and again. Unless of course you're using it again and again, which you are. -- Rhodri James *-* Wildebeeste Herder to the Masses From python-list at smetj.net Wed May 6 17:27:51 2009 From: python-list at smetj.net (J) Date: Wed, 06 May 2009 23:27:51 +0200 Subject: SimpleXMLRPCServer and creating a new object on for each new client request. Message-ID: <6de5adbe461e36d4756ae63194286e45@smetj.net> Hi list, My goals is to have concurrent and separated client sessions using xmlrpc. Initially my though was that SimpleXMLRPCServer was able to create a new object instance for each incoming request. But this doesn't appear to be the case, unless I'm overlooking something, if so please point me out. Concider following simplified code #!/usr/bin/python from SimpleXMLRPCServer import SimpleXMLRPCServer from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler import random # Restrict to a particular path. class RequestHandler(SimpleXMLRPCRequestHandler): rpc_paths = ('/RPC2',) # Create a simple example class class Randomizer: def __init__(self): self.random=random.randrange(0,100000) def show_random(self): return self.random # Create server server = SimpleXMLRPCServer(("localhost", 8000),requestHandler=RequestHandler,allow_none=1) server.register_introspection_functions() server.register_instance(Randomizer()) server.serve_forever() I start python interactively: >>> import xmlrpclib >>> session1=xmlrpclib.ServerProxy('http://localhost:8000') >>> session2=xmlrpclib.ServerProxy('http://localhost:8000') >>> print session1.show_random() 13930 >>> print session2.show_random() 13930 >>> I though that session1 and session2 would be 2 different Randomizer objects each having a different result for self.random But as the example shows this is not the case. How can I solve this? Thanks Jelle From mabdelkader at gmail.com Wed May 6 17:36:02 2009 From: mabdelkader at gmail.com (ma) Date: Wed, 6 May 2009 17:36:02 -0400 Subject: fcntl and siginfo_t in python In-Reply-To: References: <148918f0904281356v534680dcmd03084fe255cb2d3@mail.gmail.com> <148918f0904291012j4c1ada35jcee6030b75b952d0@mail.gmail.com> <148918f0904301637t5e63537atd85e2a475f4e069f@mail.gmail.com> <148918f0905011208u4946c377uce7603bb2e456370@mail.gmail.com> <148918f0905040903s7a5c7e5exc2968d0e65540d81@mail.gmail.com> Message-ID: <169B8277-4F9A-4DC3-B486-72796E239238@gmail.com> Sure, I'll send you the source files when I get a chance! --Sent from my iPhone On May 6, 2009, at 4:03 PM, Philip wrote: > ma gmail.com> writes: >> >> Ok! So, I decided to write a C-extension instead of using ctypes... >> >> This works beautifully. Now, I want to release this to the public, so >> I'm thinking of making a bit of code cleanup. Should I just pack the >> entire siginfo_t struct, right now I just use the fd, into a >> dictionary and pass it to the python callback handler function? Maybe >> there might be some more suggestions to what data structures to use, >> so I'm open right now to any of them. > > Could we have a look at your working prototype? > > Philip J. Tait > http://subarutelescope.org > > -- > http://mail.python.org/mailman/listinfo/python-list From pavlovevidence at gmail.com Wed May 6 17:48:16 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Wed, 6 May 2009 14:48:16 -0700 (PDT) Subject: call function of class instance with no assigned name? References: <447f2963-07b1-40bb-b084-ddf6c9cc880b@c18g2000prh.googlegroups.com> Message-ID: <4d8e738a-bc03-4598-8161-e584a4617699@r36g2000vbr.googlegroups.com> On May 5, 7:55?pm, Dave Angel wrote: > George Oliver wrote: > > On May 5, 11:59 am, Dave Angel wrote: > > >> 1) forget about getattr() unless you have hundreds of methods in your > >> map. ?The real question is why you need two maps. What good is the > >> "command string" doing you? ? Why not just map the keyvalues directly > >> into function objects? > > > Thanks for the reply Dave. I understand your example and it's what I > > originally used. Here is more detail on what I'm doing now, I hope > > this will explain my question better. > > > In the game I'm writing the player, monsters, items and so on are > > instances of class Thing, like: > > > class Thing(object): > > ? ? def __init__(self, x, y, name): > > ? ? ? ? self.x, self.y = x, y > > ? ? ? ? self.name = name > > ? ? ? ? self.brain = None > > > Some Things have an instance of class Brain attached. The Brain > > instance has a list of handlers, like: > > > class Brain(object): > > ? ? def __init__(self): > > ? ? ? ? self.handlers = [] > > > A handler class defines some functionality for the Brain. Each Brain > > has an update method like this: > > > def update(self, arguments): > > ? ? for handler in self.handlers: > > ? ? ? ? handler.update(arguments) > > > So on each pass through the main game loop, it calls the update method > > of all the brains in the game, which run their handler update methods > > to change the state of the game. > > > A handler would be something like a key input handler. The key input > > handler is defined separately from the command handler in the case I > > want to use a different method of input, for example a mouse or > > joystick. > > > In the dictionary of key inputs I could map each input directly to a > > function. However there is no instance name I can call the function > > on, as I create a thing, add a brain, and add handlers to the brain > > like this: > > > player = Thing(26, 16, 'player') > > player.brain = Brain() > > player.brain.add_handlers( > > ? ? ? ? ? ? ? ? ? ? ? ? ? ? commandHandler(player.brain, player), > > ? ? ? ? ? ? ? ? ? ? ? ? ? ? keyboardHandler(player.brain), > > ? ? ? ? ? ? ? ? ? ? ? ? ? ? fovHandler(player.brain)) > > > So what I'm wondering is how to reference the instance in each brain's > > list of handlers when I want to map something like a key input to a > > command, or what a better way might be to structure the code. > > ?>> > > >>player.brain.add_handlers( > >> ? ? ? ? ? ? ? ? ? ? ? ? ? ?commandHandler(player.brain, player), > >> ? ? ? ? ? ? ? ? ? ? ? ? ? ?keyboardHandler(player.brain), > >> ? ? ? ? ? ? ? ? ? ? ? ? ? ?fovHandler(player.brain)) > > You're executing commandHandler, and passing its return value into the > add_handlers() method. ?So commandHandler is a function, not a method, > and what is its relationship to anything that actually processes commands? It seems to be a factory function. > Sorry, your pseudo-code is so far from real code that I can't figure out > what you're doing. ?So I guess I can't be any help till something else > turns up to make it clearer. ?Maybe it's just me. I think it's you--and probably a lot of other people who haven't ever written games. No offense. As someone who's written games before I will tell you that George's pseudo-code is not far from real code. His overall approach is fairly typical of how games handle input, although there are some problems in the details. Carl Banks From mensanator at aol.com Wed May 6 17:49:17 2009 From: mensanator at aol.com (Mensanator) Date: Wed, 6 May 2009 14:49:17 -0700 (PDT) Subject: Copy & Paste in a Dos box (was: Pyhton script to call another program) References: <4A01AD6E.4010807@umbc.edu> <4A01CD29.3010507@hughes.net> Message-ID: <82feff6c-e238-4a08-b4e5-d88c14157211@g19g2000vbi.googlegroups.com> On May 6, 3:46?pm, Dave Angel wrote: > Mensanator wrote: > > > > > And when prompted, do "(.) modify shortcut that started this window" > > > After which, you can dispense with the menus (except when pasting), > > just select the text and hit . > > To paste into a DOS box, once Quick Edit is enabled, use Right-Click. ? > They keystrokes will be sent to the command editor. ?Note that the > interpretation is rather literal, so be careful if copy/pasting more > than one line, or a line that was wrapped. Well I'll be dipped. Didn't know you could do that. Of course, since I learned how to call programs from the script and capture their StdOut, I don't have much call for cut/paste from dos windows. Hopefully, I'll remember that the next time I need it. From jelle at smetj.net Wed May 6 17:52:47 2009 From: jelle at smetj.net (Jelle Smet) Date: Wed, 06 May 2009 23:52:47 +0200 Subject: SimpleXMLRPCServer and creating a new object on for each new client request. Message-ID: Hi list, My goals is to have concurrent and separated client sessions using xmlrpc. Initially my though was that SimpleXMLRPCServer was able to create a new object instance for each incoming request. But this doesn't appear to be the case, unless I'm overlooking something, if so please point me out. Concider following simplified code #!/usr/bin/python from SimpleXMLRPCServer import SimpleXMLRPCServer from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler import random # Restrict to a particular path. class RequestHandler(SimpleXMLRPCRequestHandler): rpc_paths = ('/RPC2',) # Create a simple example class class Randomizer: def __init__(self): self.random=random.randrange(0,100000) def show_random(self): return self.random # Create server server = SimpleXMLRPCServer(("localhost", 8000),requestHandler=RequestHandler,allow_none=1) server.register_introspection_functions() server.register_instance(Randomizer()) server.serve_forever() I start python interactively: >>> import xmlrpclib >>> session1=xmlrpclib.ServerProxy('http://localhost:8000') >>> session2=xmlrpclib.ServerProxy('http://localhost:8000') >>> print session1.show_random() 13930 >>> print session2.show_random() 13930 >>> I though that session1 and session2 would be 2 different Randomizer objects each having a different result for self.random But as the example shows this is not the case. How can I solve this? Thanks Jelle From pavlovevidence at gmail.com Wed May 6 18:07:42 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Wed, 6 May 2009 15:07:42 -0700 (PDT) Subject: call function of class instance with no assigned name? References: <447f2963-07b1-40bb-b084-ddf6c9cc880b@c18g2000prh.googlegroups.com> Message-ID: <42c18697-15ab-4813-8267-c416fae38642@p4g2000vba.googlegroups.com> On May 5, 12:17?pm, George Oliver wrote: > A handler would be something like a key input handler. The key input > handler is defined separately from the command handler in the case I > want to use a different method of input, for example a mouse or > joystick. > > In the dictionary of key inputs I could map each input directly to a > function. However there is no instance name I can call the function > on, as I create a thing, add a brain, and add handlers to the brain > like this: What instance do you want to call the functions on? This is the part that confuses me. > player = Thing(26, 16, 'player') > player.brain = Brain() > player.brain.add_handlers( > ? ? ? ? ? ? ? ? ? ? ? ? ? ? commandHandler(player.brain, player), > ? ? ? ? ? ? ? ? ? ? ? ? ? ? keyboardHandler(player.brain), > ? ? ? ? ? ? ? ? ? ? ? ? ? ? fovHandler(player.brain)) > > So what I'm wondering is how to reference the instance in each brain's > list of handlers when I want to map something like a key input to a > command, or what a better way might be to structure the code. I'm going to guess that you want the keyboardHandler to call method of commandHandler. There's no reason for commandHandler to be a handler at all then: keyboardHandler is already handling it. So here is how I would change it: player = Thing(26,16,'player') player.brain = Brain() responder = commandResponder(player.brain,player) player.brain.add_handlers( keyboardHandler(player.brain,responder), joystickHandler(player.brain,responder), fovHandler(player.brain), ) Carl Banks From gallium.arsenide at gmail.com Wed May 6 18:14:30 2009 From: gallium.arsenide at gmail.com (John Yeung) Date: Wed, 6 May 2009 15:14:30 -0700 (PDT) Subject: Code works fine except... References: <8d4ec1df-dddb-469a-99a1-695152db7c04@n4g2000vba.googlegroups.com> <16d47b2f-14d4-4266-84d2-c26501beb0c1@r34g2000vbi.googlegroups.com> <1691f042-bc14-46b9-b7b3-5a44348e9965@b1g2000vbc.googlegroups.com> <1a497471-498a-43d8-90ba-d676e8acf68b@r36g2000vbr.googlegroups.com> <8b49b775-66a7-42d5-9b00-cf23d9b044cc@e23g2000vbe.googlegroups.com> <54d76f11-c255-487d-9c3a-aaa380d7827a@z7g2000vbh.googlegroups.com> <991ccae3-f3e2-4d73-bd32-bc6f560e6f5f@n8g2000vbb.googlegroups.com> Message-ID: On May 6, 3:29?am, MRAB wrote: > I have the feeling that if the number of rounds is restricted then the > difference between the minimum and maximum number of byes could be 2 > because of the requirement that players shouldn't play each other more > than once, meaning that the players have to be shuffled around a bit, so > a player might play a week earlier or later than would otherwise be the > case. This is the feeling that I am getting also. All my efforts to keep everything as balanced as possible at all times (to be ready for the "season" to end suddenly at any time) result in messy jams that could otherwise be alleviated if I allowed temporary imbalances, knowing that there are more weeks later to make them up. John From e.kroske at gmail.com Wed May 6 18:25:48 2009 From: e.kroske at gmail.com (Evan Kroske) Date: Wed, 06 May 2009 18:25:48 -0400 Subject: Python 2.6 worth the hassle? Message-ID: <4A020E6C.5020406@gmail.com> I'm planning on learning Python, and I'd like to know which version to start with. I know that Python 3.0 isn't ready for production and it doesn't have enough libraries yet, so I thought I should learn Python 2.6. Unfortunately, installing Python 2.6 on my Linux distro (Ubuntu Intrepid Ibex) has given me lots of complicated problems I really don't want to deal with. My essential question is "Is Python 2.6 similar enough to Python 3.0 to justify its complexity of installation?" Upgrading to Jaunty is NOT an option (http://welcome2obscurity.blogspot.com/2009/05/jaunty-jackalope-released-vista-all.html). Best regards, Evan Kroske Undecided Python Student From mirthyn at gmail.com Wed May 6 18:32:58 2009 From: mirthyn at gmail.com (Ani) Date: Wed, 6 May 2009 19:32:58 -0300 Subject: removing row from table. PyQT Message-ID: <75097f100905061532y4685fed6ue7f19904a4964552@mail.gmail.com> I'm trying to remove the selected rows from the table, but it's not working, i've tried many ways, don't know what I'm missing. code below: class MonitorUi(QWidget): def __init__(self,parent = None): QWidget.__init__(self,parent) self._initalTable = [["none","none","none"],["none","none","none"],["none","none","none"]] self.lista="" self.headerH = ["Id","UserName","Engine","Proj","FileToRender","StartFrame","EndFrame","Packets","Skip","Attr","Status"] self.setWindowTitle("RenderMonitor") self.setGeometry(150,150,1000,500) self.setWindowIcon(QIcon('monkey.png')) timer = QTimer(self) self.ItemEdit = QLineEdit() self.buttonRemove = QPushButton("remove") self.buttonRende = QPushButton("Rende!") self.tableWidget = QTableWidget(1000,11) layoutH = QHBoxLayout() layoutH.addWidget(self.ItemEdit) #layoutH.addStretch(1) layoutH.addWidget(self.buttonRemove) layoutH.addWidget(self.buttonRende) layoutV = QVBoxLayout() layoutV.addLayout(layoutH) layoutV.addWidget(self.tableWidget) self.setLayout(layoutV) self.connect(self.buttonRemove, SIGNAL("clicked()"), self.remove) self.connect(self.buttonRende, SIGNAL("clicked()"), self.MsgServerRender) timer.start(1000) QObject.connect(timer, SIGNAL("timeout()"), self.displayTable) def setDataBridge(self,dataInst): self.lista = dataInst def clearTable(self): self.tableWidget.clear() def remove(self): print "removendo da tabela" #self.tableWidget.removeRow(self.tableView.selectedIndexes()) self.tableWidget.removeRow(self.tableWidget.selectedRows) #self.mapper.toNext() #self.tableView.selectRow(self.mapper.currentIndex()) def MsgServerRender(self): sender = ClientMsgRender() sender.sendTo("run") def displayTable(self): print "display table" self.tableWidget.setHorizontalHeaderLabels(self.headerH) self.headerV=[] list2= self.lista.getInfo() item = 0 linha = 0 while item < len(list2): #self.tableWidget = QTableWidget(item,9) self.headerV.append(("job" + str(item))) elemento = 0 coluna = 0 while elemento < 11 : str(list2[item][elemento]) NewTableitem = QTableWidgetItem(list2[item][elemento]) #NewTableitem.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled )#no editable item #NewTableitem.setFlags(Qt.ItemIsEnabled )#no editable item self.tableWidget.setItem(linha,coluna, NewTableitem) elemento += 1 coluna += 1 item += 1 linha += 1 self.tableWidget.setVerticalHeaderLabels(self.headerV) -------------- next part -------------- An HTML attachment was scrubbed... URL: From kyrie at uh.cu Wed May 6 18:33:20 2009 From: kyrie at uh.cu (Luis Alberto Zarrabeitia Gomez) Date: Wed, 06 May 2009 18:33:20 -0400 Subject: Self function In-Reply-To: References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> <1292fea0-96fb-4a21-b39d-b6ce610ea6b2@t10g2000vbg.googlegroups.com> <4f13a110-b3e5-4de5-b6d6-0d64da20b61e@r34g2000vbi.googlegroups.com> Message-ID: <1241649200.4a0210301f77e@mail.uh.cu> Quoting Rhodri James : > >> I'm sorry, but while I'm mildly positive towards the proposal (and more > >> so towards Aaron's decorator), I don't buy this argument at all. What > >> is broken about your editor's global search-and-replace function that > >> makes it "usually useless" for making these name changes? > > > > It happened to me sometimes. If a module defines some functions, and it > > doesn't *use* them, why should I use a global search-and-replace to > > rename something? Modifying the "def" line should be enough - unless the > > function happens to be recursive. > > So the answer to my question would be "nothing"? Indeed, there is nothing broken with the search and replace feature of his editor. When he is copying a non-recursive function, it is _useless_ to do a search and replace. When he is copying a recursive function, it is _required_ to do a search and replace. So, the very same task requires you to either perform a task that will almost always be useless (or even dangerous), or to read the source code to find out if the function is recursive, so that you can use the search and replace only then. (I know that the "problem" is present in almost all programming languages... but that's not what is being discussed. Bearophile's concerns seem legitimate, and you should not dismiss them so lightly just because there are ways to do more work and hopefully avoid the problems. I'd say that the "problem" is even aggravated in python, where the dynamic nature of the language makes it near to impossible to build good refactoring tools) -- Luis Zarrabeitia Facultad de Matem?tica y Computaci?n, UH http://profesores.matcom.uh.cu/~kyrie -- Participe en Universidad 2010, del 8 al 12 de febrero de 2010 La Habana, Cuba http://www.universidad2010.cu From Scott.Daniels at Acm.Org Wed May 6 18:41:00 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Wed, 06 May 2009 15:41:00 -0700 Subject: Copy & Paste in a Dos box In-Reply-To: References: <4A01AD6E.4010807@umbc.edu> <4A01CD29.3010507@hughes.net> Message-ID: Dave Angel wrote: > Mensanator wrote: >> ... After which, you can dispense with the menus (except when pasting), >> just select the text and hit . >> > To paste into a DOS box, once Quick Edit is enabled, use Right-Click. > They keystrokes will be sent to the command editor. Note that the > interpretation is rather literal, so be careful if copy/pasting more > than one line, or a line that was wrapped. Also, you can type into the cmd window: Alt-Space E P Alt-Space E P --Scott David Daniels Scott.Daniels at Acm.Org From tjreedy at udel.edu Wed May 6 18:52:22 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 06 May 2009 18:52:22 -0400 Subject: Python 2.6 worth the hassle? In-Reply-To: <4A020E6C.5020406@gmail.com> References: <4A020E6C.5020406@gmail.com> Message-ID: Evan Kroske wrote: > I'm planning on learning Python, and I'd like to know which version to > start with. I know that Python 3.0 isn't ready for production and it 3.1 will be. The first beta will be out very soon and 3.1 in less than 2 months. > doesn't have enough libraries yet, That will take longer. I hope the improvement in 3.1 will inspire more conversions. > so I thought I should learn Python 2.6. Unfortunately, installing Python 2.6 on my Linux distro (Ubuntu > Intrepid Ibex) has given me lots of complicated problems I really don't > want to deal with. > > My essential question is "Is Python 2.6 similar enough to Python 3.0 to > justify its complexity of installation?" Upgrading to Jaunty is NOT an > option > (http://welcome2obscurity.blogspot.com/2009/05/jaunty-jackalope-released-vista-all.html). You could use 2.5 and write with an eye to the future. Use 'object' or some derivative thereof as a base for all your classes. Never use cmp() or .__cmp__(). Don't use string exceptions. Look in 3.0 What's new for anything else to avoid. tjr From benjamin at python.org Wed May 6 19:01:25 2009 From: benjamin at python.org (Benjamin Peterson) Date: Wed, 6 May 2009 18:01:25 -0500 Subject: [RELEASED] Python 3.1 beta 1 Message-ID: <1afaf6160905061601l1fac114ei4ffd0f4f35826640@mail.gmail.com> On behalf of the Python development team, I'm thrilled to announce the first and only beta release of Python 3.1. Python 3.1 focuses on the stabilization and optimization of features and changes Python 3.0 introduced. For example, the new I/O system has been rewritten in C for speed. File system APIs that use unicode strings now handle paths with undecodable bytes in them. [1] Other features include an ordered dictionary implementation and support for ttk Tile in Tkinter. For a more extensive list of changes in 3.1, see http://doc.python.org/dev/py3k/whatsnew/3.1.html or Misc/NEWS in the Python distribution. Please note that this is a beta release, and as such is not suitable for production environments. We continue to strive for a high degree of quality, but there are still some known problems and the feature sets have not been finalized. This beta is being released to solicit feedback and hopefully discover bugs, as well as allowing you to determine how changes in 3.1 might impact you. If you find things broken or incorrect, please submit a bug report at http://bugs.python.org For more information and downloadable distributions, see the Python 3.1 website: http://www.python.org/download/releases/3.1/ See PEP 375 for release schedule details: http://www.python.org/dev/peps/pep-0375/ Enjoy, -- Benjamin Benjamin Peterson benjamin at python.org Release Manager (on behalf of the entire python-dev team and 3.1's contributors) From norseman at hughes.net Wed May 6 19:02:06 2009 From: norseman at hughes.net (norseman) Date: Wed, 06 May 2009 16:02:06 -0700 Subject: Copy & Paste in a Dos box In-Reply-To: <2dc0c81b0905061112l32426cb8rafa3a86dca12810d@mail.gmail.com> References: <4A01AD6E.4010807@umbc.edu> <4A01CD29.3010507@hughes.net> <4A01CEC5.4090008@tim.thechases.com> <2dc0c81b0905061112l32426cb8rafa3a86dca12810d@mail.gmail.com> Message-ID: <4A0216EE.8050007@hughes.net> Shawn Milochik wrote: > On Wed, May 6, 2009 at 1:54 PM, Tim Chase wrote: >>> for windows this works: >>> (can't cut and paste from a dos box!###%*&!!!) >> Depending on how it was spawned, you can either right-click in the window >> and choose Mark/Paste (when marking, use to terminate the selection; >> and selections are blockwise rectangular rather than linewise or >> characterwise). Alternatively, if you click on the system menu (the icon in >> the title-bar with resize/minimize/maximize/close/help), there should be an >> Edit submenu with the same options within. >> >> A pain, but at least feasible. > > > I know I'm coming to the conversation late, but here's what I do*: > > 1. Use Cygwin. (http://www.cygwin.com/) > 2. Use PuttyCYG (http://code.google.com/p/puttycyg/) > > That way, you can basically use PuTTY to shell into your Windows box. > > Note: If you are familiar with the Linux command line, you will be in > love with this solution. If you're a Windows-only type, then be > forewarned that doing this will require you to type Unix/Linux > commands (for the most part) instead of DOS commands. > > *Disclaimer: I use a Mac and Linux. This is my workaround when forced > to use Windows. Your environment preferences may vary. > -- > http://mail.python.org/mailman/listinfo/python-list =================================== Nope and no thanks. I'm talking about a Dos Box on Win XP Pro For Tim - I get junk from somewhere else in the system. I recognize it from hours and many open/close boxes ago, but it's not what I 'swiped". For Shawn - why would I want to run synthesizers? Or any other ... (There is nothing wrong with Cygwin, just not on everybody's system. Didn't come with the package.) I write in Linux and test on other peoples Window$ ^ Thunderbird did not mark that as a miss spelling?!!! Three cheers for them! Objective - stick with out of the box to get way less install problems. Steve From tjreedy at udel.edu Wed May 6 19:02:59 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 06 May 2009 19:02:59 -0400 Subject: About ODFPY links In-Reply-To: <862e00220905060253j290672acu86f18fc1bcd6bb2a@mail.gmail.com> References: <862e00220905060253j290672acu86f18fc1bcd6bb2a@mail.gmail.com> Message-ID: shruti surve wrote: > hey all, > For my project, i am using ODFpy open office spreadsheets. I am > creating ledgers in python ie.ledger.py . So when > i run ledger.py, spreadsheet will be opened in open office. since ledger > is made of number of accounts, i am creating multiple tables for all > accounts in single spreadsheet. So, there are number of tables in one > spreadsheet. Now i want create links in each table of spreadsheet so > that i can connect multiple tables. SO can anyone please tell me how to > create links in odfpy.. The general answer to such questions is to make a minimal doc in some ODF application such of OOo that has the desired feature, save, open the doc as a zip file (unless saved as xml) and extract the pieces as xml. Then find the text that represents that feature. There is a close correspondance between the xml and the internal document object model created by odfpy (and OOo, etc). In your case, make a spreadsheet with two table. Give one one literal entry. Give the other one entry that links to the first. Then look in the xml to see how links are presented. That should give a hint as to how to do the same in odfpy. Then try to recreate the minimal doc with odfpy and look at the xml *it* produces. If it looks similar enough, open with OOo to see what you get. tjr From norseman at hughes.net Wed May 6 19:10:18 2009 From: norseman at hughes.net (norseman) Date: Wed, 06 May 2009 16:10:18 -0700 Subject: Copy & Paste in a Dos box In-Reply-To: References: <4A01AD6E.4010807@umbc.edu> <4A01CD29.3010507@hughes.net> Message-ID: <4A0218DA.8080209@hughes.net> Mensanator wrote: > On May 6, 12:54 pm, Tim Chase wrote: >>> for windows this works: >>> (can't cut and paste from a dos box!###%*&!!!) >> Depending on how it was spawned, you can either right-click in >> the window and choose Mark/Paste (when marking, use to >> terminate the selection; and selections are blockwise rectangular >> rather than linewise or characterwise). Alternatively, if you >> click on the system menu (the icon in the title-bar with >> resize/minimize/maximize/close/help), there should be an Edit >> submenu with the same options within. >> >> A pain, but at least feasible. > > Less painful is to set the properties of the dos box: > > Edit Options: > [x] Quick Edit mode > > And when prompted, do "(.) modify shortcut that started this window" > > After which, you can dispense with the menus (except when pasting), > just select the text and hit . > > Makes a world of difference. > >> HTH, >> >> -tkc > > -- > http://mail.python.org/mailman/listinfo/python-list > =================== On a straight box - that doesn't work either. The select all does, but paste yanked something from hours ago. Yes - set as stated, closed and used icon to open box. MicroSoft still need to soften its hard head and join the 21st century. I love the ease of Linux. One thing we do get from using both OS's - our first release code is much more solid as a result. Steve From gherron at islandtraining.com Wed May 6 19:12:48 2009 From: gherron at islandtraining.com (Gary Herron) Date: Wed, 06 May 2009 16:12:48 -0700 Subject: Python 2.6 worth the hassle? In-Reply-To: <4A020E6C.5020406@gmail.com> References: <4A020E6C.5020406@gmail.com> Message-ID: <4A021970.9090705@islandtraining.com> Evan Kroske wrote: > I'm planning on learning Python, and I'd like to know which version to > start with. I know that Python 3.0 isn't ready for production and it > doesn't have enough libraries yet, so I thought I should learn Python > 2.6. Unfortunately, installing Python 2.6 on my Linux distro (Ubuntu > Intrepid Ibex) has given me lots of complicated problems I really > don't want to deal with. Just learn 2.5. From the beginners point of view, 2.5 and 2.6 are virtually the same. (If you work hard, you can find differences, but you did say you were a beginner -- so don't worry about the differences -- they are minor and evolutionary not revolutionary.) Gary Herron > > My essential question is "Is Python 2.6 similar enough to Python 3.0 > to justify its complexity of installation?" Upgrading to Jaunty is NOT > an option > (http://welcome2obscurity.blogspot.com/2009/05/jaunty-jackalope-released-vista-all.html). > > > Best regards, > Evan Kroske > Undecided Python Student > -- > http://mail.python.org/mailman/listinfo/python-list From martin.hellwig at dcuktec.org Wed May 6 19:13:53 2009 From: martin.hellwig at dcuktec.org (Martin P. Hellwig) Date: Thu, 07 May 2009 00:13:53 +0100 Subject: SimpleXMLRPCServer and creating a new object on for each new client request. In-Reply-To: References: Message-ID: Jelle Smet wrote: > Hi list, > > My goals is to have concurrent and separated client sessions using xmlrpc. > Initially my though was that SimpleXMLRPCServer was able to create a new > object instance for each incoming request. > But this doesn't appear to be the case, unless I'm overlooking something, > if so please point me out. > > Concider following simplified code > SimpleXMLRPCServer, is rather, well eh, simple and does not support sessions, which in an HTTP point of view makes sense (where XML-RPC is based on). So either build in some sort of session management or rewrite your functionality in such a ways that it isn't dependent on sessions (see my reimplementation after my sig). -- MPH http://blog.dcuktec.com 'If consumed, best digested with added seasoning to own preference.' #! /usr/bin/env python #'----------------example reimplementation----------------------------' "XML RPC test" import SocketServer import SimpleXMLRPCServer import random import xmlrpclib import threading # Create a simple example class class Randomizer(object): "Randomizer Docstring" def __init__(self): self.random_number = None def randomize(self): "create a new random number" self.random_number = random.randrange(0, 100000) def show_random(self): "show_random Docstring" self.randomize() return(self.random_number) class Server(SocketServer.ThreadingMixIn, SimpleXMLRPCServer.SimpleXMLRPCServer): "Multi-threaded XML RPC Server" # pylint: disable-msg=R0904 def __init__(self): address = ('localhost', 8000) SimpleXMLRPCServer.SimpleXMLRPCServer.__init__(self, address) self.allow_none = True self.register_instance(Randomizer()) self.register_function(self.shutdown_service) self.register_introspection_functions() # pylint: enable-msg=R0904 def shutdown_service(self): "Stop server" self.shutdown() class Thread(threading.Thread): "Run Server in a thread for testing purposes" def run(self): "Run the service" service = Server() service.serve_forever() if __name__ == '__main__': URL_PORT = 'http://localhost:8000' TEST_THREAD = Thread() TEST_THREAD.start() SESSION1 = xmlrpclib.ServerProxy(URL_PORT) SESSION2 = xmlrpclib.ServerProxy(URL_PORT) print(SESSION1.show_random()) print(SESSION2.show_random()) SESSION_STOP = xmlrpclib.ServerProxy(URL_PORT) SESSION_STOP.shutdown_service() #'----------------example reimplementation----------------------------' From google at mrabarnett.plus.com Wed May 6 19:15:27 2009 From: google at mrabarnett.plus.com (MRAB) Date: Thu, 07 May 2009 00:15:27 +0100 Subject: Copy & Paste in a Dos box In-Reply-To: <82feff6c-e238-4a08-b4e5-d88c14157211@g19g2000vbi.googlegroups.com> References: <4A01AD6E.4010807@umbc.edu> <4A01CD29.3010507@hughes.net> <82feff6c-e238-4a08-b4e5-d88c14157211@g19g2000vbi.googlegroups.com> Message-ID: <4A021A0F.1040800@mrabarnett.plus.com> Mensanator wrote: > On May 6, 3:46 pm, Dave Angel wrote: >> Mensanator wrote: >>> >>> And when prompted, do "(.) modify shortcut that started this window" >>> After which, you can dispense with the menus (except when pasting), >>> just select the text and hit . >> To paste into a DOS box, once Quick Edit is enabled, use Right-Click. >> They keystrokes will be sent to the command editor. Note that the >> interpretation is rather literal, so be careful if copy/pasting more >> than one line, or a line that was wrapped. > > Well I'll be dipped. Didn't know you could do that. > > Of course, since I learned how to call programs from the > script and capture their StdOut, I don't have much call for > cut/paste from dos windows. Hopefully, I'll remember that the > next time I need it. > That's new to me too! You probably already know that if you drag-and-drop a file onto the window you get its path. From amrbekhit at gmail.com Wed May 6 19:23:14 2009 From: amrbekhit at gmail.com (Amr) Date: Wed, 6 May 2009 16:23:14 -0700 (PDT) Subject: Could this expression parser be more 'Pythonic'? Message-ID: Hello all, I've been spending the last few weeks learning Python, and I've just started to use it to write a simple BASIC compiler. I'm writing a mathematical expression parser and wrote a function that would take a string and split it into high level tokens. The code can be found at http://pastebin.com/f757252ec (have a look at the function tokenize). I was interested to see if the tokenize function could be written in a better way, so as to make better use of the Python language and libraries. One thing that sprung to mind was whether it would be a good idea to perhaps take advantage of regular expressions. I had a look at a few compiler sites and regexps seem to come into that role quite a bit. I've only done a little work on regexps and they seem very powerful, but can easily get out of control! So, any suggestions as to how to make better use of the language writing something like this? Thanks, --Amr From norseman at hughes.net Wed May 6 19:28:34 2009 From: norseman at hughes.net (norseman) Date: Wed, 06 May 2009 16:28:34 -0700 Subject: Copy & Paste in a Dos box In-Reply-To: <4A01F323.4040808@ieee.org> References: <4A01AD6E.4010807@umbc.edu> <4A01CD29.3010507@hughes.net> <4A01CEC5.4090008@tim.thechases.com> <4A01F323.4040808@ieee.org> Message-ID: <4A021D22.5010508@hughes.net> Dave Angel wrote: > Tim Chase wrote: >>
> for >> windows this works: >>> (can't cut and paste from a dos box!###%*&!!!) >> >> Depending on how it was spawned, you can either right-click in the >> window and choose Mark/Paste (when marking, use to terminate >> the selection; and selections are blockwise rectangular rather than >> linewise or characterwise). Alternatively, if you click on the system >> menu (the icon in the title-bar with >> resize/minimize/maximize/close/help), there should be an Edit submenu >> with the same options within. >> >> A pain, but at least feasible. >> >> HTH, >> >> -tkc >> >> > Note that if you like copy/paste, you can turn on "Quick Edit" mode in > your cmd.exe windows. From that same System menu on your DOS box, > choose "Defaults". From the first tab that box, enable "Quick Edit" > checkbox. This will change the default for subsequent DOS boxes. You > can change it just for the current one by using Properties in the same > system menu. > > > -- > http://mail.python.org/mailman/listinfo/python-list > ==================== HuMMMMMM - Dave's use of cmd.exe as different from Dos Box points out a miss name on my part. I should have said cmd.exe originally. Yes - I use dosemu on Linux Slackware 10.2 with enlightenment 16.2 and swipe/paste works as it should. (Of course gpm is loaded. That's a must. :) I have been cutting back. Used to have 6 keyboards going at once. (well in packet passing mode shall we say? When computers were slower. :) Down to two close by and two across the way. One Solaris, two Linux, one Win XP. Gotta keep the Win machine nearby. It needs its daily crash. I have learned to stick to 'stock' setups or pay a heavy price at "show time". I tried the settings again just now. I did both Properties and Default and made sure they were set the same. I did the --- for icon --- and killed the open box and clicked the icon and swiped and pasted to wordpad. IT FINALLY WORKED!!! Hadn't intended to start such a rash of dialog, but I benefited too! Thanks guys!!! Steve From mensanator at aol.com Wed May 6 19:38:37 2009 From: mensanator at aol.com (Mensanator) Date: Wed, 6 May 2009 16:38:37 -0700 (PDT) Subject: Copy & Paste in a Dos box References: <4A01AD6E.4010807@umbc.edu> <4A01CD29.3010507@hughes.net> <82feff6c-e238-4a08-b4e5-d88c14157211@g19g2000vbi.googlegroups.com> Message-ID: <6640b734-80a7-4292-8223-7c00f5538261@r3g2000vbp.googlegroups.com> On May 6, 6:15?pm, MRAB wrote: > Mensanator wrote: > > On May 6, 3:46 pm, Dave Angel wrote: > >> Mensanator wrote: > >>> > >>> And when prompted, do "(.) modify shortcut that started this window" > >>> After which, you can dispense with the menus (except when pasting), > >>> just select the text and hit . > >> To paste into a DOS box, once Quick Edit is enabled, use Right-Click. ? > >> They keystrokes will be sent to the command editor. ?Note that the > >> interpretation is rather literal, so be careful if copy/pasting more > >> than one line, or a line that was wrapped. > > > Well I'll be dipped. Didn't know you could do that. > > > Of course, since I learned how to call programs from the > > script and capture their StdOut, I don't have much call for > > cut/paste from dos windows. Hopefully, I'll remember that the > > next time I need it. > > That's new to me too! > > You probably already know that if you drag-and-drop a file onto the > window you get its path. Damn! I may just go back to using Python from the command prompt instead of using IDLE. On second thought, IDLE is way too useful for indenting, dedenting, commenting and uncommenting blocks of code. Can't go back to using Notepad. From tomf.sessile at gmail.com Wed May 6 19:40:19 2009 From: tomf.sessile at gmail.com (TomF) Date: Wed, 6 May 2009 16:40:19 -0700 Subject: Simple way of handling errors Message-ID: <2009050616401916807-tomfsessile@gmailcom> As a relative newcomer to Python, I like it a lot but I'm dismayed at the difficulty of handling simple errors. In Perl if you want to anticipate a file-not-found error you can simply do: open($file) or die("open($file): $!"); and you get an intelligible error message. In Python, to get the same thing it appears you need at least: try: f=open(file) except IOError, err: print "open(%s): got %s" % (file, err.strerror) exit(-1) Is there a simpler interface or idiom for handling such errors? I appreciate that Python's exception handling is much more sophisticated but often I don't need it. -Tom From DustanGroups at gmail.com Wed May 6 19:41:25 2009 From: DustanGroups at gmail.com (Dustan) Date: Wed, 6 May 2009 16:41:25 -0700 (PDT) Subject: How do I escape slashes the string formatting operator? (or: why is it behaving this way?) Message-ID: Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> 'HELP!%(xyz)/' % {'xyz':' PLEASE! '} Traceback (most recent call last): File "", line 1, in ValueError: unsupported format character '/' (0x2f) at index 11 >>> It doesn't like the forward slash after the closed parentheses. I don't know what it is expecting, but I need that slash there (obviously this is a simplified example). All help appreciated. From marek.rocki at wp.pl Wed May 6 19:51:57 2009 From: marek.rocki at wp.pl (marek.rocki at wp.pl) Date: Wed, 6 May 2009 16:51:57 -0700 (PDT) Subject: How do I escape slashes the string formatting operator? (or: why is it behaving this way?) References: Message-ID: <73c3f3a0-9fda-4eab-b46b-2cbe3f4baf1f@v17g2000vbb.googlegroups.com> Dustan napisa?(a): > Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit > (Intel)] on > win32 > Type "help", "copyright", "credits" or "license" for more information. > >>> 'HELP!%(xyz)/' % {'xyz':' PLEASE! '} > Traceback (most recent call last): > File "", line 1, in > ValueError: unsupported format character '/' (0x2f) at index 11 > >>> > > It doesn't like the forward slash after the closed parentheses. I > don't know what it is expecting, but I need that slash there > (obviously this is a simplified example). > > All help appreciated. Strign formatting docs (http://docs.python.org/library/ stdtypes.html#string-formatting) say that specifying a conversion type is mandatory. So you actually should write something like: 'HELP!%(xyz)s/' % {'xyz':' PLEASE! '} From DustanGroups at gmail.com Wed May 6 19:58:42 2009 From: DustanGroups at gmail.com (Dustan) Date: Wed, 6 May 2009 16:58:42 -0700 (PDT) Subject: How do I escape slashes the string formatting operator? (or: why is it behaving this way?) References: <73c3f3a0-9fda-4eab-b46b-2cbe3f4baf1f@v17g2000vbb.googlegroups.com> Message-ID: <8a4c5d87-9a8b-479a-991c-38704787042a@s31g2000vbp.googlegroups.com> On May 6, 6:51?pm, marek.ro... at wp.pl wrote: > Dustan napisa?(a): > > > Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit > > (Intel)] on > > win32 > > Type "help", "copyright", "credits" or "license" for more information. > > >>> 'HELP!%(xyz)/' % {'xyz':' PLEASE! '} > > Traceback (most recent call last): > > File "", line 1, in > > ValueError: unsupported format character '/' (0x2f) at index 11 > > > It doesn't like the forward slash after the closed parentheses. I > > don't know what it is expecting, but I need that slash there > > (obviously this is a simplified example). > > > All help appreciated. > > Strign formatting docs (http://docs.python.org/library/ > stdtypes.html#string-formatting) say that specifying a conversion type > is mandatory. So you actually should write something like: > 'HELP!%(xyz)s/' % {'xyz':' PLEASE! '} Thanks. That seems to have worked nicely. From google at mrabarnett.plus.com Wed May 6 20:01:46 2009 From: google at mrabarnett.plus.com (MRAB) Date: Thu, 07 May 2009 01:01:46 +0100 Subject: removing row from table. PyQT In-Reply-To: <75097f100905061532y4685fed6ue7f19904a4964552@mail.gmail.com> References: <75097f100905061532y4685fed6ue7f19904a4964552@mail.gmail.com> Message-ID: <4A0224EA.7040108@mrabarnett.plus.com> Ani wrote: > I'm trying to remove the selected rows from the table, > but it's not working, i've tried many ways, don't know what I'm missing. > > code below: > [snip] > def remove(self): > print "removendo da tabela" > #self.tableWidget.removeRow(self.tableView.selectedIndexes()) > self.tableWidget.removeRow(self.tableWidget.selectedRows) > #self.mapper.toNext() > #self.tableView.selectRow(self.mapper.currentIndex()) > [snip] From the name I'd say that removeRow() can remove only a single row at a time. Try removing the rows one at a time, in reverse order (from highest to lowest index). From googler.1.webmaster at spamgourmet.com Wed May 6 20:13:29 2009 From: googler.1.webmaster at spamgourmet.com (googler.1.webmaster at spamgourmet.com) Date: Wed, 6 May 2009 17:13:29 -0700 (PDT) Subject: Threading and GIL Message-ID: <60538e1c-47ea-4120-bc91-a9dcebd8f14b@r34g2000vbi.googlegroups.com> Hi! I have a big problem I can't solve and I hope anyone of you can help me. I use the Python C API and in C++ I have a class which represets a thread object, similiar to the thread class of the known Python Thread class but with some needed additions so thats the reason why I have to built my own. Well, this is the problem. There is a method called Thread::End() which I can call in Python. But now the End() Function waits until the "Main" Function of my C++ class is done, but in this method I want to ensure the Global Interpreter Lock but this is already locked by the Thread which executes the End() command. So both is waiting and stops. The thread which calls the End () method is waiting for the finish of the Main() method of the Thread Class and the Main-Method in the other thread waits for the thread of the End() command that this is done - welcome Deadlock. Any suggestions what I have to do? Thank you veeery much for you help, otherwise I become really crazy here because this is the first time I have such a problem. Bye :) From davea at ieee.org Wed May 6 20:15:11 2009 From: davea at ieee.org (Dave Angel) Date: Wed, 06 May 2009 20:15:11 -0400 Subject: Copy & Paste in a Dos box (was: Pyhton script to call another program) In-Reply-To: <82feff6c-e238-4a08-b4e5-d88c14157211@g19g2000vbi.googlegroups.com> References: <4A01AD6E.4010807@umbc.edu> <4A01CD29.3010507@hughes.net> <82feff6c-e238-4a08-b4e5-d88c14157211@g19g2000vbi.googlegroups.com> Message-ID: <4A02280F.9010606@ieee.org> Mensanator wrote: > On May 6, 3:46 pm, Dave Angel wrote: > >> Mensanator wrote: >> >>> >>> >>> And when prompted, do "(.) modify shortcut that started this window" >>> >>> After which, you can dispense with the menus (except when pasting), >>> just select the text and hit . >>> >> To paste into a DOS box, once Quick Edit is enabled, use Right-Click. >> They keystrokes will be sent to the command editor. Note that the >> interpretation is rather literal, so be careful if copy/pasting more >> than one line, or a line that was wrapped. >> > > Well I'll be dipped. Didn't know you could do that. > > Of course, since I learned how to call programs from the > script and capture their StdOut, I don't have much call for > cut/paste from dos windows. Hopefully, I'll remember that the > next time I need it. > > As long as we're in Quick Edit mode, another trick is that you can double-click on a "word" in a DOS box to select it and copy to clipboard. Obviously it isn't always the exact string you want, but sometimes it beats dragging the mouse over the text. From davea at ieee.org Wed May 6 20:29:42 2009 From: davea at ieee.org (Dave Angel) Date: Wed, 06 May 2009 20:29:42 -0400 Subject: Copy & Paste in a Dos box In-Reply-To: <4A0218DA.8080209@hughes.net> References: <4A01AD6E.4010807@umbc.edu> <4A01CD29.3010507@hughes.net> <4A0218DA.8080209@hughes.net> Message-ID: <4A022B76.4040507@ieee.org> norseman wrote: > > On a straight box - that doesn't work either. > The select all does, but paste yanked something from hours ago. > > Once you have Quick Edit enabled, paste will work using right-click. If it got something from hours ago, then that must be the last time you copied anything to the real clipboard. Note that some applications use a private clipboard to give extra features (like multiple clipboxes). But I suspect your real problem is you thought you did a copy to the clipboard from the DOS box, and you did not. When you drag the mouse over the box, you *select* text, but do not copy it to the clipboard. That doesn't happen till your right-click. Then *another* right-click without anything selected will insert that new text into the command line. Try the following on a DOS box (after enabling Quick Edit). Double-click on a "word". Then right click *twice* to get it to the command line. From pavlovevidence at gmail.com Wed May 6 20:30:48 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Wed, 6 May 2009 17:30:48 -0700 (PDT) Subject: Threading and GIL References: <60538e1c-47ea-4120-bc91-a9dcebd8f14b@r34g2000vbi.googlegroups.com> Message-ID: On May 6, 5:13?pm, googler.1.webmas... at spamgourmet.com wrote: > Hi! > > I have a big problem I can't solve and I hope anyone of you can help > me. I use the Python C API and in C++ I have a class which represets a > thread object, similiar to the thread class of the known Python Thread > class but with some needed additions so thats the reason why I have to > built my own. Well, this is the problem. > > There is a method called Thread::End() which I can call in Python. But > now the End() Function waits until the "Main" Function of my C++ class > is done, but in this method I want to ensure the Global Interpreter > Lock but this is already locked by the Thread which executes the End() > command. So both is waiting and stops. The thread which calls the End > () method is waiting for the finish of the Main() method of the Thread > Class and the Main-Method in the other thread waits for the thread of > the End() command that this is done - welcome Deadlock. > > Any suggestions what I have to do? Thank you veeery much for you help, > otherwise I become really crazy here because this is the first time I > have such a problem. > > Bye :) You can try the PyGILState_Ensure and PyGILState_Release macros (see PEP 311, http://www.python.org/dev/peps/pep-0311/ for more information). Make sure you have the GIL whenever you make any calls into Python (with a couple exceptions) and make sure you DON'T have the GIL when creating or deleting threads. In fact, I can't think of any reason why it wouldn't work to ensure a GIL state first thing after the thread starts and to release it right before the thread ends. Carl Banks From lists at cheimes.de Wed May 6 20:39:23 2009 From: lists at cheimes.de (Christian Heimes) Date: Thu, 07 May 2009 02:39:23 +0200 Subject: Python 2.6 worth the hassle? In-Reply-To: <4A020E6C.5020406@gmail.com> References: <4A020E6C.5020406@gmail.com> Message-ID: Evan Kroske schrieb: > I'm planning on learning Python, and I'd like to know which version to > start with. I know that Python 3.0 isn't ready for production and it > doesn't have enough libraries yet, so I thought I should learn Python > 2.6. Unfortunately, installing Python 2.6 on my Linux distro (Ubuntu > Intrepid Ibex) has given me lots of complicated problems I really don't > want to deal with. It's rather easy to install Python 2.6 on Ubuntu 8.10 if you know how to do it right: # get all dependencies and necessary header files sudo apt-get build-dep python2.5 # download and extract it curl http://www.python.org/ftp/python/2.6.2/Python-2.6.2.tgz | tar -xzv # Python is compiled with UCS 4 on all Debian systems cd Python-2.6.2 ./configure --enable-unicode=ucs4 --prefix=/usr/local # compile it make # use altinstall so you don't overwrite the python command # and just install the python2.6 command sudo make altinstall Now you have Python 2.6.2 in /usr/local In order to remove it complete follow the instructions of my blog: http://lipyrary.blogspot.com/2009/04/ubuntu-904-jaunty-and-python.html Christian From giuott at gmail.com Wed May 6 20:39:32 2009 From: giuott at gmail.com (Giuseppe Ottaviano) Date: Thu, 7 May 2009 02:39:32 +0200 Subject: [ANN] BPT (Boxed Package Tool) 0.3 Message-ID: Hi all, I am happy to announce BPT 0.3 http://pypi.python.org/pypi/bpt This release solves some important bugs, and the documentation has less TODOs. Version 0.3 =========== - Improved documentation - Fixed a bug that prevented the ``env`` script to be ``source``\'d (Works only with bash >= 3.0) - Fixed a bug in the creation of symlinks that broken relocatability. - ``env`` script does no more rely on GNU's ``readlink``, that is not present in vanilla OSX - Other minor fixes What is BPT =========== BPT is a Python library (``bpt``) and a command line application (``box``) to create and manage isolated enviroments, or *boxes*. - Boxes are *relocatable*, which means that they can be moved to a different directory or even distributed to other machines (provided that the architecture is compatible). - Packages inside the box can be easily disabled, enabled and removed, so that different versions of the same software can be installed simultaneously, allowing to switch between them. - Boxes can be *nested*, in the sense that it is possible to activate a box environment while inside another box environment, so that all the packages installed in both boxes are available, and so on. BPT is similar in some ways to `virtualenv `_, but it is not restricted to Python packages, allowing to install virtually any Unix software. It also takes some ideas from `jhbuild `_, but without the dependency resolution and automatic downloading machinery, and the ``bpt-rules`` format is inspired by `Gentoo `_'s ebuilds. Enjoy, Giuseppe Ottaviano From davea at ieee.org Wed May 6 20:56:30 2009 From: davea at ieee.org (Dave Angel) Date: Wed, 06 May 2009 20:56:30 -0400 Subject: Copy & Paste in a Dos box In-Reply-To: <6640b734-80a7-4292-8223-7c00f5538261@r3g2000vbp.googlegroups.com> References: <4A01AD6E.4010807@umbc.edu> <4A01CD29.3010507@hughes.net> <82feff6c-e238-4a08-b4e5-d88c14157211@g19g2000vbi.googlegroups.com> <6640b734-80a7-4292-8223-7c00f5538261@r3g2000vbp.googlegroups.com> Message-ID: <4A0231BE.50603@ieee.org> Mensanator wrote: > On May 6, 6:15 pm, MRAB wrote: > >> Mensanator wrote: >> >>> On May 6, 3:46 pm, Dave Angel wrote: >>> >>>> Mensanator wrote: >>>> >>>>> >>>>> And when prompted, do "(.) modify shortcut that started this window" >>>>> After which, you can dispense with the menus (except when pasting), >>>>> just select the text and hit . >>>>> >>>> To paste into a DOS box, once Quick Edit is enabled, use Right-Click. >>>> They keystrokes will be sent to the command editor. Note that the >>>> interpretation is rather literal, so be careful if copy/pasting more >>>> than one line, or a line that was wrapped. >>>> >>> Well I'll be dipped. Didn't know you could do that. >>> >>> Of course, since I learned how to call programs from the >>> script and capture their StdOut, I don't have much call for >>> cut/paste from dos windows. Hopefully, I'll remember that the >>> next time I need it. >>> >> That's new to me too! >> >> You probably already know that if you drag-and-drop a file onto the >> window you get its path. >> > > Damn! I may just go back to using Python from the command prompt > instead of using IDLE. > > On second thought, IDLE is way too useful for indenting, dedenting, > commenting and uncommenting blocks of code. Can't go back to using > Notepad. > > > When I started with Python, I was using Metapad for editing. Although it doesn't help with commenting-blocks, it's much better than Notepad for all purposes I used. Then I discovered Komodo. Although not cheap, it's a great IDE for my uses. And there's a free version called Komodo-Edit, which is missing the debugger but otherwise may be quite useful. From rhodri at wildebst.demon.co.uk Wed May 6 20:59:02 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Thu, 07 May 2009 01:59:02 +0100 Subject: Self function In-Reply-To: <1241649200.4a0210301f77e@mail.uh.cu> References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> <1292fea0-96fb-4a21-b39d-b6ce610ea6b2@t10g2000vbg.googlegroups.com> <4f13a110-b3e5-4de5-b6d6-0d64da20b61e@r34g2000vbi.googlegroups.com> <1241649200.4a0210301f77e@mail.uh.cu> Message-ID: On Wed, 06 May 2009 23:33:20 +0100, Luis Alberto Zarrabeitia Gomez wrote: > Quoting Rhodri James : >> So the answer to my question would be "nothing"? > > Indeed, there is nothing broken with the search and replace feature of > his editor. When he is copying a non-recursive function, it is > _useless_ to do a search and replace. When he is copying a recursive > function, it is _required_ to do a search and replace. On the contrary, it's merely not very useful for a non-recursive function. It still changes the function's name, which was one of the objectives. Seriously, the lack of effort in (say) doing a quick incremental search for the function's name is a no-brainer. > So, the very same task requires you to either perform a task that will > almost always be useless (or even dangerous), or to read the source code > to find out if the function is recursive, so that you can use the search > and replace only then. If you're copying code without reading it, you're going to get bitten anyway some time. My sympathy is still limited. > (I know that the "problem" is present in almost all programming > languages... but that's not what is being discussed. Bearophile's > concerns seem legitimate, and you should not dismiss them so lightly > just because there are ways to do more work and hopefully avoid the > problems. I'd say that the "problem" is even aggravated in python, > where the dynamic nature of the language makes it near to > impossible to build good refactoring tools) As with that other great "problem" of python, the lack of private class attributes, a tiny amount of self-discipline solves 90% of the issues. -- Rhodri James *-* Wildebeeste Herder to the Masses From benjamin at python.org Wed May 6 21:32:47 2009 From: benjamin at python.org (Benjamin Peterson) Date: Wed, 6 May 2009 20:32:47 -0500 Subject: [RELEASED] Python 3.1 beta 1 Message-ID: <1afaf6160905061832xfc295e3y881c7c8e81083ee6@mail.gmail.com> On behalf of the Python development team, I'm thrilled to announce the first and only beta release of Python 3.1. Python 3.1 focuses on the stabilization and optimization of features and changes Python 3.0 introduced. For example, the new I/O system has been rewritten in C for speed. File system APIs that use unicode strings now handle paths with undecodable bytes in them. [1] Other features include an ordered dictionary implementation and support for ttk Tile in Tkinter. For a more extensive list of changes in 3.1, see http://doc.python.org/dev/py3k/whatsnew/3.1.html or Misc/NEWS in the Python distribution. Please note that this is a beta release, and as such is not suitable for production environments. We continue to strive for a high degree of quality, but there are still some known problems and the feature sets have not been finalized. This beta is being released to solicit feedback and hopefully discover bugs, as well as allowing you to determine how changes in 3.1 might impact you. If you find things broken or incorrect, please submit a bug report at http://bugs.python.org For more information and downloadable distributions, see the Python 3.1 website: http://www.python.org/download/releases/3.1/ See PEP 375 for release schedule details: http://www.python.org/dev/peps/pep-0375/ Enjoy, -- Benjamin Benjamin Peterson benjamin at python.org Release Manager (on behalf of the entire python-dev team and 3.1's contributors) From steven at REMOVE.THIS.cybersource.com.au Wed May 6 21:34:18 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 07 May 2009 01:34:18 GMT Subject: Python 2.6 worth the hassle? References: Message-ID: On Wed, 06 May 2009 18:25:48 -0400, Evan Kroske wrote: > I'm planning on learning Python, and I'd like to know which version to > start with. I know that Python 3.0 isn't ready for production and it > doesn't have enough libraries yet, so I thought I should learn Python > 2.6. Unfortunately, installing Python 2.6 on my Linux distro (Ubuntu > Intrepid Ibex) has given me lots of complicated problems I really don't > want to deal with. What happens if you use the altinstall option and install from source? > My essential question is "Is Python 2.6 similar enough to Python 3.0 to > justify its complexity of installation?" Upgrading to Jaunty is NOT an > option > (http://welcome2obscurity.blogspot.com/2009/05/jaunty-jackalope- released-vista-all.html). Are you saying that Intrepid uses Python3.0 as it's default Python? If not, then just use the default Python installed. Possibly Python 2.5. The differences between 2.5 and 2.6 are minor. -- Steven From steven at REMOVE.THIS.cybersource.com.au Wed May 6 21:35:12 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 07 May 2009 01:35:12 GMT Subject: list comprehension question References: <6d502cfd-e252-4722-96a5-c52fc3579499@v1g2000prd.googlegroups.com> <85ocucnbce.fsf@agentultra.com> <85ljpb8r1o.fsf@agentultra.com> <85d4am8hq4.fsf@agentultra.com> Message-ID: On Wed, 06 May 2009 09:48:51 -0400, J Kenneth King wrote: > Emile van Sebille writes: > >> On 5/5/2009 9:15 AM J Kenneth King said... >> >>> List comprehensions can make a reader of your code apprehensive >>> because it can read like a run-on sentence and thus be difficult to >>> parse. The Python documentation discourages their use and I believe >>> for good reason. >> >> Can you provide a link for this? I'd like to see specifically what's >> being discouraged, as I'd be surprised to find routine usage frowned >> upon. >> >> Emile > > http://docs.python.org/tutorial/datastructures.html#nested-list- comprehensions > > > "If you?ve got the stomach for it, list comprehensions can be nested. > They are a powerful tool but ? like all powerful tools ? they need to be > used carefully, if at all." How does this discourage the use of list comprehensions? At most, it warns that complicated list comps are tricky. Complicated *anything* are tricky. > and > > "In real world, you should prefer builtin functions to complex flow > statements." That's ridiculous. The example given is a special case. That's like saying "Loops are hard, so in the real world, if you want a loop, find a builtin function that does what you want instead." What's the "builtin function" we're supposed to prefer over a "complex flow statement" like this? # split text into word fragments of length <= 3 sentence = "a sentence is a series of words" new = [word[i:i+3] for word in sentence.split() for i in range(0, len(word), 3)] -- Steven From legoandrew at schjelderup.org Wed May 6 21:39:01 2009 From: legoandrew at schjelderup.org (Andrew Free) Date: Wed, 6 May 2009 18:39:01 -0700 Subject: Tkinter Caesar Cipher GUI (speed errors?) Message-ID: <3F213EA7-F667-4BAA-93EA-6D7205188FCB@schjelderup.org> I have been told the mailing lists talk a lot about Tkinter and I have not been able to find anyone who knows the answer to this question. This is also the first time I have used the mailing lists, so hey guys :D. I decided to work on a GUI for this because I need the practice. However I ran into this weird problem. If I change line 67 from 1000 to 100 my program prints two of the last letter...http://pastebin.com/d656cfd09 (with 1000) http://www.grabup.com/uploads/9d477c68231951f4025a27090de798e1.png?direct with (100) http://www.grabup.com/uploads/8adfdc83e9b5a2e9e7af1f557a97a3ee.png?direct That just makes no sense at all. Is 100 to fast for it to run? I would think it would wait for it to finish and update the GUI before it moved on. Thanks, Andrew -------------- next part -------------- An HTML attachment was scrubbed... URL: From lists at cheimes.de Wed May 6 21:40:16 2009 From: lists at cheimes.de (Christian Heimes) Date: Thu, 07 May 2009 03:40:16 +0200 Subject: Python 2.6 worth the hassle? In-Reply-To: References: Message-ID: Steven D'Aprano wrote: > Are you saying that Intrepid uses Python3.0 as it's default Python? > > If not, then just use the default Python installed. Possibly Python 2.5. > The differences between 2.5 and 2.6 are minor. Ubuntu 9.04 has Python 2.6.2 as default Python interpreter. It's also shipped with Python 2.5. Python 2.4 and 3.0 are in its software list, too. Christian From steven at REMOVE.THIS.cybersource.com.au Wed May 6 21:48:28 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 07 May 2009 01:48:28 GMT Subject: Self function References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> <1292fea0-96fb-4a21-b39d-b6ce610ea6b2@t10g2000vbg.googlegroups.com> <4f13a110-b3e5-4de5-b6d6-0d64da20b61e@r34g2000vbi.googlegroups.com> <1241649200.4a0210301f77e@mail.uh.cu> Message-ID: On Thu, 07 May 2009 01:59:02 +0100, Rhodri James wrote: > On Wed, 06 May 2009 23:33:20 +0100, Luis Alberto Zarrabeitia Gomez > wrote: > >> Quoting Rhodri James : >>> So the answer to my question would be "nothing"? >> >> Indeed, there is nothing broken with the search and replace feature of >> his editor. When he is copying a non-recursive function, it is >> _useless_ to do a search and replace. When he is copying a recursive >> function, it is _required_ to do a search and replace. > > On the contrary, it's merely not very useful for a non-recursive > function. [...] Whether search and replace is "not very useful" or "not useful at all", it's still an inconvenience and a PITA. Calling up S&R to search through a six line function is ridiculously overkill, but if you don't do it, you'll surely be bitten. But regardless, everyone is missing the most important point: why are you copying and pasting code in the first place? That is surely very close to the top of the list of Worst Ever Anti-Patterns, and it should be avoided whenever possible. In Python, we can avoid much copy-and-paste coding with decorators. Given one function, we can create as many variants as we want, differing in pre- processing of arguments and post-processing of results, by using decorators. This is a powerful ability to have, but it's crippled for many recursive functions, because recursive functions in Python don't actually call themselves, they call whatever happens to be bound to their name at runtime. As Luis said, all(?) programming languages have this same weakness. Just because it's a weakness shared by all languages, doesn't stop it from being a weakness. My proposal will lesson (but not entirely eliminate) this weakness. -- Steven From Shawn at Milochik.com Wed May 6 21:51:47 2009 From: Shawn at Milochik.com (Shawn Milochik) Date: Wed, 6 May 2009 21:51:47 -0400 Subject: Copy & Paste in a Dos box In-Reply-To: <6640b734-80a7-4292-8223-7c00f5538261@r3g2000vbp.googlegroups.com> References: <4A01AD6E.4010807@umbc.edu> <4A01CD29.3010507@hughes.net> <82feff6c-e238-4a08-b4e5-d88c14157211@g19g2000vbi.googlegroups.com> <6640b734-80a7-4292-8223-7c00f5538261@r3g2000vbp.googlegroups.com> Message-ID: <2dc0c81b0905061851xbfb5ac3q403c51adbe510c46@mail.gmail.com> > Damn! I may just go back to using Python from the command prompt > instead of using IDLE. > > On second thought, IDLE is way too useful for indenting, dedenting, > commenting and uncommenting blocks of code. Can't go back to using > Notepad. > You might want to check out iPython, then -- it's an interactive Python interpreter that's meant to be used as a shell environment. It has very useful help and auto-completion features. If you're just talking about an IDE, you might want to try TextMate if you're on a Mac. It's great. Notepad++ is good on Windows, but I haven't used it in Python much, so I don't know about its indentation features. From kyrie at uh.cu Wed May 6 22:32:23 2009 From: kyrie at uh.cu (Luis Alberto Zarrabeitia Gomez) Date: Wed, 06 May 2009 22:32:23 -0400 Subject: Self function In-Reply-To: References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> <1292fea0-96fb-4a21-b39d-b6ce610ea6b2@t10g2000vbg.googlegroups.com> <4f13a110-b3e5-4de5-b6d6-0d64da20b61e@r34g2000vbi.googlegroups.com> <1241649200.4a0210301f77e@mail.uh.cu> Message-ID: <1241663543.4a024837a9680@mail.uh.cu> Quoting Steven D'Aprano : > But regardless, everyone is missing the most important point: why are you > copying and pasting code in the first place? That is surely very close to > the top of the list of Worst Ever Anti-Patterns, and it should be avoided > whenever possible. [btw, I took this long before jumping into this thread for this precise reason... Copy/paste is an evil anti-pattern. > In Python, we can avoid much copy-and-paste coding with decorators. Given > one function, we can create as many variants as we want, differing in pre- > processing of arguments and post-processing of results, by using > decorators. This is a powerful ability to have, but it's crippled for > many recursive functions, because recursive functions in Python don't > actually call themselves, they call whatever happens to be bound to their > name at runtime. A bit offtopic: a while ago I think I saw a recipe for a decorator that, via bytecode hacks, would bind otherwise global names to the local namespace of the function. Can anyone remember it/point me to it? An @bind decorator that would 'localize' all the global names, including the still unexistent but already know function name, would guarantee that at least recursion calls the same function instead of "whatever happens to be bound to their name at runtime". If it wasn't a hack, anyway. -- Luis Zarrabeitia Facultad de Matem?tica y Computaci?n, UH http://profesores.matcom.uh.cu/~kyrie -- Participe en Universidad 2010, del 8 al 12 de febrero de 2010 La Habana, Cuba http://www.universidad2010.cu From georgeoliverGO at gmail.com Wed May 6 22:38:41 2009 From: georgeoliverGO at gmail.com (George Oliver) Date: Wed, 6 May 2009 19:38:41 -0700 (PDT) Subject: call function of class instance with no assigned name? References: <447f2963-07b1-40bb-b084-ddf6c9cc880b@c18g2000prh.googlegroups.com> <42c18697-15ab-4813-8267-c416fae38642@p4g2000vba.googlegroups.com> Message-ID: On May 6, 3:07 pm, Carl Banks wrote: > I'm going to guess that you want the keyboardHandler to call method of > commandHandler. There's no reason for commandHandler to be a handler > at all then: keyboardHandler is already handling it. Thanks Carl, you've got it right and your following example is what I ended up using, it's good to know it's a workable solution. From steven at REMOVE.THIS.cybersource.com.au Wed May 6 22:41:29 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 07 May 2009 02:41:29 GMT Subject: Simple way of handling errors References: <2009050616401916807-tomfsessile@gmailcom> Message-ID: On Wed, 06 May 2009 16:40:19 -0700, TomF wrote: > As a relative newcomer to Python, I like it a lot but I'm dismayed at > the difficulty of handling simple errors. In Perl if you want to > anticipate a file-not-found error you can simply do: > > open($file) or die("open($file): $!"); > > and you get an intelligible error message. In Python, to get the same > thing it appears you need at least: > > try: > f=open(file) > except IOError, err: > print "open(%s): got %s" % (file, err.strerror) > exit(-1) Functions never fail silently in Python. (At least built-in functions never fail silently. Functions you write yourself can do anything you want.) The canonical way of doing "open or die" in Python is to just call open: f = open(filename) If it fails, you get both a straight-forward error message and a useful traceback: Traceback (most recent call last): File "", line 1, in IOError: [Errno 2] No such file or directory: 'foomanchu' The only reason you would bother going to the time and effort of catching the error, printing your own error message, and then exiting, is if you explicitly want to hide the traceback from the user. Oh, and if you do that, I recommend that you print to stderr instead of stdout: print >>sys.stderr, "open(%s): got %s" % (file, err.strerror) or sys.stderr.write("open(%s): got %s\n" % (file, err.strerror)) -- Steven From jianchun.zhou at gmail.com Wed May 6 22:49:33 2009 From: jianchun.zhou at gmail.com (Jianchun Zhou) Date: Thu, 7 May 2009 10:49:33 +0800 Subject: Why there is a parameter named "self" for classmethod function? Message-ID: <2b767f890905061949g12d0bf03m68c6b466ffedfbd9@mail.gmail.com> Hi, ALL: I have a sample code as bellow: #!/usr/bin/env python class Hello: def __init__(self): print "Hello __init__" @classmethod def print_hello(self): print "hello" Hello.print_hello() If I move "self" parameter of print_hello away, this code fragment won't work. I am wondering when Hello.print_hello() executes, what value will "self" be asigned? -- Best Regards -------------- next part -------------- An HTML attachment was scrubbed... URL: From clp2 at rebertia.com Wed May 6 22:55:57 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Wed, 6 May 2009 19:55:57 -0700 Subject: Why there is a parameter named "self" for classmethod function? In-Reply-To: <2b767f890905061949g12d0bf03m68c6b466ffedfbd9@mail.gmail.com> References: <2b767f890905061949g12d0bf03m68c6b466ffedfbd9@mail.gmail.com> Message-ID: <50697b2c0905061955s7bd16296lca71f98039e8f292@mail.gmail.com> On Wed, May 6, 2009 at 7:49 PM, Jianchun Zhou wrote: > Hi, ALL: > > I have a sample code as bellow: > > #!/usr/bin/env python > > class Hello: > ??? def __init__(self): > ??????? print "Hello __init__" > ??? @classmethod > ??? def print_hello(self): > ??????? print "hello" > > Hello.print_hello() > > If I move "self" parameter of print_hello away, this code fragment won't > work. > > I am wondering when Hello.print_hello() executes, what value will "self" be > asigned? The class itself will be the value of self (hence the "class" in "classmethod") in that case. For this reason, the parameter is conventionally called "cls" rather than "self", to avoid confusion. Cheers, Chris -- http://blog.rebertia.com From kurt at kbsymanzik.org Wed May 6 23:01:44 2009 From: kurt at kbsymanzik.org (Kurt Symanzik) Date: Thu, 07 May 2009 11:01:44 +0800 Subject: Why there is a parameter named "self" for classmethod function? In-Reply-To: <2b767f890905061949g12d0bf03m68c6b466ffedfbd9@mail.gmail.com> References: <2b767f890905061949g12d0bf03m68c6b466ffedfbd9@mail.gmail.com> Message-ID: <4A024F18.9080407@kbsymanzik.org> Jianchun Zhou wrote on 2009-05-07 10:49:33 AM +0800 > I have a sample code as bellow: > > #!/usr/bin/env python > > class Hello: > def __init__(self): > print "Hello __init__" > @classmethod > def print_hello(self): > print "hello" > > Hello.print_hello() > > If I move "self" parameter of print_hello away, this code fragment won't > work. > > I am wondering when Hello.print_hello() executes, what value will "self" > be asigned? The self variable above with be populated with a reference to the class, so it would be more appropriately named cls such as: @classmethod def print_hello(cls): print "hello" But you might consider decorating the method as a static method instead since in your example you are not using the parameter at all. A static method would not require a parameter. @staticmethod def print_hello(): print "hello" Kurt -- Kurt Symanzik kurt at kbsymanzik.org Skype id: ksymanzik http://kbsymanzik.org From sjmachin at lexicon.net Wed May 6 23:11:17 2009 From: sjmachin at lexicon.net (John Machin) Date: Wed, 6 May 2009 20:11:17 -0700 (PDT) Subject: Could this expression parser be more 'Pythonic'? References: Message-ID: On May 7, 9:23?am, Amr wrote: > Hello all, > > I've been spending the last few weeks learning Python, and I've just > started to use it to write a simple BASIC compiler. I'm writing a > mathematical expression parser and wrote a function that would take a > string and split it into high level tokens. > > The code can be found athttp://pastebin.com/f757252ec(have a look at > the function tokenize). It's always a good idea to describe to yourself what you are actually trying to achieve before you get too embrolied in the implementation details. Adding a few more tests like this: if __name__ == '__main__': tests = """\ 1 + 2 * 3 (1 + 2) * 3 1 + (2 * 3) !@#$%^ foo = bar + zot foo=bar+zot xyzzy(bar, zot + 2.34) """ for test in tests.splitlines(): test = test.strip() e = Expression(test) result = e.tokenize(e.expression) print "%r => %r" % (test, result) produces this: '1 + 2 * 3' => ['1', '+', '2', '*', '3'] '(1 + 2) * 3' => ['1 + 2', '*', '3'] '1 + (2 * 3)' => ['1', '+', '2 * 3'] '!@#$%^' => ['!@#$%^'] 'foo = bar + zot' => ['foo', '=', 'bar', '+', 'zot'] 'foo=bar+zot' => ['foo=bar', '+', 'zot'] 'xyzzy(bar, zot + 2.34)' => ['xyzzy', 'bar, zot + 2.34'] '' => [] which indicates that your notions of what a token is and what you might use them for must be rather "interesting". Most other folks' tokenisers would have no concept of "bracket depth", would not throw the parentheses away, and would return not only the tokens but a classification as well e.g. 'xyzzy' ID '(' LPAREN 'bar' ID ',' LISTSEP 'zot' ID '+' OPER '2.34' FLOAT_CONST ')' RPAREN > I was interested to see if the tokenize function could be written in a > better way, so as to make better use of the Python language and > libraries. One thing that sprung to mind was whether it would be a > good idea to perhaps take advantage of regular expressions. I had a > look at a few compiler sites and regexps seem to come into that role > quite a bit. I've only done a little work on regexps and they seem > very powerful, but can easily get out of control! > > So, any suggestions as to how to make better use of the language > writing something like this? There are various packages like pyparsing, plex, ply, ... which are all worth looking at. However if you want to get stuck into the details, you can lash up a quick and dirty lexer (tokeniser) using regexps in no time flat, e.g. an ID is matched by r"[A-Za-z_][A-Za-z0-9_]*" an INT_CONST is matched by r"[0-9]+" an OPER is matched by r"[-+/*]" etc so you set up a (longish) re containing all the alternatives surrounded by capturing parentheses and separated by "|". r"([A-Za-z_][A-Za-z0-9_]*)|([0-9]+)|([-+/*])etc" Hint: don't do it manually, use ")|(".join() and you just step through your input string: 1. skip over any leading whitespace 2. look for a match of your re starting at current offset 3. matchobj.lastindex gives you 0 if it's an ID, 1 if it's an INT_CONST, etc 4. save the slice that represents the token 5. adjust offset, loop around until you hit the end of the input Hint: the order in which you list the alternatives can matter a lot e.g. you better have "<=" before "<" otherwise it would regard "<=" as comprising two tokens "<" and "=" HTH, John From apt.shansen at gmail.com Wed May 6 23:19:56 2009 From: apt.shansen at gmail.com (Stephen Hansen) Date: Wed, 6 May 2009 20:19:56 -0700 Subject: Simple way of handling errors In-Reply-To: References: <2009050616401916807-tomfsessile@gmailcom> Message-ID: <7a9c25c20905062019m2d428612i1f23f221bf8b898@mail.gmail.com> > If it fails, you get both a straight-forward error message and a useful > traceback: > > Traceback (most recent call last): > File "", line 1, in > IOError: [Errno 2] No such file or directory: 'foomanchu' > > > The only reason you would bother going to the time and effort of catching > the error, printing your own error message, and then exiting, is if you > explicitly want to hide the traceback from the user. > Unfortunately, I find that even halfway technically trained (not necessarily naturally inclined or talented, but trained) users are utterly unable to read tracebacks in real world situations. I had to put up a rather big guide on how to read and interpret even the simplest ones complete with color coding, "how to pick out a traceback visually", "what line to focus your attention on (the last)", and "how trying to /read/ it instead of immediately sending it to me as if it were an opaque symbol containing secret knowledge only I can make sense of, generally saves you time in debugging issues". I'm not talking about esoteric tracebacks either resulting from real bugs and stuff. I'm talking about 'file not found' 'permission denied' IO/OS level ones that should give serious hints at things people should check/diagnose themselves. There's a serious, "oh my god, traceback, /eyes glazed over/" reaction all around. Now these aren't unix people or command line people (except my rote), but still. I don't quite find printing out tracebacks to stdout or stderr ever acceptable :( Especially if its an expected error condition (file not found is a very good example of that). I route all my tracebacks into log files for diagnosing later (people also can not be relied upon to say anything but 'traceback' when they see one: so storing it for later is vital. They read the first word and stop. The thought of most-significant-information at the bottom is a difficult thing, alas) Anyways. ;-) That's just my experience. As to the OP: try/except is how you do it in Python. There's no shorter or easier way; exceptions are how Python handles failure-states (even soft/expected errors quite often) and it does so pervasively (but not universally). --S -------------- next part -------------- An HTML attachment was scrubbed... URL: From tomf.sessile at gmail.com Wed May 6 23:21:38 2009 From: tomf.sessile at gmail.com (TomF) Date: Wed, 6 May 2009 20:21:38 -0700 Subject: Simple way of handling errors References: <2009050616401916807-tomfsessile@gmailcom> Message-ID: <2009050620213875249-tomfsessile@gmailcom> On 2009-05-06 19:41:29 -0700, Steven D'Aprano said: > On Wed, 06 May 2009 16:40:19 -0700, TomF wrote: > >> As a relative newcomer to Python, I like it a lot but I'm dismayed at >> the difficulty of handling simple errors. In Perl if you want to >> anticipate a file-not-found error you can simply do: >> >> open($file) or die("open($file): $!"); >> >> and you get an intelligible error message. In Python, to get the same >> thing it appears you need at least: >> >> try: >> f=open(file) >> except IOError, err: >> print "open(%s): got %s" % (file, err.strerror) >> exit(-1) > > > Functions never fail silently in Python. (At least built-in functions > never fail silently. Functions you write yourself can do anything you > want.) Well, yes, I'm aware that if you don't handle errors Python barfs out a backtrace. > If it fails, you get both a straight-forward error message and a useful > traceback: > > Traceback (most recent call last): > File "", line 1, in > IOError: [Errno 2] No such file or directory: 'foomanchu' > > The only reason you would bother going to the time and effort of catching > the error, printing your own error message, and then exiting, is if you > explicitly want to hide the traceback from the user. Well, to me, exposing the user to such raw backtraces is unprofessional, which is why I try to catch user-caused errors. But I suppose I have an answer to my question. Thanks, -Tom From steven at REMOVE.THIS.cybersource.com.au Wed May 6 23:38:22 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 07 May 2009 03:38:22 GMT Subject: Simple way of handling errors References: <2009050616401916807-tomfsessile@gmailcom> <2009050620213875249-tomfsessile@gmailcom> Message-ID: On Wed, 06 May 2009 20:21:38 -0700, TomF wrote: >> The only reason you would bother going to the time and effort of >> catching the error, printing your own error message, and then exiting, >> is if you explicitly want to hide the traceback from the user. > > Well, to me, exposing the user to such raw backtraces is unprofessional, > which is why I try to catch user-caused errors. But I suppose I have an > answer to my question. That depends on your audience. Not every program is written to be used for a technical incompetent audience. Some users actually *want* to see the errors. But certainly there are large classes of applications where you do want to suppress the traceback. That's why I said "if you explicitly want to hide the traceback from the user" rather than "don't do this". The idiom I use is to wrap the *entire* application in a single try...except block, and then put all your user-friendly error handling in one place, instead of scattered over the entire application: try: main(sys.argv[1:]) except KeyboardInterrupt, SystemExit: raise except Exception, e: log(e) print >>sys.stderr, str(e) sys.exit(1) Hope this helps. -- Steven From smiles at worksmail.net Wed May 6 23:43:49 2009 From: smiles at worksmail.net (C or L Smith) Date: Thu, 7 May 2009 09:28:49 +0545 Subject: Parsing text Message-ID: <48E1A76D33C74BF888B381EB5E1F4E14@kisc.edu.np> > Hi, > I'm trying to write a fairly basic text parser to split up scenes and > acts in plays to put them into XML. I've managed to get the text split > into the blocks of scenes and acts and returned correctly but I'm > trying to refine this and get the relevant scene number when the split > is made but I keep getting an NoneType error trying to read the block > inside the for loop and nothing is being returned. I'd be grateful for > some suggestions as to how to get this working. > > for scene in text.split('Scene'): > num = re.compile("^\s\[0-9, i{1,4}, v]", re.I) > textNum = num.match(scene) > if textNum: > print textNum > else: > print "No scene number" > m = '
>> scene=re.compile('Scene\s+([1-9][0-9]*(iv|v?i{0,3}))?') >>> scene.split('The front matter Scene 1i The beginning was the best. Scene 1ii And then came the next act. Scene The last one has no number.') ['The front matter ', '1i', 'i', ' The beginning was the best. ', '1ii', 'ii', ' And then came the next act. ', None, None, 'The last one has no number.'] >>> ### [2] http://diveintopython.org/regular_expressions/roman_numerals.html From pierregmcode at gmail.com Wed May 6 23:51:35 2009 From: pierregmcode at gmail.com (Pierre GM) Date: Wed, 6 May 2009 20:51:35 -0700 (PDT) Subject: Logging exceptions to a file Message-ID: <597627b8-d30b-4b74-9202-9cd46fb1d129@s28g2000vbp.googlegroups.com> All, I need to log messages to both the console and a given file. I use the following code (on Python 2.5) >>> import logging >>> # >>> logging.basicConfig(level=logging.DEBUG,) >>> logfile = logging.FileHandler('log.log') >>> logfile.setLevel(level=logging.INFO) >>> logging.getLogger('').addHandler(logfile) >>> # >>> mylogger = logging.getLogger('mylogger') >>> # >>> mylogger.info("an info message") So far so good, but I'd like to record (possibly unhandled) exceptions in the logfile. * Do I need to explicitly trap every single exception ? * In that case, won't I get 2 log messages on the console (as illustrated in the code below: >>> try: >>> 1/0 >>> except ZeroDivisionError: >>> mylogger.exception(":(") >>> raise Any comments/idea welcomed Cheers. From ross.jett at gmail.com Thu May 7 00:02:47 2009 From: ross.jett at gmail.com (Ross) Date: Wed, 6 May 2009 21:02:47 -0700 (PDT) Subject: Code works fine except... References: <8d4ec1df-dddb-469a-99a1-695152db7c04@n4g2000vba.googlegroups.com> <1691f042-bc14-46b9-b7b3-5a44348e9965@b1g2000vbc.googlegroups.com> <1a497471-498a-43d8-90ba-d676e8acf68b@r36g2000vbr.googlegroups.com> <8b49b775-66a7-42d5-9b00-cf23d9b044cc@e23g2000vbe.googlegroups.com> <54d76f11-c255-487d-9c3a-aaa380d7827a@z7g2000vbh.googlegroups.com> <991ccae3-f3e2-4d73-bd32-bc6f560e6f5f@n8g2000vbb.googlegroups.com> Message-ID: <51631d39-ad19-4081-8e71-787cd565f252@s20g2000vbp.googlegroups.com> On May 6, 3:14?pm, John Yeung wrote: > On May 6, 3:29?am, MRAB wrote: > > > I have the feeling that if the number of rounds is restricted then the > > difference between the minimum and maximum number of byes could be 2 > > because of the requirement that players shouldn't play each other more > > than once, meaning that the players have to be shuffled around a bit, so > > a player might play a week earlier or later than would otherwise be the > > case. > > This is the feeling that I am getting also. ?All my efforts to keep > everything as balanced as possible at all times (to be ready for the > "season" to end suddenly at any time) result in messy jams that could > otherwise be alleviated if I allowed temporary imbalances, knowing > that there are more weeks later to make them up. > > John If I were to set up a dictionary that counted players used in the bye list and only allowed players to be added to the bye list if they were within 2 of the least used player, would this be a good approach for managing bye selection or would using a dictionary in this manner be unnecessary/redundant? From ross.jett at gmail.com Thu May 7 00:30:12 2009 From: ross.jett at gmail.com (Ross) Date: Wed, 6 May 2009 21:30:12 -0700 (PDT) Subject: Code works fine except... References: <8d4ec1df-dddb-469a-99a1-695152db7c04@n4g2000vba.googlegroups.com> <1691f042-bc14-46b9-b7b3-5a44348e9965@b1g2000vbc.googlegroups.com> <1a497471-498a-43d8-90ba-d676e8acf68b@r36g2000vbr.googlegroups.com> <8b49b775-66a7-42d5-9b00-cf23d9b044cc@e23g2000vbe.googlegroups.com> <54d76f11-c255-487d-9c3a-aaa380d7827a@z7g2000vbh.googlegroups.com> <991ccae3-f3e2-4d73-bd32-bc6f560e6f5f@n8g2000vbb.googlegroups.com> Message-ID: <1e781afa-598d-41e6-941f-76b7f8ca2a32@21g2000vbk.googlegroups.com> On May 6, 3:14?pm, John Yeung wrote: > On May 6, 3:29?am, MRAB wrote: > > > I have the feeling that if the number of rounds is restricted then the > > difference between the minimum and maximum number of byes could be 2 > > because of the requirement that players shouldn't play each other more > > than once, meaning that the players have to be shuffled around a bit, so > > a player might play a week earlier or later than would otherwise be the > > case. > > This is the feeling that I am getting also. ?All my efforts to keep > everything as balanced as possible at all times (to be ready for the > "season" to end suddenly at any time) result in messy jams that could > otherwise be alleviated if I allowed temporary imbalances, knowing > that there are more weeks later to make them up. > > John If I were to set up a dictionary that counted players used in the bye list and only allowed players to be added to the bye list if they were within 2 of the least used player, would this be a good approach for managing bye selection or would using a dictionary in this manner be unnecessary/redundant? From ross.jett at gmail.com Thu May 7 00:30:26 2009 From: ross.jett at gmail.com (Ross) Date: Wed, 6 May 2009 21:30:26 -0700 (PDT) Subject: Code works fine except... References: <8d4ec1df-dddb-469a-99a1-695152db7c04@n4g2000vba.googlegroups.com> <1691f042-bc14-46b9-b7b3-5a44348e9965@b1g2000vbc.googlegroups.com> <1a497471-498a-43d8-90ba-d676e8acf68b@r36g2000vbr.googlegroups.com> <8b49b775-66a7-42d5-9b00-cf23d9b044cc@e23g2000vbe.googlegroups.com> <54d76f11-c255-487d-9c3a-aaa380d7827a@z7g2000vbh.googlegroups.com> <991ccae3-f3e2-4d73-bd32-bc6f560e6f5f@n8g2000vbb.googlegroups.com> Message-ID: <1d8802ff-8211-467f-95c1-4616e3bd7300@s20g2000vbp.googlegroups.com> On May 6, 3:14?pm, John Yeung wrote: > On May 6, 3:29?am, MRAB wrote: > > > I have the feeling that if the number of rounds is restricted then the > > difference between the minimum and maximum number of byes could be 2 > > because of the requirement that players shouldn't play each other more > > than once, meaning that the players have to be shuffled around a bit, so > > a player might play a week earlier or later than would otherwise be the > > case. > > This is the feeling that I am getting also. ?All my efforts to keep > everything as balanced as possible at all times (to be ready for the > "season" to end suddenly at any time) result in messy jams that could > otherwise be alleviated if I allowed temporary imbalances, knowing > that there are more weeks later to make them up. > > John If I were to set up a dictionary that counted players used in the bye list and only allowed players to be added to the bye list if they were within 2 of the least used player, would this be a good approach for managing bye selection or would using a dictionary in this manner be unnecessary/redundant? From tjreedy at udel.edu Thu May 7 00:33:26 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 07 May 2009 00:33:26 -0400 Subject: Self function In-Reply-To: References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> <1292fea0-96fb-4a21-b39d-b6ce610ea6b2@t10g2000vbg.googlegroups.com> <4f13a110-b3e5-4de5-b6d6-0d64da20b61e@r34g2000vbi.googlegroups.com> <1241649200.4a0210301f77e@mail.uh.cu> Message-ID: Steven D'Aprano wrote: I am writing a book (with Python package) on algorithms that has *lots* of recursive functions. I have also discovered that changing names can be a pain. So in the text, (but not code) I have tried the equivalent of # real_name def f(params): ... f(args) (This also keeps lines short enough for side-by-side versions, when I want to do that.) > But regardless, everyone is missing the most important point: why are you > copying and pasting code in the first place? In my case, I will be discussing variations of algorithms. Copy and edit minimizes fatigue and errors and maximizes parallelism. That is surely very close to > the top of the list of Worst Ever Anti-Patterns, and it should be avoided > whenever possible. Not if the alternative is re-typing ;-). > In Python, we can avoid much copy-and-paste coding with decorators. But not when the variation is in the guts of the code. > because recursive functions in Python don't actually call themselves, > they call whatever happens to be bound to their name at runtime. I have mentioned that. Also, apparently non-recursive functions can become recursive after the fact by being bound to another name used in the function. tjr From tjreedy at udel.edu Thu May 7 00:39:28 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 07 May 2009 00:39:28 -0400 Subject: Why there is a parameter named "self" for classmethod function? In-Reply-To: <4A024F18.9080407@kbsymanzik.org> References: <2b767f890905061949g12d0bf03m68c6b466ffedfbd9@mail.gmail.com> <4A024F18.9080407@kbsymanzik.org> Message-ID: Kurt Symanzik wrote: > > But you might consider decorating the method as a static method instead > since in your example you are not using the parameter at all. A static > method would not require a parameter. > > @staticmethod > def print_hello(): > print "hello" Functions that refer to neither the class nor an instance thereof can usually be moved outside the class altogether. Python is not Java. I believe staticmethod() was mainly added because it is needed for .__new__(), at least in some appearances. tjr From gallium.arsenide at gmail.com Thu May 7 01:11:04 2009 From: gallium.arsenide at gmail.com (John Yeung) Date: Wed, 6 May 2009 22:11:04 -0700 (PDT) Subject: Code works fine except... References: <8d4ec1df-dddb-469a-99a1-695152db7c04@n4g2000vba.googlegroups.com> <1a497471-498a-43d8-90ba-d676e8acf68b@r36g2000vbr.googlegroups.com> <8b49b775-66a7-42d5-9b00-cf23d9b044cc@e23g2000vbe.googlegroups.com> <54d76f11-c255-487d-9c3a-aaa380d7827a@z7g2000vbh.googlegroups.com> <991ccae3-f3e2-4d73-bd32-bc6f560e6f5f@n8g2000vbb.googlegroups.com> <1d8802ff-8211-467f-95c1-4616e3bd7300@s20g2000vbp.googlegroups.com> Message-ID: On May 7, 12:30?am, Ross wrote: > > If I were to set up a dictionary that counted players used in the bye > list and only allowed players to be added to the bye list if they were > within 2 of the least used player, would this be a good approach for > managing bye selection or would using a dictionary in this manner be > unnecessary/redundant? I certainly have not proved it, but I think you don't need to resort to anything fancy if you are OK with the maximum byes being within two of the minimum byes. (Maybe this needs to be larger with larger numbers of players.) Try using your original shuffle but instead of selecting matches to "throw away" each week, just use the matches you need (to fill up the courts) and pick up where you left off the next week. For example, with 10 players, each "round" ideally consists of five matches. If you only have four courts, don't throw away the fifth match; save it as the first match next week. To be honest, I didn't look too carefully at your original shuffle. It may be good enough. It's a little different than the "standard" rotation as presented on Wikipedia: http://en.wikipedia.org/wiki/Round-robin_tournament#Scheduling_algorithm The one on Wikipedia happened to pass my casual tests with no more than a 2-bye difference between the most-played and least-played players, and didn't run into the problem of scheduling the same player for two matches the same week. But I have to stress I only tried a few starting values, which all worked; I didn't try to break it, or run an extensive battery of tests. John From ganesh.gbg at gmail.com Thu May 7 01:17:17 2009 From: ganesh.gbg at gmail.com (gganesh) Date: Wed, 6 May 2009 22:17:17 -0700 (PDT) Subject: problem in using sendmail in multi thread References: Message-ID: <8254344f-a31e-489a-9adc-7f0eee9915e2@g31g2000pra.googlegroups.com> On May 6, 7:10?pm, Jean-Paul Calderone wrote: > On Tue, 5 May 2009 22:17:35 -0700 (PDT), gganesh wrote: > >On May 5, 9:25?pm, Piet van Oostrum wrote: > >> >>>>> gganesh (g) wrote: > >> >g> hi, > >> >g> I'm a beginner in using Python script > >> >g> I'm trying to send mails using multi-thread > >> >g> I wrote > >> >g> FROM = 'ganeshx... at xxxx.com' > >> >g> # for more mail add';' the another mail id > >> >g> listTo = ['g.... at gmail.com', 'xxjango... at gmail.com', > >> >g> 'xx... at yahoo.co.in'] > >> >g> SUBJECT = 'This is the subject' > >> >g> MSGBODY = 'This the body of the message ' > >> >g> MAILSERVER = 'mail.xxxx.com' > >> >g> port = 25 > >> >g> username = 'xxxxx' > >> >g> password = 'xxxxx' > >> >g> # trim the strings of any leading or trailing spaces > >> >g> FROM = FROM.strip() > >> >g> SUBJECT = SUBJECT.strip() > >> >g> MSGBODY = MSGBODY.strip() > >> >g> MAILSERVER = MAILSERVER.strip() > >> >g> username = username.strip() > >> >g> password = password.strip() > >> >g> #Connect to server > >> >g> print 'Connecting to mail server ', MAILSERVER > >> >g> try: > >> >g> ? ? ? s = smtplib.SMTP(MAILSERVER,port) > > >> You can't have a single SMTP connection that's used in multiple threads. > >> That is what causes the error. Each thread should have its own SMTP > >> connection. So move this code (above and below) into the run() method. > > >> >g> ? ? ? print 'connected' > >> >g> #s.set_debuglevel(1) > >> >g> except: > >> >g> ? ? ? ?print 'ERROR: Unable to connect to mail server', sys.exc_info ?()[0] > >> >g> ? ? ? ?sys.exit(1) > >> >g> #login to server > >> >g> if password <> '': > >> >g> ? ? ? print 'Logging into mail server' > > >> I think this try except block should be inside the if statement, i.e. > >> indented 4 spaces. > > >> >g> try: > >> >g> ? ? ? s.login(username,password) > >> >g> except: > >> >g> ? ? ? print 'ERROR: Unable to login to mail server', MAILSERVER > >> >g> ? ? ? print 'Please recheck your password' > >> >g> ? ? ? sys.exit(1) > >> >g> #-------------------------------------------------- > >> >g> print "Starting Multi Thread Method" > >> >g> class MyThread(Thread): > >> >g> ? ? def __init__(self, site, s, FROM, MSGBODY): > >> >g> ? ? ? ? Thread.__init__(self) > >> >g> ? ? ? ? self.site = site > >> >g> ? ? ? self.s=s > >> >g> ? ? ? self.FROM=FROM > >> >g> ? ? ? self.MSGBODY=MSGBODY > > >> You give the s (connection) here as a parameter, store it in self.s and > >> never use that attribute. > > >> >g> ? ? def run(self): > >> >g> ? ? ? print "running for %s " %self.site > >> >g> ? ? ? s.sendmail(self.FROM, self.site, self.MSGBODY) > > >> Here you use the global s, not self.s > > >> As I said above you should do the SMTP connection setup, including the > >> login, here, and store the connection either in self.s or in a local > >> variable s in the method. As you don't use the s in another method, I > >> think a local variable is better. > > >> >g> ? ? ? print "Emailed for ?site %s" %self.site > >> >g> a= time.time() > >> >g> threads = [] > >> >g> for site in listTo: > >> >g> ? ? T = MyThread(site,s,FROM,MSGBODY) > >> >g> ? ? threads.append(T) > >> >g> ? ? T.start() > >> >g> for i in threads: > >> >g> ? ? i.join() > > >> Of course the next 2 lines should also be moved to run(). > > >> >g> s.quit() > >> >g> s.close() > >> >g> print "Took %s seconds" %str(time.time()-a) > >> >g> #----------------------------------------------------- > > >> -- > >> Piet van Oostrum > >> URL:http://pietvanoostrum.com[PGP8DAE142BE17999C4] > >> Private email: p... at vanoostrum.org > > >Thanks Everyone By your guidance the code worked fine > >I can send mails in multi threaded environment. > >Is this only way to send mails concurrently ?or any other method > >aviable? > > Here's another way: > > ? ? from twisted.mail.smtp import sendmail > ? ? from twisted.internet import reactor > ? ? from twisted.python.log import err > > ? ? MAILSERVER = ... > ? ? listTo = [...] > ? ? FROM = ... > ? ? MSGBODY = ... > > ? ? done = sendmail(MAILSERVER, FROM, listTo, MSGBODY) > ? ? done.addErrback(err) > ? ? done.addCallback(lambda ignored: reactor.stop()) > ? ? reactor.run() > > Jean-Paul Hi Jean-Paul This is what I'm searching for ,Thanks a lot Based on your posting I wrote a programme,but it showed me a error like "Failure: twisted.mail.smtp.SMTPDeliveryError: 554 No recipients accepted" and "Relay access denied" errors I can clearly figure out, that this error is because, i haven't authenticated my email account to send mail. I have username and password of smpt server ,how to authenticate in Twisted Framework From sasurve at gmail.com Thu May 7 02:06:44 2009 From: sasurve at gmail.com (shruti surve) Date: Thu, 7 May 2009 11:36:44 +0530 Subject: About Odfpy links Message-ID: <862e00220905062306q6b7519e3h3a1ece11c8dd9462@mail.gmail.com> hey all, For my project, i am using ODFpy open office spreadsheets. I am creating ledgers in python ie.ledger.py. So when i run ledger.py, spreadsheet will be opened in open office. since ledger is made of number of accounts, i am creating multiple tables for all accounts in single spreadsheet. So, there are number of tables in one spreadsheet. Now i want create links in each table of spreadsheet so that i can connect multiple tables. SO can anyone please tell me how to create links in odfpy.. -------------- next part -------------- An HTML attachment was scrubbed... URL: From steven at REMOVE.THIS.cybersource.com.au Thu May 7 02:29:57 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 07 May 2009 06:29:57 GMT Subject: Why there is a parameter named "self" for classmethod function? References: <2b767f890905061949g12d0bf03m68c6b466ffedfbd9@mail.gmail.com> <4A024F18.9080407@kbsymanzik.org> Message-ID: On Thu, 07 May 2009 00:39:28 -0400, Terry Reedy wrote: > Functions that refer to neither the class nor an instance thereof can > usually be moved outside the class altogether. Python is not Java. I > believe staticmethod() was mainly added because it is needed for > .__new__(), at least in some appearances. Can be, but if there's reason enough to keep it with a class, there's no reason not to. Sometimes I have a class with a few methods that share common code, but that code happens to not include self (or cls). Since the common code is only of interest to that class, even though it doesn't need the instance, I factor the common code out into a method. This would be a good candidate for a staticmethod, only due to laziness I don't usually bother :) -- Steven From ldo at geek-central.gen.new_zealand Thu May 7 02:43:02 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Thu, 07 May 2009 18:43:02 +1200 Subject: Logging exceptions to a file References: <597627b8-d30b-4b74-9202-9cd46fb1d129@s28g2000vbp.googlegroups.com> Message-ID: In message <597627b8- d30b-4b74-9202-9cd46fb1d129 at s28g2000vbp.googlegroups.com>, Pierre GM wrote: > ... I'd like to record (possibly unhandled) exceptions in the logfile. python myscript.py 2>error.log From sasurve at gmail.com Thu May 7 02:46:26 2009 From: sasurve at gmail.com (shruti surve) Date: Thu, 7 May 2009 12:16:26 +0530 Subject: Python-list Digest, Vol 68, Issue 79 In-Reply-To: References: Message-ID: <862e00220905062346te99fe1bk320fd5bd43c37fcb@mail.gmail.com> > > > 3. Re: About ODFPY links (Terry Reedy) > > > > > > > ---------- Forwarded message ---------- > From: Terry Reedy > To: python-list at python.org > Date: Wed, 06 May 2009 19:02:59 -0400 > Subject: Re: About ODFPY links > shruti surve wrote: > >> hey all, >> For my project, i am using ODFpy open office spreadsheets. I am creating >> ledgers in python ie.ledger.py . So when i run >> ledger.py, spreadsheet will be opened in open office. since ledger is made >> of number of accounts, i am creating multiple tables for all accounts in >> single spreadsheet. So, there are number of tables in one spreadsheet. Now >> i want create links in each table of spreadsheet so that i can connect >> multiple tables. SO can anyone please tell me how to create links in odfpy.. >> > > The general answer to such questions is to make a minimal doc in some ODF > application such of OOo that has the desired feature, save, open the doc as > a zip file (unless saved as xml) and extract the pieces as xml. Then find > the text that represents that feature. There is a close correspondance > between the xml and the internal document object model created by odfpy (and > OOo, etc). > > In your case, make a spreadsheet with two table. Give one one literal > entry. Give the other one entry that links to the first. Then look in the > xml to see how links are presented. That should give a hint as to how to do > the same in odfpy. Then try to recreate the minimal doc with odfpy and look > at the xml *it* produces. If it looks similar enough, open with OOo to see > what you get. > > tjr hi i have tried that and i found Investments...so in my ledger.py i have written: para = P(text=lst_debit[i][0]) lan=A(href=lst_debit[i][0],targetframename="_self",text=lst_debit[i][0]) lan.addElement(para) tc.addElement(lan) tr.addElement(tc) (where lst_debit[i][0] is my account name coming from database) but still its not coming erroris : in addElement odf.element.IllegalChild: is not allowed in i have tried it with: tc.addElement(P(text=lst_credit[i][0]),A(href=lst_debit[i][0],targetframename="_self")) but still its not coming, it is not giving any error, but not creating link, giving simple text field.. so i basically i am not getting how to manage P and A properties of cell together Regards shruti surve -------------- next part -------------- An HTML attachment was scrubbed... URL: From OldGrantonian at googlemail.com Thu May 7 03:03:11 2009 From: OldGrantonian at googlemail.com (OldGrantonian) Date: Thu, 7 May 2009 00:03:11 -0700 (PDT) Subject: Which python version do I use with "virtualenv"? Message-ID: I have Windows Vista Home Premium. As a non-techy, I want to use "virtualenv" I had Python 2.6 on my laptop. I needed "easyinstall" to install virtualenv. During installation of easyinstall, I got the message "Python 2.5 not found" So I installed Python 2.5, then installed virtualenv So I now have c:\Python 2.5 and c:\Python 2.6 On the virtualenv web site, the instructions for use are: $ python virtualenv.py ENV My question is, which "python" should I use on this command line: 2.5 or 2.6? BTW: I am currently running a Python app using 2.6. I want to install a beta upagrade of this app, without interfering with my current setup, so the app developer recommends using virtualenv. From googler.1.webmaster at spamgourmet.com Thu May 7 03:20:09 2009 From: googler.1.webmaster at spamgourmet.com (googler.1.webmaster at spamgourmet.com) Date: Thu, 7 May 2009 00:20:09 -0700 (PDT) Subject: Threading and GIL References: <60538e1c-47ea-4120-bc91-a9dcebd8f14b@r34g2000vbi.googlegroups.com> Message-ID: Hi, thats the reason why its not working. Imagine the end() method of the thread object is called so the C++ Function is opened where the code for this method is in. At a line the Code ...->End() is called which waits that the C++ Thread class is finished. BUT here is the problem: In the Method of the C++ class which is in threaded mode can't run because its still waiting that the GIL is released by the thread which executed the ->End() command. So the app has a deadlock when it arrives at ->End() and PyGILState_Ensure function. From fetchinson at googlemail.com Thu May 7 03:29:29 2009 From: fetchinson at googlemail.com (Daniel Fetchinson) Date: Thu, 7 May 2009 00:29:29 -0700 Subject: [Python-Dev] [RELEASED] Python 3.1 beta 1 In-Reply-To: <1afaf6160905061832xfc295e3y881c7c8e81083ee6@mail.gmail.com> References: <1afaf6160905061832xfc295e3y881c7c8e81083ee6@mail.gmail.com> Message-ID: > On behalf of the Python development team, I'm thrilled to announce the first > and > only beta release of Python 3.1. .............. > Other features include an ordered dictionary implementation Are there plans for backporting this to python 2.x just as multiprocessing has been? I know that there are several ordered dictionary implementations for 2.x but they are all a little bit different from the one going into 3.1, hence my question. Cheers, Daniel -- Psss, psss, put it down! - http://www.cafepress.com/putitdown From Lacrima.Maxim at gmail.com Thu May 7 03:34:52 2009 From: Lacrima.Maxim at gmail.com (Lacrima) Date: Thu, 7 May 2009 00:34:52 -0700 (PDT) Subject: Checking for required arguments when instantiating class. References: Message-ID: <7c9defb8-9b4a-4062-a79f-ff6683c6c9f6@m24g2000vbp.googlegroups.com> On May 6, 3:36?pm, Chris Rebert wrote: > On Wed, May 6, 2009 at 5:24 AM, Piet van Oostrum wrote: > > > > >>>>>> Lacrima (L) wrote: > > >>L> Hello! > >>L> For example I have two classes: > > >>>>>> class First: > >>L> ? ? def __init__(self, *args, **kwargs): > >>L> ? ? ? ? ? ? pass > > >>>>>> class Second: > >>L> ? ? def __init__(self, somearg, *args, **kwargs): > >>L> ? ? ? ? ? ? self.somearg = somearg > > >>L> How can I test that First class takes 1 required argument and Second > >>L> class takes no required arguments? > >>L> So that I could instantiate them in a for loop. > > >>>>>> a = [First, Second] > >>>>>> for cls in a: > >>L> ? ? instance = cls() > > >>L> Traceback (most recent call last): > >>L> ? File "", line 2, in > >>L> ? ? instance = cls() > >>L> TypeError: __init__() takes at least 2 arguments (1 given) > > >>L> Of course, I can do like this: > >>>>>> for cls in a: > >>L> ? ? try: > >>L> ? ? ? ? ? ? instance = cls() > >>L> ? ? except TypeError: > >>L> ? ? ? ? ? ? instance = cls('hello') > > >>>>>> print instance.somearg > >>L> hello > > >>L> But what if I have to instantiate any class with 3 or 4 required > >>L> arguments? How can I do it? > > > cls.__init__.im_func.__code__.co_argcount > > > This will include self, so it will be 1 in First and 2 in Second. > > AFAICT, that would count non-required arguments too, which isn't > strictly what the OP requested. > > > However this is very dirty trickery and should not be recommended. It > > may also change in future versions and other implementations of Python. > > Very much agreed. > > > I think it would be cleaner to put a class attribute in the classes that > > defines how they should be initialized (e.g. just the number of required > > arguments or more specific information) or have a special factory method > > for this use case. > > Seconded. I'd recommend the latter personally, though it's impossible > to give a definitive answer without more context. > > Cheers, > Chris > --http://blog.rebertia.com Thanks for all of you! I think I'll try to write a special method for this case and will report you result. -Max From steven at REMOVE.THIS.cybersource.com.au Thu May 7 03:46:06 2009 From: steven at REMOVE.THIS.cybersource.com.au (Steven D'Aprano) Date: 07 May 2009 07:46:06 GMT Subject: Which python version do I use with "virtualenv"? References: Message-ID: On Thu, 07 May 2009 00:03:11 -0700, OldGrantonian wrote: > So I now have c:\Python 2.5 and c:\Python 2.6 > > On the virtualenv web site, the instructions for use are: > > $ python virtualenv.py ENV > > My question is, which "python" should I use on this command line: 2.5 or > 2.6? I'm not an expert on virtualenv, nor do I use Windows, but I'd start off with this: $ c:\Python 2.5\python.exe vitualenv.py ENV and if that doesn't do what you expect, report the precise error you get and hopefully somebody who knows more about Windows can help. -- Steven From __peter__ at web.de Thu May 7 04:01:57 2009 From: __peter__ at web.de (Peter Otten) Date: Thu, 07 May 2009 10:01:57 +0200 Subject: Simple way of handling errors References: <2009050616401916807-tomfsessile@gmailcom> Message-ID: TomF wrote: > As a relative newcomer to Python, I like it a lot but I'm dismayed at > the difficulty of handling simple errors. In Perl if you want to > anticipate a file-not-found error you can simply do: > > open($file) or die("open($file): $!"); > > and you get an intelligible error message. In Python, to get the same > thing it appears you need at least: > > try: > f=open(file) > except IOError, err: > print "open(%s): got %s" % (file, err.strerror) > exit(-1) > > Is there a simpler interface or idiom for handling such errors? I > appreciate that Python's exception handling is much more sophisticated > but often I don't need it. > > -Tom While you are making the transition you could write from perl_idioms import open_or_die f = open_or_die("does-not-exist") with the perl_idioms module looking like import sys def open_or_die(*args): try: return open(*args) except IOError, e: sys.exit(e) Peter From r-w at inet.net.au Thu May 7 04:41:04 2009 From: r-w at inet.net.au (r-w) Date: Thu, 07 May 2009 18:41:04 +1000 Subject: idea Message-ID: <4A029EA0.3030403@inet.net.au> Redirect sys.stderr to the log file in ANUGA logging. This might catch unexpected exceptions. From lie.1296 at gmail.com Thu May 7 05:08:17 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 07 May 2009 09:08:17 GMT Subject: list comprehension question In-Reply-To: References: <6d502cfd-e252-4722-96a5-c52fc3579499@v1g2000prd.googlegroups.com> <85ocucnbce.fsf@agentultra.com> <85ljpb8r1o.fsf@agentultra.com> <85d4am8hq4.fsf@agentultra.com> Message-ID: <5yxMl.9285$y61.6301@news-server.bigpond.net.au> Steven D'Aprano wrote: >> "If you?ve got the stomach for it, list comprehensions can be nested. >> They are a powerful tool but ? like all powerful tools ? they need to be >> used carefully, if at all." > > How does this discourage the use of list comprehensions? At most, it > warns that complicated list comps are tricky. Complicated *anything* are > tricky. > > >> and >> >> "In real world, you should prefer builtin functions to complex flow >> statements." > > That's ridiculous. The example given is a special case. That's like > saying "Loops are hard, so in the real world, if you want a loop, find a > builtin function that does what you want instead." > > What's the "builtin function" we're supposed to prefer over a "complex > flow statement" like this? > > # split text into word fragments of length <= 3 > sentence = "a sentence is a series of words" > new = [word[i:i+3] for word in sentence.split() for i in range(0, len(word), 3)] I often found list comprehension *more* readable than the equivalent for-loop because of its density. Seeing complex for-loop requires one step of thinking for each line, but in list comprehension I can skip some of the steps because I know what list comprehension would roughly look like. i.e. when I process this: lst = [] #1 for i in range(10): #2 lst.append(i) #3 I do 3 steps of thinking for each line but seeing this is only one step, since I know it is going to create new list (skipping step #1) and I know i will be appended to that list (skipping step #3) From lie.1296 at gmail.com Thu May 7 05:18:56 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 07 May 2009 09:18:56 GMT Subject: list comprehension question In-Reply-To: <5yxMl.9285$y61.6301@news-server.bigpond.net.au> References: <6d502cfd-e252-4722-96a5-c52fc3579499@v1g2000prd.googlegroups.com> <85ocucnbce.fsf@agentultra.com> <85ljpb8r1o.fsf@agentultra.com> <85d4am8hq4.fsf@agentultra.com> <5yxMl.9285$y61.6301@news-server.bigpond.net.au> Message-ID: <4IxMl.9287$y61.5756@news-server.bigpond.net.au> Lie Ryan wrote: > Steven D'Aprano wrote: >>> "If you?ve got the stomach for it, list comprehensions can be nested. >>> They are a powerful tool but ? like all powerful tools ? they need to be >>> used carefully, if at all." >> >> How does this discourage the use of list comprehensions? At most, it >> warns that complicated list comps are tricky. Complicated *anything* >> are tricky. >> >> >>> and >>> >>> "In real world, you should prefer builtin functions to complex flow >>> statements." >> >> That's ridiculous. The example given is a special case. That's like >> saying "Loops are hard, so in the real world, if you want a loop, find >> a builtin function that does what you want instead." >> >> What's the "builtin function" we're supposed to prefer over a "complex >> flow statement" like this? >> >> # split text into word fragments of length <= 3 >> sentence = "a sentence is a series of words" >> new = [word[i:i+3] for word in sentence.split() for i in range(0, >> len(word), 3)] > > I often found list comprehension *more* readable than the equivalent > for-loop because of its density. Seeing complex for-loop requires one > step of thinking for each line, but in list comprehension I can skip > some of the steps because I know what list comprehension would roughly > look like. > > i.e. > > when I process this: > > lst = [] #1 > for i in range(10): #2 > lst.append(i) #3 > > I do 3 steps of thinking for each line > > but seeing this is only one step, since I know it is going to create new > list (skipping step #1) and I know i will be appended to that list > (skipping step #3) I don't know what I was thinking before sending that last line, here is what it was supposed to be: but in list comprehension I see this in only one step, since I know it is going to create new list (skipping step #1) and I know 'i' will be appended to that list (skipping step #3) and this also continues to nested list comprehensions From antroy at gmail.com Thu May 7 05:20:03 2009 From: antroy at gmail.com (Ant) Date: Thu, 7 May 2009 02:20:03 -0700 (PDT) Subject: Which python version do I use with "virtualenv"? References: Message-ID: On May 7, 8:03?am, OldGrantonian wrote: ... > I had Python 2.6 on my laptop. > > I needed "easyinstall" to install virtualenv. During installation of > easyinstall, I got the message "Python 2.5 not found" > > So I installed Python 2.5, then installed virtualenv > > So I now have c:\Python 2.5 and c:\Python 2.6 ... Sounds like you've downloaded the Python 2.5 version of Easy Install. If I were you I'd uninstall Python 2.5, and re-install easy_install using the correct exe for Python 2.6. Check that typing 'python' on the commandline still works, as installing and uninstalling 2.5 may have messed up your path. if it has, either manually add the Python install directory to the path, or reinstall Python 2.6. Note: have you changed the default install folder? I haven't got Vista, but on XP it installs to c:\Python26 not "C:\Python 2.6". I'd be carefull of changing the default install directory of Python, particularly if you are "non-techy" as some packages installers may look for Python in the default location. -- Ant From lie.1296 at gmail.com Thu May 7 05:23:30 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 07 May 2009 09:23:30 GMT Subject: list comprehension question In-Reply-To: <0_qdnYwTacv122bUnZ2dnUVZ_rqdnZ2d@pdx.net> References: <6d502cfd-e252-4722-96a5-c52fc3579499@v1g2000prd.googlegroups.com> <50697b2c0904301805p7158e167n7f84b18afd0f55f3@mail.gmail.com> <49FB2225.4030601@councilforeconed.org> <0_qdnYwTacv122bUnZ2dnUVZ_rqdnZ2d@pdx.net> Message-ID: Scott David Daniels wrote: > John Posner wrote: >> Shane Geiger wrote: >>> if type(el) == list or type(el) is tuple: >> A tiny improvement: >> >> if type(el) in (list, tuple): > > or (even better) > if isinstance(el, (list, tuple)) > > However, it is my contention that you shouldn't be flattening by type -- > you should know where, explicitly to flatten. If I have 3-D points as > triples, a tree of points would, by this code, get flattened into a > a list of numbers. If, however, I create a class with the same > elements, but a method or two, suddenly flatten will produce a list > of points. It is my contention that the tree abstraction should be > responsible for producing its leaves, rather than a blunderbus that > doesn't know where one container abstraction ends, and its contents > begin. In other words, I'd like to see thigs like "flatten one layer." Maybe that is why some modules like PIL or pygame accepts list of coordinates in flattened list of number instead of nested (i.e. they accepts [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] and do the grouping themself). From lie.1296 at gmail.com Thu May 7 05:32:38 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 07 May 2009 09:32:38 GMT Subject: Logging exceptions to a file In-Reply-To: <597627b8-d30b-4b74-9202-9cd46fb1d129@s28g2000vbp.googlegroups.com> References: <597627b8-d30b-4b74-9202-9cd46fb1d129@s28g2000vbp.googlegroups.com> Message-ID: Pierre GM wrote: > All, > I need to log messages to both the console and a given file. I use the > following code (on Python 2.5) > >>>> import logging >>>> # >>>> logging.basicConfig(level=logging.DEBUG,) >>>> logfile = logging.FileHandler('log.log') >>>> logfile.setLevel(level=logging.INFO) >>>> logging.getLogger('').addHandler(logfile) >>>> # >>>> mylogger = logging.getLogger('mylogger') >>>> # >>>> mylogger.info("an info message") > > So far so good, but I'd like to record (possibly unhandled) exceptions > in the logfile. > * Do I need to explicitly trap every single exception ? > * In that case, won't I get 2 log messages on the console (as > illustrated in the code below: >>>> try: >>>> 1/0 >>>> except ZeroDivisionError: >>>> mylogger.exception(":(") >>>> raise > > Any comments/idea welcomed > Cheers. > Although it is usually not recommended to use a catch-all except, this is the case where it might be useful. JUST DON'T FORGET TO RE-RAISE THE EXCEPTION. if __name__ == '__main__': try: main(): except Exception, e: # log('Unhandled Exception', e) raise From cournape at gmail.com Thu May 7 05:33:10 2009 From: cournape at gmail.com (David Cournapeau) Date: Thu, 7 May 2009 18:33:10 +0900 Subject: Statically linked extension and relative import Message-ID: <5b8d13220905070233m4140472xd8fbcdc74985d073@mail.gmail.com> Hi, I am trying to build a 3rd party extension and link it statically to python. I managed to get things working by customizing Setup.local in python source tree, but I have a problem for imports of the 'foo.bar' form. For example, let's say the 3rd party module is laid out as follows: foo/__init__.py foo/bar.so and __init__.py contains: import foo.bar I can build the bar extension and link it statically to python such as from python, "import bar" works. But how can I make it "recognize" "from foo import bar" ? I tried to understand how this works at the C level in import.c, but I am a bit lost in the code. The ultimate goal is to manage to get code coverage of the C code of the extension through gcov, which does not support coverage of shared libraries ATM. thanks, David From deets at nospam.web.de Thu May 7 06:26:59 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Thu, 07 May 2009 12:26:59 +0200 Subject: idea References: <4A029EA0.3030403@inet.net.au> Message-ID: <76fr8gF1cosptU1@mid.uni-berlin.de> r-w wrote: > Redirect sys.stderr to the log file in ANUGA logging. > This might catch unexpected exceptions. brillant. Diez From OJOHANS at StatoilHydro.com Thu May 7 06:54:45 2009 From: OJOHANS at StatoilHydro.com (=?iso-8859-1?Q?=D8ystein_Johansen_=28OJOHANS=29?=) Date: Thu, 7 May 2009 12:54:45 +0200 Subject: subprocess.Popen howto? Message-ID: Hi, I have problems understanding the subprocess.Popen object. I have a iterative calculation in a process running and I want to pipe the output (stdout) from this calculation to a Python script. Let me include a simple code that simulates the calculating process: /* This code simulates a big iterative calculation */ #include #include int main() { float val[2] = { M_PI, M_E }; int i; for ( i = 0; i < 2 i++) { sleep( 15 ); /* It's a hard calculation. It take 15 seconds */ printf("Value: %5.6f\n", val[i] ); fflush( stdout ); } return 0; } let's compile this to mycalc: gcc -o mycalc calc.c ... (untested code) In C I have this code which starts the mycalc process and handles the output from it: #include #include #define BUF_SIZE 256 int main() { FILE *pip; char line[BUF_SIZE]; pip = popen("mycalc", "r"); assert( pip != NULL ); while ( fgets( line, BUF_SIZE, pip )) { printf( "Hello; I got: %s \n", line ); fflush( stdout ); } pclose( pip ); return 0; } How can I make such while-loop in Python? I assume I should use subprocess.Popen(), but I can't figure out how? -?ystein ------------------------------------------------------------------- The information contained in this message may be CONFIDENTIAL and is intended for the addressee only. Any unauthorised use, dissemination of the information or copying of this message is prohibited. If you are not the addressee, please notify the sender immediately by return e-mail and delete this message. Thank you. -------------- next part -------------- An HTML attachment was scrubbed... URL: From OldGrantonian at googlemail.com Thu May 7 06:58:50 2009 From: OldGrantonian at googlemail.com (OldGrantonian) Date: Thu, 7 May 2009 03:58:50 -0700 (PDT) Subject: Which python version do I use with "virtualenv"? References: Message-ID: <61fc8aa3-764c-465b-86aa-1b25631609e4@r34g2000vba.googlegroups.com> Thanks to both Steven D'Aprano and Ant :) >> Sounds like you've downloaded the Python 2.5 version of Easy Install. There's no Python 2.6 version of EasyInstall :( For 2.5, there is: setuptools-0.6c9.win32-py2.5.exe But for 2.6, it's: setuptools-0.6c9-py2.6.egg For any other egg file, I would use EasyInstall, but I don't think I can use EasyInstall to install EasyInstall :) From deets at nospam.web.de Thu May 7 07:01:02 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Thu, 07 May 2009 13:01:02 +0200 Subject: The Fujitsu Lifebook A6120 References: <7498de19-2aa8-4e3f-9988-8bb59fde8ff2@x1g2000prh.googlegroups.com> Message-ID: <76ft8aF1cqqq0U1@mid.uni-berlin.de> > > Does it run Python? > > (sorry, could not resist....) You could at least resist to quote the spammers url, so it doesn't get higher pagerank. Diez From ggardiner at iee.org Thu May 7 07:08:52 2009 From: ggardiner at iee.org (Geoff Gardiner) Date: Thu, 07 May 2009 12:08:52 +0100 Subject: How do I test the integrity of a Python installation in Debian and Ubuntu Message-ID: <4A02C144.1010704@iee.org> How do I assure myself of the integrity of a Python installation acquired using apt-get install on Debian and Ubuntu? I can run regrtest but there's nothing in the basic installation to run, viz.: gegard at gegard:~$ python Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52) [GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> from test import regrtest >>> regrtest.main() test_grammar test_grammar skipped -- No module named test_grammar ... more of the same... 9 tests skipped: test_builtin test_doctest test_doctest2 test_exceptions test_grammar test_opcodes test_operations test_types test_unittest Traceback (most recent call last): File "", line 1, in File "/usr/lib/python2.5/test/regrtest.py", line 416, in main e = _ExpectedSkips() File "/usr/lib/python2.5/test/regrtest.py", line 1321, in __init__ from test import test_socket_ssl ImportError: cannot import name test_socket_ssl >>> I don't see where to go from here, so advice would be extremely helpful. Background I have a large Python-based system that I am trying to install on an externally-hosted VM. It doesn't build and install correctly most of the time, and I have tried successive images of Debian Lenny and Ubuntu Hardy with mostly different build/installation results each time. The installation proceeds Ok on a local Ubuntu VM. A number of modules are installed in addition to python, but I can't even see how to test the core python installation. There are few test_*.py files in the installation. I have previously encountered a fault on the server hosting the VM and would like to be more comfortable that the python installation itself is Ok (or have evidence that it's not). Thank you, Geoff From castironpi at gmail.com Thu May 7 07:27:15 2009 From: castironpi at gmail.com (Aaron Brady) Date: Thu, 7 May 2009 04:27:15 -0700 (PDT) Subject: Why there is a parameter named "self" for classmethod function? References: <2b767f890905061949g12d0bf03m68c6b466ffedfbd9@mail.gmail.com> <4A024F18.9080407@kbsymanzik.org> Message-ID: <2c80e8d0-cc44-48b3-8b90-7b165eb20ed4@g20g2000vba.googlegroups.com> On May 7, 1:29?am, Steven D'Aprano wrote: > On Thu, 07 May 2009 00:39:28 -0400, Terry Reedy wrote: > > Functions that refer to neither the class nor an instance thereof can > > usually be moved outside the class altogether. ?Python is not Java. ?I > > believe staticmethod() was mainly added because it is needed for > > .__new__(), at least in some appearances. > > Can be, but if there's reason enough to keep it with a class, there's no > reason not to. Sometimes I have a class with a few methods that share > common code, but that code happens to not include self (or cls). Since > the common code is only of interest to that class, even though it doesn't > need the instance, I factor the common code out into a method. > > This would be a good candidate for a staticmethod, only due to laziness I > don't usually bother :) > > -- > Steven It's also useful if you want to access the function using the syntax of attributes, as in 'self.amethod' where 'amethod' is a static method. If I understand correctly, no bound method is created if the function is static, and as such, the decorator can increase performance. > Can be, but if there's reason enough to keep it with a class, there's no > reason not to. That's a bit of hyperbole; the usual reasons such as code bloat, namespace bloat, maintainability etc. all weigh against it. It's just that the benefits weigh heavier. There is something that is 'correct' about it IMO, that is, provides syntax to what is valid semantics in an OO context. That may just be another way of stating the thesis though. From duncan.booth at invalid.invalid Thu May 7 07:34:01 2009 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 7 May 2009 11:34:01 GMT Subject: Which python version do I use with "virtualenv"? References: <61fc8aa3-764c-465b-86aa-1b25631609e4@r34g2000vba.googlegroups.com> Message-ID: OldGrantonian wrote: > Thanks to both Steven D'Aprano and Ant :) > >>> Sounds like you've downloaded the Python 2.5 version of Easy Install. > > There's no Python 2.6 version of EasyInstall :( I wonder what I've been running then? > > For 2.5, there is: > > setuptools-0.6c9.win32-py2.5.exe > > But for 2.6, it's: > > setuptools-0.6c9-py2.6.egg > > For any other egg file, I would use EasyInstall, but I don't think I > can use EasyInstall to install EasyInstall :) No, but you can use ez_setup. Go to http://peak.telecommunity.com/dist/ez_setup.py and save the content of that file as ez_setup.py (cut and paste into your favourite editor may be the simplest way) then run it with your chosen Python. It will find and install the appropriate version of easy install. -- Duncan Booth http://kupuguy.blogspot.com From andymac at bullseye.apana.org.au Thu May 7 07:34:22 2009 From: andymac at bullseye.apana.org.au (Andrew MacIntyre) Date: Thu, 07 May 2009 21:34:22 +1000 Subject: Statically linked extension and relative import In-Reply-To: <5b8d13220905070233m4140472xd8fbcdc74985d073@mail.gmail.com> References: <5b8d13220905070233m4140472xd8fbcdc74985d073@mail.gmail.com> Message-ID: <4A02C73E.8040105@bullseye.andymac.org> David Cournapeau wrote: > Hi, > > I am trying to build a 3rd party extension and link it statically to > python. I managed to get things working by customizing Setup.local in > python source tree, but I have a problem for imports of the 'foo.bar' > form. For example, let's say the 3rd party module is laid out as > follows: > > foo/__init__.py > foo/bar.so > > and __init__.py contains: > import foo.bar > > I can build the bar extension and link it statically to python such as > from python, "import bar" works. But how can I make it "recognize" > "from foo import bar" ? I tried to understand how this works at the C > level in import.c, but I am a bit lost in the code. The ultimate goal > is to manage to get code coverage of the C code of the extension > through gcov, which does not support coverage of shared libraries ATM. If you link the bar extension statically, it is not in the foo package namespace (which is why "import bar" works), so should be imported as bar in foo's __init__.py. from foo import bar should then work from other code by virtue of the package namespace (instantiated by __init__.py) then having a bar symbol. -- ------------------------------------------------------------------------- Andrew I MacIntyre "These thoughts are mine alone..." E-mail: andymac at bullseye.apana.org.au (pref) | Snail: PO Box 370 andymac at pcug.org.au (alt) | Belconnen ACT 2616 Web: http://www.andymac.org/ | Australia From cournape at gmail.com Thu May 7 07:45:17 2009 From: cournape at gmail.com (David Cournapeau) Date: Thu, 7 May 2009 20:45:17 +0900 Subject: Statically linked extension and relative import In-Reply-To: <4A02C73E.8040105@bullseye.andymac.org> References: <5b8d13220905070233m4140472xd8fbcdc74985d073@mail.gmail.com> <4A02C73E.8040105@bullseye.andymac.org> Message-ID: <5b8d13220905070445q7734fdffsff108c04e492a74a@mail.gmail.com> On Thu, May 7, 2009 at 8:34 PM, Andrew MacIntyre wrote: > David Cournapeau wrote: >> >> Hi, >> >> I am trying to build a 3rd party extension and link it statically to >> python. I managed to get things working by customizing Setup.local in >> python source tree, but I have a problem for imports of the 'foo.bar' >> form. For example, let's say the 3rd party module is laid out as >> follows: >> >> foo/__init__.py >> foo/bar.so >> >> and __init__.py contains: >> import foo.bar >> >> I can build the bar extension and link it statically to python such as >> from python, "import bar" works. But how can I make it "recognize" >> "from foo import bar" ? I tried to understand how this works at the C >> level in import.c, but I am a bit lost in the code. The ultimate goal >> is to manage to get code coverage of the C code of the extension >> through gcov, which does not support coverage of shared libraries ATM. > > If you link the bar extension statically, it is not in the foo package > namespace (which is why "import bar" works), Yes, I understand this. > so should be imported as bar > in foo's __init__.py. > > from foo import bar But then I have to modify the 3rd party package for statically linked versions. This means in particular that the same source cannot be used for static and dynamic builds ? cheers, David From deank at yahoo.com Thu May 7 08:25:55 2009 From: deank at yahoo.com (dean) Date: Thu, 7 May 2009 14:25:55 +0200 Subject: ply and threads Message-ID: <69x52b9mwk23.1efmsdyffrpz4$.dlg@40tude.net> I have two threads that exchange information by passing messages. I wanted to parse those messages with ply. I'd prefer using two independent parsers so that I can avoid using locks. Can somebody explain how to do this with ply? Ply seems to be doing a lot of odd things, such as reading the the python file that imported it, reading doc strings of it's functions, etc, so I'm not sure how to make this thread safe. This is from yacc.py: # The current implementation is only somewhat object-oriented. The # LR parser itself is defined in terms of an object (which allows multiple # parsers to co-exist). However, most of the variables used during table # construction are defined in terms of global variables. Users shouldn't # notice unless they are trying to define multiple parsers at the same # time using threads (in which case they should have their head examined). It would seem that creating two parsers in the main thread, and then passing one of them to the other thread, is all I need to do to be thread safe? But then again I'm not even sure how to create two parsers. Examples I've seen just call yacc.yacc() at the end, and do not even access yacc instances. Any help is appreciated and thanks in advance. Dean From garryvin at gmail.com Thu May 7 08:49:31 2009 From: garryvin at gmail.com (garryvin) Date: Thu, 7 May 2009 05:49:31 -0700 (PDT) Subject: Open the franchisee of Karvy Message-ID: <0c55a0e5-fb85-41db-acb4-222e0d0f42fc@y6g2000prf.googlegroups.com> Karvy stock broking introduces, Karvy Fortune a business opportunity for individuals in similar business from Karvy, it gives the opportunity to associate with ?Karvy Family? as Franchisee, Remisser, or as an Independent Financial Advisors. Karvy Stock Broking Ltd ranks among 5 stock brokers in India, it ranks among top 5 Depositary Participants in India, with the huge net work of over 500 branches in 375 locations across India and overseas at Dubai, executes 150,000+ trades in NSE/BSE. Karvy Has a top notch research team, advisory team, technology support team to serve it?s customers in 24/7 manner. http://www.karvy.com/v2/fortune/fortunegroups/index.asp From matt at tplus1.com Thu May 7 08:58:50 2009 From: matt at tplus1.com (Matthew Wilson) Date: Thu, 07 May 2009 12:58:50 GMT Subject: How should I use grep from python? Message-ID: I'm writing a command-line application and I want to search through lots of text files for a string. Instead of writing the python code to do this, I want to use grep. This is the command I want to run: $ grep -l foo dir In other words, I want to list all files in the directory dir that contain the string "foo". I'm looking for the "one obvious way to do it" and instead I found no consensus. I could os.popen, commands.getstatusoutput, the subprocess module, backticks, etc. As of May 2009, what is the recommended way to run an external process like grep and capture STDOUT and the error code? TIA Matt From google at mrabarnett.plus.com Thu May 7 09:03:54 2009 From: google at mrabarnett.plus.com (MRAB) Date: Thu, 07 May 2009 14:03:54 +0100 Subject: Code works fine except... In-Reply-To: References: <8d4ec1df-dddb-469a-99a1-695152db7c04@n4g2000vba.googlegroups.com> <1a497471-498a-43d8-90ba-d676e8acf68b@r36g2000vbr.googlegroups.com> <8b49b775-66a7-42d5-9b00-cf23d9b044cc@e23g2000vbe.googlegroups.com> <54d76f11-c255-487d-9c3a-aaa380d7827a@z7g2000vbh.googlegroups.com> <991ccae3-f3e2-4d73-bd32-bc6f560e6f5f@n8g2000vbb.googlegroups.com> <1d8802ff-8211-467f-95c1-4616e3bd7300@s20g2000vbp.googlegroups.com> Message-ID: <4A02DC3A.6050300@mrabarnett.plus.com> John Yeung wrote: > On May 7, 12:30 am, Ross wrote: >> If I were to set up a dictionary that counted players used in the bye >> list and only allowed players to be added to the bye list if they were >> within 2 of the least used player, would this be a good approach for >> managing bye selection or would using a dictionary in this manner be >> unnecessary/redundant? > > I certainly have not proved it, but I think you don't need to resort > to anything fancy if you are OK with the maximum byes being within two > of the minimum byes. (Maybe this needs to be larger with larger > numbers of players.) Try using your original shuffle but instead of > selecting matches to "throw away" each week, just use the matches you > need (to fill up the courts) and pick up where you left off the next > week. For example, with 10 players, each "round" ideally consists of > five matches. If you only have four courts, don't throw away the > fifth match; save it as the first match next week. > > To be honest, I didn't look too carefully at your original shuffle. > It may be good enough. It's a little different than the "standard" > rotation as presented on Wikipedia: > > http://en.wikipedia.org/wiki/Round-robin_tournament#Scheduling_algorithm > > The one on Wikipedia happened to pass my casual tests with no more > than a 2-bye difference between the most-played and least-played > players, and didn't run into the problem of scheduling the same player > for two matches the same week. But I have to stress I only tried a > few starting values, which all worked; I didn't try to break it, or > run an extensive battery of tests. > It might be an idea to run an exhaustive test on small, but not too small, values in order to see whether the minimum difference can, in fact, be one. If the minimum difference turns out to be two then you might as well stick to a simple algorithm, as long as its minimum difference is two. From piet at cs.uu.nl Thu May 7 09:04:57 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Thu, 07 May 2009 15:04:57 +0200 Subject: Checking for required arguments when instantiating class. References: Message-ID: >>>>> Chris Rebert (CR) wrote: >CR> On Wed, May 6, 2009 at 5:24 AM, Piet van Oostrum wrote: >>>>>>>> Lacrima (L) wrote: >L> But what if I have to instantiate any class with 3 or 4 required >L> arguments? How can I do it? >>> >>> cls.__init__.im_func.__code__.co_argcount >>> >>> This will include self, so it will be 1 in First and 2 in Second. >CR> AFAICT, that would count non-required arguments too, which isn't >CR> strictly what the OP requested. If you mean, parameters with default values, yes. Of course you can find out how many of these there are from cls.__init__.func_defaults, but this will get dirtier and dirtier. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From deets at nospam.web.de Thu May 7 09:09:53 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Thu, 07 May 2009 15:09:53 +0200 Subject: How should I use grep from python? References: Message-ID: <76g4ptF1ce8m3U1@mid.uni-berlin.de> Matthew Wilson wrote: > I'm writing a command-line application and I want to search through lots > of text files for a string. Instead of writing the python code to do > this, I want to use grep. > > This is the command I want to run: > > $ grep -l foo dir > > In other words, I want to list all files in the directory dir that > contain the string "foo". > > I'm looking for the "one obvious way to do it" and instead I found no > consensus. I could os.popen, commands.getstatusoutput, the subprocess > module, backticks, etc. > > As of May 2009, what is the recommended way to run an external process > like grep and capture STDOUT and the error code? subprocess. Which becomes pretty clear when reading it's docs: """ The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. This module intends to replace several other, older modules and functions, such as: os.system os.spawn* os.popen* popen2.* commands.* """ Diez From Scott.Daniels at Acm.Org Thu May 7 09:12:08 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Thu, 07 May 2009 06:12:08 -0700 Subject: [Python-Dev] [RELEASED] Python 3.1 beta 1 In-Reply-To: References: <1afaf6160905061832xfc295e3y881c7c8e81083ee6@mail.gmail.com> Message-ID: <_fKdncjfm5xjQZ_XnZ2dnUVZ_rSdnZ2d@pdx.net> Daniel Fetchinson wrote: >> Other features include an ordered dictionary implementation > > Are there plans for backporting this to python 2.x just as > multiprocessing has been? Why not grab the 3.1 code and do it yourself for your 2.X's? It should be far less work than attempting something as fidgety as multiprocessing. --Scott David Daniels Scott.Daniels at Acm.Org From pierregmcode at gmail.com Thu May 7 09:19:02 2009 From: pierregmcode at gmail.com (Pierre GM) Date: Thu, 7 May 2009 06:19:02 -0700 (PDT) Subject: Logging exceptions to a file References: <597627b8-d30b-4b74-9202-9cd46fb1d129@s28g2000vbp.googlegroups.com> Message-ID: <01129a12-312c-4cb9-a0f4-5a456bd9eb6a@e24g2000vbe.googlegroups.com> On May 7, 5:32?am, Lie Ryan wrote: > Pierre GM wrote: > > All, > > I need to log messages to both the console and a given file. I use the > > following code (on Python 2.5) > > >>>> import logging > >>>> # > >>>> logging.basicConfig(level=logging.DEBUG,) > >>>> logfile = logging.FileHandler('log.log') > >>>> logfile.setLevel(level=logging.INFO) > >>>> logging.getLogger('').addHandler(logfile) > >>>> # > >>>> mylogger = logging.getLogger('mylogger') > >>>> # > >>>> mylogger.info("an info message") > > > So far so good, but I'd like to record (possibly unhandled) exceptions > > in the logfile. > > * Do I need to explicitly trap every single exception ? > > * In that case, won't I get 2 log messages on the console (as > > illustrated in the code below: > >>>> try: > >>>> ? ? 1/0 > >>>> except ZeroDivisionError: > >>>> ? ? mylogger.exception(":(") > >>>> ? ? raise > > > Any comments/idea welcomed > > Cheers. > > Although it is usually not recommended to use a catch-all except, this > is the case where it might be useful. JUST DON'T FORGET TO RE-RAISE THE > EXCEPTION. > > if __name__ == '__main__': > ? ? ?try: > ? ? ? ? ?main(): > ? ? ?except Exception, e: > ? ? ? ? ?# log('Unhandled Exception', e) > ? ? ? ? ?raise OK for a simple script, but the (unhandled) exceptions need to be caught at the module level. Any idea? From python.list at tim.thechases.com Thu May 7 09:25:52 2009 From: python.list at tim.thechases.com (Tim Chase) Date: Thu, 07 May 2009 08:25:52 -0500 Subject: How should I use grep from python? In-Reply-To: References: Message-ID: <4A02E160.3090105@tim.thechases.com> > I'm writing a command-line application and I want to search through lots > of text files for a string. Instead of writing the python code to do > this, I want to use grep. > > This is the command I want to run: > > $ grep -l foo dir > > In other words, I want to list all files in the directory dir that > contain the string "foo". > > I'm looking for the "one obvious way to do it" and instead I found no > consensus. I could os.popen, commands.getstatusoutput, the subprocess > module, backticks, etc. While it doesn't use grep or external processes, I'd just do it in pure Python: def files_containing(location, search_term): for fname in os.listdir(location): fullpath = os.path.join(location, fname) if os.isfile(fullpath): for line in file(fullpath): if search_term in line: yield fname break for fname in files_containing('/tmp', 'term'): print fname It's fairly readable, you can easily tweak the search methods (case sensitive, etc), change it to be recursive by using os.walk() instead of listdir(), it's cross-platform, and doesn't require the overhead of an external process (along with the "which call do I use to spawn the function" questions that come with it :) However, to answer your original question, I'd use os.popen which is the one I see suggested most frequently. -tkc From jnoller at gmail.com Thu May 7 09:28:46 2009 From: jnoller at gmail.com (Jesse Noller) Date: Thu, 7 May 2009 09:28:46 -0400 Subject: [Python-Dev] [RELEASED] Python 3.1 beta 1 In-Reply-To: <_fKdncjfm5xjQZ_XnZ2dnUVZ_rSdnZ2d@pdx.net> References: <1afaf6160905061832xfc295e3y881c7c8e81083ee6@mail.gmail.com> <_fKdncjfm5xjQZ_XnZ2dnUVZ_rSdnZ2d@pdx.net> Message-ID: <4222a8490905070628m22cb28eeiad04969822e741c@mail.gmail.com> On Thu, May 7, 2009 at 9:12 AM, Scott David Daniels wrote: > Daniel Fetchinson wrote: >>> >>> Other features include an ordered dictionary implementation >> >> Are there plans for backporting this to python 2.x just as >> multiprocessing has been? > > Why not grab the 3.1 code and do it yourself for your 2.X's? > It should be far less work than attempting something as > fidgety as multiprocessing. > > --Scott David Daniels > Scott.Daniels at Acm.Org Don't worry, we gave multiprocessing some ADHD meds :) From matt at tplus1.com Thu May 7 09:31:44 2009 From: matt at tplus1.com (Matthew Wilson) Date: Thu, 07 May 2009 13:31:44 GMT Subject: How should I use grep from python? References: <76g4ptF1ce8m3U1@mid.uni-berlin.de> Message-ID: <4pBMl.21853$as4.605@nlpi069.nbdc.sbc.com> On Thu 07 May 2009 09:09:53 AM EDT, Diez B. Roggisch wrote: > Matthew Wilson wrote: >> >> As of May 2009, what is the recommended way to run an external process >> like grep and capture STDOUT and the error code? > > subprocess. Which becomes pretty clear when reading it's docs: Yeah, that's what I figured, but I wondered if there was already something newer and shinier aiming to bump subprocess off its throne. I'll just stick with subprocess for now. Thanks for the feedback! From muhammadanushanif at gmail.com Thu May 7 09:42:42 2009 From: muhammadanushanif at gmail.com (muhammadanushanif at gmail.com) Date: Thu, 7 May 2009 06:42:42 -0700 (PDT) Subject: Get Into The World Of YOUSUF JUSANI Message-ID: Get The Most Valuable and Best For Investment Properties IN ASIA, !! Real Estate Builders.. Real Estate Dealer Buy, Sell REAL EASTAE IN GULF ,DUBAI AND AUSTRIA FOR MORE INFO http://www.yousufjusani.blogspot.com From matt at tplus1.com Thu May 7 09:46:13 2009 From: matt at tplus1.com (Matthew Wilson) Date: Thu, 07 May 2009 13:46:13 GMT Subject: How should I use grep from python? References: Message-ID: On Thu 07 May 2009 09:25:52 AM EDT, Tim Chase wrote: > While it doesn't use grep or external processes, I'd just do it > in pure Python: Thanks for the code! I'm reluctant to take that approach for a few reasons: 1. Writing tests for that code seems like a fairly large amount of work. I think I'd need to to either mock out lots of stuff or create a bunch of temporary directories and files for each test run. I don't intend to test that grep works like it says it does. I'll just test that my code calls a mocked-out grep with the right options and arguments, and that my code behaves nicely when my mocked-out grep returns errors. 2. grep is crazy fast. For a search through just a few files, I doubt it would matter, but when searching through a thousand files (which is likely) I suspect that an all-python approach might lag behind. I'm speculating here, though. 3. grep has lots and lots of cute options. I don't want to think about implementing stuff like --color, for example. If I just pass all the heavy lifting to grep, I'm already done. On the other hand, your solution is platform-independent and has no dependencies. Mine depends on an external grep command. Thanks again for the feedback! Matt From marco at sferacarta.com Thu May 7 09:55:18 2009 From: marco at sferacarta.com (Marco Mariani) Date: Thu, 07 May 2009 15:55:18 +0200 Subject: How should I use grep from python? In-Reply-To: References: Message-ID: Matthew Wilson wrote: > consensus. I could os.popen, commands.getstatusoutput, the subprocess > module, backticks, etc. Backticks do_not_do what you think they do. And with py3k they're also as dead as a dead parrot. From dina.ali08 at googlemail.com Thu May 7 10:02:00 2009 From: dina.ali08 at googlemail.com (Dina Ali) Date: Thu, 7 May 2009 15:02:00 +0100 Subject: pyMPI error on mpi.barrier() Message-ID: <4a031c930905070702x169d2141ibeadbae2853a64e4@mail.gmail.com> Hello there I am trying to paralleize GA code using pyMPI. part of the code and and the error message is as below. i write the new positions in a file by root (which is mpi.rank = 0) then other processes are suppose to wait until the written in the file finishes to start evaluating the objective. the problem arises in the barrier method... any ideas on how to do this would be very much appreciated.. Thanks very much Dina for i in xrange(max_iter): if (mpi.rank == 0): update_positions() save_pso_pops_iter() mpi.barrier() <----------------------------error is here evaluate_objective() mpi.barrier() error message: mpi.barrier() mpi.MPIError: MPI_Error_string: invalid communicator: Input/output error (pyMPI_comm_io.c:367) -------------- next part -------------- An HTML attachment was scrubbed... URL: From debatem1 at gmail.com Thu May 7 10:02:20 2009 From: debatem1 at gmail.com (CTO) Date: Thu, 7 May 2009 07:02:20 -0700 (PDT) Subject: [RELEASED] Python 3.1 beta 1 References: <1afaf6160905061832xfc295e3y881c7c8e81083ee6@mail.gmail.com> <_fKdncjfm5xjQZ_XnZ2dnUVZ_rSdnZ2d@pdx.net> Message-ID: <6f0c942f-f393-454c-b921-8eb42c5ab18d@s16g2000vbp.googlegroups.com> On May 7, 9:12?am, Scott David Daniels wrote: > Daniel Fetchinson wrote: > >> Other features include an ordered dictionary implementation > > > Are there plans for backporting this to python 2.x just as > > multiprocessing has been? > > Why not grab the 3.1 code and do it yourself for your 2.X's? > It should be far less work than attempting something as > fidgety as multiprocessing. > > --Scott David Daniels > Scott.Dani... at Acm.Org If OrderedDict winds up being backported, will you include it in 2.x? Geremy Condra From gnewsg at gmail.com Thu May 7 10:03:16 2009 From: gnewsg at gmail.com (Giampaolo Rodola') Date: Thu, 7 May 2009 07:03:16 -0700 (PDT) Subject: ANN: Python process utility (psutil) 0.1.2 released Message-ID: <65f15b53-aacf-4651-a113-960cce25f205@p4g2000vba.googlegroups.com> Hi, I'm pleased to announce the 0.1.2 release of psutil: http://code.google.com/p/psutil === About === psutil is a module providing an interface for retrieving information on running processes and system utilization (CPU, memory) in a portable way by using Python, implementing many functionalities offered by tools like ps, top and Windows task manager. It currently supports Linux, OS X, FreeBSD and Windows. === Enhancements === Aside from fixing some bugs psutil 0.1.2 includes the following major enhancements: * per-process CPU user/kernel times * per-process create time * per-process CPU utilization percentage * per-process memory usage (bytes) * per-process memory utilization (percent) * total, used and free system physical/virtual memory As of now psutil is released to the general public, and should be considered a beta release implementing basic functionality. With this third release we'd like to receive feedback from users and suggestions for additional functionality. Patches and volunteers to test/develop for additional platforms welcome! === Links === * Home page: http://code.google.com/p/psutil * Mailing list: http://groups.google.com/group/psutil/topics * Source tarball: http://psutil.googlecode.com/files/psutil-0.1.2.tar.gz * OS X installer: http://psutil.googlecode.com/files/psutil-0.1.2-py2.6-macosx10.4.dmg * Windows Installer (Python 2.6): http://psutil.googlecode.com/files/psutil-0.1.2.win32-py2.6.exe * Api Reference: http://code.google.com/p/psutil/wiki/Documentation Thanks --- Giampaolo Rodola' http://code.google.com/p/pyftpdlib http://code.google.com/p/psutil/ From suraj at barkale.com Thu May 7 10:09:36 2009 From: suraj at barkale.com (Suraj Barkale) Date: Thu, 7 May 2009 14:09:36 +0000 (UTC) Subject: Parsing text References: <37255869-6cc8-44c4-8b13-6376604df842@h23g2000vbc.googlegroups.com> Message-ID: iainemsley googlemail.com> writes: > > Hi, > I'm trying to write a fairly basic text parser to split up scenes and > acts in plays to put them into XML. I've managed to get the text split > into the blocks of scenes and acts and returned correctly but I'm > trying to refine this and get the relevant scene number when the split > is made but I keep getting an NoneType error trying to read the block > inside the for loop and nothing is being returned. I'd be grateful for > some suggestions as to how to get this working. > > for scene in text.split('Scene'): > num = re.compile("^\s\[0-9, i{1,4}, v]", re.I) > textNum = num.match(scene) > if textNum: > print textNum > else: > print "No scene number" > m = '
/dev/null 2>&1 & -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "It is easier to optimize correct code than to correct optimized code." --Bill Harlan From nick at craig-wood.com Thu May 7 10:30:11 2009 From: nick at craig-wood.com (Nick Craig-Wood) Date: Thu, 07 May 2009 09:30:11 -0500 Subject: How should I use grep from python? References: Message-ID: Matthew Wilson wrote: > I'm writing a command-line application and I want to search through lots > of text files for a string. Instead of writing the python code to do > this, I want to use grep. > > This is the command I want to run: > > $ grep -l foo dir > > In other words, I want to list all files in the directory dir that > contain the string "foo". > > I'm looking for the "one obvious way to do it" and instead I found no > consensus. I could os.popen, commands.getstatusoutput, the subprocess > module, backticks, etc. backticks is some other language ;-) > As of May 2009, what is the recommended way to run an external process > like grep and capture STDOUT and the error code? This is the one true way now-a-days >>> from subprocess import Popen, PIPE >>> p = Popen(["ls", "-l"], stdout=PIPE) >>> for line in p.stdout: ... print line ... total 93332 -rw-r--r-- 1 ncw ncw 181 2007-10-18 14:01 - drwxr-xr-x 2 ncw ncw 4096 2007-08-29 22:56 10_files -rw-r--r-- 1 ncw ncw 124713 2007-08-29 22:56 10.html [snip] >>> p.wait() # returns the error code 0 >>> There was talk of removing the other methods from public use for 3.x. Not sure of the conclusion. -- Nick Craig-Wood -- http://www.craig-wood.com/nick From hackingkk at gmail.com Thu May 7 10:41:16 2009 From: hackingkk at gmail.com (Krishnakant) Date: Thu, 07 May 2009 20:11:16 +0530 Subject: Which python version do I use with "virtualenv"? In-Reply-To: References: Message-ID: <1241707276.5822.3.camel@kk-laptop> I have another question in this same context. I have python 2.6 and want to set up a vertualenv in /opt/turbogears/python2.5. Then use this for all the things a turbogears based application would need for project execution. so I have decided that I will download python 2.5 and compile it with ./configure --prefix=/opt/turbogears/python2.5 and then install turbogears. Is this possible? will this kind of python compilation help me to create a vertualenv in the place where I have python2.5? or is there any other way to do this? I really need vertualenv and would want to use python2.5 because I feel easy install won't work with python2.6 happy hacking. Krishnakant. From deets at nospam.web.de Thu May 7 10:52:49 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Thu, 07 May 2009 16:52:49 +0200 Subject: Which python version do I use with "virtualenv"? References: Message-ID: <76gaqtF1d8bv6U1@mid.uni-berlin.de> Krishnakant wrote: > I have another question in this same context. > I have python 2.6 and want to set up a vertualenv > in /opt/turbogears/python2.5. > Then use this for all the things a turbogears based application would > need for project execution. > > so I have decided that I will download python 2.5 and compile it > with ./configure --prefix=/opt/turbogears/python2.5 and then install > turbogears. Is this possible? will this kind of python compilation > help me to create a vertualenv in the place where I have python2.5? You are confusing virtualenv with a custom-build python. You can of course use VE with a custom-build python, but then there isn't as much use for it, as you then have a distinct python-instance already - unless you are going to share it amongst projects, which then leads to the question why you don't use VE *without* the custom-build python. > or is there any other way to do this? I really need vertualenv and > would want to use python2.5 because I feel easy install won't work with > python2.6 Feelings are a fine thing in bed, on weddings or other social contexts. Usually, they don't belong in the more mundane world of computers, and in this case it's an outright wrong feeling. Easy_install works just fine with py2.6 It might be though that there are some 3rd-party-libs that don't. Diez From hackingkk at gmail.com Thu May 7 11:03:18 2009 From: hackingkk at gmail.com (Krishnakant) Date: Thu, 07 May 2009 20:33:18 +0530 Subject: Which python version do I use with "virtualenv"? In-Reply-To: <76gaqtF1d8bv6U1@mid.uni-berlin.de> References: <76gaqtF1d8bv6U1@mid.uni-berlin.de> Message-ID: <1241708598.5822.8.camel@kk-laptop> > You are confusing virtualenv with a custom-build python. You can of course > use VE with a custom-build python, but then there isn't as much use for it, > as you then have a distinct python-instance already - unless you are going > to share it amongst projects, which then leads to the question why you > don't use VE *without* the custom-build python. Well, Turbogears works perfectly with python2.5, although there are people who use it with 2.6 as well. I have to keep 2.6 because my screen reader orca giave me some problem with 2.5. So I have no option but to keep a seperate python2.5 in some different directory and have ve use it. would just like to know how I can get ve to use that installation and not the one on my system by default. happy hacking. Krishnakant. From deets at nospam.web.de Thu May 7 11:18:52 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Thu, 07 May 2009 17:18:52 +0200 Subject: Which python version do I use with "virtualenv"? References: <76gaqtF1d8bv6U1@mid.uni-berlin.de> Message-ID: <76gcboF1c6nm5U1@mid.uni-berlin.de> Krishnakant wrote: > >> You are confusing virtualenv with a custom-build python. You can of >> course use VE with a custom-build python, but then there isn't as much >> use for it, as you then have a distinct python-instance already - unless >> you are going to share it amongst projects, which then leads to the >> question why you don't use VE *without* the custom-build python. > Well, Turbogears works perfectly with python2.5, although there are > people who use it with 2.6 as well. > I have to keep 2.6 because my screen reader orca giave me some problem > with 2.5. > > So I have no option but to keep a seperate python2.5 in some different > directory and have ve use it. > > would just like to know how I can get ve to use that installation and > not the one on my system by default. By *using* it? /opt/my_python/bin/python2.5 and you're done. If you e.g. download ez_setup.py & execute that through your custom python, you'll end up with an easy_install in /opt/my_python/bin, which you then can use to e.g. install VE. Diez From jamieson74 at gmail.com Thu May 7 11:20:25 2009 From: jamieson74 at gmail.com (jamieson) Date: Thu, 7 May 2009 08:20:25 -0700 (PDT) Subject: tkinter and widget placement after resizing Message-ID: Hello, I've got a fairly simple GUI that places pmw.EntryFields into a window starting in the upper left corner. When the first column is filled with these widgets I'd like to start a new column and continue placement, and so on. It is working now with the grid manager if I explicitly set the max number of widgets per column, but if I resize the main window there is wasted space as the number of widgets per column is fixed. I'd like to find a design that could change the number of widgets per column when the window is resized. Any suggestions? i.e. start out with a window like this: [1][4][7] [2][5][8] [3][6][9] make the main window larger and end up with this: [1][6] [2][7] [3][8] [4][9] [5] From coldtortuga at gmail.com Thu May 7 11:21:51 2009 From: coldtortuga at gmail.com (Francis Carr) Date: Thu, 7 May 2009 08:21:51 -0700 (PDT) Subject: Self function References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> Message-ID: Scheme is arguably the programming language with the most support for recursion, including the most complete support for tail-call optimization, constructs like "letrec" to define collections of multiply-recursive functions (which get used very frequently -- by no means is it an uncommon situation, as you suggest in your initial post), and hardly any iterative loops. Yet -- scheme does not provide out-of-the-box support for your proposed "let-a-function-implicitly- refer-to-itself" idea. This suggests that the idea itself runs counter to more important aspects of a programming language. In the special case of a self-recursive function, you would like re- naming "to just work". Out of morbid curiosity, I am compelled to ask... why do you expect that re-naming anything --- a function, a class, a module, a variable, anything --- in just one place but not all the others should ever, under any circumstances, "just work" ? From elwinter at verizon.net Thu May 7 11:22:06 2009 From: elwinter at verizon.net (Eric Winter) Date: Thu, 7 May 2009 08:22:06 -0700 (PDT) Subject: Unable to build libpython2.5.so on OS X 10.4 Message-ID: Hi all. I'm trying to build some internal code that needs to link against libpython2.5.so on a OS X 10.4 (Tiger) machine. It seems that no matter what combination of options and environment variables I give to the configure script from python 2.5.1, all I get is the libpython2.5.a (the static library). I've googled the problem and searched the comp.lang.python archives, but I have been unable to find anything that works. Is there some special magic I have to invoke to do this? I've not done any Mac development, so there may be some obvious point I am missing. The Python README file and configure script comments are not getting me very far. TIA, Eric Winter NASA GSFC Fermi Gamma-Ray Space Telescope Science Support Center From navanjo at gmail.com Thu May 7 11:34:46 2009 From: navanjo at gmail.com (Navanjo) Date: Thu, 7 May 2009 08:34:46 -0700 (PDT) Subject: P2P text chat engine Message-ID: If you have the source code of a p2p text chat engine please send to me From tomf.sessile at gmail.com Thu May 7 11:39:54 2009 From: tomf.sessile at gmail.com (TomF) Date: Thu, 7 May 2009 08:39:54 -0700 Subject: Simple way of handling errors References: <2009050616401916807-tomfsessile@gmailcom> Message-ID: <2009050708395450073-tomfsessile@gmailcom> On 2009-05-07 01:01:57 -0700, Peter Otten <__peter__ at web.de> said: > TomF wrote: > >> As a relative newcomer to Python, I like it a lot but I'm dismayed at >> the difficulty of handling simple errors. In Perl if you want to >> anticipate a file-not-found error you can simply do: >> >> open($file) or die("open($file): $!"); >> >> and you get an intelligible error message. In Python, to get the same >> thing it appears you need at least: >> >> try: >> f=open(file) >> except IOError, err: >> print "open(%s): got %s" % (file, err.strerror) >> exit(-1) >> >> Is there a simpler interface or idiom for handling such errors? I >> appreciate that Python's exception handling is much more sophisticated >> but often I don't need it. >> >> -Tom > > While you are making the transition you could write > > from perl_idioms import open_or_die > > f = open_or_die("does-not-exist") > > > with the perl_idioms module looking like > > import sys > > def open_or_die(*args): > try: > return open(*args) > except IOError, e: > sys.exit(e) > > Peter Thanks. Rolling my own error module for common errors may be the best way to go. -Tom From nickle at gmail.com Thu May 7 11:45:06 2009 From: nickle at gmail.com (Nick) Date: Thu, 7 May 2009 08:45:06 -0700 (PDT) Subject: SQL and CSV References: <1d2bb50f-2e3f-429b-aa98-7e1fe1f011b0@t10g2000vbg.googlegroups.com> Message-ID: On May 5, 8:27?pm, Tim Golden wrote: > Nick wrote: > > Part of the problem is that the 'selection' needs to be in a config > > file. I can put the if row['status'] != 'Cancelled': return True into > > a config, read it and eval it, but its not quite as clean as ansql > > route. > > Still not clear what the restriction is. If you were writingSQLyou'd have to read *something* from your config file, > unless you're suggesting that the "config file" is in fact > aSQLfile. Which is one way of doing it, but then you might > just as well have your config file as a Python file and > import it. > > Have I missed the point somewhere here? Can you give an > example -- even a fictional one -- of what you couldn't > do using, say, the example I gave earlier? > > TJG Solution found. In the end I used SQLite to read from a csv file, and now I can query the CSV file. The file is read using the csv module First create a function def fraction(p, denom): num, frac = p.split ('-') return float (num) + float (frac) / denom for use within queries. Now build the class. self.filename = filename self.dialect = dialect self.query = query reader = csv.reader (open (filename, 'r')) self.connection = sqlite.connect(":memory:") self.connection.create_function("fraction", 2, fraction) # Adds in function self.cursor = self.connection.cursor() first = True for row in reader: if first: headers = [] for r in row: n = r.strip().replace (' ', '_').replace ('-','_') headers.append (n) command = 'create table csv (%s)' % ','.join (headers) self.cursor.execute (command) first = False else: command = 'insert into csv values ("%s")' % '","'.join (row) self.cursor.execute (command) and then I can use this self.cursor.execute (self.query) rows = self.cursor.fetchall() headers = [] for r in self.cursor.description: headers.append (r[0]) results = Results.Results (headers, self.name, {}) i = 0 for row in rows: results.add (row, i) i = i + 1 return results to query the results. Results.Results is one of my classes that's reused in lots of places. The query then looks somethign like this select Client_Reference_Number as TrdNbr, Asset_Number as ISIN, Quantity as Qty, status from csv where status in ("CANCELLED") union select Client_Reference_Number as TrdNbr, Asset_Number as ISIN, Quantity as Qty, status from csv where status not in ("CANCELLED") All incredibly neat and the first time I've used SQLite. nick From lists at cheimes.de Thu May 7 11:45:51 2009 From: lists at cheimes.de (Christian Heimes) Date: Thu, 07 May 2009 17:45:51 +0200 Subject: Unable to build libpython2.5.so on OS X 10.4 In-Reply-To: References: Message-ID: <4A03022F.408@cheimes.de> Eric Winter schrieb: > Hi all. I'm trying to build some internal code that needs to link > against libpython2.5.so on a OS X 10.4 (Tiger) machine. It seems that > no matter what combination of options and environment variables I give > to the configure script from python 2.5.1, all I get is the > libpython2.5.a (the static library). I've googled the problem and > searched the comp.lang.python archives, but I have been unable to find > anything that works. > > Is there some special magic I have to invoke to do this? I've not done > any Mac development, so there may be some obvious point I am missing. > The Python README file and configure script comments are not getting > me very far. You have to start with a clean plate and use the --enable-shared option to configure: make distclean ./configure --enable-unicode=ucs4 --enable-shared make That will give you a libpython2.5.dylib. The suffix for shared libraries is .dylib on On Mac OS X, not .so! Christian From Scott.Daniels at Acm.Org Thu May 7 11:53:30 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Thu, 07 May 2009 08:53:30 -0700 Subject: [RELEASED] Python 3.1 beta 1 In-Reply-To: <6f0c942f-f393-454c-b921-8eb42c5ab18d@s16g2000vbp.googlegroups.com> References: <1afaf6160905061832xfc295e3y881c7c8e81083ee6@mail.gmail.com> <_fKdncjfm5xjQZ_XnZ2dnUVZ_rSdnZ2d@pdx.net> <6f0c942f-f393-454c-b921-8eb42c5ab18d@s16g2000vbp.googlegroups.com> Message-ID: CTO wrote: > ... > If OrderedDict winds up being backported, will you include it > in 2.x? I see it in the 2.7 sources. --Scott David Daniels Scott.Daniels at Acm.Org From elwinter at verizon.net Thu May 7 11:56:50 2009 From: elwinter at verizon.net (elwinter) Date: Thu, 7 May 2009 08:56:50 -0700 (PDT) Subject: Unable to build libpython2.5.so on OS X 10.4 References: Message-ID: <73cd15ab-1aed-494f-b8a6-7d9c3a275ef7@o27g2000vbd.googlegroups.com> Christian, Thanks for the response. I knew about the .dylib suffix, but that's not being built either, even when I supply the --enable-shared option to configure. I also tried the --enable-unicode configure option, but no joy. Might there be some additional OS X package I need to install to get this to work? Thanks, Eric On May 7, 11:45?am, Christian Heimes wrote: > Eric Winter schrieb: > > > Hi all. I'm trying to build some internal code that needs to link > > against libpython2.5.so on a OS X 10.4 (Tiger) machine. It seems that > > no matter what combination of options and environment variables I give > > to the configure script from python 2.5.1, all I get is the > > libpython2.5.a (the static library). I've googled the problem and > > searched the comp.lang.python archives, but I have been unable to find > > anything that works. > > > Is there some special magic I have to invoke to do this? I've not done > > any Mac development, so there may be some obvious point I am missing. > > The Python README file and configure script comments are not getting > > me very far. > > You have to start with a clean plate and use the --enable-shared option > to configure: > > make distclean > ./configure --enable-unicode=ucs4 --enable-shared > make > > That will give you a libpython2.5.dylib. The suffix for shared libraries > is .dylib on On Mac OS X, not .so! > > Christian From bearophileHUGS at lycos.com Thu May 7 11:57:22 2009 From: bearophileHUGS at lycos.com (bearophileHUGS at lycos.com) Date: Thu, 7 May 2009 08:57:22 -0700 (PDT) Subject: Python 3.1 beta 1 References: Message-ID: <7c96bd34-b199-4646-a790-4097adcc6999@q14g2000vbn.googlegroups.com> I appreciate the tables "Infinite Iterators" and "Iterators terminating on the shortest input sequence" at the top of the itertools module, they are quite handy. I'd like to see similar summary tables at the top of other docs pages too (such pages are often quite long), for example the collections docs page. collections.Counter and collections.OrderedDict: very nice and useful. Is the order inside OrderedDict kept with a double linked list of the items? Counter.most_common([n]): a good idea. If I need a dictionary that is both ordered and has a default too I can't combine their qualities (but I can use the __missing__(key) hook). >Equality tests between OrderedDict objects are order-sensitive and are implemented as list(od1.items())==list(od2.items()). Equality tests between OrderedDict objects and other Mapping objects are order-insensitive< very nice idea. >The OrderedDict constructor and update() method both accept keyword arguments, but their order is lost because Python's function call semantics pass-in keyword arguments using a regular unordered dictionary.< This is unfortunate :-( Well, I'd like function call semantics pass-in keyword arguments to use OrderedDicts then... :-) >Several mathematical operations are provided for combining Counter objects to produce multisets< Very nice. itertools.count(): when increments are non-integer I hope the results are computed with a multiplication instead an interated sum, to increase precision. Issue 4688 and the work by Antoine Pitrou: interesting GC optimization. The performance difference of the BinaryTrees benchmark is big. Bye, bearophile From bearophileHUGS at lycos.com Thu May 7 12:07:58 2009 From: bearophileHUGS at lycos.com (bearophileHUGS at lycos.com) Date: Thu, 7 May 2009 09:07:58 -0700 (PDT) Subject: Self function References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> Message-ID: <26f0fa28-f3d9-4549-a748-51dc00827b1d@r3g2000vbp.googlegroups.com> Francis Carr: I don't know who are you talking to, but I can give you few answers anyway. >collections of multiply-recursive functions (which get used very frequently -- by no means is it an uncommon situation, as you suggest in your initial post),< They may be frequent in Scheme (because it's often used as an almost pure language), but in Python code they are quite rare. I have used two mutual recursive functions only once in non-toy Python code in two or more years. >Yet -- scheme does not provide out-of-the-box support for your proposed "let-a-function-implicitly- refer-to-itself" idea. This suggests that the idea itself runs counter to more important aspects of a programming language.< I see. It's not a very implicit thing, because you have to call a function anyway, it's just it has a fixed name, like __func__. I think it doesn't runs counter to Python & D languages (I have asked for a similar feature in D2 too, and the designers seem to have accepted the idea, already suggested there by another person in the past). Bye, bearophile From Florian.Wollenschein at FernUni-Hagen.de Thu May 7 12:22:22 2009 From: Florian.Wollenschein at FernUni-Hagen.de (Florian Wollenschein) Date: Thu, 07 May 2009 18:22:22 +0200 Subject: hex(dummy)[2:] - issue... In-Reply-To: References: Message-ID: Tim Chase wrote: >> I need some advice :-) >> I'm using hex(dummy)[2:] to represent a color in hexadecimal format >> for the bgcolor in an html file. dummy is the color value in RGB of >> course... >> >> Now, if there's an R, G or B value of zero, this command only prints >> one single 0 instead of two. What's wrong with the code? > > You can try > > PLACES = 2 # 6? > hex(dummy)[2:].zfill(PLACES) > > Alternatively, you can output decimal numbers in HTML/CSS with > > rgb(r, g, b) > > such as > > style="rgb(255,0,0)" > > However, I recommend doing this via CSS unless you have a strong reason > to sully your HTML with style information. > > -tkc > > > > > > > hey tkc, I used your first alternative. This did it! Thanks a lot. I think I will write the style stuff into a .css file in the next few days but until then I'm just working on get my program to work... Listick http://www.listick.org From mnordhoff at mattnordhoff.com Thu May 7 12:24:54 2009 From: mnordhoff at mattnordhoff.com (Matt Nordhoff) Date: Thu, 07 May 2009 16:24:54 +0000 Subject: Simple way of handling errors In-Reply-To: References: <2009050616401916807-tomfsessile@gmailcom> <2009050620213875249-tomfsessile@gmailcom> Message-ID: <4A030B56.5000503@mattnordhoff.com> Steven D'Aprano wrote: > On Wed, 06 May 2009 20:21:38 -0700, TomF wrote: > >>> The only reason you would bother going to the time and effort of >>> catching the error, printing your own error message, and then exiting, >>> is if you explicitly want to hide the traceback from the user. >> Well, to me, exposing the user to such raw backtraces is unprofessional, >> which is why I try to catch user-caused errors. But I suppose I have an >> answer to my question. > > That depends on your audience. Not every program is written to be used > for a technical incompetent audience. Some users actually *want* to see > the errors. > > But certainly there are large classes of applications where you do want > to suppress the traceback. That's why I said "if you explicitly want to > hide the traceback from the user" rather than "don't do this". > > The idiom I use is to wrap the *entire* application in a single > try...except block, and then put all your user-friendly error handling in > one place, instead of scattered over the entire application: > > > try: > main(sys.argv[1:]) > except KeyboardInterrupt, SystemExit: That should be: except (KeyboardInterrupt, SystemExit): ;-D > raise > except Exception, e: > log(e) > print >>sys.stderr, str(e) > sys.exit(1) > > > > Hope this helps. -- From Florian.Wollenschein at FernUni-Hagen.de Thu May 7 12:32:03 2009 From: Florian.Wollenschein at FernUni-Hagen.de (Florian Wollenschein) Date: Thu, 07 May 2009 18:32:03 +0200 Subject: What would YOU like to see in a txt to html converter? Message-ID: As you might have mentioned I'm just working on a txt to html converter called "thc". This project is intended for me to learn Python and now pyQT4 to which I changed a few days ago (started with Tkinter). I have implemented the following features so far: - Giving a title for the html - Choose whether it's Transitional or Strict Doctype - Select a background color - Show the converted file with the standard browser - Working on a font size slider I don't really know if this is of any use for anybody but it's just a fun project by a beginner :-) Now I'd like to know what kind of features you'd like to see in version 0.3 of thc!? Please post them... Have fun! Listick http://www.lictick.org From nad at acm.org Thu May 7 12:41:42 2009 From: nad at acm.org (Ned Deily) Date: Thu, 07 May 2009 12:41:42 -0400 Subject: Unable to build libpython2.5.so on OS X 10.4 References: Message-ID: In article , Eric Winter wrote: > Hi all. I'm trying to build some internal code that needs to link > against libpython2.5.so on a OS X 10.4 (Tiger) machine. It seems that > no matter what combination of options and environment variables I give > to the configure script from python 2.5.1, all I get is the > libpython2.5.a (the static library). I've googled the problem and > searched the comp.lang.python archives, but I have been unable to find > anything that works. Perhaps I misunderstand, but if you are trying to build a C extension for an existing Python 2.5 installation, using Distutils from that installation should take care of everything for you. Is there a setup.py file by any chance? Are you using a standard python installation (i.e. python.org installer for instance)? More details might help. -- Ned Deily, nad at acm.org From apt.shansen at gmail.com Thu May 7 12:46:11 2009 From: apt.shansen at gmail.com (Stephen Hansen) Date: Thu, 7 May 2009 09:46:11 -0700 Subject: Logging exceptions to a file In-Reply-To: <01129a12-312c-4cb9-a0f4-5a456bd9eb6a@e24g2000vbe.googlegroups.com> References: <597627b8-d30b-4b74-9202-9cd46fb1d129@s28g2000vbp.googlegroups.com> <01129a12-312c-4cb9-a0f4-5a456bd9eb6a@e24g2000vbe.googlegroups.com> Message-ID: <7a9c25c20905070946w78822390n1ff2915663f6260@mail.gmail.com> > > > > > So far so good, but I'd like to record (possibly unhandled) exceptions > > > in the logfile. > > > * Do I need to explicitly trap every single exception ? > > > * In that case, won't I get 2 log messages on the console (as > > > illustrated in the code below: > Check out sys.excepthook, something vaguely like: import sys def handle_exceptions(exctype, value, traceback): logging.exception("An unhandled exception was detected.") sys.excepthook = handle_exceptions -------------- next part -------------- An HTML attachment was scrubbed... URL: From arnodel at googlemail.com Thu May 7 12:55:11 2009 From: arnodel at googlemail.com (Arnaud Delobelle) Date: Thu, 07 May 2009 17:55:11 +0100 Subject: Self function References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> <1292fea0-96fb-4a21-b39d-b6ce610ea6b2@t10g2000vbg.googlegroups.com> <4f13a110-b3e5-4de5-b6d6-0d64da20b61e@r34g2000vbi.googlegroups.com> <1241649200.4a0210301f77e@mail.uh.cu> Message-ID: Luis Alberto Zarrabeitia Gomez writes: > A bit offtopic: a while ago I think I saw a recipe for a decorator > that, via bytecode hacks, would bind otherwise global names to the > local namespace of the function. Can anyone remember it/point me to > it? An @bind decorator that would 'localize' all the global names, > including the still unexistent but already know function name, would > guarantee that at least recursion calls the same function instead of > "whatever happens to be bound to their name at runtime". If it wasn't > a hack, anyway. I remember a discussion on python-ideas, and I wrote this decorator at the time: def new_closure(vals): args = ','.join('x%i' % i for i in range(len(vals))) f = eval("lambda %s:lambda:(%s)" % (args, args)) return f(*vals).func_closure def localize(f): f_globals = dict((n, f.func_globals[n]) for n in f.func_code.co_names) f_closure = ( f.func_closure and new_closure([c.cell_contents for c in f.func_closure]) ) return type(f)(f.func_code, f_globals, f.func_name, f.func_defaults, f_closure) Unfortunately it fails for recursive functions: >>> @localize ... def fac(n): ... return 1 if n <= 1 else n*fac(n - 1) ... Traceback (most recent call last): File "", line 1, in File "localize.py", line 7, in localize f_globals = dict((n, f.func_globals[n]) for n in f.func_code.co_names) File "localize.py", line 7, in f_globals = dict((n, f.func_globals[n]) for n in f.func_code.co_names) KeyError: 'fac' It can be fixed, but not very easily precisely because the function is recursive. I've hacked a quick way to do it below. def reclocalize(f): def wrapper(*args, **kwargs): return recf(*args, **kwargs) def getglobal(name): return wrapper if name == f.__name__ else f.func_globals[name] f_globals = dict((n, getglobal(n)) for n in f.func_code.co_names) f_closure = ( f.func_closure and new_closure([c.cell_contents for c in f.func_closure]) ) recf = type(f)(f.func_code, f_globals, f.func_name, f.func_defaults, f_closure) return wrapper Now it works as intended: >>> @reclocalize ... def fac(n): ... return 1 if n <= 1 else n*fac(n - 1) ... >>> fac(10) 3628800 >>> foo = fac >>> del fac >>> foo(10) 3628800 -- Arnaud From segfaulthunter at gmail.com Thu May 7 12:56:16 2009 From: segfaulthunter at gmail.com (segfaulthunter) Date: Thu, 7 May 2009 09:56:16 -0700 (PDT) Subject: Logging exceptions to a file References: <597627b8-d30b-4b74-9202-9cd46fb1d129@s28g2000vbp.googlegroups.com> <01129a12-312c-4cb9-a0f4-5a456bd9eb6a@e24g2000vbe.googlegroups.com> Message-ID: <5a779db4-9dd3-4b85-bda2-12bea38a1026@o27g2000vbd.googlegroups.com> On May 7, 1:19?pm, Pierre GM wrote: > On May 7, 5:32?am, Lie Ryan wrote: > > > > > Pierre GM wrote: > > > All, > > > I need to log messages to both the console and a given file. I use the > > > following code (on Python 2.5) > > > >>>> import logging > > >>>> # > > >>>> logging.basicConfig(level=logging.DEBUG,) > > >>>> logfile = logging.FileHandler('log.log') > > >>>> logfile.setLevel(level=logging.INFO) > > >>>> logging.getLogger('').addHandler(logfile) > > >>>> # > > >>>> mylogger = logging.getLogger('mylogger') > > >>>> # > > >>>> mylogger.info("an info message") > > > > So far so good, but I'd like to record (possibly unhandled) exceptions > > > in the logfile. > > > * Do I need to explicitly trap every single exception ? > > > * In that case, won't I get 2 log messages on the console (as > > > illustrated in the code below: > > >>>> try: > > >>>> ? ? 1/0 > > >>>> except ZeroDivisionError: > > >>>> ? ? mylogger.exception(":(") > > >>>> ? ? raise > > > > Any comments/idea welcomed > > > Cheers. > > > Although it is usually not recommended to use a catch-all except, this > > is the case where it might be useful. JUST DON'T FORGET TO RE-RAISE THE > > EXCEPTION. > > > if __name__ == '__main__': > > ? ? ?try: > > ? ? ? ? ?main(): > > ? ? ?except Exception, e: > > ? ? ? ? ?# log('Unhandled Exception', e) > > ? ? ? ? ?raise > > OK for a simple script, but the (unhandled) exceptions need to be > caught at the module level. Any idea? Override sys.excepthook. http://docs.python.org/library/sys.html#sys.excepthook From elwinter at verizon.net Thu May 7 12:57:09 2009 From: elwinter at verizon.net (elwinter) Date: Thu, 7 May 2009 09:57:09 -0700 (PDT) Subject: Unable to build libpython2.5.so on OS X 10.4 References: Message-ID: <0e05eca2-b460-4e01-aa54-cc1055f51e7d@q14g2000vbn.googlegroups.com> Hi Ned. The Python module I am building is actually the Python module for ROOT, a large package from CERN. However, the problem arises before that code enters the picture, when I am building Python itself. All I want to do is create "libpython2.5.dylib", or its equivalent, and I can't seem to make that happen on Tiger. Thanks, Eric On May 7, 12:41?pm, Ned Deily wrote: > In article > , > ?Eric Winter wrote: > > > Hi all. I'm trying to build some internal code that needs to link > > against libpython2.5.so on a OS X 10.4 (Tiger) machine. It seems that > > no matter what combination of options and environment variables I give > > to the configure script from python 2.5.1, all I get is the > > libpython2.5.a (the static library). I've googled the problem and > > searched the comp.lang.python archives, but I have been unable to find > > anything that works. > > Perhaps I misunderstand, but if you are trying to build a C extension > for an existing Python 2.5 installation, using Distutils from that > installation should take care of everything for you. ?Is there a > setup.py file by any chance? ?Are you using a standard python > installation (i.e. python.org installer for instance)? ?More details > might help. > > -- > ?Ned Deily, > ?n... at acm.org From lhanser at gmail.com Thu May 7 13:24:08 2009 From: lhanser at gmail.com (Lawrence Hanser) Date: Thu, 7 May 2009 10:24:08 -0700 Subject: Wing vs Netbeans IDE? Message-ID: Dear Colleagues, I have been using NetBeans for a month or so now and am reasonably happy with it. I'm considering other options, and ran across Wing. I'm interested in opinions about NetBeans and Wing as IDE for Python. Thanks, Larry From james at agentultra.com Thu May 7 13:28:10 2009 From: james at agentultra.com (J Kenneth King) Date: Thu, 07 May 2009 13:28:10 -0400 Subject: list comprehension question References: <6d502cfd-e252-4722-96a5-c52fc3579499@v1g2000prd.googlegroups.com> <85ocucnbce.fsf@agentultra.com> <85ljpb8r1o.fsf@agentultra.com> <85d4am8hq4.fsf@agentultra.com> Message-ID: <8563gcx1p1.fsf@agentultra.com> Steven D'Aprano writes: > On Wed, 06 May 2009 09:48:51 -0400, J Kenneth King wrote: > >> Emile van Sebille writes: >> >>> On 5/5/2009 9:15 AM J Kenneth King said... >>> >>>> List comprehensions can make a reader of your code apprehensive >>>> because it can read like a run-on sentence and thus be difficult to >>>> parse. The Python documentation discourages their use and I believe >>>> for good reason. >>> >>> Can you provide a link for this? I'd like to see specifically what's >>> being discouraged, as I'd be surprised to find routine usage frowned >>> upon. >>> >>> Emile >> >> http://docs.python.org/tutorial/datastructures.html#nested-list- > comprehensions >> >> >> "If you?ve got the stomach for it, list comprehensions can be nested. >> They are a powerful tool but ? like all powerful tools ? they need to be >> used carefully, if at all." > > How does this discourage the use of list comprehensions? At most, it > warns that complicated list comps are tricky. Complicated *anything* are > tricky. They are tricky and need to be used carefully, *if at all*. IMO this means that if there's a way to do it without a nested list comprehension, then that solution should be preferred. >> and >> >> "In real world, you should prefer builtin functions to complex flow >> statements." > > That's ridiculous. The example given is a special case. That's like > saying "Loops are hard, so in the real world, if you want a loop, find a > builtin function that does what you want instead." > > What's the "builtin function" we're supposed to prefer over a "complex > flow statement" like this? It's not ridiculous and says nothing of the sort. You're jumping to conclusions. If you have a problem with it, I suggest you complain to the Python documentation people. I don't make this stuff up. Just look at many of the example solutions provided in this thread to the OP's problem. The examples in the link I provided are also good as are ones provided in the itertools documentation. Keep in mind that nested comprehensions are still available because they do have a use case that justifies their existence. However, I think the Python documentation warns against their use because people might rely on them for problems where they aren't necessary and since they are difficult to read... it can lead to difficult to read code. From version5 at gmail.com Thu May 7 13:30:18 2009 From: version5 at gmail.com (nnp) Date: Thu, 7 May 2009 18:30:18 +0100 Subject: Wing vs Netbeans IDE? In-Reply-To: References: Message-ID: <28749c0e0905071030p15540b99nc73b37edb72bbe9a@mail.gmail.com> I've tried Wing but not NetBeans. I would personally recommend Eclipse with the PyDev plugin. I prefer it to Wing by *far* and if you prefer Eclipse to NetBeans for Java then it might be worth your while checking it out. If you take a few minutes to learn a few of the shortcuts in Eclipse you can really cut down the time to do a lot of administrative/organisational tasks. On Windows I remember Eclipse + Pydev being a bit of a memory hog but on OS X and Linux it works smoothly. On Thu, May 7, 2009 at 6:24 PM, Lawrence Hanser wrote: > Dear Colleagues, > > I have been using NetBeans for a month or so now and am reasonably > happy with it. ?I'm considering other options, and ran across Wing. > I'm interested in opinions about NetBeans and Wing as IDE for Python. > > Thanks, > > Larry > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://www.unprotectedhex.com http://www.smashthestack.org From dotancohen at gmail.com Thu May 7 13:35:02 2009 From: dotancohen at gmail.com (Dotan Cohen) Date: Thu, 7 May 2009 20:35:02 +0300 Subject: What would YOU like to see in a txt to html converter? In-Reply-To: References: Message-ID: <880dece00905071035v7d7749fbj6e0ccd9907b813c8@mail.gmail.com> > As you might have mentioned I'm just working on a txt to html converter > called "thc". This project is intended for me to learn Python and now pyQT4 > to which I changed a few days ago (started with Tkinter). > > I have implemented the following features so far: > > - Giving a title for the html > - Choose whether it's Transitional or Strict Doctype Don't give this choice. What benefit does this give the user? > - Select a background color > - Show the converted file with the standard browser That should probably be valid [X]HTML and be done with it. > - Working on a font size slider This should be a browser feature, not a page feature. Use the default sizes of the HTML elements, and let the UA override those defaults as the user sees fit ot configure it. If you think that there should be a font size slider, then file a feature request at the bug tracker of your favourite browser. > I don't really know if this is of any use for anybody but it's just a fun > project by a beginner :-) > > Now I'd like to know what kind of features you'd like to see in version 0.3 > of thc!? > You should probably convert the text to UTF-8 and make sure to declare that with a meta tag. You might want to look at markdown for a nice way to have hyperlink support. Lists conversion, like these: * item one * item two * item three 1. item one 2. item two 3. item three -- Dotan Cohen http://what-is-what.com http://gibberish.co.il From nad at acm.org Thu May 7 13:36:49 2009 From: nad at acm.org (Ned Deily) Date: Thu, 07 May 2009 13:36:49 -0400 Subject: Unable to build libpython2.5.so on OS X 10.4 References: <0e05eca2-b460-4e01-aa54-cc1055f51e7d@q14g2000vbn.googlegroups.com> Message-ID: In article <0e05eca2-b460-4e01-aa54-cc1055f51e7d at q14g2000vbn.googlegroups.com>, elwinter wrote: > The Python module I am building is actually the Python module > for ROOT, a large package from CERN. However, the problem arises > before that code enters the picture, when I am building Python itself. > All I want to do is create "libpython2.5.dylib", or its equivalent, > and I can't seem to make that happen on Tiger. Any chance you can use the python.org installer for 2.5.4? Or the Macports version? They both are supported on Tiger. Again, I may be missing something but libpythonx.x by itself is not very useful, i.e. it's not your usual standalone lib. You'll almost certainly need the whole python installation. There are a number of ways to build python on OS X (google will find them or ask on the Mac python sig) but, unless you have some special requirement, use an existing python installation and build the ROOT Python module with that. -- Ned Deily, nad at acm.org From bjracine at glosten.com Thu May 7 13:49:04 2009 From: bjracine at glosten.com (Benjamin J. Racine) Date: Thu, 7 May 2009 10:49:04 -0700 Subject: Wing vs Netbeans IDE? In-Reply-To: Message-ID: I'd love to see an updated shootout between these three, as I cannot for the life of me seem to be able to settle down with one of them. Things I don't like: Wing's lack of integrated mercurial/svn support. The clunkiness and scattered plugin approach of Eclipse (the latter is relavent when starting work on new machines all the time). Netbeans project browser doesn't show class or functions inside the files. Netbeans autocompletion doesn't seem immediately impressive. But... Wing has mentioned future support of svn,git,bazaar,mercurial,etc in the future. QT designer is integrated with Eclipse. So is Photran, which is convenient for some of science types. It seems that there are reasons to be optimistic about the future of netbeans. It seems to be well put together so far, but the least mature. I know that this comes up time and again, but I think a feature-specific debate over these might be worth revisiting for a lot of newcomers. Ben Racine From donovan.parks at gmail.com Thu May 7 14:07:33 2009 From: donovan.parks at gmail.com (Donovan Parks) Date: Thu, 7 May 2009 15:07:33 -0300 Subject: Updating an Imported Function Message-ID: <8fd818a30905071107h21c2eff7uad5aa177a84ff21b@mail.gmail.com> Hello, I'm new to Python and have what is probably a very basic question. I am writing a helloWorld() function within a file called helloWorld.py: def helloWorld(): print 'hi' Now, I can import and run this function: import helloWorld helloWorld.helloWorld() Which will print 'hi' as expected. Now, I'd like to update this function in my text editor so it is: def helloWorld(): print 'hello world' Without having to exit my Python interpreter, how can I import this revised function? If I do the import again: import helloWorld And run helloWorld.helloWorld() it will still print out just 'hi'. I've tried deleting this function (a weird concept to me as I come from a C++ background) using del helloWorld and than importing again, but the function will still print out just 'hi'. Can this be done? How so? Thanks for any and all help. Cheers, Donovan From norseman at hughes.net Thu May 7 14:13:27 2009 From: norseman at hughes.net (norseman) Date: Thu, 07 May 2009 11:13:27 -0700 Subject: What would YOU like to see in a txt to html converter? In-Reply-To: <880dece00905071035v7d7749fbj6e0ccd9907b813c8@mail.gmail.com> References: <880dece00905071035v7d7749fbj6e0ccd9907b813c8@mail.gmail.com> Message-ID: <4A0324C7.9090708@hughes.net> Dotan Cohen wrote: >> As you might have mentioned I'm just working on a txt to html converter >> called "thc". This project is intended for me to learn Python and now pyQT4 >> to which I changed a few days ago (started with Tkinter). >> >> I have implemented the following features so far: >> >> - Giving a title for the html >> - Choose whether it's Transitional or Strict Doctype > > Don't give this choice. What benefit does this give the user? > >> - Select a background color >> - Show the converted file with the standard browser > > That should probably be valid [X]HTML and be done with it. > >> - Working on a font size slider > > This should be a browser feature, not a page feature. Use the default > sizes of the HTML elements, and let the UA override those defaults as > the user sees fit ot configure it. > > If you think that there should be a font size slider, then file a > feature request at the bug tracker of your favourite browser. > >> I don't really know if this is of any use for anybody but it's just a fun >> project by a beginner :-) >> >> Now I'd like to know what kind of features you'd like to see in version 0.3 >> of thc!? >> > > You should probably convert the text to UTF-8 and make sure to declare > that with a meta tag. > > You might want to look at markdown for a nice way to have hyperlink support. > > Lists conversion, like these: > * item one > * item two > * item three > > 1. item one > 2. item two > 3. item three > ================================ Actually I would like to see a good HTML to txt or odt converter. Perhaps someone you know has the time and inclination to make one. In Unix, grep is a fantastic search tool. Since HTML's today seem to be one page one file, trying to search a "help collection" for a phrase is useless in the practical sense. I mean no offense to your effort. There are many who wish a good version of such a converter. It's just that I personally wish HTMLs used for Help and Documentation were banned from the universe. To me, a well written, easily understood, no frills, fully grep'able text file that covers the subject well is a truly fantastic thing to be well cherished. Steve From allenreas at gmail.com Thu May 7 14:19:36 2009 From: allenreas at gmail.com (AllenLars) Date: Thu, 7 May 2009 11:19:36 -0700 (PDT) Subject: Downloading most recently modified files Message-ID: <23432457.post@talk.nabble.com> I am trying to code a script that will allow me to go to ftp site and download files based on most recently modified file (date, time). I am brand new to programming. Any and all help is appreciated. -- View this message in context: http://www.nabble.com/Downloading-most-recently-modified-files-tp23432457p23432457.html Sent from the Python - python-list mailing list archive at Nabble.com. From cnbrat.at.gmail at dot.com.no.spam Thu May 7 14:22:57 2009 From: cnbrat.at.gmail at dot.com.no.spam (Will Wang) Date: Fri, 08 May 2009 02:22:57 +0800 Subject: What would YOU like to see in a txt to html converter? References: Message-ID: <87my9okc1q.fsf@lakeside.sjtu.edu.cn> >>>>> "Florian" == Florian Wollenschein writes: Florian> As you might have mentioned I'm just working on a txt to html converter called Florian> "thc". This project is intended for me to learn Python and now pyQT4 to which I Florian> changed a few days ago (started with Tkinter). Florian> I have implemented the following features so far: Florian> - Giving a title for the html Florian> - Choose whether it's Transitional or Strict Doctype Florian> - Select a background color Florian> - Show the converted file with the standard browser Florian> - Working on a font size slider Florian> I don't really know if this is of any use for anybody but it's just a fun Florian> project by a beginner :-) Florian> Now I'd like to know what kind of features you'd like to see in version 0.3 of Florian> thc!? Florian> Please post them... Florian> Have fun! Florian> Listick Florian> http://www.lictick.org You could learn something from emacs-muse. That plugin can help emacs to convert txt to html, tex, pdf, docbook and some other document format. In emacs-muse, the title and subtitle is defined like this: *emphasis* **strong emphasis** ***very strong emphasis*** _underlined_ =verbatim and monospace= emacs-muse : http://mwolson.org/projects/EmacsMuse.html From deets at nospam.web.de Thu May 7 14:23:21 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Thu, 07 May 2009 20:23:21 +0200 Subject: P2P text chat engine In-Reply-To: References: Message-ID: <76gn8qF1cliefU1@mid.uni-berlin.de> Navanjo schrieb: > If you have the source code of a p2p text chat engine please send to me I found that & a pot of gold under my bed. Care to give me your address so that I can send it to you? SCNR, Diez From deets at nospam.web.de Thu May 7 14:25:56 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Thu, 07 May 2009 20:25:56 +0200 Subject: Updating an Imported Function In-Reply-To: References: Message-ID: <76gndkF1cliefU2@mid.uni-berlin.de> Donovan Parks schrieb: > Hello, > > I'm new to Python and have what is probably a very basic question. I > am writing a helloWorld() function within a file called helloWorld.py: > > def helloWorld(): > print 'hi' > > Now, I can import and run this function: > > import helloWorld > helloWorld.helloWorld() > > Which will print 'hi' as expected. Now, I'd like to update this > function in my text editor so it is: > > def helloWorld(): > print 'hello world' > > Without having to exit my Python interpreter, how can I import this > revised function? If I do the import again: > > import helloWorld > > And run helloWorld.helloWorld() it will still print out just 'hi'. > I've tried deleting this function (a weird concept to me as I come > from a C++ background) using del helloWorld and than importing again, > but the function will still print out just 'hi'. > > Can this be done? How so? reload(helloWorld) But there are some caveats, and IMHO reloading isn't worth the trouble - it is *so* easy to write a second script, test.py or whatever, that you just call from a shell. And this has the added benefit that you can create even somewhat more elaborate toy-code. If one wants to play around with objects created, either do python -i test.py or try to use pdb, the python debugger, through import pdb; pdb.set_trace() to get an interactive prompt at whatever point you want. Diez From pavlovevidence at gmail.com Thu May 7 14:26:37 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Thu, 7 May 2009 11:26:37 -0700 (PDT) Subject: Threading and GIL References: <60538e1c-47ea-4120-bc91-a9dcebd8f14b@r34g2000vbi.googlegroups.com> Message-ID: <72ecd73c-2e6b-4a2a-85fc-e6dd8f8a591a@o27g2000vbd.googlegroups.com> On May 7, 12:20?am, googler.1.webmas... at spamgourmet.com wrote: > thats the reason why its not working. Imagine the end() method of the > thread object is called so the C++ Function is opened where the code > for this method is in. You're going to have to post some code if you want better help; this description is unintelligible. I don't know what you mean by end() method, nor whether it's a Python method or C++ method, nor what exactly you mean by thread object (C++ or Python object?) or C++ Function. > At a line the Code ...->End() is called which waits that the C++ > Thread class > is finished. BUT here is the problem: In the Method of the C++ class > which is in threaded mode can't run because its still waiting that the > GIL > is released by the thread which executed the ->End() command. > > So the app has a deadlock when it arrives at ->End() and > PyGILState_Ensure > function. So you have to release the GIL before calling End(). Just surround End () by Py_BEGIN_ALLOW_THREADS and Py_END_ALLOW_THREADS. (Presumably you know the thread state when you call End(); if you don't then surround the calls to Py_{BEGIN,END}_ALLOW_THREADS with calls to PyGILState_{Ensure,Release}. Yes, that means you acquire the GIL just so you can be sure that you've released it. I know of no other way to be sure you don't have the GIL.) If you are trying to kill a different thread, and I can't quite tell if that's what you're doing, then be aware that it is very tricky to do right and most think it to be a bad idea. If you don't allow the thread you're killing to clean up it can deadlock, and even if you do, you have to be careful to clean up properly and you have to be constantly on guard for what might happen in a thread is killed in the middle of something. Carl Banks From Florian.Wollenschein at FernUni-Hagen.de Thu May 7 14:33:56 2009 From: Florian.Wollenschein at FernUni-Hagen.de (Florian Wollenschein) Date: Thu, 07 May 2009 20:33:56 +0200 Subject: What would YOU like to see in a txt to html converter? In-Reply-To: <87my9okc1q.fsf@lakeside.sjtu.edu.cn> References: <87my9okc1q.fsf@lakeside.sjtu.edu.cn> Message-ID: Will Wang wrote: >>>>>> "Florian" == Florian Wollenschein writes: > > Florian> As you might have mentioned I'm just working on a txt to html converter called > Florian> "thc". This project is intended for me to learn Python and now pyQT4 to which I > Florian> changed a few days ago (started with Tkinter). > > Florian> I have implemented the following features so far: > > Florian> - Giving a title for the html > Florian> - Choose whether it's Transitional or Strict Doctype > Florian> - Select a background color > Florian> - Show the converted file with the standard browser > Florian> - Working on a font size slider > > Florian> I don't really know if this is of any use for anybody but it's just a fun > Florian> project by a beginner :-) > > Florian> Now I'd like to know what kind of features you'd like to see in version 0.3 of > Florian> thc!? > > Florian> Please post them... > > Florian> Have fun! > Florian> Listick > Florian> http://www.lictick.org > > You could learn something from emacs-muse. That plugin can help emacs to > convert txt to html, tex, pdf, docbook and some other document format. > > In emacs-muse, the title and subtitle is defined like this: > > *emphasis* > **strong emphasis** > ***very strong emphasis*** > _underlined_ > =verbatim and monospace= > > emacs-muse : http://mwolson.org/projects/EmacsMuse.html Thank you for this information. I already thought of using dots or asterisks or whatever to let the user format the text instead of using html tags (this would be quite paradox ;-) Please keep on posting ideas... Thanks again, Listick http://www.listick.org From alan.cameron at iname.com Thu May 7 14:35:07 2009 From: alan.cameron at iname.com (Alan Cameron) Date: Thu, 7 May 2009 19:35:07 +0100 Subject: Newcomer to Python tutorial question Message-ID: I am not sure of this is the right place to ask a question about the tutorial http://docs.python.org/3.0/tutorial/datastructures.html#sets why is the printed result of >>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'} >>> print(basket) {'orange', 'banana', 'pear', 'apple'} in the sequence given? -- Alan Cameron From muhammadanushanif at gmail.com Thu May 7 14:38:04 2009 From: muhammadanushanif at gmail.com (muhammadanushanif at gmail.com) Date: Thu, 7 May 2009 11:38:04 -0700 (PDT) Subject: Get Into The World Of YOUSUF JUSANI Message-ID: Get The Most Valuable and Best For Investment Properties IN ASIA, !! Real Estate Builders.. Real Estate Dealer Buy, Sell REAL EASTAE IN GULF ,DUBAI AND AUSTRIA FOR MORE INFO http://www.yousufjusani.blogspot.com From clp2 at rebertia.com Thu May 7 14:40:57 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Thu, 7 May 2009 11:40:57 -0700 Subject: Newcomer to Python tutorial question In-Reply-To: References: Message-ID: <50697b2c0905071140m3978dfa9x6b320e8a3f9d28ee@mail.gmail.com> On Thu, May 7, 2009 at 11:35 AM, Alan Cameron wrote> I am not sure of this is the right place to ask a question about the > tutorial > > http://docs.python.org/3.0/tutorial/datastructures.html#sets > > why is the printed result of > >>>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'} >>>> print(basket) > {'orange', 'banana', 'pear', 'apple'} > > in the sequence given? Because it's *not a sequence* at all, it's a set. Sets are unordered and contain no duplicate items, hence why the output ordering is arbitrary and only the unique subset of original elements is present. Further info: http://docs.python.org/3.0/library/stdtypes.html#set-types-set-frozenset Cheers, Chris -- http://blog.rebertia.com From Florian.Wollenschein at FernUni-Hagen.de Thu May 7 14:42:20 2009 From: Florian.Wollenschein at FernUni-Hagen.de (Florian Wollenschein) Date: Thu, 07 May 2009 20:42:20 +0200 Subject: Newcomer to Python tutorial question In-Reply-To: References: Message-ID: Alan Cameron wrote: > I am not sure of this is the right place to ask a question about the > tutorial > > http://docs.python.org/3.0/tutorial/datastructures.html#sets > > why is the printed result of > >>>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'} >>>> print(basket) > {'orange', 'banana', 'pear', 'apple'} > > in the sequence given? > A set is not ordered and eliminates duplicate elements. So the output is random in terms of the order and only shows each single item once... Correct me if I'm wrong :-) Listick http://www.listick.org From arnodel at googlemail.com Thu May 7 14:43:08 2009 From: arnodel at googlemail.com (Arnaud Delobelle) Date: Thu, 07 May 2009 19:43:08 +0100 Subject: Newcomer to Python tutorial question References: Message-ID: "Alan Cameron" writes: > I am not sure of this is the right place to ask a question about the > tutorial > > http://docs.python.org/3.0/tutorial/datastructures.html#sets > > why is the printed result of > >>>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'} >>>> print(basket) > {'orange', 'banana', 'pear', 'apple'} > > in the sequence given? A set is an unordered container, but due to the nature of an object representation (which is a sequence of characters), its representation has to list the elements in a certain order. However, this order is not significant. -- Arnaud From news123 at free.fr Thu May 7 14:56:13 2009 From: news123 at free.fr (News123) Date: Thu, 07 May 2009 20:56:13 +0200 Subject: What would YOU like to see in a txt to html converter? In-Reply-To: References: Message-ID: <4a032ece$0$4849$426a74cc@news.free.fr> Florian Wollenschein wrote: > As you might have mentioned I'm just working on a txt to html converter > called "thc". This project is intended for me to learn Python and now > pyQT4 to which I changed a few days ago (started with Tkinter). > > I have implemented the following features so far: > > - Giving a title for the html > - Choose whether it's Transitional or Strict Doctype > - Select a background color > - Show the converted file with the standard browser > - Working on a font size slider > > I don't really know if this is of any use for anybody but it's just a > fun project by a beginner :-) > > Now I'd like to know what kind of features you'd like to see in version > 0.3 of thc!? > > Please post them... > > Have fun! > Listick > http://www.lictick.org I would first think about your personal requirements / use cases. What kind of texts do you want to convert? I think there's already quite some text to html converters, which you could take as inspiration. Most of them were written for a specific purpose though and started from text files with some 'formatting syntax' Exanples: man2html converts man pages to html pod2html covnerts perl online documentation to html There's also converters from certain wiki formats to html. There's also source code formatters for certain programming languages or log file to html converters which color certain message types If it is just for plain text with no special formattings, then you need probably only: - escape all characters, which have to be escap for html. (but there is probably already a function for it. ) - seperate paragraphs (at double new lines or at single new lines. This could be an option) - additionally you could allow to specify a html prefix (before the converted text) and a html post fix after the converted text. - I would handle settings lik color / font size etc with style sheets and not within the generated html. If you want you could autgenerate style sheets from some function arguments. They could contain Font color / background color / font formatting . . . . - you could add a configuration, that automatically tags certain words with certain tags, to allow simle keyword hihglighting. It all depends on your use case. From alan.cameron at iname.com Thu May 7 14:58:34 2009 From: alan.cameron at iname.com (Alan Cameron) Date: Thu, 7 May 2009 19:58:34 +0100 Subject: Newcomer to Python tutorial question References: Message-ID: "Alan Cameron" wrote in message news:hRFMl.50224$tb.4955 at newsfe07.ams2... >I am not sure of this is the right place to ask a question about the >tutorial > > http://docs.python.org/3.0/tutorial/datastructures.html#sets > > why is the printed result of > >>>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'} >>>> print(basket) > {'orange', 'banana', 'pear', 'apple'} > > in the sequence given? > > Thanks to all who replied. I assume therefore that the order in which the items of the set are printed could vary each time it is printed? -- Alan Cameron From __peter__ at web.de Thu May 7 14:58:49 2009 From: __peter__ at web.de (Peter Otten) Date: Thu, 07 May 2009 20:58:49 +0200 Subject: Newcomer to Python tutorial question References: Message-ID: Alan Cameron wrote: > I am not sure of this is the right place to ask a question about the > tutorial > > http://docs.python.org/3.0/tutorial/datastructures.html#sets > > why is the printed result of > >>>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'} >>>> print(basket) > {'orange', 'banana', 'pear', 'apple'} > > in the sequence given? As already said by others, the order of items in a set is not part of the concept of a set. You can even have sets with equal contents that display differently: Python 3.0.1+ (r301:69556, Apr 15 2009, 17:25:52) [GCC 4.3.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> a = {'orange', 'banana', 'apple', 'orange', 'pear', 'apple'} >>> b = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'} >>> a == b True >>> repr(a) == repr(b) False >>> a {'orange', 'pear', 'apple', 'banana'} >>> b {'orange', 'pear', 'banana', 'apple'} Peter From amrbekhit at gmail.com Thu May 7 15:04:18 2009 From: amrbekhit at gmail.com (Amr) Date: Thu, 7 May 2009 12:04:18 -0700 (PDT) Subject: Could this expression parser be more 'Pythonic'? References: Message-ID: Hi John, Thanks for the tips, I will check them out. --Amr From stefan_ml at behnel.de Thu May 7 15:05:10 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Thu, 07 May 2009 21:05:10 +0200 Subject: What would YOU like to see in a txt to html converter? In-Reply-To: References: <87my9okc1q.fsf@lakeside.sjtu.edu.cn> Message-ID: <4a0330e6$0$30238$9b4e6d93@newsspool1.arcor-online.net> Florian Wollenschein wrote: > Will Wang wrote: >> *emphasis* >> **strong emphasis** >> ***very strong emphasis*** >> _underlined_ >> =verbatim and monospace= >> >> emacs-muse : http://mwolson.org/projects/EmacsMuse.html > > Thank you for this information. I already thought of using dots or > asterisks or whatever to let the user format the text instead of using > html tags (this would be quite paradox ;-) First thing I'd look at is actually docutils' RestructuredText (ReST). Stefan From clp2 at rebertia.com Thu May 7 15:08:51 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Thu, 7 May 2009 12:08:51 -0700 Subject: Newcomer to Python tutorial question In-Reply-To: References: Message-ID: <50697b2c0905071208q1fd88762xd007706f11b8a829@mail.gmail.com> On Thu, May 7, 2009 at 11:58 AM, Alan Cameron wrote: > "Alan Cameron" wrote in message > news:hRFMl.50224$tb.4955 at newsfe07.ams2... >>I am not sure of this is the right place to ask a question about the >>tutorial >> >> http://docs.python.org/3.0/tutorial/datastructures.html#sets >> >> why is the printed result of >> >>>>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'} >>>>> print(basket) >> {'orange', 'banana', 'pear', 'apple'} >> >> in the sequence given? >> >> > > Thanks to all who replied. > I assume therefore that the order in which the items of the set are printed > could vary each time it is printed? Due to the underlying dict-based implementation, the order will stay the same until you modify the set (i.e. add or remove an element), at which point it may change; it's basically the same behavior as with printing a dict. So this will always print the same thing twice: print basket print basket Whereas this might not: print basket #modify the set basket.discard("banana") basket.add("banana") print basket Cheers, Chris -- http://blog.rebertia.com From jcd at sdf.lonestar.org Thu May 7 15:10:38 2009 From: jcd at sdf.lonestar.org (J. Clifford Dyer) Date: Thu, 07 May 2009 15:10:38 -0400 Subject: Wing vs Netbeans IDE? In-Reply-To: References: Message-ID: <1241723438.22531.31.camel@mctell> On Thu, 2009-05-07 at 10:49 -0700, Benjamin J. Racine wrote: > I'd love to see an updated shootout between these three, as I cannot for the life of me seem to be able to settle down with one of them. > > Things I don't like: > Wing's lack of integrated mercurial/svn support. Wing *does* have integrated SVN support already, but nothing for the distributed systems yet. I just purchased a Wing license this past week. So far I'm very happy with it, though I can't speak to a comparison with other IDEs. From __peter__ at web.de Thu May 7 15:14:07 2009 From: __peter__ at web.de (Peter Otten) Date: Thu, 07 May 2009 21:14:07 +0200 Subject: Newcomer to Python tutorial question References: Message-ID: Alan Cameron wrote: > "Alan Cameron" wrote in message > news:hRFMl.50224$tb.4955 at newsfe07.ams2... >>I am not sure of this is the right place to ask a question about the >>tutorial >> >> http://docs.python.org/3.0/tutorial/datastructures.html#sets >> >> why is the printed result of >> >>>>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'} >>>>> print(basket) >> {'orange', 'banana', 'pear', 'apple'} >> >> in the sequence given? >> >> > > Thanks to all who replied. > I assume therefore that the order in which the items of the set are > printed could vary each time it is printed? If you don't add or remove items the printed order will not change in the current implementation. But as shown in my other post it is possible to create sets with equal contents that are printed differently. The actual order depends on the set's history of insertions/deletions, so it is not truly random. But these are implementation details that may change across versions of python and your code should never rely on them. If you want a defined order convert the set to a sorted list before printing: >>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'} >>> sorted(basket) ['apple', 'banana', 'orange', 'pear'] Peter From alan.cameron at iname.com Thu May 7 15:15:44 2009 From: alan.cameron at iname.com (Alan Cameron) Date: Thu, 7 May 2009 20:15:44 +0100 Subject: Newcomer to Python tutorial question References: Message-ID: "Chris Rebert" wrote in message news:mailman.5238.1241723354.11746.python-list at python.org... > On Thu, May 7, 2009 at 11:58 AM, Alan Cameron > wrote: >> "Alan Cameron" wrote in message >> news:hRFMl.50224$tb.4955 at newsfe07.ams2... >>>I am not sure of this is the right place to ask a question about the >>>tutorial >>> >>> http://docs.python.org/3.0/tutorial/datastructures.html#sets >>> >>> why is the printed result of >>> >>>>>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'} >>>>>> print(basket) >>> {'orange', 'banana', 'pear', 'apple'} >>> >>> in the sequence given? >>> >>> >> >> Thanks to all who replied. >> I assume therefore that the order in which the items of the set are >> printed >> could vary each time it is printed? > > Due to the underlying dict-based implementation, the order will stay > the same until you modify the set (i.e. add or remove an element), at > which point it may change; it's basically the same behavior as with > printing a dict. > > So this will always print the same thing twice: > print basket > print basket > > Whereas this might not: > print basket > #modify the set > basket.discard("banana") > basket.add("banana") > print basket > > Cheers, > Chris Thanks Chris, It appears that I used a reserved term when I used 'sequence'. I just had not reached that far in the tutorial. I have many years of programming (roughly 50) and want to learn new languages. I find tutorials always fraught with problems due to the knowledge of the writer exceeding the knowledge of the reader and using terms and examples not yet covered in the tutorial thus far. I am persevering with my initial foray into Python. -- Alan Cameron From googler.1.webmaster at spamgourmet.com Thu May 7 15:27:00 2009 From: googler.1.webmaster at spamgourmet.com (googler.1.webmaster at spamgourmet.com) Date: Thu, 7 May 2009 12:27:00 -0700 (PDT) Subject: Threading and GIL References: <60538e1c-47ea-4120-bc91-a9dcebd8f14b@r34g2000vbi.googlegroups.com> <72ecd73c-2e6b-4a2a-85fc-e6dd8f8a591a@o27g2000vbd.googlegroups.com> Message-ID: <7a659f98-47c3-45f8-b29d-ab3944a2ebb4@s31g2000vbp.googlegroups.com> hey, thanks, that works fine. I wrapped it around, done a lot of tests and it works fine. Just had done a few other things to make it stable. cheers. From pruebauno at latinmail.com Thu May 7 15:47:37 2009 From: pruebauno at latinmail.com (pruebauno at latinmail.com) Date: Thu, 7 May 2009 12:47:37 -0700 (PDT) Subject: Python 3.1 beta 1 References: Message-ID: On May 6, 9:32?pm, Benjamin Peterson wrote: > On behalf of the Python development team, I'm thrilled to announce the first and > only beta release of Python 3.1. > > Python 3.1 focuses on the stabilization and optimization of features and changes > Python 3.0 introduced. ?For example, the new I/O system has been rewritten in C > for speed. ?File system APIs that use unicode strings now handle paths with > undecodable bytes in them. [1] Other features include an ordered dictionary > implementation and support for ttk Tile in Tkinter. ?For a more extensive list > of changes in 3.1, seehttp://doc.python.org/dev/py3k/whatsnew/3.1.htmlor > Misc/NEWS in the Python distribution. > > Please note that this is a beta release, and as such is not suitable for > production environments. ?We continue to strive for a high degree of quality, > but there are still some known problems and the feature sets have not been > finalized. ?This beta is being released to solicit feedback and hopefully > discover bugs, as well as allowing you to determine how changes in 3.1 might > impact you. ?If you find things broken or incorrect, please submit a bug report > at > > ? ?http://bugs.python.org > > For more information and downloadable distributions, see the Python 3.1 website: > > ? ?http://www.python.org/download/releases/3.1/ > > See PEP 375 for release schedule details: > > ? ?http://www.python.org/dev/peps/pep-0375/ > > Enjoy, > -- Benjamin > > Benjamin Peterson > benjamin at python.org > Release Manager > (on behalf of the entire python-dev team and 3.1's contributors) Congratulations! Is it just me or was some nice summary output added to the make process? I get a nice list of modules that didn't compile and the ones where the library could not be found. From OldGrantonian at googlemail.com Thu May 7 15:53:38 2009 From: OldGrantonian at googlemail.com (OldGrantonian) Date: Thu, 7 May 2009 12:53:38 -0700 (PDT) Subject: Which python version do I use with "virtualenv"? References: <61fc8aa3-764c-465b-86aa-1b25631609e4@r34g2000vba.googlegroups.com> Message-ID: <3f3497db-a8a3-4c79-92cc-2a65d99baf71@r34g2000vba.googlegroups.com> On May 7, 12:34?pm, Duncan Booth wrote: > OldGrantonian wrote: > > Thanks to both Steven D'Aprano and Ant :) > > >>> ?Sounds like you've downloaded the Python 2.5 version of Easy Install. > > > There's no Python 2.6 version of EasyInstall :( > > I wonder what I've been running then? > > > > > For 2.5, there is: > > > setuptools-0.6c9.win32-py2.5.exe > > > But for 2.6, it's: > > > setuptools-0.6c9-py2.6.egg > > > For any other egg file, I would use EasyInstall, but I don't think I > > can use EasyInstall to install EasyInstall :) > > No, but you can use ez_setup. > > Go tohttp://peak.telecommunity.com/dist/ez_setup.pyand save the content > of that file as ez_setup.py (cut and paste into your favourite editor may > be the simplest way) then run it with your chosen Python. It will find and > install the appropriate version of easy install. > > -- > Duncan Boothhttp://kupuguy.blogspot.com ez_setup worked fine. Thanks for that :) According to the web site: http://pypi.python.org/pypi/virtualenv the next step is to do: virtualenv ENV If I do that, I get the following message: -------------------------------- C:\Python26\Scripts>virtualenv ENV Traceback (most recent call last): File "C:\Python26\Scripts\virtualenv-script.py", line 8, in load_entry_point('virtualenv==1.3.3', 'console_scripts', 'virtualenv')() File "C:\Python26\lib\site-packages\virtualenv-1.3.3-py2.6.egg \virtualenv.py", line 420 unzip_setuptools=options.unzip_setuptools) File "C:\Python26\lib\site-packages\virtualenv-1.3.3-py2.6.egg \virtualenv.py", line 499 home_dir, lib_dir, inc_dir, bin_dir = path_locations(home_dir) File "C:\Python26\lib\site-packages\virtualenv-1.3.3-py2.6.egg \virtualenv.py", line 521 import win32api ImportError: No module named win32api C:\Python26\Scripts> -------------------------------- Any advice? From piet at cs.uu.nl Thu May 7 15:53:46 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Thu, 07 May 2009 21:53:46 +0200 Subject: Unable to build libpython2.5.so on OS X 10.4 References: <0e05eca2-b460-4e01-aa54-cc1055f51e7d@q14g2000vbn.googlegroups.com> Message-ID: >>>>> elwinter (e) wrote: >e> Hi Ned. The Python module I am building is actually the Python module >e> for ROOT, a large package from CERN. However, the problem arises >e> before that code enters the picture, when I am building Python itself. >e> All I want to do is create "libpython2.5.dylib", or its equivalent, >e> and I can't seem to make that happen on Tiger. The standard installer for Python 2.5.4 when installed on Tiger, will contain a file libpython2.5.a which is a symbolic link to /Library/Frameworks/Python.framework/Versions/2.5/Python, which according to the file command, is a shared library. So it is not a static library, despite of its name! >e> Thanks, >e> Eric >e> On May 7, 12:41?pm, Ned Deily wrote: >>> In article >>> , >>> ?Eric Winter wrote: >>> >>> > Hi all. I'm trying to build some internal code that needs to link >>> > against libpython2.5.so on a OS X 10.4 (Tiger) machine. It seems that >>> > no matter what combination of options and environment variables I give >>> > to the configure script from python 2.5.1, all I get is the >>> > libpython2.5.a (the static library). I've googled the problem and >>> > searched the comp.lang.python archives, but I have been unable to find >>> > anything that works. >>> >>> Perhaps I misunderstand, but if you are trying to build a C extension >>> for an existing Python 2.5 installation, using Distutils from that >>> installation should take care of everything for you. ?Is there a >>> setup.py file by any chance? ?Are you using a standard python >>> installation (i.e. python.org installer for instance)? ?More details >>> might help. >>> >>> -- >>> ?Ned Deily, >>> ?n... at acm.org -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From rent.lupin.road at gmail.com Thu May 7 15:59:50 2009 From: rent.lupin.road at gmail.com (James) Date: Thu, 7 May 2009 12:59:50 -0700 (PDT) Subject: Best practice for operations on streams of text Message-ID: <340d1511-8b2c-41bc-950d-3e25bdd433fb@j9g2000prh.googlegroups.com> Hello all, I'm working on some NLP code - what I'm doing is passing a large number of tokens through a number of filtering / processing steps. The filters take a token as input, and may or may not yield a token as a result. For example, I might have filters which lowercases the input, filter out boring words and filter out duplicates chained together. I originally had code like this: for t0 in token_stream: for t1 in lowercase_token(t0): for t2 in remove_boring(t1): for t3 in remove_dupes(t2): yield t3 Apart from being ugly as sin, I only get one token out as StopIteration is raised before the whole token stream is consumed. Any suggestions on an elegant way to chain together a bunch of generators, with processing steps in between? Thanks, James From emile at fenx.com Thu May 7 16:01:11 2009 From: emile at fenx.com (Emile van Sebille) Date: Thu, 07 May 2009 13:01:11 -0700 Subject: P2P text chat engine In-Reply-To: <76gn8qF1cliefU1@mid.uni-berlin.de> References: <76gn8qF1cliefU1@mid.uni-berlin.de> Message-ID: On 5/7/2009 11:23 AM Diez B. Roggisch said... > Navanjo schrieb: >> If you have the source code of a p2p text chat engine please send to me > > I found that & a pot of gold under my bed. Care to give me your address > so that I can send it to you? > Yeah -- this is how it starts. Before too long you'll need my bank account info to facilitate the transfer... :) Emile From emile at fenx.com Thu May 7 16:03:27 2009 From: emile at fenx.com (Emile van Sebille) Date: Thu, 07 May 2009 13:03:27 -0700 Subject: Which python version do I use with "virtualenv"? In-Reply-To: <3f3497db-a8a3-4c79-92cc-2a65d99baf71@r34g2000vba.googlegroups.com> References: <61fc8aa3-764c-465b-86aa-1b25631609e4@r34g2000vba.googlegroups.com> <3f3497db-a8a3-4c79-92cc-2a65d99baf71@r34g2000vba.googlegroups.com> Message-ID: On 5/7/2009 12:53 PM OldGrantonian said... > On May 7, 12:34 pm, Duncan Booth wrote: >> OldGrantonian wrote: >>> Thanks to both Steven D'Aprano and Ant :) >>>>> Sounds like you've downloaded the Python 2.5 version of Easy Install. >>> There's no Python 2.6 version of EasyInstall :( >> I wonder what I've been running then? >> >> >> >>> For 2.5, there is: >>> setuptools-0.6c9.win32-py2.5.exe >>> But for 2.6, it's: >>> setuptools-0.6c9-py2.6.egg >>> For any other egg file, I would use EasyInstall, but I don't think I >>> can use EasyInstall to install EasyInstall :) >> No, but you can use ez_setup. >> >> Go tohttp://peak.telecommunity.com/dist/ez_setup.pyand save the content >> of that file as ez_setup.py (cut and paste into your favourite editor may >> be the simplest way) then run it with your chosen Python. It will find and >> install the appropriate version of easy install. >> >> -- >> Duncan Boothhttp://kupuguy.blogspot.com > > > ez_setup worked fine. Thanks for that :) > > According to the web site: > > http://pypi.python.org/pypi/virtualenv > > the next step is to do: > > virtualenv ENV > > If I do that, I get the following message: > > -------------------------------- > > C:\Python26\Scripts>virtualenv ENV > Traceback (most recent call last): > File "C:\Python26\Scripts\virtualenv-script.py", line 8, in > load_entry_point('virtualenv==1.3.3', 'console_scripts', > 'virtualenv')() > File "C:\Python26\lib\site-packages\virtualenv-1.3.3-py2.6.egg > \virtualenv.py", line 420 > unzip_setuptools=options.unzip_setuptools) > File "C:\Python26\lib\site-packages\virtualenv-1.3.3-py2.6.egg > \virtualenv.py", line 499 > home_dir, lib_dir, inc_dir, bin_dir = path_locations(home_dir) > File "C:\Python26\lib\site-packages\virtualenv-1.3.3-py2.6.egg > \virtualenv.py", line 521 > import win32api > ImportError: No module named win32api > > C:\Python26\Scripts> > > -------------------------------- > > Any advice? Install the win32 extensions or install the 2.6 Activestate release. Emile From james at agentultra.com Thu May 7 16:06:43 2009 From: james at agentultra.com (J Kenneth King) Date: Thu, 07 May 2009 16:06:43 -0400 Subject: Best practice for operations on streams of text References: <340d1511-8b2c-41bc-950d-3e25bdd433fb@j9g2000prh.googlegroups.com> Message-ID: <851vr0wucs.fsf@agentultra.com> James writes: > Hello all, > I'm working on some NLP code - what I'm doing is passing a large > number of tokens through a number of filtering / processing steps. > > The filters take a token as input, and may or may not yield a token as > a result. For example, I might have filters which lowercases the > input, filter out boring words and filter out duplicates chained > together. > > I originally had code like this: > for t0 in token_stream: > for t1 in lowercase_token(t0): > for t2 in remove_boring(t1): > for t3 in remove_dupes(t2): > yield t3 > > Apart from being ugly as sin, I only get one token out as > StopIteration is raised before the whole token stream is consumed. > > Any suggestions on an elegant way to chain together a bunch of > generators, with processing steps in between? > > Thanks, > James Co-routines my friends. Google will help you greatly in discovering this processing wonder. From tjreedy at udel.edu Thu May 7 16:10:03 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 07 May 2009 16:10:03 -0400 Subject: list comprehension question In-Reply-To: <8563gcx1p1.fsf@agentultra.com> References: <6d502cfd-e252-4722-96a5-c52fc3579499@v1g2000prd.googlegroups.com> <85ocucnbce.fsf@agentultra.com> <85ljpb8r1o.fsf@agentultra.com> <85d4am8hq4.fsf@agentultra.com> <8563gcx1p1.fsf@agentultra.com> Message-ID: J Kenneth King wrote: > > Keep in mind that nested comprehensions are still available because > they do have a use case that justifies their existence. Nested comprehensions are available because because the syntax makes them available by default and making a fiddly exception would be contrary to Python's style. A list comp creates an iterable. A list comp requires use of an iterable. Therefore, a list comp may use a list comp. > However, I > think the Python documentation warns against their use because people > might rely on them for problems where they aren't necessary and since > they are difficult to read... it can lead to difficult to read code. Whenever the expression that results in the iterable used by a list comp is sufficiently complex, readability is improved by pulling it out as a separate statement. Nested list comps are often examplex of such sufficiently complex expressions, but not the only possible one. tjr From OldGrantonian at googlemail.com Thu May 7 16:15:41 2009 From: OldGrantonian at googlemail.com (OldGrantonian) Date: Thu, 7 May 2009 13:15:41 -0700 (PDT) Subject: Which python version do I use with "virtualenv"? References: <61fc8aa3-764c-465b-86aa-1b25631609e4@r34g2000vba.googlegroups.com> <3f3497db-a8a3-4c79-92cc-2a65d99baf71@r34g2000vba.googlegroups.com> Message-ID: <2be30c71-4eb3-4ad7-a138-31ee0698a297@e20g2000vbc.googlegroups.com> On May 7, 9:03?pm, Emile van Sebille wrote: > On 5/7/2009 12:53 PM OldGrantonian said... > > > > > On May 7, 12:34 pm, Duncan Booth wrote: > >> OldGrantonian wrote: > >>> Thanks to both Steven D'Aprano and Ant :) > >>>>> ?Sounds like you've downloaded the Python 2.5 version of Easy Install. > >>> There's no Python 2.6 version of EasyInstall :( > >> I wonder what I've been running then? > > >>> For 2.5, there is: > >>> setuptools-0.6c9.win32-py2.5.exe > >>> But for 2.6, it's: > >>> setuptools-0.6c9-py2.6.egg > >>> For any other egg file, I would use EasyInstall, but I don't think I > >>> can use EasyInstall to install EasyInstall :) > >> No, but you can use ez_setup. > > >> Go tohttp://peak.telecommunity.com/dist/ez_setup.pyandsave the content > >> of that file as ez_setup.py (cut and paste into your favourite editor may > >> be the simplest way) then run it with your chosen Python. It will find and > >> install the appropriate version of easy install. > > >> -- > >> Duncan Boothhttp://kupuguy.blogspot.com > > > ez_setup worked fine. Thanks for that :) > > > According to the web site: > > >http://pypi.python.org/pypi/virtualenv > > > the next step is to do: > > > virtualenv ENV > > > If I do that, I get the following message: > > > -------------------------------- > > > C:\Python26\Scripts>virtualenv ENV > > Traceback (most recent call last): > > ? File "C:\Python26\Scripts\virtualenv-script.py", line 8, in > > ? ? load_entry_point('virtualenv==1.3.3', 'console_scripts', > > 'virtualenv')() > > ? File "C:\Python26\lib\site-packages\virtualenv-1.3.3-py2.6.egg > > \virtualenv.py", line 420 > > ? ? unzip_setuptools=options.unzip_setuptools) > > ? File "C:\Python26\lib\site-packages\virtualenv-1.3.3-py2.6.egg > > \virtualenv.py", line 499 > > ? ? home_dir, lib_dir, inc_dir, bin_dir = path_locations(home_dir) > > ? File "C:\Python26\lib\site-packages\virtualenv-1.3.3-py2.6.egg > > \virtualenv.py", line 521 > > ? ? import win32api > > ImportError: No module named win32api > > > C:\Python26\Scripts> > > > -------------------------------- > > > Any advice? > > Install the win32 extensions or install the 2.6 Activestate release. > > Emile Thanks for the response :) Where do I find the win32 extensions? From tjreedy at udel.edu Thu May 7 16:21:52 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 07 May 2009 16:21:52 -0400 Subject: Python 3.1 beta 1 In-Reply-To: <7c96bd34-b199-4646-a790-4097adcc6999@q14g2000vbn.googlegroups.com> References: <7c96bd34-b199-4646-a790-4097adcc6999@q14g2000vbn.googlegroups.com> Message-ID: bearophileHUGS at lycos.com wrote: > Is the order inside OrderedDict kept with a double linked list of the > items? That is one of the things Raymond tried. Check the code for what he settled on for the Python version. I believe he thinks the best C implementation might be different from the best Python version. >> The OrderedDict constructor and update() method both accept >> keyword arguments, but their order is lost because Python's >> function call semantics pass-in keyword arguments using a regular >> unordered dictionary. > > This is unfortunate :-( > Well, I'd like function call semantics pass-in keyword arguments to > use OrderedDicts then... :-) This possibility has been discussed on pydev. It would require a sufficiently fast C implementation. tjr From gherron at islandtraining.com Thu May 7 16:23:57 2009 From: gherron at islandtraining.com (Gary Herron) Date: Thu, 07 May 2009 13:23:57 -0700 Subject: Best practice for operations on streams of text In-Reply-To: <340d1511-8b2c-41bc-950d-3e25bdd433fb@j9g2000prh.googlegroups.com> References: <340d1511-8b2c-41bc-950d-3e25bdd433fb@j9g2000prh.googlegroups.com> Message-ID: <4A03435D.20908@islandtraining.com> James wrote: > Hello all, > I'm working on some NLP code - what I'm doing is passing a large > number of tokens through a number of filtering / processing steps. > > The filters take a token as input, and may or may not yield a token as > a result. For example, I might have filters which lowercases the > input, filter out boring words and filter out duplicates chained > together. > > I originally had code like this: > for t0 in token_stream: > for t1 in lowercase_token(t0): > for t2 in remove_boring(t1): > for t3 in remove_dupes(t2): > yield t3 > > Apart from being ugly as sin, I only get one token out as > StopIteration is raised before the whole token stream is consumed. > > Any suggestions on an elegant way to chain together a bunch of > generators, with processing steps in between? > > Thanks, > James > -- > http://mail.python.org/mailman/listinfo/python-list > David Beazly has a very interesting talk on using generators for building and linking together individual stream filters. Its very cool and surprisingly eye-opening. See "Generator Tricks for Systems Programmers" at http://www.dabeaz.com/generators/ Gary Herron From duncan.booth at invalid.invalid Thu May 7 16:33:12 2009 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 7 May 2009 20:33:12 GMT Subject: Which python version do I use with "virtualenv"? References: <61fc8aa3-764c-465b-86aa-1b25631609e4@r34g2000vba.googlegroups.com> <3f3497db-a8a3-4c79-92cc-2a65d99baf71@r34g2000vba.googlegroups.com> <2be30c71-4eb3-4ad7-a138-31ee0698a297@e20g2000vbc.googlegroups.com> Message-ID: OldGrantonian wrote: > Where do I find the win32 extensions? http://www.google.com/search?q=python+win32 Any of the first 4 hits should help. From Shawn at Milochik.com Thu May 7 16:36:55 2009 From: Shawn at Milochik.com (Shawn Milochik) Date: Thu, 7 May 2009 16:36:55 -0400 Subject: Downloading most recently modified files In-Reply-To: <23432457.post@talk.nabble.com> References: <23432457.post@talk.nabble.com> Message-ID: <2dc0c81b0905071336l365f6b3aq51761873b52e38d0@mail.gmail.com> On Thu, May 7, 2009 at 2:19 PM, AllenLars wrote: > > I am trying to code a script that will allow me to go to ftp site and > download files based on most recently modified file (date, time). ?I am > brand new to programming. ?Any and all help is appreciated. > -- I've actually written code to do this, and it's fairly easy. Just use the FTP module to run the "ls" (directory listing) command and parse the results to get the filenames and timestamps. Then you will be able to easily do download requests for the file(s) you want. Perhaps someone else on the list can chime in with a more elegant solution. Here's enough to get you started: from ftplib import FTP #(http://docs.python.org/library/ftplib.html) ShawnMilo From pruebauno at latinmail.com Thu May 7 16:39:25 2009 From: pruebauno at latinmail.com (pruebauno at latinmail.com) Date: Thu, 7 May 2009 13:39:25 -0700 (PDT) Subject: Python 3.1 beta 1 References: <7c96bd34-b199-4646-a790-4097adcc6999@q14g2000vbn.googlegroups.com> Message-ID: On May 7, 11:57?am, bearophileH... at lycos.com wrote: > >Equality tests between OrderedDict objects are order-sensitive and are implemented as list(od1.items())==list(od2.items()). Equality tests between OrderedDict objects and other Mapping objects are order-insensitive< > > very nice idea. > I don't know if somebody else is interested but I wouldn't mind support to OrderedDict and namedtuple in the csv module: Have csv.DictReader return an OrderedDict instead of a regular one (based on the order of the column names in the header line). Have csv.DictWriter accept an OrderedDict. That would take care of this: "Note that unlike the DictReader class, the fieldnames parameter of the DictWriter is not optional. Since Python?s dict objects are not ordered, there is not enough information available to deduce the order in which the row should be written to the csvfile." http://docs.python.org/dev/py3k/library/csv.html#csv.DictWriter Add a new csv.NamedTupleReader and csv.NamedTupleWriter that work similar to the DictReader and DictWriter then this snippet (in examples http://docs.python.org/dev/py3k/library/collections.html#collections.namedtuple ): EmployeeRecord = namedtuple('EmployeeRecord', 'name, age, title, department, paygrade') import csv for emp in map(EmployeeRecord._make, csv.reader(open("employees.csv", "rb"))): print(emp.name, emp.title) Could be rewritten as: import csv for emp in csv.NamedTupleReader("employees.csv"): print(emp.name, emp.title) Also, is there a way to convert a OrderedDict into a namedtuple? It is possible to go the other direction using namedtuple._asdict, but I don't see a _fromdict method. And thanks Steven and Raymond for the Counter class. Several people had started to see the common pattern and now we have it in the stdlib. From piet at cs.uu.nl Thu May 7 16:43:26 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Thu, 07 May 2009 22:43:26 +0200 Subject: SimpleXMLRPCServer and creating a new object on for each new client request. References: Message-ID: >>>>> Jelle Smet (JS) wrote: >JS> Hi list, >JS> My goals is to have concurrent and separated client sessions using xmlrpc. >JS> Initially my though was that SimpleXMLRPCServer was able to create a new >JS> object instance for each incoming request. >JS> But this doesn't appear to be the case, unless I'm overlooking something, >JS> if so please point me out. You create a single instance of your class and then register that. There is nothing in your code that makes the server believe it should create a new instance for each request. And then of course this instance always returns the same random number. It could generate a new random number for each call as follows: def show_random(self): return random.randrange(0,100000) but I guess this is not what you want. Otherwise you would have to register a function that creates a new instance on every call. But as Martin P. Hellwig has noted, it wouldn't give you sessions anyway. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From google at mrabarnett.plus.com Thu May 7 17:07:42 2009 From: google at mrabarnett.plus.com (MRAB) Date: Thu, 07 May 2009 22:07:42 +0100 Subject: Best practice for operations on streams of text In-Reply-To: <340d1511-8b2c-41bc-950d-3e25bdd433fb@j9g2000prh.googlegroups.com> References: <340d1511-8b2c-41bc-950d-3e25bdd433fb@j9g2000prh.googlegroups.com> Message-ID: <4A034D9E.7030106@mrabarnett.plus.com> James wrote: > Hello all, > I'm working on some NLP code - what I'm doing is passing a large > number of tokens through a number of filtering / processing steps. > > The filters take a token as input, and may or may not yield a token as > a result. For example, I might have filters which lowercases the > input, filter out boring words and filter out duplicates chained > together. > > I originally had code like this: > for t0 in token_stream: > for t1 in lowercase_token(t0): > for t2 in remove_boring(t1): > for t3 in remove_dupes(t2): > yield t3 > > Apart from being ugly as sin, I only get one token out as > StopIteration is raised before the whole token stream is consumed. > > Any suggestions on an elegant way to chain together a bunch of > generators, with processing steps in between? > What you should be doing is letting the filters accept an iterator and yield values on demand: def lowercase_token(stream): for t in stream: yield t.lower() def remove_boring(stream): for t in stream: if t not in boring: yield t def remove_dupes(stream): seen = set() for t in stream: if t not in seen: yield t seen.add(t) def compound_filter(token_stream): stream = lowercase_token(token_stream) stream = remove_boring(stream) stream = remove_dupes(stream) for t in stream(t): yield t From Kevin.Smith at sas.com Thu May 7 17:23:31 2009 From: Kevin.Smith at sas.com (Kevin D. Smith) Date: Thu, 7 May 2009 16:23:31 -0500 Subject: heapq.merge with key= Message-ID: I need the behavior of heapq.merge to merge a bunch of results from a database. I was doing this with sorted(itertools.chain(...), key=...), but I would prefer to do this with generators. My issue is that I need the key= argument to sort on the correct field in the database. heapq.merge doesn't have this argument and I don't understand the code enough to know if it's possible to add it. Is this enhancement possible without drastically changing the current code? -- Kevin D. Smith From danmcleran at yahoo.com Thu May 7 17:31:40 2009 From: danmcleran at yahoo.com (danmcleran at yahoo.com) Date: Thu, 7 May 2009 14:31:40 -0700 (PDT) Subject: Python 2.4.3 array memory leak Message-ID: <19f299bd-7a10-435e-b727-6f72bbf8ece9@l28g2000vba.googlegroups.com> I am using the array module to instantiate many arrays in my application. It seems like there is a memory leak in there somewhere. Can anyone confim this and let me know what, if anything, I can do about it? I am using Fedora Core 5 Linux: import commands import array import itertools import sys from itertools import repeat print '*** before ***' print commands.getoutput('cat /proc/meminfo').split('\n')[1] for i in range(100): a = array.array('I', repeat(0, int(2E6))) del a print '*** after ***' print commands.getoutput('cat /proc/meminfo').split('\n')[1] Output: *** before *** MemFree: 1459772 kB *** after *** MemFree: 1457688 kB From bearophileHUGS at lycos.com Thu May 7 17:33:07 2009 From: bearophileHUGS at lycos.com (bearophileHUGS at lycos.com) Date: Thu, 7 May 2009 14:33:07 -0700 (PDT) Subject: Python 3.1 beta 1 References: <7c96bd34-b199-4646-a790-4097adcc6999@q14g2000vbn.googlegroups.com> Message-ID: <854351d6-79ae-47b9-8757-22028443011c@x6g2000vbg.googlegroups.com> Terry Reedy: bearophile: > > Well, I'd like function call semantics pass-in keyword arguments to > > use OrderedDicts then... :-) [...] > It would require a sufficiently fast C implementation. Right. Such dict is usually small, so if people want it ordered, it may be better to just use an array of name-value structs instead of a dict or ordered dict. Iterating on a 10-pair array is fast on modern CPUs. Bye, bearophile From tjreedy at udel.edu Thu May 7 17:44:50 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 07 May 2009 17:44:50 -0400 Subject: Newcomer to Python tutorial question In-Reply-To: References: Message-ID: Alan Cameron wrote: > >>>> why is the printed result of >>>> >>>>>>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'} >>>>>>> print(basket) >>>> {'orange', 'banana', 'pear', 'apple'} >>>> >>>> in the sequence given? > It appears that I used a reserved term when I used 'sequence'. No and Sort-of. No: We often use it in the normal English sense of ordered items, as I and I think others assume you did. Your question is quite legitimate, and the answer, as indicated, is how an implementation interacts with the sequence of additions. Sort-of: The library manual section of Sequence Types lists the sequence operations common to all or most built-in Python sequence classes. But it does not explicitly define sequence. Ranges, which are iterables that directly support only indexing and len(), are called sequences. Dicts, which are iterables that support len() but are usually not indexed by integers, are not. So that suggests a minimal definition of sequence, but all the other sequence classes support much more that is typically assumed. Keywords are reserved terms in the language such as 'if' and 'None' that are specially recognized by the parser and which affect compilation. Identifiers of the form '__x...y__' are reserved names. Non-terminal terms in the grammar are reserved terms, in a sense, within the reference manual, but 'expression_list', not 'sequence', is used for comma-separated sequences of expressions in code. The comma-separated sequence of items in a function call is separately defined as an 'argument_list' because 'keyword_item's like 'a=b' and '*' and '**' are not expressions and because there are some order restrictions on argument items. Terry Jan Reedy From danmcleran at yahoo.com Thu May 7 17:47:01 2009 From: danmcleran at yahoo.com (danmcleran at yahoo.com) Date: Thu, 7 May 2009 14:47:01 -0700 (PDT) Subject: Python 2.4.3 array memory leak References: <19f299bd-7a10-435e-b727-6f72bbf8ece9@l28g2000vba.googlegroups.com> Message-ID: <3291c0b8-7e4b-40fe-8984-8562b5c49038@n4g2000vba.googlegroups.com> On May 7, 3:31 pm, "danmcle... at yahoo.com" wrote: > I am using the array module to instantiate many arrays in my > application. It seems like there is a memory leak in there somewhere. > Can anyone confim this and let me know what, if anything, I can do > about it? I am using Fedora Core 5 Linux: > > import commands > import array > import itertools > import sys > > from itertools import repeat > > print '*** before ***' > print commands.getoutput('cat /proc/meminfo').split('\n')[1] > for i in range(100): > a = array.array('I', repeat(0, int(2E6))) > del a > print '*** after ***' > print commands.getoutput('cat /proc/meminfo').split('\n')[1] > > Output: > *** before *** > MemFree: 1459772 kB > *** after *** > MemFree: 1457688 kB I hate to reply to my own thread but I wanted to update it. I tried the same code using ctypes arrays and see no memory leak: import commands import array import itertools import sys import ctypes from itertools import repeat from ctypes import * print '*** before ***' print commands.getoutput('cat /proc/meminfo').split('\n')[1] for i in range(100): a = ARRAY(c_uint, int(2E6)) del a print '*** after ***' print commands.getoutput('cat /proc/meminfo').split('\n')[1] *** before *** MemFree: 1416364 kB *** after *** MemFree: 1416364 kB From danmcleran at yahoo.com Thu May 7 17:58:26 2009 From: danmcleran at yahoo.com (danmcleran at yahoo.com) Date: Thu, 7 May 2009 14:58:26 -0700 (PDT) Subject: Python 2.4.3 array memory leak References: <19f299bd-7a10-435e-b727-6f72bbf8ece9@l28g2000vba.googlegroups.com> <3291c0b8-7e4b-40fe-8984-8562b5c49038@n4g2000vba.googlegroups.com> Message-ID: On May 7, 3:47 pm, "danmcle... at yahoo.com" wrote: > On May 7, 3:31 pm, "danmcle... at yahoo.com" > wrote: > > > > > I am using the array module to instantiate many arrays in my > > application. It seems like there is a memory leak in there somewhere. > > Can anyone confim this and let me know what, if anything, I can do > > about it? I am using Fedora Core 5 Linux: > > > import commands > > import array > > import itertools > > import sys > > > from itertools import repeat > > > print '*** before ***' > > print commands.getoutput('cat /proc/meminfo').split('\n')[1] > > for i in range(100): > > a = array.array('I', repeat(0, int(2E6))) > > del a > > print '*** after ***' > > print commands.getoutput('cat /proc/meminfo').split('\n')[1] > > > Output: > > *** before *** > > MemFree: 1459772 kB > > *** after *** > > MemFree: 1457688 kB > > I hate to reply to my own thread but I wanted to update it. I tried > the same code using ctypes arrays and see no memory leak: > > import commands > import array > import itertools > import sys > import ctypes > > from itertools import repeat > from ctypes import * > > print '*** before ***' > print commands.getoutput('cat /proc/meminfo').split('\n')[1] > for i in range(100): > a = ARRAY(c_uint, int(2E6)) > del a > print '*** after ***' > print commands.getoutput('cat /proc/meminfo').split('\n')[1] > > *** before *** > MemFree: 1416364 kB > *** after *** > MemFree: 1416364 kB The above code was not correct. I actually do see a memory leak when using ctypes arrays also. What is going on? import commands import array import itertools import sys import ctypes from itertools import repeat from ctypes import * print '*** before ***' print commands.getoutput('cat /proc/meminfo').split('\n')[1] for i in range(100): a = ARRAY(c_uint, int(2E6))() del a print '*** after ***' print commands.getoutput('cat /proc/meminfo').split('\n')[1] Output *** before *** MemFree: 1564096 kB *** after *** MemFree: 1556884 kB From norseman at hughes.net Thu May 7 17:58:29 2009 From: norseman at hughes.net (norseman) Date: Thu, 07 May 2009 14:58:29 -0700 Subject: subprocess.Popen howto? In-Reply-To: References: Message-ID: <4A035985.1080806@hughes.net> ?ystein Johansen (OJOHANS) wrote: > Hi, > > I have problems understanding the subprocess.Popen object. I have a > iterative calculation in a process running and I want to pipe the output > (stdout) from this calculation to a Python script. > > Let me include a simple code that simulates the calculating process: > /* This code simulates a big iterative calculation */ > #include > #include > > int main() > { > float val[2] = { M_PI, M_E }; > int i; > > for ( i = 0; i < 2 i++) { > sleep( 15 ); /* It's a hard calculation. It take 15 seconds */ > printf("Value: %5.6f\n", val[i] ); > fflush( stdout ); > } > return 0; > } > > let's compile this to mycalc: gcc -o mycalc calc.c ... (untested code) > > In C I have this code which starts the mycalc process and handles the > output from it: > > #include > #include > #define BUF_SIZE 256 > > int main() > { > FILE *pip; > char line[BUF_SIZE]; > > pip = popen("mycalc", "r"); > assert( pip != NULL ); > > while ( fgets( line, BUF_SIZE, pip )) { > printf( "Hello; I got: %s \n", line ); > fflush( stdout ); > } > pclose( pip ); > return 0; > } > How can I make such while-loop in Python? I assume I should use > subprocess.Popen(), but I can't figure out how? > > -?ystein > > > > ------------------------------------------------------------------- > The information contained in this message may be CONFIDENTIAL and is > intended for the addressee only. Any unauthorised use, dissemination of the > information or copying of this message is prohibited. If you are not the > addressee, please notify the sender immediately by return e-mail and delete > this message. > Thank you. > > > ------------------------------------------------------------------------ > > -- > http://mail.python.org/mailman/listinfo/python-list ================================ If you don't like a lot of typing that obscures the process, take a look at os.Popen2 Pg.39 or so in Lib.pdf for 2.5.2 In this case - the popen3 is probably your best bet. I took a test run on "subprocess" a few months ago. My review: excessive typing to accomplish the simple. BlackBox stuff is supposed to be short and sweet. BlackBox was the term applied to drivers and couplers back when. Back when: one started by writing a subroutine that got used. next one got to write a program next one got to write an application then a BlackBox from there one could graduate to OS That was back when! to repeat from another of my postings: --- processPython 2.5.2 (r252:60911, Mar 4 2008, 10:40:55) [GCC 3.3.6] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import os >>> AbWd = os.spawnlp( os.P_WAIT,"abiword","abiword","") The P_WAIT stops python until the program (abiword in this case) completes. The "" at the end are for tokens to be given to the program and yes - contrary to manual, the program MUST be there TWICE (complete with any path needed). for windows this works: (for cut and paste from cmd.exe, see: Re: Copy & Paste in a Dos box from norseman 05/06/2009 4:28PM ) Python 2.5.1 ... on win32 >>> import os >>> result = os.spawnl( os.P_WAIT, "d:\\winmcad\\mcad","") Runs the program mcad. Returns to python when mcad exits. --- In your case: substitute ..."your_compiled_c_program", " >yourPy.py") at the obvious places. In case this isn't clear; method 1: py1.py starts compiled.c which redirects to yourPy.py method 2: py1.py uses os.popen3(compiled.c...) & the two work it out. In either case the receiver at the end stays open until completion so sleep() things are not needed. (may need to test for EOL and EOT) HTH Steve norseman at hughes.net From danmcleran at yahoo.com Thu May 7 18:12:32 2009 From: danmcleran at yahoo.com (danmcleran at yahoo.com) Date: Thu, 7 May 2009 15:12:32 -0700 (PDT) Subject: Python 2.4.3 array memory leak References: <19f299bd-7a10-435e-b727-6f72bbf8ece9@l28g2000vba.googlegroups.com> <3291c0b8-7e4b-40fe-8984-8562b5c49038@n4g2000vba.googlegroups.com> Message-ID: <404a2491-5f31-4268-9d03-4f59e7eabd51@p4g2000vba.googlegroups.com> On May 7, 3:58 pm, "danmcle... at yahoo.com" wrote: > On May 7, 3:47 pm, "danmcle... at yahoo.com" > wrote: > > > > > On May 7, 3:31 pm, "danmcle... at yahoo.com" > > wrote: > > > > I am using the array module to instantiate many arrays in my > > > application. It seems like there is a memory leak in there somewhere. > > > Can anyone confim this and let me know what, if anything, I can do > > > about it? I am using Fedora Core 5 Linux: > > > > import commands > > > import array > > > import itertools > > > import sys > > > > from itertools import repeat > > > > print '*** before ***' > > > print commands.getoutput('cat /proc/meminfo').split('\n')[1] > > > for i in range(100): > > > a = array.array('I', repeat(0, int(2E6))) > > > del a > > > print '*** after ***' > > > print commands.getoutput('cat /proc/meminfo').split('\n')[1] > > > > Output: > > > *** before *** > > > MemFree: 1459772 kB > > > *** after *** > > > MemFree: 1457688 kB > > > I hate to reply to my own thread but I wanted to update it. I tried > > the same code using ctypes arrays and see no memory leak: > > > import commands > > import array > > import itertools > > import sys > > import ctypes > > > from itertools import repeat > > from ctypes import * > > > print '*** before ***' > > print commands.getoutput('cat /proc/meminfo').split('\n')[1] > > for i in range(100): > > a = ARRAY(c_uint, int(2E6)) > > del a > > print '*** after ***' > > print commands.getoutput('cat /proc/meminfo').split('\n')[1] > > > *** before *** > > MemFree: 1416364 kB > > *** after *** > > MemFree: 1416364 kB > > The above code was not correct. I actually do see a memory leak when > using ctypes arrays also. What is going on? > > import commands > import array > import itertools > import sys > import ctypes > > from itertools import repeat > from ctypes import * > > print '*** before ***' > print commands.getoutput('cat /proc/meminfo').split('\n')[1] > for i in range(100): > a = ARRAY(c_uint, int(2E6))() > del a > print '*** after ***' > print commands.getoutput('cat /proc/meminfo').split('\n')[1] > > Output > *** before *** > MemFree: 1564096 kB > *** after *** > MemFree: 1556884 kB Maybe the problem is with the way I was looking at free memory. I changed my code to look at virtual memory allocated to the process in question and I do not see any appreciable before/after difference. import commands import array import itertools import sys import os from itertools import repeat deltas = [] print '*** before ***' s = commands.getoutput('cat /proc/%d/status' % os.getpid()).split('\n') [12] print s for i in range(100): a = array.array('I', repeat(0, int(2E6))) del a print '*** after ***' s = commands.getoutput('cat /proc/%d/status' % os.getpid()).split('\n') [12] print s Output: *** before *** VmSize: 5952 kB *** after *** VmSize: 5956 kB From tjreedy at udel.edu Thu May 7 18:32:25 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 07 May 2009 18:32:25 -0400 Subject: Best practice for operations on streams of text In-Reply-To: <4A034D9E.7030106@mrabarnett.plus.com> References: <340d1511-8b2c-41bc-950d-3e25bdd433fb@j9g2000prh.googlegroups.com> <4A034D9E.7030106@mrabarnett.plus.com> Message-ID: MRAB wrote: > James wrote: >> Hello all, >> I'm working on some NLP code - what I'm doing is passing a large >> number of tokens through a number of filtering / processing steps. >> >> The filters take a token as input, and may or may not yield a token as >> a result. For example, I might have filters which lowercases the >> input, filter out boring words and filter out duplicates chained >> together. >> >> I originally had code like this: >> for t0 in token_stream: >> for t1 in lowercase_token(t0): >> for t2 in remove_boring(t1): >> for t3 in remove_dupes(t2): >> yield t3 For that to work at all, the three functions would have to turn each token into an iterable of 0 or 1 tokens. Hence the inner 'loops' would execute 0 or 1 times. Better to return a token or None, and replace the three inner 'loops' with three conditional statements (ugly too) or less efficiently (due to lack of short circuiting), t = remove_dupes(remove_boring(lowercase_token(t0))) if t is not None: yield t >> Apart from being ugly as sin, I only get one token out as >> StopIteration is raised before the whole token stream is consumed. That puzzles me. Your actual code must be slightly different from the above and what I imagine the functions to be. But nevermind, because >> Any suggestions on an elegant way to chain together a bunch of >> generators, with processing steps in between? MRAB's suggestion is the way to go. Your automatically get short-circuiting because each generator only gets what is passed on. And resuming a generator is much faster that re-calling a function. > What you should be doing is letting the filters accept an iterator and > yield values on demand: > > def lowercase_token(stream): > for t in stream: > yield t.lower() > > def remove_boring(stream): > for t in stream: > if t not in boring: > yield t > > def remove_dupes(stream): > seen = set() > for t in stream: > if t not in seen: > yield t > seen.add(t) > > def compound_filter(token_stream): > stream = lowercase_token(token_stream) > stream = remove_boring(stream) > stream = remove_dupes(stream) > for t in stream(t): > yield t I also recommend the Beazly reference Herron gave. tjr From benjamin at python.org Thu May 7 18:33:49 2009 From: benjamin at python.org (Benjamin Peterson) Date: Thu, 7 May 2009 22:33:49 +0000 (UTC) Subject: Python 3.1 beta 1 References: Message-ID: latinmail.com> writes: > Congratulations! Thanks! > > Is it just me or was some nice summary output added to the make > process? I get a nice list of modules that didn't compile and the ones > where the library could not be found. Are you compiling on a different platform? The nice output has been around for a while, bu only on non-Windows platforms. From benjamin at python.org Thu May 7 18:37:46 2009 From: benjamin at python.org (Benjamin Peterson) Date: Thu, 7 May 2009 22:37:46 +0000 (UTC) Subject: Python 3.1 beta 1 References: <7c96bd34-b199-4646-a790-4097adcc6999@q14g2000vbn.googlegroups.com> Message-ID: lycos.com> writes: > > collections.Counter and collections.OrderedDict: very nice and useful. > Is the order inside OrderedDict kept with a double linked list of the > items? There's a doubly-linked list containing the values. Another dictionary maps keys to the list. From tjreedy at udel.edu Thu May 7 18:41:10 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 07 May 2009 18:41:10 -0400 Subject: Python 2.4.3 array memory leak In-Reply-To: <19f299bd-7a10-435e-b727-6f72bbf8ece9@l28g2000vba.googlegroups.com> References: <19f299bd-7a10-435e-b727-6f72bbf8ece9@l28g2000vba.googlegroups.com> Message-ID: danmcleran at yahoo.com wrote: > I am using the array module to instantiate many arrays in my > application. It seems like there is a memory leak in there somewhere. > Can anyone confim this and let me know what, if anything, I can do > about it? I am using Fedora Core 5 Linux: > > import commands > import array > import itertools > import sys > > from itertools import repeat > > print '*** before ***' > print commands.getoutput('cat /proc/meminfo').split('\n')[1] > for i in range(100): > a = array.array('I', repeat(0, int(2E6))) > del a > print '*** after ***' > print commands.getoutput('cat /proc/meminfo').split('\n')[1] > > Output: > *** before *** > MemFree: 1459772 kB > *** after *** > MemFree: 1457688 kB What happens if you remove the loop? I would not be surprised if Python grabs the memory once, reuses it, and does not let go. That is not a leak. What happens if you put the after inside the loop? Does mem usage steadily increase, and continue if you increase range to 1000, 10000? That would be a leak. If there actually is a problem, try a later version of Python. From danmcleran at yahoo.com Thu May 7 18:48:51 2009 From: danmcleran at yahoo.com (danmcleran at yahoo.com) Date: Thu, 7 May 2009 15:48:51 -0700 (PDT) Subject: Python 2.4.3 array memory leak References: <19f299bd-7a10-435e-b727-6f72bbf8ece9@l28g2000vba.googlegroups.com> Message-ID: <8a515208-f90c-4a1f-a467-0271e623ed77@t11g2000vbc.googlegroups.com> On May 7, 4:41?pm, Terry Reedy wrote: > danmcle... at yahoo.com wrote: > > I am using the array module to instantiate many arrays in my > > application. It seems like there is a memory leak in there somewhere. > > Can anyone confim this and let me know what, if anything, I can do > > about it? I am using Fedora Core 5 Linux: > > > import commands > > import array > > import itertools > > import sys > > > from itertools import repeat > > > print '*** before ***' > > print commands.getoutput('cat /proc/meminfo').split('\n')[1] > > for i in range(100): > > ? ? a = array.array('I', repeat(0, int(2E6))) > > ? ? del a > > print '*** after ***' > > print commands.getoutput('cat /proc/meminfo').split('\n')[1] > > > Output: > > *** before *** > > MemFree: ? ? ? 1459772 kB > > *** after *** > > MemFree: ? ? ? 1457688 kB > > What happens if you remove the loop? ?I would not be surprised if Python > grabs the memory once, reuses it, and does not let go. ?That is not a > leak. ?What happens if you put the after inside the loop? ?Does mem > usage steadily increase, and continue if you increase range to 1000, > 10000? ?That would be a leak. > > If there actually is a problem, try a later version of Python. I'm not convinced there is a problem anymore. In my latest code, I record the virtual mem allocated to my specific process and I do not see it increasing. I would think that if I was leaking memory, I should see a steady increase in virtual memory consumed by my process, which I do not. thanks From clp2 at rebertia.com Thu May 7 18:54:39 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Thu, 7 May 2009 15:54:39 -0700 Subject: subprocess.Popen howto? In-Reply-To: References: Message-ID: <50697b2c0905071554pa58d997k405f00662cbb03d1@mail.gmail.com> 2009/5/7 ?ystein Johansen (OJOHANS) : > Hi, > > I have problems understanding the subprocess.Popen object. I have a > iterative calculation in a process running and I want to pipe the output > (stdout)?from this calculation to a Python script. > > Let me include a simple code that simulates the calculating process: > /* This code simulates a big iterative calculation */ > #include > #include > > int main() > { > ?float val[2] = { M_PI, M_E }; > ?int i; > > ?for ( i = 0; i < 2 i++) { > ??sleep( 15 );?? /* It's a hard calculation. It take 15 seconds */ > ??printf("Value: %5.6f\n", val[i] ); > ??fflush( stdout ); > ?} > ?return 0; > } > > let's compile this to mycalc: gcc -o mycalc calc.c ... (untested code) > > In C I have this code?which starts the mycalc process and handles the output > from it: > > #include > #include > #define BUF_SIZE 256 > > int main() > { > ?FILE *pip; > ?char line[BUF_SIZE]; > > ?pip = popen("mycalc", "r"); > ?assert( pip != NULL ); > > ?while ( fgets( line, BUF_SIZE, pip )) { > ??printf( "Hello; I got: %s \n",?line ); > ??fflush( stdout ); > ?} > ?pclose( pip ); > ?return 0; > } > How can I make such while-loop?in Python? I assume I should use > subprocess.Popen(), but I can't figure out how? import subprocess subprocess.call(["./mycalc"]) # add `bufsize=-1` as argument to have output be buffered #process inherits our filehandles and writes directly to them #.call() only returns once the process has exited Cheers, Chris -- http://blog.rebertia.com From robert.kern at gmail.com Thu May 7 18:57:04 2009 From: robert.kern at gmail.com (Robert Kern) Date: Thu, 07 May 2009 17:57:04 -0500 Subject: Python 2.4.3 array memory leak In-Reply-To: <19f299bd-7a10-435e-b727-6f72bbf8ece9@l28g2000vba.googlegroups.com> References: <19f299bd-7a10-435e-b727-6f72bbf8ece9@l28g2000vba.googlegroups.com> Message-ID: On 2009-05-07 16:31, danmcleran at yahoo.com wrote: > I am using the array module to instantiate many arrays in my > application. It seems like there is a memory leak in there somewhere. > Can anyone confim this and let me know what, if anything, I can do > about it? I am using Fedora Core 5 Linux: > > import commands > import array > import itertools > import sys > > from itertools import repeat > > print '*** before ***' > print commands.getoutput('cat /proc/meminfo').split('\n')[1] > for i in range(100): > a = array.array('I', repeat(0, int(2E6))) > del a > print '*** after ***' > print commands.getoutput('cat /proc/meminfo').split('\n')[1] > > Output: > *** before *** > MemFree: 1459772 kB > *** after *** > MemFree: 1457688 kB This is not necessarily indicative of a memory leak, per se. Python sometimes does not release memory back to the OS. The memory isn't leaked; it will be reused by Python for later allocations. Now, if you saw Python's memory usage *grow* with increasing iterations, then that would be evidence of a leak. Try measuring the "before" memory, then do one iteration, measure the memory again (let's call it "intermediate"), then do your loop, and then measure the "after" memory. The "intermediate" measurement should be about your "after" measurement. Also, try to measure the process's own memory usage, not the system's free memory. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco From piet at cs.uu.nl Thu May 7 19:17:08 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 08 May 2009 01:17:08 +0200 Subject: SimpleXMLRPCServer and creating a new object on for each new client request. References: Message-ID: >>>>> Jelle Smet (JS) wrote: One more thing: >JS> I start python interactively: >>>>> import xmlrpclib >>>>> session1=xmlrpclib.ServerProxy('http://localhost:8000') >>>>> session2=xmlrpclib.ServerProxy('http://localhost:8000') >>>>> print session1.show_random() >JS> 13930 >>>>> print session2.show_random() >JS> 13930 >>>>> I get the impression, also from your use of the variable names 'session1' and 'session2' that xmlrpclib.ServerProxy() gives you some kind of connection to the XMLRPC server. This is not the case. It gives just an administration object *in the client* that will communicate with the server when you call a method on it. The two session's in your code are therefore functionally equivalent and there is no advantage in having two of them instead of one. And the name session is misleading. Please note also that XMLRPC isn't object-oriented. There is just the server; in the protocol there are no objects other than the server. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From pavlovevidence at gmail.com Thu May 7 19:35:32 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Thu, 7 May 2009 16:35:32 -0700 (PDT) Subject: subprocess.Popen howto? References: Message-ID: <3aaa4e6e-c2c0-43e0-a483-7c79c46137a9@s16g2000vbp.googlegroups.com> On May 7, 2:58?pm, norseman wrote: > If you don't like a lot of typing that obscures the process, > take a look at os.Popen2 ?Pg.39 or so in Lib.pdf for 2.5.2 > In this case - the popen3 is probably your best bet. > > I took a test run on "subprocess" a few months ago. My review: > excessive typing to accomplish the simple. Hmm, I won't argue that it can be excessive typing, but I think its purpose isn't to accomplish the simple but to hit all the corners of subprocesses. What if you want os.system without the shell? What if you want popen with the shell? On Windows what if I want a subprocess without a console from a Python program in a console. Stuff like that. I still use os.system for Q&D stuff, but now use subprocess for everything else since I prefer thorough to short and sweet. Carl Banks From rajanikanth at gmail.com Thu May 7 19:51:14 2009 From: rajanikanth at gmail.com (Rajanikanth Jammalamadaka) Date: Thu, 7 May 2009 16:51:14 -0700 Subject: regular expression for getting content between parentheses Message-ID: <84bdef3c0905071651p60232adagd98aa1138f91d4f9@mail.gmail.com> Hi I have a text file as follows: testName = ( someParam = value1 anotherParam = (value2, value3) ) how do I write a regular expression to get all the contents of the file which are between the first and last parentheses. In this case, I want: someParam = value1 anotherParam = (value2, value3) Thanks, Raj From cripplemeal at gmail.com Thu May 7 19:52:54 2009 From: cripplemeal at gmail.com (cripplemeal at gmail.com) Date: Thu, 7 May 2009 16:52:54 -0700 (PDT) Subject: Pygame Message-ID: I am following this tutorial on python, and it has instructed me to download pygame. I am not sure it works with the new version of python I am using, as the last version it states on the tutorial is 2.6. It is telling me the module does not exist after I download, and I even tried placing it in the python folder, but it still came up with a problem. Any way to fix it, or do I try something else? From clp2 at rebertia.com Thu May 7 20:01:01 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Thu, 7 May 2009 17:01:01 -0700 Subject: Pygame In-Reply-To: References: Message-ID: <50697b2c0905071701k5ad5480u332d5f7e211e71e7@mail.gmail.com> On Thu, May 7, 2009 at 4:52 PM, wrote: > I am following this tutorial on python, and it has instructed me to > download pygame. I am not sure it works with the new version of python > I am using, as the last version it states on the tutorial is 2.6. > > It is telling me the module does not exist after I download, and I > even tried placing it in the python folder, but it still came up with > a problem. Please give the actual full text of the error message (and traceback if possible). Please explain precisely which folder you mean by "the python folder". Cheers, Chris -- http://blog.rebertia.com From mensanator at aol.com Thu May 7 20:04:05 2009 From: mensanator at aol.com (Mensanator) Date: Thu, 7 May 2009 17:04:05 -0700 (PDT) Subject: Pygame References: Message-ID: On May 7, 6:52?pm, cripplem... at gmail.com wrote: > I am following this tutorial on python, and it has instructed me to > download pygame. I am not sure it works with the new version of python > I am using, as the last version it states on the tutorial is 2.6. If the module says 2.6, then that's the version you must use. There's not much point in using 3.x until the 3rd party modules catch up. Unless said modules are not important to you. And even that's no guarantee. The sympy module writers just assumed their code would work in 2.6 without testing it. Turns out they were seriously wrong and had to scramble to make a bug fix. > > It is telling me the module does not exist after I download, and I > even tried placing it in the python folder, but it still came up with > a problem. > > Any way to fix it, or do I try something else? From emile at fenx.com Thu May 7 20:14:59 2009 From: emile at fenx.com (Emile van Sebille) Date: Thu, 07 May 2009 17:14:59 -0700 Subject: regular expression for getting content between parentheses In-Reply-To: <84bdef3c0905071651p60232adagd98aa1138f91d4f9@mail.gmail.com> References: <84bdef3c0905071651p60232adagd98aa1138f91d4f9@mail.gmail.com> Message-ID: On 5/7/2009 4:51 PM Rajanikanth Jammalamadaka said... > Hi > > I have a text file as follows: > > testName = ( > someParam = value1 > anotherParam = (value2, value3) > ) > > how do I write a regular expression to get all the contents of the > file which are between the first and last parentheses. > > In this case, I want: > > someParam = value1 > anotherParam = (value2, value3) > > Thanks, It's not a regex nor probably what you want, but I'd start with... >>> filetext = '''testName = ( ... someParam = value1 ... anotherParam = (value2, value3) ... )''' >>> >>> print filetext.split("(",1)[-1].rsplit(")",1)[0] someParam = value1 anotherParam = (value2, value3) >>> Emile From rhodri at wildebst.demon.co.uk Thu May 7 20:19:21 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Fri, 08 May 2009 01:19:21 +0100 Subject: regular expression for getting content between parentheses In-Reply-To: <84bdef3c0905071651p60232adagd98aa1138f91d4f9@mail.gmail.com> References: <84bdef3c0905071651p60232adagd98aa1138f91d4f9@mail.gmail.com> Message-ID: On Fri, 08 May 2009 00:51:14 +0100, Rajanikanth Jammalamadaka wrote: > Hi > > I have a text file as follows: > > testName = ( > someParam = value1 > anotherParam = (value2, value3) > ) > > how do I write a regular expression to get all the contents of the > file which are between the first and last parentheses. You don't, or at least you don't without some cast-iron guarantees that your file will look *exactly* like this. If you can guarantee that, then this should work: import re f = open(filename) data = f.read() m = re.match(r"""[^(]*\( # Find the first open parenthesis (.*) # Gobble up everything... \)[^)]*$ # ...to the last close paren""", data, re.X) if m: print m.group(1) Notice that this will do *exactly* what you asked; pick up everything between the first and the last parentheses. In particular, if your text looks like this: testName1 = ( someParam1 = value1 anotherParam1 = (value2, value3) ) testName2 = ( sameParam2 = value4 ) ...then what you'll get out is: someParam1 = value1 anotherParam1 = (value2, value3) ) testName2 = ( sameParam2 = value4 You can't get around that with regular expressions, you'll have to parse your way through the input string counting open and close parentheses as you go. -- Rhodri James *-* Wildebeeste Herder to the Masses From m.irfan.n84 at gmail.com Thu May 7 20:21:20 2009 From: m.irfan.n84 at gmail.com (Irfan) Date: Thu, 7 May 2009 17:21:20 -0700 (PDT) Subject: Computer,Software and Laptops Message-ID: Get more information related about computers,laptops and software visit (http://in-computersandlaptops.blogspot.com) From ross.jett at gmail.com Thu May 7 20:32:22 2009 From: ross.jett at gmail.com (Ross) Date: Thu, 7 May 2009 17:32:22 -0700 (PDT) Subject: Code works fine except... References: <8d4ec1df-dddb-469a-99a1-695152db7c04@n4g2000vba.googlegroups.com> <8b49b775-66a7-42d5-9b00-cf23d9b044cc@e23g2000vbe.googlegroups.com> <54d76f11-c255-487d-9c3a-aaa380d7827a@z7g2000vbh.googlegroups.com> <991ccae3-f3e2-4d73-bd32-bc6f560e6f5f@n8g2000vbb.googlegroups.com> <1d8802ff-8211-467f-95c1-4616e3bd7300@s20g2000vbp.googlegroups.com> Message-ID: On May 7, 1:11?am, John Yeung wrote: > On May 7, 12:30?am, Ross wrote: > > > > > If I were to set up a dictionary that counted players used in the bye > > list and only allowed players to be added to the bye list if they were > > within 2 of the least used player, would this be a good approach for > > managing bye selection or would using a dictionary in this manner be > > unnecessary/redundant? > > I certainly have not proved it, but I think you don't need to resort > to anything fancy if you are OK with the maximum byes being within two > of the minimum byes. ?(Maybe this needs to be larger with larger > numbers of players.) ?Try using your original shuffle but instead of > selecting matches to "throw away" each week, just use the matches you > need (to fill up the courts) and pick up where you left off the next > week. ?For example, with 10 players, each "round" ideally consists of > five matches. ?If you only have four courts, don't throw away the > fifth match; save it as the first match next week. > > To be honest, I didn't look too carefully at your original shuffle. > It may be good enough. ?It's a little different than the "standard" > rotation as presented on Wikipedia: > > ?http://en.wikipedia.org/wiki/Round-robin_tournament#Scheduling_algorithm > > The one on Wikipedia happened to pass my casual tests with no more > than a 2-bye difference between the most-played and least-played > players, and didn't run into the problem of scheduling the same player > for two matches the same week. ?But I have to stress I only tried a > few starting values, which all worked; I didn't try to break it, or > run an extensive battery of tests. > > John John, I really appreciate your help with this problem. Thanks to your suggestions, I've managed to solve the problem. Here's what I did: I used my original round_robin generator to generate each week. I then took every week and chained them all together end to end. Then, depending on how many courts are available, I can select that many tuples at a time from the list. If you go in order, the discrepancy between the player with the least amount of byes and the greatest amount of byes is only 1. If you can make it exactly all the way through a cycle, there will be no discrepancy. Anyways, I've done all this by hand and it works so now I'm going to go ahead and code it up. Again, thanks for your help. -Ross From davea at ieee.org Thu May 7 20:32:48 2009 From: davea at ieee.org (Dave Angel) Date: Thu, 07 May 2009 20:32:48 -0400 Subject: Pygame In-Reply-To: References: Message-ID: <4A037DB0.9070901@ieee.org> cripplemeal at gmail.com wrote: > I am following this tutorial on python, and it has instructed me to > download pygame. I am not sure it works with the new version of python > I am using, and what version would that be? What release, and what platform? print sys.version and include the output in your next message. > as the last version it states on the tutorial is 2.6. > > It is telling me the module what module? Copy & paste the error message into your query, and you're likely to get useful response. Also copy & paste the command you're issuing to cause this error message. > does not exist after I download, Then your download manager is complaining about a missing module? > and I > even tried placing it in the python folder, which "python folder". Be specific. Do you mean the folder that contains Python.exe? And just what are you "placing" there? > but it still came up with > a problem. > A new problem? Be specific. > Any way to fix it, or do I try something else? > > Wild guess - maybe it's complaining about a missing SDL module. PyGame is dependent on SDL, as far as I can tell. Also, see http://www.pygame.org/download.shtml If you're on Windows, PyGame has different versions for different versions of Python. This page recommends Python 2.5.4. And there isn't a version for Python 3.0 From aahz at pythoncraft.com Thu May 7 20:55:03 2009 From: aahz at pythoncraft.com (Aahz) Date: 7 May 2009 17:55:03 -0700 Subject: IIR filter conversion routines for Python? References: Message-ID: In article , wzab wrote: > >I'm looking for procedures converting the IIR filters into cascade and/ >or parallel forms. Something like dir2cas.m or dir2par.m known in the >Matlab/Octave world. >Unfortunately SciPy does not contain such functions. >If they are not available, I would be grateful for any hints helping >me to implement them myself. Even though they're not in SciPy, the mailing lists for NumPy/SciPY are probably good places to get advice. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "It is easier to optimize correct code than to correct optimized code." --Bill Harlan From gnewsg at gmail.com Thu May 7 21:12:44 2009 From: gnewsg at gmail.com (Giampaolo Rodola') Date: Thu, 7 May 2009 18:12:44 -0700 (PDT) Subject: Call a function when a thread exits Message-ID: <5664df89-2966-45e4-a959-f4fc85c2e50f@g20g2000vba.googlegroups.com> Hi, I'm searching for a smooth way to call a certain function when a thread has finished its job. I guess I can keep calling isAlive() in a loop and call my function when it returns False but it's not very elegant. Actually I'm a bit surprised it doesn't exists an "atexit" function. Something like: import threading, time def myfun(): time.sleep(1) print "hello" def cleanup(): print "thread finished, starting cleanup operations..." t = threading.Thread(target=myfun) t.atexit(target=cleanup) t.start() Is there a reason why there's no such thing in the threading module? --- Giampaolo http://code.google.com/p/pyftpdlib/ From rajanikanth at gmail.com Thu May 7 21:25:39 2009 From: rajanikanth at gmail.com (Rajanikanth Jammalamadaka) Date: Thu, 7 May 2009 18:25:39 -0700 Subject: regular expression for getting content between parentheses In-Reply-To: References: <84bdef3c0905071651p60232adagd98aa1138f91d4f9@mail.gmail.com> Message-ID: <84bdef3c0905071825r523ed786g4a5d03e893cc924a@mail.gmail.com> Thanks for your replies. I changed the file to look like this: { testName : {"someParam": "value1", "anotherParam": (value2, value3)}, } to make it look like a hash to Python. Thanks, Raj On Thu, May 7, 2009 at 5:19 PM, Rhodri James wrote: > On Fri, 08 May 2009 00:51:14 +0100, Rajanikanth Jammalamadaka > wrote: > >> Hi >> >> I have a text file as follows: >> >> testName = ( >> ?someParam = value1 >> ?anotherParam = (value2, value3) >> ) >> >> how do I write a regular expression to get all the contents of the >> file which are between the first and last parentheses. > > You don't, or at least you don't without some cast-iron guarantees > that your file will look *exactly* like this. ?If you can guarantee > that, then this should work: > > import re > > f = open(filename) > data = f.read() > m = re.match(r"""[^(]*\( ? ? # Find the first open parenthesis > ? ? ? ? ? ? ? ? (.*) ? ? ? ?# Gobble up everything... > ? ? ? ? ? ? ? ? \)[^)]*$ ? ?# ...to the last close paren""", > ? ? ? ? ? ? data, re.X) > if m: > ?print m.group(1) > > > Notice that this will do *exactly* what you asked; pick up > everything between the first and the last parentheses. ?In > particular, if your text looks like this: > > testName1 = ( > ?someParam1 = value1 > ?anotherParam1 = (value2, value3) > ) > testName2 = ( > ?sameParam2 = value4 > ) > > ...then what you'll get out is: > > ?someParam1 = value1 > ?anotherParam1 = (value2, value3) > ) > testName2 = ( > ?sameParam2 = value4 > > > You can't get around that with regular expressions, you'll > have to parse your way through the input string counting > open and close parentheses as you go. > > -- > Rhodri James *-* Wildebeeste Herder to the Masses > -- > http://mail.python.org/mailman/listinfo/python-list > -- Rajanikanth From pavlovevidence at gmail.com Thu May 7 21:33:38 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Thu, 7 May 2009 18:33:38 -0700 (PDT) Subject: Call a function when a thread exits References: <5664df89-2966-45e4-a959-f4fc85c2e50f@g20g2000vba.googlegroups.com> Message-ID: On May 7, 6:12?pm, "Giampaolo Rodola'" wrote: > Hi, > I'm searching for a smooth way to call a certain function when a > thread has finished its job. > I guess I can keep calling isAlive() in a loop and call my function > when it returns False but it's not very elegant. > Actually I'm a bit surprised it doesn't exists an "atexit" function. > Something like: > > import threading, time > > def myfun(): > ? ? time.sleep(1) > ? ? print "hello" > > def cleanup(): > ? ? print "thread finished, starting cleanup operations..." > > t = threading.Thread(target=myfun) > t.atexit(target=cleanup) > t.start() > > Is there a reason why there's no such thing in the threading module? You can define your target function to clean up on exit. Using your definitions of myfun and cleanup, def mycleanfun(): try: myfun() finally: cleanup() t = threading.Thread(target=mycleanfun) t.start() Carl Banks From baseelinger at yahoo.com Thu May 7 21:52:01 2009 From: baseelinger at yahoo.com (baseelinger at yahoo.com) Date: Thu, 7 May 2009 18:52:01 -0700 (PDT) Subject: Web Based Front End? Message-ID: <18804639-32ae-4bdf-9ecc-4c8b21f8a43f@g19g2000vbi.googlegroups.com> I have been using Python for several years now and although my main occupation is not directly related to writing code I have been able to use Python to accomplish writing complex configuration files and analyzing data against standards, etc. I now have a requirement to provide a web based interface rather than say using the IDLE GUI for several reasons, (allowing others to use a server based Python script and to provide a slicker user interface via the users web browser). As I wade into very new territory I see articles on PHP, mod_python, Python Server Pages, CGI, Zope, Python in PHP, CherryPy, Pylons, etc., etc. It's all pretty confusing and while one method may work to accomplish the task at hand someone with far greater experience may look at the situation and say, "um, yeah, it will work that way but it would have been a whole lot easier if you had done it this way...". So, my question is what is the best method to be able to have a user enter specific data on a web page, have that data be passed to the Python script and then have the output returned to the web page? Essentially, I want to use a web based front end to accomplish the same thing that the IDLE GUI does, (asks me a few questions, I answer them, it builds a configuration file and prints it to the screen). A basic example can be found at http://webpython.codepoint.net/wsgi_request_parsing_post using httpd in Python but its seems very basic. Can anyone point me in the right direction? Thanks in advance for any assistance you may be able to provide. From gallium.arsenide at gmail.com Thu May 7 22:00:20 2009 From: gallium.arsenide at gmail.com (John Yeung) Date: Thu, 7 May 2009 19:00:20 -0700 (PDT) Subject: Code works fine except... References: <8d4ec1df-dddb-469a-99a1-695152db7c04@n4g2000vba.googlegroups.com> <54d76f11-c255-487d-9c3a-aaa380d7827a@z7g2000vbh.googlegroups.com> <991ccae3-f3e2-4d73-bd32-bc6f560e6f5f@n8g2000vbb.googlegroups.com> <1d8802ff-8211-467f-95c1-4616e3bd7300@s20g2000vbp.googlegroups.com> Message-ID: <0226fb35-a21e-4ba8-bafd-71af8abbd459@p4g2000vba.googlegroups.com> On May 7, 8:32?pm, Ross wrote: > I've managed to solve the problem. If you go in > order, the discrepancy between the player with the > least amount of byes and the greatest amount of byes > is only 1. I don't mean to rain on your parade, but that's not the case for all values. For example, with 7 players and only 1 or 2 courts, there will be weeks in the middle where the bye-difference is 2. This behavior is present in both your shuffle and the standard one in Wikipedia. Ultimately, as it pertains to your physical tennis scheduling with actual courts and actual players, you may never encounter the imbalance. But as a math problem, it's still not solved. (Someone may have solved it or proved it insoluble, but obviously I am not aware of such a proof, because otherwise I would have presented it already.) John From lepto.python at gmail.com Thu May 7 22:27:08 2009 From: lepto.python at gmail.com (oyster) Date: Fri, 8 May 2009 10:27:08 +0800 Subject: free chart lib for Python? Message-ID: <6a4f17690905071927u56bc48b9k51cb55af693b685f@mail.gmail.com> I mean chart, not plot. If you don't know the difference, you can check www.advsofteng.com, which is a commercial program is there such a thing with many kinds of chart, i.e. pie-chart, line-chart, ......? A long time ago, I programmed a rmchart interface, however rmchart is windows only, and www.rmchart.com disappeared now See you From srikrishnamohan at gmail.com Thu May 7 22:32:37 2009 From: srikrishnamohan at gmail.com (km) Date: Fri, 8 May 2009 11:32:37 +0900 Subject: update python version Message-ID: Hi all, Is there a way to update python 2.6.1 to 2.6.2 using easy_install ? thanks, regards, KM -------------- next part -------------- An HTML attachment was scrubbed... URL: From sjmachin at lexicon.net Thu May 7 22:42:04 2009 From: sjmachin at lexicon.net (John Machin) Date: Thu, 7 May 2009 19:42:04 -0700 (PDT) Subject: SQL and CSV References: <1d2bb50f-2e3f-429b-aa98-7e1fe1f011b0@t10g2000vbg.googlegroups.com> Message-ID: On May 8, 1:45?am, Nick wrote: > On May 5, 8:27?pm, Tim Golden wrote: > > > > > Nick wrote: > > > Part of the problem is that the 'selection' needs to be in a config > > > file. I can put the if row['status'] != 'Cancelled': return True into > > > a config, read it and eval it, but its not quite as clean as ansql > > > route. > > > Still not clear what the restriction is. If you were writingSQLyou'd have to read *something* from your config file, > > unless you're suggesting that the "config file" is in fact > > aSQLfile. Which is one way of doing it, but then you might > > just as well have your config file as a Python file and > > import it. > > > Have I missed the point somewhere here? Can you give an > > example -- even a fictional one -- of what you couldn't > > do using, say, the example I gave earlier? > > > TJG > > Solution found. In the end I used SQLite to read from a csv file, and > now I can query the CSV file. The file is read using the csv module > > First create a function > > def fraction(p, denom): > ? ? num, frac = p.split ('-') > ? ? return float (num) + float (frac) / denom > > for use within queries. > > Now build the class. > > ? ? ? ? self.filename ?= filename > ? ? ? ? self.dialect ? = dialect > ? ? ? ? self.query ? ? = query > ? ? ? ? reader = csv.reader (open (filename, 'r')) > ? ? ? ? self.connection = sqlite.connect(":memory:") > ? ? ? ? self.connection.create_function("fraction", 2, fraction) # > Adds in function > ? ? ? ? self.cursor = self.connection.cursor() > ? ? ? ? first = True > ? ? ? ? for row in reader: > ? ? ? ? ? ? if first: > ? ? ? ? ? ? ? ? headers = [] > ? ? ? ? ? ? ? ? for r in row: > ? ? ? ? ? ? ? ? ? ? n = r.strip().replace (' ', '_').replace ('-','_') > ? ? ? ? ? ? ? ? ? ? headers.append (n) > ? ? ? ? ? ? ? ? command = 'create table csv (%s)' % ','.join (headers) > ? ? ? ? ? ? ? ? self.cursor.execute (command) > ? ? ? ? ? ? ? ? first = False > ? ? ? ? ? ? else: > ? ? ? ? ? ? ? ? command = 'insert into csv values ("%s")' % '","'.join > (row) > ? ? ? ? ? ? ? ? self.cursor.execute (command) > > and then I can use this > > ? ? ? ? self.cursor.execute (self.query) > ? ? ? ? rows = self.cursor.fetchall() > ? ? ? ? headers = [] > ? ? ? ? for r in self.cursor.description: > ? ? ? ? ? ? headers.append (r[0]) > ? ? ? ? results = Results.Results (headers, self.name, {}) > ? ? ? ? i = 0 > ? ? ? ? for row in rows: > ? ? ? ? ? ? results.add (row, i) > ? ? ? ? ? ? i = i + 1 > ? ? ? ? return results > > to query the results. > > Results.Results is one of my classes that's reused in lots of places. > > The query then looks somethign like this > > ? ? ? ? ? ? ? ? select > ? ? ? ? ? ? ? ? ? ? Client_Reference_Number as TrdNbr, > ? ? ? ? ? ? ? ? ? ? Asset_Number as ISIN, > ? ? ? ? ? ? ? ? ? ? Quantity as Qty, > ? ? ? ? ? ? ? ? ? ? status > ? ? ? ? ? ? ? ? from > ? ? ? ? ? ? ? ? ? ? csv The remaining lines of your SELECT statement are incredibly redundant AFAICT. It seems you have pushed the contents of your csv file into a data base and pulled them ALL out again ... not what I'd call a "query". What's the point? > ? ? ? ? ? ? ? ? where status in ("CANCELLED") > > ? ? ? ? ? ? ? ? union > > ? ? ? ? ? ? ? ? select > ? ? ? ? ? ? ? ? ? ? Client_Reference_Number as TrdNbr, > ? ? ? ? ? ? ? ? ? ? Asset_Number as ISIN, > ? ? ? ? ? ? ? ? ? ? Quantity as Qty, > ? ? ? ? ? ? ? ? ? ? status > ? ? ? ? ? ? ? ? from > ? ? ? ? ? ? ? ? ? ? csv > ? ? ? ? ? ? ? ? where status not in ("CANCELLED") > > All incredibly neat and the first time I've used SQLite. > > nick From davecook at nowhere.net Thu May 7 22:45:12 2009 From: davecook at nowhere.net (David Cook) Date: 08 May 2009 02:45:12 GMT Subject: Which one is best Python or Java for developing GUI applications? References: Message-ID: <4a039cb8$0$32213$c3e8da3@news.astraweb.com> On 2009-05-05, srinivasan srinivas wrote: > Could you tell me does Python have any advantages over Java for the > development of GUI applications? You don't have to choose between them. You can program Swing applications in Jython. And Jython is just a jar that you can bundle in another jar for distribution just like any other Java jar. You will have to understand Java code to learn Swing, though. I suggest also looking into the Swing Application Framework and Netbeans. However, I would look at PyQt first (if the license requirements are OK). PyQT is well thought out, consistent, and featureful. And then maybe try wxPython if you need a more liberal license. wxPython is clunky and inconsistent, but gets the job done. Dave Cook From wuwei23 at gmail.com Thu May 7 23:32:18 2009 From: wuwei23 at gmail.com (alex23) Date: Thu, 7 May 2009 20:32:18 -0700 (PDT) Subject: free chart lib for Python? References: Message-ID: <6d770dc1-6efa-4535-a16d-55667591c328@j18g2000prm.googlegroups.com> On May 8, 12:27?pm, oyster wrote: > is there such a thing with many kinds of chart, i.e. pie-chart, > line-chart, ......? The best place to look is PyPI, there are several possible candidates there: http://pypi.python.org/pypi?%3Aaction=search&term=charts&submit=search I've had a lot of success with the Python Google Chart module: http://pygooglechart.slowchop.com/ From jjposner at snet.net Thu May 7 23:56:48 2009 From: jjposner at snet.net (John Posner) Date: Thu, 07 May 2009 23:56:48 -0400 Subject: tkinter and widget placement after resizing In-Reply-To: References: Message-ID: <4A03AD80.7050905@snet.net> jamieson wrote: > i.e. start out with a window like this: > > [1][4][7] > [2][5][8] > [3][6][9] > > > make the main window larger and end up with this: > > [1][6] > [2][7] > [3][8] > [4][9] > [5 Here's a solution, using Label widgets for clarity. The keys are: * start numbering from zero, not one * use divmod() on an item's index within the list to determine the item's column/row position * bind the event for window resizing # redo_layout.py import sys from Tkinter import * CHAR_WIDTH = 10 PAD = 3 INIT_COL_COUNT = 3 def grid_positions(count, numcols): """ return a generator for (colnum, rownum) position tuples, given the total number of items and the number of columns the first column is filled with items, then the second, etc. """ numrows, rem = divmod(count, numcols) # need an extra row if there was a remainder if rem: numrows += 1 # return value is a generator return ( divmod(i, numrows) for i in range(count) ) def place_labels(event): """ reposition all the items in the sequence "label_list" """ # assumption: all labels have same width label_width = label_list[0].winfo_width() window_width = frm.winfo_width() # calculate new column count new_col_count = window_width // (label_width+PAD) # get new position iterator pos_iter = grid_positions(LABEL_COUNT, new_col_count) # redo layout for lab in label_list: lab.grid_forget() colnum, rownum = pos_iter.next() lab.grid(column=colnum, row=rownum, padx=PAD, pady=PAD) def create_labels(count): """ create a list of Label items, with text "1", "2", etc. """ return [ Label(frm, bg='cyan', width=CHAR_WIDTH, text="%d" % i) for i in range(count) ] if __name__ == "__main__": try: LABEL_COUNT = int(sys.argv[1]) except (ValueError, IndexError): print "ERROR: Must specify number of labels" sys.exit(1) # Tkinter window and whole-window Frame root = Tk() frm = Frame(root) frm.pack(expand=True, fill=BOTH) # create some labels label_list = create_labels(LABEL_COUNT) # perform initial layout pos_iter = grid_positions(LABEL_COUNT, INIT_COL_COUNT) for lab in label_list: coloff, rowoff = pos_iter.next() lab.grid(column=coloff, row=rowoff, padx=PAD, pady=PAD) del pos_iter # event configuration: redo the layout when the window size changes frm.bind('', place_labels) # go root.mainloop() From clp2 at rebertia.com Fri May 8 00:48:43 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Thu, 7 May 2009 21:48:43 -0700 Subject: heapq.merge with key= In-Reply-To: References: Message-ID: <50697b2c0905072148n6e126fb8l91bf049cdbe451ec@mail.gmail.com> On Thu, May 7, 2009 at 2:23 PM, Kevin D. Smith wrote: > I need the behavior of heapq.merge to merge a bunch of results from a > database. ?I was doing this with sorted(itertools.chain(...), key=...), but > I would prefer to do this with generators. ?My issue is that I need the key= > argument to sort on the correct field in the database. ?heapq.merge doesn't > have this argument and I don't understand the code enough to know if it's > possible to add it. ?Is this enhancement possible without drastically > changing the current code? I think so. Completely untested code: def key(ob): #code here class Keyed(object): def __init__(self, obj): self.obj = obj def __cmp__(self, other): return cmp(key(self.obj), key(other.obj)) def keyify(gen): for item in gen: yield Keyed(item) def stripify(gen): for keyed in gen: yield keyed.obj merged = stripify(merge(keyify(A), keyify(B), keyify(C))) #A,B,C being the iterables Cheers, Chris -- http://blog.rebertia.com From ganesh.gbg at gmail.com Fri May 8 00:51:36 2009 From: ganesh.gbg at gmail.com (gganesh) Date: Thu, 7 May 2009 21:51:36 -0700 (PDT) Subject: About twisted.mail.smtp.SMTPDeliveryError Message-ID: <8bb91b91-c00d-48d1-8b64-6f4765754e1c@d19g2000prh.googlegroups.com> hi group, I wrote a code as below from twisted.mail.smtp import sendmail from twisted.internet import reactor from twisted.python.log import err import time MAILSERVER = 'mail.xxx.com' listTo = ['gxxxxxx at gmail.com', 'gjangoxxxxx.py at gmail.com', 'lxxxxt at yahoo.co.in', 'gk1xx at gmail.com'] FROM = 'ganeshxx at .....com' MSGBODY = "hi this final test" a= time.time() done = sendmail(MAILSERVER, FROM, listTo, MSGBODY ,senderDomainName=None, port=25) done.addErrback(err) done.addCallback(lambda ignored: reactor.stop()) reactor.run() print "Took %s seconds" %str(time.time()-a) it showed me a error like "Failure: twisted.mail.smtp.SMTPDeliveryError: 554 No recipients accepted" and "Relay access denied" errors Is this error is because, i haven't authenticated my email account to send mail. Is so , having username and password of smpt server ,how to authenticate in Twisted Framework Thanks From li.wang.d at gmail.com Fri May 8 01:04:34 2009 From: li.wang.d at gmail.com (Li Wang) Date: Fri, 8 May 2009 15:04:34 +1000 Subject: Python 2.6, File read() problems in Windows Xp Message-ID: <11aabe3b0905072204w499679f8q9d1be6d63cddd597@mail.gmail.com> Hi all: I am trying to read a non-text file as a string by using Python read(), however, it seems there is some thing wrong with it. I can use read() on text file correctly, but unable to read .xls file correctly. (The program can read any file correctly in Fedora 10) Any idea how to solve this problem? Thank you very much! -- Li ------ Time is all we have and you may find one day you have less than you think From clp2 at rebertia.com Fri May 8 01:38:10 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Thu, 7 May 2009 22:38:10 -0700 Subject: Python 2.6, File read() problems in Windows Xp In-Reply-To: <11aabe3b0905072204w499679f8q9d1be6d63cddd597@mail.gmail.com> References: <11aabe3b0905072204w499679f8q9d1be6d63cddd597@mail.gmail.com> Message-ID: <50697b2c0905072238o6a00fc07ibc92423926313131@mail.gmail.com> On Thu, May 7, 2009 at 10:04 PM, Li Wang wrote: > Hi all: > > I am trying to read a non-text file as a string by using Python > read(), however, it seems there is some thing wrong with it. I can use > read() on text file correctly, but unable to read .xls file correctly. > (The program can read any file correctly in Fedora 10) > > Any idea how to solve this problem? You need to add a "b" to the `flags` argument to open() when you open the file, e.g. open("the_file.xls", "rb") Unlike *nix, Windows differentiates between binary and text files, hence the need for the "b" flag to specify which you're dealing with. Further info: http://docs.python.org/library/functions.html#open Cheers, Chris -- http://blog.rebertia.com From dchandran1 at yahoo.com Fri May 8 02:05:36 2009 From: dchandran1 at yahoo.com (Deepak Chandran) Date: Thu, 7 May 2009 23:05:36 -0700 (PDT) Subject: configuring python with disable-thread in Ubuntu Message-ID: <968094.67933.qm@web110411.mail.gq1.yahoo.com> Hello, I am embedding python inside a C++ program. For some reason (I think libxml2), I am getting Segmentation fault at PyThread_release_lock. The solution I found online was to configure python with --disable-thread. I used "apt-get install python-dev" to install python. How do I re-configure it using the --disable-thread option? Thanks. -------------- next part -------------- An HTML attachment was scrubbed... URL: From list at qtrac.plus.com Fri May 8 02:37:03 2009 From: list at qtrac.plus.com (Mark Summerfield) Date: Thu, 7 May 2009 23:37:03 -0700 (PDT) Subject: Public attributes with really private data Message-ID: <7b93110c-6000-4e71-8da9-ce6ea25c8a12@p4g2000vba.googlegroups.com> Hi, I had a quick search & didn't find anything _nice_ that produced attributes with really private data, so I came up with a possible solution---for Python 3. (For Python 2 there does seem to be an approach although I'm not keen on it: http://www.builderau.com.au/blogs/byteclub/viewblogpost.htm?p=339270875 ) Here's a standard class with one read-only and one writable property that has a tiny bit of validation. class P: def __init__(self, w): self.w = w @property def r(self): return 5 @property def w(self): return self.__w @w.setter def w(self, value): if value > 0: # Only +ve values allowed self.__w = value else: raise ValueError("'{0}' is not valid for w".format(value)) The read-only property is completely private because it isn't actually stored as such. But if we do dir() on an instance, in addition to 'r' and 'w', we also have '_P__w'. So the writable property's data is easily accessible, and the validation can be got around: >>> p = P(9) >>> p.r, p.w (5, 9) >>> p.w = 43 >>> p.r, p.w (5, 43) >>> p.w = -7 Traceback (most recent call last): ... ValueError: '-7' is not valid for w >>> p._P__w = -7 >>> p.r, p.w (5, -7) Here's a class where I can't think of a way to access the private data and set invalid values. class A: r = Attribute("r", 5) w = Attribute("w", None, lambda self, value: value > 0) def __init__(self, w): self.w = w The Attribute class is a descriptor that takes three arguments: name of attribute, initial value (essential for read-only attributes!), and a validator function (which could be "lambda *a: True" if any value is accepatble). >>> a = A(9) >>> a.r, a.w (5, 9) >>> a.w = 43 >>> a.r, a.w (5, 43) >>> a.w = -7 Traceback (most recent call last): ... ValueError: '-7' is not valid for w If we do a dir(a) the only attributes we get (beyond those from object) are 'r' and 'w', so it shouldn't be possible to get around the validation---at least not easily. Here's a rough & ready implementation of the Attribute class: class Attribute: def __init__(self, name, first_value=None, validator=None): self.__name__ = name hidden_value = first_value self.__getter = lambda self: hidden_value if validator is not None: def set(self, value): if validator(self, value): nonlocal hidden_value hidden_value = value else: raise ValueError("'{0}' is not valid for {1}".format(value, name)) self.__setter = set else: self.__setter = None def __get__(self, instance, owner=None): if instance is None: return self return self.__getter(instance) def __set__(self, instance, value): if self.__setter is None: raise AttributeError("'{0}' is read-only".format( self.__name__)) return self.__setter(instance, value) The key to making the attribute data private is that it is held as part of a closure's state. Notice that nonlocal is needed, so you need Python 3. -- Mark Summerfield, Qtrac Ltd, www.qtrac.eu C++, Python, Qt, PyQt - training and consultancy "Programming in Python 3" - ISBN 0137129297 From gjango.py at gmail.com Fri May 8 02:37:27 2009 From: gjango.py at gmail.com (guptha) Date: Thu, 7 May 2009 23:37:27 -0700 (PDT) Subject: Simple programme - Just want to know whether this is correct way of coding Message-ID: <49f8cb54-f268-46bc-a585-6d116674d00d@k19g2000prh.googlegroups.com> Hi group, This is my first programme in python ,I need to know whether my code is in the right path of performance I wrote a code using multithreading to send mails FROM = '....com' SUBJECT = 'This is the subject' MSGBODY = 'This the body of the message ' MAILSERVER = 'mail....com' port = 25 username = 'username' password = 'pass' # trim the strings of any leading or trailing spaces FROM = FROM.strip() SUBJECT = SUBJECT.strip() MSGBODY = MSGBODY.strip() MAILSERVER = MAILSERVER.strip() username = username.strip() password = password.strip() # set up email parameters msg = MIMEMultipart() msg['From'] = FROM msg['Subject'] = SUBJECT #-------------------------------------------------- print "Starting Multi Thread Method" class MyThread(Thread): def __init__(self, site, FROM, MSGBODY): Thread.__init__(self) self.site = site self.FROM=FROM self.MSGBODY=MSGBODY def run(self): #Connect to server print 'Connecting to mail server ', MAILSERVER try: s = smtplib.SMTP(MAILSERVER,port) print 'connected' #s.set_debuglevel(1) except: print 'ERROR: Unable to connect to mail server', sys.exc_info ()[0] sys.exit(1) #login to server if password <> '': print 'Logging into mail server' try: s.login(username,password) except: print 'ERROR: Unable to login to mail server', MAILSERVER print 'Please recheck your password' sys.exit(1) print "running for %s " %self.site print s s.sendmail(self.FROM, self.site, self.MSGBODY) print "from %s" %self.FROM print "msg %s" %self.MSGBODY print "Emailed for site %s" %self.site s.quit() s.close() a= time.time() threads = [] for site in listTo: T = MyThread(site,FROM,MSGBODY) threads.append(T) T.start() for i in threads: i.join() print "Took %s seconds" %str(time.time()-a) #----------------------------------------------------- The code Works fine ,but I doubt about the performance issue ,My intention is to send mails concurrently to large number of mail. 1.For every mail id i send It creates a new SMTP object,in case, if i send to 1000 or more ids a) It obliviously creates that much SMPT connections ,is this a right approach . Thanks in Advance From gh at ghaering.de Fri May 8 02:40:40 2009 From: gh at ghaering.de (=?ISO-8859-1?Q?Gerhard_H=E4ring?=) Date: Fri, 08 May 2009 08:40:40 +0200 Subject: configuring python with disable-thread in Ubuntu In-Reply-To: <968094.67933.qm@web110411.mail.gq1.yahoo.com> References: <968094.67933.qm@web110411.mail.gq1.yahoo.com> Message-ID: Deepak Chandran wrote: > Hello, > > I am embedding python inside a C++ program. For some reason (I think > libxml2), I am getting Segmentation fault at PyThread_release_lock. > > The solution I found online was to configure python with --disable-thread. That doesn't sound like a solution, but like a kludge. > I used "apt-get install python-dev" to install python. > How do I re-configure it using the --disable-thread option? You don't want your system Python to be built with "--disable-thread". Other utilities might depend on threads being available in Python. If you really want to go this route, download Python from python.org and build it from source with the required options to "./configure". -- Gerhard From shstein2002 at yahoo.com Fri May 8 02:42:35 2009 From: shstein2002 at yahoo.com (shstein2002 at yahoo.com) Date: Thu, 7 May 2009 23:42:35 -0700 (PDT) Subject: scrolledtext widget error? Message-ID: <02b12656-f849-4856-bfe5-fdf3b926577f@j18g2000prm.googlegroups.com> I wrote a twitter client which writes the statuses into a ScrolledText widget, but it rarely gives a error that I can't nail down. The code is: twitwin.scrolled.config(state=NORMAL) twitwin.scrolled.delete("1.0", END) evens = True _.links = [] for x in _.parser.tweets: start = _.scrolled.index(INSERT) print "start = ",start _.scrolled.insert(END, x['screen_name']+" ", ("bold",)) txt = _.whitere.sub(" ",x['text']) #the text with whitespace compressed islink = False for y in _.urlre.split(txt): #handle urls if y != "": if islink: a = _.scrolled.index(INSERT) _.scrolled.insert(END, y)#, ("link",)) b = _.scrolled.index(INSERT) _.scrolled.tag_add("link", a, b) _.scrolled.tag_add("href%d"%len(_.links), a, b) _.links.append((a,b,y)) #save url info and position in the widget else: _.scrolled.insert(END, y) islink = not islink _.scrolled.insert(END, "\n") if evens: _.scrolled.tag_add("light", start, END) #alternating background colors else: _.scrolled.tag_add("dark", start, END) evens = not evens twitwin.scrolled.config(state=DISABLED) The print statement usually prints a valid index, but rarely prints "start=None" and it crashes. Should I be using something other than INSERT or another method here? Thanks in advance, -- Rick From gh at ghaering.de Fri May 8 02:42:44 2009 From: gh at ghaering.de (=?ISO-8859-1?Q?Gerhard_H=E4ring?=) Date: Fri, 08 May 2009 08:42:44 +0200 Subject: update python version In-Reply-To: References: Message-ID: km wrote: > Hi all, > > Is there a way to update python 2.6.1 to 2.6.2 using easy_install ? No, easy_install installs Python packages. It doesn't upgrade Python itself. If this is Windows, just install the newer Python version. No need to uninstall the 2.6.1 first. If this is some Unix variant, hopefully they will provide updated Python packages soon. -- Gerhard From texaomruzj at hotmail.com Fri May 8 02:52:43 2009 From: texaomruzj at hotmail.com (texaomruzj at hotmail.com) Date: Thu, 7 May 2009 23:52:43 -0700 (PDT) Subject: =?Big5?B?qMisd6aopEi8dq21q7A=?= Message-ID: <3b546c4b-3808-423d-ab62-d63856df045e@n7g2000prc.googlegroups.com> ????!!????!!????????

???:sex369????? -
?2?:???????? -
?3?:live173??live? -
?4?:0401??? -
?5?:????? -
?6?:??????? -
?7?:?????? -
?8?:??????? -
?9?:mm158???? -
?10?:????? -

-
From lie.1296 at gmail.com Fri May 8 03:16:59 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Fri, 08 May 2009 07:16:59 GMT Subject: Simple programme - Just want to know whether this is correct way of coding In-Reply-To: <49f8cb54-f268-46bc-a585-6d116674d00d@k19g2000prh.googlegroups.com> References: <49f8cb54-f268-46bc-a585-6d116674d00d@k19g2000prh.googlegroups.com> Message-ID: guptha wrote: > Hi group, > This is my first programme in python ,I need to know whether my code > is in the right path of performance > > I wrote a code using multithreading to send mails Can't you use BCC? > The code Works fine ,but I doubt about the performance issue ,My > intention is to send mails concurrently to large number of mail. > 1.For every mail id i send It creates a new SMTP object,in case, if i > send to 1000 or more ids > a) It obliviously creates that much SMPT connections ,is this a > right approach . > Thanks in Advance In a lot of cases involving internet, the bottleneck would be internet speed. And for most purpose (except spamming) it is not really that necessary to send so much email in so little time (assuming you send 1 mail per second -- which is really slow considering today's processor and internet speed -- you can send 86400 mails per day). Larger mailing list can also use BCC. From __peter__ at web.de Fri May 8 03:19:13 2009 From: __peter__ at web.de (Peter Otten) Date: Fri, 08 May 2009 09:19:13 +0200 Subject: Public attributes with really private data References: <7b93110c-6000-4e71-8da9-ce6ea25c8a12@p4g2000vba.googlegroups.com> Message-ID: Mark Summerfield wrote: > I had a quick search & didn't find anything _nice_ that produced > attributes with really private data, so I came up with a possible > solution---for Python 3. Do really you think what you suggest below is "nice"? By the way, your Attribute descriptor stores the value for all instances of A in the same variable... Peter From davea at ieee.org Fri May 8 03:25:56 2009 From: davea at ieee.org (Dave Angel) Date: Fri, 08 May 2009 03:25:56 -0400 Subject: Python 2.6, File read() problems in Windows Xp In-Reply-To: <11aabe3b0905072204w499679f8q9d1be6d63cddd597@mail.gmail.com> References: <11aabe3b0905072204w499679f8q9d1be6d63cddd597@mail.gmail.com> Message-ID: <4A03DE84.40406@ieee.org> Li Wang wrote: > Hi all: > > I am trying to read a non-text file as a string by using Python > read(), however, it seems there is some thing wrong with it. I can use > read() on text file correctly, but unable to read .xls file correctly. > (The program can read any file correctly in Fedora 10) > > Any idea how to solve this problem? > > Thank you very much! > > Chances are you forgot the "b" parameter to open(). Unnecessary in Unix, it tells the library to *not* translate \r\n to \n upon read, or the inverse on write. In other words, with the "b" parameter, the file is read in unchanged. infile = open(filename, "rb") .... From rustompmody at gmail.com Fri May 8 03:46:05 2009 From: rustompmody at gmail.com (Rustom Mody) Date: Fri, 8 May 2009 13:16:05 +0530 Subject: xml in python Message-ID: Can someone give me a heads up on xml parsing in python? The context is that I want to write a simple docbook to text converter. DOM is alright -- dont want to break my head with SAX just for performance when my documents are not likely to be large. My problem is that there seems to be so many nearly equivalent ways (Pyxml? Amara?) some of which have moved from 3rd party to builtin status that I am not clear what is the current method of choice. Thanks -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Fri May 8 03:54:30 2009 From: __peter__ at web.de (Peter Otten) Date: Fri, 08 May 2009 09:54:30 +0200 Subject: SQL and CSV References: <1d2bb50f-2e3f-429b-aa98-7e1fe1f011b0@t10g2000vbg.googlegroups.com> Message-ID: Nick wrote: > self.cursor = self.connection.cursor() > first = True > for row in reader: > if first: > headers = [] > for r in row: > n = r.strip().replace (' ', '_').replace ('-','_') > headers.append (n) > command = 'create table csv (%s)' % ','.join (headers) > self.cursor.execute (command) > first = False > else: > command = 'insert into csv values ("%s")' % '","'.join > (row) > self.cursor.execute (command) > You can simplify that a bit: cursor = self.cursor = self.connection.cursor() first_row = next(reader) headers = [column.strip().replace(" ", "_").replace("-", "_") for column in first_row] cursor.execute("create table csv (%s)" % ", ".join(headers)) placeholders = ", ".join("?"*len(headers)) command = "insert into csv values (%s)" % placeholders cursor.executemany(command, reader) While it may not matter here using placeholders instead of manually escaping user-provided values is a good habit to get into. > self.cursor.execute (self.query) > rows = self.cursor.fetchall() rows = self.cursor.execute(self.query) doesn't build an intermediate list. > i = 0 > for row in rows: > results.add (row, i) > i = i + 1 This is written for i, row in enumerate(rows): results.add(row, i) in idiomatic Python. Peter From davea at ieee.org Fri May 8 04:02:21 2009 From: davea at ieee.org (Dave Angel) Date: Fri, 08 May 2009 04:02:21 -0400 Subject: Simple programme - Just want to know whether this is correct way of coding In-Reply-To: <49f8cb54-f268-46bc-a585-6d116674d00d@k19g2000prh.googlegroups.com> References: <49f8cb54-f268-46bc-a585-6d116674d00d@k19g2000prh.googlegroups.com> Message-ID: <4A03E70D.305@ieee.org> guptha wrote: > Hi group, > This is my first programme in python ,I need to know whether my code > is in the right path of performance > > I wrote a code using multithreading to send mails > > FROM = '....com' > > SUBJECT = 'This is the subject' > MSGBODY = 'This the body of the message ' > MAILSERVER = 'mail....com' > port = 25 > username = 'username' > password = 'pass' > > # trim the strings of any leading or trailing spaces > FROM = FROM.strip() > SUBJECT = SUBJECT.strip() > MSGBODY = MSGBODY.strip() > MAILSERVER = MAILSERVER.strip() > username = username.strip() > password = password.strip() > > > # set up email parameters > msg = MIMEMultipart() > msg['From'] = FROM > msg['Subject'] = SUBJECT > > #-------------------------------------------------- > print "Starting Multi Thread Method" > > class MyThread(Thread): > > def __init__(self, site, FROM, MSGBODY): > Thread.__init__(self) > self.site = site > self.FROM=FROM > self.MSGBODY=MSGBODY > > def run(self): > #Connect to server > print 'Connecting to mail server ', MAILSERVER > try: > s = smtplib.SMTP(MAILSERVER,port) > print 'connected' > #s.set_debuglevel(1) > except: > print 'ERROR: Unable to connect to mail server', sys.exc_info ()[0] > sys.exit(1) > > #login to server > if password <> '': > print 'Logging into mail server' > try: > s.login(username,password) > except: > print 'ERROR: Unable to login to mail server', MAILSERVER > print 'Please recheck your password' > sys.exit(1) > print "running for %s " %self.site > print s > s.sendmail(self.FROM, self.site, self.MSGBODY) > print "from %s" %self.FROM > print "msg %s" %self.MSGBODY > print "Emailed for site %s" %self.site > s.quit() > s.close() > > > a= time.time() > threads = [] > > for site in listTo: > T = MyThread(site,FROM,MSGBODY) > threads.append(T) > T.start() > > > for i in threads: > i.join() > > print "Took %s seconds" %str(time.time()-a) > > #----------------------------------------------------- > > The code Works fine ,but I doubt about the performance issue ,My > intention is to send mails concurrently to large number of mail. > 1.For every mail id i send It creates a new SMTP object,in case, if i > send to 1000 or more ids > a) It obliviously creates that much SMPT connections ,is this a > right approach . > Thanks in Advance > > Any program that launches multiple threads is not a "simple program," especially for a first time user. I don't know smtplib at all, so these questions may be off-base. First, sendmail() takes as its second argument a list of to_addr, so why not send these all as a single operation? If that would work, this would degenerate into a much simpler program. More importantly, it should generate much less traffic between your machine and your smtp server, and probably less traffic between that server and the actual destination domains. I have to assume that if it's a single sendmail request, the server would then batch-send them to each unique domain in the to_addrs list. Anyway, what if the message body be a couple of megabytes, and you're sending it to 100 people? Sending a single message with a list in to_list would save tons of time. Second, if you do have to send them all separately (for example, if you had different mail_options, which you don't yet), then I question the need or workability of using a separate thread for all of them. Trying to open 1000 threads is very resource hungry, Trying to open that many smtp connections is even worse. One of these is likely to fail. And I'm guessing that beyond 10 or so, it'd run at least as fast with a different approach. If I had to do something like this, I'd expect to wind up with some number (say 10) of threads, and a queue of things for them to do. So if you had 1000 things, each thread would do approximately 100 of them. Now, sharing a queue like that is more work, with more things to mess up. Currently your threads share only read-only data, which makes threads pretty straightforward. From soumen08 at gmail.com Fri May 8 04:12:30 2009 From: soumen08 at gmail.com (Soumen banerjee) Date: Fri, 8 May 2009 13:42:30 +0530 Subject: confused with subprocess.Popen Message-ID: Hello, for a certain app, i used to use a command: os.system("soundwrapper espeak -f line.txt") now, if i wanted to kill espeak, i would have to run: os.system("killall espeak") since the subprocess module allows sending SIGKILL to the process, i decided to switch to using it. However i cant pass the spaced out arguments to it. For example: a=subprocess.Popen("soundwrapper espeak -f line.txt") results in a OSError, no such file or directory. How do i use the Popen function? Regards Soumen From li.wang.d at gmail.com Fri May 8 04:14:16 2009 From: li.wang.d at gmail.com (Li Wang) Date: Fri, 8 May 2009 18:14:16 +1000 Subject: Python 2.6, File read() problems in Windows Xp In-Reply-To: <50697b2c0905072238o6a00fc07ibc92423926313131@mail.gmail.com> References: <11aabe3b0905072204w499679f8q9d1be6d63cddd597@mail.gmail.com> <50697b2c0905072238o6a00fc07ibc92423926313131@mail.gmail.com> Message-ID: <11aabe3b0905080114ma4efb2dv7879e64104e0f7f4@mail.gmail.com> 2009/5/8 Chris Rebert : > On Thu, May 7, 2009 at 10:04 PM, Li Wang wrote: >> Hi all: >> > the file, e.g. open("the_file.xls", "rb") > Unlike *nix, Windows differentiates between binary and text files, > hence the need for the "b" flag to specify which you're dealing with. Hi Thank you very much for reply, The method doesn't work. Here is the problem: after reading the whole fie as a string, I need the string to be made of 8bits-symbols. I do not mind what's the content in the file, what I need to do is something like reading the file byte by byte and concatenate these bytes into a single string. Any suggestions? Thank you very much -- Li ------ Time is all we have and you may find one day you have less than you think From dotancohen at gmail.com Fri May 8 04:21:40 2009 From: dotancohen at gmail.com (Dotan Cohen) Date: Fri, 8 May 2009 11:21:40 +0300 Subject: What would YOU like to see in a txt to html converter? In-Reply-To: References: <87my9okc1q.fsf@lakeside.sjtu.edu.cn> Message-ID: <880dece00905080121o6d63b49ap9f51b5fa5fbf5264@mail.gmail.com> > I already thought of using dots or asterisks > or whatever to let the user format the text instead of using html tags (this > would be quite paradox ;-) > Then please do not invent another wheel. Write a markdown parser: http://en.wikipedia.org/wiki/Markdown -- Dotan Cohen http://what-is-what.com http://gibberish.co.il From jeremiah.dodds at gmail.com Fri May 8 04:31:07 2009 From: jeremiah.dodds at gmail.com (Jeremiah Dodds) Date: Fri, 8 May 2009 09:31:07 +0100 Subject: Web Based Front End? In-Reply-To: <18804639-32ae-4bdf-9ecc-4c8b21f8a43f@g19g2000vbi.googlegroups.com> References: <18804639-32ae-4bdf-9ecc-4c8b21f8a43f@g19g2000vbi.googlegroups.com> Message-ID: <12cbbbfc0905080131g5c48d15oab489e06318150ac@mail.gmail.com> On Fri, May 8, 2009 at 2:52 AM, wrote: > So, my question is what is the best method to be able to have a user > enter specific data on a web page, have that data be passed to the > Python script and then have the output returned to the web page? > Essentially, I want to use a web based front end to accomplish the > same thing that the IDLE GUI does, (asks me a few questions, I answer > them, it builds a configuration file and prints it to the screen). > > There isn't a best way, really. I'm partial to CherryPy. A minimal example would look like so: ------------------------------- import cherrypy class Root(object): page = "%s" @cherrypy.expose def enter_data(self, data): return self.page % ("You entered: " + data,) @cherrypy.expose def index(self): form = """

""" return self.page % form if __name__ == '__main__': cherrypy.quickstart(Root()) ----------------------------------- Note, I wouldn't actually recommend using string building like that for returning pages unless your app is seriously trivial. I use Genshi for templating, the tutorial that they have is very nice (and uses cherrypy). There's a plethora of different ways to build web apps in python. In all reality, you should probably try out a few and see what suits your tastes. -------------- next part -------------- An HTML attachment was scrubbed... URL: From piet at cs.uu.nl Fri May 8 04:49:39 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 08 May 2009 10:49:39 +0200 Subject: SimpleXMLRPCServer and creating a new object on for each new client request. References: Message-ID: >>>>> Piet van Oostrum (PvO) wrote: >>>>> Jelle Smet (JS) wrote: >PvO> One more thing: >JS> I start python interactively: >>>>> import xmlrpclib >>>>> session1=xmlrpclib.ServerProxy('http://localhost:8000') >>>>> session2=xmlrpclib.ServerProxy('http://localhost:8000') >>>>> print session1.show_random() >JS> 13930 >>>>> print session2.show_random() >JS> 13930 >>>>> >PvO> I get the impression, also from your use of the variable names >PvO> 'session1' and 'session2' that xmlrpclib.ServerProxy() gives you some >PvO> kind of connection to the XMLRPC server. This should have been: I get the impression, also from your use of the variable names 'session1' and 'session2', *that you think* that xmlrpclib.ServerProxy() gives you some kind of connection to the XMLRPC server. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From pavlovevidence at gmail.com Fri May 8 04:52:31 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Fri, 8 May 2009 01:52:31 -0700 (PDT) Subject: Public attributes with really private data References: <7b93110c-6000-4e71-8da9-ce6ea25c8a12@p4g2000vba.googlegroups.com> Message-ID: On May 7, 11:37?pm, Mark Summerfield wrote: > Hi, > > I had a quick search & didn't find anything _nice_ that produced > attributes with really private data, so I came up with a possible > solution---for Python 3. > (For Python 2 there does seem to be an approach although I'm not > keen on it:http://www.builderau.com.au/blogs/byteclub/viewblogpost.htm?p=339270875 > ) > > Here's a standard class with one read-only and one writable > property that has a tiny bit of validation. > > class P: > ? ? def __init__(self, w): > ? ? ? ? self.w = w > ? ? @property > ? ? def r(self): return 5 > ? ? @property > ? ? def w(self): return self.__w > ? ? @w.setter > ? ? def w(self, value): > ? ? ? ? if value > 0: # Only +ve values allowed > ? ? ? ? ? ? self.__w = value > ? ? ? ? else: > ? ? ? ? ? ? raise ValueError("'{0}' is not valid for w".format(value)) > > The read-only property is completely private because it isn't > actually stored as such. > > But if we do dir() on an instance, in addition to 'r' and 'w', we > also have '_P__w'. So the writable property's data is easily > accessible, and the validation can be got around: > > >>> p = P(9) > >>> p.r, p.w > (5, 9) > >>> p.w = 43 > >>> p.r, p.w > (5, 43) > >>> p.w = -7 > > Traceback (most recent call last): > ... > ValueError: '-7' is not valid for w>>> p._P__w = -7 > >>> p.r, p.w > > (5, -7) > > Here's a class where I can't think of a way to access the private > data and set invalid values. > > class A: > ? ? r = Attribute("r", 5) > ? ? w = Attribute("w", None, lambda self, value: value > 0) > ? ? def __init__(self, w): > ? ? ? ? self.w = w > > The Attribute class is a descriptor that takes three arguments: > name of attribute, initial value (essential for read-only > attributes!), and a validator function (which could be "lambda > *a: True" if any value is accepatble). > > >>> a = A(9) > >>> a.r, a.w > (5, 9) > >>> a.w = 43 > >>> a.r, a.w > (5, 43) > >>> a.w = -7 > > Traceback (most recent call last): > ... > ValueError: '-7' is not valid for w > > If we do a dir(a) the only attributes we get (beyond those from > object) are 'r' and 'w', so it shouldn't be possible to get > around the validation---at least not easily. Ok, I'll bite. A.w = -7 A.__class__ = A_without_validation a.w = -7 Attribute.__set__ = function_that_ignores_validation a.w = -7 I am not opposed to adding a little well-considered protection to some attributes where mistakes are prone to happen and/or dangerous, but it is futile to try to stop access entirely. There's just too many back doors. I would suggest that there really isn't much point in anything more complex than your first solution, which was to validate with properties and to store the value in a separate attribute. Respectable programmers won't lightly bypass your validation if they see that you've taken steps to enforce it. OTOH, once a programmer, respectable or not, decides to override your protection, they are not likely to be stopped by something more complex. So there is no point in making it more complex. Carl Banks From li.wang.d at gmail.com Fri May 8 05:04:34 2009 From: li.wang.d at gmail.com (Li Wang) Date: Fri, 8 May 2009 19:04:34 +1000 Subject: Python 2.6, File read() problems in Windows Xp In-Reply-To: <11aabe3b0905080114ma4efb2dv7879e64104e0f7f4@mail.gmail.com> References: <11aabe3b0905072204w499679f8q9d1be6d63cddd597@mail.gmail.com> <50697b2c0905072238o6a00fc07ibc92423926313131@mail.gmail.com> <11aabe3b0905080114ma4efb2dv7879e64104e0f7f4@mail.gmail.com> Message-ID: <11aabe3b0905080204w1aee64d1ma587cbeb45720876@mail.gmail.com> Hi: Problem solved Thank you very much, it works, It is my own problem:) All the best, Li 2009/5/8 Li Wang : > 2009/5/8 Chris Rebert : >> On Thu, May 7, 2009 at 10:04 PM, Li Wang wrote: >>> Hi all: >>> > >> the file, e.g. open("the_file.xls", "rb") >> Unlike *nix, Windows differentiates between binary and text files, >> hence the need for the "b" flag to specify which you're dealing with. > > Hi > Thank you very much for reply, > > The method doesn't work. > Here is the problem: after reading the whole fie as a string, I need > the string to be made of 8bits-symbols. > > I do not mind what's the content in the file, what I need to do is > something like reading the file byte by byte and concatenate these > bytes into a single string. > > Any suggestions? > > Thank you very much > > > > > > -- > Li > ------ > Time is all we have > and you may find one day > you have less than you think > -- Li ------ Time is all we have and you may find one day you have less than you think From li.wang.d at gmail.com Fri May 8 05:08:03 2009 From: li.wang.d at gmail.com (Li Wang) Date: Fri, 8 May 2009 19:08:03 +1000 Subject: Python 2.6, File read() problems in Windows Xp In-Reply-To: <4A03DE84.40406@ieee.org> References: <11aabe3b0905072204w499679f8q9d1be6d63cddd597@mail.gmail.com> <4A03DE84.40406@ieee.org> Message-ID: <11aabe3b0905080208x5df4df5bu4eec2712d842320@mail.gmail.com> Hi Dave: Thank you very much for you explanation:) > Chances are you forgot the "b" parameter to open(). Unnecessary in Unix, it > tells the library to *not* translate \r\n to \n upon read, or the inverse > on write. In other words, with the "b" parameter, the file is read in > unchanged. So, if I am using python in Linux, do open('file', 'r') and open('file', 'rb') work the same way? Thanks, Best regards, Li From piet at cs.uu.nl Fri May 8 05:12:21 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 08 May 2009 11:12:21 +0200 Subject: Simple programme - Just want to know whether this is correct way of coding References: <49f8cb54-f268-46bc-a585-6d116674d00d@k19g2000prh.googlegroups.com> Message-ID: >>>>> guptha (g) wrote: >g> Hi group, >g> This is my first programme in python ,I need to know whether my code >g> is in the right path of performance >g> I wrote a code using multithreading to send mails >g> FROM = '....com' >g> SUBJECT = 'This is the subject' >g> MSGBODY = 'This the body of the message ' >g> MAILSERVER = 'mail....com' >g> port = 25 >g> username = 'username' >g> password = 'pass' >g> # trim the strings of any leading or trailing spaces >g> FROM = FROM.strip() >g> SUBJECT = SUBJECT.strip() >g> MSGBODY = MSGBODY.strip() >g> MAILSERVER = MAILSERVER.strip() >g> username = username.strip() >g> password = password.strip() >g> # set up email parameters >g> msg = MIMEMultipart() >g> msg['From'] = FROM >g> msg['Subject'] = SUBJECT >g> #-------------------------------------------------- >g> print "Starting Multi Thread Method" >g> class MyThread(Thread): >g> def __init__(self, site, FROM, MSGBODY): >g> Thread.__init__(self) >g> self.site = site >g> self.FROM=FROM >g> self.MSGBODY=MSGBODY >g> def run(self): >g> #Connect to server >g> print 'Connecting to mail server ', MAILSERVER >g> try: >g> s = smtplib.SMTP(MAILSERVER,port) >g> print 'connected' >g> #s.set_debuglevel(1) >g> except: >g> print 'ERROR: Unable to connect to mail server', sys.exc_info ()[0] >g> sys.exit(1) >g> #login to server >g> if password <> '': >g> print 'Logging into mail server' >g> try: >g> s.login(username,password) >g> except: >g> print 'ERROR: Unable to login to mail server', MAILSERVER >g> print 'Please recheck your password' >g> sys.exit(1) >g> print "running for %s " %self.site >g> print s >g> s.sendmail(self.FROM, self.site, self.MSGBODY) >g> print "from %s" %self.FROM >g> print "msg %s" %self.MSGBODY >g> print "Emailed for site %s" %self.site >g> s.quit() >g> s.close() >g> a= time.time() >g> threads = [] >g> for site in listTo: >g> T = MyThread(site,FROM,MSGBODY) >g> threads.append(T) >g> T.start() >g> for i in threads: >g> i.join() >g> print "Took %s seconds" %str(time.time()-a) >g> #----------------------------------------------------- Tuesday this same program with some minor differences was posted by gganesh . There was a discussion about that. So did you copied that program with the suggested changes? I don't think it is nice to have two parallel discussions on the same subject. And copying without giving credits is not considered nice behaviour either. Usually it is called plagiarism. >g> The code Works fine ,but I doubt about the performance issue ,My >g> intention is to send mails concurrently to large number of mail. >g> 1.For every mail id i send It creates a new SMTP object,in case, if i >g> send to 1000 or more ids >g> a) It obliviously creates that much SMPT connections ,is this a >g> right approach . For such a big number of mails this is not the right approach. First your operating system may not like it to have such a large number of threads active. Second, the mail server probably doesn't like it that you make such a large number of connections simultaneously. If the mail server doesn't protest, probably its systems administrator will. Moreover there is probably no benefit in having 1000 connections open to the same mail server because long before there will be another bottleneck such as your network connection or the CPU load of either your computer or the mail server, unless you are on a very scalable infrastructure. A better solution will be to use a Thread Pool with a limited number of simultaneous threads (my guess is that 10-20 or so would be good enough). And as I said to my students yesterday: You shouldn't program multithreaded applications unless you have studied this subject thoroughly. Of course I don't know how this applies to you. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From OldGrantonian at googlemail.com Fri May 8 05:14:55 2009 From: OldGrantonian at googlemail.com (OldGrantonian) Date: Fri, 8 May 2009 02:14:55 -0700 (PDT) Subject: Which python version do I use with "virtualenv"? References: <61fc8aa3-764c-465b-86aa-1b25631609e4@r34g2000vba.googlegroups.com> <3f3497db-a8a3-4c79-92cc-2a65d99baf71@r34g2000vba.googlegroups.com> <2be30c71-4eb3-4ad7-a138-31ee0698a297@e20g2000vbc.googlegroups.com> Message-ID: On May 7, 9:33?pm, Duncan Booth wrote: > OldGrantonian wrote: > > Where do I find the win32 extensions? > > http://www.google.com/search?q=python+win32 > > Any of the first 4 hits should help. Success :) Many thanks to all responders in this thread :) From OldGrantonian at googlemail.com Fri May 8 05:15:18 2009 From: OldGrantonian at googlemail.com (OldGrantonian) Date: Fri, 8 May 2009 02:15:18 -0700 (PDT) Subject: Which python version do I use with "virtualenv"? References: <61fc8aa3-764c-465b-86aa-1b25631609e4@r34g2000vba.googlegroups.com> <3f3497db-a8a3-4c79-92cc-2a65d99baf71@r34g2000vba.googlegroups.com> <2be30c71-4eb3-4ad7-a138-31ee0698a297@e20g2000vbc.googlegroups.com> Message-ID: On May 7, 9:33?pm, Duncan Booth wrote: > OldGrantonian wrote: > > Where do I find the win32 extensions? > > http://www.google.com/search?q=python+win32 > > Any of the first 4 hits should help. Success :) Many thanks to all responders in this thread :) From alan.cameron at iname.com Fri May 8 05:16:55 2009 From: alan.cameron at iname.com (Alan Cameron) Date: Fri, 8 May 2009 10:16:55 +0100 Subject: Newcomer to Python tutorial question References: Message-ID: "Terry Reedy" wrote in message news:mailman.5248.1241732704.11746.python-list at python.org... > Alan Cameron wrote: >> >>>>> why is the printed result of >>>>> >>>>>>>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'} >>>>>>>> print(basket) >>>>> {'orange', 'banana', 'pear', 'apple'} >>>>> >>>>> in the sequence given? > >> It appears that I used a reserved term when I used 'sequence'. > > No and Sort-of. > > No: We often use it in the normal English sense of ordered items, as I and > I think others assume you did. Your question is quite legitimate, and the > answer, as indicated, is how an implementation interacts with the sequence > of additions. > > Sort-of: The library manual section of Sequence Types lists the sequence > operations common to all or most built-in Python sequence classes. But it > does not explicitly define sequence. Ranges, which are iterables that > directly support only indexing and len(), are called sequences. Dicts, > which are iterables that support len() but are usually not indexed by > integers, are not. So that suggests a minimal definition of sequence, but > all the other sequence classes support much more that is typically > assumed. > > Keywords are reserved terms in the language such as 'if' and 'None' that > are specially recognized by the parser and which affect compilation. > Identifiers of the form '__x...y__' are reserved names. Non-terminal > terms in the grammar are reserved terms, in a sense, within the reference > manual, but 'expression_list', not 'sequence', is used for comma-separated > sequences of expressions in code. The comma-separated sequence of items > in a function call is separately defined as an 'argument_list' because > 'keyword_item's like 'a=b' and '*' and '**' are not expressions and > because there are some order restrictions on argument items. > > Terry Jan Reedy > Thanks for the explanation. In particular reference to the tutorial section http://docs.python.org/3.0/tutorial/datastructures.html#nested-list-comprehensions There is a word which is ambiguous, at least to me. Perhaps you can explain the use of the word 'comprehensions'. Comprehension I understand Comprehensions I don't. Is there a glossary of terms somewhere? -- Alan Cameron From koranthala at gmail.com Fri May 8 05:26:36 2009 From: koranthala at gmail.com (koranthala) Date: Fri, 8 May 2009 02:26:36 -0700 (PDT) Subject: Creating temperory files for a web application Message-ID: <8ecb859e-9a35-4213-93d5-53833e5be7d7@f41g2000pra.googlegroups.com> Hi, I am doing web development using Django. I need to create an image (chart) and show it to the users - based on some data which user selects. My question is - how do I create a temporary image for the user? I thought of tempfile, but I think it will be deleted once the process is done - which would happen by the time user starts seeing the image. I can think of no other option other than to have another script which will delete all images based on time of creation. Since python is extensively used for web development, I guess this should be an usual scenario for many people here. How do you usually handle this? From bearophileHUGS at lycos.com Fri May 8 05:44:20 2009 From: bearophileHUGS at lycos.com (bearophileHUGS at lycos.com) Date: Fri, 8 May 2009 02:44:20 -0700 (PDT) Subject: heapq.merge with key= References: Message-ID: <5de878bd-52c4-4b2d-895b-c72f71519b7b@o14g2000vbo.googlegroups.com> Looking for this, Kevin D. Smith? http://code.activestate.com/recipes/502295/ Bye, bearophile From deets at nospam.web.de Fri May 8 05:49:41 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Fri, 08 May 2009 11:49:41 +0200 Subject: Creating temperory files for a web application References: <8ecb859e-9a35-4213-93d5-53833e5be7d7@f41g2000pra.googlegroups.com> Message-ID: <76ideeF1d1gbsU1@mid.uni-berlin.de> koranthala wrote: > Hi, > I am doing web development using Django. I need to create an image > (chart) and show it to the users - based on some data which user > selects. > My question is - how do I create a temporary image for the user? I > thought of tempfile, but I think it will be deleted once the process > is done - which would happen by the time user starts seeing the image. What makes you think that? You are the one creating it, you are responsible for deleting it. > I can think of no other option other than to have another script which > will delete all images based on time of creation. > Since python is extensively used for web development, I guess this > should be an usual scenario for many people here. How do you usually > handle this? There are various solutions - tempfiles, files based on a criteria (e.g. username and image-properties) so that they don't pollute the harddrive. For both approaches cleaning up as you suggest might be needed. Alternatively, if you use sessions, you can use memory-cached images. Or you can use the database. But the cleaning-up might get necessary here as well. The cleanest solution would be if the image would be rendered on the fly, based on GET-parameters (or REST-ful urls) so that you can render it into memory as string, but then forget immediately about it. Diez From bvidinli at gmail.com Fri May 8 06:38:25 2009 From: bvidinli at gmail.com (bvidinli) Date: Fri, 8 May 2009 13:38:25 +0300 Subject: php to python code converter Message-ID: <36e8a7020905080338p140a3945if3325c7a83368970@mail.gmail.com> if anybody needs: http://code.google.com/p/phppython/ From news.btinternet.com at erik.feegles.net Fri May 8 06:58:49 2009 From: news.btinternet.com at erik.feegles.net (Basil Brush) Date: Fri, 08 May 2009 11:58:49 +0100 Subject: Compiling snippets of python code Message-ID: I need some advice on compiling snippets of python source at runtime. So perhaps some python experts can point the way. I am rewriting an old Java app in python. It tries to find solutions to computational problems based on the idea of genetic evolution. The Java version just used an array of bytes for the genomes. These byte sequences were bytecode which told a rudimentary VM (virtual machine) which maths functions to call. It used another array as a small chunk of virtual memory for doing the calculations. Having seen the docs on python's compile module, I'm thinking of doing it differently in the new python version. My aim is to have the genomes made up of a sequence of python source snippets, with each snippet to be randomly generated, compiled, and executed at runtime. The advantage of this is that it gives me greater flexibility and I can change what actions the genomes can do without having to add new functions to my VM. But there's some crucial points. When the snippet is executed I would need to pass in a list or array, and I need the list back again with any changes that were made; this is the virtual memory and how I would get the results back, and is also how the snippet would get data from a previous snippet in a sequence. How would I do this? Also, there is a database of input data. Rather than pass all that data to the snippet I'd rather have functions in my module which the snippet could call to get data. So what's the scope of an executing piece of code? Can it access my modules and functions? My linux system has python 2.5 and I noticed that the docs say the compile module has been removed from python 3.0. So I'm concerned about whether my app would work in 3.0 if I upgraded. Does anyone know if they moved the compiler to another module or simply removed it entirely? From ldo at geek-central.gen.new_zealand Fri May 8 07:10:12 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Fri, 08 May 2009 23:10:12 +1200 Subject: IIR filter conversion routines for Python? References: Message-ID: In message , wzab wrote: > I'm looking for procedures converting the IIR filters into cascade and/ > or parallel forms. Tried a DSP textbook? From ldo at geek-central.gen.new_zealand Fri May 8 07:11:52 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Fri, 08 May 2009 23:11:52 +1200 Subject: SQL and CSV References: <1d2bb50f-2e3f-429b-aa98-7e1fe1f011b0@t10g2000vbg.googlegroups.com> Message-ID: In message , Peter Otten wrote: > While it may not matter here using placeholders instead of manually > escaping user-provided values is a good habit to get into. Until you hit things it can't deal with. From micpringle at gmail.com Fri May 8 07:39:38 2009 From: micpringle at gmail.com (Mic Pringle) Date: Fri, 8 May 2009 12:39:38 +0100 Subject: String.Template Source ... Message-ID: <51e08b630905080439u5ea51117j2dcaafe9b11eeb05@mail.gmail.com> Hi, I'd like to have a 'dig around' in the source for String.Template, but can't seem to find where it lives. Could someone please let me know where about's in the source (perhaps a path?) the template functions live ? I'm working on a Mac, using Leopard and the bundled version of Python. Thanks -Mic From koranthala at gmail.com Fri May 8 08:22:44 2009 From: koranthala at gmail.com (koranthala) Date: Fri, 8 May 2009 05:22:44 -0700 (PDT) Subject: Creating temperory files for a web application References: <8ecb859e-9a35-4213-93d5-53833e5be7d7@f41g2000pra.googlegroups.com> <76ideeF1d1gbsU1@mid.uni-berlin.de> Message-ID: <354db318-25b3-4af1-8fb1-cd86a0695637@x31g2000prc.googlegroups.com> On May 8, 2:49?pm, "Diez B. Roggisch" wrote: > koranthala wrote: > > Hi, > > ? ?I am doing web development using Django. I need to create an image > > (chart) and show it to the users - based on some data which user > > selects. > > ? ?My question is - how do I create a temporary image for the user? I > > thought of tempfile, but I think it will be deleted once the process > > is done - which would happen by the time user starts seeing the image. > > What makes you think that? You are the one creating it, you are responsible > for deleting it. > > > I can think of no other option other than to have another script which > > will delete all images based on time of creation. > > ? ?Since python is extensively used for web development, I guess this > > should be an usual scenario for many people here. How do you usually > > handle this? > > There are various solutions - tempfiles, files based on a criteria (e.g. > username and image-properties) so that they don't pollute the harddrive. > > For both approaches cleaning up as you suggest might be needed. > > Alternatively, if you use sessions, you can use memory-cached images. > > Or you can use the database. > > But the cleaning-up might get necessary here as well. > > The cleanest solution would be if the image would be rendered on the fly, > based on GET-parameters (or REST-ful urls) so that you can render it into > memory as string, but then forget immediately about it. > > Diez Thank you Diez. I would like to go ahead with the cleanest solution indeed. I am creating the image on the fly. But I could not understand what you meant by render to memory as a string. How do we send the image to the browser? Were you mentioning about having the image as a string and then passing to the browser based on data URL scheme ? Or is it something else like XBM format or something? I am sorry - but I am quite unsure of what you meant? I am actually creating PDFs, CSVs and images on the fly. So, I need to delete them all after that. The images can be somewhat large - ~2MB. The RFC mentions that large data cannot be sent that way. Is it that I will have to delete the images using a separate script later? From __peter__ at web.de Fri May 8 08:23:53 2009 From: __peter__ at web.de (Peter Otten) Date: Fri, 08 May 2009 14:23:53 +0200 Subject: String.Template Source ... References: Message-ID: Mic Pringle wrote: > I'd like to have a 'dig around' in the source for String.Template, but > can't seem to find where it lives. I assume that you mean string.Template. > Could someone please let me know where about's in the source (perhaps > a path?) the template functions live ? > > I'm working on a Mac, using Leopard and the bundled version of Python. Import the module and inspect its __file__ attribute: >>> import string >>> string.__file__ '/usr/lib/python2.6/string.pyc' This means that the string module's path is /usr/lib/python2.6/string.py over here. From micpringle at gmail.com Fri May 8 08:33:33 2009 From: micpringle at gmail.com (Mic Pringle) Date: Fri, 8 May 2009 13:33:33 +0100 Subject: String.Template Source ... In-Reply-To: References: Message-ID: <51e08b630905080533j6624dc8cy9886bbe49dc9d46a@mail.gmail.com> Brilliant, thanks Peter. -Mic 2009/5/8 Peter Otten <__peter__ at web.de>: > Mic Pringle wrote: > >> I'd like to have a 'dig around' in the source for String.Template, but >> can't seem to find where it lives. > > I assume that you mean string.Template. > >> Could someone please let me know where about's in the source (perhaps >> a path?) the template functions live ? >> >> I'm working on a Mac, using Leopard and the bundled version of Python. > > Import the module and inspect its __file__ attribute: > >>>> import string >>>> string.__file__ > '/usr/lib/python2.6/string.pyc' > > This means that the string module's path is /usr/lib/python2.6/string.py > over here. > > -- > http://mail.python.org/mailman/listinfo/python-list > From list at qtrac.plus.com Fri May 8 08:34:40 2009 From: list at qtrac.plus.com (Mark Summerfield) Date: Fri, 8 May 2009 05:34:40 -0700 (PDT) Subject: Public attributes with really private data References: <7b93110c-6000-4e71-8da9-ce6ea25c8a12@p4g2000vba.googlegroups.com> Message-ID: <5a532192-3e70-48d4-99b8-c8c75c3ef7cb@t10g2000vbg.googlegroups.com> On 8 May, 08:19, Peter Otten <__pete... at web.de> wrote: > MarkSummerfieldwrote: > > I had a quick search & didn't find anything _nice_ that produced > > attributes with really private data, so I came up with a possible > > solution---for Python 3. > > Do really you think what you suggest below is "nice"? Well the code isn't ugly and doesn't mess with the call stack etc. > By the way, your Attribute descriptor stores the value for all instances of > A in the same variable... It seems like it does, but it doesn't. The hidden_value is an instance variable that is created every time an Attribute object is created. >>> from Attribute import * >>> class A: a = Attribute("a", 5, lambda *a: True) b = Attribute("b", 5, lambda *a: True) >>> class B: a = Attribute("a", 5, lambda *a: True) b = Attribute("b", 5, lambda *a: True) >>> a = A() >>> b = B() >>> a.a,a.b,b.a,b.b (5, 5, 5, 5) >>> a.a=1;a.b=2;b.a=3;b.b=4 >>> a.a,a.b,b.a,b.b (1, 2, 3, 4) -- Mark Summerfield, Qtrac Ltd, www.qtrac.eu C++, Python, Qt, PyQt - training and consultancy "Programming in Python 3" - ISBN 0137129297 From koranthala at gmail.com Fri May 8 08:38:44 2009 From: koranthala at gmail.com (koranthala) Date: Fri, 8 May 2009 05:38:44 -0700 (PDT) Subject: Creating temperory files for a web application References: <8ecb859e-9a35-4213-93d5-53833e5be7d7@f41g2000pra.googlegroups.com> <76ideeF1d1gbsU1@mid.uni-berlin.de> <354db318-25b3-4af1-8fb1-cd86a0695637@x31g2000prc.googlegroups.com> Message-ID: <275bb932-644b-4f64-80e8-7061ec7b2dff@a5g2000pre.googlegroups.com> On May 8, 5:22?pm, koranthala wrote: > On May 8, 2:49?pm, "Diez B. Roggisch" wrote: > > > > > koranthala wrote: > > > Hi, > > > ? ?I am doing web development using Django. I need to create an image > > > (chart) and show it to the users - based on some data which user > > > selects. > > > ? ?My question is - how do I create a temporary image for the user? I > > > thought of tempfile, but I think it will be deleted once the process > > > is done - which would happen by the time user starts seeing the image. > > > What makes you think that? You are the one creating it, you are responsible > > for deleting it. > > > > I can think of no other option other than to have another script which > > > will delete all images based on time of creation. > > > ? ?Since python is extensively used for web development, I guess this > > > should be an usual scenario for many people here. How do you usually > > > handle this? > > > There are various solutions - tempfiles, files based on a criteria (e.g. > > username and image-properties) so that they don't pollute the harddrive. > > > For both approaches cleaning up as you suggest might be needed. > > > Alternatively, if you use sessions, you can use memory-cached images. > > > Or you can use the database. > > > But the cleaning-up might get necessary here as well. > > > The cleanest solution would be if the image would be rendered on the fly, > > based on GET-parameters (or REST-ful urls) so that you can render it into > > memory as string, but then forget immediately about it. > > > Diez > > Thank you Diez. > I would like to go ahead with the cleanest solution indeed. > I am creating the image on the fly. But I could not understand what > you meant by render to memory as a string. > How do we send the image to the browser? > > Were you mentioning about having the image as a string and then > passing to the browser based on data URL scheme ? > Or is it something else like XBM format or something? I am sorry - but > I am quite unsure of what you meant? > > I am actually creating PDFs, CSVs and images on the fly. So, I need to > delete them all after that. > The images can be somewhat large - ~2MB. The RFC mentions that large > data cannot be sent that way. > > Is it that I will have to delete the images using a separate script > later? Hi Diez, I think I understood your point now. Is it ? (1) Have a separate URL for the image - update urls.py for that (2) Pass all the GET parameters to that URL again. (3) Recalculate the fields again in that URL (4) Create the image and send back as image/png based on the received fields. Other than the fact that I will be hitting the DB twice - for the fields in the original URL and for creating Image in the second URL, I think this is the best option. Please let me know whether I have understood correctly. The same option can be done for CSV and PDF files too. Thank you very much Diez. From list at qtrac.plus.com Fri May 8 08:44:19 2009 From: list at qtrac.plus.com (Mark Summerfield) Date: Fri, 8 May 2009 05:44:19 -0700 (PDT) Subject: Public attributes with really private data References: <7b93110c-6000-4e71-8da9-ce6ea25c8a12@p4g2000vba.googlegroups.com> Message-ID: <450ca2c0-1982-4554-8e28-1490c6c06510@q2g2000vbr.googlegroups.com> On 8 May, 09:52, Carl Banks wrote: > On May 7, 11:37?pm, MarkSummerfield wrote: > > > > > Hi, > > > I had a quick search & didn't find anything _nice_ that produced > > attributes with really private data, so I came up with a possible > > solution---for Python 3. > > (For Python 2 there does seem to be an approach although I'm not > > keen on it:http://www.builderau.com.au/blogs/byteclub/viewblogpost.htm?p=339270875 > > ) > > > Here's a standard class with one read-only and one writable > > property that has a tiny bit of validation. > > > class P: > > ? ? def __init__(self, w): > > ? ? ? ? self.w = w > > ? ? @property > > ? ? def r(self): return 5 > > ? ? @property > > ? ? def w(self): return self.__w > > ? ? @w.setter > > ? ? def w(self, value): > > ? ? ? ? if value > 0: # Only +ve values allowed > > ? ? ? ? ? ? self.__w = value > > ? ? ? ? else: > > ? ? ? ? ? ? raise ValueError("'{0}' is not valid for w".format(value)) > > > The read-only property is completely private because it isn't > > actually stored as such. > > > But if we do dir() on an instance, in addition to 'r' and 'w', we > > also have '_P__w'. So the writable property's data is easily > > accessible, and the validation can be got around: > > > >>> p = P(9) > > >>> p.r, p.w > > (5, 9) > > >>> p.w = 43 > > >>> p.r, p.w > > (5, 43) > > >>> p.w = -7 > > > Traceback (most recent call last): > > ... > > ValueError: '-7' is not valid for w>>> p._P__w = -7 > > >>> p.r, p.w > > > (5, -7) > > > Here's a class where I can't think of a way to access the private > > data and set invalid values. > > > class A: > > ? ? r = Attribute("r", 5) > > ? ? w = Attribute("w", None, lambda self, value: value > 0) > > ? ? def __init__(self, w): > > ? ? ? ? self.w = w > > > The Attribute class is a descriptor that takes three arguments: > > name of attribute, initial value (essential for read-only > > attributes!), and a validator function (which could be "lambda > > *a: True" if any value is accepatble). > > > >>> a = A(9) > > >>> a.r, a.w > > (5, 9) > > >>> a.w = 43 > > >>> a.r, a.w > > (5, 43) > > >>> a.w = -7 > > > Traceback (most recent call last): > > ... > > ValueError: '-7' is not valid for w > > > If we do a dir(a) the only attributes we get (beyond those from > > object) are 'r' and 'w', so it shouldn't be possible to get > > around the validation---at least not easily. > > Ok, I'll bite. > > A.w = -7 > > A.__class__ = A_without_validation > a.w = -7 > > Attribute.__set__ = function_that_ignores_validation > a.w = -7 I don't see how that can work? Sure you could replace the __set__ method, or the Attribute.__setter method. But neither can actually change the hidden_value because AFAIK that only exists within the scope of a closure and so isn't accesssible. (Well, maybe if you started using the inspect module you'd find it.) > I am not opposed to adding a little well-considered protection to some > attributes where mistakes are prone to happen and/or dangerous, but it > is futile to try to stop access entirely. ?There's just too many back > doors. Sure, but I like the fact that there is no "shadow" attribute, so just "w" not "w" _and_ "_A__w". > I would suggest that there really isn't much point in anything more > complex than your first solution, which was to validate with > properties and to store the value in a separate attribute. > Respectable programmers won't lightly bypass your validation if they > see that you've taken steps to enforce it. ?OTOH, once a programmer, > respectable or not, decides to override your protection, they are not > likely to be stopped by something more complex. ?So there is no point > in making it more complex. > > Carl Banks I think that's a fair viewpoint, but I still like the idea of there being no _A__w available for property w. -- Mark Summerfield, Qtrac Ltd, www.qtrac.eu C++, Python, Qt, PyQt - training and consultancy "Programming in Python 3" - ISBN 0137129297 From andrew at acooke.org Fri May 8 08:49:07 2009 From: andrew at acooke.org (andrew cooke) Date: Fri, 8 May 2009 08:49:07 -0400 (CLT) Subject: SQL and CSV In-Reply-To: References: <1d2bb50f-2e3f-429b-aa98-7e1fe1f011b0@t10g2000vbg.googlegroups.com> Message-ID: <0631a4e5e48a064640dadd6e05f783cd.squirrel@acooke.dyndns.org> Lawrence D'Oliveiro wrote: > In message , Peter Otten wrote: > >> While it may not matter here using placeholders instead of manually >> escaping user-provided values is a good habit to get into. > > Until you hit things it can't deal with. The post you are replying to was talking about using the SQL library's "?" syntax that automatically escapes values. The usual reason this is recommended (if I have understood correctly) is that the library code is much more likely to foil injection attacks. I have seen this mentioned often and assume it is good advice. Can you expand on your comment? I assume you are thinking of how the library might handle some strange class. But aren't the number of types limited by SQL? In which case a "thing that can't be handled" could presumably be managed by adding an appropriate __str__ or __float__ or whatever? And you would still use the library to give safety with other values. Maybe you could give an example of the kind of problem you're thinking of? Thanks, Andrew From list at qtrac.plus.com Fri May 8 08:51:24 2009 From: list at qtrac.plus.com (Mark Summerfield) Date: Fri, 8 May 2009 05:51:24 -0700 (PDT) Subject: Public attributes with really private data References: <7b93110c-6000-4e71-8da9-ce6ea25c8a12@p4g2000vba.googlegroups.com> Message-ID: <423ca62c-094d-42ca-9257-cf36b5d38e1d@p4g2000vba.googlegroups.com> [snip] > By the way, your Attribute descriptor stores the value for all instances of > A in the same variable... > > Peter You're right. From chambon.pascal at wanadoo.fr Fri May 8 08:51:48 2009 From: chambon.pascal at wanadoo.fr (Pascal Chambon) Date: Fri, 08 May 2009 14:51:48 +0200 Subject: Which one is best Python or Java for developing GUI applications? Message-ID: <4A042AE4.5030601@wanadoo.fr> On Thu, May 7, 2009 at 1:42 PM, Pascal Chambon > wrote: Hello When a lot of code using wxwidgets is in place, it's sure that moving to qt is a big task ; even though, thanks to qt's GUI designer, it's possible to quickly reproduce the structure of the wxwidget application with QT widgets. If you want to see the power of Qt, I highly advice you to browse the very nice "demos" included in both the Qt and PyQt packages - those are Gui applications that let you watch all kind of abilities very fastly. Also, something that wxwidgets will surely never be able to do : http://labs.trolltech.com/blogs/2008/12/02/widgets-enter-the-third-dimension-wolfenqt/ The 2 products (and the Gui designer, docs and demos) can be downloaded from these pages : http://www.qtsoftware.com/products/ http://www.riverbankcomputing.co.uk/software/pyqt/download And if you have some time for reading : http://www.qtsoftware.com/files/pdf/qt-4.4-whitepaper http://doc.trolltech.com/4.5/index.html Good time with all that, regards, Pascal Qijing Li a ?crit : > Thank you for sharing this information. > > I started to use wxPython two years ago, which fit my needy very > well because > the jobs I worked on didn't focus on GUI. But now , the project I > am working on > involves much drawing shapes and looking, most of wxPython works > for me, > but one thing, as you mentioned, transparency drove me nuts. > > wxPython suppose transparent window, but not transparent > background that is right what I need. I did research a lot and was > trying to find a proper way to get it, it turns out that I found > two tricky way, one is to use wx.EVT_ERASE_BACKGROUND tricky, the > other is to copy the image of background under the window as the > background image. Although the problem is solved, I feel > uncomfortable about this. > I hope wxPython supports real transparency some day. > > Recently, I have no plan to transmit to other frameworks, such as > PyQt. > I'm really interested in what is differences between them, I'll > check it. > Is there demo of PyQt ? or could you give me some links if they > are in the bag. > > Have a good day! > Miles > > > > > > > > On Tue, May 5, 2009 at 11:42 AM, Pascal Chambon > > wrote: > > > The fact that Python is a dynamic language offers, in my > opinion, a huge advantage to quickly setup a GUI, without > caring about the infinite details of the variable types and > function signatures. > Its good handling of "function as first-class objects" is also > precious when comes the time of setting callbacks (I can't > bear anymore the way Swing does it, with interfaces etc.) > > But much depends on the framework used, too. I've used > wxPython for a multimedia project, and actually it lacked a > lot of necessary features (transparency, event loop tuning, > multithreading support...), but that was 1 year ago, maybe > things have changed. > Anyway, I'd advocate the use of PyQt, which really offers > tremendous possibilities - if your application isn't a simple > office application, its' really worth turning towards pyqt. > > Regards, > Pascal > > > > Leon a ?crit : >> I think there are two advantages over java for GUI application >> >> First, python is more productive and has very rich third modules >> support, >> you can check the demo of wxPython. >> >> Second, you can develop native-looking GUI >> >> BTW: I'm developing GUI application using python and wxPython. >> >> >> >> Second, >> On May 4, 11:41 pm, srinivasan srinivas >> wrote: >> >>> Could you tell me does Python have any advantages over Java for the development of GUI applications? >>> >>> Thanks, >>> Srini >>> >>> Now surf faster and smarter ! Check out the new Firefox 3 - Yahoo! Editionhttp://downloads.yahoo.com/in/firefox/?fr=om_email_firefox >>> >> -- >> http://mail.python.org/mailman/listinfo/python-list >> >> >> > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Fri May 8 08:56:53 2009 From: __peter__ at web.de (Peter Otten) Date: Fri, 08 May 2009 14:56:53 +0200 Subject: Public attributes with really private data References: <7b93110c-6000-4e71-8da9-ce6ea25c8a12@p4g2000vba.googlegroups.com> <5a532192-3e70-48d4-99b8-c8c75c3ef7cb@t10g2000vbg.googlegroups.com> Message-ID: Mark Summerfield wrote: > On 8 May, 08:19, Peter Otten <__pete... at web.de> wrote: >> MarkSummerfieldwrote: >> > I had a quick search & didn't find anything _nice_ that produced >> > attributes with really private data, so I came up with a possible >> > solution---for Python 3. >> >> Do really you think what you suggest below is "nice"? > > Well the code isn't ugly and doesn't mess with the call stack etc. > >> By the way, your Attribute descriptor stores the value for all instances >> of A in the same variable... > > It seems like it does, but it doesn't. The hidden_value is an instance > variable that is created every time an Attribute object is created. > >>>> from Attribute import * >>>> class A: > a = Attribute("a", 5, lambda *a: True) > b = Attribute("b", 5, lambda *a: True) >>>> class B: > a = Attribute("a", 5, lambda *a: True) > b = Attribute("b", 5, lambda *a: True) >>>> a = A() >>>> b = B() >>>> a.a,a.b,b.a,b.b > (5, 5, 5, 5) >>>> a.a=1;a.b=2;b.a=3;b.b=4 >>>> a.a,a.b,b.a,b.b > (1, 2, 3, 4) But attribute values are shared between all instances of the same class: >>> class A: ... x = Attribute("x", 42, lambda *a: True) ... >>> a = A() >>> b = A() >>> a.x, b.x (42, 42) >>> a.x = "yadda" >>> a.x, b.x ('yadda', 'yadda') Peter From pruebauno at latinmail.com Fri May 8 09:06:43 2009 From: pruebauno at latinmail.com (pruebauno at latinmail.com) Date: Fri, 8 May 2009 06:06:43 -0700 (PDT) Subject: Python 3.1 beta 1 References: Message-ID: <8f1780d2-96c5-4292-b4eb-64449000d9b3@t11g2000vbc.googlegroups.com> On May 7, 6:33?pm, Benjamin Peterson wrote: > ? latinmail.com> writes: > > > Congratulations! > > Thanks! > > > > > Is it just me or was some nice summary output added to the make > > process? I get a nice list of modules that didn't compile and the ones > > where the library could not be found. > > Are you compiling on a different platform? The nice output has been around for a > while, bu only on non-Windows platforms. Not really, I was on AIX. It is probably just me then, probably because for the first time I see it making it through the whole process even with the Tkinter libraries missing. From list at qtrac.plus.com Fri May 8 09:17:28 2009 From: list at qtrac.plus.com (Mark Summerfield) Date: Fri, 8 May 2009 06:17:28 -0700 (PDT) Subject: Public attributes with really private data References: <7b93110c-6000-4e71-8da9-ce6ea25c8a12@p4g2000vba.googlegroups.com> <5a532192-3e70-48d4-99b8-c8c75c3ef7cb@t10g2000vbg.googlegroups.com> Message-ID: <0096da6a-38a4-405a-894e-046ec69f1d50@b1g2000vbc.googlegroups.com> On 8 May, 13:56, Peter Otten <__pete... at web.de> wrote: > MarkSummerfieldwrote: > > On 8 May, 08:19, Peter Otten <__pete... at web.de> wrote: > >> MarkSummerfieldwrote: > >> > I had a quick search & didn't find anything _nice_ that produced > >> > attributes with really private data, so I came up with a possible > >> > solution---for Python 3. > > >> Do really you think what you suggest below is "nice"? > > > Well the code isn't ugly and doesn't mess with the call stack etc. > > >> By the way, your Attribute descriptor stores the value for all instances > >> of A in the same variable... > > > It seems like it does, but it doesn't. The hidden_value is an instance > > variable that is created every time an Attribute object is created. > > >>>> from Attribute import * > >>>> class A: > > a = Attribute("a", 5, lambda *a: True) > > b = Attribute("b", 5, lambda *a: True) > >>>> class B: > > a = Attribute("a", 5, lambda *a: True) > > b = Attribute("b", 5, lambda *a: True) > >>>> a = A() > >>>> b = B() > >>>> a.a,a.b,b.a,b.b > > (5, 5, 5, 5) > >>>> a.a=1;a.b=2;b.a=3;b.b=4 > >>>> a.a,a.b,b.a,b.b > > (1, 2, 3, 4) > > But attribute values are shared between all instances of the same class: > > >>> class A: > > ... ? ? x = Attribute("x", 42, lambda *a: True) > ...>>> a = A() > >>> b = A() > >>> a.x, b.x > (42, 42) > >>> a.x = "yadda" > >>> a.x, b.x > > ('yadda', 'yadda') > > Peter Yes. I did think of trying to create the closures dynamically the first time the getter or setter was called---but that _is_ getting ugly, so I'll give it up:-) Thanks! -- Mark Summerfield, Qtrac Ltd, www.qtrac.eu C++, Python, Qt, PyQt - training and consultancy "Programming in Python 3" - ISBN 0137129297 From pruebauno at latinmail.com Fri May 8 09:20:37 2009 From: pruebauno at latinmail.com (pruebauno at latinmail.com) Date: Fri, 8 May 2009 06:20:37 -0700 (PDT) Subject: Python 2.6, File read() problems in Windows Xp References: <11aabe3b0905072204w499679f8q9d1be6d63cddd597@mail.gmail.com> <4A03DE84.40406@ieee.org> Message-ID: On May 8, 5:08?am, Li Wang wrote: > Hi ?Dave: > Thank you very much for you explanation:) > > > Chances are you forgot the "b" parameter to open(). ?Unnecessary in Unix, it > > tells the library to *not* translate \r\n ?to \n upon read, or the inverse > > on write. ?In other words, with the "b" parameter, the file is read in > > unchanged. > > So, if I am using python in Linux, do open('file', 'r') and > open('file', 'rb') work the same way? > > Thanks, > > Best regards, > Li In old Python up to 2.6, YES. From p.f.moore at gmail.com Fri May 8 09:21:30 2009 From: p.f.moore at gmail.com (Paul Moore) Date: Fri, 8 May 2009 06:21:30 -0700 (PDT) Subject: Tutorial or example use for python-graph library Message-ID: <030fcef4-2bf5-4194-97f2-78ce95ca4ef5@v17g2000vbb.googlegroups.com> I have just discovered the python-graph library. I've been interested in graph algorithms for a long time, so I'd like to give this a try. But there seems to be very little in the way of examples, or tutorial documentation available. There's the API documentation, but that's not really narrative form. And there are some examples in the distribution, but these seem to be essentially artificial examples, rather than real-world code (and they don't have much in the way of documentation!) Are there any reasonably-sized tutorials, or examples of common real- world code, using this package? My google-fu wasn't strong enough to find anything directly :-( Thanks, Paul From Scott.Daniels at Acm.Org Fri May 8 09:32:56 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Fri, 08 May 2009 06:32:56 -0700 Subject: Python 2.6, File read() problems in Windows Xp In-Reply-To: References: <11aabe3b0905072204w499679f8q9d1be6d63cddd597@mail.gmail.com> <4A03DE84.40406@ieee.org> Message-ID: <8YudnbqqeKjLrpnXnZ2dnUVZ_hidnZ2d@pdx.net> Li Wang wrote: > So, if I am using python in Linux, do open('file', 'r') and > open('file', 'rb') work the same way? You get identical results, but you ar lying to the reader of your code. you should include the 'b' if what you want is bytes (or octets if you prefer), and not use it if what you expect is "text." Your code becomes less confusing by using 'b' properly, even if you see no particular difference. --Scott David Daniels Scott.Daniels at Acm.Org From li.wang.d at gmail.com Fri May 8 09:47:23 2009 From: li.wang.d at gmail.com (Li Wang) Date: Fri, 8 May 2009 23:47:23 +1000 Subject: Python 2.6, File read() problems in Windows Xp In-Reply-To: <8YudnbqqeKjLrpnXnZ2dnUVZ_hidnZ2d@pdx.net> References: <11aabe3b0905072204w499679f8q9d1be6d63cddd597@mail.gmail.com> <4A03DE84.40406@ieee.org> <8YudnbqqeKjLrpnXnZ2dnUVZ_hidnZ2d@pdx.net> Message-ID: <11aabe3b0905080647h12c5553bt7f96515ecec79ddf@mail.gmail.com> 2009/5/8 Scott David Daniels : > Li Wang wrote: >> >> So, if I am using python in Linux, do open('file', 'r') and >> open('file', 'rb') work the same way? > > You get identical results, but you ar lying to the reader of your code. > you should include the 'b' if what you want is bytes (or octets if you > prefer), and not use it if what you expect is "text." Your code > becomes less confusing by using 'b' properly, even if you see no > particular difference. > Thanks a lot, very helpful:D > -- Li ------ Time is all we have and you may find one day you have less than you think From li.wang.d at gmail.com Fri May 8 09:47:51 2009 From: li.wang.d at gmail.com (Li Wang) Date: Fri, 8 May 2009 23:47:51 +1000 Subject: Python 2.6, File read() problems in Windows Xp In-Reply-To: References: <11aabe3b0905072204w499679f8q9d1be6d63cddd597@mail.gmail.com> <4A03DE84.40406@ieee.org> Message-ID: <11aabe3b0905080647s64bcea1ao521f312f9609c502@mail.gmail.com> 2009/5/8 : > On May 8, 5:08 am, Li Wang wrote: >> Hi Dave: >> Thank you very much for you explanation:) >> >> > Chances are you forgot the "b" parameter to open(). Unnecessary in Unix, it >> > tells the library to *not* translate \r\n to \n upon read, or the inverse >> > on write. In other words, with the "b" parameter, the file is read in >> > unchanged. >> >> So, if I am using python in Linux, do open('file', 'r') and >> open('file', 'rb') work the same way? >> >> Thanks, >> >> Best regards, >> Li > > In old Python up to 2.6, YES. Thank you:D -- Li ------ Time is all we have and you may find one day you have less than you think From emreguney at gmail.com Fri May 8 10:19:45 2009 From: emreguney at gmail.com (e.g. emre) Date: Fri, 8 May 2009 07:19:45 -0700 (PDT) Subject: Tutorial or example use for python-graph library References: <030fcef4-2bf5-4194-97f2-78ce95ca4ef5@v17g2000vbb.googlegroups.com> Message-ID: <8f032f4b-ae7a-45a0-baeb-82005229be22@n4g2000vba.googlegroups.com> On May 8, 3:21?pm, Paul Moore wrote: > I have just discovered the python-graph library. I've been interested > in graph algorithms for a long time, so I'd like to give this a try. > But there seems to be very little in the way of examples, or tutorial > documentation available. There's the API documentation, but that's not > really narrative form. And there are some examples in the > distribution, but these seem to be essentially artificial examples, > rather than real-world code (and they don't have much in the way of > documentation!) > > Are there any reasonably-sized tutorials, or examples of common real- > world code, using this package? My google-fu wasn't strong enough to > find anything directly :-( > > Thanks, > Paul Hi, you might want to check networkx as well, it is considerably well documented: http://networkx.lanl.gov/ Emre From james at agentultra.com Fri May 8 10:21:12 2009 From: james at agentultra.com (J Kenneth King) Date: Fri, 08 May 2009 10:21:12 -0400 Subject: list comprehension question References: <6d502cfd-e252-4722-96a5-c52fc3579499@v1g2000prd.googlegroups.com> <85ocucnbce.fsf@agentultra.com> <85ljpb8r1o.fsf@agentultra.com> <85d4am8hq4.fsf@agentultra.com> <8563gcx1p1.fsf@agentultra.com> Message-ID: <854ovv7k13.fsf@agentultra.com> Terry Reedy writes: > J Kenneth King wrote: >> >> Keep in mind that nested comprehensions are still available because >> they do have a use case that justifies their existence. > > Nested comprehensions are available because because the syntax makes > them available by default and making a fiddly exception would be > contrary to Python's style. A list comp creates an iterable. A list > comp requires use of an iterable. Therefore, a list comp may use a > list comp. > >> However, I >> think the Python documentation warns against their use because people >> might rely on them for problems where they aren't necessary and since >> they are difficult to read... it can lead to difficult to read code. > > Whenever the expression that results in the iterable used by a list > comp is sufficiently complex, readability is improved by pulling it > out as a separate statement. Nested list comps are often examplex of > such sufficiently complex expressions, but not the only possible one. > > tjr Agreed. A very succinct explanation of the point I was trying to make. From nickle at gmail.com Fri May 8 10:22:02 2009 From: nickle at gmail.com (Nick) Date: Fri, 8 May 2009 07:22:02 -0700 (PDT) Subject: SQL and CSV References: <1d2bb50f-2e3f-429b-aa98-7e1fe1f011b0@t10g2000vbg.googlegroups.com> Message-ID: On May 8, 1:49?pm, "andrew cooke" wrote: > Lawrence D'Oliveiro wrote: > > In message , Peter Otten wrote: > > >> While it may not matter here using placeholders instead of manually > >> escaping user-provided values is a good habit to get into. > > > Until you hit things it can't deal with. > > The post you are replying to was talking about using the SQL library's "?" > syntax that automatically escapes values. ?The usual reason this is > recommended (if I have understood correctly) is that the library code is > much more likely to foil injection attacks. ?I have seen this mentioned > often and assume it is good advice. > > Can you expand on your comment? ?I assume you are thinking of how the > library might handle some strange class. ?But aren't the number of types > limited by SQL? ?In which case a "thing that can't be handled" could > presumably be managed by adding an appropriate __str__ or __float__ or > whatever? ?And you would still use the library to give safety with other > values. > > Maybe you could give an example of the kind of problem you're thinking of? > > Thanks, > Andrew Injection attacks aren't an issue, its a local app. It's part of a reconciliation system, where sometimes data is in csv files. If you want the whole csv file, you can use csv module without a problem. In some cases, I need to manipulate the data. The choices are hard code the manipulation, or load the data from a config file. So what I've got is the query in the config and I can process it. Nick From Scott.Daniels at Acm.Org Fri May 8 10:31:12 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Fri, 08 May 2009 07:31:12 -0700 Subject: Compiling snippets of python code In-Reply-To: References: Message-ID: <3pmdnQdqr4ic3JnXnZ2dnUVZ_jWdnZ2d@pdx.net> Basil Brush wrote: > I need some advice on compiling snippets of python source at runtime. So > perhaps some python experts can point the way. > > I am rewriting an old Java app in python. It tries to find solutions to > computational problems based on the idea of genetic evolution. The Java > version just used an array of bytes for the genomes. These byte > sequences were bytecode which told a rudimentary VM (virtual machine) > which maths functions to call. It used another array as a small chunk of > virtual memory for doing the calculations. Unlike Java and C++, functions and methods are first class. You can make lists and dictionaries containing functions, and that would be the normal Python way of handling this, rather than generating source from snippets. You should take a look at Numpy if the "maths functions" are operating on large chunks of data (fft, matrix operations, and the like). If the operations are smallish things, pure Python is likely the way to go. I don't understand what the "other array as a small chunk of virtual memory" might be; my first take is YAGNI. > Having seen the docs on python's compile module, I'm thinking of doing > it differently in the new python version. My aim is to have the genomes > made up of a sequence of python source snippets, with each snippet to be > randomly generated, compiled, and executed at runtime. The advantage of > this is that it gives me greater flexibility and I can change what > actions the genomes can do without having to add new functions to my VM. > > But there's some crucial points. When the snippet is executed I would > need to pass in a list or array, and I need the list back again with any > changes that were made; this is the virtual memory and how I would get > the results back, and is also how the snippet would get data from a > previous snippet in a sequence. Here is a toy-like version of what you might want to do: # Some operations: def double(vec): return [x * 2 for x in vec] def alternate(vec): '''An "unshuffling" step''' return vec[::2] + vec[1::2] def halve(vec): return [x / 2 for x in vec] # Some execution lists: processing = [double, alternate, halve, alternate] new_processing = alternate(processing) # you can shuffle any list assert new_processing == [double, halve, alternate, alternate] def perform(processes, data): for step in processes: data = step(data) return data start = [x * x for x in range(10)] assert perform(processing, start) == perform(new_processing, start) print start mid = perform(processing, start) print mid print perform(processing, mid) print perform(processing * 2, start) prints: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] [0, 16, 64, 9, 49, 4, 36, 1, 25, 81] [0, 49, 25, 9, 1, 64, 36, 16, 4, 81] [0, 49, 25, 9, 1, 64, 36, 16, 4, 81] How would I do this? Also, there is a > database of input data. Rather than pass all that data to the snippet > I'd rather have functions in my module which the snippet could call to > get data. So what's the scope of an executing piece of code? Can it > access my modules and functions? First, let go of managing the data allocation. Second, realize passing a list or array is simple and cheap, and _not_ proportional to the size of the list or array. You are passing the actual object. You really need to run through the tutorial; these questions will be answered there. Sit down with the tutorial and the interpretter, executing anything you don't quite understand. I did it in IDLE, but you may prefer straight command line. > My linux system has python 2.5 and I noticed that the docs say the > compile module has been removed from python 3.0. So I'm concerned about > whether my app would work in 3.0 if I upgraded. Does anyone know if they > moved the compiler to another module or simply removed it entirely? Although I don't think you should be working with the compiler, if you want to use something like that, you'd be better advised to use the "ast" module first added in 2.6. But for now, don't borrow trouble you don't need. Rethink your problem and write it in Python. Don't try to do a line-by-line translation, you'll just have a slow awkward Python program with a Java heart. --Scott David Daniels Scott.Daniels at Acm.Org From apt.shansen at gmail.com Fri May 8 10:36:52 2009 From: apt.shansen at gmail.com (Stephen Hansen) Date: Fri, 8 May 2009 07:36:52 -0700 Subject: Creating temperory files for a web application In-Reply-To: <354db318-25b3-4af1-8fb1-cd86a0695637@x31g2000prc.googlegroups.com> References: <8ecb859e-9a35-4213-93d5-53833e5be7d7@f41g2000pra.googlegroups.com> <76ideeF1d1gbsU1@mid.uni-berlin.de> <354db318-25b3-4af1-8fb1-cd86a0695637@x31g2000prc.googlegroups.com> Message-ID: <7a9c25c20905080736t5124faedue1a2dde40a41e28f@mail.gmail.com> > > > Thank you Diez. > I would like to go ahead with the cleanest solution indeed. > I am creating the image on the fly. But I could not understand what > you meant by render to memory as a string. > How do we send the image to the browser? > > Were you mentioning about having the image as a string and then > passing to the browser based on data URL scheme ? > Or is it something else like XBM format or something? I am sorry - but > I am quite unsure of what you meant? > > I am actually creating PDFs, CSVs and images on the fly. So, I need to > delete them all after that. > The images can be somewhat large - ~2MB. The RFC mentions that large > data cannot be sent that way. > The question is, how are you generating these images? What you consider a "file" and what a web server considers a "file" is really meaningless actually. To a web browser, all a "file" is is a stream of content with a certain content-type which it uses to determine how to treat it. This isn't about the RFC2397. The web browser has no idea if the content its fetching is from a file or a database or generated on the fly-- its all the same thing. There's a URL which returns data, when requested. That data might be a HTML file (as determined by its Content-type), in which case the browser renders it. Or it might be a JPG, in which case it displays it. Or it might be a zip file in which case it probably asks you where you want to save it. Depending on how exactly you're actually "generating" the image, its quite possible you don't need to use a real file at all. You can use a file-like object, such as (c)StringIO and pass it to your image generation routines which will generate the entire image in memory. You can write to this file-like object as if it were a real file on the disk somewhere, seek, scan, pass it to most libraries that need file objects.... and then when done, you can return its contents... all without a -real- file on disk ever being touched. Like so vaguely: f = cStringIO.StringIO() my_library_which_makes_images(f) f.seek(0) return f.read() Exactly how you indicate what the Content-type will be and do that will depend on what web framework you're using. For example, on one app I have -- I'm using TurboGears -- I do something like this: @expose(format="image/jpeg", content_type="image/jpeg") def getJPEG(self, element_number): element = self._resourceManager.getElement(element_number) if element: return element.data return '' Now the specifics of what's going on aren't important. But I'm connecting to a remote system, and getting an object which contains the entire contents of a JPEG in a single string. So the entire file is in memory. I could then tweak or manipulate it with PIL to change that file all without any filesystem access, working entirely on file-like objects in memory, then return the contents. --S -------------- next part -------------- An HTML attachment was scrubbed... URL: From Shawn at Milochik.com Fri May 8 10:43:08 2009 From: Shawn at Milochik.com (Shawn Milochik) Date: Fri, 8 May 2009 10:43:08 -0400 Subject: OT (humor): 'import antigravity' in action! Message-ID: <2dc0c81b0905080743o334a04efg9e029f346b5b0795@mail.gmail.com> I know you've probably all seen this 50 times, but just in case: http://xkcd.com/353/ And here's the result: http://icanhascheezburger.com/2009/05/06/funny-pictures-behavior-20/ From list at qtrac.plus.com Fri May 8 10:48:59 2009 From: list at qtrac.plus.com (Mark Summerfield) Date: Fri, 8 May 2009 07:48:59 -0700 (PDT) Subject: Public attributes with really private data References: <7b93110c-6000-4e71-8da9-ce6ea25c8a12@p4g2000vba.googlegroups.com> <5a532192-3e70-48d4-99b8-c8c75c3ef7cb@t10g2000vbg.googlegroups.com> Message-ID: On 8 May, 13:56, Peter Otten <__pete... at web.de> wrote: > Mark Summerfield wrote: > > On 8 May, 08:19, Peter Otten <__pete... at web.de> wrote: > >> MarkSummerfieldwrote: > >> > I had a quick search & didn't find anything _nice_ that produced > >> > attributes with really private data, so I came up with a possible > >> > solution---for Python 3. > > >> Do really you think what you suggest below is "nice"? > > > Well the code isn't ugly and doesn't mess with the call stack etc. > > >> By the way, your Attribute descriptor stores the value for all instances > >> of A in the same variable... > > > It seems like it does, but it doesn't. The hidden_value is an instance > > variable that is created every time an Attribute object is created. > > >>>> from Attribute import * > >>>> class A: > > a = Attribute("a", 5, lambda *a: True) > > b = Attribute("b", 5, lambda *a: True) > >>>> class B: > > a = Attribute("a", 5, lambda *a: True) > > b = Attribute("b", 5, lambda *a: True) > >>>> a = A() > >>>> b = B() > >>>> a.a,a.b,b.a,b.b > > (5, 5, 5, 5) > >>>> a.a=1;a.b=2;b.a=3;b.b=4 > >>>> a.a,a.b,b.a,b.b > > (1, 2, 3, 4) > > But attribute values are shared between all instances of the same class: > > >>> class A: > > ... ? ? x = Attribute("x", 42, lambda *a: True) > ...>>> a = A() > >>> b = A() > >>> a.x, b.x > (42, 42) > >>> a.x = "yadda" > >>> a.x, b.x > > ('yadda', 'yadda') > > Peter OK, I couldn't quite give it up. But the solution isn't nice or good. It does work, but at the cost of an extra dictionary lookup on every get or set, plus a dictionary to hold all the getters & setters. I _don't recommend it_, but here it is anyway. I've done with it now:-) class Attribute: __accessors = {} def __init__(self, name, first_value=None, validator=None): self.name = name self.first_value = first_value self.validator = validator def __get__(self, instance, owner=None): if instance is None: return self if (id(instance), self.name) not in self.__accessors: self.__makeAccessors(instance) return self.__accessors[id(instance), self.name][0](instance) def __set__(self, instance, value): if (id(instance), self.name) not in self.__accessors: self.__makeAccessors(instance) setter = self.__accessors[id(instance), self.name][1] if setter is None: raise AttributeError("'{0}' is read-only".format( self.__name__)) return setter(instance, value) def __makeAccessors(self, instance): hidden_value = self.first_value getter = lambda self: hidden_value if self.validator is not None: def set(instance, value): if self.validator(instance, value): nonlocal hidden_value hidden_value = value else: raise ValueError("'{0}' is not valid for {1}" .format(value, name)) setter = set else: setter = None self.__accessors[id(instance), self.name] = (getter, setter) -- Mark Summerfield, Qtrac Ltd, www.qtrac.eu C++, Python, Qt, PyQt - training and consultancy "Programming in Python 3" - ISBN 0137129297 From Kevin.Smith at sas.com Fri May 8 10:49:23 2009 From: Kevin.Smith at sas.com (Kevin D. Smith) Date: Fri, 8 May 2009 09:49:23 -0500 Subject: heapq.merge with key= References: Message-ID: On 2009-05-07 23:48:43 -0500, Chris Rebert said: > On Thu, May 7, 2009 at 2:23 PM, Kevin D. Smith wrote: >> I need the behavior of heapq.merge to merge a bunch of results from a >> database. ?I was doing this with sorted(itertools.chain(...), key= > ...), but >> I would prefer to do this with generators. ?My issue is that I need > the key >> argument to sort on the correct field in the database. ?heapq.merge > doesn't >> have this argument and I don't understand the code enough to know if it's >> possible to add it. ?Is this enhancement possible without drasticall > y >> changing the current code? > > I think so. Completely untested code: > > def key(ob): > #code here > > class Keyed(object): > def __init__(self, obj): > self.obj = obj > def __cmp__(self, other): > return cmp(key(self.obj), key(other.obj)) > > def keyify(gen): > for item in gen: > yield Keyed(item) > > def stripify(gen): > for keyed in gen: > yield keyed.obj > > merged = stripify(merge(keyify(A), keyify(B), keyify(C))) #A,B,C being > the iterables Ah, that's not a bad idea. I think it could be simplified by letting Python do the comparison work as follows (also untested). def keyify(gen, key=lamda x:x): for item in gen: yield (key(item), item) def stripify(gen): for item in gen: yield item[1] After looking at the heapq.merge code, it seems like something like this could easily be added to that code. If the next() calls were wrapped with the tuple creating code above and the yield simply returned the item. It would, of course, have to assume that the iterables were sorted using the same key, but that's better than not having the key option at all. -- Kevin D. Smith From anuraguniyal at yahoo.com Fri May 8 10:53:44 2009 From: anuraguniyal at yahoo.com (anuraguniyal at yahoo.com) Date: Fri, 8 May 2009 07:53:44 -0700 (PDT) Subject: unicode bit me Message-ID: #how can I print a list of object which may return unicode representation? # -*- coding: utf-8 -*- class A(object): def __unicode__(self): return u"?au" __str__ = __repr__ = __unicode__ a = A() try: print a # doesn't work? except UnicodeEncodeError,e: print e try: print unicode(a) # works, ok fine, great except UnicodeEncodeError,e: print e try: print unicode([a]) # what!!!! doesn't work? except UnicodeEncodeError,e: print e """ Now how can I print a list of object which may return unicode representation? loop/map is not an option as it goes much deepr in my real code any can anyoen explain what is happening here under the hood? """ From gnewsg at gmail.com Fri May 8 10:57:07 2009 From: gnewsg at gmail.com (Giampaolo Rodola') Date: Fri, 8 May 2009 07:57:07 -0700 (PDT) Subject: Call a function when a thread exits References: <5664df89-2966-45e4-a959-f4fc85c2e50f@g20g2000vba.googlegroups.com> Message-ID: <13b3dc70-fcc6-48c1-927c-f9f79b6914ae@b1g2000vbc.googlegroups.com> On 8 Mag, 03:33, Carl Banks wrote: > On May 7, 6:12?pm, "Giampaolo Rodola'" wrote: > > > > > > > Hi, > > I'm searching for a smooth way to call a certain function when a > > thread has finished its job. > > I guess I can keep calling isAlive() in a loop and call my function > > when it returns False but it's not very elegant. > > Actually I'm a bit surprised it doesn't exists an "atexit" function. > > Something like: > > > import threading, time > > > def myfun(): > > ? ? time.sleep(1) > > ? ? print "hello" > > > def cleanup(): > > ? ? print "thread finished, starting cleanup operations..." > > > t = threading.Thread(target=myfun) > > t.atexit(target=cleanup) > > t.start() > > > Is there a reason why there's no such thing in the threading module? > > You can define your target function to clean up on exit. ?Using your > definitions of myfun and cleanup, > > def mycleanfun(): > ? ? try: > ? ? ? ? myfun() > ? ? finally: > ? ? ? ? cleanup() > > t = threading.Thread(target=mycleanfun) > t.start() > > Carl Banks Yes, I know. I was wondering if it could be a good idea proposing something like an atexit() function to be included in threading module. --- Giampaolo http://code.google.com/p/pyftpdlib http://code.google.com/p/psutil From Scott.Daniels at Acm.Org Fri May 8 11:03:57 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Fri, 08 May 2009 08:03:57 -0700 Subject: Tutorial or example use for python-graph library In-Reply-To: <030fcef4-2bf5-4194-97f2-78ce95ca4ef5@v17g2000vbb.googlegroups.com> References: <030fcef4-2bf5-4194-97f2-78ce95ca4ef5@v17g2000vbb.googlegroups.com> Message-ID: Paul Moore wrote: > I have just discovered the python-graph library. I've been interested > in graph algorithms for a long time, so I'd like to give this a try. > But there seems to be very little in the way of examples, or tutorial > documentation available. There's the API documentation, but that's not > really narrative form. And there are some examples in the > distribution, but these seem to be essentially artificial examples, > rather than real-world code (and they don't have much in the way of > documentation!) > > Are there any reasonably-sized tutorials, or examples of common real- > world code, using this package? My google-fu wasn't strong enough to > find anything directly :-( Try writing one yourself, as you are learning the interface. The package authors will love it, and will probably be overjoyed to help you as you stumble. --Scott David Daniels Scott.Daniels at Acm.Org From ppearson at nowhere.invalid Fri May 8 11:08:35 2009 From: ppearson at nowhere.invalid (Peter Pearson) Date: 8 May 2009 15:08:35 GMT Subject: Newcomer to Python tutorial question References: Message-ID: <76j07jF17rmbsU1@mid.individual.net> On Fri, 8 May 2009 10:16:55 +0100, Alan Cameron wrote: [snip] > > In particular reference to the tutorial section > http://docs.python.org/3.0/tutorial/datastructures.html#nested-list-comprehensions > > There is a word which is ambiguous, at least to me. > > Perhaps you can explain the use of the word 'comprehensions'. > > Comprehension I understand > Comprehensions I don't. > > Is there a glossary of terms somewhere? "Comprehension", in this context, is a computer-science term, described in section 5.1.3 of the web page you quoted. If you look up "comprehension" in Wikipedia, you'll get a description of various uses of the term, including a pointer to: http://en.wikipedia.org/wiki/List_comprehension -- To email me, substitute nowhere->spamcop, invalid->net. From chambon.pascal at gmail.com Fri May 8 11:19:13 2009 From: chambon.pascal at gmail.com (Pascal Chambon) Date: Fri, 08 May 2009 17:19:13 +0200 Subject: php to python code converter Message-ID: <4A044D71.1000500@gmail.com> Hello That's funny, I was precisely thinking about a php to python converter, some weeks ago. Such a tool, allowing for example to convert some CMS like Drupal to python, would be a killer app, when we consider the amount of php code available. But of course, there are lots of issues that'd have to be fixed : - translating the php syntax to python syntax - forcing scope limitations where php doesn't have any - handling differences in semantics (for example, the booleanness of "0" or) - handling the automatic variable creation and coertion that php features - handling the php types like arrays (which are neither python lists nor python dicts) - providing a whole mirror of the php stdlib (string and file functions, access to environment vars...) Some things, like PECL modules, would imo be almost impossible to handle (there are already so much trouble to make Cpython extensions available to other python implementations...), but I guess that 95% of existing php websites could be wholly translated "just" with a language translator and an incomplete stddlib replacement. That's hell a lot of work anyway, so it'd be worth weighing it ^^ Actually, your module "phppython" might already be rather useful, because I've crossed here and there people desperately asking for help when translating some php function of their own to python. But if the project were to become bigger, I guess some choices would have to be rechecked. For example it seems you parse the php code your own way, instead of using existing php parsers ; I think the most flexible way would be walking some kind of php abstract syntax tree, and translating it to python AST on the way. Also, writting the comments in english woudl be mandatory :p I'd like to have the opinion of people around : do you think that complete language translators like php<->python or ruby<->python are possible ? Impossible ? Not worth the effort ? Or must be reached by another way (eg. Parrot and stuffs) ? Regards, Pascal PS : Am I the only one having most of answers rejected by the antispam system of python-list ? That's humiliating :p bvidinli a ?crit : > if anybody needs: > http://code.google.com/p/phppython/ > -- > http://mail.python.org/mailman/listinfo/python-list > > > From walterbyrd at iname.com Fri May 8 11:22:22 2009 From: walterbyrd at iname.com (walterbyrd) Date: Fri, 8 May 2009 08:22:22 -0700 (PDT) Subject: Path difficulties with Python 2.5 running on Cygwin Message-ID: <856d93a6-4bbe-4513-b2f2-8e0fa304c797@n7g2000prc.googlegroups.com> It workerd fine yesterday, but it won't work today. I can still run python, but when I try to import csv, I get an error: File "/usr/lib/python2.5/csv.py", line 8 in . . . The module is there. I am guessing that python can not find it. From walterbyrd at iname.com Fri May 8 11:29:59 2009 From: walterbyrd at iname.com (walterbyrd) Date: Fri, 8 May 2009 08:29:59 -0700 (PDT) Subject: Path difficulties with Python 2.5 running on Cygwin References: <856d93a6-4bbe-4513-b2f2-8e0fa304c797@n7g2000prc.googlegroups.com> Message-ID: <17a04789-b47e-465e-a715-9b512048405d@u39g2000pru.googlegroups.com> On May 8, 9:22?am, walterbyrd wrote: > It workerd fine yesterday, but it won't work today. > > I can still run python, but when I try to import csv, I get an error: > > File "/usr/lib/python2.5/csv.py", line 8 in ?. . . > > The module is there. I am guessing that python can not find it. I should have added: When I am in /cygdrive/c/Documents and Settings, everything works fine. But, when I am in /cygdrive/c/Documents and Settings/wbyrd, it no longer works. I also get the error: Attribute error: module has no attribute 'reader' I accidently named a script csv.py, put I deleted that. It seems like I might need to clear out the python interprator, but I don't know how to do that. From mikael at isy.liu.se Fri May 8 11:33:34 2009 From: mikael at isy.liu.se (Mikael Olofsson) Date: Fri, 08 May 2009 17:33:34 +0200 Subject: Decorating methods - where do my arguments go? Message-ID: Hi all! I have long tried to avoid decorators, but now I find myself in a situation where I think they can help. I seem to be able to decorate functions, but I fail miserably when trying to decorate methods. The information I have been able to find on-line focuses on decorating functions, and do not mention any special problem regarding methods. Consider the following example. I start by defining a simple decorator: >>> class test_decorator(object): ... def __init__(self,func): ... self._func = func ... def __call__(self, *args): ... print 'Decorator:', args ... self._func(*args) Then I decorate a function: >>> @test_decorator ... def func(*args): ... print 'Function: ', args Let's try that: >>> func(1,2,3) Decorator: (1, 2, 3) Function: (1, 2, 3) OK! That was what I expected. Let's decorate a method: >>> class cls(object): ... @test_decorator ... def meth(self,*args): ... print 'Method: ', args Then try that: >>> cls().meth(1,2,3) Decorator: (1, 2, 3) Method: (2, 3) Oops! That's weird. I had expected - or at least wanted - the same result as for the function above. Where did the first argument go? If it was sent to the original method meth as the real first argument (self), then why did I not get an exception? More importantly: How do I write a decorator that does not drop arguments? I guess it all has to do with the fact that the returned callable isn't a method of cls. Is it possible to write a decorator that returns a callable that is a method of cls, when used on methods in cls? /MiO From Ning.Zhang at centrica.com Fri May 8 11:35:08 2009 From: Ning.Zhang at centrica.com (Zhang, Ning) Date: Fri, 8 May 2009 16:35:08 +0100 Subject: SOAPpy(10060, 'Operation timed out') Message-ID: <07BA348A0DE16649800F91416F354B8E8F5B0E@WC000046A.uk.centricaplc.com> Hi all I am working for a energy company, we currently want to use api to load data into the database. I am new for soappy package. I have done a bit research on http://www.ibm.com/developerworks/webse ... #resources and http://www.diveintopython.org/soap_web_ ... gging.html I can make none of the examples from tutorial correct. I have also tried the the example from Endolith posted last year. viewtopic.php?f=19&t=10038&p=45872&hilit=soap#p45872 Code: Select all from SOAPpy import WSDL wsdl_file = 'http://xurrency.com/api.wsdl' server = WSDL.Proxy(wsdl_file) values = server.getValuesInverse('usd') However, I got the error message below: error: (10060, 'Operation timed out') Can anyone kind help me with this issue? Alternatively if anyone could provide any worked examples, i will be much appreciated. Kind regards, Ning Zhang _____________________________________________________________________ The information contained in or attached to this email is intended only for the use of the individual or entity to which it is addressed. If you are not the intended recipient, or a person responsible for delivering it to the intended recipient, you are not authorised to and must not disclose, copy, distribute, or retain this message or any part of it. It may contain information which is confidential and/or covered by legal professional or other privilege (or other rules or laws with similar effect in jurisdictions outside England and Wales). The views expressed in this email are not necessarily the views of Centrica plc, and the company, its directors, officers or employees make no representation or accept any liability for its accuracy or completeness unless expressly stated to the contrary. Centrica plc Registered office: Millstream, Maidenhead Road, Windsor, Berkshire SL4 5GD Registered in England and Wales No 3033654 -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: image001.gif Type: image/gif Size: 2990 bytes Desc: image001.gif URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: image002.gif Type: image/gif Size: 1153 bytes Desc: image002.gif URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: image003.gif Type: image/gif Size: 172 bytes Desc: image003.gif URL: From malaclypse2 at gmail.com Fri May 8 11:36:57 2009 From: malaclypse2 at gmail.com (Jerry Hill) Date: Fri, 8 May 2009 11:36:57 -0400 Subject: Path difficulties with Python 2.5 running on Cygwin In-Reply-To: <17a04789-b47e-465e-a715-9b512048405d@u39g2000pru.googlegroups.com> References: <856d93a6-4bbe-4513-b2f2-8e0fa304c797@n7g2000prc.googlegroups.com> <17a04789-b47e-465e-a715-9b512048405d@u39g2000pru.googlegroups.com> Message-ID: <16651e80905080836w2c6e159cu1b44127c9aef0d70@mail.gmail.com> On Fri, May 8, 2009 at 11:29 AM, walterbyrd wrote: > I accidently named a script csv.py, put I deleted that. You probably still have a stale csv.pyc in that directory. Delete that too. To tell for sure, open the python interpreter and do: import csv print csv.__file__ That will tell you exactly where the csv module is being loaded from. -- Jerry From darcy at druid.net Fri May 8 11:37:20 2009 From: darcy at druid.net (D'Arcy J.M. Cain) Date: Fri, 8 May 2009 11:37:20 -0400 Subject: php to python code converter In-Reply-To: <4A044D71.1000500@gmail.com> References: <4A044D71.1000500@gmail.com> Message-ID: <20090508113720.06d4a48e.darcy@druid.net> On Fri, 08 May 2009 17:19:13 +0200 Pascal Chambon wrote: > That's funny, I was precisely thinking about a php to python converter, > some weeks ago. > Such a tool, allowing for example to convert some CMS like Drupal to > python, would be a killer app, when we consider the amount of php code > available. I'm not a big fan of PHP but I don't understand the desireability of such a tool. If you have a good PHP app just run it. The point of Python in my mind is that it is a cleaner syntax and promotes better code. Anything that converts PHP to Python is going to leave you with some butt-ugly Python. It also risks adding new bugs. If you want a Python version of Drupal then perhaps that is the project that you want to start. Start fresh using the existing project as your requirements specification. Don't automatically import all their coding choices that may be partially based on the language used. -- D'Arcy J.M. Cain | Democracy is three wolves http://www.druid.net/darcy/ | and a sheep voting on +1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner. From arnodel at googlemail.com Fri May 8 11:41:12 2009 From: arnodel at googlemail.com (Arnaud Delobelle) Date: Fri, 08 May 2009 16:41:12 +0100 Subject: Code works fine except... References: <8d4ec1df-dddb-469a-99a1-695152db7c04@n4g2000vba.googlegroups.com> <7f3493ca-efcb-48cc-aa29-487bb11fcd09@p4g2000vba.googlegroups.com> <16d47b2f-14d4-4266-84d2-c26501beb0c1@r34g2000vbi.googlegroups.com> <1691f042-bc14-46b9-b7b3-5a44348e9965@b1g2000vbc.googlegroups.com> <1a497471-498a-43d8-90ba-d676e8acf68b@r36g2000vbr.googlegroups.com> <8b49b775-66a7-42d5-9b00-cf23d9b044cc@e23g2000vbe.googlegroups.com> <54d76f11-c255-487d-9c3a-aaa380d7827a@z7g2000vbh.googlegroups.com> <991ccae3-f3e2-4d73-bd32-bc6f560e6f5f@n8g2000vbb.googlegroups.com> Message-ID: John Yeung writes: > On May 5, 11:37?pm, Ross wrote: > >> On May 5, 10:33?am, MRAB wrote: >> >> > Here's my approach (incomplete): >> >> FYI... I was testing your code further and discovered a strange >> outcome... when there are 16 people for 7 courts, every 7th >> round your code produces 4 byes instead of the correct 2 byes. > > Well, MRAB did say his was incomplete, and it's significantly shorter > and cleaner than mine. > > Mine has even weirder-looking behavior at 16 players on 7 courts, but > I think it's because mine tries harder to keep things balanced. After > a few rounds, the inequalities start to build up, and my routine picks > some people more often to "catch up", but it doesn't prevent the same > person from being scheduled for multiple matches the same week. There > may also be other, more subtle ways mine is broken. > > It also may be that the problem is inherently unsolvable for some > values. (I'm still waiting for someone with sufficient math ability > to swoop in and either provide a way that works for all values, or > confirm that there are just some unavoidable pathological cases.) > > John I was thinking about this problem today. What is needed is to generate all matches between n players in an order such that any sequence of (n//2-1) matches does not repeat any player. If there are an even number of players, this guarantees an even distribution of byes as well (i.e. a difference of at most 1 between the highest and lowest number of byes). Unfortunately, if there are an odd number of players, I don't think this is achievable because there are two sources of byes unbalance: * a player needs to be left out of each 'complete round' (where each player plays each other' * some players are left out of each weekly round because there are not enough tennis courts for a complete round to be played in one week. What I believe is achievable in the case of an odd number of players is a difference of at most two between the highest and the lowest number of byes for any player at any point in the tournament. I don't have a proof of this because I haven't had the time to think about it for long enough, but I have a toy implementation which I have tested in a very limited manner. I think it will generate tournaments with the property that bye imbalance never exceeds 2. I also think it is not possible to achieve better in general (it's a conjecture!). ---------------------------------------- from itertools import count def matches(players): "Generates all matches between players" n = len(players) if n % 2: for i in xrange(n): for j in xrange(1, 1 + n/2): yield players[(i+j)%n], players[(i-j)%n] else: m = n - 1 for i in xrange(m): yield players[-1], players[i] for j in xrange(1, n/2): yield players[(i+j)%m], players[(i-j)%m] def print_matches(players): for line in zip(*matches(players)): print ' '.join(line) def tournament(players, courts, nrounds=None): """ players: list of players courts: list of courts nrounds: number of rounds needed or """ if nrounds is None: rounds = count(1) else: rounds = xrange(1, nrounds + 1) opponents = defaultdict(list) courts = courts[:len(players)/2] get_match = matches(players).next try: for round in rounds: print '-'*10 print 'Round', round for court in courts: p1, p2 = get_match() print 'Court %s: %s - %s' % (court, p1, p2) except StopIteration: print "All matches played" ---------------------------------------- Example: >>> players = ['Ann', 'Bea', 'Clo', 'Dee', 'Evi', 'Fae', 'Gil', 'Haz'] >>> courts = ['One', 'Two', 'Three'] >>> tournament(players, courts, 4) ---------- Round 1 Court One: Haz - Ann Court Two: Bea - Gil Court Three: Clo - Fae ---------- Round 2 Court One: Dee - Evi Court Two: Haz - Bea Court Three: Clo - Ann ---------- Round 3 Court One: Dee - Gil Court Two: Evi - Fae Court Three: Haz - Clo ---------- Round 4 Court One: Dee - Bea Court Two: Evi - Ann Court Three: Fae - Gil HTH -- Arnaud From Scott.Daniels at Acm.Org Fri May 8 11:47:11 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Fri, 08 May 2009 08:47:11 -0700 Subject: unicode bit me In-Reply-To: References: Message-ID: <0vadnQqkdrdQz5nXnZ2dnUVZ_uOdnZ2d@pdx.net> anuraguniyal at yahoo.com wrote: > #how can I print a list of object which may return unicode > representation? > # -*- coding: utf-8 -*- > > class A(object): > > def __unicode__(self): > return u"?au" > > __str__ = __repr__ = __unicode__ > > a = A() > > try: > print a # doesn't work? > except UnicodeEncodeError,e: > print e > try: > print unicode(a) # works, ok fine, great > except UnicodeEncodeError,e: > print e > try: > print unicode([a]) # what!!!! doesn't work? > except UnicodeEncodeError,e: > print e > """ > Now how can I print a list of object which may return unicode > representation? > loop/map is not an option as it goes much deepr in my real code > any can anyoen explain what is happening here under the hood? > """ It would be a bit easier if people would bother to mention their Python version, as we regularly get questions from people running 2.3, 2.4, 2.5, 2.6, 2.7a, 3.0, and 3.1b. They run computers with differing operating systems and versions such as: Windows 2000, OS/X Leopard, ubuntu Hardy Heron, SuSE, .... You might shocked to learn that a good answer often depends on the particular situation above. Even though it is easy to say, for example: platform.platform() returns 'Windows-XP-5.1.2600-SP3' sys.version is '2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)]' What is happening is that print is writing to sys.stdout, and apparently that doesn't know how to send unicode to that destination. If you are running under IDLE, print goes to the output window, and if you are running from the command line, it is going elsewhere. the encoding that is being used for output is sys.stdout.encoding. --Scott David Daniels Scott.Daniels at Acm.Org From Rumpf_A at web.de Fri May 8 11:48:05 2009 From: Rumpf_A at web.de (Andreas Rumpf) Date: Fri, 08 May 2009 17:48:05 +0200 Subject: Nimrod programming language Message-ID: <159308160@web.de> Dear Python-users, I invented a new programming language called "Nimrod" that combines Python's readability with C's performance. Please check it out: http://force7.de/nimrod/ Any feedback is appreciated. Regards, Andreas Rumpf ______________________________________________________ GRATIS f?r alle WEB.DE-Nutzer: Die maxdome Movie-FLAT! Jetzt freischalten unter http://movieflat.web.de From python at bdurham.com Fri May 8 11:53:37 2009 From: python at bdurham.com (python at bdurham.com) Date: Fri, 08 May 2009 11:53:37 -0400 Subject: Pythonic way to normalize vertical whitespace Message-ID: <1241798017.7066.1314483739@webmail.messagingengine.com> I'm looking for suggestions on technique (not necessarily code) about the most pythonic way to normalize vertical whitespace in blocks of text so that there is never more than 1 blank line between paragraphs. Our source text has newlines normalized to single newlines (\n vs. combinations of \r and \n), but there may be leading and trailing whitespace around each newline. Approaches: 1. split text to list of lines that get stripped then: a. walk this list building a new list of lines that track and ignore extra blank lines -OR- b. re-join lines and replace '\n\n\n' wth' \n\n' until no more '\n\n\n' matches exist 2. use regular expressions to match and replace whitespace pattern of 3 or more adjacent \n's with surrounding whitespace 3. a 3rd party text processing library designed for efficiently cleaning up text Thanks! -------------- next part -------------- An HTML attachment was scrubbed... URL: From walterbyrd at iname.com Fri May 8 12:01:53 2009 From: walterbyrd at iname.com (walterbyrd) Date: Fri, 8 May 2009 09:01:53 -0700 (PDT) Subject: Path difficulties with Python 2.5 running on Cygwin References: <856d93a6-4bbe-4513-b2f2-8e0fa304c797@n7g2000prc.googlegroups.com> <17a04789-b47e-465e-a715-9b512048405d@u39g2000pru.googlegroups.com> Message-ID: <7cf3e5c5-1367-4272-af69-fa1d95d48123@b7g2000pre.googlegroups.com> On May 8, 9:36?am, Jerry Hill wrote: > On Fri, May 8, 2009 at 11:29 AM, walterbyrd wrote: > > I accidently named a script csv.py, put I deleted that. > > You probably still have a stale csv.pyc in that directory. ?Delete that too. > I don't, I am very certain of that. I have also used find to make certain there are no other stray versions of csv.py. > To tell for sure, open the python interpreter and do: > import csv > print csv.__file__ > > That will tell you exactly where the csv module is being loaded from. Problem is, python will not load the csv module, since it detects an error. And since I can not load the module, csv.__file__ will not work. This file will work: ----------------- #!/usr/bin/env python # import csv print "hello" ------------------ This file will not work: ----------------- #!/usr/bin/env python import csv print "hello" ------------------ In later case, I get that error about 'module' object has no attribute 'reader' From zondo42 at googlemail.com Fri May 8 12:02:01 2009 From: zondo42 at googlemail.com (Glenn Hutchings) Date: Fri, 08 May 2009 17:02:01 +0100 Subject: free chart lib for Python? References: Message-ID: On Fri, 08 May 2009 10:27:08 +0800, oyster wrote: > I mean chart, not plot. If you don't know the difference, you can > check www.advsofteng.com, which is a commercial program > > is there such a thing with many kinds of chart, i.e. pie-chart, > line-chart, ......? You could try matplotlib: http://matplotlib.sourceforge.net. That can do all kinds of wizzo plotty/charty stuff. From walterbyrd at iname.com Fri May 8 12:07:42 2009 From: walterbyrd at iname.com (walterbyrd) Date: Fri, 8 May 2009 09:07:42 -0700 (PDT) Subject: Path difficulties with Python 2.5 running on Cygwin References: <856d93a6-4bbe-4513-b2f2-8e0fa304c797@n7g2000prc.googlegroups.com> <17a04789-b47e-465e-a715-9b512048405d@u39g2000pru.googlegroups.com> <7cf3e5c5-1367-4272-af69-fa1d95d48123@b7g2000pre.googlegroups.com> Message-ID: On May 8, 10:01?am, walterbyrd wrote: > On May 8, 9:36?am, Jerry Hill wrote: > > > On Fri, May 8, 2009 at 11:29 AM, walterbyrd wrote: > > > I accidently named a script csv.py, put I deleted that. > > > You probably still have a stale csv.pyc in that directory. ?Delete that too. > Okay, I got it. I was looking for csv.py - because that is what I accidently created. I don't know where csv.pyc came from, unless python renamed it that when it tried to load csv.py, or something. In any case, it seems to work now. Thanks again for your help. From norseman at hughes.net Fri May 8 12:09:01 2009 From: norseman at hughes.net (norseman) Date: Fri, 08 May 2009 09:09:01 -0700 Subject: subprocess.Popen howto? In-Reply-To: <4A035985.1080806@hughes.net> References: <4A035985.1080806@hughes.net> Message-ID: <4A04591D.5070501@hughes.net> ?ystein ; Down below: change >yourPy.py to | yourPy.py I just noticed the typo. Sorry Steve norseman wrote: > ?ystein Johansen (OJOHANS) wrote: >> Hi, >> >> I have problems understanding the subprocess.Popen object. I have a >> iterative calculation in a process running and I want to pipe the >> output (stdout) from this calculation to a Python script. >> >> Let me include a simple code that simulates the calculating process: >> /* This code simulates a big iterative calculation */ >> #include >> #include >> >> int main() >> { >> float val[2] = { M_PI, M_E }; >> int i; >> >> for ( i = 0; i < 2 i++) { >> sleep( 15 ); /* It's a hard calculation. It take 15 seconds */ >> printf("Value: %5.6f\n", val[i] ); >> fflush( stdout ); >> } >> return 0; >> } >> >> let's compile this to mycalc: gcc -o mycalc calc.c ... (untested code) >> >> In C I have this code which starts the mycalc process and handles the >> output from it: >> >> #include >> #include >> #define BUF_SIZE 256 >> >> int main() >> { >> FILE *pip; >> char line[BUF_SIZE]; >> >> pip = popen("mycalc", "r"); >> assert( pip != NULL ); >> >> while ( fgets( line, BUF_SIZE, pip )) { >> printf( "Hello; I got: %s \n", line ); >> fflush( stdout ); >> } >> pclose( pip ); >> return 0; >> } >> How can I make such while-loop in Python? I assume I should use >> subprocess.Popen(), but I can't figure out how? >> >> -?ystein >> >> >> >> ------------------------------------------------------------------- >> The information contained in this message may be CONFIDENTIAL and is >> intended for the addressee only. Any unauthorised use, dissemination >> of the >> information or copying of this message is prohibited. If you are not the >> addressee, please notify the sender immediately by return e-mail and >> delete >> this message. >> Thank you. >> >> >> ------------------------------------------------------------------------ >> >> -- >> http://mail.python.org/mailman/listinfo/python-list > ================================ > > If you don't like a lot of typing that obscures the process, > take a look at os.Popen2 Pg.39 or so in Lib.pdf for 2.5.2 > In this case - the popen3 is probably your best bet. > > I took a test run on "subprocess" a few months ago. My review: > excessive typing to accomplish the simple. BlackBox stuff is supposed > to be short and sweet. > > BlackBox was the term applied to drivers and couplers back when. > Back when: one started by writing a subroutine that got used. > next one got to write a program > next one got to write an application > then a BlackBox > from there one could graduate to OS > That was back when! > > > to repeat from another of my postings: > --- > processPython 2.5.2 (r252:60911, Mar 4 2008, 10:40:55) > [GCC 3.3.6] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > > >>> import os > >>> AbWd = os.spawnlp( os.P_WAIT,"abiword","abiword","") > > The P_WAIT stops python until the program (abiword in this case) > completes. The "" at the end are for tokens to be given to the program > and yes - contrary to manual, the program MUST be there TWICE (complete > with any path needed). > > for windows this works: > (for cut and paste from cmd.exe, see: > Re: Copy & Paste in a Dos box from norseman 05/06/2009 4:28PM > ) > > Python 2.5.1 ... on win32 > >>> import os > >>> result = os.spawnl( os.P_WAIT, "d:\\winmcad\\mcad","") > > Runs the program mcad. Returns to python when mcad exits. > --- > > > In your case: substitute ..."your_compiled_c_program", " >yourPy.py") > at the obvious places. > > In case this isn't clear; > method 1: py1.py starts compiled.c which redirects to yourPy.py > method 2: py1.py uses os.popen3(compiled.c...) & the two work it out. > In either case the receiver at the end stays open until completion so > sleep() things are not needed. (may need to test for EOL and EOT) > > HTH > > Steve > norseman at hughes.net > -- > http://mail.python.org/mailman/listinfo/python-list > From news.btinternet.com at erik.feegles.net Fri May 8 12:11:14 2009 From: news.btinternet.com at erik.feegles.net (Basil Brush) Date: Fri, 08 May 2009 17:11:14 +0100 Subject: Compiling snippets of python code In-Reply-To: <3pmdnQdqr4ic3JnXnZ2dnUVZ_jWdnZ2d@pdx.net> References: <3pmdnQdqr4ic3JnXnZ2dnUVZ_jWdnZ2d@pdx.net> Message-ID: Scott David Daniels wrote: > make lists and dictionaries containing functions, and that would be > the normal Python way of handling this, rather than generating source > from snippets. You should take a look at Numpy if the "maths functions" Thank you very much for your response. I hadn't thought of doing it that way and I feel that would be the best way, especially as a list of maths functions would make the code for genome recombination and mutation simpler to rewrite. Regards, Erik From google at mrabarnett.plus.com Fri May 8 12:27:36 2009 From: google at mrabarnett.plus.com (MRAB) Date: Fri, 08 May 2009 17:27:36 +0100 Subject: Decorating methods - where do my arguments go? In-Reply-To: References: Message-ID: <4A045D78.4000908@mrabarnett.plus.com> Mikael Olofsson wrote: > Hi all! > > I have long tried to avoid decorators, but now I find myself in a > situation where I think they can help. I seem to be able to decorate > functions, but I fail miserably when trying to decorate methods. The > information I have been able to find on-line focuses on decorating > functions, and do not mention any special problem regarding methods. > > Consider the following example. I start by defining a simple decorator: > > >>> class test_decorator(object): > ... def __init__(self,func): > ... self._func = func > ... def __call__(self, *args): > ... print 'Decorator:', args > ... self._func(*args) > > Then I decorate a function: > > >>> @test_decorator > ... def func(*args): > ... print 'Function: ', args > > Let's try that: > > >>> func(1,2,3) > Decorator: (1, 2, 3) > Function: (1, 2, 3) > > OK! That was what I expected. Let's decorate a method: > > >>> class cls(object): > ... @test_decorator > ... def meth(self,*args): > ... print 'Method: ', args > > Then try that: > > >>> cls().meth(1,2,3) > Decorator: (1, 2, 3) > Method: (2, 3) > > Oops! That's weird. I had expected - or at least wanted - the same > result as for the function above. > > Where did the first argument go? If it was sent to the original method > meth as the real first argument (self), then why did I not get an > exception? > > More importantly: How do I write a decorator that does not drop arguments? > > I guess it all has to do with the fact that the returned callable isn't > a method of cls. Is it possible to write a decorator that returns a > callable that is a method of cls, when used on methods in cls? > >>> class cls(object): @test_decorator def meth(self,*args): print 'self: ', self print 'Method: ', args >>> cls().meth(1,2,3) Decorator: (1, 2, 3) self: 1 Method: (2, 3) From chambon.pascal at wanadoo.fr Fri May 8 12:27:42 2009 From: chambon.pascal at wanadoo.fr (Pascal Chambon) Date: Fri, 08 May 2009 18:27:42 +0200 Subject: php to python code converter In-Reply-To: <20090508113720.06d4a48e.darcy@druid.net> References: <4A044D71.1000500@gmail.com> <20090508113720.06d4a48e.darcy@druid.net> Message-ID: <4A045D7E.8060405@wanadoo.fr> D'Arcy J.M. Cain a ?crit : > On Fri, 08 May 2009 17:19:13 +0200 > Pascal Chambon wrote: > >> That's funny, I was precisely thinking about a php to python converter, >> some weeks ago. >> Such a tool, allowing for example to convert some CMS like Drupal to >> python, would be a killer app, when we consider the amount of php code >> available. >> > > I'm not a big fan of PHP but I don't understand the desireability of > such a tool. If you have a good PHP app just run it. The point of > Python in my mind is that it is a cleaner syntax and promotes better > code. Anything that converts PHP to Python is going to leave you with > some butt-ugly Python. It also risks adding new bugs. > > If you want a Python version of Drupal then perhaps that is the project > that you want to start. Start fresh using the existing project as your > requirements specification. Don't automatically import all their > coding choices that may be partially based on the language used. > > I agree that, in any way, the code resulting from this translation would be anything but pythonic :p The point would rather be to consider code translated from php, ruby, perl or anything as extension modules, which have to be wrapped with pythonic interfaces (much like c/c++ libraries actually), or which are supposed to be deeply refactored. It's always a pity when libraries available in a language have to be reproduced in another - even though building the translation tools would maybe ask for too much effort. Concerning applications, like Drupal, it'd be different of course - I have no precise plan on how Drupal could be translated and then made "maintainable" in python, but for sure Python lacks a similar CMS, easier to approach than Plone and much more feature-full than others (Slemetonz, Pylucid...). And I don't feel quite ready for implementing such a thing myself ^^ Maybe the translation approach isn't the right one ; surely the best would be an interoperability at a lower level, like we have in Jpype, the DLR of .Net, Parrot, ironclad etc... but even with those projects, we're far from a fluent interoperability between languages, which would allow you to pick the best modules in the languages that fit the most for each of your tasks, on the platform you want (I guess that's just a geeky dream). Regards, pascal -------------- next part -------------- An HTML attachment was scrubbed... URL: From chambon.pascal at wanadoo.fr Fri May 8 12:30:47 2009 From: chambon.pascal at wanadoo.fr (Pascal Chambon) Date: Fri, 08 May 2009 18:30:47 +0200 Subject: php to python code converter Message-ID: <4A045E37.2070304@wanadoo.fr> D'Arcy J.M. Cain a ?crit : > On Fri, 08 May 2009 17:19:13 +0200 > Pascal Chambon wrote: > >> That's funny, I was precisely thinking about a php to python converter, >> some weeks ago. >> Such a tool, allowing for example to convert some CMS like Drupal to >> python, would be a killer app, when we consider the amount of php code >> available. >> > > I'm not a big fan of PHP but I don't understand the desireability of > such a tool. If you have a good PHP app just run it. The point of > Python in my mind is that it is a cleaner syntax and promotes better > code. Anything that converts PHP to Python is going to leave you with > some butt-ugly Python. It also risks adding new bugs. > > If you want a Python version of Drupal then perhaps that is the project > that you want to start. Start fresh using the existing project as your > requirements specification. Don't automatically import all their > coding choices that may be partially based on the language used. > > I agree that, in any way, the code resulting from this translation would be anything but pythonic :p The point would rather be to consider code translated from php, ruby, perl or anything as extension modules, which have to be wrapped with pythonic interfaces (much like c/c++ libraries actually), or which are supposed to be deeply refactored. It's always a pity when libraries available in a language have to be reproduced in another - even though building the translation tools would maybe ask for too much effort. Concerning applications, like Drupal, it'd be different of course - I have no precise plan on how Drupal could be translated and then made "maintainable" in python, but for sure Python lacks a similar CMS, easier to approach than Plone and much more feature-full than others (Slemetonz, Pylucid...). And I don't feel quite ready for implementing such a thing myself ^^ Maybe the translation approach isn't the right one ; surely the best would be an interoperability at a lower level, like we have in Jpype, the DLR of .Net, Parrot, ironclad etc... but even with those projects, we're far from a fluent interoperability between languages, which would allow you to pick the best modules in the languages that fit the most for each of your tasks, on the platform you want (I guess that's just a geeky dream). Regards, pascal -------------- next part -------------- An HTML attachment was scrubbed... URL: From cgoldberg at gmail.com Fri May 8 12:32:31 2009 From: cgoldberg at gmail.com (cgoldberg) Date: Fri, 8 May 2009 09:32:31 -0700 (PDT) Subject: free chart lib for Python? References: Message-ID: > You could try matplotlib:http://matplotlib.sourceforge.net. +1 Matplotlib. It's a very nice module with impressive graphing/charting/plotting capabilities. From dotancohen at gmail.com Fri May 8 12:33:10 2009 From: dotancohen at gmail.com (Dotan Cohen) Date: Fri, 8 May 2009 19:33:10 +0300 Subject: php to python code converter In-Reply-To: <20090508113720.06d4a48e.darcy@druid.net> References: <4A044D71.1000500@gmail.com> <20090508113720.06d4a48e.darcy@druid.net> Message-ID: <880dece00905080933t4379ab21h3973e8d624769d06@mail.gmail.com> > I'm not a big fan of PHP but I don't understand the desireability of > such a tool. ?If you have a good PHP app just run it. ?The point of > Python in my mind is that it is a cleaner syntax and promotes better > code. ?Anything that converts PHP to Python is going to leave you with > some butt-ugly Python. ?It also risks adding new bugs. > > If you want a Python version of Drupal then perhaps that is the project > that you want to start. ?Start fresh using the existing project as your > requirements specification. ?Don't automatically import all their > coding choices that may be partially based on the language used. > It would be a great learning tool. I am pretty decent with PHP but just getting started in Python. This might help such users flatten out the learning curve. -- Dotan Cohen http://what-is-what.com http://gibberish.co.il From Shawn at Milochik.com Fri May 8 12:36:12 2009 From: Shawn at Milochik.com (Shawn Milochik) Date: Fri, 8 May 2009 12:36:12 -0400 Subject: xml in python In-Reply-To: References: Message-ID: <2dc0c81b0905080936m7cf9aaecn243d2b9c17cc3b8@mail.gmail.com> On Fri, May 8, 2009 at 3:46 AM, Rustom Mody wrote: > Can someone give me a heads up on xml parsing in python? > The context is that I want to write a simple docbook to text converter. > DOM is alright -- dont want to break my head with SAX just for performance > when my documents are not likely to be large. > > My problem is that there seems to be so many nearly equivalent ways (Pyxml? > Amara?) some of which have moved from 3rd party to builtin status that I am > not clear what is the current method of choice. > > Thanks > -- I've been using minidom for simple XML processing with success. The XML module seems to be the way to go, considering how many of its child modules are listed on http://docs.python.org/library/. import xml.dom.minidom as minidom Shawn From andrew at acooke.org Fri May 8 12:38:33 2009 From: andrew at acooke.org (andrew cooke) Date: Fri, 8 May 2009 12:38:33 -0400 (CLT) Subject: SQL and CSV In-Reply-To: References: <1d2bb50f-2e3f-429b-aa98-7e1fe1f011b0@t10g2000vbg.googlegroups.com> Message-ID: <7aae1530c7ea92c927154597c34c49cc.squirrel@localhost> even if you're not open to injection attacks, you're still less likely to get escaping correct than a puprose written, widely used library. my request for more information was directed to lawrence, who said "until you hit things it can't deal with" which seemed to be some kind of cryptic argument against parameters. andrew Nick wrote: > On May 8, 1:49?pm, "andrew cooke" wrote: >> Lawrence D'Oliveiro wrote: >> > In message , Peter Otten wrote: >> >> >> While it may not matter here using placeholders instead of manually >> >> escaping user-provided values is a good habit to get into. >> >> > Until you hit things it can't deal with. >> >> The post you are replying to was talking about using the SQL library's >> "?" >> syntax that automatically escapes values. ?The usual reason this is >> recommended (if I have understood correctly) is that the library code is >> much more likely to foil injection attacks. ?I have seen this mentioned >> often and assume it is good advice. >> >> Can you expand on your comment? ?I assume you are thinking of how the >> library might handle some strange class. ?But aren't the number of types >> limited by SQL? ?In which case a "thing that can't be handled" could >> presumably be managed by adding an appropriate __str__ or __float__ or >> whatever? ?And you would still use the library to give safety with other >> values. >> >> Maybe you could give an example of the kind of problem you're thinking >> of? >> >> Thanks, >> Andrew > > Injection attacks aren't an issue, its a local app. > > It's part of a reconciliation system, where sometimes data is in csv > files. If you want the whole csv file, you can use csv module without > a problem. > > In some cases, I need to manipulate the data. > > The choices are hard code the manipulation, or load the data from a > config file. > > So what I've got is the query in the config and I can process it. > > Nick > -- > http://mail.python.org/mailman/listinfo/python-list > > From __peter__ at web.de Fri May 8 12:39:29 2009 From: __peter__ at web.de (Peter Otten) Date: Fri, 08 May 2009 18:39:29 +0200 Subject: Decorating methods - where do my arguments go? References: Message-ID: Mikael Olofsson wrote: > Hi all! > > I have long tried to avoid decorators, but now I find myself in a > situation where I think they can help. I seem to be able to decorate > functions, but I fail miserably when trying to decorate methods. The > information I have been able to find on-line focuses on decorating > functions, and do not mention any special problem regarding methods. > > Consider the following example. I start by defining a simple decorator: > > >>> class test_decorator(object): > ... def __init__(self,func): > ... self._func = func > ... def __call__(self, *args): > ... print 'Decorator:', args > ... self._func(*args) > > Then I decorate a function: > > >>> @test_decorator > ... def func(*args): > ... print 'Function: ', args > > Let's try that: > > >>> func(1,2,3) > Decorator: (1, 2, 3) > Function: (1, 2, 3) > > OK! That was what I expected. Let's decorate a method: > > >>> class cls(object): > ... @test_decorator > ... def meth(self,*args): > ... print 'Method: ', args > > Then try that: > > >>> cls().meth(1,2,3) > Decorator: (1, 2, 3) > Method: (2, 3) > > Oops! That's weird. I had expected - or at least wanted - the same > result as for the function above. > > Where did the first argument go? If it was sent to the original method > meth as the real first argument (self), then why did I not get an > exception? > > More importantly: How do I write a decorator that does not drop arguments? > > I guess it all has to do with the fact that the returned callable isn't > a method of cls. Is it possible to write a decorator that returns a > callable that is a method of cls, when used on methods in cls? > > /MiO You have to turn your decorator into a descriptor by providing a __get__() method. A primitive example: class test_decorator(object): def __init__(self,func): self._func = func def __call__(self, *args): print 'Decorator:', args self._func(self.inst, *args) def __get__(self, inst, cls): self.inst = inst return self Read more about the details at http://users.rcn.com/python/download/Descriptor.htm Peter From Scott.Daniels at Acm.Org Fri May 8 12:44:37 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Fri, 08 May 2009 09:44:37 -0700 Subject: Path difficulties with Python 2.5 running on Cygwin In-Reply-To: References: <856d93a6-4bbe-4513-b2f2-8e0fa304c797@n7g2000prc.googlegroups.com> <17a04789-b47e-465e-a715-9b512048405d@u39g2000pru.googlegroups.com> <7cf3e5c5-1367-4272-af69-fa1d95d48123@b7g2000pre.googlegroups.com> Message-ID: walterbyrd wrote: > On May 8, 10:01 am, walterbyrd wrote: >> On May 8, 9:36 am, Jerry Hill wrote: >> >>> On Fri, May 8, 2009 at 11:29 AM, walterbyrd wrote: >>>> I accidently named a script csv.py, put I deleted that. >>> You probably still have a stale csv.pyc in that directory. Delete that too. > Okay, I got it. I was looking for csv.py - because that is what I > accidently created. I don't know where csv.pyc came from, unless > python renamed it that when it tried to load csv.py, or something. You _need_ to understand where csv.pyc came from, or you will hit this problem frequently. When you import any module in python, the python interpreter looks for a "compiled" version of that mdule -- a .pyc (or when optimizing .pyo) file. If it finds it, it checks that it was compiled from the corresponding .py (or .pyw) file. If no .pyc is found, or if the .pyc that was found doesn't match the corresponding .py file, the .py (or .pyw) file is "compiled into either a .pyc or .pyo, and the contents of that .pyc or .pyo are used to build the module. So, importing a module will write a .pyc or .pyo file unless an appropriate .pyc or .pyo file is found. --Scott David Daniels Scott.Daniels at Acm.Org From iwan at reahl.org Fri May 8 13:01:57 2009 From: iwan at reahl.org (Iwan Vosloo) Date: Fri, 08 May 2009 19:01:57 +0200 Subject: How to debug this import problem? Message-ID: <1241802117.7241.37.camel@easymoney> Hi there, We have a rather complicated program which does a bit of os.chdir and sys.path manipulations. In between all of this, it imports the decimal module several times. However, it imports a new instance of decimal sometimes. (Which is a problem, since a decimal.Decimal (imported at point A) now is not the same as that imported somewhere else. In trying to figure out what's happening, we've changed the code in decimal to print out id(sys) when decimal gets imported. This also gives back different values at different times. My suspicion is that when importing python tries to check sys.modules - but each time sys is a different sys already. Any ideas of how we can gather more data to find out exactly what causes this? The path manipulations is a suspect, but unfortunately we cannot remove it. If we can trace the problem from the import end though, we may be able to isolate the exact one which is the problem. This code incidentally also runs in a virtualenv environment AND uses setuptools. None of these complications can be removed... Regards - Iwan From enricong at gmail.com Fri May 8 13:04:09 2009 From: enricong at gmail.com (enricong at gmail.com) Date: Fri, 8 May 2009 10:04:09 -0700 (PDT) Subject: building 32-bit PyQT4 and matplotlib on X86_64 Message-ID: <038e847e-49aa-4cef-b2f6-20ef93fed62d@q14g2000vbn.googlegroups.com> I'm having some issues attempting to build PyQT4 and matplotlib. I'm on a 64-bit machine and am trying to build a 32-bit version. I have already installed seperate 32-bit libs and a 32-bit version of python however I continue to run into two basic problems With PyQT4, I dont know how to tell it that I want to compile 32bit. I run into a problem at the very beginning when it runts qtdirs.mk and there -m32 isnt there. I tried adding it in and manually running make, but then I have the second problem with matplotlib and PyQT4, eventhough the "-m32" flag is being used with g++ and /usr/lib is included in the library search path, I run into problems where I get a message saying something like: /usr/bin/ld: skipping incompatible /usr/lib64/libz.so when searching for -lz then /usr/bin/ld: cannot find -lz I had this exact problem with a few modules while trying to build Python 32 bit, however I was able to substitude -lz with the full path to the library /usr/lib/libz.so.1 This would be impractical to do with matplotlib. This is also just one example, I have problems with other libraries too. From aahz at pythoncraft.com Fri May 8 13:14:48 2009 From: aahz at pythoncraft.com (Aahz) Date: 8 May 2009 10:14:48 -0700 Subject: How do I test the integrity of a Python installation in Debian and Ubuntu References: Message-ID: In article , Geoff Gardiner wrote: > >How do I assure myself of the integrity of a Python installation >acquired using apt-get install on Debian and Ubuntu? How important is the apt-get requirement? Building Python yourself in this situation sounds like it would be simpler/safer. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "It is easier to optimize correct code than to correct optimized code." --Bill Harlan From marco.mandl at gmx.at Fri May 8 13:14:52 2009 From: marco.mandl at gmx.at (Marco) Date: Fri, 08 May 2009 19:14:52 +0200 Subject: import overwrites __name__ Message-ID: Hi, There happened something that I do not understand. Actually I don't even know how it can be possible. I import a module and then the name space of the importing module seems do be overwritten. my_name = __name__ print my_name print len(dir()) from x import y as z print __name__ print len(dir()) print my_name -> __main__ 119 x 117 unhandled NameError "name 'my_name' is not defined" The module x is from me, and I am not aware of doing anything cruel there. What evil thing can be done in its sub module that can result in that strange thing? br Marco From darcy at druid.net Fri May 8 13:28:45 2009 From: darcy at druid.net (D'Arcy J.M. Cain) Date: Fri, 8 May 2009 13:28:45 -0400 Subject: php to python code converter In-Reply-To: <880dece00905080933t4379ab21h3973e8d624769d06@mail.gmail.com> References: <4A044D71.1000500@gmail.com> <20090508113720.06d4a48e.darcy@druid.net> <880dece00905080933t4379ab21h3973e8d624769d06@mail.gmail.com> Message-ID: <20090508132845.0b3a1976.darcy@druid.net> On Fri, 8 May 2009 19:33:10 +0300 Dotan Cohen wrote: > It would be a great learning tool. I am pretty decent with PHP but > just getting started in Python. This might help such users flatten out > the learning curve. If there were such a tool available I would sugegst it come with huge warnings suggesting that the resulting code not be used for learning purposes. You would still be much better off with the tutorials. -- D'Arcy J.M. Cain | Democracy is three wolves http://www.druid.net/darcy/ | and a sheep voting on +1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner. From gagsl-py2 at yahoo.com.ar Fri May 8 13:28:50 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Fri, 08 May 2009 14:28:50 -0300 Subject: Self function References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> <1292fea0-96fb-4a21-b39d-b6ce610ea6b2@t10g2000vbg.googlegroups.com> <4f13a110-b3e5-4de5-b6d6-0d64da20b61e@r34g2000vbi.googlegroups.com> <1241649200.4a0210301f77e@mail.uh.cu> <1241663543.4a024837a9680@mail.uh.cu> Message-ID: En Wed, 06 May 2009 23:32:23 -0300, Luis Alberto Zarrabeitia Gomez escribi?: > > Quoting Steven D'Aprano : > >> But regardless, everyone is missing the most important point: why are >> you >> copying and pasting code in the first place? That is surely very close >> to >> the top of the list of Worst Ever Anti-Patterns, and it should be >> avoided >> whenever possible. > > [btw, I took this long before jumping into this thread for this precise > reason... Copy/paste is an evil anti-pattern. > >> In Python, we can avoid much copy-and-paste coding with decorators. >> Given >> one function, we can create as many variants as we want, differing in >> pre- >> processing of arguments and post-processing of results, by using >> decorators. This is a powerful ability to have, but it's crippled for >> many recursive functions, because recursive functions in Python don't >> actually call themselves, they call whatever happens to be bound to >> their >> name at runtime. > > A bit offtopic: a while ago I think I saw a recipe for a decorator that, > via > bytecode hacks, would bind otherwise global names to the local namespace > of the > function. Can anyone remember it/point me to it? An @bind decorator that > would > 'localize' all the global names, including the still unexistent but > already know > function name, would guarantee that at least recursion calls the same > function > instead of "whatever happens to be bound to their name at runtime". If > it wasn't > a hack, anyway. I could not find such thing, but here it is something similar (mostly a proof-of-concept). Basically, modifies the bytecode so global references become local ones, and then injects a new argument whose default value is the desired value. It can be used to make all references to the function name to refer to the function itself; also, the __function__ variant can be implemented. from opcode import HAVE_ARGUMENT, opmap def iter_code(co): """Iterate over a code object.""" code = co.co_code n = len(code) i = 0 while i < n: op = ord(code[i]) if op >= HAVE_ARGUMENT: oparg = ord(code[i+1]) + ord(code[i+2])*256 yield code[i:i+3], op, oparg i += 3 else: yield code[i], op, None i += 1 def replace_bytecode(code, iname_global, iname_local): LOAD_GLOBAL = opmap['LOAD_GLOBAL'] LOAD_FAST = opmap['LOAD_FAST'] STORE_GLOBAL = opmap['STORE_GLOBAL'] STORE_FAST = opmap['STORE_FAST'] for codestr, op, oparg in iter_code(code): if op==LOAD_GLOBAL and oparg==iname_global: codestr = chr(LOAD_FAST) + chr(iname_local % 256) + chr(iname_local // 256) elif op==STORE_GLOBAL and oparg==iname_global: codestr = chr(STORE_FAST) + chr(iname_local % 256) + chr(iname_local // 256) yield(codestr) class GlobalToLocalWarning(RuntimeWarning): pass def global_to_local(function, refname, value): """ Make all references to `refname` local instead of global. Actually, transform def function(a,b,c): into def function(a,b,c,refname=value) """ code = function.func_code if refname not in code.co_names: import warnings warnings.warn(GlobalToLocalWarning('no reference to "%s" found in %s' % (refname, function.func_name)), stacklevel=2) return function # insert refname just after the last argument varnames = list(code.co_varnames) varnames.insert(code.co_argcount, refname) # new bytecode using LOAD_FAST instead of LOAD_GLOBAL iname_global = code.co_names.index(refname) # LOAD_GLOBAL/STORE_GLOBAL operand iname_local = code.co_argcount # LOAD_FAST/STORE_FAST operand rawcode = ''.join(replace_bytecode(code, iname_global, iname_local)) function.func_code = type(code)( code.co_argcount + 1, code.co_nlocals + 1, code.co_stacksize, code.co_flags, rawcode, code.co_consts, code.co_names, tuple(varnames), code.co_filename, code.co_name, code.co_firstlineno, code.co_lnotab ) # the desired value is the default value of the new, fake argument if function.func_defaults is None: function.func_defaults = (value,) else: function.func_defaults += (value,) return function # decorator: function name refers to itself def recursive3(function): return global_to_local(function, function.func_name, function) @recursive3 def fibo(n): if n<=1: return 1 return fibo(n-1) + fibo(n-2) # those "fibo" won't be global anymore # decorator: name '__function__' refers to function def recursive4(function): return global_to_local(function, '__function__', function) def factorial(n): @recursive4 def _factorial(n, acc): if n<=1: return acc return __function__(n-1, n*acc) return _factorial(n, 1) print fibo, fibo.__name__, fibo.func_name, fibo.func_code.co_name assert fibo(5)==8 F10=factorial(10) assert F10==2*3*4*5*6*7*8*9*10 foo = fibo bar = factorial del fibo, factorial assert foo(5)==8 assert bar(10)==F10 import inspect assert inspect.getargspec(foo).defaults[0] is foo -- Gabriel Genellina From norseman at hughes.net Fri May 8 13:32:34 2009 From: norseman at hughes.net (norseman) Date: Fri, 08 May 2009 10:32:34 -0700 Subject: subprocess.Popen howto? In-Reply-To: <3aaa4e6e-c2c0-43e0-a483-7c79c46137a9@s16g2000vbp.googlegroups.com> References: <3aaa4e6e-c2c0-43e0-a483-7c79c46137a9@s16g2000vbp.googlegroups.com> Message-ID: <4A046CB2.3080101@hughes.net> Carl Banks wrote: > On May 7, 2:58 pm, norseman wrote: >> If you don't like a lot of typing that obscures the process, >> take a look at os.Popen2 Pg.39 or so in Lib.pdf for 2.5.2 >> In this case - the popen3 is probably your best bet. >> >> I took a test run on "subprocess" a few months ago. My review: >> excessive typing to accomplish the simple. > > > Hmm, I won't argue that it can be excessive typing, but I think its > purpose isn't to accomplish the simple but to hit all the corners of > subprocesses. > > What if you want os.system without the shell? What if you want popen > with the shell? On Windows what if I want a subprocess without a > console from a Python program in a console. Stuff like that. > Shell? You lost me. Why would you even use Windows? Just kidding. Build the TSR and Start from Python or the bootup autostart and use it from Python or whatever. Those in Unix, put the " &" after the program name in the starter to get it to background. Be sure to use P_NOWAIT in the starter. > I still use os.system for Q&D stuff, but now use subprocess for > everything else since I prefer thorough to short and sweet. As long as speed (volume vs time) isn't a problem - OK. > > > Carl Banks > -- > http://mail.python.org/mailman/listinfo/python-list > ================================== 1) That one has access to and can make for ones self choices is freedom. 2) As far as forcing a 'do all' into use is concerned, especially at the expense of removing others choices (Python notes say subprocess is supplanting popen et al), well... that is dictitorial, anti-freedom. 3) Consider this: a do all is as useful as driving a Ferrari on a Jeep Jamboree cross crountry race. Or chasing a Ferrari through paved mountain roads with a Jeep. The SUV is supposed to be a "do all". Would you actually expect it to win either race? After all, they are all "cars". Use/create the proper tool for the job. I do not know any good mechanics that have a monkey-wrench (a crescent) in their tool box. And all of them will tell you that the guy who picks one up and starts toward the car may be upset with the driver but he is not a mechanic. (Wrong tool of choice. Besides, should not have been available. :) (Use the long handle 1/2" socket drive ratchet. Weight, length and balance are just right. ;) Everybody - Read #1 one more time and make it (speak out loud) "my way". If the client is happy, who cares how it was done? But don't force others .... (Python admins taking notes?) Steve From deets at nospam.web.de Fri May 8 13:39:31 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Fri, 08 May 2009 19:39:31 +0200 Subject: How to debug this import problem? References: Message-ID: <76j8vcF1crfkoU1@mid.uni-berlin.de> Iwan Vosloo wrote: > Hi there, > > We have a rather complicated program which does a bit of os.chdir and > sys.path manipulations. In between all of this, it imports the decimal > module several times. > > However, it imports a new instance of decimal sometimes. (Which is a > problem, since a decimal.Decimal (imported at point A) now is not the > same as that imported somewhere else. > > In trying to figure out what's happening, we've changed the code in > decimal to print out id(sys) when decimal gets imported. This also > gives back different values at different times. My suspicion is that > when importing python tries to check sys.modules - but each time sys is > a different sys already. > > Any ideas of how we can gather more data to find out exactly what causes > this? The path manipulations is a suspect, but unfortunately we cannot > remove it. If we can trace the problem from the import end though, we > may be able to isolate the exact one which is the problem. > > This code incidentally also runs in a virtualenv environment AND uses > setuptools. None of these complications can be removed... Try putting an "import pdb; pdb.set_trace()" on top of the decimal module. Then you can see whenever it gets imported new, and with "bt" get a backtrace, and you can inspect (and thus eventually compare) the various states of sys.path and sys.modules. Diez From deets at nospam.web.de Fri May 8 13:43:50 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Fri, 08 May 2009 19:43:50 +0200 Subject: php to python code converter References: <4A044D71.1000500@gmail.com> <20090508113720.06d4a48e.darcy@druid.net> Message-ID: <76j97fF1crfkoU2@mid.uni-berlin.de> Dotan Cohen wrote: >> I'm not a big fan of PHP but I don't understand the desireability of >> such a tool. ?If you have a good PHP app just run it. ?The point of >> Python in my mind is that it is a cleaner syntax and promotes better >> code. ?Anything that converts PHP to Python is going to leave you with >> some butt-ugly Python. ?It also risks adding new bugs. >> >> If you want a Python version of Drupal then perhaps that is the project >> that you want to start. ?Start fresh using the existing project as your >> requirements specification. ?Don't automatically import all their >> coding choices that may be partially based on the language used. >> > > It would be a great learning tool. I am pretty decent with PHP but > just getting started in Python. This might help such users flatten out > the learning curve. I'm almost 100% sure it won't. The code is machine-generated, thus not easy to the human eye, not idiomatic so you don't learn how to write good python through it. You learn how to jump through hoops to code the same way in python that you do in PHP. Diez From dotancohen at gmail.com Fri May 8 14:03:35 2009 From: dotancohen at gmail.com (Dotan Cohen) Date: Fri, 8 May 2009 21:03:35 +0300 Subject: php to python code converter In-Reply-To: <76j97fF1crfkoU2@mid.uni-berlin.de> References: <4A044D71.1000500@gmail.com> <20090508113720.06d4a48e.darcy@druid.net> <76j97fF1crfkoU2@mid.uni-berlin.de> Message-ID: <880dece00905081103i4e6127c4kd8e6c60642bb69ef@mail.gmail.com> > I'm almost 100% sure it won't. The code is machine-generated, thus not easy > to the human eye, not idiomatic so you don't learn how to write good python > through it. You learn how to jump through hoops to code the same way in > python that you do in PHP. > I meant for single functions, not for translating entire apps. -- Dotan Cohen http://what-is-what.com http://gibberish.co.il From gruszczy at gmail.com Fri May 8 14:04:17 2009 From: gruszczy at gmail.com (=?UTF-8?Q?Filip_Gruszczy=C5=84ski?=) Date: Fri, 8 May 2009 20:04:17 +0200 Subject: ECG segmentation Message-ID: <1be78d220905081104s64e7191na38e3517b24828d8@mail.gmail.com> Hi! I need to create a script, that performs ECG segmentation, but I can hardly find any useful materials on the web. Did anyone try to do this and could point me to some good materials/snippets about this? -- Filip Gruszczy?ski From musicguy at alphaios.net Fri May 8 14:05:45 2009 From: musicguy at alphaios.net (The Music Guy) Date: Fri, 8 May 2009 13:05:45 -0500 Subject: FLV download script works, but I want to enhance it In-Reply-To: References: Message-ID: On Thu, May 7, 2009 at 9:29 AM, Aahz wrote: > > Here's my download script to get you started figuring this out, it does > the wget in the background so that several downloads can run in parallel > from a single terminal window: > > #!/bin/bash > > echo "Downloading $1" > wget "$1" > /dev/null 2>&1 & > -- > Aahz (aahz at pythoncraft.com) ? ? ? ? ? <*> ? ? ? ? http://www.pythoncraft.com/ > > "It is easier to optimize correct code than to correct optimized code." > --Bill Harlan > -- > http://mail.python.org/mailman/listinfo/python-list > Aahz, Thanks for the reply, but unfortunately that script is going in the complete wrong direction. Firstly, downloading multiple files in tandem does not speed up the process as it merely cuts up the already limited bandwidth into even smaller pieces and delays every download in progress. It is much better to queue downloads to occur one-by-one. Secondly, that approach is based on bash rather than Python. I know I could use the `&` operator on a command line to background processes, but I would like to be able to have more control through Python and the use of the subprocess or threading modules. From google at mrabarnett.plus.com Fri May 8 14:10:48 2009 From: google at mrabarnett.plus.com (MRAB) Date: Fri, 08 May 2009 19:10:48 +0100 Subject: import overwrites __name__ In-Reply-To: References: Message-ID: <4A0475A8.4060206@mrabarnett.plus.com> Marco wrote: > Hi, > > There happened something that I do not understand. Actually I don't even > know how it can be possible. > > I import a module and then the name space of the importing module seems do > be overwritten. > > my_name = __name__ > print my_name > print len(dir()) > from x import y as z > print __name__ > print len(dir()) > print my_name > > -> > __main__ > 119 > x > 117 > unhandled NameError "name 'my_name' is not defined" > > The module x is from me, and I am not aware of doing anything cruel there. > What evil thing can be done in its sub module that can result in that > strange thing? > You'd have to show us the module. Try to make the smallest module that has the same effect. From Scott.Daniels at Acm.Org Fri May 8 14:21:24 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Fri, 08 May 2009 11:21:24 -0700 Subject: How to debug this import problem? In-Reply-To: <76j8vcF1crfkoU1@mid.uni-berlin.de> References: <76j8vcF1crfkoU1@mid.uni-berlin.de> Message-ID: Diez B. Roggisch wrote: > Iwan Vosloo wrote: >> We have a rather complicated program which does a bit of os.chdir and >> sys.path manipulations. In between all of this, it imports the decimal >> module several times.... >> However, it imports a new instance of decimal sometimes. (Which is a >> problem, since a decimal.Decimal (imported at point A) now is not the >> same as that imported somewhere else.... >> >> In trying to figure out what's happening, we've changed the code in >> decimal to print out id(sys) when decimal gets imported. This also >> gives back different values at different times. My suspicion is that >> when importing python tries to check sys.modules - but each time sys is >> a different sys already. >> >> Any ideas of how we can gather more data to find out exactly what causes >> this? The path manipulations is a suspect, but unfortunately we cannot >> remove it. If we can trace the problem from the import end though, we >> may be able to isolate the exact one which is the problem. >> >> This code incidentally also runs in a virtualenv environment AND uses >> setuptools. None of these complications can be removed... > > Try putting an "import pdb; pdb.set_trace()" on top of the decimal module. > Then you can see whenever it gets imported new, and with "bt" get a > backtrace, and you can inspect (and thus eventually compare) the various > states of sys.path and sys.modules. You can also run Python with the "-v" or "-vv" flags to get output about exactly what files are getting imported from the file system and when. "-v" iswhat it got, "-vv" is everywhere it tries. --Scott David Daniels Scott.Daniels at Acm.Org From tjreedy at udel.edu Fri May 8 14:22:32 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 08 May 2009 14:22:32 -0400 Subject: unicode bit me In-Reply-To: <0vadnQqkdrdQz5nXnZ2dnUVZ_uOdnZ2d@pdx.net> References: <0vadnQqkdrdQz5nXnZ2dnUVZ_uOdnZ2d@pdx.net> Message-ID: Scott David Daniels wrote: > It would be a bit easier if people would bother to mention > their Python version, as we regularly get questions from people > running 2.3, 2.4, 2.5, 2.6, 2.7a, 3.0, and 3.1b. They run computers > with differing operating systems and versions such as: Windows 2000, > OS/X Leopard, ubuntu Hardy Heron, SuSE, .... And if they copy and paste the actual error messages instead of saying 'It doesn't work' From tjreedy at udel.edu Fri May 8 14:25:15 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 08 May 2009 14:25:15 -0400 Subject: import overwrites __name__ In-Reply-To: References: Message-ID: Marco wrote: > Hi, > > There happened something that I do not understand. Actually I don't even > know how it can be possible. > > I import a module and then the name space of the importing module seems do > be overwritten. > > my_name = __name__ > print my_name > print len(dir()) > from x import y as z > print __name__ > print len(dir()) > print my_name > > -> > __main__ > 119 > x > 117 > unhandled NameError "name 'my_name' is not defined" > > The module x is from me, and I am not aware of doing anything cruel there. > What evil thing can be done in its sub module that can result in that > strange thing? see responses to "unicode bit me' thread From walterbyrd at iname.com Fri May 8 15:03:49 2009 From: walterbyrd at iname.com (walterbyrd) Date: Fri, 8 May 2009 12:03:49 -0700 (PDT) Subject: Is there a better way to chose a slice of a list? Message-ID: This works, but it seems like there should be a better way. -------------- week = ['sun','mon','tue','wed','thu','fri','sat'] for day in week[week.index('tue'):week.index('fri')]: print day --------------- From caseyhHAMMER_TIME at istar.ca Fri May 8 15:04:01 2009 From: caseyhHAMMER_TIME at istar.ca (Casey Hawthorne) Date: Fri, 08 May 2009 12:04:01 -0700 Subject: I'm intrigued that Python has some functional constructions in the language. Message-ID: I'm intrigued that Python has some functional constructions in the language. Would it be possible to more clearly separate the pure code (without side effects) from the impure code (that deals with state changes, I/O, etc.), so that the pure code could be compiled and have aggressive functional transformations applied to it for efficiency. That way, the syntax would be a lot easier to understand, than most functional languages, like Haskell. I gave a presentation at the beginning of last year on Haskell and at the end, someone's comment was, "I can see the benefits of functional programming but why does it have to be so cryptic." -- Regards, Casey From jcd at sdf.lonestar.org Fri May 8 15:04:55 2009 From: jcd at sdf.lonestar.org (J. Cliff Dyer) Date: Fri, 08 May 2009 15:04:55 -0400 Subject: unicode bit me In-Reply-To: References: Message-ID: <1241809495.6209.13.camel@aalcdl07> On Fri, 2009-05-08 at 07:53 -0700, anuraguniyal at yahoo.com wrote: > #how can I print a list of object which may return unicode > representation? > # -*- coding: utf-8 -*- > > class A(object): > > def __unicode__(self): > return u"?au" > > __str__ = __repr__ = __unicode__ > Your __str__ and __repr__ methods don't return strings. You should encode your unicode to the encoding you want before you try to print it. class A(object): def __unicode__(self): return u"?au" def get_utf8_repr(self): return self.__unicode__().encode('utf-8') def get_koi8_repr(self): return self.__unicode__().encode('koi-8') __str__ = __repr__ = self.get_utf8_repr > a = A() > > try: > print a # doesn't work? > except UnicodeEncodeError,e: > print e > try: > print unicode(a) # works, ok fine, great > except UnicodeEncodeError,e: > print e > try: > print unicode([a]) # what!!!! doesn't work? > except UnicodeEncodeError,e: > print e > """ > Now how can I print a list of object which may return unicode > representation? > loop/map is not an option as it goes much deepr in my real code > any can anyoen explain what is happening here under the hood? > """ > -- > http://mail.python.org/mailman/listinfo/python-list > From debatem1 at gmail.com Fri May 8 16:01:26 2009 From: debatem1 at gmail.com (CTO) Date: Fri, 8 May 2009 13:01:26 -0700 (PDT) Subject: ECG segmentation References: Message-ID: On May 8, 2:04?pm, Filip Gruszczy?ski wrote: > Hi! > > I need to create a script, that performs ECG segmentation, but I can > hardly find any useful materials on the web. Did anyone try to do this > and could point me to some good materials/snippets about this? > > -- > Filip Gruszczy?ski How are you looking to do it? If you want an HMM-based solution I'd recommend . Geremy Condra From jcd at sdf.lonestar.org Fri May 8 16:03:13 2009 From: jcd at sdf.lonestar.org (J. Cliff Dyer) Date: Fri, 08 May 2009 16:03:13 -0400 Subject: php to python code converter In-Reply-To: <4A044D71.1000500@gmail.com> References: <4A044D71.1000500@gmail.com> Message-ID: <1241812993.9340.3.camel@aalcdl07> On Fri, 2009-05-08 at 17:19 +0200, Pascal Chambon wrote: > PS : Am I the only one having most of answers rejected by the > antispam > system of python-list ? That's humiliating :p > I've had several messages not make it through. :( From pruebauno at latinmail.com Fri May 8 16:22:14 2009 From: pruebauno at latinmail.com (pruebauno at latinmail.com) Date: Fri, 8 May 2009 13:22:14 -0700 (PDT) Subject: Is there a better way to chose a slice of a list? References: Message-ID: <51c104f3-4e22-4e3d-a89d-a8133186f3e5@h23g2000vbc.googlegroups.com> On May 8, 3:03?pm, walterbyrd wrote: > This works, but it seems like there should be a better way. > > -------------- > week = ['sun','mon','tue','wed','thu','fri','sat'] > for day in week[week.index('tue'):week.index('fri')]: > ? ?print day > --------------- Depending on the context this style might help: >>> week = ['sun','mon','tue','wed','thu','fri','sat'] >>> tue_fri = slice(week.index('tue'), week.index('fri')) >>> for day in week[tue_fri]: print day But I don't really see it as an improvement unless you are using those intervals repeatedly. From pruebauno at latinmail.com Fri May 8 16:28:44 2009 From: pruebauno at latinmail.com (pruebauno at latinmail.com) Date: Fri, 8 May 2009 13:28:44 -0700 (PDT) Subject: I'm intrigued that Python has some functional constructions in the language. References: Message-ID: <17b230fc-71e2-4a38-8136-e5aa984aff81@o14g2000vbo.googlegroups.com> On May 8, 3:04?pm, Casey Hawthorne wrote: > I'm intrigued that Python has some functional constructions in the > language. > > Would it be possible to more clearly separate the pure code (without > side effects) from the impure code (that deals with state changes, > I/O, etc.), so that the pure code could be compiled and have > aggressive functional transformations applied to it for efficiency. > > That way, the syntax would be a lot easier to understand, than most > functional languages, like Haskell. > > I gave a presentation at the beginning of last year on Haskell and at > the end, someone's comment was, "I can see the benefits of functional > programming but why does it have to be so cryptic." > -- > Regards, > Casey Don't forget that the Python interpreter is simple. It makes maintenance easier and allows embedding it into other programs. Good optimizing compilers for functional languages are not simple. Your idea would be something that could be added to the PyPy project in the future. From pavlovevidence at gmail.com Fri May 8 16:34:03 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Fri, 8 May 2009 13:34:03 -0700 (PDT) Subject: I'm intrigued that Python has some functional constructions in the language. References: Message-ID: <44350436-de1a-451e-95f5-13b902a902ce@s21g2000vbb.googlegroups.com> On May 8, 12:04?pm, Casey Hawthorne wrote: > I'm intrigued that Python has some functional constructions in the > language. > > Would it be possible to more clearly separate the pure code (without > side effects) from the impure code (that deals with state changes, > I/O, etc.), so that the pure code could be compiled and have > aggressive functional transformations applied to it for efficiency. No not really, at least not in any way that it maintains compatibility with Python. Python does either expose or mimic the parsing process to the user, so you could maybe exploit it to get parse trees of functions (checking that there is nothing that has side-effects) to feed to a specialized optimizer, but if you do that it might as well be a new langauge. > That way, the syntax would be a lot easier to understand, than most > functional languages, like Haskell. > > I gave a presentation at the beginning of last year on Haskell and at > the end, someone's comment was, "I can see the benefits of functional > programming but why does it have to be so cryptic." A couple thoughts: 1. It's just one person's opinion. 2. However, functional programming is cryptic at some level no matter how nice you make the syntax. Carl Banks From marco.mandl at gmx.at Fri May 8 16:35:26 2009 From: marco.mandl at gmx.at (Marco) Date: Fri, 08 May 2009 22:35:26 +0200 Subject: import overwrites __name__ References: Message-ID: Terry Reedy wrote: > Marco wrote: >> Hi, >> >> There happened something that I do not understand. Actually I don't even >> know how it can be possible. >> >> I import a module and then the name space of the importing module seems >> do be overwritten. >> >> my_name = __name__ >> print my_name >> print len(dir()) >> from x import y as z >> print __name__ >> print len(dir()) >> print my_name >> >> -> >> __main__ >> 119 >> x >> 117 >> unhandled NameError "name 'my_name' is not defined" >> >> The module x is from me, and I am not aware of doing anything cruel >> there. What evil thing can be done in its sub module that can result in >> that strange thing? > > see responses to "unicode bit me' thread Therry, What shall I find there? From ericgparker at gmail.com Fri May 8 16:45:11 2009 From: ericgparker at gmail.com (hellcats) Date: Fri, 8 May 2009 13:45:11 -0700 (PDT) Subject: Complete frustration Message-ID: I have Python2.5 installed on Windows XP. Whenever I double click on a something.pyw file, IDLE launches and opens something.pyw in the editor. I would prefer to actually *RUN* the program, not edit it. If I want to edit it then I'll choose the "Edit with IDLE" context menu. So I then have to press F5 to get the program to execute. Now I have my program's windows along with two IDLE windows cluttering my screen and task bar. WHAT IS GOING ON? I've tried changing the file association to python.exe, and that works, but just ONCE (even if I choose "always use the slected program to launch this kind of file"). IDLE may be great and all, but I fricken' don't want to see it every time I just want to run a python program!!!!! From namekuseijin at gmail.com Fri May 8 16:52:48 2009 From: namekuseijin at gmail.com (namekuseijin) Date: Fri, 08 May 2009 17:52:48 -0300 Subject: I'm intrigued that Python has some functional constructions in the language. In-Reply-To: <17b230fc-71e2-4a38-8136-e5aa984aff81@o14g2000vbo.googlegroups.com> References: <17b230fc-71e2-4a38-8136-e5aa984aff81@o14g2000vbo.googlegroups.com> Message-ID: pruebauno at latinmail.com escreveu: > Don't forget that the Python interpreter is simple. It makes > maintenance easier and allows embedding it into other programs. Good > optimizing compilers for functional languages are not simple. Good optimizing compilers are not simple, period. The python interpreter is not all that simple either. Have you ever taken a look at it? It's a good bytecode interpreter, not some dumb toy interpreter. -- a game sig: http://tinyurl.com/d3rxz9 From namekuseijin at gmail.com Fri May 8 16:56:17 2009 From: namekuseijin at gmail.com (namekuseijin) Date: Fri, 08 May 2009 17:56:17 -0300 Subject: I'm intrigued that Python has some functional constructions in the language. In-Reply-To: <44350436-de1a-451e-95f5-13b902a902ce@s21g2000vbb.googlegroups.com> References: <44350436-de1a-451e-95f5-13b902a902ce@s21g2000vbb.googlegroups.com> Message-ID: Carl Banks escreveu: > 2. However, functional programming is cryptic at some level no matter > how nice you make the syntax. When your program is nothing but function definition and function application, syntax is meaningless. It's kinda like scripting, say, Microsoft Word in either Visual Basic, Python, Haskell or whatever: you're just calling functions provided by the host, barely using any syntax or intrinsic language feature anyway. Any language will do just fine. -- a game sig: http://tinyurl.com/d3rxz9 From jeff_barish at earthlink.net Fri May 8 17:06:02 2009 From: jeff_barish at earthlink.net (Jeffrey Barish) Date: Fri, 08 May 2009 15:06:02 -0600 Subject: Threads and daemon processes Message-ID: I have a program that uses threading.Timer to execute a function after a delay. It works fine when I run the program normally, but when I run the program as a daemon process, I can't find any evidence that the function ever runs. Is it the case that daemon processes are not allowed to create a thread? -- Jeffrey Barish From google at mrabarnett.plus.com Fri May 8 17:06:48 2009 From: google at mrabarnett.plus.com (MRAB) Date: Fri, 08 May 2009 22:06:48 +0100 Subject: php to python code converter In-Reply-To: <1241812993.9340.3.camel@aalcdl07> References: <4A044D71.1000500@gmail.com> <1241812993.9340.3.camel@aalcdl07> Message-ID: <4A049EE8.3020003@mrabarnett.plus.com> J. Cliff Dyer wrote: > On Fri, 2009-05-08 at 17:19 +0200, Pascal Chambon wrote: >> PS : Am I the only one having most of answers rejected by the >> antispam >> system of python-list ? That's humiliating :p >> > > I've had several messages not make it through. > > :( > Same here ("Message has a suspicious header"). From marco.mandl at gmx.at Fri May 8 17:15:00 2009 From: marco.mandl at gmx.at (Marco) Date: Fri, 08 May 2009 23:15 +0200 Subject: import overwrites __name__ References: Message-ID: Marco wrote: > Hi, > > There happened something that I do not understand. Actually I don't even > know how it can be possible. > > I import a module and then the name space of the importing module seems do > be overwritten. > > my_name = __name__ > print my_name > print len(dir()) > from x import y as z > print __name__ > print len(dir()) > print my_name > > -> > __main__ > 119 > x > 117 > unhandled NameError "name 'my_name' is not defined" > > The module x is from me, and I am not aware of doing anything cruel there. > What evil thing can be done in its sub module that can result in that > strange thing? I just discovered the .pyc file for the main module is not created. When I delete the old pyc and import the main module from the python shell it works fine. But when I start it with "python main_module.py" the .pyc is not created and it happens as shown above. So what leads to the behavior that the .pyc is not created? br Marco From emile at fenx.com Fri May 8 17:18:44 2009 From: emile at fenx.com (Emile van Sebille) Date: Fri, 08 May 2009 14:18:44 -0700 Subject: Complete frustration In-Reply-To: References: Message-ID: On 5/8/2009 1:45 PM hellcats said... > I have Python2.5 installed on Windows XP. Whenever I double click on a > something.pyw file, IDLE launches and opens something.pyw in the > editor. I would prefer to actually *RUN* the program, not edit it. If > I want to edit it then I'll choose the "Edit with IDLE" context menu. > So I then have to press F5 to get the program to execute. Now I have > my program's windows along with two IDLE windows cluttering my screen > and task bar. WHAT IS GOING ON? I've tried changing the file > association to python.exe, and that works, but just ONCE (even if I > choose "always use the slected program to launch this kind of file"). > IDLE may be great and all, but I fricken' don't want to see it every > time I just want to run a python program!!!!! > -- > http://mail.python.org/mailman/listinfo/python-list > This is a windows question -- you'll need to reassign the default association of PYW files. Look in explorer|tools|folder options. Emile From piet at cs.uu.nl Fri May 8 17:22:26 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Fri, 08 May 2009 23:22:26 +0200 Subject: unicode bit me References: Message-ID: >>>>> "J. Cliff Dyer" (JCD) a ?crit: >JCD> On Fri, 2009-05-08 at 07:53 -0700, anuraguniyal at yahoo.com wrote: >>> #how can I print a list of object which may return unicode >>> representation? >>> # -*- coding: utf-8 -*- >>> >>> class A(object): >>> >>> def __unicode__(self): >>> return u"?au" >>> >>> __str__ = __repr__ = __unicode__ >>> >JCD> Your __str__ and __repr__ methods don't return strings. You should >JCD> encode your unicode to the encoding you want before you try to print it. >JCD> class A(object): >JCD> def __unicode__(self): >JCD> return u"?au" >JCD> def get_utf8_repr(self): >JCD> return self.__unicode__().encode('utf-8') >JCD> def get_koi8_repr(self): >JCD> return self.__unicode__().encode('koi-8') >JCD> __str__ = __repr__ = self.get_utf8_repr It might be nicer to have a method that specifies the encoding to be used in order to make switching encodings easier: *untested code* class A(object): def __unicode__(self): return u"?au" def set_encoding(self, encoding): self._encoding = encoding def __repr__(self): return self.__unicode__().encode(self._encoding) __str__ = __repr__ Of course this feels very wrong because the encoding should be chosen when the string goes to the output channel, i.e. outside of the object. Unfortunately this is one of the leftovers from Python's pre-unicode heritage. Hopefully in Python3 this will work without problems. Anyway, in Python 3 the string type is unicode, so at least __repr__ can return unicode. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From apt.shansen at gmail.com Fri May 8 17:25:45 2009 From: apt.shansen at gmail.com (Stephen Hansen) Date: Fri, 8 May 2009 14:25:45 -0700 Subject: import overwrites __name__ In-Reply-To: References: Message-ID: <7a9c25c20905081425n15a34acetf25cb6f2e7cbdad6@mail.gmail.com> > > So what leads to the behavior that the .pyc is not created? > PYC's are only generated on import, they're never made for the main script. I personally like to keep my 'main script' very small. Largely just something which imports other modules and sends them along on their business. --S -------------- next part -------------- An HTML attachment was scrubbed... URL: From marco.mandl at gmx.at Fri May 8 17:37:25 2009 From: marco.mandl at gmx.at (Marco) Date: Fri, 08 May 2009 23:37:25 +0200 Subject: import overwrites __name__ References: <7a9c25c20905081425n15a34acetf25cb6f2e7cbdad6@mail.gmail.com> Message-ID: Stephen Hansen wrote: >> >> So what leads to the behavior that the .pyc is not created? >> > > PYC's are only generated on import, they're never made for the main > script. > > I personally like to keep my 'main script' very small. Largely just > something which imports other modules and sends them along on their > business. Thanks Stephen, I just learned that. But still I don't know why there is a different (correct) behavior when the main module is compiled. As a work-around I put this on top of my main script: import py_compile py_compile.compile(__file__) Still I appreciate if someone could explain me why. br Marco From ldo at geek-central.gen.new_zealand Fri May 8 17:48:58 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Sat, 09 May 2009 09:48:58 +1200 Subject: I'm intrigued that Python has some functional constructions in the language. References: <44350436-de1a-451e-95f5-13b902a902ce@s21g2000vbb.googlegroups.com> Message-ID: In message , namekuseijin wrote: > Carl Banks escreveu: > >> 2. However, functional programming is cryptic at some level no matter >> how nice you make the syntax. > > When your program is nothing but function definition and function > application, syntax is meaningless. On the contrary, syntax is vital to the correct interpretation of functional constructs. See, for example, . From apt.shansen at gmail.com Fri May 8 18:08:03 2009 From: apt.shansen at gmail.com (Stephen Hansen) Date: Fri, 8 May 2009 15:08:03 -0700 Subject: import overwrites __name__ In-Reply-To: References: <7a9c25c20905081425n15a34acetf25cb6f2e7cbdad6@mail.gmail.com> Message-ID: <7a9c25c20905081508rdf944c6mf7eb15592ea4ec24@mail.gmail.com> > > > Still I appreciate if someone could explain me why. > We can't without seeing the code. There shouldn't be any difference, so if there is then its because your code is doing something to cause it-- break it down to a runnable example and we may be able to tell you why, giving us real code and real errors / output. When you distill it into a pseudocode explanation of what you're doing and ask why it doesn't work, quite often what you end up doing is unintentionally leaving out the cause. E.g., I made two files; main.py my_name = __name__ print my_name print len(dir()) from other import y as z print __name__ print len(dir()) print my_name other.py: y = True And the output is -- no matter if there's a pyc or not -- always: __main__ 5 __main__ 6 __main__ There's nothing in default/standard Python which is causing this behavior-- Python doesn't run code differently based on if there's a pyc or not or if its generating a pyc or not, not by itself. You have to be doing something which is goofying up the interpreter somewhere. Not sure what, but it has to be in that 'x' you're importing from :) --S -------------- next part -------------- An HTML attachment was scrubbed... URL: From ericgparker at gmail.com Fri May 8 18:08:16 2009 From: ericgparker at gmail.com (hellcats) Date: Fri, 8 May 2009 15:08:16 -0700 (PDT) Subject: Complete frustration References: Message-ID: <0e27e21f-a09b-4f33-8795-f96d66b0db24@l5g2000vbc.googlegroups.com> On May 8, 4:18?pm, Emile van Sebille wrote: > On 5/8/2009 1:45 PM hellcats said...> I have Python2.5 installed on Windows XP. Whenever I double click on a > > something.pyw file, IDLE launches and opens something.pyw in the > > editor. I would prefer to actually *RUN* the program, not edit it. If > > I want to edit it then I'll choose the "Edit with IDLE" context menu. > > So I then have to press F5 to get the program to execute. Now I have > > my program's windows along with two IDLE windows cluttering my screen > > and task bar. WHAT IS GOING ON? I've tried changing the file > > association to python.exe, and that works, but just ONCE (even if I > > choose "always use the slected program to launch this kind of file"). > > IDLE may be great and all, but I fricken' don't want to see it every > > time I just want to run a python program!!!!! > > -- > >http://mail.python.org/mailman/listinfo/python-list > > This is a windows question -- you'll need to reassign the default > association of PYW files. ?Look in explorer|tools|folder options. > > Emile I know that. I've tried reassigning it from pythonw.exe to python.exe, and like I said, it will work for one invocation. But it seems to revert back the next time I double click. But also, what is wrong with it being pythonw.exe? Why does pythonw.exe open up IDLE? From pavlovevidence at gmail.com Fri May 8 18:22:04 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Fri, 8 May 2009 15:22:04 -0700 (PDT) Subject: I'm intrigued that Python has some functional constructions in the language. References: <44350436-de1a-451e-95f5-13b902a902ce@s21g2000vbb.googlegroups.com> Message-ID: On May 8, 1:56?pm, namekuseijin wrote: > Carl Banks escreveu: > > > 2. However, functional programming is cryptic at some level no matter > > how nice you make the syntax. > > When your program is nothing but function definition and function > application, syntax is meaningless. For mere function application you could maybe argue that (and it'd be a stretch), but there is no reasonable way to claim that syntax is meaningless for defining functions. Unless you meant "function declaration", and I think you did because you don't seem to know what functional programming is. > It's kinda like scripting, say, Microsoft Word in either Visual Basic, > Python, Haskell or whatever: No it's nothing like that at all. > ?you're just calling functions provided by > the host, That's not what functional programming means, nor is it remotely comparable to functional programming. > barely using any syntax or intrinsic language feature anyway. > ? Any language will do just fine. Well, that's not true since I found it to be quite a different experience to invoke Microsoft library functions in JScript than in Visual Basic. (Mostly because it's a PITA even to "barely use any syntax or intrinsic language feature" of Visual Basic.) However, that has nothing to do with functional programming. Carl Banks From usenet at janc.invalid Fri May 8 18:24:54 2009 From: usenet at janc.invalid (JanC) Date: Fri, 8 May 2009 22:24:54 +0000 (UTC) Subject: Is it better to use threads or fork in the following case References: <4e76cd62-e0ba-41dc-8259-103b42730e56@x31g2000prc.googlegroups.com> <766cdhF1bk2ipU1@mid.uni-berlin.de> <5212a09e-37be-4e5f-b040-515913ccf2e8@p6g2000pre.googlegroups.com> Message-ID: Gabriel Genellina wrote: > In addition, the zip file format stores the directory at the end of the > file. So you can't process it until it's completely downloaded. Well, you *can* download the directory part first (if the HTTP server supports it), and if you only need some files, you could then only download these files out of the .zip, saving a lot in download time... -- JanC From emile at fenx.com Fri May 8 18:36:17 2009 From: emile at fenx.com (Emile van Sebille) Date: Fri, 08 May 2009 15:36:17 -0700 Subject: Complete frustration In-Reply-To: <0e27e21f-a09b-4f33-8795-f96d66b0db24@l5g2000vbc.googlegroups.com> References: <0e27e21f-a09b-4f33-8795-f96d66b0db24@l5g2000vbc.googlegroups.com> Message-ID: On 5/8/2009 3:08 PM hellcats said... > On May 8, 4:18 pm, Emile van Sebille wrote: >> This is a windows question -- you'll need to reassign the default >> association of PYW files. Look in explorer|tools|folder options. >> >> Emile > > I know that. I've tried reassigning it from pythonw.exe to python.exe, > and like I said, it will work for one invocation. But it seems to > revert back the next time I double click. But also, what is wrong with > it being pythonw.exe? Why does pythonw.exe open up IDLE? When you right click your source file, do you have an 'open with' python option? Does that do the right thing? If so, you're fighting your windows installation which is where you'll need to address this problem. You might try downloading and installing the activestate python distribution (assuming you're not already there). That will likely set your windows registry appropriately. Emile From Scott.Daniels at Acm.Org Fri May 8 18:37:09 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Fri, 08 May 2009 15:37:09 -0700 Subject: Complete frustration In-Reply-To: <0e27e21f-a09b-4f33-8795-f96d66b0db24@l5g2000vbc.googlegroups.com> References: <0e27e21f-a09b-4f33-8795-f96d66b0db24@l5g2000vbc.googlegroups.com> Message-ID: hellcats wrote: > On May 8, 4:18 pm, Emile van Sebille wrote: >> On 5/8/2009 1:45 PM hellcats said...> I have Python2.5 installed on Windows XP. Whenever I double click on a >>> something.pyw file, IDLE launches and opens something.pyw in the >>> editor. I would prefer to actually *RUN* the program, not edit it. If >>> I want to edit it then I'll choose the "Edit with IDLE" context menu. >>> So I then have to press F5 to get the program to execute. Now I have >>> my program's windows along with two IDLE windows cluttering my screen >>> and task bar. WHAT IS GOING ON? I've tried changing the file >>> association to python.exe, and that works, but just ONCE (even if I >>> choose "always use the slected program to launch this kind of file"). >>> IDLE may be great and all, but I fricken' don't want to see it every >>> time I just want to run a python program!!!!! >>> -- >>> http://mail.python.org/mailman/listinfo/python-list >> This is a windows question -- you'll need to reassign the default >> association of PYW files. Look in explorer|tools|folder options. >> >> Emile > > I know that. I've tried reassigning it from pythonw.exe to python.exe, > and like I said, it will work for one invocation. But it seems to > revert back the next time I double click. But also, what is wrong with > it being pythonw.exe? Why does pythonw.exe open up IDLE? Try opening up a command window, and determine the current associations. If I do this on mine: C:\Projects> assoc .pyw .pyw=Python.NoConFile C:\Projects> ftype Python.NoConFile Python.NoConFile="C:\Python31\pythonw.exe" "%1" %* It is possible you installed something that switched those settings. --Scott David Daniels Scott.Daniels at Acm.Org From caseyhHAMMER_TIME at istar.ca Fri May 8 18:59:28 2009 From: caseyhHAMMER_TIME at istar.ca (Casey Hawthorne) Date: Fri, 08 May 2009 15:59:28 -0700 Subject: Complete frustration References: Message-ID: <99e905to9s933pa6lpap31mn4mmdgks763@4ax.com> On Fri, 08 May 2009 14:18:44 -0700, Emile van Sebille wrote: >On 5/8/2009 1:45 PM hellcats said... >> I have Python2.5 installed on Windows XP. Whenever I double click on a >> something.pyw file, IDLE launches and opens something.pyw in the >> editor. I would prefer to actually *RUN* the program, not edit it. If >> I want to edit it then I'll choose the "Edit with IDLE" context menu. >> So I then have to press F5 to get the program to execute. Now I have >> my program's windows along with two IDLE windows cluttering my screen >> and task bar. WHAT IS GOING ON? I've tried changing the file >> association to python.exe, and that works, but just ONCE (even if I >> choose "always use the slected program to launch this kind of file"). >> IDLE may be great and all, but I fricken' don't want to see it every >> time I just want to run a python program!!!!! >> -- >> http://mail.python.org/mailman/listinfo/python-list >> >This is a windows question -- you'll need to reassign the default >association of PYW files. Look in explorer|tools|folder options. > >Emile I believe this option is in the Python init file! -- Regards, Casey From zondo42 at googlemail.com Fri May 8 19:16:35 2009 From: zondo42 at googlemail.com (Glenn Hutchings) Date: Sat, 09 May 2009 00:16:35 +0100 Subject: import overwrites __name__ References: Message-ID: On Fri, 08 May 2009 19:14:52 +0200, Marco wrote: > I import a module and then the name space of the importing module seems do > be overwritten. > > my_name = __name__ > print my_name > print len(dir()) > from x import y as z > print __name__ > print len(dir()) > print my_name > > -> > __main__ > 119 > x > 117 > unhandled NameError "name 'my_name' is not defined" Interesting. Here's one possibility for the contents of module x.py: import sys del sys.modules['__main__'].my_name y = 42 I can't think of any other way apart from grovelling through sys.modules. Glenn From ericgparker at gmail.com Fri May 8 19:20:52 2009 From: ericgparker at gmail.com (hellcats) Date: Fri, 8 May 2009 16:20:52 -0700 (PDT) Subject: Complete frustration References: <0e27e21f-a09b-4f33-8795-f96d66b0db24@l5g2000vbc.googlegroups.com> Message-ID: <83ebb5c0-4381-4905-a245-a18ce1c6c33a@s16g2000vbp.googlegroups.com> On May 8, 5:36?pm, Emile van Sebille wrote: > On 5/8/2009 3:08 PM hellcats said... > > > On May 8, 4:18 pm, Emile van Sebille wrote: > > >> This is a windows question -- you'll need to reassign the default > >> association of PYW files. ?Look in explorer|tools|folder options. > > >> Emile > > > I know that. I've tried reassigning it from pythonw.exe to python.exe, > > and like I said, it will work for one invocation. But it seems to > > revert back the next time I double click. But also, what is wrong with > > it being pythonw.exe? Why does pythonw.exe open up IDLE? > > When you right click your source file, do you have an 'open with' python > option? ?Does that do the right thing? ?If so, you're fighting your > windows installation which is where you'll need to address this problem. > > You might try downloading and installing the activestate python > distribution (assuming you're not already there). ?That will likely set > your windows registry appropriately. > > Emile Thanks everyone for the suggestions. I played with the registry a little bit. It looks like I had Python2.6 installed at one point, so maybe that corrupted things. I uninstalled everything and reinstalled Python2.5 (but without Tk/Tcl) and now double clicking a .pyw runs the program without IDLE or the MSDOS window showing up (as I had hoped). So I am happy now. Thanks again. From denis.papathanasiou at gmail.com Fri May 8 19:36:06 2009 From: denis.papathanasiou at gmail.com (dpapathanasiou) Date: Fri, 8 May 2009 16:36:06 -0700 (PDT) Subject: mod_python and xml.dom.minidom Message-ID: <83b63d98-9871-47eb-8846-46404b70896e@z7g2000vbh.googlegroups.com> I wrote a python script called xml_utils.py which parses xml using minidom. It works when it's run on its own, but when I try to import it and run it inside a mod_python handler, I get this error: File "../common/xml_utils.py", line 80, in parse_item_attribute File "/usr/lib/python2.5/xml/dom/minidom.py", line 1924, in parseString from xml.dom import expatbuilder SystemError: Parent module 'xml.dom' not loaded Basically, it's the same problem I found in this post: http://mail.python.org/pipermail/python-list/2007-January/424018.html This site (http://www.dscpl.com.au/wiki/ModPython/Articles/ ExpatCausingApacheCrash) goes through a detailed explanation, and I found that the version of pyexpat is newer than libexpat: # ldd /usr/local/apache2/bin/httpd | grep expat libexpat.so.0 => /usr/local/apache2/lib/libexpat.so.0 (0xb7f71000) # strings /usr/local/apache2/lib/libexpat.so.0 | grep expat_ expat_1.95.2 $ python Python 2.5.2 (r252:60911, Jan 4 2009, 17:40:26) [GCC 4.3.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import pyexpat >>> pyexpat.version_info (2, 0, 0) But this is where I'm stuck: the article suggests recompiling apache with the newer version of expat. Apache's configure utility (I'm using httpd version 2.2.11) doesn't explicitly describe an expat library option. Also, if libexpat is version 1.95.2, wouldn't I have to get version 2.0 to be compatible with pyexpat? If anyone has any advice or suggestions, I'd appreciate hearing them. From gallium.arsenide at gmail.com Fri May 8 19:55:39 2009 From: gallium.arsenide at gmail.com (John Yeung) Date: Fri, 8 May 2009 16:55:39 -0700 (PDT) Subject: Is there a better way to chose a slice of a list? References: Message-ID: <6b004ea2-e4e3-4160-a6bf-4a217b13a4a8@s31g2000vbp.googlegroups.com> On May 8, 3:03?pm, walterbyrd wrote: > This works, but it seems like there should be a better way. > > -------------- > week = ['sun','mon','tue','wed','thu','fri','sat'] > for day in week[week.index('tue'):week.index('fri')]: > ? ?print day > --------------- I think you should provide much more information, primarily why you want to do this. What is the larger goal you are trying to achieve? In the absence of further information, it seems to me that you are trying to create an enumerated type. For various ideas to achieve this simply, depending on your purpose, see http://norvig.com/python-iaq.html If you want a more thorough treatment, maybe try this package: http://pypi.python.org/pypi/enum/ There may be other recipes and packages; you can Google for them using keywords "python enum" or similar. John From emile at fenx.com Fri May 8 20:03:34 2009 From: emile at fenx.com (Emile van Sebille) Date: Fri, 08 May 2009 17:03:34 -0700 Subject: Complete frustration In-Reply-To: <99e905to9s933pa6lpap31mn4mmdgks763@4ax.com> References: <99e905to9s933pa6lpap31mn4mmdgks763@4ax.com> Message-ID: On 5/8/2009 3:59 PM Casey Hawthorne said... > On Fri, 08 May 2009 14:18:44 -0700, Emile van Sebille >> This is a windows question -- you'll need to reassign the default >> association of PYW files. Look in explorer|tools|folder options. >> >> Emile > > I believe this option is in the Python init file! As in part of site? Such that invoking "python.exe myfile.py" turns it around and invokes idle myfile.py? Who knew? :) Emile From namekuseijin at gmail.com Fri May 8 20:16:35 2009 From: namekuseijin at gmail.com (namekuseijin) Date: Fri, 8 May 2009 17:16:35 -0700 (PDT) Subject: I'm intrigued that Python has some functional constructions in the language. References: <44350436-de1a-451e-95f5-13b902a902ce@s21g2000vbb.googlegroups.com> Message-ID: <692b7ae8-0c5b-498a-a012-51bda980f3b9@s28g2000vbp.googlegroups.com> On May 8, 6:48?pm, Lawrence D'Oliveiro wrote: > In message , namekuseijin wrote: > > > Carl Banks escreveu: > > >> 2. However, functional programming is cryptic at some level no matter > >> how nice you make the syntax. > > > When your program is nothing but function definition and function > > application, syntax is meaningless. > > On the contrary, syntax is vital to the correct interpretation of functional constructs. See, for example, > . Yeah, it's important in an imperative language to have predefined syntax to allow you to do things. Point taken. From steve at REMOVE-THIS-cybersource.com.au Fri May 8 20:28:34 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 09 May 2009 00:28:34 GMT Subject: Newcomer to Python tutorial question References: Message-ID: <0214c170$0$20647$c3e8da3@news.astraweb.com> On Thu, 07 May 2009 11:40:57 -0700, Chris Rebert wrote: >> why is the printed result of >> >>>>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'} >>>>> print(basket) >> {'orange', 'banana', 'pear', 'apple'} >> >> in the sequence given? > > Because it's *not a sequence* at all, it's a set. [pedant] But the *printed output* is a sequence. It's a sequence of characters. The OP doesn't claim that basket is a sequence-type, he is using "sequence" in a generic, plain English way. [/pedant] I agree with the rest of your explanation about arbitrary ordering of sets :) -- Steven From gagsl-py2 at yahoo.com.ar Fri May 8 20:30:40 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Fri, 08 May 2009 21:30:40 -0300 Subject: Threads and daemon processes References: Message-ID: En Fri, 08 May 2009 18:06:02 -0300, Jeffrey Barish escribi?: > I have a program that uses threading.Timer to execute a function after a > delay. It works fine when I run the program normally, but when I run the > program as a daemon process, I can't find any evidence that the function > ever runs. Is it the case that daemon processes are not allowed to > create a thread? Try redirecting stdout/stderr to some log file; maybe your daemon dies due to some error. -- Gabriel Genellina From socyl at 987jk.com.invalid Fri May 8 20:35:03 2009 From: socyl at 987jk.com.invalid (kj) Date: Sat, 9 May 2009 00:35:03 +0000 (UTC) Subject: ISO exemplary Python scripts Message-ID: I'm learning python, for the umpteenth time. This time I decided to start out by writing Python scripts for everyday tasks. Most of these scripts are meant to be used only once or twice, but a few of them have become useful enough that I'd like to make them a little bit more solid... I'd like to learn from well-written Python scripts, with well-structured options parsing, good error and help messages, clear organization, etc. Can someone point me to good examples of Python scripts? TIA! kynnjo -- NOTE: In my address everything before the first period is backwards; and the last period, and everything after it, should be discarded. From Scott.Daniels at Acm.Org Fri May 8 20:35:34 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Fri, 08 May 2009 17:35:34 -0700 Subject: import overwrites __name__ In-Reply-To: References: Message-ID: Glenn Hutchings wrote: > On Fri, 08 May 2009 19:14:52 +0200, Marco wrote: > >> I import a module and then the name space of the importing module seems do >> be overwritten. >> >> my_name = __name__ >> print my_name >> print len(dir()) >> from x import y as z >> print __name__ >> print len(dir()) >> print my_name >> >> -> >> __main__ >> 119 >> x >> 117 >> unhandled NameError "name 'my_name' is not defined" > > Interesting. Here's one possibility for the contents of module x.py: > > import sys > del sys.modules['__main__'].my_name > y = 42 > > I can't think of any other way apart from grovelling through sys.modules. OK, here's one way to see more detail: import sys my_name = __name__ pre = set(sys.modules), set(dir()) from x import y as z post = set(sys.modules), set(dir()) print 'Modules added: %s, removed: %s' % ( post[0] - pre[0], pre[0] - post[0]) print 'Globals added: %s, removed: %s' % ( post[1] - pre[1], pre[1] - post[1]) --Scott David Daniels Scott.Daniels at Acm.Org From Scott.Daniels at Acm.Org Fri May 8 20:39:33 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Fri, 08 May 2009 17:39:33 -0700 Subject: Complete frustration In-Reply-To: <83ebb5c0-4381-4905-a245-a18ce1c6c33a@s16g2000vbp.googlegroups.com> References: <0e27e21f-a09b-4f33-8795-f96d66b0db24@l5g2000vbc.googlegroups.com> <83ebb5c0-4381-4905-a245-a18ce1c6c33a@s16g2000vbp.googlegroups.com> Message-ID: hellcats wrote: > On May 8, 5:36 pm, Emile van Sebille wrote: >> On 5/8/2009 3:08 PM hellcats said... >> >>> On May 8, 4:18 pm, Emile van Sebille wrote: >> >>>> This is a windows question -- you'll need to reassign the default >>>> association of PYW files. Look in explorer|tools|folder options. >>>> Emile >>> I know that. I've tried reassigning it from pythonw.exe to python.exe, >>> and like I said, it will work for one invocation. But it seems to >>> revert back the next time I double click. But also, what is wrong with >>> it being pythonw.exe? Why does pythonw.exe open up IDLE? >> When you right click your source file, do you have an 'open with' python >> option? Does that do the right thing? If so, you're fighting your >> windows installation which is where you'll need to address this problem. >> >> You might try downloading and installing the activestate python >> distribution (assuming you're not already there). That will likely set >> your windows registry appropriately. >> >> Emile > > Thanks everyone for the suggestions. I played with the registry a > little bit. It looks like I had Python2.6 installed at one point, so > maybe that corrupted things. I uninstalled everything and reinstalled > Python2.5 (but without Tk/Tcl) and now double clicking a .pyw runs the > program without IDLE or the MSDOS window showing up (as I had hoped). > So I am happy now. Thanks again. You probably can have Tk/Tcl there, I suspect something like VPython or the 2.6 and remove it caused the problem. By the way, it is safe to simply uninstall-reinstall a python .msi in order to make that version of python be the default. uninstalling only removes that which it installs, so after a reinstall, your other libraries are still safely there. --Scott David Daniels Scott.Daniels at Acm.Org From steve at REMOVE-THIS-cybersource.com.au Fri May 8 20:39:37 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 09 May 2009 00:39:37 GMT Subject: list comprehension question References: <6d502cfd-e252-4722-96a5-c52fc3579499@v1g2000prd.googlegroups.com> <85ocucnbce.fsf@agentultra.com> <85ljpb8r1o.fsf@agentultra.com> <85d4am8hq4.fsf@agentultra.com> <8563gcx1p1.fsf@agentultra.com> Message-ID: <0214c407$0$20647$c3e8da3@news.astraweb.com> On Thu, 07 May 2009 13:28:10 -0400, J Kenneth King wrote: > Steven D'Aprano writes: > >> On Wed, 06 May 2009 09:48:51 -0400, J Kenneth King wrote: >> >>> Emile van Sebille writes: >>> >>>> On 5/5/2009 9:15 AM J Kenneth King said... >>>> >>>>> List comprehensions can make a reader of your code apprehensive >>>>> because it can read like a run-on sentence and thus be difficult to >>>>> parse. The Python documentation discourages their use and I believe >>>>> for good reason. >>>> >>>> Can you provide a link for this? I'd like to see specifically what's >>>> being discouraged, as I'd be surprised to find routine usage frowned >>>> upon. >>>> >>>> Emile >>> >>> http://docs.python.org/tutorial/datastructures.html#nested-list- >> comprehensions >>> >>> >>> "If you?ve got the stomach for it, list comprehensions can be nested. >>> They are a powerful tool but ? like all powerful tools ? they need to >>> be used carefully, if at all." >> >> How does this discourage the use of list comprehensions? At most, it >> warns that complicated list comps are tricky. Complicated *anything* >> are tricky. > > They are tricky and need to be used carefully, *if at all*. > > IMO this means that if there's a way to do it without a nested list > comprehension, then that solution should be preferred. Earlier, you claimed that list comps in general were discouraged: "List comprehensions can make a reader of your code apprehensive because it can read like a run-on sentence and thus be difficult to parse. The Python documentation discourages their use and I believe for good reason." Emile said "I'd be surprised to find routine usage frowned upon", giving you the opportunity to correct his (mis)understanding. You failed to do so, which I took as meaning that you agreed that routine usage of simple list comps were frowned upon. Now you're talking about *nested* list comps. You started off (apparently) objecting to list comps in general, because they "can" make readers apprehensive. Now you seem to be saying that it's only the complicated, overly-dense ones which rightly make readers apprehensive which you object to. I suppose we're making progress if we agree that the Python docs warn against unnecessarily complicated nested list comps. Whether it discourages as well as warns is a matter of interpretation. But there's certainly no sign that list comps as a general technique are discouraged just because overly-complicated list comps are tricky to read. The same can be said about *any* piece of code. -- Steven From steve at REMOVE-THIS-cybersource.com.au Fri May 8 20:41:08 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 09 May 2009 00:41:08 GMT Subject: Self function References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> Message-ID: <0214c461$0$20647$c3e8da3@news.astraweb.com> On Thu, 07 May 2009 08:21:51 -0700, Francis Carr wrote: > In the special case of a self-recursive function, you would like re- > naming "to just work". Out of morbid curiosity, I am compelled to > ask... why do you expect that re-naming anything --- a function, a > class, a module, a variable, anything --- in just one place but not all > the others should ever, under any circumstances, "just work" ? Patching functions to do pre-processing or post-processing can be a useful technique, (e.g. for tracing and debugging). This works for regular functions, but not recursive functions. >>> def listify(x): ... return [x] ... >>> listify(42) [42] >>> >>> _old_listify = listify >>> def listify(x): ... print "Calling listify" ... return _old_listify(x) ... >>> listify(42) Calling listify [42] Note that the call to listify "just works" -- the patch runs once, the original version of the function runs once, and we get the expected results without any problems. Now try the same technique on a recursive function, and it fails: >>> def rec(x): ... if x > 0: return [x] + rec(x-1) ... else: return [] ... >>> rec(5) [5, 4, 3, 2, 1] >>> >>> _old_rec = rec >>> def rec(x): ... print "Calling rec" ... return _old_rec(x) ... >>> rec(5) Calling rec Calling rec Calling rec Calling rec Calling rec Calling rec [5, 4, 3, 2, 1] Note too that if you're given an arbitrary function, there's no simple way of telling whether it's recursive or not. Properly written "self- reflective" functions using my proposed technique should be immune to this problem. -- Steven From steve at REMOVE-THIS-cybersource.com.au Fri May 8 20:47:24 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 09 May 2009 00:47:24 GMT Subject: unicode bit me References: <0vadnQqkdrdQz5nXnZ2dnUVZ_uOdnZ2d@pdx.net> Message-ID: <0214c5da$0$20647$c3e8da3@news.astraweb.com> On Fri, 08 May 2009 14:22:32 -0400, Terry Reedy wrote: > Scott David Daniels wrote: > >> It would be a bit easier if people would bother to mention their >> Python version, as we regularly get questions from people running 2.3, >> 2.4, 2.5, 2.6, 2.7a, 3.0, and 3.1b. They run computers with differing >> operating systems and versions such as: Windows 2000, OS/X Leopard, >> ubuntu Hardy Heron, SuSE, .... > > And if they copy and paste the actual error messages instead of saying > 'It doesn't work' "I tried to copy and paste the actual error message, but it doesn't work..." *grin* -- Steven From namekuseijin at gmail.com Fri May 8 20:47:30 2009 From: namekuseijin at gmail.com (namekuseijin) Date: Fri, 8 May 2009 17:47:30 -0700 (PDT) Subject: I'm intrigued that Python has some functional constructions in the language. References: <44350436-de1a-451e-95f5-13b902a902ce@s21g2000vbb.googlegroups.com> Message-ID: <9e68a155-177d-4e03-97b6-c19cbb986515@z19g2000vbz.googlegroups.com> On May 8, 7:22?pm, Carl Banks wrote: > On May 8, 1:56?pm, namekuseijin wrote: > > Carl Banks escreveu: > > > 2. However, functional programming is cryptic at some level no matter > > > how nice you make the syntax. > > > When your program is nothing but function definition and function > > application, syntax is meaningless. > > For mere function application you could maybe argue that (and it'd be > a stretch), but there is no reasonable way to claim that syntax is > meaningless for defining functions. ?Unless you meant "function > declaration", and I think you did because you don't seem to know what > functional programming is. > > > ?you're just calling functions provided by > > the host, > > That's not what functional programming means, nor is it remotely > comparable to functional programming. My point is that when all you do is call functions, syntax is irrelevant. You call functions pretty much in the same way regardless of language: functionname, optionalOpenPar, parameters, optionalClosePar. Office automation is all about calling predefined functions in the host application, that's all my example was about. Functional programming is all about defining functions and applying functions. Core ML, Haskell and Scheme are all like that, pretty much an extended lambda calculus. Haskell provides a bunch of syntatic sugar, more so than Scheme for instance, but in the end, it all gets converted into lambda expressions and application of arguments to lambda expressions. Python has a bunch of handy predefined syntax, because not everything can be defined out of functions alone. Syntax is much more important here than in true functional programming languages, where pretty much everything besides basic "if" branching is a "userland" function -- including looping constructs. I have written my own list comprehensions and generators in Scheme! When you read a Haskell or Scheme program, it's truly hard to spot predefined syntax: most of it is composable function application. Once in a while you spot an "if". > > barely using any syntax or intrinsic language feature anyway. > > ? Any language will do just fine. > > Well, that's not true since I found it to be quite a different > experience to invoke Microsoft library functions ?in JScript than in > Visual Basic. ?(Mostly because it's a PITA even to "barely use any > syntax or intrinsic language feature" of Visual Basic.) Not quite Word, but here's in OpenOffice scripted either in BeanShell, JScript and Java: http://wiki.services.openoffice.org/wiki/Documentation/DevGuide/Scripting/Writing_Macros oDoc = context.getDocument(); xTextDoc = (XTextDocument) UnoRuntime.queryInterface (XTextDocument.class,oDoc); xText = xTextDoc.getText(); xTextRange = xText.getEnd(); xTextRange.setString( "Hello World (in BeanShell)" ); oDoc = XSCRIPTCONTEXT.getDocument(); xTextDoc = UnoRuntime.queryInterface(XTextDocument,oDoc); xText = xTextDoc.getText(); xTextRange = xText.getEnd(); xTextRange.setString( "Hello World (in JavaScript)" ); XTextDocument xtextdocument = (XTextDocument) UnoRuntime.queryInterface ( XTextDocument.class, xDocModel); XText xText = xtextdocument.getText(); XTextRange xTextRange = xText.getEnd(); xTextRange.setString( "Hello World (in Java)" ); Although this is a bad example because of the closeness of syntax between the languages, it would not be much different in a completely alien language. It would still make a call to get the data model of the current document, another to get the text, another to get the end and another to set the string. It's all function calls, really. No, it's not functional programming, but it illustrates what I said: when all you do is call functions, syntax is irrelevant. From http Fri May 8 20:49:51 2009 From: http (Paul Rubin) Date: 08 May 2009 17:49:51 -0700 Subject: I'm intrigued that Python has some functional constructions in the language. References: Message-ID: <7x1vqzaymo.fsf@ruckus.brouhaha.com> Casey Hawthorne writes: > Would it be possible to more clearly separate the pure code (without > side effects) from the impure code (that deals with state changes, > I/O, etc.), so that the pure code could be compiled and have > aggressive functional transformations applied to it for efficiency. This doesn't fit Python semantics terribly well. > That way, the syntax would be a lot easier to understand, than most > functional languages, like Haskell. I don't think it would help much, even if it was done (see below). > I gave a presentation at the beginning of last year on Haskell and at > the end, someone's comment was, "I can see the benefits of functional > programming but why does it have to be so cryptic." Haskell's syntax (like Python's or Lisp's) takes a little getting used to, but in my experience it's not really cryptic. IMHO, functional programming is difficult for imperative programmers to understand at first, because the concepts in it are really different from what imperative programmers are generally used to, and that take some head scratching to make sense of. Think of a beginning imperative programmer encountering recursion or coroutines for the first time, or how someone learning calculus deals with the concepts of limits and continuity. These subjects have certain hurdles that require effort to get past, but which expand the range of problems that someone who has put in the effort can solve. Functional programming presents more such hurdles, and more such expansion. From steve at REMOVE-THIS-cybersource.com.au Fri May 8 20:51:46 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 09 May 2009 00:51:46 GMT Subject: Simple programme - Just want to know whether this is correct way of coding References: <49f8cb54-f268-46bc-a585-6d116674d00d@k19g2000prh.googlegroups.com> Message-ID: <0214c6e0$0$20647$c3e8da3@news.astraweb.com> On Thu, 07 May 2009 23:37:27 -0700, guptha wrote: > The code Works fine ,but I doubt about the performance issue ,My > intention is to send mails concurrently to large number of mail. 1.For > every mail id i send It creates a new SMTP object,in case, if i send to > 1000 or more ids > a) It obliviously creates that much SMPT connections ,is this a > right approach . Why do you care? I assume you're running this code on a botnet, so why care about performance? [ha ha only serious] -- Steven From steve at REMOVE-THIS-cybersource.com.au Fri May 8 20:52:20 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 09 May 2009 00:52:20 GMT Subject: Why there is a parameter named "self" for classmethod function? References: <2b767f890905061949g12d0bf03m68c6b466ffedfbd9@mail.gmail.com> <4A024F18.9080407@kbsymanzik.org> <2c80e8d0-cc44-48b3-8b90-7b165eb20ed4@g20g2000vba.googlegroups.com> Message-ID: <0214c701$0$20647$c3e8da3@news.astraweb.com> On Thu, 07 May 2009 04:27:15 -0700, Aaron Brady wrote: >> Can be, but if there's reason enough to keep it with a class, there's >> no reason not to. > > That's a bit of hyperbole; the usual reasons such as code bloat, > namespace bloat, maintainability etc. all weigh against it. It's just > that the benefits weigh heavier. I'm not sure I understand. Whether the function is in the class or extracted out into the module, it is exactly the same function. (Apart from an extra level of indentation, and either an unused "self" parameter or a @staticmethod line.) Same amount of code, same difficulty of maintainability. Namespace bloat is also the same, the only difference being which namespace is bloated: the class or the method. -- Steven From pavlovevidence at gmail.com Fri May 8 21:13:44 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Fri, 8 May 2009 18:13:44 -0700 (PDT) Subject: I'm intrigued that Python has some functional constructions in the language. References: <44350436-de1a-451e-95f5-13b902a902ce@s21g2000vbb.googlegroups.com> <9e68a155-177d-4e03-97b6-c19cbb986515@z19g2000vbz.googlegroups.com> Message-ID: <7fa66f4e-e88c-4fb4-a509-911756814f89@o27g2000vbd.googlegroups.com> On May 8, 5:47?pm, namekuseijin wrote: > My point is that when all you do is call functions, syntax is > irrelevant. ?You call functions pretty much in the same way regardless > of language: ?functionname, optionalOpenPar, parameters, > optionalClosePar. then... > Functional programming is all about defining functions and applying > functions. ?Core ML, Haskell and Scheme are all like that, Yet all three use a different syntax to call functions, none of them the "pretty much the same way" you listed above. Haskell, Python, and (I think) ML can define operators with different syntax than function calls, that matters. Haskell and Python have syntax for list operations, that matters. Haskell nexts using indentation, the others nest using tokens(**), that matters. I can go on, but you get the idea. Point is: functional programmint isn't "nothing but calling functions". [snip irrelevant stuff about office scripting] Carl Banks (**) Python does using indentation to nest, of course, but not at the expression level. From http Fri May 8 21:36:41 2009 From: http (Paul Rubin) Date: 08 May 2009 18:36:41 -0700 Subject: I'm intrigued that Python has some functional constructions in the language. References: <44350436-de1a-451e-95f5-13b902a902ce@s21g2000vbb.googlegroups.com> <9e68a155-177d-4e03-97b6-c19cbb986515@z19g2000vbz.googlegroups.com> <7fa66f4e-e88c-4fb4-a509-911756814f89@o27g2000vbd.googlegroups.com> Message-ID: <7xmy9nnjkm.fsf@ruckus.brouhaha.com> Carl Banks writes: > I can go on, but you get the idea. Point is: functional programmint > isn't "nothing but calling functions". I would mainly describe functional programming as programming with the pervasive use of higher order functions. For example, loops in functional programming can be implemented using recursion, but in practice, one doesn't see actual explicit recursion in Haskell code all that often. Instead we see wide use of functions like map, filter, and fold (a/k/a "reduce"), which can take the place of looping constructs. Python also has higher-order functions like that, but their use is disfavored in certain circles. With Python 3, there has actually been movement towards removing them from the language. From namekuseijin at gmail.com Fri May 8 22:19:21 2009 From: namekuseijin at gmail.com (namekuseijin) Date: Fri, 8 May 2009 19:19:21 -0700 (PDT) Subject: I'm intrigued that Python has some functional constructions in the language. References: <44350436-de1a-451e-95f5-13b902a902ce@s21g2000vbb.googlegroups.com> <9e68a155-177d-4e03-97b6-c19cbb986515@z19g2000vbz.googlegroups.com> <7fa66f4e-e88c-4fb4-a509-911756814f89@o27g2000vbd.googlegroups.com> Message-ID: On May 8, 10:13?pm, Carl Banks wrote: > On May 8, 5:47?pm, namekuseijin wrote: > > > My point is that when all you do is call functions, syntax is > > irrelevant. ?You call functions pretty much in the same way regardless > > of language: ?functionname, optionalOpenPar, parameters, > > optionalClosePar. > > then... > > > Functional programming is all about defining functions and applying > > functions. ?Core ML, Haskell and Scheme are all like that, > > Yet all three use a different syntax to call functions, none of them > the "pretty much the same way" you listed above. It's still functionName arguments AFAIK. Some using parentheses around the arguments, some around all, some not using parentheses at all. > Haskell and Python have syntax for list operations, that matters. > > Haskell nexts using indentation, the others nest using tokens(**), > that matters. In Haskell, Lisp and other functional programming languages, any extra syntax gets converted into the core lambda constructs. In Lisp languages, that syntax is merely user-defined macros, but in Haskell it's builtin the compiler for convenience. > I can go on, but you get the idea. ?Point is: functional programmint > isn't "nothing but calling functions". Oh yes, functional programming is all about function definition and function calling. You have a point about higher order functions, but that's really only useful as far as your lambda expressions are useful -- that is, conveniently defining anonymous functions on-the-fly and immediately applying them. From namekuseijin at gmail.com Fri May 8 22:22:26 2009 From: namekuseijin at gmail.com (namekuseijin) Date: Fri, 8 May 2009 19:22:26 -0700 (PDT) Subject: Simple programme - Just want to know whether this is correct way of coding References: <49f8cb54-f268-46bc-a585-6d116674d00d@k19g2000prh.googlegroups.com> Message-ID: <7d44d587-15c7-40ea-abe2-6a174f1fd0cd@q14g2000vbn.googlegroups.com> On May 8, 3:37?am, guptha wrote: > The code Works fine ,but I doubt about the performance issue ,My > intention is to send mails concurrently to large number of mail. > 1.For every mail id i send It creates a new SMTP object,in case, if i > send to 1000 or more ids why should I help a spammer... ;) From li.wang.d at gmail.com Fri May 8 22:59:31 2009 From: li.wang.d at gmail.com (Li Wang) Date: Sat, 9 May 2009 12:59:31 +1000 Subject: Python 2.6, File read() problems in Windows Xp In-Reply-To: <4A049811.9010600@ieee.org> References: <11aabe3b0905072204w499679f8q9d1be6d63cddd597@mail.gmail.com> <4A03DE84.40406@ieee.org> <11aabe3b0905080208x5df4df5bu4eec2712d842320@mail.gmail.com> <4A049811.9010600@ieee.org> Message-ID: <11aabe3b0905081959x5a0a0c80jde3dc500f9ccda9b@mail.gmail.com> > (my smtp mail server has been down for a few hours, so you have some other > responses.) > > yes, as far as I can tell from the docs, the 'b' flag doesn't matter in > Unix/Linux. Note that I haven't actually been on a Unix machine since > starting with Python, but it matches what else I know. And if it's > different in Python 3.0, I wouldn't know that either., Thank you very much! -- Li ------ Time is all we have and you may find one day you have less than you think From apt.shansen at gmail.com Fri May 8 23:05:10 2009 From: apt.shansen at gmail.com (Stephen Hansen) Date: Fri, 8 May 2009 20:05:10 -0700 Subject: I'm intrigued that Python has some functional constructions in the language. In-Reply-To: <7xmy9nnjkm.fsf@ruckus.brouhaha.com> References: <44350436-de1a-451e-95f5-13b902a902ce@s21g2000vbb.googlegroups.com> <9e68a155-177d-4e03-97b6-c19cbb986515@z19g2000vbz.googlegroups.com> <7fa66f4e-e88c-4fb4-a509-911756814f89@o27g2000vbd.googlegroups.com> <7xmy9nnjkm.fsf@ruckus.brouhaha.com> Message-ID: <7a9c25c20905082005r7c3e5ec4y57e95a681fe5cba1@mail.gmail.com> > Python also has higher-order functions like that, but their use is > disfavored in certain circles. With Python 3, there has actually been > movement towards removing them from the language. ... Buh? Reduce was moved to functools, map and filter weren't touched; there was some discussion before Python 3 was released to remove them in favor of new Python tools(comprehensions, itertools, and whatever else), but they decided not to. You can't call that movement; they're now here to stay. Maybe when Python 4000 comes around Guido might consider it again and not get talked out of it this that time-- but there's no way that they're going to be deprecated or removed in the next ten years at this point. It'd be code-breakage on a massive scale. Some people don't like them. But there's quite a few Pythoniacs who do and use them to great effect. Don't make it sound like they're some dangerous thing which has a looming deprecation over its head so it shouldn't be bothered with. --S -------------- next part -------------- An HTML attachment was scrubbed... URL: From ross.jett at gmail.com Fri May 8 23:17:15 2009 From: ross.jett at gmail.com (Ross) Date: Fri, 8 May 2009 20:17:15 -0700 (PDT) Subject: slice iterator? Message-ID: <44c5bef2-d19c-49ac-ad3b-24ade0470ae1@z7g2000vbh.googlegroups.com> I have a really long list that I would like segmented into smaller lists. Let's say I had a list a = [1,2,3,4,5,6,7,8,9,10,11,12] and I wanted to split it into groups of 2 or groups of 3 or 4, etc. Is there a way to do this without explicitly defining new lists? If the above problem were to be split into groups of 3, I've tried something like: start = 0 stop = 3 for i in range(len(a)): segment = a[start:stop] print segment start += stop stop += stop Unfortunately start and stop don't increment using this code. Ideally, my outcome would be [1,2,3] [4,5,6] [7,8,9] [10,11,12] From http Fri May 8 23:29:51 2009 From: http (Paul Rubin) Date: 08 May 2009 20:29:51 -0700 Subject: slice iterator? References: <44c5bef2-d19c-49ac-ad3b-24ade0470ae1@z7g2000vbh.googlegroups.com> Message-ID: <7xprejoswg.fsf@ruckus.brouhaha.com> Ross writes: > I have a really long list that I would like segmented into smaller > lists. Let's say I had a list a = [1,2,3,4,5,6,7,8,9,10,11,12] and I > wanted to split it into groups of 2 or groups of 3 or 4, etc. Is there > a way to do this without explicitly defining new lists? That question comes up so often it should probably be a standard library function. Anyway, here is an iterator, if that's what you want: >>> from itertools import islice >>> a = range(12) >>> xs = iter(lambda x=iter(a): list(islice(x,3)), []) >>> print list(xs) [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]] Of course, as the saying goes, there's more than one way to do it ;-) From davea at ieee.org Fri May 8 23:45:46 2009 From: davea at ieee.org (Dave Angel) Date: Fri, 08 May 2009 23:45:46 -0400 Subject: import overwrites __name__ In-Reply-To: References: Message-ID: <4A04FC6A.3020802@ieee.org> Marco wrote: > Marco wrote: > > >> Hi, >> >> There happened something that I do not understand. Actually I don't even >> know how it can be possible. >> >> I import a module and then the name space of the importing module seems do >> be overwritten. >> >> my_name = __name__ >> print my_name >> print len(dir()) >> from x import y as z >> print __name__ >> print len(dir()) >> print my_name >> >> -> >> __main__ >> 119 >> x >> 117 >> unhandled NameError "name 'my_name' is not defined" >> >> The module x is from me, and I am not aware of doing anything cruel there. >> What evil thing can be done in its sub module that can result in that >> strange thing? >> > > > I just discovered the .pyc file for the main module is not created. When I > delete the old pyc and import the main module from the python shell it works > fine. But when I start it with "python main_module.py" the .pyc is not > created and it happens as shown above. > > So what leads to the behavior that the .pyc is not created? > > br > Marco > > > As others have said, you didn't post nearly enough of your code to duplicate the problem. But I have a reasonable guess: I suspect you've got two modules that each import the other. If you run one of them as the (__main__) script from the command line, you get some pretty weird behavior. To put it simply, don't ever do an import of "main_module" if you expect to be able to run "python main_module.py" I suspect the underlying cause is that that module is called __name__ ( you know that if you've ever coded if __name__ == "__main__" When you try to import it by its actual file name, it thinks it's a new one. And you don't ever want two instances of the same module. You end up with two copies of the global variables, and some code refers to one and some refers to the other, for example. You can get into trouble any time you have mutual importing (a imports b., and b import a) or indirect mutual importing (a imports b, which imports c, which imports a). Many languages have similar problems, as do Windows DLL's. But it's even worse if your __main__ script is in the mix. As Stephen suggests, write a very small script which does the initial import. From research at johnohagan.com Fri May 8 23:48:48 2009 From: research at johnohagan.com (John O'Hagan) Date: Sat, 9 May 2009 03:48:48 +0000 Subject: slice iterator? In-Reply-To: <44c5bef2-d19c-49ac-ad3b-24ade0470ae1@z7g2000vbh.googlegroups.com> References: <44c5bef2-d19c-49ac-ad3b-24ade0470ae1@z7g2000vbh.googlegroups.com> Message-ID: <200905090348.48444.research@johnohagan.com> On Sat, 9 May 2009, Ross wrote: > lists. Let's say I had a list a = [1,2,3,4,5,6,7,8,9,10,11,12] and I > wanted to split it into groups of 2 or groups of 3 or 4, etc. Is there > a way to do this without explicitly defining new lists? If the above > problem were to be split into groups of 3, I've tried something like: > > start = 0 > stop = 3 > for i in range(len(a)): > ? ? segment = a[start:stop] > ? ? print segment > ? ? start += stop > ? ? stop += stop This doesn't work because you're looping over every element in your list (instead of every slice) and because you're incrementing the increment by using "stop" for two different things (endpoint and increment). This seems to work: >>> for i in range (len(a) / 3): ... segment = a[3 * i:3 * (i + 1)] ... print segment Regards, john From lie.1296 at gmail.com Fri May 8 23:58:46 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Sat, 09 May 2009 03:58:46 GMT Subject: I'm intrigued that Python has some functional constructions in the language. In-Reply-To: References: Message-ID: Casey Hawthorne wrote: > I'm intrigued that Python has some functional constructions in the > language. > > Would it be possible to more clearly separate the pure code (without > side effects) from the impure code (that deals with state changes, > I/O, etc.), so that the pure code could be compiled and have > aggressive functional transformations applied to it for efficiency. Choosing a better algorithm usually have much more impact on efficiency than programming to the language's optimization scheme. A slow algorithm will always be slow no matter how much (functional or regular) optimization in it[1]. If you bother efficiency that much, maybe python is not the best suited language for you. > That way, the syntax would be a lot easier to understand, than most > functional languages, like Haskell. Python's syntax is already easier to understand than most functional language, and it also support functional-style programming. However, python is still an imperative language, no matter how much functional functionalities (pun not intended) is in it. [1] of course there are certain types of optimizations -- that actually changes the algorithm -- that may have a significant impact, such as memoization. However if you know memoization well enough, you wouldn't want to activate it on all pure functions... From research at johnohagan.com Sat May 9 00:05:13 2009 From: research at johnohagan.com (John O'Hagan) Date: Sat, 9 May 2009 04:05:13 +0000 Subject: slice iterator? In-Reply-To: <200905090348.48444.research@johnohagan.com> References: <44c5bef2-d19c-49ac-ad3b-24ade0470ae1@z7g2000vbh.googlegroups.com> <200905090348.48444.research@johnohagan.com> Message-ID: <200905090405.14088.research@johnohagan.com> On Sat, 9 May 2009, John O'Hagan wrote: > On Sat, 9 May 2009, Ross wrote: > > lists. Let's say I had a list a = [1,2,3,4,5,6,7,8,9,10,11,12] and I > > wanted to split it into groups of 2 or groups of 3 or 4, etc. Is there > > a way to do this without explicitly defining new lists? If the above > > problem were to be split into groups of 3, I've tried something like: [..] > > This seems to work: > >>> for i in range (len(a) / 3): > > ... segment = a[3 * i:3 * (i + 1)] > ... print segment [...] But if len(a) is not divisible by the increment, that misses the last (short) slice, so using the step argument to range: >>> for i in range (0, len(a), 3): ... segment = a[i:i + 3] ... print segment is better. Or as a list comprehension: [a[i:i + 3] for i in range (0, len(a), 3)] HTH, John From nad at acm.org Sat May 9 00:14:32 2009 From: nad at acm.org (Ned Deily) Date: Sat, 09 May 2009 00:14:32 -0400 Subject: slice iterator? References: <44c5bef2-d19c-49ac-ad3b-24ade0470ae1@z7g2000vbh.googlegroups.com> <7xprejoswg.fsf@ruckus.brouhaha.com> Message-ID: In article <7xprejoswg.fsf at ruckus.brouhaha.com>, Paul Rubin wrote: > Ross writes: > > I have a really long list that I would like segmented into smaller > > lists. Let's say I had a list a = [1,2,3,4,5,6,7,8,9,10,11,12] and I > > wanted to split it into groups of 2 or groups of 3 or 4, etc. Is there > > a way to do this without explicitly defining new lists? > > That question comes up so often it should probably be a standard > library function. > > Anyway, here is an iterator, if that's what you want: > >>> from itertools import islice > >>> a = range(12) > >>> xs = iter(lambda x=iter(a): list(islice(x,3)), []) > >>> print list(xs) > [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]] > Of course, as the saying goes, there's more than one way to do it ;-) python2.6 itertools introduces the izip_longest function and the grouper recipe : def grouper(n, iterable, fillvalue=None): "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx" args = [iter(iterable)] * n return izip_longest(fillvalue=fillvalue, *args) -- Ned Deily, nad at acm.org From fetchinson at googlemail.com Sat May 9 00:34:54 2009 From: fetchinson at googlemail.com (Daniel Fetchinson) Date: Fri, 8 May 2009 21:34:54 -0700 Subject: mod_python and xml.dom.minidom In-Reply-To: <83b63d98-9871-47eb-8846-46404b70896e@z7g2000vbh.googlegroups.com> References: <83b63d98-9871-47eb-8846-46404b70896e@z7g2000vbh.googlegroups.com> Message-ID: On 5/8/09, dpapathanasiou wrote: > I wrote a python script called xml_utils.py which parses xml using > minidom. > > It works when it's run on its own, but when I try to import it and run > it inside a mod_python handler, I get this error: > > File "../common/xml_utils.py", line 80, in parse_item_attribute > File "/usr/lib/python2.5/xml/dom/minidom.py", line 1924, in > parseString > from xml.dom import expatbuilder > SystemError: Parent module 'xml.dom' not loaded > > Basically, it's the same problem I found in this post: > http://mail.python.org/pipermail/python-list/2007-January/424018.html > > This site (http://www.dscpl.com.au/wiki/ModPython/Articles/ > ExpatCausingApacheCrash) goes through a detailed explanation, and I > found that the version of pyexpat is newer than libexpat: > > # ldd /usr/local/apache2/bin/httpd | grep expat > libexpat.so.0 => /usr/local/apache2/lib/libexpat.so.0 (0xb7f71000) > # strings /usr/local/apache2/lib/libexpat.so.0 | grep expat_ > expat_1.95.2 > > $ python > Python 2.5.2 (r252:60911, Jan 4 2009, 17:40:26) > [GCC 4.3.2] on linux2 > Type "help", "copyright", "credits" or "license" for more information. >>>> import pyexpat >>>> pyexpat.version_info > (2, 0, 0) > > But this is where I'm stuck: the article suggests recompiling apache > with the newer version of expat. > > Apache's configure utility (I'm using httpd version 2.2.11) doesn't > explicitly describe an expat library option. > > Also, if libexpat is version 1.95.2, wouldn't I have to get version > 2.0 to be compatible with pyexpat? > > If anyone has any advice or suggestions, I'd appreciate hearing them. My only advice is, don't use mod_python. The project is dead, you should use mod_wsgi instead: http://code.google.com/p/modwsgi/ Cheers, Daniel -- Psss, psss, put it down! - http://www.cafepress.com/putitdown From timr at probo.com Sat May 9 00:36:35 2009 From: timr at probo.com (Tim Roberts) Date: Fri, 08 May 2009 21:36:35 -0700 Subject: ISO exemplary Python scripts References: Message-ID: <802a051qoqmpnih0j2su9hhriqumagu326@4ax.com> kj wrote: > >I'm learning python, for the umpteenth time. This time I decided >to start out by writing Python scripts for everyday tasks. Most >of these scripts are meant to be used only once or twice, but a >few of them have become useful enough that I'd like to make them >a little bit more solid... > >I'd like to learn from well-written Python scripts, with well-structured >options parsing, good error and help messages, clear organization, >etc. > >Can someone point me to good examples of Python scripts? The standard library that was installed with your interpreter is one of the best repositories. It contains hundreds of working, well-tested scripts, most of which have the ability to run by themselves. -- Tim Roberts, timr at probo.com Providenza & Boekelheide, Inc. From anuraguniyal at yahoo.com Sat May 9 00:44:39 2009 From: anuraguniyal at yahoo.com (anuraguniyal at yahoo.com) Date: Fri, 8 May 2009 21:44:39 -0700 (PDT) Subject: unicode bit me References: <0vadnQqkdrdQz5nXnZ2dnUVZ_uOdnZ2d@pdx.net> Message-ID: sorry for not being specfic and not given all info """ Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52) [GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2 'Linux-2.6.24-19-generic-i686-with-debian-lenny-sid' """ My question has not much to do with stdout because I am able to print unicode so print unicode(a) works print unicode([a]) doesn't without print too s1 = u"%s"%a works s2 = u"%s"%[a] doesn't niether does s3 = u"%s"%unicode([a]) error is UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 1: ordinal not in range(128) so question is how can I use a list of object whose representation contains unicode in another unicode string I am now using __repr__ = unicode(self).encode("utf-8") but it give error anyway From ldo at geek-central.gen.new_zealand Sat May 9 00:47:15 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Sat, 09 May 2009 16:47:15 +1200 Subject: I'm intrigued that Python has some functional constructions in the language. References: <44350436-de1a-451e-95f5-13b902a902ce@s21g2000vbb.googlegroups.com> <692b7ae8-0c5b-498a-a012-51bda980f3b9@s28g2000vbp.googlegroups.com> Message-ID: In message <692b7ae8-0c5b-498a- a012-51bda980f3b9 at s28g2000vbp.googlegroups.com>, namekuseijin wrote: > On May 8, 6:48 pm, Lawrence D'Oliveiro central.gen.new_zealand> wrote: >> In message , namekuseijin wrote: >> >> > Carl Banks escreveu: >> >> >> 2. However, functional programming is cryptic at some level no matter >> >> how nice you make the syntax. >> >> > When your program is nothing but function definition and function >> > application, syntax is meaningless. >> >> On the contrary, syntax is vital to the correct interpretation of >> functional constructs. See, for example, >> . > > Yeah, it's important in an imperative language ... That was an example of functional programming. From ldo at geek-central.gen.new_zealand Sat May 9 00:48:48 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Sat, 09 May 2009 16:48:48 +1200 Subject: SQL and CSV References: <1d2bb50f-2e3f-429b-aa98-7e1fe1f011b0@t10g2000vbg.googlegroups.com> Message-ID: In message , andrew cooke wrote: > Lawrence D'Oliveiro wrote: >> In message , Peter Otten wrote: >> >>> While it may not matter here using placeholders instead of manually >>> escaping user-provided values is a good habit to get into. >> >> Until you hit things it can't deal with. > > Can you expand on your comment? I assume you are thinking of how the > library might handle some strange class. Consider something as simple as a "like" clause. From Scott.Daniels at Acm.Org Sat May 9 00:49:43 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Fri, 08 May 2009 21:49:43 -0700 Subject: slice iterator? In-Reply-To: <44c5bef2-d19c-49ac-ad3b-24ade0470ae1@z7g2000vbh.googlegroups.com> References: <44c5bef2-d19c-49ac-ad3b-24ade0470ae1@z7g2000vbh.googlegroups.com> Message-ID: Ross wrote: > I have a really long list that I would like segmented into smaller > lists. Let's say I had a list a = [1,2,3,4,5,6,7,8,9,10,11,12] and I > wanted to split it into groups of 2 or groups of 3 or 4, etc. Is there > a way to do this without explicitly defining new lists? If the above > problem were to be split into groups of 3, I've tried something like: > ... Ideally, my outcome would be > [1,2,3] > [4,5,6] > [7,8,9] > [10,11,12] for i in range(0, len(a), 3): segment = a[i : i +3] print segment --Scott David Daniels Scott.Daniels at Acm.Org From ldo at geek-central.gen.new_zealand Sat May 9 00:50:55 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Sat, 09 May 2009 16:50:55 +1200 Subject: SQL and CSV References: <1d2bb50f-2e3f-429b-aa98-7e1fe1f011b0@t10g2000vbg.googlegroups.com> Message-ID: In message , andrew cooke wrote: > even if you're not open to injection attacks, you're still less likely to > get escaping correct than a puprose written, widely used library. Escaping isn't really that hard to do. For example, the rules for MySQL literals are clearly spelled out here . You can easily check that against my quoting routines here . From apt.shansen at gmail.com Sat May 9 00:50:59 2009 From: apt.shansen at gmail.com (Stephen Hansen) Date: Fri, 8 May 2009 21:50:59 -0700 Subject: mod_python and xml.dom.minidom In-Reply-To: References: <83b63d98-9871-47eb-8846-46404b70896e@z7g2000vbh.googlegroups.com> Message-ID: <7a9c25c20905082150y6a8bd08cnaa32fefdfce8ae1e@mail.gmail.com> > > My only advice is, don't use mod_python. The project is dead, you > should use mod_wsgi instead: http://code.google.com/p/modwsgi/ > To echo what Daniel said, mod_wsgi is really the way to go. It might still not work in "embedded" mode where like mod_python the Python interpreter is in the Apache instance thus there might be binary linking conflicts, but you can use it in daemon mode where it launches external python processes and passes requests back and forth through them. That's not as slow as it sounds too :) Also maybe check out lxml or ElementTree (cElementTree); minidom is about *the* slowest way to parse XML in Python I believe. And is terribly unpythonic-- which may be why you made xml_utils, I dunno. Check out lxml/ElementTree :) --S -------------- next part -------------- An HTML attachment was scrubbed... URL: From pavlovevidence at gmail.com Sat May 9 00:52:22 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Fri, 8 May 2009 21:52:22 -0700 (PDT) Subject: I'm intrigued that Python has some functional constructions in the language. References: <44350436-de1a-451e-95f5-13b902a902ce@s21g2000vbb.googlegroups.com> <9e68a155-177d-4e03-97b6-c19cbb986515@z19g2000vbz.googlegroups.com> <7fa66f4e-e88c-4fb4-a509-911756814f89@o27g2000vbd.googlegroups.com> Message-ID: On May 8, 7:19?pm, namekuseijin wrote: > On May 8, 10:13?pm, Carl Banks wrote: > > > On May 8, 5:47?pm, namekuseijin wrote: > > > > My point is that when all you do is call functions, syntax is > > > irrelevant. ?You call functions pretty much in the same way regardless > > > of language: ?functionname, optionalOpenPar, parameters, > > > optionalClosePar. > > > then... > > > > Functional programming is all about defining functions and applying > > > functions. ?Core ML, Haskell and Scheme are all like that, > > > Yet all three use a different syntax to call functions, none of them > > the "pretty much the same way" you listed above. > > It's still functionName arguments AFAIK. ?Some using parentheses > around the arguments, some around all, some not using parentheses at > all. Some using commas, some not, some grouping arguments by arity, some not, some allowing keyword argument, some not, etc. Point is, even the thing you claim hardly varies between languages does vary a significant bit, but it's not nearly as much as other syntactic differences in functional languages. > > Haskell and Python have syntax for list operations, that matters. > > > Haskell nexts using indentation, the others nest using tokens(**), > > that matters. > > In Haskell, Lisp and other functional programming languages, any extra > syntax gets converted into the core lambda constructs. So? The user still uses that syntax, so how can you claim it doesn't matter? >?In Lisp > languages, that syntax is merely user-defined macros, but in Haskell > it's builtin the compiler for convenience. I don't even know what you're saying here > > I can go on, but you get the idea. ?Point is: functional programmint > > isn't "nothing but calling functions". > > Oh yes, functional programming is all about function definition and > function calling. Fine. Functional programming isn't "nothing but calling functions USING FUNCTION-CALL SYNTAX". >?You have a point about higher order functions, but > that's really only useful as far as your lambda expressions are useful > -- that is, conveniently defining anonymous functions on-the-fly and > immediately applying them. I made no such point. You may be confusing me with Paul, and I can't imagine what higher-order functions have to do with whether syntax is important or not. Carl Banks From ptmcg at austin.rr.com Sat May 9 01:04:33 2009 From: ptmcg at austin.rr.com (Paul McGuire) Date: Fri, 8 May 2009 22:04:33 -0700 (PDT) Subject: slice iterator? References: <44c5bef2-d19c-49ac-ad3b-24ade0470ae1@z7g2000vbh.googlegroups.com> <7xprejoswg.fsf@ruckus.brouhaha.com> Message-ID: On May 8, 11:14?pm, Ned Deily wrote: > In article <7xprejoswg.... at ruckus.brouhaha.com>, > ?Paul Rubin wrote: > > > > > > > Ross writes: > > > I have a really long list that I would like segmented into smaller > > > lists. Let's say I had a list a = [1,2,3,4,5,6,7,8,9,10,11,12] and I > > > wanted to split it into groups of 2 or groups of 3 or 4, etc. Is there > > > a way to do this without explicitly defining new lists? > > > That question comes up so often it should probably be a standard > > library function. > > > Anyway, here is an iterator, if that's what you want: > > ? ?>>> from itertools import islice > > ? ?>>> a = range(12) > > ? ?>>> xs = iter(lambda x=iter(a): list(islice(x,3)), []) > > ? ?>>> print list(xs) > > ? ?[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]] > > Of course, as the saying goes, there's more than one way to do it ;-) > > python2.6 itertools introduces the izip_longest function and the grouper > recipe : > > def grouper(n, iterable, fillvalue=None): > ? ? "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx" > ? ? args = [iter(iterable)] * n > ? ? return izip_longest(fillvalue=fillvalue, *args) > > -- > ?Ned Deily, > ?n... at acm.org- Hide quoted text - > > - Show quoted text - Here's a version that works pre-2.6: >>> grouper = lambda iterable,size,fill=None : zip(*[(iterable+[fill,]*(size-1))[i::size] for i in range(size)]) >>> a = range(12) >>> grouper(a,6) [(0, 1, 2, 3, 4, 5), (6, 7, 8, 9, 10, 11)] >>> grouper(a,5) [(0, 1, 2, 3, 4), (5, 6, 7, 8, 9), (10, 11, None, None, None)] >>> grouper(a,3) [(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, 10, 11)] -- Paul From stefan_ml at behnel.de Sat May 9 01:07:25 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sat, 09 May 2009 07:07:25 +0200 Subject: mod_python and xml.dom.minidom In-Reply-To: References: <83b63d98-9871-47eb-8846-46404b70896e@z7g2000vbh.googlegroups.com> Message-ID: <4a050f8d$0$31862$9b4e6d93@newsspool3.arcor-online.net> Daniel Fetchinson wrote: > On 5/8/09, dpapathanasiou wrote: >> I wrote a python script called xml_utils.py which parses xml using >> minidom. > > My only advice is, don't use mod_python. The project is dead, you > should use mod_wsgi instead: http://code.google.com/p/modwsgi/ Now that we're at it anyway: don't use minidom, either. It's rather slow and very memory expensive. The stdlib comes with ElementTree since Python 2.5. http://effbot.org/zone/celementtree.htm#benchmarks Stefan From ldo at geek-central.gen.new_zealand Sat May 9 02:44:47 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Sat, 09 May 2009 18:44:47 +1200 Subject: bulk_copy Message-ID: Every now and then I've had the need to copy a bunch of records in a MySQL table--perhaps take a copy of some production data into a test database to track down a bug, that kind of thing. So I wrote a utility to do it. From anuraguniyal at yahoo.com Sat May 9 03:04:46 2009 From: anuraguniyal at yahoo.com (anuraguniyal at yahoo.com) Date: Sat, 9 May 2009 00:04:46 -0700 (PDT) Subject: unicode bit me References: Message-ID: <977863c9-c4d9-4563-985a-3849d6ed0d75@d2g2000pra.googlegroups.com> also not sure why (python 2.5) print a # works print unicode(a) # works print [a] # works print unicode([a]) # doesn't works From george.sakkis at gmail.com Sat May 9 03:48:53 2009 From: george.sakkis at gmail.com (George Sakkis) Date: Sat, 9 May 2009 00:48:53 -0700 (PDT) Subject: Self function References: <5e36fb9c-dc1e-40de-bbe4-55eb46210b00@v17g2000vbb.googlegroups.com> <1292fea0-96fb-4a21-b39d-b6ce610ea6b2@t10g2000vbg.googlegroups.com> <4f13a110-b3e5-4de5-b6d6-0d64da20b61e@r34g2000vbi.googlegroups.com> <1241649200.4a0210301f77e@mail.uh.cu> Message-ID: On May 6, 10:32?pm, Luis Alberto Zarrabeitia Gomez wrote: > A bit offtopic: a while ago I think I saw a recipe for a decorator that, via > bytecode hacks, would bind otherwise global names to the local namespace of the > function. Can anyone remember it/point me to it? Maybe you saw http://code.activestate.com/recipes/277940/ ? George From iwan at reahl.org Sat May 9 04:02:08 2009 From: iwan at reahl.org (Iwan) Date: Sat, 9 May 2009 01:02:08 -0700 (PDT) Subject: How to debug this import problem? References: <76j8vcF1crfkoU1@mid.uni-berlin.de> Message-ID: Hi Scott, Diez, On May 8, 8:21?pm, Scott David Daniels wrote: > Diez B. Roggisch wrote: > > Try putting an "import pdb; pdb.set_trace()" on top of the decimal module. [snip] > You can also run Python with the "-v" or "-vv" flags to get output > about exactly what files are getting imported from the file > system and when. ?"-v" iswhat it got, "-vv" is everywhere it tries. Thanks, yes we did do the pdb.set_trace(), but everything looked fine (ie, the paths used to import each module from was the same each time) in there except for the fact that id(sys) and id(decimal) were different each time decimal got imported. Python -vv also reveals that the paths used to import the modules from are always the same paths and correct. However, on the stack trace, there is another strangeness: We have a little script that loads the tests that have to be run. This loading happens 3 times, and the module is imported 3 times - each time as a result of loading tests. This happens with the following line: module = __import__(moduleName, globals(), locals(), [className]) I was wondering whether this could have something to do with it? Regards - Iwan From __peter__ at web.de Sat May 9 04:09:24 2009 From: __peter__ at web.de (Peter Otten) Date: Sat, 09 May 2009 10:09:24 +0200 Subject: heapq.merge with key= References: Message-ID: Kevin D. Smith wrote: > On 2009-05-07 23:48:43 -0500, Chris Rebert said: > >> On Thu, May 7, 2009 at 2:23 PM, Kevin D. Smith >> wrote: >>> I need the behavior of heapq.merge to merge a bunch of results from a >>> database. I was doing this with sorted(itertools.chain(...), key= >> ...), but >>> I would prefer to do this with generators. My issue is that I need >> the key >>> argument to sort on the correct field in the database. heapq.merge >> doesn't >>> have this argument and I don't understand the code enough to know if >>> it's possible to add it. Is this enhancement possible without >>> drasticall >> y >>> changing the current code? >> >> I think so. Completely untested code: >> >> def key(ob): >> #code here >> >> class Keyed(object): >> def __init__(self, obj): >> self.obj = obj >> def __cmp__(self, other): >> return cmp(key(self.obj), key(other.obj)) >> >> def keyify(gen): >> for item in gen: >> yield Keyed(item) >> >> def stripify(gen): >> for keyed in gen: >> yield keyed.obj >> >> merged = stripify(merge(keyify(A), keyify(B), keyify(C))) #A,B,C being >> the iterables > > Ah, that's not a bad idea. I think it could be simplified by letting > Python do the comparison work as follows (also untested). > > def keyify(gen, key=lamda x:x): > for item in gen: > yield (key(item), item) > > def stripify(gen): > for item in gen: > yield item[1] This is not as general. It makes sorting unstable and can even fail if the items are unorderable: >>> def decorate(gen, key): ... for item in gen: ... yield key(item), item ... >>> def undecorate(gen): ... for item in gen: ... yield item[-1] ... >>> sorted(decorate([1j, -1j, 1+1j], key=lambda c: c.real)) Traceback (most recent call last): File "", line 1, in TypeError: no ordering relation is defined for complex numbers One way to fix it: >>> def decorate(gen, key): ... for index, item in enumerate(gen): ... yield key(item), index, item ... >>> sorted(decorate([1j, -1j, 1+1j], key=lambda c: c.real)) [(0.0, 0, 1j), (0.0, 1, -1j), (1.0, 2, (1+1j))] >>> list(undecorate(_)) [1j, -1j, (1+1j)] Peter From bvdmyrby at jyxk16274849.net Sat May 9 04:30:46 2009 From: bvdmyrby at jyxk16274849.net (Raoul Gough) Date: Sat, 09 May 2009 09:30:46 +0100 Subject: MacPython 3.0.1 installation problem, no /usr/local/bin/python* Message-ID: I just installed MacPython 3.0.1 (woot!) but the installer didn't seem to do an absolutely complete job. According to the ReadMe.txt, "The installer puts [...] command-line tools in /usr/local/bin [...]". This didn't happen - even after the install reported Success, there were no python files or symlinks under /usr/local/bin. Also, I'm guessing there should have been a symlink to the current version under /Library/Frameworks/Python.framework/Versions, which wasn't there either: $ cd /Library/Frameworks/Python.framework $ ls -l Python lrwxr-xr-x 1 root admin 23 May 9 08:34 Python -> Versions/Current/Python $ ls -l Versions/Current ls: Versions/Current: No such file or directory So I fixed this all up as follows: $ cd /Library/Frameworks/Python.framework/Versions $ sudo ln -s 3.0 Current $ cd /usr/local/bin $ sudo ln -s $(find /Library/Frameworks/Python.framework/Versions/Current/bin -type f) . So did something go wrong with the installer, or is it all supposed to work somehow differently? -- Raoul Gough. From george.sakkis at gmail.com Sat May 9 04:37:32 2009 From: george.sakkis at gmail.com (George Sakkis) Date: Sat, 9 May 2009 01:37:32 -0700 (PDT) Subject: Decorating methods - where do my arguments go? References: Message-ID: On May 8, 11:33 am, Mikael Olofsson wrote: > Hi all! > > I have long tried to avoid decorators, but now I find myself in a > situation where I think they can help. I seem to be able to decorate > functions, but I fail miserably when trying to decorate methods. The > information I have been able to find on-line focuses on decorating > functions, and do not mention any special problem regarding methods. > > Consider the following example. I start by defining a simple decorator: > > >>> class test_decorator(object): > ... def __init__(self,func): > ... self._func = func > ... def __call__(self, *args): > ... print 'Decorator:', args > ... self._func(*args) > > Then I decorate a function: > > >>> @test_decorator > ... def func(*args): > ... print 'Function: ', args > > Let's try that: > > >>> func(1,2,3) > Decorator: (1, 2, 3) > Function: (1, 2, 3) > > OK! That was what I expected. Let's decorate a method: > > >>> class cls(object): > ... @test_decorator > ... def meth(self,*args): > ... print 'Method: ', args > > Then try that: > > >>> cls().meth(1,2,3) > Decorator: (1, 2, 3) > Method: (2, 3) > > Oops! That's weird. I had expected - or at least wanted - the same > result as for the function above. > > Where did the first argument go? If it was sent to the original method > meth as the real first argument (self), then why did I not get an exception? > > More importantly: How do I write a decorator that does not drop arguments? > > I guess it all has to do with the fact that the returned callable isn't > a method of cls. Is it possible to write a decorator that returns a > callable that is a method of cls, when used on methods in cls? Yes, just return an actual function from the decorator instead of a callable object: def test_decorator2(func): def wrapper(*args): print 'Decorator2:', args func(*args) return wrapper class cls(object): @test_decorator def meth(self,*args): print 'Method: ', args @test_decorator2 def meth2(self,*args): print 'Method2: ', args >>> cls.meth <__main__.test_decorator object at 0x87663cc> >>> cls.meth2 >>> cls().meth2(1,2,3) Decorator2: (<__main__.cls object at 0x8766ecc>, 1, 2, 3) Method2: (1, 2, 3) The reason this works is that functions are already descriptors, so you don't have to explicitly define __get__() as you would for a new callable class (see Peter's example). HTH, George From tuomas.vesterinen at iki.fi Sat May 9 04:57:48 2009 From: tuomas.vesterinen at iki.fi (Tuomas Vesterinen) Date: Sat, 09 May 2009 11:57:48 +0300 Subject: Problems with datetime.datetime.strptime Message-ID: <4a05458d$0$24784$9b536df3@news.fv.fi> I hoped that I could get rid of my special module _strptime2 when porting to Python 3.0. But testing is a disappointment. Python 3.0.1 (r301:69556, Apr 14 2009, 14:30:31) [GCC 4.1.2 20070925 (Red Hat 4.1.2-27)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import locale, datetime >>> def test1(locale_str, val): ... print(locale.setlocale(locale.LC_ALL, locale_str), ':') ... test_cases = [ ... locale.nl_langinfo(locale.D_T_FMT), # test default formats ... locale.nl_langinfo(locale.D_FMT), ... locale.nl_langinfo(locale.T_FMT) ... ] ... for form in test_cases: ... string = val.strftime(form) ... print(" strftime(%s)=%s" % (repr(form), repr(string))) ... print(" strptime(%s,%s)=" % (repr(string), repr(form)), end=' ') ... try: ... val2 = datetime.datetime.strptime(string, form) ... except ValueError as e: ... print('\n ', str(e)) ... else: ... print('\n ', str(val2)) ... >>> now = datetime.datetime.now() >>> for loc in ('C', 'en_US.UTF-8', 'fi_FI.UTF-8'): ... test1(loc, now) ... C : strftime('%a %b %e %H:%M:%S %Y')='Sat May 9 11:26:12 2009' strptime('Sat May 9 11:26:12 2009','%a %b %e %H:%M:%S %Y')= 'e' is a bad directive in format '%a %b %e %H:%M:%S %Y' strftime('%m/%d/%y')='05/09/09' strptime('05/09/09','%m/%d/%y')= 2009-05-09 00:00:00 strftime('%H:%M:%S')='11:26:12' strptime('11:26:12','%H:%M:%S')= 1900-01-01 11:26:12 en_US.UTF-8 : strftime('%a %d %b %Y %r %Z')='Sat 09 May 2009 11:26:12 AM ' strptime('Sat 09 May 2009 11:26:12 AM ','%a %d %b %Y %r %Z')= 'r' is a bad directive in format '%a %d %b %Y %r %Z' strftime('%m/%d/%Y')='05/09/2009' strptime('05/09/2009','%m/%d/%Y')= 2009-05-09 00:00:00 strftime('%r')='11:26:12 AM' strptime('11:26:12 AM','%r')= 'r' is a bad directive in format '%r' fi_FI.UTF-8 : strftime('%a %e. %Bta %Y %T')='la 9. toukokuuta 2009 11:26:12' strptime('la 9. toukokuuta 2009 11:26:12','%a %e. %Bta %Y %T')= 'e' is a bad directive in format '%a %e. %Bta %Y %T' strftime('%d.%m.%Y')='09.05.2009' strptime('09.05.2009','%d.%m.%Y')= 2009-05-09 00:00:00 strftime('%T')='11:26:12' strptime('11:26:12','%T')= 'T' is a bad directive in format '%T' What to do. Tuomas Vesterinen From ggardiner at iee.org Sat May 9 05:01:44 2009 From: ggardiner at iee.org (Geoff Gardiner) Date: Sat, 09 May 2009 10:01:44 +0100 Subject: How do I test the integrity of a Python installation in Debian and Ubuntu In-Reply-To: References: Message-ID: <4A054678.40503@iee.org> Aahz wrote: > How important is the apt-get requirement? That's a significant point, thank you. Two parts to the response: a) I don't feel that I am sufficiently expert to launch into compilation of Python if I can avoid it. b) I hope that I can put all platform risk into the lap of the hosting provider, and apt-get is probably the only delivery mechanism they will take any responsibility for. (I want to use their 'standard'.) All the best, Geoff From chris at simplistix.co.uk Sat May 9 05:06:52 2009 From: chris at simplistix.co.uk (Chris Withers) Date: Sat, 09 May 2009 10:06:52 +0100 Subject: [Python-Dev] PEP 382: little help for stupid people? In-Reply-To: <49FB3384.1030106@v.loewis.de> References: <49D4DA72.60401@v.loewis.de> <49D52115.6020001@egenix.com> <49D66C6E.3090602@v.loewis.de> <49DB475B.8060504@egenix.com> <20090407140317.EBD383A4063@sparrow.telecommunity.com> <49DB6A1F.50801@egenix.com> <20090407174355.B62983A4063@sparrow.telecommunity.com> <49E4A58F.70309@egenix.com> <20090414162603.70C843A4100@sparrow.telecommunity.com> <49E4F93B.6010802@egenix.com> <20090415003026.B0A783A4114@sparrow.telecommunity.com> <49E59202.6050809@egenix.com> <20090415144147.6845F3A4100@sparrow.telecommunity.com> <49E60832.8030806@egenix.com> <49FB2398.5000708@simplistix.co.uk> <49FB261F.9080306@v.loewis.de> <49FB2A2A.4090606@simplistix.co.uk> <49FB3384.1030106@v.loewis.de> Message-ID: <4A0547AC.7060103@simplistix.co.uk> Martin v. L?wis wrote: > Ok, so create three tar files: > > 1. base.tar, containing > > simplistix/ > simplistix/__init__.py So this __init__.py can have code in it? And base.tar can have other modules and subpackages in it? What happens if the base and an addon both define a package called simplistix.somepackage? > 2. addon1.tar, containing > > simplistix/addon1.pth (containing a single "*") What does that * mean? I thought .pth files just had python in them? > Unpack each of them anywhere on sys.path, in any order. How would this work if base, addon1 and addon2 were eggs managed by buildout or setuptools? cheers, Chris -- Simplistix - Content Management, Zope & Python Consulting - http://www.simplistix.co.uk From martin at v.loewis.de Sat May 9 05:27:22 2009 From: martin at v.loewis.de (=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Sat, 09 May 2009 11:27:22 +0200 Subject: [Python-Dev] PEP 382: little help for stupid people? In-Reply-To: <4A0547AC.7060103@simplistix.co.uk> References: <49D4DA72.60401@v.loewis.de> <49D52115.6020001@egenix.com> <49D66C6E.3090602@v.loewis.de> <49DB475B.8060504@egenix.com> <20090407140317.EBD383A4063@sparrow.telecommunity.com> <49DB6A1F.50801@egenix.com> <20090407174355.B62983A4063@sparrow.telecommunity.com> <49E4A58F.70309@egenix.com> <20090414162603.70C843A4100@sparrow.telecommunity.com> <49E4F93B.6010802@egenix.com> <20090415003026.B0A783A4114@sparrow.telecommunity.com> <49E59202.6050809@egenix.com> <20090415144147.6845F3A4100@sparrow.telecommunity.com> <49E60832.8030806@egenix.com> <49FB2398.5000708@simplistix.co.uk> <49FB261F.9080306@v.loewis.de> <49FB2A2A.4090606@simplistix.co.uk> <49FB3384.1030106@v.loewis.de> <4A0547AC.7060103@simplistix.co.uk> Message-ID: <4A054C7A.8020806@v.loewis.de> >> Ok, so create three tar files: >> >> 1. base.tar, containing >> >> simplistix/ >> simplistix/__init__.py > > So this __init__.py can have code in it? That's the point, yes. > And base.tar can have other modules and subpackages in it? Certainly, yes. > What happens if the base and an addon both define a package called > simplistix.somepackage? Depends on whether simplistix.somepackage is a namespace package (it should). If so, they get merged just as any other namespace package. >> 2. addon1.tar, containing >> >> simplistix/addon1.pth (containing a single "*") > > What does that * mean? See PEP 382 (search for "*"). > I thought .pth files just had python in them? Not at all - they never did. They have paths in them. >> Unpack each of them anywhere on sys.path, in any order. > > How would this work if base, addon1 and addon2 were eggs managed by > buildout or setuptools? What is a managed egg (i.e. what kind of management does buildout or setuptools apply to it)? Regards, Martin From nick at craig-wood.com Sat May 9 05:30:06 2009 From: nick at craig-wood.com (Nick Craig-Wood) Date: Sat, 09 May 2009 04:30:06 -0500 Subject: ANN: Python process utility (psutil) 0.1.2 released References: <65f15b53-aacf-4651-a113-960cce25f205@p4g2000vba.googlegroups.com> Message-ID: Giampaolo Rodola' wrote: > psutil is a module providing an interface for retrieving information > on running processes and system utilization (CPU, memory) in a > portable way by using Python, implementing many functionalities > offered by tools like ps, top and Windows task manager. > It currently supports Linux, OS X, FreeBSD and Windows. Very nice! Maybe you should make Process() mean Process(os.getpid()) as I often want ask how much memory/cpu is my process using but rarely want to ask about other processes? It would be nice if "pydoc psutil" returned something useful also! You could do this by replacing your current __init__.py (which just contains "from _psutil import *") with _psutil.py -- Nick Craig-Wood -- http://www.craig-wood.com/nick From Florian.Wollenschein at FernUni-Hagen.de Sat May 9 05:41:25 2009 From: Florian.Wollenschein at FernUni-Hagen.de (Florian Wollenschein) Date: Sat, 09 May 2009 11:41:25 +0200 Subject: Code - what could be done better? Message-ID: Hi all, here's the main code of thc, my txt to html converter. Since I'm a beginner it is far, far, faaaaaaar away from perfect or even good :-) What could be done better? ==================================================================================== #!/usr/bin/env python # -*- coding: utf-8 -*- # thc # Copyright (C) 2007 - 2009 Listick Lorch # # # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA import sys import webbrowser import texts_en as texts # you have to change only the countrycode like texts_de ... from PyQt4 import QtGui, QtCore from mainframe_ui import Ui_MainWindow as Frm import abtDialogFrame class ThcMainframe(QtGui.QMainWindow, Frm): def __init__(self): QtGui.QMainWindow.__init__(self) self.setupUi(self) # ---MEMBERS--- self.file_to_convert = '' self.file_to_write = '' self.txt_file_to_convert = '' # stores the text of the .txt file self.bgColor_R = hex(255)[2:].zfill(2) self.bgColor_G = hex(255)[2:].zfill(2) self.bgColor_B = hex(255)[2:].zfill(2) self.font_size = 0 # ---SLOTS--- # browse file to convert self.connect(self.btnBrowseFileConv, QtCore.SIGNAL("clicked()"), self.onBrowseFileConv) # browse file to write self.connect(self.btnBrowseFileWrite, QtCore.SIGNAL("clicked()"), self.onBrowseFileWrite) # convert self.connect(self.btnConvert, QtCore.SIGNAL("clicked()"), self.onConvert) # show result self.connect(self.btnShowResult, QtCore.SIGNAL("clicked()"), self.onShowResult) # select color self.connect(self.btnColorSelect, QtCore.SIGNAL("clicked()"), self.SelectColor) # font size self.connect(self.horizontalSlider, QtCore.SIGNAL("changed()"), self.onFontSizeChanged) # help menu self.connect(self.actionGo_to_listick_org, QtCore.SIGNAL("triggered()"), self.GoToListickOrg) self.connect(self.actionAbout, QtCore.SIGNAL("triggered()"), self.OpenAbout) # quit self.connect(self.btnQuit, QtCore.SIGNAL("clicked()"), self.onQuit) self.connect(self.actionExit, QtCore.SIGNAL("triggered()"), self.onQuit) def onBrowseFileConv(self): self.file_to_convert = QtGui.QFileDialog.getOpenFileName(self, texts.FileMenuOpen, '/home', '*.txt') self.inpFileConv.setText(self.file_to_convert) def onBrowseFileWrite(self): self.file_to_write = QtGui.QFileDialog.getSaveFileName(self, texts.FileMenuSaveAs, '/home', '*.htm*') self.inpFileWrite.setText(self.file_to_write) def onConvert(self): # read the title html_file_title = str(self.inpTitle.displayText()) # read the txt file_open = open(self.file_to_convert) print texts.FileOpened self.txtOutput.insertPlainText(texts.FileOpened) self.txt_file_to_convert = file_open.read() file_open.close() print texts.FileClosed self.txtOutput.insertPlainText(texts.FileClosed) # write html file_open = open(self.file_to_write, "w") print texts.WritingFile self.txtOutput.insertPlainText(texts.WritingFile) strict_or_transitional = {True: 'Transitional', False: 'Strict'} spec = strict_or_transitional[self.rdioBtnTransitional.isChecked()] doctype = '\n' % spec file_open.write(doctype) file_open.write('\n') # HTML head stuff file_open.write('\n') file_open.write('\n') file_open.write('%s\n' % html_file_title) file_open.write('\n' % texts.versionInfo) file_open.write('\n') #head end #HTML body file_open.write('\n' % (self.bgColor_R, self.bgColor_G, self.bgColor_B)) file_open.write(self.txt_file_to_convert) file_open.write('
') # Created-with-thc notice file_open.write(texts.createdWithNote) file_open.write('\n') #body end file_open.write('') file_open.close() print texts.FileWritten self.txtOutput.insertPlainText(texts.FileWritten) self.txtOutput.insertPlainText(texts.Success) def onShowResult(self): webbrowser.open_new(self.file_to_write) def SelectColor(self): color = QtGui.QColorDialog.getColor() self.bgColor_R = hex(color.red())[2:].zfill(2) self.bgColor_G = hex(color.green())[2:].zfill(2) self.bgColor_B = hex(color.blue())[2:].zfill(2) self.txtOutput.insertPlainText("\nSelected color (RGB Hex): %s%s%s" % (self.bgColor_R, self.bgColor_G, self.bgColor_B)) def onFontSizeChanged(self): self.font_size = horizontalSlider.value() self.txtOutput.insertPlainText("font size: %s" % self.font_size) def OpenAbout(self): abtDialog = abtDialogFrame.ThcAboutDialog() abtDialog.setAttribute(QtCore.Qt.WA_DeleteOnClose) abtDialog.exec_() def GoToListickOrg(self): webbrowser.open_new('http://www.listick.org') def onQuit(self): print texts.Quit self.close() app = QtGui.QApplication(sys.argv) mainframe = ThcMainframe() mainframe.show() sys.exit(app.exec_()) ======================================================================================== Thanks in advance. Listick http://www.listick.org From wzab01 at gmail.com Sat May 9 07:54:50 2009 From: wzab01 at gmail.com (wzab) Date: Sat, 9 May 2009 04:54:50 -0700 (PDT) Subject: IIR filter conversion routines for Python? References: Message-ID: <28232dc7-d7d2-4cec-a48e-a0144efa1a5b@v17g2000vbb.googlegroups.com> On May 8, 7:30?am, Dennis Lee Bieber wrote: > On Wed, 6 May 2009 11:03:57 -0700 (PDT), wzab > declaimed the following in gmane.comp.python.general: > > > Hi, > > I'm looking for procedures converting theIIRfilters into cascade and/ > > or parallel forms. Something like dir2cas.m or dir2par.m known in the > > Matlab/Octave world. > > Unfortunately SciPy does not contain such functions. > > ? ? ? ? Well... If they are Matlab scripts (as the .m seems to imply), > couldn't one just take the source and recode them in SciPy terms. > -- > ? ? ? ? Wulfraed ? ? ? ?Dennis Lee Bieber ? ? ? ? ? ? ? KD6MOG > ? ? ? ? wlfr... at ix.netcom.com ? ? ? ? ? ? ? wulfr... at bestiaria.com > ? ? ? ? ? ? ? ? HTTP://wlfraed.home.netcom.com/ > ? ? ? ? (Bestiaria Support Staff: ? ? ? ? ? ? ? web-a... at bestiaria.com) > ? ? ? ? ? ? ? ? HTTP://www.bestiaria.com/ Unfortunately the Matlab scripts that are floating around the net do not contain the license information. (e.g. takasila.coe.psu.ac.th/~s4810685/DSP/proakis/PWS_DSP/dir2cas.m ) So I don't know if it sufficient to consider the "public domain"... Additionally Python is simply different and allows some thing to do in different (better?) way. In fact I have almost finished the simple dir2cas.py converter. I'll post the source as soon, as it is ready. -- Regards, WZab From piet at cs.uu.nl Sat May 9 08:01:51 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sat, 09 May 2009 14:01:51 +0200 Subject: unicode bit me References: <977863c9-c4d9-4563-985a-3849d6ed0d75@d2g2000pra.googlegroups.com> Message-ID: >>>>> "anuraguniyal at yahoo.com" (ac) a ?crit: >ac> also not sure why (python 2.5) >ac> print a # works >ac> print unicode(a) # works >ac> print [a] # works >ac> print unicode([a]) # doesn't works Which code do you use now? And what does this print? import sys print sys.stdout.encoding -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From piet at cs.uu.nl Sat May 9 08:21:31 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sat, 09 May 2009 14:21:31 +0200 Subject: ANN: Python process utility (psutil) 0.1.2 released References: <65f15b53-aacf-4651-a113-960cce25f205@p4g2000vba.googlegroups.com> Message-ID: >>>>> "Giampaolo Rodola'" (GR) wrote: >GR> Hi, >GR> I'm pleased to announce the 0.1.2 release of psutil: >GR> http://code.google.com/p/psutil Very nice. I was just contemplating writing some python code to get information from ps (actually virtual memory size of the process) when your announcement came along. Fortunately I had not yet started writing the code :=) -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From wzab01 at gmail.com Sat May 9 09:34:18 2009 From: wzab01 at gmail.com (wzab) Date: Sat, 9 May 2009 06:34:18 -0700 (PDT) Subject: IIR filter conversion routines for Python? References: <28232dc7-d7d2-4cec-a48e-a0144efa1a5b@v17g2000vbb.googlegroups.com> Message-ID: <2a7b69f5-9059-4eb0-a1ad-c6c58c98b4dd@t11g2000vbc.googlegroups.com> > In fact I have almost finished the simple dir2cas.py converter. > I'll post the source as soon, as it is ready. OK. So here it is as public domain code. Any hints and suggestions of improvements are appreciated. http://groups.google.com/group/alt.sources/browse_thread/thread/924fbe2255f8c42d -- Regards, WZab From zookog at gmail.com Sat May 9 09:49:13 2009 From: zookog at gmail.com (Zooko O'Whielacronx) Date: Sat, 9 May 2009 07:49:13 -0600 Subject: [Python-Dev] .pth files are evil In-Reply-To: <49FB22B5.3040507@simplistix.co.uk> References: <49D4DA72.60401@v.loewis.de> <20090407174355.B62983A4063@sparrow.telecommunity.com> <49E4A58F.70309@egenix.com> <20090414162603.70C843A4100@sparrow.telecommunity.com> <49E4F93B.6010802@egenix.com> <20090415003026.B0A783A4114@sparrow.telecommunity.com> <49E59202.6050809@egenix.com> <20090415144147.6845F3A4100@sparrow.telecommunity.com> <49E60832.8030806@egenix.com> <49FB22B5.3040507@simplistix.co.uk> Message-ID: .pth files are why I can't easily use GNU stow with easy_install. If installing a Python package involved writing new files into the filesystem, but did not require reading, updating, and re-writing any extant files such as .pth files, then GNU stow would Just Work with easy_install the way it Just Works with most things. Regards, Zooko From chris at simplistix.co.uk Sat May 9 10:10:23 2009 From: chris at simplistix.co.uk (Chris Withers) Date: Sat, 09 May 2009 15:10:23 +0100 Subject: [Python-Dev] PEP 382: little help for stupid people? In-Reply-To: <4A054C7A.8020806@v.loewis.de> References: <49D4DA72.60401@v.loewis.de> <49D52115.6020001@egenix.com> <49D66C6E.3090602@v.loewis.de> <49DB475B.8060504@egenix.com> <20090407140317.EBD383A4063@sparrow.telecommunity.com> <49DB6A1F.50801@egenix.com> <20090407174355.B62983A4063@sparrow.telecommunity.com> <49E4A58F.70309@egenix.com> <20090414162603.70C843A4100@sparrow.telecommunity.com> <49E4F93B.6010802@egenix.com> <20090415003026.B0A783A4114@sparrow.telecommunity.com> <49E59202.6050809@egenix.com> <20090415144147.6845F3A4100@sparrow.telecommunity.com> <49E60832.8030806@egenix.com> <49FB2398.5000708@simplistix.co.uk> <49FB261F.9080306@v.loewis.de> <49FB2A2A.4090606@simplistix.co.uk> <49FB3384.1030106@v.loewis.de> <4A0547AC.7060103@simplistix.co.uk> <4A054C7A.8020806@v.loewis.de> Message-ID: <4A058ECF.6050203@simplistix.co.uk> Martin v. L?wis wrote: >> So this __init__.py can have code in it? > > That's the point, yes. > >> And base.tar can have other modules and subpackages in it? > > Certainly, yes. Great, when is the PEP due to land in 2.x? ;-) >> What happens if the base and an addon both define a package called >> simplistix.somepackage? > > Depends on whether simplistix.somepackage is a namespace package > (it should). If so, they get merged just as any other namespace > package. Sorry, I was looking at potential bug cases here. What happens if it's not a namespace package? > See PEP 382 (search for "*"). > >> I thought .pth files just had python in them? > > Not at all - they never did. They have paths in them. I've certainly seen them with python in, and that's what I hate about them... >>> Unpack each of them anywhere on sys.path, in any order. >> How would this work if base, addon1 and addon2 were eggs managed by >> buildout or setuptools? > > What is a managed egg (i.e. what kind of management does buildout > or setuptools apply to it)? Sorry, bad wording on my part... I guess I meant more how would buildout/setuptools go about installing/uninstalling/etc packages thatconform to PEP 382? Would setuptools/buildout need modification or would the changes take effect lower down in the stack? cheers, Chris -- Simplistix - Content Management, Zope & Python Consulting - http://www.simplistix.co.uk From deets at nospam.web.de Sat May 9 10:13:20 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Sat, 09 May 2009 16:13:20 +0200 Subject: Creating temperory files for a web application In-Reply-To: <275bb932-644b-4f64-80e8-7061ec7b2dff@a5g2000pre.googlegroups.com> References: <8ecb859e-9a35-4213-93d5-53833e5be7d7@f41g2000pra.googlegroups.com> <76ideeF1d1gbsU1@mid.uni-berlin.de> <354db318-25b3-4af1-8fb1-cd86a0695637@x31g2000prc.googlegroups.com> <275bb932-644b-4f64-80e8-7061ec7b2dff@a5g2000pre.googlegroups.com> Message-ID: <76lhc0F1diso3U1@mid.uni-berlin.de> > > Hi Diez, > I think I understood your point now. > Is it ? > (1) Have a separate URL for the image - update urls.py for that > (2) Pass all the GET parameters to that URL again. > (3) Recalculate the fields again in that URL > (4) Create the image and send back as image/png based on the > received fields. > Other than the fact that I will be hitting the DB twice - for the > fields in the original URL and for creating Image in the second URL, I > think this is the best option. > Please let me know whether I have understood correctly. > > The same option can be done for CSV and PDF files too. Thank you > very much Diez. Yep, that's what I meant. Diez From deets at nospam.web.de Sat May 9 10:14:17 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Sat, 09 May 2009 16:14:17 +0200 Subject: php to python code converter In-Reply-To: References: <4A044D71.1000500@gmail.com> <20090508113720.06d4a48e.darcy@druid.net> <76j97fF1crfkoU2@mid.uni-berlin.de> Message-ID: <76lhdpF1diso3U2@mid.uni-berlin.de> Dotan Cohen schrieb: >> I'm almost 100% sure it won't. The code is machine-generated, thus not easy >> to the human eye, not idiomatic so you don't learn how to write good python >> through it. You learn how to jump through hoops to code the same way in >> python that you do in PHP. >> > > I meant for single functions, not for translating entire apps. No difference. Either the functions are trivial, or they become non-idiomatic. Diez From martin at v.loewis.de Sat May 9 10:18:44 2009 From: martin at v.loewis.de (=?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?=) Date: Sat, 09 May 2009 16:18:44 +0200 Subject: [Python-Dev] .pth files are evil In-Reply-To: References: <49D4DA72.60401@v.loewis.de> <20090407174355.B62983A4063@sparrow.telecommunity.com> <49E4A58F.70309@egenix.com> <20090414162603.70C843A4100@sparrow.telecommunity.com> <49E4F93B.6010802@egenix.com> <20090415003026.B0A783A4114@sparrow.telecommunity.com> <49E59202.6050809@egenix.com> <20090415144147.6845F3A4100@sparrow.telecommunity.com> <49E60832.8030806@egenix.com> <49FB22B5.3040507@simplistix.co.uk> Message-ID: <4A0590C4.1020904@v.loewis.de> Zooko O'Whielacronx wrote: > .pth files are why I can't easily use GNU stow with easy_install. > If installing a Python package involved writing new files into the > filesystem, but did not require reading, updating, and re-writing any > extant files such as .pth files, then GNU stow would Just Work with > easy_install the way it Just Works with most things. Please understand that this is the fault of easy_install, not of .pth files. There is no technical need for easy_install to rewrite .pth files on installation. It could just as well have created new .pth files, rather than modifying existing ones. If you always use --single-version-externally-managed with easy_install, it will stop editing .pth files on installation. Regards, Martin From Scott.Daniels at Acm.Org Sat May 9 10:19:45 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Sat, 09 May 2009 07:19:45 -0700 Subject: Wrapping comments In-Reply-To: References: Message-ID: Tobias Weber wrote: > the guideline (PEP 8) is hard wrap to 7x characters. The reason given is > that soft wrap makes code illegible. > > So what if you hard wrap code but let comments and docstrings soft-wrap? You apparently have caught the soft-/hard- disease spread by several software retailers. There are no such characters as "hard break," "soft break," "hard space," and "soft space." Check you ASCII or Unicode standards if you disagree. There is a "non-breaking space" in Unicode, but it doesn't mean what you mean. If you cannot fix everyone else's editors, print drivers, and display software to have a "soft wrap" that suits their style, then you are asking others to bend their workflow to suit your style. This rule is not to make the writing or altering of code easier; it is about making the _reading_ of code easier. Think of it as a "treaty point," like the "no tabs" rule -- violating it works fine for any individual, but following it eases cooperative work. > ... Or are there editors that can do a "soft hard wrap" while > keeping indentation and #comment markers intact? At least vim and emacs can do so. Some other editors redefine standards in order to make their work easier. --Scott David Daniels Scott.Daniels at Acm.Org From martin at v.loewis.de Sat May 9 10:32:39 2009 From: martin at v.loewis.de (=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Sat, 09 May 2009 16:32:39 +0200 Subject: [Python-Dev] PEP 382: little help for stupid people? In-Reply-To: <4A058ECF.6050203@simplistix.co.uk> References: <49D4DA72.60401@v.loewis.de> <49D52115.6020001@egenix.com> <49D66C6E.3090602@v.loewis.de> <49DB475B.8060504@egenix.com> <20090407140317.EBD383A4063@sparrow.telecommunity.com> <49DB6A1F.50801@egenix.com> <20090407174355.B62983A4063@sparrow.telecommunity.com> <49E4A58F.70309@egenix.com> <20090414162603.70C843A4100@sparrow.telecommunity.com> <49E4F93B.6010802@egenix.com> <20090415003026.B0A783A4114@sparrow.telecommunity.com> <49E59202.6050809@egenix.com> <20090415144147.6845F3A4100@sparrow.telecommunity.com> <49E60832.8030806@egenix.com> <49FB2398.5000708@simplistix.co.uk> <49FB261F.9080306@v.loewis.de> <49FB2A2A.4090606@simplistix.co.uk> <49FB3384.1030106@v.loewis.de> <4A0547AC.7060103@simplistix.co.uk> <4A054C7A.8020806@v.loewis.de> <4A058ECF.6050203@simplistix.co.uk> Message-ID: <4A059407.2060803@v.loewis.de> Chris Withers wrote: > Martin v. L?wis wrote: >>> So this __init__.py can have code in it? >> >> That's the point, yes. >> >>> And base.tar can have other modules and subpackages in it? >> >> Certainly, yes. > > Great, when is the PEP due to land in 2.x? ;-) Most likely, never - it probably will be implemented only after the last feature release of 2.x was made. >>> What happens if the base and an addon both define a package called >>> simplistix.somepackage? >> >> Depends on whether simplistix.somepackage is a namespace package >> (it should). If so, they get merged just as any other namespace >> package. > > Sorry, I was looking at potential bug cases here. What happens if it's > not a namespace package? Then it will be imported as a regular child package. >>>> Unpack each of them anywhere on sys.path, in any order. >>> How would this work if base, addon1 and addon2 were eggs managed by >>> buildout or setuptools? >> >> What is a managed egg (i.e. what kind of management does buildout >> or setuptools apply to it)? > > Sorry, bad wording on my part... I guess I meant more how would > buildout/setuptools go about installing/uninstalling/etc packages > thatconform to PEP 382? Would setuptools/buildout need modification or > would the changes take effect lower down in the stack? Unfortunately, I don't know precisely what they do, so I don't know whether any of it needs modification. All I can say is that if they want to install namespace packages using the mechanism of PEP 382, they will have to produce the file layout specified in the PEP. For distutils (which is the only library in that area that I do know), I think just installing any .pth files inside a package would be sufficient. Regards, Martin From benjamin.kaplan at case.edu Sat May 9 10:35:42 2009 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Sat, 9 May 2009 10:35:42 -0400 Subject: MacPython 3.0.1 installation problem, no /usr/local/bin/python* In-Reply-To: References: Message-ID: On Sat, May 9, 2009 at 4:30 AM, Raoul Gough wrote: > I just installed MacPython 3.0.1 (woot!) but the installer didn't seem > to do an absolutely complete job. According to the ReadMe.txt, "The > installer puts [...] command-line tools in /usr/local/bin [...]". This > didn't happen - even after the install reported Success, there were no > python files or symlinks under /usr/local/bin. > > Also, I'm guessing there should have been a symlink to the current > version under /Library/Frameworks/Python.framework/Versions, which > wasn't there either: > > $ cd /Library/Frameworks/Python.framework > $ ls -l Python > lrwxr-xr-x 1 root admin 23 May 9 08:34 Python -> > Versions/Current/Python > $ ls -l Versions/Current > ls: Versions/Current: No such file or directory > > So I fixed this all up as follows: > > $ cd /Library/Frameworks/Python.framework/Versions > $ sudo ln -s 3.0 Current > $ cd /usr/local/bin > $ sudo ln -s $(find > /Library/Frameworks/Python.framework/Versions/Current/bin -type f) . > > So did something go wrong with the installer, or is it all supposed to > work somehow differently? > Because Python 3 breaks compatibility with Python 2, the installers don't install it as the default. The executable should be at /usr/local/bin/python3.0 > > -- > Raoul Gough. > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From pje at telecommunity.com Sat May 9 10:41:02 2009 From: pje at telecommunity.com (P.J. Eby) Date: Sat, 09 May 2009 10:41:02 -0400 Subject: [Python-Dev] .pth files are evil In-Reply-To: <4A0590C4.1020904@v.loewis.de> References: <49D4DA72.60401@v.loewis.de> <20090407174355.B62983A4063@sparrow.telecommunity.com> <49E4A58F.70309@egenix.com> <20090414162603.70C843A4100@sparrow.telecommunity.com> <49E4F93B.6010802@egenix.com> <20090415003026.B0A783A4114@sparrow.telecommunity.com> <49E59202.6050809@egenix.com> <20090415144147.6845F3A4100@sparrow.telecommunity.com> <49E60832.8030806@egenix.com> <49FB22B5.3040507@simplistix.co.uk> <4A0590C4.1020904@v.loewis.de> Message-ID: <20090509143829.17F293A4080@sparrow.telecommunity.com> At 04:18 PM 5/9/2009 +0200, Martin v. L??wis wrote: >Zooko O'Whielacronx wrote: > > .pth files are why I can't easily use GNU stow with easy_install. > > If installing a Python package involved writing new files into the > > filesystem, but did not require reading, updating, and re-writing any > > extant files such as .pth files, then GNU stow would Just Work with > > easy_install the way it Just Works with most things. > >Please understand that this is the fault of easy_install, not of .pth >files. There is no technical need for easy_install to rewrite .pth >files on installation. It could just as well have created new .pth >files, rather than modifying existing ones. > >If you always use --single-version-externally-managed with easy_install, >it will stop editing .pth files on installation. It's --multi-version (-m) that does that. --single-version-externally-managed is a "setup.py install" option. Both have the effect of not editing .pth files, but they do so in different ways. The "setup.py install" option causes it to install in a distutils-compatible layout, whereas --multi-version simply drops .egg files or directories in the target location and leaves it to the user (or the generated script wrappers) to add them to sys.path. From martin at v.loewis.de Sat May 9 10:42:01 2009 From: martin at v.loewis.de (=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Sat, 09 May 2009 16:42:01 +0200 Subject: [Python-Dev] .pth files are evil In-Reply-To: <20090509143829.17F293A4080@sparrow.telecommunity.com> References: <49D4DA72.60401@v.loewis.de> <20090407174355.B62983A4063@sparrow.telecommunity.com> <49E4A58F.70309@egenix.com> <20090414162603.70C843A4100@sparrow.telecommunity.com> <49E4F93B.6010802@egenix.com> <20090415003026.B0A783A4114@sparrow.telecommunity.com> <49E59202.6050809@egenix.com> <20090415144147.6845F3A4100@sparrow.telecommunity.com> <49E60832.8030806@egenix.com> <49FB22B5.3040507@simplistix.co.uk> <4A0590C4.1020904@v.loewis.de> <20090509143829.17F293A4080@sparrow.telecommunity.com> Message-ID: <4A059639.7040505@v.loewis.de> >> If you always use --single-version-externally-managed with easy_install, >> it will stop editing .pth files on installation. > > It's --multi-version (-m) that does that. > --single-version-externally-managed is a "setup.py install" option. > > Both have the effect of not editing .pth files, but they do so in > different ways. The "setup.py install" option causes it to install in a > distutils-compatible layout, whereas --multi-version simply drops .egg > files or directories in the target location and leaves it to the user > (or the generated script wrappers) to add them to sys.path. Ah, ok. Is there also an easy_install invocation that unpacks the zip file into some location of sys.path (which then wouldn't require editing sys.path)? Regards, Martin From ben+python at benfinney.id.au Sat May 9 11:00:51 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Sun, 10 May 2009 01:00:51 +1000 Subject: Wrapping comments References: Message-ID: <87zldmcod8.fsf@benfinney.id.au> Tobias Weber writes: > Or are there editors that can do a "soft hard wrap" while keeping > indentation and #comment markers intact? Yes. Both Emacs and Vim will do exactly this when re-wrapping a paragraph of commented lines, provided they have support for the language's comment syntax (which, for Python, they do). -- \ ?Only the educated are free.? ?Epictetus | `\ | _o__) | Ben Finney From cdalten at gmail.com Sat May 9 11:20:12 2009 From: cdalten at gmail.com (grocery_stocker) Date: Sat, 9 May 2009 08:20:12 -0700 (PDT) Subject: Thread locking question. Message-ID: <30763887-0b49-47c7-b4c2-76892024b36a@d38g2000prn.googlegroups.com> The following code gets data from 5 different websites at the "same time". #!/usr/bin/python import Queue import threading import urllib2 import time hosts = ["http://yahoo.com", "http://google.com", "http://amazon.com", "http://ibm.com", "http://apple.com"] queue = Queue.Queue() class MyUrl(threading.Thread): def __init__(self, queue): threading.Thread.__init__(self) self.queue = queue def run(self): while True: host = self.queue.get() if host is None: break url = urllib2.urlopen(host) print url.read(1024) #self.queue.task_done() start = time.time() def main(): for i in range(5): t = MyUrl(queue) t.setDaemon(True) t.start() for host in hosts: print "pushing", host queue.put(host) for i in range(5): queue.put(None) t.join() if __name__ == "__main__": main() print "Elapsed Time: %s" % (time.time() - start) How does the parallel download work if each thread has a lock? When the program opens www.yahoo.com, it places a lock on the thread, right? If so, then doesn't that mean the other 4 sites have to wait for the thread to release the lock? From jcd at sdf.lonestar.org Sat May 9 11:26:53 2009 From: jcd at sdf.lonestar.org (J. Clifford Dyer) Date: Sat, 09 May 2009 11:26:53 -0400 Subject: unicode bit me In-Reply-To: <977863c9-c4d9-4563-985a-3849d6ed0d75@d2g2000pra.googlegroups.com> References: <977863c9-c4d9-4563-985a-3849d6ed0d75@d2g2000pra.googlegroups.com> Message-ID: <1241882813.17260.0.camel@mctell> You're still not asking questions in a way that we can answer them. Define "Doesn't work." Define "a". On Sat, 2009-05-09 at 00:04 -0700, anuraguniyal at yahoo.com wrote: > also not sure why (python 2.5) > print a # works > print unicode(a) # works > print [a] # works > print unicode([a]) # doesn't works > -- > http://mail.python.org/mailman/listinfo/python-list > From ebonak at hotmail.com Sat May 9 11:30:02 2009 From: ebonak at hotmail.com (Esmail) Date: Sat, 09 May 2009 11:30:02 -0400 Subject: need help with properties Message-ID: Hi, I am just reading about properties in Python. I am thinking of this as an indirection mechanism, is that wrong? If so, how come the getter/setters aren't called when I use properties instead of the functions directly? What am I missing here? I have a feeling I am overlooking something simple here... Thanks, Esmail --- #!/usr/bin/env python # # quick test to deal with 'private' attributes and # python properties ... # import sys class Rectangle(object): def __init__(self): self.__width = 0 self.__height = 0 def setSize(self, width, height): print 'in setter' if (width < 0) or (height < 0): print >> sys.stderr, 'Negative values not allowed' else: self.__width = width self.__height = height def getSize(self): print 'in getter' return self.__width, self.__height size = property(getSize, setSize) r = Rectangle() print r.getSize() r.setSize(-40, 30) print r.getSize() print '-----------------' size = 3, 7 print size print '-----------------' size = -3,7 print size The program output: in getter (0, 0) in setter Negative values not allowed in getter (0, 0) ----------------- (3, 7) ----------------- (-3, 7) From piet at cs.uu.nl Sat May 9 11:36:21 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sat, 09 May 2009 17:36:21 +0200 Subject: Thread locking question. References: <30763887-0b49-47c7-b4c2-76892024b36a@d38g2000prn.googlegroups.com> Message-ID: >>>>> grocery_stocker (gs) wrote: >gs> The following code gets data from 5 different websites at the "same >gs> time". >gs> #!/usr/bin/python >gs> import Queue >gs> import threading >gs> import urllib2 >gs> import time >gs> hosts = ["http://yahoo.com", "http://google.com", "http://amazon.com", >gs> "http://ibm.com", "http://apple.com"] >gs> queue = Queue.Queue() >gs> class MyUrl(threading.Thread): >gs> def __init__(self, queue): >gs> threading.Thread.__init__(self) >gs> self.queue = queue >gs> def run(self): >gs> while True: >gs> host = self.queue.get() >gs> if host is None: >gs> break >gs> url = urllib2.urlopen(host) >gs> print url.read(1024) >gs> #self.queue.task_done() >gs> start = time.time() >gs> def main(): >gs> for i in range(5): >gs> t = MyUrl(queue) >gs> t.setDaemon(True) >gs> t.start() >gs> for host in hosts: >gs> print "pushing", host >gs> queue.put(host) >gs> for i in range(5): >gs> queue.put(None) >gs> t.join() >gs> if __name__ == "__main__": >gs> main() >gs> print "Elapsed Time: %s" % (time.time() - start) >gs> How does the parallel download work if each thread has a lock? When >gs> the program opens www.yahoo.com, it places a lock on the thread, >gs> right? If so, then doesn't that mean the other 4 sites have to wait >gs> for the thread to release the lock? No. Where does it set a lock? There is only a short lock period in the queue when an item is put in the queue or got from the queue. And of course we have the GIL, but this is released as soon as a long during operation is started - in this case when the Internet communication is done. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From steve at REMOVE-THIS-cybersource.com.au Sat May 9 11:37:37 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 09 May 2009 15:37:37 GMT Subject: Wrapping comments References: Message-ID: <02159679$0$20647$c3e8da3@news.astraweb.com> On Sat, 09 May 2009 07:19:45 -0700, Scott David Daniels wrote: > > So what if you hard wrap code but let comments and docstrings > > soft-wrap? > > You apparently have caught the soft-/hard- disease spread by several > software retailers. There are no such characters as "hard break," "soft > break," "hard space," and "soft space." Check you ASCII or Unicode > standards if you disagree. There is a "non-breaking space" in Unicode, > but it doesn't mean what you mean. You say that as if ASCII and Unicode are the only conceivable sets of characters. Even if they are (and they're not), the OP didn't ask about inserting hard/soft *characters*. He asked about *actions*: hard-wrapping code and soft-wrapping comments. He does that (presumably) by inserting newline characters in code where he wants to force a new line, and telling his editor to wrap comments according to the editor's own algorithm (presumably some variation of the rule "if a line extends beyond the right-edge of the window, wrap it to the next line in the display, but do not insert a newline character"). This is not a disease, it is very useful when editing unstructured text. Believe it or not, text editors are not only useful for editing source code *wink* [aside] Personally, I think it is just a little bit sad that in 2009 we still write programs using editors which are character-based instead of token- based and syntax-aware. At least good editors have syntax highlighting. Maybe in another 20 years, nobody will care about low-level details like the characters used to write code. Only the tokens really matter. [/aside] My own opinion is that if the OP is not sharing his code with anyone else, he can do whatever he likes. Nevertheless, there is value in sticking to the standards that the majority use, and so I tend to stick to a line length of 70 characters or less for both code and comments. I'm not religious about it though. -- Steven From anuraguniyal at yahoo.com Sat May 9 11:37:59 2009 From: anuraguniyal at yahoo.com (anuraguniyal at yahoo.com) Date: Sat, 9 May 2009 08:37:59 -0700 (PDT) Subject: unicode bit me References: <977863c9-c4d9-4563-985a-3849d6ed0d75@d2g2000pra.googlegroups.com> Message-ID: <994147fb-cdf3-4c55-8dc5-62d769b12cdc@u9g2000pre.googlegroups.com> Sorry being unclear again, hmm I am becoming an expert in it. I pasted that code as continuation of my old code at start i.e class A(object): def __unicode__(self): return u"?au" def __repr__(self): return unicode(self).encode("utf-8") __str__ = __repr__ doesn't work means throws unicode error my question boils down to what is diff between, why one doesn't throws error and another does print unicode(a) vs print unicode([a]) From rylesny at gmail.com Sat May 9 11:39:16 2009 From: rylesny at gmail.com (ryles) Date: Sat, 9 May 2009 08:39:16 -0700 (PDT) Subject: slice iterator? References: <44c5bef2-d19c-49ac-ad3b-24ade0470ae1@z7g2000vbh.googlegroups.com> Message-ID: <405bb614-28eb-469a-b490-2d38db901a8f@o27g2000vbd.googlegroups.com> On May 8, 11:17?pm, Ross wrote: > I have a really long list that I would like segmented into smaller > lists. Let's say I had a list a = [1,2,3,4,5,6,7,8,9,10,11,12] and I > wanted to split it into groups of 2 or groups of 3 or 4, etc. Is there > a way to do this without explicitly defining new lists? If the above > problem were to be split into groups of 3, I've tried something like: > > start = 0 > stop = 3 > for i in range(len(a)): > ? ? segment = a[start:stop] > ? ? print segment > ? ? start += stop > ? ? stop += stop > > Unfortunately start and stop don't increment using this code. Ideally, > my outcome would be > [1,2,3] > [4,5,6] > [7,8,9] > [10,11,12] There is also an iterator version here: http://code.activestate.com/recipes/303279 From pje at telecommunity.com Sat May 9 11:39:52 2009 From: pje at telecommunity.com (P.J. Eby) Date: Sat, 09 May 2009 11:39:52 -0400 Subject: [Python-Dev] .pth files are evil In-Reply-To: <4A059639.7040505@v.loewis.de> References: <49D4DA72.60401@v.loewis.de> <20090407174355.B62983A4063@sparrow.telecommunity.com> <49E4A58F.70309@egenix.com> <20090414162603.70C843A4100@sparrow.telecommunity.com> <49E4F93B.6010802@egenix.com> <20090415003026.B0A783A4114@sparrow.telecommunity.com> <49E59202.6050809@egenix.com> <20090415144147.6845F3A4100@sparrow.telecommunity.com> <49E60832.8030806@egenix.com> <49FB22B5.3040507@simplistix.co.uk> <4A0590C4.1020904@v.loewis.de> <20090509143829.17F293A4080@sparrow.telecommunity.com> <4A059639.7040505@v.loewis.de> Message-ID: <20090509153716.D44633A4080@sparrow.telecommunity.com> At 04:42 PM 5/9/2009 +0200, Martin v. L?wis wrote: > >> If you always use --single-version-externally-managed with easy_install, > >> it will stop editing .pth files on installation. > > > > It's --multi-version (-m) that does that. > > --single-version-externally-managed is a "setup.py install" option. > > > > Both have the effect of not editing .pth files, but they do so in > > different ways. The "setup.py install" option causes it to install in a > > distutils-compatible layout, whereas --multi-version simply drops .egg > > files or directories in the target location and leaves it to the user > > (or the generated script wrappers) to add them to sys.path. > >Ah, ok. Is there also an easy_install invocation that unpacks the zip >file into some location of sys.path (which then wouldn't require >editing sys.path)? Not as yet. I'm sort of waiting to see what comes out of PEP 376 discussions re: an installation manifest... but then, if I actually had time to work on it right now, I'd probably just implement something. Currently, you can use pip to do that, though, as long as the packages you want are in source form. pip doesn't unzip eggs as yet. It would be really straightforward, though, for someone to implement an easy_install variant that does this. Just invoke "easy_install -Zmaxd /some/tmpdir packagelist" to get a full set of unpacked .egg directories in /some/tmpdir, and then move the contents of the resulting .egg subdirs to the target location, renaming EGG-INFO subdirs to projectname-version.egg-info subdirs. (Of course, this ignores the issue of uninstalling previous versions, or overwriting of conflicting files in the target -- does pip handle these?) From news.btinternet.com at erik.feegles.net Sat May 9 11:41:59 2009 From: news.btinternet.com at erik.feegles.net (Basil Brush) Date: Sat, 09 May 2009 16:41:59 +0100 Subject: Anyone familiar with python bittorrent? Message-ID: I was just looking at the bittorrent python code as I'm thinking of combining it with some other stuff to expand it. Am I right in thinking that bittorrent launches a new python process for every torrent you download? That seems not good for what I want to do; if I've got 500 torrents I want to queue I don't want 500 processes running. Does anyone know if bittorrent can resume a download if the process gets killed? From Scott.Daniels at Acm.Org Sat May 9 11:55:15 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Sat, 09 May 2009 08:55:15 -0700 Subject: need help with properties In-Reply-To: References: Message-ID: Esmail wrote: > I am just reading about properties in Python. I am thinking > of this as an indirection mechanism, is that wrong? If so, how > come the getter/setters aren't called when I use properties > instead of the functions directly? Because you weren't actually using them. You were writing: size = 3, 7 not: r.size = 3, 7 Try: class Rectangle(object): def __init__(self): self._width = 0 # use single underscore unless you self._height = 0 # have a serious reason for __x def setSize(self, shape): # a setter takes a single arg width, height = shape print 'in setter' if width < 0 or height < 0: print >> sys.stderr, 'Negative values not allowed' else: self._width = width self._height = height def getSize(self): print 'in getter' return self._width, self._height size = property(getSize, setSize) r = Rectangle() print r.getSize() r.setSize((-40, 30)) print r.getSize() print '-----------------' r.size = 3, 7 print r.size print '-----------------' r.size = -3,7 print r.size --Scott David Daniels Scott.Daniels at Acm.Org From steve at REMOVE-THIS-cybersource.com.au Sat May 9 11:56:37 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 09 May 2009 15:56:37 GMT Subject: need help with properties References: Message-ID: <02159aed$0$20647$c3e8da3@news.astraweb.com> On Sat, 09 May 2009 11:30:02 -0400, Esmail wrote: > Hi, > > I am just reading about properties in Python. I am thinking of this as > an indirection mechanism, is that wrong? If so, how come the > getter/setters aren't called when I use properties instead of the > functions directly? > > What am I missing here? I have a feeling I am overlooking something > simple here... Sure are. In your test code, you have: > r = Rectangle() > print r.getSize() > r.setSize(-40, 30) > print r.getSize() > > print '-----------------' > size = 3, 7 > print size All you've done in this second block is define a new object called "size" and assign the tuple (3, 7) to it. [Aside: you might not be aware that it is commas that make tuples, not brackets. The brackets are for grouping.) "size" has nothing to do with your Rectangle class, or the property. Apart from the accidental similarity of name between "Rectangle.size" and "size", it's unrelated to the property you created. What you probably intended to do was this: print '-----------------' r.size = 3, 7 print r.size In other words, your property is attached to the Rectangle instance. -- Steven From google at mrabarnett.plus.com Sat May 9 11:57:11 2009 From: google at mrabarnett.plus.com (MRAB) Date: Sat, 09 May 2009 16:57:11 +0100 Subject: Thread locking question. In-Reply-To: References: <30763887-0b49-47c7-b4c2-76892024b36a@d38g2000prn.googlegroups.com> Message-ID: <4A05A7D7.6050208@mrabarnett.plus.com> Piet van Oostrum wrote: >>>>>> grocery_stocker (gs) wrote: > >> gs> The following code gets data from 5 different websites at the "same >> gs> time". > [snip] >> gs> start = time.time() > >> gs> def main(): >> gs> for i in range(5): >> gs> t = MyUrl(queue) >> gs> t.setDaemon(True) >> gs> t.start() > >> gs> for host in hosts: >> gs> print "pushing", host >> gs> queue.put(host) > >> gs> for i in range(5): >> gs> queue.put(None) > >> gs> t.join() > >> gs> if __name__ == "__main__": >> gs> main() >> gs> print "Elapsed Time: %s" % (time.time() - start) > > >> gs> How does the parallel download work if each thread has a lock? When >> gs> the program opens www.yahoo.com, it places a lock on the thread, >> gs> right? If so, then doesn't that mean the other 4 sites have to wait >> gs> for the thread to release the lock? > > No. Where does it set a lock? There is only a short lock period in the queue > when an item is put in the queue or got from the queue. And of course we > have the GIL, but this is released as soon as a long during operation is > started - in this case when the Internet communication is done. Also, the code is creating 5 threads, but using join() on only the last one. From cdalten at gmail.com Sat May 9 12:02:17 2009 From: cdalten at gmail.com (grocery_stocker) Date: Sat, 9 May 2009 09:02:17 -0700 (PDT) Subject: Thread locking question. References: <30763887-0b49-47c7-b4c2-76892024b36a@d38g2000prn.googlegroups.com> Message-ID: On May 9, 8:36 am, Piet van Oostrum wrote: > >>>>> grocery_stocker (gs) wrote: > >gs> The following code gets data from 5 different websites at the "same > >gs> time". > >gs> #!/usr/bin/python > >gs> import Queue > >gs> import threading > >gs> import urllib2 > >gs> import time > >gs> hosts = ["http://yahoo.com", "http://google.com", "http://amazon.com", > >gs> "http://ibm.com", "http://apple.com"] > >gs> queue = Queue.Queue() > >gs> class MyUrl(threading.Thread): > >gs> def __init__(self, queue): > >gs> threading.Thread.__init__(self) > >gs> self.queue = queue > >gs> def run(self): > >gs> while True: > >gs> host = self.queue.get() > >gs> if host is None: > >gs> break > >gs> url = urllib2.urlopen(host) > >gs> print url.read(1024) > >gs> #self.queue.task_done() > >gs> start = time.time() > >gs> def main(): > >gs> for i in range(5): > >gs> t = MyUrl(queue) > >gs> t.setDaemon(True) > >gs> t.start() > >gs> for host in hosts: > >gs> print "pushing", host > >gs> queue.put(host) > >gs> for i in range(5): > >gs> queue.put(None) > >gs> t.join() > >gs> if __name__ == "__main__": > >gs> main() > >gs> print "Elapsed Time: %s" % (time.time() - start) > >gs> How does the parallel download work if each thread has a lock? When > >gs> the program openswww.yahoo.com, it places a lock on the thread, > >gs> right? If so, then doesn't that mean the other 4 sites have to wait > >gs> for the thread to release the lock? > > No. Where does it set a lock? There is only a short lock period in the queue > when an item is put in the queue or got from the queue. And of course we > have the GIL, but this is released as soon as a long during operation is > started - in this case when the Internet communication is done. > -- Maybe I'm being a bit daft, but what prevents the data from www.yahoo.com from being mixed up with the data from www.google.com? Doesn't using queue() prevent the data from being mixed up? From ebonak at hotmail.com Sat May 9 12:05:33 2009 From: ebonak at hotmail.com (Esmail) Date: Sat, 09 May 2009 12:05:33 -0400 Subject: need help with properties In-Reply-To: References: Message-ID: <4A05A9CD.3010001@hotmail.com> hi Scott, Scott David Daniels wrote: > Esmail wrote: >> I am just reading about properties in Python. I am thinking >> of this as an indirection mechanism, is that wrong? If so, how >> come the getter/setters aren't called when I use properties >> instead of the functions directly? > Because you weren't actually using them. You were writing: > size = 3, 7 > not: > r.size = 3, 7 duh!! .. I knew it had to be something sill .. how do I represent a red face in ASCII ? ;) I was concentrating so much on the new feature I totally didn't see this. I appreciate your code with annotations, helpful. > def setSize(self, shape): # a setter takes a single arg > width, height = shape > print 'in setter' > if width < 0 or height < 0: > print >> sys.stderr, 'Negative values not allowed' > else: > self._width = width > self._height = height > r.setSize((-40, 30)) Just curious, is there a way to pass more than one arg to a setter, or do we always have use some sort of unpacking at the other end? Thanks, Esmail From steve at REMOVE-THIS-cybersource.com.au Sat May 9 12:08:37 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 09 May 2009 16:08:37 GMT Subject: unicode bit me References: <977863c9-c4d9-4563-985a-3849d6ed0d75@d2g2000pra.googlegroups.com> <994147fb-cdf3-4c55-8dc5-62d769b12cdc@u9g2000pre.googlegroups.com> Message-ID: <02159dbd$0$20647$c3e8da3@news.astraweb.com> On Sat, 09 May 2009 08:37:59 -0700, anuraguniyal at yahoo.com wrote: > Sorry being unclear again, hmm I am becoming an expert in it. > > I pasted that code as continuation of my old code at start i.e > class A(object): > def __unicode__(self): > return u"?au" > > def __repr__(self): > return unicode(self).encode("utf-8") > __str__ = __repr__ > > doesn't work means throws unicode error my question What unicode error? Stop asking us to GUESS what the error is, and please copy and paste the ENTIRE TRACEBACK that you get. When you ask for free help, make it easy for the people trying to help you. If you expect them to copy and paste your code and run it just to answer the smallest questions, most of them won't bother. -- Steven From ebonak at hotmail.com Sat May 9 12:08:58 2009 From: ebonak at hotmail.com (Esmail) Date: Sat, 09 May 2009 12:08:58 -0400 Subject: need help with properties In-Reply-To: <02159aed$0$20647$c3e8da3@news.astraweb.com> References: <02159aed$0$20647$c3e8da3@news.astraweb.com> Message-ID: <4A05AA9A.3000105@hotmail.com> Steven D'Aprano wrote: > > All you've done in this second block is define a new object called "size" > and assign the tuple (3, 7) to it. oops .. yes, you are right, and I am embarrassed... [Aside: you might not be aware that it > is commas that make tuples, not brackets. The brackets are for grouping.) Helpful clarification, thanks! > "size" has nothing to do with your Rectangle class, or the property. > Apart from the accidental similarity of name between "Rectangle.size" and > "size", it's unrelated to the property you created. > > What you probably intended to do was this: > > print '-----------------' > r.size = 3, 7 > print r.size Yup, I know Python is a dynamically typed language, but I wish it would point this sort of silliness out .. but there are tradeoffs. I should probably run pyflakes/pychecker/pylint on my my tiny test/try out codes too. Best, Esmail From socyl at 987jk.com.invalid Sat May 9 12:09:38 2009 From: socyl at 987jk.com.invalid (kj) Date: Sat, 9 May 2009 16:09:38 +0000 (UTC) Subject: ISO exemplary Python scripts References: <802a051qoqmpnih0j2su9hhriqumagu326@4ax.com> Message-ID: In <802a051qoqmpnih0j2su9hhriqumagu326 at 4ax.com> Tim Roberts writes: >kj wrote: >> >>I'm learning python, for the umpteenth time. This time I decided >>to start out by writing Python scripts for everyday tasks. Most >>of these scripts are meant to be used only once or twice, but a >>few of them have become useful enough that I'd like to make them >>a little bit more solid... >> >>I'd like to learn from well-written Python scripts, with well-structured >>options parsing, good error and help messages, clear organization, >>etc. >> >>Can someone point me to good examples of Python scripts? >The standard library that was installed with your interpreter is one of the >best repositories. It contains hundreds of working, well-tested scripts, >most of which have the ability to run by themselves. Thanks, but the last bit of your post ("...most of which have the ability to run by themselves") makes me wonder whether we mean the same thing when we talk of "scripts." Can you give me an example of a script that *does not* have the ability to run by itself? When I use the word "script" I mean, *by definition*, a piece of code that has the ability to run by itself. I know that in the python world the distinction between a script and a (library) module is not so clear-cut, and it is common for library modules to have "top-level" stuff (typically test code) that gets run if the module is invoked directly from the command line. But this is not *really* a script as I understand it, because, even though it "runs" directly from the command-line, it lacks the typical CLI amenities, such as command-line flags, help messages, diagnostic messages that are aimed to the "naive user" (i.e. as opposed to the developer), etc. The coding of these "CLI amenities" is one of aspects of these "exemplary Python scripts" I'm most interested in learning about. Anyway, thanks, I'll poke around in the standard library... kynn -- NOTE: In my address everything before the first period is backwards; and the last period, and everything after it, should be discarded. From chambon.pascal at wanadoo.fr Sat May 9 12:32:41 2009 From: chambon.pascal at wanadoo.fr (Pascal Chambon) Date: Sat, 09 May 2009 18:32:41 +0200 Subject: Thread locking question. In-Reply-To: References: <30763887-0b49-47c7-b4c2-76892024b36a@d38g2000prn.googlegroups.com> Message-ID: <4A05B029.1090009@wanadoo.fr> grocery_stocker a ?crit : > On May 9, 8:36 am, Piet van Oostrum wrote: > >>>>>>> grocery_stocker (gs) wrote: >>>>>>> >>> gs> The following code gets data from 5 different websites at the "same >>> gs> time". >>> gs> #!/usr/bin/python >>> gs> import Queue >>> gs> import threading >>> gs> import urllib2 >>> gs> import time >>> gs> hosts = ["http://yahoo.com", "http://google.com", "http://amazon.com", >>> gs> "http://ibm.com", "http://apple.com"] >>> gs> queue = Queue.Queue() >>> gs> class MyUrl(threading.Thread): >>> gs> def __init__(self, queue): >>> gs> threading.Thread.__init__(self) >>> gs> self.queue = queue >>> gs> def run(self): >>> gs> while True: >>> gs> host = self.queue.get() >>> gs> if host is None: >>> gs> break >>> gs> url = urllib2.urlopen(host) >>> gs> print url.read(1024) >>> gs> #self.queue.task_done() >>> gs> start = time.time() >>> gs> def main(): >>> gs> for i in range(5): >>> gs> t = MyUrl(queue) >>> gs> t.setDaemon(True) >>> gs> t.start() >>> gs> for host in hosts: >>> gs> print "pushing", host >>> gs> queue.put(host) >>> gs> for i in range(5): >>> gs> queue.put(None) >>> gs> t.join() >>> gs> if __name__ == "__main__": >>> gs> main() >>> gs> print "Elapsed Time: %s" % (time.time() - start) >>> gs> How does the parallel download work if each thread has a lock? When >>> gs> the program openswww.yahoo.com, it places a lock on the thread, >>> gs> right? If so, then doesn't that mean the other 4 sites have to wait >>> gs> for the thread to release the lock? >>> >> No. Where does it set a lock? There is only a short lock period in the queue >> when an item is put in the queue or got from the queue. And of course we >> have the GIL, but this is released as soon as a long during operation is >> started - in this case when the Internet communication is done. >> -- >> > > Maybe I'm being a bit daft, but what prevents the data from www.yahoo.com > from being mixed up with the data from www.google.com? Doesn't using > queue() prevent the data from being mixed up? > > -- > http://mail.python.org/mailman/listinfo/python-list > > > Hello Each thread has a separate access to internet (its own TCP/IP connection, port number etc.), so the incoming data will never get mixed between the thread on the input. The only problem is when you explicitly use shared data structures be the threads - like the queue here that they all access. But the queue is protected against multithreading so there is no problem there (another data structure might give bugs, if not explicitly locked before use). On the contarry, there will be mixes on the console (stdout), since each thread can write to it at any moment. It's likely that the sources of all the pages will get mixed, on your screen, yep. ^^ Regards, pascal -------------- next part -------------- An HTML attachment was scrubbed... URL: From rurpy at yahoo.com Sat May 9 12:41:45 2009 From: rurpy at yahoo.com (rurpy at yahoo.com) Date: Sat, 9 May 2009 09:41:45 -0700 (PDT) Subject: unicode bit me References: <977863c9-c4d9-4563-985a-3849d6ed0d75@d2g2000pra.googlegroups.com> <994147fb-cdf3-4c55-8dc5-62d769b12cdc@u9g2000pre.googlegroups.com> <02159dbd$0$20647$c3e8da3@news.astraweb.com> Message-ID: On May 9, 10:08 am, Steven D'Aprano wrote: > On Sat, 09 May 2009 08:37:59 -0700, anuraguni... at yahoo.com wrote: > > Sorry being unclear again, hmm I am becoming an expert in it. > > > I pasted that code as continuation of my old code at start i.e > > class A(object): > > def __unicode__(self): > > return u"?au" > > > def __repr__(self): > > return unicode(self).encode("utf-8") > > __str__ = __repr__ > > > doesn't work means throws unicode error my question > > What unicode error? > > Stop asking us to GUESS what the error is, and please copy and paste the > ENTIRE TRACEBACK that you get. When you ask for free help, make it easy > for the people trying to help you. If you expect them to copy and paste > your code and run it just to answer the smallest questions, most of them > won't bother. > > -- > Steven Creua H Jiest! It took me less then 45 seconds to open a terminal window, start Python, and paste the OPs code to get: >>> class A(object): ... def __unicode__(self): ... return u"?au" ... def __repr__(self): ... return unicode(self).encode("utf-8") ... __str__ = __repr__ ... >>> print unicode(a) Traceback (most recent call last): File "", line 1, in NameError: name 'a' is not defined >>> a=A() >>> print unicode(a) ?au >>> print unicode([a]) Traceback (most recent call last): File "", line 1, in UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 1: ordinal not in range(128) Which is the same error he had already posted! I am all for encouraging posters to provide a good description but let's not be ridiculous. Anecdote: My sister always gives her dogs the table scraps after eating dinner. One day when I ate there, I tossed the dogs a piece of meat I hadn't eaten. "No", she cried! "You mustn't give him anything without making him do a trick first! Otherwise he'll forget that you are the boss!". From M8R-gqmgb4 at mailinator.com Sat May 9 12:42:34 2009 From: M8R-gqmgb4 at mailinator.com (uuid) Date: Sat, 9 May 2009 18:42:34 +0200 Subject: Get multiprocessing.Queue to do priorities Message-ID: <4a05b27a_2@news.arcor-ip.de> Hello, I was wondering whether there was a way to make multiprocessing.Queue behave in a priority queue-like fashion. Subclassing with heappush and heappop for put and get doesn't work the old way (multiprocessing.Queue seems to use different data structures than Queue.Queue?) Could one create a heapq within the producer as a proxy, and then feed a proper queue from that? Does anyone have an idea on how to deal with the queue item flow control? (Since all of the sorting has to happen within the heapq, it should only pass items to the real Queue if it's empty?) Thanks in advance! From jass.zhao at gmail.com Sat May 9 12:44:22 2009 From: jass.zhao at gmail.com (Gnep) Date: Sat, 9 May 2009 09:44:22 -0700 (PDT) Subject: Frozen executable to load external python code at the runtime Message-ID: Hi, all, Just curious whether I can do the following: 1. My python source code will dynamically load and run external python scripts; 2. I need to compile the source code to the executable using freeze; 3. Is the executable still able to load&run the scripts? 4. If cannot, is it possible to freeze the scripts into .so and load the .so library with dlopen? BR, Gnep From Scott.Daniels at Acm.Org Sat May 9 12:51:24 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Sat, 09 May 2009 09:51:24 -0700 Subject: heapq.merge with key= In-Reply-To: References: Message-ID: Peter Otten wrote: > Kevin D. Smith wrote: > >> On 2009-05-07 23:48:43 -0500, Chris Rebert said: >> >>> On Thu, May 7, 2009 at 2:23 PM, Kevin D. Smith >>> wrote: >>>> I need the behavior of heapq.merge to merge a bunch of results from a >>>> database.... I would prefer to do this with generators.... I need >>>> the key argument to sort on the correct field in the database... >>> I think so.... [some code] >> Ah, that's not a bad idea. I think it could be simplified by letting >> Python do the comparison work as follows (also untested). >> >> def keyify(gen, key=lamda x:x): >> for item in gen: >> yield (key(item), item) > ... This is not as general. It makes sorting unstable and can even fail > if the items are unorderable: > One way to fix it: >>>> def decorate(gen, key): > ... for index, item in enumerate(gen): > ... yield key(item), index, item >>>> sorted(decorate([1j, -1j, 1+1j], key=lambda c: c.real)) > [(0.0, 0, 1j), (0.0, 1, -1j), (1.0, 2, (1+1j))] >>>> list(undecorate(_)) > [1j, -1j, (1+1j)] However, a heap gets faster if equal items are equal, so you really want the decorated value to be the sole component of a comparison. (and remember, sorts are now stable). As in your code, key generation is likely best calculated once. Here's another fix: class Keyed(object): def __init__(self, obj, key): self.obj = obj self.key = key def __lt__(self, other): # cmp is old-style magic return self.key < other.key def __eq__(self, other): return self.key == other.key def __repr__(self): return '%s(%r, %r)' % ( self.__class__.__name__, self.obj, self.key) def kmerge(key, *streams): keyed_streams = [(Keyed(obj, key(obj)) for obj in stream) for stream in streams] for element in heapq.merge(*keyed_streams): yield element.obj --Scott David Daniels Scott.Daniels at Acm.Org From M8R-gqmgb4 at mailinator.com Sat May 9 13:00:54 2009 From: M8R-gqmgb4 at mailinator.com (uuid) Date: Sat, 9 May 2009 19:00:54 +0200 Subject: Get multiprocessing.Queue to do priorities References: <4a05b27a_2@news.arcor-ip.de> Message-ID: <4a05b6c6_2@news.arcor-ip.de> I just read up and it seems that no matter the approach, it's futile to use multiprocessing.Queue, since there is a bug that prevents true FIFO. (http://bugs.python.org/issue4999) Any recommendation on an alternate way to build a priority queue to use with a "one producer, many consumers" type multiprocessing setup would be welcomed! :| On 2009-05-09 18:42:34 +0200, uuid said: > Hello, > I was wondering whether there was a way to make multiprocessing.Queue > behave in a priority queue-like fashion. Subclassing with heappush and > heappop for put and get doesn't work the old way (multiprocessing.Queue > seems to use different data structures than Queue.Queue?) > > Could one create a heapq within the producer as a proxy, and then feed > a proper queue from that? Does anyone have an idea on how to deal with > the queue item flow control? (Since all of the sorting has to happen > within the heapq, it should only pass items to the real Queue if it's > empty?) > > Thanks in advance! From Scott.Daniels at Acm.Org Sat May 9 13:39:50 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Sat, 09 May 2009 10:39:50 -0700 Subject: unicode bit me In-Reply-To: References: <977863c9-c4d9-4563-985a-3849d6ed0d75@d2g2000pra.googlegroups.com> <994147fb-cdf3-4c55-8dc5-62d769b12cdc@u9g2000pre.googlegroups.com> <02159dbd$0$20647$c3e8da3@news.astraweb.com> Message-ID: rurpy at yahoo.com wrote: > On May 9, 10:08 am, Steven D'Aprano cybersource.com.au> wrote: >> On Sat, 09 May 2009 08:37:59 -0700, anuraguni... at yahoo.com wrote: >>> Sorry being unclear again, hmm I am becoming an expert in it. >>> I pasted that code as continuation of my old code at start i.e >>> class A(object): >>> def __unicode__(self): >>> return u"?au" >>> def __repr__(self): >>> return unicode(self).encode("utf-8") >>> __str__ = __repr__ >>> doesn't work means throws unicode error my question >> What unicode error? >> >> Stop asking us to GUESS what the error is, and please copy and paste the >> ENTIRE TRACEBACK that you get. When you ask for free help, make it easy >> for the people trying to help you. If you expect them to copy and paste >> your code and run it just to answer the smallest questions, most of them >> won't bother. > It took me less then 45 seconds to open a terminal window, start > Python, and paste the OPs code to get: >>>> class A(object): > ... def __unicode__(self): > ... return u"?au" > ... def __repr__(self): > ... return unicode(self).encode("utf-8") > ... __str__ = __repr__ > ... >>>> print unicode(a) > Traceback (most recent call last): > File "", line 1, in > NameError: name 'a' is not defined >>>> a=A() >>>> print unicode(a) > ?au >>>> print unicode([a]) > Traceback (most recent call last): > File "", line 1, in > UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position > 1: ordinal not in range(128) > > Which is the same error he had already posted! It is _not_clear_ that is what was going on. Your 45 seconds could have been his 45 seconds. He was describing results rather than showing them. From your demo, I get to: unicode(u'\N{COPYRIGHT SIGN}au'.encode('utf-8')) raises an exception (which it should). unicode(u'\N{COPYRIGHT SIGN}au'.encode('utf-8'), 'utf-8') Does _not_ raise an exception (as it should not). Note that his __repr__ produces characters which are not ASCII. So, str or repr of a list containing those elements will also be non-ascii. To convert non-ASCII strings to unicode, you must specify a character encoding. The object a (created with A()) can be converted directly to unicode (via its unicode method). No problem. The object A() may have its repr taken, which is a (non-unicode) string which is not ASCII. But you cannot take unicode(repr(a)), because repr(a) contains a character > '\x7f'. What he was trying to do was masking the issue. Imagine: class B(object): def __unicode__(self): return u'one' def __repr__(self): return 'two' def __str__(self): return 'three' b = B() print b, unicode(b), [b] By the way, pasting code with non-ASCII characters does not mean your recipient will get the characters you pasted. --Scott David Daniels Scott.Daniels at Acm.Org From paul at boddie.org.uk Sat May 9 13:40:42 2009 From: paul at boddie.org.uk (Paul Boddie) Date: Sat, 9 May 2009 10:40:42 -0700 (PDT) Subject: mod_python and xml.dom.minidom References: <83b63d98-9871-47eb-8846-46404b70896e@z7g2000vbh.googlegroups.com> Message-ID: <12ef7b00-f0c5-4030-a353-533d93d23cdd@r3g2000vbp.googlegroups.com> On 9 Mai, 01:36, dpapathanasiou wrote: > > Apache's configure utility (I'm using httpd version 2.2.11) doesn't > explicitly describe an expat library option. > > Also, if libexpat is version 1.95.2, wouldn't I have to get version > 2.0 to be compatible with pyexpat? The aim would be to persuade Apache to configure itself against the same Expat library that pyexpat is using, which would involve the headers and libraries referenced during the pyexpat configuration process, although I seem to recall something about pyexpat bundling its own version of Expat - that would complicate matters somewhat. > If anyone has any advice or suggestions, I'd appreciate hearing them. Expat might be getting brought into Apache via mod_dav: http://www.webdav.org/mod_dav/install.html Perhaps disabling mod_dav when configuring Apache might drop Expat from Apache's library dependencies. Paul From namekuseijin.nospam at gmail.com Sat May 9 13:46:43 2009 From: namekuseijin.nospam at gmail.com (namekuseijin) Date: Sat, 09 May 2009 14:46:43 -0300 Subject: I'm intrigued that Python has some functional constructions in the language. In-Reply-To: References: <44350436-de1a-451e-95f5-13b902a902ce@s21g2000vbb.googlegroups.com> <692b7ae8-0c5b-498a-a012-51bda980f3b9@s28g2000vbp.googlegroups.com> Message-ID: Lawrence D'Oliveiro wrote: > In message <692b7ae8-0c5b-498a- > a012-51bda980f3b9 at s28g2000vbp.googlegroups.com>, namekuseijin wrote: > >> On May 8, 6:48 pm, Lawrence D'Oliveiro > central.gen.new_zealand> wrote: >>> In message , namekuseijin wrote: >>> >>>> Carl Banks escreveu: >>>>> 2. However, functional programming is cryptic at some level no matter >>>>> how nice you make the syntax. >>>> When your program is nothing but function definition and function >>>> application, syntax is meaningless. >>> On the contrary, syntax is vital to the correct interpretation of >>> functional constructs. See, for example, >>> . >> Yeah, it's important in an imperative language ... > > That was an example of functional programming. I noticed the use of list comprehensions. The fact that list comprehensions are only available as predefined syntax in Python is noticed too. From Scott.Daniels at Acm.Org Sat May 9 13:51:56 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Sat, 09 May 2009 10:51:56 -0700 Subject: need help with properties In-Reply-To: References: Message-ID: <28KdnTscUpwaXJjXnZ2dnUVZ_sudnZ2d@pdx.net> Esmail wrote: > Just curious, is there a way to pass more than one arg to a setter, or > do we always have use some sort of unpacking at the other end? Think about the source for a setter. it comes from: .name = You could add optional args, but the option would never be taken. If you never intend to move to the 3.X versions of Python, you code define the function as: def setter(self, (height, width)): ... but since introspecting such things to give nice tracebacks and good help messages drastically complicates the internal python structure, and since the facility is so fraught with traps for newbies, who might want to ask for, for example: def setter(self, (height=4, width=3)): the Python 3.X world is wisely losing the unpacking in parameter passing trick. --Scott David Daniels Scott.Daniels at Acm.Org From ebonak at hotmail.com Sat May 9 13:53:10 2009 From: ebonak at hotmail.com (Esmail) Date: Sat, 09 May 2009 13:53:10 -0400 Subject: need help with properties In-Reply-To: <28KdnTscUpwaXJjXnZ2dnUVZ_sudnZ2d@pdx.net> References: <28KdnTscUpwaXJjXnZ2dnUVZ_sudnZ2d@pdx.net> Message-ID: <4A05C306.3060203@hotmail.com> Scott David Daniels wrote: > <... good stuff ... > > > the Python 3.X world is wisely losing the unpacking in parameter > passing trick. Thanks Scott, useful information, Esmail From emile at fenx.com Sat May 9 13:53:31 2009 From: emile at fenx.com (Emile van Sebille) Date: Sat, 09 May 2009 10:53:31 -0700 Subject: slice iterator? In-Reply-To: <44c5bef2-d19c-49ac-ad3b-24ade0470ae1@z7g2000vbh.googlegroups.com> References: <44c5bef2-d19c-49ac-ad3b-24ade0470ae1@z7g2000vbh.googlegroups.com> Message-ID: On 5/8/2009 8:17 PM Ross said... > I have a really long list that I would like segmented into smaller > lists. Let's say I had a list a = [1,2,3,4,5,6,7,8,9,10,11,12] and I > wanted to split it into groups of 2 or groups of 3 or 4, etc. Is there > a way to do this without explicitly defining new lists? >>> a = [1,2,3,4,5,6,7,8,9,10,11,12] >>> >>> for k in [2,3,4,5,6]: ... print [ a[j:j+k] for j in range(0,len(a),k) ] ... [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]] [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12]] [[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12]] The point is of course to use the form a[j:j+k] which doesn't create new lists. Emile From Scott.Daniels at Acm.Org Sat May 9 13:56:44 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Sat, 09 May 2009 10:56:44 -0700 Subject: Get multiprocessing.Queue to do priorities In-Reply-To: <4a05b6c6_2@news.arcor-ip.de> References: <4a05b27a_2@news.arcor-ip.de> <4a05b6c6_2@news.arcor-ip.de> Message-ID: <28KdnTocUpw2X5jXnZ2dnUVZ_sudnZ2d@pdx.net> uuid wrote: > Any recommendation on an alternate way to build a priority queue to use > with a "one producer, many consumers" type multiprocessing setup would > be welcomed! ? "one producer, many consumers" ? What would the priority queue do? Choose a consumer? --Scott David Daniels Scott.Daniels at Acm.Org From namekuseijin.nospam at gmail.com Sat May 9 13:57:24 2009 From: namekuseijin.nospam at gmail.com (namekuseijin) Date: Sat, 09 May 2009 14:57:24 -0300 Subject: I'm intrigued that Python has some functional constructions in the language. In-Reply-To: References: <44350436-de1a-451e-95f5-13b902a902ce@s21g2000vbb.googlegroups.com> <9e68a155-177d-4e03-97b6-c19cbb986515@z19g2000vbz.googlegroups.com> <7fa66f4e-e88c-4fb4-a509-911756814f89@o27g2000vbd.googlegroups.com> Message-ID: Carl Banks wrote: > On May 8, 7:19 pm, namekuseijin wrote: >> On May 8, 10:13 pm, Carl Banks wrote: >> In Haskell, Lisp and other functional programming languages, any extra >> syntax gets converted into the core lambda constructs. > > So? The user still uses that syntax, so how can you claim it doesn't > matter? >> In Lisp >> languages, that syntax is merely user-defined macros, but in Haskell >> it's builtin the compiler for convenience. > > I don't even know what you're saying here I'm saying syntax is nothing special. They are user-defined, as functions. And it all gets converted into functions. Functions matter, syntax is irrelevant because you can do away with it. In Haskell, point free style of programming shows almost no signs of predefined syntax at all. It's all function composition. In functional programming languages, predefined syntax is mostly irrelevant. In Python and other imperative languages, it's absolutely necessary. That's my point. From metolone+gmane at gmail.com Sat May 9 14:06:56 2009 From: metolone+gmane at gmail.com (Mark Tolonen) Date: Sat, 9 May 2009 11:06:56 -0700 Subject: unicode bit me References: <977863c9-c4d9-4563-985a-3849d6ed0d75@d2g2000pra.googlegroups.com> <994147fb-cdf3-4c55-8dc5-62d769b12cdc@u9g2000pre.googlegroups.com> Message-ID: wrote in message news:994147fb-cdf3-4c55-8dc5-62d769b12cdc at u9g2000pre.googlegroups.com... > Sorry being unclear again, hmm I am becoming an expert in it. > > I pasted that code as continuation of my old code at start > i.e > class A(object): > def __unicode__(self): > return u"?au" > > def __repr__(self): > return unicode(self).encode("utf-8") > __str__ = __repr__ > > doesn't work means throws unicode error > my question boils down to > what is diff between, why one doesn't throws error and another does > print unicode(a) > vs > print unicode([a]) That is still an incomplete example. Your results depend on your source code's encoding and your system's stdout encoding. Assuming a=A(), unicode(a) returns u'?au', but then is converted to stdout's encoding for display. An encoding such as cp437 (U.S. Windows console) will fail. the repr of [a] is a byte string in the encoding of your source file. The unicode() function, given a byte string of unspecified encoding, uses the ASCII codec. Assuming your source encoding was utf-8, unicode([a],'utf-8') will correctly convert it to unicode, and then printing that unicode string will attempt to convert it to stdout encoding. On a utf-8 console, it will work, on a cp437 console it will not. Here's a new one: In PythonWin (from pywin32-313), stdout is utf-8, so: >>> print '?' # this is a utf8 byte string ? >>> '?' # view the utf8 bytes '\xc2\xa9' >>> u'?' # view the unicode character u'\xa9' >>> print '\xc2\xa9' # stdout is utf8, so it is understood ? >>> print u'\xa9' # auto-converts to utf8. ? >>> print unicode('\xc2\xa9') # encoding not given, defaults to ASCII. Traceback (most recent call last): File "", line 1, in UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 0: ordinal not in range(128) >>> print unicode('\xc2\xa9','utf8') # provide the encoding ? This gives different results when the stdout encoding is different. Here's a couple of the same instructions on my Windows console with cp437 encoding, which doesn't support the copyright character: >>> print '\xc2\xa9' # stdout is cp437 ?? >>> print u'\xa9' # tries to convert to cp437 Traceback (most recent call last): File "", line 1, in File "C:\dev\python\lib\encodings\cp437.py", line 12, in encode return codecs.charmap_encode(input,errors,encoding_map) UnicodeEncodeError: 'charmap' codec can't encode character u'\xa9' in position 0: character maps to Hope that helps your understanding, Mark From steve at REMOVE-THIS-cybersource.com.au Sat May 9 14:21:07 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 09 May 2009 18:21:07 GMT Subject: I'm intrigued that Python has some functional constructions in the language. References: <44350436-de1a-451e-95f5-13b902a902ce@s21g2000vbb.googlegroups.com> <9e68a155-177d-4e03-97b6-c19cbb986515@z19g2000vbz.googlegroups.com> <7fa66f4e-e88c-4fb4-a509-911756814f89@o27g2000vbd.googlegroups.com> Message-ID: <0215bccb$0$20647$c3e8da3@news.astraweb.com> On Sat, 09 May 2009 14:57:24 -0300, namekuseijin wrote: > I'm saying syntax is nothing special. They are user-defined, as > functions. And it all gets converted into functions. Functions matter, > syntax is irrelevant because you can do away with it. How do you call functions without syntax? By mental telepathy? By direct manipulation of the electromagnetic fields inside the CPU? > In Haskell, point free style of programming shows almost no signs of > predefined syntax at all. It's all function composition. But it takes syntax in order to write function composition. There's at least six ways of doing function composition: f(g(x)) # used in many programming languages and mathematics f . g (x) # Haskell f o g (x) # mathematics g f # stack-based languages like Forth g x | f # Unix-like shells compose(f, g)(x) # possible in many languages. > In functional programming languages, predefined syntax is mostly > irrelevant. In Python and other imperative languages, it's absolutely > necessary. That's my point. I think your point is wrong. Without syntax, there can be no written communication. In Haskell, f.g is not the same as f+g -- the difference is one of syntax. -- Steven From address_is at invalid.invalid Sat May 9 14:23:25 2009 From: address_is at invalid.invalid (uuid) Date: 09 May 2009 18:23:25 GMT Subject: Get multiprocessing.Queue to do priorities References: <28KdnTocUpw2X5jXnZ2dnUVZ_sudnZ2d@pdx.net> Message-ID: <529683651263584828.031438address_is-invalid.invalid@news.arcor.de> Scott David Daniels wrote: > > ? "one producer, many consumers" ? > What would the priority queue do? Choose a consumer? Sorry, I should have provided a little more detail. There is one producer thread, reading urls from multiple files and external input. These urls have a certain priority, and are fed to multiple consumer threads for fetching and further processing (XML parsing and such CPU intensive stuff). Since the content of the urls is changing over time, it is crucial to have a certain amount of control over the order in which the requests occur. So, to answer the question: The priority queue would make sure that out of a number of asynchronously added items, those with a high priority are fetched first by the worker threads. Sounds like a perfect case for a heap, if only I could :) From gdamjan at gmail.com Sat May 9 14:40:06 2009 From: gdamjan at gmail.com (=?UTF-8?B?0JTQsNC80ZjQsNC9INCT0LXQvtGA0LPQuNC10LLRgdC60Lg=?=) Date: Sat, 09 May 2009 20:40:06 +0200 Subject: [Python-Dev] .pth files are evil References: <49D4DA72.60401@v.loewis.de> <20090407174355.B62983A4063@sparrow.telecommunity.com> <49E4A58F.70309@egenix.com> <20090414162603.70C843A4100@sparrow.telecommunity.com> <49E4F93B.6010802@egenix.com> <20090415003026.B0A783A4114@sparrow.telecommunity.com> <49E59202.6050809@egenix.com> <20090415144147.6845F3A4100@sparrow.telecommunity.com> <49E60832.8030806@egenix.com> <49FB22B5.3040507@simplistix.co.uk> <4A0590C4.1020904@v.loewis.de> <20090509143829.17F293A4080@sparrow.telecommunity.com> Message-ID: <69vgd6-hde.ln1@archaeopteryx.softver.org.mk> > Ah, ok. Is there also an easy_install invocation that unpacks the zip > file into some location of sys.path (which then wouldn't require > editing sys.path)? You have pip that does that :) -- ?????? ( http://softver.org.mk/damjan/ ) ... knowledge is exactly like power - something to be distributed as widely as humanly possible, for the betterment of all. -- jd From google at smetj.net Sat May 9 15:02:14 2009 From: google at smetj.net (google at smetj.net) Date: Sat, 9 May 2009 12:02:14 -0700 (PDT) Subject: SimpleXMLRPCServer and creating a new object on for each new client request. References: Message-ID: <09a3e655-06ad-46cb-9955-303948f2b271@e24g2000vbe.googlegroups.com> On May 8, 1:17?am, Piet van Oostrum wrote: > >>>>> Jelle Smet (JS) wrote: > > One more thing: > > >JS> I start python interactively: > >>>>> import xmlrpclib > >>>>> session1=xmlrpclib.ServerProxy('http://localhost:8000') > >>>>> session2=xmlrpclib.ServerProxy('http://localhost:8000') > >>>>> print session1.show_random() > >JS> 13930 > >>>>> print session2.show_random() > >JS> 13930 > > I get the impression, also from your use of the variable names > 'session1' and 'session2' that xmlrpclib.ServerProxy() gives you some > kind of connection to the XMLRPC server. This is not the case. It gives > just an administration object *in the client* that will communicate with > the server when you call a method on it. The two session's in your code > are therefore functionally equivalent and there is no advantage in > having two of them instead of one. And the name session is misleading. > > Please note also that XMLRPC isn't object-oriented. There is just the > server; in the protocol there are no objects other than the server. > -- > Piet van Oostrum > URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4] > Private email: p... at vanoostrum.org Hi Piet, Yes, I'm aware of this. >You create a single instance of your class and then register that. There >is nothing in your code that makes the server believe it should create a >new instance for each request. >On the other hand, If SimpleXMLRPCServer had this capability, my example wouldn't be correct anyway, because only one instance of class Randomizer is >created when the SimpleXMLRPCServer starts. Exactly, I understand. >Otherwise you would have to register a function that creates a new >instance on every call. >But as Martin P. Hellwig has noted, it wouldn't give you sessions anyway. Well, I think Martin's example will suit my needs. Thanks for the explanation! Jelle Thanks for the feedback, ... From http Sat May 9 15:08:49 2009 From: http (Paul Rubin) Date: 09 May 2009 12:08:49 -0700 Subject: I'm intrigued that Python has some functional constructions in the language. References: <44350436-de1a-451e-95f5-13b902a902ce@s21g2000vbb.googlegroups.com> <9e68a155-177d-4e03-97b6-c19cbb986515@z19g2000vbz.googlegroups.com> <7fa66f4e-e88c-4fb4-a509-911756814f89@o27g2000vbd.googlegroups.com> <0215bccb$0$20647$c3e8da3@news.astraweb.com> Message-ID: <7xfxfet7pa.fsf@ruckus.brouhaha.com> Steven D'Aprano writes: > I think your point is wrong. Without syntax, there can be no written > communication. In Haskell, f.g is not the same as f+g -- the difference > is one of syntax. In Haskell, (+) and (.) are both functions. (+) takes two numbers as arguments and returns their sum. (.) takes two functions as arguments and returns a new function which composes f and g. 2 + 3 is the same as (+) 2 3. f . g is the same as (.) f g. The infix notations are considered syntax sugar. The parentheses around (+) and (.) are so the compiler doesn't get lexically confused, but you could say add = (+) compose = (.) a = add 2 3 b = compose square cube c = b 2 now "print a" will print 5, and "print c" would print 64 (which is 2**6). (There aren't actually builtins square and cube, but pretend there are). So it's really true you can get rid of almost all Haskell expression syntax. There's actually a preprocessor called Liskell that lets you write Haskell as Lisp-like S-expressions, which is handy if you want to use a macro processor on it. From piet at cs.uu.nl Sat May 9 15:31:26 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sat, 09 May 2009 21:31:26 +0200 Subject: unicode bit me References: <977863c9-c4d9-4563-985a-3849d6ed0d75@d2g2000pra.googlegroups.com> <994147fb-cdf3-4c55-8dc5-62d769b12cdc@u9g2000pre.googlegroups.com> Message-ID: >>>>> "Mark Tolonen" (MT) wrote: >MT> wrote in message >MT> news:994147fb-cdf3-4c55-8dc5-62d769b12cdc at u9g2000pre.googlegroups.com... >>> Sorry being unclear again, hmm I am becoming an expert in it. >>> >>> I pasted that code as continuation of my old code at start >>> i.e >>> class A(object): >>> def __unicode__(self): >>> return u"?au" >>> >>> def __repr__(self): >>> return unicode(self).encode("utf-8") >>> __str__ = __repr__ >>> >>> doesn't work means throws unicode error >>> my question boils down to >>> what is diff between, why one doesn't throws error and another does >>> print unicode(a) >>> vs >>> print unicode([a]) >MT> That is still an incomplete example. Your results depend on your source >MT> code's encoding and your system's stdout encoding. Assuming a=A(), >MT> unicode(a) returns u'?au', but then is converted to stdout's encoding for >MT> display. You are confusing the issue. It does not depend on the source code's encoding (supposing that the encoding declaration in the source is correct). repr returns unicode(self).encode("utf-8"), so it is utf-8 encoded even when the source code had a different encoding. The u"?au" string is not dependent on the source encoding. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From piet at cs.uu.nl Sat May 9 15:43:57 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sat, 09 May 2009 21:43:57 +0200 Subject: Thread locking question. References: <30763887-0b49-47c7-b4c2-76892024b36a@d38g2000prn.googlegroups.com> Message-ID: >>>>> grocery_stocker (gs) wrote: [snip] >gs> Maybe I'm being a bit daft, but what prevents the data from www.yahoo.com >gs> from being mixed up with the data from www.google.com? Doesn't using >gs> queue() prevent the data from being mixed up? Nothing in your script prevents the data from getting mixed up. Now it seems from some experimentation that the print statements might be atomic, although I can't find anything about that in the Python doc, and I think you shouldn't count on that. I would expect it not to be atomic when it does a blocking I/O. If I make your example more complete, printing the documents completely, like: def run(self): while True: host = self.queue.get() if host is None: break url = urllib2.urlopen(host) while True: txt = url.read(1024) if not txt: break print txt, then the document will get mixed up in the output. Likewise if you would want to put them in a shared datastructure, you must use locking when you insert them (for example you could put them in another Queue). The Queue you use here only prevent the urls from getting mixed up, but it has no effect on the further processing. As I told my students two days ago: you shouldn't do thread programming unless you have thoroughly studied the subject. By the way there is another flaw in your program: you do the join only on the last spawned thread. Because the threads are daemonic all other threads that are still working will be killed prematurely when this thread finishes. The code should be like this: def main(): threads = [] for i in range(5): t = MyUrl(queue) t.setDaemon(True) t.start() threads.append(t) ... for t in threads: t.join() Or just don't make them daemonic. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From Scott.Daniels at Acm.Org Sat May 9 16:00:36 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Sat, 09 May 2009 13:00:36 -0700 Subject: Get multiprocessing.Queue to do priorities In-Reply-To: <529683651263584828.031438address_is-invalid.invalid@news.arcor.de> References: <28KdnTocUpw2X5jXnZ2dnUVZ_sudnZ2d@pdx.net> <529683651263584828.031438address_is-invalid.invalid@news.arcor.de> Message-ID: <-vKdnex07oQzQpjXnZ2dnUVZ_hZi4p2d@pdx.net> uuid wrote: > Scott David Daniels wrote: >> ? "one producer, many consumers" ? >> What would the priority queue do? Choose a consumer? > > Sorry, I should have provided a little more detail. There is one > producer thread, reading urls from multiple files and external input. > These urls have a certain priority, and are fed to multiple consumer > threads for fetching and further processing (XML parsing and such CPU > intensive stuff). Since the content of the urls is changing over time, > it is crucial to have a certain amount of control over the order in > which the requests occur. So, to answer the question: > The priority queue would make sure that out of a number of > asynchronously added items, those with a high priority are fetched first > by the worker threads. Sounds like a perfect case for a heap, if only I > could :) 2.6 has a PriorityQueue in the Queue module. If you aren't using 2.6, you could copy the code for your own version. --Scott David Daniels Scott.Daniels at Acm.Org From robert.kern at gmail.com Sat May 9 16:15:40 2009 From: robert.kern at gmail.com (Robert Kern) Date: Sat, 09 May 2009 15:15:40 -0500 Subject: IIR filter conversion routines for Python? In-Reply-To: <28232dc7-d7d2-4cec-a48e-a0144efa1a5b@v17g2000vbb.googlegroups.com> References: <28232dc7-d7d2-4cec-a48e-a0144efa1a5b@v17g2000vbb.googlegroups.com> Message-ID: On 2009-05-09 06:54, wzab wrote: > Unfortunately the Matlab scripts that are floating around the net do > not contain the license information. > (e.g. takasila.coe.psu.ac.th/~s4810685/DSP/proakis/PWS_DSP/dir2cas.m ) > So I don't know if it sufficient to consider the "public domain"... No! The opposite in fact. No license information simply means you do not have a license. It is copyrighted at the moment of creation. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco From robert.kern at gmail.com Sat May 9 16:27:02 2009 From: robert.kern at gmail.com (Robert Kern) Date: Sat, 09 May 2009 15:27:02 -0500 Subject: IIR filter conversion routines for Python? In-Reply-To: <2a7b69f5-9059-4eb0-a1ad-c6c58c98b4dd@t11g2000vbc.googlegroups.com> References: <28232dc7-d7d2-4cec-a48e-a0144efa1a5b@v17g2000vbb.googlegroups.com> <2a7b69f5-9059-4eb0-a1ad-c6c58c98b4dd@t11g2000vbc.googlegroups.com> Message-ID: On 2009-05-09 08:34, wzab wrote: >> In fact I have almost finished the simple dir2cas.py converter. >> I'll post the source as soon, as it is ready. > > OK. So here it is as public domain code. Any hints and suggestions > of improvements are appreciated. > > http://groups.google.com/group/alt.sources/browse_thread/thread/924fbe2255f8c42d Wow! A shar archive posted to alt.sources. I haven't seen one of those used in anger in a *long* time. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco From arnodel at googlemail.com Sat May 9 16:47:42 2009 From: arnodel at googlemail.com (Arnaud Delobelle) Date: Sat, 09 May 2009 21:47:42 +0100 Subject: Wrapping comments References: Message-ID: Tobias Weber writes: > In article , > Scott David Daniels wrote: > >> At least vim and emacs can do so. > > Just tried Aquamacs. Using the defaults it correctly re-wraped > docstrings using newlines when inserting, but not when removing words. > So even with the prime editor hard wrap seems, for writing, inferior to > soft wrap. A simple Alt-Q will reformat everything nicely. -- Arnaud (posting using gnus on Aquamacs) From piet at cs.uu.nl Sat May 9 16:47:47 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sat, 09 May 2009 22:47:47 +0200 Subject: SimpleXMLRPCServer and creating a new object on for each new client request. References: <09a3e655-06ad-46cb-9955-303948f2b271@e24g2000vbe.googlegroups.com> Message-ID: >>>>> google at smetj.net (g) wrote: >g> Well, I think Martin's example will suit my needs. >g> Thanks for the explanation! His client code is unnecessarily complicated with 3 session variables. The following code does the same: SESSION = xmlrpclib.ServerProxy(URL_PORT) print(SESSION.show_random()) print(SESSION.show_random()) SESSION.shutdown_service() -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From metolone+gmane at gmail.com Sat May 9 17:21:43 2009 From: metolone+gmane at gmail.com (Mark Tolonen) Date: Sat, 9 May 2009 14:21:43 -0700 Subject: unicode bit me References: <977863c9-c4d9-4563-985a-3849d6ed0d75@d2g2000pra.googlegroups.com><994147fb-cdf3-4c55-8dc5-62d769b12cdc@u9g2000pre.googlegroups.com> Message-ID: "Piet van Oostrum" wrote in message news:m263gagjjl.fsf at cs.uu.nl... >>>>> "Mark Tolonen" (MT) wrote: >>MT> wrote in message >>MT> >>news:994147fb-cdf3-4c55-8dc5-62d769b12cdc at u9g2000pre.googlegroups.com... >>>> Sorry being unclear again, hmm I am becoming an expert in it. >>>> >>>> I pasted that code as continuation of my old code at start >>>> i.e >>>> class A(object): >>>> def __unicode__(self): >>>> return u"?au" >>>> >>>> def __repr__(self): >>>> return unicode(self).encode("utf-8") >>>> __str__ = __repr__ >>>> >>>> doesn't work means throws unicode error >>>> my question boils down to >>>> what is diff between, why one doesn't throws error and another does >>>> print unicode(a) >>>> vs >>>> print unicode([a]) >>MT> That is still an incomplete example. Your results depend on your >>source >>MT> code's encoding and your system's stdout encoding. Assuming a=A(), >>MT> unicode(a) returns u'?au', but then is converted to stdout's encoding >>for >>MT> display. >You are confusing the issue. It does not depend on the source code's >encoding (supposing that the encoding declaration in the source is >correct). repr returns unicode(self).encode("utf-8"), so it is utf-8 >encoded even when the source code had a different encoding. The u"?au" >string is not dependent on the source encoding. Sorry about that. I'd forgotten that the OP'd forced __repr__ to utf-8. You bring up a good point, though, that the encoding the file is actually saved in and the encoding declaration in the source have to match. Many people get that wrong as well. -Mark From UrsusMaximus at gmail.com Sat May 9 17:28:21 2009 From: UrsusMaximus at gmail.com (UrsusMaximus at gmail.com) Date: Sat, 9 May 2009 14:28:21 -0700 (PDT) Subject: "Beginning Python Visualization" by Shai Vaingast Message-ID: I just read this book and liked it. Here is a review: Sometimes a picture is worth a thousand words. "Beginning Python Visualization: Creating Visual Transformation Scripts", published in February 2009 by Apress, shows how Python and its related tools can be used to easily and effectively turn raw data into visual representations that communicate effectively. The author is Shai Vaingast, a professional engineer and engineering manager who needed to train scientists and engineers to do this kind of programming work. He was looking for a tutorial and reference work, and unable to find a suitable text, wound up writing his first book. He wrote in the easy and clear style of someone comfortable and engaged with the subject matter. The book uses several very specific examples that illustrate general principles. The first example is using GPS data. By using Python one can extract data from GPS receivers and enter it into the computer and manipulate it to do what one wants including creating graphs and charts. In this section he shows how to use CSV, comma separated values, as a most useful file format. He shows show to extract data from real world GPS devices and import it via serial ports and the PySerial module. It would be easy for the reader to duplicate and extend this project. The heart of the book is coverage of useful examples utilizing MatPlotLib, NumPy and SciPy. These related tools are easy to use and fully integrated with Python. MatPlotLib is for plotting data and graphs, including interactive graphs and image files. NumPy is a powerful math library comparable to commercial tools like MatLab, and SciPy extends NumPy to for the sciences. Examples are numerous and include signal analysis using Fourier transforms. There is also a section on Image Processing using PIL, the Python Imaging Library. This is used for relatively simple image cropping and sizing and also for bit by bit image processing. Interpolation and curve fitting are also well covered. For anyone wanting an introduction to graphical analysis of statistical data, this would be an excellent resource. The author is obviously a professional in this field. He has a knack for good organizational style and a pragmatic approach to the work. In the book he says "Most of the time, research is organized chaos. The emphasis, however, should be on organized, not chaos." A real value I got from the book is a better understanding of data files, format, and organization as well as methods and guidelines for selecting file formats and storing and organizing data to enable fast and efficient data processing. It is obvious that this book was written by a practicing engineer. The theme of the book is that Python can be an all purpose environment for data manipulation and visualization, using nothing but free and open source tools that are easily integrated and scriptable without using multiple programming languages. The book should be an invaluable tool for scientists and engineers but it is also easily accessible to anyone interested in math and data analysis. There is no need for an advanced math background. While, as a matter of full disclosure, I have undergraduate degrees in Math and Physics, I feel the book should be easily accessible to anyone with a solid high school math background who is seriously interested in the subject. The book contains a short introductory tutorial on the basics of Python so anyone familiar with programming in any language should be fine. The book is an easy read from front to back, and I am sure it will also be a good reference resource for the future. The writing style is very clear and unforced and I found surprisingly few errors. While the Python world has a surplus of introductory and general books, books covering this kind of specific domain are especially welcome, and we could use more on other topics by competent authors. At 363 pages the book is a surprisingly fast read. Its methodology is to use specific, short code examples to make all the key points. Most of the code samples are well selected, short and written in clear, concise Python. This is not the kind of book that overwhelms you with massive amounts of code. Either the book was well edited or else it was written by an exceptionally lucid thinker, or both. So, if you want to learn how to process, organize, and visualize data from various sources using the Python language, I recommend this book to you. I also posted a podcast of an interview with the author, Shai Vaingast, at Python411 From castironpi at gmail.com Sat May 9 17:33:06 2009 From: castironpi at gmail.com (Aaron Brady) Date: Sat, 9 May 2009 14:33:06 -0700 (PDT) Subject: Why there is a parameter named "self" for classmethod function? References: <2b767f890905061949g12d0bf03m68c6b466ffedfbd9@mail.gmail.com> <4A024F18.9080407@kbsymanzik.org> <2c80e8d0-cc44-48b3-8b90-7b165eb20ed4@g20g2000vba.googlegroups.com> <0214c701$0$20647$c3e8da3@news.astraweb.com> Message-ID: <25e645ad-e471-4f81-9cfa-c979dd1e9f2b@b1g2000vbc.googlegroups.com> On May 8, 7:52?pm, Steven D'Aprano wrote: > On Thu, 07 May 2009 04:27:15 -0700, Aaron Brady wrote: > >> Can be, but if there's reason enough to keep it with a class, there's > >> no reason not to. > > > That's a bit of hyperbole; the usual reasons such as code bloat, > > namespace bloat, maintainability etc. all weigh against it. ?It's just > > that the benefits weigh heavier. > > I'm not sure I understand. Whether the function is in the class or > extracted out into the module, it is exactly the same function. (Apart > from an extra level of indentation, and either an unused "self" parameter > or a @staticmethod line.) Same amount of code, same difficulty of > maintainability. Namespace bloat is also the same, the only difference > being which namespace is bloated: the class or the method. > > -- > Steven My post was a little harsh. I meant to say that I think I got your idea, but the form it came in was a tautology. Its form was X if not not X. Tautologies come across as particularly low bandwidth in writing, though they do have some content in context. I observe people say them a lot too: 'it is what it is', 'if I want to I want to', 'two bucks is two bucks', etc. Some of the context comes from the partial conventional asymmetry of sentences in English between subject and predicate (nominative and accusative), though there is none in logic. Not to mention, 'hyperbole' isn't the word for what I was thinking either... ahem. The best place for such a function between those two choices can depend on many things, some of which are matters of taste. If the definition occurs in the class, it may be more convenient to read or edit. That difference is pretty negligible. There is semantic difference to inherited classes: whether you have to import the class or the class /and/ the function. Then when it is called, the form will be either 'class.methodA' or just 'methodA', which makes a difference in readability (obviousness) elsewhere that it is called. > being which namespace is bloated: the class or the method. I think you meant 'class or module'. From dina.ali08 at googlemail.com Sat May 9 17:56:17 2009 From: dina.ali08 at googlemail.com (Dina Ali) Date: Sat, 9 May 2009 22:56:17 +0100 Subject: pyMPI error on mpi.barrier Message-ID: <4a031c930905091456gd21694ex5cb93ca15cae21f8@mail.gmail.com> Hello there I am trying to paralleize GA code using pyMPI. part of the code and and the error message is as below. i write the new positions in a file by root (which is mpi.rank = 0) then other processes are suppose to wait until the written in the file finishes to start evaluating the objective. the problem arises in the barrier method... any ideas on how to do this would be very much appreciated.. for i in xrange(max_iter): if (mpi.rank == 0): update_positions() save_GA_pops_iter() mpi.barrier() <---------------------------- error is here evaluate_objective() mpi.barrier() error message: mpi.barrier() mpi.MPIError: MPI_Error_string: invalid communicator: Input/output error (pyMPI_comm_io.c:367) Thanks very much Dina -------------- next part -------------- An HTML attachment was scrubbed... URL: From dina.ali08 at googlemail.com Sat May 9 17:56:55 2009 From: dina.ali08 at googlemail.com (Dina Ali) Date: Sat, 9 May 2009 22:56:55 +0100 Subject: pyMPI error on mpi.barrier In-Reply-To: <4a031c930905091456gd21694ex5cb93ca15cae21f8@mail.gmail.com> References: <4a031c930905091456gd21694ex5cb93ca15cae21f8@mail.gmail.com> Message-ID: <4a031c930905091456r7ac4e827o5cff61b959f092c8@mail.gmail.com> Hello there I am trying to paralleize GA code using pyMPI. part of the code and and the error message is as below. i write the new positions in a file by root (which is mpi.rank = 0) then other processes are suppose to wait until the written in the file finishes to start evaluating the objective. the problem arises in the barrier method... any ideas on how to do this would be very much appreciated.. for i in xrange(max_iter): if (mpi.rank == 0): update_positions() save_GA_pops_iter() mpi.barrier() <---------------------------- error is here evaluate_objective() mpi.barrier() error message: mpi.barrier() mpi.MPIError: MPI_Error_string: invalid communicator: Input/output error (pyMPI_comm_io.c:367) Thanks very much Dina -------------- next part -------------- An HTML attachment was scrubbed... URL: From dina.ali08 at googlemail.com Sat May 9 17:57:13 2009 From: dina.ali08 at googlemail.com (Dina Ali) Date: Sat, 9 May 2009 22:57:13 +0100 Subject: pyMPI error on mpi.barrier In-Reply-To: <4a031c930905091456r7ac4e827o5cff61b959f092c8@mail.gmail.com> References: <4a031c930905091456gd21694ex5cb93ca15cae21f8@mail.gmail.com> <4a031c930905091456r7ac4e827o5cff61b959f092c8@mail.gmail.com> Message-ID: <4a031c930905091457s614e0f9bkb2dc7a6426aa71b0@mail.gmail.com> Hello there I am trying to paralleize GA code using pyMPI. part of the code and and the error message is as below. i write the new positions in a file by root (which is mpi.rank = 0) then other processes are suppose to wait until the written in the file finishes to start evaluating the objective. the problem arises in the barrier method... any ideas on how to do this would be very much appreciated.. for i in xrange(max_iter): if (mpi.rank == 0): update_positions() save_GA_pops_iter() mpi.barrier() <---------------------------- error is here evaluate_objective() mpi.barrier() error message: mpi.barrier() mpi.MPIError: MPI_Error_string: invalid communicator: Input/output error (pyMPI_comm_io.c:367) Thanks very much Dina -------------- next part -------------- An HTML attachment was scrubbed... URL: From M8R-gqmgb4 at mailinator.com Sat May 9 18:11:21 2009 From: M8R-gqmgb4 at mailinator.com (uuid) Date: Sun, 10 May 2009 00:11:21 +0200 Subject: Get multiprocessing.Queue to do priorities References: <4a05b27a_2@news.arcor-ip.de> <4a05b6c6_2@news.arcor-ip.de> <28KdnTocUpw2X5jXnZ2dnUVZ_sudnZ2d@pdx.net> <529683651263584828.031438address_is-invalid.invalid@news.arcor.de> <-vKdnex07oQzQpjXnZ2dnUVZ_hZi4p2d@pdx.net> Message-ID: <4a05ff89_1@news.arcor-ip.de> The Queue module, apparently, is thread safe, but *not* process safe. If you try to use an ordinary Queue, it appears inaccessible to the worker process. (Which, after all, is quite logical, since methods for moving items between the threads of the same process are quite different from inter-process communication.) It appears that creating a manager that holds a shared queue might be an option (http://stackoverflow.com/questions/342556/python-2-6-multiprocessing-queue-compatible-with-threads). Just for illustration: This shows that Queue.Queue doesn't work with processes: ------------------------ def worker(queue): while True: item = queue.get() print item queue.task_done() queue_queue = Queue.Queue() worker_thread = multiprocessing.Process(target=worker, args=(queue_queue,)) worker_thread.start() for i in range(10): queue_queue.put(str(i)) time.sleep(10) while True: try: print 'still on queue: ' + queue_queue.get(False) except Queue.Empty: break worker_thread.join() ------------------------ This yields: still on queue: 0 still on queue: 1 still on queue: 2 still on queue: 3 still on queue: 4 still on queue: 5 still on queue: 6 still on queue: 7 still on queue: 8 still on queue: 9 So no queue item ever arrives at the worker process. On 2009-05-09 22:00:36 +0200, Scott David Daniels said: > 2.6 has a PriorityQueue in the Queue module. > If you aren't using 2.6, you could copy the code for your own version. From ben+python at benfinney.id.au Sat May 9 19:24:11 2009 From: ben+python at benfinney.id.au (Ben Finney) Date: Sun, 10 May 2009 09:24:11 +1000 Subject: Wrapping comments References: <87zldmcod8.fsf@benfinney.id.au> Message-ID: <87vdo9dfms.fsf@benfinney.id.au> Tobias Weber writes: > In article <87zldmcod8.fsf at benfinney.id.au>, > Ben Finney wrote: > > > Yes. Both Emacs and Vim will do exactly this when re-wrapping a > > Thought so. And editors without a learning curve? A text editor that will be useful in all of your computing is something worth learning. Instead of needing to re-learn an editor every time you take up a new text editing task, pick a powerful, customisable editor and learn it *once*. Either of Emacs or Vim are good choices. -- \ ?The trouble with the world is that the stupid are cocksure and | `\ the intelligent are full of doubt.? ?Bertrand Russell | _o__) | Ben Finney From ldo at geek-central.gen.new_zealand Sat May 9 20:09:13 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Sun, 10 May 2009 12:09:13 +1200 Subject: Wrapping comments References: Message-ID: In message , Tobias Weber wrote: > the guideline (PEP 8) is hard wrap to 7x characters. Nowadays I set my maximum line lengths, and the widths of my terminal windows to 100 characters. And I'm wondering whether or not to go wider still. After all, the screens can take it. From aahz at pythoncraft.com Sat May 9 20:09:48 2009 From: aahz at pythoncraft.com (Aahz) Date: 9 May 2009 17:09:48 -0700 Subject: About twisted.mail.smtp.SMTPDeliveryError References: <8bb91b91-c00d-48d1-8b64-6f4765754e1c@d19g2000prh.googlegroups.com> Message-ID: In article <8bb91b91-c00d-48d1-8b64-6f4765754e1c at d19g2000prh.googlegroups.com>, gganesh wrote: > >MAILSERVER = 'mail.xxx.com' >done = sendmail(MAILSERVER, FROM, listTo, >MSGBODY ,senderDomainName=None, port=25) > >"Failure: twisted.mail.smtp.SMTPDeliveryError: 554 No recipients >accepted" >and >"Relay access denied" errors > >Is this error is because, i haven't authenticated my email account to >send mail. Is so , having username and password of smpt server ,how to >authenticate in Twisted Framework Are you doing this from your local machine? If yes, you probably need to use your ISP's SMTP server; blocking non-local access is one technique for keeping zombied machines from spewing malware. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "It is easier to optimize correct code than to correct optimized code." --Bill Harlan From Scott.Daniels at Acm.Org Sat May 9 20:20:40 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Sat, 09 May 2009 17:20:40 -0700 Subject: Wrapping comments In-Reply-To: References: Message-ID: Lawrence D'Oliveiro wrote: > In message , Tobias Weber > wrote: > >> the guideline (PEP 8) is hard wrap to 7x characters. > > Nowadays I set my maximum line lengths, and the widths of my terminal > windows to 100 characters. And I'm wondering whether or not to go wider > still. After all, the screens can take it. > But the printers cannot. And I wind up quietly cursing code writers who figure the only window I look at is the one they put the code in. In other words, I have a large enough screen to look at two pages side-by-side, and somebody decides he wants to use 2/3 of that width. Think of your reader, not just your operational environment, unless you are programming on your own and don't care to share. --Scott David Daniels Scott.Daniels at Acm.Org From steve at REMOVE-THIS-cybersource.com.au Sat May 9 20:36:27 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 10 May 2009 00:36:27 GMT Subject: I'm intrigued that Python has some functional constructions in the language. References: <44350436-de1a-451e-95f5-13b902a902ce@s21g2000vbb.googlegroups.com> <9e68a155-177d-4e03-97b6-c19cbb986515@z19g2000vbz.googlegroups.com> <7fa66f4e-e88c-4fb4-a509-911756814f89@o27g2000vbd.googlegroups.com> <0215bccb$0$20647$c3e8da3@news.astraweb.com> <7xfxfet7pa.fsf@ruckus.brouhaha.com> Message-ID: <021614c1$0$20647$c3e8da3@news.astraweb.com> On Sat, 09 May 2009 12:08:49 -0700, Paul Rubin wrote: > Steven D'Aprano writes: >> I think your point is wrong. Without syntax, there can be no written >> communication. In Haskell, f.g is not the same as f+g -- the difference >> is one of syntax. > > In Haskell, (+) and (.) are both functions. "Left-parens op right-parens" is syntax. > So it's really true you can get rid of almost all Haskell expression > syntax. And what you've got left is syntax. Without syntax, how can you tell the difference between a meaningful character string and a jumble of random gibberish? Without syntax, how can you tell where one token finishes and the next begins? -- Steven From pavlovevidence at gmail.com Sat May 9 21:23:13 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Sat, 9 May 2009 18:23:13 -0700 (PDT) Subject: I'm intrigued that Python has some functional constructions in the language. References: <44350436-de1a-451e-95f5-13b902a902ce@s21g2000vbb.googlegroups.com> <9e68a155-177d-4e03-97b6-c19cbb986515@z19g2000vbz.googlegroups.com> <7fa66f4e-e88c-4fb4-a509-911756814f89@o27g2000vbd.googlegroups.com> Message-ID: <24381226-a7a0-4fbb-b928-644609e5909b@w31g2000prd.googlegroups.com> On May 9, 10:57?am, namekuseijin wrote: > Carl Banks wrote: > > On May 8, 7:19 pm, namekuseijin wrote: > >> On May 8, 10:13 pm, Carl Banks wrote: > >> In Haskell, Lisp and other functional programming languages, any extra > >> syntax gets converted into the core lambda constructs. > > > So? ?The user still uses that syntax, so how can you claim it doesn't > > matter? > >> ?In Lisp > >> languages, that syntax is merely user-defined macros, but in Haskell > >> it's builtin the compiler for convenience. > > > I don't even know what you're saying here > > I'm saying syntax is nothing special. ?They are user-defined, as > functions. ?And it all gets converted into functions. ?Functions matter, > syntax is irrelevant because you can do away with it. Nope, sorry, you're ignoring half the problem here. Syntax is only irrelevant if you actually do do away with it. As long as syntax is there and people use it, then it matters, regardless of whether it all reduces to function calls. For a very benign example, consider the ways that Python and Haskell write listcomps: [ x for x in ss ] [ x | x <- ss ] One of these might be more readable than the other (I make no implication which); however, readability has nothing to do with whether the compiler internally reduces it to a function call or not. Readibility counts, therefore syntax matters. Now, maybe readability concerns don't matter to you personally, but it does matter to the OP, who is trying to advocate functional programming but is having difficulty because most purely functional languages have hideous minimalist syntax that turns people off. If that syntax could be made a little more readable and workable, maybe people wouldn't turn up their noses at it on first sight. > In Haskell, point free style of programming shows almost no signs of > predefined syntax at all. ?It's all function composition. > > In functional programming languages, predefined syntax is mostly > irrelevant. ?In Python and other imperative languages, it's absolutely > necessary. ?That's my point. I think you are overstating this by a lot. Carl Banks From namekuseijin.nospam at gmail.com Sat May 9 21:28:05 2009 From: namekuseijin.nospam at gmail.com (namekuseijin) Date: Sat, 09 May 2009 22:28:05 -0300 Subject: I'm intrigued that Python has some functional constructions in the language. In-Reply-To: <0215bccb$0$20647$c3e8da3@news.astraweb.com> References: <44350436-de1a-451e-95f5-13b902a902ce@s21g2000vbb.googlegroups.com> <9e68a155-177d-4e03-97b6-c19cbb986515@z19g2000vbz.googlegroups.com> <7fa66f4e-e88c-4fb4-a509-911756814f89@o27g2000vbd.googlegroups.com> <0215bccb$0$20647$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > On Sat, 09 May 2009 14:57:24 -0300, namekuseijin wrote: > >> I'm saying syntax is nothing special. They are user-defined, as >> functions. And it all gets converted into functions. Functions matter, >> syntax is irrelevant because you can do away with it. > > How do you call functions without syntax? By mental telepathy? By direct > manipulation of the electromagnetic fields inside the CPU? That's all the syntax notation you need in functional programming languages: one to define functions, and another to call them. Any extras are sugar around the basic definition and calling forms. From ldo at geek-central.gen.new_zealand Sat May 9 21:41:15 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Sun, 10 May 2009 13:41:15 +1200 Subject: Wrapping comments References: Message-ID: In message , Scott David Daniels wrote: > Lawrence D'Oliveiro wrote: > >> In message , Tobias Weber >> wrote: >> >>> the guideline (PEP 8) is hard wrap to 7x characters. >> >> Nowadays I set my maximum line lengths, and the widths of my terminal >> windows to 100 characters. And I'm wondering whether or not to go wider >> still. After all, the screens can take it. >> > But the printers cannot. If I had to print out all the code and documentation I have to look at, I'd need to add another room to my house. From ldo at geek-central.gen.new_zealand Sat May 9 21:48:20 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Sun, 10 May 2009 13:48:20 +1200 Subject: Wrapping comments References: Message-ID: In message , Lawrence D'Oliveiro wrote: > In message , Scott David Daniels > wrote: > >> Lawrence D'Oliveiro wrote: >> >>> In message , Tobias Weber >>> wrote: >>> >>>> the guideline (PEP 8) is hard wrap to 7x characters. >>> >>> Nowadays I set my maximum line lengths, and the widths of my terminal >>> windows to 100 characters. And I'm wondering whether or not to go wider >>> still. After all, the screens can take it. >>> >> But the printers cannot. > > If I had to print out all the code and documentation I have to look at, > I'd need to add another room to my house. And yes, the printers most certainly can take it. Back in the days when you couldn't even choose different fonts, the most common width for printouts was 132 columns. From http Sat May 9 21:51:20 2009 From: http (Paul Rubin) Date: 09 May 2009 18:51:20 -0700 Subject: I'm intrigued that Python has some functional constructions in the language. References: <44350436-de1a-451e-95f5-13b902a902ce@s21g2000vbb.googlegroups.com> <9e68a155-177d-4e03-97b6-c19cbb986515@z19g2000vbz.googlegroups.com> <7fa66f4e-e88c-4fb4-a509-911756814f89@o27g2000vbd.googlegroups.com> <0215bccb$0$20647$c3e8da3@news.astraweb.com> <7xfxfet7pa.fsf@ruckus.brouhaha.com> <021614c1$0$20647$c3e8da3@news.astraweb.com> Message-ID: <7xeiux9147.fsf@ruckus.brouhaha.com> Steven D'Aprano writes: > > So it's really true you can get rid of almost all Haskell expression > > syntax. > > And what you've got left is syntax. Without syntax, how can you tell the > difference between a meaningful character string and a jumble of random > gibberish? Without syntax, how can you tell where one token finishes and > the next begins? I didn't say you could get rid of absolutely all of the syntax. Just most of it. All that stuff you'd write in Python with loops, if-statements, listcomps, etc. can be done with prefix function calls in Haskell. Haskell's type system, on the other hand, is much more complex than Python's, and writing out type specifications does require some syntax. Overall though, Haskell's syntax, either at the sugared or desugared level, is not very complicated. The hurdles to learning Haskell really are because the concepts it contains are really different than what most programmers are familiar with. It's not like many languages that contain familiar concepts wrapped in new syntax. From gdamjan at gmail.com Sat May 9 23:04:17 2009 From: gdamjan at gmail.com (=?UTF-8?B?0JTQsNC80ZjQsNC9INCT0LXQvtGA0LPQuNC10LLRgdC60Lg=?=) Date: Sun, 10 May 2009 05:04:17 +0200 Subject: How can I dynamically insert a base class in a given class Message-ID: How can I dynamically insert a base class in a given class? Yeah, I'm writing a class decorator that needs to manipulate the class by inserting another base class in it. Something like: class ReallyBase(object): def someimportantmethod(self): return 'really really' @expose(args) class Unsuspecting(object): def stuff(self): return "ok" I've come up with this ... but I'm sure it has some shortcomings def expose(args): def wrap(cls): # do stuff with args and cls return type(cls.__name__, cls.__bases__ + (ReallyBase,), dict(cls.__dict__)) return wrap -- ?????? ( http://softver.org.mk/damjan/ ) war is peace freedom is slavery restrictions are enablement From mnordhoff at mattnordhoff.com Sat May 9 23:21:43 2009 From: mnordhoff at mattnordhoff.com (Matt Nordhoff) Date: Sun, 10 May 2009 03:21:43 +0000 Subject: confused with subprocess.Popen In-Reply-To: References: Message-ID: <4A064847.4080509@mattnordhoff.com> Soumen banerjee wrote: > Hello, > for a certain app, i used to use a command: > os.system("soundwrapper espeak -f line.txt") > now, if i wanted to kill espeak, i would have to run: > os.system("killall espeak") > since the subprocess module allows sending SIGKILL to the process, i > decided to switch to using it. However i cant pass the spaced out > arguments to it. For example: > a=subprocess.Popen("soundwrapper espeak -f line.txt") > results in a OSError, no such file or directory. > How do i use the Popen function? > Regards > Soumen Read subprocess's documentation. It takes a list, not a string: >>> a = subprocess.Popen(["soundwrapper", "espeak", "-f", "line.txt"]) -- From grante at visi.com Sat May 9 23:34:48 2009 From: grante at visi.com (Grant Edwards) Date: Sat, 09 May 2009 22:34:48 -0500 Subject: Wrapping comments References: Message-ID: On 2009-05-10, Scott David Daniels wrote: >> Nowadays I set my maximum line lengths, and the widths of my terminal >> windows to 100 characters. And I'm wondering whether or not to go wider >> still. After all, the screens can take it. >> > But the printers cannot. Printers? The people who produce books? What've they got to do with it? -- Grant From dmw at coder.cl Sun May 10 00:09:13 2009 From: dmw at coder.cl (Daniel Molina Wegener) Date: Sun, 10 May 2009 00:09:13 -0400 Subject: [ANN] pyxser-1.0R --- python xml serialization Message-ID: <7592896.nUPlyArG6x@coder.cl> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello, I'm pleased to announce pyxser-1.0R, a Python-Object to XML serializer and deserializer. This package it's completly written in C and licensed under LGPLv3. The tested Python versions are 2.5.X and 2.7.X. * home page: http://coder.cl/software/pyxser * hosted at: https://sourceforge.net/projects/pyxser/ The current ChangeLog is as follows: 1.0rc1 (2009.04.26): Daniel Molina Wegener * Added both byte string and unicode returning and procesing functions. This means that we have serialize(), serialize_c14n(), serialize_c14n_strict(), unserialize(), validate() and validate_c14n() functions for byte strings and u_serialize(), u_serialize_c14n(), u_serialize_c14n_strict(), u_unserialize(), u_validate() and u_validate_c14n() for unicode strings. * Removed memory leaks, now it runs over 1400000 pyxser operations without crashing (100000 cicles testing every function). * Removed bugs concerning reference counting that was crashing when trying to access unserialized objects attributes. * This is an approach to a production environment release ;) 1.0r (2009.05.09): Daniel Molina Wegener * Set default validation to XML Schema validation, to allow pyxser to work with WebServices and similar technologies, without restricting the namespace prefix to pyxs. * Removed memory leaks concerning XML Schema validation. * Succeful tests with more than 1.200.000 pyxser operations. * Added pyxser 1.0 XML Schema to allow imports of the pyxser model into WebServices WSDL and similar technologies. * Addded getxsd(), getxsd_c14n(), u_validate_c14n_dtd(), u_validate_dtd(), validate_c14n_dtd(), validate_dtd() functions. * All *_dtd() related functions allows you to validate pyxser documents using the pyxser 1.0 DTD, instead of the pyxser 1.0 XML Schema. Feel free to use and test. You can modify test-utf8-prof.py to use more than 100.000 cicles without crashing. Best regards, - -- .O.| Daniel Molina Wegener | C/C++ Developer ..O| dmw [at] coder [dot] cl | FreeBSD & Linux OOO| http://coder.cl/ | Standards Basis -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (GNU/Linux) iQIcBAEBAgAGBQJKBlOHAAoJEHxqfq6Y4O5NMvQP/30ZaiM93XSOc4HTY9Qu/Ezg kdpBhMDpLRKjHTm2g5qN1aYbpAb2wg5EOuAP9ynuKKmxXGEfFxLfsamyVsQfpohg wJauTSyIdbvfmHk0MwF4fHs9UpTWaC68uxcwYEDxeQllTQje55LQnTyGbPdN0I1j u1PDZSwtaB2ivTUJI30RDSmdtGQFUo9xyCQpyc3chyA1tkVUwiUQ5be7DQvCeZYS xoq33jVDcGCDDgUVmmF6WZup37gL458lK2OV41F8HCBljuOh9iwqiNjc760fYzH+ ZKhjCC45jPpptFgv/b/8AZ6bp3wVYf6fK0g40uJnh9Lpbu4mZUl7l68/3lfsc9UI FQPVUY0wdzYEyq3Th7fT3XnbjHdNSRYMrDKQnNeeWZcNaKVtCzRom1rZqib2bp2x Lyb1hMP80Zcy1U/HCZxqdPt+zMiQHWFT+mxBNBoepFC4NAIKbfciXRz/MUdQqnEb n+5mxv5j7Xyrd62ifWM2Bq/76+GO0bIvlb3floQV5lVkq7qmUojrZFHz04u07IuU 3y4l1V2MbdZDhRTmPNWETZp5DloLV5pDD+4sdZcbxDdPOIvEJgb0vLiQEtXOGWvM v+rqkKlhI/s8pqdAhTXoGcRWgRjQPuF9OmRkGcmnSingruaeC1Wteuk8dVIqwq37 isi/Utv8KKRehRknIfFL =YN3e -----END PGP SIGNATURE----- From anuraguniyal at yahoo.com Sun May 10 00:19:36 2009 From: anuraguniyal at yahoo.com (anuraguniyal at yahoo.com) Date: Sat, 9 May 2009 21:19:36 -0700 (PDT) Subject: unicode bit me References: <977863c9-c4d9-4563-985a-3849d6ed0d75@d2g2000pra.googlegroups.com><994147fb-cdf3-4c55-8dc5-62d769b12cdc@u9g2000pre.googlegroups.com> Message-ID: <5e8242d0-1225-41e6-bc40-3016670b8618@x31g2000prc.googlegroups.com> First of all thanks everybody for putting time with my confusing post and I apologize for not being clear after so many efforts. here is my last try (you are free to ignore my request for free advice) # -*- coding: utf-8 -*- class A(object): def __unicode__(self): return u"?au" def __repr__(self): return unicode(self).encode("utf-8") __str__ = __repr__ a = A() u1 = unicode(a) u2 = unicode([a]) now I am not using print so that doesn't matter stdout can print unicode or not my naive question is line u2 = unicode([a]) throws UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 1: ordinal not in range(128) shouldn't list class call unicode on its elements? I was expecting that so instead do i had to do this u3 = "["+u",".join(map(unicode,[a]))+"]" From anuraguniyal at yahoo.com Sun May 10 00:21:22 2009 From: anuraguniyal at yahoo.com (anuraguniyal at yahoo.com) Date: Sat, 9 May 2009 21:21:22 -0700 (PDT) Subject: unicode bit me References: <977863c9-c4d9-4563-985a-3849d6ed0d75@d2g2000pra.googlegroups.com><994147fb-cdf3-4c55-8dc5-62d769b12cdc@u9g2000pre.googlegroups.com> <5e8242d0-1225-41e6-bc40-3016670b8618@x31g2000prc.googlegroups.com> Message-ID: <805c54f1-95ca-4947-8f3e-3ec1fdb90c6e@v1g2000prd.googlegroups.com> and yes replace string by u'\N{COPYRIGHT SIGN}au' as mentioned earlier non-ascii char may not come correct posted here. On May 10, 9:19?am, "anuraguni... at yahoo.com" wrote: > First of all thanks everybody for putting time with my confusing post > and I apologize for not being clear after so many efforts. > > here is my last try (you are free to ignore my request for free > advice) > > # -*- coding: utf-8 -*- > > class A(object): > > ? ? def __unicode__(self): > ? ? ? ? return u"?au" > > ? ? def __repr__(self): > ? ? ? ? return unicode(self).encode("utf-8") > > ? ? __str__ = __repr__ > > a = A() > u1 = unicode(a) > u2 = unicode([a]) > > now I am not using print so that doesn't matter stdout can print > unicode or not > my naive question is line u2 = unicode([a]) throws > UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position > 1: ordinal not in range(128) > > shouldn't list class call unicode on its elements? I was expecting > that > so instead do i had to do this > u3 = "["+u",".join(map(unicode,[a]))+"]" From nad at acm.org Sun May 10 00:27:56 2009 From: nad at acm.org (Ned Deily) Date: Sun, 10 May 2009 00:27:56 -0400 Subject: MacPython 3.0.1 installation problem, no /usr/local/bin/python* References: Message-ID: In article , Benjamin Kaplan wrote: > On Sat, May 9, 2009 at 4:30 AM, Raoul Gough wrote: > > I just installed MacPython 3.0.1 (woot!) but the installer didn't seem > > to do an absolutely complete job. According to the ReadMe.txt, "The > > installer puts [...] command-line tools in /usr/local/bin [...]". This > > didn't happen - even after the install reported Success, there were no > > python files or symlinks under /usr/local/bin. > > > > Also, I'm guessing there should have been a symlink to the current > > version under /Library/Frameworks/Python.framework/Versions, which > > wasn't there either: > > > > $ cd /Library/Frameworks/Python.framework > > $ ls -l Python > > lrwxr-xr-x 1 root admin 23 May 9 08:34 Python -> > > Versions/Current/Python > > $ ls -l Versions/Current > > ls: Versions/Current: No such file or directory > > > > So I fixed this all up as follows: > > > > $ cd /Library/Frameworks/Python.framework/Versions > > $ sudo ln -s 3.0 Current > > $ cd /usr/local/bin > > $ sudo ln -s $(find > > /Library/Frameworks/Python.framework/Versions/Current/bin -type f) . > > > > So did something go wrong with the installer, or is it all supposed to > > work somehow differently? > > Because Python 3 breaks compatibility with Python 2, the installers don't > install it as the default. The executable should be at > /usr/local/bin/python3.0 By default, the 3.0.1 installer does not install any links in /usr/local/bin. To do so, run or re-run the installer, click Customize, and then select the "UNIX command-line tools" package. That creates /usr/local/python3.0 and /usr/local/python links, among others. -- Ned Deily, nad at acm.org From aahz at pythoncraft.com Sun May 10 00:59:34 2009 From: aahz at pythoncraft.com (Aahz) Date: 9 May 2009 21:59:34 -0700 Subject: Simple programme - Just want to know whether this is correct way of coding References: <49f8cb54-f268-46bc-a585-6d116674d00d@k19g2000prh.googlegroups.com> <7d44d587-15c7-40ea-abe2-6a174f1fd0cd@q14g2000vbn.googlegroups.com> Message-ID: In article <7d44d587-15c7-40ea-abe2-6a174f1fd0cd at q14g2000vbn.googlegroups.com>, namekuseijin wrote: >On May 8, 3:37=A0am, guptha wrote: >> >> The code Works fine ,but I doubt about the performance issue ,My >> intention is to send mails concurrently to large number of mail. >> 1.For every mail id i send It creates a new SMTP object,in case, if i >> send to 1000 or more ids > >why should I help a spammer... ;) How many subscribers do you think there are to python-list? -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "It is easier to optimize correct code than to correct optimized code." --Bill Harlan From aahz at pythoncraft.com Sun May 10 01:01:19 2009 From: aahz at pythoncraft.com (Aahz) Date: 9 May 2009 22:01:19 -0700 Subject: Simple programme - Just want to know whether this is correct way of coding References: <49f8cb54-f268-46bc-a585-6d116674d00d@k19g2000prh.googlegroups.com> Message-ID: In article <49f8cb54-f268-46bc-a585-6d116674d00d at k19g2000prh.googlegroups.com>, guptha wrote: > >This is my first programme in python ,I need to know whether my code >is in the right path of performance > >I wrote a code using multithreading to send mails Here's the Right Way to do this: install your a local mailserver on your own machine and configure it to send to your external server. You should be able to pump hundreds of messages through it in minimal time. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "It is easier to optimize correct code than to correct optimized code." --Bill Harlan From kay at fiber-space.de Sun May 10 01:36:36 2009 From: kay at fiber-space.de (kay at fiber-space.de) Date: Sat, 9 May 2009 22:36:36 -0700 (PDT) Subject: Nimrod programming language References: Message-ID: On 8 Mai, 17:48, Andreas Rumpf wrote: > Dear Python-users, > > I invented a new programming language called "Nimrod" that combines Python's readability with C's performance. Please check it out:http://force7.de/nimrod/ > Any feedback is appreciated. > > Regards, > Andreas Rumpf > > ______________________________________________________ > GRATIS f?r alle WEB.DE-Nutzer: Die maxdome Movie-FLAT! > Jetzt freischalten unterhttp://movieflat.web.de On a first impression it looks quite pleasant to me, your new language! Two obvious questions on a Python list. 1) How to interface with Nimrod from Python? 2) Lack of typed hash-tables. Is this one of those particular features that are planned to be implemented in version 0.80 and summarized under "generic containers"? From soumen08 at gmail.com Sun May 10 01:37:20 2009 From: soumen08 at gmail.com (Soumen banerjee) Date: Sun, 10 May 2009 11:07:20 +0530 Subject: confused with subprocess.Popen In-Reply-To: <4A064847.4080509@mattnordhoff.com> References: <4A064847.4080509@mattnordhoff.com> Message-ID: Thanks!, i have found that alternately setting shell=True also works From Scott.Daniels at Acm.Org Sun May 10 02:19:21 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Sat, 09 May 2009 23:19:21 -0700 Subject: unicode bit me In-Reply-To: <5e8242d0-1225-41e6-bc40-3016670b8618@x31g2000prc.googlegroups.com> References: <977863c9-c4d9-4563-985a-3849d6ed0d75@d2g2000pra.googlegroups.com><994147fb-cdf3-4c55-8dc5-62d769b12cdc@u9g2000pre.googlegroups.com> <5e8242d0-1225-41e6-bc40-3016670b8618@x31g2000prc.googlegroups.com> Message-ID: anuraguniyal at yahoo.com wrote: > class A(object): > def __unicode__(self): > return u"?au" > def __repr__(self): > return unicode(self).encode("utf-8") > __str__ = __repr__ > a = A() > u1 = unicode(a) > u2 = unicode([a]) > > now I am not using print so that doesn't matter stdout can print > unicode or not > my naive question is line u2 = unicode([a]) throws > UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position > 1: ordinal not in range(128) > > shouldn't list class call unicode on its elements? > I was expecting that so instead do i had to do this > u3 = "["+u",".join(map(unicode,[a]))+"]" Why would you expect that? str([a]) doesn't call str on its elements. Using our simple expedient: class B(object): def __unicode__(self): return u'unicode' def __repr__(self): return 'repr' def __str__(self): return 'str' >>> unicode(B()) u'unicode' >>> unicode([B()]) u'[repr]' >>> str(B()) 'str' >>> str([B()]) '[repr]' Now if you ask _why_ call repr on its elements, the answer is, "so that the following is not deceptive: >>> repr(["a, b", "c"]) "['a, b', 'c']" which does not look like a 3-element list. --Scott David Daniels Scott.Daniels at Acm.Org From piet at cs.uu.nl Sun May 10 02:29:12 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sun, 10 May 2009 08:29:12 +0200 Subject: unicode bit me References: <977863c9-c4d9-4563-985a-3849d6ed0d75@d2g2000pra.googlegroups.com> <994147fb-cdf3-4c55-8dc5-62d769b12cdc@u9g2000pre.googlegroups.com> <5e8242d0-1225-41e6-bc40-3016670b8618@x31g2000prc.googlegroups.com> <805c54f1-95ca-4947-8f3e-3ec1fdb90c6e@v1g2000prd.googlegroups.com> Message-ID: >>>>> "anuraguniyal at yahoo.com" (ac) wrote: >ac> and yes replace string by u'\N{COPYRIGHT SIGN}au' >ac> as mentioned earlier non-ascii char may not come correct posted here. That shouldn't be a problem for any decent new agent when there is a proper charset declaration in the headers. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From __peter__ at web.de Sun May 10 02:32:34 2009 From: __peter__ at web.de (Peter Otten) Date: Sun, 10 May 2009 08:32:34 +0200 Subject: unicode bit me References: <977863c9-c4d9-4563-985a-3849d6ed0d75@d2g2000pra.googlegroups.com> <994147fb-cdf3-4c55-8dc5-62d769b12cdc@u9g2000pre.googlegroups.com> <5e8242d0-1225-41e6-bc40-3016670b8618@x31g2000prc.googlegroups.com> Message-ID: anuraguniyal at yahoo.com wrote: > First of all thanks everybody for putting time with my confusing post > and I apologize for not being clear after so many efforts. > > here is my last try (you are free to ignore my request for free > advice) Finally! This is the first of your posts that makes sense to me ;) > # -*- coding: utf-8 -*- > > class A(object): > > def __unicode__(self): > return u"?au" > > def __repr__(self): > return unicode(self).encode("utf-8") > > __str__ = __repr__ > > a = A() > u1 = unicode(a) > u2 = unicode([a]) > > now I am not using print so that doesn't matter stdout can print > unicode or not > my naive question is line u2 = unicode([a]) throws > UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position > 1: ordinal not in range(128) list doesn't have a __unicode__ method. unicode() therefore converts the list to str as a fallback and then uses sys.getdefaultencoding() to convert the result to unicode. > shouldn't list class call unicode on its elements? No, it calls repr() on its elements. This is done to avoid confusing output: >>> items = ["a, b", "[c]"] >>> items ['a, b', '[c]'] >>> "[%s]" % ", ".join(map(str, items)) '[a, b, [c]]' > I was expecting that so instead do i had to do this > u3 = "["+u",".join(map(unicode,[a]))+"]" Peter From piet at cs.uu.nl Sun May 10 03:24:36 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sun, 10 May 2009 09:24:36 +0200 Subject: xml in python References: Message-ID: >>>>> Shawn Milochik (SM) wrote: >SM> On Fri, May 8, 2009 at 3:46 AM, Rustom Mody wrote: >>> Can someone give me a heads up on xml parsing in python? >>> The context is that I want to write a simple docbook to text converter. >>> DOM is alright -- dont want to break my head with SAX just for performance >>> when my documents are not likely to be large. >>> >>> My problem is that there seems to be so many nearly equivalent ways (Pyxml? >>> Amara?) some of which have moved from 3rd party to builtin status that I am >>> not clear what is the current method of choice. >>> >>> Thanks >>> -- >SM> I've been using minidom for simple XML processing with success. The >SM> XML module seems to be the way to go, considering how many of its >SM> child modules are listed on http://docs.python.org/library/. >SM> import xml.dom.minidom as minidom These days ElementTree is considered the most pythonic way. http://docs.python.org/library/xml.etree.elementtree.html There is also a reimplementation of the ElementTree API based on libxml2 and libxslt, which has more features but requires a separate install. It is largely compatible with ElementTree, however. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From nick at craig-wood.com Sun May 10 03:30:05 2009 From: nick at craig-wood.com (Nick Craig-Wood) Date: Sun, 10 May 2009 02:30:05 -0500 Subject: Problems with datetime.datetime.strptime References: <4a05458d$0$24784$9b536df3@news.fv.fi> Message-ID: Tuomas Vesterinen wrote: > I hoped that I could get rid of my special module _strptime2 when > porting to Python 3.0. But testing is a disappointment. [snip] > C : > strftime('%a %b %e %H:%M:%S %Y')='Sat May 9 11:26:12 2009' > strptime('Sat May 9 11:26:12 2009','%a %b %e %H:%M:%S %Y')= > 'e' is a bad directive in format '%a %b %e %H:%M:%S %Y' [snip] > What to do. I'd start by checking out the code for python 3 trunk and seeing if I could fix it. If I could I'd update the unit tests and the documentation then submit the patch to the python bugtracker. If I couldn't fix it then I'd report it as a bug. -- Nick Craig-Wood -- http://www.craig-wood.com/nick From nick at craig-wood.com Sun May 10 03:30:05 2009 From: nick at craig-wood.com (Nick Craig-Wood) Date: Sun, 10 May 2009 02:30:05 -0500 Subject: unicode bit me References: <977863c9-c4d9-4563-985a-3849d6ed0d75@d2g2000pra.googlegroups.com> <994147fb-cdf3-4c55-8dc5-62d769b12cdc@u9g2000pre.googlegroups.com> <5e8242d0-1225-41e6-bc40-3016670b8618@x31g2000prc.googlegroups.com> Message-ID: anuraguniyal at yahoo.com wrote: > First of all thanks everybody for putting time with my confusing post > and I apologize for not being clear after so many efforts. > > here is my last try (you are free to ignore my request for free > advice) > > # -*- coding: utf-8 -*- > > class A(object): > > def __unicode__(self): > return u"?au" > > def __repr__(self): > return unicode(self).encode("utf-8") > > __str__ = __repr__ > > a = A() > u1 = unicode(a) > u2 = unicode([a]) > > now I am not using print so that doesn't matter stdout can print > unicode or not > my naive question is line u2 = unicode([a]) throws > UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position > 1: ordinal not in range(128) > > shouldn't list class call unicode on its elements? You mean when you call unicode(a_list) it should unicode() on each of the elements to build the resultq? Yes that does seem sensible, however list doesn't have a __unicode__ method at all so I guess it is falling back to using __str__ on each element, and which explains your problem exactly. If you try your example on python 3 then you don't need the __unicode__ method at all (all strings are unicode) and you won't have the problem I predict. (I haven't got a python 3 in front of me at the moment to test.) So I doubt you'll find the momentum to fix this since unicode and str integration was the main focus of python 3, but you could report a bug. If you attach a patch to fix it - so much the better! Here is my demonstration of the problem with python 2.5.2 >> class A(object): ... def __unicode__(self): ... return u"\N{COPYRIGHT SIGN}au" ... def __repr__(self): ... return unicode(self).encode("utf-8") ... __str__ = __repr__ ... >>> a = A() >>> str(a) '\xc2\xa9au' >>> repr(a) '\xc2\xa9au' >>> unicode(a) u'\xa9au' >>> L=[a] >>> str(L) '[\xc2\xa9au]' >>> repr(L) '[\xc2\xa9au]' >>> unicode(L) Traceback (most recent call last): File "", line 1, in UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 1: ordinal not in range(128) >>> unicode('[\xc2\xa9au]') Traceback (most recent call last): File "", line 1, in UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 1: ordinal not in range(128) >>> L.__unicode__ Traceback (most recent call last): File "", line 1, in AttributeError: 'list' object has no attribute '__unicode__' >>> unicode(str(L),"utf-8") u'[\xa9au]' -- Nick Craig-Wood -- http://www.craig-wood.com/nick From denize.paul at gmail.com Sun May 10 03:32:07 2009 From: denize.paul at gmail.com (pdenize) Date: Sun, 10 May 2009 00:32:07 -0700 (PDT) Subject: Help needed with filenames Message-ID: <64840f40-af1c-4e37-a0ab-d928594fca63@v35g2000pro.googlegroups.com> I have a program that reads files using glob and puts them into an XML file in UTF-8 using unicode(file, sys.getfilesystemencoding()).encode("UTF-8") This all works fine including all the odd characters like accents etc. However I also print what it is doing and someone pointed out that many characters are not printing correctly in the Windows command window. I have tried to figure this out but simply get lost in the translation stuff. if I just use print filename it has characters that dont match the ones in the filename (I sorta expected that). So I tried print unicode(file, sys.getfilesystemencoding()) expecting the correct result, but no. UnicodeEncodeError: 'charmap' codec can't encode character u'\u2013' I did notice that when a windows command window does a directory listing of these files the characters seem to be translated into close approximations (long dash to minus, special double quotes to simple double quotes, but still retains many of the accent chars). I looked at translate to do this but did not know how to determine which characters to map. Can anyone tell me what I should be doing here? From __peter__ at web.de Sun May 10 03:33:40 2009 From: __peter__ at web.de (Peter Otten) Date: Sun, 10 May 2009 09:33:40 +0200 Subject: import overwrites __name__ References: Message-ID: MRAB wrote: > Marco wrote: >> Hi, >> >> There happened something that I do not understand. Actually I don't even >> know how it can be possible. >> >> I import a module and then the name space of the importing module seems >> do be overwritten. >> >> my_name = __name__ >> print my_name >> print len(dir()) >> from x import y as z >> print __name__ >> print len(dir()) >> print my_name >> >> -> >> __main__ >> 119 >> x >> 117 >> unhandled NameError "name 'my_name' is not defined" >> >> The module x is from me, and I am not aware of doing anything cruel >> there. What evil thing can be done in its sub module that can result in >> that strange thing? >> > You'd have to show us the module. Try to make the smallest module that > has the same effect. $ cat x.py import sys globals().update(zip(*(range(110),)*2)) y = 42 print __name__ if __name__ == "__main__": a = b = 42 print len(dir()) from x import y as z try: print my_name except NameError, e: print 'unhandled NameError: "%s"' % e sys.exit() $ python x.py __main__ 119 x 117 unhandled NameError: "name 'my_name' is not defined" ;) From M8R-gqmgb4 at mailinator.com Sun May 10 03:40:04 2009 From: M8R-gqmgb4 at mailinator.com (uuid) Date: Sun, 10 May 2009 09:40:04 +0200 Subject: xml in python References: Message-ID: <4a0684d4_2@news.arcor-ip.de> On 2009-05-10 09:24:36 +0200, Piet van Oostrum said: >> > > These days ElementTree is considered the most pythonic way. > http://docs.python.org/library/xml.etree.elementtree.html > > There is also a reimplementation of the ElementTree API based on libxml2 > and libxslt, which has more features but requires a separate install. It > is largely compatible with ElementTree, however. Indeed - elementtree and its blazing C implementation celementtree are included in the standard install. However, I would also recommend checking out the latter option - lxml. It's very fast, has an elementtree compatible API and sports some tricks like XPath and XSLT. There's comprehensive documentation as well als tutorials, hints and tips here: http://codespeak.net/lxml/ From martin at v.loewis.de Sun May 10 03:40:44 2009 From: martin at v.loewis.de (=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Sun, 10 May 2009 09:40:44 +0200 Subject: Help needed with filenames In-Reply-To: <64840f40-af1c-4e37-a0ab-d928594fca63@v35g2000pro.googlegroups.com> References: <64840f40-af1c-4e37-a0ab-d928594fca63@v35g2000pro.googlegroups.com> Message-ID: <4a0684fc$0$24009$9b622d9e@news.freenet.de> > Can anyone tell me what I should be doing here? The console uses the "OEM code page". The Windows conversion routine from Unicode to the OEM code page provides the lossy conversion that you observe in listing. Unfortunately, the OEM code page conversion is not available from Python. What you can do is to use u.encode(sys.stdout.encoding, "replace") This will replace unprintable characters with question marks. Regards, Martin From yinon.me at gmail.com Sun May 10 04:19:29 2009 From: yinon.me at gmail.com (Yinon Ehrlich) Date: Sun, 10 May 2009 01:19:29 -0700 (PDT) Subject: Help needed with filenames References: <64840f40-af1c-4e37-a0ab-d928594fca63@v35g2000pro.googlegroups.com> Message-ID: <0e0974b3-781e-4b6b-a8d6-7448b31b49e2@o14g2000vbo.googlegroups.com> > I did notice that when a windows command window does a directory > listing of these files the characters seem to be translated into close > approximations (long dash to minus, special double quotes to simple > double quotes, but still retains many of the accent chars). ?I looked > at translate to do this but did not know how to determine which > characters to map. > > Can anyone tell me what I should be doing here? Hi, Seems like your problem is just with the correct representation of those characters in Windows command-line. I have seen two solutions for that: * Right-click on the top bar of the the command-line window, go to "properties--> Font", select a font that shows all characters correctly. * Even better: use http://sourceforge.net/projects/console - much better that Windows' default console. Good luck, Yinon From http Sun May 10 04:40:56 2009 From: http (Paul Rubin) Date: 10 May 2009 01:40:56 -0700 Subject: Nimrod programming language References: Message-ID: <7xd4ah73l3.fsf@ruckus.brouhaha.com> Andreas Rumpf writes: > I invented a new programming language called "Nimrod" that combines > Python's readability with C's performance. Please check it out: > http://force7.de/nimrod/ Any feedback is appreciated. Looks nice in many ways. You also know about PyPy and Felix (felix.sf.net)? It seems a little bit hackish that the character type is a byte. Python did something like that but it ended up causing a lot of hassles when dealing with real Unicode. I think you have to bite the bullet and have separate byte and char types, where char=unicode. It scares me a little about how there are references that can have naked pointers, escape the gc, etc. It would be good if the type system were powerful enough to guarantee the absence of these effects in some piece of data, unless the data is somehow marked with an "unsafe" type. I'd like to see ML-like algebraic types with pattern matching, not just tuple unpacking. The generic system should be able to analyze the AST of type parameters, e.g. iterator would be able to automatically generate an iterator over a list, a tree, or some other type of structure, by actually figuring out at compile time how T is constructed. It's sort of surprising that the compiler is written in a Pascal subset. Why not implement in Nimrod directly, and bootstrap through C? The language and library are missing arbitrary precision integer arithmetic, using GMP or something like that. It would be good to also be able to have value serialization and deserialization to both human readable (nested lists, etc) and binary (for IPC) formats. It's not obvious from the blurbs whether simple functional programming is supported, e.g. how would you implement a function like "map" or "filter"? What would the types be of those two functions? The roadmap says you might implement threading with transactional memory. How will the transactional memory interact with mutable data? Are you going to use OS threads, lightweight threads, or both? From anuraguniyal at yahoo.com Sun May 10 05:46:46 2009 From: anuraguniyal at yahoo.com (anuraguniyal at yahoo.com) Date: Sun, 10 May 2009 02:46:46 -0700 (PDT) Subject: unicode bit me References: <977863c9-c4d9-4563-985a-3849d6ed0d75@d2g2000pra.googlegroups.com> <994147fb-cdf3-4c55-8dc5-62d769b12cdc@u9g2000pre.googlegroups.com> <5e8242d0-1225-41e6-bc40-3016670b8618@x31g2000prc.googlegroups.com> Message-ID: <05b9f8b9-c8ea-4591-b81e-d34ed2ccea29@s38g2000prg.googlegroups.com> ok that explains it, so unicode(obj) calls __unicode__ on that object and if it isn't there __repr__ is used __repr__ of list by default return a str even if __repr__ of element is unicode so my only solution looks like to use my own list class everywhere i use list class mylist(list): def __unicode__(self): return u"["+u''.join(map(unicode,self))+u"]" From deets at nospam.web.de Sun May 10 05:59:58 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Sun, 10 May 2009 11:59:58 +0200 Subject: unicode bit me In-Reply-To: <05b9f8b9-c8ea-4591-b81e-d34ed2ccea29@s38g2000prg.googlegroups.com> References: <977863c9-c4d9-4563-985a-3849d6ed0d75@d2g2000pra.googlegroups.com> <994147fb-cdf3-4c55-8dc5-62d769b12cdc@u9g2000pre.googlegroups.com> <5e8242d0-1225-41e6-bc40-3016670b8618@x31g2000prc.googlegroups.com> <05b9f8b9-c8ea-4591-b81e-d34ed2ccea29@s38g2000prg.googlegroups.com> Message-ID: <76nmsuF1djnc0U1@mid.uni-berlin.de> anuraguniyal at yahoo.com schrieb: > ok that explains it, > so > unicode(obj) calls __unicode__ on that object and if it isn't there > __repr__ is used > __repr__ of list by default return a str even if __repr__ of element > is unicode > > > so my only solution looks like to use my own list class everywhere i > use list > class mylist(list): > def __unicode__(self): > return u"["+u''.join(map(unicode,self))+u"]" Or you use a custom unicode_list-function whenever you care to print out a list. Diez From Shawn at Milochik.com Sun May 10 07:06:03 2009 From: Shawn at Milochik.com (Shawn Milochik) Date: Sun, 10 May 2009 07:06:03 -0400 Subject: "Beginning Python Visualization" by Shai Vaingast In-Reply-To: References: Message-ID: <2dc0c81b0905100406i2bcc8152i10c5ec8b67933ede@mail.gmail.com> Thanks for the review and the podcast. I ordered the book on Friday. I look forward to playing with it. Also (assuming you're Ron Stephens), thanks for the Python 411 podcast. It's a great resource, and I recommend it to all list members. Shawn From steve at REMOVE-THIS-cybersource.com.au Sun May 10 08:14:35 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 10 May 2009 12:14:35 GMT Subject: Wrapping comments References: Message-ID: <0216b85e$0$20647$c3e8da3@news.astraweb.com> On Sun, 10 May 2009 13:41:15 +1200, Lawrence D'Oliveiro wrote: > If I had to print out all the code and documentation I have to look at, > I'd need to add another room to my house. You are allowed to throw it away when you're done with it :) -- Steven From brian at sweetapp.com Sun May 10 08:16:41 2009 From: brian at sweetapp.com (Brian Quinlan) Date: Sun, 10 May 2009 13:16:41 +0100 Subject: Java-style futures in Python - only better Message-ID: <4A06C5A9.7070102@sweetapp.com> Hey all, I've been working on an Java-style futures implementation in Python. Futures are a way of representing asynchronous operations e.g. operations that are run in another thread or process. The are are a easy but powerful way of parallelizing sequential operations. The also provide a consistent interface across implementations e.g. they can provide the same interface to threading and to multiprocessing. For example: def is_prime(n): "Return True iff n is prime" ... def check_primes(numbers): return map(is_prime, numbers) Could be parallelized as: def check_primes(numbers): # ProcessPoolExecutor will create one worker process # per CPU if called without arguments. Using threads # is valueless because of the GIL. with futures.ProcessPoolExecutor() as executor: return executor.map(is_prime, numbers) A more complex example: def load_url(url, timeout): return urllib.request.urlopen(url, timeout=timeout).read() ### Download the content of some URLs - ignore failures. def download_urls(urls, timeout=60): url_to_content = {} for url in urls: try: url_to_content[url] = load_url(url, timeout=timeout) except: pass return url_to_content Could be parallelized as: # Making this a global variable used any many functions ensures that # the global thread count is kept under control. executor = futures.ThreadPoolExecutor(50) def download_urls(urls, timeout=60): url_to_content = {} # Run load_url for every url and get the result as a FuturesList. fs = executor.run_to_futures( (functools.partial(load_url, url, timeout) for url in urls), timeout=timeout) for url, future in zip(urls, fs.successful_futures()): url_to_content[url] = future.result() return url_to_content The Python 3.0+ code is here: http://code.google.com/p/pythonfutures/source Any feedback on the API would be very much appreciated! Cheers, Brian From aahz at pythoncraft.com Sun May 10 08:27:05 2009 From: aahz at pythoncraft.com (Aahz) Date: 10 May 2009 05:27:05 -0700 Subject: Missing c.l.py posts (was Re: php to python code converter) References: <4A044D71.1000500@gmail.com> <1241812993.9340.3.camel@aalcdl07> Message-ID: In article , MRAB wrote: >J. Cliff Dyer wrote: >> On Fri, 2009-05-08 at 17:19 +0200, Pascal Chambon wrote: >>> >>> PS : Am I the only one having most of answers rejected by the >>> antispam >>> system of python-list ? That's humiliating :p >> >> I've had several messages not make it through. >> >> :( > >Same here ("Message has a suspicious header"). I've been trying to investigate several instances of posts to comp.lang.python.announce getting approved and making it out to the mailing list but not the newsgroup. In each case, reposting as plain text fixed the problem. I suggest that anyone having similar problems on c.l.py do the same thing. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "It is easier to optimize correct code than to correct optimized code." --Bill Harlan From joshua.pearce at gmail.com Sun May 10 08:28:03 2009 From: joshua.pearce at gmail.com (joshua.pearce at gmail.com) Date: Sun, 10 May 2009 05:28:03 -0700 (PDT) Subject: Learning C++ for Python Development Message-ID: <5d97efc2-af41-4dd6-a243-50f85d54746c@s16g2000vbp.googlegroups.com> I am a recovering C# web developer who has recently picked up Django and I'm loving it. I would eventually like to get a job as a Django/Python developer. It seems that many Python jobs require that you also be a C++ developer. While I want to remain primarily a web developer, I don't want be stuck doing CRUD applications, so I would like to learn C++ for Python development. I have taken two basic programming courses in straight up C++, no STL, Boost or anything like that, but I do have a very basic knowledge of the language. Can you folks suggest some Python packages which use C++ and are relevant for web development, so I can dig through the source and contribute in some way? Or, just give me some general advice on learning C++ for Python? P.S. I want to develop on Linux not Windows. Thanks! From aahz at pythoncraft.com Sun May 10 08:28:52 2009 From: aahz at pythoncraft.com (Aahz) Date: 10 May 2009 05:28:52 -0700 Subject: OT (humor): 'import antigravity' in action! References: Message-ID: In article , Shawn Milochik wrote: > >I know you've probably all seen this 50 times, but just in case: >http://xkcd.com/353/ > >And here's the result: >http://icanhascheezburger.com/2009/05/06/funny-pictures-behavior-20/ Thanks! -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "It is easier to optimize correct code than to correct optimized code." --Bill Harlan From aahz at pythoncraft.com Sun May 10 08:42:16 2009 From: aahz at pythoncraft.com (Aahz) Date: 10 May 2009 05:42:16 -0700 Subject: How do I test the integrity of a Python installation in Debian and Ubuntu References: Message-ID: In article , Geoff Gardiner wrote: > >gegard at gegard:~$ python >Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52) >[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2 >Type "help", "copyright", "credits" or "license" for more information. >>>> from test import regrtest >>>> regrtest.main() >test_grammar >test_grammar skipped -- No module named test_grammar >... more of the same... >9 tests skipped: > test_builtin test_doctest test_doctest2 test_exceptions > test_grammar test_opcodes test_operations test_types test_unittest >Traceback (most recent call last): > File "", line 1, in > File "/usr/lib/python2.5/test/regrtest.py", line 416, in main > e = _ExpectedSkips() > File "/usr/lib/python2.5/test/regrtest.py", line 1321, in __init__ > from test import test_socket_ssl >ImportError: cannot import name test_socket_ssl What directory are you running this from? What happens if you switch to running "python Lib/test/regrtest.py"? Taking a closer look, this looks more like a plain import error. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "It is easier to optimize correct code than to correct optimized code." --Bill Harlan From socyl at 987jk.com.invalid Sun May 10 08:52:21 2009 From: socyl at 987jk.com.invalid (kj) Date: Sun, 10 May 2009 12:52:21 +0000 (UTC) Subject: Q's on my first python script Message-ID: Below is my very firs python script. This was just a learning exercise; the script doesn't do anything terribly exciting: for an argument of the form YYMMDD (year, month, day) it prints out the corresponding string YYMMDDW, where W is a one-letter abbreviation for the day of the week. E.g. % wd 090511 090511M The script runs OK, but I can still see a few areas of improvement, for which I could use your advice. 1. The name of the BadArgument exception class defined in the script does not seem to me sufficiently specific. If one were to import the script in order to reuse its wkday_abbrev function, I'd like this exception's name to be more unequivocally tied to this script. What I'm looking for is something like a "namespace" for this script. What's the pythonic way to construct a namespace? 2. In some python modules I've seen the idiom if __name__ == "__main__": # run some tests here I'd like to set up tests for this script, mostly to ensure that it handles the error cases properly, but I'm alread using the idiom above to actually run the script under normal operation. What's the typical python idiom for running tests on a *script* (as opposed to a module that is normally not supposed to be run directly)? 3. Still on the subject of testing, how does one capture in a variable the output that would normally have gone to stdout or stderr? 4. What's the python way to emit warnings? (The script below should warn the user that arguments after the first one are ignored.) 5. The variable wd is meant to be "global" to the script. In other languages I've programmed in I've seen some typographic convention used for the name of such variables (e.g. all caps) to signal this widened scope. Does python have such a convention? Any comments/suggestions on these questions, or anything else about the script, would be much appreciated. TIA! kynn ---------------------------------------------------------------- from optparse import OptionParser import re import datetime import sys class BadArgument(Exception): pass wd = ("M", "T", "W", "H", "F", "S", "U") def wkday_abbrev(str): try: mm = re.match("(\d{2})(\d{2})(\d{2})\Z", str) y, m, d = map(lambda x: int(mm.group(x)), (1,2,3)) if y < 38: y = y + 1900 else: y = y + 2000 return wd[datetime.datetime(y, m, d).weekday()] except (AttributeError, ValueError): raise BadArgument() def main(): usage = '''Usage: %prog [options] YYMMDD %prog -h|--help ''' parser = OptionParser(usage=usage) parser.add_option("-n", "--no-newline", dest="nonl", action="store_true", help="omit newline in output") (options, args) = parser.parse_args(); try: sys.stdout.write("%s%s" % (args[0], wkday_abbrev(args[0]))) if not options.nonl: print except (IndexError, BadArgument): print usage sys.exit(1) except: raise if __name__ == "__main__": main() -- NOTE: In my address everything before the first period is backwards; and the last period, and everything after it, should be discarded. From skip at pobox.com Sun May 10 09:11:30 2009 From: skip at pobox.com (skip at pobox.com) Date: Sun, 10 May 2009 08:11:30 -0500 Subject: Missing c.l.py posts (was Re: php to python code converter) In-Reply-To: References: <4A044D71.1000500@gmail.com> <1241812993.9340.3.camel@aalcdl07> Message-ID: <18950.53890.865512.979083@montanaro.dyndns.org> >>>> PS : Am I the only one having most of answers rejected by the >>>> antispam system of python-list ? That's humiliating :p >>> >>> I've had several messages not make it through. >>> >>> :( >> >> Same here ("Message has a suspicious header"). Aahz> I've been trying to investigate several instances of posts to Aahz> comp.lang.python.announce getting approved and making it out to Aahz> the mailing list but not the newsgroup. In each case, reposting Aahz> as plain text fixed the problem. I suggest that anyone having Aahz> similar problems on c.l.py do the same thing. The problem lies with moi. The ham/spam database had grown rather huge. I tossed out a bunch of older ham and spam messages from the database and retrained. That caused a large fraction of new posts to be held as "unsure". Unfortunately, I didn't notice that for a couple days. I added a couple hundred of the recently held hams and spams to the database and retrained last night. This morning nothing new was held. Hopefully that will solve the problem. -- Skip Montanaro - skip at pobox.com - http://www.smontanaro.net/ America's vaunted "free press" notwithstanding, story ideas that expose the unseemly side of actual or potential advertisers tend to fall by the wayside. Not quite sure why. -- Jim Thornton From ggardiner at iee.org Sun May 10 09:26:40 2009 From: ggardiner at iee.org (Geoff Gardiner) Date: Sun, 10 May 2009 14:26:40 +0100 Subject: How do I test the integrity of a Python installation in Debian and Ubuntu In-Reply-To: References: Message-ID: <4A06D610.5090107@iee.org> Aahz wrote: > .... > What directory are you running this from? What happens if you switch to > running "python Lib/test/regrtest.py"? Taking a closer look, this looks > more like a plain import error. > Thank you for your suggestion. I couldn't do quite that because there's no Lib, but instead (in Ubuntu Hardy, this time): .... gegard at gegard:~$ cd /usr/lib/python2.5/ gegard at gegard:/usr/lib/python2.5$ python test/regrtest.py test_grammar test_grammar skipped -- No module named test_grammar test_opcodes test_opcodes skipped -- No module named test_opcodes test_operations test_operations skipped -- No module named test_operations test_builtin test_builtin skipped -- No module named test_builtin test_exceptions test_exceptions skipped -- No module named test_exceptions test_types test_types skipped -- No module named test_types test_unittest test_unittest skipped -- No module named test_unittest test_doctest test_doctest skipped -- No module named test_doctest test_doctest2 test_doctest2 skipped -- No module named test_doctest2 9 tests skipped: test_builtin test_doctest test_doctest2 test_exceptions test_grammar test_opcodes test_operations test_types test_unittest Traceback (most recent call last): File "test/regrtest.py", line 1384, in main() File "test/regrtest.py", line 416, in main e = _ExpectedSkips() File "test/regrtest.py", line 1321, in __init__ from test import test_socket_ssl ImportError: cannot import name test_socket_ssl gegard at gegard:/usr/lib/python2.5$ Also gegard at gegard:~$ locate */test_socket_ssl.* gegard at gegard:~$ #returns nothing And gegard at gegard:~$ locate /usr/lib/python2.5/test/test_*.* /usr/lib/python2.5/test/test_support.py /usr/lib/python2.5/test/test_support.pyc gegard at gegard:~$ All the best, Geoff From jnoller at gmail.com Sun May 10 09:35:03 2009 From: jnoller at gmail.com (Jesse Noller) Date: Sun, 10 May 2009 09:35:03 -0400 Subject: Get multiprocessing.Queue to do priorities In-Reply-To: <4a05ff89_1@news.arcor-ip.de> References: <4a05b27a_2@news.arcor-ip.de> <4a05b6c6_2@news.arcor-ip.de> <28KdnTocUpw2X5jXnZ2dnUVZ_sudnZ2d@pdx.net> <529683651263584828.031438address_is-invalid.invalid@news.arcor.de> <-vKdnex07oQzQpjXnZ2dnUVZ_hZi4p2d@pdx.net> <4a05ff89_1@news.arcor-ip.de> Message-ID: <4222a8490905100635o7b64bb88n403feae50a320973@mail.gmail.com> On Sat, May 9, 2009 at 6:11 PM, uuid wrote: > The Queue module, apparently, is thread safe, but *not* process safe. If you > try to use an ordinary Queue, it appears inaccessible to the worker process. > (Which, after all, is quite logical, since methods for moving items between > the threads of the same process are quite different from inter-process > communication.) It appears that creating a manager that holds a shared queue > might be an option > (http://stackoverflow.com/questions/342556/python-2-6-multiprocessing-queue-compatible-with-threads). Using a manager, or submitting a patch which adds priority queue to the multiprocessing.queue module is the correct solution for this. You can file an enhancement in the tracker, and assign/add me to it, but without a patch it may take me a bit (wicked busy right now). jesse From tuomas.vesterinen at iki.fi Sun May 10 10:18:30 2009 From: tuomas.vesterinen at iki.fi (Tuomas Vesterinen) Date: Sun, 10 May 2009 17:18:30 +0300 Subject: Problems with datetime.datetime.strptime In-Reply-To: References: <4a05458d$0$24784$9b536df3@news.fv.fi> Message-ID: <4a06e236$0$24790$9b536df3@news.fv.fi> Nick Craig-Wood wrote: > Tuomas Vesterinen wrote: >> I hoped that I could get rid of my special module _strptime2 when >> porting to Python 3.0. But testing is a disappointment. > [snip] >> C : >> strftime('%a %b %e %H:%M:%S %Y')='Sat May 9 11:26:12 2009' >> strptime('Sat May 9 11:26:12 2009','%a %b %e %H:%M:%S %Y')= >> 'e' is a bad directive in format '%a %b %e %H:%M:%S %Y' > [snip] >> What to do. > > I'd start by checking out the code for python 3 trunk and seeing if I > could fix it. If I could I'd update the unit tests and the > documentation then submit the patch to the python bugtracker. > > If I couldn't fix it then I'd report it as a bug. > Thanks. I have a Python implementation here: http://kotisivu.dnainternet.net/tuovest1/booker/booker-0.1.2.tar.gz See folder booker/csvcnt modules _strptime2.py and time_forms.py. TV From paul at boddie.org.uk Sun May 10 10:27:13 2009 From: paul at boddie.org.uk (Paul Boddie) Date: Sun, 10 May 2009 07:27:13 -0700 (PDT) Subject: Nimrod programming language References: <7xd4ah73l3.fsf@ruckus.brouhaha.com> Message-ID: <09709e90-6c58-47f1-94e7-43605875d3ae@l28g2000vba.googlegroups.com> On 10 Mai, 10:40, Paul Rubin wrote: > > Looks nice in many ways. ?You also know about PyPy and Felix > (felix.sf.net)? Try http://felix-lang.org/ for the latter, I believe. Paul From M8R-gqmgb4 at mailinator.com Sun May 10 10:30:53 2009 From: M8R-gqmgb4 at mailinator.com (uuid) Date: Sun, 10 May 2009 16:30:53 +0200 Subject: Get multiprocessing.Queue to do priorities References: <4a05b27a_2@news.arcor-ip.de> <4a05b6c6_2@news.arcor-ip.de> <28KdnTocUpw2X5jXnZ2dnUVZ_sudnZ2d@pdx.net> <529683651263584828.031438address_is-invalid.invalid@news.arcor.de> <-vKdnex07oQzQpjXnZ2dnUVZ_hZi4p2d@pdx.net> <4a05ff89_1@news.arcor-ip.de> Message-ID: <4a06e51d_2@news.arcor-ip.de> Dear Jesse, thanks for the hint. I see you are already assigned to the FIFO bug (http://bugs.python.org/issue4999), so I won't burden you even more. Clearly, a reliable FIFO behavior of multiprocessing.Queue helps more than a priority queue, since it can be used to build one, so that should really be the first thing to fix. In the meantime, I think I'll whip up a hack that uses sort of a bucket- strategy: fill up a prioritized heapq, and then, in regular intervals, unload its contents into a size-limited multiprocessing queue. I'll post this as soon as it works. -u On 2009-05-10 15:35:03 +0200, Jesse Noller said: > Using a manager, or submitting a patch which adds priority queue to > the multiprocessing.queue module is the correct solution for this. > > You can file an enhancement in the tracker, and assign/add me to it, > but without a patch it may take me a bit (wicked busy right now). > > jesse From piet at cs.uu.nl Sun May 10 11:10:50 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sun, 10 May 2009 17:10:50 +0200 Subject: import overwrites __name__ References: Message-ID: >>>>> Peter Otten <__peter__ at web.de> (PO) wrote: >PO> $ cat x.py >PO> import sys >PO> globals().update(zip(*(range(110),)*2)) >PO> y = 42 >PO> print __name__ >PO> if __name__ == "__main__": >PO> a = b = 42 >PO> print len(dir()) >PO> from x import y as z >PO> try: >PO> print my_name >PO> except NameError, e: >PO> print 'unhandled NameError: "%s"' % e >PO> sys.exit() >PO> $ python x.py >PO> __main__ >PO> 119 >PO> x >PO> 117 >PO> unhandled NameError: "name 'my_name' is not defined" This is perfectly normal. python x.py command runs normally (although the globals().update is a very weird thing to do), until the from x import y command. Then x.py is loaded again but now as the module 'x' instead of '__main__'. Python doesn't know it's the same file, and actually it doesn't want to know. It only knows it when you do import twice on the same file. Not when you run it as a main script first and then imports it. **This is a thing you shouldn't do**. There are now two namespaces: one for __main__ and one for x. These are distinct and have no relationship. The reason that the first number is 119 and the second is 117 is that while importing x the variables a and b are not created. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From chiendarret at gmail.com Sun May 10 11:18:51 2009 From: chiendarret at gmail.com (Francesco Pietra) Date: Sun, 10 May 2009 17:18:51 +0200 Subject: strip part of string Message-ID: Hi: I would like to delete everything from column 54 on for each line beginning with "ATOM". In the example in the attachment (sorry for the attachment; I found no way with Google mail to have plain text mail) the new line would become: ATOM 49 NH1 ARG 84 84.628 41.570 44.395 without any blank space to the right. . Everything else should remain at its original column. I have been looking for ???? statement, unable (my big fault ) to find a right one. I tried with slices % but it becomes unnecessarily complex. Thanks francesco -------------- next part -------------- A non-text attachment was scrubbed... Name: strip_string.py Type: text/x-python Size: 403 bytes Desc: not available URL: From google at mrabarnett.plus.com Sun May 10 11:34:42 2009 From: google at mrabarnett.plus.com (MRAB) Date: Sun, 10 May 2009 16:34:42 +0100 Subject: strip part of string In-Reply-To: References: Message-ID: <4A06F412.8050405@mrabarnett.plus.com> Francesco Pietra wrote: > Hi: > I would like to delete everything from column 54 on for each line > beginning with "ATOM". In the example in the attachment (sorry for the > attachment; I found no way with Google mail to have plain text mail) > the new line would become: > > ATOM 49 NH1 ARG 84 84.628 41.570 44.395 > > without any blank space to the right. . Everything else should remain > at its original column. I have been looking for ???? statement, unable > (my big fault ) to find a right one. I tried with slices % but it > becomes unnecessarily complex. > data = open('rec.crg', 'r') outp = open('rec.strip.crg', 'w') for L in data: if L[3] == 'M': L = L[:55].rstrip() + '\n' outp.write(L) data.close() outp.close() Note that .rstrip() will strip all whitespace from the right-hand end of the string, including the '\n' at the end of the string/line, so a new '\n' has to be added. From steve at REMOVE-THIS-cybersource.com.au Sun May 10 11:36:17 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 10 May 2009 15:36:17 GMT Subject: strip part of string References: Message-ID: <0216e7a2$0$20647$c3e8da3@news.astraweb.com> On Sun, 10 May 2009 17:18:51 +0200, Francesco Pietra wrote: > Hi: > I would like to delete everything from column 54 on for each line > beginning with "ATOM". In the example in the attachment (sorry for the > attachment; I found no way with Google mail to have plain text mail) the > new line would become: > > ATOM 49 NH1 ARG 84 84.628 41.570 44.395 if line.startswith("ATOM"): line = line[0:54].rstrip() should do what you want. -- Steven From zooko at zooko.com Sun May 10 11:41:33 2009 From: zooko at zooko.com (Zooko Wilcox-O'Hearn) Date: Sun, 10 May 2009 09:41:33 -0600 Subject: [Python-Dev] .pth files are evil In-Reply-To: <20090509153716.D44633A4080@sparrow.telecommunity.com> References: <49D4DA72.60401@v.loewis.de> <20090407174355.B62983A4063@sparrow.telecommunity.com> <49E4A58F.70309@egenix.com> <20090414162603.70C843A4100@sparrow.telecommunity.com> <49E4F93B.6010802@egenix.com> <20090415003026.B0A783A4114@sparrow.telecommunity.com> <49E59202.6050809@egenix.com> <20090415144147.6845F3A4100@sparrow.telecommunity.com> <49E60832.8030806@egenix.com> <49FB22B5.3040507@simplistix.co.uk> <4A0590C4.1020904@v.loewis.de> <20090509143829.17F293A4080@sparrow.telecommunity.com> <4A059639.7040505@v.loewis.de> <20090509153716.D44633A4080@sparrow.telecommunity.com> Message-ID: <7FF9D9A9-211E-4E5D-BDD0-9C0315123975@zooko.com> On May 9, 2009, at 9:39 AM, P.J. Eby wrote: > It would be really straightforward, though, for someone to > implement an easy_install variant that does this. Just invoke > "easy_install -Zmaxd /some/tmpdir packagelist" to get a full set of > unpacked .egg directories in /some/tmpdir, and then move the > contents of the resulting .egg subdirs to the target location, > renaming EGG-INFO subdirs to projectname-version.egg-info subdirs. Except for the renaming part, this is exactly what GNU stow does. > (Of course, this ignores the issue of uninstalling previous > versions, or overwriting of conflicting files in the target -- does > pip handle these?) GNU stow does handle these issues. Regards, Zooko From sjmachin at lexicon.net Sun May 10 11:52:51 2009 From: sjmachin at lexicon.net (John Machin) Date: Sun, 10 May 2009 08:52:51 -0700 (PDT) Subject: strip part of string References: Message-ID: On May 11, 1:18?am, Francesco Pietra wrote: > Hi: > I would like to delete everything from column 54 on for each line > beginning with "ATOM". In the example in the attachment (sorry for the > attachment; I found no way with Google mail to have plain text mail) Many of your audience won't have seen your quite unnecessary attachment; you could have in-lined the text just like this: [except that I've deleted empty lines, and chopped some irrelevant stuff off the right hand end of some lines so that line-wrapping won't confuse anybody any more than they are already :-) ] 8<--- # Sample line # 1 2 3 4 5 # 012345678901234567890123456789012345678901234567890123456789 # ATOM 49 NH1 ARG 84 84.628 41.570 44.395 0.00 data = open('rec.crg', 'r') outp = open('rec.strip.crg', 'w') for L in data: if L[3] == 'M': L = L[:55] ??????? outp.write(L) 8<--- > the new line would become: > > ATOM ? ? 49 ?NH1 ARG ? ?84 ? ? ?84.628 ?41.570 ?44.395 > > without any blank space to the right. . Everything else should remain > at its original column. I have been looking for ???? statement, unable > (my big fault ) to find a right one. I tried with slices % but it > becomes unnecessarily complex. You wish to retain 54 characters, but are retaining 55. You need a newline on the end of the line. So the statement L = L[:55] should be L = L[:54] + '\n' You may wish to precede that with assert len(L) >= 54 You may wish to change if L[3] == 'M': to if L.startswith('ATOM'): to accord with your requirements statement. HTH, John From Florian.Wollenschein at FernUni-Hagen.de Sun May 10 11:55:20 2009 From: Florian.Wollenschein at FernUni-Hagen.de (Florian Wollenschein) Date: Sun, 10 May 2009 17:55:20 +0200 Subject: Nimrod programming language In-Reply-To: References: Message-ID: Andreas Rumpf wrote: > Dear Python-users, > > I invented a new programming language called "Nimrod" that combines Python's readability with C's performance. Please check it out: http://force7.de/nimrod/ > Any feedback is appreciated. > > Regards, > Andreas Rumpf > > ______________________________________________________ > GRATIS f?r alle WEB.DE-Nutzer: Die maxdome Movie-FLAT! > Jetzt freischalten unter http://movieflat.web.de > Looks really interesting. I think I'll give it a try later on. Thanks for your announcement. Reagrds, Listick http://www.listick.org From gnewsg at gmail.com Sun May 10 11:55:39 2009 From: gnewsg at gmail.com (Giampaolo Rodola') Date: Sun, 10 May 2009 08:55:39 -0700 (PDT) Subject: ANN: Python process utility (psutil) 0.1.2 released References: <65f15b53-aacf-4651-a113-960cce25f205@p4g2000vba.googlegroups.com> Message-ID: On 9 Mag, 11:30, Nick Craig-Wood wrote: > Giampaolo Rodola' wrote: > > ?psutil is a module providing an interface for retrieving information > > ?on running processes and system utilization (CPU, memory) in a > > ?portable way by using Python, implementing many functionalities > > ?offered by tools like ps, top and Windows task manager. > > ?It currently supports Linux, OS X, FreeBSD and Windows. > > Very nice! > > Maybe you should make > > ? Process() > > mean > > ? Process(os.getpid()) > > as I often want ask how much memory/cpu is my process using but rarely > want to ask about other processes? > > It would be nice if "pydoc psutil" returned something useful also! > You could do this by replacing your current __init__.py (which just > contains "from _psutil import *") with _psutil.py > > -- > Nick Craig-Wood --http://www.craig-wood.com/nick Thanks for the hints. I'll do that. --- Giampaolo Rodola' http://code.google.com/p/pyftpdlib http://code.google.com/p/psutil/ From steve at REMOVE-THIS-cybersource.com.au Sun May 10 11:56:00 2009 From: steve at REMOVE-THIS-cybersource.com.au (Steven D'Aprano) Date: 10 May 2009 15:56:00 GMT Subject: Q's on my first python script References: Message-ID: <0216ec41$0$20647$c3e8da3@news.astraweb.com> On Sun, 10 May 2009 12:52:21 +0000, kj wrote: > 1. The name of the BadArgument exception class defined in the script > does not seem to me sufficiently specific. If one were to import the > script in order to reuse its wkday_abbrev function, I'd like this > exception's name to be more unequivocally tied to this script. What > I'm looking for is something like a "namespace" for this script. > What's the pythonic way to construct a namespace? You already have one. The module you have created is a namespace. If your script is called "myscript.py", then to use it elsewhere you would do: import myscript raise myscript.BadArgument > 2. In some python modules I've seen the idiom > > if __name__ == "__main__": > # run some tests here > > I'd like to set up tests for this script, mostly to ensure that it > handles the error cases properly, but I'm alread using the idiom > above to actually run the script under normal operation. What's the > typical python idiom for running tests on a *script* (as opposed to a > module that is normally not supposed to be run directly)? I sometimes give my scripts an option -t or --self-test, and then run tests if that option is passed on the command line. Alternatively, put your tests in another module, say, myscript_tests.py, and then just run that when you want to test myscript. > 3. Still on the subject of testing, how does one capture in a > variable the output that would normally have gone to stdout or > stderr? Normally you would write the function to *return* the result, rather than *print* the result. If all output goes through the function return mechanism, then it's easy to capture: x = func(). However, for cases where the function does print directly, you can redefine stdout and strerr to be any file-like object, so you can do something like this: # untested import sys import cStringIO save_stdout, save_stderr = sys.stdout, sys.stderr c1 = cStringIO.StringIO() c2 = cStringIO.StringIO() try: sys.stdout = c1 sys.stderr = c2 result = func(*args, **kwargs) # normally prints some stuff finally: # restore standard files sys.stdout = save_stdout sys.stderr = save_stderr captured_from_stdout = c1.getvalue() captured_from_stderr = c2.getvalue() > 4. What's the python way to emit warnings? (The script below should > warn the user that arguments after the first one are ignored.) import warnings warnings.warn("The end of the world is coming!") > 5. The variable wd is meant to be "global" to the script. In other > languages I've programmed in I've seen some typographic convention > used for the name of such variables (e.g. all caps) to signal this > widened scope. Does python have such a convention? As a general rule, it's best to avoid globals variables as much as possible. One convention I occasionally use is to prefix global variables with a lowercase g. And then ruthlessly refactor my code until any variable starting with a lowercase g is removed :) -- Steven From sjmachin at lexicon.net Sun May 10 11:59:53 2009 From: sjmachin at lexicon.net (John Machin) Date: Sun, 10 May 2009 08:59:53 -0700 (PDT) Subject: strip part of string References: Message-ID: On May 11, 1:34?am, MRAB wrote: > Francesco Pietra wrote: > > Hi: > > I would like to delete everything from column 54 on for each line > > beginning with "ATOM". In the example in the attachment (sorry for the > > attachment; I found no way with Google mail to have plain text mail) > > the new line would become: > > > ATOM ? ? 49 ?NH1 ARG ? ?84 ? ? ?84.628 ?41.570 ?44.395 > > > without any blank space to the right. . Everything else should remain > > at its original column. I have been looking for ???? statement, unable > > (my big fault ) to find a right one. I tried with slices % but it > > becomes unnecessarily complex. > > data = open('rec.crg', 'r') > outp = open('rec.strip.crg', 'w') > > for L in data: > ? ? if L[3] == 'M': > ? ? ? L = L[:55].rstrip() + '\n' > ? ? outp.write(L) > > data.close() > outp.close() > > Note that .rstrip() will strip all whitespace from the right-hand end of > the string, including the '\n' at the end of the string/line, so a new > '\n' has to be added. His line-terminal '\n' has already disappeared before the rstrip gets a guernsey. His "column 54" is counting from 0, not from 1 -- his [:55] is getting him an extra space which he doesn't want. From anuraguniyal at yahoo.com Sun May 10 12:04:03 2009 From: anuraguniyal at yahoo.com (anuraguniyal at yahoo.com) Date: Sun, 10 May 2009 09:04:03 -0700 (PDT) Subject: unicode bit me References: <977863c9-c4d9-4563-985a-3849d6ed0d75@d2g2000pra.googlegroups.com> <994147fb-cdf3-4c55-8dc5-62d769b12cdc@u9g2000pre.googlegroups.com> <5e8242d0-1225-41e6-bc40-3016670b8618@x31g2000prc.googlegroups.com> <05b9f8b9-c8ea-4591-b81e-d34ed2ccea29@s38g2000prg.googlegroups.com> <76nmsuF1djnc0U1@mid.uni-berlin.de> Message-ID: <3334d202-a5ad-4819-aab6-cb736845b955@y6g2000prf.googlegroups.com> yes but my list sometimes have list of lists On May 10, 2:59?pm, "Diez B. Roggisch" wrote: > anuraguni... at yahoo.com schrieb: > > > ok that explains it, > > so > > unicode(obj) calls __unicode__ on that object and if it isn't there > > __repr__ is used > > __repr__ of list by default return a str even if __repr__ of element > > is unicode > > > so my only solution looks like to use my own list class everywhere i > > use list > > class mylist(list): > > ? ? def __unicode__(self): > > ? ? ? ? return u"["+u''.join(map(unicode,self))+u"]" > > Or you use a custom unicode_list-function whenever you care to print out > ? a list. > > Diez From rustompmody at gmail.com Sun May 10 12:10:11 2009 From: rustompmody at gmail.com (Rustom Mody) Date: Sun, 10 May 2009 21:40:11 +0530 Subject: help with recursive whitespace filter in Message-ID: I am trying to write a recursive filter to remove whitespace-only nodes for minidom. The code is below. Strangely it deletes some whitespace nodes and leaves some. If I keep calling it -- like so: fws(fws(fws(doc))) then at some stage all the ws nodes disappear Does anybody have a clue? from xml.dom.minidom import parse #The input to fws is the output of parse("something.xml") def fws(ele): """ filter white space (recursive)""" for c in ele.childNodes: if isWsNode(c): ele.removeChild(c) #c.unlink() Makes no diff whether this is there or not elif c.nodeType == ele.ELEMENT_NODE: fws(c) def isWsNode(ele): return (ele.nodeType == ele.TEXT_NODE and not ele.data.strip()) From brian at sweetapp.com Sun May 10 12:23:16 2009 From: brian at sweetapp.com (Brian Quinlan) Date: Sun, 10 May 2009 17:23:16 +0100 Subject: Java-style futures in Python - only better In-Reply-To: <4A06FA0B.1060000@ncf.ca> References: <4A06FA0B.1060000@ncf.ca> Message-ID: <4A06FF74.7060405@sweetapp.com> Colin J. Williams wrote: > Brian, > > Since the word "future" is part of the Python lingo: > > A future statement is a directive to the compiler that a particular > module should be compiled using syntax or semantics that will be > available in a specified future release of Python. The future statement > is intended to ease migration to future versions of Python that > introduce incompatible changes to the language. It allows use of the new > features on a per-module basis before the release in which the feature > becomes standard. > > Have you given thought to the use of another word? I named the module "futures" (plural) to try to reduce the potential confusion with the "__futures__" module. The concept of a future is fairly well known in CS [http://en.wikipedia.org/wiki/Future_(programming)] so giving it a completely different name would be a bit annoying. Cheers, Brian > Colin W. > > Brian Quinlan wrote: >> Hey all, >> >> I've been working on an Java-style futures implementation in Python. >> Futures are a way of representing asynchronous operations e.g. >> operations that are run in another thread or process. The are are a >> easy but powerful way of parallelizing sequential operations. The also >> provide a consistent interface across implementations e.g. they can >> provide the same interface to threading and to multiprocessing. >> >> For example: >> >> def is_prime(n): >> "Return True iff n is prime" >> ... >> >> def check_primes(numbers): >> return map(is_prime, numbers) >> >> Could be parallelized as: >> >> def check_primes(numbers): >> # ProcessPoolExecutor will create one worker process >> # per CPU if called without arguments. Using threads >> # is valueless because of the GIL. >> with futures.ProcessPoolExecutor() as executor: >> return executor.map(is_prime, numbers) >> >> A more complex example: >> >> def load_url(url, timeout): >> return urllib.request.urlopen(url, timeout=timeout).read() >> >> ### Download the content of some URLs - ignore failures. >> def download_urls(urls, timeout=60): >> url_to_content = {} >> for url in urls: >> try: >> url_to_content[url] = load_url(url, timeout=timeout) >> except: >> pass >> return url_to_content >> >> Could be parallelized as: >> >> # Making this a global variable used any many functions ensures that >> # the global thread count is kept under control. >> executor = futures.ThreadPoolExecutor(50) >> def download_urls(urls, timeout=60): >> url_to_content = {} >> # Run load_url for every url and get the result as a FuturesList. >> fs = executor.run_to_futures( >> (functools.partial(load_url, url, timeout) for url in urls), >> timeout=timeout) >> for url, future in zip(urls, fs.successful_futures()): >> url_to_content[url] = future.result() >> return url_to_content >> >> >> The Python 3.0+ code is here: >> http://code.google.com/p/pythonfutures/source >> >> Any feedback on the API would be very much appreciated! >> >> Cheers, >> Brian >> >> From showell30 at yahoo.com Sun May 10 12:49:46 2009 From: showell30 at yahoo.com (Steve Howell) Date: Sun, 10 May 2009 09:49:46 -0700 (PDT) Subject: help with recursive whitespace filter in References: Message-ID: <53fc8859-5254-4f49-94f3-c6c430d77371@z5g2000vba.googlegroups.com> On May 10, 9:10?am, Rustom Mody wrote: > I am trying to write a recursive filter to remove whitespace-only > nodes for minidom. > The code is below. > > Strangely it deletes some whitespace nodes and leaves some. > If I keep calling it -- like so: fws(fws(fws(doc))) ?then at some > stage all the ws nodes disappear > > Does anybody have a clue? > > from xml.dom.minidom import parse > > #The input to fws is the output of parse("something.xml") > > def fws(ele): > ? ? """ filter white space (recursive)""" > > ? ?for c in ele.childNodes: > ? ? ? ? if isWsNode(c): > ? ? ? ? ? ? ele.removeChild(c) > ? ? ? ? ? ? #c.unlink() Makes no diff whether this is there or not > ? ? ? ? elif c.nodeType == ele.ELEMENT_NODE: > ? ? ? ? ? ? fws(c) > > def isWsNode(ele): > ? ? return (ele.nodeType == ele.TEXT_NODE and not ele.data.strip()) I would avoid doing things like delete/remove in a loop. Instead build a list of things to delete. From andreengels at gmail.com Sun May 10 12:56:52 2009 From: andreengels at gmail.com (Andre Engels) Date: Sun, 10 May 2009 18:56:52 +0200 Subject: Q's on my first python script In-Reply-To: <0216ec41$0$20647$c3e8da3@news.astraweb.com> References: <0216ec41$0$20647$c3e8da3@news.astraweb.com> Message-ID: <6faf39c90905100956ya8666c6rc9236837db0edf0d@mail.gmail.com> On Sun, May 10, 2009 at 5:56 PM, Steven D'Aprano wrote: >> 5. The variable wd is meant to be "global" to the script. ?In other >> ? ?languages I've programmed in I've seen some typographic convention >> ? ?used for the name of such variables (e.g. all caps) to signal this >> ? ?widened scope. ?Does python have such a convention? > > As a general rule, it's best to avoid globals variables as much as > possible. However, this is not really a global variable, this is a constant, which is not something to be avoided, nor something that there is a convention for (as far as I know). -- Andr? Engels, andreengels at gmail.com From allenreas at gmail.com Sun May 10 13:04:04 2009 From: allenreas at gmail.com (AllenLars) Date: Sun, 10 May 2009 10:04:04 -0700 (PDT) Subject: Downloading most recently modified files In-Reply-To: <2dc0c81b0905071336l365f6b3aq51761873b52e38d0@mail.gmail.com> References: <23432457.post@talk.nabble.com> <2dc0c81b0905071336l365f6b3aq51761873b52e38d0@mail.gmail.com> Message-ID: <23471969.post@talk.nabble.com> Thanks Shawn. I went through the ftplib info and I was able to generate a list. I am needing to figure out parsing the list. AllenLars Shawn Milochik wrote: > > On Thu, May 7, 2009 at 2:19 PM, AllenLars wrote: >> >> I am trying to code a script that will allow me to go to ftp site and >> download files based on most recently modified file (date, time). ?I am >> brand new to programming. ?Any and all help is appreciated. >> -- > > > I've actually written code to do this, and it's fairly easy. Just use > the FTP module to run the "ls" (directory listing) command and parse > the results to get the filenames and timestamps. Then you will be able > to easily do download requests for the file(s) you want. > > Perhaps someone else on the list can chime in with a more elegant > solution. > > Here's enough to get you started: > > from ftplib import FTP #(http://docs.python.org/library/ftplib.html) > > ShawnMilo > -- > http://mail.python.org/mailman/listinfo/python-list > > -- View this message in context: http://www.nabble.com/Downloading-most-recently-modified-files-tp23432457p23471969.html Sent from the Python - python-list mailing list archive at Nabble.com. From Scott.Daniels at Acm.Org Sun May 10 13:06:04 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Sun, 10 May 2009 10:06:04 -0700 Subject: import overwrites __name__ In-Reply-To: References: Message-ID: Piet van Oostrum wrote: >>>>>> Peter Otten <__peter__ at web.de> (PO) wrote: > >> PO> $ cat x.py >> PO> import sys >> PO> globals().update(zip(*(range(110),)*2)) >> PO> y = 42 >> PO> print __name__ >> PO> if __name__ == "__main__": >> PO> a = b = 42 >> PO> print len(dir()) >> PO> from x import y as z >> PO> try: >> PO> print my_name >> PO> except NameError, e: >> PO> print 'unhandled NameError: "%s"' % e >> PO> sys.exit() > >> PO> $ python x.py >> PO> __main__ >> PO> 119 >> PO> x >> PO> 117 >> PO> unhandled NameError: "name 'my_name' is not defined" > > This is perfectly normal. python x.py command runs normally (although > the globals().update is a very weird thing to do), until the from x > import y command. Then x.py is loaded again but now as the module 'x' > instead of '__main__'. Python doesn't know it's the same file, and > actually it doesn't want to know. It only knows it when you do import > twice on the same file. Not when you run it as a main script first and > then imports it. **This is a thing you shouldn't do**. > There are now two namespaces: one for __main__ and one for x. These are > distinct and have no relationship. > > The reason that the first number is 119 and the second is 117 is that > while importing x the variables a and b are not created. After a bit more boiling down: x.py: import sys y = 42 if __name__ == "__main__": a = b = 42 print __name__, 'size', len(dir()) from x import y as z print __name__, 'size', len(dir()), 'completed import of', z try: print my_name except NameError, e: print '%s found unhandled NameError: "%s"' % (__name__, e) sys.exit() produces: __main__ __main__ size 8 x x size 6 x size 7 completed import of 42 x found unhandled NameError: "name 'my_name' is not defined" --Scott David Daniels Scott.Daniels at Acm.Org From rtomek at ceti.com.pl Sun May 10 13:08:47 2009 From: rtomek at ceti.com.pl (Tomasz Rola) Date: Sun, 10 May 2009 19:08:47 +0200 (CEST) Subject: Nimrod programming language In-Reply-To: <159308160@web.de> References: <159308160@web.de> Message-ID: On Fri, 8 May 2009, Andreas Rumpf wrote: > Dear Python-users, > > I invented a new programming language called "Nimrod" that combines > Python's readability with C's performance. Please check it out: > http://force7.de/nimrod/ > Any feedback is appreciated. > > Regards, > Andreas Rumpf Interesting. One question I ask myself upon seeing a new language is if it is possible to program amb (amb=ambiguous) operator in it. This page gives a very nice, "code first" explanation of amb and how it is supposed to work: http://www.randomhacks.net/articles/2005/10/11/amb-operator I am not sure if this kind of extension is doable in your Nimrod. Perhaps it can be somewhat extrapolated from docs, but at the moment I have no time to do this (and could not google anything interesting). As can be seen on the page mentioned, in Ruby this seems to be quite light and effortless. From what I know about Python, it is either hard or impractical (to the point of being impossible) to write amb in it. Two additional notes regarding amb: 1. Even if Nimrod cannot support amb in an elegant way, it is still nice idea. Especially if you can make it to be kind of Python superset, so that Python programmer can cross the boundary between the two without much hassle (and maybe in both directions). Of course, not everything can be copied. 2. The amb itself is not really important so much, and I may never feel any need to actually use it. But it stroke me how nice it was looking in Ruby, even if I finally decided not to learn Ruby (not in this year, at least). In Scheme, it is a bit more hackish, but still looks nice, at least to me: http://www.ccs.neu.edu/home/dorai/t-y-scheme/t-y-scheme-Z-H-16.html#node_chap_14 http://planet.plt-scheme.org/package-source/murphy/amb.plt/1/0/planet-docs/amb/index.html Anyway, I think amb is quite a test of a language. If you can do it, please show the code. Regards, Tomasz Rola -- ** A C programmer asked whether computer had Buddha's nature. ** ** As the answer, master did "rm -rif" on the programmer's home ** ** directory. And then the C programmer became enlightened... ** ** ** ** Tomasz Rola mailto:tomasz_rola at bigfoot.com ** From Scott.Daniels at Acm.Org Sun May 10 13:09:29 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Sun, 10 May 2009 10:09:29 -0700 Subject: Wrapping comments In-Reply-To: <0216b85e$0$20647$c3e8da3@news.astraweb.com> References: <0216b85e$0$20647$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > On Sun, 10 May 2009 13:41:15 +1200, Lawrence D'Oliveiro wrote: >> If I had to print out all the code and documentation I have to look at, >> I'd need to add another room to my house. > > You are allowed to throw it away when you're done with it :) Except here in Portland, where you'd better recycle it. ;) --Scott David Daniels Scott.Daniels at Acm.Org From drobinow at gmail.com Sun May 10 13:16:52 2009 From: drobinow at gmail.com (David Robinow) Date: Sun, 10 May 2009 13:16:52 -0400 Subject: Wrapping comments In-Reply-To: References: Message-ID: <4eb0089f0905101016x13dc34fp2eb0ec2b241f1c80@mail.gmail.com> On Sun, May 10, 2009 at 3:32 AM, Tobias Weber wrote: > In article , > ?Arnaud Delobelle wrote: > >> A simple Alt-Q will reformat everything nicely. > > Now that's something. Thanks! > > (still not gonna use software that doesn't let me type # because it's > alt+3 on a UK layout; having to re-learn or configure that is just sick) > Put the following in your .emacs file: (define-key key-translation-map [?\M-3] "#") or, if you prefer, just be sick. From martin at v.loewis.de Sun May 10 13:18:16 2009 From: martin at v.loewis.de (=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Sun, 10 May 2009 19:18:16 +0200 Subject: [Python-Dev] .pth files are evil In-Reply-To: <7FF9D9A9-211E-4E5D-BDD0-9C0315123975@zooko.com> References: <49D4DA72.60401@v.loewis.de> <20090407174355.B62983A4063@sparrow.telecommunity.com> <49E4A58F.70309@egenix.com> <20090414162603.70C843A4100@sparrow.telecommunity.com> <49E4F93B.6010802@egenix.com> <20090415003026.B0A783A4114@sparrow.telecommunity.com> <49E59202.6050809@egenix.com> <20090415144147.6845F3A4100@sparrow.telecommunity.com> <49E60832.8030806@egenix.com> <49FB22B5.3040507@simplistix.co.uk> <4A0590C4.1020904@v.loewis.de> <20090509143829.17F293A4080@sparrow.telecommunity.com> <4A059639.7040505@v.loewis.de> <20090509153716.D44633A4080@sparrow.telecommunity.com> <7FF9D9A9-211E-4E5D-BDD0-9C0315123975@zooko.com> Message-ID: <4A070C58.4070003@v.loewis.de> > GNU stow does handle these issues. If GNU stow solves all your problems, why do you want to use easy_install in the first place? Regards, Martin From rustompmody at gmail.com Sun May 10 13:23:29 2009 From: rustompmody at gmail.com (rustom) Date: Sun, 10 May 2009 10:23:29 -0700 (PDT) Subject: help with recursive whitespace filter in References: <53fc8859-5254-4f49-94f3-c6c430d77371@z5g2000vba.googlegroups.com> Message-ID: <0291fad2-a744-4ced-bcd4-fb8529e968b8@x29g2000prf.googlegroups.com> On May 10, 9:49?pm, Steve Howell wrote: > On May 10, 9:10?am, Rustom Mody wrote: > > > > > I am trying to write a recursive filter to remove whitespace-only > > nodes for minidom. > > The code is below. > > > Strangely it deletes some whitespace nodes and leaves some. > > If I keep calling it -- like so: fws(fws(fws(doc))) ?then at some > > stage all the ws nodes disappear > > > Does anybody have a clue? > > > from xml.dom.minidom import parse > > > #The input to fws is the output of parse("something.xml") > > > def fws(ele): > > ? ? """ filter white space (recursive)""" > > > ? ?for c in ele.childNodes: > > ? ? ? ? if isWsNode(c): > > ? ? ? ? ? ? ele.removeChild(c) > > ? ? ? ? ? ? #c.unlink() Makes no diff whether this is there or not > > ? ? ? ? elif c.nodeType == ele.ELEMENT_NODE: > > ? ? ? ? ? ? fws(c) > > > def isWsNode(ele): > > ? ? return (ele.nodeType == ele.TEXT_NODE and not ele.data.strip()) > > I would avoid doing things like delete/remove in a loop. ?Instead > build a list of things to delete. Yeah I know. I would write the whole damn thing functionally if I knew how. But cant figure out the API. I actually started out to write a (haskell-style) copy out the whole tree minus the unwanted nodes but could not figure it out From Scott.Daniels at Acm.Org Sun May 10 13:31:54 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Sun, 10 May 2009 10:31:54 -0700 Subject: Q's on my first python script In-Reply-To: <0216ec41$0$20647$c3e8da3@news.astraweb.com> References: <0216ec41$0$20647$c3e8da3@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > On Sun, 10 May 2009 12:52:21 +0000, kj wrote: > .... >> 5. The variable wd is meant to be "global" to the script. In other >> languages I've programmed in I've seen some typographic convention >> used for the name of such variables (e.g. all caps) to signal this >> widened scope. Does python have such a convention? > > As a general rule, it's best to avoid globals variables as much as > possible. > > One convention I occasionally use is to prefix global variables with a > lowercase g. And then ruthlessly refactor my code until any variable > starting with a lowercase g is removed :) You (the OP) don't seem to know about PEP 8, which advises 4-space indents, all-caps constants, and many more style specifics. http://www.python.org/dev/peps/pep-0008/ --Scott David Daniels Scott.Daniels at Acm.Org From google at mrabarnett.plus.com Sun May 10 13:35:59 2009 From: google at mrabarnett.plus.com (MRAB) Date: Sun, 10 May 2009 18:35:59 +0100 Subject: help with recursive whitespace filter in In-Reply-To: <0291fad2-a744-4ced-bcd4-fb8529e968b8@x29g2000prf.googlegroups.com> References: <53fc8859-5254-4f49-94f3-c6c430d77371@z5g2000vba.googlegroups.com> <0291fad2-a744-4ced-bcd4-fb8529e968b8@x29g2000prf.googlegroups.com> Message-ID: <4A07107F.5000703@mrabarnett.plus.com> rustom wrote: > On May 10, 9:49 pm, Steve Howell wrote: >> On May 10, 9:10 am, Rustom Mody wrote: >> >> >> >>> I am trying to write a recursive filter to remove whitespace-only >>> nodes for minidom. >>> The code is below. >>> Strangely it deletes some whitespace nodes and leaves some. >>> If I keep calling it -- like so: fws(fws(fws(doc))) then at some >>> stage all the ws nodes disappear >>> Does anybody have a clue? >>> from xml.dom.minidom import parse >>> #The input to fws is the output of parse("something.xml") >>> def fws(ele): >>> """ filter white space (recursive)""" >>> for c in ele.childNodes: >>> if isWsNode(c): >>> ele.removeChild(c) >>> #c.unlink() Makes no diff whether this is there or not >>> elif c.nodeType == ele.ELEMENT_NODE: >>> fws(c) >>> def isWsNode(ele): >>> return (ele.nodeType == ele.TEXT_NODE and not ele.data.strip()) >> I would avoid doing things like delete/remove in a loop. Instead >> build a list of things to delete. > > Yeah I know. I would write the whole damn thing functionally if I knew > how. But cant figure out the API. > I actually started out to write a (haskell-style) copy out the whole > tree minus the unwanted nodes but could not figure it out > def fws(ele): """ filter white space (recursive)""" empty_nodes = [] for c in ele.childNodes: if isWsNode(c): empty_nodes.append(c) elif c.nodeType == ele.ELEMENT_NODE: fws(c) for c in empty_nodes: ele.removeChild(c) From google at mrabarnett.plus.com Sun May 10 13:37:56 2009 From: google at mrabarnett.plus.com (MRAB) Date: Sun, 10 May 2009 18:37:56 +0100 Subject: Q's on my first python script In-Reply-To: <6faf39c90905100956ya8666c6rc9236837db0edf0d@mail.gmail.com> References: <0216ec41$0$20647$c3e8da3@news.astraweb.com> <6faf39c90905100956ya8666c6rc9236837db0edf0d@mail.gmail.com> Message-ID: <4A0710F4.1070807@mrabarnett.plus.com> Andre Engels wrote: > On Sun, May 10, 2009 at 5:56 PM, Steven D'Aprano > wrote: > >>> 5. The variable wd is meant to be "global" to the script. In other >>> languages I've programmed in I've seen some typographic convention >>> used for the name of such variables (e.g. all caps) to signal this >>> widened scope. Does python have such a convention? >> As a general rule, it's best to avoid globals variables as much as >> possible. > > However, this is not really a global variable, this is a constant, > which is not something to be avoided, nor something that there is a > convention for (as far as I know). > In Python the convention for constants is CAPS_WITH_UNDERSCORES. From Scott.Daniels at Acm.Org Sun May 10 13:53:02 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Sun, 10 May 2009 10:53:02 -0700 Subject: help with recursive whitespace filter in In-Reply-To: References: Message-ID: Rustom Mody wrote: > I am trying to write a recursive filter to remove whitespace-only > nodes for minidom. > The code is below. > > Strangely it deletes some whitespace nodes and leaves some. > If I keep calling it -- like so: fws(fws(fws(doc))) then at some > stage all the ws nodes disappear > > Does anybody have a clue? Yup, don't stand on the limb you are sawing off. > def fws(ele): ... > for c in ele.childNodes: > if isWsNode(c): > ele.removeChild(c) ... At the very least: for c in reversed(ele.childNodes): if isWsNode(c): ele.removeChild(c) --Scott David Daniels Scott.Daniels at Acm.Org From __peter__ at web.de Sun May 10 13:59:18 2009 From: __peter__ at web.de (Peter Otten) Date: Sun, 10 May 2009 19:59:18 +0200 Subject: import overwrites __name__ References: Message-ID: Piet van Oostrum wrote: >>>>>> Peter Otten <__peter__ at web.de> (PO) wrote: > >>PO> $ cat x.py >>PO> import sys >>PO> globals().update(zip(*(range(110),)*2)) >>PO> y = 42 >>PO> print __name__ >>PO> if __name__ == "__main__": >>PO> a = b = 42 >>PO> print len(dir()) >>PO> from x import y as z >>PO> try: >>PO> print my_name >>PO> except NameError, e: >>PO> print 'unhandled NameError: "%s"' % e >>PO> sys.exit() > >>PO> $ python x.py >>PO> __main__ >>PO> 119 >>PO> x >>PO> 117 >>PO> unhandled NameError: "name 'my_name' is not defined" > > This is perfectly normal. I'm not 100% sure of that. Just in case you didn't notice: I'm not the OP. The above piece of junk code was my attempt to keep as close to the code he posted while producing the same output that he got. > python x.py command runs normally (although > the globals().update is a very weird thing to do), until the from x > import y command. Then x.py is loaded again but now as the module 'x' > instead of '__main__'. Python doesn't know it's the same file, and > actually it doesn't want to know. It only knows it when you do import > twice on the same file. Not when you run it as a main script first and > then imports it. **This is a thing you shouldn't do**. > There are now two namespaces: one for __main__ and one for x. These are > distinct and have no relationship. > > The reason that the first number is 119 and the second is 117 is that > while importing x the variables a and b are not created. Your explanation is of course between 119 and 117% correct while I was only 99% serious... Cheers, Peter From zooko at zooko.com Sun May 10 14:04:57 2009 From: zooko at zooko.com (Zooko Wilcox-O'Hearn) Date: Sun, 10 May 2009 12:04:57 -0600 Subject: how GNU stow is complementary rather than alternative to distutils In-Reply-To: <4A070C58.4070003@v.loewis.de> References: <49D4DA72.60401@v.loewis.de> <20090407174355.B62983A4063@sparrow.telecommunity.com> <49E4A58F.70309@egenix.com> <20090414162603.70C843A4100@sparrow.telecommunity.com> <49E4F93B.6010802@egenix.com> <20090415003026.B0A783A4114@sparrow.telecommunity.com> <49E59202.6050809@egenix.com> <20090415144147.6845F3A4100@sparrow.telecommunity.com> <49E60832.8030806@egenix.com> <49FB22B5.3040507@simplistix.co.uk> <4A0590C4.1020904@v.loewis.de> <20090509143829.17F293A4080@sparrow.telecommunity.com> <4A059639.7040505@v.loewis.de> <20090509153716.D44633A4080@sparrow.telecommunity.com> <7FF9D9A9-211E-4E5D-BDD0-9C0315123975@zooko.com> <4A070C58.4070003@v.loewis.de> Message-ID: On May 10, 2009, at 11:18 AM, Martin v. L?wis wrote: > If GNU stow solves all your problems, why do you want to use > easy_install in the first place? That's a good question. The answer is that there are two separate jobs: building executables and putting them in a directory structure of the appropriate shape for your system is one job, and installing or uninstalling that tree into your system is another. GNU stow does only the latter. The input to GNU stow is a set of executables, library files, etc., in a directory tree that is of the right shape for your system. For example, if you are on a Linux system, then your scripts all need to be in $prefix/bin/, your shared libs should be in $prefix/lib, your Python packages ought to be in $prefix/lib/python$x.$y/site- packages/, etc. GNU stow is blissfully ignorant about all issues of building binaries, and choosing where to place files, etc. -- that's the job of the build system of the package, e.g. the "./configure -- prefix=foo && make && make install" for most C packages, or the "python ./setup.py install --prefix=foo" for Python packages using distutils (footnote 1). Once GNU stow has the well-shaped directory which is the output of the build process, then it follows a very dumb, completely reversible (uninstallable) process of symlinking those files into the system directory structure. It is a beautiful, elegant hack because it is sooo dumb. It is also very nice to use the same tool to manage packages written in any programming language, provided only that they can build a directory tree of the right shape and content. However, there are lots of things that it doesn't do, such as automatically acquiring and building dependencies, or producing executables for the target platform for each of your console scripts. Not to mention creating a directory named "$prefx/lib/python $x.$y/site-packages" and cp'ing your Python files into it. That's why you still need a build system even if you use GNU stow for an install-and-uninstall system. The thing that prevents this from working with setuptools is that setuptools creates a file named easy_install.pth during the "python ./ setup.py install --prefix=foo" if you build two different Python packages this way, they will each create an easy_install.pth file, and then when you ask GNU stow to link the two resulting packages into your system, it will say "You are asking me to install two different packages which both claim that they need to write a file named '/usr/local/lib/python2.5/site-packages/easy_install.pth'. I'm too dumb to deal with this conflict, so I give up.". If I understand correctly, your (MvL's) suggestion that easy_install create a .pth file named "easy_install-$PACKAGE-$VERSION.pth" instead of "easy_install.pth" would indeed make it work with GNU stow. Regards, Zooko footnote 1: Aside from the .pth file issue, the other reason that setuptools doesn't work for this use while distutils does is that setuptools tries to hard to save you from making a mistake: maybe you don't know what you are doing if you ask it to install into a previously non-existent prefix dir "foo". This one is easier to fix: http://bugs.python.org/setuptools/issue54 # "be more like distutils with regard to --prefix=" . From kay at fiber-space.de Sun May 10 14:05:49 2009 From: kay at fiber-space.de (kay at fiber-space.de) Date: Sun, 10 May 2009 11:05:49 -0700 (PDT) Subject: php to python code converter References: Message-ID: <1ca12653-4c28-48f1-bf17-def74a89ef15@h23g2000vbc.googlegroups.com> On 8 Mai, 17:19, Pascal Chambon wrote: > Hello > > That's funny, I was precisely thinking about a php to python converter, > some weeks ago. > Such a tool, allowing for example to convert some CMS like Drupal to > python, would be a killer app, when we consider the amount of php code > available. > > But of course, there are lots of issues that'd have to be fixed : > - translating the php syntax to python syntax > - forcing scope limitations where php doesn't have any > - handling differences in semantics (for example, the booleanness of "0" > or) > - handling the automatic variable creation and coertion that php features > - handling the php types like arrays (which are neither python lists nor > python dicts) > - providing a whole mirror of the php stdlib (string and file functions, > access to environment vars...) Some thoughts. 1) Syntax. Not a big deal. 2) Semantics. My favourite approach was to create a Python framework that represents PHP in Python and enables round-trips. So one could translate forth and back. Python code that is compliant to the conventions of the framework can also be translated to PHP and for each PHP program P following equation holds: py2php(php2py(P)) = P This makes readable code mandatory. 3) PHP stdlib via C bindings ( ctypes? ) 4) Corner cases of bindings: cut them off. Not everything has to be translated. But produce stubs that raise NotImplementedError exceptions. Same arguments apply to Javascript. Not sure about Ruby but I do think a parser is feasible despite context sensitivities. Ruby is not my concern though. Personally I'd be interested in Wordpress which I like and use. Kay From davea at ieee.org Sun May 10 14:21:03 2009 From: davea at ieee.org (Dave Angel) Date: Sun, 10 May 2009 14:21:03 -0400 Subject: import overwrites __name__ In-Reply-To: References: Message-ID: <4A071B0F.2020206@ieee.org> Scott David Daniels wrote: >
Piet van > Oostrum wrote: >>>>>>> Peter Otten <__peter__ at web.de> (PO) wrote: >> >>> PO> $ cat x.py >>> PO> import sys >>> PO> globals().update(zip(*(range(110),)*2)) >>> PO> y = 42 >>> PO> print __name__ >>> PO> if __name__ == "__main__": >>> PO> a = b = 42 >>> PO> print len(dir()) >>> PO> from x import y as z >>> PO> try: >>> PO> print my_name >>> PO> except NameError, e: >>> PO> print 'unhandled NameError: "%s"' % e >>> PO> sys.exit() >> >>> PO> $ python x.py >>> PO> __main__ >>> PO> 119 >>> PO> x >>> PO> 117 >>> PO> unhandled NameError: "name 'my_name' is not defined" >> >> This is perfectly normal. python x.py command runs normally (although >> the globals().update is a very weird thing to do), until the from x >> import y command. Then x.py is loaded again but now as the module 'x' >> instead of '__main__'. Python doesn't know it's the same file, and >> actually it doesn't want to know. It only knows it when you do import >> twice on the same file. Not when you run it as a main script first and >> then imports it. **This is a thing you shouldn't do**. >> There are now two namespaces: one for __main__ and one for x. These are >> distinct and have no relationship. >> The reason that the first number is 119 and the second is 117 is that >> while importing x the variables a and b are not created. > After a bit more boiling down: > > x.py: > > import sys > y = 42 > if __name__ == "__main__": > a = b = 42 > print __name__, 'size', len(dir()) > from x import y as z > print __name__, 'size', len(dir()), 'completed import of', z > try: > print my_name > except NameError, e: > print '%s found unhandled NameError: "%s"' % (__name__, e) > sys.exit() > > produces: > __main__ > __main__ size 8 > x > x size 6 > x size 7 completed import of 42 > x found unhandled NameError: "name 'my_name' is not defined" > > --Scott David Daniels > Scott.Daniels at Acm.Org > >
> I already said this before - don't ever import the file you're using as a script. Now that we have real names for these files, I can say it more strongly. If you use "import x" or its variant "from x import..." then *DO NOT* run x.py as your main script. You must have a separate script file that you run, and that nobody tries to import. Now, once you get past that problem, you'll probably have others. Why on earth would you try to import a module from itself? The line: from x import y as z is probably equivalent to z=y From martin at v.loewis.de Sun May 10 14:21:48 2009 From: martin at v.loewis.de (=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=) Date: Sun, 10 May 2009 20:21:48 +0200 Subject: how GNU stow is complementary rather than alternative to distutils In-Reply-To: References: <49D4DA72.60401@v.loewis.de> <20090407174355.B62983A4063@sparrow.telecommunity.com> <49E4A58F.70309@egenix.com> <20090414162603.70C843A4100@sparrow.telecommunity.com> <49E4F93B.6010802@egenix.com> <20090415003026.B0A783A4114@sparrow.telecommunity.com> <49E59202.6050809@egenix.com> <20090415144147.6845F3A4100@sparrow.telecommunity.com> <49E60832.8030806@egenix.com> <49FB22B5.3040507@simplistix.co.uk> <4A0590C4.1020904@v.loewis.de> <20090509143829.17F293A4080@sparrow.telecommunity.com> <4A059639.7040505@v.loewis.de> <20090509153716.D44633A4080@sparrow.telecommunity.com> <7FF9D9A9-211E-4E5D-BDD0-9C0315123975@zooko.com> <4A070C58.4070003@v.loewis.de> Message-ID: <4A071B3C.2060808@v.loewis.de> Zooko Wilcox-O'Hearn wrote: > On May 10, 2009, at 11:18 AM, Martin v. L?wis wrote: > >> If GNU stow solves all your problems, why do you want to use >> easy_install in the first place? > > That's a good question. The answer is that there are two separate jobs: > building executables and putting them in a directory structure of the > appropriate shape for your system is one job, and installing or > uninstalling that tree into your system is another. GNU stow does only > the latter. And so does easy_install - it's job is *not* to build the executables and to put them in a directory structure. Instead, it's distutils/setuptools which has this job. The primary purpose of easy_install is to download the files from PyPI (IIUC). > The thing that prevents this from working with setuptools is that > setuptools creates a file named easy_install.pth It will stop doing that if you ask nicely. That's why I recommended earlier that you do ask it not to edit .pth files. > If I understand correctly, > your (MvL's) suggestion that easy_install create a .pth file named > "easy_install-$PACKAGE-$VERSION.pth" instead of "easy_install.pth" would > indeed make it work with GNU stow. My recommendation is that you use the already existing flag to setup.py install that stops it from editing .pth files. Regards, Martin From zookog at gmail.com Sun May 10 14:21:57 2009 From: zookog at gmail.com (Zooko O'Whielacronx) Date: Sun, 10 May 2009 12:21:57 -0600 Subject: [Python-Dev] how GNU stow is complementary rather than alternative to distutils In-Reply-To: References: <49D4DA72.60401@v.loewis.de> <49FB22B5.3040507@simplistix.co.uk> <4A0590C4.1020904@v.loewis.de> <20090509143829.17F293A4080@sparrow.telecommunity.com> <4A059639.7040505@v.loewis.de> <20090509153716.D44633A4080@sparrow.telecommunity.com> <7FF9D9A9-211E-4E5D-BDD0-9C0315123975@zooko.com> <4A070C58.4070003@v.loewis.de> Message-ID: following-up to my own post to mention one very important reason why anyone cares: On Sun, May 10, 2009 at 12:04 PM, Zooko Wilcox-O'Hearn wrote: > It is a beautiful, elegant hack because it is sooo dumb. ?It is also very > nice to use the same tool to manage packages written in any programming > language, provided only that they can build a directory tree of the right > shape and content. And, you are not relying on the author of the package that you are installing to avoid accidentally or maliciously screwing up your system. You're not even relying on the authors of the *build system* (e.g. the authors of distutils or easy_install). You are relying *only* on GNU stow to avoid accidentally or maliciously screwing up your system, and GNU stow is very dumb, so it is easy to understand what it is going to do and why that isn't going to irreversibly screw up your system. That is: you don't run the "build yourself and install into $prefix" step as root. This is an important consideration for a lot of people, who absolutely refuse on principle to ever run "sudo python ./setup.py" on a system that they care about unless they wrote the "setup.py" script themselves. (Likewise they refuse to run "sudo make install" on packages written in C.) Regards, Zooko From pje at telecommunity.com Sun May 10 14:48:46 2009 From: pje at telecommunity.com (P.J. Eby) Date: Sun, 10 May 2009 14:48:46 -0400 Subject: how GNU stow is complementary rather than alternative to distutils In-Reply-To: References: <49D4DA72.60401@v.loewis.de> <20090407174355.B62983A4063@sparrow.telecommunity.com> <49E4A58F.70309@egenix.com> <20090414162603.70C843A4100@sparrow.telecommunity.com> <49E4F93B.6010802@egenix.com> <20090415003026.B0A783A4114@sparrow.telecommunity.com> <49E59202.6050809@egenix.com> <20090415144147.6845F3A4100@sparrow.telecommunity.com> <49E60832.8030806@egenix.com> <49FB22B5.3040507@simplistix.co.uk> <4A0590C4.1020904@v.loewis.de> <20090509143829.17F293A4080@sparrow.telecommunity.com> <4A059639.7040505@v.loewis.de> <20090509153716.D44633A4080@sparrow.telecommunity.com> <7FF9D9A9-211E-4E5D-BDD0-9C0315123975@zooko.com> <4A070C58.4070003@v.loewis.de> Message-ID: <20090510184609.D238E3A4061@sparrow.telecommunity.com> At 12:04 PM 5/10/2009 -0600, Zooko Wilcox-O'Hearn wrote: >The thing that prevents this from working with setuptools is that >setuptools creates a file named easy_install.pth during the "python >./ setup.py install --prefix=foo" if you build two different Python >packages this way, they will each create an easy_install.pth file, >and then when you ask GNU stow to link the two resulting packages >into your system, it will say "You are asking me to install two >different packages which both claim that they need to write a file >named '/usr/local/lib/python2.5/site-packages/easy_install.pth'. Adding --record and --single-version-externally-managed to that command line will prevent the .pth file from being used or needed, although I believe you already know this. (What that mode won't do is install dependencies automatically.) From piet at cs.uu.nl Sun May 10 14:52:07 2009 From: piet at cs.uu.nl (Piet van Oostrum) Date: Sun, 10 May 2009 20:52:07 +0200 Subject: import overwrites __name__ References: Message-ID: >>>>> Peter Otten <__peter__ at web.de> (PO) wrote: >PO> Piet van Oostrum wrote: >>> >>> This is perfectly normal. >PO> I'm not 100% sure of that. Why not? >PO> Just in case you didn't notice: I'm not the OP. The above piece of junk code >PO> was my attempt to keep as close to the code he posted while producing the >PO> same output that he got. Sorry about that. I was confused because your code was a response tp > You'd have to show us the module. Try to make the smallest module that > has the same effect. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: piet at vanoostrum.org From aahz at pythoncraft.com Sun May 10 15:30:40 2009 From: aahz at pythoncraft.com (Aahz) Date: 10 May 2009 12:30:40 -0700 Subject: FLV download script works, but I want to enhance it References: Message-ID: In article , The Music Guy wrote: >On Thu, May 7, 2009 at 9:29 AM, Aahz wrote: >> >> Here's my download script to get you started figuring this out, it does >> the wget in the background so that several downloads can run in parallel >> from a single terminal window: >> >> #!/bin/bash >> >> echo "Downloading $1" >> wget "$1" > /dev/null 2>&1 & > >Thanks for the reply, but unfortunately that script is going in the >complete wrong direction. Not really; my point was that you could use something similar to process files after downloading. >Firstly, downloading multiple files in tandem does not speed up the >process as it merely cuts up the already limited bandwidth into even >smaller pieces and delays every download in progress. It is much >better to queue downloads to occur one-by-one. > >Secondly, that approach is based on bash rather than Python. I know I >could use the `&` operator on a command line to background processes, >but I would like to be able to have more control through Python and >the use of the subprocess or threading modules. Threading probably won't get you anywhere; I bet processing the files is CPU-intensive. I suggest looking into the multiprocessing module, which would then run the file processing. -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "It is easier to optimize correct code than to correct optimized code." --Bill Harlan From davea at ieee.org Sun May 10 15:33:31 2009 From: davea at ieee.org (Dave Angel) Date: Sun, 10 May 2009 15:33:31 -0400 Subject: Q's on my first python script In-Reply-To: References: Message-ID: <4A072C0B.2@ieee.org> kj wrote: > Below is my very firs python script. > > This was just a learning exercise; the script doesn't do anything > terribly exciting: for an argument of the form YYMMDD (year, month, > day) it prints out the corresponding string YYMMDDW, where W is a > one-letter abbreviation for the day of the week. E.g. > > % wd 090511 > 090511M > > The script runs OK, but I can still see a few areas of improvement, > for which I could use your advice. > > 1. The name of the BadArgument exception class defined in the script > does not seem to me sufficiently specific. If one were to import > the script in order to reuse its wkday_abbrev function, I'd like > this exception's name to be more unequivocally tied to this > script. What I'm looking for is something like a "namespace" > for this script. What's the pythonic way to construct a namespace? > > 2. In some python modules I've seen the idiom > > if __name__ == "__main__": > # run some tests here > > I'd like to set up tests for this script, mostly to ensure that > it handles the error cases properly, but I'm alread using the > idiom above to actually run the script under normal operation. > What's the typical python idiom for running tests on a *script* > (as opposed to a module that is normally not supposed to be run > directly)? > > 3. Still on the subject of testing, how does one capture in a > variable the output that would normally have gone to stdout or > stderr? > > 4. What's the python way to emit warnings? (The script below should > warn the user that arguments after the first one are ignored.) > > 5. The variable wd is meant to be "global" to the script. In other > languages I've programmed in I've seen some typographic convention > used for the name of such variables (e.g. all caps) to signal > this widened scope. Does python have such a convention? > > Any comments/suggestions on these questions, or anything else about > the script, would be much appreciated. > > TIA! > > kynn > > > ---------------------------------------------------------------- > from optparse import OptionParser > import re > import datetime > import sys > > class BadArgument(Exception): pass > > wd = ("M", "T", "W", "H", "F", "S", "U") > > def wkday_abbrev(str): > try: > mm = re.match("(\d{2})(\d{2})(\d{2})\Z", str) > > y, m, d = map(lambda x: int(mm.group(x)), (1,2,3)) > > if y < 38: y = y + 1900 > else: y = y + 2000 > > return wd[datetime.datetime(y, m, d).weekday()] > except (AttributeError, ValueError): > raise BadArgument() > > > def main(): > > usage = '''Usage: %prog [options] YYMMDD > %prog -h|--help > ''' > > parser = OptionParser(usage=usage) > parser.add_option("-n", "--no-newline", dest="nonl", > action="store_true", help="omit newline in output") > > (options, args) = parser.parse_args(); > > try: > sys.stdout.write("%s%s" % (args[0], wkday_abbrev(args[0]))) > if not options.nonl: print > > except (IndexError, BadArgument): > print usage > sys.exit(1) > except: raise > > if __name__ == "__main__": main() > > Other replies cover most of your questions nicely. But for the testing question: Rename this script to some other name, and call it a module Write a new script with just three lines in it: import othermodule if __name__ == "__main__": othermodule.main() Now your testing code can go in "othermodule.py" Note that I would put the argument parsing logic in the new file, so parts of main() would actually move. Normally I'd call main() with two arguments - options, args Your use of optparse could be more streamlined. For example, it'll build the help string for you, based on the various calls to add_option(), and it includes its own -h and --help implicitly. From namekuseijin.nospam at gmail.com Sun May 10 15:40:50 2009 From: namekuseijin.nospam at gmail.com (namekuseijin) Date: Sun, 10 May 2009 16:40:50 -0300 Subject: I'm intrigued that Python has some functional constructions in the language. In-Reply-To: <24381226-a7a0-4fbb-b928-644609e5909b@w31g2000prd.googlegroups.com> References: <44350436-de1a-451e-95f5-13b902a902ce@s21g2000vbb.googlegroups.com> <9e68a155-177d-4e03-97b6-c19cbb986515@z19g2000vbz.googlegroups.com> <7fa66f4e-e88c-4fb4-a509-911756814f89@o27g2000vbd.googlegroups.com> <24381226-a7a0-4fbb-b928-644609e5909b@w31g2000prd.googlegroups.com> Message-ID: Carl Banks wrote: > On May 9, 10:57 am, namekuseijin > wrote: >> Carl Banks wrote: >>> On May 8, 7:19 pm, namekuseijin wrote: >>>> On May 8, 10:13 pm, Carl Banks wrote: >>>> In Haskell, Lisp and other functional programming languages, any extra >>>> syntax gets converted into the core lambda constructs. >>> So? The user still uses that syntax, so how can you claim it doesn't >>> matter? >>>> In Lisp >>>> languages, that syntax is merely user-defined macros, but in Haskell >>>> it's builtin the compiler for convenience. >>> I don't even know what you're saying here >> I'm saying syntax is nothing special. They are user-defined, as >> functions. And it all gets converted into functions. Functions matter, >> syntax is irrelevant because you can do away with it. > > Nope, sorry, you're ignoring half the problem here. Syntax is only > irrelevant if you actually do do away with it. As long as syntax is > there and people use it, then it matters, regardless of whether it all > reduces to function calls. It's handy, yes. It's just nothing special, specially when composing programs out of function composition. > For a very benign example, consider the ways that Python and Haskell > write listcomps: > > [ x for x in ss ] > [ x | x <- ss ] > > One of these might be more readable than the other (I make no > implication which); Haskell uses math notation, which may not be to everyone's tastes as far as IT has largely left its compsci and math roots and every Joe Sixpack might assembly an usable app with a scripting language, some template-writing IDE and a few frameworks. > however, readability has nothing to do with > whether the compiler internally reduces it to a function call or not. > Readibility counts, therefore syntax matters. Function calls are perfectly readable. > Now, maybe readability concerns don't matter to you personally, but it > does matter to the OP, who is trying to advocate functional > programming but is having difficulty because most purely functional > languages have hideous minimalist syntax that turns people off. BTW, rereading it again, the OP was originally questioning about compiler optimizations for purely functional constructs within Python, but somehow syntax got in the middle. I don't see how "syntax would be a lot easier to understand" if it would "be possible to more clearly separate the pure code (without side effects) from the impure code (that deals with state changes, I/O, etc.), so that the pure code could be compiled and have aggressive functional transformations applied to it for efficiency." Syntax would remain the same, I guess. >> In functional programming languages, predefined syntax is mostly >> irrelevant. In Python and other imperative languages, it's absolutely >> necessary. That's my point. > > I think you are overstating this by a lot. Perhaps. From __peter__ at web.de Sun May 10 16:05:49 2009 From: __peter__ at web.de (Peter Otten) Date: Sun, 10 May 2009 22:05:49 +0200 Subject: import overwrites __name__ References: Message-ID: Piet van Oostrum wrote: >>>>>> Peter Otten <__peter__ at web.de> (PO) wrote: > >>PO> Piet van Oostrum wrote: >>>> >>>> This is perfectly normal. > >>PO> I'm not 100% sure of that. > > Why not? What you quoted was not code I would /normally/ write. I was playing with the context and meaning of your sentence. Sorry, this is just a joke that went awry. Peter From socyl at 987jk.com.invalid Sun May 10 16:43:21 2009 From: socyl at 987jk.com.invalid (kj) Date: Sun, 10 May 2009 20:43:21 +0000 (UTC) Subject: Q's on my first python script References: <0216ec41$0$20647$c3e8da3@news.astraweb.com> Message-ID: In <0216ec41$0$20647$c3e8da3 at news.astraweb.com> Steven D'Aprano writes: >On Sun, 10 May 2009 12:52:21 +0000, kj wrote: >> 1. The name of the BadArgument exception class defined in the script >> does not seem to me sufficiently specific. If one were to import the >> script in order to reuse its wkday_abbrev function, I'd like this >> exception's name to be more unequivocally tied to this script. What >> I'm looking for is something like a "namespace" for this script. >> What's the pythonic way to construct a namespace? >You already have one. The module you have created is a namespace. If your >script is called "myscript.py", then to use it elsewhere you would do: >import myscript >raise myscript.BadArgument >> 2. In some python modules I've seen the idiom >> >> if __name__ == "__main__": >> # run some tests here >> >> I'd like to set up tests for this script, mostly to ensure that it >> handles the error cases properly, but I'm alread using the idiom >> above to actually run the script under normal operation. What's the >> typical python idiom for running tests on a *script* (as opposed to a >> module that is normally not supposed to be run directly)? >I sometimes give my scripts an option -t or --self-test, and then run >tests if that option is passed on the command line. >Alternatively, put your tests in another module, say, myscript_tests.py, >and then just run that when you want to test myscript. > >> 3. Still on the subject of testing, how does one capture in a >> variable the output that would normally have gone to stdout or >> stderr? >Normally you would write the function to *return* the result, rather than >*print* the result. If all output goes through the function return >mechanism, then it's easy to capture: x = func(). >However, for cases where the function does print directly, you can >redefine stdout and strerr to be any file-like object, so you can do >something like this: ># untested >import sys >import cStringIO >save_stdout, save_stderr = sys.stdout, sys.stderr >c1 = cStringIO.StringIO() >c2 = cStringIO.StringIO() >try: > sys.stdout = c1 > sys.stderr = c2 > result = func(*args, **kwargs) # normally prints some stuff >finally: > # restore standard files > sys.stdout = save_stdout > sys.stderr = save_stderr >captured_from_stdout = c1.getvalue() >captured_from_stderr = c2.getvalue() > >> 4. What's the python way to emit warnings? (The script below should >> warn the user that arguments after the first one are ignored.) >import warnings >warnings.warn("The end of the world is coming!") >> 5. The variable wd is meant to be "global" to the script. In other >> languages I've programmed in I've seen some typographic convention >> used for the name of such variables (e.g. all caps) to signal this >> widened scope. Does python have such a convention? >As a general rule, it's best to avoid globals variables as much as >possible. >One convention I occasionally use is to prefix global variables with a >lowercase g. And then ruthlessly refactor my code until any variable >starting with a lowercase g is removed :) Thanks! That was very helpful! Kynn -- NOTE: In my address everything before the first period is backwards; and the last period, and everything after it, should be discarded. From socyl at 987jk.com.invalid Sun May 10 16:52:04 2009 From: socyl at 987jk.com.invalid (kj) Date: Sun, 10 May 2009 20:52:04 +0000 (UTC) Subject: Q's on my first python script References: Message-ID: Thank you all very much! I really appreciate it. kynn -- NOTE: In my address everything before the first period is backwards; and the last period, and everything after it, should be discarded. From socyl at 987jk.com.invalid Sun May 10 17:00:23 2009 From: socyl at 987jk.com.invalid (kj) Date: Sun, 10 May 2009 21:00:23 +0000 (UTC) Subject: What's the use of the else in try/except/else? Message-ID: I know about the construct: try: # do something except ...: # handle exception else: # do something else ...but I can't come with an example in which the same couldn't be accomplished with try: # do something # do something else except ...: # handle exception The only significant difference I can come up with is that in the second form, the except clause may be masking some unexpected exceptions from the "do something else" part. Is this the rationale behind this else clause? Or is there something more to it? TIA! kynn -- NOTE: In my address everything before the first period is backwards; and the last period, and everything after it, should be discarded. From Shawn at Milochik.com Sun May 10 17:52:42 2009 From: Shawn at Milochik.com (Shawn Milochik) Date: Sun, 10 May 2009 17:52:42 -0400 Subject: Downloading most recently modified files In-Reply-To: <23471969.post@talk.nabble.com> References: <23432457.post@talk.nabble.com> <2dc0c81b0905071336l365f6b3aq51761873b52e38d0@mail.gmail.com> <23471969.post@talk.nabble.com> Message-ID: <2dc0c81b0905101452h4e5cad78g6d4f677fa116206a@mail.gmail.com> On Sun, May 10, 2009 at 1:04 PM, AllenLars wrote: > > Thanks Shawn. I went through the ftplib info and I was able to generate a > list. I am needing to figure out parsing the list. > > AllenLars Well, start by separating out the date and the file name. You'll want the date for sorting, and the file name for when you decide to download it. If you want help with regular expressions, let me know. If so, post a sample of the code you have so far for parsing, sample input (the raw listing), and sample output showing exactly how you want it to look. As long as you write some code (even if it's not working the way you want), I don't mind helping you fix it up. From pavlovevidence at gmail.com Sun May 10 18:18:37 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Sun, 10 May 2009 15:18:37 -0700 (PDT) Subject: I'm intrigued that Python has some functional constructions in the language. References: <44350436-de1a-451e-95f5-13b902a902ce@s21g2000vbb.googlegroups.com> <9e68a155-177d-4e03-97b6-c19cbb986515@z19g2000vbz.googlegroups.com> <7fa66f4e-e88c-4fb4-a509-911756814f89@o27g2000vbd.googlegroups.com> <24381226-a7a0-4fbb-b928-644609e5909b@w31g2000prd.googlegroups.com> Message-ID: <2abd8518-fbb1-4a84-b84d-4e1e4a74aa3b@s1g2000prd.googlegroups.com> On May 10, 12:40?pm, namekuseijin wrote: > Carl Banks wrote: > > Now, maybe readability concerns don't matter to you personally, but it > > does matter to the OP, who is trying to advocate functional > > programming but is having difficulty because most purely functional > > languages have hideous minimalist syntax that turns people off. > > BTW, rereading it again, the OP was originally questioning about > compiler optimizations for purely functional constructs within Python, > but somehow syntax got in the middle. Syntax--the thing you claim doesn't matter--got in the middle because it was the main factor that drove the OP to look for alternatives to Haskell. That's what makes your entire argument ridiculous; you are making this theoretical argument like, "everything reduces to a function so it doesn't matter what syntax you use," yet people in the real world are out there trying to find alternatives because functional languages' syntax sucks so bad in general. > I don't see how "syntax would be a lot easier to understand" if it would > "be possible to more clearly separate the pure code (without side > effects) from the impure code (that deals with state changes, I/O, > etc.), so that the pure code could be compiled and have aggressive > functional transformations applied to it for efficiency." > > Syntax would remain the same, I guess. You totally missed the point. The thing that would make the syntax a lot easier to understand is that it's in Python and not Haskell. The reason the OP was asking about separating pure code from impure was to see if some subset of Python could be used as a pure functional language, that way they could employ Python and its already-much- better-than-Haskell's syntax as a pedagogical replacement for Haskell. I am sure there are many people who think that even "f a b" is cryptic compared to "f(a,b)", but if that's the only issue it wouldn't be that big of a deal. It's not the only issue. When a language requires you to read and write stuff like "map :: (x -> y) -> f x -> f y" or "f s@ (x:xs) = x:s" then it's going to turn a lot of people off. Carl Banks From Scott.Daniels at Acm.Org Sun May 10 18:52:23 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Sun, 10 May 2009 15:52:23 -0700 Subject: What's the use of the else in try/except/else? In-Reply-To: References: Message-ID: kj wrote: > ... I can't come with an example in which the same couldn't be > accomplished with > > try: > # do something > # do something else > except ...: > # handle exception > > The only significant difference I can come up with is that in the > second form, the except clause may be masking some unexpected > exceptions from the "do something else" part. Is this the rationale > behind this else clause? Or is there something more to it? Yes, in a way. The idea of catching particular exceptions is to only handle exceptions you expect (let the others go out to more general reporters). So, not only should you choose the tightest exception to catch that you can, but you should look for it in a very narrow window: exactly where you expect it. try: v = mumble.field except AttributeError: pass else: sys.warning('field was actually there?') as opposed to: try: v = mumble.field sys.warning('field was actually there?') except AttributeError: pass The idea is to make it clear what you expect might go wrong that you are prepared to handle. --Scott David Daniels Scott.Daniels at Acm.Org From toby at tobiah.org Sun May 10 19:06:34 2009 From: toby at tobiah.org (Tobiah) Date: Sun, 10 May 2009 23:06:34 GMT Subject: There may be a much better way to manage artillery. Message-ID: <_5JNl.4895$qP.3696@newsfe03.iad> I'm writing a video game with armed space ships. I decided to make a class to manage all of the bullets that may be on the screen at a given time: class Bullets(): def __init__(self): self.bullets = [] def update(self): temp = [] for bullet in self.bullets: bullet.update() bullet.time_to_live -= 1 if bullet.time_to_live: temp.append(bullet) self.bullets = temp def add(self, new_bullet): self.bullets.append(new_bullet) When the main loop calls .update() on the bullets object, I want it to decrement a counter on each bullet, and destroy the bullets who's time has come. I wanted the bullets to be responsible for destroying themselves, but a little Googling brought me to points about dangling references and how an object is not allowed (nor does it seem to have the means) to destroy itself. That's why I made this wrapper class for all of the bullets. The idea is to copy bullets with time left into a temp list and then overwrite the man bullets array with the good bullets. I believe that the other bullets will be garbage collected. I could not delete the items in place within the loop, of course. Was there a better container than a list for my purposes? Above is what I settled on, but I wonder whether my approach is a good one. Thanks very much, Toby From rhodri at wildebst.demon.co.uk Sun May 10 19:10:14 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Mon, 11 May 2009 00:10:14 +0100 Subject: ISO exemplary Python scripts In-Reply-To: References: <802a051qoqmpnih0j2su9hhriqumagu326@4ax.com> Message-ID: On Sat, 09 May 2009 17:09:38 +0100, kj wrote: > I know that in the python world the distinction between a script > and a (library) module is not so clear-cut, and it is common for > library modules to have "top-level" stuff (typically test code) > that gets run if the module is invoked directly from the command > line. But this is not *really* a script as I understand it, because, > even though it "runs" directly from the command-line, it lacks the > typical CLI amenities, such as command-line flags, help messages, > diagnostic messages that are aimed to the "naive user" (i.e. as > opposed to the developer), etc. The coding of these "CLI amenities" > is one of aspects of these "exemplary Python scripts" I'm most > interested in learning about. While I don't entirely agree with your definition of a script, for a lot of those CLI amenities "optparse" is your friend. Possibly also your enemy if you don't want to do things its way, but it does at least make parameter handling in a user-friendly way easy. -- Rhodri James *-* Wildebeeste Herder to the Masses From Graham.Dumpleton at gmail.com Sun May 10 19:11:09 2009 From: Graham.Dumpleton at gmail.com (Graham Dumpleton) Date: Sun, 10 May 2009 16:11:09 -0700 (PDT) Subject: mod_python and xml.dom.minidom References: <83b63d98-9871-47eb-8846-46404b70896e@z7g2000vbh.googlegroups.com> <12ef7b00-f0c5-4030-a353-533d93d23cdd@r3g2000vbp.googlegroups.com> Message-ID: <794f117f-4df5-4c6c-be68-f901959b2579@y10g2000prc.googlegroups.com> On May 10, 3:40?am, Paul Boddie wrote: > On 9 Mai, 01:36, dpapathanasiou wrote: > > > > > Apache's configure utility (I'm using httpd version 2.2.11) doesn't > > explicitly describe an expat library option. > > > Also, if libexpat is version 1.95.2, wouldn't I have to get version > > 2.0 to be compatible with pyexpat? > > The aim would be to persuade Apache to configure itself against the > same Expat library that pyexpat is using, which would involve the > headers and libraries referenced during the pyexpat configuration > process, although I seem to recall something about pyexpat bundling > its own version of Expat - that would complicate matters somewhat. > > > If anyone has any advice or suggestions, I'd appreciate hearing them. > > Expat might be getting brought into Apache via mod_dav: > > http://www.webdav.org/mod_dav/install.html > > Perhaps disabling mod_dav when configuring Apache might drop Expat > from Apache's library dependencies. The OP was using Python 2.5, so shouldn't be an issue because pyexpat properly name space prefixes its version of expat. See: http://code.google.com/p/modwsgi/wiki/IssuesWithExpatLibrary where explicitly says that only applies to Python prior to Python 2.5. His problem is therefore likely to be something completely different. Graham From rhodri at wildebst.demon.co.uk Sun May 10 19:48:25 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Mon, 11 May 2009 00:48:25 +0100 Subject: There may be a much better way to manage artillery. In-Reply-To: <_5JNl.4895$qP.3696@newsfe03.iad> References: <_5JNl.4895$qP.3696@newsfe03.iad> Message-ID: On Mon, 11 May 2009 00:06:34 +0100, Tobiah wrote: [Snippety snip] > I wanted the bullets to be responsible for destroying > themselves, but a little Googling brought me to points > about dangling references and how an object is not allowed > (nor does it seem to have the means) to destroy itself. > That's why I made this wrapper class for all of the bullets. An object can, however, erase its representation from the screen and tell whatever control system is keeping track of objects to forget about it. Once the last reference to the object is deleted, the object will be garbage collected. What framework are you writing your game in? Pygame has facilities for handling this sort of thing fairly straightforwardly. -- Rhodri James *-* Wildebeeste Herder to the Masses From rhodri at wildebst.demon.co.uk Sun May 10 20:12:04 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Mon, 11 May 2009 01:12:04 +0100 Subject: Wrapping comments In-Reply-To: References: Message-ID: On Sun, 10 May 2009 08:32:23 +0100, Tobias Weber wrote: > In article , > Arnaud Delobelle wrote: > >> A simple Alt-Q will reformat everything nicely. > > Now that's something. Thanks! > > (still not gonna use software that doesn't let me type # because it's > alt+3 on a UK layout; having to re-learn or configure that is just sick) What on earth are you talking about? '#' has its own key on a UK layout (shared with '~', but you know what I mean), just to the left of the RETURN key. Emacs is my editor of choice, and I've never once come across anything like this. -- Rhodri James *-* Wildebeeste Herder to the Masses From martin.hellwig at dcuktec.org Sun May 10 20:12:26 2009 From: martin.hellwig at dcuktec.org (Martin P. Hellwig) Date: Mon, 11 May 2009 01:12:26 +0100 Subject: SimpleXMLRPCServer and creating a new object on for each new client request. In-Reply-To: References: <09a3e655-06ad-46cb-9955-303948f2b271@e24g2000vbe.googlegroups.com> Message-ID: <5LSdnUoJjZr28JrXnZ2dnUVZ8o2dnZ2d@bt.com> Piet van Oostrum wrote: >>>>>> google at smetj.net (g) wrote: > >> g> Well, I think Martin's example will suit my needs. > >> g> Thanks for the explanation! > > His client code is unnecessarily complicated with 3 session variables. > The following code does the same: > > SESSION = xmlrpclib.ServerProxy(URL_PORT) > print(SESSION.show_random()) > print(SESSION.show_random()) > SESSION.shutdown_service() > I was actually aware of that, but I thought it would be more educational if I rewrote Jelle's example by using code conformity and a way to test server and client in a single script instead of just rewriting it to the given intent. With the knowledge provided, Jelle should be able to redesign his program to what he actually wants to do with it and cutting out the croft himself. P.S. Piet ben je toevallig lid van NLUUG? -- MPH http://blog.dcuktec.com 'If consumed, best digested with added seasoning to own preference.' From showell30 at yahoo.com Sun May 10 20:17:23 2009 From: showell30 at yahoo.com (Steve Howell) Date: Sun, 10 May 2009 17:17:23 -0700 (PDT) Subject: help with recursive whitespace filter in References: <53fc8859-5254-4f49-94f3-c6c430d77371@z5g2000vba.googlegroups.com> <0291fad2-a744-4ced-bcd4-fb8529e968b8@x29g2000prf.googlegroups.com> Message-ID: <3c107c60-09a1-49b7-9b1d-32d32dce4f7d@x6g2000vbg.googlegroups.com> On May 10, 10:23?am, rustom wrote: > On May 10, 9:49?pm, Steve Howell wrote: > > > > > On May 10, 9:10?am, Rustom Mody wrote: > > > > I am trying to write a recursive filter to remove whitespace-only > > > nodes for minidom. > > > The code is below. > > > > Strangely it deletes some whitespace nodes and leaves some. > > > If I keep calling it -- like so: fws(fws(fws(doc))) ?then at some > > > stage all the ws nodes disappear > > > > Does anybody have a clue? > > > > from xml.dom.minidom import parse > > > > #The input to fws is the output of parse("something.xml") > > > > def fws(ele): > > > ? ? """ filter white space (recursive)""" > > > > ? ?for c in ele.childNodes: > > > ? ? ? ? if isWsNode(c): > > > ? ? ? ? ? ? ele.removeChild(c) > > > ? ? ? ? ? ? #c.unlink() Makes no diff whether this is there or not > > > ? ? ? ? elif c.nodeType == ele.ELEMENT_NODE: > > > ? ? ? ? ? ? fws(c) > > > > def isWsNode(ele): > > > ? ? return (ele.nodeType == ele.TEXT_NODE and not ele.data.strip()) > > > I would avoid doing things like delete/remove in a loop. ?Instead > > build a list of things to delete. > > Yeah I know. I would write the whole damn thing functionally if I knew > how. ?But cant figure out the API. > I actually started out to write a (haskell-style) copy out the whole > tree minus the unwanted nodes but could not figure it out You can use list comprehensions for a more functional style. Instead of deleting the unwanted nodes in place, try to create new lists of just the wanted results. From apt.shansen at gmail.com Sun May 10 20:21:56 2009 From: apt.shansen at gmail.com (Stephen Hansen) Date: Sun, 10 May 2009 17:21:56 -0700 Subject: Pythonic way to normalize vertical whitespace In-Reply-To: <1241798017.7066.1314483739@webmail.messagingengine.com> References: <1241798017.7066.1314483739@webmail.messagingengine.com> Message-ID: <7a9c25c20905101721g4953dd53i826e9774c8c68092@mail.gmail.com> On Fri, May 8, 2009 at 8:53 AM, wrote: > I'm looking for suggestions on technique (not necessarily code) about the > most pythonic way to normalize vertical whitespace in blocks of text so that > there is never more than 1 blank line between paragraphs. Our source text > has newlines normalized to single newlines (\n vs. combinations of \r and > \n), but there may be leading and trailing whitespace around each newline. > I'm not sure what's more Pythonic in this case, but the approach I'd use is a generator. Like so: --- code to follow -- def cleaner(body): empty = False for line in body.splitlines(): line = line.rstrip() if line: empty = False yield line else: if empty: continue else: empty = True yield '' Text = """ This is my first paragraph. This is my second. This is my third. This is my fourth. This is my fifth. This is my sixth. This is my seventh. Okay? """ print '\n'.join(cleaner(Text)) --- end code --- You maybe want to tweak the logic a bit depending on how you want to handle initial blank lines or such, and I'm two paragraphs w/o a blank line between them (you talk some of going from \n\n\n to \n\n, so I'm not sure if you want to allow blank lines separating the paragraphs or require it). I guess this basically is your first approach, just specifically going with "Use a generator to do it" instead of regular list building cuz that should be faster and cleaner int his case.... and I don't think the third approach is really all that needed. Python can do this pretty quickly. The second approach seems distinctly unPythonic. I use regular expressions in several places, mind, but if there's a straight-forward clean way to do anything without them that always seems like the Pythonic thing to me. :) But that's just me :) --S -------------- next part -------------- An HTML attachment was scrubbed... URL: From gagsl-py2 at yahoo.com.ar Sun May 10 20:44:59 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Sun, 10 May 2009 21:44:59 -0300 Subject: How can I dynamically insert a base class in a given class References: Message-ID: En Sun, 10 May 2009 00:04:17 -0300, ?????? ??????????? escribi?: > How can I dynamically insert a base class in a given class? Yeah, I'm > writing a class decorator that needs to manipulate the class by > inserting another base class in it. In this case, as you want to modify the base classes, I think a metaclass is more apropiate (the decorator acts too late, *after* the class has already been created). Based on your code: class ReallyBase(object): def someimportantmethod(self): return 'really really' def dynamically_determine_metaclass(args): class MyMetaclass(type): def __new__(meta, name, bases, namespace): bases = (ReallyBase,) + bases namespace['some_value'] = args return type.__new__(meta, name, bases, namespace) return MyMetaclass class Unsuspecting(object): __metaclass__ = dynamically_determine_metaclass(123) def stuff(self): return "ok" u = Unsuspecting() print u.stuff(), u.someimportantmethod(), u.some_value, Unsuspecting.some_value -- Gabriel Genellina From draeath.spamtrap at gmail.com Sun May 10 22:25:56 2009 From: draeath.spamtrap at gmail.com (draeath) Date: Sun, 10 May 2009 22:25:56 -0400 Subject: There may be a much better way to manage artillery. References: <_5JNl.4895$qP.3696@newsfe03.iad> Message-ID: <_rKdnU2errA3EZrXnZ2dnUVZ_gFi4p2d@giganews.com> Tobiah wrote: > I'm writing a video game with armed space ships. > I decided to make a class to manage all of the bullets > that may be on the screen at a given time: > > class Bullets(): > > def __init__(self): > self.bullets = [] > > def update(self): > temp = [] > for bullet in self.bullets: > bullet.update() > bullet.time_to_live -= 1 > if bullet.time_to_live: > temp.append(bullet) > self.bullets = temp > > def add(self, new_bullet): > self.bullets.append(new_bullet) > > When the main loop calls .update() on the bullets > object, I want it to decrement a counter on each > bullet, and destroy the bullets who's time has come. > > I wanted the bullets to be responsible for destroying > themselves, but a little Googling brought me to points > about dangling references and how an object is not allowed > (nor does it seem to have the means) to destroy itself. > That's why I made this wrapper class for all of the bullets. > > The idea is to copy bullets with time left into a temp list > and then overwrite the man bullets array with the good bullets. > I believe that the other bullets will be garbage collected. > I could not delete the items in place within the loop, of course. > Was there a better container than a list for my purposes? > > Above is what I settled on, but I wonder > whether my approach is a good one. > > Thanks very much, > > Toby This also gives you the means to destroy the oldest bullet when creating a new one, once some arbitrary limit was reached. Think back to how old games like Raptor worked. If you had the bullets take care of their lifetime themselves, you would have to have more code touching all the bullets to do this. Now, you can add a little bit while you are already working through them all, saving yourself some cycles I would wager. From http Sun May 10 22:33:04 2009 From: http (Paul Rubin) Date: 10 May 2009 19:33:04 -0700 Subject: I'm intrigued that Python has some functional constructions in the language. References: <44350436-de1a-451e-95f5-13b902a902ce@s21g2000vbb.googlegroups.com> <9e68a155-177d-4e03-97b6-c19cbb986515@z19g2000vbz.googlegroups.com> <7fa66f4e-e88c-4fb4-a509-911756814f89@o27g2000vbd.googlegroups.com> <24381226-a7a0-4fbb-b928-644609e5909b@w31g2000prd.googlegroups.com> <2abd8518-fbb1-4a84-b84d-4e1e4a74aa3b@s1g2000prd.googlegroups.com> Message-ID: <7xws8oid27.fsf@ruckus.brouhaha.com> Carl Banks writes: > Syntax--the thing you claim doesn't matter--got in the middle because > it was the main factor that drove the OP to look for alternatives to > Haskell. I don't think so. The OP said that "... the syntax would be a lot easier to understand, than most functional languages, like Haskell". He didn't say that Haskell syntax caused him to look for alternatives and didn't say that Haskell wouldn't still be difficult even if its syntax were simpler. Hint: Liskell (Haskell with S-expression syntax) is still difficult. > yet people in the real world are out there trying to find > alternatives because functional languages' syntax sucks so bad in general. Again, I don't think so. Functional languages' syntax turns some people off before they even look at other aspects closely, just like Python's indentation-based syntax turns some people off. But, once you get past the syntax, functional languages STILL present a lot of obstacles that lots of users never get past. > The reason the OP was asking about separating pure code from impure > was to see if some subset of Python could be used as a pure functional > language, that way they could employ Python and its already-much- > better-than-Haskell's syntax as a pedagogical replacement for Haskell. That wouldn't make sense at all. 60% of what makes Haskell different (and harder) than Python is probably the complicated (but powerful) static type system; 35% is the nonstrict evaluation strategy, and 5% the syntax. If you somehow added a method in Python to separate pure from impure code, but it was still a dynamic language with strict evaluation, you'd only be 5% of the way to replacing Haskell. From prakash.stack at gmail.com Sun May 10 23:06:20 2009 From: prakash.stack at gmail.com (prakash jp) Date: Mon, 11 May 2009 08:36:20 +0530 Subject: stand alone exec Message-ID: <805f59d50905102006obd4e42bo6b8923b62f84f6d6@mail.gmail.com> Hi all, I want to run dos commands through python stand alone execs. The created Python stand alone executable (py2exe) works fine in my machine but on transferring the "dist" folder to other systems the executable fails to run. I tried to copy the MSVCP90.dll in the "dist" folder. Also tried to exclude the same dll in the options of the setup.py file The error reads as follows : "The application has failed to start because the application configuration is incorrect. Reinstalling the application may fix this problem". Details of the installed setup files may be useful : 1- python-2.6.1.msi 2- py2exe-0.6.9.win32-py2.6.exe 3- pywin32-212.win32-py2.6.exe Thanks in advance Regards Prakash -------------- next part -------------- An HTML attachment was scrubbed... URL: From viniciusban at gmail.com Sun May 10 23:30:16 2009 From: viniciusban at gmail.com (Vinicius Assef) Date: Mon, 11 May 2009 00:30:16 -0300 Subject: Web Based Front End? In-Reply-To: <18804639-32ae-4bdf-9ecc-4c8b21f8a43f@g19g2000vbi.googlegroups.com> References: <18804639-32ae-4bdf-9ecc-4c8b21f8a43f@g19g2000vbi.googlegroups.com> Message-ID: On Thu, May 7, 2009 at 10:52 PM, wrote: [snip] > > So, my question is what is the best method to be able to have a user > enter specific data on a web page, have that data be passed to the > Python script and then have the output returned to the web page? Django is the buzzword, among python community here, in Brazil. In fact it is really powerful. Give it a try. ;-) -- [ ]s Vinicius Assef. http://aprenda-python.blogspot.com From kalman_g at msn.com Sun May 10 23:57:05 2009 From: kalman_g at msn.com (GKalman) Date: Sun, 10 May 2009 20:57:05 -0700 (PDT) Subject: Importing from a module which contains more than one Class... Message-ID: <23476815.post@talk.nabble.com> OS= MS Vista File structure: .../Module (i.e a Folder with 2 sub-folders) .../Module_Class (sub-folder #1) /MyClass.py .../Module_Class_Testing (sub_folder #2) /module_class_driver.py Here is the code for the two (simplified) Python files: #this is module_class_driver.py #====================== import sys dir1='C:\\Programming\\Python\\Prototypes\\Core_Python_Prototypes\\Module\\Module_Class' sys.path.append(dir1) from MyClass import * from MyOtherClass import * # error msg: no such module! c=MyClass(2.5) print c.double_it() cc=MyOtherClass(5.0) #error print cc.triple_it() #error #this is MyClass.py #============ class MyClass: def __init__(self,x): self.x=x def double_it(self): return 2*self.x class MyOtherClass: def __init__(self,y): self.y=y def triple_it(self): return 3*self.y Question: ******* As I mentioned above, the code for MyClass & MyOtherClass is in the same file . This program only works with a single Class in a file. That is when the File name is the SAME as the Class name. How to import from a File which contains more than one (unrelated) Classes? -- View this message in context: http://www.nabble.com/Importing-from-a-module-which-contains-more-than-one-Class...-tp23476815p23476815.html Sent from the Python - python-list mailing list archive at Nabble.com. From wuwei23 at gmail.com Mon May 11 00:19:52 2009 From: wuwei23 at gmail.com (alex23) Date: Sun, 10 May 2009 21:19:52 -0700 (PDT) Subject: Importing from a module which contains more than one Class... References: Message-ID: GKalman wrote: > from MyClass import * > from MyOtherClass import * ? ? # error msg: no such module! > > As I mentioned above, ?the code for MyClass & MyOtherClass is in the same > file . This program only works with a single Class in a file. That is when > the File name is the SAME as the Class name. > > How to import from a File which contains more than one (unrelated) Classes? You seem to have misunderstood how 'import' works, I strongly recommend re-reading the tutorial section: http://docs.python.org/tutorial/modules.html Basically, the import statement has two forms: 1. from import 2. import So your example of 'from import *' just doesn't make a lot of sense within Python. If MyClass.py contains two classes, MyClass and MyOtherClass, you should be able to import them with: from MyClass import MyClass, MyOtherClass However, you _should_ be already getting _everything_ MyClass.py contains as you're using the grab-all asterisk. Could you open a python shell in your Module_Class folder, type 'from MyClass import MyClass, MyOtherClass', and past whatever traceback you get here? From timr at probo.com Mon May 11 00:34:26 2009 From: timr at probo.com (Tim Roberts) Date: Sun, 10 May 2009 21:34:26 -0700 Subject: ISO exemplary Python scripts References: <802a051qoqmpnih0j2su9hhriqumagu326@4ax.com> Message-ID: <6eaf05d1tmhmhrb7tvd1rsojhco22utaq4@4ax.com> kj wrote: > >Thanks, but the last bit of your post ("...most of which have the >ability to run by themselves") makes me wonder whether we mean the >same thing when we talk of "scripts." Can you give me an example >of a script that *does not* have the ability to run by itself? >When I use the word "script" I mean, *by definition*, a piece of >code that has the ability to run by itself. > >I know that in the python world the distinction between a script >and a (library) module is not so clear-cut, Exactly. This is true for many of the languages that are traditionally interpreted. Things start off as standalone applications and morph into general purpose modules, or vice versa. As a result, I lazily call any Python source file a "script". >But this is not *really* a script as I understand it, because, >even though it "runs" directly from the command-line, it lacks the >typical CLI amenities, such as command-line flags, help messages, >diagnostic messages that are aimed to the "naive user" (i.e. as >opposed to the developer), etc. The coding of these "CLI amenities" >is one of aspects of these "exemplary Python scripts" I'm most >interested in learning about. In my vocabulary, I'd probably call this an "application". I guess my basic point is that the standard library contains good examples of Python programming, and the "Python way of thinking". Command-line argument processing is not a particularly unique task, so the same techniques that work for parsing things from files, or for handling arguments in a list, work equally well for handling arguments, especially with the help of getopt and optparse. -- Tim Roberts, timr at probo.com Providenza & Boekelheide, Inc. From tjreedy at udel.edu Mon May 11 01:47:06 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 11 May 2009 01:47:06 -0400 Subject: unicode bit me In-Reply-To: <05b9f8b9-c8ea-4591-b81e-d34ed2ccea29@s38g2000prg.googlegroups.com> References: <977863c9-c4d9-4563-985a-3849d6ed0d75@d2g2000pra.googlegroups.com> <994147fb-cdf3-4c55-8dc5-62d769b12cdc@u9g2000pre.googlegroups.com> <5e8242d0-1225-41e6-bc40-3016670b8618@x31g2000prc.googlegroups.com> <05b9f8b9-c8ea-4591-b81e-d34ed2ccea29@s38g2000prg.googlegroups.com> Message-ID: anuraguniyal at yahoo.com wrote: > so unicode(obj) calls __unicode__ on that object It will look for the existence of type(ob).__unicode__ ... > and if it isn't there __repr__ is used According to the below, type(ob).__str__ is tried first. > __repr__ of list by default return a str even if __repr__ of element > is unicode From the fine library manual, built-in functions section: (I reccommend using it, along with interactive experiments.) "repr( object) Return a string ..." "str( [object]) Return a string ..." "unicode( [object[, encoding [, errors]]]) Return the Unicode string version of object using one of the following modes: If encoding and/or errors are given, ... If no optional parameters are given, unicode() will mimic the behaviour of str() except that it returns Unicode strings instead of 8-bit strings. More precisely, if object is a Unicode string or subclass it will return that Unicode string without any additional decoding applied. For objects which provide a __unicode__() method, it will call this method without arguments to create a Unicode string. For all other objects, the 8-bit string version or representation is requested and then converted to a Unicode string using the codec for the default encoding in 'strict' mode. " 'unicode(somelist)' has no optional parameters, so skip to third paragraph. Somelist is not a unicode instance, so skip to the last paragraph. If you do dir(list) I presume you will *not* see '__unicode__' listed. So skip to the last sentence. unicode(somelist) == str(somelist).decode(default,'strict'). I do not believe str() and repr() are specifically documented for builtin classes other than the general description, but you can figure that str(collection) or repr(collection) will call str or repr on the members of the collection in order to return a str, as the doc says. (Details are available by experiment.) Str(uni_string) encodes with the default encoding, which seems to be 'ascii' in 2.x. I am sure it uses 'strict' errors. I would agree that str(some_unicode) could be better documented, like unicode(some_str) is. > so my only solution looks like to use my own list class everywhere i > use list > class mylist(list): > def __unicode__(self): > return u"["+u''.join(map(unicode,self))+u"]" Or write a function and use that instead, or, if and when you can, switch to 3.x where str and repr accept and produce unicode. tjr From rogerb at rogerbinns.com Mon May 11 02:55:52 2009 From: rogerb at rogerbinns.com (Roger Binns) Date: Sun, 10 May 2009 23:55:52 -0700 Subject: Learning C++ for Python Development In-Reply-To: <5d97efc2-af41-4dd6-a243-50f85d54746c@s16g2000vbp.googlegroups.com> References: <5d97efc2-af41-4dd6-a243-50f85d54746c@s16g2000vbp.googlegroups.com> Message-ID: -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 joshua.pearce at gmail.com wrote: > Or, just give me some general advice on learning C++ for Python? You may want to start with Cython first. It lets you intersperse C and C level information with Python code to produce extensions. That will give you a gentler introduction to using a C like language with Python and for many real world uses is actually sufficient to get C levels of performance and interfacing with Python. After you get that working then you can try coding your own extensions (the Python docs include all the necessary information) and also trying other tools such as SWIG which automate some of the code for wrapping C++, or the wrapping functionality present in Boost. I find it most helpful to explore doing the same thing in multiple different ways as it teaches you what is important and what isn't. Later on it will help you a lot better in choosing the right tool for the job (no one tool or approach is perfect) and help evaluate anything that comes up later. My suggestion would be to pick a particular task and code it in pure Python. Benchmark it (cpu and memory) and then try the other approaches (Cython, Boost, hand coded, SWIG) and see how they compare. Something like a brute force Suduko solver would let you have a few classes/objects but not be too much code. Roger -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (GNU/Linux) iEYEARECAAYFAkoHy/QACgkQmOOfHg372QTxywCgrJPcHLQ5BzD8O29nSAW+I5xo juYAnjMP0xqn/TzC5mrTCqBT3ZnIQo24 =KeIk -----END PGP SIGNATURE----- From ldo at geek-central.gen.new_zealand Mon May 11 02:58:48 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Mon, 11 May 2009 18:58:48 +1200 Subject: OT (humor): 'import antigravity' in action! References: Message-ID: In message , Shawn Milochik wrote: > I know you've probably all seen this 50 times, but just in case: > http://xkcd.com/353/ Ironically, that's no longer valid in Python 3.0. From clp2 at rebertia.com Mon May 11 03:10:32 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Mon, 11 May 2009 00:10:32 -0700 Subject: OT (humor): 'import antigravity' in action! In-Reply-To: References: Message-ID: <50697b2c0905110010j66a4ac11t9836417c23041a4b@mail.gmail.com> On Sun, May 10, 2009 at 11:58 PM, Lawrence D'Oliveiro wrote: > In message , Shawn > Milochik wrote: > >> I know you've probably all seen this 50 times, but just in case: >> http://xkcd.com/353/ > > Ironically, that's no longer valid in Python 3.0. But only by a pair of parens! Still beats the pants off Java. Cheers, Chris -- http://blog.rebertia.com From ldo at geek-central.gen.new_zealand Mon May 11 03:11:00 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Mon, 11 May 2009 19:11 +1200 Subject: How do I test the integrity of a Python installation in Debian and Ubuntu References: Message-ID: In message , Geoff Gardiner wrote: > How do I assure myself of the integrity of a Python installation > acquired using apt-get install on Debian and Ubuntu? apt-get install debsums man debsums From chris.mahan at gmail.com Mon May 11 03:23:29 2009 From: chris.mahan at gmail.com (Christopher Mahan) Date: Mon, 11 May 2009 00:23:29 -0700 Subject: HTTP HEAD and docxmlrpcserver Message-ID: <4d9b53db0905110023r6d92d0dcx4135603713295a02@mail.gmail.com> I have a docxmlrpcserver install (kissws.com) that's returning HTTP code 501 when the client makes a HEAD request. Any idea as to whether that's by design? Thanks in advance. Chris Mahan chris.mahan at gmail.com gv (818) 671-1709 -------------- next part -------------- An HTML attachment was scrubbed... URL: From namekuseijin at gmail.com Mon May 11 03:56:12 2009 From: namekuseijin at gmail.com (namekuseijin) Date: Mon, 11 May 2009 00:56:12 -0700 (PDT) Subject: I'm intrigued that Python has some functional constructions in the language. References: <44350436-de1a-451e-95f5-13b902a902ce@s21g2000vbb.googlegroups.com> <9e68a155-177d-4e03-97b6-c19cbb986515@z19g2000vbz.googlegroups.com> <7fa66f4e-e88c-4fb4-a509-911756814f89@o27g2000vbd.googlegroups.com> <24381226-a7a0-4fbb-b928-644609e5909b@w31g2000prd.googlegroups.com> <2abd8518-fbb1-4a84-b84d-4e1e4a74aa3b@s1g2000prd.googlegroups.com> Message-ID: <9f3d6fc6-8082-497a-bcc2-2514be39f9e3@x6g2000vbg.googlegroups.com> On May 10, 7:18?pm, Carl Banks wrote: > On May 10, 12:40?pm, namekuseijin > wrote: > theoretical argument like, "everything reduces to a function so it > doesn't matter what syntax you use," yet people in the real world are > out there trying to find alternatives because functional languages' > syntax sucks so bad in general. It's not that it sucks. More that it's pretty much non-existent. Yes, many people are very sensitive to such issue -- that is, that they feel lost without common constructs permeating the code rather than the fluid and recursive nature of functional programming and its never-ending flow of userland function calls. OTOH, I can't help but think part of it has to do with plain text in your face rather than lots of colorful keywords in your fav editor. > The reason the OP was asking about separating pure code from impure > was to see if some subset of Python could be used as a pure functional > language, that way they could employ Python and its already-much- > better-than-Haskell's syntax as a pedagogical replacement for Haskell. ah, probably. Python has replaced Scheme in certain pedagogical circles too. Scheme was considered a great tool for teaching basic compsci concepts *precisely* for it's lack of syntax so that students could concentrate on the concepts and problems at hand rather than fight against the compiler or have to learn a huge tome of libs and frameworks before even outputting a single "hello world". But Java was all the rage in the past few years at introductory compsci and at least no doubt Python is a blessing compared to that. Though it too is not without a lot of quirks itself... > I am sure there are many people who think that even "f a b" is cryptic > compared to "f(a,b)", but if that's the only issue it wouldn't be that > big of a deal. ?It's not the only issue. ?When a language requires you > to read and write stuff like "map :: (x -> y) -> f x -> f y" This is a type annotation and there's nothing similar in Python given it's typeless nature. Should I call Python's non-existent type annotations horrible too? >"f s@ (x:xs) = x:s" then it's going to turn a lot of people off. what about @foobize def dingle( n, *boz ): return boz[n:-2] ? Sounds perfectly cryptic for the non-initiated too. From nick at craig-wood.com Mon May 11 04:30:03 2009 From: nick at craig-wood.com (Nick Craig-Wood) Date: Mon, 11 May 2009 03:30:03 -0500 Subject: Learning C++ for Python Development References: <5d97efc2-af41-4dd6-a243-50f85d54746c@s16g2000vbp.googlegroups.com> Message-ID: joshua.pearce at gmail.com wrote: > I am a recovering C# web developer who has recently picked up Django > and I'm loving it. > > I would eventually like to get a job as a Django/Python developer. It > seems that many Python jobs require that you also be a C++ developer. > While I want to remain primarily a web developer, I don't want be > stuck doing CRUD applications, so I would like to learn C++ for Python > development. I have taken two basic programming courses in straight up > C++, no STL, Boost or anything like that, but I do have a very basic > knowledge of the language. > > Can you folks suggest some Python packages which use C++ and are > relevant for web development, so I can dig through the source and > contribute in some way? > > Or, just give me some general advice on learning C++ for Python? I'm guessing that people who advertise for C++ and Python are embedding python into C++ not extending python with C++. Having done both things, I can say that embedding python into C++ is not too much trouble, but that it is slightly easier to extend python with C rather than C++ since the python executable is a C program not a C++ program. I'd start by reading this http://docs.python.org/extending/ And try some of the examples. I'd also read all about ctypes http://docs.python.org/library/ctypes.html Which has saved me a huge amount of time in both the embedding and extending cases! One of the apps I've worked on is C++ but can have "drivers" written in python run by an embedded python interpreter. These drivers can call back into the C++ code, and the C++ can call into the drivers. That is done with a amount of C++ to load the drivers and to export the C++ symbols into the python code at runtime with ctypes. A bit of C++ implements the shims for the callbacks from python -> C++ (which are exported by ctypes). > P.S. I want to develop on Linux not Windows. Should be just the same on both. Once you've made your setup.py (for extending python) the build process will work on all supported architectures. -- Nick Craig-Wood -- http://www.craig-wood.com/nick From david.geldreich at free.fr Mon May 11 04:49:15 2009 From: david.geldreich at free.fr (r2d3) Date: Mon, 11 May 2009 01:49:15 -0700 (PDT) Subject: Compiling Python on Windows : how to deal with modules ? Message-ID: Hi Pythoners, I am using Python embedded in my application (OSX,Windows) and I need to distribute Python as part of my application. On OSX, no problem, I got a self contained framework (with dynamic library and all the modules). On Windows, I manage to compile Python 2.6.2 with PCbuild\build.bat and I get python26.dll/python26_d.dll. But the readme gives no clue about how to deal with the module "compilation"/installation. So how do I "compile"/package all the modules that comes with Python source distribution on Windows ? To embed it in my distribution, I just copy the whole Modules tree (only pyc/pyo/pyd files ?) ? and set the right sys.path from my C code (using PySys_SetPath) ? Thanks in advance. From gh at ghaering.de Mon May 11 05:11:46 2009 From: gh at ghaering.de (=?ISO-8859-1?Q?Gerhard_H=E4ring?=) Date: Mon, 11 May 2009 11:11:46 +0200 Subject: Learning C++ for Python Development In-Reply-To: <5d97efc2-af41-4dd6-a243-50f85d54746c@s16g2000vbp.googlegroups.com> References: <5d97efc2-af41-4dd6-a243-50f85d54746c@s16g2000vbp.googlegroups.com> Message-ID: joshua.pearce at gmail.com wrote: > I am a recovering C# web developer who has recently picked up Django > and I'm loving it. > > I would eventually like to get a job as a Django/Python developer. It > seems that many Python jobs require that you also be a C++ developer. I've seen the C++/Python combination in job descriptions, too. ISTM that these are about systems that are written in C++ and then scripted in Python. For speeding up Python applications. > While I want to remain primarily a web developer, I don't want be > stuck doing CRUD applications, so I would like to learn C++ for Python > development. In the web development field, the probability for doing custom C/C++ development is very low in my experience. > I have taken two basic programming courses in straight up > C++, no STL, Boost or anything like that, but I do have a very basic > knowledge of the language. Learning progamming languages never hurts, but I'd recommend you learn C instead, perhaps by using the Python C API (writing small extension modules). There are not so many Python extensions written in C++, most are written in C. C++ usually doesn't buy you enough to warrant using it instead of C here. So my advice: go with C. Or learn something more relevant to web programming. Like improve your JavaScript skills ;-) > [...] -- Gerhard From wojtulek at gmail.com Mon May 11 05:21:11 2009 From: wojtulek at gmail.com (w.p.) Date: Mon, 11 May 2009 02:21:11 -0700 (PDT) Subject: cx_freeze - cannot get importer instance Message-ID: <8f393e1a-0413-4817-b07c-dc630dc56798@x6g2000vbg.googlegroups.com> Hello! My application is frozen with cx_freeze. Everything is ok, but on one PC with Windows 2003 64bit i got cx_freeze fatal error "cannot get importer instance". I don't use zipimport in my python code and i don't know why i get this error :/ Any idea? How test this error? w.p. From nick at craig-wood.com Mon May 11 05:30:04 2009 From: nick at craig-wood.com (Nick Craig-Wood) Date: Mon, 11 May 2009 04:30:04 -0500 Subject: Q's on my first python script References: Message-ID: kj wrote: > > Below is my very firs python script. > > This was just a learning exercise; the script doesn't do anything > terribly exciting: for an argument of the form YYMMDD (year, month, > day) it prints out the corresponding string YYMMDDW, where W is a > one-letter abbreviation for the day of the week. E.g. > > % wd 090511 > 090511M > > The script runs OK, but I can still see a few areas of improvement, > for which I could use your advice. > > 1. The name of the BadArgument exception class defined in the script > does not seem to me sufficiently specific. If one were to import > the script in order to reuse its wkday_abbrev function, I'd like > this exception's name to be more unequivocally tied to this > script. What I'm looking for is something like a "namespace" > for this script. What's the pythonic way to construct a namespace? > > 2. In some python modules I've seen the idiom > > if __name__ == "__main__": > # run some tests here > > I'd like to set up tests for this script, mostly to ensure that > it handles the error cases properly, but I'm alread using the > idiom above to actually run the script under normal operation. > What's the typical python idiom for running tests on a *script* > (as opposed to a module that is normally not supposed to be run > directly)? > > 3. Still on the subject of testing, how does one capture in a > variable the output that would normally have gone to stdout or > stderr? > > 4. What's the python way to emit warnings? (The script below should > warn the user that arguments after the first one are ignored.) > > 5. The variable wd is meant to be "global" to the script. In other > languages I've programmed in I've seen some typographic convention > used for the name of such variables (e.g. all caps) to signal > this widened scope. Does python have such a convention? > > Any comments/suggestions on these questions, or anything else about > the script, would be much appreciated. To be honest, my first try at this script probably would have looked quite like yours! Once it was working I'd have tidied it as below. * Put into a class to stop spread of globals * WD made class member and upper case * 4 space indents as per PEP8 * remove lamda as it isn't needed * only try:/except: the minimum possible amount of code * new name for exception from optparse import OptionParser import re import datetime import sys class BadDateString(Exception): """Malformed date string""" class Main(object): """Script to implement weekday""" WD = ("M", "T", "W", "H", "F", "S", "U") def wkday_abbrev(self, date_string): mm = re.match("(\d{2})(\d{2})(\d{2})\Z", date_string) if not mm: raise BadDateString("Couldn't match date string from %r" % date_string) y, m, d = map(int, mm.groups()) if y < 38: y = y + 1900 else: y = y + 2000 try: return self.WD[datetime.datetime(y, m, d).weekday()] except ValueError: raise BadDateString("Bad date in %r" % date_string) def __init__(self): usage = '''Usage: %prog [options] YYMMDD %prog -h|--help ''' parser = OptionParser(usage=usage) parser.add_option("-n", "--no-newline", dest="nonl", action="store_true", help="omit newline in output") (options, args) = parser.parse_args(); try: weekday = self.wkday_abbrev(args[0]) except BadDateString, e: print usage print e sys.exit(1) sys.stdout.write("%s%s" % (args[0], weekday)) if not options.nonl: print if __name__ == "__main__": Main() -- Nick Craig-Wood -- http://www.craig-wood.com/nick From nick at craig-wood.com Mon May 11 05:30:04 2009 From: nick at craig-wood.com (Nick Craig-Wood) Date: Mon, 11 May 2009 04:30:04 -0500 Subject: how GNU stow is complementary rather than alternative to distutils References: <49D4DA72.60401@v.loewis.de> <20090407174355.B62983A4063@sparrow.telecommunity.com> <49E4A58F.70309@egenix.com> <20090414162603.70C843A4100@sparrow.telecommunity.com> <49E4F93B.6010802@egenix.com> <20090415003026.B0A783A4114@sparrow.telecommunity.com> <49E59202.6050809@egenix.com> <20090415144147.6845F3A4100@sparrow.telecommunity.com> <49E60832.8030806@egenix.com> <49FB22B5.3040507@simplistix.co.uk> <4A0590C4.1020904@v.loewis.de> <20090509143829.17F293A4080@sparrow.telecommunity.com> <4A059639.7040505@v.loewis.de> <20090509153716.D44633A4080@sparrow.telecommunity.com> <7FF9D9A9-211E-4E5D-BDD0-9C0315123975@zooko.com> <4A070C58.4070003@v.loewis.de> Message-ID: Zooko Wilcox-O'Hearn wrote: > On May 10, 2009, at 11:18 AM, Martin v. L?wis wrote: > > > If GNU stow solves all your problems, why do you want to use > > easy_install in the first place? > > That's a good question. The answer is that there are two separate > jobs: building executables and putting them in a directory structure > of the appropriate shape for your system is one job, and installing > or uninstalling that tree into your system is another. GNU stow does > only the latter. > > The input to GNU stow is a set of executables, library files, etc., > in a directory tree that is of the right shape for your system. For > example, if you are on a Linux system, then your scripts all need to > be in $prefix/bin/, your shared libs should be in $prefix/lib, your > Python packages ought to be in $prefix/lib/python$x.$y/site- > packages/, etc. GNU stow is blissfully ignorant about all issues of > building binaries, and choosing where to place files, etc. -- that's > the job of the build system of the package, e.g. the "./configure -- > prefix=foo && make && make install" for most C packages, or the > "python ./setup.py install --prefix=foo" for Python packages using > distutils (footnote 1). > > Once GNU stow has the well-shaped directory which is the output of > the build process, then it follows a very dumb, completely reversible > (uninstallable) process of symlinking those files into the system > directory structure. Once you've got that well formed directory structure it is very easy to make it into a package (eg deb or rpm) so that idea is useful in general for package managers, not just stow. -- Nick Craig-Wood -- http://www.craig-wood.com/nick From nick at craig-wood.com Mon May 11 05:30:04 2009 From: nick at craig-wood.com (Nick Craig-Wood) Date: Mon, 11 May 2009 04:30:04 -0500 Subject: Wrapping comments References: Message-ID: Rhodri James wrote: > On Sun, 10 May 2009 08:32:23 +0100, Tobias Weber wrote: > > > In article , > > Arnaud Delobelle wrote: > > > >> A simple Alt-Q will reformat everything nicely. > > > > Now that's something. Thanks! > > > > (still not gonna use software that doesn't let me type # because it's > > alt+3 on a UK layout; having to re-learn or configure that is just sick) > > What on earth are you talking about? '#' has its own key on a UK layout > (shared with '~', but you know what I mean), just to the left of the > RETURN key. Emacs is my editor of choice, and I've never once come > across anything like this. You probably haven't used MAC OS X then! I vnc to a mac and use emacs and I just can't type a #. "Ctrl-Q 43 Return" is my best effort! -- Nick Craig-Wood -- http://www.craig-wood.com/nick From bruno.42.desthuilliers at websiteburo.invalid Mon May 11 05:40:03 2009 From: bruno.42.desthuilliers at websiteburo.invalid (Bruno Desthuilliers) Date: Mon, 11 May 2009 11:40:03 +0200 Subject: Code - what could be done better? In-Reply-To: References: Message-ID: <4a07f273$0$29638$426a74cc@news.free.fr> Florian Wollenschein a ?crit : > Hi all, > > here's the main code of thc, my txt to html converter. Since I'm a > beginner it is far, far, faaaaaaar away from perfect or even good :-) > What could be done better? > (snip code) 1/ decouple the text => html conversion part from your (or any other) GUI 2/ decouple the text => html conversion part from files handling (hint: given your code, any object having a 'write' method should do - IOW, the conversion code just a need an object with a "write(somestring)" method as argument, eventually defaulting to sys.stdout. 3/ write tags in lowercase, and get rid from the 'bgcolor' attribute in 4/ or even better, use a templating system, to avoid hardcoding html in your code Also an FWIW, there are quite a few 'simple markup' text format (textile, markdown, ReST, various wiki idioms etc). You may want to be able to use them too. My 2 cents. From smattehulah at gmail.com Mon May 11 06:18:25 2009 From: smattehulah at gmail.com (smattehulah at gmail.com) Date: Mon, 11 May 2009 03:18:25 -0700 (PDT) Subject: Germany will provide project-specific aid to pakistan Message-ID: Germany will provide project-specific aid to the tune of 115 million euro to Pakistan for next two years. for more details visit www.empiresnews.blogspot.com From p.f.moore at gmail.com Mon May 11 06:56:57 2009 From: p.f.moore at gmail.com (Paul Moore) Date: Mon, 11 May 2009 03:56:57 -0700 (PDT) Subject: Tutorial or example use for python-graph library References: <030fcef4-2bf5-4194-97f2-78ce95ca4ef5@v17g2000vbb.googlegroups.com> Message-ID: <5e68ac4d-676b-4fad-ad04-b6335e1decbe@n21g2000vba.googlegroups.com> On May 8, 4:03?pm, Scott David Daniels wrote: > Paul Moore wrote: > > I have just discovered the python-graph library. I've been interested > > in graph algorithms for a long time, so I'd like to give this a try. > > But there seems to be very little in the way of examples, or tutorial > > documentation available. There's the API documentation, but that's not > > really narrative form. And there are some examples in the > > distribution, but these seem to be essentially artificial examples, > > rather than real-world code (and they don't have much in the way of > > documentation!) > > > Are there any reasonably-sized tutorials, or examples of common real- > > world code, using this package? My google-fu wasn't strong enough to > > find anything directly :-( > > Try writing one yourself, as you are learning the interface. ?The > package authors will love it, and will probably be overjoyed to > help you as you stumble. You're probably right. Some of the concepts (hypergraphs, for example) are new to me, and I guess I was hoping for some sample code to start me off understanding what they might be useful for. But I'll go the other route and search around for some of the theory first and see how I might apply it, and then try building code from that. Paul From p.f.moore at gmail.com Mon May 11 06:58:40 2009 From: p.f.moore at gmail.com (Paul Moore) Date: Mon, 11 May 2009 03:58:40 -0700 (PDT) Subject: Tutorial or example use for python-graph library References: <030fcef4-2bf5-4194-97f2-78ce95ca4ef5@v17g2000vbb.googlegroups.com> <8f032f4b-ae7a-45a0-baeb-82005229be22@n4g2000vba.googlegroups.com> Message-ID: On May 8, 3:19?pm, "(e.g. emre)" wrote: > you might want to check networkx as well, it is considerably well > documented:http://networkx.lanl.gov/ Interesting, I hadn't seen that before. I'll certainly check it out! Thanks, Paul From kalman_g at msn.com Mon May 11 07:04:00 2009 From: kalman_g at msn.com (GKalman) Date: Mon, 11 May 2009 04:04:00 -0700 (PDT) Subject: Importing from a module which contains more than one Class... In-Reply-To: References: <23476815.post@talk.nabble.com> Message-ID: <23481160.post@talk.nabble.com> alex23 wrote: > > GKalman wrote: >> from MyClass import * >> from MyOtherClass import * ? ? # error msg: no such module! >> >> As I mentioned above, ?the code for MyClass & MyOtherClass is in the same >> file . This program only works with a single Class in a file. That is >> when >> the File name is the SAME as the Class name. >> >> How to import from a File which contains more than one (unrelated) >> Classes? > > You seem to have misunderstood how 'import' works, I strongly > recommend re-reading the tutorial section: > http://docs.python.org/tutorial/modules.html > > Basically, the import statement has two forms: > > 1. from import > 2. import > > So your example of 'from import *' just doesn't make a lot > of sense within Python. > > If MyClass.py contains two classes, MyClass and MyOtherClass, you > should be able to import them with: > > from MyClass import MyClass, MyOtherClass > > However, you _should_ be already getting _everything_ MyClass.py > contains as you're using the grab-all asterisk. Could you open a > python shell in your Module_Class folder, type 'from MyClass import > MyClass, MyOtherClass', and past whatever traceback you get here? > -- > http://mail.python.org/mailman/listinfo/python-list > > Alex! Looks like I did misunderstand how 'import' works. Thanks for your help, now it works OK. -- View this message in context: http://www.nabble.com/Importing-from-a-module-which-contains-more-than-one-Class...-tp23476815p23481160.html Sent from the Python - python-list mailing list archive at Nabble.com. From anuraguniyal at yahoo.com Mon May 11 08:14:18 2009 From: anuraguniyal at yahoo.com (anuraguniyal at yahoo.com) Date: Mon, 11 May 2009 05:14:18 -0700 (PDT) Subject: unicode bit me References: <977863c9-c4d9-4563-985a-3849d6ed0d75@d2g2000pra.googlegroups.com> <994147fb-cdf3-4c55-8dc5-62d769b12cdc@u9g2000pre.googlegroups.com> <5e8242d0-1225-41e6-bc40-3016670b8618@x31g2000prc.googlegroups.com> <05b9f8b9-c8ea-4591-b81e-d34ed2ccea29@s38g2000prg.googlegroups.com> Message-ID: <08d5f526-aa96-4917-8377-2837ba2fc089@g31g2000pra.googlegroups.com> On May 11, 10:47?am, Terry Reedy wrote: > anuraguni... at yahoo.com wrote: > > so unicode(obj) calls __unicode__ on that object > > It will look for the existence of type(ob).__unicode__ ... > > ?> and if it isn't there __repr__ is used > > According to the below, type(ob).__str__ is tried first. > > > __repr__ of list by default return a str even if __repr__ of element > > is unicode > > ?From the fine library manual, built-in functions section: > (I reccommend using it, along with interactive experiments.) > > "repr( object) > Return a string ..." > > "str( [object]) > Return a string ..." > > "unicode( [object[, encoding [, errors]]]) > > Return the Unicode string version of object using one of the following > modes: > > If encoding and/or errors are given, ... > > If no optional parameters are given, unicode() will mimic the behaviour > of str() except that it returns Unicode strings instead of 8-bit > strings. More precisely, if object is a Unicode string or subclass it > will return that Unicode string without any additional decoding applied. > > For objects which provide a __unicode__() method, it will call this > method without arguments to create a Unicode string. For all other > objects, the 8-bit string version or representation is requested and > then converted to a Unicode string using the codec for the default > encoding in 'strict' mode. > " > > 'unicode(somelist)' has no optional parameters, so skip to third > paragraph. ?Somelist is not a unicode instance, so skip to the last > paragraph. ?If you do dir(list) I presume you will *not* see > '__unicode__' listed. ?So skip to the last sentence. > unicode(somelist) == str(somelist).decode(default,'strict'). > > I do not believe str() and repr() are specifically documented for > builtin classes other than the general description, but you can figure > that str(collection) or repr(collection) will call str or repr on the > members of the collection in order to return a str, as the doc says. Thanks for the explanation. > (Details are available by experiment.) ?Str(uni_string) encodes with the > default encoding, which seems to be 'ascii' in 2.x. ?I am sure it uses > 'strict' errors. > > I would agree that str(some_unicode) could be better documented, like > unicode(some_str) is. > > > so my only solution looks like to use my own list class everywhere i > > use list > > class mylist(list): > > ? ? def __unicode__(self): > > ? ? ? ? return u"["+u''.join(map(unicode,self))+u"]" > > Or write a function and use that instead, or, if and when you can, > switch to 3.x where str and repr accept and produce unicode. > > tjr From giuott at gmail.com Mon May 11 08:26:49 2009 From: giuott at gmail.com (Giuseppe Ottaviano) Date: Mon, 11 May 2009 14:26:49 +0200 Subject: [Python-Dev] how GNU stow is complementary rather than alternative to distutils In-Reply-To: References: <49D4DA72.60401@v.loewis.de> <20090407174355.B62983A4063@sparrow.telecommunity.com> <49E4A58F.70309@egenix.com> <20090414162603.70C843A4100@sparrow.telecommunity.com> <49E4F93B.6010802@egenix.com> <20090415003026.B0A783A4114@sparrow.telecommunity.com> <49E59202.6050809@egenix.com> <20090415144147.6845F3A4100@sparrow.telecommunity.com> <49E60832.8030806@egenix.com> <49FB22B5.3040507@simplistix.co.uk> <4A0590C4.1020904@v.loewis.de> <20090509143829.17F293A4080@sparrow.telecommunity.com> <4A059639.7040505@v.loewis.de> <20090509153716.D44633A4080@sparrow.telecommunity.com> <7FF9D9A9-211E-4E5D-BDD0-9C0315123975@zooko.com> <4A070C58.4070003@v.loewis.de> Message-ID: <6AA82018-2DCA-4F58-BEF9-28D021553247@gmail.com> Talking of stow, I take advantage of this thread to do some shameless advertising :) Recently I uploaded to PyPI a software of mine, BPT [1], which does the same symlinking trick of stow, but it is written in Python (and with a simple api) and, more importantly, it allows with another trick the relocation of the installation directory (it creates a semi- isolated environment, similar to virtualenv). I find it very convenient when I have to switch between several versions of the same packages (for example during development), or I have to deploy on the same machine software that needs different versions of the dependencies. I am planning to write an integration layer with buildout and easy_install. It should be very easy, since BPT can handle directly tarballs (and directories, in trunk) which contain a setup.py. HTH, Giuseppe [1] http://pypi.python.org/pypi/bpt P.S. I was not aware of stow, I'll add it to the references and see if there are any features that I can steal From michele.simionato at gmail.com Mon May 11 08:37:59 2009 From: michele.simionato at gmail.com (Michele Simionato) Date: Mon, 11 May 2009 05:37:59 -0700 (PDT) Subject: Decorating methods - where do my arguments go? References: Message-ID: On May 8, 5:33?pm, Mikael Olofsson wrote: > ?>>> class test_decorator(object): > ... ? ? def __init__(self,func): > ... ? ? ? ? self._func = func > ... ? ? def __call__(self, *args): > ... ? ? ? ? print 'Decorator:', args > ... ? ? ? ? self._func(*args) Or you could use the decorator module (http://pypi.python.org/pypi/ decorator): from decorator import decorator @decorator def test_decorator(func, *args, **kw): print 'Decorator:', args return func(*args) From aahz at pythoncraft.com Mon May 11 08:46:18 2009 From: aahz at pythoncraft.com (Aahz) Date: Mon, 11 May 2009 05:46:18 -0700 Subject: Switchover: mail.python.org Message-ID: <20090511124618.GA19400@panix.com> On Monday 2009-05-11, mail.python.org will be switched to another machine starting roughly at 14:00 UTC. This should be invisible (expected downtime is less than ten minutes). -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "It is easier to optimize correct code than to correct optimized code." --Bill Harlan From bruno.42.desthuilliers at websiteburo.invalid Mon May 11 09:12:12 2009 From: bruno.42.desthuilliers at websiteburo.invalid (Bruno Desthuilliers) Date: Mon, 11 May 2009 15:12:12 +0200 Subject: Why there is a parameter named "self" for classmethod function? In-Reply-To: References: <2b767f890905061949g12d0bf03m68c6b466ffedfbd9@mail.gmail.com> <4A024F18.9080407@kbsymanzik.org> Message-ID: <4a08242b$0$24000$426a34cc@news.free.fr> Terry Reedy a ?crit : > Kurt Symanzik wrote: > >> >> But you might consider decorating the method as a static method >> instead since in your example you are not using the parameter at all. >> A static method would not require a parameter. >> >> @staticmethod >> def print_hello(): >> print "hello" > > Functions that refer to neither the class nor an instance thereof can > usually be moved outside the class altogether. Python is not Java. Indeed. But there are a couple uses for staticmethods - one of them being polymorphic dispatch on the receiver object. And yes, I know, modules are objects too, but this is not always an option (legacy code, integration with a framework etc). From eckhardt at satorlaser.com Mon May 11 09:33:03 2009 From: eckhardt at satorlaser.com (Ulrich Eckhardt) Date: Mon, 11 May 2009 15:33:03 +0200 Subject: Skipping unit tests Message-ID: Hi! We have a few tests for some module here. These tests are under development and applied to older versions (with less features) of the module, too. That means that if I have module version 42, tests A and B can not possibly work. I don't want to have test failures but I also don't want to fork the test suite, so how do I get the tests to behave like Python's test suite, which also skips some "expected failures". Thank you! Uli -- Sator Laser GmbH Gesch?ftsf?hrer: Thorsten F?cking, Amtsgericht Hamburg HR B62 932 From justin.donato at gmail.com Mon May 11 09:51:42 2009 From: justin.donato at gmail.com (justind) Date: Mon, 11 May 2009 06:51:42 -0700 (PDT) Subject: win32 How to make sure a file is completely written? Message-ID: <6b33c5b3-ae51-4245-a3d5-28d48b76dd07@j12g2000vbl.googlegroups.com> Hello, I'm using http://code.activestate.com/recipes/156178/ to watch a folder in windows. It's working perfectly, but sometimes when I try to open the file immediately after receiving the event, it's not ready to be opened--if I try to open it with PIL I get "IOError: cannot identify image file" and if I try it with a text file, it's empty. This doesn't happen all the time, just occasionally. I think the problem is that the file isn't completely written because if I make the script sleep for a second, it works every time. But that doesn't seem very elegant or robust. What's the proper way to make sure the file is ready to be read? I'm just passing the file names from the above recipe to a function. Something like def handler(files): for file in files: im = Image.open(file) Thanks From mabdelkader at gmail.com Mon May 11 10:02:21 2009 From: mabdelkader at gmail.com (ma) Date: Mon, 11 May 2009 10:02:21 -0400 Subject: win32 How to make sure a file is completely written? In-Reply-To: <6b33c5b3-ae51-4245-a3d5-28d48b76dd07@j12g2000vbl.googlegroups.com> References: <6b33c5b3-ae51-4245-a3d5-28d48b76dd07@j12g2000vbl.googlegroups.com> Message-ID: <148918f0905110702q13f514a8k7e884b5a152d1394@mail.gmail.com> You have to wait until IO is ready. In Unix, we accomplish this with fcntl and the default signal SIGIO, I am not sure how you would do this in Windows. On Mon, May 11, 2009 at 9:51 AM, justind wrote: > Hello, > > I'm using http://code.activestate.com/recipes/156178/ to watch a > folder in windows. It's working perfectly, but sometimes when I try to > open the file immediately after receiving the event, it's not ready to > be opened--if I try to open it with PIL I get "IOError: cannot > identify image file" and if I try it with a text file, it's empty. > This doesn't happen all the time, just occasionally. I think the > problem is that the file isn't completely written because if I make > the script sleep for a second, it works every time. But that doesn't > seem very elegant or robust. > > What's the proper way to make sure the file is ready to be read? > > I'm just passing the file names from the above recipe to a function. > Something like > > def handler(files): > ? ?for file in files: > ? ? ? ?im = Image.open(file) > > Thanks > > > -- > http://mail.python.org/mailman/listinfo/python-list > From mail at timgolden.me.uk Mon May 11 10:03:34 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Mon, 11 May 2009 15:03:34 +0100 Subject: win32 How to make sure a file is completely written? In-Reply-To: <6b33c5b3-ae51-4245-a3d5-28d48b76dd07@j12g2000vbl.googlegroups.com> References: <6b33c5b3-ae51-4245-a3d5-28d48b76dd07@j12g2000vbl.googlegroups.com> Message-ID: <4A083036.90609@timgolden.me.uk> justind wrote: > Hello, > > I'm using http://code.activestate.com/recipes/156178/ to watch a > folder in windows. Wow, that takes me back. There's a bit more info (and a different technique) here if you're interested: http://timgolden.me.uk/python/win32_how_do_i/watch_directory_for_changes.html But to come back your question... > It's working perfectly, but sometimes when I try to > open the file immediately after receiving the event, it's not ready to > be opened--if I try to open it with PIL I get "IOError: cannot > identify image file" and if I try it with a text file, it's empty. > This doesn't happen all the time, just occasionally. I think the > problem is that the file isn't completely written because if I make > the script sleep for a second, it works every time. But that doesn't > seem very elegant or robust. > > What's the proper way to make sure the file is ready to be read? I don't believe there's an easy answer to this question. From the filesystem's point of view some process creates a file. So it tells you that a file has been created. The filesystem has no knowledge of when a file is "complete". It could -- altho' it doesn't -- fire an event when a handle on that file is closed, but even that wouldn't tell you much: you'd have to be able to distinguish between the handle which is behind the data you're looking for and a handle from a virus scanner which is checking the new file for malware. To add difficulty, when Windows initiates a file copy (as opposed to creating a new file) it immediately creates the file at its full size and then copies data in. Which makes some sense as it should avoid some element of fragmentation etc. But it removes one of your options: noting the size of an original file and comparing it with the size of a copy. So I think you're out of luck and you're going to have to put a try-except loop in place. TJG From mikael at isy.liu.se Mon May 11 10:06:11 2009 From: mikael at isy.liu.se (Mikael Olofsson) Date: Mon, 11 May 2009 16:06:11 +0200 Subject: Decorating methods - where do my arguments go? In-Reply-To: References: Message-ID: Peter Otten wrote: > You have to turn your decorator into a descriptor by providing a __get__() > method. A primitive example: > > class test_decorator(object): > def __init__(self,func): > self._func = func > def __call__(self, *args): > print 'Decorator:', args > self._func(self.inst, *args) > def __get__(self, inst, cls): > self.inst = inst > return self Thanks! Works perfectly for methods, as far as I can see. That's perfectly OK, since that is what I was asking for. What I failed to include in my original post is that I intend to use the decorator for functions as well. The above fails for functions on the second row of __call__ with AttributeError: 'test_decorator' object has no attribute 'inst' It seems to me like __get__ is not called when decorating a function. I guess there's no reasonable value for inst in that case. It might be the function itself, but it doesn't make sense to call the function with itself as its first argument, when it isn't supposed to be called that way. George Sakkis decorator function solution seems to work equally well for functions and methods. However, I prefer the cleaner encapsulation given by a class. Based on those observations, I think I will use the following approach: >>> class test_decorator(object): ... _inst = None ... def __init__(self,func): ... self._func = func ... def __call__(self, *args): ... print 'Decorator:', args ... if self._inst is None: ... print 'We seem to be decorating a function.' ... self._func(*args) ... else: ... print 'We seem to be decorating a method.' ... self._func(self._inst,*args) ... def __get__(self, inst, cls): ... self._inst = inst ... return self ... >>> >>> @test_decorator ... def func(*args): ... print 'Function: ', args ... >>> >>> func(1,2,3) Decorator: (1, 2, 3) We seem to be decorating a function. Function: (1, 2, 3) >>> >>> class cls(object): ... @test_decorator3 ... def meth(self,*args): ... print 'Method: ', args ... >>> >>> cls().meth(1,2,3) Decorator: (1, 2, 3) We seem to be decorating a method. Method: (1, 2, 3) If there are any drawbacks with this approach that I fail to see, please enlighten me. /MiO From mikael at isy.liu.se Mon May 11 10:06:26 2009 From: mikael at isy.liu.se (Mikael Olofsson) Date: Mon, 11 May 2009 16:06:26 +0200 Subject: Decorating methods - where do my arguments go? In-Reply-To: References: Message-ID: George Sakkis wrote: > Yes, just return an actual function from the decorator instead of a > callable object: > > def test_decorator2(func): > def wrapper(*args): > print 'Decorator2:', args > func(*args) > return wrapper > > > class cls(object): > @test_decorator > def meth(self,*args): > print 'Method: ', args > > @test_decorator2 > def meth2(self,*args): > print 'Method2: ', args Thanks! This has the merit over Peter's solution that it seems to work for both functions and methods. However, as you can see from my answer to Peter, I think I will go for a class based approach anyway. Still, your answer helped me grasping the concepts. /MiO From justin.donato at gmail.com Mon May 11 10:12:12 2009 From: justin.donato at gmail.com (justind) Date: Mon, 11 May 2009 07:12:12 -0700 (PDT) Subject: win32 How to make sure a file is completely written? References: <6b33c5b3-ae51-4245-a3d5-28d48b76dd07@j12g2000vbl.googlegroups.com> Message-ID: <2cd511d8-7789-4f47-9b99-616205f9794c@t10g2000vbg.googlegroups.com> On May 11, 10:03?am, Tim Golden wrote: > justind wrote: > > Hello, > > > I'm usinghttp://code.activestate.com/recipes/156178/to watch a > > folder in windows. > > Wow, that takes me back. There's a bit more info (and a different > technique) here if you're interested: > > ?http://timgolden.me.uk/python/win32_how_do_i/watch_directory_for_chan... > > But to come back your question... > > > It's working perfectly, but sometimes when I try to > > open the file immediately after receiving the event, it's not ready to > > be opened--if I try to open it with PIL I get "IOError: cannot > > identify image file" and if I try it with a text file, it's empty. > > This doesn't happen all the time, just occasionally. I think the > > problem is that the file isn't completely written because if I make > > the script sleep for a second, it works every time. But that doesn't > > seem very elegant or robust. > > > What's the proper way to make sure the file is ready to be read? > > I don't believe there's an easy answer to this question. From the > filesystem's point of view some process creates a file. So it > tells you that a file has been created. The filesystem has no > knowledge of when a file is "complete". It could -- altho' it > doesn't -- fire an event when a handle on that file is closed, > but even that wouldn't tell you much: you'd have to be able > to distinguish between the handle which is behind the data > you're looking for and a handle from a virus scanner which is > checking the new file for malware. > > To add difficulty, when Windows initiates a file copy (as opposed > to creating a new file) it immediately creates the file at its > full size and then copies data in. Which makes some sense as it > should avoid some element of fragmentation etc. But it removes > one of your options: noting the size of an original file and > comparing it with the size of a copy. > > So I think you're out of luck and you're going to have to > put a try-except loop in place. > > TJG Thanks Guys. From denis.papathanasiou at gmail.com Mon May 11 10:39:14 2009 From: denis.papathanasiou at gmail.com (dpapathanasiou) Date: Mon, 11 May 2009 07:39:14 -0700 (PDT) Subject: mod_python and xml.dom.minidom References: <83b63d98-9871-47eb-8846-46404b70896e@z7g2000vbh.googlegroups.com> <12ef7b00-f0c5-4030-a353-533d93d23cdd@r3g2000vbp.googlegroups.com> <794f117f-4df5-4c6c-be68-f901959b2579@y10g2000prc.googlegroups.com> Message-ID: > His problem is therefore likely to be something completely different. You are correct. As per the earlier advice, I switched from mod_python to mod_wsgi but I still see the same error: [Mon May 11 10:30:21 2009] [notice] Apache/2.2.11 (Unix) mod_wsgi/2.4 Python/2.5.2 configured -- resuming normal operations [Mon May 11 10:30:26 2009] [error] Traceback (most recent call last): [Mon May 11 10:30:26 2009] [error] File "../db/items_db.py", line 38, in [Mon May 11 10:30:26 2009] [error] db_object.associate(sdb_object, (lambda primary_key, primary_data:xml_utils.parse_item_attribute (primary_data, attribute))) [Mon May 11 10:30:26 2009] [error] File "../common/xml_utils.py", line 80, in parse_item_attribute [Mon May 11 10:30:26 2009] [error] item_doc = minidom.parseString (item) [Mon May 11 10:30:26 2009] [error] File "/usr/lib/python2.5/xml/dom/ minidom.py", line 1924, in parseString [Mon May 11 10:30:26 2009] [error] from xml.dom import expatbuilder [Mon May 11 10:30:26 2009] [error] SystemError: Parent module 'xml.dom' not loaded [Mon May 11 10:30:26 2009] [error] Traceback (most recent call last): [Mon May 11 10:30:26 2009] [error] File "../db/items_db.py", line 38, in [Mon May 11 10:30:26 2009] [error] db_object.associate(sdb_object, (lambda primary_key, primary_data:xml_utils.parse_item_attribute (primary_data, attribute))) [Mon May 11 10:30:26 2009] [error] File "../common/xml_utils.py", line 80, in parse_item_attribute [Mon May 11 10:30:26 2009] [error] item_doc = minidom.parseString (item) [Mon May 11 10:30:26 2009] [error] File "/usr/lib/python2.5/xml/dom/ minidom.py", line 1924, in parseString [Mon May 11 10:30:26 2009] [error] from xml.dom import expatbuilder [Mon May 11 10:30:26 2009] [error] SystemError: Parent module 'xml.dom' not loaded The odd thing is that when xml_utils.py is run outside of either apache module, xml.dom does load, and the minidom parsing works. I'm not sure why this is happening, but the next thing I'll do is try replacing minidom with ElementTree, and see if that has any issues running under either apache module. From agaskins_ng at kelleramerica.com Mon May 11 10:53:38 2009 From: agaskins_ng at kelleramerica.com (Adam Gaskins) Date: Mon, 11 May 2009 10:53:38 -0400 Subject: OOP & Abstract Classes Message-ID: Hi all, -- Non critical info-- I am a fairly seasoned PHP developer (don't shoot, I'm changing teams!:) who is admittedly behind the curve with OOP. Like most who learned PHP, I started doing web app backend stuff, but I have moved to full blown windows apps in the last 6 months using Winbinder. As you may know Winbinder is essentially abandoned, and programming windows apps with Winbinder or even GTK-PHP is less than ideal. I also deal heavily in serial/rs-232 communications which is a pain in PHP. Long story short, I'm tired of doing things in such a hackish manner and want to write applications that are cross platform (I'd like to get our production dept on linux eventually) and truely object oriented. -- END non critical stuff-- So I was beginning to learn OOP for PHP, and it seemed to me that abstract classes were just right for my application. In my application I must communicate with several peices of test equipment that communicate via RS-232. Most use SCPI instructions, some do not and require low level communication. The way I understand abstract classes is that I could have a class that has all my abstract methods such as 'init', 'getMeasurement', 'setPressure', etc... then I could use this common interface to to control my different pieces of hardware (after I write a library for each of them). Is this a valid application for abstract classes? Or am I making life more complicated than it need be? Now, I have read that Pythons OOP implimentation is so much better and more complete then PHP's, but I cant find anything about abstract classes except this: http://norvig.com/python-iaq.html ...which says it doesn't exist in Python and you can only do this hack to make it work (and I don't particularly understand how to impliment it from this article). This makes me suspect I am making things more comlicated than they need to be. Could someone help me understand the proper way to impliment a set classes/methods to deal with several peices of similar and not so similar hardware? Not looking for anyone to write code for me, just help understanding some of these OOP concepts and how they would apply to my situation. Thanks! -Adam From marco at sferacarta.com Mon May 11 10:58:57 2009 From: marco at sferacarta.com (Marco Mariani) Date: Mon, 11 May 2009 16:58:57 +0200 Subject: Pycon Tre, grazie! In-Reply-To: <470a7d8f-0bd0-4b17-92b2-6c703ecad2fe@o14g2000vbo.googlegroups.com> References: <470a7d8f-0bd0-4b17-92b2-6c703ecad2fe@o14g2000vbo.googlegroups.com> Message-ID: daniele wrote: > Si ? concluso ieri la pycon tre, ? stata molto interessante, un bel > evento per una bella comunit?. Sempre meglio.. anche se mi preoccupa un po' un eventuale cambio di location > Ho visto con grande piacere, oltre all'incremento dei partecipanti, > anche un sensibile incremento delle partecipanti! Si'... in particolare durante il talk di Raymond :) From eckhardt at satorlaser.com Mon May 11 11:13:10 2009 From: eckhardt at satorlaser.com (Ulrich Eckhardt) Date: Mon, 11 May 2009 17:13:10 +0200 Subject: OOP & Abstract Classes References: Message-ID: <6trld6-98j.ln1@satorlaser.homedns.org> Adam Gaskins wrote: > Long story short, I'm tired of doing things in such a hackish manner > and want to write applications that are cross platform (I'd like to > get our production dept on linux eventually) and truely object > oriented. Adam, there is one notion here that I seriously dislike: you sound as if OOP was a goal by itself for you. The point is that OOP is a tool, not a goal. So, if it helps you do the things you really want, go ahead and use it. Otherwise, just don't. > The way I understand abstract classes is that I could have a class that > has all my abstract methods such as 'init', 'getMeasurement', > 'setPressure', etc... then I could use this common interface to to control > my different pieces of hardware (after I write a library for each of > them). > > Is this a valid application for abstract classes? Or am I making life more > complicated than it need be? I think that the term is rather "abstract base class" (ABC, a.k.a. "interface"). Note that in Python, where you don't have to use prototypes or declaration, you often don't do that. Instead you use "duck typing", i.e. you simply create different classes for the different hardware and if all of them support 'init' and 'setPressure' instances are interchangeable ("Liskov substitution principle", IIRC) without knowing anything about each other. > This makes me suspect I am making things more comlicated than they need to > be. Could someone help me understand the proper way to impliment a set > classes/methods to deal with several peices of similar and not so similar > hardware? Just write a class for each piece and adhere to a common interface and you're done. ;) Uli -- Sator Laser GmbH Gesch?ftsf?hrer: Thorsten F?cking, Amtsgericht Hamburg HR B62 932 From duncan.booth at invalid.invalid Mon May 11 11:17:55 2009 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 11 May 2009 15:17:55 GMT Subject: Decorating methods - where do my arguments go? References: Message-ID: Mikael Olofsson wrote: > George Sakkis decorator function solution seems to work equally well for > functions and methods. However, I prefer the cleaner encapsulation given > by a class. Based on those observations, I think I will use the > following approach: > > >>> class test_decorator(object): > ... _inst = None > ... def __init__(self,func): > ... self._func = func > ... def __call__(self, *args): > ... print 'Decorator:', args > ... if self._inst is None: > ... print 'We seem to be decorating a function.' > ... self._func(*args) > ... else: > ... print 'We seem to be decorating a method.' > ... self._func(self._inst,*args) > ... def __get__(self, inst, cls): > ... self._inst = inst > ... return self > ... The __get__ method should be returning a new object, NOT modifying the state of the decorator. As written it will break badly and unexpectedly in a variety of situations: >>> inst1 = cls() >>> inst2 = cls() >>> inst1, inst2 (<__main__.cls object at 0x011BC470>, <__main__.cls object at 0x011BCC90>) >>> m = inst1.meth >>> m() Decorator: () We seem to be decorating a method. Method: <__main__.cls object at 0x011BC470> () >>> inst2.meth() Decorator: () We seem to be decorating a method. Method: <__main__.cls object at 0x011BCC90> () >>> m() Decorator: () We seem to be decorating a method. Method: <__main__.cls object at 0x011BCC90> () or even this: >>> inst1.meth(inst2.meth()) Decorator: () We seem to be decorating a method. Method: <__main__.cls object at 0x011BCC90> () Decorator: (None,) We seem to be decorating a method. Method: <__main__.cls object at 0x011BCC90> (None,) -- Duncan Booth http://kupuguy.blogspot.com From socyl at 987jk.com.invalid Mon May 11 11:19:05 2009 From: socyl at 987jk.com.invalid (kj) Date: Mon, 11 May 2009 15:19:05 +0000 (UTC) Subject: What's the use of the else in try/except/else? References: Message-ID: In Scott David Daniels writes: >kj wrote: >> ... I can't come with an example in which the same couldn't be >> accomplished with >> >> try: >> # do something >> # do something else >> except ...: >> # handle exception >> >> The only significant difference I can come up with is that in the >> second form, the except clause may be masking some unexpected >> exceptions from the "do something else" part. Is this the rationale >> behind this else clause? Or is there something more to it? >Yes, in a way. The idea of catching particular exceptions is to only >handle exceptions you expect (let the others go out to more general >reporters). So, not only should you choose the tightest exception to >catch that you can, but you should look for it in a very narrow window: >exactly where you expect it. > try: > v = mumble.field > except AttributeError: > pass > else: > sys.warning('field was actually there?') >as opposed to: > try: > v = mumble.field > sys.warning('field was actually there?') > except AttributeError: > pass >The idea is to make it clear what you expect might go >wrong that you are prepared to handle. Wow. As rationales for syntax constructs go, this has got to be the most subtle one I've ever seen... Thanks! kynn -- NOTE: In my address everything before the first period is backwards; and the last period, and everything after it, should be discarded. From aahz at pythoncraft.com Mon May 11 11:22:10 2009 From: aahz at pythoncraft.com (Aahz) Date: 11 May 2009 08:22:10 -0700 Subject: How do I test the integrity of a Python installation in Debian and Ubuntu References: Message-ID: In article , Geoff Gardiner wrote: >Aahz wrote: >> >> What directory are you running this from? What happens if you switch to >> running "python Lib/test/regrtest.py"? Taking a closer look, this looks >> more like a plain import error. > >I couldn't do quite that because there's no Lib, but instead (in Ubuntu >Hardy, this time): >.... >gegard at gegard:~$ cd /usr/lib/python2.5/ >gegard at gegard:/usr/lib/python2.5$ python test/regrtest.py > >9 tests skipped: > test_builtin test_doctest test_doctest2 test_exceptions > test_grammar test_opcodes test_operations test_types test_unittest >Traceback (most recent call last): > File "test/regrtest.py", line 1384, in > main() > File "test/regrtest.py", line 416, in main > e = _ExpectedSkips() > File "test/regrtest.py", line 1321, in __init__ > from test import test_socket_ssl >ImportError: cannot import name test_socket_ssl >gegard at gegard:/usr/lib/python2.5$ > >Also >gegard at gegard:~$ locate */test_socket_ssl.* >gegard at gegard:~$ #returns nothing > >And >gegard at gegard:~$ locate /usr/lib/python2.5/test/test_*.* >/usr/lib/python2.5/test/test_support.py >/usr/lib/python2.5/test/test_support.pyc >gegard at gegard:~$ That seems to demonstrate that regrtest.py is indeed a good mechanism for finding out whether it's a b0rked install! -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "It is easier to optimize correct code than to correct optimized code." --Bill Harlan From kyosohma at gmail.com Mon May 11 11:22:25 2009 From: kyosohma at gmail.com (Mike Driscoll) Date: Mon, 11 May 2009 08:22:25 -0700 (PDT) Subject: OOP & Abstract Classes References: Message-ID: <74c79941-af9b-4b43-b02b-8316b108f08d@l28g2000vba.googlegroups.com> On May 11, 9:53?am, "Adam Gaskins" wrote: > Hi all, > > -- Non critical info-- > I am a fairly seasoned PHP developer (don't shoot, I'm changing teams!:) who > is admittedly behind the curve with OOP. Like most who learned PHP, I > started doing web app backend stuff, but I have moved to full blown windows > apps in the last 6 months using Winbinder. As you may know Winbinder is > essentially abandoned, and programming windows apps with Winbinder or even > GTK-PHP is less than ideal. I also deal heavily in serial/rs-232 > communications which is a pain in PHP. Long story short, I'm tired of doing > things in such a hackish manner and want to write applications that are > cross platform (I'd like to get our production dept on linux eventually) and > truely object oriented. > -- END non critical stuff-- > > So I was beginning to learn OOP for PHP, and it seemed to me that abstract > classes were just right for my application. In my application I must > communicate with several peices of test equipment that communicate via > RS-232. Most use SCPI instructions, some do not and require low level > communication. > > The way I understand abstract classes is that I could have a class that has > all my abstract methods such as 'init', 'getMeasurement', 'setPressure', > etc... then I could use this common interface to to control my different > pieces of hardware (after I write a library for each of them). > > Is this a valid application for abstract classes? Or am I making life more > complicated than it need be? > > Now, I have read that Pythons OOP implimentation is so much better and more > complete then PHP's, but I cant find anything about abstract classes except > this:http://norvig.com/python-iaq.html > > ...which says it doesn't exist in Python and you can only do this hack to > make it work (and I don't particularly understand how to impliment it from > this article). > > This makes me suspect I am making things more comlicated than they need to > be. Could someone help me understand the proper way to impliment a set > classes/methods to deal with several peices of similar and not so similar > hardware? Not looking for anyone to write code for me, just help > understanding some of these OOP concepts and how they would apply to my > situation. > > Thanks! > -Adam I've never used (or heard of) the Abstract type...and the guy who wrote the FAQ was being a jerk. It looks like he was just throwing in an undefined variable name just to make his Python program break while taking a pot shot at people who use that sort of thing. Whatever. According to wikipedia, dynamic languages don't implement Abstract as they can accomplish the same thing via duck typing: http://en.wikipedia.org/wiki/Abstract_class . The way it describes the class, it made me think of decorators as well. Hopefully someone who has used Abstract classes will jump in here and give you more information about whether or not they matter in Python. Mike From denis.papathanasiou at gmail.com Mon May 11 11:59:20 2009 From: denis.papathanasiou at gmail.com (dpapathanasiou) Date: Mon, 11 May 2009 08:59:20 -0700 (PDT) Subject: mod_python and xml.dom.minidom References: <83b63d98-9871-47eb-8846-46404b70896e@z7g2000vbh.googlegroups.com> <12ef7b00-f0c5-4030-a353-533d93d23cdd@r3g2000vbp.googlegroups.com> <794f117f-4df5-4c6c-be68-f901959b2579@y10g2000prc.googlegroups.com> Message-ID: <037bc6d0-3d2e-45bd-91d3-d3051fbfa149@s31g2000vbp.googlegroups.com> For the record, and in case anyone else runs into this particular problem, here's how resolved it. My original xml_utils.py was written this way: from xml.dom import minidom def parse_item_attribute (item, attribute_name): item_doc = minidom.parseString(item) ... That version worked under the python interpreter, but failed under both mod_python and mod_wsgi apache modules with an error ("Parent module 'xml.dom' not loaded"). I found that changing the import statement and the minidom reference within the function resolved the problem. I.e., after rewriting xml_utils.py this way, it works under both apache modules as well as in the python interpreter: import xml.dom.minidom def parse_item_attribute (item, attribute_name): item_doc = xml.dom.minidom.parseString(item) ... From marco at sferacarta.com Mon May 11 11:59:31 2009 From: marco at sferacarta.com (Marco Mariani) Date: Mon, 11 May 2009 17:59:31 +0200 Subject: OOP & Abstract Classes In-Reply-To: <74c79941-af9b-4b43-b02b-8316b108f08d@l28g2000vba.googlegroups.com> References: <74c79941-af9b-4b43-b02b-8316b108f08d@l28g2000vba.googlegroups.com> Message-ID: Mike Driscoll wrote: > I've never used (or heard of) the Abstract type...and the guy who > wrote the FAQ was being a jerk. Who, Peter Norvig? (from wikipedia) Peter Norvig is an American computer scientist. He is currently the Director of Research (formerly Director of Search Quality) at Google Inc. He is a Fellow and Councilor of the American Association for Artificial Intelligence and co-author, with Stuart Russell, of Artificial Intelligence: A Modern Approach, now the standard college text. He previously was head of the Computational Sciences Division (now the Intelligent Systems Division) at NASA Ames Research Center, where he oversaw a staff of 200 scientists performing NASA's research and development in autonomy and robotics, automated software engineering and data analysis, neuroengineering, collaborative systems research, and simulation-based decision-making. Before that he was Chief Scientist at Junglee, where he helped develop one of the first Internet comparison shopping services; Chief designer at Harlequin Inc.; and Senior Scientist at Sun Microsystems Laboratories. etc. etc. Yes, I usually look up in wikipedia before calling anyone a jerk :) :) From mikael at isy.liu.se Mon May 11 12:04:04 2009 From: mikael at isy.liu.se (Mikael Olofsson) Date: Mon, 11 May 2009 18:04:04 +0200 Subject: Decorating methods - where do my arguments go? In-Reply-To: References: Message-ID: Duncan Booth wrote: > The __get__ method should be returning a new object, NOT modifying the > state of the decorator. As written it will break badly and unexpectedly > in a variety of situations: > > [snip good examples of things going bad] Ouch! So, does that mean that George's solution based on a function is the way to go, or does that mean that my __call__ should analyze the passed callable? /MiO From iwan at reahl.org Mon May 11 12:19:41 2009 From: iwan at reahl.org (Iwan) Date: Mon, 11 May 2009 09:19:41 -0700 (PDT) Subject: How to debug this import problem? References: <76j8vcF1crfkoU1@mid.uni-berlin.de> Message-ID: <9b48b208-f656-4de0-9fd7-2aec9394cc2c@q2g2000vbr.googlegroups.com> Mmm, we solved half of the cause of this one. Test runs are kicked off via setuptools's test command. But this happens programmatically, and successively in one process. But setuptools's test command clears all modules imported during a test run from sys.modules - hence it is intended that modules would be re- imported. Why certain of our code managed to still get to the classes contained in prior imports is still a mystery though. -Iwan From gazathome at gmail.com Mon May 11 12:27:16 2009 From: gazathome at gmail.com (gazathome at gmail.com) Date: Mon, 11 May 2009 09:27:16 -0700 (PDT) Subject: Writing text to a Word Document Message-ID: <8ba265c9-4697-41b4-a117-04761a86a945@g20g2000vba.googlegroups.com> Hi everyone, I am trying to write several attributes from a database table and using the code below I can write the values however it is only overwriting on the first line. I am new to the win32com bit and I would like to know what is the recommended reference to loop down the page and add multiple records rather than the worddoc.Content. I have read a little about writing this to an RTF or htmol but I am getting information overload and going nowhere. Does anyone have a nice tutorial out there that simply explains how to do this? Much appreciated for any thoughts. Cheers, Gareth import win32com.client, arcgisscripting, sys, os gp = arcgisscripting.create(9.3) #Create word document component - http://www.faqts.com/knowledge_base/view.phtml/aid/37034/fid/244 wordapp = win32com.client.Dispatch("Word.Application") # Create new Word Object wordapp.Visible = 1 # Word Application should`t be visible worddoc = wordapp.Documents.Add() # Create new Document Object worddoc.Content.Font.Size = 11 worddoc.Content.Paragraphs.TabStops.Add (100) #Setup of GDB Connection # Get the Featureclass from the Mobel - Property Box PropBox = "C:\\Avon Fire Rescue\\data\AvonFire_1.gdb\\PropertyBox" # Make a search cursor for the Property Box Feature class PBselCur = gp.searchcursor(PropBox) PBrow = PBselCur.reset() PBrow = PBselCur.next() #Using the search cursor get the Premise ID from a Property box while PBrow: PBrowVal = PBrow.getvalue("PremisesID") #Add data to Word Document worddoc.Content.Text = (PBrowVal) PBrow = PBselCur.next() #worddoc.Close() # Close the Word Document (a save-Dialog pops up) print "Fin" From __peter__ at web.de Mon May 11 12:28:54 2009 From: __peter__ at web.de (Peter Otten) Date: Mon, 11 May 2009 18:28:54 +0200 Subject: Decorating methods - where do my arguments go? References: Message-ID: Mikael Olofsson wrote: > Duncan Booth wrote: > >> The __get__ method should be returning a new object, NOT modifying the >> state of the decorator. As written it will break badly and unexpectedly >> in a variety of situations: >> >> [snip good examples of things going bad] > > Ouch! So, does that mean that George's solution based on a function is > the way to go, or does that mean that my __call__ should analyze the > passed callable? I usually use decorator functions, but I think the following should work, too: class deco(object): def __init__(self, func): self._func = func def __call__(self, *args): print "Decorator:", args self._func(*args) def __get__(self, *args): return deco(self._func.__get__(*args)) Peter From james at agentultra.com Mon May 11 12:29:25 2009 From: james at agentultra.com (J Kenneth King) Date: Mon, 11 May 2009 12:29:25 -0400 Subject: list comprehension question References: <6d502cfd-e252-4722-96a5-c52fc3579499@v1g2000prd.googlegroups.com> <85ocucnbce.fsf@agentultra.com> <85ljpb8r1o.fsf@agentultra.com> <85d4am8hq4.fsf@agentultra.com> <8563gcx1p1.fsf@agentultra.com> <0214c407$0$20647$c3e8da3@news.astraweb.com> Message-ID: <85zldj38nu.fsf@agentultra.com> Steven D'Aprano writes: > On Thu, 07 May 2009 13:28:10 -0400, J Kenneth King wrote: > >> Steven D'Aprano writes: >> >>> On Wed, 06 May 2009 09:48:51 -0400, J Kenneth King wrote: >>> >>>> Emile van Sebille writes: >>>> >>>>> On 5/5/2009 9:15 AM J Kenneth King said... >>>>> >>>>>> List comprehensions can make a reader of your code apprehensive >>>>>> because it can read like a run-on sentence and thus be difficult to >>>>>> parse. The Python documentation discourages their use and I believe >>>>>> for good reason. >>>>> >>>>> Can you provide a link for this? I'd like to see specifically what's >>>>> being discouraged, as I'd be surprised to find routine usage frowned >>>>> upon. >>>>> >>>>> Emile >>>> >>>> http://docs.python.org/tutorial/datastructures.html#nested-list- >>> comprehensions >>>> >>>> >>>> "If you?ve got the stomach for it, list comprehensions can be nested. >>>> They are a powerful tool but ? like all powerful tools ? they need to >>>> be used carefully, if at all." >>> >>> How does this discourage the use of list comprehensions? At most, it >>> warns that complicated list comps are tricky. Complicated *anything* >>> are tricky. >> >> They are tricky and need to be used carefully, *if at all*. >> >> IMO this means that if there's a way to do it without a nested list >> comprehension, then that solution should be preferred. > > Earlier, you claimed that list comps in general were discouraged: > > "List comprehensions can make a reader of your code apprehensive because > it can read like a run-on sentence and thus be difficult to parse. The > Python documentation discourages their use and I believe for good reason." Ooops. Typo. My bad. Had it been quoted earlier it would have saved a few posts for sure. Such is the Internet. :) > Emile said "I'd be surprised to find routine usage frowned upon", giving > you the opportunity to correct his (mis)understanding. You failed to do > so, which I took as meaning that you agreed that routine usage of simple > list comps were frowned upon. Now you're talking about *nested* list > comps. You started off (apparently) objecting to list comps in general, > because they "can" make readers apprehensive. Now you seem to be saying > that it's only the complicated, overly-dense ones which rightly make > readers apprehensive which you object to. I was rather confused by that. I use list comps all the time and said several times that I don't object to list comps, just nested ones. > I suppose we're making progress if we agree that the Python docs warn > against unnecessarily complicated nested list comps. Whether it > discourages as well as warns is a matter of interpretation. But there's > certainly no sign that list comps as a general technique are discouraged > just because overly-complicated list comps are tricky to read. The same > can be said about *any* piece of code. Well I did my best to make my interpretation clear. If the documentation says that nested list comps are difficult to read and should be used rarely, if at all, then I generally consider that "discouraging" their use. Meaning of course that in the right situation one may be able to justify their own use of a nested comp. However, for every day problems one wouldn't be encouraged to use them so liberally as some tend to do. So.. we have some sort of consensus then? This might be a rare phenomenon on Usenet... :) Cheers, J. From yahoo at al-got-rhythm.net Mon May 11 12:30:03 2009 From: yahoo at al-got-rhythm.net (jalanb3) Date: Mon, 11 May 2009 16:30:03 -0000 Subject: Can I get a value's name Message-ID: Context for this question arises from some recent code. In particular the "replace_line" method, which takes in a regexp to look for, and a replacement for when it matches. It is supposed to work for single lines only (we add ^ and $ to the regexp), so arguments which have '\n' in them are not accepted. So at start of the method we search for such bad chars, and the code ends up something like this: def replace_line(pattern,replacement): errors = '\n' in pattern and [ 'pattern' ] or [] errors += '\n' in replacement and [ 'replacement' ] or [] values = [ locals()[e] for e in errors ] # etc, etc, and eventually: print 'Argument %s is bad : "%s"' % (errors[0],values[0]) And the question arises from that locals() line: Given a variable name I can use locals() to get the value Is there a way to do it the other way round Given the value, can I get the variable name ? For example, suppose I had started like this (using the variables, not strings with their names) def replace_line(pattern,replacement): values = '\n' in pattern and [ pattern ] or [] values += '\n' in replacement and [ replacement ] or [] Can I later get the name "pattern" via values[0]? If this was an array in another language: Of course not, values[0] is a copy of the value so the connection to the variable is lost But, AFAIK, in Python values[0] is "just a rename" of pattern so there might be a way to get "through to" the original variable Thank you for reading this far. If you were now to start writing, I'd be very grateful indeed. -- Alan P.S. This is just curiosity - replace_lines() works great as is. From pje at telecommunity.com Mon May 11 12:35:58 2009 From: pje at telecommunity.com (P.J. Eby) Date: Mon, 11 May 2009 12:35:58 -0400 Subject: [Python-Dev] .pth files are evil In-Reply-To: <4A059639.7040505@v.loewis.de> References: <49D4DA72.60401@v.loewis.de> <20090407174355.B62983A4063@sparrow.telecommunity.com> <49E4A58F.70309@egenix.com> <20090414162603.70C843A4100@sparrow.telecommunity.com> <49E4F93B.6010802@egenix.com> <20090415003026.B0A783A4114@sparrow.telecommunity.com> <49E59202.6050809@egenix.com> <20090415144147.6845F3A4100@sparrow.telecommunity.com> <49E60832.8030806@egenix.com> <49FB22B5.3040507@simplistix.co.uk> <4A0590C4.1020904@v.loewis.de> <20090509143829.17F293A4080@sparrow.telecommunity.com> <4A059639.7040505@v.loewis.de> Message-ID: <20090511163321.984D53A4109@sparrow.telecommunity.com> At 04:42 PM 5/9/2009 +0200, Martin v. L?wis wrote: > >> If you always use --single-version-externally-managed with easy_install, > >> it will stop editing .pth files on installation. > > > > It's --multi-version (-m) that does that. > > --single-version-externally-managed is a "setup.py install" option. > > > > Both have the effect of not editing .pth files, but they do so in > > different ways. The "setup.py install" option causes it to install in a > > distutils-compatible layout, whereas --multi-version simply drops .egg > > files or directories in the target location and leaves it to the user > > (or the generated script wrappers) to add them to sys.path. > >Ah, ok. Is there also an easy_install invocation that unpacks the zip >file into some location of sys.path (which then wouldn't require >editing sys.path)? No; you'd have to use the -e option to easy_install to download and extract a source version of the package; then run that package's setup.py, e.g.: easy_install -eb /some/tmpdir SomeProject cd /some/tmpdir/someproject # subdir is always lowercased/normalized setup.py install --single-version-externally-managed --record=... I suspect that this is basically what pip is doing under the hood, as that would explain why it doesn't support .egg files. I previously posted code to the distutils-sig that was an .egg unpacker with appropriate renaming, though. It was untested, and assumes you already checked for collisions in the target directory, and that you're handling any uninstall manifest yourself. It could probably be modified to take a filter function, though, something like: def flatten_egg(egg_filename, extract_dir, filter=lambda s,d: d): eggbase = os.path.filename(egg_filename)+'-info' def file_filter(src, dst): if src.startswith('EGG-INFO/'): src = eggbase+s[8:] dst = os.path.join(extract_dir, *src.split('/')) return filter(src, dst) return unpack_archive(egg_filename, extract_dir, file_filter) Then you could pass in a None-returning filter function to check and accumulate collisions and generate a manifest. A second run with the default filter would do the unpacking. (This function should work with either .egg files or .egg directories as input, btw, since unpack_archive treats a directory input as if it were an archive.) Anyway, if you used "easy_install -mxd /some/tmpdir [specs]" to get your target eggs found/built, you could then run this flattening function (with appropriate filter functions) over the *.egg contents of /some/tmpdir to do the actual installation. (The reason for using -mxd instead of -Zmaxd or -zmaxd is that we don't care whether the eggs are zipped or not, and we leave out the -a so that dependencies already present on sys.path aren't copied or re-downloaded to the target; only dependencies we don't already have will get dropped in /some/tmpdir.) Of course, the devil of this is in the details; to handle conflicts and uninstalls properly you would need to know what namespace packages were in the eggs you are installing. But if you don't care about blindly overwriting things (as the distutils does not), then it's actually pretty easy to make such an unpacker. I mainly haven't made one myself because I *do* care about things being blindly overwritten. From norseman at hughes.net Mon May 11 12:51:48 2009 From: norseman at hughes.net (norseman) Date: Mon, 11 May 2009 09:51:48 -0700 Subject: Wrapping comments In-Reply-To: References: Message-ID: <4A0857A4.9090108@hughes.net> Tobias Weber wrote: > Hi, > the guideline (PEP 8) is hard wrap to 7x characters. The reason given is > that soft wrap makes code illegible. > > So what if you hard wrap code but let comments and docstrings soft-wrap? > > Otherwise it's hugely annoying to edit them. Say you remove the first > three words of a 150 character sentence. Either keep the ugly or rewrap > manually. > > Or are there editors that can do a "soft hard wrap" while keeping > indentation and #comment markers intact? > ======================= Paragraph 1: 65 and 72 cols are US typewriter standard 12 and 10 pt respectively. (MSDOS screen, business standard paper, ..) And yes, soft wrap does. Check the hardcopy which wraps code with lots of long lines. Paragraph 2: Comments? I vote no. These are in the code and should conform to helping at that location. Doc_stuff - I vote yes. For the obvious reason that it makes formating the Docs easier AND is to be 'extracted' to a separate file for that purpose in the first place. Paragraph 3: True Could you give a short example of what you are referring to in your last paragraph? I showed this to several friends and got several 'views' as to what is intended. I assume you are NOT intending to use third party programs to write code in but rather to use Python somehow? I assume you are intending that the editor add the backslash newline at appropriate places without causing a word or code break when needed and simply wrapping with indent without breaking the code the rest of the time and doing so in such a fashion that ALL general text editors will be able to display code properly as well as be usable in modifying code? Not to mention that the interpreter and/or compiler will still be able to use the file. Steve From norseman at hughes.net Mon May 11 13:06:28 2009 From: norseman at hughes.net (norseman) Date: Mon, 11 May 2009 10:06:28 -0700 Subject: Complete frustration In-Reply-To: References: Message-ID: <4A085B14.2060908@hughes.net> hellcats wrote: > I have Python2.5 installed on Windows XP. Whenever I double click on a > something.pyw file, IDLE launches and opens something.pyw in the > editor. I would prefer to actually *RUN* the program, not edit it. If > I want to edit it then I'll choose the "Edit with IDLE" context menu. > So I then have to press F5 to get the program to execute. Now I have > my program's windows along with two IDLE windows cluttering my screen > and task bar. WHAT IS GOING ON? I've tried changing the file > association to python.exe, and that works, but just ONCE (even if I > choose "always use the slected program to launch this kind of file"). > IDLE may be great and all, but I fricken' don't want to see it every > time I just want to run a python program!!!!! > -- > http://mail.python.org/mailman/listinfo/python-list > ------------------------ I agree completely. Check the Python-List archives. Look for a file that is dated July 8, 2008 (07/08/2008) that was sent by norseman. It works for me. Steve From agaskins_ng at kelleramerica.com Mon May 11 13:06:42 2009 From: agaskins_ng at kelleramerica.com (Adam Gaskins) Date: Mon, 11 May 2009 13:06:42 -0400 Subject: OOP & Abstract Classes References: <74c79941-af9b-4b43-b02b-8316b108f08d@l28g2000vba.googlegroups.com> Message-ID: Any idea why I didn't see this reply on my ng? I only see the reply from Marco. Can't help but wonder if there is more that is not getting through here. Would someone mind forwarding me any other replies? FWIW I'm using news.east.cox.net. Thanks, -Adam > Mike Driscoll wrote: > >> I've never used (or heard of) the Abstract type...and the guy who >> wrote the FAQ was being a jerk. From norseman at hughes.net Mon May 11 13:16:19 2009 From: norseman at hughes.net (norseman) Date: Mon, 11 May 2009 10:16:19 -0700 Subject: unicode bit me In-Reply-To: <0214c5da$0$20647$c3e8da3@news.astraweb.com> References: <0vadnQqkdrdQz5nXnZ2dnUVZ_uOdnZ2d@pdx.net> <0214c5da$0$20647$c3e8da3@news.astraweb.com> Message-ID: <4A085D63.7000605@hughes.net> Steven D'Aprano wrote: > On Fri, 08 May 2009 14:22:32 -0400, Terry Reedy wrote: > >> Scott David Daniels wrote: >> >>> It would be a bit easier if people would bother to mention their >>> Python version, as we regularly get questions from people running 2.3, >>> 2.4, 2.5, 2.6, 2.7a, 3.0, and 3.1b. They run computers with differing >>> operating systems and versions such as: Windows 2000, OS/X Leopard, >>> ubuntu Hardy Heron, SuSE, .... >> And if they copy and paste the actual error messages instead of saying >> 'It doesn't work' > > "I tried to copy and paste the actual error message, but it doesn't > work..." > > > *grin* > > ========================== In Linux get/use gpm and copy paste is simple. In Microsoft see: Python-List file dated May 6, 2009 (05/06/2009) sent by norseman. From Scott.Daniels at Acm.Org Mon May 11 13:18:18 2009 From: Scott.Daniels at Acm.Org (Scott David Daniels) Date: Mon, 11 May 2009 10:18:18 -0700 Subject: Can I get a value's name In-Reply-To: References: Message-ID: jalanb3 wrote: > ... Given a variable name I can use locals() to get the value > Is there a way to do it the other way round > Given the value, can I get the variable name ? (1) Yes you can in some cases. (2) You should not, things do not inherently have a name. With that prelude: def find_names(value, dictionary): for name, val in dictionary.items(): if val is value: # note: "is", not "==" yield name x = 123456 y = 123456 * 3 // 3 z = 123456.0 q = y print list(find_names(y, locals())) --Scott David Daniels Scott.Daniels at Acm.Org From chambon.pascal at wanadoo.fr Mon May 11 13:59:12 2009 From: chambon.pascal at wanadoo.fr (Pascal Chambon) Date: Mon, 11 May 2009 19:59:12 +0200 Subject: stand alone exec In-Reply-To: <805f59d50905102006obd4e42bo6b8923b62f84f6d6@mail.gmail.com> References: <805f59d50905102006obd4e42bo6b8923b62f84f6d6@mail.gmail.com> Message-ID: <4A086770.6060607@wanadoo.fr> Hello It sounds indeed like a runtime library problem... You should run a dependancy finder (like dependency walker - http://www.dependencywalker.com/) on your executable, and thus see what might be lacking on other systems. I know that on *nix systems there are tools to see more precisely what's missing, but on windows, exept that tool I don't know much. Regards, Pascal prakash jp a ?crit : > Hi all, > > I want to run dos commands through python stand alone execs. The > created Python stand alone executable (py2exe) works fine > > in my machine but on transferring the "dist" folder to other systems > the executable fails to run. > > I tried to copy the MSVCP90.dll in the "dist" folder. Also tried to > exclude the same dll in the options of the setup.py file > > The error reads as follows : > > "The application has failed to start because the application > configuration is incorrect. Reinstalling the application may fix this > problem". > > Details of the installed setup files may be useful : > > 1- python-2.6.1.msi > 2- py2exe-0.6.9.win32-py2.6.exe > 3- pywin32-212.win32-py2.6.exe > > Thanks in advance > > Regards > Prakash > ------------------------------------------------------------------------ > > -- > http://mail.python.org/mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From tim.arnold at sas.com Mon May 11 14:04:30 2009 From: tim.arnold at sas.com (Tim Arnold) Date: Mon, 11 May 2009 14:04:30 -0400 Subject: piping input to an external script Message-ID: Hi, I have some html files that I want to validate by using an external script 'validate'. The html files need a doctype header attached before validation. The files are in utf8 encoding. My code: --------------- import os,sys import codecs,subprocess HEADER = '' filename = 'mytest.html' fd = codecs.open(filename,'rb',encoding='utf8') s = HEADER + fd.read() fd.close() p = subprocess.Popen(['validate'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) validate = p.communicate(unicode(s,encoding='utf8')) print validate --------------- I get lots of lines like this: Error at line 1, character 66:\tillegal character number 0 etc etc. But I can give the command in a terminal 'cat mytest.html | validate' and get reasonable output. My subprocess code must be wrong, but I could use some help to see what the problem is. python2.5.1, freebsd6 thanks, --Tim From gert.cuykens at gmail.com Mon May 11 14:08:03 2009 From: gert.cuykens at gmail.com (gert) Date: Mon, 11 May 2009 11:08:03 -0700 (PDT) Subject: sqlite single transaction without foreign key or triggers Message-ID: I am trying to do this in a single transaction, the 3 separate statements work fine, but i am screwed if they are not executed together. ########### db.execute('BEGIN') ############# db.execute('UPDATE users SET uid=? WHERE uid=?',(v['uid'],s.UID)) db.execute('UPDATE sessions SET uid=? WHERE sid=?',(v['uid'],s.SID)) # only do this if there is no primary key conflict in the above if db.ERROR == None: db.execute('UPDATE groups SET uid=? WHERE uid=?', (v['uid'],s.UID)) ########### db.execute('END') ##################### My tables are as follows CREATE TABLE users ( uid VARCHAR(64) PRIMARY KEY, name VARCHAR(64) DEFAULT '', adress VARCHAR(64) DEFAULT '', city VARCHAR(64) DEFAULT '', country VARCHAR(64) DEFAULT '', phone VARCHAR(64) DEFAULT '', picture BLOB ); CREATE TABLE groups ( gid VARCHAR(64), uid VARCHAR(64), PRIMARY KEY(gid,uid), FOREIGN KEY(uid) REFERENCES users(uid) ON UPDATE CASCADE ON DELETE CASCADE ); CREATE TABLE sessions ( uid VARCHAR(64) UNIQUE, pwd VARCHAR(64) DEFAULT '', sid VARCHAR(64) PRIMARY KEY, exp DATETIME, FOREIGN KEY(uid) REFERENCES users(uid) ON UPDATE CASCADE ON DELETE CASCADE ); What is the python or sql way of doing this kind of things ? From samwyse at gmail.com Mon May 11 14:16:14 2009 From: samwyse at gmail.com (samwyse) Date: Mon, 11 May 2009 11:16:14 -0700 (PDT) Subject: creating classes with mix-ins Message-ID: I'm writing a class that derives it's functionality from mix-ins. Here's the code: def boilerplate(what): # This used to be a decorator, but all of the ##what = f.__name__ # function bodies turned out to be 'pass'. 'Validate the user, then call the appropriate plug-in.' def template(self, which, username, password, *args): if not self.security.isAuthorised(username, password, which, what): raise Exception('Unauthorised access') return getattr(self.blog, what)(which, *args) template.__name__ = what template.__doc__ = getattr(self.blog, what).__doc__ return template class MetaWeblog(object): def __init__(self, securityHandler=SimpleSecurityHandler, blogHandler=SimpleBlogHandler): self.security = securityHandler() self.blog = blogHandler() newPost = boilerplate('newPost') editPost = boilerplate('editPost') getPost = boilerplate('getPost') # etc, etc, etc I'd like to replace the method definitions with a loop: for what in attr_list: setattr(klass, what, boilerplate(what)) That begs the question of where I define 'klass' and 'attr_list'. Should I use a class decorator, or a metaclass? In favor of decorators is that I can see how to do it; in favor of a metaclass is that I get to learn how to use them. ;-) What are the other pros and cons for each choice? From research at johnohagan.com Mon May 11 14:21:58 2009 From: research at johnohagan.com (John O'Hagan) Date: Mon, 11 May 2009 18:21:58 +0000 Subject: Can I get a value's name In-Reply-To: References: Message-ID: <200905111821.59253.research@johnohagan.com> On Mon, 11 May 2009, jalanb3 wrote: [...] > > def replace_line(pattern,replacement): > errors = '\n' in pattern and [ 'pattern' ] or [] > errors += '\n' in replacement and [ 'replacement' ] or [] > values = [ locals()[e] for e in errors ] > # etc, etc, and eventually: > print 'Argument %s is bad : "%s"' % (errors[0],values[0]) > > And the question arises from that locals() line: > Given a variable name I can use locals() to get the value > Is there a way to do it the other way round > Given the value, can I get the variable name ? > > For example, suppose I had started like this (using the variables, not > strings with their names) > > def replace_line(pattern,replacement): > values = '\n' in pattern and [ pattern ] or [] > values += '\n' in replacement and [ replacement ] or [] > > Can I later get the name "pattern" via values[0]? > [...] def replace_line(pattern,replacement): values = '\n' in pattern and [ pattern ] or [] values += '\n' in replacement and [replacement] or [] loc=locals() print [i for i in loc if loc[i] is pattern if pattern in values] will print ['pattern'] if the value of pattern is in values (if there's a newline in pattern). Is that what you're after? HTH, John From arnodel at googlemail.com Mon May 11 14:25:15 2009 From: arnodel at googlemail.com (Arnaud Delobelle) Date: Mon, 11 May 2009 19:25:15 +0100 Subject: Wrapping comments References: Message-ID: Tobias Weber writes: > In article , > David Robinow wrote: > >> (define-key key-translation-map [?\M-3] "#") >> >> >> or, if you prefer, just be sick. > > Thanks, but I don't believe using releases from people who think I jump > should through hoops just to make my keyboard work is a good plan for a > relaxed future. According to their release notes this was (once) fixed > years ago. No need for editing .emacs. Click 'Options' on the menu bar, hover down to the 'Option Key >' item, then select the 'Meta & British' option. This is IMHO the best option (if you have a UK keyboard, of course!). This way, Alt-3 gives you a '#'. It would be a shame to abandon Aquamacs just for this, they have made a lot of efforts to make it integrate well with OSX and I think this is one of the few hurdles that are left. -- Arnaud From nick at craig-wood.com Mon May 11 14:30:04 2009 From: nick at craig-wood.com (Nick Craig-Wood) Date: Mon, 11 May 2009 13:30:04 -0500 Subject: OOP & Abstract Classes References: Message-ID: Adam Gaskins wrote: > I am a fairly seasoned PHP developer (don't shoot, I'm changing teams!:) who > is admittedly behind the curve with OOP. Like most who learned PHP, I > started doing web app backend stuff, but I have moved to full blown windows > apps in the last 6 months using Winbinder. As you may know Winbinder is > essentially abandoned, and programming windows apps with Winbinder or even > GTK-PHP is less than ideal. I also deal heavily in serial/rs-232 > communications which is a pain in PHP. Long story short, I'm tired of doing > things in such a hackish manner and want to write applications that are > cross platform (I'd like to get our production dept on linux eventually) and > truely object oriented. > > So I was beginning to learn OOP for PHP, and it seemed to me that abstract > classes were just right for my application. In my application I must > communicate with several peices of test equipment that communicate via > RS-232. Most use SCPI instructions, some do not and require low level > communication. > > The way I understand abstract classes is that I could have a class that has > all my abstract methods such as 'init', 'getMeasurement', 'setPressure', > etc... then I could use this common interface to to control my different > pieces of hardware (after I write a library for each of them). > > Is this a valid application for abstract classes? Or am I making life more > complicated than it need be? This sounds like a fine use for Abstract classes. Python doesn't have language support for them directly, but they are usually written like this class DeviceBase(object): def __init__(self): raise NotImplementedError() def getMeasusrement(self): raise NotImplementedError() def setPressure(self, pressure): raise NotImplementedError() Then subclass it for the real functionality class RealDevice(DeviceBase): def __init__(self): self.pressure = 0 def getMeasusrement(self): return 0 def setPressure(self, pressure): self.pressure = pressure However, I normally found that there is some functionality that is common to all subclasses so there may be some real methods in your DeviceBase class, eg class DeviceBase(object): def __init__(self, device): self.device = device def getMeasurement(self): raise NotImplementedError() def setPressure(self, pressure): raise NotImplementedError() def __repr__(self): return "%s(%r)" % (self.__class__.__name__, self.device) class RealDevice(DeviceBase): def __init__(self, device): DeviceBase.__init__(self, device) self.pressure = 0 def getMeasurement(self): return self.pressure def setPressure(self, pressure): self.pressure = pressure Which looks like this when you test it >>> base = DeviceBase("/dev/ttyS0") >>> base DeviceBase('/dev/ttyS0') >>> base.getMeasurement() Traceback (most recent call last): File "", line 1, in File "", line 5, in getMeasurement NotImplementedError >>> base.setPressure(14) Traceback (most recent call last): File "", line 1, in File "", line 7, in setPressure NotImplementedError >>> >>> real = RealDevice("/dev/ttyS1") >>> real RealDevice('/dev/ttyS1') >>> real.getMeasurement() 0 >>> real.setPressure(14) >>> real.getMeasurement() 14 >>> -- Nick Craig-Wood -- http://www.craig-wood.com/nick From google at mrabarnett.plus.com Mon May 11 14:38:03 2009 From: google at mrabarnett.plus.com (MRAB) Date: Mon, 11 May 2009 19:38:03 +0100 Subject: Wrapping comments In-Reply-To: <4A0857A4.9090108@hughes.net> References: <4A0857A4.9090108@hughes.net> Message-ID: <4A08708B.7000002@mrabarnett.plus.com> norseman wrote: > Tobias Weber wrote: >> Hi, >> the guideline (PEP 8) is hard wrap to 7x characters. The reason given >> is that soft wrap makes code illegible. >> >> So what if you hard wrap code but let comments and docstrings soft-wrap? >> >> Otherwise it's hugely annoying to edit them. Say you remove the first >> three words of a 150 character sentence. Either keep the ugly or >> rewrap manually. >> >> Or are there editors that can do a "soft hard wrap" while keeping >> indentation and #comment markers intact? >> > > ======================= > Paragraph 1: 65 and 72 cols are US typewriter standard 12 and 10 pt > respectively. (MSDOS screen, business standard paper, ..) > And yes, soft wrap does. Check the hardcopy which wraps > code with lots of long lines. > > Paragraph 2: Comments? I vote no. These are in the code and should > conform to helping at that location. > Doc_stuff - I vote yes. For the obvious reason that it > makes formating the Docs easier AND is to be 'extracted' > to a separate file for that purpose in the first place. > > Paragraph 3: True > > Could you give a short example of what you are referring to in your last > paragraph? I showed this to several friends and got several 'views' as > to what is intended. > I think he means something like: # 65 and 72 cols are US typewriter standard 12 and 10 pt respectively. when wrapped to 40 gives: # 65 and 72 cols are US typewriter # standard 12 and 10 pt respectively. and when rewrapped to 60 gives: # 65 and 72 cols are US typewriter standard 12 and 10 pt # respectively. Indentation of lines wouldn't be affected, so: # 65 and 72 cols are US typewriter standard 12 and 10 pt respectively. when wrapped to 40 gives: # 65 and 72 cols are US # typewriter standard 12 and 10 # pt respectively. and when rewrapped to 60 gives: # 65 and 72 cols are US typewriter standard 12 and # 10 pt respectively. > > I assume you are NOT intending to use third party programs to write code > in but rather to use Python somehow? > > I assume you are intending that the editor add the backslash newline at > appropriate places without causing a word or code break when needed and > simply wrapping with indent without breaking the code the rest of the > time and doing so in such a fashion that ALL general text editors will > be able to display code properly as well as be usable in modifying code? > Not to mention that the interpreter and/or compiler will still be able > to use the file. > From __peter__ at web.de Mon May 11 14:59:41 2009 From: __peter__ at web.de (Peter Otten) Date: Mon, 11 May 2009 20:59:41 +0200 Subject: OOP & Abstract Classes References: Message-ID: Adam Gaskins wrote: > So I was beginning to learn OOP for PHP, and it seemed to me that abstract > classes were just right for my application. In my application I must > communicate with several peices of test equipment that communicate via > RS-232. Most use SCPI instructions, some do not and require low level > communication. > > The way I understand abstract classes is that I could have a class that > has all my abstract methods such as 'init', 'getMeasurement', > 'setPressure', etc... then I could use this common interface to to control > my different pieces of hardware (after I write a library for each of > them). > > Is this a valid application for abstract classes? Or am I making life more > complicated than it need be? > > Now, I have read that Pythons OOP implimentation is so much better and > more complete then PHP's, but I cant find anything about abstract classes > except this: > http://norvig.com/python-iaq.html > > ...which says it doesn't exist in Python and you can only do this hack to > make it work (and I don't particularly understand how to impliment it from > this article). It is basically a non-implementaton. If you don't understand it you should not mess with abstract base classes and work your way through an introductory python textbook. That said, the page is outdated; Python 2.6 has some support for abstract classes. They even fail a bit earlier, when the class is instantiated instead of when the method is called. Example: from abc import ABCMeta, abstractmethod, abstractproperty class AbstractDevice: __metaclass__ = ABCMeta @abstractmethod def init(self): pass @abstractproperty def pressure(self): pass class Compressor(AbstractDevice): def init(self): print "starting compressor" def set_pressure(self, value): print "setting pressure to", value pressure = property(fset=set_pressure) c = Compressor() c.init() c.pressure = 42 a = AbstractDevice() # fails with TypeError > This makes me suspect I am making things more comlicated than they need to > be. Could someone help me understand the proper way to impliment a set > classes/methods to deal with several peices of similar and not so similar > hardware? Not looking for anyone to write code for me, just help > understanding some of these OOP concepts and how they would apply to my > situation. Personally I would just write the class I need class Compressor(object): def init(self): print "starting compressor" def set_pressure(self, value): print "setting pressure to", value pressure = property(fset=set_pressure) and forget about the bureaucracy. Instead I recommend that you learn about unit tests and write a few tests to ensure your classes work as specified. Peter From ken at seehart.com Mon May 11 15:11:22 2009 From: ken at seehart.com (Ken Seehart) Date: Mon, 11 May 2009 12:11:22 -0700 Subject: Can I get a value's name In-Reply-To: References: Message-ID: <4A08785A.6070906@seehart.com> jalanb3 wrote: > Context for this question arises from some recent code. In particular the "replace_line" method, which takes in a regexp to look for, and a replacement for when it matches. > > It is supposed to work for single lines only (we add ^ and $ to the regexp), so arguments which have '\n' in them are not accepted. > > So at start of the method we search for such bad chars, and the code ends up something like this: > > def replace_line(pattern,replacement): > errors = '\n' in pattern and [ 'pattern' ] or [] > errors += '\n' in replacement and [ 'replacement' ] or [] > values = [ locals()[e] for e in errors ] > # etc, etc, and eventually: > print 'Argument %s is bad : "%s"' % (errors[0],values[0]) > > And the question arises from that locals() line: > Given a variable name I can use locals() to get the value > Is there a way to do it the other way round > Given the value, can I get the variable name ? > > For example, suppose I had started like this (using the variables, not strings with their names) > > def replace_line(pattern,replacement): > values = '\n' in pattern and [ pattern ] or [] > values += '\n' in replacement and [ replacement ] or [] > > Can I later get the name "pattern" via values[0]? > > If this was an array in another language: > Of course not, values[0] is a copy of the value > so the connection to the variable is lost > But, AFAIK, in Python values[0] is "just a rename" of pattern > so there might be a way to get "through to" the original variable > No, values[0] gives a reference to the value stored at index 0 of the values array, which does not contain the name. In particular, the name and all other information about the expression itself is lost. A value generally does not contain the "pattern" or any kind of name information (although class, method, and function instances contain some symbolic information in them). Study the concept of 'references' and all of this will become more clear. The only way to do something like what you want would be to search globals() or locals() for the value (depending on the context), but that would be an extremely ugly hack. Chances are that if you want to do this, you need to rethink the problem instead. # horribly bad, but sort of working, code follows: foo = 'find me!' def find_var(x): '''returns the name associated with a value''' for k,v in globals().items(): if v == x: return k print find_var(foo) Note that a value can have several different names (and unnamed references) pointing at it, so the above code is not very general, but it may illustrate some interesting points. My guess is that your entire approach may need rethinking. What is the ultimate objective of your project? > Thank you for reading this far. > If you were now to start writing, I'd be very grateful indeed. > > From simon at brunningonline.net Mon May 11 15:23:08 2009 From: simon at brunningonline.net (Simon Brunning) Date: Mon, 11 May 2009 20:23:08 +0100 Subject: Wrapping comments In-Reply-To: References: Message-ID: <8c7f10c60905111223k34ef1130n21acc0cc39206fc5@mail.gmail.com> 2009/5/10 Tobias Weber : > (still not gonna use software that doesn't let me type # because it's > alt+3 on a UK layout; having to re-learn or configure that is just sick) To use Aquamacs with a UK keyboard, you want to select Options, Option Key, Meta & British. Things just work then. -- Cheers, Simon B. From cripplemeal at gmail.com Mon May 11 15:54:21 2009 From: cripplemeal at gmail.com (cripplemeal at gmail.com) Date: Mon, 11 May 2009 12:54:21 -0700 (PDT) Subject: Using Pygame with Python Message-ID: Hi. I would just like to know which of the versions of python and pygame would be best to download for use together. I am a windows xp user. From kyosohma at gmail.com Mon May 11 16:02:58 2009 From: kyosohma at gmail.com (Mike Driscoll) Date: Mon, 11 May 2009 13:02:58 -0700 (PDT) Subject: Writing text to a Word Document References: <8ba265c9-4697-41b4-a117-04761a86a945@g20g2000vba.googlegroups.com> Message-ID: <33867bab-9237-4a60-be14-46647522edd9@l28g2000vba.googlegroups.com> On May 11, 11:27?am, gazath... at gmail.com wrote: > Hi everyone, > > I am trying to write several attributes from a database table and > using the code below I can write the values however it is only > overwriting on the first line. > > I am new to the win32com bit and I would like to know what is the > recommended reference to loop down the page and add multiple records > rather than the worddoc.Content. > > I have read a little about writing this to an RTF or htmol but I am > getting information overload and going nowhere. ?Does anyone have a > nice tutorial out there that simply explains how to do this? > > Much appreciated for any thoughts. > > Cheers, > > Gareth > > import win32com.client, arcgisscripting, sys, os > gp = arcgisscripting.create(9.3) > > #Create word document component -http://www.faqts.com/knowledge_base/view.phtml/aid/37034/fid/244 > wordapp = win32com.client.Dispatch("Word.Application") # Create new > Word Object > wordapp.Visible = 1 # Word Application should`t be visible > worddoc = wordapp.Documents.Add() # Create new Document Object > worddoc.Content.Font.Size = 11 > worddoc.Content.Paragraphs.TabStops.Add (100) > > #Setup of GDB Connection > # Get the Featureclass from the Mobel - Property Box > PropBox = "C:\\Avon Fire Rescue\\data\AvonFire_1.gdb\\PropertyBox" > > # Make a search cursor for the Property Box Feature class > PBselCur = gp.searchcursor(PropBox) > PBrow = PBselCur.reset() > PBrow = PBselCur.next() > > #Using the search cursor get the Premise ID from a Property box > > while PBrow: > ? ? PBrowVal = PBrow.getvalue("PremisesID") > ? ? #Add data to Word Document > ? ? worddoc.Content.Text = (PBrowVal) > ? ? PBrow = PBselCur.next() > > #worddoc.Close() # Close the Word Document (a save-Dialog pops up) > > print "Fin" What you probably want is something like this: rng = worddoc.Range(0,0) while PBrow: rng.InsertAfter(PBrowVal + "\r\n") # needs return and new line character At least, that's what I found in one of my books. It seemed to work when I tested it in the interpreter. Mike From kyosohma at gmail.com Mon May 11 16:06:49 2009 From: kyosohma at gmail.com (Mike Driscoll) Date: Mon, 11 May 2009 13:06:49 -0700 (PDT) Subject: Using Pygame with Python References: Message-ID: <3efb2b2f-60ba-4d70-80de-7f032909cc3c@s28g2000vbp.googlegroups.com> On May 11, 2:54?pm, cripplem... at gmail.com wrote: > Hi. I would just like to know which of the versions of python and > pygame would be best to download for use together. I am a windows xp > user. Look at the pygame website to see what the newest version of Python it supports and go with that unless there are known issues listed. If you plan to make an executable of the game, then I recommend Python 2.5 since there are some known issues with 2.6 (don't know if that's true for 3.0 or not, but it's probable). - Mike From baudewijn.vermeire at skynet.be Mon May 11 16:09:15 2009 From: baudewijn.vermeire at skynet.be (bav) Date: Mon, 11 May 2009 22:09:15 +0200 Subject: how to consume .NET webservice Message-ID: <4a0885e2$0$2853$ba620e4c@news.skynet.be> question from a python newbie; how can i consume in python language, a .NET web service, passing a string array as parameter in some easy steps? best regards From ggardiner at iee.org Mon May 11 16:19:02 2009 From: ggardiner at iee.org (Geoff Gardiner) Date: Mon, 11 May 2009 21:19:02 +0100 Subject: How do I test the integrity of a Python installation in Debian and Ubuntu In-Reply-To: References: Message-ID: <4A088836.3080006@iee.org> Aahz wrote: > ... That seems to demonstrate that regrtest.py is indeed a good mechanism for > finding out whether it's a b0rked install! > I agree that regrtest.py looks a good mechanism. It just appears that `apt-get install python` on Debian and Ubuntu brings no tests with it. @Lawrence D'Oliveiro: `apt-get install debsums` does indeed show that the packages that it checks have matching checksums. `debsums [-a] python` checks all sorts of /usr/share/doc/python files while `debsums [-a] python2.5` checks /usr/share/doc/python2.5, /usr/lib/python2.5 and others as well. Although that wasn't what I was originally looking for it has been helpful. Thank you for your suggestions. All the best, Geoff From namekuseijin at gmail.com Mon May 11 16:25:03 2009 From: namekuseijin at gmail.com (namekuseijin) Date: Mon, 11 May 2009 17:25:03 -0300 Subject: how to consume .NET webservice In-Reply-To: <4a0885e2$0$2853$ba620e4c@news.skynet.be> References: <4a0885e2$0$2853$ba620e4c@news.skynet.be> Message-ID: bav escreveu: > question from a python newbie; > > how can i consume in python language, a .NET web service, passing > a string array as parameter in some easy steps? Unless Microsoft extended the standard in any way, then it should be just as you consume any web service, I guess. ;) -- a game sig: http://tinyurl.com/d3rxz9 From kyosohma at gmail.com Mon May 11 16:29:16 2009 From: kyosohma at gmail.com (Mike Driscoll) Date: Mon, 11 May 2009 13:29:16 -0700 (PDT) Subject: how to consume .NET webservice References: <4a0885e2$0$2853$ba620e4c@news.skynet.be> Message-ID: On May 11, 3:09?pm, "bav" wrote: > question from a python newbie; > > ? how can i consume in python language, a .NET web service, passing > ? a string array as parameter in some easy steps? > > best regards You're being pretty vague here. Try using Google first...I got plenty of hits with "python .net web service". This one sounds good: http://www.beardygeek.com/2009/01/how-to-call-a-net-webservice-using-python/ It sounds like you consume it much like you do with other web services...find the API and use Python to access it or create a parser of some sort. Mike From agaskins_ng at kelleramerica.com Mon May 11 16:34:02 2009 From: agaskins_ng at kelleramerica.com (Adam Gaskins) Date: Mon, 11 May 2009 16:34:02 -0400 Subject: OOP & Abstract Classes References: Message-ID: Wow, thanks Nick! This is just what I was looking for! Thanks to Peter as well. And as for your suggestion that I probably shouldn't mess with things I don't understand and learn the basics first... well, that is probably sound advice, but I figured out years ago that I learn things best by a) getting in over my head b) panicking a little c) sorting it all out, and fianlly d) (in the case of programming) refactoring to make it all proper later. It may be bss aackwards but it works for me :-) I am going to work through these examples in the morning, thanks again guys! From tadwelessar at gmail.com Mon May 11 16:39:26 2009 From: tadwelessar at gmail.com (Matteo) Date: Mon, 11 May 2009 13:39:26 -0700 (PDT) Subject: Fill Javascript form Message-ID: <3f9c74cf-72f4-45e2-8724-3939366d13bf@e24g2000vbe.googlegroups.com> Hi everybody, I have to fill a web form to authenticate and connect to the internet. I thought it would have been easy to make a script to do that automatically on startup. Unfortunately, it turned out that the form is written in JavaScript, and urllib2 therefore fails to even fetch the form. The form itself is very simple, just name and password fields and a "submit" button. Do you know of any workaround? From rogeeff at gmail.com Mon May 11 16:39:41 2009 From: rogeeff at gmail.com (rogeeff at gmail.com) Date: Mon, 11 May 2009 13:39:41 -0700 (PDT) Subject: How to replace constructor with factory method Message-ID: <630cbc45-9809-45c8-addd-c229cd2d07d9@e23g2000vbe.googlegroups.com> Hi, Just wonder if it's possible in Python. what I want is to tweak an existing Python class A with no constructor, so that A() results in fuctory method call? so o = A() instead being equivalent to: s = object() A.__init__(s) o = s becomes: o = my_factory_function( A ) Thanks, Gennadiy From gabriel at opensuse.org Mon May 11 16:40:57 2009 From: gabriel at opensuse.org (Gabriel) Date: Mon, 11 May 2009 17:40:57 -0300 Subject: issue with twisted and reactor. Can't stop reactor Message-ID: <1adde6890905111340o7ca2b19er762a76518eac9d05@mail.gmail.com> Hello all!, I'm trying to implement a simple one way communication using twisted. Sender: > send message > close connection Receiver: > receive > do something > wait for other message I'm testing with this simple examples: Sender: [code] class SenderClient(protocol.Protocol): def __init__(self, data): self.data = data def connectionMade(self): self.transport.write(self.data) def dataReceived(self, data): self.transport.loseConnection() def connectionLost(self, reason): pass class SenderFactory(protocol.ClientFactory): def __init__(self, data): self.data = data def buildProtocol(self, addr): return SenderClient(self.data) def clientConnectionFailed(self, connector, reason): logger.error("Connection failed. Reason %s" % reason) reactor.stop() def clientConnectionLost(self, connector, reason): reactor.stop() class Sender(object): def __init__(self, host='localhost', port=61610): self.host = host self.port = port def send(self, data): reactor.connectTCP(self.host, self.port, SenderFactory(data)) reactor.run() [/code] Receiver: [code] from twisted.internet import reactor, protocol class Receiver(protocol.Protocol): def dataReceived(self, data): self.transport.write("success") print data def main(): factory = protocol.ServerFactory() factory.protocol = Receiver reactor.listenTCP(61610,factory) reactor.run() if __name__ == '__main__': main() [/code] When I call send the first time it works fine, when I call send a second time the sender hangs. If I hit ctrl+c it throws: [quote] .../twisted/internet/base.py", line 527, in stop "Can't stop reactor that isn't running.") twisted.internet.error.ReactorNotRunning: Can't stop reactor that isn't running. [/quote] Any idea why this is happening? King regards. From sam at tregar.com Mon May 11 16:44:08 2009 From: sam at tregar.com (Sam Tregar) Date: Mon, 11 May 2009 16:44:08 -0400 Subject: Your Favorite Python Book Message-ID: Greetings. I'm working on learning Python and I'm looking for good books to read. I'm almost done with Dive into Python and I liked it a lot. I found Programming Python a little dry the last time I looked at it, but I'm more motivated now so I might return to it. What's your favorite? Why? -sam -------------- next part -------------- An HTML attachment was scrubbed... URL: From Shawn at Milochik.com Mon May 11 17:00:21 2009 From: Shawn at Milochik.com (Shawn Milochik) Date: Mon, 11 May 2009 17:00:21 -0400 Subject: Your Favorite Python Book In-Reply-To: References: Message-ID: <2dc0c81b0905111400k4b3876cdj80f500d30ff8660d@mail.gmail.com> It depends on what you want to do. If you still want to beef up on general knowledge, maybe skim through "The Python Cookbook" or something reference-like. If you feel ready to start doing something with Python, look into one of the recent titles that applies Python for a specific purpose. Examples: "Gray Hat Python" (debugging, reverse engineering) "Natural Language Processing with Python" (pre-order at this time) Any up-to-date (version 1.0 and up) Django book (if you're into Web development) "Expert Python Programming" (best practices) "Beginning Python Visualization" (presenting data visually) "Programming Collective Intelligence" (extracting valuable information from public data sources, and more) Shawn From Shawn at Milochik.com Mon May 11 17:06:52 2009 From: Shawn at Milochik.com (Shawn Milochik) Date: Mon, 11 May 2009 17:06:52 -0400 Subject: Fill Javascript form In-Reply-To: <3f9c74cf-72f4-45e2-8724-3939366d13bf@e24g2000vbe.googlegroups.com> References: <3f9c74cf-72f4-45e2-8724-3939366d13bf@e24g2000vbe.googlegroups.com> Message-ID: <2dc0c81b0905111406u6be0bb11nc5a0cb239dce909e@mail.gmail.com> How is the form "written in JavaScript"? Is it dynamically generated? In any case, can you just send a POST request if you know the values required? From rtw at freenet.co.uk Mon May 11 17:07:42 2009 From: rtw at freenet.co.uk (Rob Williscroft) Date: Mon, 11 May 2009 16:07:42 -0500 Subject: sqlite single transaction without foreign key or triggers References: Message-ID: gert wrote in news:d7591495-4661-4243-ad7e-f142d8244e88 @e24g2000vbe.googlegroups.com in comp.lang.python: > I am trying to do this in a single transaction, the 3 separate > statements work fine, but i am screwed if they are not executed > together. Well you're in luck, Python DBAPI 2 connections handle this for you, you do need to call commit() on the connection though. The default, for DBAPI 2 connections, is that all "work" occurs in a transaction (if the DB actually supports transactions) so you have to call commit() on the connection after doing updates. > > ########### db.execute('BEGIN') ############# > db.execute('UPDATE users SET uid=? WHERE uid=?',(v['uid'],s.UID)) This is a fragile way to do it, your code won't work with a DB that has real foreign keys (and maybe sqlite will get them one day). A less fragile way of doing it is: db = connection.cursor() # First copy the row if it exists db.execute( ''' insert into "users" select ?, "name", adress, city, country, phone, picture from "users" where "uid" = ? ''', (v['uid'],s.UID) ) > db.execute('UPDATE sessions SET uid=? WHERE sid=?',(v['uid'],s.SID)) # Second update foriegn key tables to point to the new row # (but only if the new row exists ) db.execute( ''' update "sessions" set "uid" = ? where "uid" = ? and exists( select * from "users" where "uid" = ? ) ''', (v['uid'],s.SID, v['uid']) ) #Do the same for the "groups" table, then # finally delete the original row (again only if the new row exists ) db.execute( ''' delete from "users" where "uid" = ? and exists( select * from "users" where "uid" = ? ) ''', (s.SID, v['uid']) ) # Finally commit the transaction connection.commit() > # only do this if there is no primary key conflict in the above > if db.ERROR == None: db.execute('UPDATE groups SET uid=? WHERE uid=?', > (v['uid'],s.UID)) Python reports errors by throwing exceptions, so if you needed somthing like this it would more likely be: try: ... # somthing that errors up ... catch sqlite3.DatabaseError: connection.rollback() Rob. -- http://www.victim-prime.dsl.pipex.com/ From clp2 at rebertia.com Mon May 11 17:08:13 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Mon, 11 May 2009 14:08:13 -0700 Subject: How to replace constructor with factory method In-Reply-To: <630cbc45-9809-45c8-addd-c229cd2d07d9@e23g2000vbe.googlegroups.com> References: <630cbc45-9809-45c8-addd-c229cd2d07d9@e23g2000vbe.googlegroups.com> Message-ID: <50697b2c0905111408n41450ecek34ec24cbe439254f@mail.gmail.com> On Mon, May 11, 2009 at 1:39 PM, wrote: > Hi, > > Just wonder if it's possible in Python. > > what I want is to tweak an existing Python class A with no > constructor, so that A() results in fuctory method call? > > so o = A() instead being equivalent to: > > s = object() > A.__init__(s) > o = s > > becomes: > > o = my_factory_function( A ) I believe you'd need to override __new__ for that. Docs on __new__: http://docs.python.org/reference/datamodel.html#object.__new__ Cheers, Chris -- http://blog.rebertia.com From usernet at ilthio.net Mon May 11 17:12:30 2009 From: usernet at ilthio.net (Tim Harig) Date: Mon, 11 May 2009 21:12:30 GMT Subject: [silly] Re: issue with twisted and reactor. Can't stop reactor References: Message-ID: <2x0Ol.29775$YU2.5233@nlpi066.nbdc.sbc.com> On 2009-05-11, Gabriel wrote: > Subject: issue with twisted and reactor. Can't stop reactor Not having written anything using twisted I cannot help you much with your code; but, I cannot resist commenting about your subject line: I suspect that if are having an issue with your reactor after being hit by a twister[d], then you probably have more at issue then your networking. My suggestion would be to run or drive away quickly. From rumpf_a at web.de Mon May 11 17:19:01 2009 From: rumpf_a at web.de (rumpf_a at web.de) Date: Mon, 11 May 2009 14:19:01 -0700 (PDT) Subject: Nimrod programming language References: Message-ID: <2cf5a78f-87da-4ace-b895-6c8283302b22@o20g2000vbh.googlegroups.com> On 10 Mai, 07:36, k... at fiber-space.de wrote: > On 8 Mai, 17:48, Andreas Rumpf wrote: > > > Dear Python-users, > > > I invented a new programming language called "Nimrod" that combines Python's readability with C's performance. Please check it out:http://force7.de/nimrod/ > > Any feedback is appreciated. > > > Regards, > > Andreas Rumpf > > > ______________________________________________________ > > GRATIS f?r alle WEB.DE-Nutzer: Die maxdome Movie-FLAT! > > Jetzt freischalten unterhttp://movieflat.web.de > > On a first impression it looks quite pleasant to me, your new > language! Two obvious questions on a Python list. > > 1) How to interface with Nimrod from Python? > 2) Lack of typed hash-tables. Is this one of those particular features > that are planned to be implemented in version 0.80 and summarized > under "generic containers"? 1) Via a wrapper that is in the works. It will work like embedding Python into a C application. 2) Yes. Some syntactic sugar will probably be provided, e.g. a constructor {} like Python. However, I have not made my mind completely: Maybe they should be built-in? This would allow more optimizations. From kyrie at uh.cu Mon May 11 17:26:49 2009 From: kyrie at uh.cu (Luis Zarrabeitia) Date: Mon, 11 May 2009 17:26:49 -0400 Subject: How to replace constructor with factory method In-Reply-To: <630cbc45-9809-45c8-addd-c229cd2d07d9@e23g2000vbe.googlegroups.com> References: <630cbc45-9809-45c8-addd-c229cd2d07d9@e23g2000vbe.googlegroups.com> Message-ID: <200905111726.49772.kyrie@uh.cu> On Monday 11 May 2009 04:39:41 pm rogeeff at gmail.com wrote: > so o = A() instead being equivalent to: > > s = object() > A.__init__(s) > o = s Actually, it would be more like this: s = A.__new__(A) if isinstance(s,A): A.__init__(s) o = s > o = my_factory_function( A ) You could tweak: *) A's constructor (__new__) [keep in mind the 'insinstance' part] *) A's initializer (changing the type, and so on... ugly, fragile and dangerous, don't ever do it!) *) A's metaclass (the instantiation happens in the metaclass' __call__ method, you could rewrite it to suit your needs, as in [1] *) or, just use a method named A instead of the A class (as the multiprocessing.Queue function does) I would use the 4th option. The fact that python classes are callable allows you to switch from instantiation to function calling without having to change the user's code. [1] http://trucosos.crv.matcom.uh.cu/snippets/95/ -- Luis Zarrabeitia (aka Kyrie) Fac. de Matem?tica y Computaci?n, UH. http://profesores.matcom.uh.cu/~kyrie From samwyse at gmail.com Mon May 11 17:44:03 2009 From: samwyse at gmail.com (samwyse) Date: Mon, 11 May 2009 14:44:03 -0700 (PDT) Subject: creating classes with mix-ins References: Message-ID: <09108020-503e-44ae-acee-943374300e18@e24g2000vbe.googlegroups.com> On May 11, 1:16?pm, samwyse wrote: > I'm writing a class that derives it's functionality from mix-ins. While waiting, I gave a try at using class decorators. Here's what I came up with: def add_methods(*m_list, **kwds): def wrapper(klass): for m_name in m_list: def template(self, which, username, password, *args): if not self.security.isAuthorised(username, password, which, m_name): raise Exception('Unauthorised access') return getattr(self.blog, m_name)(which, *args) dotted_name = kwds.get('prefix', '') + m_name template.__name__ = dotted_name template.__doc__ = dotted_name setattr(klass, dotted_name, template) return klass return wrapper @add_methods('newPost', 'editPost', 'getPost', 'newMediaObject', 'getCategories', 'getRecentPosts', prefix='metaWeblog.') class MetaWeblog(object): def __init__(self, securityHandler=SimpleSecurityHandler, blogHandler=SimpleBlogHandler): self.security = securityHandler() self.blog = blogHandler() if __name__ == '__main__': server = SimpleXMLRPCServer(("localhost", 8080)) server.register_instance(MetaWeblog()) server.register_introspection_functions() server.serve_forever() The problem that I'm having is that when I call newPost, SimpleBlogHandler.getRecentPosts gets invoked. Apparently add_methods isn't generating unique templates. I think I need to move 'template' into another function, but I'm starting to wonder if metaclasses might work better. Any ideas? From clp2 at rebertia.com Mon May 11 17:45:37 2009 From: clp2 at rebertia.com (Chris Rebert) Date: Mon, 11 May 2009 14:45:37 -0700 Subject: Your Favorite Python Book In-Reply-To: References: Message-ID: <50697b2c0905111445w40f53bdkdb7dd731ce77a919@mail.gmail.com> On Mon, May 11, 2009 at 1:44 PM, Sam Tregar wrote: > Greetings.? I'm working on learning Python and I'm looking for good books to > read.? I'm almost done with Dive into Python and I liked it a lot. I found > Programming Python a little dry the last time I looked at it, but I'm more > motivated now so I might return to it.? What's your favorite?? Why? I like "Python in a Nutshell" as a reference book, although it's now slightly outdated given Python 3.0's release (the book is circa 2.5). Cheers, Chris -- http://blog.rebertia.com From python at bdurham.com Mon May 11 17:52:41 2009 From: python at bdurham.com (python at bdurham.com) Date: Mon, 11 May 2009 17:52:41 -0400 Subject: Your Favorite Python Book In-Reply-To: <50697b2c0905111445w40f53bdkdb7dd731ce77a919@mail.gmail.com> References: <50697b2c0905111445w40f53bdkdb7dd731ce77a919@mail.gmail.com> Message-ID: <1242078761.3662.1314940065@webmail.messagingengine.com> Sam, In no specific order (I brought them all): Wesley Chun's "Core Python Programming" David Mertz's "Text Processing in Python" (older, but excellent) Mark Lutz's "Learning Python" All highly recommended. Best of luck on your Python journey! Regards, Malcolm From rumpf_a at web.de Mon May 11 17:55:46 2009 From: rumpf_a at web.de (rumpf_a at web.de) Date: Mon, 11 May 2009 14:55:46 -0700 (PDT) Subject: Nimrod programming language References: <7xd4ah73l3.fsf@ruckus.brouhaha.com> Message-ID: <57f4c81a-3537-49fa-a5f6-a0cc0d43d599@o14g2000vbo.googlegroups.com> On 10 Mai, 10:40, Paul Rubin wrote: > Andreas Rumpf writes: > > I invented a new programming language called "Nimrod" that combines > > Python's readability with C's performance. Please check it out: > >http://force7.de/nimrod/Any feedback is appreciated. > > Looks nice in many ways. ?You also know about PyPy and Felix > (felix.sf.net)? > Nimrod has very different goals from PyPy, I cannot comment on Felix. > It seems a little bit hackish that the character type is a byte. > Python did something like that but it ended up causing a lot of > hassles when dealing with real Unicode. ?I think you have to bite > the bullet and have separate byte and char types, where char=unicode. > I am dissatisfied with Python's (or Java's) Unicode handling: 1) IO overhead to convert UTF-8 (defacto standard on UNIX) into UTF-16. 2) For simple tasks the output file will be in the same encoding as the input file automatically. 3) Most text files have no header specifying the encoding anyway. How should the programm/programmer know? And often he does not need to know anyway. 4) UTF-16 (or 32) use more memory. 5) In particular using UTF-16 internally does not make sense: It just encourages programmers to ignore surrogates. Unicode has more than 2^16 characters nowadays. > It scares me a little about how there are references that can have > naked pointers, escape the gc, etc. ?It would be good if the type > system were powerful enough to guarantee the absence of these effects > in some piece of data, unless the data is somehow marked with an > "unsafe" type. > Well "ptr" is unsafe, "ref" is not; mixing the two requires a "cast". Basically "cast" is the only unsafe feature. > I'd like to see ML-like algebraic types with pattern matching, not > just tuple unpacking. ? > Me too. > The generic system should be able to analyze the AST of type > parameters, e.g. iterator would be able to automatically generate > an iterator over a list, a tree, or some other type of structure, by > actually figuring out at compile time how T is constructed. > Interesting idea. Right now the generic iterator is named "items" and can be overloaded for user-defined types. > It's sort of surprising that the compiler is written in a Pascal > subset. ?Why not implement in Nimrod directly, and bootstrap through C? > That is already the case: The Pascal version is translated to Nimrod and than compiles itself. Bootstrapping works. > The language and library are missing arbitrary precision integer > arithmetic, using GMP or something like that. > True, but currently not a high priority for me. > It would be good to also be able to have value serialization and > deserialization to both human readable (nested lists, etc) and binary > (for IPC) formats. > Yes. Planned for version 0.9.0. > It's not obvious from the blurbs whether simple functional programming > is supported, e.g. how would you implement a function like "map" or > "filter"? ?What would the types be of those two functions? > proc map[T, S](data: openarray[T], op: proc(x: T): S): seq[S] = result = @[] for d in items(data): result.add(op(d)) Or - if you don't want the "openarray[T]" restriction: proc map[T, S, U](data: T, op: proc(x: U): S): seq[S] = result = @[] for d in items(data): result.add(op(d)) > The roadmap says you might implement threading with transactional > memory. ?How will the transactional memory interact with mutable data? > Are you going to use OS threads, lightweight threads, or both? Probably both. Lightweight threads could be done using coroutines and explicit task switching. OS threads with transactional memory. The plan is to use LLVM as a backend and perhaps http://tinystm.org/. Of course, this is only wishful thinking. ;-) From toby at tobiah.org Mon May 11 17:59:43 2009 From: toby at tobiah.org (Tobiah) Date: Mon, 11 May 2009 14:59:43 -0700 Subject: There may be a much better way to manage artillery. References: <_5JNl.4895$qP.3696@newsfe03.iad> Message-ID: On Mon, 11 May 2009 00:48:25 +0100, Rhodri James wrote: > On Mon, 11 May 2009 00:06:34 +0100, Tobiah wrote: > > [Snippety snip] > >> I wanted the bullets to be responsible for destroying themselves, but a >> little Googling brought me to points about dangling references and how >> an object is not allowed (nor does it seem to have the means) to destroy >> itself. That's why I made this wrapper class for all of the bullets. > > An object can, however, erase its representation from the screen and tell > whatever control system is keeping track of objects to forget about it. > Once the last reference to the object is deleted, the object will be > garbage collected. > > What framework are you writing your game in? Pygame has facilities for > handling this sort of thing fairly straightforwardly. Actually I am using Pygame, although I'm quite new to it. Which module has the facilities that you describe? Thanks, Toby From exarkun at divmod.com Mon May 11 18:09:19 2009 From: exarkun at divmod.com (Jean-Paul Calderone) Date: Mon, 11 May 2009 18:09:19 -0400 Subject: issue with twisted and reactor. Can't stop reactor In-Reply-To: <1adde6890905111340o7ca2b19er762a76518eac9d05@mail.gmail.com> Message-ID: <20090511220919.21531.119723250.divmod.quotient.19655@henry.divmod.com> On Mon, 11 May 2009 17:40:57 -0300, Gabriel wrote: >Hello all!, > >I'm trying to implement a simple one way communication using twisted. > > [snip] > >When I call send the first time it works fine, when I call send a >second time the sender hangs. > [snip] None of the reactors in Twisted are restartable. You can run and stop them once. After you've stopped a reactor, you cannot run it again. This is the cause of your problem. Jean-Paul From rumpf_a at web.de Mon May 11 18:26:14 2009 From: rumpf_a at web.de (rumpf_a at web.de) Date: Mon, 11 May 2009 15:26:14 -0700 (PDT) Subject: Nimrod programming language References: <159308160@web.de> Message-ID: > One question I ask myself upon seeing a new language is if it is possible > to program amb (amb=ambiguous) operator in it. This page gives a very > nice, "code first" explanation of amb and how it is supposed to work: > > http://www.randomhacks.net/articles/2005/10/11/amb-operator > Hm. I am not sure either. To me, it looks like a constraint solver by using brute force. It seems to require continuations. AFAIK continuations are extremely hard to implement efficiently, so probably it won't be done in Nimrod. From rhodri at wildebst.demon.co.uk Mon May 11 18:39:51 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Mon, 11 May 2009 23:39:51 +0100 Subject: There may be a much better way to manage artillery. In-Reply-To: References: <_5JNl.4895$qP.3696@newsfe03.iad> Message-ID: On Mon, 11 May 2009 22:59:43 +0100, Tobiah wrote: > On Mon, 11 May 2009 00:48:25 +0100, Rhodri James wrote: > >> On Mon, 11 May 2009 00:06:34 +0100, Tobiah wrote: >> >> [Snippety snip] >> >>> I wanted the bullets to be responsible for destroying themselves, but a >>> little Googling brought me to points about dangling references and how >>> an object is not allowed (nor does it seem to have the means) to >>> destroy >>> itself. That's why I made this wrapper class for all of the bullets. >> >> An object can, however, erase its representation from the screen and >> tell >> whatever control system is keeping track of objects to forget about it. >> Once the last reference to the object is deleted, the object will be >> garbage collected. >> >> What framework are you writing your game in? Pygame has facilities for >> handling this sort of thing fairly straightforwardly. > > Actually I am using Pygame, although I'm quite new to it. Which > module has the facilities that you describe? Looking at the code, it's not quite as straightforward as I was making out! I'm used to writing games using a veneer library which has a Screen class that handles all the basic drawing functions and the main processing loop. It keeps a list of objects, and is responsible for calling appropriate methods on those objects when things happen. The Object class has a destroy() method which erases the object and asks the Screen to remove it from its internal list. Needless to say there is a lot of list copying going on in the Screen class main loop to make sure that lists aren't corrupted as they are being iterated through. This code was written back in the days of Python 2.1, and hasn't had more than simple bug-fixing done to it since. We're contemplating a rewrite when we shift to Python 3, and we're putting that decision off as long as possible :-) You're welcome to use or scavenge from it, as it's on a fairly free license. It's available here: http://www.livewires.co.uk/python/home (though apparently I must go and bug the webmaster about putting the latest version of the worksheets up. What's there at the moment is years out of date!) -- Rhodri James *-* Wildebeeste Herder to the Masses From deets at nospam.web.de Mon May 11 18:40:05 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Tue, 12 May 2009 00:40:05 +0200 Subject: how to consume .NET webservice In-Reply-To: References: <4a0885e2$0$2853$ba620e4c@news.skynet.be> Message-ID: <76rnq5F1dp6gpU1@mid.uni-berlin.de> namekuseijin schrieb: > bav escreveu: >> question from a python newbie; >> >> how can i consume in python language, a .NET web service, passing >> a string array as parameter in some easy steps? > > Unless Microsoft extended the standard in any way, then it should be > just as you consume any web service, I guess. ;) Microsoft *created* the standard.. .and how feasible it is to work with it, see this : http://72.249.21.88/nonintersecting/?year=2006&monthnum=11&day=15&name=the-s-stands-for-simple&page= Diez From mensanator at aol.com Mon May 11 18:40:49 2009 From: mensanator at aol.com (Mensanator) Date: Mon, 11 May 2009 15:40:49 -0700 (PDT) Subject: Nimrod programming language References: <7xd4ah73l3.fsf@ruckus.brouhaha.com> <57f4c81a-3537-49fa-a5f6-a0cc0d43d599@o14g2000vbo.googlegroups.com> Message-ID: <4519cc9d-f765-4f55-81b2-58abfa20b988@p4g2000vba.googlegroups.com> On May 11, 4:55?pm, rump... at web.de wrote: > On 10 Mai, 10:40, Paul Rubin wrote:> Andreas Rumpf writes: > > > I invented a new programming language called "Nimrod" that combines > > > Python's readability with C's performance. Please check it out: > > >http://force7.de/nimrod/Anyfeedback is appreciated. > > > Looks nice in many ways. ?You also know about PyPy and Felix > > (felix.sf.net)? > > Nimrod has very different goals from PyPy, I cannot comment on Felix. > > > It seems a little bit hackish that the character type is a byte. > > Python did something like that but it ended up causing a lot of > > hassles when dealing with real Unicode. ?I think you have to bite > > the bullet and have separate byte and char types, where char=unicode. > > I am dissatisfied with Python's (or Java's) Unicode handling: > 1) IO overhead to convert UTF-8 (defacto standard on UNIX) into > UTF-16. > 2) For simple tasks the output file will be in the same encoding as > the input file automatically. > 3) Most text files have no header specifying the encoding anyway. How > should the programm/programmer know? And often he does not need to > know anyway. > 4) UTF-16 (or 32) use more memory. > 5) In particular using UTF-16 internally does not make sense: It just > encourages programmers to ignore surrogates. Unicode has more than > 2^16 characters nowadays. > > > It scares me a little about how there are references that can have > > naked pointers, escape the gc, etc. ?It would be good if the type > > system were powerful enough to guarantee the absence of these effects > > in some piece of data, unless the data is somehow marked with an > > "unsafe" type. > > Well "ptr" is unsafe, "ref" is not; mixing the two requires a "cast". > Basically "cast" is the only unsafe feature. > > > I'd like to see ML-like algebraic types with pattern matching, not > > just tuple unpacking. ? > > Me too. > > > The generic system should be able to analyze the AST of type > > parameters, e.g. iterator would be able to automatically generate > > an iterator over a list, a tree, or some other type of structure, by > > actually figuring out at compile time how T is constructed. > > Interesting idea. Right now the generic iterator is named "items" and > can be overloaded for user-defined types. > > > It's sort of surprising that the compiler is written in a Pascal > > subset. ?Why not implement in Nimrod directly, and bootstrap through C? > > That is already the case: The Pascal version is translated to Nimrod > and than compiles itself. Bootstrapping works. > > > The language and library are missing arbitrary precision integer > > arithmetic, using GMP or something like that. > > True, but currently not a high priority for me. Too bad. That makes Nimrod officially "worthless", i.e., of no value to me. > > > It would be good to also be able to have value serialization and > > deserialization to both human readable (nested lists, etc) and binary > > (for IPC) formats. > > Yes. Planned for version 0.9.0. > > > It's not obvious from the blurbs whether simple functional programming > > is supported, e.g. how would you implement a function like "map" or > > "filter"? ?What would the types be of those two functions? > > proc map[T, S](data: openarray[T], op: proc(x: T): S): seq[S] = > ? result = @[] > ? for d in items(data): result.add(op(d)) > > Or - if you don't want the "openarray[T]" restriction: > proc map[T, S, U](data: T, op: proc(x: U): S): seq[S] = > ? result = @[] > ? for d in items(data): result.add(op(d)) > > > The roadmap says you might implement threading with transactional > > memory. ?How will the transactional memory interact with mutable data? > > Are you going to use OS threads, lightweight threads, or both? > > Probably both. Lightweight threads could be done using coroutines and > explicit task switching. OS threads with transactional memory. The > plan is to use LLVM as a backend and perhapshttp://tinystm.org/. Of > course, this is only wishful thinking. ;-) From ljqyhvrl at glvn84004389.net Mon May 11 18:50:59 2009 From: ljqyhvrl at glvn84004389.net (Raoul Gough) Date: Mon, 11 May 2009 23:50:59 +0100 Subject: MacPython 3.0.1 installation problem, no /usr/local/bin/python* References: Message-ID: Ned Deily writes: > In article > , > Benjamin Kaplan wrote: >> On Sat, May 9, 2009 at 4:30 AM, Raoul Gough >> wrote: [snip] >> > So did something go wrong with the installer, or is it all >> > supposed to work somehow differently? >> >> Because Python 3 breaks compatibility with Python 2, the installers >> don't install it as the default. The executable should be at >> /usr/local/bin/python3.0 > > By default, the 3.0.1 installer does not install any links in > /usr/local/bin. To do so, run or re-run the installer, click > Customize, and then select the "UNIX command-line tools" package. > That creates /usr/local/python3.0 and /usr/local/python links, among > others. Ah, I see! Thanks, I missed the "Customize" option during the install dialogues. That takes care of the symlinks in /usr/local/bin, although I notice that it still leaves some dangling symlinks under /Library/Frameworks/Python.framework that point to a nonexistent "Current" directory. I guess it's safe to ignore these. -- Raoul Gough. From gabriel at opensuse.org Mon May 11 18:51:50 2009 From: gabriel at opensuse.org (Gabriel) Date: Mon, 11 May 2009 19:51:50 -0300 Subject: issue with twisted and reactor. Can't stop reactor In-Reply-To: <20090511220919.21531.119723250.divmod.quotient.19655@henry.divmod.com> References: <20090511220919.21531.119723250.divmod.quotient.19655@henry.divmod.com> Message-ID: <4A08AC06.6030708@opensuse.org> Jean-Paul Calderone escribi?: > None of the reactors in Twisted are restartable. You can run and stop them > once. After you've stopped a reactor, you cannot run it again. This is > the > cause of your problem. > > Jean-Paul I see. Is it possible to do what I want using twisted? or I should found another way? -- Kind regards. From rhodri at wildebst.demon.co.uk Mon May 11 18:53:20 2009 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Mon, 11 May 2009 23:53:20 +0100 Subject: Wrapping comments In-Reply-To: References: Message-ID: On Mon, 11 May 2009 08:39:48 +0100, Tobias Weber wrote: > In article , > "Rhodri James" wrote: > >> What on earth are you talking about? '#' has its own key on a UK layout > > Not on Apple keyboards, and the emacs release in question is Mac only. My commiserations. That was a bad design decision on so many levels. -- Rhodri James *-* Wildebeeste Herder to the Masses From gert.cuykens at gmail.com Mon May 11 18:59:49 2009 From: gert.cuykens at gmail.com (gert) Date: Mon, 11 May 2009 15:59:49 -0700 (PDT) Subject: sqlite single transaction without foreign key or triggers References: Message-ID: <86fd1d41-2e7d-4b91-a9fc-8a9f4c05e119@e20g2000vbc.googlegroups.com> On 11 mei, 23:07, Rob Williscroft wrote: > gert wrote in news:d7591495-4661-4243-ad7e-f142d8244e88 > @e24g2000vbe.googlegroups.com in comp.lang.python: > > > I am trying to do this in a single transaction, the 3 separate > > statements work fine, but i am screwed if they are not executed > > together. > > Well you're in luck, Python DBAPI 2 connections handle this > for you, you do need to call commit() on the connection though. > > The default, for DBAPI 2 connections, is that all "work" occurs in a > transaction (if the DB actually supports transactions) so you have to > call commit() on the connection after doing updates. > > > > > ?########### db.execute('BEGIN') ############# > > ?db.execute('UPDATE users SET uid=? WHERE uid=?',(v['uid'],s.UID)) > > This is a fragile way to do it, your code won't work with a DB that > has real foreign keys (and maybe sqlite will get them one day). > > A less fragile way of doing it is: > > db = connection.cursor() > > # First copy the row if it exists > > db.execute( ''' > ? ? ? ? insert into "users" > ? ? ? ? select ?, "name", adress, city, country, phone, picture > ? ? ? ? from "users" where "uid" = ? ? ? ? ? > ? ? ''', (v['uid'],s.UID) > ? ) > > > ?db.execute('UPDATE sessions SET uid=? WHERE sid=?',(v['uid'],s.SID)) > > # Second update foriegn key tables to point to the new row > # (but only if the new row exists ) > > db.execute( ''' > ? ? ? ? update "sessions" set "uid" = ? > ? ? ? ? where "uid" = ? > ? ? ? ? and exists( > ? ? ? ? ? ? ? ? select * from "users" where "uid" = ? > ? ? ? ? ? ) > ? ? ''', > ? ? (v['uid'],s.SID, v['uid']) > ? ) > > #Do the same for the "groups" table, then > > # finally delete the original row (again only if the new row exists ) > > db.execute( ''' > ? ? ? ? delete from "users" > ? ? ? ? where "uid" = ? > ? ? ? ? and exists( > ? ? ? ? ? ? ? ? select * from "users" where "uid" = ? > ? ? ? ? ? ) > ? ? ''', > ? ? (s.SID, v['uid']) > ? ) > > # Finally commit the transaction > > connection.commit() > > > ?# only do this if there is no primary key conflict in the above > > ?if db.ERROR == None: db.execute('UPDATE groups SET uid=? WHERE uid=?', > > (v['uid'],s.UID)) > > Python reports errors by throwing exceptions, so if you needed somthing > like this it would more likely be: > > try: > > ? ... # somthing that errors up ... > > catch sqlite3.DatabaseError: > ? connection.rollback() > > Rob. > --http://www.victim-prime.dsl.pipex.com/ ok go it, thanks From Shawn at Milochik.com Mon May 11 19:35:44 2009 From: Shawn at Milochik.com (Shawn Milochik) Date: Mon, 11 May 2009 19:35:44 -0400 Subject: Your Favorite Python Book In-Reply-To: <1242078761.3662.1314940065@webmail.messagingengine.com> References: <50697b2c0905111445w40f53bdkdb7dd731ce77a919@mail.gmail.com> <1242078761.3662.1314940065@webmail.messagingengine.com> Message-ID: <2dc0c81b0905111635x5a598d8bh74e4f7fa76e20b33@mail.gmail.com> On Mon, May 11, 2009 at 5:52 PM, wrote: > Sam, > > In no specific order (I brought them all): > > Wesley Chun's "Core Python Programming" > David Mertz's "Text Processing in Python" (older, but excellent) > Mark Lutz's "Learning Python" > > All highly recommended. > > Best of luck on your Python journey! > > Regards, > Malcolm > -- > http://mail.python.org/mailman/listinfo/python-list > I second the Wesley Chun recommendation wholeheartedly. Also, "Text Processing in Python" is available for free online. http://gnosis.cx/TPiP/ Shawn From http Mon May 11 19:45:58 2009 From: http (Paul Rubin) Date: 11 May 2009 16:45:58 -0700 Subject: Nimrod programming language References: <7xd4ah73l3.fsf@ruckus.brouhaha.com> <57f4c81a-3537-49fa-a5f6-a0cc0d43d599@o14g2000vbo.googlegroups.com> Message-ID: <7x4ovrkxu1.fsf@ruckus.brouhaha.com> rumpf_a at web.de writes: > I am dissatisfied with Python's (or Java's) Unicode handling: > 1) IO overhead to convert UTF-8 (defacto standard on UNIX) into > UTF-16. So use UTF-8 internally. You can still iterate through strings efficiently. Random access would take a performance hit. When that's an issue, the programmer can choose to use a char array (char = UCS-4). The same generic functions could largely handle both types. > That is already the case: The Pascal version is translated to Nimrod > and than compiles itself. Bootstrapping works. I meant why not get rid of the translation step and implement the compiler in idiomatic Nimrod. > Or - if you don't want the "openarray[T]" restriction: > proc map[T, S, U](data: T, op: proc(x: U): S): seq[S] = > result = @[] > for d in items(data): result.add(op(d)) This is hard for me to comprehend but I mayb look at the docs to try to figure it out. > The plan is to use LLVM as a backend and perhaps > http://tinystm.org/. Of course, this is only wishful thinking. ;-) Oh cool, I didn't know about tinystm. But, my question about mutability is how you protect STM transactions when other threads can have references into shared mutable structures and clobber the referents. GHC uses its type system to prevent that, but I don't see how Nimrod can really do the same, especially without GHC's rich set of immutable types including stuff like immutable maps. Have you ever looked at Tim Sweeney's presentation "The Next Mainstream Programming Language"? It's at: http://www.st.cs.uni-saarland.de/edu/seminare/2005/advanced-fp/docs/sweeny.pdf From tjreedy at udel.edu Mon May 11 20:04:37 2009 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 11 May 2009 20:04:37 -0400 Subject: OOP & Abstract Classes In-Reply-To: References: Message-ID: Adam Gaskins wrote: > Wow, thanks Nick! This is just what I was looking for! I agree that Nick's semi-abstract class is what you need. After doing some subclasses, you may discover that there is more common code that you can factor out and push to the base class. You may also find it convenient to derive a mock subclass that returns simulated data so you can test user code initially without involving (or waiting on) real devices. From rtomek at ceti.com.pl Mon May 11 20:10:06 2009 From: rtomek at ceti.com.pl (Tomasz Rola) Date: Tue, 12 May 2009 02:10:06 +0200 Subject: Nimrod programming language In-Reply-To: References: <159308160@web.de> Message-ID: On Mon, 11 May 2009, rumpf_a at web.de wrote: > > One question I ask myself upon seeing a new language is if it is possible > > to program amb (amb=ambiguous) operator in it. This page gives a very > > nice, "code first" explanation of amb and how it is supposed to work: > > > > http://www.randomhacks.net/articles/2005/10/11/amb-operator > > > Hm. I am not sure either. To me, it looks like a constraint solver by > using brute force. Yes, kind of. From what I understand [1], it's goal is to simulate nondeterministic "get right answer in one try", only this "one try" requires browsing the solution space (because we have only "classic" hardware). It is easy to use amb in wrong way, but I can see some cases when I would like it. [1] John McCarthy's paper still waits to be read by me, so... Ok, I have just had a look at it and it seems that Ruby implementation is a bit primitive compared to original concept. But still, this is what would be needed in most real life cases, I suppose. I think ability to define amb would allow me to write more concise code. As far as I can tell, this kind of stuff plays more and more important role in my programing. > It seems to require continuations. AFAIK > continuations are extremely hard to implement efficiently, so probably > it won't be done in Nimrod. Pity, a little. But not really big problem for me. Nimrod may be interesting anyway, time will tell :-). Regards, Tomasz Rola -- ** A C programmer asked whether computer had Buddha's nature. ** ** As the answer, master did "rm -rif" on the programmer's home ** ** directory. And then the C programmer became enlightened... ** ** ** ** Tomasz Rola mailto:tomasz_rola at bigfoot.com ** From namekuseijin.nospam at gmail.com Mon May 11 20:27:56 2009 From: namekuseijin.nospam at gmail.com (namekuseijin) Date: Mon, 11 May 2009 21:27:56 -0300 Subject: how to consume .NET webservice In-Reply-To: <76rnq5F1dp6gpU1@mid.uni-berlin.de> References: <4a0885e2$0$2853$ba620e4c@news.skynet.be> <76rnq5F1dp6gpU1@mid.uni-berlin.de> Message-ID: Diez B. Roggisch wrote: > namekuseijin schrieb: >> bav escreveu: >>> question from a python newbie; >>> >>> how can i consume in python language, a .NET web service, passing >>> a string array as parameter in some easy steps? >> >> Unless Microsoft extended the standard in any way, then it should be >> just as you consume any web service, I guess. ;) > > Microsoft *created* the standard.. No: http://en.wikipedia.org/wiki/Web_service It's a W3C internet standard. Microsoft is one of many software companies behind the Consortium. Here's the relevant official standards: http://www.w3.org/2002/ws/#documents From davea at ieee.org Mon May 11 20:30:37 2009 From: davea at ieee.org (Dave Angel) Date: Mon, 11 May 2009 20:30:37 -0400 Subject: Complete frustration In-Reply-To: <4A085B14.2060908@hughes.net> References: <4A085B14.2060908@hughes.net> Message-ID: <4A08C32D.3020904@ieee.org> norseman wrote: >
hellcats > wrote: >> I have Python2.5 installed on Windows XP. Whenever I double click on a >> something.pyw file, IDLE launches and opens something.pyw in the >> editor. I would prefer to actually *RUN* the program, not edit it. If >> I want to edit it then I'll choose the "Edit with IDLE" context menu. >> So I then have to press F5 to get the program to execute. Now I have >> my program's windows along with two IDLE windows cluttering my screen >> and task bar. WHAT IS GOING ON? I've tried changing the file >> association to python.exe, and that works, but just ONCE (even if I >> choose "always use the slected program to launch this kind of file"). >> IDLE may be great and all, but I fricken' don't want to see it every >> time I just want to run a python program!!!!! >> -- >> http://mail.python.org/mailman/listinfo/python-list >> > ------------------------ > I agree completely. Check the Python-List archives. Look for a file > that is dated July 8, 2008 (07/08/2008) that was sent by norseman. > > It works for me. > > > Steve > >
For a Unix person, you sure act like you know Windows. But changing the PATH environment variable affects a command typed in the shell, but does not affect what happens when you either type the name of a python script directly, or when you double-click on such a file. That's controlled by file associations. (PATH isn't the answer in Unix either, most shells use the shebang at the beginning of the script.) File associations can be controlled in a number of ways. OPEN-WITH is one way. REGEDIT is another. In fact, any program can modify these settings, including your own scripts. Something is changing things back on you. But once you're having the problem of things reverting "automatically," a command line tool is sometimes the easiest. Two built-in commands (they're inside the cmd.exe shell) can help. They're ASSOC and FTYPE Since the extension you're bothered by is .pyw, try the following: C:> assoc .pyw .pyw=Python.NoConFile C:> ftype Python.NoConFile Python.NoConFile="C:\ProgFiles\Python26\pythonw.exe" "%1" %* Type them once and copy/paste results somewhere. Then fix the problem, and look again. Check that they work by double-clicking a xxx.pyw file in Explorer. Now, run other things, especially IDLE, and recheck these two commands to see if something changed. If it did, then blame whatever program you just ran. My guess is that it changed when some Python installation was done. And some programs have a habit of continually resetting their own associations. Sometimes this is configurable, sometimes you just have to stop using the brain-dead program. Notes: your path is unlikely to be the same as mine, so replace the C:\ProgFiles... string as appropriate to your preferred Python version location. Note you want Pythonw.exe, or you'll wind up with an extra shell window. You can do the same thing for the .py extension, which should wind up running python.exe. And you can make a simple two-line batch file to set these (assoc/ftype). If you had several such batch files you could change the default python very easily. Unlike environment variables, these associations are global, across all users, across all applications. So changes made in one shell affect any other shells, as well as the Explorer windows. Final note: once you've got these right, you can run a script in the obvious way (like Unix). myprogram.py parm1 parm2 will actually run python.exe myprogram.py parm1 parm2 And if you add a .PY and .PYW to PATHEXT environment variable, you can just run myprogram parm1 parm2 without any extension on the script name. This should normally be done in the Control Panel->Environment vars so it'll work in multiple DOS boxes, same as all environment variable changes. From ldo at geek-central.gen.new_zealand Mon May 11 20:37:20 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Tue, 12 May 2009 12:37:20 +1200 Subject: Nimrod programming language References: <7xd4ah73l3.fsf@ruckus.brouhaha.com> <57f4c81a-3537-49fa-a5f6-a0cc0d43d599@o14g2000vbo.googlegroups.com> Message-ID: In message <57f4c81a-3537-49fa-a5f6- a0cc0d43d599 at o14g2000vbo.googlegroups.com>, rumpf_a at web.de wrote: > I am dissatisfied with Python's (or Java's) Unicode handling: > 1) IO overhead to convert UTF-8 (defacto standard on UNIX) into > UTF-16. Are you sure they're using UTF-16? I would use UCS-2 or UCS-4. From ldo at geek-central.gen.new_zealand Mon May 11 20:44:57 2009 From: ldo at geek-central.gen.new_zealand (Lawrence D'Oliveiro) Date: Tue, 12 May 2009 12:44:57 +1200 Subject: How do I test the integrity of a Python installation in Debian and Ubuntu References: Message-ID: In message , Geoff Gardiner wrote: > @Lawrence D'Oliveiro: > ... I see that you published my unobfuscated e-mail address on USENET for all to see. I obfuscated it for a reason, to keep the spammers away. I'm assuming this was a momentary lapse of judgement, for which I expect an apology. Otherwise, it becomes grounds for an abuse complaint to your ISP. From socyl at 987jk.com.invalid Mon May 11 21:55:51 2009 From: socyl at 987jk.com.invalid (kj) Date: Tue, 12 May 2009 01:55:51 +0000 (UTC) Subject: OS X: How to play .wav file w/Python? Message-ID: Hi. I'm trying to learn how to play a .wav file in OS X with Python. I tried the following, which ran without errors, but produced nothing audible (even though the file bell.wav plays perfectly well otherwise, e.g. view the Finder's Preview): import pygame.mixer pygame.mixer.init() pygame.mixer.Sound("bell.wav").play print "done" What am I doing wrong? TIA! kynn -- NOTE: In my address everything before the first period is backwards; and the last period, and everything after it, should be discarded. From pavlovevidence at gmail.com Mon May 11 22:01:13 2009 From: pavlovevidence at gmail.com (Carl Banks) Date: Mon, 11 May 2009 19:01:13 -0700 (PDT) Subject: creating classes with mix-ins References: Message-ID: On May 11, 11:16?am, samwyse wrote: > I'm writing a class that derives it's functionality from mix-ins. > Here's the code: > > ? ? def boilerplate(what): ? # This used to be a decorator, but all of > the > ? ? ? ? ##what = f.__name__ ?# function bodies turned out to be > 'pass'. > ? ? ? ? 'Validate the user, then call the appropriate plug-in.' > ? ? ? ? def template(self, which, username, password, *args): > ? ? ? ? ? ? if not self.security.isAuthorised(username, password, > which, what): > ? ? ? ? ? ? ? ? raise Exception('Unauthorised access') > ? ? ? ? ? ? return getattr(self.blog, what)(which, *args) > ? ? ? ? template.__name__ = what > ? ? ? ? template.__doc__ = getattr(self.blog, what).__doc__ > ? ? ? ? return template > > ? ? class MetaWeblog(object): > ? ? ? ? def __init__(self, > ? ? ? ? ? ? ? ? ? ? ?securityHandler=SimpleSecurityHandler, > ? ? ? ? ? ? ? ? ? ? ?blogHandler=SimpleBlogHandler): > ? ? ? ? ? ? self.security = securityHandler() > ? ? ? ? ? ? self.blog = blogHandler() > ? ? ? ? newPost = boilerplate('newPost') > ? ? ? ? editPost = boilerplate('editPost') > ? ? ? ? getPost = boilerplate('getPost') > ? ? ? ? # etc, etc, etc > > I'd like to replace the method definitions with a loop: > ? ? ? ? for what in attr_list: > ? ? ? ? ? ? setattr(klass, what, boilerplate(what)) > > That begs the question of where I define 'klass' and 'attr_list'. > Should I use a class decorator, or a metaclass? Here's the thing: unless you have advance knowledge of the methods defined by self.blog, you can't get the attr_list at class definition time, which means neither the metaclass nor the decorator would be a good approach. If that's the case, you should define newPost, editPost, and whatever other methods of self.blog as ordinary attributes of self, within the init function. boilerplate would be the same except you would pass self to it and allow template to use it from its nested scope (it would no longer be an instance method since it's an ordinary attribute). If you do know what the methods of self.blog will be, then that's where you get attr_list from. So, for instance, if blogHandler always returns an object of type Blog, then you could inspect Blog's type dict to see what methods are defined in it; in fact you probably want to check the whole MRO for Blog, like this (untested): attr_list = [] for cls in Blog.__mro__: for value in cls.__dict__: if is_wrapped_method(value): attr_list.append(value) A metaclass is probably overkill to assign the wrapped blog methods. I probably wouldn't even bother with the decorator, and just write the loop after the class definition. Then you can use MetaBlog directly for klass. class MetaBlog(object): ... for what in attr_list: setattr(MetaBlog, what, boilerplate(what)) If it were the kind of thing I found myself doing often I'd refactor into a decorator. Carl Banks From debl2NoSpam at verizon.net Mon May 11 22:22:56 2009 From: debl2NoSpam at verizon.net (David Lees) Date: Tue, 12 May 2009 02:22:56 GMT Subject: Unable to install Pywin32 for Python 2.6.2 Message-ID: <445Ol.724$5F2.395@nwrddc01.gnilink.net> I have no problem installing Python 2.6.2 for windows under XP SP3 and IDLE and the command line versions work fine. When I run the pywin32 installer downloaded from sourceforge (pywin32-212.win32-py2.6.exe) I get the following error message: Traceback (most recent call last): File "", line 565, in File "", line 291, in install ImportError: Module use of python25.dll conflicts with this version of Python. *** run_installscript: internal error 0xFFFFFFFF *** I have tried uninstalling Python 2.6 and reinstalling, but still get the same message. I do have Python 2.5.4 and its associated Pywin32 on the same machine, but I have a laptop with pywin32 installed for both python 2.5 and 2.6. TIA David Lees From wuwei23 at gmail.com Mon May 11 22:47:29 2009 From: wuwei23 at gmail.com (alex23) Date: Mon, 11 May 2009 19:47:29 -0700 (PDT) Subject: OS X: How to play .wav file w/Python? References: Message-ID: On May 12, 11:55?am, kj wrote: > import pygame.mixer > > pygame.mixer.init() > pygame.mixer.Sound("bell.wav").play > print "done" > > What am I doing wrong? Your first mistake is not pasting here the traceback you received. That always makes it easier to assist with problems like this. However, my guess is it's this line: > pygame.mixer.Sound("bell.wav").play 'play' is a method, which means you need to call it. Try the following instead: > pygame.mixer.Sound("bell.wav").play() And let us know how that goes :) From aahz at pythoncraft.com Mon May 11 22:50:08 2009 From: aahz at pythoncraft.com (Aahz) Date: 11 May 2009 19:50:08 -0700 Subject: sqlite single transaction without foreign key or triggers References: Message-ID: In article , Rob Williscroft wrote: > >db.execute( ''' > update "sessions" set "uid" = ? > where "uid" = ? > and exists( > select * from "users" where "uid" = ? > ) > ''', > (v['uid'],s.SID, v['uid']) > ) This will be more efficient if you do "select uid from users". -- Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/ "It is easier to optimize correct code than to correct optimized code." --Bill Harlan From jenn.duerr at gmail.com Mon May 11 23:06:43 2009 From: jenn.duerr at gmail.com (noydb) Date: Mon, 11 May 2009 20:06:43 -0700 (PDT) Subject: putting date strings in order Message-ID: <2e52f7ed-c7e6-47f8-8891-f2f7ecc33cf1@x6g2000vbg.googlegroups.com> All, How best to go about this? >> I have a list containing strings referring to months, like ["x_apr", "x_jul", "x_jan", "x_aug", "x_may", etc] -- always using the 3-chars for month. The list will contain all 12 months, however the starting month may not necessarily be jan. I want to order the list chronologically, but starting with the user-defined starting month. So a list could be [x_aug, x_sep, .... x_jul]. The list might just start off in any random order, but I need to ensure, in the end, that the list is in chronological order and starting with the proper month per user needs. Anyone have any good ideas? I was curious to see what people came up with. From wuwei23 at gmail.com Mon May 11 23:13:02 2009 From: wuwei23 at gmail.com (alex23) Date: Mon, 11 May 2009 20:13:02 -0700 (PDT) Subject: OOP & Abstract Classes References: <74c79941-af9b-4b43-b02b-8316b108f08d@l28g2000vba.googlegroups.com> Message-ID: <83d2209a-8540-4cbb-8766-64d86b350314@c18g2000prh.googlegroups.com> On May 12, 1:22?am, Mike Driscoll wrote: > I've never used (or heard of) the Abstract type...and the guy who > wrote the FAQ was being a jerk. It looks like he was just throwing in > an undefined variable name just to make his Python program break while > taking a pot shot at people who use that sort of thing. Whatever. The IAQ's entry on Abstract classes predates the introduction of Abstract Base Classes to Python 2.6/3.0. Rather than being a "jerk", the example shows how to produce similar behaviour without code AS WELL as demonstrating a more extended approach. (You're not breaking your program if the behaviour is intentional...) > Hopefully someone who has used Abstract classes will jump in here and > give you more information about whether or not they matter in Python. Abstract classes are one mechanism for defining interfaces, but as we've covered it's not the only approach that can be taken. I've always been happy defining abstract methods using 'raise NotImplementedError', but clearly the ABC approach is perceived as having value otherwise it would never have been introduced into 2.6/3.0. From yahoo at al-got-rhythm.net Mon May 11 23:15:50 2009 From: yahoo at al-got-rhythm.net (Alan Brogan) Date: Tue, 12 May 2009 04:15:50 +0100 Subject: Can I get a value's name In-Reply-To: <4A08785A.6070906@seehart.com> References: <4A08785A.6070906@seehart.com> Message-ID: <12a6d6600905112015u174d4c4fg703913e4b1d220fd@mail.gmail.com> Thank you Ken for your help answer On Mon, May 11, 2009 at 8:11 PM, Ken Seehart wrote: > jalanb3 wrote: >> >> def replace_line(pattern,replacement): >> ? ?values = '\n' in pattern and [ pattern ] or [] >> ? ?values += '\n' in replacement and [ replacement ] or [] >> >> Can I later get the name "pattern" via values[0]? >> > ?A value generally does not contain the > "pattern" or any kind of name information (although class, method, and > function instances contain some symbolic information in them). > That's why I took the question seriously at all: It was an entirely different project - pulling info out of tracebacks into logs. And I can get names from a reference for classes, modules, ... Why not variables ? > Study the concept of 'references' and all of this will become more clear. I shall dive into that koan :-) > The only way to do something like what you want would be to search globals() > or locals() for the value (depending on the context), but that would be an > extremely ugly hack. Agreed > Chances are that if you want to do this, you need to > rethink the problem instead. > That does seem reasonable, but before I started writing Python it would never occurred to me to add some attributes on to an object whose definition I've never seen. So - unlikely, but I wouldn't rule it out. > Note that a value can have several different names (and unnamed references) > pointing at it, Ah - there's the rub ! All it takes (from my earlier example) is fred = pattern values += [ fred ] Now which is "the real name" of values[0]? In my example I would have wanted "pattern", and now whatever ugly hack we used to get at "the name" is turning into an complex module. > so the above code is not very general, but it may illustrate > some interesting points. Thanks - it does indeed. > My guess is that your entire approach may need rethinking. ?What is the > ultimate objective of your project? Find out what some other Pythonista have thought about this problem OTOH: the actual objective was to remove the redundancy of using the string "pattern" when I already have a perfectly good eponymous variable. On balance I think allowing a little bit of redundancy is prudent in this case :-) From Gforrest.yang at gmail.com Mon May 11 23:28:13 2009 From: Gforrest.yang at gmail.com (forrest yang) Date: Mon, 11 May 2009 20:28:13 -0700 (PDT) Subject: dict would be very slow for big data Message-ID: hi i am trying to insert a lot of data into a dict, which may be 10,000,000 level. after inserting 100000 unit, the insert rate become very slow, 50,000/ s, and the entire time used for this task would be very long,also. would anyone know some solution for this case? thanks From http Mon May 11 23:30:17 2009 From: http (Paul Rubin) Date: 11 May 2009 20:30:17 -0700 Subject: putting date strings in order References: <2e52f7ed-c7e6-47f8-8891-f2f7ecc33cf1@x6g2000vbg.googlegroups.com> Message-ID: <7x8wl3c81i.fsf@ruckus.brouhaha.com> noydb writes: > Anyone have any good ideas? I was curious to see what people came up > with. Is this a homework assignment? Some hints: 1) figure out how to compare two month names for chronological order, leaving out the issue of the starting month not being january. 2) figure out how to adjust for the starting month. The exact semantics of the "%" operator might help do this concisely. From kai.diefenbach at googlemail.com Mon May 11 23:35:04 2009 From: kai.diefenbach at googlemail.com (Kai Diefenbach) Date: Mon, 11 May 2009 20:35:04 -0700 (PDT) Subject: ANN: Demo online shop for LFS Message-ID: <9f40e0d7-d1f2-474b-89b5-7ec8dd14b78d@z5g2000vba.googlegroups.com> Hi, I'm pleased to announce that there is new demo shop for LFS: http://demo.getlfs.com LFS is an online shop based on Django and distributed under the BSD- License For more information please visit http://www.getlfs.com, subscribe to our feed http://www.getlfs.com/feeds/news or follow us on Twitter http://twitter.com/lfsproject. Thanks K?i From showell30 at yahoo.com Tue May 12 00:07:44 2009 From: showell30 at yahoo.com (Steve Howell) Date: Mon, 11 May 2009 21:07:44 -0700 (PDT) Subject: dict would be very slow for big data References: Message-ID: <7d5e78ae-2ef8-4260-b389-4660d6aff139@t10g2000vbg.googlegroups.com> On May 11, 8:28?pm, forrest yang wrote: > hi > i am trying to insert a lot of data into a dict, which may be > 10,000,000 level. > after inserting 100000 unit, the insert rate become very slow, 50,000/ > s, and the entire time used for this task would be very long,also. > would anyone know some solution for this case? > Are you running out of memory? What are your keys? Are you able to gather any more specific data about the slowdown--do all operations slow down equally or are there spurts of slowness? From gagsl-py2 at yahoo.com.ar Tue May 12 00:14:43 2009 From: gagsl-py2 at yahoo.com.ar (Gabriel Genellina) Date: Tue, 12 May 2009 01:14:43 -0300 Subject: web access through vpn client References: <0aa2beb9-b2b1-4bba-b130-e88821b914e8@d19g2000prh.googlegroups.com> <7a9c25c20905011741m6b5d4360yf32acdf99238a685@mail.gmail.com> Message-ID: En Fri, 01 May 2009 21:41:03 -0300, Stephen Hansen escribi?: >> urllib2 and BeautifulSoup work pretty well to do what I want, and the >> first little routine actually gets the data from the web page... >> except if my VPN client is turned on. > > Usually when something like that happens with a VPN its because the VPN > has been configured to do so. Its the default usually and is a security > precaution. The exact wording of the option varies from VPN client to > VPN client; and its usually hidden deep in 'advanced'. E.g., on the mac > its "Send all traffic over VPN connection", on Windows I /believe/ its > something like "Set as default gateway" but its been awhile since I had > to tweak my windows install. No idea what it looks like on the Cisco > client. Reading c.l.p. helps even for non-Python questions! A friend of mine had a problem with a VPN, I vaguely remembered this post and looked for it, and in a few minutes we fixed the problem. Thanks! -- Gabriel Genellina From david.lyon at preisshare.net Tue May 12 00:16:58 2009 From: david.lyon at preisshare.net (David Lyon) Date: Tue, 12 May 2009 00:16:58 -0400 Subject: Unable to install Pywin32 for Python 2.6.2 In-Reply-To: <445Ol.724$5F2.395@nwrddc01.gnilink.net> References: <445Ol.724$5F2.395@nwrddc01.gnilink.net> Message-ID: <7dc7b3785ee4e6817b7b1b1946e1e41c@preisshare.net> Hi, hmmm... that's annoying.. Whilst I don't have an exact answer I have a few hunches... Perphaps what has happened is that the windows installer between the versions of pywin32 has installed newer versions of the same dlls over the top of older ones. Or, possibly, the installer hasn't wanted to replace older dll's because they already exist on the system. This would result in a mismatch. You may be lucky and find an install log. This might tell you which files were actually installed. And you could play around with moving them around. Good luck with your night vision task.... David On Tue, 12 May 2009 02:22:56 GMT, David Lees wrote: > I have no problem installing Python 2.6.2 for windows under XP SP3 and > IDLE and the command line versions work fine. When I run the pywin32 > installer downloaded from sourceforge (pywin32-212.win32-py2.6.exe) I > get the following error message: > > Traceback (most recent call last): > File "", line 565, in > File "", line 291, in install > ImportError: Module use of python25.dll conflicts with this version of > Python. > *** run_installscript: internal error 0xFFFFFFFF *** > > I have tried uninstalling Python 2.6 and reinstalling, but still get the > same message. I do have Python 2.5.4 and its associated Pywin32 on the > same machine, but I have a laptop with pywin32 installed for both python > 2.5 and 2.6. > > TIA > > David Lees From david.lyon at preisshare.net Tue May 12 00:18:28 2009 From: david.lyon at preisshare.net (David Lyon) Date: Tue, 12 May 2009 00:18:28 -0400 Subject: Unable to install Pywin32 for Python 2.6.2 In-Reply-To: <445Ol.724$5F2.395@nwrddc01.gnilink.net> References: <445Ol.724$5F2.395@nwrddc01.gnilink.net> Message-ID: <974a7ad00eec34e80f30a544643cc5d9@preisshare.net> On Tue, 12 May 2009 02:22:56 GMT, David Lees wrote: > I have no problem installing Python 2.6.2 for windows under XP SP3 and > IDLE and the command line versions work fine. When I run the pywin32 > installer downloaded from sourceforge (pywin32-212.win32-py2.6.exe) I > get the following error message: > > Traceback (most recent call last): > File "", line 565, in > File "", line 291, in install > ImportError: Module use of python25.dll conflicts with this version of > Python. > *** run_installscript: internal error 0xFFFFFFFF *** > > I have tried uninstalling Python 2.6 and reinstalling, but still get the > same message. I do have Python 2.5.4 and its associated Pywin32 on the > same machine, but I have a laptop with pywin32 installed for both python > 2.5 and 2.6. > > TIA > > David Lees From showell30 at yahoo.com Tue May 12 00:49:51 2009 From: showell30 at yahoo.com (Steve Howell) Date: Mon, 11 May 2009 21:49:51 -0700 (PDT) Subject: piping input to an external script References: Message-ID: <73b5bc5d-df66-465c-819d-aa8758afe110@z7g2000vbh.googlegroups.com> On May 11, 11:04?am, "Tim Arnold" wrote: > Hi, I have some html files that I want to validate by using an external > script 'validate'. The html files need a doctype header attached before > validation. The files are in utf8 encoding. My code: > --------------- > import os,sys > import codecs,subprocess > HEADER = '' > > filename ?= 'mytest.html' > fd = codecs.open(filename,'rb',encoding='utf8') > s = HEADER + fd.read() > fd.close() > > p = subprocess.Popen(['validate'], > ? ? ? ? ? ? ? ? ? ? stdin=subprocess.PIPE, > ? ? ? ? ? ? ? ? ? ? stdout=subprocess.PIPE, > ? ? ? ? ? ? ? ? ? ? stderr=subprocess.STDOUT) > validate = p.communicate(unicode(s,encoding='utf8')) > print validate > --------------- > > I get lots of lines like this: > Error at line 1, character 66:\tillegal character number 0 > etc etc. > > But I can give the command in a terminal 'cat mytest.html | validate' and > get reasonable output. My subprocess code must be wrong, but I could use > some help to see what the problem is. > Newline missing after the header is my guess. From kay at fiber-space.de Tue May 12 00:54:33 2009 From: kay at fiber-space.de (kay at fiber-space.de) Date: Mon, 11 May 2009 21:54:33 -0700 (PDT) Subject: Nimrod programming language References: <159308160@web.de> Message-ID: On 12 Mai, 02:10, Tomasz Rola wrote: > On Mon, 11 May 2009, rump... at web.de wrote: > > > One question I ask myself upon seeing a new language is if it is possible > > > to program amb (amb=ambiguous) operator in it. This page gives a very > > > nice, "code first" explanation of amb and how it is supposed to work: > > > >http://www.randomhacks.net/articles/2005/10/11/amb-operator > > > Hm. I am not sure either. To me, it looks like a constraint solver by > > using brute force. > > Yes, kind of. From what I understand [1], it's goal is to simulate > nondeterministic "get right answer in one try", only this "one try" > requires browsing the solution space (because we have only "classic" > hardware). It is easy to use amb in wrong way, but I can see some cases > when I would like it. In Python you can use a generator expression. Dan Piponis example can be stated as g = ((i,j) for i in range(2,100) for j in range(2,i) if i*j == 481) which doesn't require any call/cc hacks but is a deterministic "get right in one try". Once Nimrod has coroutines it's just a matter of sugaring them into comprehensions. > > [1] John McCarthy's paper still waits to be read by me, so... Ok, I have > just had a look at it and it seems that Ruby implementation is a bit > primitive compared to original concept. But still, this is what would be > needed in most real life cases, I suppose. > > I think ability to define amb would allow me to write more concise code. > As far as I can tell, this kind of stuff plays more and more important > role in my programing. > > > It seems to require continuations. AFAIK > > continuations are extremely hard to implement efficiently, so probably > > it won't be done in Nimrod. > > Pity, a little. But not really big problem for me. Nimrod may be > interesting anyway, time will tell :-). > > Regards, > Tomasz Rola > > -- > ** A C programmer asked whether computer had Buddha's nature. ** > ** As the answer, master did "rm -rif" on the programmer's home ** > ** directory. And then the C programmer became enlightened... ** > ** ** > ** Tomasz Rola mailto:tomasz_r... at bigfoot.com ** From david.lyon at preisshare.net Tue May 12 01:13:25 2009 From: david.lyon at preisshare.net (David Lyon) Date: Tue, 12 May 2009 01:13:25 -0400 Subject: Fwd: Re: Unable to install Pywin32 for Python 2.6.2 Message-ID: <90e1d7598b339eb4350e84024d2cf04a@preisshare.net> Hi, hmmm... that's annoying.. Whilst I don't have an exact answer I have a few hunches... Perphaps what has happened is that the windows installer between the versions of pywin32 has installed newer versions of the same dlls over the top of older ones. Or, possibly, the installer hasn't wanted to replace older dll's because they already exist on the system. This would result in a mismatch. You may be lucky and find an install log. This might tell you which files were actually installed. And you could play around with moving them around. Good luck with your night vision task.... David On Tue, 12 May 2009 02:22:56 GMT, David Lees wrote: > I have no problem installing Python 2.6.2 for windows under XP SP3 and > IDLE and the command line versions work fine. When I run the pywin32 > installer downloaded from sourceforge (pywin32-212.win32-py2.6.exe) I > get the following error message: > > Traceback (most recent call last): > File "", line 565, in > File "", line 291, in install > ImportError: Module use of python25.dll conflicts with this version of > Python. > *** run_installscript: internal error 0xFFFFFFFF *** > > I have tried uninstalling Python 2.6 and reinstalling, but still get the > same message. I do have Python 2.5.4 and its associated Pywin32 on the > same machine, but I have a laptop with pywin32 installed for both python > 2.5 and 2.6. > > TIA > > David Lees From norseman at hughes.net Tue May 12 01:16:48 2009 From: norseman at hughes.net (norseman) Date: Mon, 11 May 2009 22:16:48 -0700 Subject: piping input to an external script In-Reply-To: References: Message-ID: <4A090640.8000303@hughes.net> Tim Arnold wrote: > Hi, I have some html files that I want to validate by using an external > script 'validate'. The html files need a doctype header attached before > validation. The files are in utf8 encoding. My code: > --------------- > import os,sys > import codecs,subprocess > HEADER = '' > > filename = 'mytest.html' > fd = codecs.open(filename,'rb',encoding='utf8') > s = HEADER + fd.read() > fd.close() > > p = subprocess.Popen(['validate'], > stdin=subprocess.PIPE, > stdout=subprocess.PIPE, > stderr=subprocess.STDOUT) > validate = p.communicate(unicode(s,encoding='utf8')) > print validate > --------------- > > I get lots of lines like this: > Error at line 1, character 66:\tillegal character number 0 > etc etc. > > But I can give the command in a terminal 'cat mytest.html | validate' and > get reasonable output. My subprocess code must be wrong, but I could use > some help to see what the problem is. > > python2.5.1, freebsd6 > thanks, > --Tim > > ============================ If you search through the recent Python-List for UTF-8 things you might get the same understanding I have come to. the problem is the use of python's 'print' subcommand or what ever it is. It 'cooks' things and someone decided that it would only handle 1/2 of a byte (in the x'00 to x'7f' range) and ignore or send error messages against anything else. I guess the person doing the deciding read the part that says ASCII printables are in the 7 bit range and chose to ignore the part about the rest of the byte being undefined. That is undefined, not disallowed. Means the high bit half can be used as wanted since it isn't already taken. Nor did whoever it was take a look around the computer world and realize the conflict that was going to be generated by using only 1/2 of a byte in a 1byte+ world. If you can modify your code to use read and write you can bypass print and be OK. Or just have python do the 'cat mytest.html | validate' for you. (Apply a var for html and let python accomplish the the equivalent of Unix's: for f in *.html; do cat $f | validate; done or for f in *.html; do validate $f; done #file name available this way If you still have problems, take a look at os.POPEN2 (and its popen3) Also take look at os.spawn.. et al HTH Steve From showell30 at yahoo.com Tue May 12 01:50:02 2009 From: showell30 at yahoo.com (Steve Howell) Date: Mon, 11 May 2009 22:50:02 -0700 (PDT) Subject: piping input to an external script References: Message-ID: <81e57098-b61e-4e8b-826c-833518c8115e@r3g2000vbp.googlegroups.com> On May 11, 10:16?pm, norseman wrote: > Tim Arnold wrote: > > Hi, I have some html files that I want to validate by using an external > > script 'validate'. The html files need a doctype header attached before > > validation. The files are in utf8 encoding. My code: > > --------------- > > import os,sys > > import codecs,subprocess > > HEADER = '' > > > filename ?= 'mytest.html' > > fd = codecs.open(filename,'rb',encoding='utf8') > > s = HEADER + fd.read() > > fd.close() > > > p = subprocess.Popen(['validate'], > > ? ? ? ? ? ? ? ? ? ? stdin=subprocess.PIPE, > > ? ? ? ? ? ? ? ? ? ? stdout=subprocess.PIPE, > > ? ? ? ? ? ? ? ? ? ? stderr=subprocess.STDOUT) > > validate = p.communicate(unicode(s,encoding='utf8')) > > print validate > > --------------- > > > I get lots of lines like this: > > Error at line 1, character 66:\tillegal character number 0 > > etc etc. > > > But I can give the command in a terminal 'cat mytest.html | validate' and > > get reasonable output. My subprocess code must be wrong, but I could use > > some help to see what the problem is. > > > python2.5.1, freebsd6 > > thanks, > > --Tim > > ============================ > If you search through the recent Python-List for UTF-8 things you might > get the same understanding I have come to. > > the problem is the use of python's 'print' subcommand or what ever it > is. It 'cooks' things and someone decided that it would only handle 1/2 > of a byte (in the x'00 to x'7f' range) and ignore or send error messages > against anything else. I guess the person doing the deciding read the > part that says ASCII printables are in the 7 bit range and chose to > ignore the part about the rest of the byte being undefined. That is > undefined, not disallowed. ?Means the high bit half can be used as > wanted since it isn't already taken. Nor did whoever it was take a look > around the computer world and realize the conflict that was going to be > generated by using only 1/2 of a byte in a 1byte+ world. > > If you can modify your code to use read and write you can bypass print > and be OK. ?Or just have python do the 'cat mytest.html | validate' for > you. (Apply a var for html and let python accomplish the the equivalent > of Unix's: > ? ? for f in *.html; do cat $f | validate; done > ? ? ? ? ? ? ? ? ? ? ? ? ?or > ? ? ?for f in *.html; do validate $f; done ?#file name available this way > > If you still have problems, take a look at os.POPEN2 (and its popen3) > Also take look at os.spawn.. et al > Wow. Unicode and subprocessing and printing can have dark corners, but common sense does apply in MOST situations. If you send the header, add the newline. But you do not need the header if you can cat the input file sans header and get sensible input. Finally, if you are concerned about adding the header, then it belongs in the original input file; otherwise, you are creating a false positive. From skippy.hammond at gmail.com Tue May 12 02:00:34 2009 From: skippy.hammond at gmail.com (Mark Hammond) Date: Tue, 12 May 2009 16:00:34 +1000 Subject: Fwd: Re: Unable to install Pywin32 for Python 2.6.2 In-Reply-To: <90e1d7598b339eb4350e84024d2cf04a@preisshare.net> References: <90e1d7598b339eb4350e84024d2cf04a@preisshare.net> Message-ID: <4A091082.8090004@gmail.com> Probably some import statement is finding a .pyd module built against Python 2.5 instead of Python 2.6; it might be that PYTHONPATH points somewhere wrong, or the registry for Python 2.6 is setup wrong, or something else entirely... Cheers, Mark On 12/05/2009 3:13 PM, David Lyon wrote: > Hi, > > hmmm... that's annoying.. > > Whilst I don't have an exact answer I have a few hunches... > > Perphaps what has happened is that the windows installer between > the versions of pywin32 has installed newer versions of the same > dlls over the top of older ones. > > Or, possibly, the installer hasn't wanted to replace older dll's > because they already exist on the system. > > This would result in a mismatch. > > You may be lucky and find an install log. This might tell you which > files were actually installed. And you could play around with > moving them around. > > Good luck with your night vision task.... > > David > > > On Tue, 12 May 2009 02:22:56 GMT, David Lees > wrote: >> I have no problem installing Python 2.6.2 for windows under XP SP3 and >> IDLE and the command line versions work fine. When I run the pywin32 >> installer downloaded from sourceforge (pywin32-212.win32-py2.6.exe) I >> get the following error message: >> >> Traceback (most recent call last): >> File "", line 565, in >> File "", line 291, in install >> ImportError: Module use of python25.dll conflicts with this version of >> Python. >> *** run_installscript: internal error 0xFFFFFFFF *** >> >> I have tried uninstalling Python 2.6 and reinstalling, but still get the >> same message. I do have Python 2.5.4 and its associated Pywin32 on the >> same machine, but I have a laptop with pywin32 installed for both python >> 2.5 and 2.6. >> >> TIA >> >> David Lees From norseman at hughes.net Tue May 12 02:31:45 2009 From: norseman at hughes.net (norseman) Date: Mon, 11 May 2009 23:31:45 -0700 Subject: piping input to an external script In-Reply-To: <81e57098-b61e-4e8b-826c-833518c8115e@r3g2000vbp.googlegroups.com> References: <81e57098-b61e-4e8b-826c-833518c8115e@r3g2000vbp.googlegroups.com> Message-ID: <4A0917D1.7020903@hughes.net> Steve Howell wrote: > On May 11, 10:16 pm, norseman wrote: >> Tim Arnold wrote: >>> Hi, I have some html files that I want to validate by using an external >>> script 'validate'. The html files need a doctype header attached before >>> validation. The files are in utf8 encoding. My code: >>> --------------- >>> import os,sys >>> import codecs,subprocess >>> HEADER = '' >>> filename = 'mytest.html' >>> fd = codecs.open(filename,'rb',encoding='utf8') >>> s = HEADER + fd.read() >>> fd.close() >>> p = subprocess.Popen(['validate'], >>> stdin=subprocess.PIPE, >>> stdout=subprocess.PIPE, >>> stderr=subprocess.STDOUT) >>> validate = p.communicate(unicode(s,encoding='utf8')) >>> print validate >>> --------------- >>> I get lots of lines like this: >>> Error at line 1, character 66:\tillegal character number 0 >>> etc etc. >>> But I can give the command in a terminal 'cat mytest.html | validate' and >>> get reasonable output. My subprocess code must be wrong, but I could use >>> some help to see what the problem is. >>> python2.5.1, freebsd6 >>> thanks, >>> --Tim >> ============================ >> If you search through the recent Python-List for UTF-8 things you might >> get the same understanding I have come to. >> >> the problem is the use of python's 'print' subcommand or what ever it >> is. It 'cooks' things and someone decided that it would only handle 1/2 >> of a byte (in the x'00 to x'7f' range) and ignore or send error messages >> against anything else. I guess the person doing the deciding read the >> part that says ASCII printables are in the 7 bit range and chose to >> ignore the part about the rest of the byte being undefined. That is >> undefined, not disallowed. Means the high bit half can be used as >> wanted since it isn't already taken. Nor did whoever it was take a look >> around the computer world and realize the conflict that was going to be >> generated by using only 1/2 of a byte in a 1byte+ world. >> >> If you can modify your code to use read and write you can bypass print >> and be OK. Or just have python do the 'cat mytest.html | validate' for >> you. (Apply a var for html and let python accomplish the the equivalent >> of Unix's: >> for f in *.html; do cat $f | validate; done >> or >> for f in *.html; do validate $f; done #file name available this way >> >> If you still have problems, take a look at os.POPEN2 (and its popen3) >> Also take look at os.spawn.. et al >> > > Wow. Unicode and subprocessing and printing can have dark corners, > but common sense does apply in MOST situations. > > If you send the header, add the newline. > > But you do not need the header if you can cat the input file sans > header and get sensible input. > Yep! The problem is with 'print' > Finally, if you are concerned about adding the header, then it belongs > in the original input file; otherwise, you are creating a false > positive. Steve From ggardiner at iee.org Tue May 12 02:36:57 2009 From: ggardiner at iee.org (Geoff Gardiner) Date: Tue, 12 May 2009 07:36:57 +0100 Subject: How do I test the integrity of a Python installation in Debian and Ubuntu In-Reply-To: References: Message-ID: <4A091909.4070108@iee.org> Lawrence D'Oliveiro wrote: > .. I expect an apology. > Otherwise, it becomes grounds for an abuse complaint to your ISP. > Yes, I do apologize profusely and publicly, and would have done so regardless of threat. I had trouble with posts making it through to the list and so was also posting in parallel to the original posters. In doing so, your address initially bounced the message - so I changed it unthinkingly in a to: line. I did not realize until this morning that they made it through python-list unchanged, as they obviously do. All the best, Geoff From ralf321 at gmx.de Tue May 12 02:48:49 2009 From: ralf321 at gmx.de (ralf321 at gmx.de) Date: Mon, 11 May 2009 23:48:49 -0700 (PDT) Subject: install pyPgSQL on Windows for python 2.5 References: <4e9538ae-15b3-4214-b3d7-ebf948bf2f17@r33g2000yqn.googlegroups.com> <72se78Frtj4nU1@mid.individual.net> Message-ID: <912b822c-e6ac-4e2f-bddf-8c422c7e8bab@r34g2000vbi.googlegroups.com> On 24 Mrz., 18:25, someon wrote: > On Mar 24, 4:57?pm, Dietmar Schwertberger > wrote: > > > someone wrote: > > > Hi, > > > > does anyone know how to install pyPgSQL on Windows? There is no > > > package for Python 2.5 on Homepage: > > > I've installed newest Visual C++ Studio 2008 from Microsoft, but still > > > no luck > > > Hello Pet, > > > you need Visual Studio 2003 to compile extensions for Python 2.5 > > If you want, I can send you a binary. > > O, thank you very much! > Where should I put hole thing? > > > Regards, > > > Dietmar > > Hello, i need the binary files too. can you send me or upload anywhere? thanks. regards Ralf From showell30 at yahoo.com Tue May 12 02:50:23 2009 From: showell30 at yahoo.com (Steve Howell) Date: Mon, 11 May 2009 23:50:23 -0700 (PDT) Subject: piping input to an external script References: <81e57098-b61e-4e8b-826c-833518c8115e@r3g2000vbp.googlegroups.com> Message-ID: On May 11, 11:31?pm, norseman wrote: > Steve Howell wrote: > > On May 11, 10:16 pm, norseman wrote: > >> Tim Arnold wrote: > >>> Hi, I have some html files that I want to validate by using an external > >>> script 'validate'. The html files need a doctype header attached before > >>> validation. The files are in utf8 encoding. My code: > >>> --------------- > >>> import os,sys > >>> import codecs,subprocess > >>> HEADER = '' > >>> filename ?= 'mytest.html' > >>> fd = codecs.open(filename,'rb',encoding='utf8') > >>> s = HEADER + fd.read() > >>> fd.close() > >>> p = subprocess.Popen(['validate'], > >>> ? ? ? ? ? ? ? ? ? ? stdin=subprocess.PIPE, > >>> ? ? ? ? ? ? ? ? ? ? stdout=subprocess.PIPE, > >>> ? ? ? ? ? ? ? ? ? ? stderr=subprocess.STDOUT) > >>> validate = p.communicate(unicode(s,encoding='utf8')) > >>> print validate > >>> --------------- > >>> I get lots of lines like this: > >>> Error at line 1, character 66:\tillegal character number 0 > >>> etc etc. > >>> But I can give the command in a terminal 'cat mytest.html | validate' and > >>> get reasonable output. My subprocess code must be wrong, but I could use > >>> some help to see what the problem is. > >>> python2.5.1, freebsd6 > >>> thanks, > >>> --Tim > >> ============================ > >> If you search through the recent Python-List for UTF-8 things you might > >> get the same understanding I have come to. > > >> the problem is the use of python's 'print' subcommand or what ever it > >> is. It 'cooks' things and someone decided that it would only handle 1/2 > >> of a byte (in the x'00 to x'7f' range) and ignore or send error messages > >> against anything else. I guess the person doing the deciding read the > >> part that says ASCII printables are in the 7 bit range and chose to > >> ignore the part about the rest of the byte being undefined. That is > >> undefined, not disallowed. ?Means the high bit half can be used as > >> wanted since it isn't already taken. Nor did whoever it was take a look > >> around the computer world and realize the conflict that was going to be > >> generated by using only 1/2 of a byte in a 1byte+ world. > > >> If you can modify your code to use read and write you can bypass print > >> and be OK. ?Or just have python do the 'cat mytest.html | validate' for > >> you. (Apply a var for html and let python accomplish the the equivalent > >> of Unix's: > >> ? ? for f in *.html; do cat $f | validate; done > >> ? ? ? ? ? ? ? ? ? ? ? ? ?or > >> ? ? ?for f in *.html; do validate $f; done ?#file name available this way > > >> If you still have problems, take a look at os.POPEN2 (and its popen3) > >> Also take look at os.spawn.. et al > > > Wow. ?Unicode and subprocessing and printing can have dark corners, > > but common sense does apply in MOST situations. > > > If you send the header, add the newline. > > > But you do not need the header if you can cat the input file sans > > header and get sensible input. > > Yep! ?The problem is with 'print' > Huh? Print is printing exactly what you expect it to print. From mikael at isy.liu.se Tue May 12 02:54:22 2009 From: mikael at isy.liu.se (Mikael Olofsson) Date: Tue, 12 May 2009 08:54:22 +0200 Subject: Decorating methods - where do my arguments go? In-Reply-To: References: Message-ID: Peter Otten wrote: > I usually use decorator functions, but I think the following should work, > too: > > class deco(object): > def __init__(self, func): > self._func = func > def __call__(self, *args): > print "Decorator:", args > self._func(*args) > def __get__(self, *args): > return deco(self._func.__get__(*args)) This looks like a winner to me. It is elegant, works for functions as well as for methods, and does not mess with the internals of the decorator which avoids the problems that Duncan pointed out. Thanks Peter, and thanks everyone else that chimed in and contributed. /MiO From rtw at freenet.co.uk Tue May 12 02:56:50 2009 From: rtw at freenet.co.uk (Rob Williscroft) Date: Tue, 12 May 2009 01:56:50 -0500 Subject: sqlite single transaction without foreign key or triggers References: Message-ID: Aahz wrote in news:guao50$1jv$1 at panix3.panix.com in comp.lang.python: > In article , > Rob Williscroft wrote: >> >>db.execute( ''' >> update "sessions" set "uid" = ? >> where "uid" = ? >> and exists( >> select * from "users" where "uid" = ? >> ) >> ''', >> (v['uid'],s.SID, v['uid']) >> ) > > This will be more efficient if you do "select uid from users". What will be more efficient ? Do you mean the "select * ..." or do you want to take the exists sub-query out and put it in a python if ? Rob. -- http://www.victim-prime.dsl.pipex.com/ From michele.simionato at gmail.com Tue May 12 03:01:10 2009 From: michele.simionato at gmail.com (Michele Simionato) Date: Tue, 12 May 2009 00:01:10 -0700 (PDT) Subject: Decorating methods - where do my arguments go? References: Message-ID: <0601fa8d-00af-41fe-86f7-5bb210ec268c@o14g2000vbo.googlegroups.com> On May 12, 8:54?am, Mikael Olofsson wrote: > Peter Otten wrote: > > I usually use decorator functions, but I think the following should work, > > too: > > > class deco(object): > > ? ? def __init__(self, func): > > ? ? ? ? self._func = func > > ? ? def __call__(self, *args): > > ? ? ? ? print "Decorator:", args > > ? ? ? ? self._func(*args) > > ? ? def __get__(self, *args): > > ? ? ? ? return deco(self._func.__get__(*args)) > > This looks like a winner to me. It is elegant, works for functions as > well as for methods, and does not mess with the internals of the > decorator which avoids the problems that Duncan pointed out. Still it turns something which is a function into an object and you lose the docstring and the signature. pydoc will not be too happy with this approach. From deets at nospam.web.de Tue May 12 03:09:46 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Tue, 12 May 2009 09:09:46 +0200 Subject: how to consume .NET webservice In-Reply-To: References: <4a0885e2$0$2853$ba620e4c@news.skynet.be> <76rnq5F1dp6gpU1@mid.uni-berlin.de> Message-ID: <76sllsF1eju82U1@mid.uni-berlin.de> namekuseijin schrieb: > Diez B. Roggisch wrote: >> namekuseijin schrieb: >>> bav escreveu: >>>> question from a python newbie; >>>> >>>> how can i consume in python language, a .NET web service, passing >>>> a string array as parameter in some easy steps? >>> >>> Unless Microsoft extended the standard in any way, then it should be >>> just as you consume any web service, I guess. ;) >> >> Microsoft *created* the standard.. > > No: Yes: http://webservices.xml.com/pub/a/ws/2001/04/04/soap.html MS created it. That it *became* a standard of the W3C later - well, they did that with OOXML as well... Diez From deets at nospam.web.de Tue May 12 03:12:21 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Tue, 12 May 2009 09:12:21 +0200 Subject: how to consume .NET webservice In-Reply-To: References: <4a0885e2$0$2853$ba620e4c@news.skynet.be> <76rnq5F1dp6gpU1@mid.uni-berlin.de> Message-ID: <76slqnF1ehacbU1@mid.uni-berlin.de> namekuseijin schrieb: > Diez B. Roggisch wrote: >> namekuseijin schrieb: >>> bav escreveu: >>>> question from a python newbie; >>>> >>>> how can i consume in python language, a .NET web service, passing >>>> a string array as parameter in some easy steps? >>> >>> Unless Microsoft extended the standard in any way, then it should be just as you consume any web service, I guess. ;) >> >> Microsoft *created* the standard.. > > No: Yes: http://webservices.xml.com/pub/a/ws/2001/04/04/soap.html MS created it. That it *became* a standard of the W3C later - well, they did that with OOXML as well... Diez From deets at nospam.web.de Tue May 12 03:14:33 2009 From: deets at nospam.web.de (Diez B. Roggisch) Date: Tue, 12 May 2009 09:14:33 +0200 Subject: how to consume .NET webservice In-Reply-To: <4a0885e2$0$2853$ba620e4c@news.skynet.be> References: <4a0885e2$0$2853$ba620e4c@news.skynet.be> Message-ID: <76sluqF1ehacbU2@mid.uni-berlin.de> bav schrieb: > question from a python newbie; > > how can i consume in python language, a .NET web service, passing > a string array as parameter in some easy steps? Try accessing the .NET-service using a .NET client, and capture the traffic (it speaks volumes that the apache axis project offers a HTTP-proxy for exactly that purposes, tcpmon). Then use an XML-templating language such as genshi & create the messages yourself. Sounds awkward and complicated? It is. As I said in another post: the "S" stands for simple... Diez From davea at ieee.org Tue May 12 03:24:18 2009 From: davea at ieee.org (Dave Angel) Date: Tue, 12 May 2009 03:24:18 -0400 Subject: piping input to an external script In-Reply-To: References: Message-ID: <4A092422.50100@ieee.org> Tim Arnold wrote: > Hi, I have some html files that I want to validate by using an external > script 'validate'. The html files need a doctype header attached before > validation. The files are in utf8 encoding. My code: > --------------- > import os,sys > import codecs,subprocess > HEADER = '' > > filename = 'mytest.html' > fd = codecs.open(filename,'rb',encoding='utf8') > s = HEADER + fd.read() > fd.close() > > p = subprocess.Popen(['validate'], > stdin=subprocess.PIPE, > stdout=subprocess.PIPE, > stderr=subprocess.STDOUT) > validate = p.communicate(unicode(s,encoding='utf8')) > print validate > --------------- > > I get lots of lines like this: > Error at line 1, character 66:\tillegal character number 0 > etc etc. > > But I can give the command in a terminal 'cat mytest.html | validate' and > get reasonable output. My subprocess code must be wrong, but I could use > some help to see what the problem is. > > python2.5.1, freebsd6 > thanks, > --Tim > > > > The usual rule in debugging: split the problem into two parts, and test each one separately, starting with the one you think most likely to be the culprit In this case the obvious place to split is with the data you're passing to the communicate call. I expect it's already wrong, long before you hand it to the subprocess. So write it to a file instead, and inspect it with a binary file viewer. And of course test it manually with your validate program. Is validate really expecting a Unicode stream in stdin ? From mail at timgolden.me.uk Tue May 12 03:26:37 2009 From: mail at timgolden.me.uk (Tim Golden) Date: Tue, 12 May 2009 08:26:37 +0100 Subject: Complete frustration In-Reply-To: <4A08C32D.3020904@ieee.org> References: <4A085B14.2060908@hughes.net> <4A08C32D.3020904@ieee.org> Message-ID: <4A0924AD.3080201@timgolden.me.uk> Dave Angel wrote: [... snip sound advice about file associations etc. ...] One thing to bear in mind is that any settings, however obtained, in the User part of the registry (HKCU\Software\....) will override those in the Machine part of the registry (HKLM\Software...). If you think you've set things correctly and you're still not seeing the effect, check under HKCU to see if there's something there which would override things. TJG From tadwelessar at gmail.com Tue May 12 04:15:48 2009 From: tadwelessar at gmail.com (Matteo) Date: Tue, 12 May 2009 01:15:48 -0700 (PDT) Subject: Fill Javascript form References: <3f9c74cf-72f4-45e2-8724-3939366d13bf@e24g2000vbe.googlegroups.com> Message-ID: On 11 Mag, 23:06, Shawn Milochik wrote: > How is the form "written in JavaScript"? Is it dynamically generated? > > In any case, can you just send a POST request if you know the values required? The problem is indeed that the form is dynamically generated. That's the .js file: if (navigator.appVersion.indexOf("MSIE")!=-1) document.write("