Re: [Python-Dev] UserString

2005-02-24 Thread Barry Warsaw
On Tue, 2005-02-22 at 11:16, Guido van Rossum wrote:
  Really?  I do this kind of thing all the time:
  
  import os
  import errno
  try:
  os.makedirs(dn)
  except OSError, e:
  if e.errno  errno.EEXIST:
  raise
 
 You have a lot more faith in the errno module than I do. Are you sure
 the same error codes work on all platforms where Python works? 

No, but I'm pretty confident the symbolic names for the errors are
consistent for any platform I've cared about wink.

 It's
 also not exactly readable (except for old Unix hacks).

Guilty as charged. ;)

-Barry



signature.asc
Description: This is a digitally signed message part
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] UserString

2005-02-24 Thread Andrew McNamara
 You have a lot more faith in the errno module than I do. Are you sure
 the same error codes work on all platforms where Python works? 

No, but I'm pretty confident the symbolic names for the errors are
consistent for any platform I've cared about wink.

 It's also not exactly readable (except for old Unix hacks).

Guilty as charged. ;)

The consistency of the semantics of core system calls is sort of trademark
of unix. Any system that claims to be Unix, but plays loose and fast
with semantics soon gets a very poor reputation (xenix, cough).

All well-coded unix apps are dependent on system calls returning
consistent errno's. Which is one thing that makes life so difficult for
posix environments layered on other operating systems.

-- 
Andrew McNamara, Senior Developer, Object Craft
http://www.object-craft.com.au/
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] UserString

2005-02-22 Thread David Ascher
On Tue, 22 Feb 2005 08:16:52 -0800, Guido van Rossum
[EMAIL PROTECTED] wrote:
  Really?  I do this kind of thing all the time:
 
  import os
  import errno
  try:
  os.makedirs(dn)
  except OSError, e:
  if e.errno  errno.EEXIST:
  raise
 
 You have a lot more faith in the errno module than I do. Are you sure
 the same error codes work on all platforms where Python works? It's
 also not exactly readable (except for old Unix hacks).

Agreed. In general, I often wish in production code (especially in
not-100% Python systems) that Python did a better job of at the very
least documenting what kinds of exceptions were raised by what
function calls.  Otherwise you end up with what are effectively
blanket try/except statements way too often for my taste.

--da
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] UserString

2005-02-21 Thread Guido van Rossum
  Anyway, can you explain why LBYL is bad?
 
 In the general case, it's bad because of a combination of issues.  It
 may violate once, and only once! -- the operations one needs to check
 may basicaly duplicate the operations one then wants to perform.  Apart
 from wasted effort, it may happen that the situation changes between
 the look and the leap (on an external file, or due perhaps to threading
 or other reentrancy).  It's often hard in the look to cover exactly the
 set of prereq's you need for the leap -- e.g. I've often seen code such
 as
  if i  len(foo):
  foo[i] = 24
 which breaks for i-len(foo); the first time this happens the guard's
 changed to 0=ilen(foo) which then stops the code from working
 w/negative index; finally it stabilizes to the correct check,
 -len(foo)=ilen(foo), but even then it's just duplicating the same
 check that Python performs again when you then use foo[i]... just
 cluttering code.  The intermediate Pythonista's who's learned to code
 try: foo[i]=24 // except IndexError: pass is much better off than the
 one who's still striving to LBYL as he had (e.g.) when using C.
 
 Etc -- this is all very general and generic.

Right. There are plenty of examples where LBYL is better, e.g. because
there are too many different exceptions to catch, or they occur in too
many places. One of my favorites is creating a directory if it doesn't
already exist; I always use this LBYL-ish pattern:

 if not os.path.exists(dn):
try:
   os.makedirs(dn)
except os.error, err:
   ...log the error...

because the specific exception for it already exists is quite subtle
to pull out of the os.error structure.

Taken to th extreme, the LBYL is bad meme would be an argument
against my optional type checking proposal, which I doubt is what you
want.

So, I'd like to take a much more balanced view on LBYL.

 I had convinced myself that strings were a special case worth singling
 out, via isinstance and basestring, just as (say) dictionaries are
 singled out quite differently by metods such as get... I may well have
 been too superficial in this conclusion.

