I don't like multi-line dictionaries. They're ugly, take up too much space, and distract me from the real logic in the code. I could put it all on one line but then that would break line length convention and be unreadable. In most dictionaries only a few values vary and the rest are constants you type once. This package moves those values into a template you declare once, leaving the call site with just what changes. Nothing crazy, just a small OCD annoyance solved.
from packd import packd, template
@template
def conn(host: str, port: int, *, timeout: int = 30, ssl: bool = True): ...
packd("conn", "localhost", 5432)
# {"host": "localhost", "port": 5432, "timeout": 30, "ssl": True}Parameters before the asterisk are passed in, matched by position. Keyword parameters are filled from their defaults, and can be overriden per call.ridden per call:
packd("conn", "localhost", 5432, timeout=5)Sometimes you need to alias the keys in your dictionary, so you can either convert
them to a case found in the CaseEnum class or alias them individually. You can
do both, the alias always wins.
from packd import CaseEnum
@template(case=CaseEnum.CAMEL, aliases={"content_type": "Content-Type"})
def headers(token: str, *, content_type: str = "application/json", read_timeout: int = 30): ...
packd("headers", "supersecrettoken")
# {"token": "abc", "Content-Type": "application/json", "readTimeout": 30}The templates register on import, so a template in a file nothing imports won't be found.