Skip to content

Instantly share code, notes, and snippets.

@FerusAndBeyond
FerusAndBeyond / main.py
Created April 30, 2022 19:53
env-variable part 6
import os
import time
for k in ["MY_PUBLIC_VARIABLE", "MY_SECRET_VARIABLE"]:
print(k, os.getenv(k))
@FerusAndBeyond
FerusAndBeyond / docker-compose.yml
Last active April 30, 2022 20:52
env-variable part 5
version: '3'
services:
my_component:
# Here you can specify an env-file with variables
# that can be used in the docker-container,
# BUT these variables can not be used here in the
# docker-compose using ${...} UNLESS it is a file named
# ".env". This file (in the same directory) is always
# loaded into the docker-compose, but not into the
# docker-containers unless the variables are mapped in the
@FerusAndBeyond
FerusAndBeyond / .env
Created April 30, 2022 19:42
env-variable part 4
MY_SECRET_VARIABLE = "I'm batman!"
@FerusAndBeyond
FerusAndBeyond / env-dockerfile
Created April 30, 2022 19:40
env-variable part 3
FROM python:3.8-alpine
COPY app .
CMD python -u main.py
@FerusAndBeyond
FerusAndBeyond / env_vars2.py
Created April 29, 2022 18:26
env-variable part 2
from dotenv import load_dotenv
import os
# by default .env will be loaded
load_dotenv()
# if we had another file called .env2
# we could load it using
# load_dotenv(".env2")
# now we can use os.getenv as before:
@FerusAndBeyond
FerusAndBeyond / env_vars.py
Last active May 2, 2023 09:28
env-variable part 1
import os
# first way
os.environ['TERM_PROGRAM']
# => 'Apple_Terminal'
# a non-existing variable will lead to error
os.environ["THIS_VARIABLE_DOESNT_EXIST"]
# => KeyError: 'THIS_VARIABLE_DOESNT_EXIST'
# second way
@FerusAndBeyond
FerusAndBeyond / cross_corr2.py
Created April 21, 2022 19:59
Cross-correlation part2
from scipy.signal import correlate
from scipy.signal import correlation_lags
# Function to calculate cross-correlation,
# extract the best matching shift and then shift
# one of the series appropriately.
def shift_for_maximum_correlation(x, y):
correlation = correlate(x, y, mode="full")
lags = correlation_lags(x.size, y.size, mode="full")
lag = lags[np.argmax(correlation)]
@FerusAndBeyond
FerusAndBeyond / cross_corr1.py
Last active April 21, 2022 20:18
Cross-correlation part 1
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# Generate two series that are correlated
original_x = pd.Series(np.random.uniform(size=100))
original_y = 1.3*original_x + np.random.normal(0, 0.1, size=100)
# Now create shifted versions,
# I create two examples, one where x is shifted
@FerusAndBeyond
FerusAndBeyond / dict_defaultdict.py
Last active April 20, 2022 18:44
Default-dicts
from collections import defaultdict
# by default a dict
a = defaultdict(dict)
assert a[5] == {}
a[5]["a"] = 5
assert a[5] == { "a": 5 }
# by default a list
a = defaultdict(list)
@FerusAndBeyond
FerusAndBeyond / dict_counter.py
Last active April 20, 2022 18:54
Using the Counter-class
from collections import Counter
import random
counter = Counter()
# add lists with random number of a, b and c.
counter.update([random.choice(["a", "b", "c"]) for _ in range(100)])
counter.update([random.choice(["a", "b"]) for _ in range(100)])
# merge with counts
counter.update({ "a": 10000, "b": 1 })
# add directly