[Tutor] resetting the python interpreter through manipulating globals()

2007-02-08 Thread Yi Qiang
Hi,
I have a program that talks to a python interpreter through pexpect  
(don't bother asking why ;).  What I would like to do is occasionally  
reset the interpreter to the state it would be in if it had just  
been launched.  I assumed I could simply clear out the globals()  
dictionary, minus '__builtins__' and some other important stuff, and  
recreate the globals.  But even a simple attempt has failed quite badly:

def reset_interpreter():
 rThis method will reset a python interpreters globals  
dictionary with
 the one provided as the input parameter.
 

 # This will just try to delete all non important keys in globals
 for k in globals().keys():
 print k
 if k == '__builtins__':
 continue
 else:
 try:
 del globals()[k]
 del locals()[k]
 except:
 continue
 print globals()

This does not seem to work at all, any suggestions?


Yi



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] hardware specs from python on OSX

2006-11-07 Thread Yi Qiang
Hi,
I am trying to get some basic information about the computer's
hardware specs in OSX in python.  In linux I can get most of what I
need from the /proc filesystem.  Is there an equivalent in OSX?  If
not, where else can I get information about the system from?  I need
CPU model, # of cpus, cpu speed, physical memory, etc.

Thanks,
Yi
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] problem with unpickling an object

2006-06-05 Thread Yi Qiang
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Hi list,
I am pickling a dictionary that has as one of it's values an object I
create on the fly.  When I try to unpickle that object, cPickle attempts
to recreate that object but of course that module is not present
anymore.  How can I just make it skip over that particular key and just
set the value to None?

Thanks,
- --
Yi Qiang ([EMAIL PROTECTED])
Web: http://www.yiqiang.net
Tel: (206) 661-8469
PGP: 0xB515564B
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.2.2 (GNU/Linux)

iD8DBQFEg8k0tXlIMrUVVksRAt2tAJ4hq/3KmDo6BbS3p3AwhKf4Ga52DQCfXFtx
GERJqs3IcLXuKaRcq/zuRUE=
=SEZ6
-END PGP SIGNATURE-
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] problem with unpickling an object

2006-06-05 Thread Yi Qiang
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Kent Johnson wrote, On 06/05/2006 03:12 AM:
  From a quick read of the docs, it looks like you should set the 
 unpickler's find_global attribute to a function you define. This 
 function can first attempt to load the desired class (see 
 pickle.find_class() for an example). If the load fails, then return a 
 placeholder class that you define.
 
 This won't give exactly the result you ask for, instead of None in the 
 dictionary you will have instances of the placeholder. You could then 
 post-process the dict to change the placeholder instances to None if you 
 like.

Interesting, so if I used this method I won't be able to use cPickle
anymore.  The documentation just says that cPickle is faster, but can
anyone quantify how much speed I am losing if I move to the regular
pickle module?


- --
Yi Qiang ([EMAIL PROTECTED])
Web: http://www.yiqiang.net
Tel: (206) 661-8469
PGP: 0xB515564B
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.2.2 (GNU/Linux)

iD8DBQFEhDw0tXlIMrUVVksRApzBAJ427uRcMuQk6TgqGfWGZu8GjlZWBgCbBTBP
83oVZ9NNswfGrzk58Bat57k=
=J5gs
-END PGP SIGNATURE-
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] problem with unpickling an object

2006-06-05 Thread Yi Qiang
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Kent Johnson wrote, On 06/05/2006 07:25 AM:
 No, the find_global() hook is for cPickle. See the next-to-last 
 paragraph on this page:
 http://docs.python.org/lib/pickle-sub.html
Sorry, I don't follow.  How does one get access to the find_global
attribute for cPickle?  I created an cPickle.Unpickler object, and the
only attributes that I have access to are .load and .noload.  What am I
missing here?

- --
Yi Qiang ([EMAIL PROTECTED])
Web: http://www.yiqiang.net
Tel: (206) 661-8469
PGP: 0xB515564B
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.2.2 (GNU/Linux)

