Re: ****SPAM(11.2)**** [Tutor] Larger program organization

2005-02-17 Thread Bob Gailer
At 03:04 PM 2/16/2005, Brian van den Broek wrote:
Terry Carroll said unto the world upon 2005-02-16 16:18:
On Fri, 11 Feb 2005, Bob Gailer wrote:
Whenever you find yourself writing an if statement ask whether this
would be better handled by subclasses. Whenever you find yourself about
to write a global statement, consider making the variables properties of
a class.
Bob --
Brian already asked for an explanation of your first statement, and I 
found the ensuing discussion very instructive.
Can you explain the second?  As an aesthetic point, I hate globals, and 
I'd love a discussion with some examples of using class variables as a 
way of avoiding this.
Terry
Brian's response is similar to what I'd say.
Hi Terry and all,
I'm probably not the best person to explain this, but I've got a use case 
that might help illustrate.

The thing that finally got me to write my first Class statement was a 
procedural program where I had a hard to find bug.

I wrote a debug_report function which would print out an informative 
report about the state of all the objects I suspected were implicated in 
the bug. Then, I put debug_report calls at the suspicious places. But, my 
procedural code had function call chains 4 or 5 links deep, and at no 
level did any of the functions have access to all of the objects of interest.

To make it work in purely procedural, I had to either make many objects 
global or litter my functions with passing objects up and down as 
parameters that weren't needed for the function's tasks, but simply so 
debug_report could see them.

I made a class, put my functions in it, and suddenly, they all could `see' 
the objects of interest without the passing and returning of objects just 
for the sake of visibility. Some `self's sprinkled around did the work instead.

So, even if you don't make use of further OOP features (as I am learning 
to do on other threads), classes give you a namespace option between 
globals and locals -- `continentals' if you like ;-)

HTH, and looking forward to more expert explanations,
Brian vdB
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
Bob Gailer
mailto:[EMAIL PROTECTED]
303 442 2625 home
720 938 2625 cell 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: ****SPAM(11.2)**** [Tutor] Larger program organization

2005-02-16 Thread Terry Carroll
On Fri, 11 Feb 2005, Bob Gailer wrote:

 Whenever you find yourself writing an if statement ask whether this
 would be better handled by subclasses. Whenever you find yourself about
 to write a global statement, consider making the variables properties of
 a class.

Bob -- 

Brian already asked for an explanation of your first statement, and I 
found the ensuing discussion very instructive.

Can you explain the second?  As an aesthetic point, I hate globals, and 
I'd love a discussion with some examples of using class variables as a way 
of avoiding this.

Terry

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: ****SPAM(11.2)**** [Tutor] Larger program organization

2005-02-16 Thread Brian van den Broek
Terry Carroll said unto the world upon 2005-02-16 16:18:
On Fri, 11 Feb 2005, Bob Gailer wrote:

Whenever you find yourself writing an if statement ask whether this
would be better handled by subclasses. Whenever you find yourself about
to write a global statement, consider making the variables properties of
a class.

Bob -- 

Brian already asked for an explanation of your first statement, and I 
found the ensuing discussion very instructive.

Can you explain the second?  As an aesthetic point, I hate globals, and 
I'd love a discussion with some examples of using class variables as a way 
of avoiding this.

Terry

Hi Terry and all,
I'm probably not the best person to explain this, but I've got a use 
case that might help illustrate.

The thing that finally got me to write my first Class statement was a 
procedural program where I had a hard to find bug.

I wrote a debug_report function which would print out an informative 
report about the state of all the objects I suspected were implicated 
in the bug. Then, I put debug_report calls at the suspicious places. 
But, my procedural code had function call chains 4 or 5 links deep, 
and at no level did any of the functions have access to all of the 
objects of interest.

To make it work in purely procedural, I had to either make many 
objects global or litter my functions with passing objects up and down 
as parameters that weren't needed for the function's tasks, but simply 
so debug_report could see them.

I made a class, put my functions in it, and suddenly, they all could 
`see' the objects of interest without the passing and returning of 
objects just for the sake of visibility. Some `self's sprinkled around 
did the work instead.

So, even if you don't make use of further OOP features (as I am 
learning to do on other threads), classes give you a namespace option 
between globals and locals -- `continentals' if you like ;-)

HTH, and looking forward to more expert explanations,
Brian vdB
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Larger program organization

2005-02-16 Thread Kent Johnson
Terry Carroll wrote:
On Fri, 11 Feb 2005, Bob Gailer wrote:

Whenever you find yourself writing an if statement ask whether this
would be better handled by subclasses. Whenever you find yourself about
to write a global statement, consider making the variables properties of
a class.

Bob -- 

Brian already asked for an explanation of your first statement, and I 
found the ensuing discussion very instructive.

Can you explain the second?  As an aesthetic point, I hate globals, and 
I'd love a discussion with some examples of using class variables as a way 
of avoiding this.
Global variables are one way to make state persist across function calls. Here's a toy example that 
might give you the idea. Suppose you want to write a function that keeps a running total. You could 
do something like this (not recommended!):

