Skip to content

Instantly share code, notes, and snippets.

@ethanal
ethanal / fizzbuzz.py
Last active August 29, 2015 14:01
FizzBuzz Code Golf
print([(i%3==0)*"Fizz"+(i%5==0)*"Buzz" or i for i in range(100)])
@ethanal
ethanal / moarjpeg.py
Last active August 29, 2015 14:01
Script to to optimize images for maximum jpeg
import cStringIO as StringIO
from random import randint
import sys
from PIL import Image
import wand.image
input_filepath = sys.argv[1]
img = Image.open(input_filepath)
@ethanal
ethanal / baseconv.py
Last active August 29, 2015 14:02
Base conversion with arbitrary alphabets
def baseconv(v1, a1, a2):
if v1 == a1[0]: return a2[0]
v2 = ""
n1 = {c: i for i, c in enumerate(a1)}
b1 = len(a1)
b2 = len(a2)
d1 = 0
for i, c in enumerate(v1):
d1 += n1[c] * pow(b1, len(v1) - i - 1)
@ethanal
ethanal / encode_polyline.py
Created October 3, 2014 00:39
Encode Polyline for Google Static Maps API
def invert(b):
return "".join(str(int(not int(c))) for c in b)
def encode_float(f):
b = bin(abs(int(round(f * 1e5))))[2:]
b = ("0" * (32 - len(b))) + b
if f < 0:
b = invert(b)
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@ethanal
ethanal / keybase.md
Created January 2, 2015 04:38
keybase.md

Keybase proof

I hereby claim:

  • I am ethanal on github.
  • I am ethanal (https://keybase.io/ethanal) on keybase.
  • I have a public key whose fingerprint is 178B 38FA 151F C237 625D 55B0 944F E97D 33AB DB60

To claim this, I am signing this object:

@ethanal
ethanal / profiler.py
Created June 25, 2015 02:53
Simple python profiler
import functools
from time import time
from .text_styles import *
class ProfileItem(object):
def __init__(self, name, elapsed, stats):
self.name = name
self.elapsed = elapsed
@ethanal
ethanal / text_styles.py
Created June 25, 2015 02:55
Python text styling with ANSI codes
import os
import sys
import platform
def _style(code):
def wrapper(string):
if os.isatty(sys.stdout.fileno()) and platform.system() != "Windows":
return "\033[{:02}m{}\033[00m".format(code, string)
return string
@ethanal
ethanal / print_template.tex
Created June 25, 2015 03:19
A LaTeX template for printing syntax-highlighted code nicely in grayscale
\documentclass[11pt]{article}
\usepackage[letterpaper]{geometry}
\geometry{top=0.5in, bottom=0.5in, left=0.7in, right=0.7in}
\usepackage{courier}
\usepackage{listings}
\usepackage{fancyhdr}
\pagestyle{fancy}
@ethanal
ethanal / ExcelConverter.java
Created July 2, 2015 02:15
Excel Converter
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;