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

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?

Related

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 *

An idea of importing an user define python package

The structure needs for python 3.8+
That x.py contains x class with a 'display' method
concept one:
from p_abcd import a as A
''' call display '''
A.x.x().display()
Got an error
Traceback (most recent call last):
File "w.py", line 4, in
A.x.x().display()
AttributeError: module 'p_abcd.a' has no attribute 'x'
concept two:
import p_abcd.a as A
''' call display '''
A.x.x().display()
Got the same error
You can import only .py files. Not folders.
So you need something like
from p_abcd.a import x
x.display()

User defined modules storage

I am pretty much new to Python and cannot quite understand the scenario below.
Firstly, there exists no module so the error below is understandable.
>>> import module1
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import module1
ImportError: No module named module1
Then I created the module and the sentences below worked perfectly.
module1.py
def func():
print 'this is a test module'
Python IDLE
>>> import module1
>>> module1.func()
this is a test module
On the execution of the first statement, module1.pyc file was created. Then I added another function to module1.py.
module1.py
def func():
print 'this is a test module'
def func1():
print 'this is func1'
and tried to import this new file and run the statements below. But error was thrown as follows.
Python IDLE
>>> import module1
>>> module1.func1()
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
module1.func1()
AttributeError: 'module' object has no attribute 'func1'
I then deleted module1.py from the folder and then re-saved module1.py (with no contents changed). Again I executed the above statements in the IDLE prompt and the same error was thrown. And this time when the import module1 statement was executed, module1.pyc file wasn't created unlike the previous time.
However, on restarting the IDLE everything works perfectly but why doesn't the IDE recompile the module1.pyc everytime import statement is executed(without restarting or opening another IDLE window) or could anyone explain what exactly happens in the memory when import statement is executed everytime.
Thanks!!
If you import a module that has already been imported, it will not "re-import" and you will just get a reference to the original module.
You should use reload to re-import the module if it has been already imported:
reload(module1)

Why does my code work from interactive shell, but not when run from a file?

I am trying to use the pprint module to check out some vars in Python, which I can happily do using the interactive shell and the code below:
import pprint
pp = pprint.PrettyPrinter()
stuff = ['cakes','bread','mead']
pp.pprint(stuff)
However, when I put the above into pprint.py and run it using python pprint.py I get the error:
$ python dev/pars/pprint.py
Traceback (most recent call last):
File "dev/pars/pprint.py", line 1, in ?
import pprint
File "/home/origina2/dev/pars/pprint.py", line 2, in ?
pp = pprint.PrettyPrinter()
AttributeError: 'module' object has no attribute 'PrettyPrinter'
What is different about the way modules are called when running Python code from a file compared to the interactive shell?
You named your program pprint.py, so at the line import pprint it tries to import itself. It succeeds, but your pprint.py doesn't contain anything called PrettyPrinter.
Change your code's name. [And, to be clear, delete any pprint.pyc or pprint.pyo files..]

Using ManagementClass.Getinstances() from IronPython

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)

Categories