How to write good Python objects?

J

John Ladasky

Hi, folks,

At the beginning of 2003, I was a frustrated computer user, and lapsed
programmer, with problems to solve that screamed for programming.
Thanks to the Python language and community, I am a programmer once
again.

My earlier solicitation to the computer world is here:

http://groups.google.com/[email protected]

Anyway, I've been busy with Python for several months now. I'm an old
procedural guy. I had never written code in an object-oriented
language before. I'm starting to get the hang of it, and to see its
advantages, but I'm still struggling. I think that I have issues with
both OOP in general, and with Python in particular.

I have written one nice, self-contained object that contained a DNA
sequence, and various functions to manipulate the data therein. My
wxPython GUI objects, in contrast, are in a state of constant flux.
Rather than importing and reusing a piece of code, I find myself
copying the code into my new program and playing with it, just a bit.
I'm starting to believe that writing a good GUI object, one that you
can really reuse, is actually quite hard. Yes, I know that you can
derive a new object that overrides properties of your old object.
Should I find myself doing this for every object that I write?

One other thing -- I would like to be able to include a statement in
my program to the effect of: "from my_package import
my_function_or_class". Python seems to look for .pyc files in the
/Lib folder. By placing a .py file in /Lib, I got it to compile, and
to be recognized by an import statement. Is this the right way for
users to package their own code? It seems kludgy to me.

If it matters (it shouldn't), I'm running Python 2.2.2 on Win2000 Pro.

Thanks for your advice!

--
John J. Ladasky Jr., Ph.D.
Department of Biology
Johns Hopkins University
Baltimore MD 21218
USA
Earth
 
M

Marijan Tadin

One other thing -- I would like to be able to include a statement in
my program to the effect of: "from my_package import
my_function_or_class". Python seems to look for .pyc files in the
/Lib folder. By placing a .py file in /Lib, I got it to compile, and
to be recognized by an import statement. Is this the right way for
users to package their own code? It seems kludgy to me.

AFAIK if you want to import a python module (actually *.py file), you
can place it in /Lib subdirectory of python distribution, but I think
this subdirectory schould better stay reserved for modules from standard
python distribution.
You can put your module in any directory, and then add this subdirectory
to the PYTHONPATH environment variable of your operating system. I've
been doing this for a while, but then I ended with unmanageable cluster
of files distributed somewhere on my computer.
I think the best way is to use distutilities (Distributing Python
Modules link in your python documentation). I've studied it a few days
ago, and it is much easier to use (at least for simple things) than I
had thought before.
There is also a /lib/site-packages directory in your python
distribution, if you put your *.py file in it, you can allways import it
in your application, and it is also intended as directory for
third-party python modules, and distutilities will install your modules
into this subdirectory. I 'd recommend you to use packages (section 6.4
Packages in the tutorial in your python documentation) if you are not
allready doing so, because otherwise you'll probably soon get name
clashes.
If you use distutillities, you get two extras too:
1. You can easily make a Windows installer for your library, and if you
install your library this way, it will be registerd by OS, so if you
want to uninstall your library, you can do it as for any other windows
programm.
2. If you want to compile C extension, the easiest way to do so is with
distutilities. The first time I succeeded to do so, was with help from
some web post by Alex Martelli (maybe it is in Python recepies, but I'm
not sure). Most information how to do this is rather linux oriented.
Actually it is not difficult to do so on Windows (also with Mingw
compiler) but there is, AFAIK, no detailed (from begin to end at one
place) instruction how to do it for dummies (I still feel like one, so
the worst thing that can happen is a hint: "look into your compiler
documentation"), it is rather distributed over the web.
I have written one nice, self-contained object that contained a DNA
sequence, and various functions to manipulate the data therein. My
wxPython GUI objects, in contrast, are in a state of constant flux.
Rather than importing and reusing a piece of code, I find myself
copying the code into my new program and playing with it, just a bit.
I'm starting to believe that writing a good GUI object, one that you
can really reuse, is actually quite hard. Yes, I know that you can
derive a new object that overrides properties of your old object.
Should I find myself doing this for every object that I write?

I do not feel competent enough to give much advice here, but I believe
that, if you start to copy and paste your code, you should reconsider
your design. But, on the other hand, if you intend your library to be
used by others, to many classes and inheritance can be cumbersome. I
remember trying to use some Java library, which can do great things, but
in order to do simplest things with it, I schould use dozen classes,
where each of them was e.g. 5-th or 10-th in some inheritance hierarchy,
and you can imagine the fun of finding documentation of the
class-methods I wanted to use. I can not judge if it is OK for complex
library which is intended to be used by professional programmers.
At the beginning of 2003, I was a frustrated computer user, and lapsed
programmer, with problems to solve that screamed for programming.
Thanks to the Python language and community, I am a programmer once
again.

I am actualy weather forecaster, and I do some programming in my spare
time. I can remember learning C++ for about 6-7 months, and then
learning Java for about 3-4 months, and still not beeing able to open
text file and do some simple processing (which I could have done easily
in Fortran77 before) without looking in a book . The best advice I've
found in the book "Thinking in Java", was to have a look at Python.

Marijan Tadin
 
A

Andy Todd

John said:
Hi, folks,
[snip]

One other thing -- I would like to be able to include a statement in
my program to the effect of: "from my_package import
my_function_or_class". Python seems to look for .pyc files in the
/Lib folder. By placing a .py file in /Lib, I got it to compile, and
to be recognized by an import statement. Is this the right way for
users to package their own code? It seems kludgy to me.

If it matters (it shouldn't), I'm running Python 2.2.2 on Win2000 Pro.

Thanks for your advice!

--
John J. Ladasky Jr., Ph.D.
Department of Biology
Johns Hopkins University
Baltimore MD 21218
USA
Earth

You can put your modules anywhere you like. You just need to tell the
Python interpreter where to look for them. You can do this one of two
ways, either by setting a PYTHONPATH environment variable;

http://www.python.org/doc/current/tut/node8.html#SECTION008110000000000000000

Do this on Windows 2000 with Start->Settings->Control Panel->System
Then select the 'Advanced' tab and click on the button labelled
"Environment variables..."

Or, you can use path configuration (.pth) files;

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

On Windows 2000, and with Python installed to C:\Python2.2 you would
place your file (called, say John.pth) in C:\Python2.2\Lib\site-packages

Regards,
Andy
 
M

Martin Franklin

I have written one nice, self-contained object that contained a DNA
sequence, and various functions to manipulate the data therein. My
wxPython GUI objects, in contrast, are in a state of constant flux.
Rather than importing and reusing a piece of code, I find myself
copying the code into my new program and playing with it, just a bit.
I'm starting to believe that writing a good GUI object, one that you
can really reuse, is actually quite hard. Yes, I know that you can
derive a new object that overrides properties of your old object.
Should I find myself doing this for every object that I write?

John ,


I see you've been helped with the /Lib folder question. I am more
interested to hear about your GUI problem. I don't use wxPython I use
Tkinter but hardly ever find myself copying code from one project to
another...(these days) perhaps you could share some examples of the kind
of GUI elements you find yourself copying...


One example I used to find myself copying would be a tool bar (Tkinter
does not have a 'toolbar' widget) I would find myself creating a Frame
then adding lots of buttons to it now however I use a Toolbar class I
created that has an add method that takes a couple of arguments.


e.g (untested) I now use something like:

class Toolbar(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)


def add(self, label="", icon=None, command=None):
b = Button(self, text=label, command=command, image=icon)
b.pack(side="left")

.... now to use it in some other module...

tb = Toolbar(root)
tb.pack(fill="x")

tools = [("Open", None, self.openFile),
("Close", None, self.closeFile),
("Quit", None, self.quit)
]
for label, image, command in tools:
tb.add(label=label, icon=image, command=command)





## rather than...


toolbar = Frame(parent)
toolbar.pack()

openButton = Button(toolbar, text="Open", command=self.openFile)
openButton.pack(side="left")

closeButton = Button(toolbar, text="Close", command=self.closeFile)
closeButton.pack(side="left")

.... you get the picture...


Is this the sort of problem you are having..?


Regards
Martin
 
J

John Roth

John Ladasky said:
Hi, folks,

At the beginning of 2003, I was a frustrated computer user, and lapsed
programmer, with problems to solve that screamed for programming.
Thanks to the Python language and community, I am a programmer once
again.

My earlier solicitation to the computer world is here:

http://groups.google.com/[email protected]

Anyway, I've been busy with Python for several months now. I'm an old
procedural guy. I had never written code in an object-oriented
language before. I'm starting to get the hang of it, and to see its
advantages, but I'm still struggling. I think that I have issues with
both OOP in general, and with Python in particular.

I have written one nice, self-contained object that contained a DNA
sequence, and various functions to manipulate the data therein. My
wxPython GUI objects, in contrast, are in a state of constant flux.
Rather than importing and reusing a piece of code, I find myself
copying the code into my new program and playing with it, just a bit.
I'm starting to believe that writing a good GUI object, one that you
can really reuse, is actually quite hard. Yes, I know that you can
derive a new object that overrides properties of your old object.
Should I find myself doing this for every object that I write?

I'd suggest you look at refactoring out duplication. What it sounds like
is that you're generating a lot of minor variations on the same theme.
If you go after duplication relentlessly, eventually the code itself will
tell you what it wants to look like.

Thanks for your advice!

You're welcome.

John Roth
 
A

Anna

Not sure if ya'll have seen these references to distutils and windows. I'm
forwarding to make sure we keep it in mind when we're working on our book.

Anna
 

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,772
Messages
2,569,588
Members
45,100
Latest member
MelodeeFaj
Top