Inexplicable behavior in simple example of a set in a class
I have written two EXTREMELY simple python classes. One class (myClass1) contains a data attribute (myNum) that contains an integer. The other class (myClass2) contains a data attribute (mySet) that contains a set. I instantiate 2 instances of myClass1 (a & b). I then change the value of a.myNum. It works as expected. Then I instantiate 2 instances of myClass2 (c & d). I then change the value of c.mySet. Bizarrely changing the value of c.mySet also affects the value of d.mySet which I haven't touched at all!?!?! Can someone explain this very strange behavior to me? I can't understand it for the life of me. Please see below the source code as well as the output. -- SOURCE CODE -- import sets class myClass1: myNum = 9 def clearNum(self): self.myNum = 0 def __str__(self): return str(self.myNum) class myClass2: mySet = sets.Set(range(1,10)) def clearSet(self): self.mySet.clear() def __str__(self): return str(len(self.mySet)) if __name__ == "__main__": # Experiment 1. Modifying values of member integers in two different instances of a class # Works as expected. a = myClass1() b = myClass1() print "a = %s" % str(a) print "b = %s" % str(b) print "a.clearNum()" a.clearNum() print "a = %s" % str(a) print "b = %s\n\n\n" % str(b) # Experiment 2. Modifying values of member sets in two different instances of a class # Fails Unexplicably. d is not being modified. Yet calling c.clearSet() seems to change d.mySet's value c = myClass2() d = myClass2() print "c = %s" % str(c) print "d = %s" % str(d) print "c.clearSet()" c.clearSet() print "c = %s" % str(c) print "d = %s" % str(d) -- OUTPUT -- > python.exe myProg.py a = 9 b = 9 a.clearNum() a = 0 b = 9 c = 9 d = 9 c.clearSet() c = 0 d = 0 -- http://mail.python.org/mailman/listinfo/python-list
Re: Inexplicable behavior in simple example of a set in a class
> Instance variables are properly created in the __init__() > initializer method, *not* directly in the class body. > > Your class would be correctly rewritten as: > > class MyClass2(object): > def __init__(self): > self.mySet = sets.Set(range(1,10)) > > def clearSet(self): > # ...rest same as before... Thanks Chris. That was certainly very helpful!! So just out of curiosity, why does it work as I had expected when the member contains an integer, but not when the member contains a set? -- http://mail.python.org/mailman/listinfo/python-list
Need help with really elementary pexpect fragment
I want to write a pexpect script that simply cd's into a directory ("~/ install") and then runs a command from there. It should be so easy. But even my cd command is failing. Can't figure out what the problem is. The command line prompt is "[my machine name here] % " Here is the code fragment: print "Spawning Expect" p = pexpect.spawn ('/bin/tcsh',) print "Sending cd command" i = p.expect([pexpect.TIMEOUT, "%",]) assert i != 0, "Time-Out exiting" p.sendline("cd ~/install") Here is the output: Spawning Expect Sending cd command Time-Out exiting How could I be screwing something so simple up?? -- http://mail.python.org/mailman/listinfo/python-list
Re: Need help with really elementary pexpect fragment
Oops! Good call. Thank you. You pointed out my mistake. - Saqib On Tue, Dec 20, 2011 at 12:31 AM, Nick Dokos wrote: > Saqib Ali wrote: > > > > > I want to write a pexpect script that simply cd's into a directory ("~/ > > install") and then runs a command from there. It should be so easy. > > But even my cd command is failing. Can't figure out what the problem > > is. The command line prompt is "[my machine name here] % " > > > > Here is the code fragment: > > > > print "Spawning Expect" > > p = pexpect.spawn ('/bin/tcsh',) > > > > If you execute /bin/tcsh by hand, do you get a "%" prompt? > > Nick > > > print "Sending cd command" > > i = p.expect([pexpect.TIMEOUT, "%",]) > > assert i != 0, "Time-Out exiting" > > p.sendline("cd ~/install") > > > > Here is the output: > > > > Spawning Expect > > Sending cd command > > Time-Out exiting > > > > > > How could I be screwing something so simple up?? > > -- > > http://mail.python.org/mailman/listinfo/python-list > > > -- http://mail.python.org/mailman/listinfo/python-list
Can I get use pexpect to control this process?
I have a program X that constantly spews output, hundreds of lines per minute. X is not an interactive program. IE: it doesn't take any user input. It just produces a lot of textual output to STDOUT. I would like to save the output produced by X into a different file every 5 seconds regardless of the actual content. I want one file to contain the output from seconds 0-5, another file should contain 6-10, etc. etc. Can I do this with pexpect? I'm not sure I can because the "before" argument gives me EVERYTHING upto the most recent match. What I really need is something that will give me what was produced between the last 2 calls to pexpect.expect(). -- http://mail.python.org/mailman/listinfo/python-list
Can't I define a decorator in a separate file and import it?
I'm using this decorator to implement singleton class in python: http://stackoverflow.com/posts/7346105/revisions The strategy described above works if and only if the Singleton is declared and defined in the same file. If it is defined in a different file and I import that file, it doesn't work. Why can't I import this Singleton decorator from a different file? What's the best work around? -- http://mail.python.org/mailman/listinfo/python-list
Re: Can't I define a decorator in a separate file and import it?
MYCLASS.PY: #!/usr/bin/env python import os, sys, string, time, re, subprocess import Singleton @Singleton class myClass: def __init__(self): print 'Constructing myClass' def __del__(self): print 'Destructing myClass' SINGLETON.PY: #!/usr/bin/env python import os, sys, string, time, re, subprocess import Singleton @Singleton class myClass: def __init__(self): print 'Constructing myClass' def __del__(self): print 'Destructing myClass' TRACEBACK: >>> import myClass Traceback (most recent call last): File "", line 1, in File "myClass.py", line 6, in @Singleton TypeError: 'module' object is not callable - Saqib >> > Post the code, and the traceback. > > ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list
Re: Can't I define a decorator in a separate file and import it?
BTW Here is the traceback: >>> import myClass Traceback (most recent call last): File "", line 1, in File "myClass.py", line 6, in @Singleton TypeError: 'module' object is not callable Here is Singleton.py: class Singleton: def __init__(self, decorated): self._decorated = decorated def Instance(self): try: return self._instance except AttributeError: self._instance = self._decorated() return self._instance def __call__(self): raise TypeError( 'Singletons must be accessed through the `Instance` method.') Here is myClass.py: #!/usr/bin/env python import os, sys, string, time, re, subprocess import Singleton @Singleton class myClass: def __init__(self): print 'Constructing myClass' def __del__(self): print 'Destructing myClass' -- http://mail.python.org/mailman/listinfo/python-list
Re: Can't I define a decorator in a separate file and import it?
Thanks for pointing out the mistake! Works. - Saqib On Thu, Dec 22, 2011 at 4:31 PM, Ethan Furman wrote: > Saqib Ali wrote: > >> MYCLASS.PY: >> >> #!/usr/bin/env python >> import os, sys, string, time, re, subprocess >> import Singleton >> > > This should be 'from Singleton import Singleton' > > > > @Singleton >> class myClass: >>def __init__(self): >>print 'Constructing myClass' >> > > At this point, the *instance* of myClass has already been constructed and > it is now being initialized. The __new__ method is where the instance is > actually created. > > > >>def __del__(self): >>print 'Destructing myClass' >> >> >> > class Singleton: > > > > def __init__(self, decorated): > > self._decorated = decorated > > > > def Instance(self): > > try: > > return self._instance > > except AttributeError: > > self._instance = self._decorated() > > return self._instance > > > > def __call__(self): > > raise TypeError( > > 'Singletons must be accessed through the `Instance` > > method.') > > > ~Ethan~ > -- http://mail.python.org/mailman/listinfo/python-list
Changing the system clock with pexpect confuses pexpect!
See my code below. I'm controlling a shell logged in as root with pexpect. The class below has a method (startProc) which spawns a shell and keeps it alive until told to destroy it (stopProc). The other 2 methods in this class allow me to change the system clock and to get the IP Address of this machine. They all work fine except when I advance the system clock, and then try to get the IP Address. In that case, I get an exception because pexpect incorrectly thinks the output it is getting from ifconfig is invalid. But it is not. Pexpect is just confused. This doesn't happen when I move the clock backwards. It only happens when I move the clock forward. I believe what is going on is that internally pexpect uses the system clock to keep track of when it receives data from spawned processes. When I mess with the clock, that messes up the internal workings of pexpect. Any suggestions what I should do? I have numerous concurrent pexpect processes running when I modify the clock. Is there anyway to prevent them all from getting totally screwed up?? - #!/usr/bin/env python import pexpect, os, time, datetime, re def reportPrint(string): print string def reportAssert(condition, string) if condition == False: print string raise Exception class rootManager: rootProc = None rootPrompt = "] % " myPrompt = "] % " def __init__(self): pass def startProc(self): if self.rootProc != None: reportPrint("\t\t- Root Process is already created") else: self.rootProc = pexpect.spawn ('/bin/tcsh',) i = self.rootProc.expect([pexpect.TIMEOUT, self.myPrompt,]) reportAssert(i != 0, "Time-Out exiting") reportPrint("\t\t- Sending su") self.rootProc.sendline("su") i = self.rootProc.expect([pexpect.TIMEOUT, "Password: ",]) reportAssert(i != 0, "Time-Out exiting") reportPrint("\t\t- Sending Password") self.rootProc.sendline(ROOT_PASSWORD) i = self.rootProc.expect([pexpect.TIMEOUT, self.rootPrompt,]) reportAssert(i != 0, "Time-Out exiting") reportPrint("\t\t- Root Process created") def getIPAddr(self): reportAssert(self.rootProc != None, "No active Root Process!") reportPrint("\t\t- Sending ifconfig -a") self.rootProc.sendline("ifconfig -a") i = self.rootProc.expect([pexpect.TIMEOUT, self.rootPrompt,]) reportAssert(i != 0, "Time-Out exiting") outputTxt = self.rootProc.before ipList = [i for i in re.compile("(?<=inet )\d{1,3}\.\d{1,3}\. \d{1,3}\.\d{1,3}").findall(outputTxt) if i != "127.0.0.1"] reportAssert(len(ipList) == 1, "Cannot determine IP Address from 'ifconfig -a': \n%s" % outputTxt) return ipList[0] def changeClock(self, secondsDelta): reportAssert(self.rootProc != None, "No active Root Process!") newTime = datetime.datetime.now() + datetime.timedelta(seconds=secondsDelta) dateStr = "%02d%02d%02d%02d%s" % (newTime.month, newTime.day, newTime.hour, newTime.minute, str(newTime.year)[-2:]) reportPrint("\t\t- Sending 'date %s' command" % dateStr) self.rootProc.sendline("date %s" % dateStr) #Remember, by changing the clock, you are confusing pexpect's timeout measurement! # so ignore timeouts in this case i = self.rootProc.expect([pexpect.TIMEOUT, self.rootPrompt,],) def stopProc(self): if self.rootProc == None: reportPrint("\t\t- Root Process is already destroyed") else: reportPrint("\t\t- Sending exit command") rootProc.sendline("exit") i = rootProc.expect([pexpect.TIMEOUT, self.myPrompt]) reportAssert(i != 0, "Time-Out exiting") reportPrint("\t\t- Sending exit command") rootProc.sendline("exit") i = rootProc.expect([pexpect.TIMEOUT, pexpect.EOF]) reportAssert(i != 0, "Time-Out exiting") self.rootProc.close() self.rootProc = None reportPrint("\t\t- Root Process Destroyed") -- http://mail.python.org/mailman/listinfo/python-list
Problem while doing a cat on a tabbed file with pexpect
I am using Solaris 10, python 2.6.2, pexpect 2.4 I create a file called me.txt which contains the letters "A", "B", "C" on the same line separated by tabs. My shell prompt is "% " I then do the following in the python shell: >>> import pexpect >>> x = pexpect.spawn("/bin/tcsh") >>> x.sendline("cat me.txt") 11 >>> x.expect([pexpect.TIMEOUT, "% "]) 1 >>> x.before 'cat me.txt\r\r\nA B C\r\n' >>> x.before.split("\t") ['cat me.txt\r\r\nA B C\r\n'] Now, clearly the file contains tabs. But when I cat it through expect, and collect cat's output, those tabs have been converted to spaces. But I need the tabs! Can anyone explain this phenomenon or suggest how I can fix it? -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem while doing a cat on a tabbed file with pexpect
Very good question. Let me explain why I'm not opening me.txt directly in python with open. The example I have posted is simplified for illustrative purpose. In reality, I'm not doing pexpect.spawn("/bin/tcsh"). I'm doing pexpect.spawn("ssh myuser@ipaddress"). Since I'm operating on a remote system, I can't simply open the file in my own python context. On Jan 15, 2:24 pm, Dennis Lee Bieber wrote: > On Sun, 15 Jan 2012 09:51:44 -0800 (PST), Saqib Ali > > wrote: > >Now, clearly the file contains tabs. But when I cat it through expect, > >and collect cat's output, those tabs have been converted to spaces. > >But I need the tabs! > > >Can anyone explain this phenomenon or suggest how I can fix it? > > My question is: > > WHY are you doing this? > > Based upon the problem discription, as given, the solution would > seem to be to just open the file IN Python -- whether you read the lines > and use split() by hand, or pass the open file to the csv module for > reading/parsing is up to you. > > -=-=-=-=-=-=- > import csv > import os > > TESTFILE = "Test.tsv" > > #create data file > fout = open(TESTFILE, "w") > for ln in [ "abc", > "defg", > "hijA" ]: > fout.write("\t".join(list(ln)) + "\n") > fout.close() > > #process tab-separated data > fin = open(TESTFILE, "rb") > rdr = csv.reader(fin, dialect="excel-tab") > for rw in rdr: > print rw > > fin.close() > del rdr > os.remove(TESTFILE) > -=-=-=-=-=-=- > ['a', 'b', 'c'] > ['d', 'e', 'f', 'g'] > ['h', 'i', 'j', 'A'] > -- > Wulfraed Dennis Lee Bieber AF6VN > wlfr...@ix.netcom.com HTTP://wlfraed.home.netcom.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem while doing a cat on a tabbed file with pexpect
The file me.txt does indeed contain tabs. I created it with vi. >>> text = open("me.txt", "r").read() >>> print "\t" in text True % od -c me.txt 000 A \t B \t C \n 006 % ls -al me.txt -rw-r--r-- 1 myUsermyGroup 6 Jan 15 12:42 me.txt On Jan 15, 6:40 pm, Cameron Simpson wrote: > On 15Jan2012 23:04, Steven D'Aprano > wrote: > | On Sun, 15 Jan 2012 09:51:44 -0800, Saqib Ali wrote: > | > I am using Solaris 10, python 2.6.2, pexpect 2.4 > | > > | > I create a file called me.txt which contains the letters "A", "B", "C" > | > on the same line separated by tabs. > | [...] > | > Now, clearly the file contains tabs. > | > | That is not clear at all. How do you know it contains tabs? How was the > | file created in the first place? > | > | Try this: > | > | text = open('me.txt', 'r').read() > | print '\t' in text > | > | My guess is that it will print False and that the file does not contain > | tabs. Check your editor used to create the file. > > I was going to post an alternative theory but on more thought I think > Steven is right here. > > What does: > > od -c me.txt > > show you? TABs or multiple spaces? > > What does: > > ls -ld me.txt > > tell you about the file size? Is it 6 bytes long (three letters, two > TABs, one newline)? > > Steven hasn't been explicit about it, but some editors will write spaces when > you type a TAB. I have configured mine to do so - it makes indentation more > reliable for others. If I really need a TAB character I have a special > finger contortion to get one, but the actual need is rare. > > So first check that the file really does contain TABs. > > Cheers, > -- > Cameron Simpson DoD#743http://www.cskk.ezoshosting.com/cs/ > > Yes Officer, yes Officer, I will Officer. Thank you. -- http://mail.python.org/mailman/listinfo/python-list
Why does os.stat() tell me that my file-group has no members?
I'm using python 2.6.4 on Solaris 5-10. I have a file named "myFile". It is owned by someone else, by I ("myuser") am in the file's group ("mygrp"). Below is my python code. Why does it tell me that mygrp has no members??? >>> import os, pwd, grp >>> stat_info = os.stat("myFile") >>> fileUID = stat_info.st_uid >>> fileGID = stat_info.st_gid >>> fileGroup = grp.getgrgid(fileGID)[0] >>> fileUser = pwd.getpwuid(fileUID)[0] >>> print "grp.getgrgid(fileGID) = %s" % grp.getgrgid(fileGID) grp.getgrgid(fileGID) = grp.struct_group(gr_name='mygrp', gr_passwd='', gr_gid=100, gr_mem=[]) -- http://mail.python.org/mailman/listinfo/python-list
Re: Why does os.stat() tell me that my file-group has no members?
Thanks!! This was very helpful. It worked perfectly. I had no clue about the intricacies of how python represents the group data from the underlying OS. This page doesn't go into to detailed explanation like you did: http://docs.python.org/2/library/grp.html On Wednesday, December 19, 2012 6:17:16 PM UTC-5, Hans Mulder wrote: > On 19/12/12 22:40:00, saqib.ali...@gmail.com wrote: > > > > > > > > > I'm using python 2.6.4 on Solaris 5-10. > > > > > > I have a file named "myFile". It is owned by someone else, by > > > I ("myuser") am in the file's group ("mygrp"). Below is my python > > > code. Why does it tell me that mygrp has no members??? > > > > > > > > import os, pwd, grp > > stat_info = os.stat("myFile") > > fileUID = stat_info.st_uid > > fileGID = stat_info.st_gid > > fileGroup = grp.getgrgid(fileGID)[0] > > fileUser = pwd.getpwuid(fileUID)[0] > > print "grp.getgrgid(fileGID) = %s" % grp.getgrgid(fileGID) > > > > > > grp.getgrgid(fileGID) = grp.struct_group(gr_name='mygrp', gr_passwd='', > > gr_gid=100, gr_mem=[]) > > > > It doesn't say that your group has no members. > > > > Every account has a primary group, and some accounts also > > have addtional groups. The primary group is the one in the > > .pw_gid attribute in the pwd entry. The additional groups > > are those that mention the account in the .gr_mem attribute > > in their grp entry. > > > > Your experiment shows that nobody has "mygrp" as an additional > > group. So if you're a member of mygrp, then it must be your > > primary group, i.e. os.getgid() should return 100 for you. > > > > You can get a complete list of members of group by adding > > two lists: > > > > def all_members(gid): > > primary_members = [ user.pw_name > > for user in pwd.getpwall() if user.pw_gid == gid ] > > additional_members = grp.getgrgid(gid).gr_mem > > return primary_members + additional_members > > > > > > Hope this helps, > > > > -- HansM -- http://mail.python.org/mailman/listinfo/python-list
Why is pexpect acting funny with sendline() and expect()?
I am running Solaris 5-10, python 2.6.2 and pexpect 2.4 I have the very simple python script below which exercises the functionality of sending and receiving text from the shell. My understanding is that pexepect([pexpect.TIMEOUT, x,y,z], timeout=w) will return the index of the match that it found *since the last time pexpect was called*, but if it takes longer than w seconds, it will return 0. Here is my very simple script: #!/usr/bin/env python import pexpect myPrompt = " % " myShell = pexpect.spawn("/bin/tcsh") print "Sending 'JUNK-0' to shell" x = myShell.sendline("JUNK-0") y = myShell.expect([pexpect.TIMEOUT], timeout=1) print "y = %s" % y print myShell.before print "=" * 80 print "\n\n" for i in range(2): print "i = %d" % (i+1) print "Sending 'JUNK-%d' to shell" % (i+1) x = myShell.sendline("JUNK-%d" % (i+1)) y = myShell.expect([pexpect.TIMEOUT, myPrompt], timeout=10) print "y = %s" % y print myShell.before print "=" * 80 print "\n\n" FYI, my shell prompt is "myMachine % ", however in this script I have simply used " % " to keep it generic. When I run it, I see the following output: Sending 'JUNK-0' to shell y = 0 JUNK-0 myMachine % JUNK-0 JUNK-0: Command not found. myMachine % i = 1 Sending 'JUNK-1' to shell y = 1 JUNK-0 myMachine i = 2 Sending 'JUNK-2' to shell y = 1 JUNK-0 JUNK-0: Command not found. myMachine Why do I see "JUNK-0" consistently recurring in the output? It should be consumed by the first myShell.expect() statement, but it keeps showing up. Why?? -- http://mail.python.org/mailman/listinfo/python-list