Partial classes

S

Sanjay

Hi All,

Not being able to figure out how are partial classes coded in Python.

Example: Suppose I have a code generator which generates part of a
business class, where as the custome part is to be written by me. In
ruby (or C#), I divide the code into two source files. Like this:

GeneratedPerson.rb
Class Person
.
.
.

End Class

HandcraftedPerson.rb
Class Person
.
.
.
End Class

The intrepretor adds the code in both to compose the class Person.

What is the equivalent in Python? Inheriting is a way, but is not
working in all scenerios.

Thanks
Sanjay
 
A

alex23

Sanjay said:
Not being able to figure out how are partial classes coded in Python.

Hi Sanjay,

To the best of my knowledge, Python currently has no support for
partial classes.

However, BOO (http://boo.codehaus.org/) - which is a Python-like
language for the .NET CLI)- _does_ support partial classes
(http://jira.codehaus.org/browse/BOO-224). While it _isn't_ Python,
there could be enough similarities to make this worthwhile for you if
you absolutely have to have partial classes.

(Disclaimer: I've never used BOO)

Hope this helps.

-alex23
 
K

Kay Schluehr

Sanjay said:
Hi All,

Not being able to figure out how are partial classes coded in Python.

Example: Suppose I have a code generator which generates part of a
business class, where as the custome part is to be written by me. In
ruby (or C#), I divide the code into two source files. Like this:

GeneratedPerson.rb
Class Person
.
.
.

End Class

HandcraftedPerson.rb
Class Person
.
.
.
End Class

The intrepretor adds the code in both to compose the class Person.

What is the equivalent in Python? Inheriting is a way, but is not
working in all scenerios.

Thanks
Sanjay

Python has no notion of a partial class because it is a pure compile
time construct. You might merge different class definitions by means of
a meta class that puts everything together but whether or not certain
methods in the merged class are available depends on which modules are
imported. This might give rise to a "virtual" or runtime module. It is
not a module that refers to a physical file on the disc but is a pure
runtime construct. When creating this module all physical modules that
define class fragments might be put together by means of the metaclass
mechanism. I indeed used this construction to unify different access
points before I reimplemented it using partial classes in C# which are
very fine IMO.
 
S

Sanjay

Hi Alex,

Thanks for the input.

Being new to Python, and after having selected Python in comparison to
ruby (Turbogears vs Rails) , is jerks me a bit. In my openion it should
be an obvious and easy to implement feature and must be, if not already
have been, planned in future releases of Python.

Would love to listen to others.

Sanjay
 
M

Marc 'BlackJack' Rintsch

Being new to Python, and after having selected Python in comparison to
ruby (Turbogears vs Rails) , is jerks me a bit. In my openion it should
be an obvious and easy to implement feature and must be, if not already
have been, planned in future releases of Python.

Can you flesh out your use case a little bit and tell why you can't solve
the problem with inheritance or a meta class?

Ciao,
Marc 'BlackJack' Rintsch
 
D

Dave Benjamin

What is the equivalent in Python? Inheriting is a way, but is not
working in all scenerios.

Have you tried multiple inheritance? For example:

from GeneratedPerson import GeneratedPerson
from HandcraftedPerson import HandcraftedPerson

class Person(GeneratedPerson, HandcraftedPerson):
pass

If this doesn't work for you, can you explain why?

Dave
 
D

Daniel Dittmar

Sanjay said:
Hi All,

Not being able to figure out how are partial classes coded in Python.

Example: Suppose I have a code generator which generates part of a
business class, where as the custome part is to be written by me. In
ruby (or C#), I divide the code into two source files. Like this:

GeneratedPerson.rb
Class Person
.
.
.

End Class

HandcraftedPerson.rb
Class Person
.
.
.
End Class

The intrepretor adds the code in both to compose the class Person.

What is the equivalent in Python? Inheriting is a way, but is not
working in all scenerios.

# HandcraftedPerson.py
import GeneratedPerson

class Person:
def somemethod (self):
pass


GeneratedPerson.Person.somemethod = Person.somemethod

Using reflection to merge all methods of HandcraftedPerson.Person into
GeneratedPerson.Person is left as an exercise.

Daniel
 
P

Peter Otten

Sanjay said:
Hi All,

Not being able to figure out how are partial classes coded in Python.

Example: Suppose I have a code generator which generates part of a
business class, where as the custome part is to be written by me. In
ruby (or C#), I divide the code into two source files. Like this:

GeneratedPerson.rb
Class Person
.
.
.

End Class

HandcraftedPerson.rb
Class Person
.
.
.
End Class

The intrepretor adds the code in both to compose the class Person.

What is the equivalent in Python? Inheriting is a way, but is not
working in all scenerios.

I, like everybody else it seems, am interested to know why/when (multiple)
inheritance doesn't work. Meanwhile

# this is a hack
import inspect
import textwrap

class Generated:
def generated(self):
print "generated"

def class_body(Class):
return textwrap.dedent(inspect.getsource(Class).split("\n", 1)[1])

class Handmade:
exec class_body(Generated)
def handmade(self):
print "handmade"

if __name__ == "__main__":
print dir(Handmade)
Handmade().generated()

Peter
 
S

Sanjay

Can you flesh out your use case a little bit and tell why you can't solve
the problem with inheritance or a meta class?

I have to study about metaclass and see whether this can be handled. It
seemed inheritence is not working.

PROBLEM: Separating plumbing code and business logic while using
SQLAlchemy ORM.

Database script:

CREATE TABLE person (
id SERIAL,
passport VARCHAR(50) NOT NULL,
blocked BOOLEAN NOT NULL DEFAULT FALSE,
first_name VARCHAR(30) NOT NULL,
middle_name VARCHAR(30) NULL,
last_name VARCHAR(30) NOT NULL,
email VARCHAR(100) NOT NULL,
used_bytes INTEGER NOT NULL DEFAULT 0,
PRIMARY KEY(id)
);

CREATE TABLE contact (
person_id INTEGER NOT NULL REFERENCES person,
contact_id INTEGER NOT NULL REFERENCES person,
favorite BOOLEAN NOT NULL DEFAULT FALSE,
PRIMARY KEY(person_id, contact_id)
);

DB definitions and plumbing code goes into one module, say db.py:

import sqlalchemy.mods.threadlocal
from sqlalchemy import *

global_connect('postgres://userid:password@localhost:5432/tm')
person_tbl = Table('person', default_metadata, autoload = True)
class Person(object):
pass
contact_tbl = Table('contact', default_metadata, autoload = True)
class Contact(object):
pass
assign_mapper(Person, person_tbl, properties = {
'contacts' :
relation(Contact,
primaryjoin=person_tbl.c.id==contact_tbl.c.person_id,
association=Person)
})

assign_mapper(Contact, contact_tbl, properties = {
'person' :
relation(Person,
primaryjoin=person_tbl.c.id==contact_tbl.c.contact_id)
})

Business logic in another module, say bo.py

Class PersonBO(Person):
def Block():
blocked = True

While using PersonBO in another module, like this:

p1 = PersonBO(passport = "(e-mail address removed)", first_name='john',
last_name='smith', email = "(e-mail address removed)")
p2 = PersonBO(passport = "(e-mail address removed)", first_name='ed',
last_name='helms', email = "(e-mail address removed)")
p3 = PersonBO(passport = "(e-mail address removed)", first_name='jonathan',
last_name='lacour', email = "(e-mail address removed)")

# add a contact
p1.contacts.append(Contact(person=p2))

the following error message occurs:

AttributeError: 'PersonBO' object has no attribute 'contacts'

What I guess, from my limited knowledge of the technologies involved,
is that assign_mapper does some magic only on Person class, and things
work. But after inheritence, it is not working.

The point in general, to my knowledge, about inheritance is that it
can't be a substitute for all the usages of partical classes. Metaclass
is a new concept for me, which I have to study.

As far as my project is concerned, I have found out some other way of
doing the things, and it is no more an issue.

Thanks a lot,
Sanjay
 
B

Bruno Desthuilliers

Sanjay said:
Hi Alex,

Thanks for the input.

Being new to Python, and after having selected Python in comparison to
ruby (Turbogears vs Rails) , is jerks me a bit. In my openion it should
be an obvious and easy to implement feature and must be, if not already
have been, planned in future releases of Python.

Would love to listen to others.

I've never had a use case for this kind of feature in the past seven years.
 
B

brianmce

Sanjay said:
Hi All,

Not being able to figure out how are partial classes coded in Python.

Example: Suppose I have a code generator which generates part of a
business class, where as the custome part is to be written by me. In
ruby (or C#), I divide the code into two source files. Like this:

I would do this by inheritance if really needed - what isn't working?
That said, you can get this behaviour fairly easy with a metaclass:

class PartialMeta(type):
registry = {}
def __new__(cls,name,bases,dct):
if name in PartialMeta.registry:
cls2=PartialMeta.registry[name]
for k,v in dct.items():
setattr(cls2, k, v)
else:
cls2 = type.__new__(cls,name,bases,dct)
PartialMeta.registry[name] = cls2
return cls2

class PartialClass(object):
__metaclass__=PartialMeta

use:
#generatedperson.py
class Person(PartialClass):
def foo(self): print "foo"

#gandcraftedperson.py
import generatedperson
class Person(PartialClass):
def bar(self): print "bar"

and you should get similar behaviour.

Caveats:
I've used just the name to determine the class involved to be the
same as your Ruby example. However, this means that any class in any
namespace with the name Person and inheriting from PartialClass will be
interpreted as the same - this might not be desirable if some other
library code has a different Person object doing the same thing. Its
easy to change to checking for some property instead - eg. have your
handcrafted class have the line "__extends__=generatedperson.Person" ,
and check for it in the metaclass instead of looking in a name
registry.

Also, if two or more classes define the same name, the last one
evaluated will overwrite the previous one.
 
S

Sanjay

Thanks for the code showing how to implement partial classes. Infact, I
was searching for this code pattern. I will have a study on metaclass
and then try it.

Thanks
Sanjay
 
K

Kay Schluehr

Sanjay said:
Hi All,

Not being able to figure out how are partial classes coded in Python.

Example: Suppose I have a code generator which generates part of a
business class, where as the custome part is to be written by me. In
ruby (or C#), I divide the code into two source files. Like this:

I would do this by inheritance if really needed - what isn't working?
That said, you can get this behaviour fairly easy with a metaclass:

class PartialMeta(type):
registry = {}
def __new__(cls,name,bases,dct):
if name in PartialMeta.registry:
cls2=PartialMeta.registry[name]
for k,v in dct.items():
setattr(cls2, k, v)
else:
cls2 = type.__new__(cls,name,bases,dct)
PartialMeta.registry[name] = cls2
return cls2

class PartialClass(object):
__metaclass__=PartialMeta

This definition lacks a check for disjointness of the parts. No two
partial classes shall contain a method with the same name.
 
B

brianmce

Kay said:
This definition lacks a check for disjointness of the parts. No two
partial classes shall contain a method with the same name.

Yes - I mentioned at the bottom that the last one evaluated will
overwrite any existing one. You're right that its probably a better
idea to check for it and throw an exception.
 
J

John Salerno

Marc said:
Can you flesh out your use case a little bit and tell why you can't solve
the problem with inheritance or a meta class?

From my experience with C#, the only real use for partial classes is
when you want to separate your GUI code from the rest of your logic. But
since GUI programming in Python isn't always done, this isn't as big of
a deal.

Aside from that, if you really need to split up your classes, it's
probably an indication that you could create multiple classes instead,
right?
 
B

Bruno Desthuilliers

John said:
From my experience with C#, the only real use for partial classes is
when you want to separate your GUI code from the rest of your logic.

What the ... is GUI code doing in a domain object ???
 
B

Bruno Desthuilliers

Sanjay said:
I have to study about metaclass and see whether this can be handled. It
seemed inheritence is not working.

PROBLEM: Separating plumbing code and business logic while using
SQLAlchemy ORM.
(snip)
DB definitions and plumbing code goes into one module, say db.py:

import sqlalchemy.mods.threadlocal
from sqlalchemy import *

global_connect('postgres://userid:password@localhost:5432/tm')
person_tbl = Table('person', default_metadata, autoload = True)
class Person(object):
pass (snip)

Business logic in another module, say bo.py

Class PersonBO(Person):
def Block():
blocked = True

<OT>
shouldn't it be:
class PersonBO(Person):
def block(self):
self.blocked = True
While using PersonBO in another module, like this:

p1 = PersonBO(passport = "(e-mail address removed)", first_name='john',
last_name='smith', email = "(e-mail address removed)")
p2 = PersonBO(passport = "(e-mail address removed)", first_name='ed',
last_name='helms', email = "(e-mail address removed)")
p3 = PersonBO(passport = "(e-mail address removed)", first_name='jonathan',
last_name='lacour', email = "(e-mail address removed)")

# add a contact
p1.contacts.append(Contact(person=p2))

the following error message occurs:

AttributeError: 'PersonBO' object has no attribute 'contacts'
Strange.

What I guess, from my limited knowledge of the technologies involved,
is that assign_mapper does some magic only on Person class, and things
work. But after inheritence, it is not working.

Anyway, there are other ways to reuse implementation, like
composition/delegation. You may want to have a look at
__getattr__/__setattr__ magic methods.
As far as my project is concerned, I have found out some other way of
doing the things,

Care to explain your solution ?
 
K

Kay Schluehr

John said:
From my experience with C#, the only real use for partial classes is
when you want to separate your GUI code from the rest of your logic. But
since GUI programming in Python isn't always done, this isn't as big of
a deal.

Experiences have the tendency to differ.

What about letting your teammates editing certain data-structures in
different files ( physical modules ) but using them in a uniform way
and enable a single access point. If you have partial classes there is
no reason why your team has to share a large file where they have to
edit a single class but break the class into different parts and edit
the parts separately. No one has to care for including any module
because the CLR fits all partial classes together at compile time. I
find this quite amazing.
Aside from that, if you really need to split up your classes, it's
probably an indication that you could create multiple classes instead,
right?

Just infrastructure overhead. I thought people are Pythonistas here and
not Java minds?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top