same: Using Poetry to Change Python Versions and Create New Virtual Environment
Posts for: #Python
Use multiple context managers in Python
In Python, you can use multiple context managers on the same block. To do that, simply separate the context manager declarations with a comma.
Using Poetry to Change Python Versions and Create New Virtual Environment
With Poetry, to change an underlying Python version (for testing, etc.) and create a new virtualenv on top of that, use poetry env use /PATH/TO/VERSION/BINARY. For pyenv, this…
Testing Python Code with Pytest
To mock outside function calls with Pytest, install the pytest-mock package and use mocker as a parameter to your test function. Then you set the patch with the following…
Force a Python Function to Only Accept Keyword Arguments
In Python 3, you can force a function to only accept keyword arguments (even without default values) by making the first parameter an asterisk .
Unpacking Object Parameters in Python
In Python, if two objects have the same parameters, you can unpack the output of the dict method of an instance of one into the constructor of another to transmute an object.
Imports and Class Instances
In Python, if you return a class instance, that instance has everything a consumer needs to use it. So for instance, if I pass a PynamoDB model object (indicating a document in…
PynamoDB Arguments
In PynamoDB, when running a DynamoDB query, the first two positional arguments are the hash key (primary key) and filters for the range key (sort key). To do a filter…
TypedDicts in Python
In Python 3.8+, you can create a dictionary with type annotations with the TypedDict class in the typing module. This is a regular dictionary that allows you to define it with…
Setting Pynamo Model’s Table After Instantiation
In some cases you may not know the table for a Pynamo model until you instantiate it. You can’t do this when you construct the object, but you can create a static method that…