Skip to content

Instantly share code, notes, and snippets.

View ergoithz's full-sized avatar
🚚
Moved to gitlab.com/ergoithz

Felipe A. Hernandez ergoithz

🚚
Moved to gitlab.com/ergoithz
View GitHub Profile
@ergoithz
ergoithz / fuzzytime.js
Created September 22, 2016 12:51
Fuzzy time from seconds.
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},
];
@ergoithz
ergoithz / getparam.js
Last active August 11, 2016 10:02
parse GET parameters from JS
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));
}
@ergoithz
ergoithz / tzsnippet.js
Last active April 7, 2016 14:38
Javascript current TZ string (for ISO_8601)
/**
* 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;
},
@ergoithz
ergoithz / dev_startup.sh
Created December 16, 2015 07:40
Generic script to start vagrant on current directory on a terminal (great for running at startup)
#!/usr/bin/env bash
DIR=$( dirname "${BASH_SOURCE[0]}" )
cd $DIR
vagrant up
gnome-terminal -e "bash -c 'vagrant ssh';bash"
@ergoithz
ergoithz / .pystartup
Last active May 10, 2016 14:02
Python autocomplete and history
# 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
@ergoithz
ergoithz / json.py
Last active September 16, 2022 18:10
python json datetime encoder
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()
@ergoithz
ergoithz / xpath_soup.py
Last active January 11, 2025 16:34
Generate unique XPATH for BeautifulSoup element
#!/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.
@ergoithz
ergoithz / port.py
Created October 8, 2014 08:50
Python port chooser
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]
@ergoithz
ergoithz / __init__.py
Created October 7, 2014 11:03
initial_data attribute support for django models (like a fixture but only once model is created)
# 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.
@ergoithz
ergoithz / safe_date.py
Created August 18, 2014 05:58
Self-healing date function
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