Skip to content

Instantly share code, notes, and snippets.

View mmas's full-sized avatar

Modesto Mas mmas

View GitHub Profile
@mmas
mmas / google_oauth.py
Last active December 26, 2015 09:19
Google OAuth2 with Tornado 3.1.1 (Python)
import json
import urllib
import httplib
import re
from tornado import httpclient
from tornado.web import asynchronous, RequestHandler
from tornado.auth import OAuth2Mixin
from tornado.gen import coroutine
from tornado.concurrent import return_future
@mmas
mmas / floyd_warshall.py
Created October 24, 2013 00:19
Floyd-Warshall algorithm implementation in Python
class FloydWarshall(object):
def __init__(self, file_):
nodes = file_.read().splitlines()
file_.close()
self.get_graph(nodes)
self.len_ = len(self.nodes)
self.create_fw_matrix()
def get_graph(self, nodes):
@mmas
mmas / dijkstra.py
Created November 20, 2013 03:57
Nodes shortest path - Dijkstra
import random
from igraph import *
from priodict import priorityDictionary
def dijkstra(graph, source, target=None):
distances = {}
predec = {}
queue = priorityDictionary()
queue[source] = 0
@mmas
mmas / pre-commit
Created December 20, 2013 21:39
Simple githook pre-commit. Uglify javascript and compile and compress less to css. Save as .git/hooks/pre-commit. uglifyJS2 - https://github.com/mishoo/UglifyJS2 lessc - https://github.com/less/less.js
# uglifyJS2 - https://github.com/mishoo/UglifyJS2
# lessc - https://github.com/less/less.js
STYLES=$(pwd)/PATH/TO/MAIN/LESS/FILE
JS_ROOT=$(pwd)/PATH/TO/JAVASCRIPT/FOLDER
echo "Uglifying javacript..."
for i in $(find $JS_ROOT -name "*.js" -not -name "*.min.js")
do
o=${i/.js/.min.js}
@mmas
mmas / calendar.js
Last active June 15, 2016 20:13
Simple javascript (jquery) datepicker/calendar. Based in some other? Don't remember.
var Calendar = function(element, context) {
var widget, utils, $calendar;
widget = this;
utils = {
mod: function(x, n) {
// Fix modulo. Javascript hates Maths.
// x: Number; n: Number.
return ((x % n) + n) % n;
@mmas
mmas / underscorize.py
Last active June 15, 2016 20:08
Camelcased and dashed text to underscored text in Python
import re
def underscorize(x):
"""
Camelcased and dashed text to underscored text.
Examples:
>>> underscorize('CamelCasedText')
'camel_cased_text'
>>> underscorize('camel-cased-text')
'camel_cased_text'
@mmas
mmas / html_word_truncator.py
Created June 20, 2016 20:39
Truncate an HTML text to a certain number of words
from HTMLParser import HTMLParser
import re
re_whitespace = re.compile(r'(\w+)')
class WordTruncatedHTMLParser(HTMLParser):
"""
Truncate an HTML text to a certain number of words.
@mmas
mmas / translate.py
Last active October 11, 2016 11:00
Google translate client
#!/usr/bin/python
"""
Google translate client
usage: python translate.py [-h] [-v] [-ua User-Agent] source_lang target_lang text
positional arguments:
source_lang
target_lang
var data, ua, platform, browser, os;
data = {
page: window.location.pathname.replace(/\/$/, ''),
language: navigator.language.slice(0, 2).toUpperCase(),
screen_resolution: window.screen.width + 'x' + window.screen.height,
screen_dpi: window.devicePixelRatio,
is_touch_device: 'ontouchstart' in window || 'onmsgesturechange' in window
};
ua = navigator.userAgent.toLowerCase();
import os
import json
from tornado.web import RequestHandler
from pygeoip import GeoIP
import dateutil.parser
from app.models import Visit
from app import settings