Skip to content

Instantly share code, notes, and snippets.

Derek Hohls gamesbook

  • CSIR
  • South Africa
Block or report user

Report or block gamesbook

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@gamesbook
gamesbook / dict_to_rst_table.py
Created Sep 6, 2019
Create reStructuredText complex table from list of dictionaries
View dict_to_rst_table.py
# coding: utf-8
"""Purpose: Create reStructuredText complex table from list of dictionaries
Created: 2019-09-05
Authors: dhohls <dhohls@csir.co.za> Derek Hohls
Example:
code::
@gamesbook
gamesbook / mysql2excel.py
Last active May 25, 2019
Save MySQL data to an Excel file
View mysql2excel.py
# -*- 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
Validate kwargs in a Python function
View validate_kwargs.py
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 Apr 27, 2018
Create summary of BoardGameGeek.com games
View bgg_games.py
# -*- 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 Apr 1, 2018
Histogram for two dice of any size
View dice_histogram.py
"""
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 Oct 16, 2017
Convert imperial units (feet, inches) to metres
View fee2metres.py
# -*- 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 Sep 27, 2017
Use lxml to validate an XML file against a Schema (XSD)
View xml_validation.py
# -*- 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 Sep 24, 2017
Extract and print directives from files in a software project
View extract_directives.py
# -*- 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 Sep 6, 2017
Capture shell script output via Python logging to Sentry.io
View log_to_sentry.py
# -*- 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 Sep 5, 2017
Python script to validate a JSON file against a schema
View json_schema_validator.py
# -*- 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
"""
You can’t perform that action at this time.