Re: [BangPypers] "The Python I would like to see" - Armin Ronacher
On Tuesday 19 August 2014 10:29 PM, Gora Mohanty wrote: [snip] To revive an ancient meme about whom to credit memorable quotes to: Do not write code. Write code that writes code I move that this list be renamed to banglispers posthaste. -Taj. ___ BangPypers mailing list BangPypers@python.org https://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] Object Oriented Programming in python
On 10/22/2013 1:10 PM, Saager Mhatre wrote: [snip] I guess we're going to have to agree to disagree on that last point. I don't find the MOP implementation in Ruby or Groovy less per for many or leading to obscure code. On the contrary, I find Python's 'rough-and-ready tools' make for more confusing metaprogramming code. YMMV Right, what I meant was "clarity on behalf of code that most people write", not necessarily metaprogramming code. As in optimizing for the common case. Ah yes, old-style classes! But I'd be hard pressed to call that MOP as there are no real metaobjects at play. Not really true. Using the lingo of the OMG/MDA world[1]: After the replacement, "MyCircularClass" is an instance of itself and therefore exists on both M3 (metametamodel) and M2 (metamodel) levels. A new instance of MyCircularClass (without class change hijinks) exists at M1 (model) level, like a "normal" class instance. What we've gained is a relatively pure (meta-)class hierarchy to work with, along with the ability to customize behaviour at all levels using standard python class and metaclass facilities (__metaclass__, get/setattr, abc). It is more than enough to build a quite functional purely runtime MOP system. Also, you could create/simulate MOP using the tools Python provides you, but I'd much rather the platform give me a consistent implementation than have to deal with N people each implementing their own special variant of what they think MOP should be. Agreed. All I'm saying is that that stuff usually comes at a cost to "normal" code. In Common Lisp/CLOS for example, it means that you have to have the most sparse concrete syntax possible so that any change to the object model continues to be natural in use. Not that python couldn't be _better_ at it, but I wouldn't know how to do it personally. -Taj. [1] This is a reasonable introduction to the OMG-focused philosophy and terminology of MOP (PDF link): http://scg.unibe.ch/download/mm/Slides/01Intro.ppt.pdf ___ BangPypers mailing list BangPypers@python.org https://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] Object Oriented Programming in python
On 10/21/2013 11:53 AM, Saager Mhatre wrote: [snip] That's pretty much what always foiled my attempts at understanding Python MetaClasses, I was looking for power where there was none to find. The best comparison I could find was to Groovy's Compile time AST transforms, but even those are even more powerful as they drop down a level of abstraction and hand you the AST for the an rated element. I agree with you partially - MOP in python can get ugly, but there's plenty of power there. I'll try to explain, though this stuff is notoriously hard to articulate (may be just for me). So the main role of metaprogramming was to expose the language's object system as an object model to the programmer, so that it could be queried and manipulated at runtime. Does python do this? Yes, but not quite in the way that Kiczales outlined. Instead we get some rough-and-ready tools with which you can simulate the effects that a "proper" MOP system (such as in CLOS) might have, and when having to make a choice between flexibility vs performance and code clarity, python's designers have almost invariably chosen the latter. To begin with, there is a level of metacircularity in the new-style type system: >>> isinstance(object, object) True >>> isinstance(type, type) True >>> isinstance(object, type) True >>> isinstance(type, object) True This is what the metaclass system uses. With old-style classes, you had a lot of flexibility to change the class of objects at runtime. Nowadays there are a lot more restrictions, mostly due to performance, but you can still do some silly stuff: >>> MyType = type('MyType', (type,), {}) >>> MyCircularType = MyType('MyCircularType', (MyType,), {}) >>> MyCircularType.__class__ = MyCircularType >>> isinstance(MyCircularType, MyCircularType) True And then of course there's things like get/setattribute - combined with the abc module in the standard library, the type trickery above and python's basic metaclass system, you could effectively create your own metacircular MOP protocol on top of python.* Another major issue is that python's syntax makes it just not a great language for embedded DSLs and MOP, even to the extent that other dynamic languages (Ruby!) are. That's not going to change though, and I'm personally okay with it for the most part. If I wanted to program CLOS, I'd program CLOS. -Taj. * In fact, I spent quite a while doing so a few years ago - implementing the OMG MOF on python - and it worked pretty well, but it proved difficult to get it to the point where I could even properly explain it to people. ___ BangPypers mailing list BangPypers@python.org https://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] WSGI app instrumentation - middleware or other
On 08-Jun-11, at 11:38 AM, Anand Chitipothu wrote: [snip] Graphite + statsd/pystatsd is pretty good for performance tracking. Thanks, I hadn't heard of Graphite before, I've been struggling with Cacti. I'll give this a try. We use nagios for availability monitoring. There are many alternatives, try asking google. I've been using icinga and cacti - my interest is in the python side of things. You've given me a lot of good pointers, thanks again. http://www.google.co.in/search?q=nagios+alternatives Very funny! -Taj. ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
[BangPypers] WSGI app instrumentation - middleware or other
Hi all, I'd like to hear from python web devs on strategies for instrumenting production wsgi apps for perfomance and availability monitoring. Are there existing tools that you use? What sort of things do you monitor (db connection state, response time stats etc) and how intrusive are the instrumentation tools on the performance of the system itself? -Taj. ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] Practical experiences with uWSGI?
On 18-May-11, at 6:32 PM, Sreekanth S Rameshaiah wrote: [snip] We just follow the client's directive as both almost had similar performance. :) Thanks again. I hope you can give us a quick post mortem when you've had some time to observe it in production. -Taj. ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] The myth of free software
On 18-May-11, at 6:27 PM, Santosh Rajan wrote: (FUNCTION TROLL ((FUNCTION PRINT) "Hello World"))) Please just stop. -Taj. ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] Practical experiences with uWSGI?
Thanks for the reply, Sree. If I might pick your brain further - On 18-May-11, at 6:13 PM, Sreekanth S Rameshaiah wrote: [snip] We have used it in one large scale project which is due to go live in next few days. What was the reasoning behind choosing it over apache+mod_wsgi? What are the deal-breaker features that you'd miss if you went back to mod_wsgi? -Taj. ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
[BangPypers] Practical experiences with uWSGI?
Hi all, I've recently discovered the uWSGI project - http://projects.unbit.it/uwsgi/ and am hoping to hear from any of you who have had the chance to use it in production. Specifically: 1. Is nginx+uwsgi easier to use and manage than apache and mod_wsgi? 2. Is there a measurable difference in memory use and performance, in practical terms? 3. Did you notice any stability issues under load? I realize that I'm fishing here, but I'd really appreciate some insight. -Taj. ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] The myth of free software
On 17-May-11, at 8:32 PM, Santosh Rajan wrote: You still haven't seen the case I am making? In which case I shall admit defeat and rest my case! Same. And I apologize to all for my part in this trainwreck. -Taj. ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] The myth of free software
On 17-May-11, at 8:12 PM, Santosh Rajan wrote: great! so let us assume for arguments sake, that whatever you say or pose is right! What answers do you have have for the questions posed below? Huh? You started this thread, with the intention of modifying the behaviour of "young immature idiots" and "intellectual ignoranti". You've got us listening. State your case. What should we be doing instead? -Taj. ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] The myth of free software
On 17-May-11, at 5:35 PM, Santosh Rajan wrote: [snip] First of all, let us get one thing out of the way. Success in software/hardware, no matter where you are, entirely depends on on how passionate you are about it. We can't quantify a person's passion for some thing. Apple's success, is Steve Job's passion for whatever he was creating. Fair enough. Having got that out of the way, let us try to answer some of your questions. They weren't really questions. You've outlined a correlation and now I am asking you to provide the causation. Right! The environment counts a lot. Not the person. If you are based in India, you are already starting to bat with 3 wickets down viz a viz San Francisco, (If I can humbly give a cricket anology). This is true no matter what sort of technology business you start. What is different about business models that are FOSS-driven? Wrong and Right!. People outside the valley are definitely capable of creating/running business, but they have a great disadvantage. Their competitors based in the valley, have a competitive edge/eco system,that the poor great guys outside the valley can't match. I agree. So what is the solution, other than moving to the Valley? Forget about your three possible hypothesis . I hope I have answered your questions above! I think you've misunderstood. I'm still not clear on the following: 1. We've established that tech startups are at a disadvantage when based outside SV. Why is this so? Specifically, what is different about FOSS-model startups? 2. Given that we are not all moving to California, what are our options? I assume you are recommending against a FOSS-model startup. What sort of business models do you recommend instead? Why? 3. At least one person on this thread has pointed out that his Indian company is making a profit while working on FOSS. I know of a few others. Do you not recommend that others attempt to follow their example? Why? -Taj. ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] The myth of free software
On 17-May-11, at 6:22 AM, Santosh Rajan wrote: Ok let me tell you the whole story scientifically. "The chances of anyone succeeding with an Open Source Project or Startup Company is inversely proportional to the square of the distance between San Fransisco and the location of this OpenSource/Startup Project". Okay, let's assume this to be true. Now we need to determine the causality. What are the possible reasons? Here are some: 1. A person in India will fail at trying to create a business around open source, but is more likely to succeed at precisely the same endeavour, if they move to the Valley. It is the environment that counts, not the person. 2. People outside the Valley are incapable of creating/running that kind of business, because they don't know how to do it right. Moving to the Valley might increase their chances, since they will have better access to people who can teach or inspire them. 3. People who are already in the Valley are inherently superior, and us little people in India are wasting our time trying to emulate them. These are three possible hypotheses. What are yours? -Taj. ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] The myth of free software
On 17-May-11, at 1:32 AM, Santosh Rajan wrote: In that case why don't you explain your point in a very "coherent" manner, so that we lesser mortals get it. What are you saying with respect to the subject of this thread? When you write something like this: > So my question is, what shit are you talking about based in India? Please, > Please Enlighten me? I have a hard time winnowing sense out of that string of words, due to my limited reading and comprehension abilities. What does this have to do with open source people in San Francisco? I can only make broad assumptions about your intentions, especially when you later talk about the size of India's software market. In light of the above, I (and others) would be grateful if you made an effort to clearly state your case. -Taj. ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] The myth of free software
On 17-May-11, at 12:59 AM, Santosh Rajan wrote: [snip] I think you must read this thread very carefully again. Where did I say anything against FOSS? Where did I say software must be charged? Don't put words into my mouth just for your arguments sake. I could certainly quote bits of your own posts back to you, but it's a waste of my time. So what is the point you are making? Are you suggesting that I have no right to make the arguments I am making because i am paying/not paying Google? Yes. I understand your core point - first start making money, then talk about giving away the fruits of your time and effort when the basic needs are taken care of. You haven't framed it very coherently though, and you haven't really addressed the points others have made. There are plenty of people making a living off giving away software, and I think it's okay for Indians to aspire to it too. -Taj. ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] The myth of free software
On 16-May-11, at 11:51 PM, Santosh Rajan wrote: [snip] As long as people like you don't see the problem, India will always remain a second class power in the software world. Do you know that the entire software exports of India today is only 5% of the total software exports of the world? Thanks to great people like you, we call ourselves a great software nation. I've heard all sorts of arguments against FOSS*, but the idea that it is a moral/nationalistic imperative to charge for software? This one is new. Kudos. I assume you are walking the walk and paying google for that gmail account, so that nobody gets the idea that you support any business model that depends on giving away free services. -Taj. * Many of them are valid, but this one is not in their august company. ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] MetaClass in python
On 12-Jan-11, at 7:43 AM, Nitin Kumar wrote: [snip] So {} can contain function also for the new class, if then can you give example for the same. As i need function to class being generated. Yes, you can replace the {} with something like { 'my_method': some_function } but the 'some_function' is very specific to your problem. Depending on what you want in the body of the function, you have a range of available options, from defining the function inside another function body, like def custom_class(original_class_name): def replacement_method(obj): print "custom method for", original_class_name new_class = type(original_class_name, (object,), {'my_method': replacement_method}) return new_class ... to using eval to create your new function from a body of generated code in text form. -Taj. ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] MetaClass in python
On 11-Jan-11, at 8:47 PM, Nitin Kumar wrote: [snip] So i do know who to create a class and its function dynamically at runtime??? You can dynamically create a class like this: NewClass = type('NewClass', (object,), {}) where 'NewClass' is the name of your class, (object,) is any tuple of superclasses and {} will be the __dict__ of the new class. As steve mentioned earlier, you can reassign any "method" of the class to a function of your choice as long as it takes at least one argument. -Taj. ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] refactoring
On 12/6/2010 11:44 AM, Gora Mohanty wrote: Man, and here was I thinking that you were killing two birds with one stone: "To understand recursion, one must first understand recursion". That's exactly the one I thought about, and figured I'd better clarify before someone accused me of being arrogant enough to mess with the sacred koans. -Taj. ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] refactoring
On 12/6/2010 11:25 AM, Sriram Narayanan wrote: I sure hope that we don't dismiss the reading of such books just because "every experienced programmer knows about refactoring". I apologize if I gave that impression. I think books like these provide programmers with a head start on techniques and tools that they would otherwise have to spend years gathering from hands-on experience, usually from working with more experienced programmers. My point was that it is important to keep an open mind, to be able to recognize when they are being applied by people who may not share the same lexicon. The shared terms are important, but they are rarely as ubiquitous as, say, "cantilever" would be amongst civil engineers. This is a different problem. -Taj. ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] refactoring
On 12/4/2010 5:36 PM, Santosh Rajan wrote [snip] Let us look at software product development, and let us look at all the top open source software development projects like linux, apache, mozilla etc etc. I have not seen refactoring as described by the book used by any of the open source software products. I would like to know if any one can show me refactoring used in software product development. This reminds me of the kind of discussion I used to hear when the GoF Design Patterns book was published. Lacking experience, I was in awe of the book since it codified so much practice that I could look at and go THAT MAKES SO MUCH SENSE. It took years and a lot of experience to understand that this was just a compendium of best practices that many OOP programmers had been applying for decades, taking them so much for granted that they hadn't even bothered to give them names. Unfortunately there were too many people who never saw past it being some sort of bible, so (as an example) you'd have people drop by KDE dev mailing lists 5-6 years later complaining that the KDE libraries were poorly designed because they couldn't go through the code and identify the design patterns by name. This is a pattern that I've seen repeated often over the last twenty years (the Java world is particularly guilty of it). There is unquestionable value to the classification and codification of best practices in software engineering, which has a real paucity of good references of this kind, but it must be kept in perspective of what people are doing. Unfortunately, compared to other forms of engineering it's both more difficult and more given to fads. So what I'm saying is, it is unlikely that this book contains anything that any sufficiently large and long-lived software project (FOSS or otherwise) is not already doing, because it is critical to their success. You will just have to work a little harder to identify it because they may not have bought into your taxonomy of refactoring terms. [dubious aside: remember this one? There once was a master programmer who wrote unstructured programs. A novice programmer, seeking to imitate him, also began to write unstructured programs. When the novice asked the master to evaluate his progress, the master criticized him for writing unstructured programs, saying, ``What is appropriate for the master is not appropriate for the novice. You must understand the Tao before transcending structure.''There once was a master programmer who wrote unstructured programs. A novice programmer, seeking to imitate him, also began to write unstructured programs. When the novice asked the master to evaluate his progress, the master criticized him for writing unstructured programs, saying, ``What is appropriate for the master is not appropriate for the novice. You must understand the Tao before transcending structure.''] -Taj. ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] refactoring
[snip] [dubious aside: remember this one? Please note that there is no deeper meaning to be read into the fact that I mistakenly pasted the quote twice. -Taj. ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] Wall street may embrace Python
On 26-Apr-10, at 5:30 PM, Dhananjay Nene wrote: [snip] Taj, the main issue isn't whether standardisation necessary for DSLs is feasible - but the sheer amount of effort, time, political bickerings, and heat that accompanies it. If one uses python we save that entire diversion which if carried to its natural conclusion would be useful, but is just too expensive to carry to its natural conclusion. Oh I agree completely; as I wrote in my first response to this thread, this is a pragmatic solution that goes part of the way to the ideal. A halfway solution that actually gets used is better than a perfect one that never leaves the standards body. A similar situation that I was discussing recently is the credit card secure code solution that was adopted surprisingly smoothly - it's not a great solution but the ideal mechanism would have never been adopted. Still, it helps to be prepared for the eventuality of folks wanting more than these waterfall programs for exchange of financial contracts. When it happens, the prepared people are going to be ahead, like all the folks who patiently waited all this time for their favourite FP languages to become relevant. ;) -Taj. ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] Wall street may embrace Python
On 26-Apr-10, at 4:47 PM, Rajeev J Sebastian wrote: [snip] With all due respect, I disagree that a DSL is useful for this purpose. In fact, I would disagree with DSLs in most cases, especially if its supposed to be used for programming. The reason for this is that creating a good language is much more harder than creating a language, and such efforts tend to end up with crappy languages. Your concern is valid, but I think you are underestimating the amount of effort already being put into creating platform-neutral DSLs for business, law and finance. Hint: an XML schema is a declarative specification of a DSL, albeit one with terrible, human-antagonistic syntax. Even the SEC document contains such a thing already, as Anand quoted in an earlier email in this thread. Here are some other examples: http://www.oasis-open.org/committees/tc_cat.php?cat=lawgov http://www.legalxml.org/ http://www.service-architecture.com/xml/articles/finance_xml.html http://www.omg.org/technology/documents/br_pm_spec_catalog.htm What all these share in common is lack of human-friendly concrete syntax. That is the real problem to be solved, but it remains to be seen who will solve it and whether the vendors of eye-wateringly expensive middleware have the motivation to do so. -Taj. ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] Wall street may embrace Python
On 26-Apr-10, at 3:46 PM, Dhananjay Nene wrote: [snip] I think a DSL based contract (or more precisely waterfall specification) may be more concise and self descriptive. But that would require a definition of a new language grammar. However reasoning about the contracts is not in the scope of the SEC specification. The scope is (in my understanding) a clear communication of the how the waterfall implications are worked out (eg. how much does each stakeholder get paid and what are the conditions under which that gets decided) and at least in terms of standard programming languages Python does pretty well. Well the question I'm asking is, what are the implicit qualifications of the humans who are going to interpret these specifications? I see two profiles: 1) Financial, actuarial and legal experts with some programming experience. 2) Programming experts with financial, actuarial and legal experience. While there are people who fit both these profiles, there is a reason they get paid high-six figure USD salaries. Python alone is fine, but consider bog-standard recurring financial patterns like compound interest and graduated tax brackets. These imply functions that will occur often in these specifications, further implying (de- facto-)standard libraries containing highly domain-specific financial routines will be written. These are already embedded proto-DSLs! So you're not side-stepping DSLs by using python, only hiding them in plain sight. But you are not necessarily gaining the benefits of using an independent, well-specified declarative grammar the primary one being that you are possibly burdening non-programmers with general purpose programming constructs that are not their primary focus. There is a reason so many finance guys use R and (gah!) Excel to do their financial modelling - they are not interested in programming. Anyhow I'll stop here, I understand that the SEC requires only a subset of what I'm talking about, but the scope for these kinds of agreements goes well past the SEC and is something worth studying and implementing in its own right for fun and profit. -Taj. ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] Wall street may embrace Python
On 26-Apr-10, at 2:25 PM, Dhananjay Nene wrote: [snip] I do see one strong plus here for Python. That is a very natural language for expression (as in being one of the most readable programming languages for non programmers) without resorting to any specific DSLs etc. On the contrary, I think treating python as a DSL host (as some are implying here) is not such a great idea. In my experience the language is not so good at allowing small languages to be embedded without resorting to lisp-like nested lists/tuples. This is not a deal- breaker of course, and this decision to use Python is a sensible, pragmatic one (lots of python programmers around, financial/ statistical libraries are available and mature etc) but IMHO a more declarative language would have been nicer from a provability standpoint. Being able to write programs that reason about the contracts is very important and trying to do it for a general purpose language like python will be difficult. I think over time once smart contract research becomes more mature (there are already lots of research papers available) we'll see more declarative languages being used for this instead, with python becoming the de-facto support language to build tools around them. Meanwhile, I'll repeat something I say far too much but anyway: domain knowledge is just as important as programming chops. Folks who want to get in on the ground floor with these kinds of applications of python should start learning the vocabulary and process flows of finance ASAP. -Taj. ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] Any python metaclasses for UID
On 27-Jul-09, at 1:50 PM, Shivaraj M S wrote: [snip] Can there be a metaclass in python which can change the behaviour of object at creation stage by extending it and nullifying friend functions? (I must confess this is the only sentence in the email that I could comprehend...) If what you are looking for is a way to customize object creation, have a look at the __new__ class method. It is called before __init__, and can return _any_ object when the client code attempts to instantiate the class. In some cases I have dynamically generated a subclass of the class using type(), and returned a new instance of that. I think this is the kind of thing you are looking for. -Taj. ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
[BangPypers] Poor man's LWP with PyQt
Hi all, I've been trying to find the path of least resistance to using cooperative multitasking in PyQt apps. There are a bunch of different options out there, including Twisted (The initial inqternet.py Qt support I wrote became the basis of the current Qt reactor, and it works great if you are using Twisted) and Kamaelia/Axon (I had a stab at implementing Qt event loop support for that - available here: http://sirtaj.net/projects/axonqt.py). However most of these approaches require you to commit to frameworks that are likely to shape the implementation of the rest of your app. The two recommended ways to doing this sort of thing in Qt/C++ are: 1) use QTimer with a timeout of 0 to call some function. 2) In a long-running loop, call processEvents to allow other events to be processed to keep the GUI interactive. ...and that's all she wrote, since C++ doesn't really allow many options besides multithreading. With the yield keyword, however, we can get the same kind of cooperative multitasking that we had back in 1991 with Visual Basic 1.0 (yay!). This is the approach that Axon uses. Using the QTimer method above, a handful of lines of code gets us this in PyQt without having to use a larger framework: --- from PyQt4.QtCore import QObject, SIGNAL def qmicro(iterations=500): '''Qt fire-and-forget microprocess decorator. ''' def wrap_qmicro(microfn): def call_qmicro(qobj, *call_args, **call_kwargs): try: call_iter = microfn(qobj, *call_args, **call_kwargs) except StopIteration, endex: return except: raise return QtMicroProcess(qobj, call_iter.next, iterations) return call_qmicro return wrap_qmicro class QtMicroProcess(QObject): '''A single running microprocess, scheduled in the event loop using timer events until completed or error. ''' def __init__(self, parent, next_fn, iterations): QObject.__init__(self, parent) self.next_fn = next_fn self.iterations = iterations self.timer_id = self.startTimer(0) def timerEvent(self, tev): next_fn = self.next_fn try: for itidx in xrange(self.iterations): next_fn() return except StopIteration, sex: pass except Exception, ex: print "QMICRO: Unhandled exception:", ex try: self.killTimer(self.timer_id) finally: self.deleteLater() -- Now we can create "fire and forget" LWPs methods that can "yield" to the Qt event loop by simply using the @qmicro decorator: class MyApp(QObject): @qmicro() def beer(n): for x in xrange(n): print x, "bottles of beer on the wall" yield def some_regular_method(): beer(99) # returns immediately (a slightly more fleshed out example is in the attached file) Note that this is a deliberately simplistic implementation that has various limitations, eg the method has to be a method of a QObject subclass, and there is no builtin way to get feedback when the LWP exits. Still, it is a convenient bit of code you can drop into your PyQt project when you want to do some background work while allowing the rest of the app to continue relatively unaffected, while sidestepping the issues that come up when calling Qt code from multiple threads. Hope someone finds this useful and any feedback appreciated. -Taj. from PyQt4.QtCore import QObject, SIGNAL # def qmicro(iterations=500): '''Qt fire-and-forget microprocess decorator. The ''' def wrap_qmicro(microfn): def call_qmicro(qobj, *call_args, **call_kwargs): try: call_iter = microfn(qobj, *call_args, **call_kwargs) except StopIteration, sex: return except: raise return QtMicroProcess(qobj, call_iter.next, iterations) return call_qmicro return wrap_qmicro class QtMicroProcess(QObject): '''A single running microprocess, scheduled in the event loop using timer events until completed or error. ''' def __init__(self, parent, next_fn, iterations): QObject.__init__(self, parent) self.next_fn = next_fn self.iterations = iterations self.timer_id = self.startTimer(0) def timerEvent(self, tev): next_fn = self.next_fn try: for itidx in xrange(self.iterations): next_fn() return except StopIteration, sex: pass except Exception, ex: print "QMICRO: Unhandled exception:", ex try: self.killTimer(se
Re: [BangPypers] Python Bof @ Freed.in
On Wednesday 18 February 2009, Ramakrishna Reddy wrote: > Hi Folks > > I'm be proposing a Python Bof @ Freed.in. I know Sentil is coming. > Anyone else coming down ? I intend to be there on both days. -Taj. ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers