Re: PEP 354: Enumerations in Python
On 27 feb 2006, at 10:13, Tim Chase wrote: >> Uniqueness imposes an odd constraint that you can't have >> synonyms in the set: >> > shades = enum({white:100, grey:50, gray:50, black:0}) > > Blast, I hate responding to my own posts, but as soon as I > hit Send, I noticed the syntax here was biffed. Should have > been something like > shades = enum(("white", 100), ("grey", 50), > ("gray",50), ("black", 50)) > > Same could go for days of the week: > dow=enum(("sunday", 0), ("monday", 1), > ("start_of_work_week", 1), ... ("friday", 5), > ("end_of_work_week", 5)...) if enum would handle a dict as an input parameter by replacing it with the dict's iteritems() this is equivalent. --eric -- http://mail.python.org/mailman/listinfo/python-list
Re: translating PHP to Python
Dave wrote: > class A(object): > def create_child(self): > self.child = B() > self.child.do_stuff(self) > > class B(object): > def do_stuff(self, parent): > self.parent = parent > if self.parent.__class__.__name__ == 'A': > print "I'm a child of an A!" > else: > print "Well, I'm a motherless child. Does that mean I can > kill Macbeth?" Depending on your actual needs you could change that to: class A(object): def create_child(self): self.child = B(self) class B(object): def __init__(self, parent): self.do_stuff(parent) def do_stuff(self, parent): self.parent = parent if self.parent.__class__.__name__ == 'A': print "I'm a child of an A!" else: print "I know ye not!" which IMHO makes it clearer from A's perspective. --eric -- http://mail.python.org/mailman/listinfo/python-list
Re: python printout format
Yong, Are you sure you indent with the same characters and there is no non-print code there? --eric On 1 feb 2006, at 17:36, Yong Wang wrote: > Hi, >I have a data set like row = [[1,2,3,4,5], [6,7,8,9,10], > [11,12,13,14,15]] >when I use loop to print out data, I got compile error in printout > statment. >while j<24: >print ' ',string.ljust(str(units[0]), 5), >print ' ',string.ljust(str(ports[j], 3), >print ' ',string.ljust(PortlinkbeatInv[row[j][0]],14), >print ' ',string.ljust(PortStatusInv[row[j][1]], 19), >print ' ',string.ljust(PortTestResultInv[row[j][2]],17), >print ' ',string.ljust(PortVisualInv[row[j][3]],19), >print ' ',string.ljust(PortAddrVioInv[row[j][4]],15) >j=j+1 > misc.doPg('Port status listing on ' + tag + ' (Page %d):', > vals,doBanner,doRow) > >Error messages are: > python -c "import compileall; compileall.compile_dir('.',0)" > Listing . ... > Compiling ./Ciscoports.py ... > File "./Ciscoports.py", line 753 > print string.ljust(PortlinkbeatInv[row[j][0]],14), > ^ > SyntaxError: invalid syntax > > >How should I change to printout row data [1,2,3,4,5] in one row in > assigned format? >Many thanks, > > Yong > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: urllib2 - My POST Request just isn't working right
Don't forget to substitute the actual Content-length for %d! --eric On 1 feb 2006, at 17:34, Gregory Piñero wrote: > Correction: > --- > -- > POST /GatewayDC HTTP/1.0 > Referer: YourCompanyNameGoesHere > Host: SSLserver.fedex.com > Accept: image/gif, image/jpeg, image/pjpeg, text/plain, text/html, */* > Content-Type: image/gif > Content-length: %d > > Your FedEx Transaction > --- > -- > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: urllib2 - My POST Request just isn't working right
Hi Greg, > values = {'request':xml_request} > headers = { 'Referer' : 'YourCompany', > 'Host':'https://gatewaybeta.fedex.com/GatewayDC', > 'Accept':'image/gif, image/jpeg, image/pjpeg, text/plain, > text/html, */*', > 'Content-Type':'image/gif' > } Could it be Host? It should be "gatewaybeta.fedex.com" AFAIKT --eric -- http://mail.python.org/mailman/listinfo/python-list
Re: Class Variable Access and Assignment
Steven D'Aprano wrote: > On Thu, 03 Nov 2005 15:13:52 +0100, Eric Nieuwland wrote: >> 2 When an instance of the class is created, what effectively happens >> is >> that a shallow copy of the class object is made. >> Simple values and object references are copied. > > No. > > py> class Parrot: > ... var = 0 > ... > py> p = Parrot() > py> Parrot.var is p.var > True > py> Parrot.var = {"Hello world": [0, 1, 2]} > py> Parrot.var is p.var > True > > It all boils down to inheritance. When Python does a look up of an > attribute, it looks for an instance attribute first (effectively trying > instance.__dict__['name']). If that fails, it looks up the class second > with instance.__class__.__dict__['name'], and if that fails it goes > into a > more complex search path looking up any superclasses (if any). Note my use of "effectively" and "shallow copy". Your example demonstrates how Python postpones the shallow copy until you tell the object to differ from the class/ The examples used only use a class an an instance thereof. They are valid without inheritance. >> This explains: >> - why methods and complex objects (e.g. lists) are shared among >> instances of a class and the class itself >> - simple values are not shared > > No. it is all about the inheritance, and mutable/immutable objects. NO. You're referring to the implemented mechanism. Other implementations with the same semantics are possible. -- http://mail.python.org/mailman/listinfo/python-list
Re: Getting Python Accepted in my Organisation
Stuart Turner wrote: > "Python is a scripting language like Perl, awk, tcl, Java etc... it > is > not quite a fully developed OO language, but does support some OO that > Perl > doesn't. To be clear, these scripting languages have their place in > our > environment, but they are not full replacements for C#, Java, C, > etc... > because they do not come with the full range of libraries e.g GDI > libraries. Python has to be compared to Perl, Awk in order to > evaluate it. > Perl, until recently, did not support threading. Why would it? it is a > scripting language and can run async shell commands. I would be > interested > to learn if Python supports a robust threading model (not just a > pointer > reference to an object), as this is a significant drawback when using a > scripting language. CGI only works because the container can thread > with > Perl. Python is object orientated, but I do not know what > implementation? > Essentially any language with a pointer can claim to be OO, although > Python > does market itself on OO capabilities. Do you know what implementation > they have used? So, Java both is and is not a scripting language? My favourite article says it all in its title: http://www.python.org/pycon/dc2004/papers/6/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Class Variable Access and Assignment
Stefan Arentz wrote: > It is really simple. When you say b.a then the instance variable 'a' > is looked up first. If it does not exist then a class variable lookup > is done. This mixing of class and instance variable might be the cause of confusion... I think of it as follows: 1 When the class statement ends a class object is created which is filled by all the statements inside the class statement This means all variables and functions (methods) are created according to the description. NOTE This happens just once. 2 When an instance of the class is created, what effectively happens is that a shallow copy of the class object is made. Simple values and object references are copied. This explains: - why methods and complex objects (e.g. lists) are shared among instances of a class and the class itself - simple values are not shared --eric -- http://mail.python.org/mailman/listinfo/python-list
Re: Class Variable Access and Assignment
Graham wrote: > > > class _class: > var = 0 > #rest of the class > > instance_b = _class() > > _class.var=5 > > print instance_b.var # -> 5 > print _class.var # -> 5 > > > [...] > > > instance_b.var = 1000 # -> _class.var = 5 > _class.var = # -> _class.var = > > > > An obvious error occurs. Nope. 'var' is NOT a class variable! It is a pre-filled instance variable. You need some explicit programming to get class variables. --eric -- http://mail.python.org/mailman/listinfo/python-list
Re: [Info] PEP 308 accepted - new conditional expressions
Dave Hansen wrote: > And Basic, and Fortran, and Lisp, and just about any programming > language you care to name, including python (if Condition: Affirmative > else: Negative). > > Not to mention that the sequence is identical to execution order. > It's just plain goofy to have to scan to the middle of an expression > to find out what happens first, then depending on the result of that, > skipping back over what you just skipped. 1 word: Perl > FWIW, in over 20 years of C programming, I use it pretty rarely too, > though I never had to look it up after I first saw it in K&R. It only > very rarely holds any advantage over a simple if/else statement, > especially in modern compilers. It's most useful in macros, which > python doesn't even have. same time span and I used it zillions of times ==eric -- http://mail.python.org/mailman/listinfo/python-list
Re: Lambda evaluation
Joshua Ginsberg wrote: > Try this one: > d = {} for x in [1,2,3]: > ... d[x] = lambda *args: args[0]*x > ... d[1](3) try it with: d[x] = (lambda x=x: (lambda *args: args[0]*x))() the outer lambda fixes the value of x and produces the inner lambda with the fixed x value --eric -- http://mail.python.org/mailman/listinfo/python-list
Re: [Info] PEP 308 accepted - new conditional expressions
Robin Becker wrote: > As mentioned earlier only a dictator can make such decisions and of > course as > with many dictatorships the wrong decision is often made. There's no > such thing > as a benevolent dictatorship. Ever cared to check what committees can do to a language ;-) --eric -- http://mail.python.org/mailman/listinfo/python-list
Re: Build spoofed IP packets
billie wrote: > Traceback (most recent call last): > File "C:\test\client.py", line 156, in ? > send_pkt() > File "C:\test\client.py", line 96, in send_pkt > s.sendto(ip.get_packet(), (dst, 0)) # send packet to server > socket.error: (10004, 'Interrupted system call') > > Note: this only happens on Windows XP prof SP2. On Linux I got no > problems. > I run the program as Administrator. It's SP2. Microsoft decided allowing raw socket access is a security threat and disabled it in SP2. --eric -- http://mail.python.org/mailman/listinfo/python-list
Re: named pipe input
max(01)* wrote: > $ cat file_input_3.py > #!/usr/bin/python > > import sys > > MIAPIPE = open("una_pipe", "r") > > for riga in MIAPIPE: >print riga, > ... > [...] > BUT if i try to do the same with the python code, something different > happens: i have to type ALL the lines on console #2 and complete the > cat > command (ctrl-d) before seeing the lines echoed on console #1. You could try: for riga in MIAPIPE: print riga # NO COMMA! sys.stdout.flush() -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Wireless Extension Module (pyiw)
Jeremy Moles wrote: > I am mostly done with writing an extension module in C that wraps (and > makes easier) interfacing with libiw (the library that powers iwconfig, > iwlist, and friends on Linux). We're using this internally for a tool > to > manage wireless connectivity. This is a million times better than > hundreds of invocations of the iw* suite of tools. :) > > Would anyone else find something like this useful? I know a few distros > maintain their own suite of config tools, but I don't think there is > yet > an actual binding for python. > > Anyway, I am just curious... we plan on using it, at any rate. It's > already made the code easier to read, faster, and more reliable. > > # example usage - > > wlan = pyiw.WirelessInfo("wlan0") > > nets = wlan.Scan() > > for net in nets: > print net.signal_quality > > wlan.essid > wlan.key > wlan.protocol > > wlan.essid = "LV-426" > > wlan.Reconfigure() So cool! YES, I'd like to have that. Preferably as source code since I have quite a number of different Un*xes. --eric -- http://mail.python.org/mailman/listinfo/python-list
Re: FTP over SSL (explicit encryption)
David Isaac wrote: > I am looking for a pure Python secure ftp solution. > Does it exist? Do you want SFTP or FTP/S? > I would have thought that the existence of OpenSSL > would imply "yes" but I cannot find anything. > > ftplib does not seem to provide any secure services. Indeed. If you want SFTP, just make a copy of ftplib and modify so it will use an SSL socket instead of a normal socket. After that, only some minor point may remain. > [...]I know about M2Crypto > http://sandbox.rulemaker.net/ngps/m2/ > but that requires installing SWIG and OpenSSL. > (If someone tells me they have found this trivial > under Windows, I am willing to try ... ) I guess SFTP should work on Windows as well. > I would have thought that this was a common need with > a standard Python solution, so I suspect I'm overlooking > something obvious. AFAIK you're not. I'm having a look at FTP/S right now. That's a little more complicated, but it seems doable. If I succeed, I guess I'll donate the stuff as an extension to ftplib. --eric -- http://mail.python.org/mailman/listinfo/python-list
plug-ins
Hi all, The app I'm working on keeps getting new transforms and I'm tired of adding them by hand. So here it goes: Can anyone provide me with clues/examples/references on how to create a plug-in framework? tx, --eric -- http://mail.python.org/mailman/listinfo/python-list
Re: Define Constants
codecraig wrote: My directory structure looks like... C:\ --> abc.py --> utils --> __init__.py --> CustomThing.py Ok, CustomThing looks like... TOP = 0 LEFT = 1 class CustomThing: def __init__(self): self.foo = "foo" so, from abc.py I have from utils.CustomThing import CustomThing print CustomThing.TOP but i get an error: AttributeError: class 'CustomThing' has no attribute 'TOP' How can I access those?? You're only importing the class. Try importing the whole module: from utils import CustomThing print CustomThing.TOP --eric -- http://mail.python.org/mailman/listinfo/python-list