Webinar on How to Manage Your Python Open Source
Hey everyone, We are conducting a webinar this Wednesday about How to Manage Your Python Open Source. the session will be mainly about challenge of managing open-source components that are embedded in your Python projects. If you are interested please register in this form: https://attendee.gotowebinar.com/register/7034105107342895362 we hope to see you there :) Rafi, WhiteSource software. -- https://mail.python.org/mailman/listinfo/python-list
Re: aproximate a number
billiejoex wrote: > Hi all. I'd need to aproximate a given float number into the next (int) > bigger one. Because of my bad english I try to explain it with some example: > > 5.7 --> 6 > 52.987 --> 53 > 3.34 --> 4 > 2.1 --> 3 > > Regards > > math.ceil returns what you need but as a float, then create an int >>> import math >>> math.ceil (12.3) 13.0 >>> int (math.ceil (12.3)) 13 hth -- rafi "Imagination is more important than knowledge." (Albert Einstein) -- http://mail.python.org/mailman/listinfo/python-list
Re: variable hell
Ron Garret wrote: >>Because eval() takes an expression as an argument, and assignment is a >>statement. > > And if you find this distinction annoying, try Lisp. that's were I come from :-) -- rafi "Imagination is more important than knowledge." (Albert Einstein) -- http://mail.python.org/mailman/listinfo/python-list
Re: a dummy python question
Learning Python wrote: >>>def outer(x): > > def inner(i): > print i, > if i: inner(i-1) > inner(x) > >>>outer(3) > > Here supposely, it should report error, because the function inner > cannot see itself since inner is only in local namespace of outer. There is no error. the function inner is defined recursively: It calls itself with a different value than the one it has been called with. When defining a recursive function, there are case when it calls itself and other when it does not (otherwise the recursion is infinite and the program crashes after all the memory is used). Here it does not call itself when the value given as parameter is 0 (the if fails). one can always see itself (even at definition time) > but I typed in this in python interface. It works! > it print out: > 3 2 1 0 > > If you turn this into a module file and run this > it print out > 3 2 1 0 none I suppose you wrote this down (instead of cut and paste) as the none is not capitalized. There must be something else in your module that writes the none as your code presented above should really not "as is". -- rafi "Imagination is more important than knowledge." (Albert Einstein) -- http://mail.python.org/mailman/listinfo/python-list
Re: variable hell
Steve Holden wrote: > Because eval() takes an expression as an argument, and assignment is a > statement. I am definitely not a language lawyer... but I should a little bit more thanks, -- rafi "Imagination is more important than knowledge." (Albert Einstein) -- http://mail.python.org/mailman/listinfo/python-list
Re: variable hell
Reinhold Birkenfeld wrote: >>> exec(eval("'a%s=%s' % (count, value)")) >> >>why using the eval? >> >>exec ('a%s=%s' % (count, value)) >> >>should be fine > > And this demonstrates why exec as a statement was a mistake ;) > > It actually is > > exec 'a%s=%s' % (count, value) Noted. In the meantime another question I cannot find an answer to: any idea why does eval() consider '=' as a syntax error? >>> eval ('a=1') Traceback (most recent call last): File "", line 1, in ? File "", line 1 a=1 ^ SyntaxError: invalid syntax Thanks -- rafi "Imagination is more important than knowledge." (Albert Einstein) -- http://mail.python.org/mailman/listinfo/python-list
Re: Speed quirk: redundant line gives six-fold speedup
Stelios Xanthakis wrote: > Mark Dickinson wrote: > >> I have a simple 192-line Python script that begins with the line: >> >> dummy0 = 47 >> >> The script runs in less than 2.5 seconds. The variable dummy0 is never >> referenced again, directly or indirectly, by the rest of the script. >> >> Here's the surprise: if I remove or comment out this first line, the >> script takes more than 15 seconds to run. So it appears that adding a >> redundant line produces a spectacular six-fold increase in speed! >> >> (Actually, I had to add 29 dummy lines at the beginning of the code to >> get the speed increase; if any one of these lines is removed the >> running time reverts to around 15 seconds again.) >> >> Questions: >> >> (1) Can anyone else reproduce this behaviour, or is it just some quirk >> of my setup? >> (2) Any possible explanations? Is there some optimization that kicks >> in at a certain number of lines, or at a certain length of >> bytecode? >> (3) If (2), is there some way to force the optimization, so that I can >> get the speed increase without having to add the extra lines? >> > > Hi. > > I haven't been able to reproduce this but I had a similar case > before (a program that some times crashed and some times worked > perfectly and the difference was whitespace in the comments!!!). > > After lots of wondering and thinking that I must be dreaming > (luckily I had pyvm which also crashed but for different amounts > of whitespace), it was solved. The explanation is this: hash > and comparison of objects depends on the state of the memory > allocator. A sample case is this: > > class A: pass > dummy0=47 # comment this to get a different result for min > a=A() > b=A() > print min (a, b) > > the result of 'min' is not only non-deterministic but also depends > on whether other things have been allocated before. The same > thing can happen for 'dictionary.keys()' if the keys are objects > and 'iterate-over-set' when the set contains objects. I do not get the point here: isn't min comparing the adress in memory as there is nothing else to compare? [python 2.4.1 on ubuntu linux] On 10 runs from within emacs I had about 50% for `a' and 50% for `b' returned by min (a,b). It was the same without the dummy0=47. On 10 runs from the command line I had always `a' returned (with and without the dummy). The only difference was the address of b's object. (For all the run of each case the two addresses were exactly the same) For your first post: On 10 tests run for each case (average time, but each time was never different one from the other more that .02s) 0.554 when the 28 dummies are in place 6.679 when commented out, removed or put in a single multiline string 2.576 when putting the 28 dummies in a list or a dict using a for loop 7.195 when creating the 28 dummies using an exec in a for loop What about memory allocation that would be performed chunk by chunk by the interpreter? Then being at the end or at the beginning of a chunk may not be the same for processing? All the instructions of the program would then be in the cpu cache for example in the same block while in the other case theyr would be in two distinct block -> thus less caching for the cpu... [this particular problem arose when I was working several year ago on a persistent object manager for a distributed operating system] -- rafi "Imagination is more important than knowledge." (Albert Einstein) -- http://mail.python.org/mailman/listinfo/python-list
Re: variable hell
Adriaan Renting wrote: > You might be able to do something along the lines of > > for count in range(0,maxcount): > value = values[count] > exec(eval("'a%s=%s' % (count, value)")) why using the eval? exec ('a%s=%s' % (count, value)) should be fine -- rafi "Imagination is more important than knowledge." (Albert Einstein) -- http://mail.python.org/mailman/listinfo/python-list
Re: RE Despair - help required
Yoav wrote: > Don't think it will do much good. I need to get them from a file and > extract the last folder in the path. For example: > if I get "c:\dos\util" > I want to extract the string "\util" like frederik says (I use '/' as I am using Unix): >>> import os >>> os.path.split ('c:/foo/bar') ('c:/foo', 'bar') >>> os.path.splitext ('c:/foo/bar') ('c:/foo/bar', '') >>> os.path.splitext ('c:/foo/bar.txt') ('c:/foo/bar', '.txt') or if you are really reluctant: >>> 'c:\\foo\\bar'.split ('\\') ['c:', 'foo', 'bar'] >>> 'c:\\foo\\bar'.split ('\\') [-1] 'bar' > Fredrik Lundh wrote: >> instead of struggling with weird REs, why not use Python's standard >> filename manipulation library instead? >> >> http://docs.python.org/lib/module-os.path.html >> >> >> >> -- rafi "Imagination is more important than knowledge." (Albert Einstein) -- http://mail.python.org/mailman/listinfo/python-list
Re: Default function arguments behaving badly
[EMAIL PROTECTED] wrote: > Hi hi > I'm having some trouble with a function I've written in Python: > > def myFunction(l1,l2,result=[]): > index=0 > for i in l1: > result.append([]) > if type(i)==list: > myFunction(i,l2,result[index]) > else: > for j in l2: > result[index].append(i*j) > index+=1 > return result > The problem is that it works if I run it once, but then on repeated > runs the value for 'result' doesn't seem to set itself to my default of > [], but instead uses the state it was in last time the function was > run. > Does anyone know what is going on here? Is there an easy solution? the list you provide as default parameter is evaluated once (at loading time of the function). so each time you call the function, it uses the same list that has been filled before... you do not have the problem with say and int or a string as they are non mutable objects. however lists are mutable objects so... modify your function as follow: def myFunction(l1,l2,result=None): if result is None: result = [] hth -- rafi "Imagination is more important than knowledge." (Albert Einstein) -- http://mail.python.org/mailman/listinfo/python-list
Re: Email client in Pyhton
Mark Lutz in Programming Python, 2nd ed from O'Reilly covers the subject in chapter 11 using only the standards modules for mails and Tkinter. hth [EMAIL PROTECTED] wrote: > Hi grp, > I new to this grp and python too. > i have started writing few python scripts myself. > > now i am planning to write a bear minimum email client in > pyhton. i found the smtp module of python could serve my > pupose. I can send message using mails using the smtp lib. > Now i'm looking for some modules which can help me in > fetching the mails from the mailserver and managing folders. > > I also look for some existing mail client, written in python > which wud serve my cause. > > i searched google for no avail. > -- rafi "Imagination is more important than knowledge." (Albert Einstein) -- http://mail.python.org/mailman/listinfo/python-list
Re: Reading just a few lines from a text file
[EMAIL PROTECTED] wrote: > I have a text file with many hundreds of lines of data. The data of > interest to me, however, resides at the bottom of the file, in the last > 20 lines. Right now, I read the entire file and discard the stuff I > don't need. I'd like to speed up my program by reading only the last 20 > lines. How do I do this? > > Thomas Philips > If you are using a Unix: tail -20 file.txt -- rafi "Imagination is more important than knowledge." (Albert Einstein) -- http://mail.python.org/mailman/listinfo/python-list
Re: how to deal with space between numbers
bruno modulix wrote: > Mohammed Altaj wrote: > >>Dear All >> >>This is my problem again , > > (snip) > Read my answers to your two previous posts. > (in short : use str.split() - but *do* read my answers if you want to > save you a lot of pain) especially when several persons replied to you... -- rafi "Imagination is more important than knowledge." (Albert Einstein) -- http://mail.python.org/mailman/listinfo/python-list
Re: while c = f.read(1)
Antoon Pardon wrote: > In that case you wouldn't return an empty sequence if you wanted > a false value in a sequence context but would throw an exception. > So this would be fine by me, I just don't understand how this > would support the use of empty sequences as false. > > I also don't see how a read from a network connection returning > either: > > a bytestringwhen data is available, > '' when no data is available > Nonewhen the connection was closed I do not get why returning '' when no data is available? If no data is available then nothing is returned, the function hangs. (Which is the cas for receive) -- rafi "Imagination is more important than knowledge." (Albert Einstein) -- http://mail.python.org/mailman/listinfo/python-list
Re: loop in python
km wrote: >>that this obsession reveals a certain inexperience. > > its neither obsession nor inexperience ... its just the requirement. > i think less buggy code is not the main concern for all. Argh (choking) Then you are definitely a dangerous person! If your program is fast to give a false answer, it is like driving a truck with the eyes closed in down town ("I can go fast, I am not scared, I do not see the dangers"). Please tell us for you are working in order for us to avoid the software / services you are providing. -- rafi "Imagination is more important than knowledge." (Albert Einstein) -- http://mail.python.org/mailman/listinfo/python-list
Re: loop in python
km wrote: > Also the first thing any newbie to python asks me is abt "raw speed > in comparison with similar languages like perl" when i advocate > python to perl. Always the same chorus... Just tell the newbies that speed is not on their top list. If most of the world's computer programs where running two times slower but with two times less bugs, imagine how many hours we would benefit... -- rafi "Imagination is more important than knowledge." (Albert Einstein) -- http://mail.python.org/mailman/listinfo/python-list
Re: split function
Mohammed Altaj wrote: > Dear All > > What i want to do is , my input is like > 0 2 > 0 3 > 0 4 > 1 2 > 1 4 > 2 3 > 3 4 > > I am comparing and put the number in group , like ,the first three lines > , all has zero as first input for each line, so the out put should look > like > 0 2 3 4 > and so on > 1 2 4 > 2 3 > 3 4 > > I managed to do what i need , but i did in this case , there is no space > between numbers , like > 02 > 03 > 04 > 12 > 14 > 23 > 34 > > so , how can i do this with spaces between numbers with a two pass processing (one for building the output, one for its printing), using a dict to make lists for each index (everything is treated as strings as you do not compare nor compute them) results = dict () input = file ('input.dat') for line in input: idx, value = line.split () if idx in results: results [idx] .append (value) else: results [idx] = [value] input.close () output = open ('output.dat', 'w') for idx, values in results.items (): output.write ('%s %s\n' % (idx, ' '.join (values))) output.close () you can define two function, in case of your output may vary in the future. > [snip your code] 8 levels of indentation seems really to much for a good ppiece of code from my point of view. my 2 cents -- rafi "Imagination is more important than knowledge." (Albert Einstein) -- http://mail.python.org/mailman/listinfo/python-list
Re: tkinter text widget question
William Gill wrote: > The tkinter text widget uses indexes to identify row:column offsets > within the text, but it seems counter intuitive to have to convert row > and column integers to a string like "0.1'. It's great that index can > take a string, but what about looping through rows and columns? Am I > missing a way to use integers directly, or should I create a class that > takes the two integers and returns them formatted as the proper string? tkinter relies on tk that is for tcl at first, and in tcl every thing is a string (more or less). "%s.%s" % (row, column) should answer your problem easily my 2 cents -- rafi "Imagination is more important than knowledge." (Albert Einstein) -- http://mail.python.org/mailman/listinfo/python-list
Re: Library vs Framework (was Dr. Dobb's Python-URL!)
Simon Brunning wrote: > On 8/15/05, Rocco Moretti <[EMAIL PROTECTED]> wrote: > >>Which lead me to the question - what's the difference between a library >>and a framework? > > If you call its code, it's a library. If it calls yours, it's a framework. Trying to add my 2 cents (As I do not agree with the above sentence): You build applications "on top of / using" libraries while you build applications "in the context of" a framework. A framework is more like a piece of swiss cheese were you fill the holes. The framework provides an overall structure with particular rules for its use, while the library provide basic blocks that you may assemble in your own way (even if some constaints exist). -- rafi "Imagination is more important than knowledge." (Albert Einstein) -- http://mail.python.org/mailman/listinfo/python-list
Re: get a list of mounted filesystems under MacOSX
[EMAIL PROTECTED] wrote: > Hi all, > I am trying to port my (linux) program to MacOSX, and I need to get a > list of mounted filesystems. Under linux, it was easy, I was parsing > /etc/mtab (or /proc/mounts), this works also on some other unices. > But I have no idea how to do it on MacOSX, apart from calling "mount" as > an external program and parsing the output - but I prefer "cleaner" > solutions. Is there one? why not looking at the content of the /Volumes folder? my 2 cents -- rafi "Imagination is more important than knowledge." (Albert Einstein) -- http://mail.python.org/mailman/listinfo/python-list
Re: Art of Unit Testing
rafi wrote: > In order to unit test a function / method one should use mock objects, > otherwise you may not be sure that your code is faulty. 'should' may be too strong, 'may' may be better. In the meantime I found: http://python-mock.sourceforge.net/ my 2 cents -- rafi "Imagination is more important than knowledge." (Albert Einstein) -- http://mail.python.org/mailman/listinfo/python-list
Re: Art of Unit Testing
Christoph Zwerschke wrote: > Björn Lindström wrote: > > I already gave the example of creating database connections > or even creating/importing whole databases. My question was, how do I > handle these cases with the standard lib unittest? I do not get why a unit test whould create/import a whole database. In order to unit test a function / method one should use mock objects, otherwise you may not be sure that your code is faulty. A mock object is an object that let the object uder test think it is connected to a database, but it is not really connected to one. This object is programmed to return well know values (correct, incorrect and exceptional ones). What you are talking about is more related to interaction / load tests and not unit tests (at least to me who am not an expert of unit testing). > According to the "extreme programming" paradigm, testing should be done > several times a day. So a requirement for extreme programm is that tests > are fast enough. If the testing needs too much time, people are > discouraged to test often. Well as I said above, a unit test is dedicated to a single function or method. So wehn you update a function / method, you should test the whole class or module, but maybe not the whole application (or the development task may have not been properly organized --splited between several developers). As someones may not write several thousand lines a day, unit tests should not be that long to run (well from my point of view, which is again not the one of an expert of unit testing). I do agree with Benjamin Niermann when he says: "Or because it is already close to perfection (at least in what it is intended to do)." unittest is for unittesting :-) my 2 cents -- rafi "Imagination is more important than knowledge." (Albert Einstein) -- http://mail.python.org/mailman/listinfo/python-list
Re: inheriting from datetime
Rob Conner wrote: > So this is simple, why can't I run the following code? I've tried many > variances of this, but simply cannot inherit from datetime or > datetime.datetime. I get this on line 3. > TypeError: function takes at most 2 arguments (3 given) line 3 of the following code or line 3 of the use of the code? on my box it runs, except that ValueError is not caught by the except. side question: what is the point of accepting invalid dates? -- rafi "Imagination is more important than knowledge." (Albert Einstein) -- http://mail.python.org/mailman/listinfo/python-list
Re: multiple inheritance super()
Michele Simionato wrote: >>http://fuhm.org/super-harmful/ > > > That is a pretty good page; I must say that my position is more radical > (i.e. it is not super which > is harmful, it is multiple inheritance itself that it is harmful: was I > going to design a new language > I would implement it *without* multiple inheritance). > >Michele Simionato > I do not agree on that point: Nobody is forced to used multiple inheritance, and when it is the right solution to be used, then it would be a shame not to have it. I remember the pain it was to implement some CORBA stuff using Java (moreover compared to C++) because of the lack of multiple inheritance. -- rafi "Imagination is more important than knowledge." (Albert Einstein) -- http://mail.python.org/mailman/listinfo/python-list
Re: multiple inheritance super()
Michele Simionato wrote: >>I am mostly >>using old style (without type unification) init but this motivate the >>shift for the new style. Is there somewhere a document about this? > > Yes, see http://www.python.org/2.3/mro.html by yours truly > >Michele Simionato Thanks a lot -- rafi "Imagination is more important than knowledge." (Albert Einstein) -- http://mail.python.org/mailman/listinfo/python-list
Re: multiple inheritance super()
Scott David Daniels wrote: >> I do understand the lookup for foo: foo is provided by both classes A >> and B and I do not state which one I want to use, so it takes the >> first one in the list of inherited classes (order of the declaration). >> However >> I cannot find an explanation (I may have googled the wrong keywords) >> for the order of the __init__ calls from C. I was expecting (following >> the same order as the method lookup): > > > This should make it clear: > class A (object): > def __init__ (self): > print '', > super (A, self) .__init__ () > print '' > class B (object): > def __init__ (self): > print '', > super (B, self) .__init__ () > print '' > class C (A, B): > def __init__ (self): > print '', > super (C, self) .__init__ () > print '' > > C() > Gosh, based on your code I added an attribute foo on both A and B, and I now understand... The super goes through all the super classes init to find the attributes that may have name conflict and keep the value of the attribute baesd upon the order of the class declaration in the definition of C (here the value of foo in A). Am I right? I am mostly using old style (without type unification) init but this motivate the shift for the new style. Is there somewhere a document about this? Thanks a lot Scott -- rafi "Imagination is more important than knowledge." (Albert Einstein) -- http://mail.python.org/mailman/listinfo/python-list
Re: multiple inheritance super()
Peter Hansen wrote: > km wrote: > >> Hi all, >> >> In the following code why am i not able to access class A's object >> attribute - 'a' ? I wishto extent class D with all the attributes of >> its base classes. how do i do that ? [snip] > Each class should do a similar super() call, with the appropriate name > substitutions. [snip] > -Peter A related question is about the order of the __init__ calls. Considering the following sample: #--8<--- class A (object): def __init__ (self): super (A, self) .__init__ () print 'i am an A' def foo (self): print 'A.foo' class B (object): def __init__ (self): super (B, self) .__init__ () print 'i am a B' def foo (self): print 'B.foo' class C (A, B): def __init__ (self): super (C, self) .__init__ () print 'i am a C' c = C () c.foo () #--8<--- aerts $ python2.4 inheritance.py i am a B i am an A i am a C A.foo I do understand the lookup for foo: foo is provided by both classes A and B and I do not state which one I want to use, so it takes the first one in the list of inherited classes (order of the declaration). However I cannot find an explanation (I may have googled the wrong keywords) for the order of the __init__ calls from C. I was expecting (following the same order as the method lookup): i am an A i am a B i am a C A.foo Thanks -- rafi "Imagination is more important than knowledge." (Albert Einstein) -- http://mail.python.org/mailman/listinfo/python-list
Re: OS X py2app confusion
bex wrote: > Im baffled about this one... > I was confused about the packaging, so I tried moving all the folders > in py2app into /Library/Python/2.3/ and deleted the py2app.pth file, > but it still wouldn't load the module. that does not sound as a good idea... > Any insight into the voodoo magic required to get this working? How many pythons do you have on your OS X? I have four different ones: Python as a Mac OS X install (original and 2.4) Python as a Fink installation (2.3 and 2.4) If you have several ones are you sure that the one you are using is the same as the one used to isntall py2app? Have you tried to re-install the py2app (even version 0.17)? my 2 cents -- rafi "Imagination is more important than knowledge." (Albert Einstein) -- http://mail.python.org/mailman/listinfo/python-list
Re: FAQ?
Keith P. Boruff wrote: > Michael Hoffman wrote: > >> Keith P. Boruff wrote: >> >>> Is there an FAQ available specific to this NG as I'm sure some of the >>> list slicing questions I have have been asked before. >> >> Try Google for . > > I tried and didn't find one. That's why I asked here. surprising as the first (for me at least on googgle.com) response is: www.python.org/doc/faq/ >> I don't know why you would want to look in a FAQ *specific* to a >> newsgroup to look up slicing questions, > > I have a specific question about list slicing and this is a python NG. > What's not to understand? Michael answer could be subtitled as: "if the faq is wider that the newsgroup, then you have more probability to find an answer." > A more general FAQ (or the stuff I've accessed on slicing) didn't answer > my question. If, after looking for information on your own you did not find a solution, it may mean that you are ready to post a question :-) my 2 cents -- rafi "Imagination is more important than knowledge." (Albert Einstein) -- http://mail.python.org/mailman/listinfo/python-list