Skip to content

Instantly share code, notes, and snippets.

View maurobaraldi's full-sized avatar

Mauro Navarro Baraldi maurobaraldi

View GitHub Profile
@maurobaraldi
maurobaraldi / count_binary_search_tree.js
Created December 16, 2014 03:06
Count elements in a binary search tree
// add an element to a tree
var add_elem = function(tree, value) {
if(tree === null) {
return {
data: value,
left: null,
right: null
};
}
if(value <= tree.data) {
@maurobaraldi
maurobaraldi / Makefile
Last active August 29, 2015 14:17
Makefile for Python-Flask projects
.PHONY: run
run:
python manage.py runserver
.PHONY: clean
clean:
@find ./ -name "*.pyc" -exec rm -f {} \;
@find ./ -name "*~" -exec rm -f {} \;
@find ./ -name "__pycache__" -exec rm -f {} \;
@maurobaraldi
maurobaraldi / even_odd.py
Last active August 29, 2015 14:21
Even and Odd in a dict - Coding Golf
(lambda n: dict(zip(['impar', 'par'], [filter(lambda x: x != False, map(lambda x: x if x % 2 else False, range(n))), filter(lambda x: x != False, map(lambda x: x if not x % 2 else False, range(n)))])))(10)
@maurobaraldi
maurobaraldi / bovespa_intraday.py
Last active March 20, 2025 20:19
Cotações da Bovespa Intraday
#!/usr/bin/env python
from datetime import datetime
from json import loads
from time import gmtime, mktime, strptime
# LevelDict é um wrapper usando dicionário para LevelDB
# https://github.com/maurobaraldi/leveldict
from leveldict import LevelJsonDict
from requests import get
@maurobaraldi
maurobaraldi / dict2obj.py
Last active August 29, 2015 14:24
Python dictionary to Python object
class dict_to_obj(dict):
def __getattr__(self, name):
if name in self:
return self[name]
else:
raise AttributeError("No such attribute: " + name)
def __setattr__(self, name, value):
self[name] = value
@maurobaraldi
maurobaraldi / json_to_obj.py
Created July 15, 2015 18:50
Transform JSON string into Python objects (not dict)
from json import loads
from collections import namedtuple
def json_to_obj(data):
def hook(d):
return namedtuple('obj', d.keys())(*d.values())
return json.loads(data, object_hook=hook)
>>> json_data = '{"lastUpdate": 1435608300000, "type": "stock", "timeOffSet": -10800000, "total": 5, "data": [{"date": 1428289200000, "high": 5.11, "price": 5.05, "open": 5.05, "low": 5.01}, {"date": 1427943600000, "high": 5.05, "price": 5, "open": 4.93, "low": 4.88}, {"date": 1427857200000, "high": 5.07, "price": 4.91, "open": 5.01, "low": 4.91}, {"date": 1427770800000, "high": 5.14, "price": 4.97, "open": 5.1, "low": 4.97}, {"date": 1427684400000, "high": 5.24, "price": 5.12, "open": 5.18, "low": 5.12}], "today": 1435697531409}'
@maurobaraldi
maurobaraldi / abcclass.py
Created August 25, 2015 04:44
OO Abstract Methods and Interface concepts in Python
# Abstract Methods and Interfaces in Python
# Interfaces are templates for classes with commont attributes to new classes
from abc import ABCMeta, abstractmethod
class Shape(object):
__metaclass__ = ABCMeta
@maurobaraldi
maurobaraldi / performance.py
Created October 13, 2015 17:37
Python Stack Implementation
from timer import Timer
from stack import Stack as S
from stack_optimized import Stack as SO
with Timer() as tstack:
st = S(100)
(st.push(i) for i in range(100, 200))
(st.pop() for i in range(99))
@maurobaraldi
maurobaraldi / strfmt.js
Last active November 24, 2015 10:58
Javascript string.format <3
// from http://stackoverflow.com/a/4256130/841339
String.prototype.format = function() {
var formatted = this;
for (var i = 0; i < arguments.length; i++) {
var regexp = new RegExp('\\{'+i+'\\}', 'gi');
formatted = formatted.replace(regexp, arguments[i]);
}
return formatted;
};
@maurobaraldi
maurobaraldi / gist:899a88fe0bf7471574c1
Created December 29, 2015 16:56 — forked from anonymous/gist:dc9c249318f3f92d716b
Download math books from springer
from urllib2 import urlopen
from re import findall, search
base_url = "http://link.springer.com"
links = []
for i in xrange(1, 13):
index = base_url + '/search/page/%d?facet-series="136"&facet-content-type="Book"&showAll=false' % i
links.extend(findall('<a class="title" href="(.*?)"', urlopen(index).read()))