What is the meaning of set.constant ?
I have a program that i need to write in python without using creating a new class. Is there an equivalent of it in python or numpy specifically ?
Python doesnt use constants.
To use a variable outside the current scope you can use:
global var
var =
Related
If I have defined a function I expect to use in most of my programs, where do I store it so that I can just import it without having to have it in the same folder as my program?
Also when should I call such functions as opposed to importing them? (I am not sure what the correct terminology is) i.e.
When would you set up a function so that you use it by:
myFunction()
And when would you set it up so that you use by :
import myFunction as mf
...
mf.blahblah
Please try below
Access modifier for your function : public
Now you can create instance of the class. And you can easily use same function from everywhere.
I'm writing some code for a calculus package in python. Ideally, I would like to support compositions of user-defined functions. For example, a user could define functions within my SingleVariable class like so:
f = SingleVariable('sin(x)')
g = SingleVariable('x+4')
and then a composition
h = SingleVariable('f(g(x))')
These functions can be integrated, differentiated, etc. The method used to evaluate the function at a point is as follows:
def infix_to_function(infix, variable):
def f(x):
expression = infix.replace(variable, str(x)).replace('^', '**')
return eval(expression)
return f
This works fine for functions like sin and ln because they can be loaded ahead of time so as to be recognized by eval. However, this obviously doesn't work for user-defined functions because they don't exist in the namespace in which the function is actually evaluated. Returning to the example functions f and g, i would like a function defined as
h = SingleVariable('f(g(x))')
to be equivalent to
h = SingleVariable('sin(x+4)')
I'm writing the package in PyCharm and testing it by importing to a Jupyter Notebook. I thought about adding the function to a list of some sort when a new SingleVariable object is initialized, but that would require somehow grabbing the name of the variable it is being assigned to.
Is there any way to add the functions I define in the Jupyter Notebook to the namespace of the PyCharm package so that they can be recognized by eval and have the behavior described?
You can try passing in globals() to eval like eval(expression, globals()). I believe Jupyter Notebooks will put any functions that have been defined into the global namespace.
However, using exec() and globals() can cause a lot of problems - so make sure you really understand what you are doing.
It will be a bit more complicated but if you plan to build this out to be very general you may want to look at a lexing libraries like http://www.dabeaz.com/ply/example.html which can help you ensure that the input provide is valid and parsed correctly.
As a small example of the type of problems you can have - based on your sample code I think:
f = SingleVariable('sin(s)')
may not work the way you expect...
I'm trying to write a code with exec and eval function to read lists of variables from a numpy .npz file.
When I ran the code without defining it as a function def, the code worked. However, when I ran the code as a function, i.e. read_file_npz("file_address") , the python 3.7 kept pop up message saying that templet_1h was not defined.
def read_file_npz(file_names_2):
import numpy as np
Delete_elements=["arr_0"]
evaluate_1= "templet_1h=np.load(\"./" +file_names_2+ ".npz\")";
exec(evaluate_1)
for i in (templet_1h.files):
if not ( (i in Delete_elements) ):
evaluate_2="global "+i;
exec(evaluate_2)
evaluate_2= i+"="+"templet_1h[\"" + i + "\"]";
exec(evaluate_2)
What's wrong with the warning and how to modify it?
I tried to declear templet_1h before the code as list(), but then the warning became .files had no ... towards lists, as if evaluate_1 was never ran.
Use exec(evaluate_1, globals()) instead to use the global dictionary for global and local variables in exec.
The code adds the defined variable to the global dictionary. Adding it as local variable of a function is not possible.
I have an embedded python interpreter in my program. I'd like to export a module with values defined in my program and be able to change them from a python script. e.g.
in c:
int x = 1;
in python:
import embedded
embedded.x = 2
in c:
printf("%d",x);
output:
2
Is this possible or do I have to export functions to change anything in c?
There's no need to export functions, but the easiest way to do this would be to use PyModule_GetDict() with PyDict_GetItemString() to get the value assigned to the x attribute.
If you don't want to actively check the value of a PyObject in your C code, I think you need to export functions to modify the representation in C. I'm no expert, but I don't think there's an automatic mapping.
I have some global variables in a Python script. Some functions in that script call into C - is it possible to set one of those variables while in C and if so, how?
I appreciate that this isn't a very nice design in the first place, but I need to make a small change to existing code, I don't want to embark on major refactoring of existing scripts.
I'm not a python guru, but I found this question interesting so I googled around. This was the first hit on "python embedding API" - does it help?
If the attributes belong to the global
scope of a module, then you can use
"PyImport_AddModule" to get a handle
to the module object. For example, if
you wanted to get the value of an
integer in the main module named
"foobar", you would do the following:
PyObject *m = PyImport_AddModule("__main__");
PyObject *v = PyObject_GetAttrString(m,"foobar");
int foobar = PyInt_AsLong(v);
Py_DECREF(v);
For anyone coming here from Google, here's the direct method:
PyObject* PyEval_GetGlobals()
https://docs.python.org/2/c-api/reflection.html
https://docs.python.org/3/c-api/reflection.html
The return value is accessed as a dictionary.
I recommend using pyrex to make an extension module you can store the values in in python, and cdef a bunch of functions which can be called from C to return the values there.
Otherwise, much depends on the type of values you're trying to transmit.
Are you willing to modify the API a little bit?
You can make the C function return the new value for the global, and then call it like this:
my_global = my_c_func(...)
If you're using Robin or the Python C API directly, you can pass the globals dictionary as an additional parameter and modify it
If your global is always in the same module, Sherm's solution looks great