generating a liste of characters

2008-12-03 Thread Yves Dorfsman
Is there any built in way to generate a list of characters, something along the line of range('a'-'z') ? Right now I am using: chars = [ chr(l) for l in range(0x30, 0x3a) ] # 0 - 9 chars += [ chr(l) for l in range(0x41, 0x5b) ] # A - Z chars += [ chr(l) for l in range(0x61, 0x7b) ] # a

Re: Unicode (UTF8) in dbhas on 2.5

2008-10-21 Thread Yves Dorfsman
Diez B. Roggisch <[EMAIL PROTECTED]> wrote: > Please write the following program and meditate at least 30min in front of > it: > while True: >print "utf-8 is not unicode" I hope you will have a better day today than yesterday ! Now, I did this: while True: print "¡ Python knows about enco

Unicode (UTF8) in dbhas on 2.5

2008-10-20 Thread Yves Dorfsman
Can you put UTF-8 characters in a dbhash in python 2.5 ? It fails when I try: #!/bin/env python # -*- coding: utf-8 -*- import dbhash db = dbhash.open('dbfile.db', 'w') db[u'smiley'] = u'☺' db.close() Do I need to change the bsd db library, or there is no way to

Re: Producing multiple items in a list comprehension

2008-05-23 Thread Yves Dorfsman
Peter Otten <[EMAIL PROTECTED]> wrote: > The speed gain is significant. Why should I throw away useful information if > I have it? My thinking was that it wasn't generic enough, and I was looking for a solution that would work for more generic problem. I agree, I shouldn't have used the world "e

Re: serach file for regexp, return if found?

2008-05-22 Thread Yves Dorfsman
[EMAIL PROTECTED] wrote: > i want to search a document for a particular regexp and then store > that regexp to a file. > but search and match only returns matchobjects(what are those anyway? > i dont get what to do with them, do they contain true/false, > stringposition etc?) > how do i do: > for

Re: Producing multiple items in a list comprehension

2008-05-22 Thread Yves Dorfsman
Peter Otten <[EMAIL PROTECTED]> wrote: > > A slightly similar problem: If I want to "merge," say, list1=[1,2,3] with > > list2=[4,5,6] to obtain [1,4,2,5,3,6], is there some clever way with "zip" > > to do so? > >>> items = [None] * 6 > >>> items[::2] = 1,2,3 > >>> items[1::2] = 4,5,6 > >>> items

How to subclass file

2008-05-14 Thread Yves Dorfsman
I want to create a subclass of 'file' but need to open the file with os.open (because I want to open it in exclusive mode), and need an additional method. Because I need an additional method, I truly need a object of my sublass. If I do something like class myFile(file): def __new__(cls, filen

Re: anonymous assignment

2008-05-12 Thread Yves Dorfsman
Marc 'BlackJack' Rintsch wrote: y, _, d, _, _, _, _, _, _ = time.localtime() But you still have have a variable that's using memory for nothing. I find this unsatisfactory... Get over it… Than what's the point of wanting a better language if every time we run in something that looks wron

Re: anonymous assignment

2008-05-12 Thread Yves Dorfsman
Scott David Daniels wrote: Yves Dorfsman wrote: ... Sorry this was a typo (again :-), I meant: d = time.local() y = d[0] d = d[2] Then: y, d = list(time.localtime())[:4:2] What is this ? Could you point me to a document on this syntax ? I've tried it, it works, but I

Re: anonymous assignment

2008-05-12 Thread Yves Dorfsman
Ben Finney wrote: y, _, d, _, _, _, _, _, _ = time.localtime() But you still have have a variable that's using memory for nothing. No, you have one extra unused name binding. The values that you don't want to use have *already* been allocated by the time the above statement is executed. Na

Re: anonymous assignment

2008-05-12 Thread Yves Dorfsman
Gabriel Genellina wrote: Uses Python 2.6! ;) No need of 2.6 - the above code works since Python 2.2 at least: Python 2.2.3 (#42, May 30 2003, 18:12:08) [MSC 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. import time t=time.localtime() type(t)

Re: anonymous assignment

2008-05-11 Thread Yves Dorfsman
D'Arcy J.M. Cain wrote: On Mon, 12 May 2008 02:28:13 GMT Yves Dorfsman <[EMAIL PROTECTED]> wrote: particular case, there's got to be a better way than: d = time.local() y = d[0] d = d[1] Like this? y, d = time.local()[:2] Sorry this was a typo (again :-), I meant:

Re: anonymous assignment

2008-05-11 Thread Yves Dorfsman
Paul Rubin wrote: Yves Dorfsman <[EMAIL PROTECTED]> writes: import time y, None, d, None, None, None, None = time.localtime() I know you can't assign anything to None, but I'm sure you get what I mean, a special keyword that means I don't care about this value. You ca

anonymous assignment

2008-05-11 Thread Yves Dorfsman
Is there anyway to tell python I don't care about a value ? Say I want today's year and day, I'd like to do something like: import time y, None, d, None, None, None, None = time.localtime() I know you can't assign anything to None, but I'm sure you get what I mean, a special keyword that means

Re: Is using range() in for loops really Pythonic?

2008-05-11 Thread Yves Dorfsman
John Salerno wrote: To me, the first example is a pure use of the for loop. You are iterating through an object and *using* the items you are stepping through. The second example, however, is simply doing something 10 times, and what it's doing has nothing to do with 'x' or xrange. So it see

Re: How to call a file

2008-05-11 Thread Yves Dorfsman
Gary Herron wrote: First of all, some terminology: You are not *calling* a file, you are *opening* it or reading. Wouldn't it be more correct to say that, in python, you either create a file object, or call a method for that object, once the object has been created ? Yves. -- http://mail.

Re: slicing lists

2008-05-09 Thread Yves Dorfsman
[EMAIL PROTECTED] wrote: The only thing is, is there is another natural meaning to [a,b:c]. Counting grids on the diagonals, the rational set is well defined: 0: 0, 0 1: 1, 0 2: 0, 1 3: 2, 0 4: 1, 1 5: 0, 2 6: 3, 0 7: 2, 1 ... Thencefore ( 2, 0 ) : ( 3, 0 ) is well defined. Thencefore, a,b:

Re: python newbie: some surprises

2008-05-09 Thread Yves Dorfsman
Gabriel Genellina wrote: I see the point of the OP. Couldn't the new-line be used as an equivalent of ':', for example, do you find this difficult to read: if a == 3 do_something() if a == 3: do_something() Yes, it could be done, there are no technical reasons to always force to use

Re: Grabbing previous iteration in a dict

2008-05-09 Thread Yves Dorfsman
[EMAIL PROTECTED] wrote: You cannot rely on the elements of a dictionary being in any particular order (dicts are internally hash tables), so the above is almost certainly ont what you want. Hi - thanks for your reply. How about if I made the dict into a list (of which I have done). How woul

Re: slicing lists

2008-05-08 Thread Yves Dorfsman
MRAB wrote: You should've read the thread entitled "Why don't generators execute until first yield?"! :-) Michael Torrie gave the URL http://www.dabeaz.com/generators/Generators.pdf. Your example can be rewritten as follows: p = file('/etc/passwd') # No need for readlines() because file's itera

Re: python newbie: some surprises

2008-05-08 Thread Yves Dorfsman
Mensanator wrote: 2. python requires to pass "self" to all instance methods Who uses methods? Is this a joke ? What are the alternatives ? and I missed ":" often. :) Try using something like Seed7, where you have to use "then" with "if" and "do" with "while" and "end" in every block. M

