This seems like a pretty obvious/dumb question, but there are a few specifications that make this a bit harder.
Let's say I have a program that takes 3 numbers from a user and does mathematical processes to them to get outputs. Then I open("file", "r") to write those variables to a file.
Then, let's say another program then imports them and uses them for other processes. I need to be able to import that file as Python code. To be clear: I am not saving text, I am saving python code to a file that is not a .py file.
Is there any way to save and import Python code to and from a non-.py file? And how?
EDIT: In the file I'm saving and importing, I'm also saving Python functions. I cannot simply save the variables themselves; I need the variable names, values, and python functions to be saved as normal text in a file, but when I import the file, it should be parsed as Python code.
Probably not a good idea to store computation result as code & then import it from elsewhere. You should use a proper data format to store the results - and import it as data. Use JSON or pickle etc.
However, if you do want to shoot yourself in the foot, Python gives you the tools to do that:
Let's say i have some code in a file temp.txt
number3=30
def f():
return 'method'
Then you can do this:
with open('temp.txt') as f:
code = f.read()
exec(code)
print(number3)
print(f())
Which outputs:
30
method
If i got this right, this might be done via eval function e.g. you save all code to be executed into a string and then save into a file.
When you need that executed read the file, tke the string and eval it
I must say however that using eval is a bad (very bad) practice and i would advice against it unless there is no other solution that you can find
Related
I'm sick of doing
with open('my.json','r') as inf:
my_dict = json.load(inf)
each time I want to get the contents of a json file as a dict in python. It seems so trivial to simply add a function that just accepts 'my.json' as a path and then implicitly opens the file and then loads the json file with whatever additional keywords you want. I found the pandas.read_json() class, but if I tried that it gave me the same error that I get if I try to call json.load() on a filepath. Is it difficult to add my custon reading function to the json package? How do I add a function to a package that is easily callable from any script with an import statement, so I don't have to write the same function over and over again? Is there a particular reason why this isn't an available function? Am I just lazy?
I know what the code for my solution is, I just don't know where to add it so it's easily importable and whether or not this is a very bad idea to begin with, since it would require a modification of a builtin python package.
I have a .txt file which contains a variable for my model. If I copy and paste the contents of the file in my program as
def Wind_phi_definition(model, i):
return m.Wind_phi[i] ==-19.995904426195736*Sinc(0.04188790204786391*(0. + m.lammda[i]*180/np.pi))*Sinc(0.08975979010256552*(-65. + m.phi[i]*180/np.pi))
m.Wind_phi_const = Constraint(m.N, rule = Wind_phi_definition)
The code is executed without issue. I want to speed this by making the program read directly from the .txt file.
I have tried to read the variable as
f = open("savedWindXPython.txt", "r")
a=f.readline()
f.close
def Wind_lammda_definition(model, i):
return m.Wind_phi[i] == a
m.Wind_phi_const = Constraint(m.N, rule = Wind_lammda_definition)
However, an error is returned
AttributeError: 'str' object has no attribute 'is_relational'
I understand that this happens because python is reading this as a string and not as a pyomo variable. I have attempted to go arround this problem using exec(a) instead of just a in the definition of m.Wind_phi. However, I still obtain an error, this time it says
'NoneType' object has no attribute 'is_relational'
Is there a way to do what I want to do and define the variable by reading the .txt file isntead of having to copy it's contents by hand?
What you are trying to achieve is called unmarshalling (or deserialization)
If your variable is a simple number, deserializing it is as easy as int(my_string_var). If it looks like the one you linked (where you need function calls in it), there is more work to be done. The lib you're using can also provide some serialization/deserialization feature. Unfortunately for you it seems like pyomo doesn't provide this feature at the moment.
So, now, if you have access to who or what is writing your .txt files in the first place, consider serializing it into json (or whatever data-storage format you're the most comfortable with; if you're not comfortable with any, try json). Then you will now how to deserialize it in your code.
Otherwise, you will have to write a string parser to extract the data from your string and serialize it into python objects. This could be trivial if you're only trying to parse this specific variable, but could also end up being rather challenging if you are trying to deserialize a lot of these variables.
html is just text, often very repetitive. I have a simple homework webpage. I'm trying to automate the webpage production.
I have Python routines for producing html (I made them, so they are primitive, but they all work):
makeCheckboxes.py
makeDropdownboxes.py
makehtmlTable.py
makeRadiobuttons.py
makeTextboxes.py
makeThankyouPHP.py
and lastly:
makeWebpage.py
They all just output a text file.
Rather than lump all these in one very big, long file (I lose the plot easily), I'd like to call the one or ones I want from makeWebpage.py and run it, then knot the sections together into 1 text file.
They are all in /home/pedro/textTohtml/ I run them in a bash terminal.
I don't need all the routines each week.
All I need to know is, how many sections I want and what's in it.
For example, let's say next week: Section 1 is radio buttons, Section 2 is Textboxes (fill in the gaps exercise)
Can I call the 2 routines from makeWebpage.py without actually defining them within as functions?
The functions themselves produce a text file which I can then open and integrate into the webpage template.
EDIT: Thanks for the answers. What I need is to import the whole file, each of which will then have its own inner functions.
If I do this:
import file as fl
Will it then run fl?
Or is it better to run subprocess?
How does this help you:
Call a function from another file in Python
You just need to use
from file import function
To import the functions at the start of your makeWebpage.py file. Then makeWebpage.py can call any of the functions any time it wants.
If you only need a function:
from FILE import FUNCTION
FUNCTION(*args,**kwargs)
If you want to run/execute the python file (everything):
import os
os.system("FILE")
[NOTE] The file must be a string and contain the extension, eg "some_file.py" whereas in import statements only the file name is specified in plaintext.
In both cases both files must be in the same directory unless a path is specified ("c:\Users\MyProfile\PythonFiles\python.py" or "/Users/MyProfile/PythonFiles\python.py")
EDIT: If you are importing an entire file (STRICTLY LIKE THIS!) from FILE import * you can name variables or functions with an underscore before it to prevent it from being imported (more info on PEP 8 https://www.python.org/dev/peps/pep-0008/#id36)
Hello I have combed threw possible solutions and I can't figure this out, I got my program working for the most part but the problem is when I close the program it doesn't remember the changes made by the user. The program stored input into variables. It works like a dictionary (I wrote the code before I knew about the dictionary function) when I open the program I can enter the input but the problem is when I close the program it forgets all the changes and goes back to how it was when first compiled. Any ideas how I can allow the data to ramain available when I close the program and reopen it?
Probably you need shelve. It is awesome Python library, which allows you to read/write dict-like data from file. For example:
import shelve
dict_with_data = dict(key1='value1', key2='value2')
storage = shelve.open('shelve_file')
storage.update(dict_with_data)
storage.close()
It is very simple example which opens shelve storage and updates it with your data. Actually you can do whatever you do with regular dictionary object, because
A “shelf” is a persistent, dictionary-like object.
Another library that you could use is pickle. It allows you serialize every kind of Python objects, but since you have dictionary structure shelve should be better for your needs.
Please fill free to ask questions/post your code, I will be glad to update my answer appropriate to you needs.
I think you need to implement two things here.
1) Your program need to take an optional input file and the best way to do it is probably to use argparse. If you pass the input file, then you extract the data from the file and you initialize your dictionary_like object with that data.
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--input', help='input file name')
args = parser.parse_args()
if args.input:
... # open the file, load the data, initialize a dict with the data
2) Just before quitting, you must save your data (key:value pairs) in the input file. If you want to save a dictionary like structure, a txt file with a key:value pairs for each line of the file should be good enough.
with open(input, 'w') as f:
for key,value in dictionary.items():
f.write(str(key)+':'+str(value)+'\n') #you can use '\t' or whatever instead the ':'
And that's it.
If you need more help on how to implement this let me know ;)
I am working with cPickle for the purpose to convert the structure data into datastream format and pass it to the library. The thing i have to do is to read file contents from manually written file name "targetstrings.txt" and convert the contents of file into that format which Netcdf library needs in the following manner,
Note: targetstrings.txt contains latin characters
op=open("targetstrings.txt",'rb')
targetStrings=cPickle.load(op)
The Netcdf library take the contents as strings.
While loading a file it stuck with the following error,
cPickle.UnpicklingError: invalid load key, 'A'.
Please tell me how can I rectify this error, I have googled around but did not find an appropriate solution.
Any suggestions,
pickle is not for reading/writing generic text files, but to serialize/deserialize Python objects to file. If you want to read text data you should use Python's usual IO functions.
with open('targetstrings.txt', 'r') as f:
fileContent = f.read()
If, as it seems, the library just wants to have a list of strings, taking each line as a list element, you just have to do:
with open('targetstrings.txt', 'r') as f:
lines=[l for l in f]
# now in lines you have the lines read from the file
As stated - Pickle is not meant to be used in this way.
If you need to manually edit complex Python objects taht are to be read and passed as Python objects to another function, there are plenty of other formats to use - for example XML, JSON, Python files themselves. Pickle uses a Python specific protocol, that while note being binary (in the version 0 of the protocol), and not changing across Python versions, is not meant for this, and is not even the recomended method to record Python objects for persistence or comunication (although it can be used for those purposes).