Re: elegant python style for loops
On May 10, 4:20 pm, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote: > for a, b in zip(lista, listb): > ... You don't even need the for loop nowadays. Just pass the zipped list to a dictionary constructor thusly: newdict = dict(zip(listKeys,listValues)) Asun -- http://mail.python.org/mailman/listinfo/python-list
Re: change of random state when pyc created??
On May 9, 6:42 am, "Alan Isaac" <[EMAIL PROTECTED]> wrote: > Is there > a warning anywhere in the docs? Should > there be? I do not think additional documentation here would be helpful. One could note that the default hash value is the object id. Somewhere else you could write that the placement of objects in memory is arbitrary and can be affected by a number of factors not explicity under user control. With those notes scattered throughout the documentation, I'm not sure that you would have found them and recognized the implications with respect to your design and with respect to the deletion of pyc files (which is just one factor among many that could cause different placements in memory). Also, the existing docs describe behavior at a more granular level. How the parts interact is typically left to third-party documentation (i.e. the set docs say what the set methods do but do not give advice on when to use them instead of a dict or list). Out of this thread, the more important lesson is that the docs intentionally do not comment on implemation specific details. When the docs do not make specific guarantees and behavior is left undefined, it is not a good practice to make assumptions about invariants that may or may not be true (in your case, you assumed that objects would be located in the same memory locations between runs -- while that sometimes happens to be true, it is certainly not guaranteed behavior as you found out -- moreover, you've made non-guaranteed assumptions about the arbitrary ordering of an unordered collection -- a definite no-no). Raymond Hettinger -- http://mail.python.org/mailman/listinfo/python-list
Re: Minor bug in tempfile module (possibly __doc__ error)
Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote: > In <[EMAIL PROTECTED]>, James T. Dennis wrote: >> Tonight I discovered something odd in the __doc__ for tempfile >> as shipped with Python 2.4.4 and 2.5: it says: >> >> This module also provides some data items to the user: >> >> TMP_MAX - maximum number of names that will be tried before >>giving up. >> template - the default prefix for all temporary names. >>You may change this to control the default prefix. >> >> ... which would lead one to think that the following code would work: >> >> >>> import tempfile >> >>> tempfile.template = 'mytest' >> >>> tf = tempfile.NamedTemporaryFile() >> >>> tf.name >> '/tmp/mytest-XX' >> >> It doesn't. > The source says: > __all__ = [ >"NamedTemporaryFile", "TemporaryFile", # high level safe interfaces >"mkstemp", "mkdtemp", # low level safe interfaces >"mktemp", # deprecated unsafe interface >"TMP_MAX", "gettempprefix",# constants >"tempdir", "gettempdir" > ] > Maybe the doc should be clearer in saying "constants" too. >> Secondly, the author(s) of the tempfile module apparently didn't >> understand this either. And no one else even noticed that the __doc__ >> is wrong (or at least misleading -- since the only way I can see to >> change tempfile.template is to edit the .py file! > You can change it by simply assigning to the name: > In [15]: tempfile.template = 'spam' > In [16]: tempfile.template > Out[16]: 'spam' I know you can change it. But changing it in your namespace doesn't change the results returned by the functions called from the module. > If you want to change the outcome of the functions and objects then simply > give the prefix as argument. I know how to provide the prefix arguments and that was never the issue. The issue was twofold: The docs are wrong (or at least confusing/misleading) I don't quite understand how this name/variable in my namespace (__main__) is able to change the value while the functions in the module still hold the old value. -- Jim Dennis, Starshine: Signed, Sealed, Delivered -- http://mail.python.org/mailman/listinfo/python-list
Re: Minor bug in tempfile module (possibly __doc__ error)
Dennis Lee Bieber <[EMAIL PROTECTED]> wrote: > On Wed, 09 May 2007 06:50:38 -, "James T. Dennis" > <[EMAIL PROTECTED]> declaimed the following in comp.lang.python: >> In fact I realized, after reading through tempfile.py in /usr/lib/... >> that the following also doesn't "work" like I'd expect: >> >No idea of the tempfile problem, but... > >> # foo.py >> tst = "foo" >> def getTst(arg): >> return "foo-%s" % arg >This return is using a literal "foo-". Change it to >return "%s-%s" % (tst, arg) Sorry that was a retyping bug in my posting ... not in my sample code which was on another system. > and try again. Try it yourself. As I said ... the value of tst in your name space will be changed, but the value returned by functions in the imported module will still use the old value! -- Jim Dennis, Starshine: Signed, Sealed, Delivered -- http://mail.python.org/mailman/listinfo/python-list
Re: Erlang style processes for Python
On Wed, 09 May 2007 18:16:32 -0700, Kay Schluehr wrote: > Every once in a while Erlang style [1] message passing concurrency [2] > is discussed for Python which does not only imply Stackless tasklets [3] > but also some process isolation semantics that lets the runtime easily > distribute tasklets ( or logical 'processes' ) across physical > processes. Syntactically a tasklet might grow out of a generator by > reusing the yield keyword for sending messages: > > yield_expr : 'yield' ([testlist] | testlist 'to' testlist) > > where the second form is specific for tasklets ( one could also use a > new keyword like "emit" if this becomes confusing - the semantics is > quite different ) and the addition of a new keyword for assigning the > "mailbox" e.g: > > required_stmt: 'required' ':' suite > > So tasklets could be identified on a lexical level ( just like > generators today ) and compiled accordingly. I just wonder about sharing > semantics. Would copy-on-read / copy-on-write and new opcodes be needed? > What would happen when sharing isn't dropped at all but when the runtime > moves a tasklet around into another OS level thread / process it will be > pickled and just separated on need? I think it would be cleaner to > separate it completely but what are the costs? > > What do you think? > > [1] http://en.wikipedia.org/wiki/Erlang_programming_language [2] > http://en.wikipedia.org/wiki/Actor_model [3] http://www.stackless.com/ Funny enough, I'm working on a project right now that is designed for exactly that: PARLEY, http://osl.cs.uiuc.edu/parley . (An announcement should show up in clp-announce as soon as the moderators release it). My essential thesis is that syntactic sugar should not be necessary -- that a nice library would be sufficient. I do admit that Erlang's pattern matching would be nice, although you can get pretty far by using uniform message formats that can easily be dispatched on -- the tuple (tag, sender, args, kwargs) in the case of PARLEY, which maps nicely to instance methods of a dispatcher class. The questions of sharing among multiple physical processes is interesting. Implicit distribution of actors may not even be necessary if it is easy enough for two hosts to coordinate with each other. In terms of the general question of assigning actors to tasklets, threads, and processes, there are added complications in terms of the physical limitations of Python and Stackless Python: - because of the GIL, actors in the same process do not gain the advantag of true parallel computation - all tasklet I/O has to be non-blocking - tasklets are cooperative, while threads are preemptive - communication across processes is slower, has to be serialized, etc. - using both threads and tasklets in a single process is tricky PARLEY currently only works within a single process, though one can choose to use either tasklets or threads. My next goal is to figure out I/O, at which point I get to tackle the fun question of distribution. So far, I've not run into any cases where I've wanted to change the interpreter, though I'd be interested in hearing ideas in this direction (especially with PyPy as such a tantalizing platform!). -- Jacob Lee <[EMAIL PROTECTED]> -- http://mail.python.org/mailman/listinfo/python-list
Re: elegant python style for loops
[EMAIL PROTECTED] wrote: > To step through a list, the python style is avoid an explicit index. > But what if the same hidden index is to be used for more than one list > > for example:- > for key,value in listKeys,listValues : > newdict[key]=value > > won't work as it is a tuple of lists, as opposed to a list of tuples. > Is there an elegant solution to this? Is there a way to merge lists > into a list of tuples to allow moving through multiple lists, or is > the for i in range(len(listkeys)): the only solution? > > Any suggestions? zip() creates a list of tuples, or better, itertools.izip() lazily creates tuples as you go. Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: elegant python style for loops
[EMAIL PROTECTED] schrieb: > To step through a list, the python style is avoid an explicit index. > But what if the same hidden index is to be used for more than one list > > for example:- > for key,value in listKeys,listValues : > newdict[key]=value > > won't work as it is a tuple of lists, as opposed to a list of tuples. > Is there an elegant solution to this? Is there a way to merge lists > into a list of tuples to allow moving through multiple lists, or is > the for i in range(len(listkeys)): the only solution? > > Any suggestions? for a, b in zip(lista, listb): ... Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: elegant python style for loops
[EMAIL PROTECTED] wrote: > To step through a list, the python style is avoid an explicit index. > But what if the same hidden index is to be used for more than one list > > for example:- > for key,value in listKeys,listValues : > newdict[key]=value > > won't work as it is a tuple of lists, as opposed to a list of tuples. > Is there an elegant solution to this? Is there a way to merge lists > into a list of tuples to allow moving through multiple lists, or is > the for i in range(len(listkeys)): the only solution? > > Any suggestions? > > Yes. The builtin function zip does just that: merging separate lists into a list of tuples. See: http://docs.python.org/lib/built-in-funcs.html#l2h-81 Gary Herron -- http://mail.python.org/mailman/listinfo/python-list
Re: preferred windows text editor?
I use VIM here too. Mainly because i always switch from Windows to Linux and using the same text editor is a way to avoid getting crazy. []'s Preto On 9 May 2007 15:21:41 -0700, BartlebyScrivener <[EMAIL PROTECTED]> wrote: On May 9, 1:26 pm, "Looney, James B" <[EMAIL PROTECTED]> wrote: > I'm using Vim (http://www.vim.org/). I too vote for VIM. I use it on both Windows XP and Debian Etch. I can't find anything it doesn't do. rd -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: change of random state when pyc created??
"Carsten Haese" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > I was simply pointing out all the ways in which you made it difficult for the > community to explain your problem. And without that community, I would still not have a clue. Thanks to all! > Please feel free to suggest specific wording changes to make the documentation > more useful. I'm sure my first pass will be flawed, but here goes: http://docs.python.org/lib/typesmapping.html: to footnote (3), add phrase "which may depend on the memory location of the keys" to get: Keys and values are listed in an arbitrary order, which may depend on the memory location of the keys. This order is non-random, varies across Python implementations, and depends on the dictionary's history of insertions and deletions. http://docs.python.org/lib/types-set.html: append a new sentence to 2nd paragraph Iteration over a set returns elements in an arbitrary order, which may depend on the memory location of the elements. fwiw, Alan Isaac -- http://mail.python.org/mailman/listinfo/python-list
elegant python style for loops
To step through a list, the python style is avoid an explicit index. But what if the same hidden index is to be used for more than one list for example:- for key,value in listKeys,listValues : newdict[key]=value won't work as it is a tuple of lists, as opposed to a list of tuples. Is there an elegant solution to this? Is there a way to merge lists into a list of tuples to allow moving through multiple lists, or is the for i in range(len(listkeys)): the only solution? Any suggestions? -- http://mail.python.org/mailman/listinfo/python-list
Re: replacing string in xml file
En Thu, 10 May 2007 01:55:25 -0300, <[EMAIL PROTECTED]> escribió: >> I am opening 2 more files in addition to the file >> where the new xml goes.One file is written using the sys.stdout >> command as most of the output has to go there printing takes place in >> many places (so cant use f_open.write) each time. >> When i attach the part of replacing the string >> 'localid' in xml file with something else as given above with >> xmlcont=xmlcont.replace('localId','dataPackageId') >> the code does not run and hangs. What do you mean by "hangs"? You have replaced stdout - what if the program is prompting the user for something, and you can't see it? Can you type some text and continue? >> Can more than 3 files be opened at a >> time Sure. The limit actually depends on the OS, but on "normal" circumstances it's far above 3 files. -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: change of random state when pyc created??
On Wed, 09 May 2007 23:10:19 -0500, Robert Kern wrote: > Steven D'Aprano wrote: >> On Wed, 09 May 2007 21:18:25 -0500, Robert Kern wrote: >> >>> Actually, the root cause of Peter's specific example is the fact that the >>> default implementation of __hash__() and __eq__() rely on identity >>> comparisons. >>> Two separate invocations of the same script give different objects by >>> identity >>> and thus the "history of insertions and deletions" is different. >> >> The history is the same. The objects inserted are the same (by equality). > > No, they *were* different by equality (identity being the default > implementation > equality that was not overridden in either Peter's code nor Alan's). Ah yes, you are right in the sense that Python's notion of equality for class instances is to fall back on identity by default. But in the vernacular human sense, an instance X with the same state as an instance Y is "equal", despite being at another memory address. I was using equality in the sense that two copies of the same edition of a book are the same, despite being in different places. For the record, and for the avoidance of all confusion, I'm not suggesting that Python's default behaviour is "wrong" or even "bad", merely pointing out to all those wise in hindsight that the behaviour was extremely puzzling for the reasons I've given. But you can be sure that I'll never forget this lesson :) -- Steven. -- http://mail.python.org/mailman/listinfo/python-list
Re: preferred windows text editor?
On May 10, 4:06 am, "T. Crane" <[EMAIL PROTECTED]> wrote: > Right now I'm using Notepad++. What are other people using? Zeus: http://www.zeusedit.com -- http://mail.python.org/mailman/listinfo/python-list
Re: change of random state when pyc created??
On Thu, 10 May 2007 01:06:33 -0400, Carsten Haese wrote: > On Thu, 10 May 2007 12:46:05 +1000, Steven D'Aprano wrote >> It is natural to expect two runs of any program to give the same >> result if there are (1) no random numbers involved; (2) the same >> input data; (3) and no permanent storage from run to run. > > Which of those three categories does time.time() fall into? What about > id("hello")? I didn't say there were no exceptions to the heuristic "expect any computer program to do the same thing on subsequent runs". I said it was a natural expectation. Obviously one of the differences between a naive programmer and a sophisticated programmer is that the sophisticated programmer has learnt more exceptions to the rule. And that's why I have described this behaviour as a gotcha, not as a bug or a mis-feature or anything else. -- Steven. -- http://mail.python.org/mailman/listinfo/python-list
Re: PYDOC replacement. (Was:Sorting attributes by catagory)
Nick Vatamaniuc wrote: > Ron, > > Consider using epydoc if you can. Epydoc will sort the methods and it > will also let you use custom CSS style sheets for the final HTML > output. Check out the documentation of my PyDBTable module. > http://www.psipy.com/PyDBTable > > -Nick Vatamaniuc Hi Nick, I already have sorting and style sheets taken care of. I'm just trying to get the content of each sub section correct at this point. The overall frame work is finished. I don't think Epydoc can replace the console help() output. The site.py module imports help(), from pydoc.py. That serves as the consoles interactive help mode. When you type help() at the console, you are using pydoc. Some of the differences... Epydoc -- Output formats: - html files - graphs (requires Graphviz) I like this! - pdf files (requires latex) * Requires explicitly generating files first. * Supports file parsing only instead of introspection. Epydoc is more of a complete application and has many nice features such as the graphs and completeness checks, that will make it better than pydoc for creating more complete pre-generated html documents with less work. Pydoc = Output formats: - live interactive console text - live interactive html with a local html server. * no files are generated. (just in the browser cache) * supports custom CSS stylesheets (API data output...) - text - html page - html section (for use in templates) - xml - reST (not yet, but will be easy to do) The reason for having additional output formats is it makes it much easier to use it as a tool to extract documentation from source code to be combined with existing more complete documentation. I am planning on writing output formatters to return docutils and docbook data structures as well. With those, you will be able to convert to latex, pdf, and other formats. The data formats for those are very close to what I'm using, so this should be easy to do. Other side benefits of doing this is that some of the modules in pydoc have been generalized so that they can be used without pydoc. The html server, and the document data and formatter classes, can be used independently of pydoc. The overall total size has not increased much, and it is more modular, maintainable, and extendable. Maintainability is a major concern for any library module or package. Of course it will need to be approved first. ;-) Cheers, Ron -- http://mail.python.org/mailman/listinfo/python-list
Re: change of random state when pyc created??
On Thu, 10 May 2007 12:46:05 +1000, Steven D'Aprano wrote > It is natural to expect two runs of any program to give the same > result if there are (1) no random numbers involved; (2) the same > input data; (3) and no permanent storage from run to run. Which of those three categories does time.time() fall into? What about id("hello")? -Carsten -- http://mail.python.org/mailman/listinfo/python-list
Re: replacing string in xml file
On May 9, 4:39 pm, [EMAIL PROTECTED] wrote: > On May 8, 4:46 pm, [EMAIL PROTECTED] wrote: > > > > > > > On May 8, 4:30 pm, Stefan Behnel <[EMAIL PROTECTED]> wrote: > > > > [EMAIL PROTECTED] schrieb: > > > > > Hi, > > > > I need to replace a string in xml file with something else.Ex > > > > > - > > > > rate > > > > rate > > > > > > > > > > > > > > > > - > > > > > Here i have opened an xml > > > > file(small part is pasted here).I want to replace the word 'localId' > > > > with 'dataPackageID' wherever it comes in xml file.I tried this but > > > > didnt work: > > > > > import sys > > > > > file_input = raw_input("Enter The ODX File Path:") > > > > input_xml = open(file_input,'r') > > > > This should say > > > > input_xml = open(file_input,'r').read() > > > > > input_xml.replace('localId','dataPackageId') > > > > This gives error ---> AttributeError: 'file' > > > > object has no attribute 'replace' > > > > Can someone help me . > > > > Thanks > > > > Stefan- Hide quoted text - > > > > - Show quoted text - > > > There is no error now,but the string is not being replaced,It remains > > the same,should we save the opened file or something- Hide quoted text - > > > - Show quoted text - > > HI, > Thanks for the reply.that seems to work,but i was doing this > so as to attach it to a bigger code where it will be utilised before a > parsing. > > #Input file and Output file path from user > > file_input = raw_input("Enter The ODX File Path:") > > (shortname,ext)=os.path.splitext(file_input) > f_open_out=shortname+".ini" > log=shortname+".xls" > test_file=shortname+"testxml.xml" > > saveout = sys.stdout > > input_xml = open(file_input,'r') > xmlcont=input_xml.read() > xmlcont=xmlcont.replace('localId','dataPackageId') > output_file = open(test_file,"w") > output_file.write(xmlcont) > output_file.close() > > f_open=open(f_open_out, 'w') > logfile=open(log,"w") > > sys.stdout = f_open > > #Parse the input file,and check for correct ODX version > > xmldoc = minidom.parse(input_xml) > > I am opening 2 more files in addition to the file > where the new xml goes.One file is written using the sys.stdout > command as most of the output has to go there printing takes place in > many places (so cant use f_open.write) each time. > When i attach the part of replacing the string > 'localid' in xml file with something else as given above with > xmlcont=xmlcont.replace('localId','dataPackageId') > the code does not run and hangs.Can more than 3 files be opened at a > time .I dotn know what the problem is here. > Thanks- Hide quoted text - > > - Show quoted text - Hi, Cna someone help me in this ,or throw some insight . Thanks -- http://mail.python.org/mailman/listinfo/python-list
Re: change of random state when pyc created??
On Thu, 10 May 2007 02:50:49 GMT, Alan Isaac wrote > "Carsten Haese" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] > > Knowing that maps don't have reproducible ordering is one thing. > > Realizing that that's the cause of the problem that's arbitrarily and > > wrongly attributed to the 'random' module, in a piece of code that's not > > posted to the public, and presumably not trimmed down to the shortest > > possible example of the problem, is quite another. > > There is no reason to be unfriendly about this. I did not mean this to be unfriendly. I'm sorry if you got that impression. I was simply pointing out all the ways in which you made it difficult for the community to explain your problem. > > I'll venture the guess that most Python programmers with a modicum of > > experience will, when asked point blank if it's safe to rely on a > > dictionary to be iterated in a particular order, answer no. > > Again, that misses the point. This is clearly documented. > I would have said the same thing: no, that's not safe. But > the question is whether the same people will be surprised when > *unchanged* code rerun with an *unchanged* implementation > produces *changed* results. That only means that a program can behave non-deterministically if you're not carefully restricting it to functions that are guaranteed to be deterministic. No experienced software engineer, whether they are experienced in Python or some other programming language should be surprised by this notion. I don't think that the cause of non-determinism in your case was exceptionally subtle, you just made it harder to find. > The docs > should not be useful only to the most sophisticated users. Please feel free to suggest specific wording changes to make the documentation more useful. > > It does, at least for dicts: "Keys and values are listed in an arbitrary > > order." If this wording is not present for sets, something to this > > effect should be added. > > Even Robert did not claim that *that* phrase was adequate. > I note that you cut off "which is non-random"! In my opinion, that phrase is adequate. I did cut off the non-random part because it's irrelevant. Non-random doesn't mean deterministic. Regards, Carsten. -- http://mail.python.org/mailman/listinfo/python-list
Re: change of random state when pyc created??
Steven D'Aprano wrote: > On Wed, 09 May 2007 21:18:25 -0500, Robert Kern wrote: > >> Actually, the root cause of Peter's specific example is the fact that the >> default implementation of __hash__() and __eq__() rely on identity >> comparisons. >> Two separate invocations of the same script give different objects by >> identity >> and thus the "history of insertions and deletions" is different. > > The history is the same. The objects inserted are the same (by equality). No, they *were* different by equality (identity being the default implementation equality that was not overridden in either Peter's code nor Alan's). -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco -- http://mail.python.org/mailman/listinfo/python-list
EOL character
I have two files apparently identical until I open them with winMerge which reports that they use different EOL characters. They are both jython scripts built using the maxq tool. When the one would not work I stripped it down to bare minimums and then duplicated it. The duplicate works, the original gives an error message stating method closeInc not found. Parts of the original file may have been edited in a python shell, notepad, or even openOffice writer. Thought all would use the Windows default. Your thoughts please. Thanks, jh #Works: # Generated by MaxQ [com.bitmechanic.maxq.generator.JythonCodeGenerator] from PyHttpTestCase import PyHttpTestCase from com.bitmechanic.maxq import Config global validatorPkg if __name__ == 'main': validatorPkg = Config.getValidatorPkgName() # Determine the validator for this testcase. exec 'from '+validatorPkg+' import Validator' class closeInc(PyHttpTestCase): print 'closeInc' def runTest(self): print 'runTest' # ^^^ Insert new recordings here. (Do not remove this line.) # Code to load and run the test if __name__ == 'main': test = closeInc("closeInc") test.runTest() # #Doesn't work. # Generated by MaxQ [com.bitmechanic.maxq.generator.JythonCodeGenerator] from PyHttpTestCase import PyHttpTestCase from com.bitmechanic.maxq import Config global validatorPkg if __name__ == 'main': validatorPkg = Config.getValidatorPkgName() # Determine the validator for this testcase. exec 'from '+validatorPkg+' import Validator' class closeInc(PyHttpTestCase): print 'closeInc' def runTest(self): print 'runTest' # ^^^ Insert new recordings here. (Do not remove this line.) # Code to load and run the test if __name__ == 'main': test = closeInc("closeInc") test.runTest() -- http://mail.python.org/mailman/listinfo/python-list
Re: interesting exercise
On May 9, 7:49 pm, Charles Sanders <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] wrote: > > On May 9, 1:13 am, Charles Sanders <[EMAIL PROTECTED]> > > wrote: > [snip] > >> or even this monstrosity ... > > >> def permute2( s, n ): > >>return [ ''.join([ s[int(i/len(s)**j)%len(s)] > >> for j in range(n-1,-1,-1)]) > >>for i in range(len(s)**n) ] > > >> print "permute2('abc',2) =", permute2('abc',2) > >> print "len(permute2('13579',3)) =", len(permute2('13579',3)) > > >> permute2('abc',2) = ['aa', 'ab', 'ac', 'ba', 'bb', 'bc', > >> 'ca', 'cb', 'cc'] > >> len(permute2('13579',3)) = 125 > > >> Charles > > > Could you explain, this one, actually? Don't forget StopIteration. > > As Michael said, simple counting in base n with the > string as the digits. No attempt at clarity or efficiency (I > did say it was a "monstrosity"). > > Also, as a python beginner I didn't know about divmod, > and have no idea what you (castironpi) mean by "Don't forget > StopIteration." > > Charles Please disregard. I have just learned that: "If the generator exits without yielding another value, a StopIteration exception is raised." "exception StopIteration Raised by an iterator's next() method to signal that there are no further values." Means normal generator termination. -- http://mail.python.org/mailman/listinfo/python-list
Re: WSGI spec clarification regarding exceptions
On May 10, 12:07 pm, Graham Dumpleton <[EMAIL PROTECTED]> wrote: > On May 10, 8:26 am, Adam Atlas <[EMAIL PROTECTED]> wrote: > > > I'm trying to figure out if there's any defined behaviour in PEP 333 > > for instances where an application returns an iterable as usual > > without error, but that iterable's next() method eventually raises an > > exception. Since any data theretofore returned by the iterable must be > > assumed to have already been written to the client, thus making it > > impossible to replace the response with a 500 error or somesuch, does > > the gateway just absorb the exception and cut off the response there? > > It seems like that's pretty much all it could do, but I'm wondering if > > that's explicitly specified anywhere (I couldn't find anything about > > that in the PEP). > > Because the WSGI specification requires that a WSGI adapter for a web > server always explicitly perform a flush after each string yielded > from the iterator then simply cutting off the response at that point > is all one can do as the first time a flush is done any response > status and headers will also have to be written out. > > Now depending on the web server being used, all the client may see is > the truncated page, or it might also see some form of error page > appended to it by the underlying web server. For example, in Apache, > if the WSGI adapter returns HTTP_INTERNAL_SERVER_ERROR back to the > server, the server disregards whether anything has already been sent > and tries to generate a 500 error page. Since the response status and > headers have already been flushed though, the original status is > received by the client, but the Apache error page content is still > sent. Thus you might get output looking like: > > Hello World! > > > 200 OK > > OK > The server encountered an internal error or > misconfiguration and was unable to complete > your request. > Please contact the server administrator, > [EMAIL PROTECTED] and inform them of the time the error occurred, > and anything you might have done that may have > caused the error. > More information about this error may be available > in the server error log. > > Apache/2.2.2 (Unix) mod_wsgi/1.0-TRUNK Python/2.3.5 Server at > localhost Port 8002 > > > It is actually hard to know what to do here. There are ways one could > stop the appending of the error page content, but is returning just > the truncated page any better. At least the error page content in > there highlights an issue even if status wasn't 500, but then not > being 500, the page content could get cached. > > This is where one wanders whether the WSGI way of always flushing is a > good idea. It may be better to always use buffering unless one > specifically knows that one wants to do streaming of data where size > could be large or take some time to produce. > > Anyway, for WSGI matters, you are probably better off bringing them up > on the Python WEB-SIG list. > > http://www.python.org/community/sigs/current/web-sig/ BTW, forgot to mention that one can always create a WSGI middleware component that does buffering and which only sends the complete response. If an error occurs while accumulating the response, then you can return a 500 status and error page of your own making. Graham -- http://mail.python.org/mailman/listinfo/python-list
Re: change of random state when pyc created??
"Carsten Haese" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Knowing that maps don't have reproducible ordering is one thing. > Realizing that that's the cause of the problem that's arbitrarily and > wrongly attributed to the 'random' module, in a piece of code that's not > posted to the public, and presumably not trimmed down to the shortest > possible example of the problem, is quite another. There is no reason to be unfriendly about this. I posted an observation about my code behavior and my best understanding of it. I asked for an explanation and did not assert a bug, although when someone doubted that the presence or absence of the .pyc file mattered for the results I said that *if* it should not matter *then* there was a bug. I offered the code to all that asked for it. I did not post it **because** I had not adequately isolated the problem. (But indeed, I was not isolating the problem due to misconceptions.) > I'll venture the guess that most Python programmers with a modicum of > experience will, when asked point blank if it's safe to rely on a > dictionary to be iterated in a particular order, answer no. Again, that misses the point. This is clearly documented. I would have said the same thing: no, that's not safe. But the question is whether the same people will be surprised when *unchanged* code rerun with an *unchanged* implementation produces *changed* results. I do not see how a reader of this thread cannot conclude that yes, even some sophisticated users (who received my code) will be surprised. The docs should not be useful only to the most sophisticated users. > It does, at least for dicts: "Keys and values are listed in an arbitrary > order." If this wording is not present for sets, something to this > effect should be added. Even Robert did not claim that *that* phrase was adequate. I note that you cut off "which is non-random"! Alan Isaac -- http://mail.python.org/mailman/listinfo/python-list
Re: change of random state when pyc created??
"Robert Kern" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Actually, the root cause of Peter's specific example is the fact that the > default implementation of __hash__() and __eq__() rely on identity comparisons. > Two separate invocations of the same script give different objects by identity > and thus the "history of insertions and deletions" is different. OK. Thank you. Alan -- http://mail.python.org/mailman/listinfo/python-list
Re: change of random state when pyc created??
On Wed, 09 May 2007 21:18:25 -0500, Robert Kern wrote: > Actually, the root cause of Peter's specific example is the fact that the > default implementation of __hash__() and __eq__() rely on identity > comparisons. > Two separate invocations of the same script give different objects by identity > and thus the "history of insertions and deletions" is different. The history is the same. The objects inserted are the same (by equality). The memory address those objects are located at is different. Would you expect that "hello world".find("w") should depend on the address of the string "w"? No, of course not. Programming in a high level language like Python, we hope to never need to think about memory addresses. And that's the gotcha. -- Steven. -- http://mail.python.org/mailman/listinfo/python-list
Re: change of random state when pyc created??
On Wed, 09 May 2007 16:01:02 -0500, Robert Kern wrote: > Alan G Isaac wrote: >> Robert Kern wrote: >>> http://docs.python.org/lib/typesmapping.html >>> """ >>> Keys and values are listed in an arbitrary order which is non-random, varies >>> across Python implementations, and depends on the dictionary's history of >>> insertions and deletions. >>> """ >> >> Even this does not tell me that if I use a specified implementation >> that my results can vary from run to run. That is, it still does >> not communicate that rerunning an *unchanged* program with an >> *unchanged* implementation can produce a change in results. > > The last clause does tell me that. Actually it doesn't. If you run a program twice, with the same inputs, and no other source of randomness (or at most have pseudo-randomness starting with the same seed), then the dictionary will have the same history of insertions and deletions from run to run. Go back to Peter Otten's diagnosis of the issue: "... your GridPlayer instances are located in different memory locations and get different hash values. This in turn affects the order in which they occur when you iterate over the GridPlayer.players_played set." There is nothing in there about the dictionary having a different history of insertions and deletions. It is having the same insertions and deletions each run, but the items being inserted are located at different memory locations, and _that_ changes their hash value and hence the order they occur in when you iterate over the set. That's quite a subtle thread to follow, and with all respect Robert, it's easy to say it is obvious in hindsight, but I didn't notice you solving the problem in the first place. Maybe you would have, if you had tried... and maybe you would have scratched your head too. Who can tell? As Carsten Haese says in another post: "The documentation shouldn't be expected to list every little thing that might change the order of keys in a dictionary. The documentation does say explicitly what *is* guaranteed: Order of keys is preserved as long as no intervening modifications happen to the dictionary. Tearing down the interpreter, starting it back up, and rebuilding the dictionary from scratch is very definitely an intervening modification." That's all very true, but nevertheless it is a significant gotcha. It is natural to expect two runs of any program to give the same result if there are (1) no random numbers involved; (2) the same input data; (3) and no permanent storage from run to run. One doesn't normally expect the output of a well-written, bug-free program to depend on the memory location of objects. And that's the gotcha -- with dicts and sets, they can. -- Steven. -- http://mail.python.org/mailman/listinfo/python-list
Re: change of random state when pyc created??
On Thu, 2007-05-10 at 01:25 +, Alan Isaac wrote: > Did this thread not demonstrate that even sophisticated users > do not see into this "implication" immediately? Knowing that maps don't have reproducible ordering is one thing. Realizing that that's the cause of the problem that's arbitrarily and wrongly attributed to the 'random' module, in a piece of code that's not posted to the public, and presumably not trimmed down to the shortest possible example of the problem, is quite another. I'll venture the guess that most Python programmers with a modicum of experience will, when asked point blank if it's safe to rely on a dictionary to be iterated in a particular order, answer no. > Replicability > of results is a huge deal in some circles. Every software engineer wants their results to be replicable. Software engineers also know that they can only expect their results to be replicable if they use deterministic functions. You wouldn't expect time.time() to return the same result just because you're running the same code, would you? > I think the docs > for sets and dicts should include a red flag: do not use > these as iterators if you want replicable results. It does, at least for dicts: "Keys and values are listed in an arbitrary order." If this wording is not present for sets, something to this effect should be added. Regards, -- Carsten Haese http://informixdb.sourceforge.net -- http://mail.python.org/mailman/listinfo/python-list
Re: Checking if string inside quotes?
On May 9, 8:48 pm, [EMAIL PROTECTED] wrote: > On May 9, 2:31 pm, "Michael Yanowitz" <[EMAIL PROTECTED]> wrote: > > > > > Thanks, but it is a little more complicated than that, > > the string could be deep in quotes. > > >The problem is in string substitution. > > Suppose I have a dictionary with MY_IP : "172.18.51.33" > > > I need to replace all instances of MY_IP with "172.18.51.33" > > in the file. > > It is easy in cases such as: > > if (MY_IP == "127.0.0.1"): > > > But suppose I encounter:" > > ("(size==23) and (MY_IP==127.0.0.1)") > > >In this case I do not want: > > ("(size==23) and ("172.18.51.33"==127.0.0.1)") > > but: > > ("(size==23) and (172.18.51.33==127.0.0.1)") > > without the internal quotes. > > How can I do this? > > I presumed that I would have to check to see if the string > > was already in quotes and if so remove the quotes. But not > > sure how to do that? > > Or is there an easier way? > > > Thanks in advance: > > Michael Yanowitz > > > -Original Message- > > From: [EMAIL PROTECTED] > > > [mailto:[EMAIL PROTECTED] Behalf > > Of [EMAIL PROTECTED] > > Sent: Wednesday, May 09, 2007 5:12 PM > > To: [EMAIL PROTECTED] > > Subject: Re: Checking if string inside quotes? > > > On May 9, 1:39 pm, "Michael Yanowitz" <[EMAIL PROTECTED]> wrote: > > > Hello: > > > >If I have a long string (such as a Python file). > > > I search for a sub-string in that string and find it. > > > Is there a way to determine if that found sub-string is > > > inside single-quotes or double-quotes or not inside any quotes? > > > If so how? > > > > Thanks in advance: > > > Michael Yanowitz > > > I think the .find() method returns the index of the found string. You > > could check one char before and then one char after the length of the > > string to see. I don't use regular expressions much, but I'm sure > > that's a more elegant approach. > > > This will work. You'll get in index error if you find the string at > > the very end of the file. > > > s = """ > > foo > > "bar" > > """ > > findme = "foo" > > index = s.find(findme) > > > if s[index-1] == "'" and s[index+len(findme)] == "'": > > print "single quoted" > > elif s[index-1] == "\"" and s[index+len(findme)] == "\"": > >print "double quoted" > > else: > >print "unquoted" > > > ~Sean > > > --http://mail.python.org/mailman/listinfo/python-list > > In that case I suppose you'd have to read the file line by line and if > you find your string in the line then search for the indexes of any > matching quotes. If you find matching quotes, see if your word lies > within any of the quote indexes. > > #!/usr/bin/env python > > file = open("file", 'r') > findme= "foo" > for j, line in enumerate(file): > found = line.find(findme) > if found != -1: > quotecount = line.count("'") > quoteindexes = [] > start = 0 > for i in xrange(quotecount): > i = line.find("'", start) > quoteindexes.append(i) > start = i+1 > > f = False > for i in xrange(len(quoteindexes)/2): > if findme in > line[quoteindexes.pop(0):quoteindexes.pop(0)]: > f = True > print "Found %s on line %s: Single-Quoted" % (findme, j > +1) > if not f: > print "Found %s on line %s: Not quoted" % (findme, j+1) > > It's not pretty but it works. > > ~Sean This approach omits double-quoted strings, escaped single-quotes "'a \'b' my tag", triple-quoted strings, as well as multi-line strings of any type. Depends what constraints you can sacrifice. Maybe character-at-a- time, or manually untokenize the solution above. For generic input, use mine. -- http://mail.python.org/mailman/listinfo/python-list
Re: change of random state when pyc created??
Alan Isaac wrote: >>> Robert Kern wrote: http://docs.python.org/lib/typesmapping.html """ Keys and values are listed in an arbitrary order which is non-random, > varies across Python implementations, and depends on the dictionary's history > of insertions and deletions. """ > >> Alan G Isaac wrote: >>> Even this does not tell me that if I use a specified implementation >>> that my results can vary from run to run. That is, it still does >>> not communicate that rerunning an *unchanged* program with an >>> *unchanged* implementation can produce a change in results. > > "Robert Kern" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] >> The last clause does tell me that. > > 1. About your reading of the current language: > I believe you, of course, but can you tell me **how** it tells you that? > To be concrete, let us suppose parallel language were added to > the description of sets. What about that language should allow > me to anticipate Peter's example (in this thread)? Actually, the root cause of Peter's specific example is the fact that the default implementation of __hash__() and __eq__() rely on identity comparisons. Two separate invocations of the same script give different objects by identity and thus the "history of insertions and deletions" is different. > 2. About possibly changing the docs: > You are much more sophisticated than ordinary users. > Did this thread not demonstrate that even sophisticated users > do not see into this "implication" immediately? Well, if you had a small test case that demonstrated the problem, we would have. Your example was large, complicated, and involved other semi-deterministic red herrings (the PRNG). It's quite easy to see the problem with Peter's example. > Replicability > of results is a huge deal in some circles. I think the docs > for sets and dicts should include a red flag: do not use > these as iterators if you want replicable results. > (Side note to Carsten: this does not require listing "every little thing".) They do. They say very explicitly that they are not ordered and that the sequence of iteration should not be relied upon. The red flags are there. But I'm not going to stop you from writing up something that's even more explicit. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco -- http://mail.python.org/mailman/listinfo/python-list
Re: Parameter checking on an interfase
On 9 mayo, 17:42, Bruno Desthuilliers <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] a écrit : > > > > > Hi all, > > I am more or less new to Python, and currently am making my > > first "serious" program. The application is a Clinical History manager > > (for my wife) which stores its data on a sqlite database. After > > googling on this newsgroup, I have read several threads where is > > stated that the LBYL way of testing parameters is not a pythonic way > > to work, and that is preferable catch the exceptions generated trying > > to use an invalid parameter passed to a function. > > Although I am generally following this approach, the problem I > > see is that sqlite docs states clearly that the engine does not check > > that the data types passed to the SQL sentences matches the types > > declared for the column, and lets any kind of information to be put in > > any column of the table. When I code the "business objects" of the > > application (don't know if this is the exact term for a layer that > > will isolate the application from the raw database, letting me change > > it in a future if necessary), > > business objects and databases are not necessarily related. And business > objects are much more than a "database abstraction layer" - they are the > "model" part of the MVC triad, and are the heart of your application's > logic. As such, they are of course in charge of validating their own > state wrt/ business rules. > > > I realize that if I pass arguments of > > wrong type (say, a numeric ID instead of the patient name), the DB > > engine will accept that gladly, and I will finish with data that could > > not be consistently retrievable if I use the DB from another program > > (no one right now, but I think of, perhaps, statistical trends on > > diseases and treatments). > > In this case, could be reasonable add type checking LBYL style > > on the methods, so if passed data is of wrong type, it generates a > > adequate exception to be catched by the caller? > > This is more a problem of validation/conversion of values than strictly > a typing problem IMHO. As someone said : "be liberal about what you > accept and strict about what you send". > > > In this way, the rest > > of the app (mostly GUI) can be coded EAFP style. As programming > > background, as you can guess, I have made some programming in C, VBA > > and JavaScript (quite procedurally). > > I hope that you can bring me some light about this kind of > > design, so I can improve my coding and get the Python way faster. > > I strongly encourage you to have a look at SQLAlchemy (a hi-level > RDBMS/python interface and an ORM) and Elixir (an ActiveRecord-like > declarative layer on top of SQLAlchemy), and FormEncode (an in/out > validation/conversion package). > > http://www.sqlalchemy.org/http://elixir.ematia.de/ > > FWIW, I'm actually working on using the second to add > validation/conversion to the > first:http://groups.google.com/group/sqlelixir/browse_thread/thread/af7b2d0... Hi all: First of all I give Bruno many thanks for the definition of business objects, because plugs a big hole on my concepts. And after reading your messages, I reach to the conclussion that the best I can do with my program is to unittest more (against my own dumbness) the classes that will call the interfase, and take out all the type checking code from the callees. And in the event that the program could grow/evolve in something more serious, change the RDBMS to another more fit to the task. Thank you very much and May Entropy be benevolent with you. Walter -- http://mail.python.org/mailman/listinfo/python-list
Re: WSGI spec clarification regarding exceptions
On May 10, 8:26 am, Adam Atlas <[EMAIL PROTECTED]> wrote: > I'm trying to figure out if there's any defined behaviour in PEP 333 > for instances where an application returns an iterable as usual > without error, but that iterable's next() method eventually raises an > exception. Since any data theretofore returned by the iterable must be > assumed to have already been written to the client, thus making it > impossible to replace the response with a 500 error or somesuch, does > the gateway just absorb the exception and cut off the response there? > It seems like that's pretty much all it could do, but I'm wondering if > that's explicitly specified anywhere (I couldn't find anything about > that in the PEP). Because the WSGI specification requires that a WSGI adapter for a web server always explicitly perform a flush after each string yielded from the iterator then simply cutting off the response at that point is all one can do as the first time a flush is done any response status and headers will also have to be written out. Now depending on the web server being used, all the client may see is the truncated page, or it might also see some form of error page appended to it by the underlying web server. For example, in Apache, if the WSGI adapter returns HTTP_INTERNAL_SERVER_ERROR back to the server, the server disregards whether anything has already been sent and tries to generate a 500 error page. Since the response status and headers have already been flushed though, the original status is received by the client, but the Apache error page content is still sent. Thus you might get output looking like: Hello World! 200 OK OK The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator, [EMAIL PROTECTED] and inform them of the time the error occurred, and anything you might have done that may have caused the error. More information about this error may be available in the server error log. Apache/2.2.2 (Unix) mod_wsgi/1.0-TRUNK Python/2.3.5 Server at localhost Port 8002 It is actually hard to know what to do here. There are ways one could stop the appending of the error page content, but is returning just the truncated page any better. At least the error page content in there highlights an issue even if status wasn't 500, but then not being 500, the page content could get cached. This is where one wanders whether the WSGI way of always flushing is a good idea. It may be better to always use buffering unless one specifically knows that one wants to do streaming of data where size could be large or take some time to produce. Anyway, for WSGI matters, you are probably better off bringing them up on the Python WEB-SIG list. http://www.python.org/community/sigs/current/web-sig/ Graham -- http://mail.python.org/mailman/listinfo/python-list
Re: path stuff
En Wed, 09 May 2007 15:11:06 -0300, fscked <[EMAIL PROTECTED]> escribió: > I am walking some directories looking for a certain filename pattern. > This part works fine, but what if I want to exclude results from a > certain directory being printed? Using os.walk you can skip undesired directories entirely: for dirpath, dirnames, filenames in os.walk(starting_dir): if "archived" in dirnames: dirnames.remove("archived") # process filenames, typically: for filename in filenames: fullfn = os.path.join(dirpath, filename) ... -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: Checking if string inside quotes?
On May 9, 2:31 pm, "Michael Yanowitz" <[EMAIL PROTECTED]> wrote: > Thanks, but it is a little more complicated than that, > the string could be deep in quotes. > >The problem is in string substitution. > Suppose I have a dictionary with MY_IP : "172.18.51.33" > > I need to replace all instances of MY_IP with "172.18.51.33" > in the file. > It is easy in cases such as: > if (MY_IP == "127.0.0.1"): > > But suppose I encounter:" > ("(size==23) and (MY_IP==127.0.0.1)") > >In this case I do not want: > ("(size==23) and ("172.18.51.33"==127.0.0.1)") > but: > ("(size==23) and (172.18.51.33==127.0.0.1)") > without the internal quotes. > How can I do this? > I presumed that I would have to check to see if the string > was already in quotes and if so remove the quotes. But not > sure how to do that? > Or is there an easier way? > > Thanks in advance: > Michael Yanowitz > > -Original Message- > From: [EMAIL PROTECTED] > > [mailto:[EMAIL PROTECTED] Behalf > Of [EMAIL PROTECTED] > Sent: Wednesday, May 09, 2007 5:12 PM > To: [EMAIL PROTECTED] > Subject: Re: Checking if string inside quotes? > > On May 9, 1:39 pm, "Michael Yanowitz" <[EMAIL PROTECTED]> wrote: > > Hello: > > >If I have a long string (such as a Python file). > > I search for a sub-string in that string and find it. > > Is there a way to determine if that found sub-string is > > inside single-quotes or double-quotes or not inside any quotes? > > If so how? > > > Thanks in advance: > > Michael Yanowitz > > I think the .find() method returns the index of the found string. You > could check one char before and then one char after the length of the > string to see. I don't use regular expressions much, but I'm sure > that's a more elegant approach. > > This will work. You'll get in index error if you find the string at > the very end of the file. > > s = """ > foo > "bar" > """ > findme = "foo" > index = s.find(findme) > > if s[index-1] == "'" and s[index+len(findme)] == "'": > print "single quoted" > elif s[index-1] == "\"" and s[index+len(findme)] == "\"": >print "double quoted" > else: >print "unquoted" > > ~Sean > > --http://mail.python.org/mailman/listinfo/python-list In that case I suppose you'd have to read the file line by line and if you find your string in the line then search for the indexes of any matching quotes. If you find matching quotes, see if your word lies within any of the quote indexes. #!/usr/bin/env python file = open("file", 'r') findme= "foo" for j, line in enumerate(file): found = line.find(findme) if found != -1: quotecount = line.count("'") quoteindexes = [] start = 0 for i in xrange(quotecount): i = line.find("'", start) quoteindexes.append(i) start = i+1 f = False for i in xrange(len(quoteindexes)/2): if findme in line[quoteindexes.pop(0):quoteindexes.pop(0)]: f = True print "Found %s on line %s: Single-Quoted" % (findme, j +1) if not f: print "Found %s on line %s: Not quoted" % (findme, j+1) It's not pretty but it works. ~Sean -- http://mail.python.org/mailman/listinfo/python-list
file uploader
Hey guys. I wanted to write a little desktop application that would upload files to an external server, to a specific directory. The desktop application is running on Mac OS X and I built a .psp script that is running on a Red Hat server. NOTE: This application is written for Python 2.5 (both py and psp) My code is giving me problems, in that it is overwriting files. I was wondering if anyone knew of a module or a method that would overcome this dilemma. I can adapt my program to search to see if the file existed already in the directory and append a random string sequence to keep this from occurring, but I do not want to reinvent the wheel (if such a thing exists) and that would clutter up my program. :) Here is what I am currently using: fname = os.path.basename(uploadFile.filename) dir_path = "/u01" open(os.path.join(dir_path, fname), 'wb').write(uploadFile.file.read()) As always, any help is greatly appreciated. .dave -- http://mail.python.org/mailman/listinfo/python-list
Re: Single precision floating point calcs?
On 2007-05-09, Robert Kern <[EMAIL PROTECTED]> wrote: > Grant Edwards wrote: >> I'm pretty sure the answer is "no", but before I give up on the >> idea, I thought I'd ask... >> >> Is there any way to do single-precision floating point >> calculations in Python? >> >> I know the various array modules generally support arrays of >> single-precision floats. I suppose I could turn all my >> variables into single-element arrays, but that would be way >> ugly... > > We also have scalar types of varying precisions in numpy: > > In [9]: from numpy import * > > In [10]: float32(1.0) + float32(1e-8) == float32(1.0) > Out[10]: True Very interesting. Converting a few key variables and intermediate values to float32 and then back to CPython floats each time through the loop would probably be more than sufficient. So far as I know, I haven't run into any cases where the differences between 64-bit prototype calculations in Python and 32-bit production calculations in C have been significant. I certainly try to design the algorithms so that it won't make any difference, but it's a nagging worry... > In [11]: 1.0 + 1e-8 == 1.0 > Out[11]: False > > If you can afford to be slow, Yes, I can afford to be slow. I'm not sure I can afford the decrease in readability. > I believe there is an ASPN Python Cookbook recipe for > simulating floating point arithmetic of any precision. Thanks, I'll go take a look. -- Grant Edwards grante Yow! It's the RINSE at CYCLE!! They've ALL IGNORED visi.comthe RINSE CYCLE!! -- http://mail.python.org/mailman/listinfo/python-list
Re: change of random state when pyc created??
>> Robert Kern wrote: >>> http://docs.python.org/lib/typesmapping.html >>> """ >>> Keys and values are listed in an arbitrary order which is non-random, varies >>> across Python implementations, and depends on the dictionary's history of >>> insertions and deletions. >>> """ > Alan G Isaac wrote: >> Even this does not tell me that if I use a specified implementation >> that my results can vary from run to run. That is, it still does >> not communicate that rerunning an *unchanged* program with an >> *unchanged* implementation can produce a change in results. "Robert Kern" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > The last clause does tell me that. 1. About your reading of the current language: I believe you, of course, but can you tell me **how** it tells you that? To be concrete, let us suppose parallel language were added to the description of sets. What about that language should allow me to anticipate Peter's example (in this thread)? 2. About possibly changing the docs: You are much more sophisticated than ordinary users. Did this thread not demonstrate that even sophisticated users do not see into this "implication" immediately? Replicability of results is a huge deal in some circles. I think the docs for sets and dicts should include a red flag: do not use these as iterators if you want replicable results. (Side note to Carsten: this does not require listing "every little thing".) Cheers, Alan Isaac -- http://mail.python.org/mailman/listinfo/python-list
Re: Single precision floating point calcs?
On 2007-05-09, Terry Reedy <[EMAIL PROTECTED]> wrote: >| I'm pretty sure the answer is "no", but before I give up on the >| idea, I thought I'd ask... >| >| Is there any way to do single-precision floating point >| calculations in Python? > > Make your own Python build from altered source. And run it on > an ancient processor/OS/C compiler combination that does not > automatically convert C floats to double when doing any sort > of calculation. It wouldn't have to be that ancient. The current version of gcc supports 32-bit doubles on quite a few platforms -- though it doesn't seem to for IA32 :/ Simply storing intermediate and final results as single-precision floats would probably be sufficient. > Standard CPython does not have C single-precision floats. I know. > The only point I can think of for doing this with single numbers, as > opposed to arrays of millions, is to show that there is no point. I use Python to test algorithms before implementing them in C. It's far, far easier to do experimentation/prototyping in Python than in C. I also like to have two sort-of independent implementations to test against each other (it's a good way to catch typos). In the C implementations, the algorithms will be done implemented in single precision, so doing my Python prototyping in as close to single precision as possible would be "a good thing". > Or do you have something else in mind? -- Grant Edwards grante Yow! Yow! Is my fallout at shelter termite proof? visi.com -- http://mail.python.org/mailman/listinfo/python-list
Re: which is more pythonic/faster append or +=[]
7stud <[EMAIL PROTECTED]> wrote: ... > > .append - easy to measure, too: > > > > brain:~ alex$ python -mtimeit 'L=range(3); n=23' 'x=L[:]; x.append(n)' > > 100 loops, best of 3: 1.31 usec per loop > > > > brain:~ alex$ python -mtimeit 'L=range(3); n=23' 'x=L[:]; x+=[n]' > > 100 loops, best of 3: 1.52 usec per loop > > > > Alex > > Why is it necessary to copy L? If you don't, then L gets longer and longer -- to over a million elements by the end of the loop -- so we're measuring something that's potentially very different from the problem under study, "what's the best way to append one item to a 3-items list". That's important to consider for any microbenchmark of code that changes some existing state: make sure you're measuring a piece of code that _overall_ does NOT change said existing state in a cumulative way, otherwise you may be measuring something very different from the issue of interest. It's maybe the only important caveat about using "python -mtimeit". Alex -- http://mail.python.org/mailman/listinfo/python-list
Erlang style processes for Python
Every once in a while Erlang style [1] message passing concurrency [2] is discussed for Python which does not only imply Stackless tasklets [3] but also some process isolation semantics that lets the runtime easily distribute tasklets ( or logical 'processes' ) across physical processes. Syntactically a tasklet might grow out of a generator by reusing the yield keyword for sending messages: yield_expr : 'yield' ([testlist] | testlist 'to' testlist) where the second form is specific for tasklets ( one could also use a new keyword like "emit" if this becomes confusing - the semantics is quite different ) and the addition of a new keyword for assigning the "mailbox" e.g: required_stmt: 'required' ':' suite So tasklets could be identified on a lexical level ( just like generators today ) and compiled accordingly. I just wonder about sharing semantics. Would copy-on-read / copy-on-write and new opcodes be needed? What would happen when sharing isn't dropped at all but when the runtime moves a tasklet around into another OS level thread / process it will be pickled and just separated on need? I think it would be cleaner to separate it completely but what are the costs? What do you think? [1] http://en.wikipedia.org/wiki/Erlang_programming_language [2] http://en.wikipedia.org/wiki/Actor_model [3] http://www.stackless.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: interesting exercise
[EMAIL PROTECTED] wrote: > On May 9, 1:13 am, Charles Sanders <[EMAIL PROTECTED]> > wrote: [snip] >> or even this monstrosity ... >> >> def permute2( s, n ): >>return [ ''.join([ s[int(i/len(s)**j)%len(s)] >> for j in range(n-1,-1,-1)]) >>for i in range(len(s)**n) ] >> >> print "permute2('abc',2) =", permute2('abc',2) >> print "len(permute2('13579',3)) =", len(permute2('13579',3)) >> >> permute2('abc',2) = ['aa', 'ab', 'ac', 'ba', 'bb', 'bc', >> 'ca', 'cb', 'cc'] >> len(permute2('13579',3)) = 125 >> >> Charles > > Could you explain, this one, actually? Don't forget StopIteration. > As Michael said, simple counting in base n with the string as the digits. No attempt at clarity or efficiency (I did say it was a "monstrosity"). Also, as a python beginner I didn't know about divmod, and have no idea what you (castironpi) mean by "Don't forget StopIteration." Charles -- http://mail.python.org/mailman/listinfo/python-list
Re: File I/O
On May 9, 2:43 pm, HMS Surprise <[EMAIL PROTECTED]> wrote: > > [lst.append(list(line.split())) for line in file] > > Thanks, this is the direction I wanted to go, BUT I must use v2.2 so > the line above gives me the error: > > AttributeError: __getitem__ > > But the write format will be helpful. (change to file.xreadlines(). Btw that snippet will fail miserably for most data). Instead, use pickle: import pickle pickle.dump(lst_of_lst, open('outfile', 'wb')) lst_of_lst = pickle.load(open('outfile', 'rb')) -Mike -- http://mail.python.org/mailman/listinfo/python-list
The odius and RACISTS WASPS of FBI can ONLY STING FOOLISH PATSIES, When will these INCOMPETENTOS catch the YANK ANTHRAX MAILER ? Re: *** Secret Technical Papers, dont tell the FBI ***
The ODIOUS WASPs of FBI (FEDERAL BUREAU OF INCOMPETENCE) can ONLY S-T- I-N-G. WASPS ONLY STING WASPS ONLY STING The fact is that the ANTHRAX mailer was a WASP BASTARD being covered up by the YANK Bastards at FBI. Mueller is DAMN Incompetent and Coward. Bush's Crimes against the Nation tower far higher than those of NIXON. Long time ago, when FBI had a competent and PATRIOTIC chief, he was able to get the NIXON BASTARD out of the office and exiled to China. If I found out some five FOOLISH PATSIES, Immigrants under mental or social imbalance were planning a STUPID and IDIOTIC operation, I would confront them with courtesy. I would tell them that this is a foolish act. That I, a white FBI officer am doing them a favor out of the goodness of my heart to not do such a foolish thing and give the YANK SOBs an excuse for genocide and stealing their oil wealth by PERPETUAL WAR of FAKE and SYNTHETIC TERROR. Thats how a good school master would confront a few stupid kids and that is the way to solve this problem. On the other hand, a HEINOUS and RACIST crime was committed in 911 CONTROLLED DEMOLITION as well as mailing those FAKE "Allah" letters with GENUINE ANTHRAX through out the country by a yank motherfucker related to the NATIONAL LABS and the ANGLO-SAXON-ZIONIST ESTABLISHMENT. On May 9, 4:32 pm, [EMAIL PROTECTED] wrote: > Can anyone upload these seminal papers on the internet? Send them to > City police chiefs, City officials, Mayors, Congressmen, District > Judges, Federal Judges, Supreme court judges, FBI Incompetentos, CIA > Bastards, Opus Dei, The Pope, Hollywood, President Vladimir Putin, > Hugo Chavez, Dr. Fidel Castro Ruz, and of course the odius SMIRK and > his VICE. Also dont forget to forward a copy to Wolfowitz and Shaha > Riza (his highly paid persian prostitute). Send one copy also to the > Madame who has caught the Washington officials by their balls. > > http://www.sciencedirect.com/science?_ob=PublicationURL&_tockey=%23TO... > > Research in Political Economy > Copyright © 2007 Elsevier Ltd. All rights reserved > > The Hidden History of 9-11-2001 > Edited by: Paul Zarembka > ISBN: 978-0-7623-1305-1 > Paul Zarembka > Part I: Hijackers - Who Were They? > Pages 3-45 > Jay Kolar > > Inconsistencies and contradictions in the US government's story of > hijackers and their masterminds are examined to account for what > happened on 9-11. A little-known initial FBI list of 19, scrutinized > for four names not on its final list, calls into question the FBI > naming process. We discovered 11 of the FBI-named finalists could not > have been on those planes, with 10 still alive and another's identity > improvised by a double. The Dulles videotape, essentially the > government's case that hijackers boarded the 9-11 flights, is found to > have serious problems including authentication, as does the so-called > bin Laden "confession" video. > > Were "hijackers" known to be in the US before intelligence alleges it > knew? Evidence is examined which shows that they were closely > monitored by agencies which denied this knowledge; in particular, an > undercover FBI agent lived with them the prior year. > > Noting government refusal to disclose evidence called for by > investigators, we find some pieces altered or fabricated and others > confiscated or destroyed. Other revelations point to hijackers with > national security overrides, protection in their alternate roles as > drug traffickers, and deep political connections with government > elites. We investigate patterns, reminiscent of historical > intelligence involvement, revealing the presence of a covert > intelligence operation disguised as an outside enemy attack. > > Part II: The Morning of 9-11-2001 > You are not entitled to access the full text of this document 4. > Initiation of the 9-11 Operation, with Evidence of Insider Trading > Beforehand > Pages 49-77 > Paul Zarembka > Abstract | Full Text + Links | PDF (216 K) |To Purchase and Download > the Full Article, Click PDF > > This chapter first examines evidence concerning departures of the four > flights out of Boston, D.C., and Newark, including identifications of > the aircrafts involved, some evidence regarding the flight paths, and > then the hijackings. Alleged video evidence at airports for the > hijackers themselves is examined, but found to be unacceptable. > > The fact of a conspiracy is uncontested by all. Three alternative > conspiracy possibilities regarding the planes are examined: the > 'official' one of suicide hijackers skillfully guiding planes with > steeled determination into targets (independent of the hijackers' > identities); use of beaconing or electronic control, similar to > ordinary commercial landings, into the targets; and use of 'drone' > airplanes. The third alternative is not supportable at this time, but > the other two are possible explanations, not necessarily equally > likely. > > The issue of insider trading before 9-11 is addressed. Publicly > a
Re: Sorting attributes by catagory
On May 9, 11:32 am, Ron Adam <[EMAIL PROTECTED]> wrote: > This is for a new version of pydoc if I can get the class attributes sorted > out. The module level attributes aren't too difficult to categorize. > > (I might be just too tired to see the obvious.) > > The original pydoc did this a somewhat round about way, so I would like to > find a more direct method if possible. > > Where dir(obj) is used to get all attributes of a module or class. And > they are then sorted into categories depending on what they are. > > (In order of precedence.) > > For modules: > > - imported_items (defined in another module, > or is another module) > - classes > - functions > - other_objects (everything else) > > For classes: > > - from_else_where(object created someplace else) > - inherited_attributes (from parents classes or type) > - static_methods_here > - class_methods_here > - other_methods > - properties > - getset_descriptors > - other_descriptors > - other_attributes (everything else) > > A single function that accepts an object and can return one of the above > (or equivalent) strings would be ideal. Finer grained categorizing is ok > as long as they don't overlap more than one group. > > It seems I can get some of these fairly easy with the inspect module, but > others I need to test in multiple ways. > > Any ideas? > > Cheers, > Ron Ron, Consider using epydoc if you can. Epydoc will sort the methods and it will also let you use custom CSS style sheets for the final HTML output. Check out the documentation of my PyDBTable module. http://www.psipy.com/PyDBTable -Nick Vatamaniuc -- http://mail.python.org/mailman/listinfo/python-list
Re: Questions about bsddb
On May 9, 4:01 pm, [EMAIL PROTECTED] wrote: > Thanks for the info Nick. I plan on accessing the data in pretty much > random order, and once the database is built, it will be read only. > At this point Im not too concerned about access times, just getting > something to work. I've been messing around with both bt and hash with > limited success, which led me to think that maybe I was going beyond > some internal limit for the data size.It works great on a limited set > of data, but once I turn it loose on the full set, usually several > hours later, it either causes a hard reset of my machine or the HD > grinds on endlessly with no apparent progress. Is there a limit to > the size of data you can place per key? > > Thanks for the MySQL suggestion, I'll take a look. > > -JM JM, If you want, take a look at my PyDBTable on www.psipy.com. The description and the examples section is being finished but the source API documentation will help you. It is a fast Python wrapper around MySQL, PostgreSQL or SQLite. It is very fast and buffers queries and insertions. You just set up the database and then pass the connection parameters to the initializer method. After that you can use the pydb object as a dictionary of { primary_key : list_of_values }. You can even create indices on individual fields and query with queries like : - pydb.query( ['id','data_field1'], ('id','<',10), ('data_field1','LIKE','Hello%') ) Which will translate into the SQL query like : -- SELECT id, data_field1 FROM ... WHERE id<10 AND data_field1 LIKE 'Hello %' -- and return an __iterator__. The iterator as a the result is excellent because you can iterate over results much larger than your virtual memory. But in the background PyDBTable will retrieve rows from the database in large batches and cache them as to optimise I/O. Anyway, on my machine PyDBTable saturates the disk I/O (it runs as fast as a pure MySQL query). Take care, -Nick Vatamaniuc -- http://mail.python.org/mailman/listinfo/python-list
Change serial timeout per read
I'm writing a driver in Python for an old fashioned piece of serial equipment. Currently I'm using the USPP serial module. From what I can see all the serial modules seem to set the timeout when you open a serial port. This is not what I want to do. I need to change the timeout each time I do a "read" on the serial port, depending on which part of the protocol I've got to. Sometimes a return character is expected within half a second, sometimes within 2 seconds, and sometimes within 20 seconds. How do I do this in USPP, or Pyserial, or anything else? Currently I'm working in Windows, but I'd prefer a platform independent solution if possible... Thanks - Rowan -- http://mail.python.org/mailman/listinfo/python-list
Re: e-mailing multiple values
Since e-mail requires a string. Here is what I could do. list.append(item1) list.append(item2) finalstr = ''.join(list) return finalstr -- Original message -- From: "Gabriel Genellina" <[EMAIL PROTECTED]> > En Tue, 08 May 2007 20:19:22 -0300, Ian Clark <[EMAIL PROTECTED]> escribió: > > > On 5/8/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > >> > >> I have a script which has a method which returns multiple strings at > >> once using the yield. I would like to send an e-mail of these values in > >> a single e-mail instead of a mail for each string. How would I be able > >> to do that? > > > > Are you looking for something like the following? If not, try posting > > a small sampling of your code. > > > def get_data(): > > ... data = ['ham', 'eggs', 'spam'] > > ... for item in data: > > ... yield item > > ... > all_data = [item for item in get_data()] > all_data > > ['ham', 'eggs', 'spam'] > > Or simply: all_data = list(get_data()) > > -- > Gabriel Genellina > > -- > http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: CPU usage.
En Wed, 09 May 2007 02:58:45 -0300, Navid Parvini <[EMAIL PROTECTED]> escribió: > I want to get the CPU usage in my code. > Is there any module in Python to get it? > Also I want to get in on Windows and Linux. On Windows you can use WMI; Tim Golden made an excellent library that let's you query WMI using Python: http://tgolden.sc.sabren.com/python/wmi.html Then you need to know *what* to query; google for "WMI CPU usage". Since WMI is just Microsoft's own implementation of WBEM, you could find a Linux version, I don't know. -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
*** Secret Technical Papers, dont tell the FBI ***
Can anyone upload these seminal papers on the internet? Send them to City police chiefs, City officials, Mayors, Congressmen, District Judges, Federal Judges, Supreme court judges, FBI Incompetentos, CIA Bastards, Opus Dei, The Pope, Hollywood, President Vladimir Putin, Hugo Chavez, Dr. Fidel Castro Ruz, and of course the odius SMIRK and his VICE. Also dont forget to forward a copy to Wolfowitz and Shaha Riza (his highly paid persian prostitute). Send one copy also to the Madame who has caught the Washington officials by their balls. http://www.sciencedirect.com/science?_ob=PublicationURL&_tockey=%23TOC%2313091%232006%2399976%23621502%23FLA%23&_auth=y&_acct=C50221&_version=1&_urlVersion=0&_userid=10&view=f&_pubType=J&md5=9f1946d25e3b2ace70258121c72cab6c Research in Political Economy Copyright © 2007 Elsevier Ltd. All rights reserved The Hidden History of 9-11-2001 Edited by: Paul Zarembka ISBN: 978-0-7623-1305-1 Paul Zarembka Part I: Hijackers - Who Were They? Pages 3-45 Jay Kolar Inconsistencies and contradictions in the US government's story of hijackers and their masterminds are examined to account for what happened on 9-11. A little-known initial FBI list of 19, scrutinized for four names not on its final list, calls into question the FBI naming process. We discovered 11 of the FBI-named finalists could not have been on those planes, with 10 still alive and another's identity improvised by a double. The Dulles videotape, essentially the government's case that hijackers boarded the 9-11 flights, is found to have serious problems including authentication, as does the so-called bin Laden "confession" video. Were "hijackers" known to be in the US before intelligence alleges it knew? Evidence is examined which shows that they were closely monitored by agencies which denied this knowledge; in particular, an undercover FBI agent lived with them the prior year. Noting government refusal to disclose evidence called for by investigators, we find some pieces altered or fabricated and others confiscated or destroyed. Other revelations point to hijackers with national security overrides, protection in their alternate roles as drug traffickers, and deep political connections with government elites. We investigate patterns, reminiscent of historical intelligence involvement, revealing the presence of a covert intelligence operation disguised as an outside enemy attack. Part II: The Morning of 9-11-2001 You are not entitled to access the full text of this document 4. Initiation of the 9-11 Operation, with Evidence of Insider Trading Beforehand Pages 49-77 Paul Zarembka Abstract | Full Text + Links | PDF (216 K) |To Purchase and Download the Full Article, Click PDF This chapter first examines evidence concerning departures of the four flights out of Boston, D.C., and Newark, including identifications of the aircrafts involved, some evidence regarding the flight paths, and then the hijackings. Alleged video evidence at airports for the hijackers themselves is examined, but found to be unacceptable. The fact of a conspiracy is uncontested by all. Three alternative conspiracy possibilities regarding the planes are examined: the 'official' one of suicide hijackers skillfully guiding planes with steeled determination into targets (independent of the hijackers' identities); use of beaconing or electronic control, similar to ordinary commercial landings, into the targets; and use of 'drone' airplanes. The third alternative is not supportable at this time, but the other two are possible explanations, not necessarily equally likely. The issue of insider trading before 9-11 is addressed. Publicly available data from OptionMetrics are provided and analyzed, indicating that many early reports were not using accurate data. Turning to an academic study in the Journal of Business which relies upon confidential, superior data, the findings are summarized that, indeed, there is evidence of insider trading before 9-11 on American Airlines and United Airlines. Larger concerns of insider trading are also summarized. Lastly, we give brief consideration to the profits certain capitalists make out of 9-11. You are not entitled to access the full text of this document 5. The Destruction of the World Trade Center: Why the Official Account Cannot Be True Pages 79-122 David Ray Griffin Abstract | Full Text + Links | PDF (326 K) |To Purchase and Download the Full Article, Click PDF I argue that the official story about the collapses of the Twin Towers and building 7 of the World Trade Center, according to which the collapses were caused by fire - combined, in the case of the Twin Towers, with the effects of the airplane impacts - cannot be true, for two major reasons. One reason is that fire has never, except allegedly three times on 9/11, caused the total collapse of steel-frame high- rise buildings. All (other) such collapses have been produced by the use of explosives in the procedure known as
Re: Behavior of mutable class variables
On May 9, 5:49 pm, [EMAIL PROTECTED] wrote: > Thanks for the insights. I solved the problem as follows: I created a > new class method called cleanUp, which resets NStocks to an empty list > and N1 to 0. Works like a charm - it's the first time I've used a > class method, and I immediately see its utility. Thanks again > > class Stock(object): > NStocks = [] #Class variables > N1 = 0 > > @classmethod > def cleanUp(cls): > Stocks.NStocks = [] > Stocks.N1 = 0 > > def simulation(N, par1, par2, idList, returnHistoryDir): > > Stock.cleanUp() > results = .. > print results. class A: b= 0 A.b a= A() a.b a.b+= 1 a.b A.b A.b=20 a.b A.b a1= A() a1.b a.b A.b a1.b+=10 a1.b a.b A.b It looks like an instance gets its own copy of A's dictionary upon creation, and -can- no longer affect A's dictionary, though both can be changed elsewhere. Doesn't seem prudent to -obscure- a class by an instance, but if that's not what goes on, then I'm missing something. -- http://mail.python.org/mailman/listinfo/python-list
Re: Towards faster Python implementations - theory
On May 9, 10:02 am, John Nagle <[EMAIL PROTECTED]> wrote: > One option might be a class "simpleobject", from which other classes > can inherit. ("object" would become a subclass of "simpleobject"). > "simpleobject" classes would have the following restrictions: > > - New fields and functions cannot be introduced from outside > the class. Every field and function name must explicitly appear > at least once in the class definition. Subclassing is still > allowed. > - Unless the class itself uses "getattr" or "setattr" on itself, > no external code can do so. This lets the compiler eliminate the > object's dictionary unless the class itself needs it. > > This lets the compiler see all the field names and assign them fixed slots > in a fixed sized object representation. Basically, this means simple objects > have a C/C++ like internal representation, with the performance that comes > with that representation. Hey look, it already exists: >>> class A(object): ... __slots__ = 'a b c d'.split() >>> a = A() >>> a.e = 2 Traceback (most recent call last): File "", line 1, in AttributeError: 'A' object has no attribute 'e' >>> hasattr(a, '__dict__') False -Mike -- http://mail.python.org/mailman/listinfo/python-list
Re: Behavior of mutable class variables
Thanks for the insights. I solved the problem as follows: I created a new class method called cleanUp, which resets NStocks to an empty list and N1 to 0. Works like a charm - it's the first time I've used a class method, and I immediately see its utility. Thanks again class Stock(object): NStocks = [] #Class variables N1 = 0 @classmethod def cleanUp(cls): Stocks.NStocks = [] Stocks.N1 = 0 def simulation(N, par1, par2, idList, returnHistoryDir): Stock.cleanUp() results = .. print results. -- http://mail.python.org/mailman/listinfo/python-list
Re: Suggestions for how to approach this problem?
John Salerno wrote: > John Salerno wrote: > >> So I need to remove the line breaks too, but of course not *all* of >> them because each reference still needs a line break between it. > > > After doing a bit of search and replace for tabs with my text editor, I > think I've narrowed down the problem to just this: > > I need to remove all newline characters that are not at the end of a > citation (and replace them with a single space). That is, those that are > not followed by the start of a new numbered citation. This seems to > involve a look-ahead RE, but I'm not sure how to write those. This is > what I came up with: > > > \n(?=(\d)+) > > (I can never remember if I need parentheses around '\d' or if the + > should be inside it or not! I included code in my previous post that will parse the entire bib, making use of the numbering and eliminating the most probable, but still fairly rare, potential ambiguity. You might want to check out that code, as my testing it showed that it worked with your example. James -- http://mail.python.org/mailman/listinfo/python-list
Re: interesting exercise
On May 9, 2:41 pm, [EMAIL PROTECTED] wrote: > On May 9, 1:13 am, Charles Sanders <[EMAIL PROTECTED]> > wrote: > > or even this monstrosity ... > > > def permute2( s, n ): > >return [ ''.join([ s[int(i/len(s)**j)%len(s)] > > for j in range(n-1,-1,-1)]) > >for i in range(len(s)**n) ] > > > print "permute2('abc',2) =", permute2('abc',2) > > print "len(permute2('13579',3)) =", len(permute2('13579',3)) > > > permute2('abc',2) = ['aa', 'ab', 'ac', 'ba', 'bb', 'bc', > > 'ca', 'cb', 'cc'] > > len(permute2('13579',3)) = 125 > > > Charles > > Could you explain, this one, actually? Don't forget StopIteration. Heh, it's cute. Not sure what the issue is with StopIteration. He is modeling the problem as casting an integer to base N. It's terse, but it does way too much arithmetic and isn't very readable. The following uses the same idea more readably and (I expect, untested) more efficiently, if less tersely. It actually is not too bad for real code, though I much prefer your recursive list comprehension and will use that. def getdigits(number,base,length,alphabet): residual = number result = "" for column in range(length): residual,digit = divmod(residual,base) result = alphabet[digit] + result return result def permu(alphabet,wordlength): total = len(alphabet)**wordlength return [getdigits(index,len(alphabet),wordlength,alphabet) for index in range(total)] if __name__ == "__main__": print permu("abc",2) print len(permu("13579",3)) mt -- http://mail.python.org/mailman/listinfo/python-list
Re: Towards faster Python implementations - theory
John Nagle wrote: > > Modifying "at a distance" is exactly what I'm getting at. That's the > killer from an optimizing compiler standpoint. The compiler, or a > maintenance programmer, looks at a block of code, and there doesn't seem > to be anything unusual going on. But, if in some other section of > code, something does a "setattr" to mess with the first block of code, > something unusual can be happening. This is tough on both optimizing > compilers and maintenance programmers. Agreed. It's tempting to look at some code and say which types one thinks are being manipulated, but the actual program behaviour can be quite different. Adding explicit type declarations "anchors" the names to a very restrictive set of types - typically those permitted through interfaces or inheritance in many object-oriented languages - and the challenge, as we've already discussed, is to attempt to "anchor" the names when they are in a sense "floating free" without any explicit annotations from the programmer. Python also presents other challenges. Unlike systems programming languages, almost nothing is a local operation: each Python function is mostly a product of function calls and the occasional conditional or looping construct, themselves wired up using yet more function calls. Whilst the expection is that most of these will return sensible results (eg. a call to the iter method on a sequence returns an iterator), there isn't any guarantee that this will be the case, and even then the precise iterator involved can vary. Some might claim that the class of operations which could be done locally are precisely the ones which would benefit from identification and optimisation, namely those involving primitive types, yet some pretty good solutions for such cases exist already: Pyrex, various numeric packages, and so on. > Python has that capability mostly because it's free in an > "everything is a dictionary" implementation. ("When all you have > is a hash, everything looks like a dictionary".) But that limits > implementation performance. Most of the time, nobody is using > "setattr" to mess with the internals of a function, class, or > module from far, far away. But the cost for that flexibility is > being paid, unnecessarily. I've heard claims that virtual machines are evolving to make dictionary-based access more performant, and I guess Microsoft's DLR and any JVM improvements might point in that direction, but Shed Skin shows the way by avoiding such things entirely. > I'm suggesting that the potential for "action at a distance" somehow > has to be made more visible. > > One option might be a class "simpleobject", from which other classes > can inherit. ("object" would become a subclass of "simpleobject"). > "simpleobject" classes would have the following restrictions: > > - New fields and functions cannot be introduced from outside > the class. Every field and function name must explicitly appear > at least once in the class definition. Subclassing is still > allowed. I suppose one could mandate that only methods defined inside class blocks belong to the class and subclasses, and that various kinds of optimisations can then be applied to the code in those methods such that the self parameter's type is constrained in a way probably already imposed by CPython. This would forbid the adding of functions to classes and instances after the fact, but in my fairly conservative world of programming, I hardly ever do such things. I do add attributes to instances outside those instances (by the above definition) quite often, though. > - Unless the class itself uses "getattr" or "setattr" on itself, > no external code can do so. This lets the compiler eliminate the > object's dictionary unless the class itself needs it. I think this is a natural progression from determining the access needs of particular classes and their objects. > This lets the compiler see all the field names and assign them fixed slots > in a fixed sized object representation. Basically, this means simple objects > have a C/C++ like internal representation, with the performance that comes > with that representation. Yes, I agree that this would be desirable. > With this, plus the "Shed Skin" restrictions, plus the array features of > "numarray", it should be possible to get computationally intensive code > written in Python up to C/C++ levels of performance. Yet all the dynamic > machinery of Python remains available if needed. > > All that's necessary is not to surprise the compiler. One thing I had cause to think about again today was the cost of function invocations. Python not only has pervasive dynamic dispatch, but there are also such things as *args and **kwargs and the cost of dealing with them. However, such things are very useful in integrating components and extending class hierarchies, so I wouldn't want to see them vanish in their entirety. Paul -- http://mail.python.org/mailman/listinfo/python-
WSGI spec clarification regarding exceptions
I'm trying to figure out if there's any defined behaviour in PEP 333 for instances where an application returns an iterable as usual without error, but that iterable's next() method eventually raises an exception. Since any data theretofore returned by the iterable must be assumed to have already been written to the client, thus making it impossible to replace the response with a 500 error or somesuch, does the gateway just absorb the exception and cut off the response there? It seems like that's pretty much all it could do, but I'm wondering if that's explicitly specified anywhere (I couldn't find anything about that in the PEP). -- http://mail.python.org/mailman/listinfo/python-list
Re: preferred windows text editor?
On May 9, 1:26 pm, "Looney, James B" <[EMAIL PROTECTED]> wrote: > I'm using Vim (http://www.vim.org/). I too vote for VIM. I use it on both Windows XP and Debian Etch. I can't find anything it doesn't do. rd -- http://mail.python.org/mailman/listinfo/python-list
Re: Checking if string inside quotes?
On May 9, 4:31 pm, "Michael Yanowitz" <[EMAIL PROTECTED]> wrote: > Thanks, but it is a little more complicated than that, > the string could be deep in quotes. > >The problem is in string substitution. > Suppose I have a dictionary with MY_IP : "172.18.51.33" > > I need to replace all instances of MY_IP with "172.18.51.33" > in the file. > It is easy in cases such as: > if (MY_IP == "127.0.0.1"): > > But suppose I encounter:" > ("(size==23) and (MY_IP==127.0.0.1)") > >In this case I do not want: > ("(size==23) and ("172.18.51.33"==127.0.0.1)") > but: > ("(size==23) and (172.18.51.33==127.0.0.1)") > without the internal quotes. > How can I do this? > I presumed that I would have to check to see if the string > was already in quotes and if so remove the quotes. But not > sure how to do that? > Or is there an easier way? > > Thanks in advance: > Michael Yanowitz > > -Original Message- > From: [EMAIL PROTECTED] > > [mailto:[EMAIL PROTECTED] Behalf > Of [EMAIL PROTECTED] > Sent: Wednesday, May 09, 2007 5:12 PM > To: [EMAIL PROTECTED] > Subject: Re: Checking if string inside quotes? > > On May 9, 1:39 pm, "Michael Yanowitz" <[EMAIL PROTECTED]> wrote: > > Hello: > > >If I have a long string (such as a Python file). > > I search for a sub-string in that string and find it. > > Is there a way to determine if that found sub-string is > > inside single-quotes or double-quotes or not inside any quotes? > > If so how? > > > Thanks in advance: > > Michael Yanowitz > > I think the .find() method returns the index of the found string. You > could check one char before and then one char after the length of the > string to see. I don't use regular expressions much, but I'm sure > that's a more elegant approach. > > This will work. You'll get in index error if you find the string at > the very end of the file. > > s = """ > foo > "bar" > """ > findme = "foo" > index = s.find(findme) > > if s[index-1] == "'" and s[index+len(findme)] == "'": > print "single quoted" > elif s[index-1] == "\"" and s[index+len(findme)] == "\"": >print "double quoted" > else: >print "unquoted" > > ~Sean > > --http://mail.python.org/mailman/listinfo/python-list In "nearby" quotes or in quotes at all? import re a='abc"def"ghijk' b=re.sub( r'([\'"])[^\1]*\1', '', a ) b.replace( 'ghi', 'the string' ) #fb: 'abcthe stringjk' edit() Here, you get the entire file -in b-, strings omitted entirely, so you can't write it back. I've used `tokenize' to parse a file, but you don't get precisely your original back. Untokenize rearrages your spacings. Equivalent semantically, so if you want to compile immedately afterwords, you're alright with that. Short example: from tokenize import * import token from StringIO import StringIO a= StringIO( 'abc "defghi" ghi jk' ) from collections import deque b= deque() for g in generate_tokens( a.readline ): if g[0]== token.NAME and g[1]== 'ghi': b.append( ( token.STRING, '"uchoose"' ) ) else: b.append( g ) untokenize( b ) #fb: 'abc "defghi""uchoose"jk ' edit() acb -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparing dates problem
On May 10, 7:34 am, [EMAIL PROTECTED] wrote: > Hi, > > I am writing a reminder program for our Zimbra email client. One of > the requirements I was given was to automatically increment or > decrement the display to show something like the following: > > 5 minutes until appointment > > or > > 10 minutes past your appointment > > Either way, as each minute goes by, I need to increment it or > decrement it. I am having trouble finding a coherent way to take the > same date and compare just the number of minutes between them to find > the difference. Like if I have an appointment at 9:30 a.m. and the app > is loaded at 8 a.m., I need to know the number of minutes or hours and > minutes until the appointment. > > I have looked at the dateutils module and it has many comparison > methods, but they seem to only return the number of days or seconds. > Ermmm ... what's wrong with minutes = seconds / 60.0 hours = minutes / 60.0 ? -- http://mail.python.org/mailman/listinfo/python-list
Re: Behavior of mutable class variables
On May 9, 5:25 pm, [EMAIL PROTECTED] wrote: > To test some theories, I created a new class variable, an int named Diez, Thanks. It is for precisely this reason that I added another class variable - the immutable int N1. But this too keeps getting incremented on subsequent calls to simulation( ). I also tried setting the class variable N1 to 0 at the end of the simulation and also deleting all the stocks in port (see below). Nothing works - and I don't understand why. def simulation(N, par1, par2, idList, returnHistoryDir): port = [] for i in range(N): port.append( Stock(idList[i], returnHistoryDir[idList[i]] ) p[0].NStocks = 0 del port[:] results = .. print results. -- http://mail.python.org/mailman/listinfo/python-list
Re: preferred windows text editor?
Notepad++ with NppExec plugin and you can launch your scripts inside Np++. some others, very Powerfull : http://e-texteditor.com/ http://intype.info/home/index.php -- http://mail.python.org/mailman/listinfo/python-list
Re: String parsing
Dennis Lee Bieber wrote: > > I was trying to stay with a solution the should have been available > in the version of Python equivalent to the Jython being used by the > original poster. HTMLParser, according to the documents, was 2.2 level. I guess I should read the whole thread before posting. ;-) I'll have to look into libxml2 availability for Java, though, as it appears (from various accounts) that some Java platform users struggle with HTML parsing or have a really limited selection of decent and performant parsers in that area. Another thing for the "to do" list... Paul -- http://mail.python.org/mailman/listinfo/python-list
Re: Behavior of mutable class variables
<[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] | Here's what I expect to happen each time simulation( ) is called: the | class variable NStocks for the class Stock is initialized to an empty | list, Why would you expect that ;-) A class statement is usually executed exactly once, as in your code. The body of a class statement usually consists of several name bindings: some are explicit, like your NStocks assignment statement; some are implicit, like you __init__ function definition. The resulting dictionary is used to create the class object, which is mostly a wrapper around the dict created by the class statement body. tjr -- http://mail.python.org/mailman/listinfo/python-list
Re: File I/O
> [lst.append(list(line.split())) for line in file] Thanks, this is the direction I wanted to go, BUT I must use v2.2 so the line above gives me the error: AttributeError: __getitem__ But the write format will be helpful. Thanks again, jh -- http://mail.python.org/mailman/listinfo/python-list
Re: File I/O
On May 9, 2:13 pm, HMS Surprise <[EMAIL PROTECTED]> wrote: > If one has a list of lists such as > > lst = [['a','1'],['b','2']] > > is there a standard python idiom for writing and reading the pairs to/ > from a file? > > Thanks, > > jh These work. Assuming you can choose the format. Or you could pickle the list. write lst = [['a','1'],['b','2']] file = open("file", 'w') [file.write(item[0] + "\t" + item[1] + "\n") for item in lst] file.close() read lst = [] file = open("file", 'r') [lst.append(list(line.split())) for line in file] file.close() print lst ~Sean -- http://mail.python.org/mailman/listinfo/python-list
Comparing dates problem
Hi, I am writing a reminder program for our Zimbra email client. One of the requirements I was given was to automatically increment or decrement the display to show something like the following: 5 minutes until appointment or 10 minutes past your appointment Either way, as each minute goes by, I need to increment it or decrement it. I am having trouble finding a coherent way to take the same date and compare just the number of minutes between them to find the difference. Like if I have an appointment at 9:30 a.m. and the app is loaded at 8 a.m., I need to know the number of minutes or hours and minutes until the appointment. I have looked at the dateutils module and it has many comparison methods, but they seem to only return the number of days or seconds. Any ideas would be great! Mike -- http://mail.python.org/mailman/listinfo/python-list
Re: File I/O
Hi, As far as I know, Python doesn't have a specific thing to handle this. You could write a tiny function that would interpre element type of list's elements. It checks type, if it is a list then get that pair manually... If list is going like that 'a' - '1', 'b' - '2', you should use dictionary. -- Sönmez Kartal -- http://mail.python.org/mailman/listinfo/python-list
RE: Checking if string inside quotes?
Thanks, but it is a little more complicated than that, the string could be deep in quotes. The problem is in string substitution. Suppose I have a dictionary with MY_IP : "172.18.51.33" I need to replace all instances of MY_IP with "172.18.51.33" in the file. It is easy in cases such as: if (MY_IP == "127.0.0.1"): But suppose I encounter:" ("(size==23) and (MY_IP==127.0.0.1)") In this case I do not want: ("(size==23) and ("172.18.51.33"==127.0.0.1)") but: ("(size==23) and (172.18.51.33==127.0.0.1)") without the internal quotes. How can I do this? I presumed that I would have to check to see if the string was already in quotes and if so remove the quotes. But not sure how to do that? Or is there an easier way? Thanks in advance: Michael Yanowitz -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Behalf Of [EMAIL PROTECTED] Sent: Wednesday, May 09, 2007 5:12 PM To: python-list@python.org Subject: Re: Checking if string inside quotes? On May 9, 1:39 pm, "Michael Yanowitz" <[EMAIL PROTECTED]> wrote: > Hello: > >If I have a long string (such as a Python file). > I search for a sub-string in that string and find it. > Is there a way to determine if that found sub-string is > inside single-quotes or double-quotes or not inside any quotes? > If so how? > > Thanks in advance: > Michael Yanowitz I think the .find() method returns the index of the found string. You could check one char before and then one char after the length of the string to see. I don't use regular expressions much, but I'm sure that's a more elegant approach. This will work. You'll get in index error if you find the string at the very end of the file. s = """ foo "bar" """ findme = "foo" index = s.find(findme) if s[index-1] == "'" and s[index+len(findme)] == "'": print "single quoted" elif s[index-1] == "\"" and s[index+len(findme)] == "\"": print "double quoted" else: print "unquoted" ~Sean -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Behavior of mutable class variables
To test some theories, I created a new class variable, an int named N1, which is not mutable. So my Stock class now looks as follows: class Stock(object): NStocks = [] #Class variables N1 = 0 def __init__(self, id, returnHistory): self.id = id self.retHist = returnHistory Stock.N1 += 1 for i in range(len(returnHistory)): if len(Stock.NStocks) <= i and retHist[i] != '': Stock.NStocks.append(1) elif len(Stock.NStocks) <= i and retHist[i] == '': Stock.NStocks.append(0) elif retHist[i] != '': Stock.NStocks[i] +=1 I expect Stock.N1 to reset to zero at each call to simulation(), but it does not - it keeps incrementing! I then added a line to simulation( ) that deletes all the stocks at the end of each simulation (see below). You guessed it - no change! NStocks and N1 keep increasing! At this point I am thoroughly mystified. What gives? def simulation(N, par1, par2, idList, returnHistoryDir): port = [] for i in range(N): port.append( Stock(idList[i], returnHistoryDir[idList[i]] ) del port[:] results = .. print results. -- http://mail.python.org/mailman/listinfo/python-list
Re: change of random state when pyc created??
On Wed, 2007-05-09 at 15:35 -0500, Alan G Isaac wrote: > Robert Kern wrote: > > http://docs.python.org/lib/typesmapping.html > > """ > > Keys and values are listed in an arbitrary order which is non-random, varies > > across Python implementations, and depends on the dictionary's history of > > insertions and deletions. > > """ > > > Even this does not tell me that if I use a specified implementation > that my results can vary from run to run. That is, it still does > not communicate that rerunning an *unchanged* program with an > *unchanged* implementation can produce a change in results. It doesn't say that rerunning the program won't produce a change in results. It doesn't say that the order depends *only* on those factors in a deterministic and reproducible manner. The documentation shouldn't be expected to list every little thing that might change the order of keys in a dictionary. The documentation does say explicitly what *is* guaranteed: Order of keys is preserved as long as no intervening modifications happen to the dictionary. Tearing down the interpreter, starting it back up, and rebuilding the dictionary from scratch is very definitely an intervening modification. Regards, -- Carsten Haese http://informixdb.sourceforge.net -- http://mail.python.org/mailman/listinfo/python-list
Re: Behavior of mutable class variables
[EMAIL PROTECTED] schrieb: > I have written a program that runs portfolio simulations with > different parameters and prints the output, but am mystified by the > behavior of a mutable class variable. A simplified version of the > program follows - would you kindly help me understand why it behaves > the way it does. > > The function main() reads some data and then repeatedly calls > simulation() with different parameter values. Each time the simulation > runs, it creates a collection of stocks, simulates their behavior and > prints the results. > > Here's what I expect to happen each time simulation( ) is called: the > class variable NStocks for the class Stock is initialized to an empty > list, and is then built up by __init__ as stocks are added to the > portfolio. Unfortunately, ths is not what actuallly happens .NStocks > is initialized to an empty list and then built up as I expect on the > first call to simulation( ), but appears to persists between calls to > simulation( ). > > Question: Why? Do I not create an entirely new list of stock objects > each time I enter simulation()? I am aware that mutable items can > behave in tricky ways, but am thoroughly mystified by the persistence > of NStocks between calls to simulation() http://www.python.org/doc/faq/general.html#why-are-default-values-shared-between-objects Diez -- http://mail.python.org/mailman/listinfo/python-list
File I/O
If one has a list of lists such as lst = [['a','1'],['b','2']] is there a standard python idiom for writing and reading the pairs to/ from a file? Thanks, jh -- http://mail.python.org/mailman/listinfo/python-list
Re: Checking if string inside quotes?
On May 9, 1:39 pm, "Michael Yanowitz" <[EMAIL PROTECTED]> wrote: > Hello: > >If I have a long string (such as a Python file). > I search for a sub-string in that string and find it. > Is there a way to determine if that found sub-string is > inside single-quotes or double-quotes or not inside any quotes? > If so how? > > Thanks in advance: > Michael Yanowitz I think the .find() method returns the index of the found string. You could check one char before and then one char after the length of the string to see. I don't use regular expressions much, but I'm sure that's a more elegant approach. This will work. You'll get in index error if you find the string at the very end of the file. s = """ foo "bar" """ findme = "foo" index = s.find(findme) if s[index-1] == "'" and s[index+len(findme)] == "'": print "single quoted" elif s[index-1] == "\"" and s[index+len(findme)] == "\"": print "double quoted" else: print "unquoted" ~Sean -- http://mail.python.org/mailman/listinfo/python-list
Re: Unzip then Zip help
[EMAIL PROTECTED] writes: > I am a true n00b... and I just using Python to complete some very > small uneventful task, but need help with one last thing. > > Basically, this I what I am trying to do. > > make a temp directory (this part I can do) > > Need help with: > ***unzip a JAR file with the complete list of subdirectories w/ > files > > modify the a set of XML files (this part I can do) > > Need help with: > ***then zip the entire contents of the temp directory with sub > directories*** > > The only thing I am having trouble with is the whole directory stuff, > if this was just straight files, no problem. > > Any help would be appreciated > Try this: import os from os.path import dirname, exists, splitext, join from zipfile import ZipFile, ZIP_DEFLATED def unpack(archname): arch = ZipFile(archname, 'r') for path in arch.namelist(): print path dname = dirname(path) if not exists(dname): os.makedirs(dname) if splitext(path)[1]: f = open(path, 'wb') f.write(arch.read(path)) f.close() arch.close() def pack(archname, paths): arch = ZipFile(archname, 'w', ZIP_DEFLATED) for path in paths: for root, dirs, files in os.walk(path): for fname in files: fname = join(root, fname) print fname arch.write(fname) arch.close() unpack('test.jar') pack('test2.jar', ['com', 'META-INF']) -- HTH, Rob -- http://mail.python.org/mailman/listinfo/python-list
Re: change of random state when pyc created??
Alan G Isaac wrote: > Robert Kern wrote: >> http://docs.python.org/lib/typesmapping.html >> """ >> Keys and values are listed in an arbitrary order which is non-random, varies >> across Python implementations, and depends on the dictionary's history of >> insertions and deletions. >> """ > > Even this does not tell me that if I use a specified implementation > that my results can vary from run to run. That is, it still does > not communicate that rerunning an *unchanged* program with an > *unchanged* implementation can produce a change in results. The last clause does tell me that. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco -- http://mail.python.org/mailman/listinfo/python-list
Behavior of mutable class variables
I have written a program that runs portfolio simulations with different parameters and prints the output, but am mystified by the behavior of a mutable class variable. A simplified version of the program follows - would you kindly help me understand why it behaves the way it does. The function main() reads some data and then repeatedly calls simulation() with different parameter values. Each time the simulation runs, it creates a collection of stocks, simulates their behavior and prints the results. Here's what I expect to happen each time simulation( ) is called: the class variable NStocks for the class Stock is initialized to an empty list, and is then built up by __init__ as stocks are added to the portfolio. Unfortunately, ths is not what actuallly happens .NStocks is initialized to an empty list and then built up as I expect on the first call to simulation( ), but appears to persists between calls to simulation( ). Question: Why? Do I not create an entirely new list of stock objects each time I enter simulation()? I am aware that mutable items can behave in tricky ways, but am thoroughly mystified by the persistence of NStocks between calls to simulation() Sincerely Thomas Philips class Stock(object): NStocks = [] #Class variable, NStocks[i] = number of valid stocks at time i def __init__(self, id, returnHistory): self.id = id self.retHist = returnHistory for i in range(len(returnHistory)): if len(Stock.NStocks) <= i and retHist[i] != '': Stock.NStocks.append(1) elif len(Stock.NStocks) <= i and retHist[i] == '': Stock.NStocks.append(0) elif retHist[i] != '': Stock.NStocks[i] +=1 def simulation(N, par1, par2, idList, returnHistoryDir): port = [] for i in range(N): port.append( Stock(idList[i], returnHistoryDir[idList[i]] ) results = .. print results. def main(): N, idList, returnHistoryDir= readData() for par1 in range(10): for par2 in range(10): simulation(N, par1, par2, idList, returnHistoryDir) -- http://mail.python.org/mailman/listinfo/python-list
Re: Unzip then Zip help
On May 9, 2:36 pm, [EMAIL PROTECTED] wrote: > I am a true n00b... and I just using Python to complete some very > small uneventful task, but need help with one last thing. > > Basically, this I what I am trying to do. > > make a temp directory (this part I can do) > > Need help with: > ***unzip a JAR file with the complete list of subdirectories w/ > files > > modify the a set of XML files (this part I can do) > > Need help with: > ***then zip the entire contents of the temp directory with sub > directories*** > > The only thing I am having trouble with is the whole directory stuff, > if this was just straight files, no problem. > > Any help would be appreciated I would use the subprocess module and command line flags for whatever zip client software you use to get the job done. For example, I have Filzip and IZArc, both of which support command line unzipping and zipping. One of my co-workers came up with a way to unzip a zipped file: def unzip(path, zipFile): """ Unzips file specified in above dictionary """ isdir = os.path.isdir join = os.path.join norm = os.path.normpath split = os.path.split for each in zipFile.namelist(): if not each.endswith('/'): root, name = split(each) directory = norm(join(path, root)) if not isdir(directory): os.makedirs(directory) file(join(directory, name), 'wb').write(zipFile.read(each)) # where path is the location you want to extract to and "zipFile" is the file.zip Good luck! Mike -- http://mail.python.org/mailman/listinfo/python-list
Re: change of random state when pyc created??
On 5/9/07, Alan G Isaac <[EMAIL PROTECTED]> wrote: > Robert Kern wrote: > > http://docs.python.org/lib/typesmapping.html > > """ > > Keys and values are listed in an arbitrary order which is non-random, varies > > across Python implementations, and depends on the dictionary's history of > > insertions and deletions. > > """ > > > Even this does not tell me that if I use a specified implementation > that my results can vary from run to run. That is, it still does > not communicate that rerunning an *unchanged* program with an > *unchanged* implementation can produce a change in results. > Well, now you know. I'm not sure why you expect any given program to be idempotent unless you take specific measures to ensure that anyway. -- http://mail.python.org/mailman/listinfo/python-list
Re: change of random state when pyc created??
Robert Kern wrote: > http://docs.python.org/lib/typesmapping.html > """ > Keys and values are listed in an arbitrary order which is non-random, varies > across Python implementations, and depends on the dictionary's history of > insertions and deletions. > """ Even this does not tell me that if I use a specified implementation that my results can vary from run to run. That is, it still does not communicate that rerunning an *unchanged* program with an *unchanged* implementation can produce a change in results. Alan Isaac -- http://mail.python.org/mailman/listinfo/python-list
Checking if string inside quotes?
Hello: If I have a long string (such as a Python file). I search for a sub-string in that string and find it. Is there a way to determine if that found sub-string is inside single-quotes or double-quotes or not inside any quotes? If so how? Thanks in advance: Michael Yanowitz -- http://mail.python.org/mailman/listinfo/python-list
Re: Questions about bsddb
Thanks for the info Nick. I plan on accessing the data in pretty much random order, and once the database is built, it will be read only. At this point Im not too concerned about access times, just getting something to work. I've been messing around with both bt and hash with limited success, which led me to think that maybe I was going beyond some internal limit for the data size.It works great on a limited set of data, but once I turn it loose on the full set, usually several hours later, it either causes a hard reset of my machine or the HD grinds on endlessly with no apparent progress. Is there a limit to the size of data you can place per key? Thanks for the MySQL suggestion, I'll take a look. -JM -- http://mail.python.org/mailman/listinfo/python-list
Re: Parameter checking on an interfase
[EMAIL PROTECTED] a écrit : > Hi all, > I am more or less new to Python, and currently am making my > first "serious" program. The application is a Clinical History manager > (for my wife) which stores its data on a sqlite database. After > googling on this newsgroup, I have read several threads where is > stated that the LBYL way of testing parameters is not a pythonic way > to work, and that is preferable catch the exceptions generated trying > to use an invalid parameter passed to a function. > Although I am generally following this approach, the problem I > see is that sqlite docs states clearly that the engine does not check > that the data types passed to the SQL sentences matches the types > declared for the column, and lets any kind of information to be put in > any column of the table. When I code the "business objects" of the > application (don't know if this is the exact term for a layer that > will isolate the application from the raw database, letting me change > it in a future if necessary), business objects and databases are not necessarily related. And business objects are much more than a "database abstraction layer" - they are the "model" part of the MVC triad, and are the heart of your application's logic. As such, they are of course in charge of validating their own state wrt/ business rules. > I realize that if I pass arguments of > wrong type (say, a numeric ID instead of the patient name), the DB > engine will accept that gladly, and I will finish with data that could > not be consistently retrievable if I use the DB from another program > (no one right now, but I think of, perhaps, statistical trends on > diseases and treatments). > In this case, could be reasonable add type checking LBYL style > on the methods, so if passed data is of wrong type, it generates a > adequate exception to be catched by the caller? This is more a problem of validation/conversion of values than strictly a typing problem IMHO. As someone said : "be liberal about what you accept and strict about what you send". > In this way, the rest > of the app (mostly GUI) can be coded EAFP style. As programming > background, as you can guess, I have made some programming in C, VBA > and JavaScript (quite procedurally). > I hope that you can bring me some light about this kind of > design, so I can improve my coding and get the Python way faster. I strongly encourage you to have a look at SQLAlchemy (a hi-level RDBMS/python interface and an ORM) and Elixir (an ActiveRecord-like declarative layer on top of SQLAlchemy), and FormEncode (an in/out validation/conversion package). http://www.sqlalchemy.org/ http://elixir.ematia.de/ FWIW, I'm actually working on using the second to add validation/conversion to the first: http://groups.google.com/group/sqlelixir/browse_thread/thread/af7b2d0b87613482 -- http://mail.python.org/mailman/listinfo/python-list
Re: tkinter get widget option value
On May 8, 6:51 pm, James Stroud <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] wrote: > > Hi, > > If I have abuttonwidget > > > w =Button(root, text = "Button", state = 'disabled') > > > How can I get the value of option 'state' from the widget 'w'. > > I want something like -- > > > print w.state >> to print out >> 'disabled' > > > Thanks > > Rahul > > print w["state"] > > James Thanks! James -- http://mail.python.org/mailman/listinfo/python-list
Re: preferred windows text editor?
GNU Emacs with python-mode -- Sönmez Kartal -- http://mail.python.org/mailman/listinfo/python-list
Re: Single precision floating point calcs?
Grant Edwards wrote: > I'm pretty sure the answer is "no", but before I give up on the > idea, I thought I'd ask... > > Is there any way to do single-precision floating point > calculations in Python? > > I know the various array modules generally support arrays of > single-precision floats. I suppose I could turn all my > variables into single-element arrays, but that would be way > ugly... We also have scalar types of varying precisions in numpy: In [9]: from numpy import * In [10]: float32(1.0) + float32(1e-8) == float32(1.0) Out[10]: True In [11]: 1.0 + 1e-8 == 1.0 Out[11]: False If you can afford to be slow, I believe there is an ASPN Python Cookbook recipe for simulating floating point arithmetic of any precision. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco -- http://mail.python.org/mailman/listinfo/python-list
Re: preferred windows text editor?
T. Crane wrote: > Right now I'm using Notepad++. What are other people using? > > trevis > > I love UltraEdit. It's not free, but very nice. -- http://mail.python.org/mailman/listinfo/python-list
Re: change of random state when pyc created??
Alan G Isaac wrote: > Diez B. Roggisch wrote: >> Not really, but that depends on what you know about the concept of sets and >> maps as collections of course. >> >> The contract for sets and dicts doesn't imply any order whatsoever. Which is >> essentially the reason why >> >> set(xrange(10))[0] >> >> doesn't exist, and quite a few times cries for an ordered dictionary as part >> of the standard libraries was made. > > It seems to me that you are missing the point, > but maybe I am missing your point. > > The question of whether a set or dict guarantees > some order seems quite different from the question > of whether rerunning an **unchanged program** yields the > **unchanged results**. The latter question is the question > of replicability. > > Again I point out that some sophisticated users > (among which I am not numbering myself) did not > see into the source of this "anomaly". This > suggests that an explicit warning is warranted. http://docs.python.org/lib/typesmapping.html """ Keys and values are listed in an arbitrary order which is non-random, varies across Python implementations, and depends on the dictionary's history of insertions and deletions. """ The sets documentation is a bit less explicit, though. http://docs.python.org/lib/types-set.html """ Like other collections, sets support x in set, len(set), and for x in set. Being an unordered collection, sets do not record element position or order of insertion. """ -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco -- http://mail.python.org/mailman/listinfo/python-list
Re: interesting exercise
On May 9, 1:13 am, Charles Sanders <[EMAIL PROTECTED]> wrote: > Michael Tobis wrote: > > Here is the bloated mess I came up with. I did see that it had to be > > recursive, and was proud of myself for getting it pretty much on the > > first try, but the thing still reeks of my sorry old fortran-addled > > mentality. > > Recursion is not necessary, but is much, much clearer. > > Here is one non-recursive version from another aging > fortran programmer. I agree it is less clear than most > of the recursive alternatives. No checks for sorted > input etc, these are left as an exercise for the reader. > > def permute( s, n ): >def _perm( m, n ): > ilist = [0]*n > while True: >yield ilist >i = n-1 >while i >= 0 and ilist[i]>=m-1: i = i - 1 >if i >= 0: > ilist = ilist[0:i] + [ilist[i]+1] + [0]*(n-i-1) >else: > return > >return [ ''.join([s[i] for i in ilist]) > for ilist in _perm(len(s),n) ] > > print "permute('abc',2) = ", permute('abc',2) > print "len(permute('13579',3)) = ", len(permute('13579',3)) > > permute('abc',2) = ['aa', 'ab', 'ac', 'ba', 'bb', 'bc', > 'ca', 'cb', 'cc'] > len(permute('13579',3)) = 125 > > or even this monstrosity ... > > def permute2( s, n ): >return [ ''.join([ s[int(i/len(s)**j)%len(s)] > for j in range(n-1,-1,-1)]) >for i in range(len(s)**n) ] > > print "permute2('abc',2) =", permute2('abc',2) > print "len(permute2('13579',3)) =", len(permute2('13579',3)) > > permute2('abc',2) = ['aa', 'ab', 'ac', 'ba', 'bb', 'bc', > 'ca', 'cb', 'cc'] > len(permute2('13579',3)) = 125 > > Charles Could you explain, this one, actually? Don't forget StopIteration. -- http://mail.python.org/mailman/listinfo/python-list
Re: preferred windows text editor?
On May 9, 3:30 pm, Trent Mick <[EMAIL PROTECTED]> wrote: > There is an ActivePython 2.5.1 > now:http://www.activestate.com/products/activepython/ > > You should give Komodo Edit a try > too:http://www.activestate.com/products/komodo_edit/ Thanks for the heads up Trent. -- http://mail.python.org/mailman/listinfo/python-list
Re: Specification for win32com.client package
On May 9, 10:26 am, Tim Golden <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] wrote: > > On May 9, 8:25 am, Tim Golden <[EMAIL PROTECTED]> wrote: > >> [EMAIL PROTECTED] wrote: > >>> The wiki idea sounds like a good one. I was thinking about doing some > >>> kind of Python site about the modules and I think the popular 3rd > >>> party ones would be a good place to start, maybe starting with win32. > >>> How much information do you think would need to be on a site like this > >>> to start out with? > >> Someone did start a Python Win32 Wiki recently (check the > >> python-win32 archives for location etc.) I did mean to put > >> things on there myself, but real life has taken over. Often, > >> these things just need someone with a bit of oomph to at > >> least get the thing going. > > >> I think what's needed (if you're offering :) is for someone > >> to put a *framework* in place on such a site which would > >> make it easy for anyone to come along and fill in the gaps > >> with their particular 3rd-party app or brand of knowledge. > >> As I say, someone did start something, but I've not heard > >> anything from him since then and I haven't found the time > >> myself. If you were to kick something off and actually get > >> it going I wouldn't say no. > > >> TJG > > > I think I found the thread you were talking about: > >http://mail.python.org/pipermail/python-win32/2007-March/005585.html > > > I went to the site listed:www.wazoozle.comand I see nothing related > > to python there. In fact, James (the poster) doesn't appear to be > > listed anywhere either. Very weird. > > Strange. Maybe he gave up and hosted something > else instead. It's not as though the name was > related :) > > > While I am not a wiki wizard, I will look into it. I might be able to > > bamboozle some free space on my friend's hosting service for such a > > project. If so, I'll let you know. > > Personally, if only to save startup pain, I'd be inclined to > use the main Python wiki, at least to get going. I know I > said at the time that I was willing to kick something off, > but days led to weeks... and you can guess the rest. > > Why not corner an area onhttp://wiki.python.org/moin/ > and put headings in place? I'm not a great fan of MoinMoin, > but it's there and it carries a (certain) measure of > authority. That said, if you want to set up on your own > space, I'm not objecting. I'll do my best (this time) to > supply info and keep things going. > > TJG It looks like there's already a "Useful Modules" page on the wiki: http://wiki.python.org/moin/ I could put the win32 stuff in there under the Platform Specific --> Windows sub-header, I suppose. Or maybe the documentation page? I dunno. Whatever I end up doing, it'll have to be one of these evenings after work is done. I'll let you know somehow. Mike -- http://mail.python.org/mailman/listinfo/python-list
Unzip then Zip help
I am a true n00b... and I just using Python to complete some very small uneventful task, but need help with one last thing. Basically, this I what I am trying to do. make a temp directory (this part I can do) Need help with: ***unzip a JAR file with the complete list of subdirectories w/ files modify the a set of XML files (this part I can do) Need help with: ***then zip the entire contents of the temp directory with sub directories*** The only thing I am having trouble with is the whole directory stuff, if this was just straight files, no problem. Any help would be appreciated -- http://mail.python.org/mailman/listinfo/python-list
Re: which is more pythonic/faster append or +=[]
On May 9, 11:08 am, 7stud <[EMAIL PROTECTED]> wrote: > On May 8, 11:05 pm, [EMAIL PROTECTED] (Alex Martelli) wrote: > > > > > alf <[EMAIL PROTECTED]> wrote: > > > two ways of achieving the same effect > > > > l+=[n] > > > > or > > > > l.append(n) > > > > so which is more pythonic/faster? > > > .append - easy to measure, too: > > > brain:~ alex$ python -mtimeit 'L=range(3); n=23' 'x=L[:]; x.append(n)' > > 100 loops, best of 3: 1.31 usec per loop > > > brain:~ alex$ python -mtimeit 'L=range(3); n=23' 'x=L[:]; x+=[n]' > > 100 loops, best of 3: 1.52 usec per loop > > > Alex > > Ah, I see. The list would grow too large with all that appending, so > you begin again with the original list for every loop. > > Is there any documentation for the syntax you are used with timeit? I > checked 'man python', and I also read the example in the python docs, > but you seem to be using a hybrid syntax. > > Thanks. If you want to have multiple statements on one line, all you need to do is add the semi-colon. That is what Alex did. I think I saw that trick in the book "Programming Python" by Lutz...or some other reference text. Here's one link to the anomaly: http://safari.oreilly.com/0201748843/ch07lev1sec5 Here's another link that mentions it as well: http://www-128.ibm.com/developerworks/opensource/library/os-python5/ Mike -- http://mail.python.org/mailman/listinfo/python-list
Re: preferred windows text editor?
[EMAIL PROTECTED] wrote: > I like IDLE but it seems to stop working after the first few times and > I would then re-install it and it would work a few times more. > ActivePython was cool but I could not find a version of it that used > Python 2.5 (as far as I can see, it only uses 2.4) ActivePython is the distribution of Python. The distro includes PyWin32 (a bunch o' Windows stuff for Python). Part of that stuff is "Pythonwin" -- the editor that you are probably referring to. There is an ActivePython 2.5.1 now: http://www.activestate.com/products/activepython/ You should give Komodo Edit a try too: http://www.activestate.com/products/komodo_edit/ Cheers, Trent -- Trent Mick trentm at activestate.com -- http://mail.python.org/mailman/listinfo/python-list
Re: preferred windows text editor?
On May 9, 2:06 pm, "T. Crane" <[EMAIL PROTECTED]> wrote: > Right now I'm using Notepad++. What are other people using? > > trevis I am very noob to this Python game and currently (I was using ActivePython and then IDLE) I have been using a combination of Notepad2 and the interpreter (Python 2.5) on a Windblows X[crement]P[roblem] SP2 machine. Get Notepad2 http://www.flos-freeware.ch/notepad2.html I like IDLE but it seems to stop working after the first few times and I would then re-install it and it would work a few times more. ActivePython was cool but I could not find a version of it that used Python 2.5 (as far as I can see, it only uses 2.4) Notepad2 allows you to launch your script directly from the editor (just like IDLE) and has configurable code highlighting. And it is FREE. -- http://mail.python.org/mailman/listinfo/python-list
Re: preferred windows text editor?
On May 9, 2:06 pm, "T. Crane" <[EMAIL PROTECTED]> wrote: > Right now I'm using Notepad++. What are other people using? I am very noob to this Python game (though it has been around since 1995 me thinks) and currently (I was using ActivePython and then IDLE) I have been using a combination of Notepad2 and the interpreter (Python 2.5) on a Windblows X[crement]P[roblem] SP2 machine. http://www.flos-freeware.ch/notepad2.html I like IDLE but it seems to stop working after the first few times and I would then re-install it and it would work a few times more. ActivePython was cool but I could not find a version of it that used Python 2.5 (as far as I can see, it only uses 2.4) Notepad2 allows you to launch your script directly from the editor (just like IDLE) and has configurable code highlighting. And it is FREE. -- http://mail.python.org/mailman/listinfo/python-list
Re: preferred windows text editor?
T. Crane wrote: > Right now I'm using Notepad++. What are other people using? > > trevis > > VIM here as well ... here we go again :P EuGeNe -- http://www.3kwa.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Inheritance problem
On May 9, 12:09 pm, [EMAIL PROTECTED] wrote: > I'm trying to solve a problem using inheritance and polymorphism in > python 2.4.2 > > I think it's easier to explain the problem using simple example: > > class shortList: > > def __init__(self): > > self.setList() > > def setList(self): > > a = [1,2,3] > > print a > > class longList(shortList): > > def __init__(self): > > shortList.setList() > > self.setList() > > def setList(self): > > a.extend([4,5,6]) > > print a > > def main(): > > a = raw_input('Do you want short or long list? (s/l)') > > if a.upper() == 'S': > > lst = shortList() > > else: > > lst = longList() > > lst.setList() > > if __name__ == '__main__': > > main() > > After that I'm getting a message: > > TypeError: unbound method setList() must be called with shortList > instance as first argument (got nothing instead) > > Where is the problem? > > Thanks in advance... As Neil indicated, you use the self object to access the current object's class members. The first parameter to a Python method is the object that the method is operating on. For a silly example: >>> class SayHi(object): ... def __init__(self, name): ... self.name = name # Save the name for later ... def Talk(self): ... print "Hi,", self.name ... >>> helloWorld = SayHi("World") >>> helloWorld.Talk() Hi, World >>> As you can see, the __init__ special method saves the name by "self.name = name". Similarly, the name is accessed in the Talk method via "self.name". Please note that the first parameter is called "self" by convention. Python doesn't care what you call the first parameter to a method. However, most of the Python world uses "self", so it is highly recommended to follow this convention. Now for the problem specific to the error message that you're getting: As above, the first parameter to a Python method is the object being operated on. Python uses a little bit of "syntactic sugar" to make calling methods easier. You can explicitly call a class method directly and pass the object in as the first parameter yourself. >>> helloWorld.Talk() # This is the normal way of calling Talk Hi, World >>> SayHi.Talk(helloWorld) # This works, too! Hi, World To easily call a superclass method, you can explicitly use the baseclass. You're attempting to do so, but you forgot one important ingrediant: the object being operated on! In C++, the object is implicit. In Python, you must explicitly send it to the superclass: >>> class SayHello(SayHi): ... def __init__(self, name): ... SayHi.__init__(self, name) # Call the superclass's init ... def Talk(self): ... print "Hello,", self.name ... >>> moreTalk = SayHello("World") >>> moreTalk.Talk() Hello, World Basically, you need to: 1. Assign your lists in setList to "self.a" instead of the local variable "a". 2. Pass the object to the superclass method in setList ("shortList.setList(self)") There are other things that may be improved with your design. Keep plugging at it! --Jason -- http://mail.python.org/mailman/listinfo/python-list
Re: preferred windows text editor?
On May 9, 2:06 pm, "T. Crane" <[EMAIL PROTECTED]> wrote: > Right now I'm using Notepad++. What are other people using? I am very noob to this Python game (though it has been around since 1995 me thinks) and currently (I was using ActivePython and then IDLE) I have been using a combination of Notepad2 http://www.flos- freeware.ch/notepad2.html and the interpreter (Python 2.5) on a Windblows X[crement]P[roblem] SP2 machine. I like IDLE but it seems to stop working after the first few times and I would then re-install it and it would work a few times more. ActivePython was cool but I could not find a version of it that used Python 2.5 (as far as I can see, it only uses 2.4) Notepad2 allows you to launch your script directly from the editor (just like IDLE) and has configurable code highlighting. And it is FREE. -- http://mail.python.org/mailman/listinfo/python-list
Re: ctypes: Problems using Windows-DLL from VB example code
Thank you very much! That did the trick. Noralf. -- http://mail.python.org/mailman/listinfo/python-list
Re: Single precision floating point calcs?
"Grant Edwards" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] | I'm pretty sure the answer is "no", but before I give up on the | idea, I thought I'd ask... | Is there any way to do single-precision floating point | calculations in Python? Make your own Python build from altered source. And run it on an ancient processor/OS/C compiler combination that does not automatically convert C floats to double when doing any sort of calculation. Standard CPython does not have C single-precision floats. The only point I can think of for doing this with single numbers, as opposed to arrays of millions, is to show that there is no point. Or do you have something else in mind? Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list