Re: [Tutor] poll question
On Wed, 26 Nov 2008 14:22:52 +0100 spir <[EMAIL PROTECTED]> wrote: > Hello pythonistas, > > I need some information about a topic. A list like python tutor is > the proper place to get it. Take this as a (stupid) game: would you > like to answer the following question? > > Imagine you are writing code. Then, you realise you need a tool > function you have not yet written. You don't want to stop coding now, > rather plan to write the func later. So that your code will hold some > calls to a not yet defined function. Right? The function is a very > short and simple one, indeed: it will return a character range, in > form of a string such as "0123456789", when passed ASCII ordinals as > parameters. How would you call this function? e.g. digits = > char_range(...) In other words: which is, for you personly, the most > practicle or natural way of calling this func? Would you give me one > or more calling example(s)? Below a list of ASCII characters. > > Note: Yes, this is trivial ;-) But there is no trick, no trap. I > cannot tell the purpose now, because it would invalid the results. I > will explain it in 3 or 4 days when I have some data. Additional > question if you wish: You may try and guess the point of this. But do > it after answering... > > Thank you very much. > Denis I would split it up in two functions: The main function would be: getStringFromOrd(list,what-ord) You'll loop through the passed parameter, a list delimited by comma. what-ord defines what you are using (dec, octal, hex) For each value in the list you call getCharFromOrd(ord,what-ord) which returns the true character Examples: MyName = getStringFromOrd((80,101,116,101,114),'Dec') MyName = getStringFromOrd((0x50,0x65,0x74,0x65,0x72),'hex') -- 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 signature.asc Description: PGP signature ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] My horrible looking program needs improvement.
On Tue, 25 Nov 2008 13:04:35 +0100 spir <[EMAIL PROTECTED]> 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): > > > 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 signature.asc Description: PGP signature ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] My horrible looking program needs improvement.
On Mon, 24 Nov 2008 11:00:59 -0500 Peter van der Does <[EMAIL PROTECTED]> wrote: > I wrote my 1st Python program and it works as I want it to work but > ,IMHO, it looks horrible. > > As to what my program does: > It downloads a file from the web, downloads .htaccess and Apache log > file from a FTP site, using the IP's from the Apache log it checks if > the IP exists in the web-file, if it is it's added to the .htaccess as > a deny from. > > Most actions are logged to file and console. > > Why I think it looks horrible: > Everything is in main(), I would like to use functions but ran into > problems with the scope of my variables. > The CLI arguments check is just a bunch of IF's > > Is this the place where somebody could go over my program and give > some pointers on how to improve the structure or is there a forum out > on the Net that can help me out. > > If this is the place, how do I show the program, in-line or attached? > > Thanks in advance. > OK, I put the code here: http://pdoes.pastebin.com/m47109194 Thanks people. -- 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 signature.asc Description: PGP signature ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] My horrible looking program needs improvement.
I wrote my 1st Python program and it works as I want it to work but ,IMHO, it looks horrible. As to what my program does: It downloads a file from the web, downloads .htaccess and Apache log file from a FTP site, using the IP's from the Apache log it checks if the IP exists in the web-file, if it is it's added to the .htaccess as a deny from. Most actions are logged to file and console. Why I think it looks horrible: Everything is in main(), I would like to use functions but ran into problems with the scope of my variables. The CLI arguments check is just a bunch of IF's Is this the place where somebody could go over my program and give some pointers on how to improve the structure or is there a forum out on the Net that can help me out. If this is the place, how do I show the program, in-line or attached? Thanks in advance. -- 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 signature.asc Description: PGP signature ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor