473,396 Members | 1,754 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Partial classes

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
Jul 19 '06 #1
26 4095
Sanjay wrote:
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

Jul 19 '06 #2

Sanjay wrote:
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.

Jul 19 '06 #3
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

Jul 19 '06 #4
In <11**********************@i3g2000cwc.googlegroups. com>, Sanjay wrote:
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
Jul 19 '06 #5
On Wed, 18 Jul 2006, Sanjay wrote:
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
Jul 19 '06 #6
Sanjay wrote:
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
Jul 19 '06 #7
Sanjay wrote:
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
Jul 19 '06 #8
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 = "sk*******@hotmail.com", first_name='john',
last_name='smith', email = "sk*******@yahoo.com")
p2 = PersonBO(passport = "sk*******@yahoo.com", first_name='ed',
last_name='helms', email = "sk*******@yahoo.com")
p3 = PersonBO(passport = "sk*******@yahoo.com", first_name='jonathan',
last_name='lacour', email = "sk*******@yahoo.com")

# 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

Jul 19 '06 #9
Sanjay wrote:
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.

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jul 19 '06 #10

Bruno Desthuilliers wrote:
Sanjay wrote:
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.
Interesting. Are there other use cases you did not have too?

Jul 19 '06 #11
Sanjay wrote:
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.

Jul 19 '06 #12
Kay Schluehr wrote:
Bruno Desthuilliers wrote:
>>Sanjay wrote:
>>>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.


Interesting. Are there other use cases you did not have too?
Probably quite a lot, why ?-)
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jul 19 '06 #13
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

Jul 19 '06 #14

br******@gmail.com wrote:
Sanjay wrote:
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.

Jul 19 '06 #15

Kay Schluehr wrote:
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.

Jul 19 '06 #16
Marc 'BlackJack' Rintsch wrote:
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?
Jul 19 '06 #17
John Salerno wrote:
Marc 'BlackJack' Rintsch wrote:
>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.
What the ... is GUI code doing in a domain object ???

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jul 19 '06 #18
Sanjay wrote:
>>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.
(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
</OT>
While using PersonBO in another module, like this:

p1 = PersonBO(passport = "sk*******@hotmail.com", first_name='john',
last_name='smith', email = "sk*******@yahoo.com")
p2 = PersonBO(passport = "sk*******@yahoo.com", first_name='ed',
last_name='helms', email = "sk*******@yahoo.com")
p3 = PersonBO(passport = "sk*******@yahoo.com", first_name='jonathan',
last_name='lacour', email = "sk*******@yahoo.com")

# 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 ?
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jul 19 '06 #19

John Salerno wrote:
Marc 'BlackJack' Rintsch wrote:
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.
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?

Jul 19 '06 #20
Class PersonBO(Person):
def Block():
blocked = True

<OT>
shouldn't it be:
class PersonBO(Person):
def block(self):
self.blocked = True
</OT>
Yes, it should be as you mentioned. However, I had posted it to
elaborate the case. Actually, I tested using the following code:

class PersonBO(Person):
pass
As far as my project is concerned, I have found out some other way of
doing the things,

Care to explain your solution ?
For the time being, I am not separating the plumbing and business
logic. When I need to, I shall come back to this post, study all the
ideas suggested, and jot down the pattern suitable to me. The code
pattern using metaclass looked interesting to me.

Thanks
Sanjay

Jul 19 '06 #21
Bruno Desthuilliers wrote:
John Salerno wrote:
>Marc 'BlackJack' Rintsch wrote:
>>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.

What the ... is GUI code doing in a domain object ???
well, as far as visual studio goes, it puts the GUI code in a partial
class that you don't see, and then you do the rest of your coding in the
other part of the same class...i suppose you can change it however you
like, but that's the way it starts...
Jul 19 '06 #22
Bruno Desthuilliers wrote in news:44***********************@news.free.fr
in comp.lang.python:
John Salerno wrote:
>Marc 'BlackJack' Rintsch wrote:
>>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.

What the ... is GUI code doing in a domain object ???
It doesn't (shouldn't) really work like that.

