Call C# DLL API from python - python

I got some C# DLL file with methods inside it and I'm trying to use it from python:
import sys
sys.path.append(r"C:\Users\ssandler\Documents\Python_projects\CLR_TEST")
import clr
clr.AddReference(r"C:\Users\ssandler\Documents\Python_projects\CLR_TEST\CAPLIB.DLL")
from CAPLIB import CapLib
cl = CapLib()
cl.CAP_ClearCsrRegister()
but get the folowing error:
Traceback (most recent call last):
File "C:/Users/ssandler/Documents/Python_projects/CLR_TEST/test.py", line 14, in
cl.CAP_ClearCsrRegister()
System.NullReferenceException: Object reference not set to an instance of an object.
at CAPLIB.CapLib.CAP_ClearCsrRegister()
this method exists in that class:
.NET reflector capture
Any ideas?

Related

python reload module for beginner. importlib.reload doesn't seem to work

I have a file called skdb and class called skmysqldb. I am trying to force reload.
I tried reloading "skdb", "skdb.skmysqldb" "skmysqldb" and none of them seem to work.
>>> from skdb import skmysqldb
>>> importlib.reload(skdb.skmysqldb)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'skdb' is not defined
>>> importlib.reload(skmysqldb)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\importlib\__init__.py", line 139, in reload
raise TypeError("reload() argument must be a module")
TypeError: reload() argument must be a module
>>> importlib.reload(skdb)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'skdb' is not defined
When you import some object using the from <module> import <obj> syntax as in
from skdb import skmysqldb
the module itself is not added to the current namespace, hence why you get a NameError when you try to do reload(skdb).
Instead try:
import skdb
importlib.reload(skdb)
Be cautious when using reload. If the module your reload imports other modules, those modules are not reloaded recursively, so depending on the exact code you can wind up in a rather broken state where it's better to just restart the whole interpreter.
I don't think this is supported, but try doing del sys.modules['mymodule'] for everything that vaguely matches. To find relevant ones, try something like [x for x in sys.modules if 'mymodule' in x].

How do I set the Engine used by ExecJS to NodeJS?

I'm trying to use the following package: https://github.com/Anorov/cloudflare-scrape/blob/master/cfscrape/init.py
I have the following test file:
import cfscrape
import sys
import execjs
who = sys.argv[1]
scraper = cfscrape.create_scraper()
print scraper.get(who).content
Which is outputting the following error:
Traceback (most recent call last): File "test.py", line 1, in import cfscrape File "build/bdist.macosx-10.10-intel/egg/cfscrape/__init__.py", line 17, in EnvironmentError: Your Javascript runtime 'JavaScriptCore' is not supported due to security concerns. Please use Node.js, V8, or PyV8.
Can anyone point me in the right direction to change my JS_ENGINE from JavaScriptCore to NodeJs

failing to import ctypes on windows 7

Any ideas why do I get this errorת while trying import ctypes:
>>> from ctypes import *
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
from ctypes import *
File "C:/Python27\ctypes.py", line 3, in <module>
libc = ctypes.windll.msvcrt
AttributeError: 'module' object has no attribute 'windll'
"C:/Python27\ctypes.py" indicates you have some stray ctypes.py in that path that is loaded instead of the standard library ctypes module (which should be Lib/ctypes/__init__.py essentially)
That file tries to load ctypes (again), ending up loading itself (more or less).
Remove/Rename that file.

Import * include submodules

I have a directory structure that looks like this:
scripts/
__init__.py
filepaths.py
Run.py
domains/
__init__.py
topspin.py
tiles.py
hanoi.py
grid.py
I would like to say:
from scripts import *
and get the stuff that is in filepaths.py but also get the things that are in hanoi.py
The outer __init__.py contains:
__all__ = ['filepaths','Run','domains','hanoi']
I can't figure out how to get the inner files to be included in that list. Putting hanoi by itself gets this error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'hanoi'
Putting domains.hanoi gets this error message:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'domains.hanoi'
The last reasonable guess I could come up with is putting scripts.domains.hanoi which gets this error message:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'scripts.domains.hanoi'
How do you get the all list to include things that are in subdirectories?
In scripts/__init__.py, before the __all__ add the following
from domains import topspin, tiles, hanoi, grid
This will add those modules to the namespace, and you will be able to import them with
from scripts import *
Note
As a soapbox, it is preferred to do things like
from scripts import topspin, tiles, hanoi, grid, filepaths, Run
over
from scripts import *
because 6 months from now, you might look at hanoi on the 400th line of code and wonder where it came from if you use the * import style. By explicitly showing what is imported from scripts it serves as a reminder where things come from. I'm sure that anyone trying to read your code in the future will thank you.
Import them first, in the __init__ files.
In scripts/__init__.py, import at least domains, and in scripts/domains/__init__.py import hanoi, etc. Or import domains.hanoi directly in scripts/__init__.py.
Without importing these, the scripts/__init__.py module has no reference to the nestend packages.

Printing docstrings in Python 3

How do you print doc strings in python 3.1.x?
I tried with the re and sys modules as a test and I keep getting errors. Thanks
import re
print(re._doc_)
Traceback (most recent call last):
File "<pyshell#91>", line 1, in <module>
print(re._doc_)
AttributeError: 'module' object has no attribute '_doc_'
It's called __doc__, not _doc_.
import re
print(re.__doc__)
Works just fine.

Categories

Resources