total = 0
def addToTotal(inc):
  global total
  total += inc
Every time you call addToTotal, total is incremented.
This is already a poor design.
- There is no encapsulation - to increment the total, you call a function, to view the total you 
look at the global variable.
- There is no API - to reset the total you would have to set the global variable.

You could try to fix this by adding more functions:
def printTotal():
  print 'Total is', total
def resetTotal():
  global total
  total = 0
That's a little better, maybe. But there are other problems:
- You can only have one total. What if you want two different totals?
- Your global namespace has extra names - total, addToTotal, etc.
For short scripts this structure can work, but for larger projects it gets unwieldy. OOP to the 
rescue! How about a Total class?

class Total:
  def __init__(self):
self.reset()
  def add(self, inc):
self.total += inc
  def print(self):
print 'Total is', self.total
  def reset(self):
self.total = 0
You can use this like this:
t = Total()
t.inc(5)
t.print()
t.reset()
Now everything is wrapped up in a nice neat package. There is a clear, consistent API, no namespace 
pollution, and you have a reusable object.

You might also be interested in this essay: 
http://www.pycs.net/users/323/stories/15.html
Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: ****SPAM(11.2)**** [Tutor] Larger program organization

2005-02-13 Thread Alan Gauld
 I am curious about Bob's Whenever you find yourself writing an if
 statement ask whether this would be better handled by subclasses.

 Could you explain a bit more?

One of the basic purposes of OOP is to eliminate if/switch statements
that are conditional on the type of the object being handled.

So when writing OO code every time you see an if statement you should
ask is this really part of the logic or am I habdling a special type
of processing that should be in a separate subclass.

For example, if you were writing your Node class parsing method and
you started writing code like:


if dataType == 'Some literal value'
# extract one format of data
else
# extract another data format

Then maybe you should have two classes and rely on
polymorphism to do the two different types of extraction.

Thus
subType = DataType()
subType.extractData()

By eliminating the if/else you potentially eliminate an
ongoing maintenance headache and this is one of the big
wins of OOP.

HTH,

Alan G.


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: ****SPAM(7.4)**** Re: ****SPAM(11.2)**** [Tutor] Larger program organization

2005-02-13 Thread Kent Johnson
Bob Gailer wrote:
At 03:21 PM 2/12/2005, Brian van den Broek wrote:
[snip]
  I am curious about Bob's Whenever you find yourself writing
  an if statement ask whether this would be better handled by subclasses.
class A:
...
class A1(A);
  def foo(self, ...):
statements to process object of type 1
class A2(A);
  def foo(self, ...):
statements to process object of type 2
That takes less code. Eliminates the type property. I get greater 
visibility about the existence and distinction of the two (or more) 
sub-types. I now can much more easily extend each subtype.
Also you can more easily *add* a new subtype. Using if statements, if you add a new type you have to 
find all the relevant conditionals and add another condition. With subclassing, you create a new 
subclass and define the necessary methods. The changes are localized to the subclass and much easier 
to figure out.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


RE: [Tutor] Larger program organization

2005-02-13 Thread Ryan Davis
My main reason right now is that I know C#/ASP.NET very well.  I don't know how 
to do things in Python yet.  Say I can make a C# web
app with a quality of X.  Until I know how to make a Python web app with 
quality X, I can't use it in a production environment.  I'm
hoping that doing some code-gen work, along with other research will help me 
along, but the rest of the world can't hold still while
I study.

So, in sum, I thing I could develop fairly complex applications faster in 
Python, but I don't know how yet.  When I do know, I'm
sure I will.

