Re: XML partial validation.
José Rui Faustino de Sousa wrote: I am writing a text to XML parser that as to be easily extensible (via new text format plug-ins) and modifiable if the XML format used changes. Since the text order does not match the XML document order I have to use a package that allows DOM-like handling of XML (elementtree for instance). The XML DTD is still (and most likelly will be) in a state of evolution. To make life much easier for future developers what I really needed was some package that implemented something like DOM level 3 XML partial validation. There seems to be already something implemented in PyXML (at least the Validition-Err exception is there) but there is no documentation and I can not figure how to turn partial validation on. lxml supports RelaxNG and XMLSchema besides DTDs, you might have more luck with those, especially since they are XML, so you can work on the schema documents and extract the sections that are relevant for a specific XML fragment. lxml is compatible with the ElementTree API, so you can reuse the ET code you already have. Stefan -- http://mail.python.org/mailman/listinfo/python-list
Alternative to python -u for binary upload to cgi on windows?
Hi all, Using a python cgi script such as the one below to handle uploaded binary files will end up with a truncated file (truncates when it hits ^Z) on Windows systems. On linux systems the code works and the file is not truncated. One solution for Windows is to use the -u flag, i.e. #!C:\Python\python.exe -u Is there another fix that doesn't require python to be run in unbuffered mode? What performance hits would I expect for running in unbuffered mode? Best regards, Cameron. Example code: import cgi, os.path as path form = cgi.FieldStorage() new_file = form.getvalue(new_file,None) if new_file is not null: fname = path.split(form[new_file].filename)[-1] fle = open(fname,wb) fle.write(new_file) fle.close() -- http://mail.python.org/mailman/listinfo/python-list
Re: RegExp Help
On Thu, 13 Dec 2007 17:49:20 -0800, Sean DiZazzo wrote: I'm wrapping up a command line util that returns xml in Python. The util is flaky, and gives me back poorly formed xml with different problems in different cases. Anyway I'm making progress. I'm not very good at regular expressions though and was wondering if someone could help with initially splitting the tags from the stdout returned from the util. […] Can anyone help me? Flaky XML is often produced by programs that treat XML as ordinary text files. If you are starting to parse XML with regular expressions you are making the very same mistake. XML may look somewhat simple but producing correct XML and parsing it isn't. Sooner or later you stumble across something that breaks producing or parsing the naive way. Ciao, Marc 'BlackJack' Rintsch -- http://mail.python.org/mailman/listinfo/python-list
Re: Best way to protect my new commercial software.
Let me be clear for you: there are someone in my company who love to use my software in other companies that she works there also. and because it is an inhouse tool, my CEO wanted me to protect it from stealing. and really we havn't time to copyright it. so I want to secure my software from some people who love to steal and use it. I am an animator and 3d programmer, what I wrote is some fast technology for calculating sub surface lightning (SSS) using mental ray and renderman. indded this tool works with maya and 3delight and mental ray standalone. very complicated process and took me tree months to wrote it. I am not a python pro, I know C++, maya Api, mel,... but I use python because it is really faster than all of them. hope you understand why I want to protect it, even if it will slower. -- http://mail.python.org/mailman/listinfo/python-list
Re: urlparse.urlparse bug - misparses long URL
Matt Nordhoff wrote: John Nagle wrote: Here's a hostile URL that urlparse.urlparse seems to have mis-parsed. ... It's breaking on the first slash, which just happens to be very late in the URL. urlparse('http://example.com?blahblah=http://example.net') ('http', 'example.com?blahblah=http:', '//example.net', '', '', '') That's what it seems to be doing: sa1 = 'http://example.com?blahblah=/foo' sa2 = 'http://example.com?blahblah=foo' print urlparse.urlparse(sa1) ('http', 'example.com?blahblah=', '/foo', '', '', '') # WRONG print urlparse.urlparse(sa2) ('http', 'example.com', '', '', 'blahblah=foo', '') # RIGHT That's wrong. RFC3896 (Uniform Resource Identifier (URI): Generic Syntax), page 23 says The characters slash (/) and question mark (?) may represent data within the query component. Beware that some older, erroneous implementations may not handle such data correctly when it is used as the base URI for relative references (Section 5.1), apparently because they fail to distinguish query data from path data when looking for hierarchical separators. So urlparse is an older, erroneous implementation. Looking at the code for urlparse, it references RFC1808 (1995), which was a long time ago, three revisions back. Here's the bad code: def _splitnetloc(url, start=0): for c in '/?#': # the order is important! delim = url.find(c, start) if delim = 0: break else: delim = len(url) return url[start:delim], url[delim:] That's just wrong. The domain ends at the first appearance of any character in '/?#', but that code returns the text before the first '/' even if there's an earlier '?'. A URL/URI doesn't have to have a path, even when it has query parameters. This bug is in Python 2.4 and 2.5. I'll file a bug report. John Nagle SiteTruth -- http://mail.python.org/mailman/listinfo/python-list
Re: Better way to searching.
Hello, You might try using os.path.walk, I think it ends up being much more flexible than glob for this. #!/usr/bin/python2.4 import os def Finder(topdir, file_extension=None, levels=None): Return all filenames in topdir. Args: topdir: Top level directory to search file_extension: file extension to match on levels: number of directory levels to search Returns: list of file names. def Levels(pathname): return pathname.count(os.path.sep) if levels is not None: pathlevels_allowed = Levels(topdir) + levels def WalkFunction(accumulator, dir, filenames): Called from os.path.walk; accumulate matching filenames. Args: accumulator: list we are adding files to dir: directory being traversed filenames: list of files in dir Returns: None Modifies: filenames, if we want to stop descending. if levels is not None: if Levels(dir) pathlevels_allowed: del filenames[:] return if file_extension is not None: accumulator.extend(os.path.join(dir, f) for f in filenames if os.path.splitext(f)[1] == file_extension) else: accumulator.extend(os.path.join(dir, f) for f in filenames) acc = [] os.path.walk(topdir, WalkFunction, acc) return acc if __name__ == '__main__': files = Finder(/your/starting/directory, file_extension=.mpg, levels=2) print files On Dec 13, 2007 11:13 PM, farsheed [EMAIL PROTECTED] wrote: my code is here: _ def Globing(self, dir, extension, nop, inputDepth): 'It creates a basic glob function that needed in other classes' self.exop = '*.' self.opr = '*/' self.counter = '' self.files = [] self.path = '' for i in range (inputDepth): self.path = dir + self.opr * i + self.exop * nop + extension #like this:*/*/*.*.mpg self.counter = glob.glob(self.path) self.files += self.counter return self.files Is there any better and faster way to do this? -- http://mail.python.org/mailman/listinfo/python-list -- -David -- http://mail.python.org/mailman/listinfo/python-list
Re: RegExp Help
On Dec 14, 12:04 am, Marc 'BlackJack' Rintsch [EMAIL PROTECTED] wrote: On Thu, 13 Dec 2007 17:49:20 -0800, Sean DiZazzo wrote: I'm wrapping up a command line util that returns xml in Python. The util is flaky, and gives me back poorly formed xml with different problems in different cases. Anyway I'm making progress. I'm not very good at regular expressions though and was wondering if someone could help with initially splitting the tags from the stdout returned from the util. [...] Can anyone help me? Flaky XML is often produced by programs that treat XML as ordinary text files. If you are starting to parse XML with regular expressions you are making the very same mistake. XML may look somewhat simple but producing correct XML and parsing it isn't. Sooner or later you stumble across something that breaks producing or parsing the naive way. Ciao, Marc 'BlackJack' Rintsch It's not really complicated xml so far, just tags with attributes. Still, using different queries against the program sometimes offers differing results...a few examples: id 123456 / tag name=foo / tag2 name=foo moreattrs=... /tag2 tag3 name=foo moreattrs=... tag3/ It's consistent (at least) in that consistent queries always return consistent tag styles. It's returned to stdout with some extra useless information, so the original question was to help get to just the tags. After getting the tags, I'm running them through some functions to fix them, and then using elementtree to parse them and get all the rest of the info. There is no api, so this is what I have to work with. Is there a better solution? Thanks for your ideas. ~Sean -- http://mail.python.org/mailman/listinfo/python-list
Re: Is a real C-Python possible?
sturlamolden a écrit : On 13 Des, 19:16, Chris Mellon [EMAIL PROTECTED] wrote: Personally I find properties atrocious and unsafe. What a strange observation from someone wanting to introduce defmacros and customizable syntax in Python One cannot distinguish between a function call and binding an attribute in a statement like: FWIW, binding an attribute will *alway* require some function call... Properties - or any other computed attributes - are just hooks into the default __setattr__ implementation so you can customize it. foo.bar = 2 # Does this call a function or bind an attribute? From the client code POV, it binds an attribute - whatever the implementation is. From the implementation POV, it will always call a couple functions. What's you point, exactly ? # Is this foo.setBar(2) or setattr(foo,'bar',2)? Why do you care ? Ever heard about the concept of encapsulation ? Even worse: if we make a typo, the error will not be detected as the syntax is still valid. So what ? This has nothing to do with properties. Properties and dynamic binding do not mix. Sorry, but IMVHO, this is total bullshit. -- http://mail.python.org/mailman/listinfo/python-list
Re: making all letters Caps/Small Letters
Merrigan wrote: I'm sure I have done this before, but cannot remember how, or find out how to do it quickly - but is there a way/function/something in python to make all the letters of a raw_input() string small/capital letters? Typing dir() in the interactive interpreter gives you a list of candidates. If you think that title is a good candidate try it with alpha BETA.title() 'Alpha Beta' Probably not what you want, but there are more. Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: making all letters Caps/Small Letters
THIS IS A STRING.lower() 'this is a string' THIS IS A STRING.title() 'This Is A String' this is a string.upper() 'THIS IS A STRING' You can browse all the string methods by doing dir(str) On Dec 14, 2007 1:30 AM, Merrigan [EMAIL PROTECTED] wrote: Hi There, I'm sure I have done this before, but cannot remember how, or find out how to do it quickly - but is there a way/function/something in python to make all the letters of a raw_input() string small/capital letters? Thanks! -- Merrigan -- http://mail.python.org/mailman/listinfo/python-list -- -David -- http://mail.python.org/mailman/listinfo/python-list
making all letters Caps/Small Letters
Hi There, I'm sure I have done this before, but cannot remember how, or find out how to do it quickly - but is there a way/function/something in python to make all the letters of a raw_input() string small/capital letters? Thanks! -- Merrigan -- http://mail.python.org/mailman/listinfo/python-list
Re: reloading modules and isinstance()
Hi, I have found that it is possible to reassign the instance.__class__ reference to the same class (but after reloading) to make the isinstance() test work again! I know that it is a kind of hacking of the Python interpreter, but it works :-) -- http://mail.python.org/mailman/listinfo/python-list
Re: Eclipse/PyQt/Eric4 question
On 14 Δεκ, 01:09, Fabio Zadrozny [EMAIL PROTECTED] wrote: Hmmm...but this means that i am forced to do this for ALL .ui files on the project, either changed or not and this can slow things down... (pyuic.bat can run for one or for ALL .ui files) The goal is to find a way to automatically do this only for the changed ones, like eric does... When you do a new builder, you can make it be run only when a file is changed, and you can select as arguments only receiving the files changed (build_files)... take a look at the variables to specify as arguments (you could also select 'python.exe' as the external program and choose some python script as argument + the files changed). The auto-build can be specified at the build options tab. Cheers, Fabio Axa! I didn't know (or better, i didn't experiment enough) this...So, basically you tell me that this is possible on Eclipse! I will test it at home on the afternoon, is sounds great! -- http://mail.python.org/mailman/listinfo/python-list
Re: Floating point subtraction rounding error (NOT display error)
Solved: used round(number,12) in this case for all of the operands of my arcsines. Not pretty, but at least VIM made it easy... You might have the same problem though: round(1.0003401032523500235,13) 1.000340103 round(1.0003401032523500235,12) 1.00034011 -- http://mail.python.org/mailman/listinfo/python-list
Re: Best way to protect my new commercial software.
sturlamolden wrote: I wrote this in another thread, And here the HOWTO for the crack: 1. Put all the compiled Python bytecode in a heavily encrypted binary file. Consider using a hardware hash in the key. Find the part in the binary where the encrypted bytecode is read, start the binary in a VM to which a debugger is attached (can't be detected, as it's in a VM) and put a watchpoint for any access on the encrypted binary. 2. Program a small binary executable (.exe file) in C or C++ that: 2a. Reads the binary file. Debugger intercepts it. 2b. Decrypts it to conventional Python byte code. Record where the decoder puts the decrypted bytecode in memory. 2c. Embeds a Python interpreter. Replace the call of the Python interpreter with a small shellcode that writes the decrypted code to a file. 2d. Executes the bytecode with the embedded Python interpreter. Execute that file with the standalone interpreter. I will not make reverse engineering impossible, but it will be extremely difficult. No. It's just a matter of reading the decrypted bytecode from memory. Since Python bytecode is independent from any containing file, it's very hard to test if a certain bytecode runs from a valid or cracked container. Any sort of bytecode will sooner or later run through some interpreter, where it can be ultimately tapped. And unlike some CPU binary a bytecode also delivers all information to deobfuscate it. So even self modifying code doesn't help here. Wolfgang Draxinger -- E-Mail address works, Jabber: [EMAIL PROTECTED], ICQ: 134682867 -- http://mail.python.org/mailman/listinfo/python-list
Re: making all letters Caps/Small Letters
Merrigan schrieb im I'm sure I have done this before, but cannot remember how, or find out how to do it quickly - but is there a way/function/something in python to make all the letters of a raw_input() string small/capital letters? upper might help.upper() OR LOWER.lower() HTH Martin -- http://mail.python.org/mailman/listinfo/python-list
Re: mailbox.Maildir question/problem
Ross Ridge [EMAIL PROTECTED] wrote: [EMAIL PROTECTED] wrote: Is there *any* way I can get python to access maildirs which are not named using this (IMHO stupid) convention? Well, the mailbox module doesn't support deleting mailboxes, so I'm not sure why you want to use it. I was hoping to be able to use it for other things as well as deleting mailboxes. Since you also seem to have a better idea of what your maildirs look like, why not just use the lower level file functions directly? Something like: def remove_empty_maildir(dirname): expected = set([cur, new, tmp]) ents = set(os.listdir(dirname)) if ents != expected: if expected.issubset(ents): raise error, unexpected subdirs in maildir raise error, not a maildir subdirs = [os.path.join(dirname, d) for d in expected] for d in subdirs: if len(os.listdir(d)) != 0: return False for d in subdirs: os.rmdir(d) os.rmdir(dirname) return True Your case is presumably different somehow, so you'll have to update and fix this completely untested code if you want to use it. I guess I will have to do something like this but the problem is more subtle than that, what if another program writes a new message to the mailbox just after you've checked that cur, new and tmp are all empty? The whole point of maildir is that locking isn't needed and I was hoping that the maildir() object in python would encapsulate correct handling of the maildir including deletion. As it is I will have to write code to do the correct handling, presumably one checks the new directory last before deleting the whole maildir and, if the deletion fails, someone must have put something there. -- Chris Green -- http://mail.python.org/mailman/listinfo/python-list
Re: making all letters Caps/Small Letters
On Dec 14, 11:30 am, Merrigan [EMAIL PROTECTED] wrote: Hi There, I'm sure I have done this before, but cannot remember how, or find out how to do it quickly - but is there a way/function/something in python to make all the letters of a raw_input() string small/capital letters? Thanks! -- Merrigan as it's a string, it supports string functions. .upper() .lower() -- http://mail.python.org/mailman/listinfo/python-list
Re: RegExp Help
En Fri, 14 Dec 2007 06:06:21 -0300, Sean DiZazzo [EMAIL PROTECTED] escribió: On Dec 14, 12:04 am, Marc 'BlackJack' Rintsch [EMAIL PROTECTED] wrote: On Thu, 13 Dec 2007 17:49:20 -0800, Sean DiZazzo wrote: I'm wrapping up a command line util that returns xml in Python. The util is flaky, and gives me back poorly formed xml with different problems in different cases. Anyway I'm making progress. I'm not very good at regular expressions though and was wondering if someone could help with initially splitting the tags from the stdout returned from the util. Flaky XML is often produced by programs that treat XML as ordinary text files. If you are starting to parse XML with regular expressions you are making the very same mistake. XML may look somewhat simple but producing correct XML and parsing it isn't. Sooner or later you stumble across something that breaks producing or parsing the naive way. It's not really complicated xml so far, just tags with attributes. Still, using different queries against the program sometimes offers differing results...a few examples: id 123456 / tag name=foo / tag2 name=foo moreattrs=... /tag2 tag3 name=foo moreattrs=... tag3/ Ouch... only the second is valid xml. Most tools require at least a well formed document. You may try using BeautifulStoneSoup, included with BeautifulSoup http://crummy.com/software/BeautifulSoup/ I found something that works, although I couldn't tell you why it works. :) retag = re.compile(r'.+?', re.DOTALL) tags = retag.findall(retag) Why does that work? That means: look for a less-than sign (), followed by the shortest sequence of (?) one or more (+) arbitrary characters (.), followed by a greater-than sign () If you never get nested tags, and never have a inside an attribute, that expression *might* work. But please try BeautifulStoneSoup, it uses a lot of heuristics trying to guess the right structure. Doesn't work always, but given your input, there isn't much one can do... -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: Floating point subtraction rounding error (NOT display error)
En Fri, 14 Dec 2007 00:22:18 -0300, Keflavich [EMAIL PROTECTED] escribió: On Dec 13, 5:52 pm, Steven D'Aprano [EMAIL PROTECTED] cybersource.com.au wrote: On Thu, 13 Dec 2007 14:30:18 -0800, Keflavich wrote: Hey, I have a bit of code that died on a domain error when doing an arcsin, and apparently it's because floating point subtraction is having problems. I'm not convinced that your diagnosis is correct. Unless you're using some weird, uncommon hardware, it's unlikely that a bug in the floating point subtraction routines has escaped detection. (Unlikely, but not impossible.) Can you tell us what values give you incorrect results? Here's a better (more complete) example [btw, I'm using ipython]: In [39]: x = 3.1 + .6 In [40]: y = x - 3.1 In [41]: y == 6 Out[41]: False In [42]: x == 3.7 Out[42]: True In [43]: 3.1+.6-3.1 == .6 Out[43]: False In [45]: (3.1+.6-3.1)/.6 Out[45]: 1.0002 In [46]: (3.1+.6-3.1)/.6 == 1 Out[46]: False In [47]: (3.1+.6-3.1)/.6 1 Out[47]: True Let's see how these float numbers are represented: significand * 2**exponent where 1=significand2 (like scientific notation but using base 2, not base 10) and with 53 binary digits available for the significand (this is more or less the format used by IEEE754 floating point, implemented in hardware on all platforms where Python is available and I know of, except maybe some cell phones): 3.1 = 3.1001 = 0.77502 x 2**2 = 1.1000110011001100110011001100110011001100110011001101 x 2**1 0.6 = 0.59998 = 0.59998 x 2**0 = 1.0011001100110011001100110011001100110011001100110011 x 2**-1 3.1+0.6 = 3.7002 = 0.92504 x 2**2 = 1.1101100110011001100110011001100110011001100110011010 x 2**1 3.1+0.6-3.1 = 0.60009 = 0.60009 x 2**0 = 1.0011001100110011001100110011001100110011001100110100 x 2**-1 where the 1. are binary fractions. Let's compute 3.1+0.6 using the binary form: 3.1 = 11.000110011001100110011001100110011001100110011001101 0.6 = .100110011001100110011001100110011001100110011001100 11 sum = 11.101100110011001100110011001100110011001100110011010 Notice that, to align both numbers at their decimal point (hmm, binary point!), we had to discard the two less significant binary digits of 0.6. The result, however, is still the same as if we had done the computation exactly and then rounded to 53 significant binary digits. (that's why x == 3.7 is True in your example above). Now let's substract 3.1 from the result: sum = 11.101100110011001100110011001100110011001100110011010 3.1 = 11.000110011001100110011001100110011001100110011001101 dif = .100110011001100110011001100110011001100110011001101 __ There are 51 significant bits there; as we use 53 bits in the representation, the two less significant bits are set to 0 (they're unknown, in fact). We lose those 2 bits because we are substracting numbers close to each other (this is known as cancellation). The error is only 1 unit of the least significant binary digit (1x2**-53) but enough to make that number different to the representation of 0.6 (they differ by the least possible amount). Note that this is the best result you can get with a limited precision of 53 bits, and it's not even Python who computes the value, but the underlying C math library (and very likely, using the hardware). IEEE754 defines substraction very precisely so people can get reliable results on any platform, and this is not an exception. I know about the impossibility of storing floating point numbers precisely, but I was under the impression that the standard used for that last digit would prevent subtraction errors from compounding. What gave you that impression? Are you referring to guard digits? I believe that's what I was referring to; I have only skimmed the topic, haven't gone into a lot of depth Substraction of numbers of similar magnitude is often a problem, like addition of very dissimilar numbers. I should also mention that of course your answer will deviate, due to the finite precision of floats. I'm adding and subtracting things with 1 decimal point; I can do the math for an given case trivially. Are you suggesting that I don't know what the exact floating point quantity should be? Notice that those values have an exact *decimal* representation but are *periodic* written in binary form, as you can see from above. Any finite binary representation must be approximate then. It's like trying to write 1/3 in decimal form, you can't do that exactly without requiring infinite digits. Have you read this?http://docs.sun.com/source/806-3568/ncg_goldberg.html I started to, but didn't get through the whole thing. I'm saving that for bedtime reading after finals. At least overview the topics - you might not be interested in the theorem proofs and some details, but the consequences and
listdir() with mask
Hello! Is there any easy way to list files using bash-like patterns? Something like listfiles(/var/log/*.log), listfiles(/var/{cache,run}/*). Also, I'll need something like listfiles(/tmp/**/*.tmp), where ** is unlimited number of folders (like is zsh). Thanks and sorry for my English. -- Vladimir Rusinov GreenMice Solutions: IT-решения на базе Linux http://greenmice.info/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Finding overlapping times...
On 2007-12-14, John Machin [EMAIL PROTECTED] wrote: On Dec 14, 10:45 am, Breal [EMAIL PROTECTED] wrote: I have a list that looks like the following [(10, 100010), (15, 17), (19, 100015)] I would like to be able to determine which of these overlap each other. So, in this case, tuple 1 overlaps with tuples 2 and 3. Your definition of overlaps appears to be reflexive, so the following two results follow automatically. Tuple 2 overlaps with 1. Tuple 3 overlaps with tuple 1. In my scenario I would have hundreds, if not thousands of these ranges. Any nice pythonic way to do this? Probably not. Just ensure that (a) your time intervals (start, end) obey start = end (b) your list of intervals is sorted, then get stuck in: # tested no more that what you see alist = [(10, 100010), (15, 17), (19, 100015), (100016, 100017), (100016, 100018)] n = len(alist) for i in xrange(n - 1): istart, iend = alist[i] for j in xrange(i + 1, n): jstart, jend = alist[j] if jstart iend: break print Overlap:, i, alist[i], j, alist[j] After the sort, it looks like O(N**2) in worst case, but you could get a few zillion results done while you are hunting for a faster algorithm. Simply printing out the list of overlaps, even if you knew, a priori, that all elements overlapped, is an O(N**2) operation. So I don't think a better algorithm exists for the worst case. -- Neil Cerutti You only get a once-in-a-lifetime opportunity so many times. --Ike Taylor -- http://mail.python.org/mailman/listinfo/python-list
Re: python vs perl performance test
[EMAIL PROTECTED] wrote: ... When I first translated it to Python verbatim, the Python script took almost 30 secs to run. So far, the best I can do is 11.2 secs using this: from random import randrange from itertools import imap, repeat from operator import getitem, add, getslice result = 0 zeros = [0]*100 for i in xrange (10): s = [chr(randrange(128))] * 1024 This doesn't do what you think it does, I'll wager. Try: s = chr(randrange(128)) * 1024 to get an equivalent result. or try: s = ''.join([chr(randrange(128)) for i in range(1024)]) -Scott -- http://mail.python.org/mailman/listinfo/python-list
Re: listdir() with mask
On Dec 14, 1:56 am, Vladimir Rusinov [EMAIL PROTECTED] wrote: glob or fnmatch http://docs.python.org/lib/module-glob.html rd -- http://mail.python.org/mailman/listinfo/python-list
Re: Best way to protect my new commercial software.
On Dec 14, 9:08 am, farsheed [EMAIL PROTECTED] wrote: Let me be clear for you: there are someone in my company who love to use my software in other companies that she works there also. and because it is an inhouse tool, my CEO wanted me to protect it from stealing. and really we havn't time to copyright it. I don't think it's particularly productive to continue this discussion, given that you're obviously in a situation where you don't have a great deal of flexibility, but I think you and/or your CEO might benefit from listening to the PyCon 2007 talk The Absolute Minimum an Open Source Developer Must Know About Intellectual Property [1]. The speaker misrepresents the FSF somewhat in stating that they don't believe in property (or some similar phrasing - I don't recall the exact choice of words), but aside from this the talk is rather well delivered, with the basic definitions of the different legal instruments described in an approachable fashion. so I want to secure my software from some people who love to steal and use it. It sounds like your CEO has issues with the people he/she employs, first and foremost. Paul [1] Slides available here: http://us.pycon.org/zope/talks/2007/sat/track4/053/talkDetails2 Audio available from here: http://pycon.blogspot.com/2007/11/pycon-2007-podcast.html -- http://mail.python.org/mailman/listinfo/python-list
Re: [Plone-Users] Why Can't I Do This?
...but applicable ONLY for that instance. The other solution provided is more universal ;) On Dec 14, 2007 10:41 AM, Encolpe Degoute [EMAIL PROTECTED] wrote: Victor Subervi a écrit : Hi; Why can't I do this? author = By Juan Garcia if author[0:2] == by | By | BY: if author[0:2].lower() == by: -- Encolpe Degoute INGENIWEB (TM) - S.A.S 5 Euros - RC B 438 725 632 17 rue Louise Michel - 92300 Levallois Perret - France web : www.ingeniweb.com - « les Services Web Ingénieux » Tel : 01.78.15.24.08 / Fax : 01 47 57 39 14 - SF.Net email is sponsored by: Check out the new SourceForge.net Marketplace. It's the best place to buy or sell services for just about anything Open Source. http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace ___ Plone-Users mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/plone-users -- http://mail.python.org/mailman/listinfo/python-list
Re: [Plone-Users] Why Can't I Do This?
Great! Thanks! On Dec 14, 2007 10:38 AM, Eric Smith [EMAIL PROTECTED] wrote: Victor Subervi wrote: Hi; Why can't I do this? author = By Juan Garcia if author[0:2] == by | By | BY: ... author = author[3:] ... Traceback (most recent call last): File stdin, line 1, in ? TypeError: unsupported operand type(s) for |: 'str' and 'str' This is a python question, not a plone question. The python logical or operator is or, but for this you might want to use in. This code might point you in the right direction, although it's not a complete (or very good) solution: author = By Juan Garcia if author[0:2] in [by, By, BY]: author = author[2:] print author - SF.Net email is sponsored by: Check out the new SourceForge.net Marketplace. It's the best place to buy or sell services for just about anything Open Source. http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace ___ Plone-Users mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/plone-users -- http://mail.python.org/mailman/listinfo/python-list
Re: listdir() with mask
On Dec 14, 2:00 pm, Vladimir Rusinov [EMAIL PROTECTED] wrote: Is there any easy way to list files using bash-like patterns? Something like listfiles(/var/log/*.log), listfiles(/var/{cache,run}/*). On 12/14/07, Jeff McNeil [EMAIL PROTECTED] wrote: Sure is.. check out the glob module: http://www.python.org/doc/current/lib/module-glob.html(Official) http://blog.doughellmann.com/2007/07/pymotw-glob.html(PyMOTW) Thanks a lot! For example: import fnmatch DEFAULTPATTERNS = ['*'] DEFAULTIGNOREDIRS = ['.svn'] def find(root=None, patterns=None, ignoredirs=None): patterns = patterns or DEFAULTPATTERNS if ignoredirs is None: ignoredirs = DEFAULTIGNOREDIRS for fname in walkdir(root, ignoredirs=ignoredirs): for pattern in patterns: if fnmatch.fnmatch(fname, pattern): yield fname break def findfiles(root=None, patterns=None, ignoredirs=None): for f in find(root, patterns, ignoredirs=ignoredirs): if os.path.isfile(f): yield f (For walkdir, see os.walk function) (if root == None: use current working directory) hth Gerard -- http://mail.python.org/mailman/listinfo/python-list
Re: Better way to searching.
thanks, I'll try it. -- http://mail.python.org/mailman/listinfo/python-list
Re: Finding overlapping times...
Breal wrote: I have a list that looks like the following [(10, 100010), (15, 17), (19, 100015)] I would like to be able to determine which of these overlap each other In relation to a similar (but not identical) problem, that of finding nested scopes and the associated name tables, I found the following order useful: def range_key(pair): return pair.least, -pair.greatest somelist.sort(key=range_key) If you sort this way, you get nested elements in a quite useful order for 1-pass scanning. Nested elements show up in order of nesting (outer to inner), so a single scan of the result can reveal nesting violations. -Scott David Daniels [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list
Re: Loops and things
I was wondering how and if it's possible to write a loop in python which updates two or more variables at a time. For instance, something like this in C: for (i = 0, j = 10; i 10 j 20; i++, j++) { printf(i = %d, j = %d\n, i, j); } Well, yes it can be done, but depending on your use-case, there might be smarter ways of doing it: for (i,j) in map(lambda i: (i, i+10), xrange(10)): print i = %d, j = %d % (i,j) or just for pair in map(lambda i: (i, i+10), xrange(10)): print i = %d, j = %d % pair or even just for i in xrange(10): print i = %d, j = %d % (i,i+10) If you need varying sources, you can use zip() to do something like for (i,j) in zip(xrange(10), myiter(72)): print i = %d, j = %d % (i,j) where myiter() produces the random sequence of items for j. If they produce voluminous output, you can import itertools and use izip and imap instead. Or, if you want a more literal mapping: i, j = 0, 10 while i 10 j 20: print i = %d, j = %d % (i,j) i += 1 j += 1 Pick your poison. So that I would get: i = 0, j = 0 i = 1, j = 1 i = 2, j = 2 ... ... I'm not sure how, with your code, j could be (0,1,2,...) instead of (10,11,12,...). -tkc -- http://mail.python.org/mailman/listinfo/python-list
Re: Loops and things
[EMAIL PROTECTED] wrote: I was wondering how and if it's possible to write a loop in python which updates two or more variables at a time. For instance, something like this in C: for (i = 0, j = 10; i 10 j 20; i++, j++) { printf(i = %d, j = %d\n, i, j); } So that I would get: i = 0, j = 0 i = 1, j = 1 i = 2, j = 2 ... ... ... i = 9, j = 19 Can this be done in Python? Thanks. You can zip two ranges/iterators to produce successive tuples with values from each iterator respectively, and loop on that: for i,j in zip(range(10),range(10,20)): ... print i,j ... 0 10 1 11 2 12 3 13 4 14 5 15 6 16 7 17 8 18 9 19 Gary Herron -- http://mail.python.org/mailman/listinfo/python-list
Re: simple string formatting question
Neal Becker a écrit : I have a list of strings (sys.argv actually). I want to print them as a space-delimited string (actually, the same way they went into the command line, so I can cut and paste) So if I run my program like: ./my_prog a b c d I want it to print: './my_prog' 'a' 'b' 'c' 'd' This should do what you want: print .join('%s' % arg for arg in sys.argv) NB : if your version of Python predates generator expressions, use a list comp instead: print .join(['%s' % arg for arg in sys.argv]) -- http://mail.python.org/mailman/listinfo/python-list
Re: looking for gui for python code
On Thu, 13 Dec 2007 09:46:32 -0800 (PST), [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: hi i have written some python scripts which take command line arguments and do some job. i would like to make it into a .exe using py2exe and distribute it with innosetup.. befor that i would like to add some GUI support..i mean select some values using a folder explorer etc..which would be a good gui builder for this? Hi, I am not able to help out concerning GUI-Builders. However, it appears that the GUI-support you neeed is not getting too complex. Therefore, I would recommend to hand-code the GUI-parts using TKInter. Advantages: -It is there already. No need to install a separate GUI-Builder and lerning how to use it. Not too much thinking what needs to be installed on your user's workstation. -TKInter and TCL/TK are very stable. -TCL/TK is well documented. (Unfortunately one has to refer to the TCL/TK documentation because the TKInter documentation does not go very far.) -It is quite easy to learn. -It contains a file-browsing dialogue (resp. a wrapper around the one provided by your OS/Desktop-Environment.) You might have a start at the TKinter-Part of the library-reference. http://docs.python.org/lib/module-Tkinter.html You will have to spend some time on learning how to use TK, of course. However, I think that is a good investment rather than spending the time evaluating different GUI-Builders. The said is valid for less complex GUIs. For complex GUIs especially with huge amounts of entry-fields it is better to use a GUI-builder especially for later maintenance. Handcoded GUI-code might get nearly unreadable after it has been changed during the lifetime of a software (, and by different developers) Regards, Thomas -- http://mail.python.org/mailman/listinfo/python-list
simple string formatting question
I have a list of strings (sys.argv actually). I want to print them as a space-delimited string (actually, the same way they went into the command line, so I can cut and paste) So if I run my program like: ./my_prog a b c d I want it to print: './my_prog' 'a' 'b' 'c' 'd' Just print sys.argv will almost work, but it's comma-delimited. There must be some clever way to do this. Any ideas? -- http://mail.python.org/mailman/listinfo/python-list
How to read a binary file into a mysql table
Good morning folks, I cannot read a binary file into a mysql database. Everything I tried did not succeed. What I tried (found from various google lookups...) is this: con = MySQLdb.connect(to server) cur = con.cursor() cur.execute(insert into data values('file1', %s), (open(test.jpg, rb).read(), )) also this doesn't work: execute(insert into data values('file1', %s), (MySQLdb.escape_string(open(test.jpg, rb).read()), )) I always get this: Warning: Data truncated for column 'file' at row 1 The blob data is actually chopped. The Table has two columns, char(100), blob Has someone a working idea how to get binary file into a blob using MySQLdb and python ?! System is SuSE 10.0 Linux with python 2.5.1, current MySQLdb version, MySQL is: 5.0.26 Thanks a lot! Greetings Hans -- http://mail.python.org/mailman/listinfo/python-list
Re: Job Offer: Python Ninja or Pirate!
On Tue, 11 Dec 2007 08:57:16 -0800, George Sakkis wrote: On Dec 10, 11:07 pm, Stargaming [EMAIL PROTECTED] wrote: On Mon, 10 Dec 2007 19:27:43 -0800, George Sakkis wrote: On Dec 10, 2:11 pm, Stargaming [EMAIL PROTECTED] wrote: [snip] Even though I do not qualify for the job, I came up with this (wink) code (modified list values for demonstration, mixed together from previous post and original task): print '\n'.join('%s: %d'%(x,len(list(y))) for x,y in __import__ ('itertools').groupby(sorted(__import__('xml').dom.minidom.parse (__import__('urllib').urlopen('http://api.etsy.com/feeds/ xml_user_details.php?id=%d'%i)).getElementsByTagName('city') [0].lastChild.data.title() for i in (71234, 729, 42346, 77290, 729, 729 [snip] Alas, it's not: AttributeError: 'module' object has no attribute 'dom' [snip] Heh, yes. I did the same error as the participant before me -- test it in a premodified environment. A fix is easy, __import__ 'xml.dom.minidom' instead of 'xml'. :-) Closer, but still wrong; for some weird reason, __import__ for modules in packages returns the top level package by default; you have to use the 'fromlist' argument: __import__('xml.dom.minidom') is __import__('xml') True __import__('xml.dom.minidom', fromlist=True) module 'xml.dom.minidom' from '/usr/local/lib/python2.5/xml/dom/ minidom.pyc' George No, it's perfectly right:: __import__('xml.dom.minidom').dom.minidom module 'xml.dom.minidom' from '/usr/lib/python2.5/xml/dom/minidom.pyc' You can observe the change pretty well in `sys.modules`:: __import__('xml') module 'xml' from '/usr/lib/python2.5/xml/__init__.pyc' import sys sys.modules.keys() The result will be a few core modules and the module `xml`. When importing `xml.dom.minidom`, though, this imports a lot more files:: __import__('xml.dom.minidom') module 'xml' from '/usr/lib/python2.5/xml/__init__.pyc' import sys sys.modules.keys() Among them, there are `xml`, `xml.dom` and `xml.dom.minidom`. Having these modules imported, `__import__('xml.dom.minidom').dom.minidom` becomes a perfectly valid access to the xml/xml.dom packages. So, your check is correct, importing xml.dom.minidom and xml both give a reference to `xml` but the import machinery is invoked to import its contents, as well, when explicitly told to. -- http://mail.python.org/mailman/listinfo/python-list
Re: listdir() with mask
Sure is.. check out the glob module: http://www.python.org/doc/current/lib/module-glob.html (Official) http://blog.doughellmann.com/2007/07/pymotw-glob.html (PyMOTW) Python 2.5 (r25:51918, Sep 19 2006, 08:49:13) [GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin Type help, copyright, credits or license for more information. import glob glob.glob(/var/log/*.log) ['/var/log/alf.log', '/var/log/asl.log', '/var/log/crashreporter.log', '/var/log/fsck_hfs.log', '/var/log/ftp.log', '/var/log/install.log', '/var/log/ipfw.log', '/var/log/lpr.log', '/var/log/mail.log', '/var/log/mb.log', '/var/log/netinfo.log', '/var/log/ppp.log', '/var/log/secure.log', '/var/log/system.log', '/var/log/windowserver.log', '/var/log/windowserver_last.log'] glob.glob(/var/*/*.log) ['/var/log/alf.log', '/var/log/asl.log', '/var/log/crashreporter.log', '/var/log/fsck_hfs.log', '/var/log/ftp.log', '/var/log/install.log', '/var/log/ipfw.log', '/var/log/lpr.log', '/var/log/mail.log', '/var/log/mb.log', '/var/log/netinfo.log', '/var/log/ppp.log', '/var/log/secure.log', '/var/log/system.log', '/var/log/windowserver.log', '/var/log/windowserver_last.log'] -Jeff On 12/14/07, Vladimir Rusinov [EMAIL PROTECTED] wrote: Hello! Is there any easy way to list files using bash-like patterns? Something like listfiles(/var/log/*.log), listfiles(/var/{cache,run}/*). Also, I'll need something like listfiles(/tmp/**/*.tmp), where ** is unlimited number of folders (like is zsh). Thanks and sorry for my English. -- Vladimir Rusinov GreenMice Solutions: IT-решения на базе Linux http://greenmice.info/ -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: simple string formatting question
Neal Becker wrote: I have a list of strings (sys.argv actually). I want to print them as a space-delimited string (actually, the same way they went into the command line, so I can cut and paste) So if I run my program like: ./my_prog a b c d I want it to print: './my_prog' 'a' 'b' 'c' 'd' Just print sys.argv will almost work, but it's comma-delimited. There must be some clever way to do this. Any ideas? Use repr to get the quotes around each output arg, and use join on a space to the the space delimited string. theList = ['./whatever', 'a', 'b', 'c'] print ' '.join([repr(i) for i in theList]) './whatever' 'a' 'b' 'c' Gary Herron -- http://mail.python.org/mailman/listinfo/python-list
Loops and things
I was wondering how and if it's possible to write a loop in python which updates two or more variables at a time. For instance, something like this in C: for (i = 0, j = 10; i 10 j 20; i++, j++) { printf(i = %d, j = %d\n, i, j); } So that I would get: i = 0, j = 0 i = 1, j = 1 i = 2, j = 2 ... ... ... i = 9, j = 19 Can this be done in Python? Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: Loops and things
[EMAIL PROTECTED] a écrit : I was wondering how and if it's possible to write a loop in python which updates two or more variables at a time. For instance, something like this in C: for (i = 0, j = 10; i 10 j 20; i++, j++) { printf(i = %d, j = %d\n, i, j); } So that I would get: i = 0, j = 0 i = 1, j = 1 i = 2, j = 2 ... ... ... i = 9, j = 19 Can this be done in Python? What's your use case exactly ? I mean, the *real* problem you're trying to solve this way ? -- http://mail.python.org/mailman/listinfo/python-list
Re: Floating point subtraction rounding error (NOT display error)
Keflavich wrote: [snip] I feel fairly certain, however, that floats are exactly what I want for my purposes: I need moderately high precision and I'm not concerned about the least-significant-bit errors except when they violate function domains. I guess the overriding lesson is that every float subtraction needs to be carefully thought through, which is a disappointing loss of simplicity for me, but I'll deal with it. Adam floats probably are what you want, but in some corner cases you have to do further math in order for your computer not to blow up; an option may be to use Taylor expansions at some point before your function call, if you can see a case of problematic arguments creeping up like that. And in general you have to use something like abs(x-y) epsilon in place of x==y, as you said. If it is simple enough, could you post the piece of code that leads to the x you use in arcsin(x)? There's a chance that someone may figure a place where a numerical trick could be used in your code. -- http://mail.python.org/mailman/listinfo/python-list
DB Query Parse Hangup
Hi There, I have been working on this script, and the part that this issue that I have occurs in is when iterating through some results from the db, asking the admin input to delete the entry or not - everything works fine up until the last entry, then it bombs out. I know why - but I am not quite sure how to fix it: The Code: select_statement = SELECT source, destination FROM mail.forwardings WHERE destination = '%s' % e_mail result = db_cursor.execute(select_statement) if result = 1: aliasLine = 0 number = result - 1 while aliasLine = number: db_cursor.execute(select_statement) answer = db_cursor.fetchall() answer_1 = answer[aliasLine] aliasLine = aliasLine + 1 print '%s is still linked to %s.' % (answer_1[0], answer_1[1]) #to_edit = raw_input(Please provide one of the above mentioned E- Mail Aliases to edit: ) to_do = str.lower(raw_input(Would you like to Delete(D) or Modify(M) the alias? [D/M] )) delete = 'd' modify = 'm' if to_do == delete: del_statement = DELETE FROM forwardings WHERE source = '%s' % answer_1[0] db_cursor.execute(del_statement) print Alias '%s', has been successfully deleted. % answer_1[0] elif to_do == modify: print WILL BE MODIFIED any help will be much appreciated. Thank you! -- Merrigan -- http://mail.python.org/mailman/listinfo/python-list
Re: listdir() with mask
On 12/14/07, Jeff McNeil [EMAIL PROTECTED] wrote: Sure is.. check out the glob module: http://www.python.org/doc/current/lib/module-glob.html (Official) http://blog.doughellmann.com/2007/07/pymotw-glob.html (PyMOTW) Thanks a lot! -- Vladimir Rusinov GreenMice Solutions: IT-решения на базе Linux http://greenmice.info/ -- http://mail.python.org/mailman/listinfo/python-list
Tk Tkinter : multiple file selection dialog over multiple directories ?
Hi, TKinter question Let's say I use: ... files = tkFileDialog.askopenFilenames(filetypes=[('AccessDB Files', '*.mdb')]) ... This works OK - I can select multiple files and the var files will be a tuple of the files. But let's say the files I want to select are in different folders/ directories, when I select a file then go to a different dir and try to select a 2nd file it overwrites the 1st file entree. Is there a way for multiple file selection over different directories? With a dialog box or another way? Thanks. Using Python 2.4.1 -- http://mail.python.org/mailman/listinfo/python-list
assemble a webpage!
Hi, Guys, Now I am able to use urlretrieve to download the text part of a webpage and wget to get the embedded image. Thanks for the tips! but how can I assemble them together so the image can be displayed in the webpage automatically once I click on the .html file. Right now I only saw the text part of the webpage when I click on it, even I put the .html file and image file in the same directory. Any script can do this job? Yi Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ -- http://mail.python.org/mailman/listinfo/python-list
Re: Changing argument value
On Thu, 13 Dec 2007 22:52:56 +0100, Bruno Desthuilliers wrote: flyfree a écrit : [snip] What is the difference between y = [3,4] and y[0]=3 y[1] =4 In the first case, you rebind the local name y to a new list object - and since the name is local, rebinding it only affects the local namespace. In the second case, you mutate the list object (bound to the local name y) passed to the function. The point to remember is that in Python, all variables are references to objects, but names (including params) are local. HTH Even though you have the assignment operator *in both cases*, it does **not** issue the same thing. As Bruno pointed out, in the first case ``y = [3,4]`` it is *rebinding* the name `y`. When used with slice notation, the assignment operator tells the object bound to y, Hey, change your item with name/number 0 to 3, please. This can succeed but it doesn't have to. Raw assignments always succeed. (You could also make assignments of y[0] result in whatever you want, a SystemExit for example; not so with pure assignments of y! The behaviour of assignments cannot be influenced in any way.) If you're interested in the special methods Python uses there, `Emulating container types http://docs.python.org/ref/sequence- types.html#l2h-232`_ might be interesting for you. To sum up, the assignment operator has multiple meanings depending on its left-hand-side operand. HTH, -- http://mail.python.org/mailman/listinfo/python-list
Re: Newbie design problem
Jonathan Garnder said: Well, if using something like PLY ( http://www.dabeaz.com/ply/ ) is considered more Pythonic than writing your own parser and lexer... Lex is very crude. I've found that it takes about half a day to organize your token definitions and another half day to write a tokenizer by hand. What's the point of the second half-day's work? My hand-written tokenizer returns everything (white space tokens, comment tokens) while Lex leaves these out. With a full token set you can use the tokenizer to color-highlight text in an editor, to emit an HTML version of source, process doc comments, etc. Python sports a tokenizer module, http://docs.python.org/lib/module-tokenize.html, but it's Python-specific. I'm working on a language for beginners, defined at http://www.MartinRinehart.com/posters/decaf.html (an 11x17 poster-like display.) Decaf, designed before I'd even looked at Python, is surprisingly Pythonic. But not totally Pythonic. I want an array of Token objects, not a list of tuples, for example. -- http://mail.python.org/mailman/listinfo/python-list
Re: simple string formatting question
On 2007-12-14, Neal Becker [EMAIL PROTECTED] wrote: I have a list of strings (sys.argv actually). I want to print them as a space-delimited string (actually, the same way they went into the command line, so I can cut and paste) So if I run my program like: ./my_prog a b c d I want it to print: './my_prog' 'a' 'b' 'c' 'd' Just print sys.argv will almost work, but it's comma-delimited. There must be some clever way to do this. Any ideas? The csv module is clever. Try the following: import sys import csv writer = csv.writer(sys.stdout, delimiter=' ', quotechar=', quoting=csv.QUOTE_ALL) writer.writerow(sys.argv) You might want to set a few more of the dialect options, too, e.g., in case an arg contains a '. The shutil module might contain something more specialized. -- Neil Cerutti -- http://mail.python.org/mailman/listinfo/python-list
Re: Loops and things
On 2007-12-14, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: I was wondering how and if it's possible to write a loop in python which updates two or more variables at a time. For instance, something like this in C: for (i = 0, j = 10; i 10 j 20; i++, j++) { printf(i = %d, j = %d\n, i, j); } So that I would get: i = 0, j = 0 i = 1, j = 1 i = 2, j = 2 ... ... ... i = 9, j = 19 Can this be done in Python? Yes, assuming you meant to say: i = 0, j = 10 i = 0, j = 11 ... i = 9, j = 19 import sys from itertools import izip for i, j in izip(xrange(10), xrange(10, 20)): sys.stdout.write(i = %d, j = %d\n, (i, j)) -- Neil Cerutti To succeed in the world it is not enough to be stupid, you must also be well- mannered. --Voltaire -- http://mail.python.org/mailman/listinfo/python-list
Re: [Plone-Users] Why Can't I Do This?
whatever[0:2] will yield THREE characters, so my by is correct and by will fail every time :)) Victor On Dec 14, 2007 12:06 PM, Derek Broughton [EMAIL PROTECTED] wrote: Encolpe Degoute wrote: Derek Broughton a écrit : Victor Subervi wrote: Hi; Why can't I do this? author = By Juan Garcia if author[0:2] == by | By | BY: ... author = author[3:] ... Traceback (most recent call last): File stdin, line 1, in ? TypeError: unsupported operand type(s) for |: 'str' and 'str' because | is unsupported, and it doesn't make sense that way. Wouldn't: if author[0:2].lower() == by: make more sense (btw, that trailing space in your by would have made it fail anyway). Are you sure of this ? Yes, because I executed it in python... -- derek - SF.Net email is sponsored by: Check out the new SourceForge.net Marketplace. It's the best place to buy or sell services for just about anything Open Source. http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace ___ Plone-Users mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/plone-users -- http://mail.python.org/mailman/listinfo/python-list
Loops and things
I was wondering how and if it's possible to write a loop in python which updates two or more variables at a time. For instance, something like this in C: for (i = 0, j = 10; i 10 j 20; i++, j++) { printf(i = %d, j = %d\n, i, j); } So that I would get: i = 0, j = 0 i = 1, j = 1 i = 2, j = 2 ... ... ... i = 9, j = 19 Can this be done in Python? Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: Floating point subtraction rounding error (NOT display error)
On Dec 14, 2:57 am, Nikos Vergas [EMAIL PROTECTED] wrote: Solved: used round(number,12) in this case for all of the operands of my arcsines. Not pretty, but at least VIM made it easy... You might have the same problem though: round(1.0003401032523500235,13) 1.000340103 round(1.0003401032523500235,12) 1.00034011 True, but I think that's actually the behavior I'm looking for: the truncation in this case gives me a smaller number (I hope it's safe to assume that 1.00034011 1.000340103), which is exactly what I want when I'm worried about subtractions giving me numbers very slightly larger than 1. Gabriel, Paddy, thanks for the overviews. Yes, floats deserve more respect, or at least more caution. I feel fairly certain, however, that floats are exactly what I want for my purposes: I need moderately high precision and I'm not concerned about the least-significant-bit errors except when they violate function domains. I guess the overriding lesson is that every float subtraction needs to be carefully thought through, which is a disappointing loss of simplicity for me, but I'll deal with it. Adam -- http://mail.python.org/mailman/listinfo/python-list
Re: Floating point subtraction rounding error (NOT display error)
On Dec 14, 8:28 am, Carl Banks [EMAIL PROTECTED] wrote: On Dec 13, 6:20 pm, Keflavich [EMAIL PROTECTED] wrote: Solved: used round(number,12) in this case for all of the operands of my arcsines. Not pretty, but at least VIM made it easy... Thanks for the help, Adam I suspect this could even fail in some circumstances. If it's for school you're probably covered, but in real world, it would be better to: 1. Rewrite calculation to avoid arccos and arcsin (using trigonometric or geometric identities) 2. Clamp it to range [-1.0:1.0]. You can do this in numpy with numpy.clip(). Carl Banks Thanks Carl. The clip task sounds a lot more reasonable. It would be clever if I could find a way around using arcsin/arccos, I'll see if that works for me. Yours is certainly the most practical advice on this thread. Adam -- http://mail.python.org/mailman/listinfo/python-list
Re: Best way to protect my new commercial software.
On 2007-12-14, farsheed [EMAIL PROTECTED] wrote: Let me be clear for you: there are someone in my company who love to use my software in other companies that she works there also. and because it is an inhouse tool, my CEO wanted me to protect it from stealing. and really we havn't time to copyright it. Uh what? I don't know what country you're in, but in the US, it doesn't take any time at all to copyright something. The mere act of writing something copyrights it. I thought it was the same in Europe as well. -- Grant Edwards grante Yow! Oh, I get it!! at The BEACH goes on, huh, visi.comSONNY?? -- http://mail.python.org/mailman/listinfo/python-list
Re: Floating point subtraction rounding error (NOT display error)
On Dec 13, 6:20 pm, Keflavich [EMAIL PROTECTED] wrote: Solved: used round(number,12) in this case for all of the operands of my arcsines. Not pretty, but at least VIM made it easy... Thanks for the help, Adam I suspect this could even fail in some circumstances. If it's for school you're probably covered, but in real world, it would be better to: 1. Rewrite calculation to avoid arccos and arcsin (using trigonometric or geometric identities) 2. Clamp it to range [-1.0:1.0]. You can do this in numpy with numpy.clip(). Carl Banks -- http://mail.python.org/mailman/listinfo/python-list
Re: Newbie design problem
Most unclear. My apologies. I'm trying to structure a tokenizer. The stupid concatenations are just placeholders for the actual tokenizing work. By rebuilding the input they demonstrate that the framework correctly processes all the input. I'm currently using a C-style design (my own pointers into an array of strings) but I suspect I've got the solution an old C guy would have. As a Python newbie I don't yet think in Python. -- http://mail.python.org/mailman/listinfo/python-list
Compressing a text file using count of continous characters
I need to count the number of continous character occurances(more than 1) in a file, and replace it with a compressed version, like below XYZDEFAAcdAA -- XYZ8ADEF2Acd2A Thanks Sumod -- http://mail.python.org/mailman/listinfo/python-list
SpamBayes/Windows volunteer help needed
The SpamBayes antispam filter (http://www.spambayes.org/) is fairly popular as a way to suppress spam. We have lots of people who use the Outlook plugin. Unfortunately, for the past year or two we've suffered from a dearth of Windows experience as our Windows development experts all got busy doing other things. If you're familiar with Python programming in the Windows environment we could use your help, especially if you use SpamBayes via Outlook or are willing to give that a whirl. If you're interested subscribe to the [EMAIL PROTECTED] list and maybe [EMAIL PROTECTED] as well. Most questions get asked on the former list. The latter is pretty quiet these days since the software is actually pretty stable. Thanks, -- Skip Montanaro - [EMAIL PROTECTED] - http://www.webfast.com/~skip/ -- http://mail.python.org/mailman/listinfo/python-list
Re: [Python] Re: efficient data loading with Python, is that possible possible?
Neil Cerutti wrote: An inefficient parsing technique is probably to blame. You first inspect the line to make sure it is valid, then you inspect it (number of column type) times to discover what data type it contains, and then you inspect it *again* to finally translate it. I was thinking just that. It is much more pythonic to simply attempt to convert the values in whatever fashion they are supposed to be converted, and handle errors in data format by means of exceptions. IMO, of course. In the trivial case, where there are no errors in the data file, this is a heck of a lot faster. -- Chris. -- http://mail.python.org/mailman/listinfo/python-list
Re: urlparse.urlparse bug - misparses long URL
John Nagle wrote: Matt Nordhoff wrote: John Nagle wrote: Here's a hostile URL that urlparse.urlparse seems to have mis-parsed. ... It's breaking on the first slash, which just happens to be very late in the URL. urlparse('http://example.com?blahblah=http://example.net') ('http', 'example.com?blahblah=http:', '//example.net', '', '', '') That's what it seems to be doing: sa1 = 'http://example.com?blahblah=/foo' sa2 = 'http://example.com?blahblah=foo' print urlparse.urlparse(sa1) ('http', 'example.com?blahblah=', '/foo', '', '', '') # WRONG print urlparse.urlparse(sa2) ('http', 'example.com', '', '', 'blahblah=foo', '') # RIGHT That's wrong. RFC3896 (Uniform Resource Identifier (URI): Generic Syntax), page 23 says The characters slash (/) and question mark (?) may represent data within the query component. Beware that some older, erroneous implementations may not handle such data correctly when it is used as the base URI for relative references (Section 5.1), apparently because they fail to distinguish query data from path data when looking for hierarchical separators. So urlparse is an older, erroneous implementation. Looking at the code for urlparse, it references RFC1808 (1995), which was a long time ago, three revisions back. Here's the bad code: def _splitnetloc(url, start=0): for c in '/?#': # the order is important! delim = url.find(c, start) if delim = 0: break else: delim = len(url) return url[start:delim], url[delim:] That's just wrong. The domain ends at the first appearance of any character in '/?#', but that code returns the text before the first '/' even if there's an earlier '?'. A URL/URI doesn't have to have a path, even when it has query parameters. urlparse doesn't use regular expressions. Is there some good reason for that? It would be easy to fix the code above with a regular expression to break on any char in '/?#'. But urlparse would have to import re. Is that undesirable? John Nagle -- http://mail.python.org/mailman/listinfo/python-list
Re: Is Python really a scripting language?
On Dec 14, 2007 2:07 AM, Dennis Lee Bieber [EMAIL PROTECTED] wrote: On Thu, 13 Dec 2007 10:43:18 +0100, Bruno Desthuilliers [EMAIL PROTECTED] declaimed the following in comp.lang.python: I still wait to see any clear, unambiguous definition of scripting language. Which one are you refering to here ? Strangely, once you leave the realm of shell languages (DCL, JCL, bash, etc.) I can think of only ONE language that I'd consider a true scripting language... ARexx on the Amiga, as it could address any application that created a compatible ARexx message port. This meant one could write ARexx programs that could, by changing the address, send application native commands to an application, retrieve returned data, and then send that data to a second application using its native commands. No hassle with subprocess spawning or pipe I/O blocking... Applescript works in a very similar way. -- http://mail.python.org/mailman/listinfo/python-list
Re: [Plone-Users] Why Can't I Do This?
On Dec 14, 2007, at 11:21 AM, Victor Subervi wrote: whatever[0:2] will yield THREE characters, so my by is correct and by will fail every time :)) Victor Nope, whatever[0:2] is two characters: ActivePython 2.5.1.1 (ActiveState Software Inc.) based on Python 2.5.1 (r251:54863, May 1 2007, 17:40:00) [GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin Type help, copyright, credits or license for more information. whatever = abcde whatever[0:2] 'ab' -- http://mail.python.org/mailman/listinfo/python-list
state machine and a global variable
Dear list, I'm writing very simple state machine library, like this: _state = None def set_state(state): global _state _state = state def get_state(): print _surface but I hate to use global variable. So, please, is there a better way of doing this? All I want is that a user has to type as little as possible, like: from state_machine import * set_state(3) get_state() I.e., nothing like: import state_machine my_machine = state_machine.new_machine() my_machine.set_state(3) my_machine.get_state() Thanks, in advance! -- http://mail.python.org/mailman/listinfo/python-list
Python for Java programmer
Yo Group, I'm excited to learn Python as new language coming year, I consider myself good Java developer and, not so unusually, with very limited experience with dynamic programming languages such as Python or Ruby. I have started with basics at http://docs.python.org, but I'm more interested in learning philosophies in programming dynamically typed languages, I really want to get rid of those Enterprisely weird Java techniques. I'm looking for good resources and a better technique to learn Python, would appreciate book references or URLs for them (Google explodes me with info. and I don't really have time to read all that). Also, I'm looking forward to hack some framework or library written in Python to get a good deal of exposure to language and class library, So it would be great if anyone would suggest such framework as well. Looking forward for suggestions Python community! Thanks, Nirav Thaker http://blog.nirav.name -- http://mail.python.org/mailman/listinfo/python-list
Re: Clearing a DOS terminal in a script
Stephen_B wrote: This doesn't seem to work in a dos terminal at the start of a script: from os import popen print popen('clear').read() Any idea why not? Thanks. As others have mentioned, you should just do: os.system(cls) Or, you can use my WConio module for fancier work. http://newcenturycomputers.net/projects/wconio.html Good luck! -- Chris. -- http://mail.python.org/mailman/listinfo/python-list
Re: Newbie design problem
[EMAIL PROTECTED] a écrit : Most unclear. My apologies. I'm trying to structure a tokenizer. The stupid concatenations are just placeholders for the actual tokenizing work. By rebuilding the input they demonstrate that the framework correctly processes all the input. I'm currently using a C-style design (my own pointers into an array of strings) but I suspect I've got the solution an old C guy would have. As a Python newbie I don't yet think in Python. Then the first move is to carefully eval existing solutions: http://wiki.python.org/moin/LanguageParsing -- http://mail.python.org/mailman/listinfo/python-list
Re: do as a keyword
But reading through the warts and reading about a lack of do while statements I also started to ponder about the 'do something' if 'true' else 'do this', and pondered if perhaps this statement could do with the including of the keyword do. I often miss what can be done in other languages that support what I call 'side effect' assignment: while x = get_val_somewhere(): process(x) Although, I know that if I write the get function, I could make it an iterator, and do: for x in get_val_somewhere(): process(x) in which case, I'm even happier. -- Posted via a free Usenet account from http://www.teranews.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Newbie design problem
[EMAIL PROTECTED] a écrit : Jonathan Garnder said: Well, if using something like PLY ( http://www.dabeaz.com/ply/ ) is considered more Pythonic than writing your own parser and lexer... Lex is very crude. Possibly. Anyway, there are quite a few other parser generators : http://wiki.python.org/moin/LanguageParsing (snip) But not totally Pythonic. I want an array of Token objects, not a list of tuples, for example. tokens = map(Token, list_of_tuples) Ok, I guess this can't apply to your current project ? -- http://mail.python.org/mailman/listinfo/python-list
Re: Compressing a text file using count of continous characters
On Fri, 14 Dec 2007 08:54:58 -0800, nirvana wrote: I need to count the number of continous character occurances(more than 1) in a file, and replace it with a compressed version, like below XYZDEFAAcdAA -- XYZ8ADEF2Acd2A Great. Then go ahead an implement it. :-) `itertools.groupby()` might be handy. And you have to think about digits in the source if that's allowed. Ciao, Marc 'BlackJack' Rintsch -- http://mail.python.org/mailman/listinfo/python-list
Re: replacing built-in exception types
On Tue, 11 Dec 2007 12:51:52 -0800, Nishkar Grover wrote: I'm trying to replace a built-in exception type and here's a simplified example of what I was hoping to do... import exceptions, __builtin__ zeroDivisionError = exceptions.ZeroDivisionError I don't know why you're fiddling that much with the names, `ZeroDivisionError` is a builtin name. If you do this to avoid overwriting the base classes, consider the following example:: class A(object): pass ... class B(A): pass ... A_backup = A A_backup is A True class A(object): pass ... A_backup is A False B.__base__ is A False B.__base__ is A_backup True The names really just point to an object. Once the resolution from a name to a real object is done, you can reuse the name. class Foo(zeroDivisionError): ... bar = 'bar' ... exceptions.ZeroDivisionError = Foo ZeroDivisionError = Foo __builtin__.ZeroDivisionError = Foo try: ... raise ZeroDivisionError ... except ZeroDivisionError, e: ... print e.bar ... bar try: ... 1/0 ... except ZeroDivisionError, e: ... print e.bar ... Traceback (most recent call last): File stdin, line 2, in ? ZeroDivisionError: integer division or modulo by zero The object that ZeroDivisionError points to in your code is *not* the raised exception. Notice that I get my customized exception type when I explicitly raise ZeroDivisionError but not when that is implicitly raised by 1/0. It seems like I have to replace that exception type at some lower level, but I'm not sure how/where. Does anyone know of a way to do this? The obvious question is: What are you trying to do? Remember that you can always catch an exception in an `except` clause and perform whatever action you want, eg. raise *another* exception. If you just want to change error representations (I doubt that you really need it, tho), you might be interested in overwriting `sys.excepthook http://docs.python.org/lib/module-sys.html#l2h-5125`_. HTH, -- http://mail.python.org/mailman/listinfo/python-list
Re: Python for Java programmer
On Dec 14, 11:53 am, Nirav Thaker [EMAIL PROTECTED] wrote: Yo Group, I'm excited to learn Python as new language coming year, I consider myself good Java developer and, not so unusually, with very limited experience with dynamic programming languages such as Python or Ruby. I have started with basics athttp://docs.python.org, but I'm more interested in learning philosophies in programming dynamically typed languages, I really want to get rid of those Enterprisely weird Java techniques. I'm looking for good resources and a better technique to learn Python, would appreciate book references or URLs for them (Google explodes me with info. and I don't really have time to read all that). Also, I'm looking forward to hack some framework or library written in Python to get a good deal of exposure to language and class library, So it would be great if anyone would suggest such framework as well. Looking forward for suggestions Python community! You may want to check out these first: http://dirtsimple.org/2004/12/python-is-not-java.html http://www.razorvine.net/python/PythonComparedToJava George -- http://mail.python.org/mailman/listinfo/python-list
Re: Job Offer: Python Ninja or Pirate!
On Dec 14, 9:57 am, Stargaming [EMAIL PROTECTED] wrote: On Tue, 11 Dec 2007 08:57:16 -0800, George Sakkis wrote: Closer, but still wrong; for some weird reason, __import__ for modules in packages returns the top level package by default; you have to use the 'fromlist' argument: __import__('xml.dom.minidom') is __import__('xml') True __import__('xml.dom.minidom', fromlist=True) module 'xml.dom.minidom' from '/usr/local/lib/python2.5/xml/dom/ minidom.pyc' George No, it's perfectly right:: __import__('xml.dom.minidom').dom.minidom module 'xml.dom.minidom' from '/usr/lib/python2.5/xml/dom/minidom.pyc' Sure, if you remember to repeat all.the.subpackages.after.the.top. Instead of __import__ I use a more intuitive and general version that doesn't stop at module boundaries but acts as getattr() within a module: for name in ['xml', 'xml.dom', 'xml.dom.minidom', 'xml.dom.minidom.parse', 'xml.dom.minidom.parse.__name__']: print '%s: %r\n' % (name, import_name(name)) output xml: module 'xml' from '/usr/local/lib/python2.5/xml/__init__.pyc' xml.dom: module 'xml.dom' from '/usr/local/lib/python2.5/xml/dom/ __init__.pyc' xml.dom.minidom: module 'xml.dom.minidom' from '/usr/local/lib/ python2.5/xml/dom/minidom.pyc' xml.dom.minidom.parse: function parse at 0xb7d40614 xml.dom.minidom.parse.__name__: 'parse' #=== import_name = def import_name(name, globals={}, locals={}): prefix,sep,tail = name.partition('.') obj = __import__(prefix, globals, locals) is_module = True while sep: head,sep,tail = tail.partition('.') if is_module: prefix += '.' + head try: __import__(prefix, globals, locals) except ImportError: is_module = False try: obj = getattr(obj,head) except AttributeError: raise ImportError('No name %s' % name) return obj George -- http://mail.python.org/mailman/listinfo/python-list
Re: Compressing a text file using count of continous characters
On Dec 14, 2007 10:54 AM, nirvana [EMAIL PROTECTED] wrote: I need to count the number of continous character occurances(more than 1) in a file, and replace it with a compressed version, like below XYZDEFAAcdAA -- XYZ8ADEF2Acd2A This sounds like homework. Google for run length encoding for algorithms, or check your textbook. -- http://mail.python.org/mailman/listinfo/python-list
Re: Compressing a text file using count of continous characters
On Dec 14, 12:07 pm, Chris Mellon [EMAIL PROTECTED] wrote: On Dec 14, 2007 10:54 AM, nirvana [EMAIL PROTECTED] wrote: I need to count the number of continous character occurances(more than 1) in a file, and replace it with a compressed version, like below XYZDEFAAcdAA -- XYZ8ADEF2Acd2A This sounds like homework. Google for run length encoding for algorithms, or check your textbook. nah, not homework... :). And yeah got it. And thanks for its name (RLE), that saved a lot of googles Thanks -- http://mail.python.org/mailman/listinfo/python-list
Re: Source formatting fixer?
The thing is, I'm not so much trying to fix indentation issues as spacing problems that affect readability but not program structure. All the indentation is fine, this is more trying to change things like: if ((one==two)and(three==four)): a=b+42 c=Classname (a,b) print Class %s created%c.__name__ None of the above is wrong, it's just painfully ugly and given Python's natural beauty, it seems really wrong all the same Bret On Dec 11, 3:26 pm, Gabriel Genellina [EMAIL PROTECTED] wrote: Python already comes with a reindenter, see Tools\scripts\reindent.py If you want to transform y= f ( x- 3 ) into y = f(x - 3) try PythonTidy (search this same group for a link) -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: state machine and a global variable
On Dec 14, 8:52 am, [EMAIL PROTECTED] wrote: Dear list, I'm writing very simple state machine library, like this: _state = None def set_state(state): global _state _state = state def get_state(): print _surface but I hate to use global variable. So, please, is there a better way of doing this? All I want is that a user has to type as little as possible, like: from state_machine import * set_state(3) get_state() I.e., nothing like: import state_machine my_machine = state_machine.new_machine() my_machine.set_state(3) my_machine.get_state() Thanks, in advance! Personally I _would_ do it the second way. That seems to be the most appropriate way to do it. However, you can do it the second way and still get the functionality you desire. [code in state_machine.py] class StateMachine(object): def __init__(self, state=None): if state is None: state = DEFAULT_INIT_STATE self._state = state def get_state(self): # print self._surface return self._state def set_state(self, state): self._state = state _sm = StateMachine() set_state = _sm.set_state get_state = _sm.get_state [/code] Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: reloading modules and isinstance()
Tlis schrieb: Hi, I have found that it is possible to reassign the instance.__class__ reference to the same class (but after reloading) to make the isinstance() test work again! I know that it is a kind of hacking of the Python interpreter, but it works :-) It's not especially hacking (albeit certainly not a common thing to do). But it of course will only work for instances you are _aware_ of. Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: Best way to protect my new commercial software.
Grant Edwards [EMAIL PROTECTED] wrote: On 2007-12-14, farsheed [EMAIL PROTECTED] wrote: Let me be clear for you: there are someone in my company who love to use my software in other companies that she works there also. and because it is an inhouse tool, my CEO wanted me to protect it from stealing. and really we havn't time to copyright it. Uh what? I don't know what country you're in, but in the US, it doesn't take any time at all to copyright something. The mere act of writing something copyrights it. I thought it was the same in Europe as well. It is, you don't have to do anything to copyright something apart from creating it to start with. -- Chris Green -- http://mail.python.org/mailman/listinfo/python-list
Re: RegExp Help
On Dec 14, 3:06 am, Gabriel Genellina [EMAIL PROTECTED] wrote: En Fri, 14 Dec 2007 06:06:21 -0300, Sean DiZazzo [EMAIL PROTECTED] escribió: On Dec 14, 12:04 am, Marc 'BlackJack' Rintsch [EMAIL PROTECTED] wrote: On Thu, 13 Dec 2007 17:49:20 -0800, Sean DiZazzo wrote: I'm wrapping up a command line util that returns xml in Python. The util is flaky, and gives me back poorly formed xml with different problems in different cases. Anyway I'm making progress. I'm not very good at regular expressions though and was wondering if someone could help with initially splitting the tags from the stdout returned from the util. Flaky XML is often produced by programs that treat XML as ordinary text files. If you are starting to parse XML with regular expressions you are making the very same mistake. XML may look somewhat simple but producing correct XML and parsing it isn't. Sooner or later you stumble across something that breaks producing or parsing the naive way. It's not really complicated xml so far, just tags with attributes. Still, using different queries against the program sometimes offers differing results...a few examples: id 123456 / tag name=foo / tag2 name=foo moreattrs=... /tag2 tag3 name=foo moreattrs=... tag3/ Ouch... only the second is valid xml. Most tools require at least a well formed document. You may try using BeautifulStoneSoup, included with BeautifulSouphttp://crummy.com/software/BeautifulSoup/ I found something that works, although I couldn't tell you why it works. :) retag = re.compile(r'.+?', re.DOTALL) tags = retag.findall(retag) Why does that work? That means: look for a less-than sign (), followed by the shortest sequence of (?) one or more (+) arbitrary characters (.), followed by a greater-than sign () If you never get nested tags, and never have a inside an attribute, that expression *might* work. But please try BeautifulStoneSoup, it uses a lot of heuristics trying to guess the right structure. Doesn't work always, but given your input, there isn't much one can do... -- Gabriel Genellina Thanks! I'll take a look at BeautifulStoneSoup today and see what I get. ~Sean -- http://mail.python.org/mailman/listinfo/python-list
High speed web services
Hello! I'm wondering: I'm really not so much into heavy frameworks like Django, because I need to build a fast, simple python based webservice. That is, a request comes in at a certain URL, and I want to utilize Python to respond to that request. Ideally, I want the script to be cached so it doesn't have to be re- parsed everytime. Phrasing this into a question: How do I built highly available and lighting fast Python webservice? I'm open to using any kind of server software, and all that under a *nix based system. thanks, Herb -- http://mail.python.org/mailman/listinfo/python-list
Re: urlparse.urlparse bug - misparses long URL - FIX
John Nagle wrote: John Nagle wrote: Matt Nordhoff wrote: John Nagle wrote: Here's a hostile URL that urlparse.urlparse seems to have mis-parsed. ... It's breaking on the first slash, which just happens to be very late in the URL. urlparse('http://example.com?blahblah=http://example.net') ('http', 'example.com?blahblah=http:', '//example.net', '', '', '') That's what it seems to be doing: sa1 = 'http://example.com?blahblah=/foo' sa2 = 'http://example.com?blahblah=foo' print urlparse.urlparse(sa1) ('http', 'example.com?blahblah=', '/foo', '', '', '') # WRONG print urlparse.urlparse(sa2) ('http', 'example.com', '', '', 'blahblah=foo', '') # RIGHT That's wrong. RFC3896 (Uniform Resource Identifier (URI): Generic Syntax), page 23 says The characters slash (/) and question mark (?) may represent data within the query component. Beware that some older, erroneous implementations may not handle such data correctly when it is used as the base URI for relative references (Section 5.1), apparently because they fail to distinguish query data from path data when looking for hierarchical separators. So urlparse is an older, erroneous implementation. Looking at the code for urlparse, it references RFC1808 (1995), which was a long time ago, three revisions back. Here's the bad code: def _splitnetloc(url, start=0): for c in '/?#': # the order is important! delim = url.find(c, start) if delim = 0: break else: delim = len(url) return url[start:delim], url[delim:] That's just wrong. The domain ends at the first appearance of any character in '/?#', but that code returns the text before the first '/' even if there's an earlier '?'. A URL/URI doesn't have to have a path, even when it has query parameters. OK, here's a fix to urlparse, replacing _splitnetloc. I didn't use a regular expression because urlparse doesn't import re, and I didn't want to change that. def _splitnetloc(url, start=0): delim = len(url)# position of end of domain part of url, default is end for c in '/?#': # look for delimiters; the order is NOT important wdelim = url.find(c, start) # find first of this delim if wdelim = 0: # if found delim = min(delim, wdelim)# use earliest delim position return url[start:delim], url[delim:]# return (domain, rest) I'll put this in the tracker once I can get back in; password changes still go through SourceForge, even though the tracker isn't there. Note: the unit test in urlparse fails in the standard Python 2.4 version. So the unit test needs fixing. Also, some of the bad cases above should be added to the unit test. John Nagle SiteTruth -- http://mail.python.org/mailman/listinfo/python-list
Re: Is Python really a scripting language?
Chris Mellon wrote: On Dec 14, 2007 2:07 AM, Dennis Lee Bieber [EMAIL PROTECTED] wrote: On Thu, 13 Dec 2007 10:43:18 +0100, Bruno Desthuilliers [EMAIL PROTECTED] declaimed the following in comp.lang.python: I still wait to see any clear, unambiguous definition of scripting language. Which one are you refering to here ? Strangely, once you leave the realm of shell languages (DCL, JCL, bash, etc.) I can think of only ONE language that I'd consider a true scripting language... ARexx on the Amiga, as it could address any application that created a compatible ARexx message port. This meant one could write ARexx programs that could, by changing the address, send application native commands to an application, retrieve returned data, and then send that data to a second application using its native commands. No hassle with subprocess spawning or pipe I/O blocking... Applescript works in a very similar way. Yes. One of the basic design flaws of UNIX was that interprocess communication was originally almost nonexistent, and it's still not all that great. It's easy to run other programs, and easy to send command line parameters, but all you get back is a status code, plus console-type output. The UNIX world might have been quite different if, when you ran a subprocess, at the end you got return values for the command line parameters (argv/argc) and the environment back. Then programs would behave more like big subroutines. But all you get back is a status code, so running programs from a script tends to be a somewhat blind one-way process. The UNIX world now has various forms of interprocess communication, but none of them is as pervasive as Microsoft OLE and its successors on Microsoft platforms. There's CORBA, for example, and in theory you can script OpenOffice and Gnome via CORBA. But nobody does that. Exercise: write a Python program to convert a .doc file to a .pdf file by invoking OpenOffice via CORBA. At least in theory, this is possible. All the necessary parts supposedly exist. Somebody tried back in 2003, but gave up. See http://mail.python.org/pipermail/python-list/2003-April/198094.html; John Nagle -- http://mail.python.org/mailman/listinfo/python-list
Re: state machine and a global variable
On Dec 14, 2007 10:52 AM, [EMAIL PROTECTED] wrote: Dear list, I'm writing very simple state machine library, like this: _state = None def set_state(state): global _state _state = state def get_state(): print _surface but I hate to use global variable. So, please, is there a better way of doing this? All I want is that a user has to type as little as possible, like: Yes, don't do it. from state_machine import * set_state(3) get_state() This isn't a state machine, it's just state I.e., nothing like: import state_machine my_machine = state_machine.new_machine() my_machine.set_state(3) my_machine.get_state() Stop being afraid of objects, especially if your fear is caused by straw-man C style APIs. from state_machine import State state = State() print state.current_state state.do_something('sadfsafsad') #does something based on the current state print state.current_state -- http://mail.python.org/mailman/listinfo/python-list
Re: searching a value of a dict (each value is a list)
On Dec 10, 1:28 pm, [EMAIL PROTECTED] wrote: Seongsu Lee: I have a dictionary with million keys. Each value in the dictionary has a list with up to thousand integers. Let's say each integer can be represented with 32 bits (if there are less numbers then a 3-byte representation may suffice, but this makes things more complex), that is 2^2 bytes. Let's say there are 2^20 keys each one associated to 2^10 values. So to represent the values you need 2^32 bytes. It means 4 GB, so I don't think Python suffices to store them in RAM, because a Python int object requires quite more than 4 bytes (only represented inside an array.array it may need just 4 bytes). So if you can use 128 MB RAM to store such data structure you need to store data on HD too. You probably can use a lower-level language. On disk you can keep the reverse index, represented as an array of records/structs, each of such structures keep two 32-bit numbers (so such array is 8 GB). Such index is sorted according to the first element of the struct. The first number is the value of the original dictionary and the second nuber is its key. Inside the RAM you can keep another sorted array that summarizes your whole data. When you need a number you can do a binary search on the array in RAM, such array gives you the position where you can read (with a seek) a little part of the file (512 bytes may suffice), to perform a little binary search (if the block is very little a linear scan suffices) on it too to find the number you need. Note that the summarizing data structure in RAM may be represented with just a Python dict too, so in the end you can use Python to solve this problem. You may need a lower-level language to create the 8 GB file on disk (or create it with Python, but it may take lot of time. You may sort it with the sort unix command). This isn't a complete solution, but I think it may work. Bye, bearophile Nice. :) Regards, Jordan -- http://mail.python.org/mailman/listinfo/python-list
Call for Project Participation in Development Sprints at PyCon 2008
Python-related projects: join the PyCon Development Sprints! The development sprints are a key part of PyCon, a chance for the contributors to open-source projects to get together face-to-face for up to four days of intensive learning and development. Newbies sit at the same table as the gurus, go out for lunch and dinner together, and have a great time while advancing their project. At PyCon 2007 in Dallas we must have had 20 projects sprinting. If your project would like to sprint at PyCon, now is the time to let us know. We need to collect the info and publish it, so participants will have time to make plans. We need to get the word out early, because no matter what we do during the conference, most people who haven't already decided to sprint won't be able to stay, because they have a planes to catch and no hotel rooms. In the past, many people have been reluctant to commit to sprinting. Some may not know what sprinting is all about; others may think that they're not qualified to sprint. We want to change that perception. * We want to help promote your sprint. The PyCon website, the PyCon blog, the PyCon podcast, and press releases will be there for you. * PyCon attendees will be asked to commit to sprints on the registration form, which will include a list of sprints with links to further info. * We will be featuring a How To Sprint session on Sunday afternoon, followed by sprint-related tutorials, all for free. * Some sponsors are helping out with the sprints as well. There's also cost. Although the sprinting itself is free, sprints have associated time and hotel costs. We can't do anything about the time cost, but we may have some complimentary rooms and funding available for sprinters. We will have more to say on financial aid later. Those who want to propose a sprint should send the following information to [EMAIL PROTECTED]: * Project/sprint name * Project URL * The name and contact info (email telephone) for the sprint leader(s) and other contributors who will attend the sprint * Instructions for accessing the project's code repository and documentation (or a URL) * Pointers to new contributor information (setup, etc.) * Any special requirements (projector? whiteboard? flux capacitor?) We will add this information to the PyCon website and set up a wiki page for you (or we can link to yours). Projects need a list of goals (bugs to fix, features to add, docs to write, etc.), especially some goals for beginners, to attract new sprinters. The more detail you put there, the more prepared your sprinters will be, and the more results you'll get. In 2007 there were sprints for Python, Jython, Zope, Django, TurboGears, Python in Education, SchoolTool, Trac, Docutils, the Python Job Board, PyCon-Tech, and other projects. We would like to see all these and more! The sprints will run from Monday, March 17 through Thursday, March 20, 2008. You can find more details here: http://us.pycon.org/2008/sprints/. Thank you very much, and happy coding! Facundo Batista, PyCon 2008 Sprint Coordinator David Goodger, PyCon 2008 Chair -- http://mail.python.org/mailman/listinfo/python-list
Re: High speed web services
herbasher wrote: I'm wondering: I'm really not so much into heavy frameworks like Django, because I need to build a fast, simple python based webservice. That is, a request comes in at a certain URL, and I want to utilize Python to respond to that request. Ideally, I want the script to be cached so it doesn't have to be re- parsed everytime. Phrasing this into a question: How do I built highly available and lighting fast Python webservice? I'm open to using any kind of server software, and all that under a *nix based system. Well, you could simply use FastCGI/SCGI/mod_python/mod_wsgi without any framework on top of it. It's not much harder than writing a CGI script. If you're doing something with multiple actual web pages or something, just use a framework. It'll be fast enough. -- -- http://mail.python.org/mailman/listinfo/python-list
Re: state machine and a global variable
On Dec 14, 7:35 pm, Matimus [EMAIL PROTECTED] wrote: On Dec 14, 8:52 am, [EMAIL PROTECTED] wrote: Dear list, I'm writing very simple state machine library, like this: _state = None def set_state(state): global _state _state = state def get_state(): print _surface but I hate to use global variable. So, please, is there a better way of doing this? All I want is that a user has to type as little as possible, like: from state_machine import * set_state(3) get_state() I.e., nothing like: import state_machine my_machine = state_machine.new_machine() my_machine.set_state(3) my_machine.get_state() Thanks, in advance! Personally I _would_ do it the second way. That seems to be the most appropriate way to do it. However, you can do it the second way and still get the functionality you desire. [code in state_machine.py] class StateMachine(object): def __init__(self, state=None): if state is None: state = DEFAULT_INIT_STATE self._state = state def get_state(self): # print self._surface return self._state def set_state(self, state): self._state = state _sm = StateMachine() set_state = _sm.set_state get_state = _sm.get_state [/code] Matt Thanks a lot! This is precisely what I had on my mind. -- http://mail.python.org/mailman/listinfo/python-list
Re: Is Python really a scripting language?
On Dec 11, 10:34 pm, Terry Reedy [EMAIL PROTECTED] wrote: Ron Provost [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] But here's my problem, most of my coworkers, when they see my apps and learn that they are written in Python ask questions like, Why would you write that in a scripting language? Whenever I hear a comment like that I can feel myself boiling inside. === I don't blame you. Python is an full-fledged algorithm/programming language that was designed to *also* be used a scripting language. When you buy a new car, which is more important, the styling or what's under the hood? Take, for example, Microsoft's slick new language F#. Wikipedia F# (pronounced F Sharp) is a multi-paradigm programming language, targeting the .NET Framework, that encompasses functional programming as well as imperative object-oriented programming disciplines. It is a variant of ML and is largely compatible with the OCaml implementation. /Wikipedia Sounds pretty sexy, eh? And programs compile to .exe files, none of that byte-code interpretation nonsense. But you would never buy a car without taking a test drive, right? So we can fire up the F# Interactive (Console) and put it in gear. #time;; -- Timing now on open Math;; Time 0.046875 let mutable Z = 0I;; val mutable Z : bigint Time 0.156250 Z - Z + (BigInt.pow 3I 123295I) * (BigInt.pow 2I 0I);; val it : unit = () Time 0.515625 About a half second to compute a number with 58827 decimal digits. OTOH, that was only the first of 123296 terms. The time to compute the full sequence is 0.515625*123296 seconds or 17.65 hours. If it takes that long for a compiled .exe to run, just imagine how long a lowly, byte-code interpreted language would take! But wait... ...we don't have to imagine, we can take the Python out for a test drive also. import collatz_functions import time sv = collatz_functions.build_sv(19,19,100,1541095) def Big_Z(sv): Z = 0 e2 = 0 e3 = len(sv) - 1 t0 = time.time() for v in sv: Z += 3**e3 * 2**e2 e3 -= 1 e2 += v t1 = time.time() print (t1-t0)/60,'minutes' Big_Z(sv) 25.677583 minutes Hold on... That's for the whole sequence, not a single term! 25.67 MINUTES compared to 17.65 HOURS! So, sure, some languages compile to .exe programs. In the trade, we call that polishing a turd. -- http://mail.python.org/mailman/listinfo/python-list
Re: High speed web services
herbasher pisze: How do I built highly available and lighting fast Python webservice? Write optimized code and use it in conjunction with twisted.web. -- Jarek Zgoda http://zgodowie.org/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Newbie design problem
Bruno Desthuilliers wrote: Then the first move is to carefully eval existing solutions: http://wiki.python.org/moin/LanguageParsing Always good advice, Bruno. How did you come by that list address? Google or is there something special known to Python experts? -- http://mail.python.org/mailman/listinfo/python-list
Re: High speed web services
Il Fri, 14 Dec 2007 11:07:49 -0800, herbasher ha scritto: Hello! I'm wondering: I'm really not so much into heavy frameworks like Django, because I need to build a fast, simple python based webservice. That is, a request comes in at a certain URL, and I want to utilize Python to respond to that request. Ideally, I want the script to be cached so it doesn't have to be re- parsed everytime. Phrasing this into a question: How do I built highly available and lighting fast Python webservice? I'm open to using any kind of server software, and all that under a *nix based system. I'm developing a mod_wsgi module for Nginx (http://www.nginx.net). Nginx is a very fast asynchronous and multiprocess web server. The development is still in alpha status: http://hg.mperillo.ath.cx/nginx/mod_wsgi/ If your application is not I/O bound mod_wsgi for nginx can be a good solution, otherwise you should look for mod_wsgi for Apache. Manlio Perillo -- http://mail.python.org/mailman/listinfo/python-list
Re: Loops and things
On Dec 14, 5:01 pm, Neil Cerutti [EMAIL PROTECTED] wrote: On 2007-12-14, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: I was wondering how and if it's possible to write a loop in python which updates two or more variables at a time. For instance, something like this in C: for (i = 0, j = 10; i 10 j 20; i++, j++) { printf(i = %d, j = %d\n, i, j); } So that I would get: i = 0, j = 0 i = 1, j = 1 i = 2, j = 2 ... ... ... i = 9, j = 19 Can this be done in Python? Yes, assuming you meant to say: i = 0, j = 10 i = 0, j = 11 ... i = 9, j = 19 import sys from itertools import izip for i, j in izip(xrange(10), xrange(10, 20)): sys.stdout.write(i = %d, j = %d\n, (i, j)) -- Neil Cerutti To succeed in the world it is not enough to be stupid, you must also be well- mannered. --Voltaire Yeah, that's what I meant ... ooops :) Thanks a lot to everyone for the useful info. In the meantime I had found out about zip and that way of doing it. But I really appreciated all the different alternative solutions that were illustrated, especially the more functional ones with map ... very cool, I'm also a big Lisp fan, and I really dig those. -- http://mail.python.org/mailman/listinfo/python-list
Re: Is Python really a scripting language?
On Dec 14, 2007 2:09 PM, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: On Dec 11, 10:34 pm, Terry Reedy [EMAIL PROTECTED] wrote: Ron Provost [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] But here's my problem, most of my coworkers, when they see my apps and learn that they are written in Python ask questions like, Why would you write that in a scripting language? Whenever I hear a comment like that I can feel myself boiling inside. === I don't blame you. Python is an full-fledged algorithm/programming language that was designed to *also* be used a scripting language. When you buy a new car, which is more important, the styling or what's under the hood? snip That's for the whole sequence, not a single term! 25.67 MINUTES compared to 17.65 HOURS! So, sure, some languages compile to .exe programs. In the trade, we call that polishing a turd. While I agree with the sentiment, surely this can't be a good example of F#s general performance? I would expect .NET code to smoke stock (non-Psycoed) Python in this benchmark. -- http://mail.python.org/mailman/listinfo/python-list
Re: Loops and things
I was wondering how and if it's possible to write a loop in python which updates two or more variables at a time. For instance, something like this in C: for (i = 0, j = 10; i 10 j 20; i++, j++) { printf(i = %d, j = %d\n, i, j); } So that I would get: i = 0, j = 0 i = 1, j = 1 i = 2, j = 2 ... ... ... i = 9, j = 19 Can this be done in Python? Thanks. -- In your case, j is simply i+10; so can just write j=i+10 inside the loop, or even use i+10 itself. ¡Capacidad ilimitada de almacenamiento en tu correo! No te preocupes más por el espacio de tu cuenta con Correo Yahoo!: http://correo.yahoo.com.mx/ -- http://mail.python.org/mailman/listinfo/python-list
speed versus threading or asyncore
I need some examples on using asycore for a client app im creating. I need to be able to connect to my server 10 times and i dont want any lag nor my cpu to be taxed. The examples ive found are for the server and i dont know how to implement asyncore on the client. -- http://mail.python.org/mailman/listinfo/python-list
Download new version of phyton language | http://freenewsoftware.blogspot.com/2007/12/python.html
follow this link http://freenewsoftware.blogspot.com/2007/12/python.html -- http://mail.python.org/mailman/listinfo/python-list
container.___le___ can use only =?
When implementing the rich comparison operators for some sort of container, it's tempting to save code by doing something like: class LarchTree: ... def __gt__(self, other): # A lot of code to traverse the tree def __le__(self): return not self other However, if I'm thinking correctly, this is a bad idea. The reasoning being that and = are not required to be each others opposite for arbitrary value types, and may not even both be supplied by the contained objects. If a LarchTree user stores objects that don't support __gt__, will he have a legitimate complaint when using LarchTree.__le__ results in an attribute error? -- Neil Cerutti -- http://mail.python.org/mailman/listinfo/python-list
Re: Is Python really a scripting language?
On Dec 14, 2:48 pm, Chris Mellon [EMAIL PROTECTED] wrote: On Dec 14, 2007 2:09 PM, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: On Dec 11, 10:34 pm, Terry Reedy [EMAIL PROTECTED] wrote: Ron Provost [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] But here's my problem, most of my coworkers, when they see my apps and learn that they are written in Python ask questions like, Why would you write that in a scripting language? Whenever I hear a comment like that I can feel myself boiling inside. === I don't blame you. Python is an full-fledged algorithm/programming language that was designed to *also* be used a scripting language. When you buy a new car, which is more important, the styling or what's under the hood? snip That's for the whole sequence, not a single term! 25.67 MINUTES compared to 17.65 HOURS! So, sure, some languages compile to .exe programs. In the trade, we call that polishing a turd. While I agree with the sentiment, surely this can't be a good example of F#s general performance? Of course. They example was deliberately chosen to make F#'s BigInt library look as bad as possible. I would expect .NET code to smoke stock (non-Psycoed) Python in this benchmark. Probably. It just so happens that the example chosen is typical of _my_ number theory research and I was toying with the idea of converting my Collatz Conjecture function library to F#. And that would take a lot of work since F# doesn't have anywhere near the functionality of gmpy. There isn't even a BigInt.mod function for cryin' out loud, let alone stuff like GCD or modular inverse, critical to my research. Sure, I could write my own Extended Euclidean Algorithm, but now that I now that F# is just a Volkswagen with a fiberglass shell that looks like a Ferrari, there isn't much point, eh? And don't even get me started about the stupid stuff, like having TWO different type of Big Rationals. One's more efficient than the other, they say. So why two? The less efficient one has functions not implemented in the other. And F# is strongly typed, so you can't use BigRational methods with BigNum types. And they have all sorts of conversion methods for to/from ints, strings, bigints, etc. Guess which conversions they don't have? BigRational - BigNum, of course. MAybe I'll check it out again in the future after it has gone through several revisions. -- http://mail.python.org/mailman/listinfo/python-list
HTTPS GET request.
Hi, I'm testing an application that sends an HTTPS GET request in the form of: https://localhost/cgi-bin/parse_eas.cgi?vers=2+msg=This+is+a+simple+%26+short+test. I need to get a hold of that entire request for comparison / verification purposes. The closet thing I found is Sebastien Martini's Simple HTTP server supporting SSL recipe (http://aspn.activestate.com/ASPN/Cookbook/ Python/Recipe/442473). In this recipe Sebastien created a SecureHTTPRequestHandler by extending the SimpleHTTPRequestHandler class. The SimpleHTTPRequestHandler extends the BaseHTTPRequestHandler that references an internal instance request variable. If getting access to that request variable is the right way to go to get hold of the entire GET request, can someone suggest a way (or a web resource for me to learn how) to do that (since I'm relatively new to Python). Or is there a better way? Many thanks! - Jon -- http://mail.python.org/mailman/listinfo/python-list