Skip to content

Instantly share code, notes, and snippets.


Block or report user

Report or block mooware

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
mooware /
Created Jan 6, 2015
Bit operations to determine whether a month has 31 days
# Bit operations to determine whether a month has 31 days.
# Just for fun, inspired by an example in the Coursera course
# 'Hardware Security',
def has31days(month):
return (month & 1) ^ ((month & 8) >> 3)
for i in range(1, 13):
print(i, has31days(i))
mooware /
Created Jun 18, 2014
Toying around with metaprogramming in Python.
def compose(*funcs):
"""Composes (or chains) two or more functions into a new function.
E.g. with "f = compose(len, str, int)", f('123')" is the same as
if len(funcs) < 2:
raise Exception("too few arguments given")
elif len(funcs) == 2:
(a, b) = funcs
def call(*args, **kwargs):
mooware /
Last active Nov 10, 2018
Colored log output for Python logging framework. Works on Windows, Linux, and probably Mac as well.
# colored stream handler for python logging framework (use the ColorStreamHandler class).
# based on:
# how to use:
# i used a dict-based logging configuration, not sure what else would work.
# import logging, logging.config, colorstreamhandler
mooware / .vimrc
Created Jan 8, 2014
My .vimrc for work
View .vimrc
set nocompatible
syntax on
filetype plugin on
filetype plugin indent on
" by itself, jellybeans doesnt enable 256 colors
set t_Co=256
set background=dark
mooware / challenge8-palindrome-number.rb
Last active Dec 31, 2015
challenge #8: find the largest palindrome made from the product of two 3-digit numbers
View challenge8-palindrome-number.rb
# challenge #8
# 'find the largest palindrome made from the product of two 3-digit numbers'
# see also
# simple one-line solution, seems fast enough
# => [906609, 993, 913]
999.downto(100).map { |a| 999.downto(100).map { |b| [a*b, a, b] }.select { |arr| s = arr.first.to_s; s == s.reverse } }.flatten(1).sort { |a, b| a.first <=> b.first }.reverse.uniq.first
# more performant solution for n-digit numbers
# 4 => [99000099, 9999, 9901]
mooware / nullptr_t.cpp
Last active Dec 16, 2015
Implementation of the C++11 feature nullptr_t and an example of why it works better than the C #define NULL
View nullptr_t.cpp
struct nullptr_t
template <typename T>
operator T*() { return static_cast<T*>(0); }
nullptr_t nullptr;
#include <iostream>
mooware /
Created Apr 2, 2013
Python script that takes lines of text containing byte hex values, and appends a printable representation of the bytes to each line. It's similar to xxd, but it takes already dumped bytes instead of the original binary data. Example: "70 79 74 68 6f 6e" -> "70 79 74 68 6f 6e python"
#!/usr/bin/env python
import fileinput
import re
# pattern to match lines of hex byte values
PATTERN = re.compile("^[a-zA-Z0-9]{1,2}( [a-zA-Z0-9]{1,2})*$")
# margin between bytes and text
mooware / sizeof_array.hpp
Last active Dec 14, 2015
Type-safe way to get the length of an array in C++.
View sizeof_array.hpp
// In C/C++, the idiom (sizeof(arr) / sizeof(*arr)) is often used to get the length of an array.
// But this will return incorrect results if arr is not actually an array, but a pointer.
// The following function template provides a type-safe way to get the length of an array,
// by "destructuring" the type and reading the array length from there.
#include <cstddef> // for size_t
template <typename T, size_t N>
constexpr size_t sizeof_array(T (&)[N])
mooware / fm4-musik.rb
Last active Dec 13, 2015
Ruby script to scrape for FM4 music events and turn the result into an RSS feed, with one post per run.
View fm4-musik.rb
#!/usr/bin/env ruby
# Ruby script to scrape for FM4 music events and
# turn the result into an RSS feed.
# Usage: ruby fm4-musik.rb <result-file> <data-file> [-digest]
# The result file is where the feed xml will be written to,
# and the data file stores some state necessary to track
# the events that were already posted.
mooware / amazon-cloud-player-download.js
Last active Oct 13, 2015
Amazon Cloud Player Download Bookmarklet
View amazon-cloud-player-download.js
// I wrote this bookmarklet because Amazon has ended Linux support for downloading MP3s,
// and now I have to download songs separately through Amazon Cloud Player.
// This bookmarklet helps a bit with this, but it's far from perfect.
// When a list of songs is shown in Amazon Cloud Player, it clicks the song checkbox and
// the download button separately for each song, while waiting a few seconds in between songs.
// I encountered the issue that I cannot download more than a few songs simultaneously;
// the rest will just not show a download window. Not sure whether my browser or the server
// is doing this, but you might want to set the value of songdelayms high enough.
// The script is not really that useful, but it was fun to play around with this.
You can’t perform that action at this time.