Forcing ArrayFire to switch backends in Python - python

I have been attempting to force ArrayFire to use its CPU backend, rather than the default CUDA backend. According to this documentation page, you only need to call arrayfire.set_backend('cpu'). However, when I attempt to do this, an error is thrown with the message global name 'backend' is not defined. If you take a look at the source code, you will see that a global variable backend is defined within the module directly before the set_backend function is implemented. The following functions set and get various attributes of this backend object. My question is: is an internal implementation error on their part causing this error, or is there something I'm doing wrong (or that can be done on my part to fix this)? I haven't worked much with Python modules before, and would greatly appreciate any help!

Related

Django Template loader doesn't have "get_contents()" function implemented. Is there a substitute?

I am working on upgrading some older Django code however I am running into issues with the loader function "get_contents()" as it has apparently been deprecated. I'm not sure how to proceed. Should I create a new child claqss from that loader and implement the function myself or is there another way?
I have basically tried looking this problem up on google and the few answers I've come across have been somewhat vague.
I can't show much code, as this is a private project, but I can show one line with the names changed.
templateVariable = loader.get_template('appName/filename.txt')
This is the error I'm getting when I try and run the function:
'Loader' object has no attribute 'get_contents'
So it turns out that there was a custom loader being used in the project and the get_contents() function hadn't been implemented yet. Another mystery solved.

Import issues with Python and celery

There is a pretty strange issue happening on my production server that doesn't come up in UAT or my development environment. Stack setup is Ubuntu 14LTS, Apache2, Python 2.7,Celery, RabbitMQ, Django 1.6.
How it works is that there is an async task that is called whenever an excel report needs to be generated. Depending on the type of company, a different report is created and emailed to the logged in user. The functions to create the different reports are all placed in a file called report_lib.py and they are not importing from any other app. The code goes like this:
if policies.carrier == "class_one":
report_lib.create_remittance_class_one_report(company_id)
else:
report_lib.create_generic_remittance_report(company_id)
Get this, the first conditional works no problem. The second conditional works when I don't have a company type set(works as expected), but for any other class it fails with the following error:
[2014-10-27 17:56:55,271: WARNING/Worker-1] There was an error: 'module' object has no attribute 'create_generic_remittance_report'
To make things more interesting I removed the conditional and defaulted to the code just calling the "create_generic_remittance_report" function for all cases, which works without complaining about that module.
I'm 99% sure that there are no-circular references. Oh, and the library being referenced is under the only app being used in the django project. Could this be a compiler caching issue?
Has anyone come across a similar issue using the same setup?
Please help!
Found the issue, the function being referenced was indented an extra tab. I guess since celery only calls it, it came back with that error.

how do you statically find dynamically loaded modules

How does one get (finds the location of) the dynamically imported modules from a python script ?
so, python from my understanding can dynamically (at run time) load modules.
Be it using _import_(module_name), or using the exec "from x import y", either using imp.find_module("module_name") and then imp.load_module(param1, param2, param3, param4) .
Knowing that I want to get all the dependencies for a python file. This would include getting (or at least I tried to) the dynamically loaded modules, those loaded either by using hard coded string objects or those returned by a function/method.
For normal import module_name and from x import y you can do either a manual scanning of the code or use module_finder.
So if I want to copy one python script and all its dependencies (including the custom dynamically loaded modules) how should I do that ?
You can't; the very nature of programming (in any language) means that you cannot predict what code will be executed without actually executing it. So you have no way of telling which modules could be included.
This is further confused by user-input, consider: __import__(sys.argv[1]).
There's a lot of theoretical information about the first problem, which is normally described as the Halting problem, the second just obviously can't be done.
From a theoretical perspective, you can never know exactly what/where modules are being imported. From a practical perspective, if you simply want to know where the modules are, check the module.__file__ attribute or run the script under python -v to find files when modules are loaded. This won't give you every module that could possibly be loaded, but will get most modules with mostly sane code.
See also: How do I find the location of Python module sources?
This is not possible to do 100% accurately. I answered a similar question here: Dependency Testing with Python
Just an idea and I'm not sure that it will work:
You could write a module that contains a wrapper for __builtin__.__import__. This wrapper would save a reference to the old __import__and then assign a function to __builtin__.__import__ that does the following:
whenever called, get the current stacktrace and work out the calling function. Maybe the information in the globals parameter to __import__ is enough.
get the module of that calling functions and store the name of this module and what will get imported
redirect the call the real __import__
After you have done this you can call your application with python -m magic_module yourapp.py. The magic module must store the information somewhere where you can retrieve it later.
That's quite of a question.
Static analysis is about predicting all possible run-time execution paths and making sure the program halts for specific input at all.
Which is equivalent to Halting Problem and unfortunately there is no generic solution.
The only way to resolve dynamic dependencies is to run the code.

