Re: append on lists

2008-09-16 Thread Maric Michaud
Le Tuesday 16 September 2008 15:57:53 Grant Edwards, vous avez écrit :
 On 2008-09-16, Maric Michaud [EMAIL PROTECTED] wrote:
  all expressions that return something, return a new object,

 That's not _quite_ true:
  a=1
  b=a.__add__(0)
  a is b

 True

 ;)

This is implementation specific, the specification of the language says that 
it should be false, and it is for higher numbers :

[15]: a=1000

[16]: b=a.__add__(0)

[17]: a is b
...[17]: False

Don't disturb our OP, with side questions, please, it was enough hard like 
this ;)

-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: literals optimization (was Re: append on lists)

2008-09-16 Thread Maric Michaud
Le Tuesday 16 September 2008 16:57:26 Grant Edwards, vous avez écrit :
 On 2008-09-16, Maric Michaud [EMAIL PROTECTED] wrote:
  Le Tuesday 16 September 2008 15:57:53 Grant Edwards, vous avez écrit :
  On 2008-09-16, Maric Michaud [EMAIL PROTECTED] wrote:
   all expressions that return something, return a new object,
 
  That's not _quite_ true:
   a=1
   b=a.__add__(0)
   a is b
 
  True
 
  ;)
 
  This is implementation specific,

 Indeed.

  the specification of the language says that it should be
  false,

 That's curious.  If so, then the implimentation is in violating
 the specification.

But it doesn't matter as the is operator is not relevant for any use case 
with scalars and even strings and the optimization is not negligible.
In fact, I'd rather say it's unspecified, each implementation does it its own 
way.

[EMAIL PROTECTED] 17:48:03:~$ ipython

[1]: int() is 0 # contructor honour cache
...[1]: True

[2]: 256 is 255+1 # still caching
...[1]: True

[3]: 257 is 255+2 # here it stops
...[3]: False

[EMAIL PROTECTED] 17:48:08:~$ jython
Jython 2.2.1 on java1.6.0_07
Type copyright, credits or license for more information.
 int() is 0 # not caching when using constructor
0
 2 is 1+1 # caching on expression
1
 500 is 250*2 # caching occurs beyond my CPython
1
 5000 is 5000 # compile-time optimization
1
 5000 is 2500*2 # not caching too big numbers
0


 Where is that in the specification?

Each literal creates a new instance, and instances of int are not supposed to 
be unique for each value (at less it's not specified so), so 1 is not 1, 
but implementation can cache integers, short strings, to avoid creating an 
new instance each time they appear in code.

jython do this also for integer (see above), but not for strings :

[EMAIL PROTECTED] 17:33:52:~$ jython
Jython 2.2.1 on java1.6.0_07
Type copyright, credits or license for more information.
 a = 
 a is 
0
[EMAIL PROTECTED] 17:36:48:~$ ipython

[1]: a = 

[2]: a is 
...[2]: True

BTW, I'm talking of Jython 2.2, Jython 2.4, which quite ready I think, can 
change his mind as well.

 I  
 suspect the statement in the specification should only apply to
 mutable objects.


No, CPython also have a unique empty tuple, but tuple literals are not cached 
nor there are compile-time optimization :

[5]: () is ()
...[5]: True

[6]: (1,) is (1,)
...[6]: False


  and it is for higher numbers :
 [15]: a=1000
 
 [16]: b=a.__add__(0)
 
 [17]: a is b
 
  ...[17]: False
 
  Don't disturb our OP, with side questions, please, it was
  enough hard like this ;)

 I'm not trying to confuse the OP, but to quote Einstein:
 Everything should be made as simple as possible, but not
 simpler.



-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: literals optimization (was Re: append on lists)

2008-09-16 Thread Maric Michaud
Le Tuesday 16 September 2008 18:26:38 Grant Edwards, vous avez écrit :
 I was asking where in the specification
 http://docs.python.org/ref/ref.html it says that all expressions that
 return something, return a
 new object.  

I never said this, I said it's the spirit of python APIs, with some noticeable 
exceptions (see my first post).
But for this case specifically, a + b *should* return a new object, see the 
documentation of __add__ and __iadd__ special methods, it's clearly stated.

The fact that an integer with value 1 is always the same integer in some 
implementation is unrelated to the original problem.

-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: Case-insensitive string compare?

2008-09-16 Thread Maric Michaud
Le Friday 05 September 2008 19:36:56 Fredrik Lundh, vous avez écrit :
 Maric Michaud wrote:
  I suspect you are coming to conclusions a bit quickly, without taking the
  pain of understanding the whole discussion.

 I'm pretty sure I was the first one to post an answer in this thread,
 and I understand Python design and performance issues very well, thank you.


