Skip to content

Instantly share code, notes, and snippets.

@ntrrgc
ntrrgc / urlnorm.py
Last active December 14, 2015 07:19 — forked from mnot/urlnorm.py
Ported to python3
#!/usr/bin/env python
"""
urlnorm.py - URL normalisation routines
Ported to python3.
urlnorm normalises a URL by;
* lowercasing the scheme and hostname
* taking out default port if present (e.g., http://www.foo.com:80/)
@ntrrgc
ntrrgc / 10-math.py
Created March 22, 2013 21:35
Magic for printing LaTeX math in IPython
from IPython.core.magic import line_magic
from IPython.display import Math
@register_line_magic
def math(tex):
return Math(repr(eval(tex, get_ipython().user_ns)))
@ntrrgc
ntrrgc / memdump.py
Created March 2, 2014 17:21
Dumps a process' memory to stdout on Linux
#!/usr/bin/env python
from __future__ import print_function
import sys
import os
import re
import ctypes
import argparse
ulseek = ctypes.cdll['libc.so.6'].lseek
ulseek.restype = ctypes.c_uint64
@ntrrgc
ntrrgc / php-fpm.service
Created March 3, 2014 15:03
php-fpm systemd unit with auto-restart (because php-fpm crashes half the times it's started)
[Unit]
Description=PHP FastCGI Server
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/run/php-fpm.pid
ExecStart=/usr/sbin/php-fpm --fpm-config /etc/php5/fpm/php-fpm.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
@ntrrgc
ntrrgc / ini_patch
Last active December 4, 2019 12:38
Fork of Ansible's ini_file, which does preserve comments and latter case
#!/usr/bin/python
# -*- coding: utf-8 -*-
# (c) 2014, Juan Luis Boya García <ntrrgc () gmail.com>
# (c) 2012, Jan-Piet Mens <jpmens () gmail.com>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@ntrrgc
ntrrgc / ssh.py
Created May 11, 2014 16:07
Run commands with ssh even if arguments contain spaces
import subprocess
import shlex
if hasattr(shlex, "quote"):
# Python 3
shquote = shlex.quote
else:
# Backport of shlex.quote for Python 2
import re
_find_unsafe = re.compile(r'[^\w@%+=:,./-]').search
@ntrrgc
ntrrgc / event.py
Created June 9, 2014 23:18
Event library (unreliable outside of CPython)
# Minimal library to create events
import inspect
from weakref import ref
from weakmethod import WeakMethod
class Event(object):
def __init__(self, handlers=[]):
self.handlers = set(handlers)
@ntrrgc
ntrrgc / it-promise.js
Created August 24, 2014 16:10
Jasmine ES6 promises
// Quick and dirty ES6 promise testing (because I couldn't find any better)
// MIT License
function makeResolvedPromise(value) {
return new Promise(function(success) {
success(value);
});
}
function makeRejectedPromise(errorValue) {
@ntrrgc
ntrrgc / fetch-gfonts
Last active August 29, 2015 14:06
Download Google fonts
#!/usr/bin/python3
# TODO: Does not support woff2 fonts (as served for Chrome)
import os, sys, re
import requests
def fetch_fonts(input_css, font_path="fonts"):
output_css = []
for line in input_css.split("\n"):
if "https://" in line:
name = re.match(r".*local\('(.*?)'\).*", line).groups()[0]
@ntrrgc
ntrrgc / mpris2
Created December 19, 2014 03:31
Simple MPRIS2 controller for use with hotkeys
#!/usr/bin/python
import sys
import dbus
session_bus = dbus.SessionBus()
players = [x for x in session_bus.list_names()
if x.startswith('org.mpris.MediaPlayer2')]
if len(players) < 0:
print("No players found! :(", file=sys.stderr)
sys.exit(1)