Skip to content

Instantly share code, notes, and snippets.

VALID_PYTHON_EXTENSIONS = ('.py', )
def get_userdefined_class(directory: str, target_baseclass: object) -> list:
"""
Load Classes that sub-classed the given 'target_baseclass' for modules in the given directory
:param directory: directory containing user-defined classes subclassing 'target_baseclass'
:param target_baseclass: the ABC class that the user class subclasses
:return: (class) [UserDefinedClass, ...]
"""
@monkut
monkut / get_package_subclassed_classes.py
Created May 31, 2018 03:40
Voodoo function to discover classes in given 'package' that subclass the given 'target_baseclass'
def _get_package_defined_classes(package, target_baseclass) -> dict:
"""
Voodoo function to discover classes in given 'package' that subclass the given 'target_baseclass'
:param package: Python Package Object
:param target_baseclass: Python Class Object
:return:
{
CLASS_NAME: CLASS_OBJECT,
...
}
@monkut
monkut / compare_directories.py
Created June 12, 2018 07:23
Compare two directory's content size with percentage
"""
This is a script to compare the content of two directories.
It was used to determine how complete a series of cp commands were done after the cp process was already started
"""
import os
def directory_size(path):
total = 0
for entry in os.scandir(path):
if entry.is_file():
@monkut
monkut / github_org_projects_info.graphql
Last active June 13, 2018 23:48
A github graphql query to obtain information on originzational projects
query {
organization(login:"your-org-name"){
name
projects(first:25, states:OPEN) {
nodes {
name,
columns(first:10){
nodes{
name,
cards (first:50){
"""
Creates a github organization project with columns from a defined template
using the Github graphql API
"""
import os
import uuid
import requests
[MASTER]
# A comma-separated list of package or module names from where C extensions may
# be loaded. Extensions are loading into the active Python interpreter and may
# run arbitrary code
extension-pkg-whitelist=
# Add files or directories to the blacklist. They should be base names, not
# paths.
ignore=CVS
@monkut
monkut / config.yml
Created June 18, 2018 17:55
WIP - sample circleci django/postgres config
version: 2
jobs:
build:
working_directory: ~/baserepository/
docker:
- image: circleci/python:3.6.5
environment:
PIPENV_VENV_IN_PROJECT: true
- image: mdillon/postgis:9.6
environment:
[
{
"name": "category:documentation",
"description": "",
"color": "AED6F1"
},
{
"name": "category:feature",
"description": "",
"color": "5DADE2"
@monkut
monkut / japanese_business_calendar.py
Created July 11, 2018 14:24
Using pandas AbstractHolidayCalendar to calculate work days
import datetime
from pandas.tseries.holiday import AbstractHolidayCalendar, Holiday, sunday_to_monday, MO
from pandas.tseries.offsets import Day, CustomBusinessDay, DateOffset
class JapanBusinessCalendar(AbstractHolidayCalendar):
rules = [
Holiday('New Years Day', month=1, day=1),
Holiday('Coming of Age Day', month=1, day=8), # second monday of Jan
Holiday('National Foundation Day', month=2, day=11, observance=sunday_to_monday), # observed monday if falls on Sunday
@monkut
monkut / csv_multiprocessing_pool.py
Created July 23, 2018 01:03
A common pattern for processing csv files in parallel
import os
import csv
from multiprocessing import cpu_count, Pool
def process_csv(args):
filepath, encoding = args
unique_ids = set()
with open(filepath, 'r', encoding=encoding) as in_f:
reader = csv.reader(in_f)