Skip to content

Instantly share code, notes, and snippets.

View JacobCallahan's full-sized avatar
🧐
🥓

Jake Callahan JacobCallahan

🧐
🥓
View GitHub Profile
@JacobCallahan
JacobCallahan / div_list.py
Last active August 14, 2020 04:30
A list that you can divide... why not?
from collections import UserList
class DivList(UserList):
def __truediv__(self, num):
list_len = len(self.data)
start = 0
step, rem = divmod(list_len, num)
stop = start + step + bool(rem)
outlist = []
@JacobCallahan
JacobCallahan / ini_to_yaml.py
Last active July 10, 2020 18:02
Convert an ini file into one or multiple yaml files
import argparse
import sys
from configparser import ConfigParser
from pathlib import Path
import yaml
def convert_data(data):
if isinstance(data, dict):
for key, value in data.items():
from collections import UserDict
class DictToObj(UserDict):
"""Converts dictionaries into dot accessible objects, great with nesting"""
def __init__(self, in_dict):
"""Initialize the class and all nested dictionaries"""
for key, value in in_dict.items():
if isinstance(value, dict):
setattr(self, key, DictToObj(value))
from collections import UserDict
class Stub(UserDict):
def __getattr__(self, name):
return self
def __getitem__(self, key):
item = getattr(self, key, self)
try:
@JacobCallahan
JacobCallahan / nefarious.py
Created March 12, 2020 19:09
python's inspect module has the potential to be incredibly dangerous
import inspect
class Dummy:
def __init__(self, name):
self.name = name
def print_name(self):
print(f"My name is {self.name}")
def call_meth(self, meth):
@JacobCallahan
JacobCallahan / miniconf.py
Last active March 6, 2020 15:09
ridiculously small config loader. i'm not joking. this is tiny!
import inspect
import os
import yaml
from pathlib import Path
CONFIG = None
class MiniConf:
def __init__(self, **kwargs):
@JacobCallahan
JacobCallahan / cache.py
Created February 14, 2020 15:07
poc for a new caching system
from collections import defaultdict
from functools import lru_cache
CACHE = Cache()
class Cache:
_free = defaultdict(lambda: defaultdict(list))
_active = defaultdict(lambda: defaultdict(list))
@JacobCallahan
JacobCallahan / session.py
Last active March 3, 2020 15:27
simple wrapper around ssh2-python's session context manager with some extra bits
import os
import socket
from ssh2.session import Session as ssh2_Session
from ssh2 import sftp as ssh2_sftp
SFTP_MODE = (
ssh2_sftp.LIBSSH2_SFTP_S_IRUSR
| ssh2_sftp.LIBSSH2_SFTP_S_IWUSR
| ssh2_sftp.LIBSSH2_SFTP_S_IRGRP
| ssh2_sftp.LIBSSH2_SFTP_S_IROTH
@JacobCallahan
JacobCallahan / async_runner.py
Created January 28, 2020 19:51
prototype general async requests thing
import aiohttp
import asyncio
import async_timeout
import click
from logzero import logger
from pathlib import path
class AsyncRunner:
"""A general class to perform async requests and process data"""
@JacobCallahan
JacobCallahan / fixtures.py
Created January 23, 2020 20:50
Fixture demo
"""Pytest fixtures
Fixtures give you a convenient way to perform setup actions outside of a test.
By limiting or expanding the scope of a fixture, you can cut down on costly setup tasks.
"""
import pytest
import random
import unittest2 # please never
NUM = "0123456789"