Re: [Tutor] samples on sort method of sequence object.

2010-01-14 Thread Stefan Behnel

Lie Ryan, 14.01.2010 01:47:

On 01/14/10 06:56, Hugo Arts wrote:

On Wed, Jan 13, 2010 at 8:21 PM, Stefan Behnel stefan...@behnel.de wrote:

Hugo Arts, 13.01.2010 15:25:

Here is my solution for the general case:

from itertools import groupby
def alphanum_key(string):
   t = []
   for isdigit, group in groupby(string, str.isdigit):
   group = ''.join(group)
   t.append(int(group) if isdigit else group)
   return t

Note that this won't work in Py3, where integers and strings are not
ordered, i.e. not comparable w.r.t the  and  operators.


True. You can accommodate by writing a ComparableInt class extending
int, and implement __lt__ and __gt__.
It's not exactly succinct, but it works.


Not necessary, you can just pass the cmp= parameter to sort that checks
the chunk's type.


Note that this won't work in Py3, where the cmp parameter is gone.

Stefan

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] samples on sort method of sequence object.

2010-01-14 Thread Hugo Arts
On Thu, Jan 14, 2010 at 10:13 AM, Stefan Behnel stefan...@behnel.de wrote:

 Note that this won't work in Py3, where the cmp parameter is gone.

 Stefan

And he's right again. I should've checked for that. Well, that makes a
CmpInt class the only other solution:

class CmpInt(int):
def __lt__(self, other):
if isinstance(other, str): return True
return int.__lt__(self, other)
def __gt__(self, other):
if isinstance(other, str): return False
return int.__gt__(self, other)

Hugo
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Keeping a list of attributes of a certain type

2010-01-14 Thread Dave Angel
(You top-posted, which puts your two comments out of order.  Now the 
solution comes before the problem statement)


Guilherme P. de Freitas wrote:

Ok, I got something that seems to work for me. Any comments are welcome.


class Member(object):
def __init__(self):
pass


class Body(object):
def __init__(self):
self.members = []

def __setattr__(self, obj, value):
if isinstance(value, Member):
self.members.append(obj)
object.__setattr__(self, obj, value)
else:
object.__setattr__(self, obj, value)

def __delattr__(self, obj):
if isinstance(getattr(self, obj), Member):
self.members.remove(obj)
object.__delattr__(self, obj)
else:
object.__delattr__(self, obj)



john = Body()
john.arm = Member()
print(john.members)
del john.arm
print(john.members)


On Wed, Jan 13, 2010 at 6:24 PM, Guilherme P. de Freitas
guilhe...@gpfreitas.com wrote:
  

Hi everybody,

Here is my problem. I have two classes, 'Body' and 'Member', and some
attributes of 'Body' can be of type 'Member', but some may not. The
precise attributes that 'Body' has depend from instance to instance,
and they can be added or deleted. I need any instance of 'Body' to
keep an up-to-date list of all its attributes that belong to the class
'Member'. How do I do this?

Best,

Guilherme

--
Guilherme P. de Freitas
http://www.gpfreitas.com


If this is a class assignment, you've probably got the desired answer.  
But if it's a real-world problem, there are tradeoffs, and until those 
are known, the simplest solution is usually the best.  Usually, what's 
desired is that the object behaves as if it has an up-to-date list of...


If order of the list doesn't matter, I'd consider simply writing a 
single method, called 'members' which does a list comprehension of the 
object's attributes, calculating the list when needed.  Then use a 
decorator to make this method look like a read-only data attribute  
(untested):


class Body (object):
@property
 def members(self):
return  [obj for .   if  ]

DaveA



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Keeping a list of attributes of a certain type

2010-01-14 Thread Kent Johnson
On Wed, Jan 13, 2010 at 9:24 PM, Guilherme P. de Freitas
guilhe...@gpfreitas.com wrote:
 Hi everybody,

 Here is my problem. I have two classes, 'Body' and 'Member', and some
 attributes of 'Body' can be of type 'Member', but some may not. The
 precise attributes that 'Body' has depend from instance to instance,
 and they can be added or deleted. I need any instance of 'Body' to
 keep an up-to-date list of all its attributes that belong to the class
 'Member'. How do I do this?

