Re: Can I set timeout for the sys.stdin.readline() ?
On Jun 10, 2006, at 5:14 AM, ywzhan wrote: > Hi, I am new here. > When I use sys.stdin.readline() to get input string from user, > how can I set a timeout value for this operation? > thank you. > > You can do a select() on sys.stdin, and put a timeout on the select, ie: rfds, wfds, efds = select.select( [sys.stdin], [], [], 5) would give you a five second timeout. If the timeout expired, then rfds would be an empty list. If the user entered something within five seconds, then rfds will be a list containing sys.stdin. Note that by "user entered something", I mean they pressed Enter/Return. The select will not exit if they just enter other text. Jay P. -- http://mail.python.org/mailman/listinfo/python-list
Re: unzip zip files
On May 12, 2006, at 5:45 PM, DataSmash wrote: > I need to unzip all zip file(s) in the current directory > into their own subdirectories. The zip file name(s) always > start with the string "usa" and end with ".zip". > The code below will make the subdirectory, move the zip > file into the subdirectory, but unzips the contents into the > root (current) directory. I want the contents of the zip file > unloaded into the newly created subdirectory where the zip file is. > > Any ideas? > Thanks. > R.D. > > import subprocess > > # Get all the zip files in the current directory. > for zip in os.listdir(''): > if zip.endswith(".zip"): > > # Remove the first 3 and the last 4 characters > # e.g. usa12345.zip becomes 12345 > zipBase = zip[3:-4] > > # Make directory for unzipping > os.mkdir(zipBase) > > # Move the zip file to the subdirectory > shutil.move(zip, zipBase) > > # Make system call "unzip" > subprocess.Popen(["unzip", zipBase + "\\" + zip]).wait() > Ouch... Is there any reason that you're using subprocess+'unzip', as opposed to using the 'zipfile' module from the standard library? Jay P. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to doctest if __name__ already used?
On May 5, 2006, at 10:33 PM, Leo Breebaart wrote: > I have a simple question (I hope), but one I'd love to get some > feedback on in case I am missing something obvious: > > If I have a Python script that is executable, and therefore > already uses '''if __name__ == "__main__"''' to call a function > of its own, what is then the easiest yet still most Pythonic way > to run doctest on the docstrings in the file? > > Trigger doctest.testmod() via a "--test" command-line option, is > what I'm thinking. But is that really the best way? > If it is just a single standalone script, then yeah, do it that way. Once your program becomes a few files though, you might want a separate "test" file that runs doctest.testmod on each of them. Jay P. -- http://mail.python.org/mailman/listinfo/python-list
Re: unittest.main-workalike that runs doctests too?
On May 5, 2006, at 6:35 AM, John J. Lee wrote: > ...I wrote something to do this once, but didn't do as good a job as I > might have done and wondered if anybody else has done it properly. > > I know about nose, but it seems just a little too magical for my > tastes, and includes stuff I don't really need. > > > John > > -- > http://mail.python.org/mailman/listinfo/python-list > nose actually has very little magic, its operation is quite straightforward. I've played with its codebase before, and it's quite clean. And the new 0.9 branch uses a plugin system for some of the extra functionality, so you don't even have to install all the things it's capable of doing. Jay P. -- http://mail.python.org/mailman/listinfo/python-list
Re: pythonic way to sort
On May 4, 2006, at 12:12 AM, [EMAIL PROTECTED] wrote: > hi > I have a file with columns delimited by '~' like this: > > 1SOME STRING ~ABC~12311232432D~20060401~ > 2SOME STRING ~DEF~13534534543C~20060401~ > 3SOME STRING ~ACD~14353453554G~20060401~ > > . > > What is the pythonic way to sort this type of structured text file? > Say i want to sort by 2nd column , ie ABC, ACD,DEF ? so that it becomes > > 1SOME STRING ~ABC~12311232432D~20060401~ > 3SOME STRING ~ACD~14353453554G~20060401~ > 2SOME STRING ~DEF~13534534543C~20060401~ > ? > I know for a start, that i have to split on '~', then append all the > second columns into a list, then sort the list using sort(), but i am > stuck with how to get the rest of the corresponding columns after the > sort > > thanks... > A couple ways. Assume that you have the lines in a list called 'lines', as follows: lines = [ "1SOME STRING ~ABC~12311232432D~20060401~", "3SOME STRING ~ACD~14353453554G~20060401~", "2SOME STRING ~DEF~13534534543C~20060401~"] The more traditional way would be to define your own comparison function: def my_cmp(x,y): return cmp( x.split("~")[1], y.split("~")[1]) lines.sort(cmp=my_cmp) The newer, faster way, would be to define your own key function: def my_key(x): return x.split("~")[1] lines.sort(key=my_key) The key function is faster because you only have to do the split("~")[1] once for each line, whereas it will be done many times for each line if you use a comparison function. Jay P. -- http://mail.python.org/mailman/listinfo/python-list
Re: Protocols for Python?
On Apr 27, 2006, at 3:26 PM, [EMAIL PROTECTED] wrote: > I think I have reached an important moment in my growth as a Python > Developer. I realize now why interfaces aren't really necessary in > Python. :] > > Still, I'm designing an application that I want to be extendable by > third-party developers. I'd like to have some sort of documentation > about what behavior is required by the components that can be added to > extend the application. I'd thought I might try documenting these > behaviors as "protocols" instead of creating abstract classes with no > method implementations. > > I stumbled across PyProtocols, but I don't think it is what I am > looking for. I don't want to "implement" a form of interfaces in the > python language, just document the required behavior for certain > objects in my application for other developers. > > Is there a standard way to document protocols in Python? Of should I > come up with something tailored to my needs. > PyProtocols, for the most part, can be replaced with RuleDispatch (also part of PEAK). On the surface they may look different, but when you get down to it, just about anything you can do with PyProtocols you can do with RuleDispatch, and there's a lot of stuff you can do with RuleDispatch that you can't with PyProtocols. And, RuleDispatch makes it MUCH easier to have your application configurable by third parties. I started a major project about a year ago with PyProtocols. About half way through, RuleDispatch came out, and now all new additions to the project use RuleDispatch instead. Jay P. -- http://mail.python.org/mailman/listinfo/python-list
Re: Instruction at "0x00FC3D70" use memory address "0x00000000". Can't be "read".
On Apr 24, 2006, at 5:38 PM, Neil Adams wrote: > How do Ifix memory message Ox033fc512 at Ox can't be read > You're going to have to provide a LOT more information if you expect anyone here to help you with that. What program caused that? For all we know, notepad.exe could have crashed and caused that error. You have to tell us what program (I'm assuming Python, but who knows, maybe you sent the message to the wrong list), what your OS is, what you were doing, what *exactly* happened, etc. etc. Then maybe someone can provide some insight. Jay P. -- http://mail.python.org/mailman/listinfo/python-list
Re: Passing data attributes as method parameters
On Apr 23, 2006, at 4:59 PM, Panos Laganakos wrote: > Thanks Ben. > > What does it mean that they're statically bound? > > It seems weird that I'm not able to access variables in the class > namespace even though these attributes come into existance after class > instantiation. > The parameters are "put together" and bound to the method when the class is defined, *not* after class instantiation. As an example: >>> class MyClass: ... def my_method(self, param1 = []): ... print param1 ... param1.append(5) ... >>> x = MyClass() >>> x.my_method() [] >>> x.my_method() [5] >>> y = MyClass() >>> y.my_method() [5, 5] >>> y.my_method() [5, 5, 5] >>> Usually, people use immutable datatypes as default parameter values, so it doesn't cause a problem. And an even more illustrative example: >>> class M: ... x = 2 ... def my_method(self, param = M.x): ... pass ... Traceback (most recent call last): File "", line 1, in ? File "", line 3, in M NameError: name 'M' is not defined The class is still being built when the method is created, so even the name "M" doesn't exist yet. Ben's solution is probably the best way to do what you're looking for. Jay P. -- http://mail.python.org/mailman/listinfo/python-list
Re: tail a file (win)
On Apr 20, 2006, at 9:33 AM, david brochu jr wrote: > Hello, > > I wrote a script to monitor ping activity and output it to a log file. > I am using windows and want to have another script constantly check > the latest entry to see if Request timed out is seen. Is there a way > to "tail" a file much like I would in Unix so I can just see the > latest entry and move from there?-- > http://mail.python.org/mailman/listinfo/python-list I've not tried it myself, but I found this via a Google search: http://mithrandr.moria.org/blog/147.html Jay P. -- http://mail.python.org/mailman/listinfo/python-list
Re: Mouse control in OS X
On Apr 20, 2006, at 1:22 PM, Mateo wrote: > I've played around with pygame, and it seems to have much of what I > need. Still, is there any way to initiate a mouse click from Python? > > Thanks. > In OS X, you'll probably need PyObjC. I've never had a chance to try it myself, but if any package is going to let you control things like native mouse clicks (outside of a custom GUI environment like PyGame, wx, gtk, QT, etc.), it will probably be PyObjC. Jay P. -- http://mail.python.org/mailman/listinfo/python-list
Re: 2.5 excitement
On Apr 18, 2006, at 9:06 PM, Alex Martelli wrote: > > Funny timing coincidence: your 1st draft of Python for Dummies going in > now, my 2nd edition of Python in a Nutshell just went to production, > AND > Wesley Chun's 2nd ed is also being finished this week. Three > Baypiggies > (or whatever we'll have to call ourselves) all finishing Python books > (be they new, or 2nd eds) all within one week of Easter -- what's the > chances of THAT?-) > > Wesley Chun is doing a second edition of Core Python? Oh wow! That's the book I learned Python with oh so long ago(between 1.5.2 and the mysterious 1.6). I told people for a long time that Core was the best book with which to learn Python, but I stopped doing that as it's too out of date now. Glad to see Ruby isn't the only language getting a bunch of new good books. Jay P. -- http://mail.python.org/mailman/listinfo/python-list
Re: Plug-Ins In A Python Application
On Apr 18, 2006, at 9:58 AM, [EMAIL PROTECTED] wrote: > Its the Java developer again... > > I'm working on an application framework that I would like to implement > in Python. Part of the application framework is a plug-in model that is > similar to the one used in Eclipse. > > Is it possible to load and use "modules" containing plug-in code > written by third party developers into a running instance of the > framework? How would I do this? Do I need to "dynamically load" the > module at runtime? (I will scan a folder in the application direcotry > for XML files containing information about the plug-ins, including the > modules that implement them.) > Well, the "state of the art" in Python plugins is moving towards Eggs: http://peak.telecommunity.com/DevCenter/PythonEggs Though that might be a bit much for you to jump into considering you're just starting out in Python. However, if you do learn it now, you won't have to learn it again later when everyone's using it :) Jay P. -- http://mail.python.org/mailman/listinfo/python-list
Re: freakin out over C++ module in python
On Apr 18, 2006, at 4:43 AM, [EMAIL PROTECTED] wrote: > ok, well enough, looked at struct and it does seem to be what i am > after. for that anyway. > thanks, guess i will just have to take the time and pull it apart. > I recommend you also take a look at http://pyconstruct.sourceforge.net/ It has the same purpose as 'struct', but is MUCH more Pythonic. I've described Contstruct as "the replacement for 'struct' I've been looking for since I started coding in Python". Jay P. -- http://mail.python.org/mailman/listinfo/python-list
Re: Ironpython book?
On Apr 17, 2006, at 8:58 PM, Alex Martelli wrote: > > I don't know of any such books, but if M$ is willing to slip me a > suitable sweetener (to make it worth my while to install Windows again > after years of blissfully Windows-free existence: it must at least > cover > the expense of the extra Zantac and Maalox consumption, plus suitable > compensation for pain and suffering), AND lend me the consulting > expertise of the Ironpython team, hey, I _could_ be persuaded to write > "IronPython in a Nutshell" (not that I'd ever admit it in public, of > course...). > Q: "So Alex, what are you doing with your 20% time at Google right now?" A: "Working for Microsoft!" Worlds collide! Jay P. -- http://mail.python.org/mailman/listinfo/python-list
Re: Passing a packed C structure to a c module
On Apr 14, 2006, at 9:44 AM, Philippe Martin wrote: > Thanks, > > It's a pretty big structure: painfull to pass each item as a param. > > Regards, > > Philippe > Maybe this can do something for you? http://pyconstruct.sourceforge.net/ Jay P. -- http://mail.python.org/mailman/listinfo/python-list
Re: print without newline "halts" program execution
On Apr 13, 2006, at 5:57 PM, Karlo Lozovina wrote: > Consider this short script: > > --- > from time import time, sleep > > st = time() > print 'Start: %f, ' % st, > sleep(10) > sp = time() > print 'Stop: %f, Duration: %f' % (sp, (st - sp)) > --- > > On my environment (Linux, py24), when run, Python first waits 10s, and > then produces the entire output. How, can I make it print first part > ('Start: %f, '), then wait 10s, and then append (WITHOUT NEWLINE) that > second print statement? > > I'm writting a script with lot of output which has to go on the same > line, > but I don't want to wait for it to end to see output, I just want it to > print parts as it's finished with them. > > Using sys.stdout.write() produces the same behavior, so what can I do? > from time import time, sleep > > Your problem is that the 'print' statement is sending the text to sys.stdout, and sys.stdout is buffered. There are other ways to do this, but try this: from time import time, sleep import sys st = time() print 'Start: %f,'% st, sys.stdout.flush() sleep(10) sp = time() print 'Stop: %f, Duration: %f' % (sp, (st-sp)) Jay P. -- http://mail.python.org/mailman/listinfo/python-list
Re: Remove Whitespace
On Apr 13, 2006, at 12:09 PM, Kelvie Wong wrote: > try this: > > string = 'D c a V e r " = d w o r d : 0 0 0 0 0 6 4 0' > import re > re.sub("\s", "", string) > > On 4/13/06, david brochu jr <[EMAIL PROTECTED]> wrote: > Even easier (if you only want to replace blank spaces, and not all whitespace): string = 'D c a V e r " = d w o r d : 0 0 0 0 0 6 4 0' string.replace(" ", "") Note to the original poster: It's a *bad* idea to call a variable 'string', as there's already a module in the standard library called 'string'. Jay P. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python2CPP ?
On Apr 12, 2006, at 5:13 AM, Michael Yanowitz wrote: >> > > Thanks. I want to translate from Python to C++ for a few reasons: > 1) Curiosity. I would like to see how well the translation goes. > 2) Efficiency. It is alot quicker to code something in Python. If I can >write it in Python and auto-convert it to C++. I would save time > coding. > 3) Education. I would learn more about Python, C++, their similarities > and > differences. > 4) Other. Just want to know how well Language translators work these > days. I > have seen >Fortran2C and Pascal2C translators in the past. Would like to see > how > well these >work with Python. > > Thanks in advance: You want this: http://shed-skin.blogspot.com/ It can only do a subset of Python, but it does generate C++ code, and it can see some big speedups. Jay P. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python-list Digest, Vol 31, Issue 119
On Apr 8, 2006, at 1:40 PM, [EMAIL PROTECTED] wrote: > > Hi there, > > I'm new to Python, but know other scripting and programming languages. > I > want to develop a script which will receive emails with attachments > from my POP3 account, perform certain actions on it and email it back > to someone else. > > However, I'm not familiar with any Python library which does it. Could > you a guide me to a relevant library which can handle emails? > > I haven't decided yet what scripting language I will use, so a nice > module for Python will probably make me choose it over Perl. :) > > Thanks in advance. :) Well, there's always the 'email' module, part of the standard library: http://docs.python.org/lib/module-email.html Jay P. -- http://mail.python.org/mailman/listinfo/python-list
Re: Missing C modules in 2.5 on OS X?
On Apr 6, 2006, at 3:25 PM, Jay Parlar wrote: > I just downloaded, built, and installed the new 2.5 alpha on OS X > 10.3, and it seems that the new 'functional' didn't get installed. > > I know that it got built (done from the 2.5 source directory): > > Jay-Computer:~/Desktop/Python-2.5a1 jayparlar$ find . -iname > functional* > ./build/lib.darwin-7.9.0-Power_Macintosh-2.5/functional.so > ./build/temp.darwin-7.9.0-Power_Macintosh-2.5/functionalmodule.o > ./Modules/functionalmodule.c > > > But it doesn't seem to have been installed, as the same 'find' from > /usr/local/lib/python2.5 produces nothing at all. > > I used the 'make altinstall' option when installing. > > Now that I'm looking closer at it, it seems that *no* modules > implemented in C have been installed... > > Jay P. > > Never mind, I just forgot to reset my PYTHONPATH variable to point to the new site-packages. It all works when I set it to /Users/jayparlar/Library/Python2.5/site-packages. Kind of weird though that only the C modules went to ~/Library/Python2.5/site-packages, but all the .py modules went to /usr/local/lib/python2.5/. Could that be because of my ~/.pydistutils.cfg having the following: [install] install_lib = ~/Library/Python$py_version_short/site-packages install_scripts = ~/bin Jay P. -- http://mail.python.org/mailman/listinfo/python-list
Missing C modules in 2.5 on OS X?
I just downloaded, built, and installed the new 2.5 alpha on OS X 10.3, and it seems that the new 'functional' didn't get installed. I know that it got built (done from the 2.5 source directory): Jay-Computer:~/Desktop/Python-2.5a1 jayparlar$ find . -iname functional* ./build/lib.darwin-7.9.0-Power_Macintosh-2.5/functional.so ./build/temp.darwin-7.9.0-Power_Macintosh-2.5/functionalmodule.o ./Modules/functionalmodule.c But it doesn't seem to have been installed, as the same 'find' from /usr/local/lib/python2.5 produces nothing at all. I used the 'make altinstall' option when installing. Now that I'm looking closer at it, it seems that *no* modules implemented in C have been installed... Jay P. -- http://mail.python.org/mailman/listinfo/python-list
Re: doctest, unittest, or if __name__='__main__'
On Mar 21, 2006, at 1:12 PM, Fredrik wrote: > >> (I notice there's no mention in PEP 3000 of deprecating >> the doctest module). > > why is it that comp.lang.python is suddenly full of folks who > want to deprecate anything they don't understand ? don't > you have better things to do with your time ? I've wondered the same sort of thing for awhile. It often comes back to people taking ideas from whatever language they're familiar with, and trying to force them into Python. The reason for this, as I see it, is that Python is so incredibly easy to get into, that many people think they fully understand it after a few days use. Jay P. -- http://mail.python.org/mailman/listinfo/python-list
Re: TypeError: unsupported operand type(s) for -: 'tuple' and 'tuple'
On Mar 16, 2006, at 3:14 PM, Peter Bismuti wrote: > Can anyone explain this error message? > > Traceback (most recent call last): > File "/acct/pjb9508/RAT/Scripts/PyQt/RatDialog.py", line 30, in > NewClipButton_clicked > > self.rat.planeClip(self.o,"SQUARE"+str(self.squareCounter),width,origin > ) > File "/acct/pjb9508/RAT/Scripts/PyQt/rat.py", line 12, in planeClip > plane1[1] = origin[1]-width > TypeError: unsupported operand type(s) for -: 'tuple' and 'tuple' > > It looks like origin[1] and 'width' are both tuples. Substraction is not defined on tuples. That's what the error message is saying at least: "TypeError: unsupported operand type(s) for -:" -This means that the '-' operator received some bad types, types it doesn't know how to work with "'tuple' and 'tuple'" - This means the two types it received were both tuples. Jay P. -- http://mail.python.org/mailman/listinfo/python-list
Re: Type Hinting vs Type Checking and Preconditions
On Mar 8, 2006, at 8:43 AM, Tom Bradford wrote: > > > This type of hinting would only break type ducking in-so-much as a > function that leveraged that hinting would be looking specifically for > an instance of a particular type, which would be absolutely no > different than a developer performing the type check manually and > throwing it out if the type were invalid. It would otherwise just be a > lot of tedious and repetitive work for the developer. > Wow, I *really* hope that you're not writing functions that check the types of the incoming arguments. It's generally accepted that using "isinstance()" and "type()" in your code are evil things to do. There are very few places where it's appropriate to use them. If you're using them a lot, then I suspect you're relatively new to Python, and are programming with a Java/C++ mindset. We'll have to do what we can to get you out of that :) Jay P. -- http://mail.python.org/mailman/listinfo/python-list
Re: Type Hinting vs Type Checking and Preconditions
On Mar 8, 2006, at 7:20 AM, Tom Bradford wrote: > Really what we're talking about here is weak typing in the form of > optional type hinting performed on a function by function basis. As an > option, what it would do is allow an author to semantically 'hint' to > the interpreter that a function is expecting a certain type, and > perform any implicit conversion if the passed type is not what was > expected, thus translating to a semantically expected result. > So how would that system deal with this? class A(object): def foo(self): print "bar" class B(object): def foo(self): print "bar" def typedfunction(x : A): x.foo() b = B() typedfunction(b) #Your system would probably consider this an error This is an example of why type checking/hinting is no good, as it would break duck typing. In terms of their definitions, A and B have nothing in common (ie. B is not a subclass of A), yet I should be able to use instances of either one, whenever the method 'foo' is expected. Type hinting would completely break that. This again is why I point you in the direction of PEP 246. > > The fact is, that with a system such as this, you could just 'not use > it', and none would be the wiser, as it's an authorship extension > rather than a calling extension. What I was suggesting is that nothing > change other than function/method prototypes, and that everything, > everwhere, at all times continues to be represented as scalars. The problem is when someone decides to use this option, and releases a library with it. Now everyone who wants to use this library is forced to use this type hinting. It would create a divide in available Python code. (Of course, adaption would result in the same issue, but I *think* more people would be willing to use adaption, as it doesn't break duck typing). Jay P. -- http://mail.python.org/mailman/listinfo/python-list
Re: Type Hinting vs Type Checking and Preconditions
On Mar 7, 2006, at 1:32 PM, Tom Bradford wrote: > > Let me first say that I'm sure that this subject has come up before, > and so forgive me for beating a dead horse. Secondly, let me say that > Python's strength is its dynamic nature, and I don't believe that it > should ever require a precondition scaffolding. With that said, I do > believe that something like type hinting would be beneficial to the > Python community, both for tool enablement and for disambiguous > programming. > > [snip] > Thoughts? You may want to look at the various "adapt" ideas that have come along (prompted by Alex Martelli's http://www.python.org/peps/pep-0246.html). The two dominant implementations of this idea are PJE's PyProtocols, and Zope.Interfaces. I've used PyProtocols with great success in the past. The basic problem with type "hinting" is that it totally breaks duck typing, which is one of the reasons Python's dynamic nature is so powerful. Just because a function expects a "list object", I should not be prevented from passing in my own custom object that supports the necessary parts of the list protocol. This is where adaption makes its mark, as it is more about supported interfaces on an object than about actual types. It's also important to note that while Guido did spend a lot of time thinking about optional type markups (and this caused a LOT of hand wringing in the Python community, the general consensus in the end was that there was no real benefit from it. (I got the impression that a lot of the push was from Java/C++ people that didn't really understand what dynamic languages are about, and wanted to make Python more like Java/C++. Just my impression though). And in case you're thinking that maybe we could get some performance gains out of adding optional types, you should check out Brett Cannon's Master's thesis. In it, he actually added some typing markup to Python, and the end result was almost no gain. HOWEVER: If you feel this is something you still want to have a go at , PLEASE try it, and let the community know what your results were. It's making me sad that there's a lot of talk these days about how unwilling the Python community is to hear new ideas. And if you do decide to go forward with it, you should look at the 2.5 branch of the CPython code, as the new AST is supposed to make it *much* easier to experiment with changes to the language. Good luck! Jay P. -- http://mail.python.org/mailman/listinfo/python-list
Re: Checking function calls
On Mar 6, 2006, at 8:44 PM, James Stroud wrote: > Since python is "weakly typed", you will not be able to check what > "type" of arguments a function expects. TypeErrors relating to the > type of argemtns you pass will be raised inside the function only and > not when it is called. I.e. there is no type checking when a function > is called, only in its body. > I don't mean to take a tangent from the original purpose of the thread, but it is generally accepted that Python is STRONGLY, dynamically typed. An example of weak dynamic typing would be Perl. A lot of it of course comes down to one's definitions of strong/weak, but general consensus (at least around c.l.p.) is that Python is a strongly typed language. Jay P. -- http://mail.python.org/mailman/listinfo/python-list
Re: Adding method at runtime - problem with self
On Mar 5, 2006, at 2:30 PM, Marek wrote: > Assume I want to add a method to an object at runtime. Yes, to an > object, not a class - because changing a class would have global > effects and I want to alter a particular object only. The following > approach fails: > > class kla: > x = 1 > > def foo(self): > print self.x > > k = kla() > k.foo = foo > k.foo() > > I know where the problem is. The method shouldn't have 'self' > parameter. But how do I access object's attributes without it? > > Best regards, > > Marek > First off, it should be 'class kla(object)', but that's minor. Try this: import types class kla(object): x = 1 def foo(self): print self.x k = kla() k.foo = types.MethodType(foo,k) #Makes 'foo' an instancemethod k.foo() Jay P. -- http://mail.python.org/mailman/listinfo/python-list
Re: Papers on Dynamic Languages
On Mar 4, 2006, at 3:00 PM, Paul Boddie wrote: > > I'd have a look at the following Python-related papers: > > Michael Salib's Starkiller paper (and presentation): > http://www.python.org/pycon/dc2004/papers/1/ > > Mark Dufour's ShedSkin paper: > http://kascade.org/optimizing_python.pdf > > John Aycock's "Aggressive Type Inference" paper: > http://www.python.org/workshops/2000-01/proceedings/papers/aycock/ > aycock.html > > I also provided a fair number of references in the following thread: > > http://groups.google.co.uk/group/comp.lang.python/browse_thread/ > thread/cc98317bdf96efda > > I hope this is of use! > > Paul All that looks fantastic, and I'd forgotten there was a paper on Shedskin. Thanks a bunch, Jay P. -- http://mail.python.org/mailman/listinfo/python-list
Papers on Dynamic Languages
For one of my grad school classes, I'm writing a paper on "Dynamic Languages". What a wonderfully vague topic (but at least it'll let me write about Python). Anyway, I want to talk about things like typing disciplines (weak, strong, etc.),"class vs. prototype, JIT technologies in dynamic languages, interactive interpreters, etc. I've got Brett Cannon's thesis already, but I'm looking for references to some more papers on these topics. A Python basis for any of the papers would be preferred, but anything will do. Any suggestions? This is my first real research paper in grad school, so I'm starting out from scratch here. Jay P. -- http://mail.python.org/mailman/listinfo/python-list
Re: AdaptionFailure: How to use Interfaces with PyProtocols ?
On Feb 10, 2006, at 4:21 AM, Nebur wrote: > Hi, > I tried to understand the docs of Peak's PyProtocols, and failed. > I use PyProtocols v0.93 final. I fetched the ...tar.gz file for Linux > and installed it using the setup.py. > Here's my Hello-World-like example, that defines a Duck, which > "implements" the given Interface: > > > from protocols import Interface,adapt,advise > > class Quackable(Interface): >def quack(loudness): >""" print how loud to quack """ > class Duck: >advise(instancesProvide=[Quackable,]) >def quack(self, loudness): >print "quack! %s loud"%(loudness) > > if __name__ == "__main__": >d = adapt(Duck, Quackable) # this line raises the failure >d.quack(3) > You've *almost* got it. The adaption adapts *instances* of a class. Your setup is correct, but change your __main__ to: if __name__ == "__main__": d = Duck() adapted_d = adapt(d, Quackable) adapted_d.quack(3) or more concisely: if __name__ == "__main__": d = adapt(Duck(), Quackable) d.quack(3) Of course, it's kind of a pointless example, because you're adapting something that declares to be Quackable to a Quackable object, but I'm sure you know that :) Most of my own work with PyProtocols would not involve objects that just had 'instancesProvide', but would also have 'asAdapterFor' (at least, I think it's 'asAdapterFor', it's been a few months since I've touched my PyProtocols related code) Jay P. -- http://mail.python.org/mailman/listinfo/python-list
Twisted book opinions?
I was hoping to get some c.l.p. opinions on O'Reilly's new Twisted book. I'm coming at Twisted as someone who's been programming mainly in Python for almost 6 years now, but who's never done any Twisted development. I've used some of its prepackaged libraries before (and did some custom tweaks to TwistedSNMP), but I don't really know much at all about Twisted fundamentals. The few reviews on Amazon seem to imply that it's more of a cookbook. Would I be better off, for now, trying to get what I can from the twistedmatrix.com docs, and then move to the book when I'm comfortable with the basics? Or would the fact that I'm already pretty strong in Python be enough that I could start with the book? I considered posting this to the Twisted list instead, but thought I'd try somewhere a little more impartial :) Thanks in advance, JayP. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python vs C for a mail server
Randall Parker wrote: > Alex Martelli wrote: > >> The "but without declaration it can't be self-documenting" issue is a >> red herring. Reading, e.g.: >> >> int zappolop(int frep) { ... >> >> gives me no _useful_ "self-documenting" information about the role and >> meaning of frep, or zappolop's result. The code's author must >> obviously >> add a little comment here to clarify -- and in that little comment, >> adding the information about type, if at all relevant, is an obvious >> task. > > Yes, one can use such simple types that the types do not tell you that > much. They do tell you something though. The arg and return types are > not list structures for example. They aren't floats either. > > However, C/C++ at least provide a way to make types tell you far more. > For example, one could declare enum types: > > typedef enum MyArgType > { >// a bunch of enum const names here > } MyArgType; > typedef enum MyResultType >// another bunch of enum const names > } MyResultType; > > Then your example above becomes > > MyResultType zappolop(MyArgType frep) { ... > > and that's a lot more insightful. > Except once a type starts to get a little bit complicated, you're not going to be able to "keep it in your head", and you'll have to go back to the C++ definition of the type anyway when you want to know what it does. Much like you'd have to go back to the Python source of a class. (This is of course assuming that you put a comment in your Python code saying that a certain kind of class is expected. I find it's obvious about 80% of the time what's expected). > I return objects in Python and in C++. In C++ I can see what their > types are right on the m method signature. In Python I've got to write > a comment on the line above it. If I change what type I return and > forget to change the comment then the code isn't correctly documented > anymore. I've done recently and found out with a runtime error while > testing the Python. In C++ if I'd changed the return type the compiler > would have told me if I didn't use it that way somewhere else. Yeah, but you can't *just* change the type you're returning, and expect to be done. You'll also have to change the source code of the function to actually return something of that type. And you'll have to change all the places where the function is called, to make sure that the caller is storing the result in an appropriately typed variable. Often times in Python, the thing you're returning is useful because of duck-typing, not because it's actually of a particular class. Maybe in some piece of code, you're returning a file() object, and all the callers really care about is the ability to do a read() and write() on that object. Maybe later in the lifecycle of your program, you no longer store things in the filesystem directly, but in a database. You'd have to change your function such that it no longer returns a file() object, but some object that instead is connecting to a database. But you still only need to call read() and write() on it. Well guess what: The *only* code you'll have to change is inside the function returning the object, none of the callers would have to change. That's completely different from C++, where you'll have to change not only the return type and the function, but you'll also have to change every single calling function to be aware of this new db object. And not just how the callers store the returned object, but also how they use that object. That's a LOT of work, for really no gain. And that's the ENTIRE point (as I see it) of the Python typing system. Usually you know what you want to do, and you shouldn't have to fight to do it. If you haven't heard the term "duck typing" before, Wikipedia has a decent page on it, and a simple Google search will return many more results. > There are a lot of stages at which to reduce the chance of bugs. While > coding an editor can give you more help with code completion if you > have more static typing. At compile time the compiler can tell you > about errors if it knows the types you are using. > Yeah, but it can only tell you about type errors, and those aren't really critical. They're the kind of error that takes a second to fix when found. Yet *how much* time is often spent fighting with a compiler to make it accept the types you actually want to use? > I use PyChecker and PyLint and while they are incredibly helpful (and > I'm grateful to their authors just as I am to Python's developers) they > do not tell me as much as Borland's C++ compiler does. I get more > runtime errors with my Python code than with my C++ code. > That's to be expected, but the time needed to get to a runtime error is so much less in Python (especially if you've got good unit/integration tests). I recommend you bring IPython into your development tools, so you can easily import small parts of your code and hand test them in isolation. > Still, I find Python useful and better
Re: Language Semantics: @ symbol??
Enigma Curry wrote: > > > Sorry, for the noob question, but I haven't been able to find > documentation on this matter. > > I've been looking for documentation that describes what the @function() > syntax is all about. > > I've seen this on a few pages, for instance: > > http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/307871 > > and also in TurboGears: > > http://www.turbogears.com/docs/wiki20/page5.html > > The general format appears to be: > > @somefunction() > def a_newfunction(): > > > What does it mean and what does it do? > > Thanks! It's called a decorator, new in Python 2.4: http://python.org/doc/2.4.2/whatsnew/node6.html Not sure where it lives in the official documentation right now though... Maybe someone else can provide that. Jay P. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python vs C for a mail server
> Paul Boddie wrote: > >> I don't think I've ever seen anyone advocating calling a function like >> getattr(obj "foo" + "bar")(). > >> From Lib/compiler/visitor.py: > > meth = getattr(self.visitor, 'visit' + className, 0) > > Later on: > > meth(node, *args) > > Of course, you can drop the "visit" prefix and make the mechanism more > predictable, but a search on the standard library for getattr produces > a lot of evidence against your assertion. > I don't think you understood my assertion, then. The example that the original poster gave was using getattr() with a simple string ("foo" + "bar") for the 'name' argument, and without a default return value (as opposed to throwing a variable in there, or a string plus a variable like you did, or a default retval). I even said you can do some "very powerful things" with getattr, by which I meant something exactly like you did. What did you think I meant by that? Jay P. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python vs C for a mail server
> Indeed, especially Eckels article shed some light about testing as an > alternative to static typing. I still can't quite understand why you > can't > do both. Clearly unit tests should be part of any software, not only > Python software. > You can do both, but why? *Especially* in a language like C++, where thanks to pointers and casting, there really isn't any type safety anyway. How much time in your C/C++ code is spent casting and trying to trick the compiler into doing something that it thinks you shouldn't be doing? > Test failures, however, don't tell you anything about the current > usage of > your program - just about the inteded usage at the point where the test > was writte. Clearly you can't test _anything_? And clearly you can > never > be sure that all you collegues did so as well? This not only about type > safety, but simply name safety. > How does type safety tell you anything about the current usage of your program? All static types can tell you is what the input/output types should be, and in no way show any semantic relationship between actual output and the input. Tests can do that. And unit tests *might* not tell about the current usage, but integration tests certainly do. > What do you do when you want to no if a certain method or function is > actually used from somewhere, say "foobar", it a language which allows > (and even encourages) that it could be called by: > > getattr(obj, "foo" + "bar")() > > ? > > There is no systematic way to find this call. > > In C++, just commend out the definition and the compiler will tell you. > I don't think I've ever seen anyone advocating calling a function like getattr(obj "foo" + "bar")(). You can do some very powerful things with getattr, thanks to Python's dynamic nature, but I don't think anyone is recommending calling a function like that. > That's true. If the programmer wants to obfuscate his intention, I'm > sure > neither Python nor C++ can stop him. The question is how much more > work is > to write comprehensible code in one language or the other. I'm a bit > afraid about Python on that matter. > And is that fear based simply on "feeling", or on actual experience. Because in all of my own industry experience, it's been MUCH easier to jump into someone else's Python code than someone else's C++ code (and at my last job, I had to do a lot of both). I find Python to be much more self-documenting, because there's not so much scaffolding all over the place obfuscating the true intention of the code. > Python provides ways to easy literal documentation. But I'd really > like to > have a way of indicating what I'm talking about in a way that's > ensured to > be in-sync with the code. Tests are not where the code is. I have > difficulties remembering the type of a lot of symbols, and looking at > testing code to fresh up usage is more difficult that just jumping to > the > definition (which the development envirnment is likely to be able to). > You need to look at doctest: http://docs.python.org/lib/module-doctest.html With doctest, tests are EXACTLY where the code is. I've used doctest with incredibly successful results, in industry. >> [smart pointers and GC] > > As you say, smart pointers are not full-blown garbage collection, > which is > usually what you want, isn't it? I my (admittedly short) life as a > professional developer I have not yet come accross a situation where > reference counting was not sufficient to model the memory management. > Reference counting by itself is not necessarily sufficient (because of circular references). That's why even Python, with its reference counting based system, has additional capabilities for finding circular references. >> At Google, we collectively have rather a lot of experience in these >> issues, since we use three general-purpose languages: Python, Java, >> C++. > > I have no doubt that goolge know what they're doing, and if you're > working > there then you're likely to know what you're talking about. > I believe that Alex's official job title at Google is "Uber Technical Lead". I'm sure I'll quickly be corrected if that's wrong, but trust me (and everyone else who's spent significant time reading c.l.p.) that Alex Martelli knows what he's talking about. A lot of your arguments are the very typical arguments that people levy against Python, when they're first around it. And that's fine, most people go through that. They are taught programming in C++ or Java, and that that *that* is the way you're supposed to program. Then they see that Python does things in different ways, and automatically assume that Python is doing it wrong. All I can say is that if you spend time with Python (and more importantly, read quality, well-established Python code that's already out there), you'll see and understand the Python way of doing things. Jay P. -- http://mail.python.org/mailman/listinfo/python-list
Re: Guido at Google
On Dec 22, 2005, at 2:20 PM, Greg Stein wrote: > Guido would acknowledge a query, but never announce it. That's not his > style. > > This should have a positive impact on Python. His job description has a > *very* significant portion of his time dedicated specifically to > working on Python. (much more than his previous "one day a week" jobs > have given him) > > Cheers, > -g > Do you actually mean "working on Python", or did you mean "working WITH Python"? Jay P -- http://mail.python.org/mailman/listinfo/python-list
Re: subset permutations
On Dec 8, 2005, at 5:29 PM, [EMAIL PROTECTED] wrote: > > Hello all, > > I'm a beginner with programming. Trying to teach myself with that > excellent rat book. Unfortunately I just can't seem to figure out a > simple problem that has come up at my work (biology lab): > let's say I have a list > ['A','C','D','E','F','G','H','I','K','L','M','N','P','Q','R','S','T','V > ','W','Y'] . I need to generate permutations of this list. I have > found plenty of code on the web to generate ALL possible permutations. > What I need are all possible 4-letter permutations. I cannot seem to > figure out how to modify Mike Davie's permutation generator > http://www.bigbold.com/snippets/posts/show/753 to acheive this. Any > help would be appreciated. > Well, it depends what you're asking for (an example would have helped). For instance, say you have the list ['A','B',C','D'], and you want all possible TWO letter permutations. Do you want the result to be: AB, AC, AD, BC, BD, CD Or, do you want AB,BA,AC,CA,AD,DA,BC,CB,BD,DB,CD,DB ? Here's a rough solution that gives you either. The first argument is the list of letters, the second is how many letters you want in the permutation, and the third is "choose", which defaults to True, meaning it gives my first output. Set it to False for the second output: def perm(l,n, choose=True): if n > 0: s = set(l) for char in l: s.remove(char) for combo in perm(s, n-1): final = str(char) + str(combo) yield final if not choose: yield "".join(reversed(final)) else: yield "" This code assumes you're using Python 2.4 (if you're using 2.3, then you'll have to change the line s = set(l) to s = sets.Set(l), and do an 'import sets' at the top of the module). Also, this function returns an iterable, not a list, so if you want a straight list out of it, pass it to list(), ie. list( perm( ['A','C','D','E','F','G','H','I','K','L','M','N','P','Q','R','S','T','V' ,'W','Y'], 4) ) or, if you want my second output case: list ( perm( ['A','C','D','E','F','G','H','I','K','L','M','N','P','Q','R','S','T','V' ,'W','Y'], 4, choose=False)) Please note that I put this together in about five minutes, so don't blame me if it horribly dies and causes your computer to blow up. Anyone have any better solutions? Jay P. -- http://mail.python.org/mailman/listinfo/python-list
reddit.com rewritten in Python
reddit.com, which was formerly implemented in LISP (and somewhat famous for that fact) has just *relaunched* and is now written in Python! They haven't given their reasons yet, but I'll certainly be interested to see why. Paul Graham's reaction should also be interesting, he said reddit.com had replaced, for him, the process of checking any other news site. On a side note, they are saying (in the faq) that they have done the site in a framework called web.py, which I've personally not heard of. But with the plethora of Python frameworks, I supposed it's pretty easy to miss one or two, here or there. Jay P. -- http://mail.python.org/mailman/listinfo/python-list
Re: [ x for x in xrange(10) when p(x) ]
On Nov 9, 2005, at 7:30 PM, Alex Martelli wrote: > No way -- the itertools module is and remains a PRECIOUS resource. If > you want an iterator rather than a list, itertools.ifilter is quite > appropriate here. Or if you're on 2.4 and want an iterator: (x for x in xrange(10) if p(x)) Jay P. -- http://mail.python.org/mailman/listinfo/python-list
Re: can i tar and untar using python
On Nov 7, 2005, at 3:00 AM, sumi wrote: > can i tar and untar using python The standard library does it again: http://docs.python.org/lib/module-tarfile.html Jay P. -- http://mail.python.org/mailman/listinfo/python-list
Re: python gc performance in large apps
You may also want to look at the following Summer of Code project: http://pysizer.8325.org/ Their SVN repo is at http://codespeak.net/svn/user/nick8325/sizer/ I haven't had a chance to try it yet, but it might be exactly what you need. The project is described as "PySizer is a library for measuring memory use of Python code" Jay P. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python-list Digest, Vol 25, Issue 522
On Oct 28, 2005, at 9:20 AM, pywrote: From what I have seen Python does not come with an snmp module built in, can anyone suggest some other SNMP module (preferably one you have used/experienced)..I have googled and seen yapsnmp and pysnmp (which seem to be the two most active SNMP modules). Without a doubt, use pysnmp. The newest version is being paid for by the Python Software Foundation, and it includes support for SNMPv3. The documentation might be lacking though. yapsnmp, IIRC, is just a wrapper around the net-snmp libraries, while pysnmp is a pure Python implementation. yapsnmp does come with a MIB parser though, not sure if pysnmp has one working yet. Jay P. -- http://mail.python.org/mailman/listinfo/python-list
Re: How best to reference parameters.
On Oct 25, 2005, at 3:10 PM, David wrote: > > It looks like I am going to have to bite the bullet and use properties. > The following should do what I want. > > class test: > > def __init__(self): > self.__HB = 0 > self.__VPG = 0 > ... FYI, for property to work, your class has to be newstyle, ie. inheriting from object: class test(object): ... stuff... The thing is, if you use property() with an old style class, no error will be raised, so you have to be careful. Jay P. -- http://mail.python.org/mailman/listinfo/python-list
Re: setuptools, ez_setup over http proxy
> I've recently configured my network such that I use squid as a http > proxy. I'd now like to be able to use setuptools and ez_setup via this > proxy. Is this possible? If so, how do I do it? It should work if you sent the environment variable (in your shell) 'http_proxy' to the address of your proxy. Python's urllib will then be able to pull it, and everything should magically work. Something like: export http_proxy='http://some_squid_server' > "If you are behind an NTLM-based firewall that prevents Python > programs from accessing the net directly, you may wish to first install > and use the APS proxy server, which lets you get past such firewalls in > the same way that your web browser(s) do. As far as I know, this is only a problem if you're using a particular proxy server from Microsoft. > ps. I'm not sure that this is the right forum for this question. If it > isn't feel free to point me to the right place. The distutils list would be the more appropriate place for setuptools questions in general. Jay P. -- http://mail.python.org/mailman/listinfo/python-list
Re: Parametric Polymorphism
I haven't been following this whole thread, but it sounds like you want something like PJE's RuleDispatch package. There's a Developer Works article about it, put up very recently: http://www-128.ibm.com/developerworks/linux/library/l-cppeak2/?ca=dgr- lnxw02aScalingPEAK PJE (IIRC) based it off of LISP's system of multi-method dispatch. Instructions for installing can be found here: http://peak.telecommunity.com/ I've used this package quite a bit in the past, and have been very happy with it. It allows for dynamic type AND value based function dispatch. Jay P. -- http://mail.python.org/mailman/listinfo/python-list
Re: what would you like to see in a 2nd edition Nutshell?
Jumping in a bit late here, but I'd personally REALLY like to see something about PEP 246, and more specifically, what Phillip Eby has done about it with the PyProtocols package. Especially with all this static typing talk going on these days, a good set of documentation (with lots of useful examples) on interfaces and adaption would probably be eye-opening to a lot of people. It took a lot of experimenting to figure out the PyProtocols package, and to just get my head around the usefulness/point of PEP 246, but now that I have, wow, I don't know how I lived without it. Jay P. smime.p7s Description: S/MIME cryptographic signature -- http://mail.python.org/mailman/listinfo/python-list