473,624 Members | 2,561 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to write good Python objects?

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/groups?selm...ing.google.com

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
Jul 18 '05 #1
5 2645
> 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
Jul 18 '05 #2
On Fri, 2003-10-24 at 02:15, John Ladasky wrote:
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...(thes e 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="le ft")

.... 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=la bel, icon=image, command=command )

## rather than...
toolbar = Frame(parent)
toolbar.pack()

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

closeButton = Button(toolbar, text="Close", command=self.cl oseFile)
closeButton.pac k(side="left")

.... you get the picture...
Is this the sort of problem you are having..?
Regards
Martin


--
Martin Franklin <mf********@gat wick.westerngec o.slb.com>
Jul 18 '05 #3
John Ladasky wrote:
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/tu...00000000000000

Do this on Windows 2000 with Start->Settings->Control Panel->System
Then select the 'Advanced' tab and click on the button labelled
"Environmen t 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\Li b\site-packages

Regards,
Andy
--
--------------------------------------------------------------------------------
From the desk of Andrew J Todd esq - http://www.halfcooked.com/

Jul 18 '05 #4

"John Ladasky" <la*****@my-deja.com> wrote in message
news:c0******** *************** **@posting.goog le.com...
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/groups?selm...ing.google.com
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
--
John J. Ladasky Jr., Ph.D.
Department of Biology
Johns Hopkins University
Baltimore MD 21218
USA
Earth

Jul 18 '05 #5
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
On Fri, 24 Oct 2003 07:01:52 +0100, Marijan Tadin wrote:
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


Jul 18 '05 #6

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

Similar topics

30
3432
by: Christian Seberino | last post by:
How does Ruby compare to Python?? How good is DESIGN of Ruby compared to Python? Python's design is godly. I'm wondering if Ruby's is godly too. I've heard it has solid OOP design but then I've also heard there are lots of weird ways to do some things kinda like Perl which is bad for me. Any other ideas?
28
3284
by: David MacQuigg | last post by:
I'm concerned that with all the focus on obj$func binding, &closures, and other not-so-pretty details of Prothon, that we are missing what is really good - the simplification of classes. There are a number of aspects to this simplification, but for me the unification of methods and functions is the biggest benefit. All methods look like functions (which students already understand). Prototypes (classes) look like modules. This will...
33
3473
by: Nick Evans | last post by:
Hello there, I have been on and off learning to code (with python being the second language I have worked on after a bit of BASIC). What I really want to know is, if you are going to actually write a program or a project of some sort, how do you actually start. Picture this, you know what you want the program to do (its features), you have a possably rough idea of how the program will work. You have opened an empty text file and saved...
1
2941
by: Jerald | last post by:
Running python 2.3.4 on valgrind (a tool like purify which checks the use of uninitialized memory, etc), gives a lot of errors. See below. jfj@cluster:~/> python -V Python 2.3.4 jfj@cluster:~/> valgrind python ==10517== Memcheck, a.k.a. Valgrind, a memory error detector for x86-linux.
13
9605
by: Stumped and Confused | last post by:
Hello, I really, really, need some help here - I've spent hours trying to find a solution. In a nutshell, I'm trying to have a user input a value in form's textfield. The value should then be assigned to a variable and output using document.write. (Note, there is no submit button or other form elements. Basically
19
1530
by: Levi Campbell | last post by:
Hi, I'm thinking about writing a system for DJing in python, but I'm not sure if Python is fast enough to handle the realtime audio needed for DJing, could a guru shed some light on this subject and tell me if this is doable or if I'm out of my fscking mind?
21
1905
by: Raj | last post by:
Hi, We just executed a project with Python using TG. The feedback was to use more python like programming rather than C style code executed in Python. The feedback is from a Python purist and for some reasons we cannot solicity his help. So we'd like to do is to scrub through the codebase and identify places where the codebase needs improvement, both from styling as well as design. Is there any website that can provide me with...
4
1704
by: Sai Krishna M | last post by:
Hi everybody, i have been developing web based applications using python+cheetah. The numbers are increasing. The applications have many common parts of code. Though these applications are separate from each other they are run in the same server. I heard about CORBA... but also heard that it has some flaws...like the programming being very complicated.
4
1338
by: MisterPete | last post by:
I created an object that inherits from file and was a bit surprised to find that print seems to bypass the write method for objects inheriting from file. An optimization I suppose. Does this surprise anyone else at all or am I missing something? import sys class FromObject(object): def write(self, string):
0
8231
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8168
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8672
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7153
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6107
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4075
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4167
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1474
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.