Skip to content

Instantly share code, notes, and snippets.

Avatar

Derek Hohls gamesbook

  • CSIR
  • South Africa
View GitHub Profile
@gamesbook
gamesbook / buzzwords.py
Created May 11, 2021
Generate techno babble for marketing content
View buzzwords.py
"""
Purpose: generate management buzzphrases from buzzwords
BUZZ_WORDS are from http://bollocksphere.co.uk/Content/generationtable.shtml
"""
import random
BUZZ_WORDS = """# verb
<option value='Aggregate'>aggregate</option>
<option value='architect'>architect</option>
@gamesbook
gamesbook / cpif.sh
Created Apr 19, 2021
Copy files from source directory if they already exist in destination
View cpif.sh
#!/bin/bash
for file in /src/*; do
cfile="${file##*/}"
if [ -f "/dest/${cfile}" ]
then
echo "${cfile}"
cp "/src/${cfile}" "/dest/${cfile}"
touch "/dest/${cfile}"
fi
done
@gamesbook
gamesbook / log_timer.py
Created Mar 28, 2021
Log time for process
View log_timer.py
import time
import logging
logger = logging.getLogger(__name__)
t0 = time.process_time()
time.sleep(3)
logger.warning("Time elapsed: %3.6f", (time.process_time() - t0))
@gamesbook
gamesbook / config.py
Created Mar 12, 2021
Load and store settings via JSON or ENV variables #dataclasses
View config.py
from dataclasses import dataclass, InitVar, asdict
import orjson
import os
@dataclass
class Config:
"""Track config/settings"""
id: int = None
name: str = None
@gamesbook
gamesbook / data.py
Created Mar 7, 2021
Example of dataclass with property
View data.py
from dataclasses import dataclass
from typing import List, Tuple
@dataclass(frozen=True)
class Position:
name: str
dates: List[int]
items: Tuple
lon: float = 0.0
lat: float = 0.0
@gamesbook
gamesbook / single.py
Created Mar 7, 2021
Example of singleton class
View single.py
"""
Is this a good idea?
https://stackoverflow.com/questions/6760685/creating-a-singleton-in-python
"""
class Singleton:
__instance = None
@staticmethod
@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}