Skip to content

Instantly share code, notes, and snippets.

View rizkyabdilah's full-sized avatar

Rizky Abdilah rizkyabdilah

View GitHub Profile
@rizkyabdilah
rizkyabdilah / regex_url.py
Created December 7, 2011 10:23
regex to capture and validate url, currently support capture scheme, username, password, host, port, path, query and fragment
import re
regex_url = re.compile("^(?:(?P<scheme>https?|ftps?):\/\/)?(?:(?:(?P<username>[\w\.\-\+%!$&'\(\)*\+,;=]+):*(?P<password>[\w\.\-\+%!$&'\(\)*\+,;=]+))@)?(?P<host>[a-z0-9-]+(?:\.[a-z0-9-]+)*(?:\.[a-z\.]{2,6})+)(?:\:(?P<port>[0-9]+))?(?P<path>\/(?:[\w_ \/\-\.~%!\$&\'\(\)\*\+,;=:@]+)?)?(?:\?(?P<query>[\w_ \-\.~%!\$&\'\(\)\*\+,;=:@\/]*))?(?:(?P<fragment>#[\w_ \-\.~%!\$&\'\(\)\*\+,;=:@\/]*))?$", re.IGNORECASE)
example_url = "http://username:[email protected]:130892/some/path/to/folder?query1=1&query2=2#go-to-fragment"
match_test = re.match(regex_url, example_url)
if match_test:
print match_test.groupdict()
@rizkyabdilah
rizkyabdilah / httputils.py
Created December 10, 2011 14:33
simple wrapper for httplib, provide simple High Level API trough python httplib
import re
import urllib
import httplib
import mimetypes
import os
regex_url = re.compile("^(?:(?P<scheme>http|ftps?):\/\/)?(?:(?:(?P<username>[\w\.\-\+%!$&'\(\)*\+,;=]+):*(?P<password>[\w\.\-\+%!$&'\(\)*\+,;=]+))@)?(?P<host>[a-z0-9-]+(?:\.[a-z0-9-]+)*(?:\.[a-z\.]{2,6})+)(?:\:(?P<port>[0-9]+))?(?P<path>\/(?:[\w_ \/\-\.~%!\$&\'\(\)\*\+,;=:@]+)?)?(?:\?(?P<query>[\w_ \-\.~%!\$&\'\(\)\*\+,;=:@\/]*))?(?:(?P<fragment>#[\w_ \-\.~%!\$&\'\(\)\*\+,;=:@\/]*))?$")
def parse_url(url):
match = re.match(regex_url, url, re.IGNORECASE)
@rizkyabdilah
rizkyabdilah / picture-grabber.go
Created January 15, 2012 15:31
Compare image downloader speed in Go, python and php.
package main
import (
"http"
"io/ioutil"
"fmt"
"os"
)
//var (
@rizkyabdilah
rizkyabdilah / radix_sort.py
Created February 4, 2012 20:46
Implementation of radix sort algorithm in python http://en.wikipedia.org/wiki/Radix_sort
#!/usr/bin/env python
def radix_sort(random_list):
len_random_list = len(random_list)
modulus = 10
div = 1
while True:
# empty array, [[] for i in range(10)]
new_list = [[], [], [], [], [], [], [], [], [], []]
for value in random_list:
@rizkyabdilah
rizkyabdilah / query_array_legth_mongodb.js
Created February 16, 2012 11:33
use $where in mongodb to get query length of array, because query $size not support $gt, $lt, $lte and $gte
db.test.insert({'name': 'rizky', 'abilities': ['eat', 'sleep', 'code']});
db.test.insert({'name': 'abdilah', 'abilities': ['drink']});
db.test.insert({'name': 'median', 'abilities': null});
condition_have_ability_more_than_2 = function(){
return typeof(this.abilities) == "object" && this.abilities != null && this.abilities.length > 2;
}
// peoples have abilities more_than_2
db.test.find(condition_have_ability_more_than_2);
@rizkyabdilah
rizkyabdilah / setup.py
Created February 24, 2012 05:39
patched setup.py in PIL, work in ubuntu x86_64
# add last 3 line in line 150 file setup.py
add_directory(library_dirs, "/lib64")
add_directory(library_dirs, "/usr/lib64")
add_directory(library_dirs, "/usr/lib/x86_64-linux-gnu")
@rizkyabdilah
rizkyabdilah / main.py
Created April 23, 2012 17:44
Using while-break to prevent 'ugly' nested if
# example nested if, in real life it will be uglier
if a:
# do some code with a
if b:
# do some code with a and b
if c:
# do some code with a, b and c
else:
print "you must set b"
else:
@rizkyabdilah
rizkyabdilah / array_intersect.coffee
Created May 1, 2012 13:40
Simple array intersect function in coffeescript
arrayIntersect_ = (arg1, arg2) ->
retVal = []
hashMap = {}
for l in arg1
hashMap[l] = 1
for l in arg2
if hashMap[l] and ((hashMap[l] += 1) == 2)
retVal.push(l)
@rizkyabdilah
rizkyabdilah / example.py
Created June 9, 2012 06:23
set jinja2 global environment on bottlepy
#!/opt/python/bin/python
from bottle import BaseTemplate
class ExampleGlobal(object):
name = "Hello World"
gob = ExampleGlobal()
BaseTemplate.global_config('jinja2_global_env', {'gob': gob})
## do with your creativity
@rizkyabdilah
rizkyabdilah / Jinja2Template.py
Created June 9, 2012 06:44
Set global_env on Jinja2Template Class
#!/opt/python/bin/python
class Jinja2Template(BaseTemplate):
global_env = None
def prepare(self, filters=None, tests=None, **kwargs):
from jinja2 import Environment, FunctionLoader
if 'prefix' in kwargs: # TODO: to be removed after a while
raise RuntimeError('The keyword argument `prefix` has been removed. '
'Use the full jinja2 environment name line_statement_prefix instead.')
self.env = Environment(loader=FunctionLoader(self.loader), **kwargs)