Skip to content

Instantly share code, notes, and snippets.

@fredrikaverpil
fredrikaverpil / normalize_timedelta.py
Last active November 6, 2022 09:34
Python timedelta does not take DST into account
from datetime import datetime, timedelta
from pytz import timezone as pytz_timezone
def normalize_timedelta(dt: datetime, delta: timedelta) -> datetime:
"""Normalize timedelta operation, to preserve DST properly."""
if hasattr(dt.tzinfo, "zone"):
timezone_ = dt.tzinfo.zone
return pytz_timezone(timezone_).normalize(dt + delta)
@fredrikaverpil
fredrikaverpil / list.ipynb
Created November 4, 2022 18:13
Pattern matching gotchas
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@fredrikaverpil
fredrikaverpil / Dockerfile
Last active July 15, 2022 14:21
Error when installing grpcio on Python 3.11.0b4
FROM python:3.11.0b4-slim-bullseye
RUN apt-get update && apt-get -y install libpq-dev git
RUN apt-get -y install build-essential
WORKDIR /workdir
# create virtual environment and activate it
RUN python -m venv /opt/venv
ENV VIRTUAL_ENV="/opt/venv" \
@fredrikaverpil
fredrikaverpil / poetry_install_ddtrace.log
Created July 14, 2022 19:37
Ddtrace 1.2.2 and Python 3.11.0b4
> [compile-image 10/10] RUN poetry install --only main:
#0 0.832 Installing dependencies from lock file
#0 1.480
#0 1.480 Package operations: 47 installs, 0 updates, 0 removals
#0 1.480
...
...
#0 25.15
@fredrikaverpil
fredrikaverpil / poetry.lock
Created June 19, 2022 05:21
Poetry update with "--only dev" also updates prod deps
[[package]]
name = "click"
version = "8.1.3"
description = "Composable command line interface toolkit"
category = "dev"
optional = false
python-versions = ">=3.7"
[package.dependencies]
colorama = {version = "*", markers = "platform_system == \"Windows\""}
@fredrikaverpil
fredrikaverpil / filter_logger.py
Created December 6, 2021 07:33
Filter out sensitive information from logs
import logging
import re
class RedactingFilter(logging.Filter):
def __init__(self, patterns):
super(RedactingFilter, self).__init__()
self._patterns = patterns
def filter(self, record):
@fredrikaverpil
fredrikaverpil / pyproject.toml
Created November 30, 2021 18:32
pandas poetry
[tool.poetry]
name = "pandastest"
version = "0.1.0"
description = ""
authors = []
[tool.poetry.dependencies]
python = "^3.10"
pandas = "^1.3.4"
@fredrikaverpil
fredrikaverpil / 01_sudo_apt_update.sh
Created September 28, 2021 07:44
Live Ubuntu 20.04.3 LTS package installation issues
ubuntu@ubuntu:~$ sudo apt update
Ign:1 cdrom://Ubuntu 20.04.3 LTS _Focal Fossa_ - Release amd64 (20210819) focal InRelease
Hit:2 cdrom://Ubuntu 20.04.3 LTS _Focal Fossa_ - Release amd64 (20210819) focal Release
Hit:4 http://archive.ubuntu.com/ubuntu focal InRelease
Hit:5 http://archive.ubuntu.com/ubuntu focal-updates InRelease
Hit:6 http://security.ubuntu.com/ubuntu focal-security InRelease
Reading package lists... Done
Building dependency tree
Reading state information... Done
125 packages can be upgraded. Run 'apt list --upgradable' to see them.
def _my_decorator(func):
def wrapper(*args, **kwargs):
print("wrapper got args...", args)
print("wrapper got kwargs...", kwargs)
print("Something is happening before the function is called.")
func(*args, **kwargs)
print("Something is happening after the function is called.")
return wrapper
@fredrikaverpil
fredrikaverpil / ftpupload.py
Created February 4, 2019 11:06
FTP upload with Python
"""Upload file to folder.
Note:
Written for Python 3.7.
"""
import os
import ftplib