Thread-safe way to prevent decorator from being nested
I'm writing a decorator that I never want to be nested. Following from the answer on my StackOverflow question (http://stackoverflow.com/a/16905779/106244), I've adapted it to the following. Can anyone spot any issues with this? It'll be run in a multi-threaded environment serving Django requests and also be a part of Celery tasks. import threading from contextlib import contextmanager from functools import wraps thread_safe_globals = threading.local() @contextmanager def flag(): thread_safe_globals._within_special_context = True try: yield finally: thread_safe_globals._within_special_context = False def within_special_wrapper(): try: return thread_safe_globals._within_special_context except AttributeError: return False def my_special_wrapper(f): @wraps(f) def internal(*args, **kwargs): if not within_special_wrapper(): with flag(): f(*args, **kwargs) else: raise Exception("No nested calls!") return internal @my_special_wrapper def foo(): print(within_special_wrapper()) bar() print('Success!') @my_special_wrapper def bar(): pass foo() -- http://mail.python.org/mailman/listinfo/python-list
Re: Python path and append
If you want to read an entire file, append a space and asterisk and write it to another file, this is the code you need: infile = open('win.txt', 'r') text = f.read() infile.close() text += " *" outfile = open('outfile.txt', 'w') outfile.write(text) outfile.close() If, on the other hand, you wish to read a file and append a space and asterisk TO THE END OF EVERY LINE, you require the following code: infile = open('win.txt', 'r') lines = infile.readlines() infile.close() outfile = open('outfile.txt', 'w') for line in lines: line = line.strip() + " *\n" outfile.write(line) outfile.close() Hope that helps! BigBadMick bigbadmick2...@hotmail.com -- https://mail.python.org/mailman/listinfo/python-list
Re: [Python-Dev] [WARNING] Some users who downloaded the Python 3.5.8 .xz tarball got the wrong version
On 31/10/2019 00:17, Larry Hastings wrote: > > > Due to awkward CDN caching, some users who downloaded the source code > tarballs of Python 3.5.8 got a preliminary version instead of the > final version. As best as we can tell, this only affects the .xz > release; there are no known instances of users downloading an > incorrect version of the .tgz file. > > If you downloaded "Python-3.5.8.tar.xz" during the first twelve hours > of its release, you might be affected. It's easy to determine this > for yourself. The file size (15,382,140 bytes) and MD5 checksum > (4464517ed6044bca4fc78ea9ed086c36) published on the release page have > always matched the correct version. Also, the GPG signature file will > only report a "Good signature" for the correct .xz file (using "gpg > --verify"). > > What's the difference between the two? The only difference is that > the final version also merges a fix for Python issue tracker #38243: > > https://bugs.python.org/issue38243 > > The fix adds a call to "html.escape" at a judicious spot, line 896 in > Lib/xmlrpc/server.py. The only other changes are one new test, to > ensure this new code is working, and an entry in the NEWS file. You > can see the complete list of changes here: > > https://github.com/python/cpython/pull/16516/files > > What should you do? It's up to you. > > * If you and your users aren't using the XMLRPC library built in to > Python, you don't need to worry about which version of 3.5.8 you > downloaded. > * If you downloaded the .tgz tarball or the Git repo, you already > have the correct version. > * If you downloaded the xz file and want to make sure you have the > fix, check the MD5 sum, and if it's wrong download a fresh copy > (and make sure that one matches the known good MD5 sum!). > > To smooth over this whole sordid mess, I plan to make a 3.5.9 release > in the next day or so. It'll be identical to the 3.5.8 release; its > only purpose is to ensure that all users have the same updated source > code, including the fix for #38243. > > > Sorry for the mess, everybody, > a) "Congratulations" on the 3.5.8 release b) excellent solution - to up the release number! c) Thanks!! > > //arry/ > > > ___ > Python-Dev mailing list -- python-...@python.org > To unsubscribe send an email to python-dev-le...@python.org > https://mail.python.org/mailman3/lists/python-dev.python.org/ > Message archived at > https://mail.python.org/archives/list/python-...@python.org/message/OYNQS2BZYABXACBRHBHV4RCEPQU5R6EP/ > Code of Conduct: http://python.org/psf/codeofconduct/ signature.asc Description: OpenPGP digital signature -- https://mail.python.org/mailman/listinfo/python-list
Re: [Python-Dev] PEP 350: Codetags
At 03:35 PM 9/26/2005 -0700, Micah Elliott wrote: > Please read/comment/vote. This circulated as a pre-PEP proposal >submitted to c.l.py on August 10, but has changed quite a bit since >then. I'm reposting this since it is now "Open (under consideration)" >at <http://www.python.org/peps/pep-0350.html>. -1 Personally I do use code tags in my code, but not from this standardised set, nor would I wish to. I tend to use: TODO SMELL STINK STENCH VOMIT ... depending on context. Sometimes they might relate to third party libraries which the code is a workaround for, and hence can't really be fixed beyond the SMELL stage. STENCH and VOMIT I tend to use as my priority mechanism for TODOs. Generally only used if the code can't be fixed or deleted when the decision to tag the code as STENCH or VOMIT. The last 3 are only very, very rarely used. (I'll also only tend to tag my own code that way since they can be taken the wrong way by people :) An example of how I use SMELL (a long lived comment on the implementation that is wrong, but exists for a reason): # # SMELL : Periodically check if this is still needed or not. # # OVERLAY_FUDGE_OFFSET_FACTOR is the result of experimentally # trying to get SDL_Overlay/pygame.Overlay to work with Xorg/fbdev # based displays on linux. If the overlay is precisely the right # size and shape for the data, it can't be displayed right. # The value must be even, and preferably small. Odd values # result in the picture being sheared/slanted. # # This problem rears itself when the following version numbers are aligned: #SDL : 1.2.8 #pygame : Anything up to/including 1.7.1prerelease #xorg : 6.8.2 #Linux (for fbdev) : 2.6.11.4 It sits there as a clear warning/comment for the next developer. This kind of comment doesn't really map to the contents of the PEP in any sensible way, and yet uses the SMELL code tag as a clear warning. Having a standard set of tags /within/ a project is good. Recommending a single set across all projects seems un-enforceable, and hence loses the stated benefits of uniformity. (Kinda the python equivalent of the C-style-language flame wars of the "right" place to place braces { } in your code) If the intent is to have an aim for a standard set of tags within the /standard library/, that seems a more reasonable goal, but have a big set of tags is a problem there, since no one will ever really remember a big list of random tags (or is that just me?). I also think the fields idea is a big mistake. As a code standard for a *particular* project it looks fine, but not for all. Michael. -- http://mail.python.org/mailman/listinfo/python-list
Re: Where to find python c-sources
John J. Lee wrote: > "Tor Erik Sønvisen" <[EMAIL PROTECTED]> writes: >> "Erik Max Francis" <[EMAIL PROTECTED]> wrote in message >> > Tor Erik S�nvisen wrote: >> >> I need to browse the socket-module source-code. I believe it's >> >> contained in the file socketmodule.c, but I can't locate this file... >> >> Where should I look? >> > The source tarball, available on python.org. Are people really too >> > lazy to do elementary research on Google? >> Thanks for the answers... And yes, I have searched google! ... > Does google vary in its results across the globe? Aside from Paul Boddie's comment to the effect of "yes", there is a very important thing that people forget - *no everyone is as good at using a search engine as others*. People are not simply as good at finding the same information using the same tools as others. You liken the problem to a library. If you understand how a library is laid out, you can find the information alot quicker. If however you're looking in a library for a book on "how to create those odd things for computers" and you've been told it involves "python" you're as likely to end up in the fiction section as you are zoology. If you can't figure out the right search terms you need, google can be useless. (That said when that happens to me, I tend to either use kartoo.com or ask a friend) The search terms might be obvious to you, but it simply means your google-fu is strong, and the strong should help the weak. (or not attack them at least...) Michael. -- http://mail.python.org/mailman/listinfo/python-list
Re: Nufox : Xul + Python
Lars Heuer wrote: >> Oops: > >> http://artyprog.noip.org:8080 > > > Again Oops: :)) > > http://artyprog.no-ip.org:8080 Looks intriguing, but the examples on the site won't work for me. I suspect they won't for anyone else either, because the code in the webpages appears to try and contact a server in a private address space, specifically 192.168.0.40. Which is a pity, because it looks interesting :-) Regards, Michael. -- http://mail.python.org/mailman/listinfo/python-list
Re: A Moronicity of Guido van Rossum
Xah Lee wrote: > as i have hinted > ( http://xahlee.org/perl-python/list_comprehension.html ), the > so-called List Comprehension is just a irregular syntax to facilitate > generating lists. The name is a terrible jargon, and the means is also > quite fucked up. The proper name should be something like > ListGenerator, and the proper means should be the plain function. List comprehensions get their name (AFAICT) very clearly from set comprehensions in mathematics. As a result anyone who has ever seen a set comprehension in maths goes "oooh, I see". They're not the same, but IMO they're close enough to warrant that name. > i'm running a project that will code Table in Perl and Python and Java. > You can read about the spec and source code here: > http://xahlee.org/tree/Table.html > (note: the Python version there isn't complete) I just took a look at your python version. I'd agree it's incomplete. Indeed it doesn't implement what you say it does. You seem to have re-invented "apply" since you simply (badly) pass a set of arguments provided by the user to a function provided by the user. The description of the code you are pointing at bears absolutely no resemblance whatsoever to the functionality you describe. And you criticise the way other people name & describe their code, when you can't show the skills you criticise in others? I know naming and documentation are not easy skills, and if people take a *civil* tone in suggested improvements, criticism (and suggestions) can be helpful. However, I'd suggest /finishing/ your glass house /before/ you start throwing stones, or else you'll never be able to smash it up the neighbourhood properly. Michael. -- http://mail.python.org/mailman/listinfo/python-list
Re: A Moronicity of Guido van Rossum
Fredrik Lundh wrote: ... > fwiw, they've also been around for ages: > > http://foldoc.doc.ic.ac.uk/foldoc/foldoc.cgi?list+comprehension > > (the name goes back to the early eighties, the construct is older than > that) Ahh... Fair enough. I hadn't come across it as a programming construct until I hit Python. I'd seen the (presumable) precursor of set comprehension it in maths & formal methods before though. To help Xah along, this shows a //more nearly// correct version of his table function (his doesn't do anything like the spec). It works correctly for tables of the form: table("i", ("i",3)) table("i,j", ("i",3), ("j",3)) table("math.cos(i+j)", ("i",3), ("j",-3,3)) It doesn't produce quite the right structure when you have more than 2 iteration rules). Problems with this implementation: * mkrange() is not passed along the existing context. This means tables of the form: table("i,j", ("i",3), ("j",1,"i")) Won't work. This would require changing the mkRange function such that it's passed the most currently built environment. This is a relatively trivial change, which I'll leave for Xah. * The structure is incorrect for more than 2 iteration rules. I think I'm missing a simple/obvious trick in my mkTable._extend_table function. I'm not really fussed though. (It's clearly something dumb :) * It's not really clear which list nests which. It's possible the lists are nested the wrong way round. I'm fairly certain that the evalTable code will work fine when the mkTable code creates the right structure. I'll leave that to Xah to fix. It's somewhat further along than his original attempt though. (actually matches his spec for 1 or 2 iteration rules). def mkRange(listspec): if len(listspec)==2: return xrange(1,listspec[1]+1) elif len(listspec)==3: return xrange(listspec[1],listspec[2]+1) return [] def mkTable(ignore, *listspecs): def _extend_table(listspecs, result): if len(listspecs) == 0: return result else: listspec = listspecs[-1] listspecs = listspecs[:-1] r2 = [] for R_ in result: for R in R_: # SMELLY inner_result = [] for i in mkRange(listspec): inner_env2 = dict(R[1]) inner_env2[listspec[0]] = i inner_result.append( (ignore, inner_env2) ) r2.append(inner_result) result = _extend_table(listspecs, r2) return result return _extend_table(listspecs, [[(ignore,dict(globals()))]]) # SMELLY def evalTable(table): if len(table) ==0: return table else: result = [] for evallist in table: inner_result = [] for eval_args in evallist: try: r = eval(*eval_args) inner_result.append(r) except TypeError: inner_result.append(evalTable(eval_args)) result.append(inner_result) return result def table(ignore, *listspecs): abstract_table = mkTable(ignore, *listspecs) return evalTable(abstract_table) Example: >>> import math >>> table("math.cos(i+j)", ("i",3), ("j",-3,3)) [[-0.41614683654714241, 0.54030230586813977, 1.0], [0.54030230586813977, 1.0, 0.54030230586813977], [1.0, 0.54030230586813977, -0.41614683654714241], [0.54030230586813977, -0.41614683654714241, -0.98999249660044542], [-0.41614683654714241, -0.98999249660044542, -0.65364362086361194], [-0.98999249660044542, -0.65364362086361194, 0.28366218546322625], [-0.65364362086361194, 0.28366218546322625, 0.96017028665036597]] Regards, Michael. -- http://mail.python.org/mailman/listinfo/python-list
Re: "no variable or argument declarations are necessary."
James A. Donald wrote: > On Sun, 02 Oct 2005 17:11:13 -0400, Jean-François Doyon > James A. Donald: >> > Surely that means that if I misspell a variable name, my program will >> > mysteriously fail to work with no error message. >> No, the error message will be pretty clear actually :) > Now why, I wonder, does this loop never end :-) > egold = 0 > while egold < 10: >ego1d = egold+1 I know (hope! :-) that's a tongue-in-cheek question, however the answer as to why that's not a problem is more to do with development habits rather than language enforcement. (yes with bad habits that can and will happen) Much python development is test-driven. Either formally using testing frameworks (I'm partial to unittest, but others like other ones), or informally using a combination of iterative development and the interactive shell. Or a mix of the two. With a formal test framework you would have noticed the bug above almost instantly - because your test would never finish (Which would presumably count as a failure for the test that exercises that code). Whilst that might seem odd, what you're actually doing with type declarations is saying "if names other than these are used, a bug exists" and "certain operations on these names are valid". (as well as a bunch of stuff that may or may not relate to memory allocation etc) With test driven development you are specifically testing the functionality you want to exist *does* exist. TDD also provides a few tricks that can help you get around writers block, and also catch bugs like above easily and more importantly early. Bruce Eckel (author of a fair few interesting C++ & Java books :-) has a couple of interesting essays on this topic which I think also take this idea a lot further than is probably suitable for here: * Strong Typing vs. Strong Testing: http://www.mindview.net/WebLog/log-0025 * How to Argue about Typing http://www.mindview.net/WebLog/log-0052 For what it's worth, if you've not come across test driven development before then I'd highly recommend Kent Beck's "Test Driven Development: By Example". You'll either love it or hate it. IMO, it's invaluable though! I suppose though the difference between static types based testing and test driven development is that static types only really help you find bugs (in terms of aiding development), whereas TDD actually helps you write your code. (Hopefully with less bugs!) Best Regards, Michael. -- http://mail.python.org/mailman/listinfo/python-list
Re: [Info] PEP 308 accepted - new conditional expressions
Rocco Moretti wrote: > That is, what would happen with the following constructs: > > A if B else C if D else F > A if B if C else D else F The correct answer should be the person who wrote it would get told off for writing code that a person reading would have no idea what the code was doing (without understanding the precedence). Whilst it's good to have clear understandable, well defined rules for these things, that's no excuse for having unreadable code that other people can't read and understand without having to remember obscure rules. Personally, I'd hope that any code-linting tools would flag such expressions as potentially bad because they're not clear. (Whereas bracketed expressions instantly help here). Best Regards, Michael -- http://mail.python.org/mailman/listinfo/python-list
Re: [Info] PEP 308 accepted - new conditional expressions
Paul Rubin wrote: > I'm not > sure what Guido saw in the "A if C else B" syntax but it's not a big deal. Maybe Guido's done some perl programming on the side? When I've been doing perl programming I've quite liked the if (...); construct, however, on occasion it's been desirable to have an else there. By having an else there it means you don't need the ?: syntax and can just have one syntax. On the flipside, people with different mother tongues often have a different way of using language. (eg lots of non-native english speakers speaking better english than those with english as their mother tongue :-) And sometimes that different way can be better ? Direct translation of german grammatical form for example - resulting in something looking like yoda speak... Personally I like this form :) Regards, Michael. -- http://mail.python.org/mailman/listinfo/python-list
Re: Where to find python c-sources
John J. Lee wrote: > [Tor Erik S�nvisen] >> socketmodule.c, but I can't locate this file... Where should I look? > > [John, finding 'socketmodule.c' responds well to "I'm Feeling Lucky"] >> Does google vary in its results across the globe? > > [Michael] >> The search terms might be obvious to you, but it simply means your >> google-fu is strong, and the strong should help the weak. (or not attack >> them at least...) > > You believe that Tor is dumb enough not to think of searching for > "socketmodule.c" when, um, searching for socketmodule.c? He said he had tried google - OK, not in the first post but early in this thread - I don't equate that with being dumb - just dumb luck :-) Message-ID: <[EMAIL PROTECTED]> After all Peter Hansen suggested the search terms "python socketmodule.c" rather than just "socketmodule.c" Message-ID: <[EMAIL PROTECTED]> To you the obvious search term was "socketmodule.c" which to me simply means you're more aligned with Google than Tor :-) These things happen :-) Regards, Michael. -- http://mail.python.org/mailman/listinfo/python-list
Re: python getopt functionality
M.N.A.Smadi wrote: > I have a perl script that I need to port to python. The script takes > input from the command line. Is there a standard way of processing > command line arguments based on the -flag preceeding the argument? Yes. # pydoc getopt Help on module getopt: NAME getopt - Parser for command line options. FILE /usr/lib/python2.4/getopt.py MODULE DOCS http://www.python.org/doc/current/lib/module-getopt.html [ ... snip ... ] Michael -- http://mail.python.org/mailman/listinfo/python-list
Re: [Info] PEP 308 accepted - new conditional expressions
Christophe wrote: > Michael a écrit : >> Rocco Moretti wrote: >> >> >>>That is, what would happen with the following constructs: >>> >>>A if B else C if D else F >>>A if B if C else D else F >> >> >> The correct answer should be the person who wrote it would get told off >> for writing code that a person reading would have no idea what the code >> was doing (without understanding the precedence). >> >> Whilst it's good to have clear understandable, well defined rules for >> these things, that's no excuse for having unreadable code that other >> people can't read and understand without having to remember obscure >> rules. >> >> Personally, I'd hope that any code-linting tools would flag such >> expressions as potentially bad because they're not clear. (Whereas >> bracketed expressions instantly help here). > > Actually, you don't need to know operator precedence here because the > notation isn't ambiguous in those 2 examples. Of course, it takes some > time to understand the thing but it has more to do with the excessive > amount of "logic" in one line than with the syntax. I noted that. However it *does* slow people down which increases the chances of misconception (which is why I'd be in favour of bracketing these expressions). Michael. -- http://mail.python.org/mailman/listinfo/python-list
Re: When someone from Britain speaks, Americans hear a "British accent"...
Steve Holden wrote: ... > Or is "the green tomato" also unacceptable? > Of course it is. We all know* it should be "the green fried tomato", or "the killer tomato". :-) (is it me, or is the subject line for this thread silly? After all, what accent would you expect from someone in the UK? However, that said, the concept of a *single* British accent is a silly as the idea. Sillier even than the suggestion that the two lines below are British vs American: > American: Minnesota is behind 7-0. The Vikings are behind 7-0. > British: Minnesota are behind 7-0. The Vikings are behind 7-0. Or even these lines: > American: The war department has decided to cancel the program. > British: The war department have decided to cancel the program. A better one might be: > British: "They installed tunnelling for the petrol pipes made of grey > coloured aluminium." > American: "They installed tunneling for the gas pipes made of gray > colored aluminum." (I think :-) I do my best with grammar, but can fail spectactularly, more often than I'd like :) Bad grammar flies at the same speed as the pedants who decide that the way that other people talk is wrong. If the majority of people use a language one way, and a small number of people say "you're wrong", who's right? Is it the people who speak the language in a shared way that they all understand, or the people who are setting rules based on how people *used* to speak and *used* to define words? (NB, I *did* say majority above ;-) Does /human/ language _require_ backwards compatibility? ;-) Michael. -- http://mail.python.org/mailman/listinfo/python-list
Re: When someone from Britain speaks, Americans hear a "British accent"...
Terry Hancock wrote: > > Well, yeah, although the correct pronunciation is apparently > "te-tra-HEE-dra-GON". > As opposed to a "te-tra-SHE-dra-GON" ? ;-) Michael. -- http://mail.python.org/mailman/listinfo/python-list
Re: Continuous system simulation in Python
Nicolas Pernetty wrote: > I'm looking for any work/paper/ressource about continuous system > simulation using Python or any similar object oriented languages (or > even UML theory !). > > I'm aware of SimPy for discrete event simulation, but I haven't found > any work about continuous system. > I would like to develop a generic continous system simulator, and so > would be eager to join any open source effort on the subject. There was going to be a talk on continuous system simulation (*) at EuroPython this year, but for some reason it didn't appear to happen (It was one of the talks I was hoping to see). I've no real idea of the content of the talk or the work involved though for obvious reasons. Maybe that can provide a lead? (*) http://www.python-in-business.org/ep2005/alisttrack.chtml?track=646 "Implementing Continuous Time Simulation Systems in Python, Paul J Nolan" Another lead //might// actually be to ask on the pygame list (MAYBE). It's not directly the same thing, but essentially lots of games are in many respects a form of continuous systems simulation, and someone on that list might be able to point you in a good direction. One book I bought a while back (looked interesting) which might be relevant for you is O'Reilly's "physics for game developers", and and another is "AI for games programmer"(*) since they both touch on these areas. If you have a Safari account already that could be an easy way of checking to see whether the book covers the sorts of answers you're after. (They don't use python as their example language, but that's a minor problem really IMO) (*) This is less obviously relevant, but deals with things deciding to move themselves around continuous spaces and visualised at indeterminate frame or display rates. Best Regards, Michael. -- http://mail.python.org/mailman/listinfo/python-list
Re: What is executed when in a generator
generators. For those purposes, I wrote a tutorial for our project which is based very much around having lots of generators. The tutorial is made up of a set of learning exercises: * http://kamaelia.sourceforge.net/MiniAxon/ We've tested this tutorial on a couple of novices at work (they learn python one week and get this tutorial the next), and they've found it relatively simple. The first hadn't done any programming before, except a small amount of VB - he was a pre-university trainee. The second was a university vacation trainee who'd done 2 years, but had no experience of the ideas in the tutorial or python before joining us. It's specifically targeted at novices and might be of use when seeing the possibilities of what you can actually use generators for. { Incidentally if anyone reading this, not just yourself, decides to give it a go, I'd really appreciate hearing back from you what you thought of it, easy/difficult, clear/unclear, what level of experience you have, etc. People don't have to really, I'm posting this because I've noticed a couple of people have tried this so far as a means to trying to understand generators :-) } Best Regards and hope the above is useful, Michael. -- http://mail.python.org/mailman/listinfo/python-list
Capturing audio from a microphone on Mac OS X
Hi, This might seem a dumb question, but every search I've done so far has come up blank - with the possible exception of Schtoom. I've seen Schtoom that does manage to capture audio under Mac OS X, but it's unclear whether the audio tools it uses can be used independently of Schtoom. So my question this: does anyone know of a library or simple approach to capturing audio (any quality :) from a microphone on Mac OS X ? Specifically something that works with Mac OS X 10.3, with python 2.3 . Ideally, something like the approach that pyalsaaudio [*] takes would be great, but at this stage I suspect that the number of available examples out there is small, so I'm more than happy to take something and adapt it. [*] http://sourceforge.net/projects/pyalsaaudio/ I haven't searched for something similar under windows yet, I'm hoping that's less problematic (famous last works). Thanks in advance to anyone who replies, Michael. -- http://mail.python.org/mailman/listinfo/python-list
Re: High Order Messages in Python
[EMAIL PROTECTED] wrote: > I'm reading about "high order messages" in Ruby by Nat Pryce, and > thinking if it could be util and if so, if it could be done in Python. Nice sunday afternoon exercise. Yes, you can do this in python. This is based on a relatively naive translation of the ruby version: class HigherOrderMessage(object): def __init__(self,handler): self.handler = handler class Do(HigherOrderMessage): def __HOM__(self, methname, *args): "implement ruby's method_missing idea" try: for e in self.handler: meth = getattr(e, methname) meth(*args) except TypeError: # Handle non-iterator, could be nicer if self.handler is not None: meth = getattr(self.handler, methname) meth(*args) def __getattribute__(self, methname): try: return super(Do,self).__getattribute__(methname) except AttributeError: def myHom(*args): return self.__HOM__(methname, *args) return myHom class Where(HigherOrderMessage): def __HOM__(self, methname, *args): "implement ruby's method_missing idea" try: r = List() for e in self.handler: meth = getattr(e, methname) if meth(*args): r.append(e) return r except TypeError: r = List() if self.handler is not None: meth = getattr(self.handler, methname) if meth(*args): r.append(self.handler) return r # def __getattribute__(self, methname): "Probably belongs in the baseclass" try: return super(Where,self).__getattribute__(methname) except AttributeError: def myHom(*args): return self.__HOM__(methname, *args) return myHom class enumerable(object): def do(self):return Do(self) def where(self): return Where(self) class List(enumerable,list): "List using enumerable as a mixin" class Claimant(enumerable): def __init__(self, name, age, gender): self.name = name self.age = age self.gender = gender self.benefits = 0 def retired(self): return (self.gender == "male" and self.age >= 65) or \ (self.gender == "female" and self.age >= 60) def receive_benefit(self,amount): self.benefits = self.benefits + amount def __str__(self): return "%s,%s age: %s benefits: %s" % ( self.name, self.gender, str(self.age), str(self.benefits) ) # # Create an enumerable list capable of responding to # claimants = List([ # Just a list which is enumerable as well Claimant("tom", 32, "male"), Claimant("dick", 64, "male"), Claimant("harry", 128, "male"), Claimant("Ivanova", 32, "female"), Claimant("Kochansky", 64, "female"), Claimant("Sung", 128, "female"), ]) # First the normal python way I prefer this) for claimant in claimants: if claimant.retired(): claimant.receive_benefit(50) # Display the results for claimant in claimants: print str(claimant) print # The more direct translation of : # claimants.select {|e| e.retired?}.each {|e| e.receive_benefit 50} [ e.receive_benefit(50) for e in claimants if e.retired() ] # Display the results for claimant in claimants: print str(claimant) print # The single claimant version of the higher order message approach # re-uses the last claimant from above. This one unconditionally # grants benefits. # claimant.do().receive_benefit(50) print claimant print # Iterating over a bunch of Claimants with the higher order message # approach. This conditionally updates the claimaints claimants.where().retired().do().receive_benefit(50) # display results for claimant in claimants: print str(claimant) I'm not convinced I'd actually *use* this approach(*), but it does show that you can certainly take this approach in python. It's certainly interesting though. (*) Largely because there's a bunch of magic happening as far as the user (next programmer to edit the file) is concerned. After all you can't find the definition of retried in "where" or "claimant"; you can't find the definition of "receive_benefit" in "do", "retired", "where" or "claimants". I'm also pretty sure there's other things you'd want to do more than the above generally speaking if you wanted to handle inheritance/etc nicely. (That does make me wonder as well if you'd need to do more in ruby as well) However, this certainly isn't a case of "ruby can do this, and python can't", because clearly python CAN do it :-) Regards, Michael -- http://mail.python.org/mailman/listinfo/python-list
Re: MSH (shell)
[EMAIL PROTECTED] wrote: > This can be of little interest of some people here, but I think it can > be interesting enough to justify a post. > > An article on the Microsoft Command Shell: > http://arstechnica.com/guides/other/msh.ars > (Its syntax is rather Python-like, but Py syntax seems better to me, > even taking into account that their purposes are a little different). > > Other info: > http://en.wikipedia.org/wiki/MSH_(shell) > > Something vaguely similar made with Python: > http://geophile.com/osh/index.html Kamaelia's Axon Shell[*] is also very similar. An Axon scheduler runs as a background thread to an IPython interpretor, allowing you to "launch" Kamaelia components interactively, including both pipelines and Graphlines of components. [*] http://kamaelia.sourceforge.net/AxonShell.html But then Kamaelia IS designed as a pipeline/graphline (1d,2d,n-d) system from the ground up, so having a shell for it was a logical step :-) ... I suppose the difference between python and MSH is you don't need a separate language, maybe a module or three, but you don't need to modify the language :) Michael. -- http://mail.python.org/mailman/listinfo/python-list
Re: Expanding Python as a macro language
[EMAIL PROTECTED] wrote: > But then I changed idea... Also if it is already one year that I try > to find a solution in Linux (mainly with Python or DCOP and KDE), This doesn't express the question you have anywhere clearly enough. Linux can run perfectly happily without any form of windowing environment. Many people run using Gnome. Many others using KDE. And the number of people who use *neither* of those but some window manager is still high. Do you want to replay against qt applications? KDE? wx? GTK? Or send raw X11 events? The answers to these questions aren't linux specific either... Michael. -- http://mail.python.org/mailman/listinfo/python-list
Re: Addressing the last element of a list
[EMAIL PROTECTED] wrote: > Is there a way to make reference to the last element of a list, to use > as a shorthand: Yes. It's not wise to use, since this is brittle and will fail hard for you, but since you're interested, this is how you /could/ do it: (largely) # First of all create a mechanism for creating and handling symbolic # references class ref(object): def __init__(self, val): self._val = val def set_val(self, val): exec("global %s\n%s = %s" % (self._val, self._val, repr(val))) def get_val(self): return eval(self._val) val = property(get_val, set_val) Now we can play with this. Note the word *PLAY* . >>> lst = ["1","2","3","4"] >>> y = ref("lst[-1]") >>> y.val '4' >>> y.val = 10 >>> y.val 10 >>> lst ['1', '2', '3', 10] >>> i = 1 >>> z = ref("i") >>> z.val = 10 >>> i 10 Once again, note the word *play* - don't use this in __anything__ :-) Python binds values to names. As a result what you're after when asking for ref := &lst[len(lst) - 1] And then for ref to actually refer to that last item, you have to realise that you're asking for an indirection on the name "lst[-1]". Short of doing silly things with eval and exec (which you really don't want to do), you can't do this. However, it's python, so of course you *can*, but just because you *can* do something doesn't mean that you *should*. You'll note that in order to make the reference to the plain mutable value (i) work the set_val had to mark the value as global, which isn't quite right. (You might be able to trick it into using the "right" scope using lambda somehow, maybe) Once again, don't use it! (hopefully of interest though) Regards, Michael. -- http://kamaelia.sourceforge.net/ -- http://mail.python.org/mailman/listinfo/python-list
Re: [OT] Map of email origins to Python list
Paul McGuire wrote: > "Claire McLister" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] > We've been working with Google Maps, and have created a web service to > map origins of emails to a group. As a trial, we've developed a map of > emails to this group at: > > http://www.zeesource.net/maps/map.do?group=668 > > This represents emails sent to the group since October 27. > > Would like to hear what you think of it. > -- > > > Another sleepless camera pointed at the fishbowl that is my online life. > > I guess it's a great way to find where there might be Python jobs to be > found, or at least kindred souls (or dissident Python posters in countries > where Internet activity is closely monitored...) > > To me, it's either cool in a creepy sort of way, or creepy in a cool sort > of way. As long as it gets my location WRONG, I'm happy. :-| Michael. -- http://mail.python.org/mailman/listinfo/python-list
Re: JAPH
Szabolcs Nagy wrote: > charset, modulo, japh = " .JPacehknorstuy", 17, "" > s = > 69859911049503515105680510599913390885187193231927247909305172858127641629 > for n in xrange(2,): > if s%n==0: > japh += charset[(n - 1) % modulo] > s /= n > if s==1: > break > print japh More pythonic: print "Just another Python hacker." :-) Michael. -- http://mail.python.org/mailman/listinfo/python-list
Re: Proposal for adding symbols within Python
Ben Finney wrote: ... > I've yet to see a convincing argument against simply assigning values > to names, then using those names. I don't like any syntax I've seen so far, but I can understand the problem. If you have a name, you can redefine a name, therefore the value a name refers to is mutable. As a result if you have 2 symbols represented by names and values, you may have two symbols with different names but the same value. Hence the two "symbols" are no longer unique) Conversely consider "NAME" to be a symbol. I can't modify "NAME". It always means the same as "NAME" and "NAME", but is never the same as "FRED". What's tricky is I can't have namespaceOne."NAME" [1] and namespaceTwo."NAME" as different "NAME"s even though logically there's no reason I couldn't treat "NAME" differently inside each. [1] Supposing for a moment that I could have a string as a name in a namespace. (Rather than a string used as a key in that namespace) However it might be useful to note that these two values (or symbols) are actually different, even if you remove their namespaces. To me, the use of a symbol implies a desire for a constant, and then to only use that constant rather than the value. In certain situations it's the fact that constant A is not the same as constant B that's important (eg modelling state machines). Often you can use strings for that sort of thing, but unfortunately even python's strings can't be used as symbols that are always the same thing in all ways. For example, we can force the id of identical strings to be different: >>> s = "h"*1 >>> x = "h"*1 >>> id(s), id(x) (135049832, 135059864) As a result I can see that *IF* you really want this kind of symbol, rather than the various other kinds people have discussed in the thread, that some special syntax (like u'hello' for unicode 'hello') could be useful. However, I'd be more interested in some real world usecases where this would be beneficial, and then seeing what sort of syntax would be nice/useful (Especially since I can think of some uses where it would be nice). On the original syntax proposal, I'm firmly in the -1 camp - to me having done lots of perl in the past $foo looks very firmly like a mutable, rather than an immutable. The reason I'm more interested in seeing usecases, is because I'd rather see where the existing approaches people use/define symbols has caused the OP problems to the extent he feels the language needs to change to fix these real world problems. Michael. -- http://mail.python.org/mailman/listinfo/python-list
Python/Qt Problem
Hi, I am experiencing something very weird with PyQt. I have created several windows based on QWidget using Designer. I can easily hide and show these with the hide and show methods. However I have just created a new window with Designer and the show method works in my main script but not inside the button action method (not using proper temrinology, sorry) of another window. Why is that? This works fine for the other windows. Have I explained my problem properly? Thanks for any advice, Michael -- http://mail.python.org/mailman/listinfo/python-list
property () for Java Programmers ?
Hi there, I am somewhat confused by the following : class C(object): def getx(self): return self.__x def setx(self, value): self.__x = "extended" + value def delx(self): del self.__x x = property(getx, setx, delx, "I'm the 'x' property.") So far so good :-) But what to do with this now >>> c = C >>> c >>> dir (c) ['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__', 'delx', 'getx', 'setx', 'x'] >>> c.x >>> ?? What can I do with this "property object" now. Confused greetings Michael -- http://mail.python.org/mailman/listinfo/python-list
dynamically inserting function into an object
Hi, below is a snipplet that could be seen as a part of a spreadsheet with getter and setter properties and a way how to dynamically insert function to be used when setting the value of a "cell" instance import new import inspect class Cell (object): def __init__ (self, initialvalue = 0): self._func = None self.__value = initialvalue def setvalue (self, newvalue): if self._func: self.__value = self._recalculate (newvalue) else: self.__value = newvalue def getvalue (self): return self.__value def _recalculate (self, value): ret_value = self._func (value) return ret_value def delvalue (self): del self.__value value = property(getvalue, setvalue, delvalue, "I'm the 'value' property.") def curry(self, func, *args): self._func = new.function(func.func_code, func.func_globals, argdefs=args) func = property(curry, "I'm the 'func' property.") def func (value, firstcell, secondcell): return value + firstcell.value + secondcell.value cell0 = Cell (10) cell1 = Cell (20) curriedcell = Cell (100) print "uncurried initial %d " % (curriedcell.value) curriedcell.value = 60 print "uncurried set %d " % (curriedcell.value) curriedcell.curry (func, cell0, cell1) curriedcell.value = 62 print "curried set %d " % (curriedcell.value) Is there a better way to do this or am I totally on the wrong way ? Regards Michael -- http://mail.python.org/mailman/listinfo/python-list
Dynamic properties
Hello again, I have a dictionary with the following content : 'LZ': {'type': 'N', 'bytes': '8'}, 'LVI000': {'type': 'N', 'bytes': '10'} This could be seen as a interface description to deal with an external program that needs a 18 Byte communication area. 8 and 18 Bytes have to be interpreted as a "Number" String representation. eg like '018000200' means LZ = 180 and LVI000 = 200 I am now thinking about doing the following class a def some_crap () ... for key in dict.keys (): setattr (self, key, value) so that pgm = a () pgm.LZ = 300 would cause the rekonstruktion of the byte field. ===>> Properties My first try is : fget = lambda self: mygetattr(self, attrname) fset = lambda self, value: mysetattr (self, attrname, value) fdel = lambda self: mydelattr(self, attrname) # fget, fset, fdel are used to reconstruct the byte field setattr (self, key, property (fget, fset, fdel)) :-) This inserts me pgm.LZ and pgm.LVI000 but when trying to access print pgm.LZ it gives me What am I doing wrong here Regards Michael -- http://mail.python.org/mailman/listinfo/python-list
Re: Finding user's home dir
My own, less than perfect, way of finding the users home directory is like this: def personal_directory ( default = None ): pdir = None if sys.platform.startswith ( 'win' ): import _winreg reg = _winreg.ConnectRegistry ( None, _winreg.HKEY_CURRENT_USER ) pkey = _winreg.OpenKey ( reg, r"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" ) pdir = _winreg.QueryValueEx ( pkey, 'Personal' )[0] _winreg.CloseKey ( pkey ) _winreg.CloseKey ( reg ) else: pdir = os.getenv ( 'HOME' ) if not pdir: pdir = default return pdir -- Michael <[EMAIL PROTECTED]> http://kavlon.org -- http://mail.python.org/mailman/listinfo/python-list
character sets? unicode?
I'm trying to import text from email I've received, run some regular expressions on it, and save the text into a database. I'm trying to figure out how to handle the issue of character sets. I've had some problems with my regular expressions on email that has interesting character sets. Korean text seems to be filled with a lot of '=3D=21' type of stuff. This doesn't look like unicode (or am I wrong?) so does anyone know how I should handle it? Do I need to do anything special when passing text with non-ascii characters to re, MySQLdb, or any other libraries? Is it better to save the text as-is in my db and save the character set type too or should I try to convert all text to some default format like UTF-8? Any advice? Thanks. -- Michael <[EMAIL PROTECTED]> http://kavlon.org -- http://mail.python.org/mailman/listinfo/python-list
Re: global variables
Probably naming it something other than 'globals' would be a good idea -- otherwise you'll hide the builtin globals() function. But I agree that the attributes of a class instance (as you suggest) or the attributes of a module (as Steve Holden suggests) is probably the right way to go. I like to use 'runtime' or 'runtime_options' to store the results of command-line options. I wasn't sure about this way of getting Python to handle global variables when I first tried it but after a little experience with it I think it works out pretty well. One thing I did learn though is that it's best to keep these modules simple. Don't make them import or define classes or functions if you can avoid it. It's easy to get into a mess of recursive imports if you start doing that. Just a good newbie tip. *** if runtime.verbose: print 'Something happened.' *** -- Michael <[EMAIL PROTECTED]> http://kavlon.org -- http://mail.python.org/mailman/listinfo/python-list
Command Line arguments
I have a question about Windows based python (2.4 and later). For example, if I make a script called test.py like so: import sys print sys.argv then run it: python test.py this is a test I see a list with ['test.py', 'this', 'is', 'a', 'test'] All is good! BUT... If i make .py extensions be run as exes (by setting the .py extension to be executable with PATHEXT setting in environment variables, the Python program will run, but NO arguments are passed! For example, after setting .py extension to be executable, i get this: test this is a test I get ['test.py]. NO arguments are passed. NOTE: This can NOT be blamed on Windows in my mind because Python2.2 and earlier works FINE. Any one have an idea? Thank you! Michael -- http://mail.python.org/mailman/listinfo/python-list
Re: Command Line arguments
On Thu, 25 Aug 2005 00:46:41 -0700, Tim Roberts wrote: > michael <[EMAIL PROTECTED]> wrote: > >>I have a question about Windows based python (2.4 and later). >> >>For example, if I make a script called test.py like so: >> >>import sys >>print sys.argv >> >>then run it: >> >>python test.py this is a test >> >>I see a list with >> >>['test.py', 'this', 'is', 'a', 'test'] >> >> >>All is good! >> >>BUT... >> >>If i make .py extensions be run as exes (by setting the .py extension to >>be executable with PATHEXT setting in environment variables, the Python >>program will run, but NO arguments are passed! >> >>For example, after setting .py extension to be executable, i get this: >> >>test this is a test >> >>I get ['test.py]. NO arguments are passed. >> >>NOTE: This can NOT be blamed on Windows in my mind because Python2.2 and >>earlier works FINE. > > It is a configuration problem. Bring up a Command Shell and run the assoc > and ftype commands like this: > > C:\Tmp>assoc .py > .py=Python.File > > C:\Tmp>ftype Python.File > Python.File="C:\Apps\Python24\python.exe" "%1" %* > > C:\Tmp> > > The KEY part of that is the %* at the end of the Python.File defintion. > That tells the system to insert the rest of the command line parameters at > that point. If you have ONLY the "%1", the command will run but no > parameters will be forwarded. If so, you can fix this by typing: > > C:\Tmp>ftype Python.File="C:\Apps\Python24\python.exe" "%1" %* > > Substituting your own path, of course. Tim, I can confirm you were right! Thank you very much. This will get us through the issue. I wonder why this was needed? One way or the other, you taught me something and I thank you. Michael Christopher -- http://mail.python.org/mailman/listinfo/python-list
Re: Command Line arguments
On Thu, 25 Aug 2005 00:46:41 -0700, Tim Roberts wrote: > michael <[EMAIL PROTECTED]> wrote: > >>I have a question about Windows based python (2.4 and later). >> >>For example, if I make a script called test.py like so: >> >>import sys >>print sys.argv >> >>then run it: >> >>python test.py this is a test >> >>I see a list with >> >>['test.py', 'this', 'is', 'a', 'test'] >> >> >>All is good! >> >>BUT... >> >>If i make .py extensions be run as exes (by setting the .py extension to >>be executable with PATHEXT setting in environment variables, the Python >>program will run, but NO arguments are passed! >> >>For example, after setting .py extension to be executable, i get this: >> >>test this is a test >> >>I get ['test.py]. NO arguments are passed. >> >>NOTE: This can NOT be blamed on Windows in my mind because Python2.2 and >>earlier works FINE. > > It is a configuration problem. Bring up a Command Shell and run the assoc > and ftype commands like this: > > C:\Tmp>assoc .py > .py=Python.File > > C:\Tmp>ftype Python.File > Python.File="C:\Apps\Python24\python.exe" "%1" %* > > C:\Tmp> > > The KEY part of that is the %* at the end of the Python.File defintion. > That tells the system to insert the rest of the command line parameters at > that point. If you have ONLY the "%1", the command will run but no > parameters will be forwarded. If so, you can fix this by typing: > > C:\Tmp>ftype Python.File="C:\Apps\Python24\python.exe" "%1" %* > > Substituting your own path, of course. SOLVED! Thank you. I wonder why this was needed for 2.4 and not 2.2? I don't think it was lingering things from old installs because it happened on a persons computer that had never had any python installed before 2.4. Anyway, THANKS! Michael -- http://mail.python.org/mailman/listinfo/python-list
Re: Doubt C and Python
On Tue, 23 Aug 2005 06:15:03 +0100, praba kar wrote: > Dear All, >I want to know the link between c and python. >Some people with C background use Python instead > of programming in C.why? > > > > regards > Prabahar > > > > > > Just my $.02 I am a long time c/c++ programmer (by profession). I fell in love with python about 2 years ago. I use python for many things now, and I always have said, "When it is too slow, I will write it in c++". I have not hit that point yet. For some reasons that are hard to explain, even though python "should" be slower and maybe even is sometimes, it never is an issue. One reason is that python has so much better data structures built in, and THEY are written in C, I end up with faster code. For example, if I am doing a bunch of string compares in C, I would use a dictionary in python. Python ends up faster because I can get to a better algorithm FASTER. The other reason is that so many times, a hardware I/O device is really the limiting factor (such as a hard disc, or a serial/network connection, or waiting for the user). I have found that GUI programs written in python/wxpython to be every bit as fast as pure C++. I guess you could say that because the LIBRARIES of python are in C, and because you are never very far from a library call, you end up running C code a large percentage of the time, even when you are writing in Python. My advice is to just TRY python and resolve the "slow" speed if you ever hit it. I never have and I write a lot of code, even hardcore math and image processing (See PIL - python Imaging Library). Michael -- http://mail.python.org/mailman/listinfo/python-list
Re: Command Line arguments
On Thu, 25 Aug 2005 11:39:48 -0700, Trent Mick wrote: > [michael wrote] >> SOLVED! Thank you. >> >> I wonder why this was needed for 2.4 and not 2.2? I don't think it was >> lingering things from old installs because it happened on a persons >> computer that had never had any python installed before 2.4. > > It might be due to a bug in the Python 2.4 installer not setting the > proper file associations. What installer package did you use? > > Trent I used the python2.4.MSI from python.org site (dated 3-6-05). I think this was the first time they went to MSI verses an exe based installer. it says Python 2.4 (#60 November 30th, 2004) when I start it. Michael -- http://mail.python.org/mailman/listinfo/python-list
Memory Based File Objects
Hi there, I am using the Python Image package to do some Image conversion. I have a "pgm" Image and this must be converted to a "1" Bits / Sample Tiff Image. There for I am using Image.save ("lala.tiff"). *hm* A Tiff Image opt. consists of several pages so I am thinking about using the pytiff package as well. The existing code is like that parser = ImageFile.Parser () parser.feed (imagecontent) image = parser.close ()# Now we have the Image image = image.convert ("1")# reduce to 1 Bits per sample image.save ("lala.tif")# Store as Tiff After everey File has been processed I am using the "tiffcp" command to append all the files to a single tiff-file containing each image as a separate page. eg. "tiffcp fileone.tiff filetwo.tiff filethree.tiff allinone.tiff" Performance is an issue here and therefore I am evaluating the pytiff package. Cause this package can reduce to "1" Bits per Sample and append to tifffiles. So the code would be like this image.save (fileobject, format="tif") tifffile = pytiff.TiffFileReader (fileobject) # do some stuff allinone = pytiff.TiffFileWriter ("allinone.tiff") allinone.append (fifffile) Of course I dont wanna write the "in between" File to disk with image.save () I do wann to to use a memory file object. The Image Documentation says that file object just has to implement seek, tell and write. Does anybody have an example how to do that or maybe a snipplet for an "In Memory" File Object with an unknown size. Kind Regards Michael -- http://mail.python.org/mailman/listinfo/python-list
Re: SysLogHandler is drivin me nuts PEBCAC
Vinay Sajip <[EMAIL PROTECTED]> wrote in message news:<[EMAIL PROTECTED]>... > michael wrote: > > Yep it was incomplete heres the complete config as it has to be > > > [config file snipped] > > The complete file looks OK. Thx. with this file it is working. The syslogd configuration under AIX was buggy. regards Michael -- http://mail.python.org/mailman/listinfo/python-list
Converting String to Class
Hello, I have a script that is called by the shell this way : mqtrigger.py "TMC2TEST.QUEUE LV1871.MQPROCESS" So argv[1] is the string "TMC2TEST.QUEUE LV1871.MQPROCESS" I would like to construct a class with that string that contains the attributes -StrucId -VersionId -QName -ProcessName I am thinking about using the following info opts = [['StrucId', CMQC.MQTMC_STRUC_ID, '4s'], ['Version', CMQC.MQTMC_VERSION_2, '4s'], ['QName', '', '48s'], ## or less :-) ['ProcessName', '', '48s']] ## or less :-) 1. is the attributename 2. is the default 3. is the format in the string So the string could be parsed with the opts Info and the attributes could be inserted into the class. I dont even have a starting point how to do that. Could somebody point my into the right direction Regards Michael -- http://mail.python.org/mailman/listinfo/python-list
I've broken PythonWin2.4 - Dialogs don't pop up!
I have broken PythonWin and I don't know how or why. It worked great until recently. I have installed / removed several apps since it worked last. I could have a virus unknown to my antivirus program, I guess. I'm stumped. -- The Symptoms - Here's what happens. I fire up PythonWin without opening a python file. Everything looks good - the interactive window is at the bottom of the main dialog and I can type python code into it and it seems to work fine. But if I select anything from the menu or toolbar that requires a new dialog box to pop up, either: 1. Nothing happens - it acts like it popped up the dialog, but it's not there. At least I can't see it. 2. I get an error down in the interactive window 3. I get a windows error telling me that the program is now going to crash. 4. PythonWin just disappears off my screen - all the processes seem to die. Note: Sometimes I can't even get a menu item to respond, but that's not very often. - Examples - Example #1. If I select "Edit Python Path" I get the following message in the interactive window: >>> Failed to execute command: from pywin.tools import regedit;regedit.EditRegistry() Traceback (most recent call last): File "C:\Python24\Lib\site-packages\pythonwin\pywin\framework\toolmenu.py", line 103, in HandleToolCommand exec "%s\n" % pyCmd File "", line 1, in ? File "C:\Python24\Lib\site-packages\pythonwin\pywin\tools\regedit.py", line 323, in ? template = RegTemplate() File "C:\Python24\Lib\site-packages\pythonwin\pywin\tools\regedit.py", line 245, in __init__ docview.DocTemplate.__init__(self, win32ui.IDR_PYTHONTYPE, None, SplitterFrame, None) File "C:\Python24\Lib\site-packages\pythonwin\pywin\mfc\docview.py", line 71, in __init__ self._SetupSharedMenu_() File "C:\Python24\Lib\site-packages\pythonwin\pywin\mfc\docview.py", line 74, in _SetupSharedMenu_ sharedMenu = self.GetSharedMenu() win32ui: The template has no menu Example #2. If I try to open a file by selecting "open" from the "file" menu or by hitting the icon on the toolbar, I almost always get the behavior in which it acts like the File Open dialog box was displayed, but it's not. Example #3. If I hit the "?" icon on the toolbar, I get the following error message: File "C:\Python24\Lib\site-packages\pythonwin\pywin\framework\app.py", line 307, in OnHelpAbout win32ui.InitRichEdit() win32ui: AfxInitRichEdit failed win32ui: Error in Command Message handler for command ID 57664, Code 0 - Here's what I know -- My Machine: + I am on WinXP-Pro using Python 2.4: + In the interactive Window this is what is says: PythonWin 2.4 (#60, Feb 9 2005, 19:03:27) [MSC v.1310 32 bit (Intel)] on win32. + The first item in my PATH env variable is: C:\Python24\. + From the PythonWin About box: Python 2.4 pywin32 extensions (build 203). + In my system registry, In HKEY_LOCAL_MACHINE\SOFTWARE\Python, I have one entry: PythonCore and it has two sub-entries: 2.3 and 2.4. Everything looks similar between the two and the registry data for Python2.4 seems fine: InstallPath = c:\Python24\ InstallGroup = ActiveState ActivePython2.4 PythonPath = C:\Python24\Lib;C:\Python24\DLLs;C:\Python24\Lib\lib-tk The only dialog box that always correctly pops up is the About box from the help menu. I have Python 2.3 installed in an adjacent directory to my 2.4 version. I can go down into the Python2.3 directories to PythonWin, which is actually ver 2.3.4, and fire up PythonWin and I don't have any of these problems. After I have a problem with PythonWin2.4 I notice weird UI behavior from my other apps until I kill PythonWin. Any ideas? Thanks, ~Michael. -- http://mail.python.org/mailman/listinfo/python-list
Re: I've broken PythonWin2.4 - Dialogs don't pop up!
Rebooting does not help. I uninstalled and removed everything from c:\python2.4 and then downloaded and installed the latest version. Right after installing it, everything worked fine. But when I rebooted, the problem came back. Sigh... ~Michael. -- http://mail.python.org/mailman/listinfo/python-list
Installation problem
Hello, Please forgive a new user's ignorance. I am trying to install Python 3.5.0 on my laptop (Windows 10). The default installation directory is shown as c:\Users\(my user name)\AppData\Local\Programs\Python\Python35-32. However, if I select Custom Install then the default installation directory is c:\Program Files(x86)\Python3.5. In either case, after the installation is complete I cannot find a Python3.5 directory under Program Files(x86) or the Users\... directory. The python.exe file can be located under c:\OpenOffice. Other python files are found scattered all over my hard drive in locations such as c:\ HamRadio\WSJT, or c:\Program Files(x86)\Cyberlink, or c:\CHIRP, or a variety of other directories, but never in the designated installation directory. Scattered files include: python.exe (in Open Office) python27.dll (in CHIRP) pythoncom27.dll (in CHIRP) python33.dll (in HamRadio\WSJT) python25.dll (in "Koan c:\Program Files(x86)\Cyberlink) I assume these various .dll files are used in the various other programs I have installed but I cannot understand, at the moment, why the main python program does not appear in the designated installation directory. Any ideas or suggestion you may have are most welcome and appreciated. Thanks very much, Mike Wolcott -- https://mail.python.org/mailman/listinfo/python-list
Re: Python on GP2X (Linux Based Handheld Console)
Jérôme Laheurte wrote: > On Thu, 15 Dec 2005 08:43:34 +0000, Michael Sparks wrote: > >> I hadn't seen any announcements regarding this, but there's a little >> device recently released called a GP2X which is a small dual CPU >> (2x200Mhz) device which runs Linux. >> >> Anyway, I thought there might be someone in here interested to hear >> that python AND pygame have both been ported to it already (not by >> me). I've also ported some of our code to it (the bouncing cats demo >> I've shown at a couple of python conferences), and found it really nice >> to work with so far. The cats bounce at an acceptable (to me) >> framerate :-) > > Nice! Another toy to buy! As if I had enough pockets left :) Considering > the threading limitations of Python, I assume it runs on the GP2X as if it > were a single 200MHz CPU... Yep. As an update to the above, I'll probably release a simple subset of Kamaelia to make games run identically on a desktop as on the machine itself. (Hmm... Interesting thought, I can even ramp back the apparent CPU availability transparently to something similar, which would be useful when developing :) > How much RAM does this thing have ? 32Mb - according to free & /proc/meminfo > I'm interested because I ported Python to the Sony PSP, which can run at > 333Mhz and has 32Mo of RAM , and almost no OS overhead. I wondered if > this setup would be enough for pygame to run... > I know of at least one person who's trying to port pygame to PSP-Python... I must admit personally I wouldn't be interested in python on a PSP because you never know when homebrew code on a PSP is going to be locked out... Michael. -- http://mail.python.org/mailman/listinfo/python-list
Re: [EVALUATION] - E04 - Leadership! Google, Guido van Rossum, PSF
Ilias Lazaridis wrote: > [ panic, fear, worry ] What's wrong with just saying "Congratulations!" ? First thing I thought was "ooh, maybe Guido will be able to work on P3K there" - after all that would benefit Google *and* everyone else :-) (Especially if he uses PyPy to experiment and play in ... :) Merry Christmas/Happy New Year :-) Michael. -- http://mail.python.org/mailman/listinfo/python-list
Re: python concurrency proposal
components (like your pardefs) which have been put together for various systems which have varying levels of concurrency which are probably a useful testbed for testing your ideas: * http://kamaelia.sourceforge.net/Components.html Example systems created using this vary from a "everything broadcast" PVR for radio [*], a networked audio mixer matrix, simple games, a presentation tool (probably including soon a live video mixer to go with the video playback), topology viewing tools, a simple "paint" program, etc) * Actually a box for creating a record of transmission, which is slightly different, but the former description is more accessible if slightly inaccurate.) If you're interested in prototyping your ideas in python, you can simulate some of your ideas using decorators. Something that might help you with prototyping your ideas is our tutorial for Kamaelia, which is a "build your own core" system type tutorial. It might also help show that your pardefs are very similar to python's generators. It can be found here: * http://kamaelia.sourceforge.net/MiniAxon/ In many respects, I feel that the API we have still isn't "100% pythonic" as many would call it, but it does try to be unsurprising and consistent. You could say we're aiming for pythonic - though personally I favour easy and unsurprising as more concrete, less abstract goals - even if it's not there yet. (portability of ideas to other languages is important to me, which again is another reason for an API based view rather than syntax). If you're willing to take a look at it, and make suggestions as to how your ideas might fit, (or what you think is dumb :-) I'd welcome it. *Especially* if it simplifies the system (*or* the way it's used). Finally though, as I say above,I'm not trying to discourage you, I like the ideas, and would like to see you expand them more since they interest me, and like you I think this is an area that needs work. I would suggest playing with the ideas though and testing them against systems before writing a PEP though. (real/useful systems rather than contrived ones! -) Best Regards & Happy New Year, Michael. -- [EMAIL PROTECTED], http://kamaelia.sourceforge.net/ British Broadcasting Corporation, Research and Development Kingswood Warren, Surrey KT20 6NP This message (and any attachments) may contain personal views which are not the views of the BBC unless specifically stated. -- http://mail.python.org/mailman/listinfo/python-list
Re: [ANNOUNCE] MyHDL 0.5 released
Jan Decaluwe wrote: > I'm pleased to announce the release of MyHDL 0.5. > > MyHDL is an open-source package for using Python as a hardware > description and verification language. Moreover, it can convert > a design to Verilog. Thus, MyHDL provides a complete path > from Python to silicon. Jan, I'm not sure if you read c.l.p, but if you do... I'm looking at the website and I see that you've now got an example showing translation to verilog - which is really cool. I also saw that someone's done what I view as a complex example - specifically the MU0 example [*] (which is a tutorial I remember from student days!) as a MyHDL simulation. * http://article.gmane.org/gmane.comp.python.myhdl/19/match=mu0 One question I've got, mainly because it strikes me as very intriguing is do you know if the MU0 processor as described is synthesisable or have a feeling as to how much work would be needed for it to be synthesisable? I've been watching your project grow over the past couple of years with great interest though little actual need at the moment, but for me seeing MU0 crop up piques my interest because that shows that MyHDL is getting up to a very interesting level. This probably comes across as a bit random, but it struck me as quite exciting to see :-) Regards, Michael. -- http://mail.python.org/mailman/listinfo/python-list
Re: [ANNOUNCE] MyHDL 0.5 released
Jan Decaluwe wrote: > Michael wrote: ... >>* http://article.gmane.org/gmane.comp.python.myhdl/19/match=mu0 >> >> One question I've got, mainly because it strikes me as very intriguing is >> do you know if the MU0 processor as described is synthesisable or have a >> feeling as to how much work would be needed for it to be synthesisable? > > This is a fairly "old" project (2003). At that time, MyHDL didn't > yet have conversion to Verilog. > > After reviewing the code again, it's clear that it's written in > RTL (register-transfer level) style. This means that the building > blocks are combinatorial, or triggered on clock edges, closely > reflecting an actual implementation. As it is, it's not > convertible to Verilog (see the MyHDL manual for conversion > constraints), but it's close. Thanks that's great to hear, and from something I find very interesting. > To someone with some synthesis experience, it should be fairly > straightforward to make the code synthesizable. I don't expect > that this would make the code more verbose or less clear. I find that even more intersting :-) >> I've been watching your project grow over the past couple of years with >> great interest though little actual need at the moment, but for me seeing >> MU0 crop up piques my interest because that shows that MyHDL is getting >> up to a very interesting level. > > As your interest was apparently triggered by an example, this > tells me that I should put more emphasis on publishing practical > examples, as conversion to Verilog was already introduced some time > ago (beginning of 2004). Practical examples are great, I'd seen that you'd introduced conversion to verilog some time back, but it wasn't clear how much was synthesisable. The example on the website & seeing MU0 description made me really wonder. After all MU0 is used as a teaching example of how a very minimal CPU can be constructed. MU0 itself is not that interesting, but for me the fact MyHDL might be able to synthesise it *is* interesting. After all, synthesising such a beast (essentially) directly from python shows to me a very powerful example which can be built upon. > Note also that by now, there are designers that use MyHDL in real > projects, showing that you really can use it to go from Python to > an FPGA (or ASIC). Moreover, with development tools such > as Xilinx WebPack (now on Linux also) that start from Verilog, > this can be done using a zero-cost development environment. Hmm... Very interesting :-) [different post] > I believe it's meaningful because in my view digital hardware > design can be regarded as just another specialized software > engineering discipline. Of course, things have to be learned, > but it's not more difficult than other application domains. > I should add that this is not the mainstream view of the > hardware design community :-) For what it's worth, I agree. I've had some limited experience with compilation to hardware in the past, specifically to asynchronous hardware, but given you write code that can include loops, conditionals and these can be translated to FPGA descriptions and then run this for me blurs the hardware/software distinction. A specific example that looks like software I'm thinking of is this: http://www.cs.man.ac.uk/fmethods/projects/AHV-PROJECT/node8.html (In particularly it's not really that different from Occam) Maybe I should continue this conversation on the MyHDL list, since I'd be interested in getting started in this in a simple way. (Mainly because my work project Kamaelia is designed, to an extent, with hardware constraints in mind. Implementing some Kamaelia components in MyHDL would be pretty cool. This might well be possible since we also use generators to model concurrency.) Best Regards, Michael. -- http://mail.python.org/mailman/listinfo/python-list
Re: Info on continuations?
vasudevram wrote: > > Hi, > > I am Googling and will do more, found some stuff, but interested to get > viewpoints of list members on: > > Continuations in Python. > > Saw a few URLs which had some info, some of which I understood. But > like I said, personal viewpoints are good to have. Python doesn't really support continuations. Generators (and co-routines in general) have similar properties to continuations, however they're not continuations. Closures are also sometimes considered cousins to continuations, and python's support for those is pretty good IMO. (again however, closures are not continuations). Since it looks like you're also looking for what continuations are, I think the following is the simplest way of explaining them. If you've ever programmed in BASIC, *in a way* if you think of methods/functions as a named GOSUB, then continuation (in a way) are a way of giving a name to a goto (it's more subtle than that though since a continuation os often defined to "remember" state in a similar way to a closure). Like a function you can pass them round as objects. Like a generator/closure they remember the state they were in. *Personally* , I think python NOT supporting full continuations is a GOOD thing, since full continuations, whilst powerful, are also a great source of confusion for people. (That said, I have a usecase I'd find them useful for - I think they'd be useful for plugin architectures - but IMO that doesn't outweigh the risk of code obfuscation :-) One particular usecase that people seem to like continuations for, specifically how they're used in seaside, is actually a subset of functionality that python *can* support (to a large extent). Essentially the idea is to be able to take a web application and make it look linear. CherryFlow allows you for example to write this: @expose @flow def example_flow(): yield view.first_page() if request.args["choice"] == "a": yield view.choice_a_page() else: yield view.choice_b_page() yield view.last_page() (example from: http://tinyurl.com/qzpqu ) This causes the user's browser to show a page where they have a choice of "a" or "b". Depending on what they choose, they then either are presented with choice_a_page or choice_b_page. Finally, no matter which option they chose, you are presented with the last page. Something similar can be done using Kamaelia, though at present only the mechanism exists there, without any sugar (http://tinyurl.com/n3bh7 - specifically websiteSessionExampleComponent). The reason I mention it is to say that it's relatively simple to do using python :-) I have to stress though, this usage of continuations in Seaside, as emulate-able in python is a subset of the full power of continuations, which isn't available in python at present. (Though it's possible hacking greenlets could result in something). (In fact the way seaside uses them as far as I can tell is more to implement co-routine like behaviour than anything else (!)) Anyway, hope that's interesting/useful - looking at your other comments, you're just looking for information and usecases at the moment :-) Regards, Michael. -- http://mail.python.org/mailman/listinfo/python-list
Re: Component framework
Jan Svec wrote: > Hi all, > some time ago I've seen an interesting component framework for Python > but I don't remember the name. I remember only one example. There were > two components: Wheel and Car, Wheel were then inserted four times into > Car and so on. This framework has had lazy instantiation of child > components. > > Does anybody know the name of this framework? PEAK - http://peak.telecommunity.com/Articles/WhatisPEAK.html """PEAK is the "Python Enterprise Application Kit". If you develop "enterprise" applications with Python, or indeed almost any sort of application with Python, PEAK may help you do it faster, easier, on a larger scale, and with fewer defects than ever before. The key is component-based development, on a reliable infrastructure.""" FWIW, I don't use it, but I'm aware of it. I use a component framework I develop for work & fun called Kamaelia - but PEAK is what you're thinking of. BTW, since it might be helpful, from your request I punched the following search terms into google - which links to the tutorial mentioning cars and wheels: * Search terms: wheel car component lazy python :-) Best Regards, Michael. -- Michael Sparks, Kamaelia Project Lead http://kamaelia.sourceforge.net/Home http://yeoldeclue.com/blog -- http://mail.python.org/mailman/listinfo/python-list
Re: Generator-based microthreads and games
t;> ") # block waiting for user self.send(data, "outbox") # send on to next thing pipeline( ConsoleReader(), # Threaded Component ConsoleEchoer(), # Generator Component ).run() Just as an indication of the sorts of things you can do with Kamaelia, we've had some Google Summer of Code students working on a small selection of Kamaelia related projects this summer. Perhaps the most relevant to you might be the 3D work, which makes it relatively simple to create 3D objects and then bounce them round the screen and interact with them. Two of the students have blogs which follow their progress which you can find here, which might also be useful when considering whether generators are a good approach :-) * http://thfsoc.blogspot.com/ * http://rjlsoc.blogspot.com/ Anyway, I've rambled on far too long, and now well off topic for the questions you were asking, so I'll be quiet :-) Have fun, and IMO, the generator approach (using Kamaelia, LGT, or home rolled), is very much worth going down. Best Regards, Michael. -- Michael Sparks, Kamaelia Project Lead http://kamaelia.sourceforge.net/Home http://yeoldeclue.com/blog -- http://mail.python.org/mailman/listinfo/python-list
Re: cloning generator iterators
Bernhard Mulder wrote: > [ attempt to clone/fork a generator ] You can do this, but you can't pickle the results. (If you want pickling, use Stackless - I've not tried pickling generators in stackless because I don't use stackless, but it looks pretty clear you can pickle them there) > Question: What is the best way to implement this clone operation? However you should be able to do what you want using the small extension "statesaver" which you can grab from * http://www.gosubway.org/install/statesaver.c The statesaver allows you to clone generators in the way that you want. FWIW, I can see lots of situations where this would be useful - mainly in the area of dealing with search spaces (After all, this effectively allows you to fork the generator). Michael. -- http://kamaelia.sourceforge.net/Home - Concurrency, Networking, Simplicity -- http://mail.python.org/mailman/listinfo/python-list
Re: Text to MP3 using pyTTS - Non-programmer question
Marc Shapiro wrote: > From what I've seen, pyTTS is Windows only. Is there a package that > runs on linux that provides similar functionality. I have festival and > festlite, but I would prefer a Python solution. From: http://www.cam.org/~nico/cicero/cicero-README.txt """ Cicero TTS: A Small, Fast and Free Text-To-Speech Engine. Copyright 2003-2006 Nicolas Pitre <[EMAIL PROTECTED]> Copyright 2003-2006 Stéphane Doyon <[EMAIL PROTECTED]> Version 0.7, March 2006 Our TTS engine currently speaks French and some resemblance of English, although we hope it can some day be taught to speak good English or other languages. The engine uses context-sensitive rules to produce phonemes from the text. It relies on MBROLA (http://tcts.fpms.ac.be/synthesis/mbrola.html) to generate actual audio output from the phonemes. The TTS engine is implemented using the Python programming language. We've come up with this TTS to try and meet our own needs as blind users. """ """ This is still an early release of our TTS, quality is beta-ish. Installation/integration surely has rough edges still, and pronunciation is constantly improving. The TODO-list is still generous. """ I've not tried this, but I suspect this might be a good place to start (though coaxing it to do english might well be a project in itself, depending on what they mean by "some semblance of English" :-) Regards, Michael. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python component model
Edward Diener No Spam wrote: > Has there ever been, or is there presently anybody, in the Python > developer community who sees the same need and is working toward that > goal of a common component model in Python, blessed and encouraged by > those who maintain the Python language and standard modules themselves ? Someone aiming towards a standard to /replace/ everyone else's? That presupposes a level of arrogance that seems unusual in the python world. (whilst everyone's proud of their own code and they _generally_ respect other people's even if it's not their cup of tea). The WSGI standard could be a form of component model, and has gone through the PEP process so that might match your criterion. As for component models, they do exist. Our component model on the Kamaelia project [1] is one that's heavily designed around the idea of composition and independent execution of components and message passing (message passing maps to events for some sorts of message), [1] http://kamaelia.sourceforge.net/Home I wouldn't think of proposing it as the single standard to rule them all though, for the simple reason every approach has its own strengths. (We do find the approach extremely useful though) If you want a quick example of the core ideas, a tutorial aimed around building a massively simplified core is here: http://kamaelia.sourceforge.net/MiniAxon/ If you want to see a substantial example, you can look here: * http://tinyurl.com/oqjfb - whiteboarding with audio where every client is a server. The entire resulting system is also a component. For something more simplistic: * http://kamaelia.sourceforge.net/Examples/SimplestPresentationTool.html Something halfway in terms of complexity (a PVR for transcoding everything broadcast on digital TV): * http://tinyurl.com/lvygq (OK, you need to add more channels, but you'd need more CPU's too) We also have tools for introspecting a running system, and also a visual composition tool (called Compose) [2] for creating simple systems graphically, and that, as you say, handles a significant chunk of dreariness. Suggestions on improving the model and composition tool are very welcome, code is even more welcome :) [2] Sample system created with the newest version of Compose: http://tinyurl.com/numwk Compose is also a Kamaelia system, and can be found here: http://tinyurl.com/p7z76 (bulk of the wiring up is in the bottom of the file - this is an interesting example because of the use of Pygame and Tk for different parts of the interface where appropriate) However, off the top of my head, you should also look at Zope's component model, Trac's component model, Twisted's model & PEAK, and any proposal to say "this is the solution", needs to be compelling for all of these projects. Note, they do change where there's a benefit - twisted adopted some interesting ideas from Zope for example - however the onus on showing the benefit is on you. (Which if you can do, would be welcome I would expect) One thing that would probably be very useful would be to identify a way the various models these projects use can be made to interact better. The reason I mention //our// model is because we're finding it useful, and has visual composition, introspection and components have a rich amount of meta data associated with them, this may or may not fit your criterion. One of the most "bling" examples we've got right now though (which I mentioned mainly because it does show reuse up quite nicely) is where we play Dirac encoded video back onto a pygame surface and then render that onto a texture that's on a plane spinning in 3D (in realtime): * Code: http://tinyurl.com/oynxv * Screenshot: http://kamaelia.sourceforge.net/t/Dirac3D.png I've uploaded a collection of other screenshots of various kamaelia related things here: * http://kamaelia.sourceforge.net/screenshots/ You may find the following interesting: * http://kamaelia.sourceforge.net/screenshots/Compose.png * http://kamaelia.sourceforge.net/screenshots/KamaeliaOpenGL.png (example of putting components in places they don't realise) * http://kamaelia.sourceforge.net/screenshots/AxonVisualiser.png Regards, Michael. -- Kamaelia Project Lead http://kamaelia.sourceforge.net/Home http://yeoldeclue.com/blog -- http://mail.python.org/mailman/listinfo/python-list
Kamaelia 0.4.0 RELEASED - Faster! More Tools! More Examples! More Docs! ; -)
t based network servers and clients * Presentation tools * A networked audio mixer matrix (think multiple audio sources over network connections mixed and sent on to multiple locations with different mixes) * Look at graph topologies & customise the rules of display & particle types. Mix and match all of the above. These are all real examples you can do today. You can also do a lot of this *visually* using the new PipeBuilder application in Tools. Essentially if the system you want to build involves audio or moving pictures, and you want to be able to make the system network aware, then this should be quick and easy to do using Kamaelia. (If it isn't, then a) it's a bug b) needs improving :-) Oh, and due to things like the visual editor, the use of pygame in a lot of examples, the use of dirac & vorbis, it's a lot of fun too :-) It runs on Linux, Windows, Mac OS X with a subset running on Series 60 phones. (Linux is the primary development system) Requirements * Python 2.3 or higher recommended, though please do report any bugs with 2.2. * Axon (1.5 required) Optional extras: (all available via the Kamaelia download page) * vorbissimple (if you want to use the vorbis decode component/examples) * dirac bindings (again from kamaelia site) if you want to use the dirac encode/decode components & examples). (And dirac of course :-) * python-dvb bindings Axon, vorbissimple and python-dirac are separate parts of the Kamaelia project, and available at the same download location - see below) Platforms = Kamaelia has been used successfully under both Linux, Windows and Mac OS X (panther). A subset of Kamaelia has been successfully tested on Series 60 Nokia mobiles when used with the Axon SERIES 60 branch. Where can I get it? === Sourceforge Download: http://sourceforge.net/project/showfiles.php?group_id=122494&package_id=133714 Web pages are here: http://kamaelia.sourceforge.net/Docs/ http://kamaelia.sourceforge.net/ (includes info on mailing lists) ViewCVS access is available here: http://cvs.sourceforge.net/viewcvs.py/kamaelia/ Tutorial for the core component/concurrency system: * http://kamaelia.sourceforge.net/MiniAxon/ Project Motivations: * http://kamaelia.sourceforge.net/Challenges/ Licensing = Kamaelia is released under the Mozilla tri-license scheme (MPL1.1/GPL2.0/LGPL2.1). See http://kamaelia.sourceforge.net/Licensing.html Best Regards, Michael. -- Michael Sparks, Senior Research Engineer, BBC Research, Technology Group [EMAIL PROTECTED], Kamaelia Project Lead, http://kamaelia.sf.net/ This message (and any attachments) may contain personal views which are not the views of the BBC unless specifically stated. -- http://mail.python.org/mailman/listinfo/python-list
ANN: Axon 1.5.0 RELEASED!
* http://kamaelia.sourceforge.net/Challenges/?tab=8 Licensing = Axon is a part of the Kamaelia project and as a result is released under the Mozilla tri-license scheme (MPL1.1/GPL2.0/LGPL2.1). See http://kamaelia.sourceforge.net/Licensing.html Best Regards, Michael. -- Michael Sparks, Senior Research Engineer, BBC Research, Technology Group [EMAIL PROTECTED], Kamaelia Project Lead, http://kamaelia.sf.net/ This message (and any attachments) may contain personal views which are not the views of the BBC unless specifically stated. -- http://mail.python.org/mailman/listinfo/python-list
Re: Kamaelia 0.4.0 RELEASED - Faster! More Tools! More Examples! More Docs! ; -)
Robin Becker wrote: > Michael wrote: >> I'm extremely pleased to say - Kamaelia 0.4.0 has been released! > Windows users might see problems with the attempt at importing libc.so.6 > in the Axon Scheduler. > > I changed the lines at line 44 of scheduler.py to something like this [ .. patch .. ] Many thanks! Yes, this issue has also been raised on the Kamaelia mailing list in the past couple of days by Stefan Rank (he had a solution based on using win32.Sleep(0,0) which calls SleepEx[1]). I'm planning on an Axon 1.5.1 release shortly to deal with this issue. [1] http://www-128.ibm.com/developerworks/eserver/library/es-MigratingWin32toLinux.html?ca=dgr-lnxw09CPP2LinuxPower#N10364 I've got three options - I can either take the approach you've suggested, the approach suggested on the list (which is to call win32.Sleep(0,0), or actually remove this code from the scheduler completely. I'm actually most tempted by the final option, largely because this code was added in an earlier release as a way of making Axon & the scheduler more system friendly. Since we've now effectively gone from a busy wait loop to a scheduler that /can/ sleep waiting for threads [2] this means that code can be a lot more system friendly by default. [2] For example, rather than calling select with a timeout of zero in the main thread, it's now called with a timeout of 5s in a thread, which wakes up the scheduler when there's something to do. The pygame code does the same trick... That said, there is still whole chunks of code that busy run (such as the physics simulation, so I'm torn. Do I take the code you put forward, the solution put forward on the the list which I can't really check (since I don't have a windows box to test with right now (I will on Monday though)), or yank it out. I suppose the "correct" thing is to put your solution into Axon 1.5.1 - to get rid of the failure, and then to test Stefan's solution locally. My hesitation with regard to Stefan's solution is simply the fact that I don't tend to do a huge amount of coding under windows, and hence have greater concern there as a result. I'll get a release out with that fix either today or tomorrow, and then roll up Stefan's more detailed fix into the 1.5.2 release along with anything else that crops up over the next few days (hopefully nothing, but you never know). > This allows somethings to run eg > > AxonVisualiser.py --navelgaze > > but I'm not sure if the results are really unaffected by not having a > real yielder. The diagram appears, but doesn't seem to settle down. I don't think the AxonVisualiser would be particularly affected by this - any wobbling you'll see there is probably more down to the simple physics model the visualiser uses not having sufficient dampening for the layout you see. (The physics model was apparently originally written by Matt for a lava lamp simulator, but has turned out to be incredibly useful as a simple layout tool :-) A little bit of manual assistance to spread things out can sometimes help it settle down though :) > whoops, forgot to say thanks as I think this is very nice work You're very welcome. If you have any other suggestions/ideas for improvement they'd be very welcome :-) Personally, I think some of the most fun stuff right now being done by new people with it is coming from 2 of our summer of code students. One, Thomas, is working on 3D components (eg putting pygame components into a PyOpenGL display). The other is doing bittorrent related work, but has also written a webserver and IRC bot - both pretty much from scratch (at the protocol level) over the past month. (The other two students are working on encryption ideas for tools for communications - eg encrypted VoIP type things) Thomas's work (3D) is discussed here: http://thfsoc.blogspot.com/ Ryan's code (Bit Torrent, webserver/client, IRC bot) is sitting here: * http://kamaelia.cvs.sourceforge.net/kamaelia/Sketches/RJL/ Thanks for the bug fix/kind words! Regards, Michael. -- http://mail.python.org/mailman/listinfo/python-list
PEP thought experiment: Unix style exec for function/method calls
this sort of thing could be useful in games for modelling complex behaviours) What is less clear about this is that I'm working on the assumption that as well as the language change making "cexe" work, is that this also allows the above set of methods to be treated as if it's one large generator that's split over multiple function definitions. This is conceptually very similar to the idea that cexe would effectively "join" functions together, as alluded to above. This has a number of downsides for the main part of the language, so I wouldn't suggest that these changes actually happen - consider it a thought experiment if you like. (I think the single function/no wrapping of yield IS actually a good thing) However, I feel the above example is quite a compelling example of how a unix style exec for python method calls could be useful, especially when combined with generators. (note this is a thought experiment ;) It also struck me that any sufficiently interesting idea is likely to have already been implemented, though perhaps not looking quite like the above, so I thought I'd ask the questions: * Has anyone tried this sort of thing? * Has anyone tried simply not creating a new stack frame when doing a function call in python? (or perhaps replacing the current one with a new one) * Has anyone else tried modelling the unix system exec function in python? If so what did you find? * Since I can't find anything in the archives, I'm presuming my searching abilities are bust today - can anyone suggest any better search terms or threads to look at? * Am I mad? :) BTW, I'm aware that this has similarities to call with continuation, and that you can use statesaver.c & generators to achieve something vaguely similar to continuations, but I'm more after this specific approach, rather than that general approach. (After all, even ruby notes that their most common use for call/cc is to obfuscate code - often accidentally - and I'm not particularly interested in that :) Whereas the unix style exec is well understood by many people, and when it's appropriate can be extremely useful. My suspicion is that my ideasabove actually maps to a common idiom, but I'm curious to find that commonidiom. I'm fairly certain something like this could be implemented using greenlets, and also fairly certain that Stackless has been down this route in the past, but I'm not able to find something like this exec style call there. (Which is after all more constrained than your usual call with continuation approach) So, sorry for the length of this, but if anyone has any thoughts, I'd be very interested. If they don't, I hope it was interesting :) Regards, Michael. -- http://mail.python.org/mailman/listinfo/python-list
Re: Event notification system - where to start ?
ookbook.html The collaborative whiteboard code we have is heavily event driven, but isn't on the web (it's in the main download Bundle though). It can be looked at view ViewCVS here: http://kamaelia.cvs.sourceforge.net/kamaelia/Code/Python/Kamaelia/Tools/Whiteboard.py?view=markup That forwards drawing events to other whiteboards, whilst filtering out their own events. Each whiteboard implements the same model (and can act as a client or server or both). However, it's a much more complex example than: pipeline( WatchSerialPort(), TCPClient("machine.b", 1500), ).run() Backplane("Events").activate() pipeline( ### INPUT TO BACKPLANE SingleServer(port=1500), EventFilter(), publishTo("Events"), ).activate() def subscriber_protocol(): return subscribeTo("Events") SimpleServer(subscriber_protocol, 1600).run() :-) For what it's worth if you want an actual chat system: Server: ~~~ from Kamaelia.Util.Backplane import * from Kamaelia.Chassis.ConnectedServer import SimpleServer Backplane("chatroom").activate() def listenToRoom(): return subscribeTo("chatroom") def talkToRoom(): return publishTo("chatroom") SimpleServer(listenToRoom, 1600).activate() SimpleServer(talkToRoom, 1601).run() Client: ~~~ import sys from Kamaelia.Util.PipelineComponent import pipeline from Kamaelia.Internet.TCPClient import TCPClient from Kamaelia.Util.Console import ConsoleReader, ConsoleEchoer pipeline( ConsoleReader(">>> "), TCPClient(sys.argv[1], 1601), ).activate() pipeline( TCPClient(sys.argv[1], 1600), ConsoleEchoer(), ).run() That's rather messy (in terms of UI) though - the talk from the chatroom will overtype the text, so a nice client would be to use a pygame text display instead (we've got something called a Ticker which would be useful here): Client: ~~~ import sys from Kamaelia.Util.PipelineComponent import pipeline from Kamaelia.Internet.TCPClient import TCPClient from Kamaelia.UI.Pygame.Ticker import Ticker from Kamaelia.Util.Console import ConsoleReader pipeline( ConsoleReader(">>> "), TCPClient(sys.argv[1], 1601), ).activate() pipeline( TCPClient(sys.argv[1], 1600), Ticker(), ).run() (Unfortunately, no, we don't have a text edit/entry box for pygame yet :-) Anyway, hope this was useful/interesting! Regard, Michael. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python CGI Scripting Documentation
Sybren Stuvel wrote: > Why use CGI when you can use a framework that's so much easier and > more powerful? Lots of possible answers, a few: * Fun * Transferable skills * No single solution is ever the answer to all problems (not all problems are nails, not all solutions are hammers) * Someone needs to understand the nuts and bolts of what's going on * Just because something is difficult doesn't mean its not worth doing * Understanding what's actually going on But the killer answer for me really is: "Why not" :-) Have fun :-) Michael. -- http://mail.python.org/mailman/listinfo/python-list
Re: PEP thought experiment: Unix style exec for function/method calls
Carl Banks wrote: > Maybe look to see how tail-recursive optimization in languages such as > Scheme work, and whether it can be generalized. Thanks for the feedback - I should've remembered tail recursion. > I doubt this would be possible without a major change in how functions > work in Python. The most obvious problem is that functions refer to > local variables by an index, not by name. If you were to execute the > bytecode of one function in the stack frame of another, Bad Things > would happen. Oh that's a pity. I can see why you'd do that, but it is a pity. That would tend to imply that _replacing_ rather than _reusing_ the top frame is the most doable/likely/sensible approach. (It's also very obviously easier to emulate) (And yes, I was consider reusing or replacing *only* the top stack frame. Replacing seems better with retrospect, even if reusing seemed like a fun idea :-) > > def set_name(): > > name = raw_input("Enter your name! > ") > > cexe greet() > > > > def greet(): > > print "hello", name > > > > cexe set_name() > > print "We don't reach here" > > -- > > > > This would execute, ask for the user's name, say hello to them and then > > exit - not reaching the final print "We don't reach here" statement. > > Only if you were to replace the whole stack. If you only replace or > reuse the top frame, I would think greet would exit and execution would > resume right after the point from which set_name was called. Or am I > misunderstanding what you want? I think you are. In the hypothetical example, your code by definition gets called by something. This leave a hypothetical return point. For the moment, lets make things clearer by what I mean by changing the example to this: > > 1 def set_name(): > > 2 name = raw_input("Enter your name! > ") > > 3 cexe greet() > > 4 > > 5 def greet(): > > 6 print "hello", name > > 7 > > 8 def main(): > > 9 cexe set_name() > > 10print "We don't reach here" > > 11 > > 12main() > > 13print "see what I mean?" > > -- at line 12, we call 8. We can argue then our stack looks like this: [ { "context" : "main", pc : 8 }, { "context" : "__main__", pc : 12 }, ] (I'm going to push/pop at the front of the list) We reach line 9, before we execute the code there: [ { "context" : "main", pc : 9 }, { "context" : "__main__", pc : 12 }, ] After we execute, it does a cexe call of set_name, not a normal call of set_name. This causes the top stack frame to be _replaced_ : [ { "context" : "set_name", pc : 1 }, { "context" : "__main__", pc : 12 }, ] We then carry on, until we reach line 3, at which point before execution our stack would look something like: [ { "context" : "set_name", pc : 3 }, { "context" : "__main__", pc : 12 }, ] And after: [ { "context" : "greet", pc : 5 }, { "context" : "__main__", pc : 12 }, ] We'd then execute line 6, and after executing that line, our stack would look like this: [ { "context" : "greet", pc : 6 }, { "context" : "__main__", pc : 12 }, ] We're falling off the end of a function at that point, so we'd pop the top stack frame, as follows: [ { "context" : "__main__", pc : 12 }, ] Which means we return to the line after 12, and continue on with line 13 print "see what I mean". That means the '''print "We don't reach here"''' code isn't executed. >> * Am I mad? :) > > Yep. :) Thought so! Thanks :-) Michael. -- http://mail.python.org/mailman/listinfo/python-list
Re: Broadcast server
SubscribeTo("periodictime") SimpleServer(protocol=getTimeProtocol, port=1600).run() (finish) Telnet to 127.0.0.1 1600 to see the result here. If you want to have the data source something from a client (eg event info coming in on port 1599) with clients of the event source coming in from elsewhere, this would change the server as follows: (start) from Kamaelia.Chassis.Pipeline import Pipeline from Kamaelia.Chassis.ConnectedServer import SimpleServer from Kamaelia.Util.Backplane import * Backplane("periodictime").activate() def publishTime(): return PublishTo("periodictime") def getTimeProtocol(): return SubscribeTo("periodictime") SimpleServer(protocol=publishTime, port=1599).run() SimpleServer(protocol=getTimeProtocol, port=1600).run() (finish) And the time (event) source would look like this: (start) import time from Kamaelia.Internet.TCPClient import TCPClient from Axon.ThreadedComponent import threadedcomponent from Kamaelia.Chassis.Pipeline import Pipeline from Kamaelia.Util.Stringify import Stringify class periodictime(threadedcomponent): def main(self): while 1: time.sleep(0.1) self.send(time.time(), "outbox") perdiodictimeserver = "127.0.0.1" Pipeline(periodictime(), Stringify(), # Make suitable for network TCPClient(perdiodictimeserver, 1599)).activate() (finish) Anyway, I hope the explanation of what's going on inside the core is useful since in many respects if you're writing your own select handling loop (which I would encourage you to do if you're learning about this!), the basics of what you have to do stay the same. (check activity, clear, when errors happen ask again, buffer data which needs to get sent, and decouple everything as best as makes sense whilst trying to avoid accidental serialisations). The reason for the examples in the end is merely for completeness. (I'll probably add these to our SVN distribution since the question does seem to crop up fairly often generally speaking!) If you're looking to do this in a production environment I'm personally an advocate of learning what's going on in the core and then using an existing library. (The reason Kamaelia exists is because I wondered if there was an alternative, potentially clearer way of writing these things, most people would quite sensibly just use Twisted - especially given you can buy a book on it! I personally think Kamaelia is cleaner, but then I would think that :) Have fun! Michael -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie pipe question
[EMAIL PROTECTED] wrote: > Hi > > I want to write a python script that accepts input ( a single line of > text) from another program (squid proxy) and sends back output ( a > single line of text). I am not sure how to go about this With a squid redirector (which is presumably what you mean) the things you need to do are pretty simple: * Read a line of text as sent to you by squid on stdin * do some transform (if appropriate) * Write the line back to stdout The key thing you want to ensure is that you're not buffered. ie make sure at the top of your python script you have "-u" after the python path. ie your minimal script should look something like: #!/usr/bin/python -u import sys while 1: line = sys.stdin.readline() # do something sys.stdout.write(line) Really quite simple you'll be pleased to see. Regards, Michael. -- http://kamaelia.sourceforge.net/Home http://yeoldeclue.com/blog -- http://mail.python.org/mailman/listinfo/python-list
Re: Help me use my Dual Core CPU!
Simon Wittber wrote: > I've just bought a new notebook, which has a dual core CPU. > > I write cross platform games in Python, and I'd really like to be able > to use this second core (on my machine, and on user's machines) for any > new games I might write. > > I know threads won't help (in CPython at least) so I'm investigating > other types of concurrency which I might be able to use. I really like > the PyLinda approach, however I need to be able to pass around all the > simple python types, which PyLinda won't help me with. Also, PyLinda is > more focused on distributing computing; I really only want to have 2 > processes cooperating (or 4, if I had 4 CPUs/cores etc). > > Is there any cross platform way to share python objects across > processes? (I've found POSH, but it's old, and doesn't appear to be > maintained). I could implement my own object space using shared memory, > but from what I can see, this is not available on Win32. > > Are there any other concurrency options I've not discovered yet? We *haven't* implemented process based components for Kamaelia yet, however if a process component base class (which is something we want to do) was created that might serve as a possiblity. If you're interested in this I could chat with you about the ideas we've had about how this would work (#kamaelia on freenode or here). (We are planning on doing this when we manage to get sufficient round tuits). It's probably worth noting that we wouldn't be thinking of using shared objects, but piping data between the two processes - with the various options including pickling to memory mapped files (however there's security issues there aside from anything else...). Also, Paul Boddie posted a module for parallel systems a while back as well which might be useful (at least for ideas): * http://cheeseshop.python.org/pypi/parallel I'd be interested in helping out BTW :) Michael. -- http://kamaelia.sourceforge.net/Home http://yeoldeclue.com/blog -- http://mail.python.org/mailman/listinfo/python-list
How do I converted a null (0) terminated string to a Python string?
Hi All, I've received (via UDP) a null terminated string and need to convert it into a Python string. Can anyone tell me how this is done? If it helps, I know the number of characters in the string. Thanks, M. McDonnell -- http://mail.python.org/mailman/listinfo/python-list
Re: How do I converted a null (0) terminated string to a Python string?
Thank you very much for your responses. To answer some of the questions... Yes, I am in Python receiving a C language 0 terminated string that was sent to my Python program in a UDP packet (which is how I know the count). Are your responses still correct given this clarification? Thanks much, MDM John Machin wrote: > Michael wrote: > > Hi All, > > > > I've received (via UDP) a null terminated string and need to convert it > > into a Python string. Can anyone tell me how this is done? If it helps, > > I know the number of characters in the string. > > > > I think you mean NUL, not null. > > What have you received it into, if it's not a Python string? > > You probably need/want this: > > if strg[-1] == "\0": > strg = strg[:-1] > alternatively: > strg = strg.rstrip("\0") # requires Python 2.2.2 or later > > It's possible you may be talking about a fixed length string which > contains useful_stuff + "\0" + padding -- in that case you need > > strg = strg.split("\0")[0] # grab upto (but not including) the first > NUL (if any) > > If you're not sure what you've got, print repr(the_input_string) > > HTH, > John -- http://mail.python.org/mailman/listinfo/python-list
Re: How do I converted a null (0) terminated string to a Python string?
John, Thanks for your reply. Just wondering... how are Python strings formatted? Evidently they're not 0 terminated. Thanks again, MDM John Machin wrote: > Michael top-posted [corrected]: > > John Machin wrote: > > > Michael wrote: > > > > Hi All, > > > > > > > > I've received (via UDP) a null terminated string and need to convert it > > > > into a Python string. Can anyone tell me how this is done? If it helps, > > > > I know the number of characters in the string. > > > > > > > > > > I think you mean NUL, not null. > > > > > > What have you received it into, if it's not a Python string? > > > > > > You probably need/want this: > > > > > > if strg[-1] == "\0": > > > strg = strg[:-1] > > > alternatively: > > > strg = strg.rstrip("\0") # requires Python 2.2.2 or later > > > > > > It's possible you may be talking about a fixed length string which > > > contains useful_stuff + "\0" + padding -- in that case you need > > > > > > strg = strg.split("\0")[0] # grab upto (but not including) the first > > > NUL (if any) > > > > > > If you're not sure what you've got, print repr(the_input_string) > > > > > > HTH, > > > John > > Thank you very much for your responses. To answer some of the > > questions... Yes, I am in Python receiving a C language 0 terminated > > string that was sent to my Python program in a UDP packet (which is how > > I know the count). Are your responses still correct given this > > clarification? > > My responses are correct. Your "clarification" indicates to me that you > are going by what you are told, not by inspection of (several instances > of) the packet contents, using repr(). It's up to you whether you want > to be skeptical about the packet contents or not. I certainly wouldn't > be throwing the last byte away without checking that it was in fact a > NUL. > > Cheers, > John -- http://mail.python.org/mailman/listinfo/python-list
Re: How do I converted a null (0) terminated string to a Python string?
John, Since I'm new to Python, I'm having trouble understanding what this means (see below). Would appreciate any help. if strg[-1] == "\0": strg = strg[:-1] Thanks, MDM John Machin wrote: > Fredrik Lundh wrote: > > Michael wrote: > > > > > Thanks for your reply. Just wondering... how are Python strings > > > formatted? Evidently they're not 0 terminated. > > > > have you tried *printing* the thing you got via UDP? > > > > to get a programmer-friendly representation of an arbitrary object, use > > > > print repr(obj) > > > > (where obj is your string, in this case). > > > > Probably not; there was no indication after the two messages where I > mentioned repr :-) -- http://mail.python.org/mailman/listinfo/python-list
Re: How do I converted a null (0) terminated string to a Python string?
I guess, I still don't see how this will work. I'm receiving a C zero-terminated string in my Python program as a 1K byte block (UDP datagram). If the string sent was "abc", then what I receive in Python is <0> How is Python going to know where in this 1K byte block the end of the string is? It seems that what I need to do is tell Python that the string ends at zero-relative index 3. What am I missing here? Marc 'BlackJack' Rintsch wrote: > In <[EMAIL PROTECTED]>, John Machin > wrote: > > > In other words, if the last byte of strg is NUL, throw it away. > > > > The truly paranoid would code that as > > if strg and strg[-1] etc etc > > so that it wouldn't blow up if strg is empty -- strange things can > > happen when you are reading other people's data :-) > > I would spell it: > > if strg.endswith('\0'): > strg = strg[:-1] > > Ciao, > Marc 'BlackJack' Rintsch -- http://mail.python.org/mailman/listinfo/python-list
Re: How do I converted a null (0) terminated string to a Python string?
Robert, Thanks to you and everyone else for the help. The "s.split('\x00', 1)[0] " solved the problem. Thanks again, MDM Robert Kern wrote: > Michael wrote: > > I guess, I still don't see how this will work. I'm receiving a C > > zero-terminated string in my Python program as a 1K byte block (UDP > > datagram). If the string sent was "abc", then what I receive in Python > > is <0> How is Python > > going to know where in this 1K byte block the end of the string is? It > > seems that what I need to do is tell Python that the string ends at > > zero-relative index 3. What am I missing here? > > Nothing. This is what I would do: > > > In [34]: s > Out[34]: 'abc\x00garbage' > > In [35]: s.split('\x00', 1)[0] > Out[35]: 'abc' > > In [36]: s.split? > Type: builtin_function_or_method > Base Class: > String Form: > Namespace: Interactive > Docstring: > S.split([sep [,maxsplit]]) -> list of strings > > Return a list of the words in the string S, using sep as the > delimiter string. If maxsplit is given, at most maxsplit > splits are done. If sep is not specified or is None, any > whitespace string is a separator. > > > Using the maxsplit argument saves split from having to do unnecessary work > splitting the garbage portion if there are nulls there, too. > > -- > Robert Kern > > "I have come to believe that the whole world is an enigma, a harmless enigma > that is made terrible by our own mad attempt to interpret it as though it > had > an underlying truth." >-- Umberto Eco -- http://mail.python.org/mailman/listinfo/python-list
Re: Help me use my Dual Core CPU!
Paul Rubin wrote: > "Michael Sparks" <[EMAIL PROTECTED]> writes: >> > Kamaelia doesn't attempt concurrency at all. Its main idea is to use >> > generators to simulate microthreads. >> >> Regarding Kamaelia, that's not been the case for over a year now. >> >> We've had threaded components as well as generator based ones since >> around last July, however their API stablised properly about 4 months >> back. If you use C extensions that release the GIL and are using an OS >> that puts threads on different CPUs then you have genuine concurrency. >> (those are albeit some big caveats, but not uncommon ones in python). > > Oh neat, this is good to hear. :) Ironically it was worth mentioning because we made a number of optimisations earlier in the year specifically to make it such that CPU usage of Kamaelia systems was much lower generally speaking to allow us to take advantage of multiple CPUs where available for an internal project described here: * http://kamaelia.sourceforge.net/KamaeliaMacro.html A "look, but can't get" front end here: * http://bbc.kamaelia.org/cgi-bin/blog/blog.cgi Code here: http://svn.sourceforge.net/viewvc/kamaelia/trunk/Code/Python/Kamaelia/Examples/DVB_Systems/Macro.py?view=markup If there were (say) 4 CPUs or cores on that system, then the graphline at the end could become: Graphline( SOURCE=DVB_Multiplex(freq, pids["NEWS24"] + pids["BBC ONE"] + pids["CBBC"] + pids["BBC TWO"]+pids["EIT"], feparams), DEMUX=DVB_Demuxer({ 600: ["BBCONE"], 601: ["BBCONE"], 610: ["BBCTWO"], 611: ["BBCTWO"], 620: ["CBBC"], 621: ["CBBC"], 640: ["NEWS24"], 641: ["NEWS24"], 18: ["BBCONE","BBCTWO", "CBBC","NEWS24"], }), NEWS24 = ChannelTranscoder(service_ids["NEWS24"], **params["HI"]), BBCONE = ChannelTranscoder(service_ids["BBC ONE"], **params["HI"]), BBCTWO = ChannelTranscoder(service_ids["BBC TWO"], **params["HI"]), CBBC = ChannelTranscoder(service_ids["CBBC"], **params["HI"]), linkages={ ("SOURCE", "outbox"):("DEMUX","inbox"), ("DEMUX", "NEWS24"): ("NEWS24", "inbox"), ("DEMUX", "BBCONE"): ("BBCONE", "inbox"), ("DEMUX", "BBCTWO"): ("BBCTWO", "inbox"), ("DEMUX", "CBBC"): ("CBBC", "inbox"), } ).run() And that would naturally take advantage of all 4 CPUs. Admittedly this is in a limited scenario right now, and is the exception not the rule, but does give an idea of where we'd like to end up, even if at the moment, like most people's machines, we default to making the most of a single CPU :-) So, whilst we don't automatically parallelise your code, or magically run across multiple CPUs, and whether that happens really depends on how practical it is. (I must admit I suspect it is doable though, and will be something worth addressing, and I'll look at it at some point if no-one else does :-) At the moment this means components explicitly working that way (such as the above ones do by the way the transcoder works). However I suspect explicit parallelisation or hinting for parallelisation (eg via baseclass, perhaps metaclass) will be doable and can be intuitive and practical :-) I might be alone in believing that of course :-) >> Personally, I'm very much in the camp that says "shared data is >> invariably a bad idea unless you really know what you're doing" >> (largely because it's the most common source of bugs for people where >> they're trying to do more than one thing at a time). People also >> generally appear to find writing threadsafe code very hard. (not >> everyone, just the people who aren't at the top end of the bell curve >> for writing code that does more than one thing at a time) > > I don't think it's that bad. Yes, free-threaded programs synchronized > by seat-of-the-pants locking turns to a mess pretty quickly. That's an agreement with my point (or rather I'm agreeing > But ordinary programmers write real-world applications with shared data > all the time, namely database apps. I don't call that shared data because access to the shared data is arbitrated by a third party - namely the dat
Re: Recursive descent algorithm able to parse Python?
[EMAIL PROTECTED] wrote: > I'm a compiler newbie and curious if Python grammar is able to > be parsed by a recursive descent parser or if it requires > a more powerful algorithm. Python is relatively simple to parse using a recursive descent parser. If you're rolling your own as a learning exercise, the grammar for python is included in one of the top level directories of the source distribution for python. The one area that is slightly different from usual is the emitting of INDENT and DEDENT tokens by the lexer due to handling of whitespace. But that's not really that complex either. A couple of years ago I decided to see what it would be like to try to parse a python-like language with no keywords and to see if you could do it test first (for fun :). If you do this, you discover the grammar is very short but you need terminator tokens to close if..elif...elif...else... like statements (eg if..elif...elif...else...end, try...except...except...except...endtry). I put that code up here: http://cerenity.org/SWP/ if you're curious. That and the python grammar file should probably help you roll your own parser. (It emits an AST that assumes everything is a function. I was pondering giving it a lisp backend or transforming to lisp but never got a round tuit) If however you're doing this because you're not aware of the compiler module, it's worth knowing that compiler.parse is a pretty useful function :-) Michael. -- http://mail.python.org/mailman/listinfo/python-list
[ANNOUNCE] Kamaelia 0.5.0 Released - now enhanced by Summer of Code :-)
and makes building software very visual. It's an open-source BBC Research project, originally designed for rapid development of server software. If you've used Unix pipes, Kamaelia is like those implemented in Python. If you want to know more about the theory read the one-page introduction [2] on the Kamaelia website. [2] http://kamaelia.sourceforge.net/Introduction.html Here's a taster of what a Kamaelia application looks like: (built with the GUI) [ Screenshot: http://photos1.blogger.com/blogger2/7070/3507/1600/composer-filedownloader.png ] And here's some equivalent Python code: Pipeline( ConsoleReader(eol=""), SimpleHTTPClient(), SimpleFileWriter("downloadedfile.txt"), ).run() Those 5 lines of Python give you a console-based HTTP downloading program (like wget or curl but with less options) using existing components. The ConsoleReader sends lines of text (URLs) the user enters to the HTTP client, which fetches the associated page. It then forwards on its contents to the file writer. It's as simple as that. You can also write your own components from scratch and use them with the existing ones. Version 0.5.0 is a major release - lots of functionality has been added from Google Summer of Code 2006. Key highlights of this release: * BitTorrent support (using the official BitTorrent client) - includes preliminary 'streaming over BitTorrent' support, can be used as a P2P backend in your own Kamaelia applications. [ screenshot in blog entry ] * HTTP client and nascent seaside-style pure-python webserver * OpenGL (e.g. the checkers/draughts board on the right) http://photos1.blogger.com/blogger2/7070/3507/400/thfcheckers.0.png {{{ http://kamaelia.sourceforge.net/t/Dirac3D.png }}} * Strong DVB (freeview TV) support on Linux - including the foundations of a PVR. {{{cf http://kamaelia.sourceforge.net/KamaeliaMacro.html}}} * Collaborative whiteboarding with audio (speex encoded) - draw and talk together over the internet. {{{To be featured in next month's Linux Format}}} * Enhanced visual composition of Kamaelia systems - create and link components on screen, see the code produced (the screenshot near the top of the article) For more information see the Kamaelia[3] website. You can get a copy of Kamaelia and Axon from Sourceforge, together with most of the dependencies[4] in the mega bundle. If you have any problems or questions, just pop along to #kamaelia on irc.freenode.net. [3] http://kamaelia.sourceforge.net/Home [4] http://sourceforge.net/project/showfiles.php?group_id=122494&package_id=183774&release_id=451251 """ -- {{{couple of added comments above added in like this}}} Other: * Axon has also been updated to 1.5.1 with enhancements to threaded components. * The current new MegaBundle version including all the dependencies is now 1.4.0! Download here: http://tinyurl.com/lfhxq * Kamaelia is licensed under the MPL/GPL/LGPL tri-license NOTE: The Megabundle is the recommended way of getting hold of Kamaelia, since given Kamaelia can act as glue between systems, it's useful to know which versions we're testing with should you encounter problems so and the detailed set of changes can be found here: http://tinyurl.com/nf5gk Kamaelia is heavily based on python generators (they're not scary, really [5]), but also support threaded components and is designed to work with any version of python since python 2.2a1, and is not limited to Python 2.5. [5] http://kamaelia.sourceforge.net/MiniAxon/ Have fun! Michael -- Michael Sparks, Kamaelia Project Lead http://kamaelia.sourceforge.net/Home blog: http://yeoldeclue.com/blog -- http://mail.python.org/mailman/listinfo/python-list
Re: Help me use my Dual Core CPU!
Paul Rubin wrote: > Michael <[EMAIL PROTECTED]> writes: >> > But ordinary programmers write real-world applications with shared data >> > all the time, namely database apps. >> >> I don't call that shared data because access to the shared data is >> arbitrated by a third party - namely the database. I mean where 2 or >> more people[*] hold a lock on an object and share it - specifically >> the kind of thing you reference above as turning into a mess. > > Ehhh, I don't see a big difference between having the shared data > arbitrated by an external process with cumbersome message passing, > or having it arbitrated by an in-process subroutine or even by support > built into the language. If you can go for that, I think we agree on > most other points. The difference from my perspective is that there are two (not mutually exclusive) options: A Have something arbitrate access and provide useful abstractions designed to simplify things for the user. B Use a lower level abstraction (eg built into the language, direct calling etc) I don't see these as mutually exclusive, except the former is aimed at helping the programmer, whereas the latter can be aimed at better performance. In which case you're back to the same sort of argument regarding assembler, compiled or dymanic languages are a good idea, and I'd always respond with "depends on the problem in hand". As for why you don't see much difference I can see why you think that, but I personally believe that with A) you can shared best practice [1], whereas B) means you need to be able to implement best practice. [1] Which is always an opinion :) (after all, once upon a time people thought goto was a good idea :) >> > This is just silly, and wasteful of the >> > efforts of the hardworking chip designers > >> Aside from the fact it's enabled millions of programmers to deal with >> shared data by communicating with a database? > > Well, sure, but like spreadsheets, its usefulness is that it lets > people get non-computationally-demanding tasks (of which there are a > lot) done with relatively little effort. More demanding tasks aren't > so well served by spreadsheets, and lots of them are using databases > running on massively powerful and expensive computers when they could > get by with lighter weight communications mechanisms and thereby get > the needed performance from much cheaper hardware. That in turn would > let normal folks run applications that are right now only feasible for > relatively complex businesses. If you want, I can go into why this is > important far beyond the nerdy realm of software geekery. I'd personally be interested to hear why you think that. I can think of reasons myself, but would be curious to hear yours. >> For generator based components we collapse inboxes into outboxes >> which means all that's happening when someone puts a piece of data >> into an outbox, they're simply saying "I'm no longer going to use >> this", and the recipient can use it straight away. > > But either you're copying stuff between processes, or you're running > in-process without multiprocessor concurrency, right? For generator components, that's in-process and not multiprocessor concurrency, yes. For threaded components we use Queue.Queues, which means essentially the reference is copied for most real world data, not the data itself. One step at a time I suppose really :-) One option for interprocess sharing we're considering (since POSH looks unsupported, alpha, and untested on recent pythons), is to use memory mapped files. Thing is that means serialising everything which could be icky, so it'll have to be something we come back to later. (Much of our day to day work on Kamaelia is focussed on solving specific problems for work which rolls back into fleshing out the toolkit. It would be extremely nice to spend time on solving a particular issue that would benefit from optimising interprocess comms). If we can make kamaelia benefit from the work the hardware people have done for shared memory, that's great. However it's interesting to see things like the CELL don't tend to use shared memory, and use this style of communications approach. What approach will be most useful going forward? Dunno :-) I'm only claiming we find it useful :) >> This is traditional-lock free, > >> > Lately I've been reading about "software transactional memory" (STM), > >> I've been hearing about it as well, but not digged into it >> If you do dig out those STM references, I'd be interested :-) > > They're in the post you responded to: Sorry, brain fart on my part. Thanks :-) Michael. -- http://mail.python.org/mailman/listinfo/python-list
Re: Will GPL Java eat into Python marketshare?
>> Short answer: People use Python instead of Java because people (at >> least intelligent people) tend to avoid pain. >> > Intelligent people don't suffer from fanboy sentiments. They just pick a > language that works best for them. I agree with the previous poster and don't think it's just being a fan boy. Some projects require Java, or at least are easier in Java, such as creating browser applets, but in the majority of cases I find Python much easier to work in. The code is more terse and easier to read and work with. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Parallel Paradigm
Sandy wrote: ... > Lots of trees, but where's the Wood? > > Where are concurrency/distributed models compared and discussed? I don't know about wood, but you can find a shrubbery[*] called Kamaelia sitting here: * http://kamaelia.sourceforge.net/Home A basic (albeit now aging) tutorial as to how to take some existing code and create components which can be used is here: * http://kamaelia.sourceforge.net/cgi-bin/blog/blog.cgi?rm=viewpost&nodeid=1113495151 Perhaps usefully, there's an overview of Kamaelia in this whitepaper: * http://www.bbc.co.uk/rd/pubs/whp/whp113.shtml It does miss out work from the past 18 months work since it was written though. Also, every single example you see here: * http://kamaelia.sourceforge.net/Cookbook.html Is rather heavily parallel, except we just call our units of execution components, components then talk in a CSP style fashion. The problem you've *REALLY* got is making things accessible to the average developer. That's the target audience for Kamaelia. ( If it doesn't work for that kind of person, it's a conceptual bug in the system. ) As a result we normally refer to components being really simple, communicating with the outside world via inboxes and outboxes (think a person at a desk with some in-trays and out-trays). Why? Because this is directly analogous to the ideas in both CSP *and* Unix pipelines - the latter has been long understood by unix people as a way of making it easier to build systems, the former understood largely by people interested in parallelism as a good mechanism. It also provices a nice route for development of new components - you simply solve the core of the problem you wanted to solve, and then remove the inputs back to inboxes and remove outputs back to outboxes. It doesn't work for every class of problem, but does for a suprising number. We're in the process of revamping the website at the moment (it's November). But for the moment, a simple tutorial on how the core concurrency tools in Kamaelia fundamentally work is here: * http://kamaelia.sourceforge.net/MiniAxon/ (The tutorial was originally written for a pre-university who'd learnt python the previous week) One interesting fact though - we have an application for collaborative whiteboarding where each whiteboard is both a client & server, and they can form a collaboration tree - whereby everything scribbled and said (each whiteboard contains an audio mixer) is distributed to everyone connected in a P2P like fashion. It's a classic example of a desktop application with a small twist, and was written because we needed it. The reason it's interesting is because it's highly parallel, with around 70 active components normally. *IF* the baseclass was changed to put the generators in seperate threads, and ran under a Python VM that could actually make the threads utilise seperate CPU's effectively (as allegedly the IronPython VM does), then you'd be able to utilise a 60, 70 odd multicore CPU with no change to the application code. It's a nice example of a real world application written to solve a specific need (we split the team multisite and needed an audio & scribbling based) collaboration tool which is able to work effectively on existing hardware systems, and *SHOULD* be trivially scalable to massively multicore systems as VMs and CPUs become available, with no real change to the application code. (Small change to Axon - our concurrency mechanism would be needed as noted above. Obviously at the moment that change makes no sense to us) It's also interesting because it was simpler to write due to the way we make concurrency available (by thinking in terms of components that form pipelines & graphlines), rather than despite using concurrency. (The initial version took around 3 days) If you want more detail on the whiteboard, there's been a tutorial in the past month's Linux Format (ie December issue). If you're interested in discussing anything concurrency related, and need a forum to do so, you're more than welcome to use the Kamaelia mailing list for doing so. Kamaelia's hardest core goal is to make concurrency actually USEFUL for the average developer. (though we tend to target maintenance over initial development - which doesn't always makes life easier initially) That said, we seem to be getting somewhere. > There's Stackless Python (which I can't make head or tail of; I have been > unable to find any lucid overview, Finally, if the descriptions you find on the Kamaelia website don't make sense to you, PLEASE tell us. I consider that as much of a bug as anything else. (and we can't know that without being told) Regards, Michael. -- Kamaelia Project Lead/Dust Puppy http://kamaelia.sourceforge.net/Home http://yeoldeclue.com/blog [*] (yes, it's a deliberate mispelling of Camellia, which is a shrubbery, rather than a backronym) -- http://mail.python.org/mailman/listinfo/python-list
Re: Why does this code crash python?
while 1: while self.dataReady("inbox"): self.recv("inbox") myturn, notmyturn = notmyturn, myturn self.send(myturn, "outbox") if not self.anyReady(): self.pause() yield 1 Or indeed a game where there are more than 2 players. (Which is a nice sideeffect of decoupling your code like this) The full thing including imports looks like this for reference: #!/usr/bin/python import Axon import time import pygame from Kamaelia.Chassis.Pipeline import Pipeline from Kamaelia.UI.Pygame.Ticker import Ticker from Kamaelia.UI.Pygame.KeyEvent import KeyEvent class ChessTurnLogic(Axon.Component.component): def main(self): myturn, notmyturn = "white", "black" self.send(myturn, "outbox") while 1: while self.dataReady("inbox"): self.recv("inbox") myturn, notmyturn = notmyturn, myturn self.send(myturn, "outbox") if not self.anyReady(): self.pause() yield 1 class TimerLogic(Axon.Component.component): def main(self): times_info = {} player = None while 1: while self.dataReady("inbox"): new_player = self.recv("inbox") now = time.time() if player is not None: (total, last) = times_info[player] total = total + (now - last) times_info[player] = (total, now) self.send(player + " " + str(total) + "\n\n", "outbox") player = new_player try: (total, last) = times_info[player] times_info[player] = (total, now) except KeyError: times_info[player] = (0, now) if not self.anyReady(): self.pause() yield 1 Pipeline( KeyEvent(key_events = { pygame.K_SPACE: ("SPACE", "outbox")} ), ChessTurnLogic(), TimerLogic(), Ticker(background_colour=(128,48,128), render_left = 1, render_top = 1, render_right = 600, render_bottom = 200, position = (100, 300), ) ).run() (I've tested the above code BTW. It's not pretty, but it works :). Prettiness could be added in all sorts of ways though :-) Regards, Michael. -- Kamaelia Project Lead/Dust Puppy http://kamaelia.sourceforge.net/Home http://yeoldeclue.com/blog -- http://mail.python.org/mailman/listinfo/python-list
Page layouts in mod_python?
Hey everyone, Is it possible to automatically insert headers/footers using mod_python? I will be not be using PSP's, so I cannot use the PSP/include solution. Furthermore, the header will be dynamic; it won't be a static HTML page. In short, I've been looking for a page layout facility using mod_python. (Similar to the layout capability in Ruby on Rails.) Thank you in advance, -Michael -- http://mail.python.org/mailman/listinfo/python-list
Re: code optimization (calc PI)
Ah, no. It was a HASH (assoziative Array or somethings like that). mm wrote: > > I konw, that for example while-loops in Perl are very slow. Maybe this > is also known in Pyhton. Then, I can translate the while-loops in to > for-loops, for example. > More general, maybe there is a speed optimazation docu out there. > -- http://mail.python.org/mailman/listinfo/python-list
Re: C/C++, Perl, etc. to Python converter
Yes, I konw what you mean. And thats the right way to do it - for beginners. --But not for someone who allready know programmings things for many years. They ask themself: How can I do this in Python? I can remember, it was that-way with Perl or C or C++ or whatever. So, not only a ververter can be useful, also a translation table. (Maybe just a table for print out.) LangX <-> LangY Matimus wrote: > I don't know of a converter, one may exist. I have seen similar > requests though and will give you a similar response to what I have > seen. A converter, if it exists, may be able to produce working code > but _not_ readable code. Python is a language whose strength comes > from, among other things, its readability and conventions. Learning > python is best done by using the online documentation > (http://docs.python.org/tut/tut.html) and reading existing code (take a > look at the built in modules). > > My biggest fear of teaching someone to program by using a program to > convert perl to python is that they will end up writing python that > still looks like perl. > > I don't know if it helps, but I know others will give you similar > advice. > > -Matt > -- http://mail.python.org/mailman/listinfo/python-list
Re: code optimization (calc PI)
Hmm.. thanks. I did this changes, but without any performance profits. Matimus wrote: > Using the '+' operator for string concatonation can be slow, especially > when done many times in a loop. > > >> pi = pi + str("%04d" % int(e + d/a)) ## this should be fast?! I dont > > > The accepted solution would be to make pi an array and append to the > end... > > pi = [] #create the array (empty) > ... > ... > pi.append(str("%04d"%int(e+d/a))) # append to it > > And when it is time to print the results do the following: > > print "".join(pi) > > It may look strange, but it is a common Python idiom. You might also > look into an optimizer such as psycho: http://psyco.sourceforge.net/. > -- http://mail.python.org/mailman/listinfo/python-list
Re: code optimization (calc PI)
Yes. But it still runns very slowly. If someone is really interested in speed optimization, I can publish my PI-calc code. Maybe for some Python compiler/interpreter hackers... ;-) (There are only 2 while-loops, one within another, and some simple basic calculations. Nothing special.) Stefan Schwarzer wrote: > On 2007-01-03 16:50, mm wrote: > >>More general, maybe there is a speed optimazation docu out there. > > > At least Alex Martellis "Python in a Nutshell" has a section on > optimization. > > I presented this at the last EuroPython conference: > http://sschwarzer.com/download/optimization_europython2006.pdf > > Stefan > -- http://mail.python.org/mailman/listinfo/python-list
Jabber/XML-RPC lib in Python?
I was wanting to write a program that lets two machines communicate (without user intervention) using XML-RPC over a Jabber network. Does anyone know of an existing library suited to that task? I'd like it if I didn't need to worry about writing any Jabber or XML-RPC code if I could and just worry about the logic of my own program. Any ideas? -- Michael <[EMAIL PROTECTED]> http://kavlon.org -- http://mail.python.org/mailman/listinfo/python-list
mod_python seg faults with xml
Trying to process xml (xml-rpx) with a handler in mod_python crashes mod_python. Previous versions worked fine. Only change is I recompiled with newest versions of Apache, Python, and mod_python. No PHP or anything like that involved. Any idea why it seg faults when I try to import xmlrpclib and similar modules? Thanks. -- Michael <[EMAIL PROTECTED]> http://kavlon.org -- http://mail.python.org/mailman/listinfo/python-list
moving from c++ to python
Hi, I'm a pretty sound programmer in C++, but would like to learn python! Does anyone know of any tutorial s aimed at me?? My biggest confusion so far is the lack of pointers in Python .. Regards Michael -- http://mail.python.org/mailman/listinfo/python-list
Newbie python design question
Hi, I'm trying to write a script to parse a .cpp file and begin to create a 'translational unit'. To do this i need to: Go through the file and remove all 'C' comments as /* Comment 1*/ (can be on multiple lines) Go through and remove all 'C++' comments, anything between // and '\n' char. The start at the top, and work the way through, with the following valid terms: #include #include "filename" - copy the contents of filename to this point in the file and continue. #define X Y -Store the term X,Y in DefineDictionary, then later if X is encountered, substitute Y. namespace n { }; -a namespace, can contain classes, functions and sub-namespaces class c { }; -a class. If i were to process this in C++, i would create some form of statemachine, similar to a regex engine... I would just like some ideas on the easiest way to implment this in python!! Regards Mike -- http://mail.python.org/mailman/listinfo/python-list
Incrementing letters
Hi, I've got a string s, and i want to shift all the letters up by one, eg a->b, b->c z->a In c++ i can do this quite simply with if(C == 'z') C='a'; else C++; but i can't work out how to do this this in python?? Regards Michael -- http://mail.python.org/mailman/listinfo/python-list
Is there a better way of doing this?
Hi, I'm fairly new at Python, and have the following code that works but isn't very concise, is there a better way of writing it?? It seems much more lengthy than python code i have read. :-) (takes a C++ block and extracts the namespaces from it) def ExtractNamespaces(data): print("Extracting Namespaces") p = re.compile( 'namespace (?P[\w]*)[\n\t ]*{') subNamespaces = [] newNS = p.search(data) while( newNS ): print "\t" + newNS.group("name") OPCount = 1 Offset = newNS.end() while(OPCount > 0): if( data[Offset] == "}" ): OPCount = OPCount -1; elif( data[Offset] == "{" ): OPCount = OPCount + 1; Offset = Offset+1; #Extract Data: newNSData = data[newNS.end():Offset-1] data = data[0:newNS.start()] + data[Offset:] newNamespace = [newNS.group("name"), newNSData]; subNamespaces.append(newNamespace) #Perform NewSearch newNS = p.search(data) return [subNamespaces,data] -- http://mail.python.org/mailman/listinfo/python-list
working with pointers
Do expicit pointers exist in python?? if i do: a = [5,7] b = a a.empty() b = ? how do i do explicit pointers?? Mike -- http://mail.python.org/mailman/listinfo/python-list
Re: working with pointers
"Steven Bethard" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Michael wrote: > > Do expicit pointers exist in python?? > > > > if i do: > > > > a = [5,7] > > b = a > > > > a.empty() > > > > b = ? > > This is what the interactive prompt is for. Try it: > > py> a = [5,7] > py> b = a > py> a.empty() > Traceback (most recent call last): >File "", line 1, in ? > AttributeError: 'list' object has no attribute 'empty' > > Well, looks like you get an AttributeError. Let's try a method that > actually exists instead: > > py> a.pop() > 7 > py> a > [5] > py> b > [5] > > So, as you can see, since 'a' and 'b' are both names referring to the > same object, when you modify the object referred to by 'a', you are also > modifying the object referred to by 'b'. > > > how do i do explicit pointers?? > > I don't know what you mean by "explicit pointers". Care to elaborate? > It also might help if you explained what it is you think you want > "explicit pointers" to do. > > STeVe sorry, I'm used to working in c++ :-p if i do a=2 b=a b=0 then a is still 2!? so when do = mean a reference to the same object and when does it mean make a copy of the object?? regards Mike -- http://mail.python.org/mailman/listinfo/python-list
Re: working with pointers
except numbers?? "Dave Brueck" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Michael wrote: > > sorry, I'm used to working in c++ :-p > > > > if i do > > a=2 > > b=a > > b=0 > > then a is still 2!? > > > > so when do = mean a reference to the same object > > Always. > > > and when does it mean make a copy of the object?? > > Never. > > -Dave -- http://mail.python.org/mailman/listinfo/python-list
wxPython
Hi, I've got a question about wxPython, wheres the best place to ask?? Mike -- http://mail.python.org/mailman/listinfo/python-list
Software for System Builders, Resellers, and Hardware Purchasers Only.
GET latest softwares, 99% savings. http://djjnjs.b0fqeab48lt0qub.risalafe.com There is a fullness of all things, even of sleep and love. Be not ashamed of mistakes and thus make them crimes. -- http://mail.python.org/mailman/listinfo/python-list
Why are functions atomic?
Why are functions atomic? (I.e. they are not copied.) For example, I would like to make a copy of a function so I can change the default values: >>> from copy import copy >>> f = lambda x: x >>> f.func_defaults = (1,) >>> g = copy(f) >>> g.func_defaults = (2,) >>> f(),g() (2, 2) I would like the following behaviour: >>> f(),g() (1,2) I know I could use a 'functor' defining __call__ and using member variables, but this is more complicated and quite a bit slower. (I also know that I can use new.function to create a new copy, but I would like to know the rational behind the decision to make functions atomic before I shoot myself in the foot;-) Thanks, Michael. -- http://mail.python.org/mailman/listinfo/python-list
Re: Why are functions atomic?
On May 1, 9:34 am, John Nagle <[EMAIL PROTECTED]> wrote: > Michael wrote: > > Why are functions atomic? (I.e. they are not copied.) > > Because Python has objects for when you need to associate > state with a function. > > John Nagle Then why are functions mutable? I can understand to some extent why functions are not picklable, because the bytecode may not be the same across python implementations (is that true?), but I do not understand why copying functions is a problem. The patch that allows copy to pass-through functions just emulates pickle, but I can find no discussion or justification for not allowing functions to be copied: http://thread.gmane.org/gmane.comp.python.devel/76636 Michael. -- http://mail.python.org/mailman/listinfo/python-list
Re: Why are functions atomic?
>From TFM "Function objects also support getting and setting arbitrary attributes, which can be used, for example, to attach metadata to functions. Regular attribute dot-notation is used to get and set such attributes. Note that the current implementation only supports function attributes on user-defined functions. Function attributes on built-in functions may be supported in the future." http://docs.python.org/ref/types.html Again, rather inconsitent with the copy sematics. > On May 1, 9:34 am, John Nagle <[EMAIL PROTECTED]> wrote: > > Because Python has objects for when you need to associate > > state with a function. > > > John Nagle -- http://mail.python.org/mailman/listinfo/python-list
Re: Why are functions atomic?
A bit more info, but still no clear picture about why functions are mutable but have immutable copy symantics. There are arguments why functions should be immutable, but the decision was to make user- defined functions mutable. My question is still: why the present ummutable copy symantics? http://www.python.org/dev/peps/pep-0232/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Why are functions atomic?
> Your use case appears to be that you > want to make multiple copies of the same function, and those copies > should be almost, but not quite, the same. > > The Pythonic solution is to produce the copies by a factory function... > > >>> def powerfactory(exponent): > ...def inner(x): > ... return x**exponent > ...return inner Is there a reason for using the closure here? Using function defaults seems to give better performance: >>> def powerfactory(exponent): ...def inner(x,exponent=exponent): ... return x**exponent ...return inner This is definitely one viable solution and is essentially what I had in mind, but I did not want to have to carry the generator arround with me: Instead, I wanted to use it once as a decorator and then carry only the function around. >>> @declare_options(first_option='opt1') >>> def f(x,opt1,opt2,opt3): ... return x*(opt1+opt2*opt3) >>> f.set_options(opt1=1,opt2=2,opt3=3) >>> f(1) 7 >>> from copy import copy >>> g = copy(f) >>> g.set_options(opt1=4,opt2=5,opt3=6) >>> f(1) 7 >>> g(1) 34 The decorator declare_options behaves like the generator above, but adds some methods (set_options) etc. to allow me to manipulate the options without generating a new function each time. I have functions with many options that may be called in the core of loops, and found that the most efficient solution was to provide all of the options through func_defaults. >>> def f(x,opt1,opt2,opt3): ... return x*(opt1 + opt2*opt3) The cleanest (and fastest) solution I found was to set the options in the defaults: >>> f.func_defaults = (1,2,3) Then f can be passed to the inner loops and f(x) is very quick. Other options include using lists and dict's: >>> opt = (1,2,3) >>> f(1,*opt) 7 but then I have to pass f and opt around. This also appears to be somewhat slower than the defaults method. Dictionaries have the advantage of associating the names with the values >>> opt = {'opt1':1, 'opt2':2, 'opt3':3} >>> f(1,**opt) 7 but this is much slower. Wrapping the function with a generator as you suggest also works and packages everything together, but again suffers in performance. It also complicates my code. The result of my declare_options decorator is that the result is a regular function, complete with docstring etc. but with added annotations that allow the options to be set. In addition, the performance optimal. I though this was a very clean solution until I realized that I could not make copies of the functions to allow for different option values with the usual python copy symantics (for example, a __copy__ method is ignored). I can easily get around this by adding a custom copy() method, but wondered if there was anything inherently dangerous with this approach that would justify the added complications of more complicated wrappings and the performance hit. Pickling is an obvious issue, but it seems like there is nothing wrong with the copy semantics and that the limitations are artificial and out of place. (It is also easily fixed: if the object has a __copy__ method, use it. Truely immutable objects will never have one. There may be subtle issues here, but I don't know what they are.) Thanks for all of the suggestions, Michael. -- http://mail.python.org/mailman/listinfo/python-list
Re: Why are functions atomic?
On May 2, 6:08 am, Carsten Haese <[EMAIL PROTECTED]> wrote: > On Tue, 2007-05-01 at 22:21 -0700, Michael wrote: > > Is there a reason for using the closure here? Using function defaults > > seems to give better performance:[...] > > It does? Not as far as I can measure it to any significant degree on my > computer. I agree the performance gains are minimal. Using function defaults rather than closures, however, seemed much cleaner an more explicit to me. For example, I have been bitten by the following before: >>> def f(x): ... def g(): ... x = x + 1 ... return x ... return g >>> g = f(3) >>> g() Traceback (most recent call last): File "", line 1, in File "", line 3, in g UnboundLocalError: local variable 'x' referenced before assignment If you use default arguments, this works as expected: >>> def f(x): ... def g(x=x): ... x = x + 1 ... return x ... return g >>> g = f(3) >>> g() 4 The fact that there also seems to be a performance gain (granted, it is extremely slight here) led me to ask if there was any advantage to using closures. It seems not. > An overriding theme in this thread is that you are greatly concerned > with the speed of your solution rather than the structure and > readability of your code. Yes, it probably does seem that way, because I am burying this code deeply and do not want to revisit it when profiling later, but my overriding concern is reliability and ease of use. Using function attributes seemed the best way to achieve both goals until I found out that the pythonic way of copying functions failed. Here was how I wanted my code to work: @define_options(first_option='abs_tol') def step(f,x,J,abs_tol=1e-12,rel_tol=1e-8,**kwargs): """Take a step to minimize f(x) using the jacobian J. Return (new_x,converged) where converged is true if the tolerance has been met. """ return (x + dx, converged) @define_options(first_option='min_h') def jacobian(f,x,min_h=1e-6,max_h=0.1): """Compute jacobian using a step min_h < h < max_h.""" return J class Minimizer(object): """Object to minimize a function.""" def __init__(self,step,jacobian,**kwargs): self.options = step.options + jacobian.options self.step = step self.jacobian = jacobian def minimize(self,f,x0,**kwargs): """Minimize the function f(x) starting at x0.""" step = self.step jacobian = self.jacobian step.set_options(**kwargs) jacobian.set_options(**kwargs) converged = False while not converged: J = jacobian(f,x) (x,converged) = step(f,x,J) return x @property def options(self): """List of supported options.""" return self.options The idea is that one can define different functions for computing the jacobian, step etc. that take various parameters, and then make a custom minimizer class that can provide the user with information about the supported options etc. The question is how to define the decorator define_options? 1) I thought the cleanest solution was to add a method f.set_options() which would set f.func_defaults, and a list f.options for documentation purposes. The docstring remains unmodified without any special "wrapping", step and jacobian are still "functions" and performance is optimal. 2) One could return an instance f of a class with f.__call__, f.options and f.set_options defined. This would probably be the most appropriate OO solution, but it makes the decorator much more messy, or requires the user to define classes rather than simply define the functions as above. In addition, this is at least a factor of 2.5 timese slower on my machine than option 1) because of the class instance overhead. (This is my only real performance concern because this is quite a large factor. Otherwise I would just use this method.) 3) I could pass generators to Minimize and construct the functions dynamically. This would have the same performance, but would require the user to define generators, or require the decorator to return a generator when the user appears to be defining a function. This just seems much less elegant. ... @define_options_generator(first_option='min_h') def jacobian_gen(f,x,min_h=1e-6,max_h=0.1): """Compute jacobian using a step min_h < h < max_h.""" return J class Minimizer(object): """Object to minimize a function.""" def __init__(self,step_gen,jacobian_gen,**kwargs): self.options = step_gen.options + jacobian_gen.options self.step_gen