Skip to content

Instantly share code, notes, and snippets.

@ms5
ms5 / fuzzyequal.py
Last active August 29, 2015 14:02
Fuzzy compare of floats in python
'''
fuzzyequal: compare two floats with tolerance
float1: float value1
float2: float value2
tolerance: float tolerance applied while comparing value1 and value2
prec: int precision when comparing float (decimal places)
returns: bool (True, False)
'''
def fuzzyequal (float1, float2, tolerance=0.01, prec=4):
# Hacky script for downloading videos from PyVideo and converting them to m4v
# format so they can be synced onto your apple device. Useful if you
# want to see everything that happened at PyCon while commuting.
#
# Requirements:
# * pip install requests BeautifulSoup
# * youtube-dl (from https://github.com/rg3/youtube-dl/) - add this to the
# directory where this script runs and ensure its execute bit is set.
# This was the only YouTube downloader I found that worked. It doesn't
# really have a good Python API so I call it through os.system.
#!/usr/bin/env python
import sys, os, time, atexit
from signal import SIGTERM
class Daemon:
"""
A generic daemon class.
Usage: subclass the Daemon class and override the run() method
import time
def RateLimited(maxPerSecond):
minInterval = 1.0 / float(maxPerSecond)
def decorate(func):
lastTimeCalled = [0.0]
def rateLimitedFunction(*args,**kargs):
elapsed = time.clock() - lastTimeCalled[0]
leftToWait = minInterval - elapsed
if leftToWait>0:
import re
import shutil
import argparse
from os import path
from sys import stderr
#
# Author: Daxda
# Date: 02.04.2014
# WTF: This is a quick tool I've hacked together to easily remove the meta
@ms5
ms5 / mongobak.py
Last active August 29, 2015 14:14 — forked from theorm/mongobak.py
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import os
import logging
import subprocess
import shutil
import tarfile
import tempfile
import argparse
@ms5
ms5 / verbos-argpary-example.py
Last active August 24, 2023 13:37
manipulating log level with python argparse
import argparse
import logging
parser = argparse.ArgumentParser()
parser.add_argument('--verbose', '-v', action='count', default=1)
args = parser.parse_args()
args.verbose = 70 - (10*args.verbose) if args.verbose > 0 else 0
logging.basicConfig(level=args.verbose, format='%(asctime)s %(levelname)s: %(message)s',
import time
def RateLimited(maxPerSecond):
minInterval = 1.0 / float(maxPerSecond)
def decorate(func):
lastTimeCalled = [0.0]
def rateLimitedFunction(*args,**kargs):
elapsed = time.clock() - lastTimeCalled[0]
leftToWait = minInterval - elapsed
if leftToWait>0:
@ms5
ms5 / IKEv2.mobileconfig
Created October 26, 2016 13:19 — forked from zqqf16/IKEv2.mobileconfig
strongSwan IKEv2 for iOS without certificate
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>PayloadContent</key>
<array>
<dict>
<key>IKEv2</key>
<dict>
<key>AuthName</key>
@ms5
ms5 / cpu-speed
Created March 19, 2020 15:33 — forked from dreamcat4/cpu-speed
Get and set the max CPU frequency on FreeBSD / FreeNAS / NAS4Free
#!/bin/sh
#
# cpu-speed:
# Requirements - be the root user, FreeBSD 9.2 or higher.
#
# Get or set the CPU frequency. This command with no arguments will
# print the current CPU frequency. CPU may be idling at it's lowest speed.
#
# This command takes 1 optional argument - new MAX cpu freq (in Mhz).
# expressed as an integer number. e.g. "cpu 800" - set max freq 800 Mhz.