[Tutor] Re: Create list of IPs
> I am wondering how I would go about making a list of > IPs between two set > IPs. > This is my initial code: > > > > def list2str(list): > ip='' > for part in list: > if len(ip)!=0: > ip=ip+'.'+part > else: > ip=ip+part > return ip > > iplist = [] > > minip = ['1','0','0','1'] # starting IP > maxip = ['1','0','15','16'] # ending IP > > currentip = minip # set currentip to value of minip > **snip** > I'd appreciate any help. > > Thanks, > Ralf > It looks like you need not concern yourself with the ['1','0',...] part of your array(s). Only to increment from '0','1' to '15','16' Keeping with arrays, you could get last position of the array and increment number until 16. If last position > 15, stop incrementation and move to next to last position in the array until it reaches 15 (also breaks to to adding 15 to each octet in the last two array positions, one by one) > > __ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Attaching an uploaded file to an email
Yeah, you really have to see a few examples to get the hang of creating MIME emails, this is one area where I think the Python docs, quite frankly, stink. I had enough trouble getting attachments from a MIME email, let alone adding one. (But, if I recall correctly, a MIME email has a distinct structure, to the point that the email module has a walk() function to traverse it.) Regards, Liam Clarke On Sun, 20 Feb 2005 15:32:13 -0500, Martin Walsh <[EMAIL PROTECTED]> wrote: > Tim Wilson wrote: > > >Hi everyone, > > > > > Hi Tim, > > I'm a newb, first time posting, so please take any of the following > advice at face value > > ># Collect form information > >form = cgi.FieldStorage() > >requestername = form["requestername"].value > >fromaddr = form["email"].value > >itemname = form["itemname"].value > >description = form["description"].value > >buildings = form.getlist("building") > >room = form["room"].value > >dateneeded = form["dateneeded"].value > >po = form["po"].value > >budgetcode = form["budgetcode"].value > >attachment = form["attachment"].value > > > > > based on this cookbook recipe > http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/273844, it looks > like cgi.FieldStorage() returns a file-like object for file input > fields, having .filename and .file attributes. the file attribute has a > read() method which may be useful. having never used cgi I'm not sure > what .value returns for file input fields, don't know if this is of any > consequence. > > >buildinglist = ", ".join(buildings) > > > >**[ misc code snipped ]** > > > ># Set some email headers > >#msg = MIMEText(msgtext) > >msg = MIMEMultipart() > >msg['Subject'] = itemname > >msg['From'] = "%s <%s>" % (requestername, fromaddr) > >msg['To'] = toaddr > >if len(buildings) != 0: > >for building in buildings: > >msg['X-HRT-Building'] = building > >if po != "": msg['X-HRT-PO'] = po > >if dateneeded != "": > >try: > >duedate = time.asctime(time.strptime(dateneeded, "%m/%d/%Y")) > >msg['X-HRT-Due-Date'] = duedate > >except ValueError: > >pass > >msg.preamble = "Tech order request" > >msg.epilogue = "" > > > > > if you know that the attachment will always be a text file and your > assignment of 'attachment' looks like this: > > attachment = form["attachment"] > > then you might try the following (untested): > > part = MIMEText(attachment.file.read()) > # if I understand correctly, the 'Content-Disposition' header is > necessary to make the file > # appear in the message as an attachment, otherwise it may occupy > the msg body. > part.add_header('Content-Disposition', 'attachment', > filename=attachment.filename) > msg.attach(part) > > ># Send the message > >server = smtplib.SMTP('localhost') > >server.sendmail(fromaddr, toaddr, msg.as_string(0)) > >server.quit() > > > > > there's a great example in the email module docs if you're dealing with > more than just text files : > http://docs.python.org/lib/node578.html (3rd example, using the > mimetypes module) > > I have collected code snippets from various sources (python docs, ASPNs > python cookbook) into a basic MIMEMailer class, that I use fairly > regularly in hobby projects. If anyone is interested, I'd be happy to > share, or post it here. > > HTH, > Marty > > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > -- 'There is only one basic human right, and that is to do as you damn well please. And with it comes the only basic human duty, to take the consequences. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dictionary dispatch for object instance attributes question
Ah, see, I should convince my bosses that I need a Python interpreter. Of course, then they'd ask what Python was, and why I was thinking about it at work Duh, I was just reading the docs, and I kept thinking that an attribute was just a class variable. Thanks, Kent, now I have all sorts of interesting experiments to undertake... On Sun, 20 Feb 2005 07:26:54 -0500, Kent Johnson <[EMAIL PROTECTED]> wrote: > Liam Clarke wrote: > > Hi, > > > > just an expansion on Brian's query, is there a variant of getattr for > > instance methods? > > > > i.e. class DBRequest: > > def __init__(self, fields, action): > > self.get(fields) > > > > def get(self, fields): > > print fields > > > > > > Instead of self.get in _init__, the value of action to call a > > function? Or, is it going to have to be dictionary dispatch? > > I don't understand your example, but instance methods are attributes too, so > getattr() works with > them as well as simple values. > > An instance method is an attribute of the class whose value is a function. > When you access the > attribute on an instance you get something called a 'bound method' which > holds a reference to the > actual function and a reference to the instance (to pass to the function as > the 'self' parameter). > You can call a bound method just like any other function. So: > > >>> class foo: > ... def __init__(self): > ... self.bar = 3 > ... def baz(self): > ... print self.bar > ... > >>> f=foo() > > getattr() of a simple attribute: > >>> getattr(f, 'bar') > 3 > > getattr() of an instance method returns a 'bound method': > >>> getattr(f, 'baz') > > > > Calling the bound method (note the added ()) is the same as calling the > instance method directly: > >>> getattr(f, 'baz')() > 3 > > Of course you can do the same thing with dot notation for attributes: > >>> b=f.baz > >>> b > > > >>> b() > 3 > > Kent > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > -- 'There is only one basic human right, and that is to do as you damn well please. And with it comes the only basic human duty, to take the consequences. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Re: Create list of IPs
Thanks alot to everyone for your help! I'm not completely sure I understand how Roel's code works so I'll be doing a bit of research :-) (but it works :-D). Thanks again. All the best, Ralf ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Attaching an uploaded file to an email
Tim Wilson wrote: Hi everyone, Hi Tim, I'm a newb, first time posting, so please take any of the following advice at face value # Collect form information form = cgi.FieldStorage() requestername = form["requestername"].value fromaddr = form["email"].value itemname = form["itemname"].value description = form["description"].value buildings = form.getlist("building") room = form["room"].value dateneeded = form["dateneeded"].value po = form["po"].value budgetcode = form["budgetcode"].value attachment = form["attachment"].value based on this cookbook recipe http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/273844, it looks like cgi.FieldStorage() returns a file-like object for file input fields, having .filename and .file attributes. the file attribute has a read() method which may be useful. having never used cgi I'm not sure what .value returns for file input fields, don't know if this is of any consequence. buildinglist = ", ".join(buildings) **[ misc code snipped ]** # Set some email headers #msg = MIMEText(msgtext) msg = MIMEMultipart() msg['Subject'] = itemname msg['From'] = "%s <%s>" % (requestername, fromaddr) msg['To'] = toaddr if len(buildings) != 0: for building in buildings: msg['X-HRT-Building'] = building if po != "": msg['X-HRT-PO'] = po if dateneeded != "": try: duedate = time.asctime(time.strptime(dateneeded, "%m/%d/%Y")) msg['X-HRT-Due-Date'] = duedate except ValueError: pass msg.preamble = "Tech order request" msg.epilogue = "" if you know that the attachment will always be a text file and your assignment of 'attachment' looks like this: attachment = form["attachment"] then you might try the following (untested): part = MIMEText(attachment.file.read()) # if I understand correctly, the 'Content-Disposition' header is necessary to make the file # appear in the message as an attachment, otherwise it may occupy the msg body. part.add_header('Content-Disposition', 'attachment', filename=attachment.filename) msg.attach(part) # Send the message server = smtplib.SMTP('localhost') server.sendmail(fromaddr, toaddr, msg.as_string(0)) server.quit() there's a great example in the email module docs if you're dealing with more than just text files : http://docs.python.org/lib/node578.html (3rd example, using the mimetypes module) I have collected code snippets from various sources (python docs, ASPNs python cookbook) into a basic MIMEMailer class, that I use fairly regularly in hobby projects. If anyone is interested, I'd be happy to share, or post it here. HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Re: Fwd: Create list of IPs
R. Alan Monroe wrote: >>>Liam Clarke wrote: > > >>[ snip ] > > >>>- increment an IP. This is the hardest part. > > >>Why? An ip (V4) is just an 32bit integer :-) > > > Indeed. Some early versions of MacTCP for MacOS made you input the > address as a single large decimal number :^) And you can use it in URLs too (at least in Mozilla): python.org's IP address is 194.109.137.226, which is 3261958626 when represented as an integer. Typing http://3261958626/ in the browsers address bar takes you to the homepage :) -- "Codito ergo sum" Roel Schroeven ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] help with .get in Tkinter
Mark, There are two problems here: > from Tkinter import * > def go():... > e=Entry(main) > e.pack() This is OK but... > b=Button(main,text='OK',command=go()).pack() Problem 1: This will store None in b because pack() always returns None. Problem 2: > For some reason the function is called before I click the button, and > I get .10037088 before I have done a thing. Thats because you are calling go() inside the widget specification, you need to pass a reference to the function instead - ie miss out the parens: b=Button(main,text='OK',command=go) b.pack() That should fix it. Alan G Author of the Learn to Program web tutor http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Instance into another instance
Ismael Garrido wrote: Kent Johnson wrote: Are you creating a tree to represent XML data? There are many packages available that do this. You might want to look at ElementTree which is one of the easiest to use. In fact, even if you aren't trying to represent XML you might find ElementTree useful. http://effbot.org/zone/element.htm Kent Yes, that was my intention. ElementTree looks very interesting. Thanks for the suggestion. You can find other ideas here: http://www.xml.com/pub/a/2004/10/13/py-xml.html Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: Fwd: [Tutor] Create list of IPs
[EMAIL PROTECTED] wrote: Liam Clarke wrote: [ snip ] - increment an IP. This is the hardest part. Why? An ip (V4) is just an 32bit integer :-) The problem arises from the representation. Use something like "http://pynms.sourceforge.net/ipv4.html"; to switch between the various representations. :-) Amazing what a difference it makes to look at a problem a different way. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: Fwd: [Tutor] Create list of IPs
>> Liam Clarke wrote: > [ snip ] >> - increment an IP. This is the hardest part. > Why? An ip (V4) is just an 32bit integer :-) Indeed. Some early versions of MacTCP for MacOS made you input the address as a single large decimal number :^) Alan ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] help with .get in Tkinter
On Sun, 20 Feb 2005 17:12:54 +0200 Mark Kels <[EMAIL PROTECTED]> wrote: > Hi all. > First, here is the code I have a problem with (I got same problem in > my project) : > from Tkinter import * > def go(): > e.get() > print e > > main=Tk() > e=Entry(main) > e.pack() > b=Button(main,text='OK',command=go()).pack() > main.mainloop() > > For some reason the function is called before I click the button, and > I get .10037088 before I have done a thing. > How do I do it right ??? > -- Hi Mark, First problem: you need to assign the command for the button without parenthesis: b = Button(main, text='OK', command=go) b.pack() I split the line you used into two lines, because pack() returns None , so you don't have a reference to the button once you created it. Of course you can do it the way you did if you don't need to reference the button anymore, however there's not much use in assigning a new variable to it, just write: Button(main,text='OK',command=go()).pack() Second problem: your go() function does just what you told it to: it prints the "window name" (or however this is called) of the entry widget. You surely meant something like this: def go(): contents = e.get() print contents or simply: def go(): print e.get() I hope this helps Michael ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Time Controlled Execution
Hi, I want to know how to do this: I have an executable file, which reads input from stdin provided output at stdout or stderr. I have to run it for a specific period of time (say 5 secs), get the output and display it. If i use popen(), this way: from subprocess import * p = Popen(["test","test.out"], shell=True) p.wait() print p.stdin,p.stdout I dont get output in p.stdout.Apart from this, can u say how to stop this subprocess after 5 secs (or whatever time frame specified)? signal SIGALRM may not work (as i want it to work in windows & unix). -Varun ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Re: Create list of IPs
Ralfas Jegorovas wrote: > I am wondering how I would go about making a list of IPs between two set > IPs. # First, list2str can be written shorter .def list2str(lst): .return '.'.join(map(str, lst)) # The inverse of that is also needed .def str2list(s): .return map(int, s.split('.')) # Note that an IP address can also be represented as an integer .def lst2int(lst): .return (lst[0] << 24) | (lst[1] << 16) | (lst[2] << 8) | lst[3] # Reverse: .def lst2int(ip_int): .return map(int, [ip_int >> 24, . (ip_int >> 16) & 0xff, . (ip_int >> 8) & 0xff, . ip_int & 0xff]) # Once the IP addresses are converted to integers, we can simply # loop from the first to the second .def generate_iplist(minip, maxip): .iplist = [] .minip_int = lst2int(str2list(minip)) .maxip_int = lst2int(str2list(maxip)) .for ip in range(minip_int, maxip_int+1): .iplist.append(list2str(int2lst(ip))) .return iplist # Example: .>>> if __name__ == '__main__': .for ip in generate_iplist('1.0.0.250', '1.0.1.5'): .print ip 1.0.0.250 1.0.0.251 1.0.0.252 1.0.0.253 1.0.0.254 1.0.0.255 1.0.1.0 1.0.1.1 1.0.1.2 1.0.1.3 1.0.1.4 1.0.1.5 -- "Codito ergo sum" Roel Schroeven ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: Fwd: [Tutor] Create list of IPs
> Liam Clarke wrote: [ snip ] > - increment an IP. This is the hardest part. Why? An ip (V4) is just an 32bit integer :-) The problem arises from the representation. Use something like "http://pynms.sourceforge.net/ipv4.html"; to switch between the various representations. > Kent HTH and Greetings, J"o! -- Wir sind jetzt ein Imperium und wir schaffen uns unsere eigene Realität. Wir sind die Akteure der Geschichte, und Ihnen, Ihnen allen bleibt nichts, als die Realität zu studieren, die wir geschaffen haben. -- Karl Rove zu Ron Suskind (NYT) DSL Komplett von GMX +++ Supergünstig und stressfrei einsteigen! AKTION "Kein Einrichtungspreis" nutzen: http://www.gmx.net/de/go/dsl ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] help with .get in Tkinter
Hi all. First, here is the code I have a problem with (I got same problem in my project) : from Tkinter import * def go(): e.get() print e main=Tk() e=Entry(main) e.pack() b=Button(main,text='OK',command=go()).pack() main.mainloop() For some reason the function is called before I click the button, and I get .10037088 before I have done a thing. How do I do it right ??? -- 1. The day Microsoft makes something that doesn't suck is probably the day they start making vacuum cleaners. 2. Unix is user friendly - it's just picky about it's friends. 3. Documentation is like sex: when it is good, it is very, very good. And when it is bad, it is better than nothing. - Dick Brandon ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: Fwd: [Tutor] Create list of IPs
Liam Clarke wrote: Hi, you could save yourself some hassle and do minipstr = '1.0.0.1' maxipstr = '1.0.15.16' minip = map(int, minipstr.split('.')) maxip = map(int, maxipstr.split('.')) iplist = [] for a in range(minip[2], maxip[2]+1): ... if a < maxip[2]: ... for b in range(minip[3], 255): ... iplist.append('.'.join(map(str, [minip[0],minip[1], a, b]))) ... else: ... for b in range(minip[3], minip[3]+1): ... iplist.append('.'.join(map(str, [minip[0],minip[1], a, b]))) Eek, that's a bit Perlish, might want to break the iplist.append line into ipintlist = [minip[0],minip[1], a, b] ipstrlist = map(str, ipintlist) iplist.append('.'.join(ipstrlist)) I don't think this will work correctly with for example minipstr = '1.0.0.1' maxipstr = '1.2.0.0' I would break this problem up conceptually. It has maybe four different parts: - convert a string representation of an IP address to a useful representation. Liam shows how to do this above, a list of ints is easy to work with. - convert the useful representation back to a string. Again, Liam shows how to do this. - compare two IPs. If the IPs are represented as lists of ints, you can compare them directly: >>> a=[1,2,1,5] >>> b=[1,2,3,4] >>> a>b False >>> a True >>> a==[1,2,1,5] True - increment an IP. This is the hardest part. You have to implement a counter that works with the list representation. Here is one way to do it - this function does the right thing with the last 'digit', then if there was a carry it calls itself recursively to increment the next digit. It rolls over from [255, 255, 255, 255] to [0, 0, 0, 0]: def incr(n, limit, ix=None): ''' Increment a number base (limit+1) represented as a list of ints ''' if ix is None:# initial call starts at the end ix = len(n) - 1 if ix < 0:# Off the end, give up return if n[ix] < limit: # Normal increment n[ix] += 1 else: # Increment with carry n[ix] = 0 incr(n, limit, ix-1) a=[1,2,1,5] incr(a, 255) print a a=[1,2,1,255] incr(a, 255) print a a=[1,2,255,255] incr(a, 255) print a a=[1,255,255,255] incr(a, 255) print a a=[255,255, 255,255] incr(a, 255) print a ## prints [1, 2, 1, 6] [1, 2, 2, 0] [1, 3, 0, 0] [2, 0, 0, 0] [0, 0, 0, 0] Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dictionary dispatch for object instance attributes question
Liam Clarke wrote: Hi, just an expansion on Brian's query, is there a variant of getattr for instance methods? i.e. class DBRequest: def __init__(self, fields, action): self.get(fields) def get(self, fields): print fields Instead of self.get in _init__, the value of action to call a function? Or, is it going to have to be dictionary dispatch? I don't understand your example, but instance methods are attributes too, so getattr() works with them as well as simple values. An instance method is an attribute of the class whose value is a function. When you access the attribute on an instance you get something called a 'bound method' which holds a reference to the actual function and a reference to the instance (to pass to the function as the 'self' parameter). You can call a bound method just like any other function. So: >>> class foo: ... def __init__(self): ... self.bar = 3 ... def baz(self): ... print self.bar ... >>> f=foo() getattr() of a simple attribute: >>> getattr(f, 'bar') 3 getattr() of an instance method returns a 'bound method': >>> getattr(f, 'baz') > Calling the bound method (note the added ()) is the same as calling the instance method directly: >>> getattr(f, 'baz')() 3 Of course you can do the same thing with dot notation for attributes: >>> b=f.baz >>> b > >>> b() 3 Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Instance into another instance
Ismael Garrido wrote: The idea of the class is to be able to create a "tree". Where each node can have subnodes, which in turn can have their subnodes... Are you creating a tree to represent XML data? There are many packages available that do this. You might want to look at ElementTree which is one of the easiest to use. In fact, even if you aren't trying to represent XML you might find ElementTree useful. http://effbot.org/zone/element.htm Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Fwd: [Tutor] Create list of IPs
Erp. -- Forwarded message -- From: Liam Clarke <[EMAIL PROTECTED]> Date: Sun, 20 Feb 2005 23:42:43 +1300 Subject: Re: [Tutor] Create list of IPs To: Ralfas Jegorovas <[EMAIL PROTECTED]> Hi, you could save yourself some hassle and do >>> minipstr = '1.0.0.1' >>> maxipstr = '1.0.15.16' >>> >>> minip = map(int, minipstr.split('.')) >>> maxip = map(int, maxipstr.split('.')) >>> >>> iplist = [] >>> >>> for a in range(minip[2], maxip[2]+1): ... if a < maxip[2]: ... for b in range(minip[3], 255): ... iplist.append('.'.join(map(str, [minip[0],minip[1], a, b]))) ... else: ... for b in range(minip[3], minip[3]+1): ... iplist.append('.'.join(map(str, [minip[0],minip[1], a, b]))) Eek, that's a bit Perlish, might want to break the iplist.append line into ipintlist = [minip[0],minip[1], a, b] ipstrlist = map(str, ipintlist) iplist.append('.'.join(ipstrlist)) HTH Liam Clarke It's ugly, but it works, someone better will shortly post a prettier, more elegant and efficient version. -- 'There is only one basic human right, and that is to do as you damn well please. And with it comes the only basic human duty, to take the consequences. -- 'There is only one basic human right, and that is to do as you damn well please. And with it comes the only basic human duty, to take the consequences. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Create list of IPs
Oops, also change this line for b in range(minip[3], 255): to for b in change(minip[3], 256): On Sun, 20 Feb 2005 23:43:07 +1300, Liam Clarke <[EMAIL PROTECTED]> wrote: > Erp. > > > -- Forwarded message -- > From: Liam Clarke <[EMAIL PROTECTED]> > Date: Sun, 20 Feb 2005 23:42:43 +1300 > Subject: Re: [Tutor] Create list of IPs > To: Ralfas Jegorovas <[EMAIL PROTECTED]> > > Hi, you could save yourself some hassle and do > > >>> minipstr = '1.0.0.1' > >>> maxipstr = '1.0.15.16' > >>> > >>> minip = map(int, minipstr.split('.')) > >>> maxip = map(int, maxipstr.split('.')) > >>> > >>> iplist = [] > >>> > >>> for a in range(minip[2], maxip[2]+1): > ... if a < maxip[2]: > ... for b in range(minip[3], 255): > ... iplist.append('.'.join(map(str, > [minip[0],minip[1], a, b]))) > ... else: > ... for b in range(minip[3], minip[3]+1): > ... iplist.append('.'.join(map(str, > [minip[0],minip[1], a, b]))) > > Eek, that's a bit Perlish, might want to break the iplist.append line into > > ipintlist = [minip[0],minip[1], a, b] > ipstrlist = map(str, ipintlist) > iplist.append('.'.join(ipstrlist)) > > HTH > > Liam Clarke > > > > It's ugly, but it works, someone better will shortly post a prettier, > more elegant and efficient version. > > -- > 'There is only one basic human right, and that is to do as you damn well > please. > And with it comes the only basic human duty, to take the consequences. > > -- > 'There is only one basic human right, and that is to do as you damn well > please. > And with it comes the only basic human duty, to take the consequences. > -- 'There is only one basic human right, and that is to do as you damn well please. And with it comes the only basic human duty, to take the consequences. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Create list of IPs
I am wondering how I would go about making a list of IPs between two set IPs. This is my initial code: def list2str(list): ip='' for part in list: if len(ip)!=0: ip=ip+'.'+part else: ip=ip+part return ip iplist = [] minip = ['1','0','0','1'] # starting IP maxip = ['1','0','15','16'] # ending IP currentip = minip # set currentip to value of minip while currentip != maxip: # carry out while currentip is not the same as the maxip iplist.append(list2str(currentip)) if currentip[2] currentip[3] = currentip[3]+1 I'd appreciate any help. Thanks, Ralf ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dictionary dispatch for object instance attributes question
Hi, just an expansion on Brian's query, is there a variant of getattr for instance methods? i.e. class DBRequest: def __init__(self, fields, action): self.get(fields) def get(self, fields): print fields Instead of self.get in _init__, the value of action to call a function? Or, is it going to have to be dictionary dispatch? Regards, Liam Clarke On Wed, 16 Feb 2005 16:49:21 -0500, Brian van den Broek <[EMAIL PROTECTED]> wrote: > Jeff Shannon said unto the world upon 2005-02-16 16:09: > > On Tue, 15 Feb 2005 23:48:31 -0500, Brian van den Broek > > <[EMAIL PROTECTED]> wrote: > > suggestions> > > > > Yes, if you know that you will only have one header per line, then > > it's reasonable to process them one line at a time. You could > > alternatively have the TP_file gather all the header lines for a given > > node into a list, and then process that list to create the Node > > instance, but given the specifics of your case you probably wouldn't > > gain anything over your current approach by doing so. > > > > This is what makes programming so interesting -- there's so many > > different choices possible, and which one is best depends on a large > > number of factors. When writing a program for some task, the best > > design for a particular set of circumstances may be completely > > different than the best design for a somewhat different particular set > > of circumstances -- and the best design for general usage is probably > > an altogether different thing still. > > > > Good luck! > > > > Jeff Shannon > > Thanks Jeff, > > the confirmation that my assessment made sense is very helpful. Due to > the my lack of experience (as discussed in my response to Kent) I'm > always uncomfortable rejecting a proposed solution -- is my assessment > that the solution isn't the best a product of that inexperience, or am > I on to something? So, thanks for taking the time to `bless' my > assessment. > > Best to all, > > Brian vdB > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > -- 'There is only one basic human right, and that is to do as you damn well please. And with it comes the only basic human duty, to take the consequences. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor