python gtk: input box and "enter"
Hello there, I am somewhat fluent in python ... good enough for the daily business scripting stuff. I have a little script that I use to start the afs service on my machine and to collect tokens for multiple afs cells. I want to enter the password using a GUI window (stdin in a terminal window is too easy to miss) ... and I found an "input_box" example for python gtk here: http://learnfobia.com/category-Computers-107/tutorial-GUI-Applications-in-Python-1935.html Problem is: this example lets me enter some text ... but when I press 'Enter' ... nothing happens. I have to move the focues to the OK button; either by clicking with the mouse or by pressing TAB. The message box example from that page works as expected - I press enter and such a window goes away. I played around a bit couldnt find the setting that would help. Can you help? How do I use gtk to enter a (hidden) text press ENTER ... done? regards, eg -- http://mail.python.org/mailman/listinfo/python-list
Re: Ironpython
Hi! IronPython is an implementation of Python. IMO, this group talk about all Pythons. Therefore, for me, this group is OK. -- Michel Claveau -- http://mail.python.org/mailman/listinfo/python-list
Retrieving exception value in C
I am trying to retrieve the value of the exception (the message part) raised in python, in C. Running the below script, import shutil fd= open("testfile","w") fd.write("some junk") fd.close() shutil.copy("testfile","testfile") will generate an exception like this, Traceback (most recent call last): File "C:\Python26\lib\myscript.py", line 10, in shutil.copy("testfile","testfile") File "C:\Python26\lib\shutil.py", line 88, in copy copyfile(src, dst) File "C:\Python26\lib\shutil.py", line 47, in copyfile raise Error, "`%s` and `%s` are the same file" % (src, dst) shutil.Error: `testfile` and `testfile` are the same file But if I run (actually import) the script from within a C code (embedding python in C), I am able to get the exception object but not the value. The code looks like, int main() { PyObject *exc, *val, *tbk, *module, *name; PyObject *exc_str; Py_Initialize(); name = PyString_FromString("myscript"); module = PyImport_Import(name); Py_DECREF(name); if(!module) { printf("error in running script\n"); if( PyErr_Occurred()) { if(PyErr_ExceptionMatches(PyExc_Exception)) { printf("exception received in C\n"); } PyErr_Fetch(&exc, &val, &tbk); exc_str = PyObject_Str(exc); printf("exception received: %s\n", PyString_AsString(exc_str)); printf("exception value: %s\n",PyString_AsString(val)); Py_DECREF(exc_str); Py_DECREF(exc); Py_DECREF(val); Py_DECREF(tbk); } else{ printf("no exception received in C\n"); } } Py_XDECREF(module); PyErr_Clear(); Py_Finalize(); return 0; } I get output, error in running script exception received in C exception received: exception value: (null) While the last line should be, exception value: `testfile` and `testfile` are the same file Although I think its not required, FYI I'm running python 2.6.2 on WinXP -- http://mail.python.org/mailman/listinfo/python-list
Re: syntax error : first python program
pradeep writes: > #!/usr/bin/env python > name = "blah" > print name These two lines are indented, but are not inside a block. > bash# /usr/bin/python sample.py > File "sample.py", line 2 > name = "blah" > ^ > SyntaxError: invalid syntax Indentation is syntax in Python. > Any one knows , whats the syntax error here? You would do well to work through all the exercises in the Python tutorial http://docs.python.org/tutorial/> to get a good grounding in all the basics like this. -- \“Human reason is snatching everything to itself, leaving | `\ nothing for faith.” —Saint Bernard, 1090–1153 | _o__) | Ben Finney -- http://mail.python.org/mailman/listinfo/python-list
Re: syntax error : first python program
pradeep wrote: I have this file in linux === sample.py #!/usr/bin/env python name = "blah" print name ... Any one knows , whats the syntax error here? You're indenting for no reason. -- Erik Max Francis && m...@alcyone.com && http://www.alcyone.com/max/ San Jose, CA, USA && 37 18 N 121 57 W && AIM/Y!M/Skype erikmaxfrancis Walk a mile in my shoes / And you'd be crazy too -- Tupac Shakur -- http://mail.python.org/mailman/listinfo/python-list
syntax error : first python program
I have this file in linux === sample.py #!/usr/bin/env python name = "blah" print name --- I executed this bash# ./sample.py File "./sample.py", line 2 name = "blah" ^ bash# /usr/bin/python sample.py File "sample.py", line 2 name = "blah" ^ SyntaxError: invalid syntax Any one knows , whats the syntax error here? -- http://mail.python.org/mailman/listinfo/python-list
Re: ScrolledText get xy index of rowheader component?
Nevermind I figured it out... I set self.dummyvar = self.scrolledtext.component('rowheader') and then did something like so self.dummyvar.index("@%d,%d" % (event.x,event.y)) not sure why it worked like that...but it did :-) -- http://mail.python.org/mailman/listinfo/python-list
Re: lib2to3 pattern creation with unexpected results
Zac Burns gmail.com> writes: > I'm trying to match any function block, the two examples were just a part of my unit tests. I know. I was just giving an example to indicate why it doesn't work. find_pattern.py is useful because it gives you a base from which it's easy to extrapolate the general pattern. > So, why should my change have any effect? Because the parser folds rules when they only have one child. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python (and me) getting confused finding keys
* Steven D'Aprano: [snip] The obvious follow-up is to ask how to make an immutable class. http://northernplanets.blogspot.com/2007/01/immutable-instances-in-python.html Thanks, I've been wondering about that. By the way, the link at the bottom in the article you linked to, referring to an earlier posting by Alex Martelli, was broken. I believe it was the posting available here: http://www.opensubscriber.com/message/python-list@python.org/2659890.html>. Cheers, - Alf -- http://mail.python.org/mailman/listinfo/python-list
Re: How to run python script in emacs
On Wed, Nov 25, 2009 at 09:38:54AM -0800, doug wrote: > > When I type C-c C-c my emacs window just hangs. If I use Task Manager > to kill cmdproxy I can get emacs back but of course interactivity with > Python is not accomplished. By the way, if I do C-c ! then I get a > functional python shell. Does anybody know a solution to this? > run emacs with --debug-init , then see the *Messages* Buffer. With Python, I use python-mode, pymacs with rope, pysmell, and anything with iPython. I prefer iPython like shell.. But see http://www.emacswiki.org/ , there's a lot of documentation. And .. Emacs version? Python version? .. etc > On Oct 13, 7:12 am, rustom wrote: > > On Sep 26, 8:54 pm, devilkin wrote: > > > > > I'm just starting learning python, and coding in emacs. I usually > > > split emacs window into two, coding in one, and run script in the > > > other, which is not very convenient. anyone can help me with it? is > > > there any tricks like emacs short cut? > > > > > also please recommand some emacs plug-ins for python programming, i'm > > > also beginner in emacs.currently i'm only using python.el. > > > > python.el comes with emacs > > python-mode.el comes from python https://launchpad.net/python-mode/ > > Because of some emacs politics the first ships with emacs although > > most uses prefer the second. > > Note 1. The key bindings are different > > Note 2. Does not work with python3. See my > > posthttp://groups.google.com/group/comp.lang.python/browse_thread/thread/... > > > > > Are any plugins supply code folding and autocomplete? > > > > See ropehttp://rope.sourceforge.net/ropemacs.htmlif you want > > but its an installation headache (requires pymacs bleeding edge > > version etc) > > I suggest you just get used to python-mode first (C-c ! and C-c C-c) > > and then explore these questions a bit later. > > > > > > > > > BTW, I'm not a english native speaker, any grammer mistakes, please > > > correct them. :) > > > > grammer is spelt grammar :-) > -- Porqué loitar e matar, se podes amar e sonhar /"\ \ / CAMPANHA DA FITA ASCII - CONTRA MAIL HTML X ASCII RIBBON CAMPAIGN - AGAINST HTML MAIL / \ -- http://mail.python.org/mailman/listinfo/python-list
Re: lib2to3 pattern creation with unexpected results
On Tue, Dec 22, 2009 at 3:21 PM, Benjamin Peterson wrote: > > The pattern for that is funcdef< 'def' 'singleLineFunc' parameters< '(' ')' > > > ':' simple_stmt< return_stmt< 'return' arith_expr< '1' '+' '2' > > '\n' > > >. No > suite. > > I'm trying to match any function block, the two examples were just a part of my unit tests. > > The first multi-line function in a file is matched twice (eg, the same > node, > results pair is passed to the transform method. > > That is odd and maybe a 2to3 bug. Could you post an example? > > Ah, found the problem here - I was running a touch_import which probably caused the node iterator to doubly-visit. Without the touch_import this works fine. > > > Yes, 2to3 pattern matching is poorly documented. scripts/find_pattern.py is > your > friend. > > This does not appear to come with python (just checked 2.6.4x86) but did find it in the trunk. Useful for sure, but it seems to only return results like the one you gave - very specific and you have to know what's going on to generalize. I eventually arrived on this pattern: """ funcdef<'def' name=NAME parameters ['->' test] ':' suite=suite> | funcdef<'def' name=NAME parameters ['->' test] ':' suite=simple_stmt> """ Which, though it works I'm still curious why because I'll be writing some more patterns here on out. The problem I have is that the grammar defines suite to be this: "simple_stmt | NEWLINE INDENT stmt+ DEDENT" So, why should my change have any effect? -- Zachary Burns (407)590-4814 Aim - Zac256FL Production Engineer (Digital Overlord) Zindagi Games -- http://mail.python.org/mailman/listinfo/python-list
Re: dict initialization
mattia wrote: Il Tue, 22 Dec 2009 23:09:04 +0100, Peter Otten ha scritto: mattia wrote: Is there a function to initialize a dictionary? Right now I'm using: d = {x+1:[] for x in range(50)} Is there any better solution? There is a dictionary variant that you don't have to initialize: from collections import defaultdict d = defaultdict(list) Peter ...and it's also the only way to do something like: def zero(): ... return 0 ... d = defaultdict(zero) In this case it's probably more Pythonic to do it this way: >>> d = defaultdict(int) s = ['one', 'two', 'three', 'four', 'two', 'two', 'one'] for x in s: ... d[x] += 1 ... d defaultdict(, {'four': 1, 'three': 1, 'two': 3, 'one': 2 }) -- http://mail.python.org/mailman/listinfo/python-list
Re: Strange Problem
Victor Subervi wrote: Hi; I have the following code: print 'printTheForm: ', descrProds, '' for value in descrProds: print 'value: ', value, '' which prints this: printTheForm: [['ID', 'tinyint', '5', '0', None], ['SKU', 'varchar', '40', '', None], ['Category', 'varchar', '40', '', None], ['Name', 'varchar', '50', '', None], ['Title', 'varchar', '100', '', None], ['Description', 'mediumtext', '100', '', None], ['Price', 'float', '8', '0.0', None], ['SortFactor', 'int', '4', '0', None], ['Availability', 'tinyint', '1', '0', '1'], ['OutOfStock', 'tinyint', '1', '0', '0'], ['ShipFlatFee', 'float', '5', '0.0', '0.00'], ['ShipPercentPrice', 'tinyint', '2', '0', '0'], ['ShipPercentWeight', 'tinyint', '2', '0', '0'], ['Associations', 'varchar', '40', '', None], ['TempPrice', 'tinyint', '1', '0', None], ['LastDatePrice', 'date', '10', '/mm/dd', None], ['Weight', 'float', '7', '0.0', None], ['Metal', 'enum', ['14k gold', '18k gold', 'white gold', 'silver', 'tungsten', 'titanium'], '', None], ['PercentMetal', 'tinyint', '2', '0', None], ['colorsShadesNumbersShort', 'set', [''], '', None]] value: ['ID', 'tinyint', '5', '0', None] value: ['SKU', 'varchar', '40', '', None] value: ['Category', 'varchar', '40', '', None] value: ['Name', 'varchar', '50', '', None] value: ['Title', 'varchar', '100', '', None] value: ['Description', 'mediumtext', '100', '', None] value: ['Price', 'float', '8', '0.0', None] value: ['SortFactor', 'int', '4', '0', None] value: ['Availability', 'tinyint', '1', '0', '1'] value: ['OutOfStock', 'tinyint', '1', '0', '0'] value: ['ShipFlatFee', 'float', '5', '0.0', '0.00'] value: ['ShipPercentPrice', 'tinyint', '2', '0', '0'] value: ['ShipPercentWeight', 'tinyint', '2', '0', '0'] value: ['Associations', 'varchar', '40', '', None] value: ['TempPrice', 'tinyint', '1', '0', None] value: ['LastDatePrice', 'date', '10', '/mm/dd', None] You'll notice that the first print statement prints out several tuples that don't get printed out in the last statement (they're truncated). Why? Perhaps you need to flush the output. -- http://mail.python.org/mailman/listinfo/python-list
Re: dict initialization
On Dec 22, 11:51 pm, mattia wrote: > Il Tue, 22 Dec 2009 23:09:04 +0100, Peter Otten ha scritto: > > > mattia wrote: > > >> Is there a function to initialize a dictionary? Right now I'm using: > >> d = {x+1:[] for x in range(50)} > >> Is there any better solution? > > > There is a dictionary variant that you don't have to initialize: > > > from collections import defaultdict > > d = defaultdict(list) > > > Peter > > ...and it's also the only way to do something like:>>> def zero(): > > ... return 0 > ...>>> d = defaultdict(zero) > >>> s = ['one', 'two', 'three', 'four', 'two', 'two', 'one'] > >>> for x in s: > > ... d[x] += 1 > ...>>> d > > defaultdict(, {'four': 1, 'three': 1, 'two': > 3, 'one': 2 > > }) > > Normally you'd write this defaultdict(int) as int() returns 0 by default. Although IIRC, the 3.1 series has a "Counter" class in collections. Jon. -- http://mail.python.org/mailman/listinfo/python-list
Re: Line indexing in Python
Lie Ryan wrote: On 12/22/2009 11:25 PM, Steve Holden wrote: > > If you want to extract an index number from the first part of of a given > line use split( split_character, maximum_splits_to_do ) and then angle > brackets to reference the first part (index 0)... > > a = "20 GOTO 10" int( a.split(' ',1)[0] ) > 20 > those are brackets, not angle brackets those [] are square brackets, not angle brackets [] are brackets, () are parentheses, {} are braces -- http://mail.python.org/mailman/listinfo/python-list
Re: dict initialization
Il Tue, 22 Dec 2009 23:09:04 +0100, Peter Otten ha scritto: > mattia wrote: > >> Is there a function to initialize a dictionary? Right now I'm using: >> d = {x+1:[] for x in range(50)} >> Is there any better solution? > > There is a dictionary variant that you don't have to initialize: > > from collections import defaultdict > d = defaultdict(list) > > Peter ...and it's also the only way to do something like: >>> def zero(): ... return 0 ... >>> d = defaultdict(zero) >>> s = ['one', 'two', 'three', 'four', 'two', 'two', 'one'] >>> for x in s: ... d[x] += 1 ... >>> d defaultdict(, {'four': 1, 'three': 1, 'two': 3, 'one': 2 }) >>> -- http://mail.python.org/mailman/listinfo/python-list
Re: Python-list Digest, Vol 75, Issue 226
En Tue, 22 Dec 2009 16:30:58 -0300, r0g escribió: Gabriel Genellina wrote: En Mon, 21 Dec 2009 16:30:13 -0300, Pulkit Agrawal escribió: I am writing a script wherein I need to merge files into existing tar.gz files. Currently, I am using tarfile module. I extract the tar.gz to a tempdir and copy the new file there and re-compress all the files back into a tar.gz. Is there a better way to do it? Since noone answered yet: no, I don't think you can avoid to decompress and recompress those files. Erm, I always thought it was OK to simply cat gzipped files together... Maybe, but still I don't think this could help the OP. As I understand the problem, originally there were e.g.: file1, file2, file3; they were tarred into file123.tar and gzipped into file123.tar.gz. And now file2 must be replaced by a newer version. It should go into the internal .tar file, replacing the old one; I don't see how to do that without decompressing it. (Ok, once the tar is decompressed one might replace the old file with the newer one in-place using the tar command, but this cannot be done with the tarfile Python module) -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: OS independent way to check if a python app is running?
Dan Sommers wrote: On Mon, 14 Dec 2009 14:14:05 -0500, python wrote: Is there an os independent way to check if a python app is running? Goal: I have a server program based on cherrypy that I only want to have running once. If a system administrator accidentally attempts to run this program more than once, I would like the 2nd instance of the program to detect that its already running and exit. Maybe I'm missing something, but the locking mechanism already exists: at some point, your server program has to bind to an IP port to listen for incoming request, and any respectable OS won't let two programs bind to the same port at the same time. Unfortunately, Windows is not a respectable OS. Unlike Unix, it allows two processes to bind to the same port. The theory is that this somehow allows the two processes to share their workload. One thing the OP can portably do, is try to connect() to the port. If that succeeds, then a server program is already running at that port, so he should exit. Hope this helps, -- HansM -- http://mail.python.org/mailman/listinfo/python-list
Re: lib2to3 pattern creation with unexpected results
Zac Burns gmail.com> writes: > > > Greetings,I'm trying to re-purpose the lib2to3 module and along the way came up with this pattern:"funcdef<'def' name=NAME parameters ['->' test] ':' suite=suite>"It seems to have 2 problems: > > Single-line defs are not matched. Eg: "def singleLineFunc(): return 1 + 2" is not matched, but "def multiLineFunc():\n a = 1 + 2\n return a" is matched. The pattern for that is funcdef< 'def' 'singleLineFunc' parameters< '(' ')' > ':' simple_stmt< return_stmt< 'return' arith_expr< '1' '+' '2' > > '\n' > >. No suite. > The first multi-line function in a file is matched twice (eg, the same node, results pair is passed to the transform method. That is odd and maybe a 2to3 bug. Could you post an example? > > Why are these happening? If anyone could point me to a general discussion about creating these would be most helpful. I haven't found a lot of resources for these and investigating has been more of a struggle than usual. Yes, 2to3 pattern matching is poorly documented. scripts/find_pattern.py is your friend. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python (and me) getting confused finding keys
On Tue, 22 Dec 2009 17:33:04 +0100, John wrote: > Hi there, > > I have a rather lengthy program that troubles me for quite some time. > After some debugging, I arrived at the following assertion error: > > for e in edges.keys(): > assert edges.has_key(e) > > Oops!? Is there ANY way that something like this can possibly happen? In another post, you assert that: (1) You aren't knowingly using threads, so it's not likely that another thread is modifying edges. (2) edges is a regular dictionary, not a custom mapping class. In that case, I would say that the most likely culprit is that the edges are mutable but given a hash function. I can reproduce the problem like this: >>> class Edge: ... def __init__(self, start, finish): ... self.ends = (start, finish) ... def __eq__(self, other): ... return self.ends == other.ends ... def __hash__(self): ... return hash(self.ends) ... >>> edges = {Edge(1, 5): None} >>> for e in edges: ... assert e in edges # same as edges.has_key(e) ... e.ends = (5, 6) ... assert e in edges, "and now it's gone" ... Traceback (most recent call last): File "", line 4, in AssertionError: and now it's gone So the likely problem is that your edge type is mutable and being mutated. Now, if your code snippet above: for e in edges.keys(): assert edges.has_key(e) is a literal copy-and-paste from the failing code, I can only assume that the edge type __eq__ or __hash__ method mutates self. The lesson of this? Do not make mutable classes hashable. The obvious follow-up is to ask how to make an immutable class. http://northernplanets.blogspot.com/2007/01/immutable-instances-in-python.html -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: dict initialization
Il Tue, 22 Dec 2009 23:09:04 +0100, Peter Otten ha scritto: > mattia wrote: > >> Is there a function to initialize a dictionary? Right now I'm using: >> d = {x+1:[] for x in range(50)} >> Is there any better solution? > > There is a dictionary variant that you don't have to initialize: > > from collections import defaultdict > d = defaultdict(list) > > Peter Great, thanks. Now when I call the dict key I also initialize the value, good also using something like: if d[n]: d[n].append(val) -- http://mail.python.org/mailman/listinfo/python-list
Re: Python (and me) getting confused finding keys
On Tue, 22 Dec 2009 17:47:14 +0100, Christian Heimes wrote: > John schrieb: >> Hi there, >> >> I have a rather lengthy program that troubles me for quite some time. >> After some debugging, I arrived at the following assertion error: >> >> for e in edges.keys(): >> assert edges.has_key(e) >> >> Oops!? Is there ANY way that something like this can possibly happen? > > Yes, it happens when another part of your program -- most likely a > thread -- modifies edges while you are iterating over its keys. The > keys() method of a dict returns a *copy* of its keys. If you had uses > "for e in edges" you'd have seen a RuntimeError "dictionary changed size > during iteration". To be pedantic, you *might* have seen a RuntimeError, as the heuristic for detecting modifications during iteration is fairly simple and can only detect changes that change the size of the dict. >>> d = {1: 'a', 2: 'b', 3: 'c'} >>> n = 1 >>> for key in d: ... del d[n] ... d[str(n)] = None ... n += 1 ... >>> d {'1': None, '2': None, '3': None} -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: dict initialization
mattia wrote: > Is there a function to initialize a dictionary? > Right now I'm using: > d = {x+1:[] for x in range(50)} > Is there any better solution? There is a dictionary variant that you don't have to initialize: from collections import defaultdict d = defaultdict(list) Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: Strange Problem
Victor Subervi wrote: Hi; I have the following code: print 'printTheForm: ', descrProds, '' for value in descrProds: print 'value: ', value, '' which prints this: printTheForm: [['ID', 'tinyint', '5', '0', None], ['SKU', 'varchar', '40', '', None], ['Category', 'varchar', '40', '', None], ['Name', 'varchar', '50', '', None], ['Title', 'varchar', '100', '', None], ['Description', 'mediumtext', '100', '', None], ['Price', 'float', '8', '0.0', None], ['SortFactor', 'int', '4', '0', None], ['Availability', 'tinyint', '1', '0', '1'], ['OutOfStock', 'tinyint', '1', '0', '0'], ['ShipFlatFee', 'float', '5', '0.0', '0.00'], ['ShipPercentPrice', 'tinyint', '2', '0', '0'], ['ShipPercentWeight', 'tinyint', '2', '0', '0'], ['Associations', 'varchar', '40', '', None], ['TempPrice', 'tinyint', '1', '0', None], ['LastDatePrice', 'date', '10', '/mm/dd', None], ['Weight', 'float', '7', '0.0', None], ['Metal', 'enum', ['14k gold', '18k gold', 'white gold', 'silver', 'tungsten', 'titanium'], '', None], ['PercentMetal', 'tinyint', '2', '0', None], ['colorsShadesNumbersShort', 'set', [''], '', None]] value: ['ID', 'tinyint', '5', '0', None] value: ['SKU', 'varchar', '40', '', None] value: ['Category', 'varchar', '40', '', None] value: ['Name', 'varchar', '50', '', None] value: ['Title', 'varchar', '100', '', None] value: ['Description', 'mediumtext', '100', '', None] value: ['Price', 'float', '8', '0.0', None] value: ['SortFactor', 'int', '4', '0', None] value: ['Availability', 'tinyint', '1', '0', '1'] value: ['OutOfStock', 'tinyint', '1', '0', '0'] value: ['ShipFlatFee', 'float', '5', '0.0', '0.00'] value: ['ShipPercentPrice', 'tinyint', '2', '0', '0'] value: ['ShipPercentWeight', 'tinyint', '2', '0', '0'] value: ['Associations', 'varchar', '40', '', None] value: ['TempPrice', 'tinyint', '1', '0', None] value: ['LastDatePrice', 'date', '10', '/mm/dd', None] You'll notice that the first print statement prints out several tuples that don't get printed out in the last statement (they're truncated). Why? TIA, beno As Emile points out, you're clearly doing some other processing between that print and the place you're capturing the output. Presumably this is a CGI script or equivalent. So several other layers of code are manipulating that stream before you see it. Have you added another print immediately after the loop, so you can tell that it ended, and that subsequent output is separated from it? My first guess was that you have some character in there that's special to html, such as "<", but I don't see such. In general, you might need to escape your data (using & escape sequences), rather than just printing it directly to the CGI stream. Another clue for this type of problem is to look at the "page source" in your browser, rather than trust its rendering. Sometimes the rendering gets messed up, especially if the html is not strictly legal. In Firefox, use "View -> Page Source" to see the source to the page, which should come pretty close to the output of your print statements. My preference would be to run the script outside of the web-server environment, either on a local copy, or by shelling into your server. DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: dict initialization
"mattia" wrote in message news:4b313b3a$0$1135$4fafb...@reader1.news.tin.it... Is there a function to initialize a dictionary? Right now I'm using: d = {x+1:[] for x in range(50)} Is there any better solution? Depending on your use case, a defaultdict might suite you: from collections import defaultdict D=defaultdict(list) D[0] [] D[49] [] If the key doesn't exist, it will be initialized by calling the factory function provided in the constructor. -Mark -- http://mail.python.org/mailman/listinfo/python-list
Re: dict initialization
On 22-12-2009 22:33, mattia wrote: Is there a function to initialize a dictionary? Right now I'm using: d = {x+1:[] for x in range(50)} Is there any better solution? Maybe you can use: dict.fromkeys(xrange(1,51)) but this will initialize all values to None instead of an empty list... -irmen -- http://mail.python.org/mailman/listinfo/python-list
Re: dict initialization
On 12/22/2009 1:33 PM mattia said... Is there a function to initialize a dictionary? Right now I'm using: d = {x+1:[] for x in range(50)} Is there any better solution? I tend to use setdefault and fill in as I go, but if you need to have a complete 50-element dict from the get go, I'd probably do the same. >>> D = {} >>> >>> import random >>> for ii in range(20): ... L=D.setdefault(random.randint(0,9),[]) ... L.append('.') Emile -- http://mail.python.org/mailman/listinfo/python-list
Re: dict initialization
On 2009-12-22 15:33 PM, mattia wrote: Is there a function to initialize a dictionary? Right now I'm using: d = {x+1:[] for x in range(50)} Is there any better solution? For things like this? No. If you find yourself writing this pattern frequently, though, you can wrap it up in a function and call that function to get your initialized dicts. -- 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
dict initialization
Is there a function to initialize a dictionary? Right now I'm using: d = {x+1:[] for x in range(50)} Is there any better solution? -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem with win32ui
On Dec 22, 11:05 am, "Gabriel Genellina" wrote: > En Tue, 22 Dec 2009 12:31:37 -0300, Marc Grondin > escribió: > > > Hello everyone, > > So i have been building an app with python(and learning as i go along) my > > knowledge of python is still kinda limited but the app work on my pc. I > > have > > also compiled it to an exe using py2exe and it also works fine this way > > on > > my pc(where python is installed) if however i try to run it from a pc > > where > > python is not installed i get this message: > > > Traceback (most recent call last): > > File "printorders.py", line 2, in > > File "win32ui.pyc", line 12, in > > File "win32ui.pyc", line 10, in __load > > ImportError: DLL load failed: The specified module could not be found. > > There is a missing DLL. Dependency Walker is a useful tool to solve this > kind of > problems:http://technet.microsoft.com/en-us/library/cc738370(WS.10).aspx > Once you know which DLL is missing, add it to your setup.py > > -- > Gabriel Genellina Also make sure that you can legally distribute the dll. --- Mike Driscoll Blog: http://blog.pythonlibrary.org PyCon 2010 Atlanta Feb 19-21 http://us.pycon.org/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Python (and me) getting confused finding keys
On 12/22/2009 11:33 AM, John wrote: Hi there, I have a rather lengthy program that troubles me for quite some time. After some debugging, I arrived at the following assertion error: for e in edges.keys(): assert edges.has_key(e) If you are claiming that the above *did* raise AssertionError, then you should show a complete, standalone example, including the code that created edges. That includes the class statement since the above would not happen with a builtin dict. Unless, of oourse, playing guessing games is your intention ;-). tjr -- http://mail.python.org/mailman/listinfo/python-list
Re: Strange Problem
On 12/22/2009 10:57 AM Victor Subervi said... Hi; I have the following code: print 'printTheForm: ', descrProds, '' This doesn't match what you say the below is the output from. There's no below... for value in descrProds: print 'value: ', value, '' When I assign your result labeled printTheForm to descrProds and execute the above I get the result you expect. The why probably lies somewhere in between... Emile which prints this: printTheForm: [['ID', 'tinyint', '5', '0', None], ['SKU', 'varchar', '40', '', None], ['Category', 'varchar', '40', '', None], ['Name', 'varchar', '50', '', None], ['Title', 'varchar', '100', '', None], ['Description', 'mediumtext', '100', '', None], ['Price', 'float', '8', '0.0', None], ['SortFactor', 'int', '4', '0', None], ['Availability', 'tinyint', '1', '0', '1'], ['OutOfStock', 'tinyint', '1', '0', '0'], ['ShipFlatFee', 'float', '5', '0.0', '0.00'], ['ShipPercentPrice', 'tinyint', '2', '0', '0'], ['ShipPercentWeight', 'tinyint', '2', '0', '0'], ['Associations', 'varchar', '40', '', None], ['TempPrice', 'tinyint', '1', '0', None], ['LastDatePrice', 'date', '10', '/mm/dd', None], ['Weight', 'float', '7', '0.0', None], ['Metal', 'enum', ['14k gold', '18k gold', 'white gold', 'silver', 'tungsten', 'titanium'], '', None], ['PercentMetal', 'tinyint', '2', '0', None], ['colorsShadesNumbersShort', 'set', [''], '', None]] value: ['ID', 'tinyint', '5', '0', None] value: ['SKU', 'varchar', '40', '', None] value: ['Category', 'varchar', '40', '', None] value: ['Name', 'varchar', '50', '', None] value: ['Title', 'varchar', '100', '', None] value: ['Description', 'mediumtext', '100', '', None] value: ['Price', 'float', '8', '0.0', None] value: ['SortFactor', 'int', '4', '0', None] value: ['Availability', 'tinyint', '1', '0', '1'] value: ['OutOfStock', 'tinyint', '1', '0', '0'] value: ['ShipFlatFee', 'float', '5', '0.0', '0.00'] value: ['ShipPercentPrice', 'tinyint', '2', '0', '0'] value: ['ShipPercentWeight', 'tinyint', '2', '0', '0'] value: ['Associations', 'varchar', '40', '', None] value: ['TempPrice', 'tinyint', '1', '0', None] value: ['LastDatePrice', 'date', '10', '/mm/dd', None] You'll notice that the first print statement prints out several tuples that don't get printed out in the last statement (they're truncated). Why? TIA, beno -- http://mail.python.org/mailman/listinfo/python-list
Re: RotatingFileHandler key error when parsing a logging config file
On Dec 21, 9:33 pm, jordilin wrote: > Hi, > I've a config forloggingwhere I set up a file rotation with > handlers.RotatingFileHandler and when the app parses thelogging > config it says keyError when trying to parse that section > ('RotatingFileHandler' is not defined). Curiously enough, I can do > importloggingand fromlogging.handlers import RotatingFileHandler. > > Example: > > [handlers] > keys=handlers.RotatingFileHandler > > [formatters] > keys=simpleFormatter > > [logger_root] > level=DEBUG > handlers=handlers.RotatingFileHandler > > [handler_handlers.RotatingFileHandler] > class=handlers.RotatingFileHandler > level=DEBUG > formatter=simpleFormatter > > I'm using python 2.4 in the servers. I'm having this in a particular > one, which seems like there must be some kind of configuration error. > Any suggestions, > Thanks Can you show the error message/traceback which you're getting? With earlier versions of the logging package, handler class names were evaluated in the context of the logging module (so handlers.RotatingFileHandler should work). Later versions (than 2.4) will try to resolve using normal import mechanisms, so foo.bar.MyHandler should work. It does seem like a configuration error (esp. if this same code works on other machines running Python 2.4), but it's hard to say what it is with the information you've provided so far. Regards, Vinay Sajip -- http://mail.python.org/mailman/listinfo/python-list
Re: Line indexing in Python
Steve Holden wrote: > r0g wrote: >> seafoid wrote: >>> Hi Guys, >>> >>> When python reads in a file, can lines be referred to via an index? >>> >>> Example: >>> >>> for line in file: >>> if line[0] == '0': >>> a.write(line) >>> >>> This works, however, I am unsure if line[0] refers only to the first line or >>> the first character in all lines. >>> >>> Is there an easy way to refer to a line with the first character being a >>> single letter that you know? >>> >>> Thanks in advance, >>> Seafoid. >> >> If you want to know the index number of an item in a sequence you are >> looping through (whether it be a file of lines or a list of characters, >> whatever) use enumerate... >> > for index, value in enumerate("ABCD"): >> print index, value >> ... >> 0 A >> 1 B >> 2 C >> 3 D >> >> >> If you want to extract an index number from the first part of of a given >> line use split( split_character, maximum_splits_to_do ) and then angle >> brackets to reference the first part (index 0)... >> >> > a = "20 GOTO 10" > int( a.split(' ',1)[0] ) >> 20 >> > > those are brackets, not angle brackets > > > regards > Steve They're actually square brackets, "brackets" on its own is more commonly used as a synonym for parentheses (round brackets). But yes, I did get that wrong in the above ;) Cheers, Roger :) -- http://mail.python.org/mailman/listinfo/python-list
Re: Python-list Digest, Vol 75, Issue 226
Gabriel Genellina wrote: > En Mon, 21 Dec 2009 16:30:13 -0300, Pulkit Agrawal > escribió: > >> I am writing a script wherein I need to merge files into existing tar.gz >> files. Currently, I am using tarfile module. I extract the tar.gz to a >> tempdir and copy the new file there and re-compress all the files back >> into >> a tar.gz. Is there a better way to do it? > > Since noone answered yet: no, I don't think you can avoid to decompress > and recompress those files. > Erm, I always thought it was OK to simply cat gzipped files together... >From man gzip... Multiple compressed files can be concatenated. In this case, gunzip will extract all members at once. For example: gzip -c file1 > foo.gz gzip -c file2 >> foo.gz Then gunzip -c foo is equivalent to cat file1 file2 Roger. -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem with win32ui
ok so that got me a little further. The app now works on win2k but on winXP i get this: File "printorders.py", line 2, in File "win32ui.pyc", line 12, in File "win32ui.pyc", line 10, in __load ImportError: DLL load failed: This application has failed to start because the a pplication configuration is incorrect. Reinstalling the application may fix this problem. Dependencie walker does not find any major issues. what am i missing here. thank you in advance for your help. -- C-ya Later Take Care Marc Grondin -- http://mail.python.org/mailman/listinfo/python-list
Re: C Structure rebuild with ctypes
"Georg" wrote in message news:7pb8ubfll...@mid.individual.net... Hi Mark, many thanks for your valuable help. # numVars contains size of returned arrays. Recast to access. varNamesArray = c.cast(varNames,c.POINTER(PCHAR * numVars.value)) varTypesArray = c.cast(varTypes,c.POINTER(INT * numVars.value)) One last question: You created an object varNamesArray as an ctypes array. This object has a method "contents". How do I find out what other methods this objects has? For instance a method to retrieve the size of the array? Is this documented somewhere? The "contents" property is how you dereference a ctypes pointer. See "16.15. ctypes" and specifically "16.15.1.14. Pointers" in the Python 2.6.4 documentation. "16.15.1.13. Arrays" documents that arrays support len(). import ctypes as c a = (c.c_int * 5)() p = c.pointer(a) p.contents # p points to an array of 5 integers <__main__.c_long_Array_5 object at 0x009B46C0> len(p.contents) # or just len(a) 5 You can use "dir(p)" or "help(p)" to find out information about methods and attributes of an object. Both show that 'p' has a contents attribute and 'a' supports the __len__ method. -Mark -- http://mail.python.org/mailman/listinfo/python-list
Strange Problem
Hi; I have the following code: print 'printTheForm: ', descrProds, '' for value in descrProds: print 'value: ', value, '' which prints this: printTheForm: [['ID', 'tinyint', '5', '0', None], ['SKU', 'varchar', '40', '', None], ['Category', 'varchar', '40', '', None], ['Name', 'varchar', '50', '', None], ['Title', 'varchar', '100', '', None], ['Description', 'mediumtext', '100', '', None], ['Price', 'float', '8', '0.0', None], ['SortFactor', 'int', '4', '0', None], ['Availability', 'tinyint', '1', '0', '1'], ['OutOfStock', 'tinyint', '1', '0', '0'], ['ShipFlatFee', 'float', '5', '0.0', '0.00'], ['ShipPercentPrice', 'tinyint', '2', '0', '0'], ['ShipPercentWeight', 'tinyint', '2', '0', '0'], ['Associations', 'varchar', '40', '', None], ['TempPrice', 'tinyint', '1', '0', None], ['LastDatePrice', 'date', '10', '/mm/dd', None], ['Weight', 'float', '7', '0.0', None], ['Metal', 'enum', ['14k gold', '18k gold', 'white gold', 'silver', 'tungsten', 'titanium'], '', None], ['PercentMetal', 'tinyint', '2', '0', None], ['colorsShadesNumbersShort', 'set', [''], '', None]] value: ['ID', 'tinyint', '5', '0', None] value: ['SKU', 'varchar', '40', '', None] value: ['Category', 'varchar', '40', '', None] value: ['Name', 'varchar', '50', '', None] value: ['Title', 'varchar', '100', '', None] value: ['Description', 'mediumtext', '100', '', None] value: ['Price', 'float', '8', '0.0', None] value: ['SortFactor', 'int', '4', '0', None] value: ['Availability', 'tinyint', '1', '0', '1'] value: ['OutOfStock', 'tinyint', '1', '0', '0'] value: ['ShipFlatFee', 'float', '5', '0.0', '0.00'] value: ['ShipPercentPrice', 'tinyint', '2', '0', '0'] value: ['ShipPercentWeight', 'tinyint', '2', '0', '0'] value: ['Associations', 'varchar', '40', '', None] value: ['TempPrice', 'tinyint', '1', '0', None] value: ['LastDatePrice', 'date', '10', '/mm/dd', None] You'll notice that the first print statement prints out several tuples that don't get printed out in the last statement (they're truncated). Why? TIA, beno -- http://mail.python.org/mailman/listinfo/python-list
Re: Python (and me) getting confused finding keys
On 12/23/2009 3:33 AM, John wrote: Hi there, I have a rather lengthy program that troubles me for quite some time. After some debugging, I arrived at the following assertion error: for e in edges.keys(): assert edges.has_key(e) Oops!? Is there ANY way that something like this can possibly happen? in a multithreaded program, it's possible another thread erased 'e' after the for-loop grabbed it but before the suite is executed. but often you'll get something like this: RuntimeError: dictionary changed size during iteration -- http://mail.python.org/mailman/listinfo/python-list
Re: Threading with queues
On 12/22/2009 10:47 AM, Gib Bogle wrote: This is indented over one indentation level too much. You want it to be at the same level as the for above. Here, its at the same level with "t" -- meaning this entire loop gets repeated five times. I sorta really recommend a tab width of 4 spaces, not 2 :) At 2, its _really_ hard (especially if you're newer to Python) to see these kinds of issues and since indentation is program logic and structure in Python, that's bad... especially since your comment is indented to the right level, but the code isn't :) --S It turns out that this code isn't a great demo of the advantages of threading, on my system anyway. The time taken to execute doesn't vary much when the number of threads is set anywhere from 1 to 6. it does in mine: Elapsed Time: 7.4737711 (with one thread) Elapsed Time: 1.9015041 (with five threads) what sort of weird machine are you in? -- http://mail.python.org/mailman/listinfo/python-list
how to read stdout without blocking
I need to run a cmd line app from python whose run time can vary from a fraction of a second to minutes. I need to be able to tell whether the process is still waiting, or whether it has stalled - and this is farily easily done by keeping an eye on whether its writing to stdout or not. The process under working conditions will write to stdout fairly regularly. So I want to bail out waiting for the process if it is no longer writing to stdout, but whenever I read stdout the script blocks and doesn't return until the process has finished. This is under windows, python 2.5. Can anyone help? The code I'm using is below. import subprocess import time TIMEOUT_PERIOD = 5 #timeout period in seconds def run( *args ): cmdStr = 'somePotentiallyLengthyCmdHere' try: proc = subprocess.Popen( cmdStr, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE ) except OSError: return False startTime = time.clock() stdoutAccum = [] stderrAccum = [] hasTimedOut = False while True: ret = proc.poll() newStdout = proc.stdout.read() newStderr = proc.stderr.read() print 'waiting...' stdoutAccum += newStdout stderrAccum += newStderr #if the proc has terminated, deal with returning appropriate data if ret is not None: if hasTimedOut: if callable( RETURNED_CALLBACK ): try: RETURNED_CALLBACK( *args ) except: pass return stdoutAccum + stderrAccum #if there has been new output, the proc is still alive so reset counters if newStderr or newStdout: startTime = time.clock() #make sure we haven't timed out curTime = time.clock() if curTime - startTime > TIMEOUT_PERIOD: hasTimedOut = True -- http://mail.python.org/mailman/listinfo/python-list
Re: Anybody use web2py?
Some may find useful to compare: - A Crash Course on Django http://articles.sitepoint.com/article/django-crash-course - A Crash Course on Web2py http://www.web2py.com/AlterEgo/default/show/253 They basically describe the same app and the steps to built it. Sorry I had not time to make screenshots. I personally think it is great that we can learn from each other from this kind of comparison and we can both improve. I also think that stressing the similarities and the differences will help prospective users understand the underlying design patterns. Massimo -- http://mail.python.org/mailman/listinfo/python-list
sqlite3 .mode option to create HTML table automatically?
the CLI for sqlite3 shows .mode of "html", which formats the output in HTML format that is good to add to . BUT i have not yet found anything for sqlite in python that does this. in fact, i found an old post saying 'if you want the output in a table, you must create it yourself'. Does anyone know if this is still the case? also, i'd like to know if i can get headers in the result set. The CLI uses '.header ON' to enable this. is there a place where i can change this too? thanks! David -- http://mail.python.org/mailman/listinfo/python-list
Re: Windows, IDLE, __doc_, other
On 12/22/2009 12:06 PM, W. eWatson wrote: Stephen Hansen wrote: On Mon, Dec 21, 2009 at 2:57 PM, W. eWatson wrote: [snip Now, I go to the script and enter from math import * dir is now bulked up with the math functions. I change back math.cos to cos and the program runs well. This sort of figures. Apparently, I've added to the namespace by importing with *. Apparently? -- you precisely and explicitly added every object in 'math' to your current namespace. "from math import *" does precisely that. Well, it's a big surprise to me, because I thought each time I ran from the editor that it reloaded the modules in my imports, and cleared out any it didn't find. > My point is that I'm betting different results. OK, fine. It appears the same thing happens with I modify the program itself with from math import * Different results? What different results are you talking about? > It seems to me as I fool around with interpreter under the script window, I"m creating a mess out of the namespace the program uses, and the program responds incorrectly. After a script's execution, IDLE's shell namespace uses the last scripts's namespace; this is similar to using the -i switch in the terminal: $ python -i myscript.py program output >>> foo() # foo() is a function defined in myscript.py this is often useful for debugging If you want to access 'sin' without 'math.', you'll have to do 'from math import *' in each file where you want to do that. So IDLE is not clearing the namespace each time I *run* the program. This is not good. I've been fooled. So how do I either clear the namespace before each Run? Do I have to open the file in the editor again each time before trying to Run it? I hope there's a better way. How do you figure its 'not clearing the namespace'? In which namespace? I fire up IDLE, and start a new file, and put in a single Try this sequence. I just started plugging away again with IDLE and am pretty convinced that IDLE is something of an enemy. I started afresh loading this into the editor: import math print "hello, math world." print math.cos(0.5) print math.sin(0.8) Run works fine. No errors. Now I do: >>> dir() ['__builtins__', '__doc__', '__file__', '__name__', 'idlelib', 'math'] >>> OK, swell. Now I import via the script window >>> import numpy as np >>> dir() ['__builtins__', '__doc__', '__file__', '__name__', 'idlelib', 'math', 'np'] I think I'm adding to the namespace, both the program the shell sees, because adding this ref to np in the program works fine. import math print "hello, math world." print math.cos(0.5) print math.sin(0.8) print np.sin(2.2) --- There's no np in that code, but yet it works. It must be in the namespace it sees, and it was put there through the interactive shell. You must be starting IDLE without subprocess. Did you see this message IDLE 2.6.1 No Subprocess when starting IDLE. If you're on Windows, don't use the "Edit with IDLE" right-click hotkey since that starts IDLE without subprocess. Use the shortcut installed in your Start menu. line: "a = 1". I choose Run Module, and it runs it. I verify in the interactive interpreter that a is 1. I then change that file to "a = a + 1", and run it. Now, it errors out-- of course-- because IDLE "cleared" the namespace and re-ran the module. Hmmm, that appears to contrary to my numpy experience. I've never seen any re-starting msg. Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. That is irrelevant with numpy. If you start IDLE with subprocess, then every time before you run a script this message appears: >>> = RESTART = PS: you can force IDLE to restart the subprocess with Ctrl+F6 It says in the interpreter its restarting, even. When IDLE is not run with subprocess, running a script is equivalent to copy and pasteing the script to the shell. -- http://mail.python.org/mailman/listinfo/python-list
Re: Default working directory
On 22 dic, 18:22, "Gabriel Genellina" wrote: > En Tue, 22 Dec 2009 14:04:23 -0300, vsoler > escribió: > > > I'm using Python 2.6.4 on Windows (Vista & 7) > > > I usually start Python by clicking on Start Menu the Python IDLE > > (Python GUI). > > > However, if I want to save a new source *.py file, the default > > directory proposed for saving is not the one that I want. > > > What can I do if I want that the change of default directory > > permanently, that is, if I quit python and I reenter it, the default > > is still the one I have chosen? > > Right click on the IDLE shortcut in the Start menu (or create a new > shortcut on your desktop), choose Properties, and set the Startup > Directory to your preferred directory. > > -- > Gabriel Genellina I'll try it! Thank you -- http://mail.python.org/mailman/listinfo/python-list
Re: Line indexing in Python
On 12/22/2009 11:25 PM, Steve Holden wrote: > > If you want to extract an index number from the first part of of a given > line use split( split_character, maximum_splits_to_do ) and then angle > brackets to reference the first part (index 0)... > > a = "20 GOTO 10" int( a.split(' ',1)[0] ) > 20 > those are brackets, not angle brackets those [] are square brackets, not angle brackets -- http://mail.python.org/mailman/listinfo/python-list
lib2to3 pattern creation with unexpected results
Greetings, I'm trying to re-purpose the lib2to3 module and along the way came up with this pattern: "funcdef<'def' name=NAME parameters ['->' test] ':' suite=suite>" It seems to have 2 problems: 1. Single-line defs are not matched. Eg: "def singleLineFunc(): return 1 + 2" is not matched, but "def multiLineFunc():\n a = 1 + 2\n return a" is matched. 2. The first multi-line function in a file is matched twice (eg, the same node, results pair is passed to the transform method. Why are these happening? If anyone could point me to a general discussion about creating these would be most helpful. I haven't found a lot of resources for these and investigating has been more of a struggle than usual. Thanks, -- Zachary Burns (407)590-4814 Aim - Zac256FL Production Engineer (Digital Overlord) Zindagi Games -- http://mail.python.org/mailman/listinfo/python-list
Re: Default working directory
En Tue, 22 Dec 2009 14:04:23 -0300, vsoler escribió: I'm using Python 2.6.4 on Windows (Vista & 7) I usually start Python by clicking on Start Menu the Python IDLE (Python GUI). However, if I want to save a new source *.py file, the default directory proposed for saving is not the one that I want. What can I do if I want that the change of default directory permanently, that is, if I quit python and I reenter it, the default is still the one I have chosen? Right click on the IDLE shortcut in the Start menu (or create a new shortcut on your desktop), choose Properties, and set the Startup Directory to your preferred directory. -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: Python (and me) getting confused finding keys
En Tue, 22 Dec 2009 13:56:36 -0300, John escribió: another thread can remove the key prior to the has_key call; or perhaps edges isn't a real dictionary? of course. But unless there is a way of using threading without being aware of it, this is not the case. Also, edges is definitely a dict (has been declared some lines before, and no obscure functions have been called in the meantime, just some adding to edges). Python would also fail on edges.keys() if edges wasn't a dict... Ok, then your edges are mutable: py> class Edge: ... def __init__(self, x): ... self.x = x ... def __eq__(self, other): ... return self.x==other.x ... def __hash__(self): ... return hash(self.x) ... py> e1 = Edge(1) py> e2 = Edge(2) py> e3 = Edge(3) py> edges = {e1:None, e2:None, e3:None} py> e2.x = 5 py> for e in edges.keys(): ... assert edges.has_key(e) ... Traceback (most recent call last): File "", line 2, in AssertionError py> for e in edges: ... assert e in edges ... Traceback (most recent call last): File "", line 2, in AssertionError Once you compute an object's hash, it must remain immutable. Is this your problem? -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: Default working directory
vsoler wrote: I'm using Python 2.6.4 on Windows (Vista & 7) I usually start Python by clicking on Start Menu the Python IDLE (Python GUI). However, if I want to save a new source *.py file, the default directory proposed for saving is not the one that I want. What can I do if I want that the change of default directory permanently, that is, if I quit python and I reenter it, the default is still the one I have chosen? Change the working directory in the shortcut? TJG -- http://mail.python.org/mailman/listinfo/python-list
Re: Python (and me) getting confused finding keys
John wrote: another thread can remove the key prior to the has_key call; or perhaps edges isn't a real dictionary? of course. But unless there is a way of using threading without being aware of it, this is not the case. Also, edges is definitely a dict (has been declared some lines before, and no obscure functions have been called in the meantime, just some adding to edges). Python would also fail on edges.keys() if edges wasn't a dict... Well of course you only showed us three lines of code which means that all possibilities are open. And edges.keys () would certainly be valid for something like this (random) class: class Edges (object): def __init__ (self): self._keys = range (10) def keys (self): return [k * k for k in self._keys] def has_key (self, key): return False def __getitem__ (self, key): return self._keys.index (key) edges = Edges () for e in edges.keys (): assert edges.has_key (e) A bizarre example, certainly, but one which illustrates what *might* be happening especially if you only show peephole code. TJG -- http://mail.python.org/mailman/listinfo/python-list
Re: Python (and me) getting confused finding keys
On Tue, Dec 22, 2009 at 8:56 AM, John wrote: > > another thread can remove the key prior to the has_key call; or perhaps > > edges isn't a real dictionary? > > > > of course. But unless there is a way of using threading without being aware > of > it, this is not the case. Also, edges is definitely a dict (has been > declared > some lines before, and no obscure functions have been called in the > meantime, > just some adding to edges). Python would also fail on edges.keys() if edges > wasn't a dict... > > > cheers, > john > -- > http://mail.python.org/mailman/listinfo/python-list > Perhaps e doesn't have a consistent hash value. -- Zachary Burns (407)590-4814 Aim - Zac256FL Production Engineer (Digital Overlord) Zindagi Games -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem with win32ui
En Tue, 22 Dec 2009 12:31:37 -0300, Marc Grondin escribió: Hello everyone, So i have been building an app with python(and learning as i go along) my knowledge of python is still kinda limited but the app work on my pc. I have also compiled it to an exe using py2exe and it also works fine this way on my pc(where python is installed) if however i try to run it from a pc where python is not installed i get this message: Traceback (most recent call last): File "printorders.py", line 2, in File "win32ui.pyc", line 12, in File "win32ui.pyc", line 10, in __load ImportError: DLL load failed: The specified module could not be found. There is a missing DLL. Dependency Walker is a useful tool to solve this kind of problems: http://technet.microsoft.com/en-us/library/cc738370(WS.10).aspx Once you know which DLL is missing, add it to your setup.py -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Default working directory
I'm using Python 2.6.4 on Windows (Vista & 7) I usually start Python by clicking on Start Menu the Python IDLE (Python GUI). However, if I want to save a new source *.py file, the default directory proposed for saving is not the one that I want. What can I do if I want that the change of default directory permanently, that is, if I quit python and I reenter it, the default is still the one I have chosen? Thank you for your help. Vicente Soler -- http://mail.python.org/mailman/listinfo/python-list
Re: Python (and me) getting confused finding keys
> another thread can remove the key prior to the has_key call; or perhaps > edges isn't a real dictionary? > of course. But unless there is a way of using threading without being aware of it, this is not the case. Also, edges is definitely a dict (has been declared some lines before, and no obscure functions have been called in the meantime, just some adding to edges). Python would also fail on edges.keys() if edges wasn't a dict... cheers, john -- http://mail.python.org/mailman/listinfo/python-list
Re: OT Question
On Tue, Dec 22, 2009 at 12:08 PM, Emile van Sebille wrote: > On 12/21/2009 10:27 PM Victor Subervi said... > > Hi; >> Back when I worked with Zope, they had this nifty form element where I >> could select from a list of elements on the right and click an arrow to make >> them go into a list on the left. I need to add this functionality to the >> store I am creating. What is it called? >> > > Custom? There're some good ideas at > http://www.ryancramer.com/journal/entries/select_multiple/ > That's it! Thanks! beno -- http://mail.python.org/mailman/listinfo/python-list
Re: Python (and me) getting confused finding keys
John schrieb: > Hi there, > > I have a rather lengthy program that troubles me for quite some time. After > some debugging, I arrived at the following assertion error: > > for e in edges.keys(): > assert edges.has_key(e) > > Oops!? Is there ANY way that something like this can possibly happen? Yes, it happens when another part of your program -- most likely a thread -- modifies edges while you are iterating over its keys. The keys() method of a dict returns a *copy* of its keys. If you had uses "for e in edges" you'd have seen a RuntimeError "dictionary changed size during iteration". With keys() you see the snapshot of edges's keys when keys() is called. Christian PS: Use "e in edges" instead of "edges.has_key(e)". It's faster. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python (and me) getting confused finding keys
John wrote: Hi there, I have a rather lengthy program that troubles me for quite some time. After some debugging, I arrived at the following assertion error: for e in edges.keys(): assert edges.has_key(e) Oops!? Is there ANY way that something like this can possibly happen? Three ways that I can think of. Doubtless there are more. 1) Mutating the dictionary within the loop: edges = dict.fromkeys (range (10)) for e in edges.keys (): assert edges.has_key (e), "edges does not have %s" % e del edges[e + 1] 2) A race condition (sort of generalisation of (1)): some other thread removes something from edges during the iteration 3) edges isn't a dictionary but a dictalike structure which doesn't do what you expect for .keys and .has_key. TJG -- http://mail.python.org/mailman/listinfo/python-list
Re: Python (and me) getting confused finding keys
On 22/12/2009 16:33, John wrote: Hi there, I have a rather lengthy program that troubles me for quite some time. After some debugging, I arrived at the following assertion error: for e in edges.keys(): assert edges.has_key(e) Oops!? Is there ANY way that something like this can possibly happen? Cheers, John another thread can remove the key prior to the has_key call; or perhaps edges isn't a real dictionary? -- Robin Becker -- http://mail.python.org/mailman/listinfo/python-list
[no subject]
Hi, I need to convert Python decimal.Decimal data to the XMLSchema xs:decimal datatype. This is reasonably straightforward, but there are some corner cases. In particular, xs:decimal does not allow exponential notation like: >>> print Decimal('0.002343000837483727772') 2.343000837483727772E-19 >>> Is there a convenient way to force a decimal.Decimal representation to not use exponential representation? While I've seen this decimal FAQ entry: Q. Some decimal values always print with exponential notation. Is there a way to get a non-exponential representation? A. For some values, exponential notation is the only way to express the number of significant places in the coefficient. For example, expressing 5.0E+3 as 5000 keeps the value constant but cannot show the original’s two-place significance. If an application does not care about tracking significance, it is easy to remove the exponent and trailing zeroes, losing significance, but keeping the value unchanged: >>> def remove_exponent(d): ... return d.quantize(Decimal(1)) if d == d.to_integral() else d.normalize() >>> remove_exponent(Decimal('5E+3')) Decimal('5000') ...this doesn't really apply to my usecase: It does not work for "small" values: remove_exponent(Decimal('0.002343000837483727772')) Decimal('2.343000837483727772E-19') >>> and it can lead to errors if the sheer number size can't be handled: >>> d2 = Decimal('1e80') >>> remove_exponent(d2) Traceback (most recent call last): File "", line 1, in File "", line 2, in remove_exponent File "/apps/prod/gcc/4.2.1/lib/python2.6/decimal.py", line 2308, in quantize 'quantize result has too many digits for current context') File "/apps/prod/gcc/4.2.1/lib/python2.6/decimal.py", line 3680, in _raise_error raise error(explanation) decimal.InvalidOperation: quantize result has too many digits for current context >>> I could easily adapt this recipe: http://code.activestate.com/recipes/358361/ which works on the string and basically removes exponential notation, moves the fraction '.'-dot and adds appropriate zeros. Doesn't seem very lightweight, though. Any obvious alternatives I'm missing? Thanks, Holger -- Preisknaller: GMX DSL Flatrate für nur 16,99 Euro/mtl.! http://portal.gmx.net/de/go/dsl02 -- http://mail.python.org/mailman/listinfo/python-list
Python (and me) getting confused finding keys
Hi there, I have a rather lengthy program that troubles me for quite some time. After some debugging, I arrived at the following assertion error: for e in edges.keys(): assert edges.has_key(e) Oops!? Is there ANY way that something like this can possibly happen? Cheers, John -- http://mail.python.org/mailman/listinfo/python-list
Re: OT Question
On 12/21/2009 10:27 PM Victor Subervi said... Hi; Back when I worked with Zope, they had this nifty form element where I could select from a list of elements on the right and click an arrow to make them go into a list on the left. I need to add this functionality to the store I am creating. What is it called? Custom? There're some good ideas at http://www.ryancramer.com/journal/entries/select_multiple/ Emile -- http://mail.python.org/mailman/listinfo/python-list
Re: Regex help needed!
On Dec 21, 5:38 am, Oltmans wrote: > Hello,. everyone. > > I've a string that looks something like > > lksjdfls kdjff lsdfs sdjfls = "amazon_35343433">sdfsdwelcome > > > From above string I need the digits within the ID attribute. For > example, required output from above string is > - 35343433 > - 345343 > - 8898 > > I've written this regex that's kind of working > re.findall("\w+\s*\W+amazon_(\d+)",str) > The issue with using regexen for parsing HTML is that you often get surprised by attributes that you never expected, or out of order, or with weird or missing quotation marks, or tags or attributes that are in upper/lower case. BeautifulSoup is one tool to use for HTML scraping, here is a pyparsing example, with hopefully descriptive comments: from pyparsing import makeHTMLTags,ParseException src = """ lksjdfls kdjff lsdfs sdjfls sdfsdwelcome hello, my age is 86 years old and I was born in 1945. Do you know that PI is roughly 3.1443534534534534534 """ # use makeHTMLTags to return an expression that will match # HTML tags, including attributes, upper/lower case, # etc. (makeHTMLTags will return expressions for both # opening and closing tags, but we only care about the # opening one, so just use the [0]th returned item div = makeHTMLTags("div")[0] # define a parse action to filter only for tags # with the proper id form def filterByIdStartingWithAmazon(tokens): if not tokens.id.startswith("amazon_"): raise ParseException( "must have id attribute starting with 'amazon_'") # define a parse action that will add a pseudo- # attribute 'amazon_id', to make it easier to get the # numeric portion of the id after the leading 'amazon_' def makeAmazonIdAttribute(tokens): tokens["amazon_id"] = tokens.id[len("amazon_"):] # attach parse action callbacks to the div expression - # these will be called during parse time div.setParseAction(filterByIdStartingWithAmazon, makeAmazonIdAttribute) # search through the input string for matching s, # and print out their amazon_id's for divtag in div.searchString(src): print divtag.amazon_id Prints: 345343 35343433 8898 -- http://mail.python.org/mailman/listinfo/python-list
how to register with pypi - no such setup.py
And the next question in the series - how to make sure the resulting package has a setup.py file? The basic steps are... - build a python package - create a minimal setup.py - (github it, natch) - throw it at pypi with: python setup.py bdist upload - attempt to install it with: sudo pip install my_package and get this: Downloading/unpacking my_package ... IOError: [Errno 2] No such file or directory: '.../setup.py' So the irony is if I had to use setup.py to build the MyPackage..tar.gz, why isn't it inside it? Any tips? (the actual package name is censored because I don't need people finding this if they google for that!;) (and this is a repost because I can't see my admittedly off-topic question on the newsgroup) -- http://mail.python.org/mailman/listinfo/python-list
Problem with win32ui
Hello everyone, So i have been building an app with python(and learning as i go along) my knowledge of python is still kinda limited but the app work on my pc. I have also compiled it to an exe using py2exe and it also works fine this way on my pc(where python is installed) if however i try to run it from a pc where python is not installed i get this message: Traceback (most recent call last): File "printorders.py", line 2, in File "win32ui.pyc", line 12, in File "win32ui.pyc", line 10, in __load ImportError: DLL load failed: The specified module could not be found. i have tried to figure out what is causing this but i come up empty. where should i be looking or what can i do? The file printorders.py first imports win32print with no problems. line 2 is where it imports win32ui. win32ui is used for printing. -- C-ya Later Take Care Marc Grondin -- http://mail.python.org/mailman/listinfo/python-list
Re: How to validate the __init__ parameters
On 12/22/2009 8:52 PM, Bruno Desthuilliers wrote: Steve Holden a écrit : (snip) What's the exact reason for requiring that a creator argument be of a specific type? So operations on the instances don't go wrong? Well, why not just admit that we don't have control over everything, and just *let things go wrong* when the wrong type is passed? validation isn't only about types, but that's not the point... What will then happen? Why, an exception will be raised! Not necessarily. Let me give a more concrete example I've just stumbled upon recently: This is a Tkinter Text widget's insert signature: insert(self, index, chars, *args) Insert CHARS before the characters at INDEX. An additional tag can be given in ARGS. Additional CHARS and tags can follow in ARGS. Let's simplify the signature to focus on one particular case (inserting just one pair of text and tag): insert(self, index, chars, tags) I want to write a wrapper/subclass Tkinter Text widget, then I read something that essentially means: tags must be a tuple-of-strings, cannot be a list-of-strings, and if you pass string "hello" it is interpreted as ('h', 'e', 'l', 'l', 'o') tags. This is a bug-prone signature, I could have written (and indeed just minutes *after* reading the warning, though I caught it before any real harm was done, I wrote): insert(INSERT, chars="some text", tags="the_tag") since I was intending to subclass Text anyway; I decided to throw an "assert isinstance(tags, tuple)" here rather than risking a bug caused by mindlessly using string. The only time the assert would fail is when I'm writing a bug, and there is no reason *I* would want a character-wise tag and write them as string instead of tuple of chars. It could have been more subtle, I might have written insert(INSERT, chars="some text", tags="b") and the code worked flawlessly, until I decided to copy'n'paste it to: insert(INSERT, chars="some text", tags="em") and wondered why I can't find the "em" tag. -- http://mail.python.org/mailman/listinfo/python-list
Re: more efficient?
thank you for your help and support. i`ll keep your advice in mind. :) cheers!!! Zubin On Tue, Dec 22, 2009 at 8:07 PM, Lie Ryan wrote: > On 12/22/2009 5:13 PM, Zubin Mithra wrote: >> >> I have the following two implementation techniques in mind. >> >> def myfunc(mystring): >> check = "hello, there " + mystring + "!!!" >> print check >> >> >> OR >> structure = ["hello, there",,"!!!"] >> def myfunc(mystring): >> structure[2] = mystring >> output = ''.join(mystring) >> >> i heard that string concatenation is very slow in python; so should i >> go for the second approach? could someone tell me why? Would there be >> another 'best-practice-style'? >> Please help. Thankx in advance! > > Python's strings are immutable and to concatenate two string the interpreter > need to copy two whole string into a new string object. This isn't a > performance problem until you're trying to concatenate a list containing a > thousand strings: > ['abc', 'bcd', 'cde', 'def', ...] > with the naive approach: > joined = '' > for s in lst: > joined = joined + s > > first python will conc. '' and 'abc', copying 0+3 = 3 chars > then it conc. 'abc' and 'bcd', copying 3+3 = 6 chars > then it conc. 'abcbcd' and 'cde' copying 6+3 = 9 chars > then it conc. 'abcbcdcde' and 'def' copying 9+3 = 12 chars > and so on... > > for four 3-letter strings, python copies 3 + 6 + 9 + 12 = 30 chars, instead > of the minimum necessary 12 chars. It gets worse as the number of strings > increases. > > When you concatenate two 1000-chars large strings, both + and ''.join will > have to copy 2000 chars. But when you join one thousand 2-chars string > you'll need to copy 1001000 chars[!] with +. > > Now, early optimization *is evil*. Don't start throwing ''.join every here > and there. The performance by the concatenations won't start to matter until > you're concatenating a large lists (>40) and + is much more readable than > ''.join(). > > When concatenating small number of strings I preferred > %-interpolation/str.format; it's often much more readable than ''.join and > +. > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: more efficient?
On 12/22/2009 5:13 PM, Zubin Mithra wrote: I have the following two implementation techniques in mind. def myfunc(mystring): check = "hello, there " + mystring + "!!!" print check OR structure = ["hello, there",,"!!!"] def myfunc(mystring): structure[2] = mystring output = ''.join(mystring) i heard that string concatenation is very slow in python; so should i go for the second approach? could someone tell me why? Would there be another 'best-practice-style'? Please help. Thankx in advance! Python's strings are immutable and to concatenate two string the interpreter need to copy two whole string into a new string object. This isn't a performance problem until you're trying to concatenate a list containing a thousand strings: ['abc', 'bcd', 'cde', 'def', ...] with the naive approach: joined = '' for s in lst: joined = joined + s first python will conc. '' and 'abc', copying 0+3 = 3 chars then it conc. 'abc' and 'bcd', copying 3+3 = 6 chars then it conc. 'abcbcd' and 'cde' copying 6+3 = 9 chars then it conc. 'abcbcdcde' and 'def' copying 9+3 = 12 chars and so on... for four 3-letter strings, python copies 3 + 6 + 9 + 12 = 30 chars, instead of the minimum necessary 12 chars. It gets worse as the number of strings increases. When you concatenate two 1000-chars large strings, both + and ''.join will have to copy 2000 chars. But when you join one thousand 2-chars string you'll need to copy 1001000 chars[!] with +. Now, early optimization *is evil*. Don't start throwing ''.join every here and there. The performance by the concatenations won't start to matter until you're concatenating a large lists (>40) and + is much more readable than ''.join(). When concatenating small number of strings I preferred %-interpolation/str.format; it's often much more readable than ''.join and +. -- http://mail.python.org/mailman/listinfo/python-list
Can I configure Markdown to not wrap my text in elements?
I want to use Markdown to process some text before displaying it in a list. However, Markdown, by default, wraps the resulting text in elements, which screws up my list and displays the list item symbol and text on different lines. Can I stop Markdown from wrapping text in paragraphs elements? Sorry if this is not the right group. Thanks -- http://mail.python.org/mailman/listinfo/python-list
Re: Regex help needed!
how about re.findall(r'\w+.=\W\D+(\d+)?',str) ? this will work for any string within id ! ~Ukanth On Dec 21, 6:06 pm, Oltmans wrote: > On Dec 21, 5:05 pm, Umakanth wrote: > > > How about re.findall(r'\d+(?:\.\d+)?',str) > > > extracts only numbers from any string > > Thank you. However, I only need the digits within the ID attribute of > the DIV. Regex that you suggested fails on the following string > > > lksjdfls kdjff lsdfs sdjfls = "amazon_35343433">sdfsdwelcome > hello, my age is 86 years old and I was born in 1945. Do you know that > PI is roughly 3.1443534534534534534 > > > > ~uk > > > On Dec 21, 4:38 pm, Oltmans wrote: > > > > Hello,. everyone. > > > > I've a string that looks something like > > > > > > lksjdfls kdjff lsdfs sdjfls > > = "amazon_35343433">sdfsdwelcome > > > > > > > From above string I need the digits within the ID attribute. For > > > example, required output from above string is > > > - 35343433 > > > - 345343 > > > - 8898 > > > > I've written this regex that's kind of working > > > re.findall("\w+\s*\W+amazon_(\d+)",str) > > > > but I was just wondering that there might be a better RegEx to do that > > > same thing. Can you kindly suggest a better/improved Regex. Thank you > > > in advance. > > -- http://mail.python.org/mailman/listinfo/python-list
Clustering technique
Dear all, excuse me if i post a simple question.. I am trying to find a software/algorythm that can "cluster" simple data on an excel sheet Example: Variable a Variable b Variable c Case 11 0 0 Case 20 1 1 Case 31 0 0 Case 41 1 0 Case 50 1 1 The systems recognizes that there are 3 possible clusters: the first with cases that has Variable a as true, the second has Variables b and c the third is "all the rest" Variabile aVariabile b Variabile c Case 1 1 00 Case 3 1 00 Case 2 0 11 Case 5 0 11 Case 4 1 10 Thank you in advance -- http://mail.python.org/mailman/listinfo/python-list
Re: which pi formula is given in the decimal module documentation?
In article <00b967e1$0$15623$c3e8...@news.astraweb.com>, Steven D'Aprano wrote: >Nice work! But I have a question... > >On Mon, 21 Dec 2009 20:40:40 +, Albert van der Horst wrote: > >> def pi4(): >> ' Calculate pi by a 5th order process, with favorable stop >> criterion' >> precision = 10e-20 > > >Why do you say 10e-20 instead of 1e-19? No thought went into that. Note that the error jumps from 1e-5 to 1e-25 between iterations, so 1e-20 or 1e-19 hardly makes a difference. >-- >Steven Groetjes Albert -- -- Albert van der Horst, UTRECHT,THE NETHERLANDS Economic growth -- being exponential -- ultimately falters. alb...@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst -- http://mail.python.org/mailman/listinfo/python-list
Re: more efficient?
Zubin Mithra wrote: I have the following two implementation techniques in mind. def myfunc(mystring): check = "hello, there " + mystring + "!!!" print check OR structure = ["hello, there",,"!!!"] def myfunc(mystring): structure[2] = mystring output = ''.join(mystring) i heard that string concatenation is very slow in python; so should i go for the second approach? could someone tell me why? Would there be another 'best-practice-style'? Please help. Thankx in advance! cheers!!! Zubin The best practice is to care more about your coding style (i.e. how you make your code easy to read & understand) than pointless speed optimization. If you *really* care about speed, maybe you shoud use something else than python. def myfunc(mystring): check = "hello, there %s !!!" % mystring print check JM -- http://mail.python.org/mailman/listinfo/python-list
Re: How to validate the __init__ parameters
Bruno Desthuilliers wrote: > Steve Holden a écrit : > (snip) >> What's the exact reason for requiring that a creator argument be of a >> specific type? So operations on the instances don't go wrong? Well, why >> not just admit that we don't have control over everything, and just *let >> things go wrong* when the wrong type is passed? > > validation isn't only about types, but that's not the point... > >> What will then happen? Why, an exception will be raised! > > Not necessarily. Yes, that's what I'm more concerned about. As Steve says, the easy ones will cause an exception somewhere anyway but the really tricky bugs might not trouble the interpreter at all and go on to cause errors and corruption elsewhere that may not even be noticed immediately. I'm sad there's no particularly elegant alternative (an 'ensure' keyword maybe ;) as I do like how they read. I think I'll carry on using them liberally anyway, armed with the knowledge I can't rely on them in any code I distribute, and deliberately raising errors when execution NEEDS to be stopped. Thanks for the insight all. Roger. -- http://mail.python.org/mailman/listinfo/python-list
Re: Line indexing in Python
r0g wrote: > seafoid wrote: >> Hi Guys, >> >> When python reads in a file, can lines be referred to via an index? >> >> Example: >> >> for line in file: >> if line[0] == '0': >> a.write(line) >> >> This works, however, I am unsure if line[0] refers only to the first line or >> the first character in all lines. >> >> Is there an easy way to refer to a line with the first character being a >> single letter that you know? >> >> Thanks in advance, >> Seafoid. > > > If you want to know the index number of an item in a sequence you are > looping through (whether it be a file of lines or a list of characters, > whatever) use enumerate... > for index, value in enumerate("ABCD"): > print index, value > ... > 0 A > 1 B > 2 C > 3 D > > > If you want to extract an index number from the first part of of a given > line use split( split_character, maximum_splits_to_do ) and then angle > brackets to reference the first part (index 0)... > > a = "20 GOTO 10" int( a.split(' ',1)[0] ) > 20 > those are brackets, not angle brackets regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/ Holden Web LLC http://www.holdenweb.com/ UPCOMING EVENTS:http://holdenweb.eventbrite.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: RotatingFileHandler key error when parsing a logging config file
jordilin wrote: Hi, I've a config for logging where I set up a file rotation with handlers.RotatingFileHandler and when the app parses the logging config it says keyError when trying to parse that section ('RotatingFileHandler' is not defined). Curiously enough, I can do import logging and from logging.handlers import RotatingFileHandler. Example: [handlers] keys=handlers.RotatingFileHandler [formatters] keys=simpleFormatter [logger_root] level=DEBUG handlers=handlers.RotatingFileHandler [handler_handlers.RotatingFileHandler] class=handlers.RotatingFileHandler level=DEBUG formatter=simpleFormatter I'm using python 2.4 in the servers. I'm having this in a particular one, which seems like there must be some kind of configuration error. Any suggestions, Thanks I'm not using config files, but by looking at the documentation, I would suggest to use another name for your handler. [handler_foo] class=handlers.RotatingFileHandler level=DEBUG formatter=simpleFormatter just name it foo for test purpose, dotted names may not be supported. Also I would not not the exact same name as logging.handlers.RotatingFileHandler, your risk some name collision wetween the structure you have created and the logging module class. JM -- http://mail.python.org/mailman/listinfo/python-list
Re: Clustering technique
On Dec 22, 11:12 am, Luca wrote: > Dear all, excuse me if i post a simple question.. I am trying to find > a software/algorythm that can "cluster" simple data on an excel sheet > > Example: > Variable a Variable b Variable c > Case 1 1 0 0 > Case 2 0 1 1 > Case 3 1 0 0 > Case 4 1 1 0 > Case 5 0 1 1 > > The systems recognizes that there are 3 possible clusters: > > the first with cases that has Variable a as true, > the second has Variables b and c > the third is "all the rest" > > Variabile a Variabile b Variabile c > > Case 1 1 0 0 > Case 3 1 0 0 > > Case 2 0 1 1 > Case 5 0 1 1 > > Case 4 1 1 0 > > Thank you in advance If you haven't already, download and install xlrd from http://www.python-excel.org for a library than can read excel workbooks (but not 2007 yet). Or, export as CSV... Then using either the csv module/xlrd (both well documented) or any other way of reading the data, you effectively want to end up with something like this: rows = [ #A #B #C #D ['Case 1', 1, 0 ,0], ['Case 2', 0, 1, 1], ['Case 3', 1, 0, 0], ['Case 4', 1, 1, 0], ['Case 5', 0, 1, 1] ] One approach is to sort 'rows' by B,C & D. This will bring the identical elements adjacent to each other in the list. Then you need an iterator to group them... take a look at itertools.groupby. Another is to use a defaultdict(list) found in collections. And just loop over the rows, again with B, C & D as a key, and A being appended to the list. hth Jon. -- http://mail.python.org/mailman/listinfo/python-list
Re: For...in statement and generators
"Gabriel Genellina" wrote: > En Mon, 21 Dec 2009 11:39:46 -0300, Lucas Prado Melo > escribió: > >> Is there a way to send() information back to a generator while using the >> for...in statement? > > No. You have to write the iteration as a while loop. > You *can* use send() to a generator while using a for loop, but it's probably more effort than it's worth. The caveats include: You have to construct the generator outside the loop so you can refer to it from inside the loop. The generator has to distinguish between the for loop iteration and additional values sent in. When using send() that call mustn't terminate the iteration (or if it does you have to be prepared to catch the StopIteration). Here's a silly example which counts up to some limit and each time yields double the current count but you can reset the counter using send(). >>> def doubler(n, limit): while n < limit: m = yield 2*n if m is not None: n = m yield "ok" else: n += 1 >>> d = doubler(1, 20) >>> for n in d: print n print "sent", n+3, d.send(n+3) 2 sent 5 ok 10 sent 13 ok 26 sent 29 ok >>> >>> for n in doubler(1, 20): print n 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 >>> -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: How to validate the __init__ parameters
Steve Holden a écrit : (snip) What's the exact reason for requiring that a creator argument be of a specific type? So operations on the instances don't go wrong? Well, why not just admit that we don't have control over everything, and just *let things go wrong* when the wrong type is passed? validation isn't only about types, but that's not the point... What will then happen? Why, an exception will be raised! Not necessarily. def nocheck(stuffs): "'stuffs' is supposed to be a sequence of strings" for s in stuffs: do_something_with(s) # correct call good_stuffs = ("role1", "role2", "role3") nocheck(good_stuffs) # incorrect call, but the error passes silently bad_stuffs = "role1" nocheck(bad_stuffs) If nocheck happens to be in the core of a huge lib or framework and stuffs defined somwhere in a huge client app's code you didn't even wrote, then you might spend hours tracing the bug - been here, done that, bought the tshirt :( IOW : while I agree that not doing anything and letting exceptions propagate is quite often the best thing to do, there are times where validation makes sense - specially when passing in a wrong value might NOT raise an exception - just produce unexpected results. My 2 cents... -- http://mail.python.org/mailman/listinfo/python-list
Re: How to validate the __init__ parameters
Denis Doria a écrit : Hi; I'm checking the best way to validate attributes inside a class. Of course I can use property to check it, but I really want to do it inside the __init__: If you use a property, you'll have the validation in the initializer AND everywhere else too. If you care about validating the attribute in the initializer then you probably want to make sure the attribute is _always_ valid, don't you ??? -- http://mail.python.org/mailman/listinfo/python-list
Re: more efficient?
On 12/22/09 7:13 AM, Zubin Mithra wrote: I have the following two implementation techniques in mind. def myfunc(mystring): check = "hello, there " + mystring + "!!!" print check OR structure = ["hello, there",,"!!!"] def myfunc(mystring): structure[2] = mystring output = ''.join(mystring) i heard that string concatenation is very slow in python; so should i go for the second approach? could someone tell me why? Would there be another 'best-practice-style'? Please help. Thankx in advance! cheers!!! Zubin 1) "premature optimization is the root of all evil." 2) if you're concerned about the speed difference of just a single string concat, you shouldn't be using Python. 3) if you're still concerned, use timeit. 4) string concat only gets slow if the number of strings get large. Personally I'm only using the join-style-concat if I know the number of strings is rather large, or unknown. Otherwise I stick with just the regular string concatenation or string formattting (with % or format). Much more readable. -irmen -- http://mail.python.org/mailman/listinfo/python-list
Re: Class variables static by default?
>> I don't think Steven cares much, he loves this type of nitpicking and >> uber pedantic formulations, but only if he can apply it to other >> people's post :) > > Heh heh :) > > I actually do care, because (not having a Java/C++ background) I actually > do get a mental "double-take" every time I read about "class variables". > It takes a real effort of will to remind myself that they're probably not > talking about something like this: > > for theclass in (int, float, Decimal, Fraction): > do_something_with(theclass) > Right. I figured you don't have much of a background in other OO languages but actually some background helps with python too. For one it helps communicating with other people on this list who do come from Java/C++/etc and also because python is heavily influenced by Java/C++/etc. Also, programming is not about a specific language but about programming :) The concepts are general, maybe it's spelled differently in python and java and C++ etc etc but the programmers in all these languages do talk about the same stuff. >> I found that his posts are generally useful and helpful, one just has to >> cut all the nitpicking, ... > > Technically you don't have to cut *all* the nitpicking, cutting 87.3% of > it is sufficient. Touche! :) Cheers, Daniel -- Psss, psss, put it down! - http://www.cafepress.com/putitdown -- http://mail.python.org/mailman/listinfo/python-list
Re: converting string to a date format
tekion wrote: > All, > I know there is a datetime module for converting and manipulate date > format. I have this string date format: 24/Nov/2009:10:39:03 -0500 > and would like to convert it to a date format of "2009-11-24 > 10:39:03". At the moment I am reading datetime module trying to find > out if I could do it with datetime module. Does any one know of a way > besides slashing my way through it using string split function? Thanks. Try mxDateTime's string parser: http://www.egenix.com/products/python/mxBase/mxDateTime/ -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, Dec 22 2009) >>> Python/Zope Consulting and Support ...http://www.egenix.com/ >>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ >>> mxODBC, mxDateTime, mxTextTools ...http://python.egenix.com/ ::: Try our new mxODBC.Connect Python Database Interface for free ! eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 http://www.egenix.com/company/contact/ -- http://mail.python.org/mailman/listinfo/python-list