Skip to content

Instantly share code, notes, and snippets.

View hngkr's full-sized avatar

Henning Kristensen hngkr

  • Aarhus, Denmark
View GitHub Profile
@hngkr
hngkr / NamedTemporaryFile_example.py
Created July 12, 2019 22:12
NamedTemporaryFile example
import boto3
import tempfile
s3_bucket = "..."
s3_path = "..."
s3 = boto3.resource('s3', region_name='eu-west-1')
with tempfile.NamedTemporaryFile() as downloaded_file:
s3.Object(s3_bucket, s3_path).download_file(downloaded_file.name)
# continue processing downloaded_file
@hngkr
hngkr / tempfile-contextmanager.py
Created July 12, 2019 22:01
tempfile.TemporaryDirectory example
with tempfile.TemporaryDirectory() as workdir:
project_dirname = "project"
project_dir = os.path.join(workdir, project_dirname)
os.mkdir(project_dir)
# do esoteric work within that temporary workdir
@hngkr
hngkr / cd.py
Created July 12, 2019 21:44
change directory source code
import contextlib
import os
@contextlib.contextmanager
def cd(path):
"""Set the working directory
Temporarily set the working directory inside the context manager and
reset it to the previous working directory afterwards
@hngkr
hngkr / cd-example.py
Created July 12, 2019 21:43
change directory example
with cd(project_dirname):
shutil.make_archive(archive_name, "zip")
@hngkr
hngkr / specific_locale-example.py
Created July 12, 2019 21:24
specific_locale example
with specific_locale(locale.LC_ALL, "en_US"):
pass # makes sure that dateformats are English
@hngkr
hngkr / specific_locale.py
Created July 12, 2019 21:19
specific_locale context manager
import contextlib
import locale
@contextlib.contextmanager
def specific_locale(category, locale_string):
prev_locale_string = locale.getlocale(category)
locale.setlocale(category, locale_string)
try:
yield
@hngkr
hngkr / setenviron-example.py
Created July 12, 2019 21:02
setenviron example
with setenviron(aws_env, PYTHONUNBUFFERED='1'):
pass # set aws_env and PYTHONUNBUFFERED before calling script
@hngkr
hngkr / setenviron.py
Created July 12, 2019 20:58
setenviron contextmanager source code
import contextlib
import os
@contextlib.contextmanager
def setenviron(envdict=None, **mapping):
"""``with`` context to temporarily modify the environment variables"""
_environ = os.environ.copy()
if envdict:
@hngkr
hngkr / vyos-optimisations
Created May 2, 2017 09:12 — forked from RafPe/vyos-optimisations
vyos throughput optimizations
Server 2 sockets,6 cores each, 2.4ghz
# Set ixgbe options
# Limit RSS queues to the number of physical cores per cpu
# Disable offload
# When you change this, you need to run the command and reboot for it to take.
echo "options ixgbe LRO=0,0 MQ=1,1 RSS=6,6 VMDQ=0,0 vxlan_rx=0,0" > /etc/modprobe.d/ixgbe.conf
# Shut down HT cores
for i in $(seq 1 2 23); do
@hngkr
hngkr / terraform.tfvars
Created December 12, 2016 20:20
tf/terraform.tfvars
s3bucketname = "hennings-roadworker-backup-bucket"
s3prefix = "roadworker/"
region = "eu-west-1"
codebuild_image = "aws/codebuild/eb-python-2.6-amazonlinux-64:2.1.3"