Skip to content

Instantly share code, notes, and snippets.

@hvgab
hvgab / prettyprint_json_jinja.py
Created January 26, 2018 16:24
Add a json prettyprint filter for jinja
# prettyprint json i jinja
def ppjson(value, indent=2):
return json.dumps(value, indent=indent)
jinja2.filters.FILTERS['ppjson'] = ppjson
@hvgab
hvgab / try import colorama
Created April 4, 2018 00:39
try import colorama
# *if* colorama is installed, get colors
try:
from colorama import Fore, Back, Style, init
init(autoreset=True)
# fallback so that the imported classes always exist
except ImportError:
class ColorFallback():
__getattr__ = lambda self, name: ''
@hvgab
hvgab / main_for_multiple.py
Created September 30, 2018 00:06
Auto-detect modules to extend hug api
import hug
from werkzeug.utils import find_modules
@hug.get('/hi')
def say_hi():
return "Hi from root"
@hug.extend_api('/something')
@hvgab
hvgab / generic_list.html
Created March 10, 2019 22:49
Dynamic ListView for Django while developing/making proof of concept
{% extends 'base.html' %}
{% load my_extras %}
{% block content %}
<section class="section container">
<table class="table">
<thead>
<tr>
{% for field in fields %}
from django.db import models
from django.db.models import Count
class Game(models.Model):
"""A game to be played and/or owned"""
name = models.CharField(max_length=255)
class Player(models.Model):
"""Someone who plays games"""
name = models.CharField(max_length=255)
# Backoffice script
# We got a file from some system, but before it could be passed on the file had to be split from sheets into separate files.
from xlrd import open_workbook
from xlwt import Workbook
""" Split sheets in excel to different files. """
rb = open_workbook('path/file.xls',formatting_info=True)
#!/usr/bin/python
"""
04.12.2015
Based on "tag" in filename, move file into directory named with the same tag.
"""
import sys, os, time, shutil, re
path = './' #this folder
"""
Comment from today, script from years ago.
Not sure if I ever fixed this.
This script was suppose to loop through a shit-ton of excel like files;
Get all the headers, make a list of all the headers it encountered, and then report back with how many times a header was encountered.
Everything gets written to files at the end.
- Heads with counter
- Errors
- Files it could not read (not excel/csv
I think it would silently fail on the different types of csv-dialects it could encounter.