NameError python api "vishnubob/kinet" - python

So I am trying to run and learn all about the vishnubob/kinet api.
After I download it I try to run the example script (also after fixing a couple syntax errors) I get this error:
Traceback (most recent call last):
File "example.py", line 31, in <module>
pds = PowerSupply("192.168.1.121")
NameError: name 'PowerSupply' is not defined
I have no clue why this is happening, as the imports look good (to me at least).
If someone knows why or can point me to the right direction then I would be grateful.

The clue here is "fixing syntax errors". That code has been written for Python 2, and it uses a thing called implicit relative import which was removed in Python 3. Namely the line in kinet/__init__.py
from kinet import *
is trying to import from the top-level package in Python 3, when Python 2 defaulted to importing from a local module first (kinet/kinet.py). The fix is to change this to
from .kinet import *
However this leads down the rabbit hole, as kinet.py has more syntax errors, after which it just fails spectacularly with
Traceback (most recent call last):
File "example.py", line 31, in <module>
pds = PowerSupply("192.168.1.121")
File "./kinet/kinet.py", line 227, in __init__
self.header = Header()
File "./kinet/kinet.py", line 22, in __init__
self._struct = struct.Struct(self.struct_format)
File "./kinet/kinet.py", line 61, in __setattr__
if key not in self.Keys:
File "./kinet/kinet.py", line 56, in __getattr__
if key not in self.Keys:
File "./kinet/kinet.py", line 56, in __getattr__
if key not in self.Keys:
File "./kinet/kinet.py", line 56, in __getattr__
if key not in self.Keys:
[Previous line repeated 325 more times]
RecursionError: maximum recursion depth exceeded while calling a Python object
It is a bad piece of code written using all the syntax that was deprecated already in Python 2.6. I'd look for a Python 3 rewrite instead of trying to fix that.

Related

Python weird ModuleNotFoundError at wrong position in code

