Skip to content

Commit 6b67f0b

Browse files
committed
parsing log w/pydantic, typer cli
1 parent f315c34 commit 6b67f0b

File tree

8 files changed

+295
-1
lines changed

8 files changed

+295
-1
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__pycache__

cli.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from pathlib import Path
2+
3+
import typer
4+
from devtools import debug
5+
6+
from library.parse import parse_log
7+
8+
app = typer.Typer()
9+
10+
11+
@app.command()
12+
def cmd_parse_log():
13+
log_path = Path("./log.toml")
14+
debug(parse_log(log_path))
15+
16+
17+
if __name__ == "__main__":
18+
app()

library/models.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from typing import Annotated, List, Literal, Union
2+
3+
from pydantic import BaseModel, Field
4+
5+
6+
class User(BaseModel):
7+
name: str
8+
github_username: str | None
9+
10+
11+
class Book(BaseModel):
12+
id: str
13+
isbn: int
14+
url: str | None
15+
cover_image: str | None
16+
17+
18+
class BaseAction(BaseModel):
19+
pass
20+
21+
22+
class AddUser(BaseAction, User):
23+
action_type: Literal["AddUser"]
24+
25+
26+
class AddBook(BaseAction):
27+
action_type: Literal["AddBook"]
28+
user: str
29+
book: Book
30+
31+
32+
class LendBook(BaseAction):
33+
action_type: Literal["LendBook"]
34+
from_user: str
35+
to_user: str
36+
book_id: str
37+
38+
39+
class Action(BaseModel):
40+
__root__: Annotated[
41+
Union[AddUser, AddBook, LendBook], Field(discriminator="action_type")
42+
]
43+
44+
45+
class Log(BaseModel):
46+
actions: List[Action]

library/parse.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from pathlib import Path
2+
3+
import toml
4+
5+
from library.models import Log
6+
7+
8+
def parse_log(log_path: Path) -> Log:
9+
return Log(**toml.loads(log_path.read_text()))

log.toml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[[actions]]
2+
action_type = 'AddUser'
3+
name = 'jack'
4+
github_username = 'jackharrhy'
5+
6+
[[actions]]
7+
action_type = 'AddUser'
8+
name = 'marty'
9+
github_username = 'lilmert'
10+
11+
[[actions]]
12+
action_type = 'AddBook'
13+
user = 'jack'
14+
book = { id = 'dop', isbn = 9781617298578, url = 'https://www.manning.com/books/data-oriented-programming', cover_image_url = 'https://images.manning.com/360/480/resize/book/0/76cfc61-95fd-4c0d-b221-0e5c3ca90583/Sharvit-DOP-HI.png' }
15+
16+
[[actions]]
17+
action_type = 'AddBook'
18+
user = 'marty'
19+
book = { id = 'ddd', isbn = 9781521822807, cover_image_url = 'https://images-na.ssl-images-amazon.com/images/I/41gvS8EAHoL._SX404_BO1,204,203,200_.jpg' }
20+
21+
[[actions]]
22+
action_type = 'LendBook'
23+
from_user = 'marty'
24+
to_user = 'jack'
25+
book_id = 'ddd'

main.py

-1
This file was deleted.

poetry.lock

+192
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ authors = ["Jack Arthur Harrhy <[email protected]>"]
66

77
[tool.poetry.dependencies]
88
python = "^3.10"
9+
pydantic = "^1.10.2"
10+
toml = "^0.10.2"
11+
typer = "^0.6.1"
12+
devtools = "^0.9.0"
913

1014
[tool.poetry.dev-dependencies]
1115

0 commit comments

Comments
 (0)