Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Optimization: Parsing a text file to store default values and create key-pair dictionary in python

I am developing a program for which I need to store user defaults and the user should also have the option to modify the defaults for future use of the program. So basically, I have a module to manage default parameters and other aspects of the program that requires certain parts to be stored and later retrieved. For example, to manage the default parameters, I have a file params.py in a module to manage params.txt. The python file params.py essentially contains two functions: (1) get_default_param(param_name) and (2) set_default_param(param_name).

The params.txt file contains the following:

NDIV: 120
EXT: txt
UNITS: cm/s2
AM: 1.0, 1.0, 1.0, 1.0

params.py file contains:

def get_default_param_values():
    default_val = {}
    # Get params function
    with open("data/defaults/params.csv") as f1:
        for line in f1:
            split_line = line.split(':')
            default_val[split_line[0]] = [x.strip(" ") for x in split_line[1].strip('\n').split(',')]
    return default_val

So in my main.py program I call the params.py which is located in data package inside a default folder as

from data.defaults.params import get_default_param_values

default_values = get_default_param_values()

for i in range(0,1000):
    #
    # use default_values in the program. These values also gets passed down to other functions
    #

If these were not to be modified I could have them easily placed them within a python file and imported the variable. What would be the best of doing it when we would need to modify it at a later stage by the user in front-end?

And another issues is that some of the values for the keys are numbers. How do i automatically identify integers or decimal numbers and convert string to int/float?

like image 440
naseefo Avatar asked May 13 '26 05:05

naseefo


2 Answers

Is it mandatory to store the key-value pairs in a text file?

If yes, what you have already done might be a way to do it.

If not, I'd suggest you to store it as a JSON file and load it as a python dictionary (this approach wards off int parsing issue as well).

like image 185
Vignesh Bayari R. Avatar answered May 14 '26 20:05

Vignesh Bayari R.


Consider pyyaml. It's a more readable superset of json.

like image 33
Chris Avatar answered May 14 '26 19:05

Chris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!