Skip to content

Instantly share code, notes, and snippets.

View M0r13n's full-sized avatar
🦔

Leon Morten Richter M0r13n

🦔
View GitHub Profile
@M0r13n
M0r13n / orderable.py
Last active July 8, 2019 14:27
Sqlalchemy Mixin class to make database models comparable and sortable by users.
class OrderableMixin(object):
""" Mixin to make database models comparable """
order_index = db.Column(db.Integer, default=_default_index, index=True)
@classmethod
def normalize(cls):
""" Normalize all order indexes """
for idx, item in enumerate(cls.query.order_by(cls.order_index).all()):
item.order_index = idx
db.session.commit()
@M0r13n
M0r13n / simple_factorial.c
Last active August 15, 2019 13:25
Super simple recursive factorial function in C.
#include <stdio.h>
#include <stdlib.h>
long factorial(long n) {
if (n == 0)
return 1;
else
return (n * factorial(n - 1));
}
@M0r13n
M0r13n / analyze.py
Created September 5, 2019 10:18
Analyze an exported Chat from WhatsApp with Pandas! Extract fascinating information from your WhatsApp chats.
import re
import pandas as pd
DATE_TIME = re.compile(r"([(\[]).*?([)\]])")
AUTHOR = re.compile(r"((( [a-zA-ZöäüÖÄÜ]+)+):)")
LTR = chr(8206)
def to_pd_row(s):
match = DATE_TIME.match(s)
@M0r13n
M0r13n / lib.js
Created September 17, 2019 07:37
Steal...
/* Copyright 2017 Google Inc. */
/**
* Swaps the display settings of two elements. Before calling, exactly one
* of the two elements should have style="display:none" and it shows the one
* that is hidden and hides the one that is shown.
*/
function _showHide(id1, id2) {
var x1 = document.getElementById(id1);
@M0r13n
M0r13n / install.bat
Created November 19, 2019 14:58
Install a Python project offline without any internet connection.
@echo off
:: Basic welcome message
echo Dieses Skript wird die grundlegenden Bestandteile installieren.
echo Nach der Installation sollte sich das Programm ohne Probleme nutzen lassen.
echo.
:: Are you sure prompt
setlocal
:PROMPT
@M0r13n
M0r13n / bla.html
Created December 4, 2019 19:05
Tooooooooooooooooooooooooooooom
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
text{
font-family:Helvetica, Arial, sans-serif;
font-size:11px;
pointer-events:none;
@M0r13n
M0r13n / lazy.js
Last active January 6, 2020 13:28
A simple JS function I use for lazy loading CSS files with Flask.
function lazyLoad(element, type, rel, link, crossOrigin = "anonymous", integrity = false) {
var x = document.createElement(element);
x.type = type;
x.rel = rel;
x.href = link;
// subresource integrity for external content from different domains
if (integrity) {
x.crossOrigin = crossOrigin;
x.integrity = integrity;
@M0r13n
M0r13n / calc.c
Last active January 13, 2020 12:48
Evaluate a string in Prefix Notation (Polish Notation) and calculate the result. Interact with a user through a simple REPL.
/**
* A simple REPL for Polish Notation / Prefix Notation.
* Compile with:
* gcc -std=c99 -Wall calc.c -ledit -o calc
*
* Examples:
* -----------
* calc> * / + 2 2 2 + 1 2
* Result is: 6.000000
*
@M0r13n
M0r13n / demo.py
Created February 20, 2020 11:06
Python caches hashes of strings
data = "ABCDEFG1234" * 1000000000
x = lambda: hash(data)
import timeit
for i in range(10):
print(f"#{i} took {timeit.timeit(x, number=1)} secs")
@M0r13n
M0r13n / self_cleaning_dict.py
Created March 4, 2020 14:05
A simple self cleaning Python dictionary (dict) with timestamps.
import time
class SelfCleaningDict(dict):
def __init__(self, time_out=3600, prone_interval=None):
self.timeout = time_out
self.prone_interval = time_out // 10 or prone_interval
self.last_prone = None
if not self.timeout: