Skip to content

Instantly share code, notes, and snippets.

View adjam's full-sized avatar
💭
Setting my GitHub status

Adam Constabaris adjam

💭
Setting my GitHub status
  • NC State University Libraries
View GitHub Profile
@adjam
adjam / css_color.py
Created May 4, 2023 16:49
Get the 'nearest' CSS color names to a given hex RGB color value
#!/usr/bin/env python
from operator import itemgetter
import sys
import json
import math
import re
# usage: ./color.py [hex RGB color]
# returns the five 'closest' CSS color names to the input color
@adjam
adjam / unique_name.sh
Created April 27, 2023 17:32
Bash function to create a unique filename
unique_name() {
local base=$1
local ext=$2
local name="${base}.${ext}"
if [[ -e $name || -L $name ]]; then
i=1
while [[ -e "${base}-${i}.${ext}" || -L "${base}-${i}.${ext}" ]]; do
let i++
done
name="${base}-${i}.${ext}"
@adjam
adjam / backup_file_writer.py
Created March 29, 2023 21:02
"safe" file writer implemented as a context manager
class BackupFileWriter:
def __init__(self, path, mode='w', prefix='buf-temp-'):
self.path = path
self.mode = mode
self.prefix = prefix
self.prev = f"{path}.prev"
self.temp = None
self.handle = None
def __enter__(self):
@adjam
adjam / find_todos.rb
Created April 15, 2022 14:00
ruby script that can be used standalone or as a pre-commit script to check staged files in git against .rubocop_todo.yml
#!/usr/bin/env ruby
require 'yaml'
require 'fileutils'
# this utility checks staged files and compares them against
# the files listed in `.rubocop_todo.yml` that have 'pending'
# rubocop violations to be fixed. It produces a report
# that shows whether any of those staged files requires
# attention
@adjam
adjam / Detector.java
Created July 5, 2019 18:03
icu4j charset detector
import com.ibm.icu.text.CharsetDetector;
import com.ibm.icu.text.CharsetMatch;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class Detector {
public static void main(String[] args) {
@adjam
adjam / item_joiner.py
Last active July 3, 2019 16:00
Example for merging item records spread out over multiple MARCXML into a single record.
#!/usr/bin/env python
# Merges items spread over multiple MARC XML records
# into a single record. Some ILSes will not export MARC21
# that is "too large", and will output the same bibliographic record
# multiple times.
# usage: first, convert your marc21 to MARCXML (see marcrenaissance.sh gist)
# ensure that all records with the same control number come out in a 'clump',
# e.g. by sorting on control number
package main
import (
"fmt"
"io"
"bytes"
"os"
"log"
"encoding/xml"
)
@adjam
adjam / backer.py
Created October 29, 2018 18:35
Simple backup class
#!/usr/bin/env python3
import os
import shutil
class Backer:
"""Implements a backup strategy for a file. You might
use this when preparing to overwrite an important configuration
or log file that's not under version control."""
@adjam
adjam / hashutil.rb
Last active September 25, 2018 14:58
Declaratively rename and delete key/value pairs in a ruby hash
# refinement for Hash class that allows declarative remapping functions
#
# #remap(mapping)
# and
# #remap!(mapping)
# methods.
# The `mapping` hash maps key names in the original hash to their
# names in the resulting hash, e.g.
# `{ foo: 'bar' }.remap({foo: :fu}) => `{ fu: 'bar' }`
# There are two special values that can be used in a mapping:
@adjam
adjam / configurable.rb
Last active August 21, 2018 13:37
'Configurable' module for Ruby, similar to https://github.com/markevans/configurable
#!/usr/bin/env ruby
# Makes a class globally configurable, e.g. in a Rails initializer.
# Usage:
# `class MyClass
# include Configurable
# create_config do
# # url attribute has a default value, which is the return value of the block
# url { 'http://www.example.com'}
# # name attribute is allowed but has no default value