Re: [Tutor] making onthefly attributes persistent

2010-12-14 Thread Knacktus

Am 13.12.2010 23:50, schrieb Jojo Mwebaze:



On Mon, Dec 13, 2010 at 8:44 PM, Alan Gauld mailto:alan.ga...@btinternet.com>> wrote:


"Jojo Mwebaze" mailto:jojo.mweb...@gmail.com>> wrote

Assuming i have a class bank as below .

class bank(object):
  def __init__(self, bal=0):
  self.bal = bal
  def deposit(self, amount):
  self.bal+=amount
  print self.bal

I define a method debit - which i add to the class onthefly

bank.debit = debit


#I can also add an attribute owner

myaccount.owner = 'jojo'


My problem is how to make the added attributes, 'owner' and 'debit'
persistent automatically


If that's your only problem with this approach congratulations!
How does your orther code know when/if these dynamic
operations/data exist so as to use them? If they just assume
they exist then why not just add them in the definition. Even as nulls?

While Python allows you to dynamically add features to classes/objects
its not something I would recommend unless you have a really good
reason - not least because you bring upon yourself all sorts of
problems!

If you are determined to do so you can make the objects persistent
using the approach I outline on my tutorial but adding a loop to cycle
over the contents of dir(). But you may find that recovering the
objects - especially if they have a mixed set of attribnutes - presents
even more problems...

IMHO This is a feature of python that should be considered unorthodox
and only to be used when no other approach will work!

HTH,




Thanks Allan for the feedback, the idea is to write a method like
store() on the object, that probably looks up all these changes and
commits them into the database.


Assuming you're planning to use a database with a scheme (e.g. a 
relational DB) you need to define your tables in advance anyway. Even if 
you're using a schemeless DB, where do you track which data is in it? 
How do you build your queries dynamically? This can become very messy.
Overall, I think Alan's recommendation to avoid dynamic attribute 
creation is the one to follow.




Please let me know where to find approach you propose in your tutorial.
I read your tutorial when i was just initiated to python, a reference
would be helpful to help me find the soln without hustle.

Cheers



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


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




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


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


Re: [Tutor] making onthefly attributes persistent

2010-12-13 Thread ALAN GAULD
The OOP topic in my tutor has a section on persisting objects 
by writing them to a text file. The basic principle being that 
each subclass only persists the attributes that it adds and 
relies on the superclass to persist itself.

In this case you would have to do someting like get the save() 
method to return the list of attributes persisted. Then the bottom 
subclass could see what all the superclasses had saved and 
only save any new attributes not in that list. 

The trick is unwinding that when you come to restore your 
objects. You will need to store the data as name/value pairs so 
that you can read the dynamic attributes, then use setattr() 
or similar to create them dynamically again. The problem is 
how do you know that nobody has created a subclass below 
the bottom level?!" So that leaves you trying to read the entire 
list of attributes in at once at the top level and restore them
which breaks the concept of objects only knowing about their 
own data...

Its all a bit messy, it is so much easier if you don't use 
dynamic attributes.

 Alan Gauld
Author of the Learn To Program website
http://www.alan-g.me.uk/



>
>From: Jojo Mwebaze 
>To: Alan Gauld 
>Cc: tutor@python.org
>Sent: Monday, 13 December, 2010 22:50:49
>Subject: Re: [Tutor] making onthefly attributes persistent
>
>
>
>
>On Mon, Dec 13, 2010 at 8:44 PM, Alan Gauld  wrote:
>
>
>>"Jojo Mwebaze"  wrote
>>
>>
>>Assuming i have a class bank as below .
>>>
>>>class bank(object):
>>> def __init__(self, bal=0):
>>> self.bal = bal
>>> def deposit(self, amount):
>>> self.bal+=amount
>>> print self.bal
>>>
>>>I define a method debit - which i add to the class onthefly
>>>
>>>
bank.debit = debit
>>>
>>>
>>>#I can also add an attribute owner
>>>
>>>myaccount.owner = 'jojo'
>>>
>
>My problem is how to make the added attributes, 'owner' and 'debit'
>>persistent automatically
>>

If that's your only problem with this approach congratulations!
>How does your orther code know when/if these dynamic
>operations/data exist so as to use them? If they just assume
>they exist then why not just add them in the definition. Even as nulls?
>
>While Python allows you to dynamically add features to classes/objects
>its not something I would recommend unless you have a really good
>reason - not least because you bring upon yourself all sorts of problems!
>
>If you are determined to do so you can make the objects persistent
>using the approach I outline on my tutorial but adding a loop to cycle
>over the contents of dir(). But you may find that recovering the
>objects - especially if they have a mixed set of attribnutes - presents
>even more problems...
>
>IMHO This is a feature of python that should be considered unorthodox
>and only to be used when no other approach will work!
>
>HTH,
>
>
>


