Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/env python
'''
Report the first caller that is not part of the Foo class.
Tested in Python 2.7.11, 3.4 and 3.5.
License: MIT Open Source
Copyright (c) 2016 Joe Linoff
'''
@jlinoff
jlinoff / Primes.java
Last active April 21, 2016 17:37
Get primes in range using java 8 streams.
// This code example shows how to use java 8 streams to quickly
// determine primes in a range. It is NOT production code. It
// is only for reference.
// MIT License
// Copyright (c) 2016 - Joe Linoff
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
@jlinoff
jlinoff / commaize.py
Created April 29, 2016 18:19
Add thousands separator to the integer portion of a string of digits.
#!/usr/bin/env python
'''
Add thousands separator to the integer portion of a string of digits.
It will handle a leading minus sign and a fraction.
Examples:
1234 --> 1,234
-1234 --> -1,234
@jlinoff
jlinoff / exec.go
Last active October 30, 2016 16:18
Go program to execute arbitrary shell commands using exec and a simple, custom shell lexer.
//
// This simple program accepts linux commands from the command line and
// executes them using the exec package.
//
// The one tricky thing about this is that Go does not provide any way to
// tokenize the command strings because that process is shell dependent.
//
// The lexer I have written is pretty dumb but it works for most common
// use cases.
//
@jlinoff
jlinoff / getPassword.go
Last active May 31, 2022 07:21
Go code to prompt for password using only standard packages by utilizing syscall.ForkExec() and syscall.Wait4(), recovers from ^C gracefully.
// License: MIT Open Source
// Copyright (c) Joe Linoff 2016
// Go code to prompt for password using only standard packages by utilizing syscall.ForkExec() and syscall.Wait4().
// Correctly resets terminal echo after ^C interrupts.
package main
import (
"bufio"
"fmt"
"os"
@jlinoff
jlinoff / runat.sh
Last active March 9, 2021 14:47
Bash script to run a command at a specific time with the resolution of a second. Useful for process concurrency testing.
#!/bin/bash
#
# Run a command at the specific time with a resolution of 1 second.
# The precision is within 1ms of the time stamp.
# This can be used to do concurrency tests.
#
# There are two input formats:
#
# HH:MM:SS - Run the command at this specific time.
#
@jlinoff
jlinoff / StudyFormat.java
Last active May 12, 2019 23:35
quick reminder of java format options
// study format statements
import static java.lang.System.out;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.function.BiConsumer;
import java.util.Date;
@jlinoff
jlinoff / TestLogging.java
Last active September 12, 2016 21:38
custom log format example in java
package com.jlinoff;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.logging.*;
/**
* Test logging.
*/
@jlinoff
jlinoff / ListClassMethods.java
Last active September 16, 2016 14:42
Java8 tool that performs introspection on java classes using reflection like javap. Useful for analyzing 3rd party classes.
/*
This class implements a CLI tool that does run time class introspection
using reflection. It is similar to javap. See the help (-h) for more details.
MIT License.
Copyright (c) 2016 Joe Linoff
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
@jlinoff
jlinoff / walker.go
Created September 26, 2016 16:20
go example that shows how to walk a directory using a regular expression filter and collect files that match
// This example demonstrates how to use go to walk a directory
// and filter the contents by using filepath.Walk with
// variables in the external scope to provide the filter and
// to capture the file names.
//
// To build:
// go build walk.go
// Usage:
// walk <pattern> <files>
//