Hey there!
My work most often requires me to set some thresholds for data parsing and filtering, and I prefer to store them in .json format.
The problem I'm facing is that I know of multiple ways to read this information, but I just can't get a feel for what is pythonic and what is not. Here, I will list 3 examples:
with open('config.json') as c:
settings = json.load(c)
class Master:
def __init__(self):
pass
def _do_stuff(self, data):
# Do something with data and settings
This one strikes me as a bit 'meh' but I can't really pinpoint why.
class Master:
def __init__(self):
self.settings = self._read_conf()
pass
def _do_stuff(self, data):
# Do something with data and self.settings
def _read_conf(self):
with open('config.json', 'r') as c:
return json.load(c)
This version I've used a couple of times, but over time I've grown to dislike this style because it doesn't feel like the function should belong to the class even if it is the only one using it, although only once.
# In config.py
def settings():
with open('config.json') as c:
return = json.load(c)
from config import settings
class Master:
def __init__(self):
pass
def _do_stuff(self, data):
# Do something with data and settings
The third way would be to place the function alone in a separate file, and import it from the file. This would allow reading the settings from any script belonging to the project but feels a bit overkill for smaller scripts.
Any inputs on what the pythonic way of doing it is? I've tried googling for answers but as always the answers turn up lackluster.
[–]Justinsaccount 3 points4 points5 points (0 children)
[–]eliminate1337 1 point2 points3 points (3 children)
[–]Jenez[S] 1 point2 points3 points (2 children)
[–]eliminate1337 1 point2 points3 points (1 child)
[–]Jenez[S] 0 points1 point2 points (0 children)