Thanks Allan for the feedback, the idea is to write a method like store() on 
the 
object, that probably  looks up all these changes and commits them into the 
database.

Please let me know where to find approach you propose in your tutorial. I read 
your tutorial when i was just initiated to python, a reference would be helpful 
to help me find the soln without hustle.

Cheers



 
-- 
>Alan Gauld
>Author of the Learn to Program web site
>http://www.alan-g.me.uk/
>
>
>___
>Tutor maillist  -  Tutor@python.org
>To unsubscribe or change subscription options:
>http://mail.python.org/mailman/listinfo/tutor
>
>___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] making onthefly attributes persistent

2010-12-13 Thread Jojo Mwebaze
On Mon, Dec 13, 2010 at 8:44 PM, Alan Gauld wrote:

>
> "Jojo Mwebaze"  wrote
>
>  Assuming i have a class bank as below .
>>
>> class bank(object):
>>  def __init__(self, bal=0):
>>  self.bal = bal
>>  def deposit(self, amount):
>>  self.bal+=amount
>>  print self.bal
>>
>> I define a method debit - which i add to the class onthefly
>>
>> bank.debit = debit
>>
>>
>> #I can also add an attribute owner
>>
>> myaccount.owner = 'jojo'
>>
>
>  My problem is how to make the added attributes, 'owner' and 'debit'
>> persistent automatically
>>
>
> If that's your only problem with this approach congratulations!
> How does your orther code know when/if these dynamic
> operations/data exist so as to use them? If they just assume
> they exist then why not just add them in the definition. Even as nulls?
>
> While Python allows you to dynamically add features to classes/objects
> its not something I would recommend unless you have a really good
> reason - not least because you bring upon yourself all sorts of problems!
>
> If you are determined to do so you can make the objects persistent
> using the approach I outline on my tutorial but adding a loop to cycle
> over the contents of dir(). But you may find that recovering the
> objects - especially if they have a mixed set of attribnutes - presents
> even more problems...
>
> IMHO This is a feature of python that should be considered unorthodox
> and only to be used when no other approach will work!
>
> HTH,
>
>
>

Thanks Allan for the feedback, the idea is to write a method like store() on
the object, that probably looks up all these changes and commits them into
the database.

Please let me know where to find approach you propose in your tutorial. I
read your tutorial when i was just initiated to python, a reference would be
helpful to help me find the soln without hustle.

Cheers





> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] making onthefly attributes persistent

2010-12-13 Thread Alan Gauld


"Jojo Mwebaze"  wrote


Assuming i have a class bank as below .

class bank(object):
  def __init__(self, bal=0):
  self.bal = bal
  def deposit(self, amount):
  self.bal+=amount
  print self.bal

I define a method debit - which i add to the class onthefly

bank.debit = debit

#I can also add an attribute owner

myaccount.owner = 'jojo'



My problem is how to make the added attributes, 'owner' and 'debit'
persistent automatically


If that's your only problem with this approach congratulations!
How does your orther code know when/if these dynamic
operations/data exist so as to use them? If they just assume
they exist then why not just add them in the definition. Even as 
nulls?


While Python allows you to dynamically add features to classes/objects
its not something I would recommend unless you have a really good
reason - not least because you bring upon yourself all sorts of 
problems!


If you are determined to do so you can make the objects persistent
using the approach I outline on my tutorial but adding a loop to cycle
over the contents of dir(). But you may find that recovering the
objects - especially if they have a mixed set of attribnutes - 
presents

even more problems...

IMHO This is a feature of python that should be considered unorthodox
and only to be used when no other approach will work!

HTH,


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


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


[Tutor] making onthefly attributes persistent

2010-12-13 Thread Jojo Mwebaze
Hey Tutor

Assuming i have a class bank as below .

class bank(object):
   def __init__(self, bal=0):
   self.bal = bal
   def deposit(self, amount):
   self.bal+=amount
   print self.bal

I define a method debit - which i add to the class onthefly

def debit(self, amt):
   self.bal-=amt
   print self.bal

bank.debit = debit

myacct = bank()
myacct.deposit(1000) # prints 1000
myacct.debit(99) # print 901

#I can also add an attribute owner

myaccount.owner = 'jojo'

dir(myacct) # prints [ 'owner', 'bal', 'debit', 'deposit']


My problem is how to make the added attributes, 'owner' and 'debit'
persistent automatically

Saving the object using pickle for example does save 'owner' and 'debit'
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor