Skip to content

Instantly share code, notes, and snippets.

View cefn's full-sized avatar

Cefn Hoile cefn

View GitHub Profile
def mapInitArgs(cls, fromType, toType, translateFun):
oldinit = cls.__init__
def newinit(self, *args, **kwargs):
fields = cls.__dataclass_fields__
fieldIter = iter(fields)
for pos,arg in enumerate(args):
fieldName = next(fieldIter)
if fields[fieldName].type is toType and type(arg) is fromType:
args[pos] = translateFun(arg)
import pytest
def checkHelper(value):
assert list(value) == list('foo')
@pytest.mark.parametrize("value", (
'foo',
'far'
))
def testInline(value):
@cefn
cefn / whitefurze_purchase.py
Created December 15, 2019 17:22
Purchase order calculation comparing storage box vendors
from dataclasses import dataclass
@dataclass
class Vendor:
pass
"""
mini45=None
midi45=None
maxi45=None
@cefn
cefn / loader.js
Last active April 5, 2020 12:52
Strategy for evaluating + unit-testing Couchdb map, reduce, update functions
const path = require("path")
const glob = require("glob")
const fs = require("fs")
const nodeEval = require("node-eval")
/** Helper object to construct a map of named design docs from a path containing js scripts.
* It populates couchdb design documents from js files targeting Spidermonkey 1.8.5
*
* File paths like
* * designdocs/map/priority.js
@cefn
cefn / priority.test.js
Created April 5, 2020 13:03
Example unit-testing for CouchDB map function
const testScriptType = "map"
const testScriptId = "priority"
const priorities = [
"!urgent",
null,
"!soon",
"!normal",
"!backlog",
"!wishlist",
]
@cefn
cefn / priority.js
Created April 5, 2020 13:13
Example view map function
var priorities = [
"!urgent",
null,
"!soon",
"!normal",
"!backlog",
"!wishlist",
]
function getPriorityOrder(task) {
@cefn
cefn / configurator.js
Last active April 5, 2020 13:18
Uploads design docs to couchdb through nano for Component testing within views
const {
Loader
} = require("./designdocs/loader")
class CouchdbConfigurator{
constructor(dbHandle){
if( //duck-type check for nano db handle
typeof dbHandle === "object" &&
typeof dbHandle.config === "object" &&
dbHandle.config.url &&
#! /home/linuxbrew/.linuxbrew/opt/[email protected]/bin/python3.8
import os
import math
import shutil
import requests
from xml.etree import ElementTree
def decimalMinuteString(decimalDegreeString, paddegrees):
degrees = float(decimalDegreeString)
@cefn
cefn / fizzbuzz.ts
Created August 22, 2021 17:18
FizzBuzz Cefn
for (let x = 1; x <= 100; x++) {
console.log(
`${x % 3 === 0 ? "Fizz" : ""}${x % 5 === 0 ? "Buzz" : ""}` || x.toString()
);
}
@cefn
cefn / lock.ts
Created January 14, 2022 11:54
Simple lock implementation
let releaseGlobalsPromise: null | Promise<void> = null;
async function acquireGlobalsLock() {
while (releaseGlobalsPromise) {
await releaseGlobalsPromise;
}
let release!: () => void;
releaseGlobalsPromise = new Promise(
(resolve) =>
(release = () => {
releaseGlobalsPromise = null;