Skip to content

Instantly share code, notes, and snippets.

View cb109's full-sized avatar
💭
🍴 🐘

Christoph Bülter cb109

💭
🍴 🐘
View GitHub Profile
@cb109
cb109 / tokenstring.py
Last active October 3, 2016 21:37
Resolve tokens in strings
# :coding: utf-8
"""Replace tokens in strings."""
import re
DOUBLE_CURLY_BRACED_TOKEN = r"{{\s*(\w+)\s*}}" # E.g.: '{{ token }}'
@cb109
cb109 / django-rest-boilerplate.py
Last active June 13, 2017 14:52
Generate code to expose Django models via the django-rest-framework
# !/bin/python
# -*- coding: utf-8 -*-
"""
Generate Django boilerplate code to expose models via a REST API.
Will generate code for the view, serialization and url routing. Results
are written to stdout; redirect them to a file and integrate them into
your project (views.py, serializers.py, urls.py) to get started quickly.
@cb109
cb109 / breakpoint.js
Last active May 5, 2022 15:40
Vue Breakpoints Mixin (also available via npm: https://www.npmjs.com/package/vue-md-breakpoint)
// Now officially integrated into Vuetify:
//
// https://github.com/vuetifyjs/vuetify/blob/master/src/components/VApp/mixins/app-breakpoint.js
// https://github.com/vuetifyjs/vuetify/blob/master/src/components/VApp/mixins/app-breakpoint.spec.js
/**
* A Vue mixin to get the current width/height and the associated breakpoint.
*
* Useful to e.g. adapt the user interface from inside a Vue component
* as opposed to using CSS classes. The breakpoint pixel values and
@cb109
cb109 / authentication.py
Last active January 25, 2023 08:42
Setting up DRF Token authentication with basic expiration
from datetime import timedelta
from django.conf import settings
from django.utils import timezone
from rest_framework.authentication import TokenAuthentication
from rest_framework.authtoken.models import Token
from rest_framework.exceptions import AuthenticationFailed
def is_token_expired(token):
@cb109
cb109 / toggl_vacation.py
Created August 21, 2017 08:53
Batch create Toggl time entries for vacation
# -*- coding: utf-8 -*-
"""
A simple script to batch-create Toggl time entries for a vacation.
Note: Time zones and offsets are not handled here.
# Prerequisites
@cb109
cb109 / colored_log_setup.py
Last active March 8, 2021 00:10
logging.dictConfig for colored logging
# Prerequisites:
#
# $ pip install colorama colorlog
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters' : {
'colored': {
'()': 'colorlog.ColoredFormatter',
@cb109
cb109 / example_output.txt
Last active September 17, 2017 12:10
The Patrician II: OCR based Trade Recommendations
_________________________________________________
___ __ _ _ ________
/ _ \___ _/ /_____(_)___(_)__ ____ / _/ _/
/ ___/ _ `/ __/ __/ / __/ / _ `/ _ \ _/ /_/ /
/_/ \_,_/\__/_/ /_/\__/_/\_,_/_//_/ /___/___/
_________________________________________________
You should buy in this town:
beer for max. 52
@cb109
cb109 / conftest.py
Last active August 19, 2021 07:44
Run and hold pytest fixtures until cancelled from commandline. Useful to manually debug/test a specific database state etc.
def pytest_addoption(parser):
parser.addoption(
"--only-fixtures",
action="store",
help=(
"Execute the given fixture(s) and hold that state until "
"manually cancelled (Ctrl+C)."
),
)
@cb109
cb109 / urlState.js
Last active November 3, 2017 13:56
vue-router: Map state to URL query
/**
* A Vue mixin to map local component state to URL query.
*
* Uses vue-router's $route.query to get/set the query params.
*
* Useful e.g. to store and restore filters to/from the URL.
*
*/
var urlState = {
data() {
@cb109
cb109 / example_usage.py
Created December 12, 2017 13:25
Python per-module excepthooks
from .hook import register_module_excepthook
from .hook import install_global_excepthook
def some_module_function_that_may_fail():
raise ValueError("whoops!")
def module_excepthook(type_, value, tb):
print("Gosh, " + __name__ + " has produced an error!")