Skip to content

Instantly share code, notes, and snippets.

Avatar

Derek Hohls gamesbook

  • CSIR
  • South Africa
View GitHub Profile
@gamesbook
gamesbook / covid19.py
Last active Apr 20, 2020
Python script to access Corona / COVID-19 data from worldometers as Excel or plain text
View covid19.py
# coding: utf-8
"""Command-line access to stats from https://www.worldometers.info/coronavirus/
Created: 2020-04-05
Author: Derek <gamesbook@gmail.com>
Requires:
chromedriver - https://chromedriver.chromium.org/
xlsxwriter - https://xlsxwriter.readthedocs.io/
@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
You can’t perform that action at this time.