Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a python file which contain a dictionary and one more file which importing the file with the dictionary.
I need to change the actual value of keys, I mean permanently - not by importing and editing for the code,
How can I accomplish that ?
There is a very good reason why programmers have learned over the years not to modify the source of their programs under program control. It results in a mess, particularly when multiple people try to use the same program at the same time, and means that you can't effectively keep your code under source control.
It would be much better to use some way of storing the keys and values outside the program. Depending on the types of key that could be a shelve, a relational database or a number of other things. If it varies, data should not be built into your program's source.
Use pickle
import pickle
myDict = {"foo": "bar", "spam": "eggs"}
pickle.dump(myDict, "myFile.py") # Stores object in a file
dictFromFile = pickle.load("myFile.py") # Retrieves object from files
print dictFromFile["spam"] # Prints eggs
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
So i have a program where the user enters in a few details e.g name, place etc. However, when i collect these details, they are all assigned to the variable name that i gave to the input collector (e.g input =). But i need to process these variables into a specific order (its a bibliography generator fyi). However i need to assign each of the instances of inputs into different variables without having basically the same code over and over again, but i couldn't think of a way to do that.
Help would be much appreciated.
Here is how I solve that problem in the past:
1) Store the data in an ordered dictionary.
2) Store the data in an object and use a function that returns the data in the order specified.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have no idea if this is possible, I want to know if it's possible to use Python dicts to read JSON, I have crawled the web for answers, but I don't think anyone has had the same idea, or it's just not possible. It may be a confusing question, But, here it goes!
I have a Python Dict as;
dict1 = {"3.8.1":"data[0]['3.8.1']","3.8":"data[1]['3.8']"}
As you can see, It'd get the json request string from the first dict with the found wordpress version number
dict1["3.8.1"]
would return the required next section to read from the loaded JSON file
I didn't "think" it was possible, but I thought I'd ask. As you can see in the above dict, It contains a way I could possibly request from the loaded JSON.
Anyway, any input, or other ways I could do it would be great, Thanks.
Are you looking for exec()?
exec("myvar = " + dict1["3.8.1"])
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Im not positive on the terminology here, so that may explain why searching on my own yielded no results.
I was just curious if there is a widely accepted, general method to writing a module in python. Obviosuly people prefer splitting things into segmented .py scripts, importing when needed, and packing it all into a folder.
What I want to know: Is there a general method to how/when/why we stop writing things together in one .py and begin a new one (And i mean other than obvious things like... one script .py for the main job, and then a preferences.py to handle reading/writing prefs)
You should split your code into multiple modules when it begins to be unwieldy to keep it all in one module. This is to some extent a matter of taste. Note that it may unwieldy for the code author (i.e., file is too big to navigate easily) or for the user of the library (e.g., too many unrelated functions/classes jammed together in the same namespace, hard to keep track of them).
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I hope there is a 'right' answer to this:
When using ConfigParser to manage default values in a .cfg or .ini file, should I copy everything I need to program variables and copy them back out on exit, or should I use the config.get(section, option) directly in my program as needed?
This is really a matter of opinion, but my advice is to have the values out of the config relatively quickly. The code that deals with data input and the layer that deals with the actual processing should be modular enough that you can change your data source by just feeding in data from a different source. (Coupling and Cohesion)
You'll have to use your own judgement to make the call as to where to draw the line, but as a guide: if you're setting the config as a global variable and reading from there or constantly throwing it around as an argument, you're doing it wrong.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to create a serialized Python object from outside of Python (in this case, from Java) in such a way that Python can read it and treat it as if it were an object in Python. I'll start with simpler objects (int, float, String, and so on) but I'd love to know if this can be done with classes as well.
Functionality is first, but being able to do it quickly is a close second. The idea is that I have some data in Java land, but some business logic in Python land. I want to be able to stream data through the python logic as quickly as possible...right now, this data is being serialized as strings and I think this is fairly wasteful.
Thank you in advance
The best answer is to use a standardized format, such as JSON, and write up something to create the objects from that format in Python, and produce the data from Java. For simple things, this will be virtually no effort, but naturally, it'll scale up.
Trying to emulate pickle from within Java will be more effort than it's worth, but I guess you could look into Jython if you were really set on the idea.