How To Properly Deal With Config Settings // Python Tips

preview_player
Показать описание


#arjancodes #softwaredesign #python
Рекомендации по теме
Комментарии
Автор

For programmers like myself, who are self taught, little pieces of wisdom like this advice, mean a lot :) thank you

zolika
Автор

Not gonna lie, I like these shorts sometimes more than the videos, because I view your videos after work and like this concise format. Please do more of these if possible.

DistortedV
Автор

That's a very simple yet very useful tip! Thanks for sharing! Loved this shorter format with tips

laml
Автор

Good idea! Using ini files is what I often use - there is a built in configparser library to handle them easily ;)

KaJaKTrial
Автор

Also makes it a lot easier to refactor so that the configuration can be gathered from an external service or given via CL arguments! (which you inevitably will have to do if your model goes to production or you want to gather more extensive validation)

jmann
Автор

Seriously good contents, I hope every developer should do this. I've been using yaml to take care of all the parameters for our ML pipelines.

bunchofmax
Автор

This can be super useful. For example at my company we have python modules setup to be clients within a web service. This method allows us to build with our python files and set the mapping within one projects and have the config accessible in more of a utilities package great way to allow the team to do updates without us touching anything.

samjay
Автор

In Python, do you also have to make sure about being able to read/write the configuration file at run time? In compiled languages like C++ Java or Go it's additionally bad practice to store configuration details in constants because you then have to recompile your code to change them

ozoniuss
Автор

Great tip, short & helpful! Even better, even easier: YAML instead of JSON. YAML supports comments, wraps long strings, isn't fussy about commas and {}.

mikec
Автор

Good advice, I would only not call the config parameters „data“

benhbr
Автор

I think JSON lacks a lot compared to a proper config file. No comments and too many brackets. If your giving it to lots of users, theres a percentage that will be turned off by it. I think that another good way would be to user configparser which is in the python standard library. It allows for things like comments so it can be understood by not so technical users. I think that something like that would be a good video idea.

coreman
Автор

In a recent project I used a package called dacite to load a yaml file with dataclasses. Seems like something up your alley

horhay
Автор

The module configparser is also pretty cool for this

obed
Автор

EXACTELY what I needed. Thanks so much

gigagaming
Автор

I like the dotenv package for this, better for security also since keys and passwords should be outside of git repository

Manwithnoname
Автор

Hello Arjan. Maybe a little off-topic, but I think, it is a good practice to use contextmanager to open a file 🙂 Besides that, a good tip

przwar
Автор

The next question is, _where_ do you put the config files? For personal programs, you could use the XDG convention of creating your own subdirectory in ~/.config/.

lawrencedoliveiro
Автор

what I'm using right now, specially because I deal mostly with enviroment variables, is pydantic "baseSettings", which let's me load configs directly from the env and gives back the dataclass we are used to. but yeah, json is nice too!

sleao_
Автор

Another tip: sometimes you just want to be able to use the script with some sensible defaults without needing to deal with a config file. In that case, you can write your defaults at the top in a dict, and then call .update() on it with the JSON like shown here, if it exists. That also allows you to only need to put config in the file if it's different from the default.

dacid
Автор

What I like to do is define all my configs in a dataclass and use a library like dacite or apischema to hydrate the dataclass with the config values from a json file.

ayehavgunne
welcome to shbcf.ru