The partial classes are used by GUI designer tools (Visual Studio[1]
and SharpDevelop[2] for example) to seperate code that the tool generates
from code (event handlers etc) that the programmer writes.

IOW hopefully both parts of the class have GUI code in them.

[1] http://msdn.microsoft.com/vstudio/
[2] http://www.sharpdevelop.net/OpenSource/SD/

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '06 #23

Sanjay ha scritto:
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
Anyway, I would suggest you NOT to use this code in production. Yes,
Python
can imitate Ruby, but using this kind of classes would confuse
everybody and
make your code extremely unpythonic. As always, consider changing your
mindset,
when you switch language. For you problem, you could just put your
client
methods in a file, and add them to your original class with setattr, if
you don't
want to use inheritance. It would be still better that magically
transform your
classes with a metaclass.

Michele Simionato

Jul 20 '06 #24
Anyway, I would suggest you NOT to use this code in production. Yes,
Python
can imitate Ruby, but using this kind of classes would confuse
everybody and
make your code extremely unpythonic. As always, consider changing your
mindset,
when you switch language. For you problem, you could just put your
client
methods in a file, and add them to your original class with setattr, if
you don't
want to use inheritance. It would be still better that magically
transform your
classes with a metaclass.
Thanks for the much needed guidence.

Sanjay

Jul 20 '06 #25
Kay Schluehr wrote:
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 can't believe I always used version control systems for that use case if
it's that easily solved with partial classes.

Stefan
Jul 20 '06 #26
Stefan Behnel wrote:
Kay Schluehr wrote:
>>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 can't believe I always used version control systems for that use case if
it's that easily solved with partial classes.
Collaborative work is only one of the needs solved by VCS.
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jul 20 '06 #27

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Gomaw Beoyr | last post by:
Two question about the "partial classes" (in the next wersion of ..NET). Question 1 ========== Will partial classes (in the next version of C#) have to be declared "partial" in ALL places. ...
16
by: Madhuri Mittal | last post by:
Hi, I know that we can define a class over multiple source files using the keyword 'Partial' in C# within a project. Can we define the class over multiple projects using the 'Patial' keyword- I...
10
by: ptass | last post by:
Hi In asp.net 2.0 an aspx files .cs file is a partial class and all works fine, however, I thought I’d be able to create another class file, call it a partial class and have that compile and...
1
by: Bishoy George | last post by:
In a web application using asp.net 2.0 All my classes are partial classes. - I noticed that partial class cannot be inherited... is that true? - I tried to remove the partial keyword , and I...
9
by: Fat Elvis | last post by:
I'd like to extend some of my Asp.net pages by using Partial Classes. Example ASP.Net Page: public partial class Admin_Customer : System.Web.UI.Page { protected void Page_Load(object sender,...
2
by: PeterKellner | last post by:
I'm trying to understand how asp.net 2.0 ties together the codebehind. I've made a simple page, turned debug on so the *0.cs,*1.cs,*2.cs files don't get erased. My first point of confusion is...
2
by: Sebastian | last post by:
Hi Everyone, we're currently developing an web-app for insurance company. We've got a long form-flows and view-classes (MVC) of about 2000 lines of code. Is there any possibility to split...
3
by: vivekian | last post by:
Hi, Is there a way to have partial classes across different namespaces ? namespace a.b.c { public partial class X { private void MyMethod () { } }
5
by: Sagar | last post by:
I am working on a migration project for a huge asp.net application now running in framwework 1.1 to dotnet 2.0 As a first step, I opened the .sln files of the project in VS.Net 2005. The...
2
by: Peted | last post by:
Hi, im moving a project from vs2005 to vs 2008. is doing so i have come across a compiler error regarding partial classes that i dont understand if anyone can explain it to me please the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.