Re: Python documentation too difficult for beginners

2010-11-03 Thread Hallvard B Furuseth
Steven D'Aprano writes: > On Tue, 02 Nov 2010 03:42:22 -0700, jk wrote: >> The former is difficult to find (try searching for 'open' in the search >> box and see what you get). > > A fair point -- the built-in open comes up as hit #30, whereas searching > for open in the PHP page brings up fopen a

PEP 380 - the 'yield from' proposal

2010-10-15 Thread Hallvard B Furuseth
Regarding http://www.python.org/dev/peps/pep-0380/, "Syntax for Delegating to a Subgenerator": The first call can only be .next(), there's no way to provide an initial value to .send(). That matches common use, but an initial .send() is possible if .next() was called before "yield from". So I su

Re: My first Python program

2010-10-14 Thread Hallvard B Furuseth
Seebs writes: >> For long strings, another option is triple-quoting as you've seen in doc >> strings: print """foo >> bar""". > > I assume that this inserts a newline, though, and in this case I don't > want that. True. $ python >>> """foo ... bar""" 'foo\nbar' >>> """foo\

Re: My first Python program

2010-10-14 Thread Hallvard B Furuseth
Seebs writes: >> You can't really rely on the destructor __del__ being called. > > Interesting. Do I just rely on files getting closed? Sometimes, but that's not it. Think Lisp, not C++. __del__ is not that useful. Python is garbage-collected and variables have dynamic lifetime, so the class c

Re: My first Python program

2010-10-13 Thread Hallvard B Furuseth
Ethan Furman writes: >Seebs wrote: >>On 2010-10-12, Hallvard B Furuseth wrote: >>>> self.type, self.name = None, None >> >>> Actually you can write self.type = self.name = None, >>> though assignment statements are more limited than in C. &g

Re: My first Python program

2010-10-12 Thread Hallvard B Furuseth
I wrote: > except IOError: > if e.errno != errno.ENOENT: raise# if you are picky Argh, I meant "except IOError, e:". That's for Python 2 but not Python 3. "except IOError as e:" works on Python 2.6 and above. -- Hallvard -- http://mail.python.org/mailman/listinfo/python-list

Re: My first Python program

2010-10-12 Thread Hallvard B Furuseth
Seebs writes: > http://github.com/wrpseudo/pseudo/blob/master/makewrappers >self.f = file(path, 'r') >if not self.f: >return None No. Failures tend to raise exceptions, not return error codes. Except in os.path.exists() & co. $ python >>> open("nonesuch") Tracebac

Re: [Python-ideas] [Python-Dev] Inclusive Range

2010-10-12 Thread Hallvard B Furuseth
Steven D'Aprano writes: > On Fri, 08 Oct 2010 22:10:35 +0200, Hallvard B Furuseth wrote: >> Jed Smith writes: >>>>>> a = [1, 2, 3, 4, 5, 6] >>>>>> a[::-1] >>> [6, 5, 4, 3, 2, 1] >> >> Nice. Is there a trick to get a &q

Re: harmful str(bytes)

2010-10-11 Thread Hallvard B Furuseth
Stefan Behnel writes: >Hallvard B Furuseth, 11.10.2010 21:50: >> Fine, so programs will have to do it themselves... > > Yes, they can finally handle bytes and Unicode data correctly and > safely. Having byte data turn into Unicode strings unexpectedly makes > the behaviou

Re: harmful str(bytes)

2010-10-11 Thread Hallvard B Furuseth
Terry Reedy writes: >On 10/8/2010 9:45 AM, Hallvard B Furuseth wrote: >>> Actually, the implicit contract of __str__ is that it never fails, so >>> that everything can be printed out (for debugging purposes, etc.). >> >> Nope: >> >> $ python2 -c 's

Re: harmful str(bytes)

2010-10-11 Thread Hallvard B Furuseth
Antoine Pitrou writes: >Hallvard B Furuseth wrote: >>Antoine Pitrou writes: >>>Hallvard B Furuseth wrote: >>>> The offender is bytes.__str__: str(b'foo') == "b'foo'". >>>> It's often not clear from looking at a piec

Re: harmful str(bytes)

2010-10-11 Thread Hallvard B Furuseth
Terry Reedy writes: >On 10/8/2010 9:31 AM, Hallvard B Furuseth wrote: >> That's not the point - the point is that for 2.* code which _uses_ str >> vs unicode, the equivalent 3.* code uses str vs bytes. Yet not the >> same way - a 2.* 'str' will somet

Re: [Python-ideas] [Python-Dev] Inclusive Range

2010-10-08 Thread Hallvard B Furuseth
Jed Smith writes: a = [1, 2, 3, 4, 5, 6] a[::-1] > [6, 5, 4, 3, 2, 1] Nice. Is there a trick to get a "-0" index too? Other than doing 'i or len(L)' instead of 'i', that is. >>> L = [1,2,3,4,5] >>> L[2:-2], L[2:-1], L[2:-0] # not quite right:-) ([3], [3, 4], []) -- Hallvard -- htt

Re: harmful str(bytes)

2010-10-08 Thread Hallvard B Furuseth
Steven D'Aprano writes: >On Fri, 08 Oct 2010 15:31:27 +0200, Hallvard B Furuseth wrote: >> That's not the point - the point is that for 2.* code which _uses_ str >> vs unicode, the equivalent 3.* code uses str vs bytes. Yet not the same >> way - a 2.* 'str&#x

Re: harmful str(bytes)

2010-10-08 Thread Hallvard B Furuseth
Antoine Pitrou writes: >Hallvard B Furuseth wrote: >> The offender is bytes.__str__: str(b'foo') == "b'foo'". >> It's often not clear from looking at a piece of code whether >> some data is treated as strings or bytes, particularly when &

Re: harmful str(bytes)

2010-10-08 Thread Hallvard B Furuseth
Arnaud Delobelle writes: >Hallvard B Furuseth writes: >> I've been playing a bit with Python3.2a2, and frankly its charset >> handling looks _less_ safe than in Python 2. >> (...) >> With 2. conversion Unicode <-> string the equivalent operation did >

harmful str(bytes)

2010-10-07 Thread Hallvard B Furuseth
I've been playing a bit with Python3.2a2, and frankly its charset handling looks _less_ safe than in Python 2. The offender is bytes.__str__: str(b'foo') == "b'foo'". It's often not clear from looking at a piece of code whether some data is treated as strings or bytes, particularly when translatin

Re: Python package to read .7z archives?

2010-08-04 Thread Hallvard B Furuseth
Giampaolo Rodolà writes: > 2010/8/4 Hallvard B Furuseth : >> Is there an equivalent of zipfile.py for .7z archives? >> I have one which extracts an archive member by running 7z e -so, >> but that's a *slow* way to read one file at a time. >> >> Google found

Python package to read .7z archives?

2010-08-04 Thread Hallvard B Furuseth
Is there an equivalent of zipfile.py for .7z archives? I have one which extracts an archive member by running 7z e -so, but that's a *slow* way to read one file at a time. Google found me some python interfaces to lzma, but apparently they only handle single compressed files, not .7z archives. (A

Re: Help to find a regular expression to parse po file

2009-07-06 Thread Hallvard B Furuseth
gialloporpora writes: > I would like to extract string from a PO file. To do this I have created > a little python function to parse po file and extract string: > > import re > regex=re.compile("msgid (.*)\\nmsgstr (.*)\\n\\n") > m=r.findall(s) I don't know the syntax of a po file, but this works

Re: [RELEASED] Python 3.1 final

2009-06-29 Thread Hallvard B Furuseth
Nobody writes: >On Sun, 28 Jun 2009 14:36:37 +0200, Martin v. Löwis wrote: >> See PEP 383. > > Okay, that's useful, except that it may have some bugs: > (...) > Assuming that this gets fixed, it should make most of the problems with > 3.0 solvable. OTOH, it wouldn't have killed them to have added

Re: [RELEASED] Python 3.1 final

2009-06-28 Thread Hallvard B Furuseth
Benjamin Peterson writes: >Nobody nowhere.com> writes: >> On Sun, 28 Jun 2009 19:21:49 +, Benjamin Peterson wrote: >> 1. Does Python offer any assistance in doing so, or do you have to >> manually convert the surrogates which are generated for unrecognised bytes? > > fs_encoding = sys.getfiles

Re: No trees in the stdlib?

2009-06-26 Thread Hallvard B Furuseth
Stefan Behnel writes: >João Valverde wrote: >> Besides some interface glitches, like returning None >> on delete if I recall correctly. > > That's actually not /that/ uncommon. Operations that change an object are > not (side-effect free) functions, so it's just purity if they do not have a > retur

Re: Python 3.0 - is this true?

2008-11-09 Thread Hallvard B Furuseth
Terry Reedy writes: > If you want to duplicate 2.x behavior, which does *not* work for all > types... > > def py2key(item): return (str(type(item)), item) Nope. sorted((-1, 2, True, False)) == [-1, False, True, 2] sorted((-1, 2, True, False), key=py2key) == [False, True, -1, 2] Mig

Re: Python 3.0 - is this true?

2008-11-09 Thread Hallvard B Furuseth
Steven D'Aprano writes: > How often do you care about equality ignoring order for lists containing > arbitrary, heterogeneous types? Arbitrary, I never have. Different types of my choice, a few times. I was only interested in there being some _sort_ order (and the same in different runs of the p

Re: class attrdict

2007-03-09 Thread Hallvard B Furuseth
Alex Martelli writes: > You make a good point. I do like being able to say foo.bar=baz rather > than foo['bar']=baz in certain cases -- not so much to save 3 chars, but > to avoid excessive punctuation; however, I don't really need this AND > all of dict's power at the same time, so, I don't inher

Re: class attrdict

2007-03-09 Thread Hallvard B Furuseth
Alex Martelli writes: > (...) >> class Namespace(object): > (...) > I might, if it weren't for the redundant "if" and the horribly buggy > interference between separate instances -- which is why I wrote it, > almost six years ago and without the bugs, as >

Re: Sorting strings containing special characters (german 'Umlaute')

2007-03-02 Thread Hallvard B Furuseth
[EMAIL PROTECTED] writes: > For sorting the letter "Ä" is supposed to be treated like "Ae", > therefore sorting this list should yield > l = ["Aber, "Ärger", "Beere"] Are you sure? Maybe I'm thinking of another language, I thought Ä shold be sorted together with A, but after A if the words are ot

class attrdict

2007-03-02 Thread Hallvard B Furuseth
Does this class need anything more? Is there any risk of a lookup loop? Seems to work... class attrdict(dict): """Dict where d['foo'] also can be accessed as d.foo""" def __init__(self, *args, **kwargs): self.__dict__ = self dict.__init__(self, *args, **kwargs) def __re

Re: LDAP/LDIF Parsing

2007-02-02 Thread Hallvard B Furuseth
Bruno Desthuilliers writes: >Hallvard B Furuseth a écrit : >>> else: >>># all LDAP attribs are multivalued by default, >>># even when the schema says they are monovalued >>>if len(data) == 1: >>> return da

Re: LDAP/LDIF Parsing

2007-02-02 Thread Hallvard B Furuseth
Bruno Desthuilliers writes: > class LdapObject(object): >(...) >def __getattr__(self, name): > try: >data = self._record[name] > except KeyError: >raise AttributeError( > "object %s has no attribute %s" % (self, name) > ) Note that LDAP attribute

Re: Python regular expression question!

2006-09-20 Thread Hallvard B Furuseth
"unexpected" <[EMAIL PROTECTED]> writes: > I'm trying to do a whole word pattern match for the term 'MULTX-' > > Currently, my regular expression syntax is: > > re.search(('^')+(keyword+'\\b') \b matches the beginning/end of a word (characters a-zA-Z_0-9). So that regex will match e.g. MULTX-FOO

Embedding python in package using autoconf

2006-05-15 Thread Hallvard B Furuseth
I want to use Python as an extension language in a package which uses autoconf. That means its and Python's autoconf #defines can conflict, so I can't safely #include both and the package's own include files:-( Do anyone have a safe way to #include at least without ? E.g. copy the files (and '

Modifying PyObject.ob_type

2006-05-08 Thread Hallvard B Furuseth
I've got some fixed-size types with identical object layout defind in C. The only differences are: Which methods they have, the name, and some are subtypes of others. Can I modify the ob_type of their instances, to switch between which of these types an object has? -- Hallvard -- http://mail.py

Re: Dispatching operations to user-defined methods

2006-05-08 Thread Hallvard B Furuseth
Michele Simionato writes: > Apparently Guido fell in love with generic functions, so > (possibly) in future Python versions you will be able to > solve dispatching problems in in an industrial strenght way. Looks interesting, I'll keep an eye on that. > Sometimes however the simplest possible way

Re: Dispatching operations to user-defined methods

2006-05-03 Thread Hallvard B Furuseth
I wrote: > I'm wondering how to design this: > (...) > One obvious implementation would be to provide a class Operation, > let the user define a single subclass of this, and have the server > call request_search(), response_search(), request_modify(), > check_access() etc in that subclass. > > Then

Dispatching operations to user-defined methods

2006-05-02 Thread Hallvard B Furuseth
I'm wondering how to design this: An API to let a request/response LDAP server be configured so a user-defined Python module can handle and/or modify some or all incoming operations, and later the outgoing responses (which are generated by the server). Operations have some common elements, and so

Re: try: except :

2006-01-13 Thread Hallvard B Furuseth
Thanks for the help. Tom Anderson writes: >> class NeverRaised(Exception): >>def __init__(self, *args): >>raise RuntimeError('NeverRaised should never be raised') > > Briliant! Although i'd be tempted to define an UnraisableExceptionError > to signal what's happened. Or ... A package

Re: try: except :

2006-01-10 Thread Hallvard B Furuseth
Paul Rubin writes: >Hallvard B Furuseth <[EMAIL PROTECTED]> writes: >> 'except None:' works for now, but I don't know if that's safe: >> >> for ex in ZeroDivisionError, None: >> try: >> 1/0 >> exc

try: except :

2006-01-10 Thread Hallvard B Furuseth
I'd like an 'except ' statement Is there a defined way to do that, for Python 2.2 and above? 'except None:' works for now, but I don't know if that's safe: for ex in ZeroDivisionError, None: try: 1/0 except ex: print "Ignored first exception." I could j