[Tutor] [Fwd: Re: My horrible looking program needs improvement.]

spir denis.spir at free.fr
Tue Nov 25 13:14:12 CET 2008


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 --, then 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.
Feel free to ask the list for more help on what a class is, or what it is 
intended for. And why it seems appropriate such alien things in the case of you 
present code.

denis

Ps: Are you dutch?



More information about the Tutor mailing list