Skip to content

Instantly share code, notes, and snippets.

@a-recknagel
a-recknagel / docs.conf.py
Created October 29, 2020 12:32
MCVE to reproduce sphinx's classvar appending
import sys
sys.path.append('src')
project = "sphinx_test"
extensions = [
"sphinx.ext.autodoc",
"sphinx.ext.napoleon",
]
@a-recknagel
a-recknagel / debug.log
Created October 26, 2020 19:48
newrelic late license_key plugging bug debug logs
$ uvicorn app:app
DEBUG:newrelic.config:agent configuration file was newrelic.ini
DEBUG:newrelic.config:agent config app_name = 'HelloWorld'
DEBUG:newrelic.config:agent config monitor_mode = True
DEBUG:newrelic.config:register module ('greenlet', 'newrelic.core.trace_cache', 'greenlet_loaded')
DEBUG:newrelic.config:register module ('asyncio', 'newrelic.core.trace_cache', 'asyncio_loaded')
DEBUG:newrelic.config:instrument module (<module 'asyncio' from '/home/user/python/lib/python3.8/asyncio/__init__.py'>, 'newrelic.core.trace_cache', 'asyncio_loaded')
DEBUG:newrelic.config:register module ('asyncio.base_events', 'newrelic.hooks.coroutines_asyncio', 'instrument_asyncio_base_events')
DEBUG:newrelic.config:instrument module (<module 'asyncio.base_events' from '/home/user/python/lib/python3.8/asyncio/base_events.py'>, 'newrelic.hooks.coroutines_asyncio', 'instrument_asyncio_base_events')
DEBUG:newrelic.config:register module ('asyncio.events', 'newrelic.hooks.coroutines_asyncio', 'instrument_asyncio_events')
@a-recknagel
a-recknagel / app.py
Last active October 28, 2020 21:36
fastapi + newrelic mcve with late license_key plugging bug
#import os
#os.environ["NEW_RELIC_LICENSE_KEY"] = "some_valid_key"
import sys
import logging
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
import newrelic.agent
newrelic.agent.initialize("newrelic.ini")
@a-recknagel
a-recknagel / pyproject.toml
Created October 1, 2020 13:11
pyproject file to reproduce faulty export lockfiles
[tool.poetry]
name = "foo"
version = "0.1.0"
description = ""
authors = ["Your Name <[email protected]>"]
[tool.poetry.dependencies]
python = "^3.8"
fastapi = "^0.60"
@a-recknagel
a-recknagel / NewRelicMiddleWare.py
Last active August 6, 2020 09:05
A starlette middleware for newrelic agent integration
import newrelic.agent
from starlette.types import ASGIApp, Receive, Scope, Send
def get_transaction_name(middleware, scope: Scope, *args, **kwargs):
# Strip off the leading slash
name = scope.get("path", "/no-path")
if name == "/":
return ""
return name[1:]
@a-recknagel
a-recknagel / interactive_session.py
Last active June 22, 2020 09:38
log arg decorator
>>> from logging.config import dictConfig
>>> dictConfig(
... {
... "version": 1,
... "disable_existing_loggers": False,
... "formatters": {
... "standard": {
... "format": "%(asctime)s [%(levelname)s] %(name)s: %(message)s"
... }
... },
@a-recknagel
a-recknagel / parse_csv.py
Created March 2, 2020 23:01
rewrite 4th column
import csv
def main():
old_file = input('please give file name: ')
new_file = old_file + '.updated'
with open(old_file) as old, open(new_file, 'w') as new:
writer = csv.writer(new)
for row in csv.reader(old):
if row[4] == 'MIS':
row[3] = row[3].replace('.com', '.net')
@a-recknagel
a-recknagel / app.py
Created February 19, 2020 15:03
honestly who still uses xml as data protocol?
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route("/", methods=['POST'])
def foo():
return jsonify([*request.form.keys()])
@a-recknagel
a-recknagel / inherit_docs.py
Last active December 10, 2019 12:42
inherit docs from superclasses via decorator
import types
def inherit_docstrings(_cls=None, *, overload=True):
def wrap(cls):
for name, member in vars(cls).items():
if not isinstance(member, types.FunctionType):
continue # only consider methods
if member.__doc__:
continue # skip documented methods
@a-recknagel
a-recknagel / unreasonable_dog.py
Created December 10, 2019 07:51
getting around access restrictions
class Dog:
__slots__ = ["foo"]
def __setattr__(self, name, value):
raise TypeError("(•ˋ _ ˊ•) no touch, only throw.")
d = Dog()
# your one line of code here
assert type(d) is Dog
print(d.foo) # "take stick"