Using ManagementClass.Getinstances() from IronPython - python

I have an IronPython script that looks for current running processes using WMI. The code looks like this:
import clr
clr.AddReference('System.Management')
from System.Management import ManagementClass
from System import Array
mc = ManagementClass('Win32_Processes')
procs = mc.GetInstances()
That last line where I call the GetInstances() method raises the following error:
Traceback (most recent call first):
File "<stdin>", line 1, in <module>
SystemError: Not Found
I am not understanding what's not being found?!? I believe that I may need to pass an instance of ManagementOperationObserver and of EnumerationOptions to GetInstance() however, I don't understand why that is, since the method with the signature Getinstance() is available in ManagementClass.

I think the only problem is that 'Win32_Processes' is a typo for 'Win32_Process'. This seems to work:
>>> mc = ManagementClass('Win32_Process')
>>> procs = mc.GetInstances()
>>> for p in procs:
... print p['Name']
...
System Idle Process
System
smss.exe
(etc)

Related

How to test `pickle`-ability in a `doctest`?

My actual application is that I'm trying to demo/test the "pickability" of some objects in a doctest, but doctest and pickle don't seem to play along well (in general, actually). Below is some minimal code to reproduce problem.
The following code works find if I run it in a console or a module.
>>> import pickle
>>> class A: ...
>>> unpickled_A = pickle.loads(pickle.dumps(A))
>>> assert unpickled_A == A
But if I include it as a doctest and run it, I'll get:
Traceback (most recent call last):
...
exec(compile(example.source, filename, "single",
File "<doctest __main__.foo[3]>", line 1, in <module>
unpickled_A = pickle.loads(pickle.dumps(A))
_pickle.PicklingError: Can't pickle <class '__main__.A'>: attribute lookup A on __main__ failed
How can I tell doctest to play nice with pickle, or (shame to have to do this though!) define my class so it'll play nicely with doctest+pickle?

Can't instantiate objects using cli python interpreter

Can't instantiate objects using python interpreter, please help.
So inside of my python file expresser.py I have something like
class Expresser:
def __init__(self):
pass
...
Now when I type in the terminal
python
and then
>>> import expresser
>>> test_object = Expresser()
I get:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'Expresser' is not defined
I'm using PyCharm
when I type where python I get three diff locations so I suspect that but don't know how to rectify
I guess you meant:
from expresser import Expresser
Or:
from expresser import *

multiprocessing and image aquisition [duplicate]

I am trying to the following code:
#!/usr/bin/python
import multiprocessing
def f(name):
print 'hello', name
if __name__ == '__main__':
p = multiprocessing.Process(target=f, args=('bob',))
p.start()
p.join()
The output I get is :
Traceback (most recent call last):
File "a.py", line 9, in <module>
p = multiprocessing.Process(target=f, args=('bob',))
AttributeError: 'module' object has no attribute 'Process'
You are trying to import multiprocessing from your local directory and not from the python library. The python interpreter first tries to import the module from the present directory. As you have got a file with the name multiprocessing.pyc in your directory, the interpreter is trying to import that. Hence you have got the error. Thus deleting multiprocessing.pyc will help resolve your problem.
Dont give the name of the file as "multiprocessing.py", give any other
thanks,
vybhav
The mistake was naming, my script as 'multiprocessing.py', once it was created. I made an another script with name 'a.py' and both of them were not working. After listing the directories, 'multiprocessing.pyc' was located. I deleted this file, and executed 'a.py' file which executed like gem!
thanks to #Bhargav Rao for highlighting
Don't give the name in the same directory as multiprocess.py, use different name instead.

Multithreading, can't run Process command

I am trying to the following code:
#!/usr/bin/python
import multiprocessing
def f(name):
print 'hello', name
if __name__ == '__main__':
p = multiprocessing.Process(target=f, args=('bob',))
p.start()
p.join()
The output I get is :
Traceback (most recent call last):
File "a.py", line 9, in <module>
p = multiprocessing.Process(target=f, args=('bob',))
AttributeError: 'module' object has no attribute 'Process'
You are trying to import multiprocessing from your local directory and not from the python library. The python interpreter first tries to import the module from the present directory. As you have got a file with the name multiprocessing.pyc in your directory, the interpreter is trying to import that. Hence you have got the error. Thus deleting multiprocessing.pyc will help resolve your problem.
Dont give the name of the file as "multiprocessing.py", give any other
thanks,
vybhav
The mistake was naming, my script as 'multiprocessing.py', once it was created. I made an another script with name 'a.py' and both of them were not working. After listing the directories, 'multiprocessing.pyc' was located. I deleted this file, and executed 'a.py' file which executed like gem!
thanks to #Bhargav Rao for highlighting
Don't give the name in the same directory as multiprocess.py, use different name instead.

Getting "ImportError: No module named" with parallel python and methods in a package

I'm trying to use parallel python in order to do some distributed benchmarking (essentially, coordinate and run some code on a set of machines from a central server). The code I had was working perfectly fine until I moved the functionality to a separate package. From then on, I keep getting ImportError: No module named some.module.pp_test.
My question is actually two-fold: has anyone ever came across this problem with pp, and if yes, how to solve it? I tried using dill (import dill), but didn't help. Also, is there a good replacement for parallelpython, that doesn't require any additional infrastructure?
The exact error I get is:
RUNNING TEST
Waiting for hosts to finish booting....A fatal error has occured during the function execution
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/ppworker.py", line 86, in run
__args = pickle.loads(__sargs)
ImportError: No module named some.module.pp_test
Caught exception in the run phase 'NoneType' object is not iterable
Traceback (most recent call last):
File "test.py", line 5, in <module>
p.ping_pong()
File "/home/ubuntu/workspace/pp-test/some/module/pp_test.py", line 5, in ping_pong
a_test.run()
File "/home/ubuntu/workspace/pp-test/some/module/pp_test.py", line 27, in run
pong, hostname = ping()
TypeError: 'NoneType' object is not iterable
The code is structured this way:
pp-test/
test.py
some/
__init__.py
module/
__init__.py
pp_test.py
The test.py is implemented as:
from some.module.pp_test import MWE
p = MWE()
p.ping_pong()
While pp_test.py is:
class MWE():
def ping_pong(self):
print "RUNNING TEST "
a_test = PPTester()
a_test.run()
import pp
import time
from sys import stdout, exit
class PPTester(object):
def run(self):
try:
ppservers = ('10.10.10.10', )
time.sleep(5)
job_server = pp.Server(0, ppservers=ppservers)
stdout.write("Waiting for hosts to finish booting...")
while len(job_server.get_active_nodes()) - 1 < len(ppservers):
stdout.write(".")
stdout.flush()
time.sleep(1)
ppmodules = ()
pings = [(server, job_server.submit(self.run_pong, modules=ppmodules)) for server in ppservers]
for server, ping in pings:
pong, hostname = ping()
print "Host ", hostname, " is alive!"
print "All servers booted up, starting benchmarks..."
job_server.print_stats()
except Exception as e:
print "Caught exception in the run phase", e
raise
pass
def run_pong(self):
import subprocess
p = subprocess.Popen("hostname", stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
(output, err) = p.communicate()
p_status = p.wait()
return "pong ", output
dill won't work with pp out of the box, because pp doesn't serialize the python objects -- pp extracts the object's source code (like the inspect module in the standard python library).
To enable pp to use dill (actually dill.source, which is inspect augmented by dill), you have to use a fork of pp called ppft. ppft installs as pp (i.e. imports with import pp), but it has much stronger source inspection, so you can automatically "serialize" most python objects and have ppft track down their dependencies automatically.
Get ppft here: https://github.com/uqfoundation
ppft is also pip installable and python 3.x compatible.

Categories