Category: programming
-
Unix – Grep, egrep and Recursive Grep
# files containing “import” in subdirectories grep -rHn “import” * # files containing “import” or “except” grep -rHnE “import|except” * # only .py files containing “import” or “except” grep –color –include=”*.py” -rHnE “import|except” * # python files, text files and java files grep –color –include=”*[py|txt|java]” -rHnE “^import [a-z,\s]+$” * # same as above with extended…
-
bzip2 – compress and decompress
Compress Files using bzip bzip2 archivefile1.txt Folder Compression The “.bz2” file extension is commonly used by the compression program bzip2, which is the counter part of bunzip2. You can use the “-r” option to recursively compress all the files under the specified directory. For example: tar cvf – foldername | bzip2 > foldername.tar.bz2 Keeping the…
-
Python – GeoIP City and Country using MaxMind
PyGeoIP is a pure python GeoIP API based on MaxMind’s C-based Python API. The python implementation is ported from the PHP GeoIP API written by Jim Winstead and Hans Lellelid. Option 1: Installation using PIP sudo pip install pygeoip Option 2: Download and Install from Github repository An alternative to pip is to download the…
-
Python – Timing Decoration
# -*- coding: utf-8 -*- from __future__ import print_function import time import sys from time import strftime import inspect import traceback # http://www.siafoo.net/article/68 # being nice to subsequent decorator calls or chains, otherwise losing the name of the function def timing(result=True, params=True, start=False): def timing_decorator(f): def wrapper(*args, **kw): t1 = time.time() function_result = None try:…
-
Python – Clustering Text in Python
# -*- coding: utf-8 -*- “”” Source: http://stackoverflow.com/questions/1789254/clustering-text-in-python See also: http://docs.scipy.org/doc/scipy/reference/cluster.html http://hackmap.blogspot.com/2007/09/k-means-clustering-in-scipy.html “”” import sys from math import log, sqrt from itertools import combinations def cosine_distance(a, b): cos = 0.0 a_tfidf = a[“tfidf”] for token, tfidf in b[“tfidf”].iteritems(): if token in a_tfidf: cos += tfidf * a_tfidf[token] return cos def normalize(features): norm = 1.0 /…
-
Python – bktree.py
“”” bktree.py, by bearophile Fast Levenshtein distance and BK-tree implementations in Python. The following functions are designed for Psyco, they are too much slow without it. “”” def editDistance(s1, s2): “””Computes the Levenshtein distance between two arrays (strings too). Such distance is the minimum number of operations needed to transform one array into the other,…
-
Python – Defining and Using Properties
Properties def property(function): keys = ‘fget’, ‘fset’, ‘fdel’ func_locals = {‘doc’:function.__doc__} def probe_func(frame, event, arg): if event == ‘return’: locals = frame.f_locals func_locals.update(dict((k, locals.get(k)) for k in keys)) sys.settrace(None) return probe_func sys.settrace(probe_func) function() return property(**func_locals) Example from math import radians, degrees, pi class Angle(object): def __init__(self, rad): self._rad = rad @property def rad(): ”’The angle…
-
Python – Re-Raise an Exception
Re-Raise Error def re_raise_exception(new_exc, exc_info=None): if not exc_info: exc_info = sys.exc_info() _exc_class, _exc, tb = exc_info raise new_exc.__class__, new_exc, tb
-
Python – Retry Decorator in Python
Retry Decorator import time def retry(ExceptionToCheck, tries=4, delay=3, backoff=2): def deco_retry(f): def f_retry(*args, **kwargs): mtries, mdelay = tries, delay while mtries > 0: try: return f(*args, **kwargs) except ExceptionToCheck, e: print “%s, Retrying in %d seconds…” % (str(e), mdelay) time.sleep(mdelay) mtries -= 1 mdelay *= backoff lastException = e raise lastException return f_retry # true…
-
Python – Singleton
Singleton in Python import functools def singleton(cls): “”” Use class as singleton. “”” cls.__new_original__ = cls.__new__ @functools.wraps(cls.__new__) def singleton_new(cls, *args, **kw): it = cls.__dict__.get(‘__it__’) if it: return it cls.__it__ = it = cls.__new_original__(cls, *args, **kw) it.__init_original__(*args, **kw) return it cls.__new__ = singleton_new cls.__init_original__ = cls.__init__ cls.__init__ = object.__init__ return cls Sample use @singleton class Foo:…