Why does Python (WLST) tell me a documented function doesn't exist?

I'm using the Weblogic Scripting Tool aka WLST, a Python-based shell environment, to programmatically edit variables in Plan.xml files for projects to be deployed to the Weblogic server. I can get as far as getting an instance of the WLSTPlan Object, and can run getVariables and other methods to check that it is populated and view its contents. However, when I try to call the setVariable method, I get an AttributeError, which to my limited understanding means the method doesn't exist:
wls:/UoADevDomain/serverConfig> plan.setVariable("foo", "bar")
Traceback (innermost last):
File "<console>", line 1, in ?
AttributeError: setVariable
As the docs linked above (which I checked are the right version) show, this method definitely should exist, and other methods listed in the same doc work. I'm not sure if this is an issue with Weblogic or with my understanding of Python, but either way it's beyond me. I tried using the dir() function to list the plan object's attributes, but it returned an empty set so I guess that trick doesn't work in this environment.
Can anyone suggest how to go about diagnosing this problem, or better still fixing it?
Using javap and looking for setters on the WLSTPlan bean shows only the following setter
void setVariableValue(java.lang.String, java.lang.String);
Could be a typo in the docs. Can you try 'setVariableValue' instead.
The documentation is rather unclear, but from reading between the lines, it looks like setVariable is a method that you invoke on a VariableBean.
I'd try using the following:
plan.createVariable("foo").setVariable("foo", "bar");
(that's without having tested the code, though)

help with python ctypes and nvapi

My end goal is to query NVAPI for gpu usage and other statistics in python. See http://developer.nvidia.com/nvapi
from ctypes import WinDLL
nvapi = WinDLL("nvapi.dll")
print nvapi# <WinDLL 'nvapi.dll', handle 718a0000 at 27c0050>
print nvapi.nvapi_QueryInterface# <_FuncPtr object at 0x026D8E40>
print nvapi.nvapi_QueryInterface()# returns 0
print nvapi.NvAPI_Initialize# AttributeError: function 'NvAPI_Initialize' not found
print nvapi.NvAPI_SYS_GetChipSetInfo# AttributeError: function 'NvAPI_SYS_GetChipSetInfo' not found
Here is a copy of the header file available for download from the link above: http://paste.pound-python.org/show/7337/
At this point, I am just trying to familiarize myself with the api... so what am I doing wrong? I can't figure out how to call any of the functions listed in the header file.
Are you sure it's a WinDLL? From the header file, it looks like a standard C calling convention to me. Have you tried CDLL instead?
EDIT:
I see now. The header you pointed to isn't actually the interface for nvapi.dll--it is a wrapper around it that must be statically linked.
From the docs downloaded from NVIDIA's developer site:
Use a Static Link with Applications
NvAPI cannot be dynamically linked to applications. You must create a static link to the library and then call NvAPI_Initialize(), which loads nvapi.dll dynamically.
If the NVIDIA drivers are not installed on the system or nvapi.dll is not present when the application calls NvAPI_Initialize(), the call just returns an error. The application will still load.
I would guess that the actual calls in nvapi.dll are completely different than the ones exposed in this wrapper library. I can't seem to find any documentation on those though. Perhaps they are internal and change between systems.
If you want to use this interface, I'm not really sure what the best solution is. It's a static library and not a dynamic one, so ctypes wouldn't handle it unless you wrapped it in another DLL. I'm not an expert at native code with Python, so maybe someone else will have an easy fix. Sorry.
Taken from your comment:
NvAPI_Initialize call still fails.
saying the function is not found.
NvAPI_Initialize is not exported from the dynamic library nvapi.dll. It is a symbol contained in nvapi.lib, the static library shipped with the NVIDIA SDK, thus it's no wonder you are unable to call it using Python.
Honestly, the easiest route here is to create a small wrapper DLL in C, statically linking to nvapi.lib and exposing a friendly interface to Python.

Categories