Thanks,
Ryan 
-Original Message-
From: Alan Gauld [mailto:[EMAIL PROTECTED] 
Sent: Saturday, February 12, 2005 3:52 AM
To: EJP; Ryan Davis; tutor@python.org
Subject: Re: [Tutor] Larger program organization

 without trying to make this one of those classic threads of great, 
 do you feel you could develop fairly complex applications faster 
 in Python than in C#/ASP.NET?  It's a rhetorical question 
 (but I'm interested in your answer as a single data point)

To be honest it wouldn't make a great deal of difference.
For a complex application (by which I assume you mean large 
as well as being technically difficult) you spend most of 
your time thinking not physically coding so the language
becomes less significant.

On a typical big project the actual coding time is likely 
to be less than 10% of the total time so even if Python were 
twice as fast you only save 5% overall.

Python may improve time to fix however since it will be easier 
to read later and therefore easier to debug and spot errors etc.

But on big projects analysis, design and testing will be 
the big contributers, actually typing in the code is a trivial 
element.

Alan G.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Larger program organization

2005-02-12 Thread Kent Johnson
Ryan Davis wrote:
I'm starting to make a code-generation suite in python, customized to 
the way we ASP.NET at my company, and I'm having some trouble finding a 
good way to organize all the code.  I keep writing it, but it feels more 
and more spaghetti-ish every day.
Organize your code into packages. Give each package a clear responsibility and clear dependencies on 
other packages. For example my current (Jython) project has these packages:

app - top-level application objects
data - database access - all the SQL code and database objects
dbload - helpers for initial database load
gui - gui classes
importer - classes to implement an import function
main - misc. main programs, not really part of the main app
server - embedded web server
tree - support for the Swing TreeModel that is a major part of the app
writer - generation of output files
I also use some packages that are not specific to the application:
db - generic database support
swing - generic Swing widgets and support classes
util - misc helpful stuff
In total there are 121 Python files (including test modules) and 49 Java files.
For a smaller project you might just have a few modules in one package, or just 
a couple of packages.
HTH
Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: ****SPAM(11.2)**** [Tutor] Larger program organization

2005-02-12 Thread Brian van den Broek
Bob Gailer said unto the world upon 2005-02-11 15:34:
At 10:39 AM 2/11/2005, Ryan Davis wrote:
I'm starting to make a code-generation suite in python, customized to 
the way we ASP.NET at my company, and I'm having some trouble finding 
a good way to organize all the code.

My take on doing that in Python:
Organize things into modules. Especially with an eye to potential reuse. 
Look at the module index in the docs to see how most of the standard 
modules focus on doing one thing well.

Within a module create classes for every conceivable object. Whenever 
you find yourself writing an if statement ask whether this would be 
better handled by subclasses. Whenever you find yourself about to write 
a global statement, consider making the variables properties of a class.
SNIP
Hi all,
as readers of another concurrent thread will know, I am, with lots of 
help, just beginning to start thinking about proper OOP design.

I am curious about Bob's Whenever you find yourself writing an if 
statement ask whether this would be better handled by subclasses.

Could you explain a bit more?
Thanks and best,
Brian vdB
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Larger program organization

2005-02-11 Thread Ryan Davis








I'm starting to make a code-generation suite in python, customized
to the way we ASP.NET at my company, and I'm having some trouble finding a good
way to organize all the code. I
keep writing it, but it feels more and more spaghetti-ish
every day.



I'm going to look at the other stuff in site-packages to
see if I can glean any wisdom, and have googled a
bit, coming up mostly blank or with trivial examples. Are there any helpful links or advice
anyone has for building larger systems?




My background is mostly C#, so I'm used to the ridiculous
rigidity of strongly-typed languages. I have been using python for helper apps
for a few months now, so am pretty familiar with the syntax now, but I don't
know any of the patterns yet. My
nefarious goal is to supplant C#/ASP.NET with Python, but I need to figure out
how to make programs clients want to pay for before I can make any reasonable
argument.

Thanks,
Ryan

Ryan Davis
Director of Programming Services
http://www.acceleration.net/ 








___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: ****SPAM(11.2)**** [Tutor] Larger program organization

2005-02-11 Thread Bob Gailer


At 10:39 AM 2/11/2005, Ryan Davis wrote:
I'm
starting to make a code-generation suite in python, customized to the way
we ASP.NET at my company, and I'm having some trouble finding a good way
to organize all the code. 
My take on doing that in Python:
Organize things into modules. Especially with an eye to potential reuse.
Look at the module index in the docs to see how most of the
standard modules focus on doing one thing well.
Within a module create classes for every conceivable object. Whenever you
find yourself writing an if statement ask whether this would be better
handled by subclasses. Whenever you find yourself about to write a global
statement, consider making the variables properties of a class.
Within classes create methods applicable to those classes. Within
subclasses create methods applicable to those subclasses.
I
keep writing it, but it feels more and more spaghetti-ish every 
day.

I'm going to look at the other stuff in site-packages to see if I can
glean any wisdom, and have googled a bit, coming up mostly blank or with
trivial examples. Are there any helpful links or advice anyone has
for building larger systems? 

My background is mostly C#, so I'm used to the ridiculous rigidity of
strongly-typed languages. I have been using python for helper apps for a
few months now, so am pretty familiar with the syntax now, but I don't
know any of the patterns yet. My nefarious goal is to supplant
C#/ASP.NET with Python, but I need to figure out how to make programs
clients want to pay for before I can make any reasonable argument.

Thanks,
Ryan

Ryan Davis
Director of Programming Services
http://www.acceleration.net/


___
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Bob Gailer
mailto:[EMAIL PROTECTED]
303 442 2625 home
720 938 2625 cell

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: ****SPAM(11.2)**** [Tutor] Larger program organization

2005-02-11 Thread Danny Yoo


 way we ASP.NET at my company, and I'm having some trouble finding a good
 way to organize all the code.

 My take on doing that in Python:

 Organize things into modules. Especially with an eye to potential reuse.
 Look at the module index in the docs to see how most of the standard
 modules focus on doing one thing well.

Hi Ryan,

And just making sure; are you using packages?

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

Most large Python programs that I've seen will first partition their
functionality into modules, and if that's not enough, then they further
break themselves down into packages.


Best of wishes to you!

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor