Python Bytes is a weekly podcast hosted by Michael Kennedy and Brian Okken. The show is a short discussion on the headlines and noteworthy news in the Python, developer, and data science space.

#296 pip: Constrain your excitement

August 09, 2022 00:32:31 31.44 MB Downloads: 0

Watch the live stream:

Watch on YouTube

About the show

Sponsored by the IRL Podcast from Mozilla

Brian #1: Pip constraints files

  • by luminousmen
  • You can put some constraints on your dependencies with a constraints file.
  • “Constraints files are requirements files that only control which version of a requirement is installed, not whether it is installed or not. “
  • Syntax is a subset of requirements.txt syntax
    • but all the restrictions seem reasonable, considering
      • must have a name
      • can’t be editable
      • can’t specify extras (that one is maybe slightly weird)
  • You can put --constraint constraints.txt right at the top of your requirements.txt file
  • or specify it on command line,
    • pip install --constraint constraints.txt -r requirements.txt
  • Or, my favorite, stick it at the top of requirements.in file.
    • yes. pip-compile correctly handles constraints when generating requirements.txt.
  • Example
    • requirements.in --constraint constraints.txt typer
    • constraints.txt click<8.1.3
    • Output from pip-compile requirements.in
      #
      # This file is autogenerated by pip-compile with python 3.10
      # To update, run:
      #
      #    pip-compile requirements.in
      #
      click==8.1.2
          # via
          #   -c constraints.txt
          #   typer
      typer==0.6.1
          # via -r requirements.in
      

Michael #2: async-cache

  • A caching solution for asyncio
  • Quite simple but looks effective and flexible too
  • Example:
    # TTL Cache
    from cache import AsyncTTL
    
    @AsyncTTL(time_to_live=60, maxsize=1024)
    async def func(*args, **kwargs):
        """
        time_to_live : max time for which a cached result  is valid
        maxsize : max number of results that are cached.
                    if  max limit  is reached the oldest result  is deleted.
        """
        pass
    

Brian #3: Organize Python code like a PRO

  • Guilherme Latrova
  • Yes, this is one author’s opinion. but…
    • lots of great advice
    • nothing too weird
    • no tool recommendations
  • Recommendations of note
    • keep a src dir.
      • A cool and simple reason: it keeps your source code together in alphabetized IDEs.
    • file/module names: plural except for config, main, core, or similar
      • slightly weird tangent that there are no files, there are modules. ok, whatever.
      • Also talking about directories as main modules. odd. but ok.
    • functions/methods should be verbs
    • variables/constants should be nouns
    • Class names should be singular, unless the class really represents a container
    • The __name__ == "__main__" trick for modules.
    • The __main__.py entry point trick for modules/packages so that -m mymodule does something.
  • -

Michael #4: keyring

  • via Trent
  • The Python keyring library provides an easy way to access the system keyring service from python. It can be used in any application that needs safe password storage.
  • It’s also helpful in that it allows you to write your own backends. Useful for testing.
  • Basically create a dummy keychain that stores to a pytest temp_path fixture instead of cluttering the real keychain.
  • You could potentially write a backend to interact with any service such as 1Password.

Extras

Brian:

  • I’m taking a class on FastAPI. The instructor is awesome!
  • Also, editing some pytest course video.

Michael:

Joke:

  • from a dad-jokes repo
    • Q: How do programming pirates pass method parameters?
    • A: Varrrrarrrgs.
    • Q: How do you get the code for the bank vault?
    • A: You checkout their branch.
    • "Unfortunately these jokes only work if you git them."
  • Screw driver