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.
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.
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
I am using tkinter to manage the GUI for a note retrieval program. I can pull my notes by typing a key word and hitting Enter in a text field but I would like to move my dictionary to a file so that my code space is not filled up with a massive dictionary.
I have been looking around but I am not sure how I would go about doing this.
I have the file in my directory. I know I can use open(“filename”, “mode”) to open said file for reading but how do I call each section of the notes.
For example what I do now is just call a keyword from my dictionary and have it write the definition for that keyword to a text box in my GUI. Can I do the same from the file?
How would I go about reading from the file the keyword and returning the definition to a variable or directly to the text box? For now I just need to figure out how to read the data. I think once I know that I can figure out how to write new notes or edit existing notes.
This is how I am set up now.
To call my my function
root.bind('<Return>', kw_entry)
How I return my definition to my text box
def kw_entry(event=None):
e1Current = keywordEntry.get().lower()
if e1Current in notes:
root.text.delete(1.0, END)
root.text.insert(tkinter.END, notes[e1Current])
root.text.see(tkinter.END)
else:
root.text.delete(1.0, END)
root.text.insert(tkinter.END, "Not a Keyword")
root.text.see(tkinter.END)
Sound's like you'd need to load the dictionary to memory at init time, and use it like a normal dictionary.
I am assuming your dictionary is a standard python dict of strings, so I recommend using the python json lib.
Easiest way to do this is to export the dictionary as json once to a file using something like:
with open(filename, 'w') as fp:
json.dump(dictionary, fp)
and then change your code to load the dict at init time using:
with open(filename) as fp:
dictionary = json.load(fp)
Alternatively, if your data is more complex than text, you can use python shelve which is a persistent, dictionary-like object to which you can pass any pickle-able object. Note that shelve has its drawbacks so read the attached doc.
sqlitedict is a project providing a persistent dictionary using sqlite in the background. You can use it like a normal dictionary e.g. by assigning arbitrary (picklable) objects to it.
If you access an element from the dictionary, only the value you requested is loaded from disk.
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).