[Tutor] My horrible looking program needs improvement.

Peter van der Does peter at avirtualhome.com
Tue Nov 25 14:28:31 CET 2008


On Tue, 25 Nov 2008 13:04:35 +0100
spir <denis.spir at free.fr> wrote:

> Hello Peter,
> 
> Your program's structure is rather well designed... with words and
> whitespace. You just need to export each consistent part of your
> main() into a specialised section: module, object, function. I would
> suggest (use the names that make sense for you, not mine):
> 
> * "Config" class that copes with *all* config.
> 	~ read/store to attributes 'period' args
> 	~ read/store ini file 'access' params
> 	~ ...
> * "AccessData" class that copes with data -- central thing -- uses
> config ~ read/store spam
> 	~ read/store htaccess list
> 	~ sort
> 	~ update access list
> 	~ ...
> * function to output results -- uses data
> 
> I do not have enough time to help you and properly shape these
> objects. Still, the AccessData class may look like that.
> 
> class AccessData(object):
> 	''' blah blah
> 		'''
> 	def __init__(self):
> 		<read (old) recorded data from file>
> 		<possibly init other data attributes>
> 	def update():
> 		''' this method only if process is always
> 		purely sequential & identical
> 		-- then may also contain init actions'''
> 		self.read_spam(self)
> 		self.read_htaccess(self)
> 		self.sorted(self)
> 		self.updated(self)
> 		self.write_to_file
> 	def read_spam(self):
> 		<...>
> 		store on attribute
> 	def read_htaccess(self):
> 		<...>
> 		store on attribute
> 	def sorted(self):
> 		<...>
> 		return sorted
> 	def updated(self):
> 		<...>
> 		return updated?
> 	def write_to_file(self):
> 		<...>
> 
> And main() could be (not more!):
> 
> def main():
> 	''' blah '''
> 	# config
> 	config = Config()
> 	config.read_args()	# from command line/ optparse
> 	config.get_access()	# from ini file /ConfigParser
> 	# update access data
> 	# uses params from config
> 	access_data=AccessData()
> 	access_data.update()
> 	# output: from access_data to ...
> 	output()
> 	
> Possibly some of the above proposal is wrong or not the best
> appropriate -- I did it fast -- adapt to reality and your taste. I'm
> rather surprised that you are able to cope with such complicated
> problem as web access, and not to properly structure your code.
> 
> denis
> 
> Ps: Are you dutch?
Thanks Denis,

Those are the kind of pointers I was looking for. I wasn't expecting
somebody to hand me the solution on a silver platter, as a matter of
fact I prefer not to.

The structure is more complicated, IMHO, as finding and writing a
solution.
For me programming is like a spoken language. When learning a new
language you learn the words and the basics of the grammar.

The Internet is full of dictionaries (programming language reference
guides), giving explanations of what functions do but to get to know
the grammar you'll have to read good written programs or ask for
advice :) because there aren't a lot, if any, sites out there that
teach you the grammar.

Right now the program is a book for children, the grammar is easy. I
prefer to write a bit more complex grammar because if I ever decide to
write a bigger program, the kids grammar won't work anymore.

Oh and yes I am Dutch, currently living in the US.

-- 
Peter van der Does

GPG key: E77E8E98

WordPress Plugin Developer
http://blog.avirtualhome.com

GetDeb Package Builder/GetDeb Site Coder
http://www.getdeb.net - Software you want for Ubuntu
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 197 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/tutor/attachments/20081125/a162a413/attachment.pgp>


More information about the Tutor mailing list