Skip to content

Instantly share code, notes, and snippets.

@slinkp
slinkp / gist:c3ec1f47d7ecfe682ad4
Last active August 10, 2023 10:11
How to use Mock Specs Properly with Classes
"""
TL/DR:
Never instantiate mock.Mock() directly.
Instead use either mock.create_autospec(YourClass) OR mock.patch('YourClass', autospec=True).
The "spec" feature of Mock is great, it helps avoid your mocked API drifting out of sync with your real API.
But there is a gotcha if you are mocking classes directly - it's easy to mock the class but you need to
ensure the spec applies to the *instance* as well.
"""
@M0r13n
M0r13n / README.md
Last active June 22, 2024 00:14
Logging with Loguru in Flask

This is a simple example of how to use loguru in your flask application

Just create a new InterceptHandler and add it to your app. Different settings should be configured in your config file, so that it is easy to change settings.

Logging is then as easy as:

from loguru import logger

logger.info("I am logging from loguru!")

backend httpbin
balance roundrobin
mode http
server master 192.168.50.10:32009 check check-sni httpbin.example.com sni str(httpbin.example.com) ssl verify none
http-request set-header Host httpbin.example.com
http-request set-header X-Forwarded-For %[src]
http-request set-header X-Forwarded-Port %[dst_port]
http-request add-header X-Forwarded-Proto https if { ssl_fc }
@DomWeldon
DomWeldon / README.md
Last active January 25, 2023 17:39
SSM + pydantic: ARNs in environment variables are queried at load time

SSM + Pydantic

Query values from SSM when deployed, by placing an SSM ARN as the environment variable

Background

I wanted to query secrets from SSM at runtime, to laod them into a pydantic.BaseSettings settings object, but still be able to pass standard values during development (and I guess, if I want, in prod).

I've done a couple of similar implementations before, but they have always felt clunky and involved altering the object after instantiation, or hard coding which values to take out of SSM.

@tommyip
tommyip / venv.fish
Last active July 16, 2024 14:06
venv.fish - Automatically activate/deactivate virtualenv in fish shell
# Based on https://gist.github.com/bastibe/c0950e463ffdfdfada7adf149ae77c6f
# Changes:
# * Instead of overriding cd, we detect directory change. This allows the script to work
# for other means of cd, such as z.
# * Update syntax to work with new versions of fish.
# * Handle virtualenvs that are not located in the root of a git directory.
function __auto_source_venv --on-variable PWD --description "Activate/Deactivate virtualenv on directory change"
status --is-command-substitution; and return
@atheiman
atheiman / config-aggregator-search.sh
Last active July 8, 2024 01:33
Get all EC2 instances from an AWS Config Aggregator using AWS CLI
aws configservice batch-get-aggregate-resource-config \
--configuration-aggregator-name 'MyConfigAggregator' \
--resource-identifiers "$(
aws configservice list-aggregate-discovered-resources \
--configuration-aggregator-name 'MyConfigAggregator' \
--resource-type 'AWS::EC2::Instance' \
--query 'ResourceIdentifiers'
)"