Skip to content

48th Week of 2021

Coding

Python

Pydantic Field Types

  • New: Using constrained strings in list attributes.

    import re
    import pydantic
    from pydantic import Field
    from typing import List
    
    class Regex(pydantic.ConstrainedStr):
        regex = re.compile("^[0-9a-z_]*$")
    
    class Data(pydantic.BaseModel):
        regex: List[Regex]
    
    data = Data(**{"regex": ["abc", "123", "asdf"]})
    print(data)
    print(data.json())
    

Pydantic Factories

  • New: Introduce the awesome, life saving library pydantic_factories.

    Pydantic factories is a library offers powerful mock data generation capabilities for pydantic based models and dataclasses. It automatically creates FactoryBoy factories from a pydantic model.

    from datetime import date, datetime
    from typing import List, Union
    
    from pydantic import BaseModel, UUID4
    
    from pydantic_factories import ModelFactory
    
    class Person(BaseModel):
        id: UUID4
        name: str
        hobbies: List[str]
        age: Union[float, int]
        birthday: Union[datetime, date]
    
    class PersonFactory(ModelFactory):
        __model__ = Person
    
    result = PersonFactory.build()
    

Package Management

  • New: Compare Poetry, Pipenv and PDM package management tools.

    Pipenv has broad support. It is an official project of the Python Packaging Authority, alongside pip. It's also supported by the Heroku Python buildpack, which is useful for anyone with Heroku or Dokku-based deployment strategies.

    Poetry is a one-stop shop for dependency management and package management. It simplifies creating a package, managing its dependencies, and publishing it. Compared to Pipenv, Poetry's separate add and install commands are more explicit, and it's faster for everything except for a full dependency install.

    I liked Poetry most, and in the end I didn't analyze pdm.

Goodconf

  • New: Introduce goodconf the pyndantic YAML friendly configuration management.

    goodconf is a thin wrapper over Pydantic's settings management. Allows you to define configuration variables and load them from environment or JSON/YAML file. Also generates initial configuration files and documentation for your defined configuration.

Pipenv

  • New: Introduce the pipenv package manager.

    Pipenv is a tool that aims to bring the best of all packaging worlds (bundler, composer, npm, cargo, yarn, etc.) to the Python world.

Poetry

  • New: Deeply introduce Poetry, a python package manager.

    Poetry is a command line program that helps you declare, manage and install dependencies of Python projects, ensuring you have the right stack everywhere.