Skip to content

Instantly share code, notes, and snippets.

View eduardonunesp's full-sized avatar

Eduardo Pereira eduardonunesp

View GitHub Profile
@eduardonunesp
eduardonunesp / gist:7155282
Created October 25, 2013 14:12
Validate UUID with Cerberus
import re
from cerberus import Validator
class Validator(Validator):
def _validate_is_uuid(self, is_uuid, field, value):
re_uuid = re.compile(r'[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}', re.I)
if is_uuid and not re_uuid.match(value):
self._error("Value for field '%s' must be valid UUID" % field)
@eduardonunesp
eduardonunesp / gist:8362413
Created January 10, 2014 20:55
Format brazilian phone numbers
def formatPhone(phone):
formatted = ''
i = 0
# clean phone. skip not digits
phone = ''.join(x for x in phone if x.isdigit())
# set pattern
if len(phone) == 12:
pattern = '(XXX) XXXXX-XXXX'
@eduardonunesp
eduardonunesp / mysqldump2djangoclass.py
Created January 13, 2014 17:58
The idea is make easy to migrate of a huge mysql table and translate to Django ORM (incomplete)
import re
import sys
CREATE_TABLE_STATEMENT = re.compile('CREATE\s+TABLE\s+(IF\s+NOT\s+EXISTS\s+)?`?(\w+)`?\s+\(', re.I)
END_CREATE_TABLE_STATEMENT = re.compile('\)\s*(ENGINE\s*=\s*(\w+))?\s*;', re.I)
STRUCTURE_FIELD = re.compile('\s+((?!UNIQUE)(?!PRIMARY)(?!KEY)(?!INDEX)`?\w+`?.*),', re.I)
CONSTRAING_FIELD = re.compile('\s+(UNIQUE.*|KEY.*|INDEX.*),?', re.I)
EXTRACT_VARCHAR_TYPE = re.compile('`?\w+`?\s+(VARCHAR\((\d+)\))\s+', re.I)
EXTRACT_INT_TYPE = re.compile('`?\w+`?\s+(INT\((\d+)\))\s+', re.I)
//
// libuuid sample program
//
// library install for debian
// $ sudo apt-get install uuid-dev
//
// compile
// $ gcc uuid_test.c -luuid -o uuid_test
//
#include <stdio.h>
@eduardonunesp
eduardonunesp / 0_reuse_code.js
Last active August 29, 2015 14:12
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
/*
* Inspired by: http://stackoverflow.com/questions/4360060/video-streaming-with-html-5-via-node-js
*/
var http = require('http'),
fs = require('fs'),
util = require('util');
http.createServer(function (req, res) {
var path = 'video.mp4';
@eduardonunesp
eduardonunesp / gist:95c226ec26a9f97f3613
Created March 25, 2015 12:46
Async execute forEach with Q Promise
var Q = require('q');
var f = function(a) {
var defer = Q.defer();
setTimeout(function() {
console.log(a);
defer.resolve(a);
}, a);
return defer.promise;
};
@eduardonunesp
eduardonunesp / gist:acfd6f8d377723a15fdc
Created March 25, 2015 12:48
Accept the first fulfilled value
var Q = require('q');
var f = function(a) {
var defer = Q.defer();
if (a % 2 === 0)
defer.resolve(a);
else
defer.reject(new Error("Rejected, module is odd" + a));
return defer.promise;
};
<script type="text/javascript>
function fisherYates ( myArray ) {
var i = myArray.length, j, temp;
if ( i === 0 ) return false;
while ( --i ) {
j = Math.floor( Math.random() * ( i + 1 ) );
temp = myArray[i];
myArray[i] = myArray[j];
myArray[j] = temp;
}
package main
import (
"encoding/json"
"github.com/codegangsta/negroni"
"github.com/gorilla/mux"
"log"
"net/http"
"strconv"
)