Skip to content

Instantly share code, notes, and snippets.

Avatar
💻
Man At Work 🚜

Eduardo Pereira eduardonunesp

💻
Man At Work 🚜
View GitHub Profile
@eduardonunesp
eduardonunesp / gist:7155282
Created Oct 25, 2013
Validate UUID with Cerberus
View gist:7155282
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 Jan 10, 2014
Format brazilian phone numbers
View gist:8362413
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 Jan 13, 2014
The idea is make easy to migrate of a huge mysql table and translate to Django ORM (incomplete)
View mysqldump2djangoclass.py
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)
View uuid_test.c
//
// 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 Aug 29, 2015
Here are some things you can do with Gists in GistBox.
View 0_reuse_code.js
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
View html5-video-streamer.js
/*
* 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 Mar 25, 2015
Async execute forEach with Q Promise
View gist:95c226ec26a9f97f3613
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 Mar 25, 2015
Accept the first fulfilled value
View gist:acfd6f8d377723a15fdc
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;
};
View Shopify Random Product Order
<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;
}
View simple_todo_api.go
package main
import (
"encoding/json"
"github.com/codegangsta/negroni"
"github.com/gorilla/mux"
"log"
"net/http"
"strconv"
)
You can’t perform that action at this time.