This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
intervals = [ | |
{t: 0.001, s: 'a millisecond', p: '%d milliseconds'}, | |
{t: 1, s: 'a second', p: '%d seconds'}, | |
{t: 60, s: 'a minute', p: '%d minutes', m: 1}, | |
{t: 3600, s: 'an hour', p: '%d hours', m: 1}, | |
{t: 86400, s: 'a day', p: '%d days', m: 1}, | |
{t: 604800, s: 'a week', p: '%d weeks', m: 1}, | |
{t: 2629800, s: 'a month', p: '%d months', m: 2}, | |
{t: 31587840, s: 'a year', p: '%d years', m: 2}, | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getParam(key, qs) { | |
const | |
results = [], | |
reg = new RegExp(`(^|\&)(${key})(=([^&]*))?($|\&)`); | |
let | |
match = reg.exec(qs = qs||document.location.search.substr(1)); | |
while(match){ | |
results.push(match[3]?decodeURIComponent(match[4].replace('+', ' ')):true); | |
match = reg.exec(qs = qs.substr(match.index + match[0].length - 1)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This snipped generates the offset part of ISO_8601 datetime strings. | |
* Useful for composed datetime strings as required for APIs complying RFC3339. | |
*/ | |
var | |
tz = (new Date()).getTimezoneOffset(), | |
pad = function (v){ | |
var sv = Math.abs(parseInt(v, 10)).toString(); | |
return '00'.substr(sv.length) + sv; | |
}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
DIR=$( dirname "${BASH_SOURCE[0]}" ) | |
cd $DIR | |
vagrant up | |
gnome-terminal -e "bash -c 'vagrant ssh';bash" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Add auto-completion and a stored history file of commands to your Python | |
# interactive interpreter. Requires Python 2.0+, readline. Autocomplete is | |
# bound to the TAB key by default (you can change it - see readline docs). | |
# | |
# Store the file in ~/.pystartup, and set an environment variable to point | |
# to it: "export PYTHONSTARTUP=$HOME/.pystartup" in bash. | |
import atexit | |
import os | |
import readline |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import datetime | |
import dateutil.parser | |
class JSONEncoder(json.JSONEncoder): | |
def default(self, obj): | |
if isinstance(obj, datetime.datetime): | |
return { | |
'type': 'datetime', | |
'data': obj.isoformat() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
import bs4 | |
def xpath_soup(element): | |
# type: (typing.Union[bs4.element.Tag, bs4.element.NavigableString]) -> str | |
""" | |
Generate xpath from BeautifulSoup4 element. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
used_ports = [None] | |
def choose_port(address='127.0.0.1'): | |
port = None | |
while port in used_ports: | |
testsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
testsocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
testsocket.bind((address, 0)) | |
port = testsocket.getsockname()[1] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# myproject/myapp/management/__init__.py | |
from django.db.models.signals import post_syncdb | |
import myapp.models | |
def post_syncdb_handler(sender, **kwargs): | |
''' | |
Handler will be called on every syncdb to set initial data for models if `initial_data` attr | |
is set. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import calendar | |
def a(year, month, day): | |
year += (month-1) // 12 | |
month = (month-1) % 12 + 1 | |
while day > calendar.monthrange(year, month)[1]: | |
day -= calendar.monthrange(year, month)[1] | |
year += month // 12 | |
month = month % 12 + 1 |