Skip to content

Instantly share code, notes, and snippets.

View lvidarte's full-sized avatar

Leo Vidarte lvidarte

View GitHub Profile
@lvidarte
lvidarte / boolean.py
Created November 30, 2011 22:57
Boolean value of objects in Python
>>> class Foo:
... def __init__(self, value):
... self.value = value
... def __nonzero__(self):
... return self.value
...
>>> a = Foo(1)
>>> b = Foo(0)
>>> assert a
>>> assert b
@lvidarte
lvidarte / borg.py
Created December 2, 2011 03:59
The Borg design pattern
>>> class Borg:
... __shared_state = {}
... def __init__(self):
... self.__dict__ = self.__shared_state
...
>>> a = Borg()
>>> b = Borg()
>>> a.x = 1
>>> print b.x
1
def foo:
pass
# -*- coding: utf-8 -*-
import argparse
import json
import re
import u1db
from datetime import datetime
@lvidarte
lvidarte / encryptor.py
Created July 16, 2012 18:41
created from lai
# encryptor.py
import sys
import os.path
from Crypto.PublicKey import RSA
PUBLIC_KEY = os.path.join(os.path.expanduser('~'), '.ssh/id_rsa.pub')
def encrypt(message):
with open(PUBLIC_KEY, 'r') as pub_key:
@lvidarte
lvidarte / decryptor.py
Created July 16, 2012 18:41
created from lai
# decryptor.py
import os.path
from Crypto.PublicKey import RSA
PRIVATE_KEY = os.path.join(os.path.expanduser('~'), '.ssh/id_rsa')
def decrypt():
with open(PRIVATE_KEY, 'r') as prv_key:
decryptor = RSA.importKey(prv_key)
@lvidarte
lvidarte / .bashrc
Created August 8, 2012 19:00
Descubrir el branch actual de git y si hay cambios
function get_git_branch ()
{
DIRTY=""
CLEAN="nothing to commit (working directory clean)"
[ "`git status 2> /dev/null | tail -n1`" != "$CLEAN" ] && DIRTY='*'
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/⚡\1$DIRTY/"
}
PS1='\h \W$(get_git_branch)\$ '
@lvidarte
lvidarte / uptime_to_cosm.py
Created September 28, 2012 18:34
Sending data to cosm.com
#!/usr/bin/python2.7
import json
import httplib
API_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
loadavg = open('/proc/loadavg').read().split()
data = {'datastreams': [
#include <stdio.h>
int main(int argc, char const **argv)
{
int list[10] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
for (int i = 0; i < (sizeof(list) / sizeof(int)); i++) {
printf("%d\n", list[i]);
}
printf("size: %d\n", sizeof(list) / sizeof(int));
return 0;
@lvidarte
lvidarte / drange.py
Last active December 20, 2015 00:09
Range version for float steps
def drange(start, stop, step=1):
r = start
while r < stop:
yield r
r += step