Skip to content

Instantly share code, notes, and snippets.

#### Hello
```
This is an %>% amazing() %>% gist()
```
import streamlit as st
# initialize the "count"-variable to session state
if "count" not in st.session_state:
st.session_state["count"] = 0
# clicked will be True when the button was clicked
# and False otherwise (for example if another action
# was performed or if it has not been clicked yet)
clicked = st.button("Click here!")
@FerusAndBeyond
FerusAndBeyond / dict_init.py
Last active April 20, 2022 17:13
Python dict initialization
# the most common way
my_object = {
"a": 5,
"b": 6
}
# what I prefer (avoiding { and ")
my_object = dict(a=5, b=6)
@FerusAndBeyond
FerusAndBeyond / dict_merge.py
Created April 20, 2022 17:14
Python dict merging
a = { "a": 5, "b": 5 }
b = { "c": 5, "d": 5 }
c = { **a, **b }
assert c == { "a": 5, "b": 5, "c": 5, "d": 5 }
# order matters!
# the last added will overwrite the first added
c = { **a, **b, "a": 10 }
assert c == { "a": 10, "b": 5, "c": 5, "d": 5 }
b["a"] = 10
c = { **a, **b }
@FerusAndBeyond
FerusAndBeyond / dict_pd.py
Last active April 20, 2022 18:11
Dictionaries to pandas
import pandas as pd
# list of dicts to pd.DataFrame
df = pd.DataFrame([
{ "a": 5, "b": 6 },
{ "a": 6, "b": 7 }
])
# df =
# a b
# 0 5 6
@FerusAndBeyond
FerusAndBeyond / dict_comprehension.py
Last active April 20, 2022 18:45
Dictionary comprehension
# remove keys
a = dict(a=5, b=6, c=7, d=8)
remove = set(["c", "d"])
a = { k: v for k,v in a.items() if k not in remove }
# a = { "a": 5, "b": 6 }
# keep keys
a = dict(a=5, b=6, c=7, d=8)
keep = remove
a = { k: v for k,v in a.items() if k in keep }
@FerusAndBeyond
FerusAndBeyond / dict_json.py
Created April 20, 2022 18:15
Dictionary to json
import json
a = dict(a=5, b=6)
# to json
json_string = json.dumps(a)
# json_string = '{"a": 5, "b": 6}'
# json to dict
assert a == json.loads(json_string)
@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
@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 / 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