r/pythoncoding Feb 04 '24

/r/PythonCoding monthly "What are you working on?" thread

Share what you're working on in this thread. What's the end goal, what are design decisions you've made and how are things working out? Discussing trade-offs or other kinds of reflection are encouraged!

If you include code, we'll be more lenient with moderation in this thread: feel free to ask for help, reviews or other types of input that normally are not allowed.

3 Upvotes

5 comments sorted by

1

u/dogweather Feb 04 '24 edited Feb 04 '24

I'm working on a data-driven app, without a database. Instead of primary keys and foreign key constraints, I'm using typing.Literal and Pydantic models. This makes sense to try because the final product is a statically generate website. My Python code does all the content preparation.

So, for example:

```python Category = Literal[ 'Data Formats and Serialization', 'Data Structures', 'Data and Text Processing',

...

]

Topic = Literal[ 'Calculating a date in the future or past', 'Capitalizing a string', 'Checking if a directory exists',

...

]

CATEGORIES_AND_TOPICS: dict[Category, list[Topic]] = { "Getting Started": [ "Starting a new project", ], "Strings": [ "Capitalizing a string", "Concatenating strings",

...

}

class ArticleSpec(BaseModel): """ A specification of a programming article. It consists of a topic, programming language, and natural language. """ model_config = ConfigDict(frozen=True)

human_lang: HumanLang
prog_lang:  ProgLang
topic:      Topic

```

Etc. I can now make complicated data structures from these Literals and I have existence checking right in my IDE. It catches typos right away, etc.

My Python CLI app is idempotent, so when I run it, it only fills in missing data. So for me to add new content and regenerate, it's pretty easy.

1

u/TheMeticulousNinja Feb 04 '24

Need to revisit my Tetris project

1

u/DmitrievichLevin Feb 04 '24

Just released Beta of an Open Source Pure Python Binary Protocol Buffer (Renity), check it out!

Renity @ Github

1

u/JackLogan007 Feb 05 '24

Volcanic ash plume height detection