Re: Reading hex to int from a binary string
On Oct 9, 3:12 am, Dennis Lee Bieber wrote: > On Thu, 8 Oct 2009 14:52:33 -0700 (PDT), Luc > declaimed the following in gmane.comp.python.general: > > > > > On Oct 8, 11:13 pm, "Diez B. Roggisch" wrote: > > > Luc schrieb: > > > > > Hi all, > > > > > I read data from a binary stream, so I get hex values as characters > > > > (in a string) with escaped x, like "\x05\x88", instead of 0x05. > > > > > I am looking for a clean way to add these two values and turn them > > > > into an integer, knowing that calling int() with base 16 throws an > > > > invalid literal exception. > > > > > Any help appreciated, thanks. > > > > Consider this (in the python interpreter): > > > > >>> chr(255) > > > '\xff' > > > >>> chr(255) == r"\xff" > > > False > > > >>> int(r"ff", 16) > > > 255 > > > > In other words: no, you *don't* get hex values. You get bytes from the > > > stream "as is", with python resorting to printing these out (in the > > > interpreter!!!) as "\xXX". Python does that so that binary data will > > > always have a "pretty" output when being inspected on the REPL. > > > > But they are bytes, and to convert them to an integer, you call "ord" on > > > them. > > > > So assuming your string is read bytewise into two variables a & b, this > > > is your desired code: > > > > >>> a = "\xff" > > > >>> b = "\xa0" > > > >>> ord(a) + ord(b) > > > 415 > > > > HTH, Diez > > > Sorry I was not clear enough. When I said "add", I meant concatenate > > because I want to read 0x0588 as one value and ord() does not allow > > that. > > > However you pointed me in the right direction and I found that int > > (binascii.hexlify(a + b, 16)) does the job. > > Yeesh... This is what struct is designed for... > > >>> import struct > >>> something = "\x05\x88and more\r\n" > >>> print something > > ˆand more > > > > >>> (h1, st, h2) = struct.unpack("H8sh", something) > >>> h1 > 34821 > >>> st > 'and more' > >>> h2 > 2573 > > >>> print "%4x, %4x" % (h1, h2) > > 8805, a0d > > You may need to adjust for expected endian mode... > > >>> (h1, st, h2) = struct.unpack(">H8sh", something) > >>> print "%4.4x, %4.4x" % (h1, h2) > 0588, 0d0a > >>> h1 > 1416 > >>> h2 > 3338 > > -- > Wulfraed Dennis Lee Bieber KD6MOG > wlfr...@ix.netcom.com HTTP://wlfraed.home.netcom.com/ Nice, thanks! -- http://mail.python.org/mailman/listinfo/python-list
Google, Bing search api wrappers for python
Does any one know of a good wrappers for both of these search engines? There seem to a few but, wondering what people experience with them. Vitaly Babiy -- http://mail.python.org/mailman/listinfo/python-list
Reading hex to int from a binary string
Hi all, I read data from a binary stream, so I get hex values as characters (in a string) with escaped x, like "\x05\x88", instead of 0x05. I am looking for a clean way to add these two values and turn them into an integer, knowing that calling int() with base 16 throws an invalid literal exception. Any help appreciated, thanks. -- http://mail.python.org/mailman/listinfo/python-list
Help configuring Lamson. Multiple domains on one box
Is there anybody here that can help me out with some configuration issues concerning using the Lamson mail server(http:// lamsonproject.org/)? 1. I would like to use it as my main MTA 2. I would like it to mange 3 domains on one box Thanks -- http://mail.python.org/mailman/listinfo/python-list
save windows clipboard content temporarily and restore later
Is there a way to: 1. save windows clipboard content temporarily in a variable 2. (the clipboard content is then overwritten by some other applications) 3. restore the saved data back into the clipboard. ? I've tried win32clipboard's GetClipboardData, SetClipboardData. The GetClipboardData method is able to retrieve clipboard content only after a format is specified. Restoring the data with that format could result in information loss, for example when HTML text is saved in ordinary text format. There is no format that could preserve 100% of any kind of clipboard content. Does anyone has a brilliant solution? -- http://mail.python.org/mailman/listinfo/python-list
Re: mktime, how to handle dates before 01-01-1970 ?
John Yeung writes: > I think the choice of epoch is not a big deal, once you pick one far > enough back. Ben Finney's suggestion to use 4004 BCE is not > appreciably different (computationally) from JDN. (Though I will say > that the Wikipedia link he provided doesn't mention 4004 BCE, and if > anything suggests using 1 CE as the epoch.) My apologies, I gave the wrong year. I was intending to refer to the http://en.wikipedia.org/wiki/Julian_date> system, which begins at the year −4712 (4713 BCE). This has the advantages of: * clearly covering spans of human history more recent than ancient civilisations * having a long-recognised standard specification * being commonly used in computing (for astronomy and other scientific computing applications) * making arithmetic on dates simple (it uses a year zero, making the time line an uninterrupted number line) -- \ “When we talk to God, we're praying. When God talks to us, | `\ we're schizophrenic.” —Jane Wagner, via Lily Tomlin, 1985 | _o__) | Ben Finney -- http://mail.python.org/mailman/listinfo/python-list
Re: save windows clipboard content temporarily and restore later
kakarukeys: > Restoring the data with that format could result in information loss, > for example when HTML text is saved in ordinary text format. There is > no format that could preserve 100% of any kind of clipboard content. > > Does anyone has a brilliant solution? Enumerate all the clipboard formats with EnumClipboardFormats and grab the contents in each format then put them all back when finished. Neil -- http://mail.python.org/mailman/listinfo/python-list
Re: web sound recording with python
Hi! On windows, you can record sound who play on the local sound-card. It is not really Python scripting, but Python can launch it. @+ -- Michel Claveau -- http://mail.python.org/mailman/listinfo/python-list
Re: Tkinter -- the best way to make a realtime loop
On Oct 8, 7:15 pm, J Wolfe wrote: > Thank you both for your replies. I had something similar to this: > > def incr(): > var.set(1 + var.get()) > root.after(1000, incr) > > except I had an extra set of parenthesis... > def incr(): > var.set(1 + var.get()) > root.after(1000, incr()) > > on the function which was screwing it up. Also needed to have a > root.update() to refresh the GUI. M, no? The root.update() should not be necessary: the triggering of the action specified in after(...) is done by the tk mainloop when it's idle, and if it's idle, it means that it already has updated the display. So no update() should be needed. What happens if you remove it? -- http://mail.python.org/mailman/listinfo/python-list
Re: Most "active" coroutine library project?
On Sep 23, 10:58 pm, Brian Hammond wrote: > On Aug 25, 12:51 am, Denis wrote: > > > You can also atgevent > > >http://pypi.python.org/pypi/gevent > > Please, please document this! There are a lot of people who would > love to use this but give up when they don't find a guide or something > similar. I've actually started doing that recently, check out http://gevent.org Feedback is appreciated. -- http://mail.python.org/mailman/listinfo/python-list
Re: Reading hex to int from a binary string
Luc schrieb: On Oct 8, 11:13 pm, "Diez B. Roggisch" wrote: Luc schrieb: Hi all, I read data from a binary stream, so I get hex values as characters (in a string) with escaped x, like "\x05\x88", instead of 0x05. I am looking for a clean way to add these two values and turn them into an integer, knowing that calling int() with base 16 throws an invalid literal exception. Any help appreciated, thanks. Consider this (in the python interpreter): >>> chr(255) '\xff' >>> chr(255) == r"\xff" False >>> int(r"ff", 16) 255 In other words: no, you *don't* get hex values. You get bytes from the stream "as is", with python resorting to printing these out (in the interpreter!!!) as "\xXX". Python does that so that binary data will always have a "pretty" output when being inspected on the REPL. But they are bytes, and to convert them to an integer, you call "ord" on them. So assuming your string is read bytewise into two variables a & b, this is your desired code: >>> a = "\xff" >>> b = "\xa0" >>> ord(a) + ord(b) 415 HTH, Diez Sorry I was not clear enough. When I said "add", I meant concatenate because I want to read 0x0588 as one value and ord() does not allow that. (ord(a) << 8) + ord(b) Diez -- http://mail.python.org/mailman/listinfo/python-list
Plotting multiple datasets with gnuplot
Hi, I'm trying to get gnuplot to display multiple data series on a single plot using gnuplot in python. I've searched around and haven't found a solution to how to do this when I have a variable-length list of plots to add. For example, the following code will work: plotData1 = Gnuplot.PlotItems.Data(data1, title="title1") plotData2 = Gnuplot.PlotItems.Data(data2, title="title2") g.plot( plotData1, plotData2 ) [I've removed the rest of the code for clarity] But how can I do the following instead: data = [] ... # Populate data ... plots = [] for dataSet in data: plots.append(dataSet) g.plot(plots) I don't know how many plots I'll be wanting to plot, but the number will be roughly 15-20 and it seems ridiculous to have to hand-write individual setup for each plot when I should be able to just loop through the datasets and add them to gnuplot automatically. Any help would be much appreciated! Thanks, Rob PS mulitplot isn't the solution - this places plots literally on top of each other, it doesn't plot different sets of data on the same axes. -- http://mail.python.org/mailman/listinfo/python-list
Tree Structure
Hello, Is there any python class to display the drive and folder structure as a tree(As you see in the windows explorer window)?? Thanks, Girish.. -- http://mail.python.org/mailman/listinfo/python-list
mxDateTime history (Re: mktime, how to handle dates before 01-01-1970 ?)
John Yeung wrote: > On Oct 6, 4:10 pm, Stef Mientki wrote: >> >> thanks guys, >> mx works a bit better > > Another popular Python date library is dateutil: > > http://labix.org/python-dateutil > > It gives a certain amount of credit to mxDateTime (praising it but not > being very clear how they are related; there is some mention of "using > the specification" of mxDateTime). History goes a bit like this: mxDateTime was the first Python library to implement native date/time types for Python. I started the project in 1997. Back then I had a good look around at the various date/time libs and then decided to take a little more academic approach to the whole thing. The result was that you only need two basic types in mxDateTime: the DateTime object, which refers to a point in time, and the DateTimeDelta object, which allows measuring the time span between two such points. Note the absence of a Date object. These would be date/time range objects since they apply to the timespan of a full day with a time reference point at midnight. I decided to leave such ranges for a later stage in development - and have so far, never needed them :-) A bit later in 1998, I also added the RelativeDateTime object, which allows doing date/time calculations based on relative terms, e.g. x minus one year (which could mean 356 or 366 days depending on the year), first of next month (which could mean anything from 1-31 days), last of next month, Tuesday in 3 weeks, last Monday next month, etc. And again a bit later in 2000, I added the RelativeDateTimeDiff object which works a bit like an age function in that it tries to determine the RelativeDateTime value which has to be applied to a DateTime object in order to reach another one, say from your birthday to today. The result reads is more user- friendly than a bare DateTimeDelta, e.g. you get 40 years, 6 months, 3 days instead of 14796 days. In 2002 Zope Corp initiated the development of the datetime module, which borrowed a lot of design and API ideas from mxDateTime. However, it did not provide a date/time string parser and also misses out on lots of the other good stuff you can find in mxDateTime. Gustavo Niemeyer started to work on a date/time parser based on the datetime module - that's the python-dateutil library. He also added a relative date/time object which was one of the bits Zope Corp left out in the datetime module from mxDateTime and added some other things that are not currently part of mxDateTime: recurring events and time zones. mxDateTime continues to be actively developed - mostly driven by eGenix' own needs and experience we find in projects. Whenever we need something new, we can just add it to mxDateTime and since its release cycle is not bound to Python's, such enhancement are more readily available to others as well. Overall, my impression is that the datetime module was designed on the drawing board without actually touching real life usage scenarios. We've just done a project that used the datetime module for date/time related things instead of mxDateTime and found that while the implementation is similar to mxDateTime (naturally, see above), many useful features you find in mxDateTime are not available on the datetime objects. For future releases, we'll make the interaction between the two implementations more user friendly, e.g. it should be possible to pass a datetime object to mx.DateTime.DateTimeFrom() and call a method on DateTime objects to get a datetime module object with the same values. > I would say mxDateTime and dateutil are the two heavyweights in this > arena. As you would expect, they have a lot of overlapping > functionality and which one is used is often just a matter of taste, > or whichever one you happened to find first. > > One thing that dateutil provides that mxDateTime doesn't is support > for "lay person" month operations. That is, as far as I can tell, > mxDateTime tries not to dirty itself with the messy business of month > arithmetic, whereas dateutil rolls up its sleeves and takes an honest > stab at it. If you are writing a calendar/appointment application, or > other end-user-facing program, I would expect dateutil to be a little > more helpful. Month arithmetic is a bit of a mess, since it's not clear how to map e.g. Jan 31 + one month. mxDateTime does support month arithmetic via the RelativeDateTime object, but I'm not all that happy with the solution to the above problem. The alternatives are basically deciding between loosing data or raising an exception - both aren't really all that nice. Perhaps I'll just add a parameter to allow customization of the behavior depending on application needs. -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, Oct 09 2009) >>> Python/Zope Consulting and Support ...http://www.egenix.com/ >>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ >>> mxODBC, mxDateTime, mxTextTools ...h
Re: Plotting multiple datasets with gnuplot
En Fri, 09 Oct 2009 06:36:45 -0300, Rob Garrett escribió: I'm trying to get gnuplot to display multiple data series on a single plot using gnuplot in python. I've searched around and haven't found a solution to how to do this when I have a variable-length list of plots to add. For example, the following code will work: plotData1 = Gnuplot.PlotItems.Data(data1, title="title1") plotData2 = Gnuplot.PlotItems.Data(data2, title="title2") g.plot( plotData1, plotData2 ) [I've removed the rest of the code for clarity] But how can I do the following instead: data = [] ... # Populate data ... plots = [] for dataSet in data: plots.append(dataSet) g.plot(plots) g.plot(*plots) should work; it's like calling g.plot(plots[0], plots[1], plots[2]...) See http://docs.python.org/reference/expressions.html#calls for the gory details. -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: Tree Structure
On Oct 9, 5:02 am, Girish wrote: > Is there any python class to display the drive and folder structure as > a tree(As you see in the windows explorer window)?? You could use a recursive function to print it out of course or you will need to use a GUI kit. wxPython has a tree widget, i think TIX has a simplistic tree widget? TIX is included in the stdlib -- http://mail.python.org/mailman/listinfo/python-list
Re: mxDateTime history (Re: mktime, how to handle dates before 01-01-1970 ?)
Month arithmetic is a bit of a mess, since it's not clear how to map e.g. Jan 31 + one month. "Jan 31 + one month" usually means "add one to the month value and then keep backing off the day if you get an exception making the date", so you'd get Feb 31, exception, Feb 30, exception, Feb 29, possibly an exception, and possibly/finally Feb 28th. This makes pretty intuitive sense to most folks and is usually what's meant. I've found that issues and confusion stem more from the non-commutative reality that "Jan 31 + (1 month) + (-1 month) != Jan 31 + (-1 month) + (1 month)" or the non-associative "Jan 31 + (1 month + 1 month) != (Jan 31 + 1 month) + 1 month" :-/ So yes, messy it is! -tkc -- http://mail.python.org/mailman/listinfo/python-list
Re: save windows clipboard content temporarily and restore later
On Oct 9, 11:30 am, Neil Hodgson wrote: > kakarukeys: > > > Restoring the data with that format could result in information loss, > > for example when HTML text is saved in ordinary text format. There is > > no format that could preserve 100% of any kind of clipboard content. > > > Does anyone has a brilliant solution? > > Enumerate all the clipboard formats with EnumClipboardFormats and > grab the contents in each format then put them all back when finished. > > Neil Hi Neil, I followed your hints, and wrote the following code. It works for most clipboard formats except files. Selecting and copying a file, followed by backup() and restore() throw an exception: Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> from test12 import * >>> backup() >>> restore() Traceback (most recent call last): File "", line 1, in File "test12.py", line 40, in restore win32clipboard.SetClipboardData(format, cb[format]) TypeError: expected a readable buffer object >>> If I try to skip the error, pasting into a folder creates a file named 'scrap' with more or less the same content as the copied file. Is there any solution? import win32clipboard storage = [] def backup(): cb = {} win32clipboard.OpenClipboard() format = 0 try: while True: format = win32clipboard.EnumClipboardFormats(format) if format == 0: break else: try: RawData = win32clipboard.GetClipboardData(format) except: continue else: cb[format] = RawData finally: win32clipboard.CloseClipboard() storage.append(cb) def restore(): if storage != []: win32clipboard.OpenClipboard() try: win32clipboard.EmptyClipboard() cb = storage.pop() for format in cb: win32clipboard.SetClipboardData(format, cb[format]) finally: win32clipboard.CloseClipboard() -- http://mail.python.org/mailman/listinfo/python-list
Re: Nested Menus
Hmm. I didn't bother to look at the "comparison post". The indenting looks right to me. I reread my post and I believe my question is straight-forward. The crux of the issue is my sentence at the bottom. I believe that details what my objective is. Not sure what I should do here. I hope you can clarify what it is you want me to clarify. V On Fri, Oct 9, 2009 at 2:04 AM, r wrote: > On Oct 8, 5:58 pm, Dennis Lee Bieber wrote: > (snip: atrocious post formatting!) > > Dear Dennis, > > This is not a personal attack on you but your writing style is so > horrendous i must at least notify you of it. Go to this link and view > your post in GG's and let me know what you think of it. > > http://groups.google.com/group/comp.lang.python/browse_thread/thread/6301372f8e581a74?hl=en# > > As you can see the post is virtually impossible to read from the large > and seemingly random indenting and tabbing going on.(notwitstanding > the bombastic verbosity and unbridled quoting). I have seen many posts > like this in the past so maybe you are just oblivious...? Are you > writing like this on purpose or is your newsreader bugging out? > > Sincerely, > concerned Pythonista > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: Python CherryPy TurboGears 2 project support needed [in Belgium]
b...@creue-consulting schrieb: Dear comp.lang.python users, Firstly, this a is a Job post for a on-site Freelance Python Job in Belgium, I know this is frowned upon by some, so I am very sorry if it is not well received, but as it is such a great job, I have been encouraged to post this to the list. So, apology done, and hopefully accepted, I need a Python *guru*, ideally with CherryPy & TurboGears 2/AJAX experience. Out of curiosity: TG1 is based upon CherryPy - TG2 on Pylons. So in my opinion, this is somewhat mutual exclusive (not the know how, but using this in one project). So where comes CherryPy into the game? Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: "cx_Freeze.freezer.ConfigError: no initscript named Console"
On Oct 1, 2:19 pm, John wrote: > cx_freeze v4.01 > Python 2.6 > Ubuntu Jaunty > > Following the example of 'cx-freeze hello.py', I'm getting the error > message below. I put all of the error keywords into google and found no > hits. > > Some people in various posts have said to use Python 2.5 but a lot of my > code is using Python 2.6 features. > > Can you telling what I'm doing wrong? > > ./cxfreeze hello.py > Traceback (most recent call last): > File "./cxfreeze", line 5, in > main() > File "/root/cx_Freeze-4.1/cx_Freeze/main.py", line 178, in main > silent = options.silent) > File "/root/cx_Freeze-4.1/cx_Freeze/freezer.py", line 85, in __init__ > self._VerifyConfiguration() > File "/root/cx_Freeze-4.1/cx_Freeze/freezer.py", line 325, in > _VerifyConfiguration > self._GetInitScriptFileName() > File "/root/cx_Freeze-4.1/cx_Freeze/freezer.py", line 246, in > _GetInitScriptFileName > raise ConfigError("no initscript named %s", name) > cx_Freeze.freezer.ConfigError: no initscript named Console Hi, I found the same problem when used to install cx_freeze via easy_install. Download the cx_freeze sorce, than: sudo setup.py install Now all is fine. Ubuntu 9.04, Python 2.6 -- http://mail.python.org/mailman/listinfo/python-list
Re: Nested Menus
On Fri, Oct 9, 2009 at 7:13 AM, Victor Subervi wrote: > Hmm. I didn't bother to look at the "comparison post". The indenting looks > right to me. I reread my post and I believe my question is straight-forward. > The crux of the issue is my sentence at the bottom. I believe that details > what my objective is. Not sure what I should do here. I hope you can clarify > what it is you want me to clarify. > He was complaining not about your post but a rather extended response Dennis made to you while trying to help. Which is a particularly asshatish thing to do, but hey. Dennis's response isn't really my style of using SQL in Python, but its perfectly legible and understandable -- both in GMail where I usually read, and via the link r provided to prove his point, which it doesn't at all. It looks fine in both. Either way, I basically do it just the same way Dennis did-- so did that solve your question? A little bit of table redesign and then recursive calls between the "items" and "relationships" tables. --S -- http://mail.python.org/mailman/listinfo/python-list
Re: Python CherryPy TurboGears 2 project support needed [in Belgium]
Diez, Thanks for pointing this out. It maybe that we are talking about TG 1.1 in that case. I will double check with the TA. Sorry for any confusion! - I'll let you know. Thanks again. Cheers, Ben On Oct 9, 3:36 pm, "Diez B. Roggisch" wrote: > b...@creue-consulting schrieb: > > > Dear comp.lang.python users, > > > Firstly, this a is a Job post for a on-site Freelance Python Job in > > Belgium, I know > > this is frowned upon by some, so I am very sorry if it is not well > > received, > > but as it is such a great job, I have been encouraged to post this to > > the list. > > > So, apology done, and hopefully accepted, I need a Python *guru*, > > ideally > > with CherryPy & TurboGears 2/AJAX experience. > > Out of curiosity: TG1 is based upon CherryPy - TG2 on Pylons. So in my > opinion, this is somewhat mutual exclusive (not the know how, but using > this in one project). > > So where comes CherryPy into the game? > > Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: Reading hex to int from a binary string
On Oct 9, 10:45 am, "Diez B. Roggisch" wrote: > Luc schrieb: > > > > > On Oct 8, 11:13 pm, "Diez B. Roggisch" wrote: > >> Luc schrieb: > > >>> Hi all, > >>> I read data from a binary stream, so I get hex values as characters > >>> (in a string) with escaped x, like "\x05\x88", instead of 0x05. > >>> I am looking for a clean way to add these two values and turn them > >>> into an integer, knowing that calling int() with base 16 throws an > >>> invalid literal exception. > >>> Any help appreciated, thanks. > >> Consider this (in the python interpreter): > > >> >>> chr(255) > >> '\xff' > >> >>> chr(255) == r"\xff" > >> False > >> >>> int(r"ff", 16) > >> 255 > > >> In other words: no, you *don't* get hex values. You get bytes from the > >> stream "as is", with python resorting to printing these out (in the > >> interpreter!!!) as "\xXX". Python does that so that binary data will > >> always have a "pretty" output when being inspected on the REPL. > > >> But they are bytes, and to convert them to an integer, you call "ord" on > >> them. > > >> So assuming your string is read bytewise into two variables a & b, this > >> is your desired code: > > >> >>> a = "\xff" > >> >>> b = "\xa0" > >> >>> ord(a) + ord(b) > >> 415 > > >> HTH, Diez > > > Sorry I was not clear enough. When I said "add", I meant concatenate > > because I want to read 0x0588 as one value and ord() does not allow > > that. > > (ord(a) << 8) + ord(b) > > Diez Yes that too. But I have four bytes fields and single bit fields to deal with as well so I'll stick with struct. Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: Nested Menus
Well, as sometimes happens, the response to Dennis' response caught my attention (out of context) and I didn't notice Dennis' response! Thanks for bringing it to my attention. I will look at it tonight, and follow-up tomorrow after I've had a chance to digest it and work with it. (And thank you Dennis!) V On Fri, Oct 9, 2009 at 11:13 AM, Stephen Hansen wrote: > > > On Fri, Oct 9, 2009 at 7:13 AM, Victor Subervi wrote: > >> Hmm. I didn't bother to look at the "comparison post". The indenting looks >> right to me. I reread my post and I believe my question is straight-forward. >> The crux of the issue is my sentence at the bottom. I believe that details >> what my objective is. Not sure what I should do here. I hope you can clarify >> what it is you want me to clarify. >> > > He was complaining not about your post but a rather extended response > Dennis made to you while trying to help. Which is a particularly asshatish > thing to do, but hey. > > Dennis's response isn't really my style of using SQL in Python, but its > perfectly legible and understandable -- both in GMail where I usually read, > and via the link r provided to prove his point, which it doesn't at all. It > looks fine in both. > > Either way, I basically do it just the same way Dennis did-- so did that > solve your question? A little bit of table redesign and then recursive calls > between the "items" and "relationships" tables. > > --S > > > > -- > http://mail.python.org/mailman/listinfo/python-list > > -- http://mail.python.org/mailman/listinfo/python-list
Zip Question
Hi; I have the following code: elif table[0] == 't': # This is a store subtype table bits = string.split(table, '0') sst.append(bits[2]) sstp.append(bits[1]) subtypes = dict(zip(sstp, sst)) When I print these out to screen, I get this: sst: ['doctors', 'patient'] sstp: ['prescriptions', 'prescriptions'] subtypes: {'prescriptions': 'patient'} Why do I only get one item from sst and sstp zipped? Why not both?? TIA, V -- http://mail.python.org/mailman/listinfo/python-list
Re: Zip Question
On Fri, Oct 9, 2009 at 10:02 AM, Victor Subervi wrote: > Hi; > I have the following code: > > elif table[0] == 't': # This is a store subtype table > bits = string.split(table, '0') > sst.append(bits[2]) > sstp.append(bits[1]) > subtypes = dict(zip(sstp, sst)) > > When I print these out to screen, I get this: > > sst: ['doctors', 'patient'] > sstp: ['prescriptions', 'prescriptions'] > subtypes: {'prescriptions': 'patient'} > > Why do I only get one item from sst and sstp zipped? Why not both?? I think you have a logic problem that's not shown in that code sample: >>> sst = ['doctors', 'patient'] >>> sstp = ['prescriptions', 'prescriptions'] >>> zip(sst,sstp) [('doctors', 'prescriptions'), ('patient', 'prescriptions')] >>> dict(zip(sst,sstp)) {'patient': 'prescriptions', 'doctors': 'prescriptions'} >>> --S -- http://mail.python.org/mailman/listinfo/python-list
Re: Zip Question
On Fri, Oct 9, 2009 at 10:10 AM, Stephen Hansen wrote: > > > On Fri, Oct 9, 2009 at 10:02 AM, Victor Subervi > wrote: > >> Hi; >> I have the following code: >> >> elif table[0] == 't': # This is a store subtype table >> bits = string.split(table, '0') >> sst.append(bits[2]) >> sstp.append(bits[1]) >> subtypes = dict(zip(sstp, sst)) >> >> When I print these out to screen, I get this: >> >> sst: ['doctors', 'patient'] >> sstp: ['prescriptions', 'prescriptions'] >> subtypes: {'prescriptions': 'patient'} >> >> Why do I only get one item from sst and sstp zipped? Why not both?? > > I think you have a logic problem that's not shown in that code sample: > > >>> sst = ['doctors', 'patient'] > >>> sstp = ['prescriptions', 'prescriptions'] > >>> zip(sst,sstp) > [('doctors', 'prescriptions'), ('patient', 'prescriptions')] > >>> dict(zip(sst,sstp)) > {'patient': 'prescriptions', 'doctors': 'prescriptions'} > >>> > > --S > > -- > http://mail.python.org/mailman/listinfo/python-list > > The issue is: subtypes = dict(zip(sstp, sst)) If you remove the dict, you'll see the following: [('prescriptions', 'doctors'), ('prescriptions', 'patient')] When this is converted to a dict, the first element of each tuple is placed as the dict's key, and the second as the value. This means that you have two keys of prescriptions, and so the final one happens to be chosen as the value. Changing the line: subtypes = dict(zip(sstp, sst)) to: subtypes = dict(zip(sst, sstp)) as I believe Stephen misread it to be causes the zip operation to return: [('doctors', 'prescriptions'), ('patient', 'prescriptions')] and thus the dict will contain: {'patient': 'prescriptions', 'doctors': 'prescriptions'} Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: Zip Question
You're right...how strange. Here's the whole code: tables = [] bst = [] bdt = [] spt = [] sst = [] sstp = [] cursor.execute('show tables;') all = cursor.fetchall() for a in all: tables.append(a[0]) for table in tables: if table[0] == 'b': # This is a basic table if table[1] == '0': # This is a basic static table bst.append(table) # elif table[1] == '1': # This is a basic dynamic table #bdt.append(table) # Basic dynamic tables, like "pic" below, have to be manually created. elif table[0] == 's': # This is a store table if table[1] == '0': # This is a store primary table spt.append(table) elif table[0] == 't': # This is a store subtype table bits = string.split(table, '0') sst.append(bits[2]) sstp.append(bits[1]) subtypes = dict(zip(sstp, sst)) print sst print '' print sstp print '' print subtypes print '' This is what prints out: ['doctors', 'patient'] ['prescriptions', 'prescriptions'] {'prescriptions': 'patient'} TIA, V On Fri, Oct 9, 2009 at 12:10 PM, Stephen Hansen wrote: > > > On Fri, Oct 9, 2009 at 10:02 AM, Victor Subervi > wrote: > >> Hi; >> I have the following code: >> >> elif table[0] == 't': # This is a store subtype table >> bits = string.split(table, '0') >> sst.append(bits[2]) >> sstp.append(bits[1]) >> subtypes = dict(zip(sstp, sst)) >> >> When I print these out to screen, I get this: >> >> sst: ['doctors', 'patient'] >> sstp: ['prescriptions', 'prescriptions'] >> subtypes: {'prescriptions': 'patient'} >> >> Why do I only get one item from sst and sstp zipped? Why not both?? > > I think you have a logic problem that's not shown in that code sample: > > >>> sst = ['doctors', 'patient'] > >>> sstp = ['prescriptions', 'prescriptions'] > >>> zip(sst,sstp) > [('doctors', 'prescriptions'), ('patient', 'prescriptions')] > >>> dict(zip(sst,sstp)) > {'patient': 'prescriptions', 'doctors': 'prescriptions'} > >>> > > --S > -- http://mail.python.org/mailman/listinfo/python-list
Re: Zip Question
Changing the line: > subtypes = dict(zip(sstp, sst)) > to: > subtypes = dict(zip(sst, sstp)) > as I believe Stephen misread it to be causes the zip operation to return: > [('doctors', 'prescriptions'), ('patient', 'prescriptions')] > and thus the dict will contain: > {'patient': 'prescriptions', 'doctors': 'prescriptions'} > > Whoops! You're absolutely right. I totally flipped the options to zip in my head when eyeing it. I suppose I'm suffering from late-onset dyslexia. Ahem. Yeah, I think the arguments to zip() were probably just flipped in Victor's code. Unless he wants a result different then what I assume is expected. I assume he's expecting {"doctors": "prescriptions", "patient": "prescriptions"}. If instead its something more like {"prescriptions": ["doctors", "patient"]} then zip() isn't how to accomplish it. --S -- http://mail.python.org/mailman/listinfo/python-list
Re: Is pythonic version of scanf() or sscanf() planned?
ryniek90 wrote: > So maybe someone, someday decide to > put in Python an alternative, really great implementation of scanf() ? My idea of a "great scanf() function" would be a clever combination of re.match(), int(), and float(). j -- http://mail.python.org/mailman/listinfo/python-list
Re: Zip Question
So, because the results in sstp were duplicates ( ['prescriptions', 'prescriptions'] ) it only returned one result in the dict(zip()) statement. Weird. Bug or feature? ;) Thanks, V On Fri, Oct 9, 2009 at 12:37 PM, Stephen Hansen wrote: > > > Changing the line: >> subtypes = dict(zip(sstp, sst)) >> to: >> subtypes = dict(zip(sst, sstp)) >> as I believe Stephen misread it to be causes the zip operation to return: >> [('doctors', 'prescriptions'), ('patient', 'prescriptions')] >> and thus the dict will contain: >> {'patient': 'prescriptions', 'doctors': 'prescriptions'} >> >> > Whoops! You're absolutely right. I totally flipped the options to zip in my > head when eyeing it. I suppose I'm suffering from late-onset dyslexia. Ahem. > > Yeah, I think the arguments to zip() were probably just flipped in Victor's > code. Unless he wants a result different then what I assume is expected. I > assume he's expecting {"doctors": "prescriptions", "patient": > "prescriptions"}. If instead its something more like {"prescriptions": > ["doctors", "patient"]} then zip() isn't how to accomplish it. > > --S > -- http://mail.python.org/mailman/listinfo/python-list
Re: Zip Question
On Fri, Oct 9, 2009 at 11:04 AM, Victor Subervi wrote: > So, because the results in sstp were duplicates ( ['prescriptions', > 'prescriptions'] ) it only returned one result in the dict(zip()) statement. > Weird. Bug or feature? ;) > Thanks, > V Feature. zip() returned two results, but dictionaries are mappings of keys to values. If you duplicate a key, you don't get multiple values for that key -- you replace the value. If you want something dictionary like which has multiple values, try something like: http://code.activestate.com/recipes/52219/ Or even this dict subclass: http://code.activestate.com/recipes/440502/ HTH, --S -- http://mail.python.org/mailman/listinfo/python-list
Python 2.5 execfile() works only once, why ?
hello, I'm working on a kind of IDE, to build and distribute Python programs. One of the parts is editing a build file for py2exe and running the modified script. In the script editor I've an accelerator key, to launch these tasks: - save modified script file - run modified script file - catch log and error information from py2exe - launch the distro executable self.Edit.SaveFile ( self.Edit.Filename ) Globalsx = {} #Globalsx [ 'stdout' ] = self.Log execfile ( self.Edit.Filename, Globalsx ) The above code (with or without the stdout redirection), works perfect, ... the first time ... but does (almost?) nothing (doesn't crash, try / except around execfile), although the source file "self.Edit.Filename" has changed. Could someone give me an explanation, why this happens ? It would be great to have a work around. I've currently switched to Popen, but can't get output / error piping working fluently. thanks, Stef Mientki -- http://mail.python.org/mailman/listinfo/python-list
Re: No threading.start_new_thread(), useful addition?
I personally find it much cleaner this way. Also, why should any code care in which thread it is executed? Why should I have to derive a class from some other only because I want to run one of its functions in a separate thread? I think you are right! Especially that you can (and probably will) call other methods from your thread. For example, functions from the standard library. Or use methods of objects that where created in a different thread. I think we can say, you must call methods of other (not Thread) objects if you ever want to do something useful in your thread. Now I'm beginnig to agree with you, Ulrich. We have threading.enumerate() - it can be used to list active threads. So if you only want to start a function in a separate thread, then you do not really need the Thread object. Then we do you HAVE TO create a Thread object explicitely? Of course you can easily create a nice decorator like this: import threading def threadlaunch(func): def launcher(*args,**kwargs): thr = threading.Thread(target=func,args=args,kwargs=kwargs) thr.start() return thr return launcher And then do something like: import time @threadlaunch def print_something(w,s): print "please wait..." time.sleep(w) print s thr = print_something(3,"Test") print "started print_something in",thr How about that? (Or was it your concern that it is not part of the standard library?) Best, Laszlo -- http://mail.python.org/mailman/listinfo/python-list
Re: Persistent Distributed Objects
I've seen evidence about this being done wrt what looks like insanely complex stuff on this list but I'm wondering if there is something to do this with any number of nodes and just farm out random classes/objects to them? Designing and opreating distributed systems is a complex thing. Especially if you do not want to specify an exact problem domain that you needed to solve - then you need to design a distributed system that is able to solve general problems. It is very complex, and - at least to my knowledge - there is no efficient solution out of the box. This is not Python specific. It would be a hard task just to design how that system should work, regardless of the computer language it is implemented in. L -- http://mail.python.org/mailman/listinfo/python-list
Re: Python 2.5 execfile() works only once, why ?
On Oct 9, 11:15 am, Stef Mientki wrote: > hello, > > I'm working on a kind of IDE, to build and distribute Python programs. > > One of the parts is editing a build file for py2exe and running the > modified script. > > In the script editor I've an accelerator key, to launch these tasks: > - save modified script file > - run modified script file > - catch log and error information from py2exe > - launch the distro executable > > self.Edit.SaveFile ( self.Edit.Filename ) > Globalsx = {} > #Globalsx [ 'stdout' ] = self.Log > execfile ( self.Edit.Filename, Globalsx ) > > The above code (with or without the stdout redirection), > works perfect, > ... the first time ... > but does (almost?) nothing (doesn't crash, try / except around execfile), > although the source file "self.Edit.Filename" has changed. > > Could someone give me an explanation, why this happens ? I'm guessing you have references to objects from the first time you ran execfile that don't get updated. It's kind of hard to tell. Carl Banks -- http://mail.python.org/mailman/listinfo/python-list
Re: No threading.start_new_thread(), useful addition?
On Oct 8, 5:03 am, Ulrich Eckhardt wrote: > sturlamolden wrote: > > On 8 Okt, 09:17, Ulrich Eckhardt wrote: > > >> I'm looking at the 'threading' module and see that other than the > >> 'thread' module it doesn't have a simple function to start a new thread. > >> Instead, you first have to instantiate a threading object and then start > >> the new thread on it: > > >> t = threading.Thread(target=my_function) > >> t.start() > > > One usually want to subclass threading.Thread instead of specifying a > > target function. > > No. You also don't derive from a file class in order to read a file. The > point is that the Thread instance is not a thread but it is an object that > can be used to access a thread, similar to a File instance which file which > is not the file but just an object to access one. > > I personally find it much cleaner this way. Also, why should any code care > in which thread it is executed? Why should I have to derive a class from > some other only because I want to run one of its functions in a separate > thread? I have to agree. I've been trying to understand some other entity's Java code lately, and it's mondo-confusing because there is a subclass of Thread that has methods that are called from other threads. To make matters worse the variable this method is assigned to is called, simply, "thread". As far as I can discern doing that way (as opposed to using a Runnable object) didn't have any effect except to make the logic harder to understand. So (getting back to Python) count me among those who say one usally want to specify a target function instead of subclassing threading.Thread. Carl Banks -- http://mail.python.org/mailman/listinfo/python-list
Re: package import dangers
Thanks to all for the answers! :) -- http://mail.python.org/mailman/listinfo/python-list
Re: Python 2.5 execfile() works only once, why ?
thanks Carl, in the mean while I found a good working solution thorugh wx.Execute. cheers, Stef Carl Banks wrote: On Oct 9, 11:15 am, Stef Mientki wrote: hello, I'm working on a kind of IDE, to build and distribute Python programs. One of the parts is editing a build file for py2exe and running the modified script. In the script editor I've an accelerator key, to launch these tasks: - save modified script file - run modified script file - catch log and error information from py2exe - launch the distro executable self.Edit.SaveFile ( self.Edit.Filename ) Globalsx = {} #Globalsx [ 'stdout' ] = self.Log execfile ( self.Edit.Filename, Globalsx ) The above code (with or without the stdout redirection), works perfect, ... the first time ... but does (almost?) nothing (doesn't crash, try / except around execfile), although the source file "self.Edit.Filename" has changed. Could someone give me an explanation, why this happens ? I'm guessing you have references to objects from the first time you ran execfile that don't get updated. It's kind of hard to tell. Carl Banks -- http://mail.python.org/mailman/listinfo/python-list
"smtplib.SMTPServerDisconnected: please run connect() first"
I'm getting the error: smtplib.SMTPServerDisconnected: please run connect() first when I run code that is essentially identical to the code given in http://docs.python.org/library/email-examples.html The error happens at the line (copied verbatim from the example linked to above): s.sendmail(me, [you], msg.as_string()) What am I doing wrong? kynn P.S. I'm running this using v. 2.6.1. P.S.2. By "essentially identical" I mean that the only change I made to the original snippet was to initialize variables that are not initialized in the original (namely, textfile, me, and you) -- http://mail.python.org/mailman/listinfo/python-list
Re: No threading.start_new_thread(), useful addition?
Laszlo Nagy wrote: > IMHO it is much cleaner to implement this as a decorator. Pro: > transparent passing of positional and keyword arguments, keeps function > documentation. You are entitled to your opinion but I STRONGLY recommend against your decorator. You MUST NOT start threads a a side effect of a module import. It can lead to severe bugs and dead lock the entire interpreter. I'm speaking as an experienced Python developer and a CPython core developer. Please trust me in this. The interaction between the import system, its global import lock and threads can and will lead to surprising and hard to debug bugs. A well designed application loads its modules first and then initializes its components explicitly. You don't want to fire up threads randomly! Christian -- http://mail.python.org/mailman/listinfo/python-list
easy install
A puzzlement: I used easy_install the other day to get xlutils on my system. It automatically installed xlrd and xlwt as well. This is cool. What's not so cool are my tracebacks. E.g. Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. --> import xlwt --> xlwt.__name__ 'xlwt' --> xlwt.__file__ 'C:\\Python25\\lib\\site-packages\\xlwt-0.7.2-py2.5-win32.egg\\xlwt\\__init__.pyc' --> xlwt.Workbook().save('non-file') Traceback (most recent call last): File "", line 1, in File "c:\docume~1\ethanf\locals~1\temp\easy_install-q1s1rb\xlwt-0.7.2-py2.5-win32.egg.tmp\xlwt\Workbook.py", line 634, in save File "c:\docume~1\ethanf\locals~1\temp\easy_install-q1s1rb\xlwt-0.7.2-py2.5-win32.egg.tmp\xlwt\Workbook.py", line 615, in get_biff_data IndexError: list index out of range --> Anyone know why that is? ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list
Re: "smtplib.SMTPServerDisconnected: please run connect() first"
kj wrote: I'm getting the error: smtplib.SMTPServerDisconnected: please run connect() first when I run code that is essentially identical to the code given in http://docs.python.org/library/email-examples.html The error happens at the line (copied verbatim from the example linked to above): s.sendmail(me, [you], msg.as_string()) What am I doing wrong? kynn P.S. I'm running this using v. 2.6.1. P.S.2. By "essentially identical" I mean that the only change I made to the original snippet was to initialize variables that are not initialized in the original (namely, textfile, me, and you) The line preceeding it, s = smtplib.SMTP() needs to have an e-mail server specified. E.g. s = smtplib.SMTP('localhost') # from the 2.5 docs Hope this helps! ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list
Re: organizing your scripts, with plenty of re-use
On Oct 5, 2:15 pm, "Rami Chowdhury" wrote: > On Mon, 05 Oct 2009 13:46:09 -0700, Buck wrote: > > Thanks. I think we're getting closer to the core of this. > > > To restate my problem more simply: > > > My core goal is to have my scripts in some sort of organization better > > than a single directory, and still have plenty of re-use between them. > > The only way I can see to implement this is to have 10+ lines of > > unintelligible hard-coded boilerplate in every runnable script. > > That doesn't seem reasonable or pythonic. > > Perhaps I've simply not followed this thread closely enough but could you > let us know a little bit more about how you intend / expect the scripts to > be used? > > If there's a standard directory you expect them to be dropped into by your > users (e.g. $HOME/scripts) ... We don't have any such convention. The production code is at a well- known path, but I need the code to be fully relocatable (cp -r /known/ path ~/mydir) for testing purposes. Here's a scenario. A user does a cvs checkout into some arbitrary directory and sees this: project/ +-- python/ +-- animals.py +-- mammals/ +-- horse.py +-- otter.py +-- reptiles/ +-- gator.py +-- newt.py +-- misc/ +-- lungs.py +-- swimming.py These are all runnable scripts that "just work" with no extra effort or knowlege, both in the testing scenario above, and for normal users that run it from some central location (maybe "/tools/mycompany/bin/ mammals"). The frustrating thing, for me, is that all these requirements are met if you leave the scripts in jumbled into a flat directory. As soon as you start organizing things, you need a good amount of boilerplate in each script to make things work anything like they did with the flat directory. I wouldn't mind having to write 'from mammals.horse import Horse' or 'from .animals import Animal' but it's not that simple. -- http://mail.python.org/mailman/listinfo/python-list
python performance on Solaris
I have been following this group for quite some time and I figured (after searching enough on google --and on this group-- and not finding anything useful) I could pose this question here. Can anyone shed some light on python's performance on Solaris? My code seem to return lookups from a in memory data structure I build combining bunch of dictionaries and lists 6-8 times faster on a 32 bit Linux box than on a Solaris zone. Is there anything that needs to be done specifically when installing/compiling python to improve performance or is it a known thing that python does not perform that well on solaris? Thanks.. -- http://mail.python.org/mailman/listinfo/python-list
Re: easy install
On Fri, Oct 9, 2009 at 5:02 PM, Ethan Furman wrote: > A puzzlement: > > I used easy_install the other day to get xlutils on my system. It > automatically installed xlrd and xlwt as well. This is cool. What's not so > cool are my tracebacks. E.g. > > Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] > on win32 > Type "help", "copyright", "credits" or "license" for more information. > --> import xlwt > --> xlwt.__name__ > 'xlwt' > --> xlwt.__file__ > 'C:\\Python25\\lib\\site-packages\\xlwt-0.7.2-py2.5-win32.egg\\xlwt\\__init__.pyc' > --> xlwt.Workbook().save('non-file') > Traceback (most recent call last): > File "", line 1, in > File > "c:\docume~1\ethanf\locals~1\temp\easy_install-q1s1rb\xlwt-0.7.2-py2.5-win32.egg.tmp\xlwt\Workbook.py", > line 634, in save > File > "c:\docume~1\ethanf\locals~1\temp\easy_install-q1s1rb\xlwt-0.7.2-py2.5-win32.egg.tmp\xlwt\Workbook.py", > line 615, in get_biff_data > IndexError: list index out of range > --> > > Anyone know why that is? > > ~Ethan~ > -- > http://mail.python.org/mailman/listinfo/python-list > You can't save a workbook with no worksheets. Try: W = xlwt.Workbook() W.add_sheet('no-sheet') W.save('non-file') -- http://mail.python.org/mailman/listinfo/python-list
Re: organizing your scripts, with plenty of re-use
On Oct 5, 8:12 pm, Steven D'Aprano wrote: > The Original Poster is confusing installation difficulties with code > organization -- his problem is that users have special requirements for > installation, and he's trying to work around those requirements by > organizing his code differently. I believe you have it backwards here. My installation requirements (no installation at all) are met with a flat organization, but as soon as I try to improve that organization, I can't meet the installation requirements. > As far as I can see, whether or not he uses a package, he will still have > the same problem with installation, namely, that his users aren't > developers, plus for non-technical reasons he can't provide an installer > and has to have users check code out of CVS. That's probably true... I was hoping it wasn't. > a simple setup script that > modifies the user's .tcshrc to include the appropriate PYTHONPATH will > solve his other problem. I haven't seen a program that needs separate installation procedure for each user. Maybe mail clients? But I have seen plenty of programs that can be installed with a simple copy. Maybe that's just not true for Python projects except with the simplest of organizations -- http://mail.python.org/mailman/listinfo/python-list
Re: easy install
On 2009-10-09 19:08 PM, David Robinow wrote: On Fri, Oct 9, 2009 at 5:02 PM, Ethan Furman wrote: A puzzlement: I used easy_install the other day to get xlutils on my system. It automatically installed xlrd and xlwt as well. This is cool. What's not so cool are my tracebacks. E.g. Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. --> import xlwt --> xlwt.__name__ 'xlwt' --> xlwt.__file__ 'C:\\Python25\\lib\\site-packages\\xlwt-0.7.2-py2.5-win32.egg\\xlwt\\__init__.pyc' --> xlwt.Workbook().save('non-file') Traceback (most recent call last): File "", line 1, in File "c:\docume~1\ethanf\locals~1\temp\easy_install-q1s1rb\xlwt-0.7.2-py2.5-win32.egg.tmp\xlwt\Workbook.py", line 634, in save File "c:\docume~1\ethanf\locals~1\temp\easy_install-q1s1rb\xlwt-0.7.2-py2.5-win32.egg.tmp\xlwt\Workbook.py", line 615, in get_biff_data IndexError: list index out of range --> Anyone know why that is? ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list You can't save a workbook with no worksheets. Try: W = xlwt.Workbook() W.add_sheet('no-sheet') W.save('non-file') I think he's asking why the exception message does not have the source lines in the traceback, like exceptions inside regularly installed packages. The answer is that once files are zipped, like the egg that you have installed, the traceback printing function in C does not know how to get at the source files any more. The traceback printing function in the pure Python traceback module does, though. Try this: >>> import sys >>> import traceback >>> sys.excepthook = traceback.print_exception -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco -- http://mail.python.org/mailman/listinfo/python-list
Re: organizing your scripts, with plenty of re-use
On Oct 9, 4:37 pm, Buck wrote: > We don't have any such convention. The production code is at a well- > known path, but I need the code to be fully relocatable (cp -r /known/ > path ~/mydir) for testing purposes. > > Here's a scenario. A user does a cvs checkout into some arbitrary > directory and sees this: > > project/ > +-- python/ > +-- animals.py > +-- mammals/ > +-- horse.py > +-- otter.py > +-- reptiles/ > +-- gator.py > +-- newt.py > +-- misc/ > +-- lungs.py > +-- swimming.py > > These are all runnable scripts that "just work" with no extra effort > or knowlege, both in the testing scenario above, and for normal users > that run it from some central location (maybe "/tools/mycompany/bin/ > mammals"). > > The frustrating thing, for me, is that all these requirements are met > if you leave the scripts in jumbled into a flat directory. As soon as > you start organizing things, you need a good amount of boilerplate in > each script to make things work anything like they did with the flat > directory. > > I wouldn't mind having to write 'from mammals.horse import Horse' or > 'from .animals import Animal' but it's not that simple. Ok, presumably each of these files has something like the following code inside them, since they can all be both imported and run as scripts, right? if __name__ == '__main__': main() So follow these steps. 1. Remove this section from all of the files. If the section contains anything nontrivial, move the nontrivial stuff into your main() function, or whatever function it is that call. I'll just assume that the function is called main. 2. In every directory and subdirectory below python, touch an __init__.py file. It can be empty. You don't need one for the python directory itself. 3. Change all your imports to package imports. I recommend using absolute imports only, but that's up to you. "import otter" would become "from mammals import otter", and so on. 4. Add the following basic script to your Python directory. This is a script, NOT A MODULE, so don't ever import it. In fact, don't even give it a py extension if you're not on Windows. I'll call the script "sa", for "simulate animals". This script accepts the name of a module to import on the command line, checks whether the top level module is acceptable, imports the module, then calls the main function in that module. Enhance as required. #!/usr/bin/python import sys allowed_top_levels = set(('animals','mammals','reptiles','misc')) modname = sys.argv[1] modpath = modname.split('.') if modpath[0] not in allowed_top_levels: raise RuntimeError('invalid top level module specified') mod = __import__(modname) for pkgname in modpath[1:]: mod = getattr(mod,pkgname) mod.main() # or main(*sys.argv[2:]), or however you call it 5. Tell your users to stop entering commands like this: $ ./python/mammal/otter.py And start entering commands like this: $ sa mammal.otter With a little care this method will work fine, and will be installable with cp -r. Carl Banks -- http://mail.python.org/mailman/listinfo/python-list
Re: easy install
Robert Kern wrote: On 2009-10-09 19:08 PM, David Robinow wrote: On Fri, Oct 9, 2009 at 5:02 PM, Ethan Furman wrote: A puzzlement: I used easy_install the other day to get xlutils on my system. It automatically installed xlrd and xlwt as well. This is cool. What's not so cool are my tracebacks. E.g. Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. --> import xlwt --> xlwt.__name__ 'xlwt' --> xlwt.__file__ 'C:\\Python25\\lib\\site-packages\\xlwt-0.7.2-py2.5-win32.egg\\xlwt\\__init__.pyc' --> xlwt.Workbook().save('non-file') Traceback (most recent call last): File "", line 1, in File "c:\docume~1\ethanf\locals~1\temp\easy_install-q1s1rb\xlwt-0.7.2-py2.5-win32.egg.tmp\xlwt\Workbook.py", line 634, in save File "c:\docume~1\ethanf\locals~1\temp\easy_install-q1s1rb\xlwt-0.7.2-py2.5-win32.egg.tmp\xlwt\Workbook.py", line 615, in get_biff_data IndexError: list index out of range --> Anyone know why that is? ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list You can't save a workbook with no worksheets. Try: W = xlwt.Workbook() W.add_sheet('no-sheet') W.save('non-file') I think he's asking why the exception message does not have the source lines in the traceback, like exceptions inside regularly installed packages. The answer is that once files are zipped, like the egg that you have installed, the traceback printing function in C does not know how to get at the source files any more. The traceback printing function in the pure Python traceback module does, though. Try this: >>> import sys >>> import traceback >>> sys.excepthook = traceback.print_exception Good to know, thank you. Besides missing the source lines, I was also wondering why the path in the traceback is showing incorrectly; I have the egg in c:\python25\Lib\site-packages, not where the trackback says I have it. In an effort to get it looking right, as well as to get the missing source lines, I broke the egg and copied the xlwt folder out of it and directly into site-packages -- everything (except the trackbacks, grrr) still worked, leaving me as mystified as ever. Any light to shed on that strangeness? ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list
Re: Reading hex to int from a binary string
Luc schrieb: On Oct 9, 10:45 am, "Diez B. Roggisch" wrote: Luc schrieb: On Oct 8, 11:13 pm, "Diez B. Roggisch" wrote: Luc schrieb: Hi all, I read data from a binary stream, so I get hex values as characters (in a string) with escaped x, like "\x05\x88", instead of 0x05. I am looking for a clean way to add these two values and turn them into an integer, knowing that calling int() with base 16 throws an invalid literal exception. Any help appreciated, thanks. Consider this (in the python interpreter): >>> chr(255) '\xff' >>> chr(255) == r"\xff" False >>> int(r"ff", 16) 255 In other words: no, you *don't* get hex values. You get bytes from the stream "as is", with python resorting to printing these out (in the interpreter!!!) as "\xXX". Python does that so that binary data will always have a "pretty" output when being inspected on the REPL. But they are bytes, and to convert them to an integer, you call "ord" on them. So assuming your string is read bytewise into two variables a & b, this is your desired code: >>> a = "\xff" >>> b = "\xa0" >>> ord(a) + ord(b) 415 HTH, Diez Sorry I was not clear enough. When I said "add", I meant concatenate because I want to read 0x0588 as one value and ord() does not allow that. (ord(a) << 8) + ord(b) Diez Yes that too. But I have four bytes fields and single bit fields to deal with as well so I'll stick with struct. For the future: it helps describing the actual problem, not something vaguely similar - this will get you better answers, and spare those who try to help you the effort to come up with solutions that aren't ones. Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: easy install
En Fri, 09 Oct 2009 21:49:46 -0300, Ethan Furman escribió: Besides missing the source lines, I was also wondering why the path in the traceback is showing incorrectly; I have the egg in c:\python25\Lib\site-packages, not where the trackback says I have it. In an effort to get it looking right, as well as to get the missing source lines, I broke the egg and copied the xlwt folder out of it and directly into site-packages -- everything (except the trackbacks, grrr) still worked, leaving me as mystified as ever. Delete all the .pyc files and let Python regenerate them (or use the compileall module). Code objects (stored in the .pyc files) carry their source file name with them. -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: easy install
David Robinow wrote: On Fri, Oct 9, 2009 at 5:02 PM, Ethan Furman wrote: A puzzlement: I used easy_install the other day to get xlutils on my system. It automatically installed xlrd and xlwt as well. This is cool. What's not so cool are my tracebacks. E.g. Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. --> import xlwt --> xlwt.__name__ 'xlwt' --> xlwt.__file__ 'C:\\Python25\\lib\\site-packages\\xlwt-0.7.2-py2.5-win32.egg\\xlwt\\__init__.pyc' --> xlwt.Workbook().save('non-file') Traceback (most recent call last): File "", line 1, in File "c:\docume~1\ethanf\locals~1\temp\easy_install-q1s1rb\xlwt-0.7.2-py2.5-win32.egg.tmp\xlwt\Workbook.py", line 634, in save File "c:\docume~1\ethanf\locals~1\temp\easy_install-q1s1rb\xlwt-0.7.2-py2.5-win32.egg.tmp\xlwt\Workbook.py", line 615, in get_biff_data IndexError: list index out of range --> Anyone know why that is? ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list You can't save a workbook with no worksheets. Try: W = xlwt.Workbook() W.add_sheet('no-sheet') W.save('non-file') Yes, I know. I was showing the irritating (at least to me) traceback message. But thanks for trying! :D ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list
Re: "smtplib.SMTPServerDisconnected: please run connect() first"
In Ethan Furman writes: >The line preceeding it, >s = smtplib.SMTP() >needs to have an e-mail server specified. E.g. >s = smtplib.SMTP('localhost') # from the 2.5 docs Perfect. Thanks! kynn -- http://mail.python.org/mailman/listinfo/python-list
Re: No threading.start_new_thread(), useful addition?
Christian Heimes wrote: Laszlo Nagy wrote: IMHO it is much cleaner to implement this as a decorator. Pro: transparent passing of positional and keyword arguments, keeps function documentation. You are entitled to your opinion but I STRONGLY recommend against your decorator. You MUST NOT start threads a a side effect of a module import. It can lead to severe bugs and dead lock the entire interpreter. I'm speaking as an experienced Python developer and a CPython core developer. Please trust me in this. The interaction between the import system, its global import lock and threads can and will lead to surprising and hard to debug bugs. A well designed application loads its modules first and then initializes its components explicitly. You don't want to fire up threads randomly! Christian Okay, I for one will never start threads as a side-effect of module import. Other than that, are there other inherent problems with using that decorator at non-import times? ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list
Re: Need feedback on subprocess-using function
In Nobody writes: >You could always lift the code from Popen._communicate(), which uses >threads for Windows and select() for POSIX. Thanks. A lot of useful advice in your replies. G. -- http://mail.python.org/mailman/listinfo/python-list
Poll on Eval in Python
I am Lisp programmer and I write an article on issues as macros, fexprs and eval. I want to compare opinions of programmers of various programming languages on eval. If you want to contribute your opinion on eval in Python (or you want to look at result), the adress is: http://kazimirmajorinc.blogspot.com Thank you, -- Kazimir Majorinc -- http://mail.python.org/mailman/listinfo/python-list
Re: Reading hex to int from a binary string
Luc wrote: Hi all, I read data from a binary stream, so I get hex values as characters (in a string) with escaped x, like "\x05\x88", instead of 0x05. I am looking for a clean way to add these two values and turn them into an integer, knowing that calling int() with base 16 throws an invalid literal exception. Any help appreciated, thanks. Hi, Check out the ord() function. Example: x = '\x34' print ord(x) output: 52 Also, if you, lets say read(4), and end up with `x = '\x05\x41\x24\x00' you can use x[i] to address each character. So if you print x[1] output: 'A' That should be enough to get you started in the right direction. -Jack -- http://mail.python.org/mailman/listinfo/python-list
Re: Persistent Distributed Objects
Sorry about being interpreted as being vague. `et me try to narrow it down. program a creates objects b c d which each need to use 1 disk space 2 ram 3 processor time. I would like to create a heckpoint which would save the work of the object to be later used and then delete it from memory [which I assume from reading about them implimented elsewhere is more or less how they work]. The other part would be to assign the objects via a network depending on the number of network machines. I.e. suppose b c and d see machines e f g on a network they can send those machines objects b c and d and have any work saved on the main machine or local ones. What I was wondering is how would I do that in python. The arbitrary part could be no more complicated than a program where b is calculating a list of prime numbers from x to infinity c is just a notepad program and d is a program that prints say the size of the file as youi type it and says " I like writing a poem of [bytes big] while m y computer foun ou that [new prime] is the biggest on [machines #] computers. Rmi supposedly does this for the distribuited part. Object persistence does this for saving to the best of what I`ve seen nothing distributes and saved at the same time. Does that help? The death of one man is a tragedy. The death of ten million is a statistic--- Joseph Stalin --Original Message-- From: Laszlo Nagy To: "John Haggerty" ,"python-list (General)" Date: Fri, 9 Oct 2009 09:18:12 PM +0430 Subject: Re: Persistent Distributed Objects > > > I've seen evidence about this being done wrt what looks like insanely > complex stuff on this list but I'm wondering if there is something to > do this with any number of nodes and just farm out random > classes/objects to them? Designing and opreating distributed systems is a complex thing. Especially if you do not want to specify an exact problem domain that you needed to solve - then you need to design a distributed system that is able to solve general problems. It is very complex, and - at least to my knowledge - there is no efficient solution out of the box. This is not Python specific. It would be a hard task just to design how that system should work, regardless of the computer language it is implemented in. L -- http://mail.python.org/mailman/listinfo/python-list
How to find number of line that is currently executing?
I would like to put a statement on line N of my program that prints the line number that is currently executing. This may sound fairly trivial, but I don't want to hard code the line number because N will change if lines are inserted or deleted above that point. Any advice will be appreciated. -- View this message in context: http://www.nabble.com/How-to-find-number-of-line-that-is-currently-executing--tp25830766p25830766.html Sent from the Python - python-list mailing list archive at Nabble.com. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to find number of line that is currently executing?
On Fri, Oct 9, 2009 at 8:46 PM, Dr. Phillip M. Feldman wrote: > > I would like to put a statement on line N of my program that prints the line > number that is currently executing. This may sound fairly trivial, but I > don't want to hard code the line number because N will change if lines are > inserted or deleted above that point. Any advice will be appreciated. If you're doing this for debugging, it's often more convenient to just come up with a unique string you can grep for, but anyway: import traceback print traceback.extract_stack()[-1][1] Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Are there any modules for IRC, that work with Python 3.1?
Does anyone know of any modules for dealing with the IRC protocol, that will work with Python 3.1? It doens't have to be super great, just less time consuming then playing with sockets directly (and obv. stable). The only module in my systems package manager is irclib for Python 2.6. I can live with writing code for Python 2.4+ easily but, ahem, I think it would be wise to write new code around Python 3.1 instead... so yeah, here we are. Searching Google doesn't offer much encouragement - just irclib and oyoyo. Has anyone here tried using oyoyo with Python 3.1, and is there anything else I might have missed? # circumstances Having recently been put into search for a new IRC client, and everything I've thrown in the cauldron having become a disappointment... let's just say, I've come to a conclusion -- either I'm going to install ircII and live with whatever it has to offer(!), or hash out something quickly in Python that fits my needs. If I'm considering writing an IRC client, it makes sense to check for modules implementing the protocol before I have to roll something myself, but nothing seems to fit the bill. (For those that don't know it, ircII is a really freaking old Internet Rely Chat client ;) -- TerryP -- http://mail.python.org/mailman/listinfo/python-list
Re: A new Internet-search website written in Python
hrg...@gmail.com wrote: The purpose of this email is to inform the Python-list mailing-list subscribers of an Internet-search website that is run by software written in Python. All the site seems to do is frame the results from other search engines. What does the Python code do? John Nagle -- http://mail.python.org/mailman/listinfo/python-list