473,498 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Coding an extendable class

Hi!

I want to write a package of modules that deal with mathematical
graphs. So my plan is to have a main.py file where the basic operations
are done, like reading the file and creating an adjacence list and all
that. In this main.py file, there is created a Graph class with several
methods, such as deg(v) or delvertex(v).
And then, for all of the other things one can do with a graph, I want
to code other modules, e.g. a file euler.py for finding an Euler way
through the graph, that extends the class by the method eulerway(). I
know that I can do something like

# graphtools/euler.py
import main
class Graph(main.Graph):
def eulerway(self):
[...]

and then the Graph class imported from euler.py will have both the old
and the new methods. But what can I do if I want the end user to be
able to combine several modules and I don't know in which order he will
do? For example, by
import graphtools.main, graphtools.hamilton
I can do the thingy described above, but what is with something like
import graphtools.hamilton, graphtools.euler
? In that case I want the class to have all of the methods defined in
the modules. How do I realize that in the best way?

Bye
Tobias

--
please send any mail to botedesschattens(at)web(dot)de
Jul 18 '05 #1
2 1502
Python sometimes uses "mixin" classes -- see the SocketServer module for
one example in a core module.

So main.Graph would be the real graph class, and
graphtools.hamilton.HamiltonMixin would be one mixin:
class MyGraph(main.Graph, hamilton.HamiltonMixin):
pass
You could use new.classobj to create a class object with a given list
of mixins at runtime (untested):
# In Main
_graph_classes = []

def add_mixin(m):
_graph_classes.append(m)
global Graph
Graph = new.classobj("Graph", _graph_classes, {})

add_mixin(main._Graph)

# In each module that defines a new mixin
class HamiltonianMixin:
pass
main.add_mixin(HamiltonianMixin)
however, having the exact nature of main.Graph depend on what other
modules have been imported is not what most Python users would expect.
I prefer the solution of having the user list mixins by creating a
"personalized" graph class.

Jeff

Jul 18 '05 #2
Tobias Pfeiffer wrote:
I want to write a package of modules that deal with mathematical
graphs. So my plan is to have a main.py file where the basic operations
are done, like reading the file and creating an adjacence list and all
that. In this main.py file, there is created a Graph class with several
methods, such as deg(v) or delvertex(v).
And then, for all of the other things one can do with a graph, I want
to code other modules, e.g. a file euler.py for finding an Euler way
through the graph, that extends the class by the method eulerway(). I
know that I can do something like

# graphtools/euler.py
import main
class Graph(main.Graph):
def eulerway(self):
[...]

and then the Graph class imported from euler.py will have both the old
and the new methods. But what can I do if I want the end user to be
able to combine several modules and I don't know in which order he will
do? For example, by
import graphtools.main, graphtools.hamilton
I can do the thingy described above, but what is with something like
import graphtools.hamilton, graphtools.euler
? In that case I want the class to have all of the methods defined in
the modules. How do I realize that in the best way?


#__init__.py
class Graph(object):
def deg(self, v):
# your code

#euler.py
class EulerMixin(object):
def eulerway(self):
# your code

#hamilton.py
class HamiltonMixin(object):
def whatever(self):
# your code

The above are all in the graphtools package directory.
Now a sample usage:

#application.py
import graphtools
from graphtools import euler, hamilton

class Graph(graphtools.Graph, euler.EulerMixin, hamilton.HamiltonMixin):
pass

The XXXMixin classes are not for standalone use, they should only call but
not override the Graph methods. As long as the different mixins are
orthogonal, i. e. do not affect each other all should be fine.

Alternatively you can derive the hamilton/euler variants from
graphtools.Graph. You must then design the different classes with
cooperation in mind, as demonstrated below with three initialization
routines which are assumed to be run only once:

#__init__.py
class Graph(object):
def __init__(self):
self.init()
def init(self):
print "init main"
def deg(self, v):
print "deg"

#euler.py
import graphtools
class EulerGraph(graphtools.Graph):
def eulerway(self):
print "eulerway"
def init(self):
print "init euler"
super(EulerGraph, self).init()

#hamilton.py
import graphtools
class HamiltonGraph(graphtools.Graph):
def whatever(self):
print "whatever"
def init(self):
print "init hamilton"
super(HamiltonGraph, self).init()

#usegraphtools.py
import graphtools
from graphtools import euler, hamilton
class Graph(euler.EulerGraph, hamilton.HamiltonGraph):
pass

g = Graph()
g.eulerway()
g.whatever()
g.deg(1)

Output:

init euler
init hamilton
init main
eulerway
whatever
deg

Peter

Jul 18 '05 #3

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

Similar topics

4
3063
by: Mikkel christensen | last post by:
Hi there I wonder if any of you could point me in a direction where I can find some usefull information about coding standarts. I have some generel experiense in programming but I lack many...
144
6728
by: Natt Serrasalmus | last post by:
After years of operating without any coding standards whatsoever, the company that I recently started working for has decided that it might be a good idea to have some. I'm involved in this...
1
1684
by: R Reyes | last post by:
Hello All, I'm always looking for ways to improve my code. Most of the time (whenever I'm working on a project) I write a bunch of functions. Then after the project is finished, I put all the...
0
1057
by: Willie Neal | last post by:
I'm new to vb.net and would like the advice of my peers. Here is my scenario: I have a user control which consists of three buttons, three listboxes and three labels. When the user selects a button...
60
4968
by: Dave | last post by:
I'm never quite sure whether to use "this." or not when referring to fields or properties in the same class. It obviously works just fine without it but sometimes I wonder if using this....
14
2388
by: key9 | last post by:
Hi All On coding , I think I need some basic help about how to write member function . I've readed the FAQ, but I am still confuse about it when coding(reference / pointer /instance) , so I...
1
1779
by: Gale Coleman - LSND | last post by:
ASP, VBscript, IIS 6, Access database Hello, I have a form page that people fill out and push submit. When they push submit, the information is stored in a database in two different tables....
19
3937
by: auratius | last post by:
http://www.auratius.co.za/CSharpCodingStandards.html Complete CSharp Coding Standards 1. Naming Conventions and Styles 2. Coding Practices 3. Project Settings and Project Structure 4....
1
4665
by: Jennifer Jazz | last post by:
My question is regarding the mapping of Class diagram to the C++ coding. There are 3 realtions in Class diagram 1) Assosication 2) Composition 3) Aggregation (Weak Composition). ...
0
7125
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
7004
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
7167
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7208
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
7379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4593
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1423
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
657
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.