What is that ? Some sort of arguments ? If it matters, I know who you are and 
I don't try to compare my understanding of python with yours.

 (but given your talk about the cost of whitespace in a response to a
 comment about performance in that other subthread, 

It's silly that you continue to fight on unrelated arguments, I've already 
said that I misunderstood the OP's requirements and the proposed solution. 

 it's obvious that 
 you're just here to provide noise.  plonk plonk.)

I don't think you should make those assumptions, in your words, there are too 
much dumb or malevolent peoples on this list for it to be that obvious.

Nevertheless, I don't really see why I should explain myself for being here 
and trying to bring a (little, not like yours, of course) contribution.


-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: Gateway to python-list is generating bounce messages.

2008-09-16 Thread Maric Michaud
Le Monday 15 September 2008 16:45:12 Maric Michaud, vous avez écrit :
 This is not sufficient for auto-responses, and given the following rfcs, it
 would smart to both :

...
 - add or modify the Return-Path and/or Reply-To header for badly
 implemented auto-responders to point to list maintainer.

Oh, no ! to the list itself of course.

This could also avoid the common mistake mailing list users do in replying 
private mail accidentally when the adress of the list is only present in CC 
field.


-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: Case-insensitive string compare?

2008-09-05 Thread Maric Michaud
Le Friday 05 September 2008 08:30:44 Fredrik Lundh, vous avez écrit :
 Maric Michaud wrote:
   You''ll often see for loops written like this :
 
  for i in (e for e in iterable if predicate(e)) :
  ...

 luckily, I don't.  most people, when faced with that problem, writes it
 in the obvious way:

 for i in iterable:
 if predicate(i):
 ...

So do I, most often, but this construct is common, I think because it makes 
clear what the for loop is iterating over, also it comes naturally once used 
to the more elaborated 

for i in (expr(e) for e in iterable if predicate(e)) :

This one is truly a gain for readability IMO, compared to :

 for i in iterable:
 i = expr(i)
 if predicate(i):

In the latter, the reader need more effort to figure out what finally i is 
in the loop.


 to avoid an extra iterator and save typing.


and at the cost of an extra indentation level.

 /F

 --
 http://mail.python.org/mailman/listinfo/python-list



-- 
_

Maric Michaud

--
http://mail.python.org/mailman/listinfo/python-list


Re: Case-insensitive string compare?

2008-09-05 Thread Maric Michaud
Le Friday 05 September 2008 08:24:29 Fredrik Lundh, vous avez écrit :
 Maric Michaud wrote:
  premature optimization is the root of all evil

 So is use by that statement by people who don't have the slightest idea
 about what it actually means.

 The full version is

 We should forget about small efficiencies, say about 97% of the time:
 premature optimization is the root of all evil.

 Note the use of small efficiencies.  That is, in Python, things like
 local binding, inlining, slots, lazy generation of short sequences, etc.
   That is, things that takes time to write and reduces maintainability.

 It's not about having an excuse for writing crappy code with large
 inefficienies.

 And it's definitely not about programmers intentionally picking a dumb
 solution so they can optimize it later.


Yes, I'm aware of that, thanks, but Chris Rebert was proposing to alter the 
data structure in order to achieve this simple comparison, I don't think it 
fails into your concern.

   If the OP's proposal seems already messy, how about ths one :
   if lib.lower() not in ( e[0] for e in stage_map.items() ) :
   ...


Sorry, should be read :

if lib.lower() not in ( e[0] for e in stage_map.values() ) :

and it was the expression for comparison Chris's solution lead to, not a 
proposal.

 Given that your solution is just a remarkably inefficient way to write
 lib.lower() not in stage_map, and thus doesn't solve the OP:s problem,
 I suspect you're taking the evil part of Hoare's dictum a few bits too
 literally.


I suspect you are coming to conclusions a bit quickly, without taking the pain 
of understanding the whole discussion.

 /F

 --
 http://mail.python.org/mailman/listinfo/python-list



-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: Core dumped while interacting with OpenOffice.org via pyuno

2008-09-05 Thread Maric Michaud
Le Friday 05 September 2008 15:04:22 Marco Bizzarri, vous avez écrit :
 Hi all.

 I'm experiencing a core dump while working in the following environment

 - debain etch
 - python2.3
 - openoffice.org 2.4
 - Zope 2.8.8

 I was able to get a core dump, and the backtrace shows the following:

 Core was generated by `python2.3 tests/testActs.py'.
 Program terminated with signal 11, Segmentation fault.
 #0  0xb6b5219c in ?? () from /usr/lib/openoffice/program/libuno_cppu.so.3
... lot of backtrace ...
 #9  0xb68c76c7 in ?? () from /usr/lib/openoffice/program/libuno_sal.so.3
 #10 0x08797840 in ?? ()
 #11 0x0989b6e0 in ?? ()
 #12 0x in ?? ()

 Now, I can understand that the information I provided are not enough
 to give me suggestions on solving this issue; so, I'm asking
 suggesions on investigating the issue, namely:

 - should I ask here or is it better if I ask on a openoffice forum?

Yes.

 - should I use a debug-enabled python in order to have more meaningful
 backtraces?

What you need is more a debug enabled version of pyuno libraries.

 - is there something else I should do in order to have more clues
 (Read The Fine Manual (tm) is an acceptable answer)

You can try to find which function call exactly provoke the exception, the 
simplest way is to trace execution flow with some print statments and using 
non forking zope instance (runzope -d if I remember well).




-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: Case-insensitive string compare?

2008-09-05 Thread Maric Michaud
Le Friday 05 September 2008 14:33:22 J. Clifford Dyer, vous avez écrit :
 On Thu, 2008-09-04 at 18:48 -0500, Robert Dailey wrote:
  Thanks everyone for your help. I'm not opposed to using [key.lower()
  for key in stage_map] at all, I was just curious to see if there were
  any cleaner alternatives. It looks like that is what I'll be using.
  I'm not familiar with how python works internally, but coming from C++
  it seems like remaking the map would be slow. However, speed is not
  my main concern in my particular situation, I'm just curious to learn
  more about python.

 You should be opposed to that particular solution.  You have just taken
 a dictionary lookup (very fast) and turned it into a list traversal
 (slow).  Even if speed isn't your main concern, this is an unnecessary
 de-optimization.  You are deliberately slowing down your program to
 avoid a slightly more verbose lookup later.  Your data structure might
 as well be a list of tuples to begin with, to avoid creating a new list.
 You have effectively made your keys useless as keys.

 If your lookups need to be case insensitive, make the key lower case,
 and store the cased version in the value, whether as a tuple or a dict
 (depending on whether you want named access).

 d = {
    'foo': {'key': 'Foo', 'value': 'val1'}
    'spam': {'key': 'sPAm', 'value': 'val2'}
 }

 search = 'FOO'.lower()
 if search in d:
     result = d[search]
     key = result['key']
     value = result['value']

 The only reason I wouldn't use this solution is if you expect to have
 keys that will be identical when made lowercase, but if you're doing
 case-insensitive lookup, you obviously don't expect this to be an issue.


The OP has already said the keys are case-sensitive, so this is not an option, 
the only way to do it fast is to index upon insertion all keys in another 
dict, so you get in final :
d = { kEy1 : 1, Key1 : 2}
indexes = { key1 : [kEy1, Key1 ] }

 Cheers,
 Cliff


 --
 http://mail.python.org/mailman/listinfo/python-list



-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: Case-insensitive string compare?

2008-09-05 Thread Maric Michaud
Le Friday 05 September 2008 16:00:39 J. Cliff Dyer, vous avez écrit :
 Please keep the discussion on-list.


Sorry for the private email, I sent it again to the list..

 On Fri, 2008-09-05 at 15:36 +0200, Maric Michaud wrote:
  Le Friday 05 September 2008 14:33:22 J. Clifford Dyer, vous avez écrit :
   On Thu, 2008-09-04 at 18:48 -0500, Robert Dailey wrote:
Thanks everyone for your help. I'm not opposed to using [key.lower()
for key in stage_map] at all, I was just curious to see if there were
any cleaner alternatives. It looks like that is what I'll be using.
I'm not familiar with how python works internally, but coming from
C++ it seems like remaking the map would be slow. However, speed is
not my main concern in my particular situation, I'm just curious to
learn more about python.
  
   You should be opposed to that particular solution.  You have just taken
   a dictionary lookup (very fast) and turned it into a list traversal
   (slow).  Even if speed isn't your main concern, this is an unnecessary
   de-optimization.  You are deliberately slowing down your program to
   avoid a slightly more verbose lookup later.  Your data structure might
   as well be a list of tuples to begin with, to avoid creating a new
   list. You have effectively made your keys useless as keys.
  
   If your lookups need to be case insensitive, make the key lower case,
   and store the cased version in the value, whether as a tuple or a dict
   (depending on whether you want named access).
  
   d = {
  'foo': {'key': 'Foo', 'value': 'val1'}
  'spam': {'key': 'sPAm', 'value': 'val2'}
   }
  
   search = 'FOO'.lower()
   if search in d:
   result = d[search]
   key = result['key']
   value = result['value']
  
   The only reason I wouldn't use this solution is if you expect to have
   keys that will be identical when made lowercase, but if you're doing
   case-insensitive lookup, you obviously don't expect this to be an
   issue.
 
  The OP has already said the keys are case-sensitive, so this is not an
  option, the only way to do it fast is to index upon insertion all keys in
  another dict, so you get in final :
  d = { kEy1 : 1, Key1 : 2}
  indexes = { key1 : [kEy1, Key1 ] }

 That does not get the same behavior as the OP's original solution.  The
 OP's solution retrieves the first matching value only.  Mine gets one
 arbitrary matching value, determined by the algorithm used for
 constructing the search dict (not specified in my solution).

string.lower() in indexes gives the same result, but your right, a set of 
lowered keys could suffice for the exact OP's requirement.

 What the 
 OP actually said was that he can't throw away the case.  You assume that
 this means there would be multiple entries distinguished only by case.
 That might be true, but given his initial solution, it seemed to me more
 likely that he wants to keep the case for display purposes, as in a
 dictionary like this:

 {
Michaud: Maric,
Dyer: Cliff,
D'Amato: Alphonse,
 }


I didn't want to make gratuitous extrapolations on the OP's data structure, 
but I still think the OP's question was how to write cleanly a 
case-insensitive comparaison versus all the keys of a dict. Maybe I'm wrong.

--
_

Maric Michaud

--
http://mail.python.org/mailman/listinfo/python-list

Re: Retrieving the name of the class calling an instance method

2008-09-05 Thread Maric Michaud
Le Friday 05 September 2008 17:29:39 mercado mercado, vous avez écrit :
 In Python, is it possible for an instance method to know the name of the
 class which is calling it? For example, in the sample below, I would like
 for the someMethod method to print the name of the class calling it (bar
 in the first case, again in the second).

Note that self for somemethod is just a local variable of the calling 
function. You cannot retrieve those local variables without playing with the 
call stack, and this reveal generally a bad design (except for inspection 
purpose).

You have two common solutions here :
- the simplest is to pass the instance of bar or again to the foo constructor 
or as parameter of someMethod, or 
- attach the foo instance to self in both cases and define a proper __get__ 
method in foo, this is far more complex and add magic to the foo class, I 
don't recommand this unless you understand well the descriptor protocol.


 ---
 class foo():
 def someMethod(self):
 print x

 class bar():
 def __init__(self):
 f = foo()
 f.someMethod()

 class again():
 def __init__(self):
 f = foo()
 f.someMethod()

 bar()
 again()
 ---



-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: cPickle

2008-09-04 Thread Maric Michaud
Le Thursday 04 September 2008 13:08:59 Gerhard Häring, vous avez écrit :
 gopal mishra wrote:
  I have 3 objects and want to save in one pickle file.
 
  I used cPickle to dump 3 objects in a pkl file
 
  i.e  cPickle.dump(object1, fileobject,  -1)
 
   cPickle.dump(object2, fileobject,  -1)
 
   cPickle.dump(object3, fileobject,  -1)
 
 
 
  I have changed the 3^rd object and  want to save the updated 3^rd object
  in the pickle file.
 
  I have to dump all the 3 objects again.
 
  Is there any way to dump only the 3^rd object in to the pkl file.

 No, there isn't. You could, of course, save each object in its own
 pickle file. Or use an object database like ZODB instead.


Shelve is another alternative, shipped with python and rather straightforward. 
Shelve as ZODB use pickles of objects but, unlike ZODB, won't need you change 
anything  to your actual classes.

[8]: import shelve

[9]: s = shelve.open('db')

[10]: a, b, c = 5, 4., (foo, bar)

[11]: s['a'], s['b'], s['c'] = a, b, c

[12]: s.close()

[13]:
[EMAIL PROTECTED] 13:46:42:~$ file db
db: Berkeley DB (Hash, version 8, native byte-order)
[EMAIL PROTECTED] 13:46:44:~$ ipython

[1]: import shelve

[2]: s = shelve.open('db')

[3]: for name, value in s.items() : print name, value
   ...:
b 4.0
a 5
c ('foo', 'bar')

[5]: del s['b']

[6]: s['c'] += ('baz',)

[7]: s.sync()

[8]:
[EMAIL PROTECTED] 13:48:12:~$ ipython

[1]: import shelve

[2]: s = shelve.open('db')

[3]: for name, value in s.items() : print name, value
   ...:
a 5
c ('foo', 'bar', 'baz')

The sync method may not work on all platform, maybe you'll have to close and 
re-open the db file to write to disk.

-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: overwrite set behavior

2008-09-04 Thread Maric Michaud
Le Thursday 04 September 2008 14:31:23 Michele Petrazzo, vous avez écrit :
 Marco Bizzarri wrote:
  looking at the source, maybe you could create a subclass of Set
  redefining the __contains__ method?

 Made some tries, but __contains__ are never called


No, __contains__ is only called  with in operator, not for internal hashing. 
Anyway this solution is bad, you'll need to compare the new element with all 
the set contain, which would result in a O(n) algorithm for adding elements 
to the set in place of the O(1) it use.

The right way to go is one like Diez show you in a previous post.

   class foo(set):
 ...  def __contains__(self, value):
 ...   print value
 ...
   a = foo((1,2))
  



-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: why is self not passed to id()?

2008-09-04 Thread Maric Michaud
Le Thursday 04 September 2008 22:26:53 Ruediger, vous avez écrit :
 Hello!


Hello,

 Executing following little program gives me an TypeError.

 What makes me wonder is that foo does get an argument passed while bar
 doesn't. Can anyone explain why??


Because id is a builtin written in the core language and doesn't subscribe to 
the descritpor protocol python functions has.


 class foo(list):
 __hash__ = lambda x: id(x)


Wow ! You are really going on trouble with this, believe me there is a real 
good reason for list not to be hashable. A dictionnary or set containing some 
of your foo is virtually inconsistent, read carefully the manual about 
prerequesites for dict keys, they *need* to be immutable.


 class bar(list):
 __hash__ = id

 _s_ = set()
 _s_.add(foo())
 _s_.add(bar())

 [EMAIL PROTECTED]:~ python test01.py
 Traceback (most recent call last):
   File test01.py, line 9, in module
 _s_.add(bar())
 TypeError: id() takes exactly one argument (0 given)

 --
 http://mail.python.org/mailman/listinfo/python-list



-- 
_

Maric Michaud
_

Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 4 26 88 00 97
Mobile: +33 6 32 77 00 21
--
http://mail.python.org/mailman/listinfo/python-list


Re: why is self not passed to id()?

2008-09-04 Thread Maric Michaud
Le Thursday 04 September 2008 23:35:18 Terry Reedy, vous avez écrit :
 Maric Michaud wrote:
  Le Thursday 04 September 2008 22:26:53 Ruediger, vous avez écrit :
  class foo(list):
      __hash__ = lambda x: id(x)
 
  Wow ! You are really going on trouble with this, believe me there is a
  real good reason for list not to be hashable. A dictionnary or set
  containing some of your foo is virtually inconsistent, read carefully the
  manual about prerequesites for dict keys, they *need* to be immutable.

 No, the id comparison needs to be immutable -- which it is by default
 for object()s, being based on id.  Mutable instances of classes derived
 from object work fine as keys as long as they keep default __eq__ and
 __hash__.  List over-rides the default, so foo needs to reverse that
 override:
    def __eq__(self, other):
      return id(self) == id(other)

 This means, of course, that foo loses value-based equality comparison.

Yes, so what's the point of using lists as keys. class a : pass already do 
this.

-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: Case-insensitive string compare?

2008-09-04 Thread Maric Michaud
Le Friday 05 September 2008 00:14:37 Robert Dailey, vous avez écrit :
 Hi,

 I currently have a dictionary object that I'm doing the following with:

 if lib not in stage_map:
 # ... do stuff ...

 However, this will perform a case-sensitive comparison between lib and each
 key in stage_map. Is there a way to make this do a case-insensitive
 comparison instead? Yes, I realize I could do this:

 if lib not in [key.lower() for key in stage_map]


This is the normal python way to compare string without case, and the idiom is 
not surprising. You''ll often see for loops written like this :

for i in (e for e in iterable if predicate(e)) :
...

 However, I don't want to do this because it'll quickly end up getting
 messy. I want this solution to be an absolute last resort. I'm not a pro
 with python, so I'm hoping there are cleaner alternatives out there.

If you fell hard to read you can break it on two lines :

lowered_keys = (key.lower() for key in stage_map)
if lib.lower() not in lowered_keys :
 

BTW, it's string manipulation, it's normal it takes more than one brain cycle 
to achieve. Try the regexp solution and make your choice, more than often, I 
keep the straightforward, read as pseudo-code, python expression.

-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: Case-insensitive string compare?

2008-09-04 Thread Maric Michaud
Le Friday 05 September 2008 00:47:00 Chris Rebert, vous avez écrit :
 On Thu, Sep 4, 2008 at 3:37 PM, Robert Dailey [EMAIL PROTECTED] wrote:
  On Thu, Sep 4, 2008 at 5:21 PM, Fredrik Lundh [EMAIL PROTECTED]
 
  wrote:
  Robert Dailey wrote:
  I currently have a dictionary object that I'm doing the following with:
 
  if lib not in stage_map:
 # ... do stuff ...
 
  However, this will perform a case-sensitive comparison between lib and
  each key in stage_map. Is there a way to make this do a
  case-insensitive comparison instead?
 
  dictionary lookups use the exact value.  to make a case-insensitive
  lookup, use key.lower() instead of key when creating the dictionary, and
  then do
 
 if lib.lower() not in state_map:
 ...
 
  So you're saying to ensure that stage_map's keys are initially lower-case
  to begin with? Well, I can't do this either since the case of the keys is
  actually valuable later on. It's only for the purposes of this specific
  comparison operation that the case should be ignored.

 Then store the string in its original case in the value part of the
 key-value pair:

 stage_map[key.lower()] = (key,whatever)


premature optimization is the root of all evil

I don't recall the OP wanted a (a bit) faster solution to his problem in 
counterpart of memory loss and syntax complication.

If the OP's proposal seems already messy, how about ths one :
if lib.lower() not in ( e[0] for e in stage_map.items() ) :
...


 - Chris

  --
  http://mail.python.org/mailman/listinfo/python-list



-- 
_

Maric Michaud

--
http://mail.python.org/mailman/listinfo/python-list


Re: properties setting each other

2008-09-03 Thread Maric Michaud
Le Wednesday 03 September 2008 15:57:50 mk, vous avez écrit :
 I try to set two properties, value and square in the following code,
 and arrange it in such way that setting one property also sets another
 one and vice versa. But the code seems to get Python into infinite loop:

   import math
   class Squared2(object):

 def __init__(self, val):
 self._internalval=val
 self.square=pow(self._internalval,2)
 
 def fgetvalue(self):
 return self._internalval
 
 def fsetvalue(self, val):
 self._internalval=val
 self.square=pow(self._internalval,2)
 
 value = property(fgetvalue, fsetvalue)

 def fgetsquare(self):
 return self.square
 def fsetsquare(self,s):
 self.square = s
 self.value = math.sqrt(self.square)
 
 square = property(fgetsquare, fsetsquare)

 
   a=Squared2(5)

 Traceback (most recent call last):
    File pyshell#11, line 1, in module
      a=Squared2(5)
    File pyshell#10, line 5, in __init__
      self.square=pow(self._internalval,2)
    File pyshell#10, line 19, in fsetsquare
      self.square = s
    File pyshell#10, line 19, in fsetsquare
      self.square = s
    File pyshell#10, line 19, in fsetsquare
      self.square = s
    File pyshell#10, line 19, in fsetsquare
      self.square = s
    File pyshell#10, line 19, in fsetsquare
      self.square = s
    File pyshell#10, line 19, in fsetsquare

 ...

 Is there a way to achieve this goal of two mutually setting properties?

Your square property is not correctly defined, it recurselively call itself, 
it should be (I also avoided the extra lookup) :

 def fgetsquare(self):
 return self._square
 def fsetsquare(self,s):
 self._square = s
 self.value = math.sqrt(s)

then the fsetvalue will be also be called recursively as it use the square 
property, you should also write it :

 def fsetvalue(self, val):
 self._internalval=val
 self._square=pow(val,2)

*but*, if you want to add more logic in the setters, you could want to add two 
extra methods :

 def _setsquare(self, v) :
 # some extra logic here
 self._square = s

 def fsetsquare(self,s):
 self._setsquare(s)
 self._setvalue = math.sqrt(s)

 def _setvalue(self, val):
 # some extra logic here
 self._internalval=val

 def fsetvalue(self, val):
 self._setvalue(val)
 self._setsquare=pow(val,2)


Note that if one property can really be computed from another, this kind of 
thing could be considered as bad design (except if the computation is heavy).

-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: properties setting each other

2008-09-03 Thread Maric Michaud
Le Wednesday 03 September 2008 16:44:10 Maric Michaud, vous avez écrit :
          def _setsquare(self, v) :
                  # some extra logic here
                  self._square = s

          def fsetsquare(self,s):
                  self._setsquare(s)
                  self._setvalue = math.sqrt(s)

          def _setvalue(self, val):
                  # some extra logic here
                  self._internalval=val

          def fsetvalue(self, val):
                  self._setvalue(val)
                  self._setsquare=pow(val,2)

Oh sorry for this last version the setters should be :

  def fsetsquare(self,s):
  self._setsquare(s)
  self._setvalue = math.sqrt(self.square)

  def fsetvalue(self, val):
  self._setvalue(val)
  self._setsquare=pow(self.value, 2)

as we don't know what is done in _setXXX methods.

-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: properties setting each other

2008-09-03 Thread Maric Michaud
Le Wednesday 03 September 2008 17:40:43 mk, vous avez écrit :
  Note that if one property can really be computed from another, this kind
  of thing could be considered as bad design (except if the computation is
  heavy).

 Hmm, why? Is the line of thinking smth like: because the variables
 should be kept to minimum and they should be calculated at the moment
 they are needed?

Because you have to make extra effort to keep the logical relation between 
value and square. self._square is not really needed, and what is not needed 
is just extra hassle.

Doesn't it clear that your code is more hard to maintain than the 
alternative :

class Squared(object):

def __init__(self, val):
self._val=val

def fgetvalue(self):
return self._val
def fsetvalue(self, val):
self._val=val
value = property(fgetvalue, fsetvalue)

def fgetsquare(self):
return self.value ** 2
def fsetsquare(self,s):
self.value = math.sqrt(s)
square = property(fgetsquare, fsetsquare)



-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: properties setting each other

2008-09-03 Thread Maric Michaud
Le Wednesday 03 September 2008 19:38:13 Bruno Desthuilliers, vous avez écrit :
 FWIW, if there's no computation on getting or setting value, you can
 make it a plain attribute.

Yes, this is the problem with these tiny examples, we don't know at which 
point we must simplify them...


 But while it's quite clear that in this example use case it would be
 better  to have only one property (weither square or value, depending on
 which is the most often use), things are not always that simple in real
 world code, and - as you mentionned - there may be times where you have
 interdependant properties and really want to avoid recomputing the same
 thing over and over. Now there's no one size fits all solution here -
 it's an optimization problem, and as such depends on real use cases.

Yes, my advice was exactly in that sense, it's an optimization problem, 
duplicating such a data is a caching mechanism, and should be done knowingly, 
in acceptance of all the complication it comes with.


-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: Multiple values for one key

2008-08-28 Thread Maric Michaud
Le Thursday 28 August 2008 03:43:16 norseman, vous avez écrit :
 Terry Reedy wrote:
  Ron Brennan wrote:
  Hello,
 
 
  How would I create a dictionary that contains multiple values for one
  key.
 
  Make the value a collection object (set or list if you plan to add and
  delete).
 
   I'd also like the key to be able to have duplicate entries.
 
  Dict keys must be hashable and unique.
 
  tjr
 
  --
  http://mail.python.org/mailman/listinfo/python-list

 
 First part I understand, second is still giving me a problem.

 For some reason I still want keys to be dbf column headers.
 like:

 name:address:zip so forth
  --- --- --
 guy: unknown:0
 girl: 123 tiny street:12345
 boy:321 here:3
 gal:999 over there: 5
 so forth

 Thus one key has many values. And you can then index on whatever key(s)
 you wish - name,zip...

 With billions plus records, trying to put a unique key on each entry
 seems to preclude the need for a dictionary, just use the entries.
 (Format to SDF and index on substr(line,1,24)+substr(line,48,+5) etc..)
name+ zip
 OK - I know I missed the whole concept of a Python Dictionary. I haven't
 read anything as yet that gives a clear picture of what it is and what
 it is for.  Please, classroom concepts usually consist of a handful of
 objects. I have files here that takes up multiple DVDs each, AFTER a 15
 to 1 compression. 3 bytes per pixel. I need to work with them. Things
 like changing geobase projections and cookie cutting based on real world
 coordinates and modifying the lumens and so forth. Based on what I've
 read, the Python Dictionary concept flat will not work for such as this.
 Yes - the example is overkill. But in my world it is reality. I deal
 with sizeable things. Not all are raster. Most all are binary!  Things
 that work on massive text files - like banking and mortgage - simply
 don't work here. There are seldom 'lines'. There are always bytes. Lots
 of bytes. Things that form a group are not necessarily stored sequentially.

 Back to What Is A Python Dictionary: Somebody please enlighten me.



Disctionaries are hash tables with a unique key and constant time lookup. What 
you want could be implemented as a complex data structures with as many dict 
as needed keys, but it seems you really want a relational table and a rdbms. 
This is exactly what they are for. A short example with the new python2.5 
sqlite package :

[107]: import sqlite3

[108]: from string import letters

[109]: db = sqlite3.connect(':memory:')

[110]: db.execute(create table 'table1' ('name' text(20), 'address' 
text(100), primary key ('name', 'address')))
...[110]: sqlite3.Cursor object at 0x2b0cd9712c38

[111]: db.executemany(insert into 'table1' values (?, ?), 
((letters[i%len(letters)]*i, %d street % i) for i in range(1000))).rowcount
...[111]: 1000

[112]: for i in db.execute(select * from 'table1' where address 
like '99 %') : print i
   .:
(u'VVV',
 
u'99 street')

[113]: for i in db.execute(select * from 'table1' where name 
like '___') : print i
   .:
(u'ddd', u'3 street')

[114]: db.execute(insert into 'table1' values (?, ?), ('ddd', '4 
street')).rowcount
...[114]: 1

[115]: for i in db.execute(select * from 'table1' where name 
like '___') : print i
   .:
(u'ddd', u'3 street')
(u'ddd', u'4 street')

[116]: db.execute(insert into 'table1' values (?, ?), ('ddd', '4 
street')).rowcount
---
IntegrityErrorTraceback (most recent call last)

/home/maric/ipython console in module()

IntegrityError: columns name, address are not unique




 Steve
 [EMAIL PROTECTED]
 --
 http://mail.python.org/mailman/listinfo/python-list



-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: import error between 2 modules

2008-08-27 Thread Maric Michaud
Le Wednesday 27 August 2008 12:38:33 jimgardener, vous avez écrit :
 empmodule.py
 --
 from workmodule import Worker

 class Employer:
     def __init__(self,n):
         self.name=n
         self.worker=Worker()
     def getemployerName(self):
         return self.name
     def callWorker(self,message):
         self.worker.answerCall(message)
 if __name__ == __main__:
     emp=Employer()
     emp.callWorker(report to work)


 workmodule.py
 --
 from empmodule import Employer
 class Worker:
     def __init__(self):
         self.emp=Employer()
     def answerCall(self,msg):
         print Worker :+msg+ received
 from :+self.emp.getemployerName()

For this case you could do your import directly in the __init__ func and watch 
the infinite loop you did in action !

empmodule.py
--
from workmodule import Worker

class Employer:
def __init__(self,n):
self.name=n
self.worker=Worker(self)
def getemployerName(self):
return self.name
def callWorker(self,message):
self.worker.answerCall(message)
if __name__ == __main__:
emp=Employer()
emp.callWorker(report to work)


workmodule.py
--
class Worker:
def __init__(self, employer):
from empmodule import Employer
if not isinstance(employer, Employer):
raise ValueError(Not an employer)
self.emp=employer()

-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: Multiple values for one key

2008-08-27 Thread Maric Michaud
Le Thursday 28 August 2008 01:56:54 Terry Reedy, vous avez écrit :
   I'd also like the key to be able to have duplicate entries.

 Dict keys must be hashable and unique.

Let's admit they don't have to be unique (it's easy to implement all sort of 
data structures in pure python), but what would be the syntax for modifying 
an existing value, for example, what should be the result of two consecutive 
assignements ?

d[5] = None
d[5] = 0
d[5] == [ None, 0 ] ??

What exactly are you trying to achieve ?

-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: generate methods at runtime, but the wrong one gets called

2008-08-25 Thread Maric Michaud
Le Monday 25 August 2008 11:37:23 Steven Samuel Cole, vous avez écrit :
 Hello,

 I am writing an application that controls robots. Different robots can
 do different kinds of movements, such as e.g. open gripper, rotate
 gripper, etc. My RobotControl class should support all kinds of
 robots. I therefore delegate the actual control work to extra
 control-specific classes, one class per movement type, e.g.
 OpenGripperControl, RotateGripperControl. These movement control
 classes are created by a simple class factory.


i don't get your design, it seems over-complicated to mee at first glance.

 This is my class half-way through:
...

 I tried this in class RobotControl in __init__() in the 'for
 movementType ...' loop:

 funcName = 'Set' + movementType
 function = lambda self, value:
 self.__setMovementTypeValue(movementType, value)
 method   = new.instancemethod(function, self, self.__class__)
 setattr(self, funcName, method)

 and the code somewhat seems to work, but instead of SetOpenGripper,
 SetRotateGripper is called.


The free variable movementType in the lambda is evaluated lately as it were in 
its last state once you return from __init__, if you want to early bind it to 
its value in each step of a for loop, you must use :

func = lambda s, v, m_t=movementType : s.__setMovementTypeValue(m_t, v)

 My questions:

 1.) Does this look like a somewhat reasonable approach to someone who
 knows more about Python than me ?

At first, what you do is creating instancemethod and bound them to an instance 
of your class, I find this confusing. Why not just use __getattr__ special 
method ?

(you don't need __setMovementTypeValue in this example)

def __getattr__(self, name) :
if name.startswith('Set') :
movement = name.lstrip('Set')
if movement in self.__controls :
return lambda value : self.__controls[movement].SetValue(value)
raise AttributeError

That said, if I understand you well, your class RobotControl seems to contain 
only logic that is not specific to an instance, nor to a class of instances. 
In other OOP language I would encourage you to implement this logic in some 
sort of singleton, but in python we don't like this construct, module level 
variables and function do perfectly the job, simple as they are.

 2.) What could I be doing wrong ?

 I have a suspicion that my error is not even related to all this fancy
 runtime code generation stuff, but something really dumb and I've just
 been coding for too long to see it.

 http://mail.python.org/pipermail/python-list/2007-June/446601.html
 shows a somewhat comparable constellation and it was a good guideline.
 But there, the function is created by the class factory, as well and I
 unfortunately can't do that.

 Thank you very much,

 Steve
 --
 http://mail.python.org/mailman/listinfo/python-list



-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: A variables variables

2008-08-24 Thread Maric Michaud
Le Sunday 24 August 2008 03:24:29 Terry Reedy, vous avez écrit :
 Gandalf wrote:
  how can I declare a variable with another variable  name?
 
  for example  I will use PHP:
 
  $a= hello;
 
  $a_hello=baybay;
 
  print ${'a_'.$a)  //output: baybay
 
 
  how can i do it with no Arrays using  python

 Others have given you the direct answer.

Well using locals() is the right answer, it is a very bad use case for eval.

 But using lists or dicts is 
 almost always a better solution in Python than synthesizing global/local
 namespace names.

 a={'hello':'baybay'}
 print a['hello']

But locals() *is* an already made dict, very convenient for use locally and 
read-only.

[155]: var1 = easy

[156]: var2 = proper

[157]: var3 = locals

[158]: print a %(var2)s and %(var1)s use of %(var3)s with %(var3)s() % 
locals()
a proper and easy use of locals with locals()


-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: Imports visibility in imported modules problem

2008-08-24 Thread Maric Michaud
Le Sunday 24 August 2008 12:11:03 Mohamed Yousef, vous avez écrit :
 On Sun, Aug 24, 2008 at 5:54 AM, Patrick Maupin [EMAIL PROTECTED] wrote:
  On Aug 23, 7:27 pm, Mohamed Yousef [EMAIL PROTECTED] wrote:
  The problem I'm asking about is how can imported modules be aware of
  other imported modules so they don't have to re-import them (avoiding
  importing problems and Consicing code and imports )
 
  You could import sys and look at sys.modules

 no even if you imported sys or used dir you will see no re in W() from
 A . and you can even try to use re and will see

  why am i doing this in the first place I'm in the process of a medium
  project where imports of modules start to make a jungle and i wanted
  all needed imports to be in a single file (namely __init__.py) and
  then all imports are made once and other modules feel it
 
  This doesn't sound like a good idea.  If A imports a module which is
  automatically imported into B's namespace, that sounds like a
  maintenance nightmare.

 why ?
 this saves time and consices whole package imports in one file
 and maintaining it is easier 

Maintaining is easier ? Not at all, refactoring maybe but it's not a big deal, 
you must be convinced that module names are exactly as APIs, they are not 
intended to change between versions.

BTW you could do that by importing all you need in one module, say libs.py, 
and from libs import * in all you r package modules.
But htis considered a very bad practice by python developpers as PEP 8 states, 
it break on of the zen's rule : explicit is better than implicit.

Really, it is very good for reading and *maintaining* purpose to have all the 
names used in one's module be easily found in the few import statments at the 
top of the file.

In case you really need to make available a name to an arbitrary piece of code 
(not a common scheme though), you could easily add it to the __builtin__ 
module, but if you know exactly what you do.

 (eg. you will never have circuular imports) 


No, circular imports are another problem that wouldn't be resolved by your 
suggestion.

  another reason to do this that my project is offering 2 interfaces
  (Console and GUI through Qt) and i needed a general state class (
  whether i'm in Console or GUI mode) to be available for all , for
  determining state and some public functions ,and just injecting
  Imports everywhere seems a bad technique in many ways (debugging ,
  modifying ...etc )
 
  I still don't understand.

 put it another way a general variable in all modules
 and some functions visible in all modules


This has no use case in python, even builtin names are overriden by the simple 
affectation stamtent, if you want to modify a global name, you need access it 
as an attribute of a specific namespace.

glob.py:

A=0
B=0

script.py:

import glob
from glob import A

A=1
glob.B =1
print A, B, glob.A, glob.B

will print 1, 1, 0, 1.

  in PHP Require would do the trick neatly ... so is there is
  something I'm missing here or the whole technique is bad in which case
  what do you suggest ?
 
  I don't know what to suggest, in that you haven't yet stated anything
  that appears to be a problem with how Python works.  If two different
  modules import the same third module, there is no big performance
  penalty.  The initialization code for the third module is only
  executed on the first import, and the cost of having the import
  statement find the already imported module is trivial.

 it's not performance it's the jungle resulting
 many imports in every file and suppose i changed the module used in
 all modules name
 guess what , it will have to be renamed in every import to it in all
 modules --

(cf my previous comment).

-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: What is class method?

2008-08-24 Thread Maric Michaud
Le Sunday 24 August 2008 10:32:34 Hussein B, vous avez écrit :
 Hi,
 I'm familiar with static method concept, but what is the class method?
 how it does differ from static method? when to use it?
 --
 class M:
  def method(cls, x):
 pass

  method = classmethod(method)

As it has been said, it adds polymorphic behavior to static method concept by 
the mean of  a reference to the real class that called it.

[159]: class A(object) :
   .: @classmethod
   .: def factory(cls, *args) : return cls(*args)
   .:
   .:

[160]: class B(A) :
   .: def __init__(self, a, b) :
   .: print a, b
   .:
   .:

[161]: A.factory()
...[161]: __main__.A object at 0x2b88d0e33710

[162]: B.factory(1, 2)
1 2
...[162]: __main__.B object at 0x2b88d0e33bd0



It is a common advice that staticmethod should not exist in python, as they do 
nothing compared to module level functions, and we should always use 
classmethods in place of them.

 --
 Thank you for your time.
 --
 http://mail.python.org/mailman/listinfo/python-list



-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: recursively change values in list of lists

2008-08-24 Thread Maric Michaud
Le Sunday 24 August 2008 16:17:46 Carson Farmer, vous avez écrit :
 Dear list,

 I'm sure this is a relatively trivial problem, but I have been unable
 to find any good examples/explanations on how to do this, so here
 goes:

 I have multi-polygon object, which is simply a list of polygons, where
 each polygon is a list of lines, where each line is a list of points.
 What I would like to do, is change the x and y values of each point,
 all the while keeping the structure of the lists intact.


As you know the structure by advance, this is not a recusive problem :

for polygon in multi :
for line in polygon :
for point in line :
point[0] += x
point[1] += y


 So in the end, the only thing that should be changed is the values,
 not the lists themselves... clear as mud?

 Any hints, and/or suggestions are greatly appreciated,


 Carson
 --
 http://mail.python.org/mailman/listinfo/python-list



-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: exception handling in complex Python programs

2008-08-22 Thread Maric Michaud
Le Thursday 21 August 2008 09:34:47 Bruno Desthuilliers, vous avez écrit :
  The point
  is that EAFP conflicts with the interest of reporting errors as soon
  as possible (on which much has been written see, for instance Ch. 8 -
  Defensive Programming in Code Complete),

 Defensive programming makes sense in the context of a low-level language
    like C where errors can lead to dramatic results. In high-level
 languages like Python, the worse thing that an unhandled exception can
 cause is an abrupt termination of the process and a nice traceback on
 screen.

... and leave your datas in inconsistent state. So, what C or any other 
language could do worse to your application ?

 In this context, defensive programming is mostly a waste of time 
 - if you can't *correctly* handle the exception where it happens, then
 doing nothing is the better solution.

If I don't buy the argument I actually agree with the conclusion. Each 
component of a program should try to manage only errors tied to their own 
logic and let pass others up to the gui logic for rendering errors the good 
way, persistence logic to rollback unwanted changes, and application logic to 
continue execution the right way. This is hard to do in C because you have no 
way to trap an error which happen randomly in the program, ie. a segfault 
will interrupt the execution anyway.

-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: exception handling in complex Python programs

2008-08-22 Thread Maric Michaud
Le Friday 22 August 2008 15:03:21 Bruno Desthuilliers, vous avez écrit :
 Maric Michaud a écrit :
  Le Thursday 21 August 2008 09:34:47 Bruno Desthuilliers, vous avez écrit :
  The point
  is that EAFP conflicts with the interest of reporting errors as soon
  as possible (on which much has been written see, for instance Ch. 8 -
  Defensive Programming in Code Complete),
 
  Defensive programming makes sense in the context of a low-level language
 like C where errors can lead to dramatic results. In high-level
  languages like Python, the worse thing that an unhandled exception can
  cause is an abrupt termination of the process and a nice traceback on
  screen.
 
  ... and leave your datas in inconsistent state.

 Not all applications persist data, so this is an application-specific
 problem, to be solved at the application level - IOW, there's no
 one-size-fits-all solution here.

... or lose open connection, reset sessions, etc.. it doesn't really matter 
what is lost when a program crash, if it can be restarted without changes it 
is hardly what I'd qualify a dramatic result. It doesn't depend on the 
language what implications of an unhandled error are, It is always 
application-specific.

 Anyway: transactions management is not 
 what I meant when talking about defensive programming.

 As far as I'm concerned, the only place where the defensive approach
 really makes sense whatever the language is when dealing with external
 datas (user inputs etc).


I agree, this is my whole point, whatever the language is.

  So, what C or any other
  language could do worse to your application ?

 An error in a C program can do *way* worse than leave an application's
 data in inconsistent state. See ART for more on this:
 http://dialspace.dial.pipex.com/prod/dialspace/town/green/gfd34/art/


I didn't read dramatic results in that sense, but with the meaning of a 
result that the program itself cannot handle.

If the whole system crash due to an unhandled error in a program, once 
missile's dropped it doesn't really matter in which language it was written. 
Reliability of a system, as high-availability of an application, is mostly a 
problem beyond the scope of application level error checking.

  In this context, defensive programming is mostly a waste of time
  - if you can't *correctly* handle the exception where it happens, then
  doing nothing is the better solution.
 
  If I don't buy the argument

 cf above - maybe you buy it after all ?-)

  I actually agree with the conclusion. Each
  component of a program should try to manage only errors tied to their own
  logic and let pass others up to the gui logic for rendering errors the
  good way, persistence logic to rollback unwanted changes, and application
  logic to continue execution the right way. This is hard to do in C
  because you have no way to trap an error which happen randomly in the
  program, ie. a segfault will interrupt the execution anyway.

 Indeed.
 --
 http://mail.python.org/mailman/listinfo/python-list



-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: Filling in Degrees in a Circle (Astronomy)

2008-08-22 Thread Maric Michaud
Le Saturday 23 August 2008 01:12:48 W. eWatson, vous avez écrit :
 The other night I surveyed a site for astronomical use by measuring the
 altitude (0-90 degrees above the horizon) and az (azimuth, 0 degrees north
 clockwise around the site to 360 degrees, almost north again) of obstacles,
 trees. My purpose was to feed this profile of obstacles (trees) to an
 astronomy program that would then account for not sighting objects below
 the trees.

 When I got around to entering them into the program by a file, I found it
 required the alt at 360 azimuth points in order from 0 to 360 (same as 0).
 Instead I have about 25 points, and expected the program to be able to do
 simple linear interpolation between those.

 Is there some simple operational device in Python that would allow me to
 create an array (vector) of 360 points from my data by interpolating
 between azimuth points when necessary? All my data I rounded to the nearest
 integer. Maybe there's an interpolation operator?

 As an example, supposed I had made 3 observations: (0,0) (180,45) and
 (360,0). I would want some thing like (note the slope of the line from 0 to
 179 is 45/180 or 0.25):
 alt: 0, 0.25, 0.50, 0.75, ... 44.75, 45.0
 az : 0, 1,2,3,  180

 Of course, I don't need the az.

Not sure I got it, but is that fulfill your specs ?

[20]: def interpolate(a, b) :
slope = float(b[1] - a[1]) / (b[0] - a[0])
return [ slope * float(i) for i in xrange(b[0]-a[0] + 1) ]
   :

[23]: interpolate((0, 0), (180, 45))
...[23]:
[0.0,
 0.25,
 0.5,
 0.75,

 44.5,
 44.75,
 45.0]

[29]: interpolate((80, 20), (180, 45))
[0.0,
 0.25,
 0.5,
 0.75,
 1.0,
 1.25,
...
 24.5,
 24.75,
 25.0]



-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: print %s

2008-08-18 Thread Maric Michaud
Le Monday 18 August 2008 09:27:33 Bruno Desthuilliers, vous avez écrit :
 Cameron Simpson a écrit :
  On 18Aug2008 11:58, Beema Shafreen [EMAIL PROTECTED] wrote:
  | In my script i have to print a series of string , so
  |
  | print %s\t%s\t%s\t%s\t%s\t%s\t %(a,v,t,R,s,f)
  |
  | I need to know instead of typing so many %s  can i write %6s in python,
  | as we do in C progm.
 
  I hate to tell you this, but %6s in C does NOT print 6 strings. It
  prints 1 string, right justified, in no less that 6 characters.
  C is just like Python in this example.
 
  | What are the other options .
 
  Write a small loop to iterate over the strings. Print a tab before each
  string except the first.

 Or use the str.join method:

 print \t.join(list(avtRsf))


Not related to OP's question, but why one would want to convert a string to a 
list to make it iterable ?


[3]: print '\t'.join('azerty')
a   z   e   r   t   y



 --
 http://mail.python.org/mailman/listinfo/python-list



-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python does not get environment variable when using cron.

2008-08-18 Thread Maric Michaud
Le Monday 18 August 2008 07:11:59 Cameron Simpson, vous avez écrit :
 You're probably confused by the fact that cron does not invoke login
 shells (with their associated initialisation from /etc/profile and
 $HOME/.profile).

This can be solved by invoking /bin/bash -l -c ..., the -l option forces a 
login shell even in non-interactive session.

-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: help with parsing email

2008-08-18 Thread Maric Michaud
Le Monday 18 August 2008 12:43:02 Gabriel Genellina, vous avez écrit :
 En Thu, 14 Aug 2008 12:50:57 -0300, Ahmed, Shakir [EMAIL PROTECTED] 
escribió:
  I need to grab/parse numeric numbers such as app number from incoming
  emails stored in Microsoft Outlook (Microsoft Exchange server) with
  specified subject line.
 
  The email body is like this
 
  myregion ; tst ; 11-Aug-2008
 
  http://my.xyz.com//content/ifs/apps/myDocFolder/NoticeOfapplication/0
 80612-21_test_337683.pdf
 
  I need to extract 080612-21_ number from above line from incoming
  emails.

 I can't help with the part dealing with Outlook - but once you retrieved
 the email body, it's easy:

Three options here :

- dealing directly with outlook mailboxes files, I used some open source code 
to parse .pst files in past, but you'll need some googling to match your 
requirements. Doesn't help if there is an instance of outlook running on the 
box.

- use the outlook COM or even some windows MAPI interface via WIN32 API 
(pywin32), can't help with this one.

- access emails directly on the Exchange server via standard python modules 
poplib or imaplib, my preferred choice if one of these protocols are 
supported by your environment. You won't need no extensions, just a standard 
python installation, and your code will work with most mail delivery agent 
and will run on all python supported platform.

-- 
_

Maric Michaud

--
http://mail.python.org/mailman/listinfo/python-list


Re: Dynamically defined functions via exec in imported module

2008-08-16 Thread Maric Michaud
Le Saturday 16 August 2008 06:50:02 Michele Simionato, vous avez écrit :
 On Aug 16, 4:48 am, Nadeem [EMAIL PROTECTED] wrote:
  I understand the 99% rule... the example I gave was to simplify the
  issue. The full thing I'm working on is a library for an introductory
  CS class I'm teaching. I'm trying, essentially, to build a library of
  macros for students to use allowing them to define records (like
  structs in C) with selector functions.

 The namedtuple recipe by Raymond Hetting does
 exactly that and, guess what, it uses exec!

It uses exec, but could not, and IMO, should not, all of the cooking could be 
done in the closure. I join version of it without exec. Of course the only 
drawback is that you need to do the job of argument checking in __new__ and 
can't rely on a dynamically compiled arglist. But it is a common idiom I 
think.

 Also the doctest module in the standard library
 uses exec at good effect. So, I would say it is
 not a 99% rule, let's say it is a 98% rule ;)

Yes, but in doctest, exec is used for what it is intended to be use, execute 
arbitrary code provided by the user, this is legal and perfect use of exec 
because here the user is a trusted one (the programer himself).

I'd say that everywhere exec/eval are used in a application/function/lib that 
doesn't mean to interpret arbitrary and user provided python code, it is a 
bad usage.




-- 
_

Maric Michaud


namedtuple.py
Description: application/python
--
http://mail.python.org/mailman/listinfo/python-list

Re: Trying ZODB, background in Relational: mimic auto_increment?

2008-08-15 Thread Maric Michaud
Le Thursday 14 August 2008 20:14:19 Christian Heimes, vous avez écrit :
 Diez B. Roggisch wrote:
  But that's a great deal more complicated! It requires merging, and while
  this of course is possible, you should have mentioned it because
  otherwise the OP might run into problems.

 A lock doesn't do the job at all. ZODB supports clustering through ZEO.
 Multiple ZEO clients can be attached to a single or more ZODB servers.
 Any kind of thread based locking is local to each ZEO client.

 You have to sync a sequence generator across multiple ZEO clients, hook
 into the two phase commit protocol or use the conflict resolution hooks.

You don't have too if you manage the conflict error anywhere else in the 
program just manage the exception ConflictError and let the user retry later, 
that will not happen that much on most application.

But,

 The problem is more complex than you think.


Not that complex, strength of ZODB is right here, this entirely covered by the 
introduction to zodb (ZODB/ZEO programming guide), see userdb and chatter 
examples.

 Christian

 --
 http://mail.python.org/mailman/listinfo/python-list



-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: Why nested scope rules do not apply to inner Class?

2008-08-13 Thread Maric Michaud

Le Tuesday 12 August 2008 23:15:23 Calvin Spealman, vous avez écrit :
 The issue has been brought up several times before. There have been
 proposals to make nested classes better supported, but they are always
 shut down. The vote is always against it. Your metaclass example is
 one thing, but its not really applicable here. I've never seen or
 would encourage nested classes even in that case. The metaclass is
 typically defined on its own, and then referenced by one or more
 classes that use it. Defining it as a nested class saves you one line
 of code, but IMHO makes the result just a bit more cluttered, while
 reducing the elegance of reusing the metaclass.

 Here are only a few examples of threads giving good reasons against
 class nesting. I've never seen any good arguments for it. There are
 dozens of good reasons we don't encourage it and won't actively
 support it.

 http://mail.python.org/pipermail/python-dev/2005-March/052454.html
 http://mail.python.org/pipermail/python-dev/2002-November/029872.html


I was not aware of any nested classes are unsupported before and didn't 
consider nested classes as bad practice till now, even with the pickle 
limitation (not every class are intended to be pickled), more you didn't give 
any evidence or any pertinent quote of this and at least one of Guido in the 
above threads seems contradict you :
http://mail.python.org/pipermail/python-dev/2002-November/029906.html

BTW my concern was not really about nested classes but the asymetry between 
function scope and classes scope, there are at least two examples that come 
to my mind and for which I don't see good argument to keep the situation as 
it is. First is the very odd behavior of genexp, second is the dynamic type 
factory, I know it's not a big deal to in each case and can live with this, 
but these pieces of code fail unexpectedly while even a experimented python 
programmer would have thought they work at first glance, and the explanation 
of why they fail to a newcomer is not easy (specially for the first)

(1) this fail  only on the last line while it would have work well with 
listcomp and works well in a function definition
class A(object) :
l = range(5)
m = list(a+b for a in l for b in range(5))
n = list(a+b for a in range(5) for b in l)

(2) This won't work as it would with nested functions, you need to build the 
new calss directly with type('dynamic', (object,), {type_: type_})

def create_type(type_) :
class dynamic(object) :
type_ = type_
return dynamic


-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list

Re: Why nested scope rules do not apply to inner Class?

2008-08-13 Thread Maric Michaud
Le Wednesday 13 August 2008 21:04:30 Rhamphoryncus, vous avez écrit :
 class X(object):
     foo = 42

     def bar(self):
         print foo
         print self.foo
 X.foo = 7

Yes this is a problem, function could not bind their free vars to the class 
namespace without introducing a subtle incompatibility with actual code.

This is exactly this case which would be a problem (your example raise an 
error and is not exactly a running code):

G = 1
class A(object) :
G = 0
def f(self) : return G

-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: Why nested scope rules do not apply to inner Class?

2008-08-13 Thread Maric Michaud

Calvin Spealman a écrit :

On Wed, Aug 13, 2008 at 7:41 PM, Maric Michaud [EMAIL PROTECTED] wrote:

I was not aware of any nested classes are unsupported before and didn't
consider nested classes as bad practice till now, even with the pickle
limitation (not every class are intended to be pickled), more you didn't give
any evidence or any pertinent quote of this and at least one of Guido in the
above threads seems contradict you :
http://mail.python.org/pipermail/python-dev/2002-November/029906.html


Of course, I don't have to agree with Guido,


neither do I, for sure.


but I don't really think
his comment there counts as some encouragement of the practice. It
also isn't an example that actually demonstrates the things people
misuse about nesting classes, although its definitely an example that
I think could be done much better without them, at all.



Yes this example is some sort of metalanguage using class statment in a 
derived way, It comes in mind sometimes by the fact there is no other 
way to extend the language to make DSL in python (perhaps easy extend 
will be popular one day, but I doubt). I did something like that before 
in an application and won't encourage anyone to do so.



The use of nested classes to define some hierarchical structure is
subject, at best. And, the examples I see aren't even hierarchical.
The nesting doesn't seem to gain them anything. As a matter of fact,
wouldn't that example be a lot more clear with one class and some
Attribute properties describing the structure?

What rubs me the wrong way about nested classes is that they aren't
even real classes. That is, they aren't being used to instantiate
instances, but just as dummy scopes to hold some attributes. They are
abusing classes as property bags.



Here, I disagree, the helper classes example is good IMO, defining an 
Iterator as an inner class and referencing it by MyIterable.Iterator is 
good style.


That said, I understood from your first post that python community in 
general will discourage nested classes as bad practice and consider them 
not supported by the language, statment for which I can't find or 
remember no evidence.



BTW my concern was not really about nested classes but the asymetry between
function scope and classes scope, there are at least two examples that come
to my mind and for which I don't see good argument to keep the situation as
it is. First is the very odd behavior of genexp, second is the dynamic type
factory, I know it's not a big deal to in each case and can live with this,
but these pieces of code fail unexpectedly while even a experimented python
programmer would have thought they work at first glance, and the explanation
of why they fail to a newcomer is not easy (specially for the first)

(1) this fail  only on the last line while it would have work well with
listcomp and works well in a function definition
class A(object) :
   l = range(5)
   m = list(a+b for a in l for b in range(5))
   n = list(a+b for a in range(5) for b in l)


I can admit that this behavior can be surprising, but it isn't
something I really see cropping up as a problem. How often do you see
that you need to have any kind of procedural behavior in a class body?
In other words, in all but rare cases, the order of the class body
lines shouldn't change the resulting class, but this example breaks
that expectation.



I wrote something like that a few months ago, and found it both simple 
concise and elegant :


class test_suite1(test_suite) :
roles = (role1, ...)
actors = (actor1, ...)
use_cases = [ (actor, role) for actor in actors for role in roles ]

which I wrote at first tuple((actor, role) for ...) and came to the problem.

Of course use_cases can be made a true generator, but in this particular 
case building the tuple/list together with the class is good enough.



(2) This won't work as it would with nested functions, you need to build the
new calss directly with type('dynamic', (object,), {type_: type_})

def create_type(type_) :
   class dynamic(object) :
   type_ = type_
   return dynamic


This is simply completely invalid. That code at the beginning of any
body will _always_ raise either an UnboundLocalError or a NameError,
because you are assigning to the same name you are dereferencing. That
line will always be either an error or a no-op, anywhere it appears.
It has nothing to do with nesting of any sort.



Yes, of course, sorry, it should be :

def create_type(t) :
class dynamic(object) :
type_ = t
return dynamic

but this work to my surprise ! I just thought the t in the class body 
would be looked up in global namespace. The design is that free vars 
doesn't bind to class namespaces not that free vars of class 
namespaces bind to global namespace, my mistake.



--
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list






--
http://mail.python.org/mailman/listinfo/python-list


Re: Why nested scope rules do not apply to inner Class?

2008-08-12 Thread Maric Michaud
Le Tuesday 12 August 2008 11:29:18 Cousson, Benoit, vous avez écrit :
 Hi,

 I'd like to be able to use a nested class (C1) from another sibling nested
 class (C3). This looks very similar to the nested scopes of functions
 except that it does not work.

 class A(object):
   pass

 class B(object):

   class C1(object):
   pass

   class C2(C1):
   foo = A

   class C3(object):
   foo = C1

 The funny thing is that C2 can inherit from C1 but C3 cannot reference C1.
 B.C1 does not work either, but in that case it makes sense since B is still
 being defined. Is this a language limitation or something that does not
 make sense at all?


This is a language limitation.
This is because nested scope is implemented for python function only since 2.3 
allow late binding of free variables. the scope in class statment is not a 
closure, so there is only two possible scope in it : local and global.

When class C2(C1): statment is interpreted, it is in the scope of class B 
for which a name C1 exists, but it doesn't within the new scope of class C2 
(the future C2.__dict__).

If you really need such a nested thing, this could help :

[39]: class A(object) :
class B(object) : pass
C = type('C', (B,), {'ref' : B})
   :

[29]:

[30]: A
...[30]: class '__main__.A'

[31]: A.B
...[31]: class '__main__.B'

[32]: A.C
...[32]: class '__main__.C'

[33]: A.C.__bases__
...[33]: (class '__main__.B',)

[34]: A.C.ref
...[34]: class '__main__.B'





 I'm wondering as well if the new nonlocal statement will fix that in py3k?


nonlocal doesn't adress this issue an I doubt python 3.0 fix it at all.

You have the same problem with generator expressions, which bind lately outer 
loop variables :

[70]: def f() :
l = list( a for a in range(5) )
return list( e + f for f in range(5) for e in l )
   :

[73]: list(f())
...[73]: [0, 1, 2, 3, 4, 1, 2, 3, 4, 5, 2, 3, 4, 5, 6, 3, 4, 5, 6, 7, 4, 5, 6, 
7, 8]

[74]: class A(object) :
l = list( a for a in range(5) )
m = [ e + f for f in range(5) for e in l ]
   :
   :

[77]: A.m
...[77]: [0, 1, 2, 3, 4, 1, 2, 3, 4, 5, 2, 3, 4, 5, 6, 3, 4, 5, 6, 7, 4, 5, 6, 
7, 8]

[78]: class A(object) :
l = list( a for a in range(5) )
m = list( e + f for f in range(5) for e in l )
   :
   :
---
NameError Traceback (most recent call last)

/home/maric/ipython console in module()

/home/maric/ipython console in A()

/home/maric/ipython console in genexpr((f,))

NameError: global name 'l' is not defined


-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: simple error i hope

2008-08-02 Thread Maric Michaud
Le Saturday 02 August 2008 00:51:50 Clay Hobbs, vous avez écrit :
 It is also a good idea to open files with the open() function and not the
 file() function.  They do the exact same thing, and take the exact same
 parameters, just open() makes your code a bit more readable (or at least
 that's what I think).

The main point with open I think is to easy later evolution. While you could 
override file name as well as open by a custom funcion, it's a bad idea 
to override the name of the type because a simple isinstance(file_, file) 
won't work anymore if you do so.


-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: Strong/weak typing

2008-08-01 Thread Maric Michaud
Le Friday 01 August 2008 17:31:25 [EMAIL PROTECTED], vous avez écrit :
 I'm writing Python as if it were strongly typed, never recycling a
 name to hold a type other than the original type.

 Is this good software engineering practice, or am I missing something
 Pythonic?

As already stated by others, Python is strongly typed, the fact is that the 
type of an object is embeded with the object itself, not by the name you give 
him at different place in a program.

But in fact you effectively re-use names in your programs, it's the basic of 
the language.

Think of the following lines :

a, b = 0, 1
a += b
a, b = b, a

s = foo
s = s.upper()

-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: Difference between type and class

2008-07-31 Thread Maric Michaud
Le Thursday 31 July 2008 14:30:19 Nikolaus Rath, vous avez écrit :
 oj [EMAIL PROTECTED] writes:
  On Jul 31, 11:37 am, Nikolaus Rath [EMAIL PROTECTED] wrote:
  So why does Python distinguish between e.g. the type 'int' and the
  class 'myclass'? Why can't I say that 'int' is a class and 'myclass'
  is a type?
 
  I might be wrong here, but I think the point is that there is no
  distinction. A class (lets call it SomeClass for this example) is an
  object of type 'type', and an instance of a class is an object of type
  'SomeClass'.

 But there seems to be a distinction:
  class int_class(object):

 ...   pass
 ...

  int_class

 class '__main__.int_class'

  int

 type 'int'

 why doesn't this print

  class int_class(object):

 ...   pass
 ...

  int_class

 type '__main__.int_class'

  int

 type 'int'

 or

  class int_class(object):

 ...   pass
 ...

  int_class

 class '__main__.int_class'

  int

 class 'int'

 If there is no distinction, how does the Python interpreter know when
 to print 'class' and when to print 'type'?


There are some confusion about the terms here.

Classes are instances of type 'type', but types are both instances and 
subclasses of 'type'. 
This recursivity is the base of the object model.

An instance of 'type' is a class (or a new type), but instances of a classes 
are not. 'type' is a metatype in term of OO.

What the type int means is that int is not a user type but a builtin type, 
instances of int are not types (or classes) but common objects, so its nature 
is the same as any classes.

The way it prints doesn't matter, it's just the __repr__ of any instance, and 
the default behavior for instances of type is to return 'class XX', but it 
can be easily customized.

[1]: class A(object) :
   ...: class __metaclass__(type) :
   ...: def __repr__(self) : return type A
   ...:
   ...:

[2]: A
...[2]: type A

[3]: type('toto', (object,), {})
...[3]: class '__main__.toto'



-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list

Re: Difference between type and class

2008-07-31 Thread Maric Michaud
Le Thursday 31 July 2008 13:32:39 Thomas Troeger, vous avez écrit :
 De :
 Thomas Troeger [EMAIL PROTECTED]  (Aioe.org NNTP Server)
   À :
 python-list@python.org
   Date :
 Aujourd'hui 13:32:39
    

  Can someone explain to me the difference between a type and a class?

 If your confusion is of a more general nature I suggest reading the
 introduction of `Design Patterns' (ISBN-10: 0201633612), under
 `Specifying Object Interfaces'.

 In short: A type denotes a certain interface, i.e. a set of signatures,
 whereas a class tells us how an object is implemented (like a
 blueprint). A class can have many types if it implements all their
 interfaces, and different classes can have the same type if they share a
 common interface. The following example should clarify matters:


Of course, this is what a type means in certain literature about OO 
(java-ish), but this absolutely not what type means in Python. Types are a 
family of object with a certain status, and they're type is type, 
conventionnaly named a metatype in standard OO.

There are three sort of objects in Python, in an inclusive order :

- ordinary object, or instance, they could not be instantiated or subclassed 
(in general), and are all an instance of type object (or a subclass of it).

- types, or classes, are all instance of type 'type' (or a subclass of it), 
they can be instantiated and they produce objects (ordinary object in 
general) with theirslef as a type.

- metatypes or metaclass, are subclasses of type, their instances are new 
types.

For all tjis work together you must admit the following recursivity :

'type' is both a subclass and an instance of 'object' while 'object' is an 
instance of 'type'. 

-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: Difference between type and class

2008-07-31 Thread Maric Michaud
Le Thursday 31 July 2008 16:46:28 Nikolaus Rath, vous avez écrit :
 Maric Michaud [EMAIL PROTECTED] writes:
   Can someone explain to me the difference between a type and a class?
 
  If your confusion is of a more general nature I suggest reading the
  introduction of `Design Patterns' (ISBN-10: 0201633612), under
  `Specifying Object Interfaces'.
 
  In short: A type denotes a certain interface, i.e. a set of signatures,
  whereas a class tells us how an object is implemented (like a
  blueprint). A class can have many types if it implements all their
  interfaces, and different classes can have the same type if they share a
  common interface. The following example should clarify matters:
 
  Of course, this is what a type means in certain literature about OO
  (java-ish), but this absolutely not what type means in Python. Types
  are a family of object with a certain status, and they're type is
  type, conventionnaly named a metatype in standard OO.

 [...]

 Hmm. Now you have said a lot about Python objects and their type, but
 you still haven't said what a type actually is (in Python) and in what
 way it is different from a class. Or did I miss something?

This paragraph ?


- types, or classes, are all instance of type 'type' (or a subclass of it), 
they can be instantiated and they produce objects (ordinary object in 
general) with theirslef as a type.


Maybe it's still unclear that types and classes *are* synonyms in Python.

-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list

Re: Difference between type and class

2008-07-31 Thread Maric Michaud
Le Thursday 31 July 2008 17:00:51 Nikolaus Rath, vous avez écrit :
  There are some confusion about the terms here.
 
  Classes are instances of type 'type',

 Could you please clarify what you mean with 'instance of type X'? I
 guess you mean that 'y is an instance of type X' iif y is constructed
 by instantiating X. Is that correct?


Correct, you can verify this with the isinstance builtin :

[1]: isinstance(int(), int)
...[1]: True

[2]: isinstance(int, object)
...[2]: True

[3]: isinstance(int, type)
...[3]: True

[4]: class A(object) : pass
   ...:

[5]: isinstance(A, type)
...[5]: True



  What the type int means is that int is not a user type but a
  builtin type, instances of int are not types (or classes) but common
  objects, so its nature is the same as any classes.
 
  The way it prints doesn't matter, it's just the __repr__ of any instance,
  and the default behavior for instances of type is to return 'class XX',
  but it can be easily customized.

 But 'int' is an instance of 'type' (the metaclass):
  int.__class__

 type 'type'

 so it should also return 'class int' if that's the default behavior
 of the 'type' metaclass.


The fact that a class is an instance of type, which it is always true, doesn't 
mean its metaclass is type, it could be any subclass of type :

[6]: class A(object) :
   ...: class __metaclass__(type) :
   ...: def __repr__(self) : return type A
   ...:
   ...:

[7]: isinstance(A, type)
...[7]: True

[8]: A.__class__
...[8]: class '__main__.__metaclass__'

[9]: issubclass(A.__class__, type)
...[9]: True


 I think that to get 'type int' one would have to define a new
 metaclass like this:

 def type_meta(type):
     def __repr__(self)
          return type %s % self.__name__

 and then one should have int.__class__ == type_meta. But obviously
 that's not the case. Why?

 Moreover:
  class myint(int):

 ...    pass
 ...

  myint.__class__ == int.__class__

 True


*is* comparaison fits better here.

  int

 type 'int'

  myint

 class '__main__.myint'

 despite int and myint having the same metaclass. So if the
 representation is really defined in the 'type' metaclass, then
 type.__repr__ has to make some kind of distinction between int and
 myint, so they cannot be on absolute equal footing.

You're right, type(int) is type, the way it renders differently is a detail of 
its implementation, you can do things with builtin types (written in C) you 
coudn't do in pure python, exactly as you couldn't write recursive types 
like 'object' and 'type'.


-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list

Re: Boolean tests [was Re: Attack a sacred Python Cow]

2008-07-30 Thread Maric Michaud
Le Tuesday 29 July 2008 23:48:31 [EMAIL PROTECTED], vous avez écrit :
 Here's a function, print_members.  It's just something that takes some
 iterable and prints its members in XML.  It's special-cased so that an
 empty iterable gets an empty tag.  (This is a bit of a trivial
 example, I admit; the point is that the empty iterable is a special
 case.)

 def print_members(iterable):
     if not iterable:
         print 'members /'
         return
     print 'members'
     for item in iterable:
         print 'item%s/item' % item
     print '/members'


... 
 So print_members can work on iterables that have no len(), and handle
 the special case of an empty iterable, as long as __nonzero__ is
 implemented.


But iterables can have no notion of emptiness too :

[25]: print_members((e for e in range(0)))
members
/members

Your function is just wrong assuming that, it should be written :

[31]: def print_members(iterable):
print 'members',
empty = True
for item in iterable:
if empty :
empty = False
print ''
print 'item%s/item' % item
print empty and '/' or '/members'
   :
   :

[40]: print_members((e for e in range(0)))
members /

[41]: print_members((e for e in range(1)))
members 
item0/item
/members



 Counterexample:

 While if x works well in some circumstances, I don't like using it
 for purely numeric types.  For instance, I have a mutable Signal class
 for doing some basic DSP.  Among other things, I can apply a DC offset
 to the signal (it just adds the offset to all the samples).  I have a
 special case for an offset of 0 so I don't have to loop through all
 the samples (I also have a two-pass remove_offset method that
 subtracts the average; if it's already properly centred, I can skip a
 step).

 class Signal:
     [...]
     def dc_offset(self, amount):
         if amount == 0:
             return
         self.samples = [sample + amount for sample in self.samples]

This function is also wrong assuming that because amount compare to zero, it 
can be added to sample.

If you want to make type checking just check the type or convert your 
parameter to an int, but the test == 0 is of no help here.

The only valuable point I see for this idiom is to make more explicit I am 
testing a numerical value.
But your example is a good objection to this, with well chosen name, ie. 
amount, it's quite clear if not amount : is testing the zero value of a 
numerical value.

-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: Boolean tests [was Re: Attack a sacred Python Cow]

2008-07-30 Thread Maric Michaud
Le Wednesday 30 July 2008 15:31:28 [EMAIL PROTECTED], vous avez écrit :
   class Signal:
       [...]
       def dc_offset(self, amount):
           if amount == 0:
               return
           self.samples = [sample + amount for sample in self.samples]
 
  This function is also wrong assuming that because amount compare to zero,
  it can be added to sample.

 Not quite.  I'm assuming that if amount equals 0, then amount is some
 scalar.  In fact, only if that comparison fails will I get my
 exception: since [] != 0, it will then try to add sample + [], which
 will raise TypeError.

  If you want to make type checking just check the type or convert your
  parameter to an int, but the test == 0 is of no help here.

 I'm not going for type checking here because I want Signal to support
 int and float samples (and numpy.int16, c.).

Ok, but the fact that amount == 0 passes, doesn't ensure you didn't a 
programming error, this add just some relative robustness (protect you from 
passing an empty list). And a reader would probably not see your intention 
here (he already expect a scalar due to the name of the variable).

This is exactly the problem ABC is intended to solve.

Without ABC, to explicitly ensure amount is a scalar, just doing a int(amount) 
or int(abs(amount)) if you want to deal with complex numbers too, at the 
begining of the function is a common idiom.

-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: Pointers/References in Python?

2008-07-30 Thread Maric Michaud
Le Wednesday 30 July 2008 16:46:37 [EMAIL PROTECTED], vous avez écrit :
 Hello,

 I have a long list of memory-heavy objects that I would like to access
 in differently sorted order. Let's say I'd like to have lists called
 by_date or by_size that I can use to access the objects in the
 specified order.

...
 I know there is a thing called shallow copy that has something to do
 with not duplicating memory content but I don't understand the
 concept. Maybe that's what would help here, too.


In fact, all containers in python contains references of objects.

[54]: a = [1]

[55]: b = [a]

[56]: c = list(sorted(b))

[57]: b, c
...[57]: ([[1]], [[1]])

[58]: b.append(3)

[59]: b, c
...[59]: ([[1], 3], [[1]])

[60]: a.append(0)

[61]: b, c
...[61]: ([[1, 0], 3], [[1, 0]])

This is if you want to make a true copy (called deep copy) that you'll have to 
do extra steps (using copy module for example).

-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: Standard module for parsing emails?

2008-07-30 Thread Maric Michaud
Le Wednesday 30 July 2008 17:15:07 Phillip B Oldham, vous avez écrit :
 If there isn't a standard library for parsing emails, is there one for
 connecting to a pop/imap resource and reading the mailbox?
 --
 http://mail.python.org/mailman/listinfo/python-list

There are both shipped with python, email module and poplib, both very well 
documented in the official doc (with examples and all).

email module is rather easy to use, and really powerful, but you'l need to 
manage yourself the many ways email clients compose a message, and broken php 
webmails that doesn't respect RFCs (notably about encoding)...

-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: Standard module for parsing emails?

2008-07-30 Thread Maric Michaud
Le Wednesday 30 July 2008 17:55:35 Aspersieman, vous avez écrit :
 For parsing the mails I would recommend pyparsing.

Why ? email module is a great parser IMO.

-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: Standard module for parsing emails?

2008-07-30 Thread Maric Michaud
Le Wednesday 30 July 2008 19:25:31 Diez B. Roggisch, vous avez écrit :
 Maric Michaud wrote:
  Le Wednesday 30 July 2008 17:55:35 Aspersieman, vous avez écrit :
  For parsing the mails I would recommend pyparsing.
 
  Why ? email module is a great parser IMO.

 He talks about parsing the *content*, not the email envelope and possible
 mime-body.

Yes ? I don't know what the OP want to do with the content, but if it's just 
filtering the lines begining with a '', pyparsing might be a bit 
overweighted.

-- 
_

Maric Michaud

--
http://mail.python.org/mailman/listinfo/python-list

Re: multiple inheritance and __getattr__

2008-07-29 Thread Maric Michaud
Le Monday 28 July 2008 16:48:09 Enrico, vous avez écrit :
 Hi there,
 I have the following situation (I tryed to minimize the code to concentrate

 on the issue):
  class A(object):

  def __getattr__(self, name):
   print 'A.__getattr__'
   if name == 'a': return 1
   raise AttributeError('%s not found in A' % name)

  class B(object):

  def __getattr__(self, name):
   print 'B.__getattr__'
   if name == 'b': return 1
   raise AttributeError('%s not found in B' % name)

 Both classes have a __getattr__ method.

 Now I want to have a class that inherits from both so I write:
  class C(B,A):

  pass

 The problem arise when I try something like this:
  c=C()
  c.a

 A.__getattr__
 1

  c.b

 A.__getattr__

 Traceback (most recent call last):
   File pyshell#47, line 1, in module
 c.b
   File pyshell#42, line 5, in __getattr__
 raise AttributeError('%s not found in A' % name)
 AttributeError: b not found in A

 I was expecting, after a fail in A.__getattr__,  a call to the __getattr__
 method of B but it seems that after A.__getattr__ fails the exception stops
 the flow. So, if I did understand well, B.__getattr__ will be never called
 automatically. I don't know if this has a reason, if it is a design
 choice or what else, any explanation is welcome.


No getattr is a lookup fallback, classes which implement them in a 
non-collaborative way are unlikely to be used for multiple inheritance.
Given how multiple inheritance work and __getattr__ semantic, I was surprised 
it is not that easy to figure out how it could work in a collaborative, and 
how far fromm this are common implementation of __getattr__.
Normally they should be implemented like that :

[89]: class A(object) :
def __getattr__(self, name) :
if name == 'a' : return 'a'
try : g = super(A, self).__getattr__
except : raise AttributeError('no more __getattr__')
return g(name)
   :
   :

[95]: class B(object) :
def __getattr__(self, name) :
if name == 'b' : return 'b'
try : g = super(B, self).__getattr__
except : raise AttributeError('no more __getattr__')
return g(name)
   .:
   .:

[101]: class C(A, B) :
def __getattr__(self, name) :
if name == 'c' : return 'c'
try : g = super(C, self).__getattr__
except : raise AttributeError('no more __getattr__')
return g(name)
   .:
   .:

[107]: C().a
...[107]: 'a'

[108]: C().b
...[108]: 'b'

[109]: C().c
...[109]: 'c'

[110]: C().k
---
AttributeErrorTraceback (most recent call last)

/home/maric/ipython console in module()

/home/maric/ipython console in __getattr__(self, name)

/home/maric/ipython console in __getattr__(self, name)

/home/maric/ipython console in __getattr__(self, name)

AttributeError: no more __getattr__


 Since A and B are not written by me I can only work on C. The solution that
 comes to my mind is to define a __getattr__ also in C and write something

 like:
  class C(A,B):

  def __getattr__(self, name):
   try:
return A.__getattr__(self, name)
   except AttributeError:
return B.__getattr__(self, name)

  c=C()
  c.a

 A.__getattr__
 1

  c.b

 A.__getattr__
 B.__getattr__
 1

 A better solution is welcome.

There is no way to repair those clases for mulitple inheritance except monkey 
patching them. 
The idea of this patch would be :

def collaborative_getattr(class_, old_name) :
old_one = getattr(class_, old_name)
def __getattr__(self, name) :
try : return old_one(self, name)
except AttributeError :
try : g = super(class_, self).__getattr__
except : raise AttributeError('no more __getattr__')
return g(name)

if not getattr(C, '_C_fixed__', False) :
C._C_fixed__ = C.__getattr__
C.__getattr__ = collaborative_getattr(C, '_C_fixed__')


That said, if your class C is a real facade for its ancestors A and B (A and B 
won't appear at all in the hierarchies of your subclasses), your solution is 
near the best one in terms of simplicity-efficiency. I said near the best one 
because your __getattr__ isn't collaborative yet ! :).

-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: Where is the correct round() method? Use math.ceil

2008-07-28 Thread Maric Michaud
Le Monday 28 July 2008 02:35:08 Herman, vous avez écrit :
 Where is the correct round() method?
 Hello,

  I need a round function that _always_ rounds to the higher integer if
  the argument is equidistant between two integers. In Python 3.0, this
  is not the advertised behavior of the built-in function round() as

  seen below:
   round(0.5)

  0

   round(1.5)

  2

   round(2.5)

  2

Hmm, I don't have the same result in python2.6, I suspect it's a floating 
point precision problem, try just to type 0.5 in the console to see the 
exact representation of this value on your system, it may be just over or 
just down by a small quantity.

On mine with 2.6 this typically give :

[26]: 0.5
...[26]: 0.5

[27]: 0.49
...[27]: 0.48999

[29]: 0.51
...[29]: 0.51001

[28]: 1.1
...[28]: 1.1001

[35]: round(0.5)
...[35]: 1.0


-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: sub typing built in type with common attributes. Am I right?

2008-07-18 Thread Maric Michaud
Le Friday 18 July 2008 11:36:20 King, vous avez écrit :
 Could you please suggest an alternative or code improvement for the
 matter.

I'm not sure what you are trying to achieve with your snippet, but I suspect 
it's some sort of templating, right ? If so, using the dynamic nature of 
python should help :

[103]: def make_subtype_with_attr(type_, ro_attr, rw_attr) :
dic = {}
for i in ro_attr : dic[i] = property(lambda s, n=i : getattr(s, '_'+n))
def __new__(cls, *args, **kwargs) :
instance = type_.__new__(cls, *args)
for i in rw_attr : setattr(instance, i, kwargs[i])
for i in ro_attr : setattr(instance, '_'+i, ro_attr[i])
return instance
dic['__new__'] = __new__
return type('my_' + type_.__name__, (type_,), dic)
   .:

[113]: my_int = make_subtype_with_attr(int, 
{'name' : 'myint', 'id':123452}, ('foo',))

[114]: i = my_int(5, foo=3)

[115]: i.foo
...[115]: 3

[116]: i
...[116]: 5

[117]: i.id
...[117]: 123452

[118]: i.id = 2
---
AttributeErrorTraceback (most recent call last)

/home/maric/ipython console in module()

AttributeError: can't set attribute


-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: (silly?) speed comparisons

2008-07-09 Thread Maric Michaud
Le Wednesday 09 July 2008 12:35:10 mk, vous avez écrit :
 vectorstring move_slice(vectorstring vec, int start, int stop, int
 dest)

I guess the point is to make a vector of referene to string if  you don't want 
to copy string objects all around but just a word for an address each time.

The signature should be :
vectorstring move_slice(vectorstring vec, int start, int stop, int 
dest)

or

vectorstring* move_slice(vectorstring* vec, int start, int stop, int 
dest)


-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: List Performance

2008-06-30 Thread Maric Michaud
Le Monday 30 June 2008 09:23:46 Peter Otten, vous avez écrit :
 Ampedesign wrote:
  If I happen to have a list that contains over 50,000 items, will the
  size of the list severely impact the performance of appending to the
  list?

 No.

 $ python -m timeit -n2 -sitems = [] items.append(42)
 2 loops, best of 3: 0.554 usec per loop
 $ python -m timeit -n2 -sitems = [42]*10**6 items.append(42)
 2 loops, best of 3: 0.529 usec per loop

But it surely could, if the box happens to be out of memory and begin to swap, 
while it's not, of course, an issue with python lists...

-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: Help with Borg design Pattern

2008-06-30 Thread Maric Michaud
Le Monday 30 June 2008 10:52:24 Casey McGinty, vous avez écrit :
 I'm running into a slight problem however that my run-time defined logging
 level is not correctly set until after the module has initialized,
 preventing any log messages from showing up. Is there a pythonic way to get
 around this? I'm thinking of adding a module init routine, but I don't feel
 like this is clean solution.


If the logging machinery is effectively not ready (and couldn't be implemented 
so) at the time import logging statment is done, then you'll need to have a 
logging procedure which queue the message waitinig for system to be 
operational and deliver them lazily. This is quite an easy thing to implement 
(hint : use stdlib data structures that support multi threading from the 
beginning).

-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: List Performance

2008-06-30 Thread Maric Michaud
Le Monday 30 June 2008 15:52:56 Gerhard Häring, vous avez écrit :
 Larry Bates wrote:
  [...]
  So its actually faster to append to a long list than an empty one?  That
  certainly would not have been intuitively obvious now would it?

 Maybe not intuitively, but if you know how dynamically growing data
 structures are implemented, it's plausible. They overallocate, and the
 amount of overallocation is dependent on the current size. Relevant
 source snippet from Python 2.6:

  /* This over-allocates proportional to the list size, making room
   * for additional growth.  The over-allocation is mild, but is
   * enough to give linear-time amortized behavior over a long
   * sequence of appends() in the presence of a poorly-performing
   * system realloc().
   * The growth pattern is:  0, 4, 8, 16, 25, 35, 46, 58, 72, 88, ...
   */
  new_allocated = (newsize  3) + (newsize  9 ? 3 : 6);

 If, on the other hand, we knew beforehand how big the list will get
 approximately, we could avoid all these reallocations. No problem with
 Python's C API:

 PyAPI_FUNC(PyObject *) PyList_New(Py_ssize_t size);

 But you can't do it directly from Python, unless you (ab)use ctypes.

 -- Gerhard

 --
 http://mail.python.org/mailman/listinfo/python-list

Well, as I posted few days ago, one could envisage, as a pure python 
optimization for dealing with long list, to replace an algorithm with a lot 
of append by something like this :

mark = object()

datas = [ mark ] * expected_size

# working with the datas while maintaining the effective currrently used size

Of course one could even subclass list and redefine __len__, append, and some 
other methods to deal with this allocated by block list.


-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: insertion sorts...

2008-06-30 Thread Maric Michaud
Le Monday 30 June 2008 16:29:11 python_newbie, vous avez écrit :
 On 25 Haziran, 17:44, MRAB [EMAIL PROTECTED] wrote:
  On Jun 25, 11:37 am, A.T.Hofkamp [EMAIL PROTECTED] wrote:
   On 2008-06-25, python_newbie [EMAIL PROTECTED] wrote:
On 24 Haziran, 04:33, Terry Reedy [EMAIL PROTECTED] wrote:
Thanks for all answers. At the end i ve only one point. If a decide
to copy list to iterate when will i have to do this ? Before the
iteration ? And then iterate through one list and change value of the
other ?
  
   Before starting the iteration would be a good point
  
   I usually do in such cases:
  
   for x in mylist[:]:
      ...
  
   making a copy just before the for loop starts.
  
   Lately, I have started avoiding in-place modification of lists.
   Instead, I construct a new list from scratch inside the for-loop, and
   replace the old list with the newly constructed list afterwards like:
  
   new_list = []
   for x in mylist:
      
      new_list.append(x)
  
   mylist = new_list
  
   by appending a different value than the original or by not appending,
   you can influence the contents of the new list.
  
   I find this solution nicer than in-place modification of an existing
   list.
  
   Sincerely,
   Albert
 
  And if you were originally doing in-place modification because there
  were also other references to the list then you could just do:
 
  mylist[:] = new_list

 Thanks again. I see that you use two different syntax for lists
 mylist = new_list and mylist[:] = new_list these are same i
 think ?

Not at all, try to figure out what happen with the following :

[3]: l=[]

[4]: g=[]

[5]: l == g
...[5]: True

[6]: l is g
...[6]: False

[7]: l = [4]

[8]: l is g
...[8]: False

[9]: l == g
...[9]: False

[10]: l = g

[11]: l is g
...[11]: True

[12]: l[:] = [4]

[13]: l == g
...[13]: True

[14]: l is g
...[14]: True

[15]: g
...[15]: [4]



-- 
_

Maric Michaud

--
http://mail.python.org/mailman/listinfo/python-list


Re: List Performance

2008-06-30 Thread Maric Michaud
Le Monday 30 June 2008 22:21:35 Terry Reedy, vous avez écrit :
  Well, as I posted few days ago, one could envisage, as a pure python
  optimization for dealing with long list, to replace an algorithm with a
  lot of append by something like this :
 
  mark = object()
 
  datas = [ mark ] * expected_size

 datas = [None] * expected_size
 has been a standard idiom since before object() existed ;-)
 and works fine *unless* one wants to add None explicitly
 and have that be different from 'unused'.


Yes, in fact I used a marker because it I thought of it primarily as outbound 
for the list (like \0 for strings in C), but it doesnt' matter what is the 
object you put in the list, if you know at every moment its size.

A subclass of list will indeed have to override most of the methods of its 
parent (not just some as I assumed before), using extend for reallocation 
with some sort of iterator with size, as it work in my previous example with 
xrange, something like that :

[31]: class iter_with_len(object) :
def __init__(self, size, obj=None) :
self.size = size
self.obj = obj
def __len__(self) : return self.size
def __iter__(self) : return itertools.repeat(self.obj, len(self))
   :
   :




-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: Email Bounce Detection

2008-06-27 Thread Maric Michaud
Le Friday 27 June 2008 09:03:15 madhav, vous avez écrit :
 Hello everybody, I need a mechanism to detect email bounces. I tried
 browsing through smtplib implementation and found not helpful in this
 case. Actually it is said in the documentation that if a mail is sent
 to say: [EMAIL PROTECTED], then _send() in the
 SMTPConnection class returns 550(Unknown recceipient). I checked the
 same but its not returning 550 and return 250(receipient ok).

This is not the same scenario, the SMTP server that will respond 550 is the MX 
for the domain, if you send your mails using an outgoing SMTP server, it 
can't know in advance the list of remote adressesbut will bounce your message 
when receiving the 550 from the remote MX.

 Later, I tried getting the smtp error code from the mail body of a
 bounced msg. the Message class doesnot have any error code attribute
 by default. We need to get it from the mailbody(by regex ing), which i
 think is not a valid solution, as 550 can occur anywhere in the body
 of any message otherthan the bounced msg also.
 Please help me regarding this.
 Thanks in advance.

Bounces are multipart messages easily parsed with the email package of the 
stdlib (dont' use regexes here).

Abounce message is of type :

Content-Type: multipart/report;
  report-type=delivery-status;  

a delivery-status imply that one of the subpart of the message is a 
message/delivery-status with all needed infos set in the headers. This one is 
taken from a real example :

Content-Description: Delivery report
Content-Type: message/delivery-status

Reporting-MTA: dns; aristote.info
X-Postfix-Queue-ID: 1D07D1409FF
X-Postfix-Sender: rfc822; [EMAIL PROTECTED]
Arrival-Date: Fri, 27 Jun 2008 09:10:14 +0200 (CEST)

Final-Recipient: rfc822; [EMAIL PROTECTED]
Original-Recipient: rfc822;[EMAIL PROTECTED]
Action: failed
Status: 5.0.0
Remote-MTA: dns; tyrande.nerim.net
Diagnostic-Code: smtp; 550 [EMAIL PROTECTED]: Recipient address 
rejected:
User unknown in local recipient table


So, for example :


[48]: import email

[49]: bounce = email.message_from_string(open('ex.eml').read())

[50]: bounce.get_content_type()
...[50]: 'multipart/report'

[51]:

[52]: for i in bounce.get_payload() :
   :if i.get_content_type() == 'message/delivery-status' :
   : print i.get_payload()[1]['status']
   :
   :
5.0.0




-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: using urllib2

2008-06-27 Thread Maric Michaud
Le Friday 27 June 2008 10:43:06 Alexnb, vous avez écrit :
 I have never used the urllib or the urllib2. I really have looked online
 for help on this issue, and mailing lists, but I can't figure out my
 problem because people haven't been helping me, which is why I am here! :].
 Okay, so basically I want to be able to submit a word to dictionary.com and
 then get the definitions. However, to start off learning urllib2, I just
 want to do a simple google search. Before you get mad, what I have found on
 urllib2 hasn't helped me. Anyway, How would you go about doing this. No, I
 did not post the html, but I mean if you want, right click on your browser
 and hit view source of the google homepage. Basically what I want to know
 is how to submit the values(the search term) and then search for that
 value. Heres what I know:

 import urllib2
 response = urllib2.urlopen(http://www.google.com/;)
 html = response.read()
 print html

 Now I know that all this does is print the source, but thats about all I
 know. I know it may be a lot to ask to have someone show/help me, but I
 really would appreciate it.

This example is for google, of course using pygoogle is easier in this case, 
but this is a valid example for the general case :

[207]: import urllib, urllib2

You need to trick the server with an imaginary User-Agent.

[208]: def google_search(terms) :
return urllib2.urlopen(urllib2.Request(http://www.google.com/search?; +  
urllib.urlencode({'hl':'fr', 'q':terms}),
   headers={'User-Agent':'MyNav 1.0 
(compatible; MSIE 6.0; Linux'})
  ).read()
   .:

[212]: res = google_search(python  co)

Now you got the whole html response, you'll have to parse it to recover datas, 
a quick  dirty try on google response page :

[213]: import re

[214]: [ re.sub('.+?', '', e) for e in re.findall('h2 class=r.*?/h2', 
res) ]
...[229]:
['Python Gallery',
 'Coffret Monty Python And Co 3 DVD : La Premi\xe8re folie des Monty ...',
 'Re: os x, panther, python amp; co: msg#00041',
 'Re: os x, panther, python amp; co: msg#00040',
 'Cardiff Web Site Design, Professional web site design services ...',
 'Python Properties',
 'Frees lt; Programs lt; Python lt; Bin-Co',
 'Torb: an interface between Tcl and CORBA',
 'Royal Python Morphs',
 'Python amp; Co']


-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: Use of the is statement

2008-06-27 Thread Maric Michaud
Le Friday 27 June 2008 18:26:45 Christian Heimes, vous avez écrit :
 Ask yourself if you are interested if f.tell() returns exactly the same
 0 object (is) or a number that is equal to 0 (==).

That said, f.tell() == 0 and f.tell() != 0 should be written f.tell() 
and not f.tell() in python.

if not f.tell() :
print 'at the beginning of the file


-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: Do I need self and other?

2008-06-27 Thread Maric Michaud
Le Saturday 28 June 2008 00:17:33 Kurda Yon, vous avez écrit :
 class Vector:
   def __add__(self, other):
     data = []
     for j in range(len(self.data)):
       data.append(self.data[j] + other.data[j])
     return Vector(data)

 In this example one uses self and other. Does one really need to
 use this words?

Yes, he does, because one really need to follow the conventions of a language, 
which belong to the language even they don't belong to its syntax. You should 
have a look to the PEP 8 : http://www.python.org/dev/peps/pep-0008/

self is the good name for the implicit argument of instancemethod.
other is the name widely used for rvalue in binary operators definition.

As for convention, your constructor, Vector(iterable), is expected to be a 
copy constructor, that means the Vector's __init__ method is somewhat like 
this :

def __init__(self, iterable=()) :
self.data=list(iterable) # create a copy of the parmeter

First, self.data should be renamed to self._data, see PEP 8 for details, 
because data will probably not be part of the API of your object.

Then, this make your code a bit curious, you create a list to add the two 
vectors, then copy it to create the resultiing vector, consuming two times 
the memory needed.

Finally, you would probably gain in inheriting directly from the builtin type 
list.

Here is my proposal for your Vector class (wth some other enhancements) :

class Vector(list) :

def __init__(self, iterable=()) :

def __init__(self, iterable=()) :
list.__init__(self, iterable)

def __repr__(self) :
return 'Vector(%s)' % list.__repr__(self)

def __add__(self, other) :
res = Vector()
for i, elt in enumerate(self) :
res.append(elt + other[i])
return res

Note that I don't know how you want to deal vectors of different dimensions, 
you should probably raise a ValueError.

Also use enumerate, i in xrange(len(iterable)) was required before enumerate 
was done a builtin but is rather ugly.

Finally, to come to a more functionnal style the method could have been 
written like this :

from itertools import izip

...

def __add__(self, other) :
return Vector(x + y for x, y in izip(self, other))



-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: Help with Borg design Pattern

2008-06-27 Thread Maric Michaud
Le Saturday 28 June 2008 03:47:43 Casey McGinty, vous avez écrit :
 On Fri, Jun 27, 2008 at 3:21 PM, Casey McGinty [EMAIL PROTECTED]

 wrote:
  Hi,
 
  I'm trying to implement a simple Borg or Singleton pattern for a class
  that inherits from 'dict'. Can someone point out why this code does not
  work?
 
  class MyDict( dict ):
 __state = {}
 def __init__(self):
self.__dict__ = self.__state
 
  a = MyDict()
  a['one'] = 1
  a['two'] = 2
 
  print a
  print MyDict()


Well, it works !

[156]: class MyDict( dict ):
   __state = {}
   def __init__(self):
  self.__dict__ = self.__state
   .:
   .:

[160]: MyDict().toto = 5

[161]: MyDict().toto
...[161]: 5

but the __dict__ attribute is the container of attributes of an instance, 
which are not used in the underlying implementation of dictinnaries.

 This looks like a good solution:

 class MyDict( dict ):
def __new__(cls,*p,**k):
   if not '_instance' in cls.__dict__:
  cls._instance = dict.__new__(cls)
   return cls._instance

Yes it is, but it's rather unneeded in Python, we prefer simply create a 
module level dictionnary, these tricks are used in language like C++ or Java.

In python :

mymodule.py :

ModuleOptions = {}

othermodule.py :

import mymodule

mymodule.ModuleOptions['Verbose'] = True

or if you think encapsulation is important :

mymodule.py :

_ModuleOptions = {}

def get_option(opt) :
return _ModuleOptions[opt]


And you're done.

-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: this is simple...

2008-06-27 Thread Maric Michaud
Le Saturday 28 June 2008 06:30:25 ToshiBoy, vous avez écrit :
 I am a newbie... and the first to admit it... but this has me stuffed:

 I have two lists A and B that are both defined as range(1,27) I want
 to find the entries that are valid for A = BxB

 so here is my code:

 A = range(1,27)
 B = range(1,27)

 for b in B:
 if b*b in A:
 print b
 else:
 B.remove(b)

 I get, as expected 1,4,9,16,25 printed out being the only members of B
 where the condition is true, but when I print B I get:

 [1, 2, 3, 4, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25]

 1 to 5 is correct, but why doesn't the remove method remove 7 and
 above? What am I doing wrong here?
 --
 http://mail.python.org/mailman/listinfo/python-list


This is a frequent error from beginner, you're iterating over a list while 
modifying it, the result is undefined.

The most immediate solution is to use a copy of the list you iterate over :

[169]: A = range(1,27)

[170]: B = range(1,27)

[171]: for b in B[:] :
if b*b in A:
print b
else:
B.remove(b)
   .:
   .:
1
2
3
4
5

[176]: B
...[176]: [1, 2, 3, 4, 5]

Note that this can be easily done with the simpler :

B = [ e for e in B if e*e not in A ]

-- 
_

Maric Michaud

--
http://mail.python.org/mailman/listinfo/python-list


Re: Mako vs. Cheetah?

2008-06-27 Thread Maric Michaud
Le Saturday 28 June 2008 06:30:50 Tim Roberts, vous avez écrit :
   Others really like the TAL scheme in Zope.
 For my taste, TAL just requires too much non-essential syntax; it
 interferes with the reading of the page.

 So, the folks who like TAL can go ahead and be productive with TAL, and
 I'll keep on being productive with Cheetah.

Yes, TAL is a bit more hard and require some added learning, but tal templates 
are very powerful and well structured, and they have a real advantage over 
all other templating system, they are valid xhtml, thus an html editor could 
modify them without breaking the logic of the template.

-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: Freeze problem with Regular Expression

2008-06-25 Thread Maric Michaud
Le Wednesday 25 June 2008 18:40:08 cirfu, vous avez écrit :
 On 25 Juni, 17:20, Kirk [EMAIL PROTECTED] wrote:
  Hi All,
  the following regular expression matching seems to enter in a infinite
  loop:
 
  
  import re
  text = ' MSX INTERNATIONAL HOLDINGS ITALIA srl (di seguito MSX ITALIA)
  una '
  re.findall('[^A-Z|0-9]*((?:[0-9]*[A-Z]+[0-9|a-z|\-]*)+\s*[a-z]*\s*(?:[0-9
 ] *[A-Z]+[0-9|a-z|\-]*\s*)*)([^A-Z]*)$', text)
  #
 
  No problem with perl with the same expression:
 
  #
  $s = ' MSX INTERNATIONAL HOLDINGS ITALIA srl (di seguito MSX ITALIA) una
  ';
  $s =~ /[^A-Z|0-9]*((?:[0-9]*[A-Z]+[0-9|a-z|\-]*)+\s*[a-z]*\s*(?:[0-9]*[A-
  Z]+[0-9|a-z|\-]*\s*)*)([^A-Z]*)$/;
  print $1;
  #
 
  I've python 2.5.2 on Ubuntu 8.04.
  any idea?
  Thanks!
 
  --
  Kirk

 what are you trying to do?

This is indeed the good question.

Whatever the implementation/language is, something like that can work with 
happiness, but I doubt you'll find one to tell you if it *should* work or if 
it shouldn't, my brain-embedded parser is doing some infinite loop too...

That said, [0-9|a-z|\-] is by itself strange, pipe (|) between square 
brackets is the character '|', so there is no reason for it to appears twice.

Very complicated regexps are always evil, and a two or three stage filtering 
is likely to do the job with good, or at least better, readability.

But once more, what are you trying to do ? This is not even clear that regexp 
matching is the best tool for it.

-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: binary representation of an integer

2008-06-24 Thread Maric Michaud
Le Tuesday 24 June 2008 10:03:58 eliben, vous avez écrit :
 Hello,

 I'm interested in converting integers to a binary representation,
 string. I.e. a desired function would produce:

 dec2bin(13) = 1101

 The other way is easily done in Python with the int() function.

 Perl has a very efficient way to do dec2bin, because its pack/unpack
 have a B format for binary representations, so it's done with:

 sub dec2bin {
 my $str = unpack(B32, pack(N, shift));
 $str =~ s/^0+(?=\d)//;   # otherwise you'll get leading zeros
 return $str;
 }

 Python's pack/unpack don't have the binary format for some reason,

Yes, but I think the %b format specifier has been added to p3k but will not in 
python 2.x.

 so 
 custom solutions have to be developed. One suggested in the ASPN
 cookbook is:
 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/111286
 However, it is very general and thus inefficient.

 What would be the quickest way to do this ? I think that for dec2bin
 conversion, using hex() and then looping with a hex-bin lookup table
 would be probably much faster than the general baseconvert from the
 recipe.

 What do you think?


Something like that, less typing with octal conversion :)

[8]: oct2bin = 
{'0':'000', '1':'001', '2':'010', '3':'011', '4':'100', '5':'101', '6':'110', 
'7':'111'}

[9]: ''.join(oct2bin[e] for e in %o%35).lstrip('0')
...[9]: '100011'



-- 
_

Maric Michaud
_

Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 4 26 88 00 97
Mobile: +33 6 32 77 00 21
--
http://mail.python.org/mailman/listinfo/python-list


Re: An idiom for code generation with exec

2008-06-23 Thread Maric Michaud
Le Monday 23 June 2008 09:22:29 Bruno Desthuilliers, vous avez écrit :
  With some help from the guys at IRC I came to realize your way doesn't
  do the same. It creates a function that, when called, creates 'foo' on
  globals(). This is not exactly what I need.

 I possibly messed up a couple things in the arguments, flags etc - I
 very seldom use compile() and function(). The point was that  it didn't
 require any extra step.

In the argument list  of function type, the code object in first place is 
expected to be created directly (no exec - eval) with the python type 'code' 
(either found as types.CodeType or new.code).


In [24]: types.CodeType?
Type:   type
Base Class: type 'type'
String Form:type 'code'
Namespace:  Interactive
Docstring:
code(argcount, nlocals, stacksize, flags, codestring, constants, names,
  varnames, filename, name, firstlineno, lnotab[, freevars[, 
cellvars]])

Create a code object.  Not for the faint of heart.

  ^^^

Even if it looks more object oriented, I'm not sure it's actually the good 
solution for the original problem. I think these interface are not a 
replacement for the quick eval-exec idiom but more intended to make massive 
code generation programs object oriented and closer to python internals.

AFAIK, the only use case I see code generation (eval - exec, playing with code 
objects) as legitime in python is in programs that actually do code 
generation, that is, parse and compile code from textual inputs (application 
buillders).

If code generation is not the best, and I fail to see any performance issue 
that could explain such a choice, except a misunderstanding of 
what compilation means in python, just don't use it, use closures or 
callable instances, there are many way to achieve this.

-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: listcomprehension, add elements?

2008-06-23 Thread Maric Michaud
Le Monday 23 June 2008 11:39:44 Boris Borcic, vous avez écrit :
 John Machin wrote:
  Instead of sum(a + b for a, b in zip(foo, bar))
  why not use sum(foo) + sum(bar)
  ?

 or even sum(foo+bar) as may apply.

Because some are better than others :


sum(foo+bar) is the worst, it create a superfluous list of len(foo) + len(bar) 
elements.

sum(a + b for a, b in zip(foo, bar)), creates a list of max(len(foo), 
len(bar)) elements, in most cases it is the same as the former.

This could have been corrected using itertools.izip.

So the winner is sum(foo) + sum(bar), which does not create anything not 
needed.

But if the question is how to build the list and sum up all elements in a 
efficient way for sequences of arbitrary length , it's important to make it 
in the same iteration, so the most effective/clear, and so pythonic, way to 
do this is (untested) :

res, sum = [], 0
for s in (a + b for a, b 
in zip(itertools.izip(xrange(1, 51),
 xrange(50, 0, -1:
sum += s
res.append(sum)


Which is pythonic in first place is to provide sequences of datas as 
iterators when they can grow in size.

-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: listcomprehension, add elements?

2008-06-23 Thread Maric Michaud
Le Monday 23 June 2008 13:51:34 John Machin, vous avez écrit :
 On Jun 23, 9:16 pm, Maric Michaud [EMAIL PROTECTED] wrote:
  Le Monday 23 June 2008 11:39:44 Boris Borcic, vous avez écrit :
   John Machin wrote:
Instead of sum(a + b for a, b in zip(foo, bar))
why not use sum(foo) + sum(bar)
?
  
   or even sum(foo+bar) as may apply.
 
  Because some are better than others :
 
  sum(foo+bar) is the worst, it create a superfluous list of len(foo) +
  len(bar) elements.
 
  sum(a + b for a, b in zip(foo, bar)), creates a list of max(len(foo),
  len(bar)) elements, in most cases it is the same as the former.
 
  This could have been corrected using itertools.izip.
 
  So the winner is sum(foo) + sum(bar), which does not create anything not
  needed.
 
  But if the question is how to build the list and sum up all elements in
  a efficient way for sequences of arbitrary length , it's important to
  make it in the same iteration, so the most effective/clear, and so
  pythonic, way to do this is (untested) :
 
  res, sum = [], 0

 Please use tot or total, not sum!

  for s in (a + b for a, b
  in zip(itertools.izip(xrange(1, 51),

 Perhaps you should not have left zip() in there ...

   xrange(50, 0,
  -1: sum += s
  res.append(sum)

 Do you mean res.append(s) ?


Yes, my mistakes, all remarks are indeed true. Sorry for this out of thoughts 
sketch.

 I would have thought that it would have been better to create the list
 and then sum it:
 res = [a + b for a, b in itertools.izip(foo_iter, bar_iter)]
 total = sum(res)


This is good enough if you accept the overhead of iterating twice over the 
whole list.

But this may be a bit more complicated, for efficiency, there is a better way 
of allocating memory than successively appending new item.

I guess this should be a far better solution, but you'll need to know the 
required number of iteration (the size of your iterators, the xrange in this 
sample) :

res, total = [None] * n, 0
for i, s in enumerate(a + b for a, b 
   in izip(xrange(1, n+1),
 xrange(n, 0, -1)):
total += s
res[i] =s

-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: Question: How do I format printing in python

2008-06-23 Thread Maric Michaud
Le Tuesday 24 June 2008 05:33:01 Larry Bates, vous avez écrit :
 [EMAIL PROTECTED] wrote:
...

 data = '''512 Jun 5 2004 X11r6
 22 Jan 17 2005 a2p
 22 Jan 17 2005 acctcom
 5374 Sep 15 2002 acledit
 5664 May 13 2004 aclget
 12020 May 13 2004 aclput
 115734 Jun 2 2004 adb
 46518 Jun 4 2004 admin
 66750 Sep 16 2002 ali
 1453 Sep 15 2002 alias
 28150 Jun 4 2004 alog
 15 May 12 2005 alstat
 '''

 for entry in data.rstrip().split('\n'):
  items = entry.split(' ')
  print %-6s %3s   %2s  %4s%-7s % tuple(items)

In python, str objects have a splitlines method for portability it is better, 
a shorthand for split(os.sep).


-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: Fwd: xml to mysql (vice versa ) too

2008-06-23 Thread Maric Michaud
Le Tuesday 24 June 2008 07:08:46 swapna mudavath, vous avez écrit :
 can anybody help me in this

 -swapna

 -- Forwarded message --
 From: swapna mudavath [EMAIL PROTECTED]
 Date: Mon, Jun 23, 2008 at 5:27 PM
 Subject: xml to mysql (vice versa ) too
 To: Python-list@python.org


 Hi,

 I need to write a python script to store data which is in XML to MYSQL and
 even vice versa
 what should be the approach?
 i am able to establish a connection,create tables and insert data .
 but how to read an xml file and store in MYSQL
 my XML structure is like

 list  title= xyz, id = 1,
 item name=  abc ,pos=1,
 /item
 item  name = hgdf, pos =3,..
 /item
.

...
 /list

This is not valid xml, there is no commas in attribute list in xml.

 can somebody please help me..i am really confused!!

 thanks in advance  :)

You could try with minidom if your xml stream isn't too large, else sax parser 
is to be considered, but minidom is pretty easy to use.

Give it a try and come back with more specific questions.

In [82]: from xml.dom.minidom import parseString

In [83]: xml = list  title= xyz id= 1
item name=  abc pos=1
/item
item  name= hgdf pos =3
 /item
/list

In [89]: def print_nodes(node) :
print node
if node.attributes :
for n, v in node.attributes.items() : print n, v
for i in node.childNodes : print_nodes(i)
   :
   :

In [94]: dom = parseString(xml)

In [95]: print_nodes(dom)
xml.dom.minidom.Document instance at 0xc2b7e8
DOM Element: list at 0xc2b998
id 1
title  xyz
DOM Text node 

DOM Element: item at 0xc2bd40
name   abc
pos 1
DOM Text node 

DOM Text node 

DOM Element: item at 0xc30050
name  hgdf
pos 3
DOM Text node 
 
DOM Text node 



-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: An idiom for code generation with exec

2008-06-23 Thread Maric Michaud
Le Tuesday 24 June 2008 07:18:47 eliben, vous avez écrit :
  If code generation is not the best, and I fail to see any performance
  issue that could explain such a choice, except a misunderstanding of
  what compilation means in python, just don't use it, use closures or
  callable instances, there are many way to achieve this.

 And while we're on the topic of what compilation means in Python, I'm
 not sure I fully understand the difference between compiled (.pyc)
 code and exec-ed code. Is the exec-ed code turned to bytecode too,
 i.e. it will be as efficient as compile-d code ?

Yes, exactly the same, cpython always interprets compiled code, when a script 
is executed for example, it is parsed/compiled to bytecode by the interpreter 
before any execution. The .pyc/pyo files are just a cache created at import 
time to avoid the rather time consuming parsing stage.

-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: 'string'.strip(chars)-like function that removes from the middle?

2008-06-16 Thread Maric Michaud
Le Monday 16 June 2008 18:58:06 Ethan Furman, vous avez écrit :
 The strip() method of strings works from both ends towards the middle.
 Is there a simple, built-in way to remove several characters from a
 string no matter their location? (besides .replace() ;)

 For example:
 .strip -- 'www.example.com'.strip('cmowz.')
 'example'
 .??? -- --- 'www.example.com'.strip('cmowz.')
 'exaple'

As Larry Bates said the python way is to use str.join, but I'd do it with a 
genexp for memory saving, and a set to get O(1) test of presence.

to_remove = set('chars')
''.join(e for in string_ if e not in to_remove)

Note that this one will hardly be defeated by other idioms in python (even 
regexp).

Using a genexp is free and a good practice, using a set, as it introduce one 
more step, can be considered as premature optimisation and the one liner :

''.join(e for in string_ if e not in 'chars')

may be preferred.

-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: Removing inheritance (decorator pattern ?)

2008-06-16 Thread Maric Michaud
),
 (algo1_strategy, algo2_strategy)
)


etc...


-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: Removing inheritance (decorator pattern ?)

2008-06-16 Thread Maric Michaud
Le Tuesday 17 June 2008 05:10:57 Maric Michaud, vous avez écrit :
 The class complextiy problem is actually solved by :

 inst_with_alg1 = MyClassUsingStrategies((algo1_strategy,),
 (algo1_strategy,)) inst_with_alg1_alg2 = MyClassUsingStrategies(
                                                       (algo1_strategy,),
                                                       (algo2_strategy,)
                                   )
 inst_with_alg12 = MyClassUsingStrategies(
                                              (algo1_strategy,
 algo2_strategy), (algo1_strategy, algo2_strategy) )


 etc...

Ah ! they should be instances here, this also permit extra configuration 
parameters to be passed in the Strategies constructors :

inst_with_alg12 = MyClassUsingStrategies(
 (algo1_strategy(), algo2_strategy()),
 (algo1_strategy(), algo2_strategy())
)


-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: Summing a 2D list

2008-06-13 Thread Maric Michaud
Le Friday 13 June 2008 14:12:40 Karsten Heymann, vous avez écrit :
 Hi Mark,

 Mark [EMAIL PROTECTED] writes:
  I have a scenario where I have a list like this:
 
  User            Score
  1                 0
  1                 1
  1                 5
  2                 3
  2                 1
  3                 2
  4                 3
  4                 3
  4                 2
 
  And I need to add up the score for each user to get something like
  this:
 
  User            Score
  1                 6
  2                 4
  3                 2
  4                 8
 
  Is this possible? If so, how can I do it? I've tried looping through
  the arrays and not had much luck so far.

 Although your problem has already been solved, I'd like to present a
 different approach which can be quite a bit faster. The most common
 approach seems to be using a dictionary:

 summed_up={}
 for user,vote in pairs:
   if summed_up.has_key(user):
     summed_up[user]+=vote
   else:
     summed_up[user]=vote

 But if the list of users is compact and the maximum value is known
 before, the using a list and coding the user into the list position is
 much more elegant:


So, writing C in python, which has dictionnary as builtin type, should be 
considered more elegant ?

 summed_up=list( (0,) * max_user )
 for user,vote in pairs:
   summed_up[user] += vote

 I've run a quick and dirty test on these approaches and found that the
 latter takes only half the time than the first. More precisely, with
 about 2 million pairs, i got:


 * dict approach: 2s
         (4s with try: ... except KeyError: instead of the if)
 * list approach: 0.9s


You are comparing apples with lemons, there is no such a difference between 
list index access and dictionnary key access in Python.

 BTW this was inspired by the book Programming Pearls I read some
 years ago where a similar approach saved some magnitudes of time
(using a bit field instead of a list to store reserved/free phone
 numbers IIRC).

If you know in advance the number and names of users, what prevent you to 
initialize completelly the target dictionnary ?

The following code compare the same algorithm, once with list and the second 
time with dict :

#!/usr/bin/env python

def do(f, u, v) :
from time import time
n=time()
f(u, v)
return time() -n

def c_dict(users, votes) :
d = dict(((e, 0) for e in users))
for u, v in votes : d[u] += v
return d.values()

def c_list(users, votes) :
d = [ 0 for i in users ]
for u, v in votes : d[u] += v
return d

u = range(3000)

import random

v = list((u[r%3000], random.randint(0,1)) for r in range(5*10**6))

print with list, do(c_list, u, v)
print with dict, do(c_dict, u, v)

The result is pretty close now :

[EMAIL PROTECTED] 17:04:36:~$ ./test.py
with list 1.40726399422
with dict 1.63094091415

So why use list where the obvious and natural data structure is a 
dictionnary ?


-- 
_

Maric Michaud

--
http://mail.python.org/mailman/listinfo/python-list


Re: Summing a 2D list

2008-06-13 Thread Maric Michaud
Hello,

Le Friday 13 June 2008 17:55:44 Karsten Heymann, vous avez écrit :
 Maric Michaud [EMAIL PROTECTED] writes:
  So, writing C in python, which has dictionnary as builtin type,
  should be considered more elegant ?

 IMO that's a bit harsh.


harsh ? Sorry, I'm not sure to understand.

  You are comparing apples with lemons, there is no such a difference
  between list index access and dictionnary key access in Python.

 [...]

  If you know in advance the number and names of users, what prevent
  you to initialize completelly the target dictionnary ?
 
  The following code compare the same algorithm, once with list and
  the second time with dict :

 [...]

  The result is pretty close now :
 
  [EMAIL PROTECTED] 17:04:36:~$ ./test.py
  with list 1.40726399422
  with dict 1.63094091415
 
  So why use list where the obvious and natural data structure is a
  dictionnary ?

 I'd never argue that using a dictionary is the obvious and natural
 data structure for this case. But is it the best? Honestly, as your
 very nice example shows, we have two solutions that are equally fast,
 equally complex to code and equally robust, but one needs

Yes, but my example take ordered integer for keys (users' names)  which they 
should not be in a real case, so retrieving the result is by way easier (and 
faster) with a dictionnary.

 approximately the double amount of memory compared to the other. 

I don't see how you came to this conclusion. Are you sure the extra list take 
twice more memory than the extra dictionary ?

 So, as much as i like dictionaries, what's the gain you get from using it
 in this corner case?

It's the very purpose of it's usage, store and retrieve data by key.

Cheers,

-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: Summing a 2D list

2008-06-13 Thread Maric Michaud
Le Friday 13 June 2008 18:55:24 Maric Michaud, vous avez écrit :
  approximately the double amount of memory compared to the other.

 I don't see how you came to this conclusion. Are you sure the extra list
 take twice more memory than the extra dictionary ?

twice less, I meant, of course...

-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: catastrophic regexp, help!

2008-06-11 Thread Maric Michaud
Le Wednesday 11 June 2008 06:20:14 cirfu, vous avez écrit :
 pat = re.compile((\w* *)*)
 this matches all sentences.
 if fed the string are you crazy? i am it will return are you
 crazy.

 i want to find a in a big string a sentence containing Zlatan
 Ibrahimovic and some other text.
 ie return the first sentence containing the name Zlatan Ibrahimovic.


 patzln = re.compile((\w* *)* zlatan ibrahimovic (\w* *)*)
 should do this according to regexcoach but it seems to send my
 computer into 100%CPU-power and not closable.

This kind of regexp are quite often harmfull, while perfectly valid, if you 
take the time it will return, this check too many things to be practical.

Read it, sequentially to make it sensible : for each sequence of word + space, 
trying with the longest first, does the string 'zlatan' follow ?

this is zlatan example.'
compare with 'this is zlatan example', 'z'=='.', false
compare with 'this is zlatan ', 'z'=='e', false
compare with 'this is zlatan', 'z'==' ', false
compare with 'this is ', zlatan==zlatan, true
compare with 'this is', 'z'==' ', false
compare with 'this ', 'z'=='i', false
compare with 'this', 'z'==' ', false
...

ouch !

The most simple are your regex, better they are, two short regex are better 
then one big, etc...
Don't do premature optimization (especially with regexp).

In [161]: s=pat = re.compile((\w* *)*)
this matches all sentences.
if fed the string are you crazy? i am it will return are you
crazy.
i want to find a in a big string a sentence containing Zlatan
Ibrahimovic and some other text.
ie return the first sentence containing the name Zlatan Ibrahimovic.
patzln = re.compile((\w* *)* zlatan ibrahimovic (\w* *)*)
should do this according to regexcoach but it seems to send my
computer into 100%CPU-power and not closable.


In [172]: list(e[0] for e in re.findall(((\w+\s*)+), s, re.M) if 
re.findall('zlatan\s+ibrahimovic', e[0], re.I))
Out[172]:
['i want to find a in a big string a sentence containing Zlatan\nIbrahimovic 
and some other text',
 'ie return the first sentence containing the name Zlatan Ibrahimovic',
 'zlatan ibrahimovic ']



-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: catastrophic regexp, help!

2008-06-11 Thread Maric Michaud
Le Wednesday 11 June 2008 09:08:53 Maric Michaud, vous avez écrit :
 this is zlatan example.'
 compare with 'this is zlatan example', 'z'=='.', false
 compare with 'this is zlatan ', 'z'=='e', false
 compare with 'this is zlatan', 'z'==' ', false
 compare with 'this is ', zlatan==zlatan, true

Ah no ! it stops here, but would have continued on the entire string upto the 
empty string if it doesn't contain zlatan at all.

 compare with 'this is', 'z'==' ', false
 compare with 'this ', 'z'=='i', false
 compare with 'this', 'z'==' ', false



-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: Why does python not have a mechanism for data hiding?

2008-06-11 Thread Maric Michaud
Le Wednesday 11 June 2008 08:11:02 Russ P., vous avez écrit :
 http://www.sofcheck.com

 Here is an excerpt from their website:

 SofCheck’s advanced static error detection solutions find bugs in
 programs before programs are run. By mathematically analyzing every
 line of software, considering every possible input, and every path
 through the program, SofCheck’s solutions find any and all errors that
 cause a program to crash or produce an undefined result.

Don't mix commercial discourse with technical, it desserves your point. 
Theoretically, wether a program has bugs or not is not computable. Static 
analysis as they imply is just nonsense.

AFAIK, the efforts needed to make good static analysis are proven, by 
experience, to be at least as time consuming than the efforts needed to make 
good unit and dynamic testing.

-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: Can I find out (dynamically) where a method is defined?

2008-06-10 Thread Maric Michaud
Le Monday 09 June 2008 19:03:18 [EMAIL PROTECTED], vous avez écrit :
 Thanks Maric!  That's very close to what I want, although using dir()
 can be misleading because if you invoke it on class B you get all of
 class A's methods as well.  I took your idea and wrote it up like
 this:

 def find_defining_class(obj, meth_name):
 find and return the class object that will provide
 the definition of meth_name (as a string) if it is
 invoked on obj.
 
 for ty in type(obj).mro():
 if meth_name in ty.__dict__:
 return ty
 return None

 Cheers,
 Allen


Oh ! you're just right, my first writing of this was :

for m in 'a', 'b', 'c' :
print [ t for t in type(i).mro() if m in t.__dict__ ]

which I carelessly ad wrongly rewrote using dir while posting.
Sorry.

-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to find the first space?

2008-06-09 Thread Maric Michaud
Le Monday 09 June 2008 17:02:51 Johny, vous avez écrit :
 How can I find the first space using regex?

 For example I have text
 Text=' This is a sample '

 The last space I can remove by
 Text=re.sub(r\s(?!\w),'',Text)


For spaces at the end of line, I prefer this one :

re.sub(r'\s*$', '', s)

 but I do not know how to remove the first space.
 Can anyone help?

At the beginning :

re.sub(r'^\s*', '', s)

And the one that strip the whole line (not obvious) :

re.sub(r'\s*(\S+.*\S+)\s*', r'\1', s)


You really should consider s.strip(), rstrip(), lstrip()

String manipulation methods cover almost all use cases, and give better codes, 
keep regexes for what they are really valuable (parsing complex idioms, 
second step optimisation).

I can't even remember the last time I used re module in production code in 
python (unlike in perl or shell).


 Thanks
 L.
 --
 http://mail.python.org/mailman/listinfo/python-list



-- 
_

Maric Michaud
_

Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 4 26 88 00 97
Mobile: +33 6 32 77 00 21
--
http://mail.python.org/mailman/listinfo/python-list


Re: Can I find out (dynamically) where a method is defined?

2008-06-09 Thread Maric Michaud
Le Monday 09 June 2008 17:28:45 [EMAIL PROTECTED], vous avez écrit :
 So, is there any way to inspect a method to see where (in what class)
 it
 is defined?

In [56]: class a(object) :
def a() : return
def b() :  return
   :
   :

In [59]: class b(a) :
def b() : return
def c() : return
   :
   :

In [62]: i=b()

In [63]: for m in 'a', 'b', 'c' :
   : print [ t for t in type(i).mro() if m in dir(t) ]
   :
   :
[class '__main__.a']
[class '__main__.b', class '__main__.a']
[class '__main__.b']

mro stands for method resolution order, check the reference on this 
and super, it worths the effort.

-- 
_

Maric Michaud
_

Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 4 26 88 00 97
Mobile: +33 6 32 77 00 21
--
http://mail.python.org/mailman/listinfo/python-list


Re: Can this be done with list comprehension?

2008-06-09 Thread Maric Michaud
Le Sunday 08 June 2008 02:27:54 Terry Reedy, vous avez écrit :
 Karlo Lozovina [EMAIL PROTECTED] wrote in message
 news:[EMAIL PROTECTED]

 | I figured that out few minutes ago, such a newbie mistake :). The fix I
 | came up with is:
 |
 |  result = ['something'] + [someMethod(i) for i in some_list]
 |
 | Are there any other alternatives to this approach?

 result = [something]
 result.extend(someMethod(i) for i in some_list)

 avoids creating and deleting an intermediate list



 --
 http://mail.python.org/mailman/listinfo/python-list

A one liner, though it's a bit lispy :

list(itertools.chain((something,), (someMethod(i) for i in some_list)))



-- 
_

Maric Michaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: Decorator metaclass

2008-05-22 Thread Maric Michaud
Le Friday 23 May 2008 04:28:22 [EMAIL PROTECTED], vous avez 
écrit :
 Hi,
 I would like to create a Decorator metaclass, which automatically
 turns a class which inherits from the Decorator type into a
 decorator.
 A decorator in this case, is simply a class which has all of its
 decorator implementation inside a decorator() method. Every other
 attribute access is being proxied to decorator().getParent().

 ...

 ---
 Unfortunately this does not work. The newly defined __init__ method
 inside __new__, does a call to impl(*args, **dargs). However, since
 the HBar.__init__ calls the Decorator.__init__ method, but the
 HBar.__init__ method no longer resides inside HBar, but rather inside
 HBarImpl (which is no longer a subtype of Decorator), the compiler
 complains that Decorator.__init__ is not being called with a Decorator
 instance as its first argument (which is true).
 I tried changing the definition of impl inside __new__ to have
 Decorator as one of its bases, but then for some reason impl(*args,
 **dargs) asks for 4 arguments (just like __new__) and I have no clue
 as to why that happens.

 Any help on this?


The problem with kind of design is that you must break the rules of class 
inheritance, and it seems like a strange idea to implement decorators by 
inheritance.

Of course you could do all sort of magic with python, but what is your goal ?
In your example, should the implementation types inherit from each other ?
In that case, do you want to preserve the same semantic for __init__ as in 
standard python class (this could be a hard job) ?

This quick fix seems to work with your example, but add extra magic to 
automatically call the super __init__ of the parent implementation, this 
could be a bad idea, use with caution ! (I still think it's a bad design, 
using composition and proxy classes is much more simple and clear)

class DecoratorType(type):
def __new__(cls, name, bases, dct):

# create a new class which will store all of the 
implementation
parent_impl_type = bases[0] is object and object \
   or bases[0]._impl_type
impl = type('%sImpl'%name,(parent_impl_type,),dict(dct))
dectype = type.__new__(cls, name, bases, {'_impl_type' : 
impl })

# update the old class to implement this implementation
def __init__(self, *args, **dargs):
print args, dargs
new_impl = impl(*args, **dargs)
super(dectype._impl_type, new_impl).__init__(*args,
 **dargs)
object.__setattr__(self, '_impl', new_impl)
def decorator(self):
return object.__getattribute__(self,'_impl')
def __getattribute__(self, attr):
if attr==decorator:
return 
object.__getattribute__(self,'decorator')
return getattr(object.__getattribute__(
self, 'decorator')(), attr)
dectype.__init__ = __init__
dectype.decorator = decorator
dectype.__getattribute__ = __getattribute__

return dectype

class Decorator(object):

__metaclass__ = DecoratorType

class HBar(Decorator):
def __init__(self, number):
print 'hb:', number
self._number = number
def inc(self):
self._number += 1
def p(self):
print self._number

class HBar2(HBar) :
def __init__(self, number):
print 'hb2:', number
self._hb2 = number
def inc2(self):
self._hb2 += 1
def p2(self):
print self._hb2


hbar = HBar(10)
for each in dir(hbar.decorator()):
print each

hbar.decorator().p()
hbar.decorator().inc()
hbar.decorator().p()

hb2 = HBar2(5)
hb2.p()
hb2.p2()
hb2.inc()
hb2.p()
hb2.p2()
hb2.inc2()
hb2.p()
hb2.p2()



-- 
_

Maric Michaud
_
--
http://mail.python.org/mailman/listinfo/python-list


Problem with generator expression and class definition

2007-12-07 Thread Maric Michaud
I faced a strange behavior with generator expression, which seems like a bug, 
for both 
python 2.4 and 2.5 :

 class A :
... a = 1, 2, 3
... b = 1, 2, 3
... C = list((e,f) for e in a for f in b)
...
Traceback (most recent call last):
  File stdin, line 1, in module
  File stdin, line 4, in A
  File stdin, line 4, in genexpr
NameError: global name 'b' is not defined
 class A :
... a = 1, 2, 3
... b = 1, 2, 3
... C = [ (e,f) for e in a for f in b ]
...
 A.C
[(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3)]

Any comment ? I'm ready to report it as a bug if there is no objection.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to clean a module?

2007-05-31 Thread Maric Michaud
ai a écrit :
 It assumes that there is a module A which have two global variables X
 and Y. If I run import A in the IDLE shell, then I can use A.X and
 A.Y correctly. But if I want to change the module A and then delete
 the variable Y, I find I can use A.Y just the same as before!

It's unlikely to be true, see below.

 In fact, I have tried all the following methods but can't remove the
 A.Y:
 execute import A again
 reload(A)
 del A; import A
 Yes, if you use del A.Y, it works. But it is stupid since there are
 probably many names. In my thought, if no one references objects in A,
 del A will release all memory about A. But it seems that the fact is
 not. So I can not refresh the namespace to follow changes of a module
 easily and I will worry about the memory if I del a module.
 I want to know if there is a way to clear a module entirely.
 

Actually I do not see your problem and your exact need, when I type the 
following in python prompt I just see expected behavior, what is a 
problem to you ? Maybe you could post a code explaining it.
 

In [64]: import a 

 

In [65]: a.X 

Out[65]: 0 

 

In [66]: a.X = 2 

 

In [67]: del a 

 

In [68]: import a as b 

 

In [69]: b.X 

Out[69]: 2 
 


In [71]: for name in [ n for n in b.__dict__ if not n.startswith('__') ] 
:
: b.__dict__.__delitem__(name) 

: 

: 

 

In [72]: b.X 

--- 

type 'exceptions.AttributeError'Traceback (most recent call 
last)
 

C:\Documents and Settings\maric\Bureau\ipython console in module() 

 

type 'exceptions.AttributeError': 'module' object has no attribute 'X' 

 

In [73]: reload(b) 

Out[73]: module 'a' from 'a.pyc' 

 

In [74]: b.X 

Out[74]: 0 


In [75]: del b.X 

 

In [76]: del b 

 

In [77]: import a 

 

In [78]: a.b 

--- 

type 'exceptions.AttributeError'Traceback (most recent call 
last)
 

C:\Documents and Settings\maric\Bureau\ipython console in module() 

 

type 'exceptions.AttributeError': 'module' object has no attribute 'b' 

 

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Rats! vararg assignments don't work

2007-05-30 Thread Maric Michaud
samwyse a écrit :
 George Sakkis wrote:
 On May 29, 11:33 pm, Matimus [EMAIL PROTECTED] wrote:


 Your attemtp:

 [code]
 first, rest = arglist[0], arglist[1:]
 [/code]

 Is the most obvious and probably the most accepted way to do what you
 are looking for. As for adding the fucntionality you first suggested,
 it isn't likely to be implemented. The first step would be to write a
 PEP though.

 The time machine did it again: http://www.python.org/dev/peps/pep-3132/.
 
 Thanks!  Now I just need to wait for Py3K and all of my problems will be 
 solved.  ;-)
 
 Actually, I'm surprised that the PEP does as much as it does.  If tuples 
 are implemented as S-expressions, then something like this:
  car, *cdr = tuple
 while leaving cdr a tuple would be trivial to implement.  Of course, I'm 
 an old-school LISPer, so what I consider surprising behavior doesn't 
 always surprise anyone else, and vice versa.

Remember all these are copies of the original sequence, the lisp
equivalent to car/cdr is feasible with an iterator :

it = iter(seq)
car, cdr = it.next(), it

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is PEP-8 a Code or More of a Guideline?

2007-05-29 Thread Maric Michaud


Steve Howell a écrit :
 --- Carsten Haese [EMAIL PROTECTED] wrote:
 
 On Sun, 2007-05-27 at 07:30 +, OKB (not
 okblacke) wrote:
 Underscores are harder to type than any
 alphanumeric character.  

 This is a discussion about underscores versus
 capital letters denoting
 the word boundaries in identifiers. How is an
 underscore harder to type
 than a capital letter?

 
 longer reach with the pinkie on American keyboards
 
 Slowly type the following two keywords, you'll see
 what I mean:
 
 hop_count
 hopCount
 
 Also note the number of characters.
 

While this point was certainly discussed by those who wrote the PEP 8, I
see hardly how it can be a valid argument. I used two keyboard maps in
my life for development, fr_FR and fr_CH, in fr_CH both '.' and the pair
'[', ']' are really more easy to type, and I think they appear at least
as often as '_'. All in all, I never felt more productive with any
keyboard, but, of course, I'm not a typist.


Is typist ok ? It's the google's translation for dactylo.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [B,IX] = sort(A,...) - Order for sort()-function

2007-05-29 Thread Maric Michaud
Orlando Döhring a écrit :
  
...
 A = [ 3 7 5
   0 4 2 ];
  
 # in Python: A = [[3,7,5],[0,4,2]]
  
 [B,IX] = sort(A,2)
  
 # sort by rows
 
 B =
  3 5 7
  0 2 4
 
 IX =
  1 3 2
  1 3 2
  
 # first line: 3 was formerly in the first position, 5 formerly in 
 position 3, 7 formerly in position 2
 # second line: similiarly
  
  

Like this :

In [122]: l=[[3,2,1], [4,6,5,0]]

In [123]: from operator import itemgetter

In [124]: [ list(sorted(enumerate(e), key=itemgetter(1))) for e in l ]
Out[124]: [[(2, 1), (1, 2), (0, 3)], [(3, 0), (0, 4), (2, 5), (1, 6)]]

except the old position is the first element of each tuple, you may have 
the same order with this one :


In [126]: [ list((pos, elt) for pos, elt in sorted(enumerate(e), 
key=itemgetter(1))) for e in l ]
Out[126]: [[(1, 2), (2, 1), (3, 0)], [(0, 3), (4, 0), (5, 2), (6, 1)]] 

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Can python create a dictionary from a list comprehension?

2007-05-28 Thread Maric Michaud
Pierre Quentel a écrit :
 On 27 mai, 22:55, erikcw [EMAIL PROTECTED] wrote:
 Hi,

 I'm trying to turn o list of objects into a dictionary using a list
 comprehension.
...
 
 entries = dict([ (int(d.date.strftime('%m')),d.id) for d in links] )
 
 With Python2.4 and above you can use a generator expression
 
 entries = dict( (int(d.date.strftime('%m')),d.id) for d in links )
 

You can also create dictionaries knowing only the keys the same way (ie. 
a two-dimensional array) :

In [77]: dict.fromkeys((a, b) for a in range(4) for b in range(2))
Out[78]:
{(0, 0): None,
  (0, 1): None,
  (1, 0): None,
  (1, 1): None,
  (2, 0): None,
  (2, 1): None,
  (3, 0): None,
  (3, 1): None}
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is PEP-8 a Code or More of a Guideline?

2007-05-28 Thread Maric Michaud
Ben Finney a écrit :
 Paul McGuire [EMAIL PROTECTED] writes:
 
 It is a bit reassuring that I am not the only one who turns a blind
 eye to this part of the PEP, that l_c_w_u bothers others as well.
 
 I see similar support for lower_case, and opposition to
 camelCase. It's nice that we're both reassured by what we see. What
 now?
 

So it's actually a matter of taste, and that's why PEP 8 exists, to
answer the subject's question, make decision about conventions when only
taste is in balance, am I wrong ?

Asking the question is like asking : I don't like the convention, can I
use another one for myself ? I think the answer is, of course, yes, but
it is highly discouraged... (and that's why PEP 8 exists after all).

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: os.path.walk not pruning descent tree (and I'm not happy with that behavior?)

2007-05-28 Thread Maric Michaud
I'm really sorry, for all that private mails, thunderbird is awfully 
stupid dealing with mailing lists folder.


Gabriel Genellina a écrit :
 En Sun, 27 May 2007 22:39:32 -0300, Joe Ardent [EMAIL PROTECTED] escribió:
 
 
 - iterate backwards:
 
 for i in range(len(names)-1, -1, -1):
fname = names[i]
if fname[:1]=='.':
  names.remove(fname)
 

This is not about iterating backward, this is about iterating over the
index of each element instead of iterating over the element (which must
be done begining by the end). In fact this code is both inefficient and
contains a subtle bug. If two objects compare equals in the list, you
will remove the wrong one.

It should be :

for i in range(len(names)-1, -1, -1):
if names[i][:1]=='.':
  del names[i]


 - filter and reassign in place 

Seems the best here.

 (the [:] is important):

Not so. Unless names is referenced in another namespace, simple
assignment is enough.

 names[:] = [fname for fname in names if fname[:1]!='.']
 
 (Notice that I haven't used a regular expression, and the remove method)
 


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Can I reference 1 instance of an object by more names ? rephrase

2007-05-23 Thread Maric Michaud
Stef Mientki a écrit :
 hi Bruno,
 
 after study it carefully,
 it's much more complex than I thought
 (I can't understand it completely, which is of less importance).
 Your solution works great,
 but I need one little extension,
 which I can create, but just at the cost of a lot of code.
 Maybe you can give me another hint.
 
 In the first case, this is *really* a binding, and that's one of the few 
 things that Python won't let you mess with. In the two last cases, it's 
 in fact a method call - as the use of __[get|set]item__ should make 
 obvious.

 here's an example using a property:

 class cpu_ports(object):
def __init__(self, value=0):
self._d = value
@apply
def value():
def fset(self, value):
print 'vv'
self._d = value
def fget(self):
return self._d
return property(**locals())
 
 # I need to read and write the individual bits of the byte object
 # so I can create 8 blocks of code like this, one for each bit position
 # I use it, like this
 #name1 = cpu_ports()
 #name1.p5 = True
 # or
 #name1.p[5] = True
 
  @apply   # read / write bit 5
def p5():
  def fset(self, value):
  index= 5
  value= ( value  1L )  index
  mask = ( 1L )  index
  self._d  = ( self._d  ~mask ) | value
  def fget(self):
  index= 5
  return ( self._d  index )  1
  return property(**locals())
 
 I can optimize the above code a little bit,
 but I've the feeling that I don't need to repeat this code 8 times.

something like that should just work (untested) :

def bit():
 def fset(self, value):
 index= 5
 value= ( value  1L )  index
 mask = ( 1L )  index
 self._d  = ( self._d  ~mask ) | value
 def fget(self):
 index= 5
 return ( self._d  index )  1
 return property(**locals())


class cpu_ports(object) :

 p1 = bit()
 p2 = bit()
 p3 = bit()
 p4 = bit()
 p5 = bit()

But i wonder if you shouldn't use arrays instead :

In [6]:import array

In [7]:help(array)




-- 
http://mail.python.org/mailman/listinfo/python-list


  1   2   3   >