Re: slicing lists

2008-05-07 Thread Yves Dorfsman
Miles wrote: On Wed, May 7, 2008 at 7:46 PM, Ivan Illarionov > > Is there a way to do: > > x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] > > x[0,2:6] > > > > That would return: > > [0, 3, 4, 5, 6] Arg... Yes, this is a typo, I meant: [1, 3, 4, 5, 6] I think Yves meant to return [1, 3, 4, 5

Re: class definition

2008-05-07 Thread Yves Dorfsman
Miles wrote: In Python 2.2, classes and types were unified. If a class inherits from object (or any other built-in), it is considered a "new-style" class; otherwise, it is an old-style (or classic) class. There are some differences in their behavior; most notably, descriptors (computer propert

class definition

2008-05-07 Thread Yves Dorfsman
Does it make a difference if you put subclass object or not ? What is the difference between c1 and c2 here: class c1: pass class c2(object): pass Thanks, Yves. http://www.SollerS.ca -- http://mail.python.org/mailman/listinfo/python-list

slicing lists

2008-05-07 Thread Yves Dorfsman
Is there a way to do: x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] x[0,2:6] That would return: [0, 3, 4, 5, 6] I am surprised this notation is not supported, it seems intuitive. A concrete example of the sort of thing I want to do: p = file('/etc/passwd').readlines() q = [ e.strip().split(':')[0,2:] for

Re: #!/usr/bin/env python vs. #!/usr/bin/python

2008-05-02 Thread Yves Dorfsman
Thanks everybody, I didn't mean to start a flamewar... I do get it now, it's whatever python is in the path, vs. the specific one you're pointing to. Ben Finney wrote: No, because it's quite common for the PATH variable to have '/usr/local/bin' appear *before* both of '/bin' and '/usr/bin'.

Re: help with list comprehension

2008-05-02 Thread Yves Dorfsman
George Sakkis wrote: Another alternative is: from operator import itemgetter def m3(): colours, nums = zip(*map(itemgetter('colour','num'), l)) It's slower than m1() but faster than m2(); it's also the most concise, especially if you extract more than two keys. Good you guys gave me som

no cleanup on TERM signal

2008-05-01 Thread Yves Dorfsman
I did a few tests with this script: class byebye: def __del__(self): print 'Bye, bye...' x = byebye() x.del() gets executed if: -I del x, then run gc.collect() -simply exit the script -get the script to abort on an exception But if I kill it with the default signal TERM, the script di

help with list comprehension

2008-05-01 Thread Yves Dorfsman
In the following script, m1() and m2() work fine. I am assuming m2() is faster although I haven't checked that (loops through the list twice instead of once). Now what I am trying to do is something like m3(). As currently written it does not work, and I have tried different ways, but I have

#!/usr/bin/env python vs. #!/usr/bin/python

2008-05-01 Thread Yves Dorfsman
On UNIX, some people use #!/usr/bin/env python While other use #!/usr/bin/python Why is one preferred over the other one ? Thanks. -- Yves. http://www.SollerS.ca -- http://mail.python.org/mailman/listinfo/python-list

Re: how to remove \n in the list

2008-04-15 Thread Yves Dorfsman
Dan Bishop wrote: >>> lines[:] = [line.rstrip('\n') for line in lines] >> What is the point of the [:] after lines ? How different is it with or >> without it ? > > It causes the result to be stored in the existing list. > If we do: lines = [line.rstrip('\n') for line in lines] lines is now a

Re: how to remove \n in the list

2008-04-14 Thread Yves Dorfsman
Gabriel Genellina wrote: > En Mon, 14 Apr 2008 01:41:55 -0300, reetesh nigam >> l=['5\n', '2\n', '7\n', '3\n', '6\n'] >> >> how to remove \n from the given list > > l is is very poor name... I'll use lines instead: > > lines[:] = [line.rstrip('\n') for line in lines] When I saw the original me