If you want to keep track of attributes as they are added and deleted,
you should override __setattr__() and __delattr__() in your Body
class.
http://docs.python.org/reference/datamodel.html#customizing-attribute-access
Here is a simple example:

In [4]: class LogAttributes(object):
   ...: def __setattr__(self, name, value):
   ...: print 'setting', name, 'to', value, type(value)
   ...: object.__setattr__(self, name, value)
   ...: def __delattr__(self, name):
   ...: print 'removing', name
   ...: object.__delattr__(self, name)

In [5]: l = LogAttributes()

In [6]: l.foo = 'bar'
setting foo to bar type 'str'

In [7]: l.foo
Out[7]: 'bar'

In [8]: del l.foo
removing foo

Kent
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Keeping a list of attributes of a certain type

2010-01-14 Thread Kent Johnson
On Wed, Jan 13, 2010 at 11:15 PM, Guilherme P. de Freitas
guilhe...@gpfreitas.com wrote:
 Ok, I got something that seems to work for me. Any comments are welcome.


 class Member(object):
    def __init__(self):
        pass


 class Body(object):
    def __init__(self):
        self.members = []

    def __setattr__(self, obj, value):
        if isinstance(value, Member):
            self.members.append(obj)
            object.__setattr__(self, obj, value)
        else:
            object.__setattr__(self, obj, value)

That's fine but there is no need to duplicate the object.__setattr__() call:
   def __setattr__(self, obj, value):
   if isinstance(value, Member):
   self.members.append(obj)
   object.__setattr__(self, obj, value)

    def __delattr__(self, obj):
        if isinstance(getattr(self, obj), Member):
            self.members.remove(obj)
            object.__delattr__(self, obj)
        else:
            object.__delattr__(self, obj)

Same here.

Kent
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] looking for tutor

2010-01-14 Thread maxwell hansen

I have a decent amount of knowledge with QBASIC and am looking for a tutor who 
can help me learn the pythonic equivalent of QBASIC features and commands such 
as COLOR, LOCATE, IF...THEN, GOTO, loops, etc. as well as visual things and 
objects, which arent available in QBASIC (due to it being mostly text 
oriented). I would really appreciate this, as I am greatly enjoying learning 
python so far, but I really really need someone to help me along. thanks!

-maxwell
  
_
Your E-mail and More On-the-Go. Get Windows Live Hotmail Free.
http://clk.atdmt.com/GBL/go/196390709/direct/01/___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Searching in a file

2010-01-14 Thread spir
On Wed, 13 Jan 2010 23:05:11 -
Alan Gauld alan.ga...@btinternet.com wrote:

 But a third option is to use a split and apply it to the whole file as
 a string thereby breaking the file into as many chunks as start with
 a line containing 'NEW'...

Why not simply a regex pattern starting with NEW and ending with '\n'?

import re
pat = re.compile(NEW(.+)\n)
source = abc\defNEWghi\njkl\nmno\nNEWpqr\nstu\n
print pat.findall(source)
# == ['ghi', 'pqr']

Denis


la vita e estrany

http://spir.wikidot.com/
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Keeping a list of attributes of a certain type

2010-01-14 Thread spir
On Wed, 13 Jan 2010 20:15:21 -0800
Guilherme P. de Freitas guilhe...@gpfreitas.com wrote:

 Ok, I got something that seems to work for me. Any comments are welcome.
 
 
 class Member(object):
 def __init__(self):
 pass
 
 
 class Body(object):
 def __init__(self):
 self.members = []
 
 def __setattr__(self, obj, value):
 if isinstance(value, Member):
 self.members.append(obj)
 object.__setattr__(self, obj, value)
 else:
 object.__setattr__(self, obj, value)
 
 def __delattr__(self, obj):
 if isinstance(getattr(self, obj), Member):
 self.members.remove(obj)
 object.__delattr__(self, obj)
 else:
 object.__delattr__(self, obj)

Seems perfectly ok to me, except for if...else constructs in place of simple 
if's: an optional statement is added in case obj is of type Member; this is not 
an alternative.

