Skip to content

Instantly share code, notes, and snippets.

@embayer
embayer / hamming_distance.py
Created March 12, 2015 21:13
hamming distance
def hamming_dist(str1, str2, case_sensitive=False):
"""
Count the amount of differences between two strings
"""
if case_sensitive != True:
str1 = str1.lower()
str2 = str2.lower()
# string lengths
l1 = len(str1)
@embayer
embayer / validate_url.py
Created February 10, 2015 14:36
simple url validation
def validate_url(url):
"""
validates a homepage
string url: the url to validate
string return: the validate url or ""
"""
regex = re.compile(
r'^((?:http|http|ftp)s?://|www\.)' # http:// or https:// or ftp
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' #domain...
r'localhost|' #localhost...
@embayer
embayer / find_duplicates_in_dict.py
Created February 10, 2015 10:53
find duplicated values in a python dict
base_dict = {"a": "python", "b": "python", "c": "swift"}
# build a reversed multidict (values becomes keys, keys become (tuple) key(s)
rev_multidict = {}
for key, value in base_dict.iteritems():
rev_multidict.setdefault(value, set()).add(key)
for key, value in rev_multidict.iteritems():
if len(value) > 1:
# item with duplicated value (key)
@embayer
embayer / re_sub_multi.py
Created December 19, 2014 10:05
replace multiple patterns in strings
text = u"ruby coding with textmate"
rep = {
u"ruby": "python",
u"textmate": "vim"
}
rep = dict((re.escape(k), v) for k, v in rep.iteritems())
pattern = re.compile(ur"|".join(rep.keys()), re.UNICODE)
text = pattern.sub(lambda m: rep[re.escape(m.group(0))], text)
@embayer
embayer / mail_tools.py
Last active August 29, 2015 14:10
useful functions for emailing
def split_email_addr(email_address):
"""
split email addresses
string email_address: the mail address with the schema "[email protected]"
dict return: the parts of the email address or a empty dict if the schema is wrong
"""
pattern = r"(^[a-zA-Z0-9_]+).([a-zA-Z0-9_]+)@([a-zA-Z0-9-]+).([a-zA-Z0-9-\.]+$)"
match = re.search(pattern, email_address)
if not match:
@embayer
embayer / climail.py
Created December 4, 2014 08:56
send emails with command line, sendmail and python
#!/usr/local/bin/python
from sys import stdin
from os import popen, system
from subprocess import check_output
logo = """
___ __ _____ _ _
/ __\ / / \_ \ /\/\ __ _ (_) | | _ __ _ _
/ / / / / /\/ / \ / _` | | | | | | '_ \ | | | |
@embayer
embayer / vim.md
Last active November 5, 2016 12:05
vim cheatsheet

misc

<Ctrl-a> ... increase the number under the cursor (in normal mode)
<Ctrl-x> ... decrease the number under the cursor (in normal mode)

movement

H .......... (H)igh: Jump to the top of the screen
M .......... (M)iddle: Jump to the middle of the screen

L .......... (L)ow: Jump to the boottom of the screen

@embayer
embayer / exc_traceback.py
Created December 1, 2014 10:38
get exception traceback
import traceback
try:
1 / 0
except Exception as exc:
type_, value_, traceback_ = sys.exc_info()
full_exc = traceback.format_exception(type_, value_, traceback_)
self.logger.debug('%s %s' % (exc, full_exc))
@embayer
embayer / sql_collection.sql
Last active August 29, 2015 14:10
collection of useful sql statements
-- find duplicated values
SELECT
address_line1, address_line2, zip, city, country, COUNT(*)
FROM
location
GROUP BY
address_line1, address_line2, zip, city, country
HAVING
COUNT(*) > 1;

PHP UTF-8

Apache configuration (in httpd.conf or .htaccess)

AddDefaultCharset utf-8

PHP (in php.ini)

default_charset = "utf-8"