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 / ticks.py
Last active February 3, 2024 13:20
Convert C# tick dates into a Python / readable date
# -*- coding: utf-8 -*-
"""
Purpose: Convert .NET ticks to formatted ISO8601 time
Author: D Hohls < dhohls@csir.co.za>
"""
from __future__ import print_function
import datetime
import sys
@gamesbook
gamesbook / excel_to_numpy
Created June 27, 2014 07:48
Load Excel into numpy array
#!/usr/bin/env python
#-*- coding: utf-8 -*-
"""
Note:
In numpy, every "append" action requires re-allocation of the array memory
and short-term doubling of memory requirements. So, the more general
solution is to allocate arrays to be as large as the final output of your
algorithm.
"""
import os
@gamesbook
gamesbook / clean_code.md
Last active May 22, 2022 10:42 — forked from wojteklu/clean_code.md
Summary of 'Clean code' by Robert C. Martin

Code is clean if it can be understood easily – by everyone on the team. Clean code can be read and enhanced by a developer other than its original author. With understandability comes readability, changeability, extensibility and maintainability.


General rules

  1. Follow standard conventions. Python: PEP-8 and black.
  2. Keep it simple stupid. Simpler is always better. Reduce complexity as much as possible.
  3. Boy scout rule. Leave the campground cleaner than you found it.
  4. Always find root cause. Always look for the root cause of a problem.

Design rules

@gamesbook
gamesbook / mysql-docker.sh
Last active May 8, 2022 06:28 — forked from spalladino/mysql-docker.sh
Backup and restore a mysql database from a running Docker mysql container
# Backup
docker exec $CONTAINERID /usr/bin/mysqldump -u root -proot $DATABASE > backup.sql
# Restore
cat backup.sql | docker exec -i $CONTAINERID /usr/bin/mysql -u root -proot $DATABASE
@gamesbook
gamesbook / buzzwords.py
Created May 11, 2021 05:50
Generate techno babble for marketing content
"""
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 April 19, 2021 07:37
Copy files from source directory if they already exist in destination
#!/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 March 28, 2021 15:42
Log time for process
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 March 12, 2021 12:31
Load and store settings via JSON or ENV variables #dataclasses
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 March 7, 2021 06:21
Example of dataclass with property
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 March 7, 2021 06:20
Example of singleton class
"""
Is this a good idea?
https://stackoverflow.com/questions/6760685/creating-a-singleton-in-python
"""
class Singleton:
__instance = None
@staticmethod