Skip to content

Instantly share code, notes, and snippets.

View gamesbook's full-sized avatar

Derek Hohls gamesbook

  • Independant Contractor
  • South Africa
View GitHub Profile
@gamesbook
gamesbook / mysql2excel.py
Last active May 25, 2019 08:39
Save MySQL data to an Excel file
# -*- coding: utf-8 -*-
"""
Purpose: Import data from a MySQL database to an Excel file
Created: 2019-05-25
Authors: Derek Hohls <gamesbook@gmail.com>
"""
import getpass
# require pip install:
import MySQLdb
from xlsxwriter.workbook import Workbook
@gamesbook
gamesbook / validate_kwargs.py
Last active May 25, 2019 06:05
Validate kwargs in a Python function
def compare(a, b, *, key=None, **kwargs):
"""Assumption here is that you only want to handle kwargs x, y or z"""
print(a, b, key, kwargs)
if list(set(kwargs.keys()).difference(['x','y','z'])):
inv = ','.join(list(set(kwargs.keys()).difference(['x','y','z'])))
raise ValueError('Unexpected arg(s) {} in kwargs'.format(inv))
'''
>>> compare(1,2,key=4,x=2)
1 2 4 {'x': 2}
@gamesbook
gamesbook / bgg_games.py
Last active April 27, 2018 18:08
Create summary of BoardGameGeek.com games
# -*- coding: utf-8 -*-
from __future__ import print_function
import csv
# via https://github.com/lcosmin/boardgamegeek
from boardgamegeek import BoardGameGeek
import logging
logging.basicConfig()
GAMES = """Monopoly
@gamesbook
gamesbook / dice_histogram.py
Created April 1, 2018 19:28
Histogram for two dice of any size
"""
https://stackoverflow.com/questions/33454739/distribution-of-dice-rolls
&
https://stackoverflow.com/questions/20335617/unlimited-sides-to-dice-in-simulator/
"""
from collections import Counter
import sys
from random import randint
# Python 2/3 compatibility
@gamesbook
gamesbook / fee2metres.py
Created October 16, 2017 14:06
Convert imperial units (feet, inches) to metres
# -*- coding: utf-8 -*-
""" Purpose: Convert imperial units (feet, inches) to metres
Created: 2017-10-17
Author: dhohls@csir.co.za
Notes:
This is quite useful if you have a house plan where all the units are
Imperial (typically American or old European plans) and you need to
compare against modern plans.
@gamesbook
gamesbook / xml_validation.py
Created September 27, 2017 14:04
Use lxml to validate an XML file against a Schema (XSD)
# -*- coding: utf-8 -*-
""" Purpose: Use lxml to validate an XML file against a Schema (XSD)
Created: 2017-09-27
Author: dhohls@csir.co.za
Requires::
pip install lxml
@gamesbook
gamesbook / extract_directives.py
Created September 24, 2017 08:45
Extract and print directives from files in a software project
# -*- coding: utf-8 -*-
""" Purpose: Extract and print directives from a software project.
Created: 2017-09-24
Contact: gamesbook@gmail.com
Usage::
python extract_directives.py
python extract_directives.py -d=/path/to/ -i=FIXME -e=js
@gamesbook
gamesbook / log_to_sentry.py
Created September 6, 2017 13:07
Capture shell script output via Python logging to Sentry.io
# -*- coding: utf-8 -*-
"""Purpose: Capture shell script output via Python logging to Sentry.io.
Setup:
First export the SENTRY_DSN value in your .bashrc file. Then::
pip install raven --upgrade
pip install autoenv
echo "source bin/activate" > .env
@gamesbook
gamesbook / json_schema_validator.py
Created September 5, 2017 15:16
Python script to validate a JSON file against a schema
# -*- coding: utf-8 -*-
""" Purpose: Use Python schema module to validate JSON files vs a schema.
Created: 2017-09-05
Author: dhohls@csir.co.za
Usage::
python json_schema_validator.py --json=ajsonfile.json --schema=aschemafile.json
"""
@gamesbook
gamesbook / xml_from_jinja.py
Created September 2, 2017 16:18
Show creating XML (with optional elements) from nested JSON via jinja2 templating
# -*- coding: utf-8 -*-
"""Purpose: Show creating XML from nested JSON with optional elements.
Created: 2017-09-03
Author: dhohls@csir.co.za
"""
from __future__ import print_function
from jinja2 import Template
xml_template = """<?xml version="1.0" encoding="UTF-8"?>