Re: Creating an iterator in a class
"Joseph L. Casale" wrote in message news:mailman.1346.1356619576.29569.python-l...@python.org... I am writing a class to provide a db backed configuration for an application. In my programs code, I import the class and pass the ODBC params to the class for its __init__ to instantiate a connection. I would like to create a function to generically access a table and provide an iterator. How does one create a function in a class that takes an argument and returns an iterator? I saw some examples where the class gets instantiated with the table defined but I was hoping not to do this so I could continue to access various tables through one connection/cursor. Thanks! jlc= Python makes it easy. Read up on "Generators", and the "yield" statement. Works very elegantly with a "for ... in" loop. -- http://mail.python.org/mailman/listinfo/python-list
Re: New to python, do I need an IDE or is vim still good enough?
"mogul" wrote in message news:ea058e5c-518f-4210-b80e-49ae2baab...@googlegroups.com... 'Aloha! I'm new to python, got 10-20 years perl and C experience, all gained on unix alike machines hacking happily in vi, and later on in vim. Now it's python, and currently mainly on my kubuntu desktop. Do I really need a real IDE, as the windows guys around me say I do, or will vim, git, make and other standalone tools make it the next 20 years too for me? Oh, by the way, after 7 days I'm completely in love with this python thing. I should have made the switch much earlier! /mogul %-) I'd say start with IDLE. I wouldn't exactly consider it an "IDE", but it gives you a decent Python-oriented editor. For me it handles 95% of what I need to do (for more ambitious projects, I use PyScripter on the Windows platform). -- http://mail.python.org/mailman/listinfo/python-list
Need to get Tags and Values from Dom
I have a very simple XML document that I need to "walk", and I'm using xml.dom.minidom. No attributes, just lots of nested tags and associated values. All I'm looking to do is iterate through each of the highest sibling nodes, check what the tag is, and process its value accordingly. If a node has children, same thing - iterate through the nodes, check the tags and process the values accordingly. I see where each node object has a "childNodes" attribute, so I can drill down the tree. But what are the node attributes which indicate Tag and Value? I thought it would have been nodeName and nodeValue, but that doesn't seem to be. Does anyone know? Thanks in advance, TommyVee -- http://mail.python.org/mailman/listinfo/python-list
Re: Need to get Tags and Values from Dom
"james hedley" wrote in message news:11852803.89.1337001575700.JavaMail.geo-discussion-forums@vbmd2... On Monday, 14 May 2012 01:50:23 UTC+1, TommyVee wrote: I have a very simple XML document that I need to "walk", and I'm using xml.dom.minidom. No attributes, just lots of nested tags and associated values. All I'm looking to do is iterate through each of the highest sibling nodes, check what the tag is, and process its value accordingly. If a node has children, same thing - iterate through the nodes, check the tags and process the values accordingly. I see where each node object has a "childNodes" attribute, so I can drill down the tree. But what are the node attributes which indicate Tag and Value? I thought it would have been nodeName and nodeValue, but that doesn't seem to be. Does anyone know? Thanks in advance, TommyVee Ah maybe you're confused about how text nodes work in minidom. Every element will have a nodeName attribute (not callable) but if you try el.nodeValue on a text node you get None. That's because the text is represented by a child node with nodeName '#text', so you want (el.nodeName, el.firstChild.nodeValue). General tips - try the docs: http://docs.python.org/library/xml.dom.minidom.html and also use dir() a lot on objects when you're learning a new api. Hope that helps. Disclaimer: haven't used minidom in anger for some time. Confused? That's an understatement. Part of the problem is that it's been a long time since I learned DOM and now I'm trying to cram to get this program done. Anyway, your suggestion to access el.firstChild.nodeValue did the trick. Thanks -- http://mail.python.org/mailman/listinfo/python-list
Generator Frustration
I'm using the SimPy package to run simulations. Anyone who's used this package knows that the way it simulates process concurrency is through the clever use of yield statements. Some of the code in my programs is very complex and contains several repeating sequences of yield statements. I want to combine these sequences into common functions. The problem of course, is that once a yield gets put into a function, the function is now a generator and its behavior changes. Is there any elegant way to do this? I suppose I can do things like ping-pong yield statements, but that solutions seems even uglier than having a very flat, single main routine with repeating sequences. Thanks in advance. -- http://mail.python.org/mailman/listinfo/python-list
Re: Generator Frustration
"Gregory Ewing" wrote in message news:95059efur...@mid.individual.net... Steven D'Aprano wrote: A nice piece of syntax that has been proposed for Python is "yield from", which will do the same thing, but you can't use that yet. Unless you're impatient enough to compile your own Python with my patch applied: http://www.cosc.canterbury.ac.nz/greg.ewing/python/yield-from/yield_from.html -- Greg Sounds like the "yield from" is the about this best solution. Thanks for all your help. -- http://mail.python.org/mailman/listinfo/python-list
Re: Generator Frustration
"Thomas Rachel" wrote in message news:isi5dk$8h1$1...@r03.glglgl.eu... Am 04.06.2011 20:27 schrieb TommyVee: I'm using the SimPy package to run simulations. Anyone who's used this package knows that the way it simulates process concurrency is through the clever use of yield statements. Some of the code in my programs is very complex and contains several repeating sequences of yield statements. I want to combine these sequences into common functions. Which are then generators. The problem of course, is that once a yield gets put into a function, the function is now a generator and its behavior changes. Isn't your "main" function a generator as well? Is there any elegant way to do this? I suppose I can do things like ping-pong yield statements, but that solutions seems even uglier than having a very flat, single main routine with repeating sequences. I'm not sure if I got it right, but I think you could emulate this "yield from" with a decorator: def subgen1(): yield 1; yield 2; def subgen2(): yield 1; yield 2; Instead of doing now def allgen(): for i in subgen1(): yield i for i in subgen2(): yield i you as well could do: def yield_from(f): def wrapper(*a, **k): for sub in f(*a, **k): for i in sub: yield i return wrapper @yield_from def allgen(): yield subgen1() yield subgen2() (Untested.) Thomas Yes, the main function is a generator. Give me a day or two to absorb your code. But in the meantime, perhaps a quick explanation of the SimPy package is in order. Also, here's the link if you're interested: http://simpy.sourceforge.net/simpy_overview.htm The way the package works is that you instantiate what I'll call an "actor" class, inheriting from one of the SimPy base classes. Once you instantiate the actor class(es), it it "registered" in SimPy. Then when you start the simulation, SimPy's main routine will begin to "dispatch" each of the actor classes by driving what they call a "PEM" method in the actor class. Within that method, you simulate the activity of the actor. The way that you tell SimPy that you are "busy", or that you want to request or release a resource, is with a yield statement. Here's a little example: from SimPy.Simulation import * svc1 = Resource(capacity=1) svc2 = Resource(capacity=1) class Customer(Process): def PEM(self): print now(), "I am starting..." print now(), "I am requesting service1" yield request, self, svc1 print now(), "I got service 1, now I'm going ask it to do 10 ticks worth of work" yield hold, self, 10 print now(), "Service 1's work is done, I am now releasing him" yield release, self, svc1 print now(), "Now I am requesting service2" yield request, self, svc2 print now(), "I got service 2, now I'm going ask him to do 5 ticks worth of work" yield hold, self, 5 print now(), "Service 2's work is done, I am now releasing him" yield release, self, svc2 print now(), "I am ending..." initialize() # Create a customer and "register" (activate) him to SimPy c = Customer() activate(c, c.PEM()) # Pass control to the SimPy main dispatch routine, and run the simulation for 100 ticks simulate(until=100) Note that when you do the yields, you actually send back information to the SimPy main routine. For example, when you do a "yield hold, self, 10", control will be yielded to SimPy, he will simulate the passage of 10 time ticks, and redispatch you (do a "next" on your PEM). Similar deal with the "yield request, self, svc1". When you yield to SimPy, he will attempt to obtain the resource for you, and if it is already taken by another actor, you will wait in a queue until released. At that time, SimPy will then redispatch you. Obviously, this is a trivial example, since it only involves the creation of a single actor. In a real simulation, there could be hundreds of these things running "concurrently", all vying for resources, holding them for varying amounts of time, etc. SimPy also uses yield statements to simulate other things too, like waiting for a signal from another actor. In the example you see above, note that I am "repeating" a generic sequence twice, e.g. "yield request, self, svcX", followed by "yield hold, self, time", followed by "yield release, self, svcX". In a workflow example, you may have literally dozens of services that you may hit in serial or parallel. Because of the generator "restriction", you'd have to code these three statements repeatedly in the PEM routine. It would be nice to
Algorithm for Creating Supersets of Smaller Sets Based on Common Elements
Start off with sets of elements as follows: 1. A,B,E,F 2. G,H,L,P,Q 3. C,D,E,F 4. E,X,Z 5. L,M,R 6. O,M,Y Note that sets 1, 3 and 4 all have the element 'E' in common, therefore they are "related" and form the following superset: A,B,C,D,E,F,X,Z Likewise, sets 2 and 5 have the element 'L' in common, then set 5 and 6 have element 'M' in common, therefore they form the following superset: G,H,L,M,O,P,Q,R,Y I think you get the point. As long as sets have at least 1 common element, they combine to form a superset. Also "links" (common elements) between sets may go down multiple levels, as described in the second case above (2->5->6). Cycles thankfully, are not possible. BTW, the number of individual sets (and resultant supersets) will be very large. I don't know where to start with this. I thought about some type of recursive algorithm, but I'm not sure. I could figure out the Python implementation easy enough, I'm just stumped on the algorithm itself. Anybody have an idea? Thanks, Tom -- https://mail.python.org/mailman/listinfo/python-list
Re: Algorithm for Creating Supersets of Smaller Sets Based on Common Elements
"TommyVee" wrote in message news:Bg5Gw.1344030$no4.494...@fx19.iad... Start off with sets of elements as follows: 1. A,B,E,F 2. G,H,L,P,Q 3. C,D,E,F 4. E,X,Z 5. L,M,R 6. O,M,Y Note that sets 1, 3 and 4 all have the element 'E' in common, therefore they are "related" and form the following superset: A,B,C,D,E,F,X,Z Likewise, sets 2 and 5 have the element 'L' in common, then set 5 and 6 have element 'M' in common, therefore they form the following superset: G,H,L,M,O,P,Q,R,Y I think you get the point. As long as sets have at least 1 common element, they combine to form a superset. Also "links" (common elements) between sets may go down multiple levels, as described in the second case above (2->5->6). Cycles thankfully, are not possible. BTW, the number of individual sets (and resultant supersets) will be very large. I don't know where to start with this. I thought about some type of recursive algorithm, but I'm not sure. I could figure out the Python implementation easy enough, I'm just stumped on the algorithm itself. Anybody have an idea? Thanks, Tom Thanks to all! Not only did you turn around my thinking on this, but I also learned about some new Python collection types. I think I have a way to get this to work now. -- https://mail.python.org/mailman/listinfo/python-list
Re: Easiest Way to Do Cross-Platform DB Access (Oracle)
"Gary Furash" wrote in message news:135759bf-0823-480c-9631-106d6cf1a...@googlegroups.com... I need to be able to access Oracle from both Windows and *nix, however, it seems kind of tortuous getting everything working each time on each server. With Java I can just drop (usually the same) JDBC library files in a location and everything works. I'm sure there's some easier way of doing this with Python! Enlighten me! (thanks) Did you take a look at this? http://code.google.com/p/pyodbc/ -- https://mail.python.org/mailman/listinfo/python-list