Re: [Tutor] Tutor Digest, Vol 61, Issue 3
Daniele wrote: >> From: W W >> Subject: Re: [Tutor] modular program > >>> Where can I find some "best practices" for writing modular programs? >>> I thought about a txt file containing function calls that my program will >>> parse and execute in order, or is it better just to execute every .py file >>> in a certain "module" folder (I don't like this as modules could need to be >>> executed in different moments)? > >> You can pretty much take a look at any of the modules in your python >> library directory. In the case of my linux box, that's >> /usr/lib/python2.5/ > > I'm sorry, I've realized I didn't explain my needs at all. > I was a little influenced by drupal's definition of modules, which is > completely different from python's. > With module here I meant plug-in or extension: a piece of code written > by someone else that can be easily (and automaticallly) integrated > into my program. > My program must provide the posibility to be extended without editing > its code, just like mozilla's add-ons. It heavily depends on the design of the main program. The simplest way to have a plug-in system is probably having user writes python modules which your program will import and call a certain agreed function. for example mainprogram.py: um_name = raw_input('Enter usermodule's filename: ') um = __import__(um_name) um.run() usermodule.py: def run(): # do initializations required to install the module ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] [Edited] Plug-in enabled Program
> -- Messaggio inoltrato -- > From: "Alan Gauld" > To: tu...@python.org > Subject: Re: [Tutor] Tutor Digest, Vol 61, Issue 3 > > OK, To do that you need to provide an intrerface in your code > that the plug-in can use. That is to say you must call a documented > set of functions/methods and then arrange your code such that the > plugin can replace the default code. Thank you Alan, that's what I wanted :) I've understood the idea, and I'll investigate it. The only doubt I have is how to adapt it to multiple plugins related to the same interface. Let's say my program downloads a file from a url and saves it in a local directory. One plugin could be written to add a prefix to the file name and another one to make the filename all-caps. In this case the same interface (let's call it "ManageFileName") should be used twice (or, in general, many times), and it's not simple to decide a priori how to merge the two actions. This lets me think about decorators, is that a possibility? Each user could write it's own decorator to the "setFileName()" method and the program will use them together. As I don't know decorators in python at all, I'm not sure this could work out, but it could be a nice push to start learning :) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] [Edited] Plug-in enabled Program
"Daniele" wrote have is how to adapt it to multiple plugins related to the same interface. There are a few approaches. There is a design pattern (facade from memory) that allows this but you can also implement the plugin calls as a list of objects. So instead of calling theInteface.foo() you load a list full of all the interface obujects and run for if in the InterfaceList: if.foo() That will apply each plugin in the sequence loaded. That can of course result in one plugin wiping out the changes by another... but I don;t know of any way to avoid that. HTH, Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Add elements to list and display it [Very newbie question]
Thanks, Eric for your help! I appreciate your explanation about the reserved word "list" as well as the code you gently wrote to me. Now, I want to show everybody what I did: #!/usr/bin/env python # # This function fills any given list # mylist = [] x = 0 while (x != 't2'): x = raw_input('Enter IP: ') mylist.append(x) mylist.pop() """ # Discontinued for i in mylist: print i """ # More useful to solve my particular problem. for i, v in enumerate(mylist): print i, v It works really fine to me. Thanks! I continue learning and making. Will. On Sat, Feb 28, 2009 at 1:36 AM, Eric Dorsey wrote: > Here is one possible implementation of your project. > > *Code:* > #Dont use list as a variable name, its one of the reserved words. > mylist = [] > > #realize any values captured here are strings > x = raw_input('Enter num or text: ') > mylist.append(x) > x = raw_input('Enter num or text: ') > mylist.append(x) > > #output the type of objects you've entered (hint: they'll always be > strings.. ;) > print type(mylist[0]) > print type(mylist[1]) > > #print the list of items > for i in mylist: > print i > > *When you run the program:* > Enter num or text: 27 > Enter num or text: Eric > > > 27 > Eric > > > On Fri, Feb 27, 2009 at 10:19 AM, Network Administrator < > administrador.de@gmail.com> wrote: > >> I am beggining to learn Python and I appreciate if you help me with this: >> >> "I want a piece of a program to request the user to input "elements" >> (numbers, text, etc) and store them into a list. Then, I want to display all >> the elements one-per-line." >> >> I started using this code: >> >> #!/usr/bin/env python >> # >> # This function fills any given list >> # and display its content. >> # >> x = 0 # Variable "x" initiallized to zero, just >> because Python required it >> while (x != 't2' ): # On user's input "t2", no more input must be >> required >> list = []# I start a zero-elements list >> x = raw_input('Enter your number or text: ')# Software >> asks for user's input. >> >> list.append(x) >> # User's input is append to the list "list" >> >> for x in list: # It asks to enter the list and... >> print x # print their elements. >> >> Unfortunately, this code fails to do what I expect. I notice that user's >> input is not being append to the list, so, when I require to print the >> elements of the list only "t2" is displayed. I don't know how to append >> elements to a list on user's input. >> >> I appreciate your clearence. >> >> Regards, >> >> >> Will. >> >> >> >> ___ >> Tutor maillist - Tutor@python.org >> http://mail.python.org/mailman/listinfo/tutor >> >> > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Add elements to list and display it [Very newbie question]
On Mon, Mar 2, 2009 at 5:36 PM, Network Administrator wrote: > I appreciate your explanation about the reserved word "list" as well as the > code you gently wrote to me. Now, I want to show everybody what I did: > > #!/usr/bin/env python > # > # This function fills any given list > # > mylist = [] > x = 0 > while (x != 't2'): > x = raw_input('Enter IP: ') > mylist.append(x) > mylist.pop() A little more straightforward, perhaps: mylist = [] while True: x = raw_input('Enter IP: ') if x == 't2': break mylist.append(x) Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Difference in minutes between two time stamps
Hello, I can't seem to figure out the syntax to calculate the difference in minutes between two time stamps. I already read the documentation about datetime and time modules, but I was unable to implement the code. My code will be fed with two timestamps (as styrings): start="09:35:23" end="10:23:00" Could someone guide me on how to calculate the difference in minutes between both stamps? Your help is very much appreciated. Thank you, Judith ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Difference in minutes between two time stamps
2009/3/3 Judith Flores : > > Hello, > > I can't seem to figure out the syntax to calculate the difference in > minutes between two time stamps. I already read the documentation about > datetime and time modules, but I was unable to implement the code. > > My code will be fed with two timestamps (as styrings): > > start="09:35:23" > end="10:23:00" > > Could someone guide me on how to calculate the difference in minutes > between both stamps? You want to use the datetime.datetime.strptime() function to parse the timestamps. Although you will probably need to look at the time module to get the different codes -- the documentation isn't superbly organised in this area, I feel. Anyway, as a start: >>> import datetime >>> s = '09:35:23' >>> datetime.datetime.strptime(s, '%H:%M:%S') datetime.datetime(1900, 1, 1, 9, 35, 23) Can you figure out how to proceed from there? -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Difference in minutes between two time stamps
Use time.strptime() to parse them into seconds since the start of epoch, and then an ordinary numeric subtraction will work. Cheers On Monday 02 March 2009 19:45, Judith Flores wrote: > Hello, > >I can't seem to figure out the syntax to calculate the difference in > minutes between two time stamps. I already read the documentation about > datetime and time modules, but I was unable to implement the code. > > My code will be fed with two timestamps (as styrings): > > start="09:35:23" > end="10:23:00" > > Could someone guide me on how to calculate the difference in minutes > between both stamps? > > Your help is very much appreciated. > > Thank you, > > Judith > > > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] What is this [] construction?
Title: Signature.html What is this: d = [ int(x) for x in s.split(":") ] I see in the program I'm looking at, the [] construction can be much more complicated, as in: self.recent_events = [ event for event in self.recent_events if os.path.exists(event) and (time.time() - os.path.getmtime(event)) < 3600.0 ] -- Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.01 Deg. W, 39.26 Deg. N) GMT-8 hr std. time) “In mathematics you don't understand things. You just get used to them.” -- John Von Neumann (P.S. The same is true in life.) Web Page:___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What is this [] construction?
2009/3/3 Wayne Watson : > What is this: d = [ int(x) for x in s.split(":") ] It's a list comprehension: http://docs.python.org/tutorial/datastructures.html#list-comprehensions -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What is this [] construction?
On Tue, Mar 3, 2009 at 4:54 AM, Wayne Watson wrote: > What is this: d = [ int(x) for x in s.split(":") ] > I see in the program I'm looking at, the [] construction can be much more > complicated, as in: > self.recent_events = [ event for event in self.recent_events > if os.path.exists(event) and > (time.time() - os.path.getmtime(event)) < > 3600.0 ] That's called list comprehension. The notation [f(x) for x in A if p(x)] means: Form a list in the following way: Start with an empty list. Then go through A, and for each x in A, if p(x) is true, add f(x) to the list. d = [f(x) for x in A if p(x)] is equivalent to: d = [] for x in A: if p(x): d.append(f(x)) Your first example had no p(x) defined, which means that it's done for all x, that is: [ int(x) for x in s.split(":") ] means: The list, formed by taking int(x) for all x in the result of s.split(":"). It is almost English, really... [f(x) for x in A if p(x)] means: f(x) for all x in A for which p(x) holds. -- André Engels, andreeng...@gmail.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor