Skip to content

Instantly share code, notes, and snippets.

@hofrob
hofrob / iterate_performance.py
Created February 8, 2023 10:46
Compare timings of iterating over sets, lists, tuples and ranges
import random
import string
import time
import typing
def create_random_list(iterator_length: int) -> list[str]:
random_list = []
for i in range(iterator_length):
random_list.append(
@hofrob
hofrob / match_astroid_examples.py
Created November 13, 2022 20:00
Examples on how to use `match` when walking through an AST using astroid
def _walk_through(node: astroid.NodeNG):
for symbol in node.get_children():
match symbol:
# check all classes
case astroid.ClassDef():
_check_classes(symbol)
# check all functions with name "some_function_name"
case astroid.FunctionDef(name="some_function_name"):
_check_some_function_name(symbol)
case astroid.Expr(
@hofrob
hofrob / string_list_join.py
Created November 1, 2022 13:08
Compare timings of joining a list of strings vs adding strings onto strings
import random
import string
import time
values = []
for i in range(10):
values.append("".join(random.choices(string.ascii_letters + string.digits, k=10)))
n = 500000
print(f"Construct {n=} strings with these random values: {values=}")
@hofrob
hofrob / dict_perf.py
Last active April 1, 2024 15:22
Compare timings of dict constructor with literal dict
import random
import string
import time
values = []
for i in range(10):
values.append("".join(random.choices(string.ascii_letters + string.digits, k=10)))
n = 500000
print(f"Construct {n=} dicts with these random values: {values=}")
@hofrob
hofrob / filter
Last active December 1, 2023 06:16
Odoo Logstash Filter
filter {
if [type] == "odoo" {
grok {
match => { "message" => "%{ODOOLOG}" }
}
date {
match => [ "timestamp", "yyyy-MM-dd HH:mm:ss,SSS" ]
}
}
}