if isinstance(value, Member):
self.members.append(obj)
object.__setattr__(self, obj, value)# in both cases

Same for __delattr__, indeed.

Denis


la vita e estrany

http://spir.wikidot.com/
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] looking for tutor

2010-01-14 Thread Wayne Werner
On Wed, Jan 13, 2010 at 11:58 PM, maxwell hansen
maxwellhan...@hotmail.comwrote:

  I have a decent amount of knowledge with QBASIC and am looking for a tutor
 who can help me learn the pythonic equivalent of QBASIC features and
 commands such as COLOR, LOCATE, IF...THEN, GOTO, loops, etc. as well as
 visual things and objects, which arent available in QBASIC (due to it being
 mostly text oriented). I would really appreciate this, as I am greatly
 enjoying learning python so far, but I really really need someone to help me
 along. thanks!


Well, there are lots of online tutorials available - at python.org if you
click documentation, and then there are several contributors to this list
who have also written tutorials.

And then if you get stuck or need help understanding something, if you ask
on this list, someone will usually have some insight.

HTH,
Wayne



-- 
To be considered stupid and to be told so is more painful than being called
gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness,
every vice, has found its defenders, its rhetoric, its ennoblement and
exaltation, but stupidity hasn’t. - Primo Levi
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Searching in a file

2010-01-14 Thread Alan Gauld


spir denis.s...@free.fr wrote 


But a third option is to use a split and apply it to the whole file as
a string thereby breaking the file into as many chunks as start with
a line containing 'NEW'...


Why not simply a regex pattern starting with NEW and ending with '\n'?


Because I understood the OP had to extract the info from 
the lines *following* the one with NEW. You could mess 
around with match object stat positions etc but split seemed a 
much easier solution. 


Alan G

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Keeping a list of attributes of a certain type

2010-01-14 Thread Alan Gauld


Guilherme P. de Freitas guilhe...@gpfreitas.com wrote


Here is my problem. I have two classes, 'Body' and 'Member', and some
attributes of 'Body' can be of type 'Member', but some may not. The
precise attributes that 'Body' has depend from instance to instance,
and they can be added or deleted. I need any instance of 'Body' to
keep an up-to-date list of all its attributes that belong to the class
'Member'. How do I do this?


Check the type of the attribute? Using isinstance()?

I'm not sure of the rationale - why it needs to know the type etc
But isinstance is the most obvious way that I can think of.

Alan G.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] looking for tutor

2010-01-14 Thread Alan Gauld


maxwell hansen maxwellhan...@hotmail.com wrote


I have a decent amount of knowledge with QBASIC and am looking
for a tutor who can help me learn the pythonic equivalent of QBASIC
features and commands


The original version of my tutor was based on QBASIC and Python(v1.5!)
It might still be useful for comparisons. The latest version uses VBScript
which is somewhat similar to QBASIC...

http://www.freenetpages.co.uk/hp/alan.gauld/oldtutor/index.htm

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/l2p/ 



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] looking for tutor

2010-01-14 Thread Ken G.


maxwell hansen wrote:
I have a decent amount of knowledge with QBASIC and am looking for a 
tutor who can help me learn the pythonic equivalent of QBASIC features 
and commands such as COLOR, LOCATE, IF...THEN, GOTO, loops, etc. as 
well as visual things and objects, which arent available in QBASIC 
(due to it being mostly text oriented). I would really appreciate 
this, as I am greatly enjoying learning python so far, but I really 
really need someone to help me along. thanks!


-maxwell


May I suggest ( http://inventwithpython.com/ ) that features Graphics 
and Animation in Chapter 16.


Ken
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Searching in a file

2010-01-14 Thread Lie Ryan
On 01/14/10 10:29, Hugo Arts wrote:
 On Thu, Jan 14, 2010 at 12:05 AM, Alan Gauld alan.ga...@btinternet.com 
 wrote:
 
  I prefer the next() approach.
 Rightfully so. IMO, The while loop with readline is basically the C
 version of that, for the poor people who don't have iterators.

I would often prefer while loop with next() though. Writing a state
machine with for-loop can become quite messy because of the implicit
next() on each iteration.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor