[Tutor] Dynamic inheritance?

2005-11-19 Thread Jan Eden
Hi,

I have a number of classes, each of which should inherit from a related 
superclass in another module:

class A(Super.A):
...

class B(Super.B):
...

class C(Super.C):
...

Is there a way to dynamically determine the value of Super at runtime? 
Background: Depending on certain object attributes which are set during the 
object initialization, I need to use a different set of templates for the 
respective object.

Example: If a page object belongs to site A, it needs to inherit from the 
template class SiteA.Page, a gallery object belonging to site B should inherit 
from SiteB.Gallery etc.

The problem is that the template classes are not self contained, i.e. 
SiteB.Gallery inherits from SiteB.List, SiteB.Base etc.

Thanks in advance,

Jan
-- 
Any sufficiently advanced technology is insufficiently documented.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Dynamic inheritance?

2005-11-19 Thread Python
On Sat, 2005-11-19 at 16:45 +0100, Jan Eden wrote:
 
 Is there a way to dynamically determine the value of Super at runtime? 
 Background: Depending on certain object attributes which are set during the 
 object initialization, I need to use a different set of templates for the 
 respective object.
 
If you use new style classes, then there is a super function that can be
used to automatically resolve a superclass reference.  You can force new
style classes by:

inherit from object easy to see, but repetitive
__metaclass__ = typeput before the class statements

super(Myclass,self).__init__(...)
will search through the inheritance tree and (in this case) invoke
__init__.


Use __bases__ to run up the inheritance yourself.

 class A:
... pass
...
 class B(A):
... pass
...
 b.__class__.__bases__
(class __main__.A at 0xb7e9311c,)

 B.__bases__
(class __main__.A at 0xb7e9311c,)

The magic class attributes don't get listed by the dir command so you
need to search the documentation to find this.

http://docs.python.org/lib/specialattrs.html

 dir(B)
['__doc__', '__module__']
 b = B()
 dir(b)
['__doc__', '__module__']


-- 
Lloyd Kvam
Venix Corp

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Dynamic inheritance?

2005-11-19 Thread Kent Johnson
Jan Eden wrote:
 Hi,
 
 I have a number of classes, each of which should inherit from a
 related superclass in another module:
 
 class A(Super.A): ...
 
 class B(Super.B): ...
 
 class C(Super.C): ...
 
 Is there a way to dynamically determine the value of Super at
 runtime? Background: Depending on certain object attributes which are
 set during the object initialization, I need to use a different set
 of templates for the respective object.

Sure, just import the correct template module conditionally and give it a new 
name:

if site == 'siteA':
  import SiteA as Super
else
  import SiteB as Super

class A(Super.A):

etc.

If you need to do the import based on a string use the __import__() function:
http://www.python.org/doc/current/lib/built-in-funcs.html

 The problem is that the template classes are not self contained, i.e.
 SiteB.Gallery inherits from SiteB.List, SiteB.Base etc. 
 Thanks in advance,

I don't see why that is a problem?

Kent
-- 
http://www.kentsjohnson.com

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Dynamic inheritance?

2005-11-19 Thread Jan Eden
Hi Kent,

Kent Johnson wrote on 19.11.2005:

Jan Eden wrote:

 Is there a way to dynamically determine the value of Super at
 runtime? Background: Depending on certain object attributes which are
 set during the object initialization, I need to use a different set
 of templates for the respective object.

Sure, just import the correct template module conditionally and give it a new 
name:

if site == 'siteA':
  import SiteA as Super
else
  import SiteB as Super

class A(Super.A):

etc.

If you need to do the import based on a string use the __import__() function:
http://www.python.org/doc/current/lib/built-in-funcs.html

Excellent. This is exactly what I need. I was not aware of the fact that you 
can rename modules upon importing them.

 The problem is that the template classes are not self contained, i.e.
 SiteB.Gallery inherits from SiteB.List, SiteB.Base etc. 
 Thanks in advance,

I don't see why that is a problem?

Please disregard the last statement in my first message: There is indeed no 
problem with this requirement.

Thanks for your help,

Jan
-- 
Common sense is what tells you that the world is flat.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] How should I store data ?

2005-11-19 Thread Bojan Raicevic
I want to store data in the shape of (for example):  First name, Last name, Age, ... few more characteristics.  I must be able to search data by any of this fields. For example, search for age, or name ... etc  Also, I want to put option in main program to change (update), add and remove that data.  How do I do that (easiest way, please) ?  
		 Yahoo! FareChase - Search multiple travel sites in one click.

 

 ___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Dynamic inheritance?

2005-11-19 Thread Python
On Sat, 2005-11-19 at 15:23 -0800, Danny Yoo wrote:

 Here's a small example that shows how classes can be treated just like any
 other value in Python:
 
 #
 def makeYa(superclass):
 class Ya(superclass):
 def sayHi(self):
 superclass.sayHi(self)
 print ya
 return Ya
 

Sorry I totally misunderstood.  

Essentially, you want the super class(es) in the class statement to be
variables.  It's a good thing Danny and Kent were around.

 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor
-- 
Lloyd Kvam
Venix Corp

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] smtplib alternative???

2005-11-19 Thread Adisegna
Hello,

I found this script to send mail online. It works fine but requires me
to enter an mail server. I'm looking for something else that doesn't
require and SMTP server. Having to specify a mail server prohibits me
from sending to alternate domains. 

Thanks in advance


smtpserver = 'my.mailserver.com'
 RECIPIENTS = ['[EMAIL PROTECTED]']
 SENDER = '[EMAIL PROTECTED]'
 mssg = SomeVariable

session = smtplib.SMTP(smtpserver)

smtpresult = session.sendmail(SENDER, RECIPIENTS, mssg)
 if smtpresult:

errstr = 

for recip in smtpresult.keys():

errstr = Could not delivery mail to: %s


Server said: %s 
 %s


%s % (recip, smtpresult[recip][0], smtpresult[recip][1], errstr)

raise smtplib.SMTPException, errstr
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Dynamic inheritance?

2005-11-19 Thread Kent Johnson
Danny Yoo wrote:
 Here's a small example that shows how classes can be treated just like any
 other value in Python:
 
 #
 def makeYa(superclass):
 class Ya(superclass):
 def sayHi(self):
 superclass.sayHi(self)
 print ya
 return Ya
 
 class ValleyPerson:
 def sayHi(self):
 print like, so, oh my gosh, hi?
 
 FrankensteinClass = makeYa(ValleyPerson)
 instance = FrankensteinClass()
 instance.sayHi()
 #

Yeah, that'll work too :-) Or even

def makeAB(moduleContainingSuperClass):
  class A(moduleContainingSuperClass.A):
pass
  class B(moduleContainingSuperClass.B)
pass
  return A, B

A, B = makeAB(siteA)
 
 So there's no need to do trickery with conditional imports; Python treats
 classes as first-class objects that can be passed around.

Trickery? ;) It's just using modules as first-class objects that are bound to 
names same as anything else. Which solution is better depends on whether you 
want to choose at the module level or with finer control.

Kent
-- 
http://www.kentsjohnson.com

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How should I store data ?

2005-11-19 Thread Danny Yoo


On Sat, 19 Nov 2005, Bojan Raicevic wrote:

 I want to store data in the shape of (for example):
   First name, Last name, Age, ... few more characteristics.
   I must be able to search data by any of this fields. For example,
 search for age, or name ... etc
   Also, I want to put option in main program to change (update), add and
 remove that data.
   How do I do that (easiest way, please) ?

Hi Bojan,

If you're not bound to use Python directly, I'd recommend looking into a
relational database.  Relational databases may solve the problem you're
describing: to search by any field, to handle storage and loading, and to
support those operations efficiently.

There are few free ones available that you can experiment with.  One
popular one is sqlite.

http://www.sqlite.org/

If you want to then write a nice interface around the database, you can
take advantage of the pysqlite module:

http://initd.org/projects/pysqlite

If you have more questions, please feel free to ask.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] smtplib alternative???

2005-11-19 Thread Danny Yoo


On Sat, 19 Nov 2005, Adisegna wrote:

 I found this script to send mail online. It works fine but requires me
 to enter an mail server. I'm looking for something else that doesn't
 require and SMTP server. Having to specify a mail server prohibits me
 from sending to alternate domains.

Hi Adisegna,

I've always assumed that emails have to talk with some SMTP server.
RFC821 seems to confirm this:

http://www.faqs.org/rfcs/rfc821.html

so what you're asking, to be able to send mail without an SMTP server, may
not be possible.  There is an 'smtpd' module that comes with Python:

http://www.python.org/doc/lib/module-smtpd.html

I'm also not sure I understand the reason you're trying to avoid talking
to an outside smtp server.  But again, I'm unfamiliar enough with how the
SMTP email protocol really works that perhaps I'm just overlooking
something.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Dynamic inheritance?

2005-11-19 Thread Jan Eden
Kent Johnson wrote on 19.11.2005:

Danny Yoo wrote:
Here's a small example that shows how classes can be treated just
like any other value in Python:
 
 #
 def makeYa(superclass):
 class Ya(superclass):
 def sayHi(self):
 superclass.sayHi(self)
 print ya
 return Ya
 
 class ValleyPerson:
 def sayHi(self):
 print like, so, oh my gosh, hi?
 
 FrankensteinClass = makeYa(ValleyPerson)
 instance = FrankensteinClass()
 instance.sayHi()
 #

Yeah, that'll work too :-) Or even

def makeAB(moduleContainingSuperClass):
  class A(moduleContainingSuperClass.A):
pass
  class B(moduleContainingSuperClass.B)
pass
  return A, B

A, B = makeAB(siteA)
 
So there's no need to do trickery with conditional imports; Python
treats classes as first-class objects that can be passed around.

Trickery? ;) It's just using modules as first-class objects that are
bound to names same as anything else. Which solution is better
depends on whether you want to choose at the module level or with
finer control.

Hm, sounds like there's more than one way to do it. ;-) Thanks a lot for your 
examples.

I hate to confess that I incorrectly described my requirements. It turns out 
that I am better off with a (variant of the) state design pattern. But I am 
stuck there, too.

The situation is this:

For every object, two string attributes are determined during initialization 
(one being read from a database, the other inherited as a superclass 
attribute). The first contains the site name, the second the object type. I'd 
like to set the template attribute to the proper class:

import Templates

self.site_name = 'SiteA'
self.template_type = 'Page'

self.template = ???

self.template should refer to Templates.SiteA.Page in this case (where SiteA is 
a module of package Templates, and Page is a class within that module). Danny 
already said that classes can be treated just like any other object - but how 
do I get from a string to a class or module name?

Thanks for all your help, and sorry for my ignorance,

Jan
-- 
Any sufficiently advanced technology is indistinguishable from a Perl script. - 
Programming Perl
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor