Week 04 / 2023

Mohamed Saif published on
5 min, 930 words

Python

  • python generic type hint
  • Generics are not just used for function and method parameters. They can also be used to define classes that can contain, or work with, multiple types. These “generic types” allow us to state what type, or types, we want to work with for each instance when we instantiate the class.
  • Data classes are just regular classes that are geared towards storing state, rather than containing a lot of logic. Also a perfect for code generators
  • My favorite documentation is objective-based: I’m trying to achieve X objective, here are some examples of how library Y can help.

Fastapi

  • docker exec -it [container] python -m pip install -r requirements-dev.txt
  • (JOSE) technologies - JSON Web Signature (JWS), JSON Web Encryption (JWE), JSON Web Key (JWK), and JSON Web Algorithms (JWA) - collectively can be used to encrypt and/or sign content using a variety of algorithms.
pyproject & toml
  • TOML values have types. TOML has no null type. TOML doesn’t include a schema language that can specify required and optional fields in a TOML document.
  • To validate toml files, use pydantic which utilizes type annotations to do data validation at runtime.
  • TOML types: [str, int, float, boolean, offset date-time, local date-time, local date, local time, array, inline table]
  • A TOML document is represented by a nameless root table that contains all other tables and key-value pairs.
  • TOML arrays represent an ordered list of values. You specify them using square brackets ([]), so that they resemble Python’s lists
  • In general, you should express an array of tables by writing table headers inside double square brackets ([[]]). equivalent to the array of inline tables
  • install tomli, with open("file.toml", "rb") as f: config = load(f)
  • If you already have the TOML document represented as a string, then you can use loads() instead of load().
  • One difference between load() and loads() is that you use regular strings and not bytes when you use the latter.
  • dotted keys
  • Tables: [regular tables with headers, dotted key tables when you need to specify a few key-value pairs that are closely tied to their parent table, inline tables {k = v, } only for very small tables with up to three key-value pairs, where the data makes up a clearly defined entity.]

EdgeDB

  • These “for loops” iterate over each element of some input set, execute some expression with it, and merge the results into a single output set.
  • The union keyword is a required part of the for statement syntax; it is intended to indicate explicitly that the results of each loop execution are ultimately merged.
  • When you make a change to the schema, the db also may need to change existing data to make it valid under the new schema. So what the migration tool is asking for here is a one-off edgeql expression that will be run when the new schema is applied, to make this change to the existing data.
  • A one to many relationship places a foreign key on the child table referencing the parent.
  • There are two kinds of events that might trigger these deletion policies: target deletion and source deletion.

Python Packaging

  • Enforcing cohesion and encapsulation through packaging
  • Promoting clear ownership of code
  • Decoupling implementation from usage
  • But it’s valuable to move slowly at first so you can move quickly later and longer as a project matures. When you’re exploring a new technology or process, it can also be helpful to practice with it first so you can use it deftly. A bit of planning up front can go a long way toward your productivity and resulting morale.
  • It’s a good practice to explicitly state the range of Python versions your package supports and test your package across all those versions.
  • By default, pip installs packages in a location related to the Python version with which pip itself was installed, known as the site packages directory.
  • Python virtual environments are an isolated copy of Python with an isolated site packages directory.
  • The Python build workflow
  • Versioning using bumpversion
  • Semantic Version
  • Flit looks for the source of the package by its import name: [tool.flit.module]. The source may be located either in the directory that holds the pyproject.toml file, or in a src/ subdirectory.
  • With Setuptools, one of the most straightforward approaches to including non-Python files is using the MANIFEST.in file. quickest ways to start is by including all files in the src/ (by graft src) directory and recursively excluding some files generated by Python.
  • Data files which your code will use should go inside the Python package folder. Flit will package these with no special configuration.
  • 1 Remove working packages from previous installs (pip uninstall -y [pkg-name])
  • 2 Build a wheel and a sdist (tarball) from the source (flit build)
  • 3 Install using Pip to ensure the package is Pip-compatible (pip install dist/[pkg-name-components].whl)
  • entry points: python.org
  • entry points: setuptools.pypa.io

Software Architecture

Testing

  • -x, exit on the first failed test/error.
  • -v, verbosity.

git

  • 1 squash all commits: git reset --soft [first-commit-hash]
  • 2 git commit --amend -m "new commit msg"
  • 3 git tag [tag-name]