Skip to content

Instantly share code, notes, and snippets.

@craig552uk
craig552uk / split-file.py
Created August 1, 2025 09:02
Split File Python
import os
import sys
# split a file into multiple files 1200 lines each
def split_file(input_file, lines_per_file=1200):
if not os.path.isfile(input_file):
print(f"Error: The file {input_file} does not exist.")
return
with open(input_file, 'r') as file:
@craig552uk
craig552uk / workflow.ts
Last active July 29, 2025 13:37
A really simple workflow management system using TypeScript
// A really simple workflow management system using TypeScript
// This code defines a Workflow class that allows you to define transitions between states,
// validate transitions, and convert the workflow to a Mermaid diagram format.
// It also includes an example of how to use the Workflow class with a simple document review process
export class Workflow<T> {
private transitions: [T, T, string?][];
private startStates: T[] | null;
private endStates: T[] | null;
private title: string | null;
@craig552uk
craig552uk / mysql.service.ts
Last active June 18, 2020 09:21
MySQL Wrapper for NodeJS
import { Connection, createPool, createConnection, Pool } from 'mysql';
import { MYSQL_DATABASE, MYSQL_HOST, MYSQL_PASSWORD, MYSQL_PORT, MYSQL_USER } from './config.service';
import { InternalServerError } from 'http-errors';
import { LoggerService } from './logger.service';
import { isEmpty } from '../lib/utilities';
const logger = LoggerService.getInstance();
export class MySQLService {
@craig552uk
craig552uk / fuck_you.php
Created June 22, 2016 12:42
Fuck You PHP
/**
* fUCK yOU php
*
* @craig552uk
*/
<?php
You echo to the browser, not to std out.
So I hunt for bugs in XHRs or (here or (there about))
@craig552uk
craig552uk / promise_chains.js
Last active February 16, 2016 10:16
Options for passing multiple data down a promise chain
// Promises can only resolve (return) a single value
// Sometimes, you want to collect multiple values as you progress through a promise chain
// e.g. Customer, Product, Basket, Payment Details
//
// I can only think of three options for handling this type of scenario (below)
// NB: Assume that FOO, BAR & BAZ are the result of some complex action (DB query etc.)
//
// What are the Pro's & Cons?
// Are there others?
@craig552uk
craig552uk / test_errors.js
Created February 15, 2016 15:23
The Right and Wrong way to use Errors in Promises
//
// Wrong ways to use Errors in Promises
//
// Passes Error constructor to next step in Promise chain
Promise.resolve('foo').then(val => {
return Error('Bad Thing 1');
}).then(val => {
console.log('LOG 1', val);
@craig552uk
craig552uk / http_exceptions.py
Created October 21, 2014 09:51
A set of HTTP Exceptions for use in Request Handlers
# -*- coding: utf-8 -*-
#
# Author: Craig Russell <[email protected]>
# Simple exceptions for HTTP Errors
# http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
#
# Example:
#
# try:
# raise BadRequest()
@craig552uk
craig552uk / netwk.py
Created September 12, 2014 08:38
A netcat type thing in python
#
# > telnet localhost 4000
# Trying 127.0.0.1...
# Connected to localhost.
# Escape character is '^]'.
# hello
# world
#
# Connection closed by foreign host.
@craig552uk
craig552uk / cache.py
Created August 8, 2014 11:03
A Simple Python Memory Cache Library
# -*- coding: utf-8 -*-
#
# A simple memory cache library
# Author: Craig Russell <[email protected]>
#
import time
class Cache(object):
@craig552uk
craig552uk / kis_link_checker.py
Last active August 29, 2015 14:03
KIS Widget Checker