, , ,

Author: Gugu Ncube

  • 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…

  • Unix – Set date and time

    sudo date -s “01/18/2013 17:18:00” # Fri Jan 18 17:18:00 CET 2013 sudo date -s “YYYY-mm-DD HH:MM:SS-MS:00” Example: sudo date -s “2013-01-16 13:57:56-06:00” > man date DATE(1) User Commands DATE(1) NAME date – print or set the system date and time SYNOPSIS date [OPTION]… [+FORMAT] date [-u|–utc|–universal] [MMDDhhmm[[CC]YY][.ss]] DESCRIPTION Display the current time in 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…

  • pgBouncer – Installation and Configuration in Ubuntu

    PgBouncer is a great lightweight connection pool for Postgres Installation sudo apt-get install -y pgbouncer Configuration Example pgbouncer.ini configuration file postgres = host=127.0.0.1 port=5432 dbname=postgres postgres = host=127.0.0.1 dbname=postgres postgres = host=127.0.0.1 port=5432 user=jon password=doe client_encoding=UNICODE datestyle=ISO connect_query=’SELECT 1′ logfile = pgbouncer.log pidfile = pgbouncer.pid listen_addr = * listen_port = 6000 unix_socket_dir = /tmp auth_type…

  • 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