subprocess.Popen on Windows
playing with subprocess.Popen on Windows I stumbled into the following problem: Python 2.4.3 (#69, Mar 29 2006, 17:35:34) IDLE 1.1.3 >>> import subprocess >>> p1=subprocess.Popen("c:\\asd.bat") #works OK >>> p2=subprocess.Popen("c:\\asd.bat",stdout=subprocess.PIPE) Traceback (most recent call last): File "", line 1, in -toplevel- p2=subprocess.Popen("c:\\asd.bat",stdout=subprocess.PIPE) File "C:\Python24\lib\subprocess.py", line 533, in __init__ (p2cread, p2cwrite, File "C:\Python24\lib\subprocess.py", line 593, in _get_handles p2cread = self._make_inheritable(p2cread) File "C:\Python24\lib\subprocess.py", line 634, in _make_inheritable DUPLICATE_SAME_ACCESS) TypeError: an integer is required >>> What do I do wrongly? -- http://mail.python.org/mailman/listinfo/python-list
Re: Python database access
arvind wrote: > Hi all, > I am going to work on Python 2.4.3 and MSSQL database server on > Windows platform. > But I don't know how to make the connectivity or rather which module to > import. > I searched for the modules in the Python library, but I couldn't find > which module to go for. > Please help me out! i suggest pymssql. It doesnt throw runtime error while executing a query where prepackaged odbc module does. -- http://mail.python.org/mailman/listinfo/python-list
How to create a limited set of instanceses of a class
For the pure theory sake and mind expansion i got a question. Experimenting with __new__ i found out how to create a singleton. class SingleStr(object): def __new__(cls,*args,**kwargs): instance=cls.__dict__.get('instance') if instance: return instance cls.instance=object.__new__(cls,*args,**kwargs) return cls.instance What if i need to create no more than 5 (for example) instances of a class. I guess that would be not much of a problema but there is a complication. If one of the instances is garbagecollected I want to be able to spawn another instance. Maybe it's difficult to perceive what i said so think of a limited number of sockets. Wherenever one is freed it can be captured again. I believe that this type of the design pattern was described in "Thinking in Java" but there was a special method finalize() utilized, that is called every time when gc() is about to clean up an unreferenced object. By using this method a counter of active instances could be easily implemented. What is the pythonic approach to this problema? -- http://mail.python.org/mailman/listinfo/python-list
Re: How to create a limited set of instanceses of a class
Thanks Alex and Scott for your lead. It would've taken me forever trying to figure it out by myself :) I am affraid I didn't specify initially one thing and that led to a confusion: there is no need to pick an instance from the weakref dictionary, just return None if there are already 5 instances. But on the other hand if a hardref to an object was deleted, it's place can be taken by another one. Here's what i mean (and where the problem is): #build a list of 5 elements instList=[] for i in range(7): ainst=A() if ainst: instList.append(ainst) for i in range(5): instList.remove(instList[0]) #here the hardref is deleted ainst=A() while not ainst: #make shure that ainst is not NoneType gc.collect() time.sleep(1) #wait 1 sec for gc() to clean the memory ainst=A() #try again instList.append(ainst) #new object added the priblem is that ~3 out of 10 times the test part run into infinite loop because of unsatisfied condition (while not ainst:) - memory cannot be freed therefore new instance of A isn't permitted. #!/usr/bin/env python import weakref,random import types,gc import time class Limited5(object): __weakrefdict=weakref.WeakValueDictionary() def __new__(cls,*args,**kwargs): if len(cls.__weakrefdict)<5: instance=super(Limited5,cls).__new__(cls,*args,**kwargs) cls.__weakrefdict[id(instance)]=instance return instance #return random.choice(cls.__weakrefdict.values()) return None #no need to pick an instance class A(Limited5): counter=0 def __init__(self): self.instCounter=self.counter A.counter+=1 def getId(self): return id(self) if __name__=="__main__": instList=[] # populate the initial list of objects #make shure that there are only 5 elements for item in range(7): ainst=A() if hasattr(ainst,"getId"): print ainst.getId()," -- ",ainst.instCounter instList.append(ainst) print "-" #delete and recreate an arbitrary element in the instList for i in range(len(instList)): instList.remove(instList[random.choice(range(len(instList)))]) ainst=A() while not ainst:#here is an unstable part ainst=A() #sometimes the loop becomes infinite print gc.collect() #decpite the explicit call for gc() to start time.sleep(1) print "*", instList.append(ainst) for item in instList: print item.getId()," -- ",item.instCounter #print "---> ",item print "" -- http://mail.python.org/mailman/listinfo/python-list
Re: How to create a limited set of instanceses of a class
Thanks, Alex, again. The lesson has been taught. I appreciate very much you spent time trying to help. Indeed the culprit of that infrequent infinite loops was that bound reference "item" in the printing loop. But frankly i thought that it only existed inside that loop. Apparently I was wrong and glad that it was revealed. I guess unless there is something to add here the problem is solved and subject is closed. Thanks everyone who took their time to ponder on it. I hope it was as much educational for you as it was for me. -- http://mail.python.org/mailman/listinfo/python-list
Very practical question
I've been doing an application with Tkinter widgets. Nothing really fancy just routine stuff. Though I have no problems with it by now I guess it would be reasonable to ask about a thing that's been bothering me a bit. Look at this piece of code: class A(object): def a(self): return "a from A" class B(object): def interClassCall(self): print globals()['c'].__dict__['a'].a() class C(object): def __init__(self): self.a=A() self.b=B() def c(self): self.b.interClassCall() if __name__=="__main__": c=C() c.c() What is another way to get data from method of another instance of a class? Or maybe print globals()['c'].__dict__['a'].a() is perfectly normal. I need your professional expertise. -- http://mail.python.org/mailman/listinfo/python-list
Re: Very practical question
Marc 'BlackJack' Rintsch wrote: > In <[EMAIL PROTECTED]>, madpython > wrote: > > > No it's not the normal way. Why don't you give `c` as argument to the > `interClassCall()`? > > class B(object): > def interClassCall(self, c): > print c.a.a() > > class C(object): > def __init__(self): > self.a=A() > self.b=B() > def c(self): > self.b.interClassCall(self) > > Much less magic involved this way. As far as I remember what you suggest would be perfect solution for Java. In other words it's a matter of getting a reference to "a" (the instance of class A) and your variant just one of the others possible. Maybe I came up with not very good example, so maybe I try to explain it verbally. As I already have written I play with Tkinter and make an application that does some stuff. It uses dynamic GUI building depending on data it process. Also one of the requriments is to separate the program logic from GUI. Here is a short illustration: ... self.b=Tkinter.Button(root,txt="Button",command=self.doSmth).pack() self.l=Tkinter.Label(root,txt="default").pack() def doSmth(self): var=globals()["m"].__dict__["progLogic"].func("some input") self.l.config(txt=var) self.l.update_idletasks() ... I guess it's all correct or at least it close to what I work on. What do you think? If I may I'd say it again that GUI is built according by the data that's passed by the "thinking" part of the program so I don't necessary know what it is (can only guess) and that's why passing references as an argument doesn't seem possible. -- http://mail.python.org/mailman/listinfo/python-list
Re: Very practical question
Thank you all for your comments. They are priceless beyond any doubt. As for the matter of the discussion it took me only a minute looking at the code to realize that with Tkinter I pass "master" reference to every widget and therefore I can access every method in the class hierarchy. I'm a fool that I haven't noticed it earlier. -- http://mail.python.org/mailman/listinfo/python-list
Re: Tkinter and dialogs
dwelch91 wrote: > I'm trying unsuccessfully to do something in Tk that I though would be > easy. It is easy. > The basic idea is that my application will consist of a series of modal > dialogs, that are chained together in "wizard" fashion. Didn't have time to get into the code you posted. Just think that the solution I use might be of some help. #!/usr/bin/env python import Tkinter class PrevNextPane(Tkinter.Frame): def __init__(self,master): self.master=master Tkinter.Frame.__init__(self,master) self.prvBtn=Tkinter.Button(self,text="Prev",command=self.do_prev).grid(row=0,column=0) self.nxtBtn=Tkinter.Button(self,text="Next",command=self.do_next).grid(row=0,column=1) def do_next(self): self.master.paneNumber+=1 self.master.displayPane() def do_prev(self): self.master.paneNumber-=1 self.master.displayPane() class Pane0(Tkinter.Frame): def __init__(self,master): Tkinter.Frame.__init__(self,master) for i in range(5): Tkinter.Entry(self).grid(row=i,column=0) class Pane1(Tkinter.Frame): def __init__(self,master): Tkinter.Frame.__init__(self,master) for i in range(5): Tkinter.Label(self,text="Label %s"% i).grid(row=i,column=0) class Pane2(Tkinter.Frame): def __init__(self,master): Tkinter.Frame.__init__(self,master) for i in range(5): Tkinter.Button(self,text="BtnPane2-%s"% i).grid(row=i,column=0) class Wizard(Tkinter.Tk): def __init__(self): Tkinter.Tk.__init__(self) self.topPane=None self.prevNextPane=PrevNextPane(self).pack(side=Tkinter.BOTTOM) self.paneNumber=0 self.displayPane() def displayPane(self): if self.topPane!=None: self.topPane.forget() try: self.topPane=globals()["Pane%s"% self.paneNumber](self) except KeyError: pass self.topPane.pack(side=Tkinter.TOP) if __name__=="__main__": w=Wizard() w.mainloop() -- http://mail.python.org/mailman/listinfo/python-list