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.
Related
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 5 years ago.
Improve this question
What are the official guidelines for the location of the main method in a Python program?
Should it be written first (at the top of the program), last (at the bottom), or is it just preferential? I read through PEP8 and couldn't find anything.
There are no official guidelines referring to this.
Whatever is most useful for someone to read first should go first.
Often, this is going to be some kind of "main" method. It describes the flow of the program. It will give developers a quick way of figuring out what is happening and where to look for things they want.
But I can envisage cases where it's not explicitly the main() method. Maybe the main() method just spins off some threads and it's what's inside it that counts. Maybe main() just does a bunch of argument parsing, and then calls another function. Maybe main() is just boilerplate.
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
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 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 7 years ago.
Improve this question
New programmer here!
I'm creating my first script on my own, and I have a particular function that is quite large, as in 50 lines.
I understand that theoretically a function can be as large as you need it to be, but etiquette-wise, where is a good place to stay under?
I'm using Python 2.something if that makes a difference.
A good rule of thumb (and it's more a guideline of thumb really) is that you should be able to view the entire function on one screen.
That makes it a lot easier to see the control flow without having to scroll all over the place in whatever editor you're using.
If you can't understand fully what a function does at first glance, it's probably a good idea to refactor chunks of code so that the more detailed steps are placed in their own, well-named, separate function and just called from this one.
However, it's not a hard-and-fast rule, you'll adapt your approach depending on your level of expertise and how complex the code actually is.
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).