blob: 4b5bacb2f596e2d3aa3447b7fcd69b2eeee7910a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
---
title: 'TIL: Initiate Config class in Python'
description: 'Today I Learned a neat trick to initiate a Config class from a JSON file in Python'
date: '2024-11-27T14:36:00+02:00'
tags: ['python']
---
I came across [anthonywritescode's](https://github.com/anthonywritescode) Github repository for his [twitch-chat-bot](https://github.com/anthonywritescode/twitch-chat-bot) which is written in Python. I have not written much Python in my life so maybe this is trivial, but I found it to be a neat little trick anyways.
<!--more-->
Consider you have the following JSON config file.
```jsonc
// file: config.json
{
"username": "claw0ry",
"token": "636DDA4D-A395-43C5-A2B1-7A0401DE51AB"
}
```
In the past I would probably have loaded it like this:
```python
import json
class Config():
username: str
token: str
def validate_username(self) -> bool:
return True if len(self.username) > 0 else False
with open('./config.json') as f:
d = json.load(f)
cfg = Config(username=d.username, token=d.token)
print(cfg.validate_username())
```
What we essentially are doing is that we load the `config.json` into a variable as dictionary and then we initiate a new Config class and assign each class property by arguments. With this little trick we can make Python do it for us.
```python
import json
from typing import NamedTuple
class Config(NamedTuple):
username: str
token: str
def validate_username(self) -> bool:
return True if len(self.username) > 0 else False
with open('./config.json') as f:
cfg = Config(**json.load(f))
print(cfg.validate_username())
```
The first thing to notice is that our `Config` class now takes arguments in the form of a `NamedTuple`. Then we have combined the `json.load()` with `**` when initiating our Config class. The `json.load()` method will give us a Python dictionary from the `config.json` file. We then unpack the dictionary (with `**`) into named arguments for the Config class.
It's not much, but a neat little shortcut and in my opinion it reads a little better too.
|