I am having a super weird error, the situation is the following:
I am writing some code in a package called hsltools. The Module is called em_combiner1_4. I recently changed the version from 1_3, and also changed all imports accordingly.
If I run my code now, I get the following error:
Traceback (most recent call last):
File "C:\Users\...\git_home\hsltools\hsltools\tests\emc_tester\EMC_tester.py", line 85, in <module>
combine_2d()
File "C:\Users\...\git_home\hsltools\hsltools\tests\emc_tester\EMC_tester.py", line 48, in combine_2d
elec_field2d = emc.EM(import_path+"2DelecMap.ef2.npz")
File "c:\users\...\git_home\hsltools\hsltools\em_combiner1_4.py", line 441, in __init__
load_npz()
File "c:\users\...\git_home\hsltools\hsltools\em_combiner1_4.py", line 251, in load_npz
self.em_type = data["metadata"][1]
File "C:\Users\...\Anaconda3\lib\site-packages\numpy\lib\npyio.py", line 254, in __getitem__
return format.read_array(bytes,
File "C:\Users\...\Anaconda3\lib\site-packages\numpy\lib\format.py", line 744, in read_array
array = pickle.load(fp, **pickle_kwargs)
ModuleNotFoundError: No module named 'hsltools.em_combiner1_3
The thing I don't get is the location of the error: ...\numpy\lib\format.py.
There is absolutaly no import there, and anyway, numpy would not import my module.
I am wondering if I am generally misunderstanding some basic concept, or if the error is wrong.
It's happening while deserializing pickled objects. Pickle stores the type information including the module name. When loading a pickled object, the module is imported to construct the final object.
Your pickle file contains an object that is part of hsltools.em_combiner1_3. So that is the module pickle tries to find.

python traceback - performance problems

we have a python script that uses a c library to call some low level functions.
Because of code structure reasons we want to store the stack trace/call stack before each c call.
To do this we use traceback.extract_stack() to extract the call stack of python.
When an exception occurs later on, we use traceback.format_list(abc) on each element to format and print the stack trace.
The problem is that the function extract_stack is too slow. I slows down our code from 1.7 seconds to 11 seconds.
Is there any function to store the stack trace in order to be able to use/print it later?
The stacktrace getter must be very fast. The format function can be slow, this is no problem.
Example:
Stack Trace:
- LXScript: '_LXS:TOOL:RUNLX'
File "_LXS:TOOL:RUNLX", line 13, in <module>
File "lxs", line 1, in <module>
- UNIFACE ACTIVATE: 'ACTQREC_SVC' 'EXECLXSRP'
- LXScript: '_DATATRT:ACTQREC:EXECUTE#Main:run'
File "_LXS:TOOL:RUNLX", line 13, in <module>
File "lxs", line 1, in <module>
File "<string>", line 63, in run
File "<string>", line 97, in __doAll
File "<string>", line 127, in __do
File "_DATATRT:ACTQREC:EXECUTE", line 7, in do
- UNIFACE ACTIVATE: 'ACTQ_CSVC' 'EXECBYREC'
- LXScript: 'TOOL:ACTQ:SYNLAB_DATA#Main:runOnBeforeExec'
File "TOOL:ACTQ:SYNLAB_DATA", line 1, in <module>
ImportError: No module named 'localls'
I resolved the problem!!!
We can use stt_obj = sys._getframe().f_back to get the "<frame>?" And then we can use estt_obj = traceback.extract_stack(f=stt_obj) and traceback.format_list(estt_obj) to get the stack trace as string list.

xlwings call from Excel VBA function produces error

The call from EXCEL sub to python using xlwings works fine but wen triggering this same VBA via a VBA function the following error is generated:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "c:\users\a112833\google drive\mytechtests\pulp\mymodule.py", line 13, in energySystemSimulator
wb = Workbook.caller()
File "C:\Python27\lib\site-packages\xlwings\main.py", line 233, in caller
xl_app, xl_workbook = xlplatform.get_open_workbook(fullname, hwnd=sys.argv[4])
File "C:\Python27\lib\site-packages\xlwings\_xlwindows.py", line 142, in get_open_workbook
duplicate_fullnames = get_duplicate_fullnames()
File "C:\Python27\lib\site-packages\xlwings\_xlwindows.py", line 124, in get_duplicate_fullnames
for xl_app in get_xl_apps():
File "C:\Python27\lib\site-packages\xlwings\_xlwindows.py", line 96, in get_xl_apps
xl_app = get_xl_app_from_hwnd(hwnd)
File "C:\Python27\lib\site-packages\xlwings\_xlwindows.py", line 73, in get_xl_app_from_hwnd
return disp.Application
File "C:\Python27\lib\site-packages\win32com\client\dynamic.py", line 522, in __getattr__
raise AttributeError("%s.%s" % (self._username_, attr))
AttributeError: <unknown>.Application
This works:
Sub eSS()
RunPython ("import mymodule; mymodule.eSS()")
End Sub
This breaks with the traceback above (ignore fn. arguments):
Function fnESS(x As Double, y As Double) As Double
Call eSS
fnESS = ActiveWorkbook.Sheets("Config").Range("T10")
End Function
I checked call signature seems exactly the same using VBA sub or function sub call. I can't see what is causing the error.
You're seeing a common trap when writing User Defined Functions: Excel only allows you to write to the calling cells, not outside of it. That's a limitation by Excel, really.
xlwings might offer a workaround for this one day, but right now, there's no official support for this.
As a side note: you might actually be better off writing UDFs by using the xlwings decorators (at least if you are using Windows only), see here.

Creating a track object with track_from_file method in pyechonest

I am new to python. I am trying to use track_from_file method in pyechonest to create a track object. This is the code I wrote:
from pyechonest import config
from pyechonest.track import track_from_file
config.ECHO_NEST_API_KEY = "KRZG4XOVUQRKI45DP"
f = open("ShayneWard-NoPromises.mp3")
t = track_from_file(f, 'mp3')
But this is not working. I got an EchoNestIOError. This is the full error Traceback that i got.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "build/bdist.linux-x86_64/egg/pyechonest/track.py", line 243, in track_from_file
File "build/bdist.linux-x86_64/egg/pyechonest/track.py", line 331, in track_from_md5
File "build/bdist.linux-x86_64/egg/pyechonest/track.py", line 209, in _profile
File "build/bdist.linux-x86_64/egg/pyechonest/util.py", line 257, in callm pyechonest.util.
EchoNestIOError: Echo Nest Unknown Error
Can anyone tell me what have I done wrong?
I finally figured it out. It seems that the upload does not take place through the proxy server of my university network which uses NAT.

lightblue Python module not working on mac

I'm trying to connect to wiimote via Python on mac osx 10.7.2.
For that I'm trying to use lightblue. When running: import lightblue Python gives me this error.
>>> import lightblue
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/lightblue/__init__.py", line 160, in <module>
from _lightblue import *
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/lightblue/_lightblue.py", line 27, in <module>
import _IOBluetooth
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/lightblue/_IOBluetooth.py", line 47, in <module>
globals=globals())
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC/objc/_bridgesupport.py", line 142, in initFrameworkWrapper
_parseBridgeSupport(data, globals, frameworkName)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC/objc/_bridgesupport.py", line 42, in _parseBridgeSupport
objc.parseBridgeSupport(data, globals, frameworkName, *args, **kwds)
ValueError: cftype for 'IOBluetoothDeviceInquiryRef' must include gettypeid_func, tollfree or both
I found one with a similar problem here: http://python.6.n6.nabble.com/Python-bridgesupport-issue-on-Lion-td2161049.html#a32196961 but the answer doesn't help me much, since it seems to me that it is already using lightblue's files. Any suggestions?
Seems like a bug with Apple's gen_bridge_metadata script. You can make the error go away by editing the generated file by hand.
Open up /System/Library/Frameworks/IOBluetooth.framework/Versions/Current/Resources/BridgeSupport/IOBluetooth.bridgesupport and delete the lines that start with <cftype.

Categories