Category: Python

  • Finding The Geometric Mean In Python

    Geometric means are a quick and easy way to benchmark system/interpreter performance. The below function is an example of calculating geometric averages in Python: {{{ lang=python def geometric_mean(nums): ”’ Return the geometric average of nums @param list nums List of nums to avg @return float Geometric avg of nums ”’ return (reduce(lambda x, y: x*y,…

  • Compiling PyPy From Source

    [[http://pypy.org/|PyPy]] is significantly faster when compared against the default [[http://en.wikipedia.org/wiki/CPython|CPython]] interpreter. For the most part, this interpreter functions the same way that CPython does, and most libraries will run with no issues. In my testing, PyPy was 5-10 times faster than CPython and I was able to install/use every library other than [[http://www.riverbankcomputing.com/software/pyqt/download|PyQt4]] and [[http://trac.edgewall.org|Trac]].…

  • OpenVPN Server Configuration Script – Ubiquiti EdgeRouter Lite

    =The lazy way to configure OpenVPN Server on a Ubiquiti EdgeRouter Lite= I have a Ubiquiti EdgeRouter Lite that I use as a staging platform for systems in production. Because I have had to reconfigure the VPN so many times on this device, I created a simple Python tool to run through the entire process…

  • Dynamic DNS Using LibCloud – EdgeRouter

    This tutorial is aimed for users with a dynamic IP, and a DNS host that does not support DynDNS. Basically, we will push our new IP every time that it changes. The steps outlined in this tutorial will work with the providers listed in [[https://ci.apache.org/projects/libcloud/docs/dns/supported_providers.html#provider-matrix|Apache’s documentation]], but I have only actually tested with Rackspace. ===…

  • Install scipy for python 2.7 in Ubuntu

    Surprisingly the `scipy` package was not as straightforward of an installation as most Python modules; this tutorial will walk you through that process. # The first step is to download `easy_install`, and the libraries required for `scipy` {{{ lang=bash sudo apt-get install python-setuptools liblapack-dev libblas-dev }}} # Use `easy_install` to install `scipy` and other required…

  • Setup & Use Pymongo

    Install mongodb [[http://docs.mongodb.org/manual/tutorial/install-mongodb-on-red-hat-centos-or-fedora-linux/|Red Hat Enterprise, CentOS, or Fedora Linux]] [[http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/|Ubuntu]] [[http://docs.mongodb.org/manual/tutorial/install-mongodb-on-debian/|Debian]] [[http://docs.mongodb.org/manual/tutorial/install-mongodb-on-linux/|Generic Linux]] [[http://docs.mongodb.org/manual/tutorial/install-mongodb-on-os-x/|OS X]] [[http://docs.mongodb.org/manual/tutorial/install-mongodb-on-windows/|Windows]] Install pymongo {{{ lang=sh sudo easy_install pymongo }}} Connect with MongoClient {{{ lang=python import pymongo client = pymongo.MongoClient(‘localhost’, 27017) }}} Get a Database {{{ lang=python db = client.test_db # OR db = client[‘test-db’] }}} Get a Collection {{{ lang=python…

  • Unix Epoch Programming Reference

    Below is a table indicating how to obtain a Unix Epoch Timestamp from various programming languages: |=Language|=Command| |Actionscript| (new Date()).time | |ASP| DateDiff(“s”, “01/01/1970 00:00:00”, Now()) | |C++| #include std::time(0); | |C#| epoch = (DateTime.Now.ToUniversalTime().Ticks – 621355968000000000) / 10000000; | |Erlang| calendar:datetime_to_gregorian_seconds( calendar:now_to_universal_time( now()) )-719528*24*3600 | |Java| long epoch = System.currentTimeMillis()/1000; | |JavaScript| Math.round(new Date().getTime()/1000.0) getTime()…

  • Convert Time String (HH:MM:SS) To Seconds

    Sometimes it is necessary to find the amount of seconds in a time string for use in equations. Below are PHP and Python functions to do this. These functions work by splitting the time string by ‘:’, then calculating 60 to the power of the split time count minus 1 (allowing for MM:SS format and…

  • super() Raises `TypeError: must be type, not classobj`

    If you try to use Python’s super method, and receive {{{TypeError: must be type, not classobj}}}, your parent class is an old style class (and does not in some way inherit from object). The below code is an illustration of the issue: {{{ lang=python lines=1 class class_A: def __init__( self ): print ‘Class A’ class…

  • Enumerate Processes Programmatically In Linux

    Using `ps` is not necessarily the best way to enumerate running applications programmatically. This is because it will gather additional information that we do not actually need in most cases. The below examples illustrate how to enumerate running applications by reading the contents of `/proc`. **Python:** {{{ lang=python line=1 def get_procs(proc_name=None): ## Loop over /proc…