iD8DBQFEhJKWtXlIMrUVVksRAhCYAJ9NFNc601e0QdqUAWOFi1yUrKSvNQCdE/eE
NJGCtVG6kYx89bsuLnCmIeo=
=CRYn
-END PGP SIGNATURE-
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] problem with unpickling an object

2006-06-05 Thread Yi Qiang
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Yi Qiang wrote, On 06/05/2006 01:22 PM:
 Kent Johnson wrote, On 06/05/2006 07:25 AM:
 No, the find_global() hook is for cPickle. See the next-to-last 
 paragraph on this page:
 http://docs.python.org/lib/pickle-sub.html
 Sorry, I don't follow.  How does one get access to the find_global
 attribute for cPickle?  I created an cPickle.Unpickler object, and the
 only attributes that I have access to are .load and .noload.  What am I
 missing here?
Ok, it looks I have to create that attribute.  Now, the next confusing
part is that the documentation says: 'If it [find_global] is None then
any attempts to unpickle instances will raise an UnpicklingError.'  What
exactly is considered an instance in this context?  I tried unpickling a
dictionary, and it unpickled just fine.  Please, someone explain :)
- --
Yi Qiang ([EMAIL PROTECTED])
Web: http://www.yiqiang.net
Tel: (206) 661-8469
PGP: 0xB515564B
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.2.2 (GNU/Linux)

iD8DBQFEhJUOtXlIMrUVVksRAm8lAJwI1IKZlwiXDRVC+1c73c+ieOmYewCghceY
F8Pqth3M/1ufF3hdkPufHv0=
=Uavt
-END PGP SIGNATURE-
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] crontab or python mistake

2006-05-25 Thread Yi Qiang
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Daniel McQuay wrote, On 05/25/2006 09:24 PM:
 Traceback (most recent call last):
  File /root/scripts/boxster_school_smtp.py, line 19, in ?
mssg = open('mssg.txt', 'r').read()
 IOError: [Errno 2] No such file or directory: ' emmssg.txt'
 /snip
Right, but I don't think the CWD for cron is in /root.  Try giving it an
absolute path.

- --
Yi Qiang ([EMAIL PROTECTED])
Web: http://www.yiqiang.net
Tel: (206) 661-8469
PGP: 0xB515564B
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.2.2 (GNU/Linux)

iD8DBQFEdofJtXlIMrUVVksRAhZOAJ9Gr8RPjS37u7IEaLdZMJjawvxMDgCghTCm
ub7rd1v+rrEAt2vTIpnwk4A=
=xhDj
-END PGP SIGNATURE-
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] question regarding subprocess.Popen

2006-05-09 Thread Yi Qiang
Hi list,
I am launching another python program in my python daemon like this:

 pobj = subprocess.Popen(tlsmd,
 stdin = subprocess.PIPE,
 stdout = subprocess.PIPE,
 stderr = subprocess.STDOUT,
 close_fds = True,
 bufsize = 0)

I want to detect when the process has exited abnormally (or when I 
ctrl+c it)so I thought I could use pobj.returncode.  However after 
fiddling with it for a while my KeyboardInterrupt does not seem to be 
sent to pobj (i tried re-raising a KeyboardInterrupt there as well) nor 
did testing for pobj.returncode yield any results.
Any ideas?

Thanks
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] decimal floating point?

2006-05-02 Thread Yi Qiang
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

michel maho wrote, On 05/02/2006 07:33 AM:
 To all
 I have the following calculation:
 
 Je Body Mass Index is,gewicht/lengte**2
 
 The result is a (digital?)floating point figure with with 10 decimal 
 numbers. For =
 example 27.2345678487
 Did I mis something? I would like to reduce it to one or two decimal =
 numbers.(27.2) but round(_,1) does not work.
What exactly doesn't work?

 n = 27.2345678487
 round(n)
27.0
 round(n, 2)
27.23

Yi
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.2.2 (GNU/Linux)

iD8DBQFEV3C7tXlIMrUVVksRAgBGAJ963EK8jkngIr53NNuT1e1NvoU97gCffslK
W/+Q4+/A2hoq3VXC0pR3J+Y=
=3RAJ
-END PGP SIGNATURE-
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor