Re: Multiple constructors
vincent wehren wrote: Philip Smith wrote: Call this a C++ programmers hang-up if you like. I don't seem to be able to define multiple versions of __init__ in my matrix class (ie to initialise either from a list of values or from 2 dimensions (rows/columns)). Even if Python couldn't resolve the __init__ to use on the basis of argument types surely it could do so on the basis of argument numbers??? At any rate - any suggestions how I code this Checking the number of arguments ain't all that hard: class Klass: def __init__(*args): self.args = args if len(self.args) == 1: # etc. This feels rather unpythonic, though. And it won't work, as `self' is not defined. ;) Reinhold -- http://mail.python.org/mailman/listinfo/python-list
Re: Python versus Perl ?
[EMAIL PROTECTED] wrote: I've read some posts on Perl versus Python and studied a bit of my Python book. I'm a software engineer, familiar with C++ objected oriented development, but have been using Perl because it is great for pattern matching, text processing, and automated testing. Our company is really fixated on risk managnemt and the only way I can do enough testing without working overtime (which some people have ended up doing) is by automating my testing. That's what got me started on Perl. I've read that many people prefer Python and that it is better than Perl. However, I want to ask a few other questions. Better than Perl is a very general statement. In my personal opinion, this is true for every project being larger than one file of ~200 LOC. 1. Perl seems to have alot of packaged utilities available through CPAN, the comprehensive perl network. These can aid in building parsers, web development, perl DBI is heavily used. This seems to be a very important benifit. I'm not sure that Python is as extenive at all in that regard ? There are the Python Package Index (PyPI), the Vaults of Parnassus, and when you don't find a needed package there, just come and ask here; almost always a decent solution is found. A thing similar to CPAN is being worked on by various people, though I don't know when it will become mature. Perl also has excellent pattern matching compared to sed, not sure about how Python measures up, but this seems to make perl ideally suited to text processing. Python has regular expressions much like Perl. The only difference is that Perl carries syntactic support for them, while in Python regular expressions are ordinary objects with methods etc. 2. Python is apparantly better at object oriented. Perl has some kind of name spacing, I have used that in a limited way. Does Perl use a cheap and less than optimal Object oriented approach ? That was what someone at work said, he advocates Python. Is it likely that Perl will improve it's object oriented features in the next few years ? There is the Perl 6 movement, but when you read some of the docs at http://dev.perl.org, you will come to the conclusion that - Perl 6 lies at least 3-5 years in the future and - it will be a huge mess. Someone here once said Perl 6 is the ultimate failure of Perl's philosophy. There may be split views about this... 3. Perl is installed on our system and alot of other systems. You don't have to make sys admins go out of there way to make it available. It's usualy allready there. Same goes with Python; it is installed per default on most modern Unices. Windows is a completely different chapter, however, Perl isn't more widespread there. I also did a search of job postings on a popular website. 108 jobs where listed that require knowledge of Perl, only 17 listed required Python. Becomeing more familiar with Perl might then be usefull for ones resume ? It doesn't harm, of course. Recent statistics about programmers' salaries indicate, however, that Python ranks top (I somehow lost the URL). If Python is better than Perl, I'm curious how really significant those advantages are ? Try to decide yourself. The Python tutorial and website are your friends. Reinhold -- http://mail.python.org/mailman/listinfo/python-list
Re: default value in a list
Michael Spencer wrote: Alex Martelli wrote: [explanation and the following code:] a, b, c = it.islice( ... it.chain( ... line.split(':'), ... it.repeat(some_default), ... ), ... 3) ... ... def pad_with_default(N, iterable, default=None): ... it = iter(iterable) ... for x in it: ... if N=0: break ... yield x ... N -= 1 ... while N0: ... yield default ... N -= 1 Why not put these together and put it in itertools, since the requirement seems to crop up every other week? line = A:B:C.split(:) ... def ipad(N,iterable, default = None): ... return it.islice(it.chain(iterable, it.repeat(default)), N) ... a,b,c,d = ipad(4,line) a,b,c,d ('A', 'B', 'C', None) Good idea! (+1 if this was posted on python-dev!) Reinhold -- http://mail.python.org/mailman/listinfo/python-list
Re: default value in a list
Nick Coghlan wrote: Reinhold Birkenfeld wrote: Why not put these together and put it in itertools, since the requirement seems to crop up every other week? line = A:B:C.split(:) ... def ipad(N,iterable, default = None): ... return it.islice(it.chain(iterable, it.repeat(default)), N) ... a,b,c,d = ipad(4,line) a,b,c,d ('A', 'B', 'C', None) Good idea! (+1 if this was posted on python-dev!) Please, please Google the python-dev archives before doing so ;) I have no intent of doing so, I would leave that to more experienced people... Reinhold ;) -- http://mail.python.org/mailman/listinfo/python-list
Re: make install with python
Uwe Mayer wrote: Hi, I am writing a Python application and use the GNU auto-tools to compile what needs compilation (i.e. Qt's .ui files). However, I don't know how to write an automake file that installs the main file (lmc.py) and some library files (i.e. ClassA.py, ClassB.py) into the appropriate directories. The regular way is to use distutils and a setup.py file (google for documentation). Reinhold -- http://mail.python.org/mailman/listinfo/python-list
What YAML engine do you use?
Hello, I know that there are different YAML engines for Python out there (Syck, PyYaml, more?). Which one do you use, and why? For those of you who don't know what YAML is: visit http://yaml.org/! You will be amazed, and never think of XML again. Well, almost. Reinhold -- http://mail.python.org/mailman/listinfo/python-list
Re: [perl-python] 20050118 keyed list
Jay Tilton wrote: : # the syntax of keyed list in Perl is too complex : # to be covered in a short message. JFTR: keyed lists are called dictionaries in Python. [1]Message-ID: [EMAIL PROTECTED] This guy's wish-wash is starting to be funny, after all! Reinhold -- http://mail.python.org/mailman/listinfo/python-list
Re: [perl-python] 20050118 keyed list
Jürgen Exner wrote: © # see perldoc perldata for an unix-styled course. Excuse me? Do you mind explaining where exactly perldata is Unix-styled? Remember: Perl == Unix == Satan. Reinhold -- http://mail.python.org/mailman/listinfo/python-list
Re: Assigning to self
Frans Englich wrote: Hello, I am having trouble with throwing class instances around. Perhaps I'm approaching my goals with the wrong solution, but here's nevertheless a stripped down example which demonstrates my scenario: #-- class foo: tests = {} def __init__( self, id ): try: me = self.__class__.tests[ id ] except KeyError: print Did not exist, initializing myself.. self.attr = exists self.__class__.tests[ id ] = self else: print Already exists! Re-using existing instance self = me print Me, self.attr + ! # line 18 def yo(self): return self.attr # line 21 As 'self' is a method parameter, changing it only affects the current function. When __init__ is called, the instance is already created, so you can't change it. What you are looking for is a class factory (is this term correct?), here is a sample implementation (using 2.4 decorators): class foo: tests = {} @classmethod def get_test(cls, id): if cls.tests.has_key(id): return cls.tests[id] else: inst = cls() inst.attr = exists cls.tests[id] = inst return inst def yo(self): return self.attr Here you define get_test as a classmethod, that is, it does not receive the instance as first argument, but the class. It can be called from the class (foo.get_test) or an instance (foo().get_test). An alternative might be to override __new__, but I'm sure someone other will suggest this. Reinhold -- http://mail.python.org/mailman/listinfo/python-list
Re: Why would I get a TypeEror?
Steven Bethard wrote: It's me wrote: Say again??? Please stop top-posting -- it makes it hard to reply in context. Reinhold Birkenfeld wrote... It's me wrote: If this is true, I would run into trouble real quick if I do a: (1/x,1.0e99)[x==0] Lazy evaluation: use the (x==0 and 1e99 or 1/x) form! If you want short-circuting behavior, where only one of the two branches gets executed, you should use Python's short-circuiting boolean operators. For example, (x == 0 and 1.0e99 or 1/x) says something like: Check if x == 0. If so, check if 1.0e99 is non-zero. It is, so return it. If x != 0, see if 1/x is non-zero. It is, so return it. Note that if you're not comfortable with short-circuiting behavior, you can also code this using lazy evaluation: (lambda: 1/x, lambda: 1.0e99)[x==0]() Or even (x==0 and lambda: 1e99 or lambda: 1/x)() Or ... Reinhold -- http://mail.python.org/mailman/listinfo/python-list
Re: Free python server.
Kartic wrote: And yes, they have python installed... Python 2.1! Reinhold -- http://mail.python.org/mailman/listinfo/python-list
Re: reference or pointer to some object?
Torsten Mohr wrote: Hi, Could you give us a more concrete use case? My suspicion is that anything complicated enough to be passed to a method to be modified will probably be more than a simple int, float, str or tuple... In which case, it will probably have methods to allow you to update it... yes, to be more explicit: I'm quite new to python and i wrote a small function that does a hexdump of a string. That string can be quite large, so i suspected a large overhead when the string would be copied and handed over to the function. It isn't. But i think my understanding was wrong (though it is not yet clear). If i hand over a large string to a function and the function had the possibility to change it, wouldn't that mean that it is necessary to hand over a _copy_ of the string? Else, how could it be immutable? You cannot modify a string. Notice that there are no in-place string methods -- str.strip() for example returns a new string. Thinking about all this i came to the idea How would i write a function that changes a string with not much overhead?. Basically, working with very large strings is costly (it saves overhead otherwise). So do make smaller parts and operate on these chunks. def func(s): change s in some way, remove all newlines, replace some charaters by others, ... return s s = func(s) This seems to be a way to go, but it becomes messy if i hand over lots of parameters and expect some more return functions. You can use tuples for that. Automatic tuple packing helps: def func(x, y): # change x, y and generate z return x, y, z x, y, z = func(x, y) Maybe it is because i did lots of perl programming, but func(\$s) looks easier to me. It does, but in fact the problem is not that the string is passed by value, but that the string is not modifiable... Reinhold -- http://mail.python.org/mailman/listinfo/python-list
Re: else condition in list comprehension
Matteo Dell'Amico wrote: Luis M. Gonzalez wrote: Hi there, I'd like to know if there is a way to add and else condition into a list comprehension. I'm sure that I read somewhere an easy way to do it, but I forgot it and now I can't find it... for example: z=[i+2 for i in range(10) if i%2==0] what if I want i to be i-2 if i%2 is not equal to 0? You could use [(i-2, i+2)[bool(i%2 == 0)] for i in range(10)] or, in a less general but shorter way [(i+2, i-2)[i%2] for i in range(10)] or even [i%2 and i-2 or i+2 for i in range(10)] One should note that the (cond and X or Y) construct only works if X can never produce a false value (such as 0, , []). In this example, it is okay, but replace 2 with 1 and you will run into trouble for i = 1. Reinhold -- http://mail.python.org/mailman/listinfo/python-list
Re: ? about file() and open()
Sean wrote: Was wondering if there was any difference between these two functions. I have read some text that said file() wasn't introduced until 2.2 and that it was synonymous with open(). Does this mean that I should be using file() where I used open() before? FYI, I submitted a patch to correct the docs: http://www.python.org/sf/1094011 Reinhold -- http://mail.python.org/mailman/listinfo/python-list
Re: What can I do with Python ??
Jabaru wrote: BTW, I don't know of a way to write fullscreen games in C#... Directx, Opengl, Gdi+, win32api, SDL... the list goes on Yes, that's right, but most of those you can use in Python, too. I should have inserted the word specific at the right point in my sentence wink Reinhold -- http://mail.python.org/mailman/listinfo/python-list
Re: Looping using iterators with fractional values
drife wrote: Hello, Making the transition from Perl to Python, and have a question about constructing a loop that uses an iterator of type float. How does one do this in Python? In Perl this construct quite easy: for (my $i=0.25; $i=2.25; $i+=0.25) { printf %9.2f\n, $i; } =Py2.3: for i in [x/4.0 for x in xrange(1, 10)]: print %9.2f % i Py2.4: for i in (x/4.0 for x in xrange(1, 20)): print %9.2f % i Reinhold -- http://mail.python.org/mailman/listinfo/python-list
Re: Looping using iterators with fractional values
Mike Meyer wrote: Or - and much safer when dealing with floating point numbers - iterate over integers and generate your float values: for j in range(1, 9): i = j * .25 print %9.2f % i There's a glitch there, though - should be range(1, 10). Reinhold PS: I'm wondering whether my genexp approach or this one is preferable. Readability is equal, I would say, but how about speed? Brought up a few timeits: Python 2.3 -- for i in [x/4.0 for x in range(1, 10)]: 36,9 sec for j in range(1, 10): i = j * 0.25: 33,7 sec Python 2.4 -- for i in (x/4.0 for x in range(1, 10)): 32,5 sec for j in range(1, 10): i = j * 0.25: 28,4 sec So what does that tell us? (a) don't use genexps where there is a simpler approach (b) Py2.4 rocks! Reinhold -- http://mail.python.org/mailman/listinfo/python-list
Re: Speed ain't bad
Craig Ringer wrote: On Fri, 2004-12-31 at 11:17, Jeremy Bowers wrote: I would point out a couple of other ideas, though you may be aware of them: Compressing all the files seperately, if they are small, may greatly reduce the final compression since similarities between the files can not be exploited. True; however, it's my understanding that compressing individual files also means that in the case of damage to the archive it is possible to recover the files after the damaged file. This cannot be guaranteed when the archive is compressed as a single stream. With gzip, you can forget the entire rest of the stream; with bzip2, there is a good chance that nothing more than one block (100-900k) is lost. regards, Reinhold -- http://mail.python.org/mailman/listinfo/python-list
Re: Parsing a search string
Freddie wrote: Happy new year! Since I have run out of alcohol, I'll ask a question that I haven't really worked out an answer for yet. Is there an elegant way to turn something like: moo cow farmer john -zug into: ['moo', 'cow', 'farmer john'], ['zug'] I'm trying to parse a search string so I can use it for SQL WHERE constraints, preferably without horrifying regular expressions. Uhh yeah. The shlex approach, finished: searchstring = 'moo cow farmer john -zug' lexer = shlex.shlex(searchstring) lexer.wordchars += '-' poslist, neglist = [], [] while 1: token = lexer.get_token() # token is '' on eof if not token: break # remove quotes if token[0] in '\'': token = token[1:-1] # select in which list to put it if token[0] == '-': neglist.append(token[1:]) else: poslist.append(token) regards, Reinhold -- http://mail.python.org/mailman/listinfo/python-list
Re: Parsing a search string
M.E.Farmer wrote: Ah! that is what the __future__ brings I guess. Damn that progress making me outdated ;) Python 2.2.3 ( a lot of extensions I use are stuck there , so I still use it) I'm also positively surprised how many cute little additions are there every new Python version. Great thanks to the great devs! Reinhold -- http://mail.python.org/mailman/listinfo/python-list
Re: what is lambda used for in real code?
Adam DePrince wrote: So, those are my thoughts on how lambdas are really used. If others out there have real-life code that uses lambdas in interesting ways, feel free to share them here! Lets not forget the real reason for lambda ... I really hoped you would point out the _real_ reason for lambda... the elegance of orthogonality. ... but you didn't. Everyone knows that lambda is there to help in one-liner contests and code obfuscation. Lambda is one of Python's very few instruments that assist in writing code reaching Perl's unreadability, and as such it should be valued highly! big-evil-grin-wink Reinhold -- http://mail.python.org/mailman/listinfo/python-list
Re: Why tuples use parentheses ()'s instead of something else like 's?
Alex Martelli wrote: Jeff Shannon [EMAIL PROTECTED] wrote: ... to remember and type some arcane alt-keycode formula to be able to do basic scripting would be obnoxious, to say the least. Most keyboards worldwide provide decent support for the ASCII character set (though some add a few extra national characters). Perhaps things will change Italian-layout support for braces is the pits (alt-keycodes ahoy): one way I managed to get a local friend interested in Python was to point out that he'd neved NEED to type braces (calling `dict' is just as good a way to make dictionaries, as braces-laden `dict display' forms;-). That's equally true for the German keyboard layout, though I believe that most programmers switch to standard English layout anyway. Reinhold -- http://mail.python.org/mailman/listinfo/python-list
Re: Why tuples use parentheses ()'s instead of something else like 's?
Roy Smith wrote: John Roth [EMAIL PROTECTED] wrote: If Python had originally been invented in a unicode world, I suppose we wouldn't have this problem. We'd just be using guillemots for tuples (and have keyboards which made it easy to type them). I suppose the forces of darkness will forever keep Python from requiring utf-8 as the source encoding. If I didn't make a fetish of trying to see the good in everybody's position, I could really work up a dislike of the notion that you should be able to use any old text editor for Python source. In any case, it's a good thing that Python can be edited with any old text editor, because that lowers the price of entry. I like emacs, the next guy likes vi, or vim, or notepad, or whatever. Nothing is keeping folks who like IDEs from inventing and using them, but I would have been a lot less likely to experiment with Python the first time if it meant getting one of them going just so I could run Hello, world. Perl6 experiments with the use of guillemots as part of the syntax. I shall be curious to see how this is accepted, of course only if Perl6 is ever going to see the light of day, which is an exciting matter of its own... With google as my witness, I predict that in 30 years from now, ASCII will be as much a dinosaur as a keypunch is today, and our children and grandchildren will both wonder how their ancestors ever managed to write programs without guillemots and be annoyed that they actually have to type on a keyboard to make the computer understand them. Well, it's not clear if they will still write programs... Reinhold -- http://mail.python.org/mailman/listinfo/python-list
Re: Why tuples use parentheses ()'s instead of something else like 's?
Grant Edwards wrote: On 2004-12-29, Reinhold Birkenfeld [EMAIL PROTECTED] wrote: Perl6 experiments with the use of guillemots as part of the syntax. As if Perl didn't look like bird-tracks already... http://www.seabird.org/education/animals/guillemot.html http://www.birdguides.com/html/vidlib/species/Uria_aalge.htm Well, (1,1,2,3,5) »+« (1,2,3,5,8); # results in (2,3,5,8,13) (+ being an operator) just isn't something I would like to read in my code... Reinhold -- http://mail.python.org/mailman/listinfo/python-list
Re: input record sepArator (not sepErator)
Peter Otten wrote: Terry Reedy wrote: 'separate' (se-parate == take a-part) and its derivatives are perhaps the most frequently misspelled English word on clp. Seems to be 'par' for the course. It has 2 e's bracketing 2 a's. It derives from the Latin 'parare', as does pare, so 'par' is the essential root of the word. My gripe for the day, just to let non-native writers know what not to imitate. I hereby suggest seperate/(separate+seperate) as the hamburger standard (see http://www.oanda.com/products/bigmac/bigmac.shtml) for technical communities. Some data points, adjectives only, s.e.e.o.: the web: 4% python: 9% slashdot: 26% perl: 29% * How did you get these data points? Reinhold -- [Windows ist wie] die Bahn: Man muss sich um nichts kuemmern, zahlt fuer jede Kleinigkeit einen Aufpreis, der Service ist mies, Fremde koennen jederzeit einsteigen, es ist unflexibel und zu allen anderen Verkehrs- mitteln inkompatibel. -- Florian Diesch in dcoulm -- http://mail.python.org/mailman/listinfo/python-list
Re: input record sepArator (not sepErator)
Peter Otten wrote: Reinhold Birkenfeld wrote: the web: 4% python: 9% slashdot: 26% perl: 29% * How did you get these data points? I copied the numbers from these pages: http://www.google.com/search?q=separate http://groups-beta.google.com/group/comp.lang.python/search?group=comp.lang.pythonq=separate http://www.google.com/search?q=site%3Aslashdot.org+separate http://groups-beta.google.com/group/comp.lang.perl.misc/search?group=comp.lang.perl.miscq=separate Same thing for the alternative spelling. Thanks. A pity that there is no de.comp.lang.python, as for German posts the Standard/Standart relation could be more accurate... or-just-count-the-misplaces-apostrophs-ly yours, Reinhold -- [Windows ist wie] die Bahn: Man muss sich um nichts kuemmern, zahlt fuer jede Kleinigkeit einen Aufpreis, der Service ist mies, Fremde koennen jederzeit einsteigen, es ist unflexibel und zu allen anderen Verkehrs- mitteln inkompatibel. -- Florian Diesch in dcoulm -- http://mail.python.org/mailman/listinfo/python-list
Re: What is on-topic for the python list [was Re: BASIC vs Python]
Doug Holton wrote: Hans Nowak wrote: Quote: this is comp.lang.python, not comp.lang.boo. Which is obviously not the same as Boo should not be mentioned on this newsgroup. I used the exact same phrase in another note except using the term logo instead of boo, and that is the exact interpretation I immediately received from others - they felt I was censuring the discussion here, as I felt you were. Non sequitur. The phrase's interpretation depends on the posting(s) it refers to. Reinhold -- [Windows ist wie] die Bahn: Man muss sich um nichts kuemmern, zahlt fuer jede Kleinigkeit einen Aufpreis, der Service ist mies, Fremde koennen jederzeit einsteigen, es ist unflexibel und zu allen anderen Verkehrs- mitteln inkompatibel. -- Florian Diesch in dcoulm -- http://mail.python.org/mailman/listinfo/python-list