, , ,

Category: programming

  • Unix – Kill Processes

    KILL kill -KILL 9573 > man kill KILL(1) User Commands KILL(1) NAME kill – send a signal to a process SYNOPSIS kill [options] […] DESCRIPTION The default signal for kill is TERM. Use -l or -L to list available signals. Particularly useful signals include HUP, INT, KILL, STOP, CONT, and 0. Alternate signals may be…

  • Unix – tar – package and compress files and directory

    Create an archive To create an archive of the entire test directory, issue the following command: tar cvf my_arch.tar /home/oracle/alex/test Compress the Files or Directory with gzip tar cvf – filenames | gzip > file.tar.gz Compress the Files or Directory with bzip2 tar cvf – directorypath | bzip2 > file.tar.bz2 If you want to include…

  • 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