I think there are lots of situations where the desire to special-case
strings is legitimate.

  Then you would be able to test whether something is sequence-like
  by the presence of __getitem__ or __iter__ methods, without
  getting tripped up by strings.
 
  There would be other ways to get out of this dilemma; we could
  introduce a char type, for example. Also, strings might be
  recognizable by other means, e.g. the presence of a lower() method or
  some other characteristic method that doesn't apply to sequence in
  general.
 
 Sure, there would many possibilities.
 
  (To Alex: leaving transform() out of the string interface seems to me
  the simplest solution.)
 
 I guess you mean translate.  Yes, that would probably be simplest.

Right.

BTW, there's *still* no sign from a PEP 246 rewrite. Maybe someone
could offer Clark a hand? (Last time I inquired he was recovering from
a week of illness.)

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] UserString

2005-02-20 Thread Alex Martelli
On 2005 Feb 20, at 05:20, Raymond Hettinger wrote:
   ...
This sort of thing is easy to test for and easy to fix.  The question 
is
whether we care about updating this module anymore or is it a relic.
Also, is the use case one that we care about.  AFAICT, this has never
come up before.
I did have some issues w/UserString at a client's, but that was 
connected to some code doing type-checking (and was fixed by injecting 
basestring as a base of the client's subclass of UserString and 
ensuring the type-checking always used isinstance and basestring).

My two cents: a *mixin* to make it easy to emulate full-fledged strings 
would be almost as precious as your DictMixin (ones to emulate lists, 
sets, files [w/buffering], ..., might be even more useful).  The point 
is all of these rich interfaces have a lot of redundancy and a mixin 
can provide all methods generically based on a few fundamental methods, 
which can be quite useful, just like DictMixin.

But a complete emulation of strings (etc) is mostly of didactical 
use, a sort of checklist to help ensure one implements all methods, not 
really useful for new code in production; at least, I haven't found 
such uses recently.  The above-mentioned client's class was an attempt 
to join RE functionality to strings and was a rather messy hack anyway, 
for example (perhaps prompted by client's previous familiarity with 
Perl, I'm not sure); at any rate, the client should probably have 
subclassed str or unicode if he really wanted that hack.  I can't think 
of a GOOD use for UserString (etc) since subclassing str (etc) was 
allowed in 2.2 or at least since a few loose ends about newstyle 
classes were neatly tied up in 2.3.

If we do decide it is a relic, no more updates perhaps some 
indication of deprecation would be warranted.  ((In any case, I do 
think the mixins would be useful)).

Alex
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] UserString

2005-02-20 Thread Greg Ewing
Alex Martelli wrote:
On 2005 Feb 20, at 17:06, Guido van Rossum wrote:
Oh, bah. That's not what basestring was for. I can't blame you or your
client, but my *intention* was that basestring would *only* be the
base of the two *real* built-in string types (str and unicode).
I think all this just reinforces the notion that LBYL is
a bad idea!
The need to check is this thingy here string-like is sort of frequent, 
because strings are sequences which, when iterated on, yield sequences 
(strings of length 1) which, when iterated on, yield sequences ad 
infinitum.
Yes, this characteristic of strings is unfortunate because it
tends to make some degree of LBYLing unavoidable. I don't
think the right solution is to try to come up with safe ways
of doing LBYL on strings, though, at least not in the long
term.
Maybe in Python 3000 this could be fixed by making strings *not*
be sequences. They would be sliceable, but *not* indexable or
iterable. If you wanted to iterate over their chars, you
would have to say 'for c in s.chars()' or something.
Then you would be able to test whether something is sequence-like
by the presence of __getitem__ or __iter__ methods, without
getting tripped up by strings.
--
Greg Ewing, Computer Science Dept, +--+
University of Canterbury,  | A citizen of NewZealandCorp, a   |
Christchurch, New Zealand  | wholly-owned subsidiary of USA Inc.  |
[EMAIL PROTECTED]  +--+
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] UserString

2005-02-20 Thread Phillip J. Eby
At 04:32 PM 2/21/05 +1300, Greg Ewing wrote:
Alex Martelli wrote:
The need to check is this thingy here string-like is sort of frequent, 
because strings are sequences which, when iterated on, yield sequences 
(strings of length 1) which, when iterated on, yield sequences ad infinitum.
Yes, this characteristic of strings is unfortunate because it
tends to make some degree of LBYLing unavoidable.
FWIW, the trick I usually use to deal with this aspect of strings in 
recursive algorithms is to check whether the current item of an iteration 
is the same object I'm iterating over; if so, I know I've descended into a 
string.  It doesn't catch it on the first recursion level of course (unless 
it was a 1-character string to start with), but it's a quick-and-dirty way 
to EAFP such algorithms.

___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com