Category: Programming

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

  • Adding number formatting to percentages in phpPowerPoint Charts

    Below is a patch to add number formatting to percentages in phpPowerPoint Pie Charts: {{{ lang=diff Index: Shape/Chart/Series.php =================================================================== — Shape/Chart/Series.php (revision 71171) +++ Shape/Chart/Series.php (working copy) @@ -101,6 +101,15 @@ * @var boolean */ private $_showPercentage = false; + + /** + * $_percentFormat + * Added to set the number format of series…

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

  • Adding HTML Emails To Trac 1.0.1

    [[[TOC]]] ——- =Intro= Below is a patch to add HTML Emails to Trac and below that is a sample template to generate a nice looking email. The completed file can be located [[file:notification.zip|Here]] and should be placed in `trac/ticket/notification.py`. This patch has been tested with [[http://trac.edgewall.org/|Trac 1.0.1dev-r11400]]. Because we will be mucking with Trac’s source,…

  • Setup Trac w/ multiple projects, mod_wsgi, LDAP & MySQL – Ubuntu 12.10

    [[[TOC]]] —– = Installation = {{{ sudo apt-get install -y apache2 libapache2-mod-python python-setuptools python-genshi mysql-server python-mysqldb easy_install a2enmod #< Base Packages sudo apt-get install -y libapache2-mod-wsgi #< WSGI support sudo apt-get install -y subversion #< Subversion (not covering the config) sudo easy_install trac #< Trac }}} ------------ = Prep MySQL = * Log in {{{…

  • Round To Nearest X

    Sometimes it is necessary to round a number to the nearest X (.1,5,10, etc). Below is some code to do that: ==PHP== {{{ lang=php line=1 # Round int to nearest $round_to function round_int($n, $round_to=10){ return round($n / $round_to) * $round_to; } # Round float to nearest $round_to, configurable rounding down function round_float($n, $round_to = .1,…

  • PHP Levenshtein distance

    Below is my implementation of Levenshtein distance in PHP. This is useful to determine the distance between two latitude & longitude pairs. {{{ lang=php line=1 $lat1 = deg2rad($location1->lat); $lng1 = deg2rad($location1->lng); $lat2 = deg2rad($location2->lat); $lng2 = deg2rad($location2->lng); $theta = $location1->lng – $location2->lng; $dist = sin($lat1) * sin($lat2) + cos($lat1) * cos($lat2) * cos(deg2rad($theta)); $dist =…

  • Median and Quartiles – Python

    [[[TOC]]] ———- = Median = {{{ lang=python line=1 import math nums = [5, 9, 10, 11, 22, 1, 0, -1] #< Fill list with values nums.sort() #< Sort the list in ascending order try: mid_num = ( len( nums ) - 1) / 2 median = nums[ mid_num ] except TypeError: #< There were an…

  • Generic Worker Thread – PyQT

    Below is a generic threading worker in PyQT: {{{ lang=python lines=1 from PyQt4 import QtGui, QtCore import logging class worker_thread(QtCore.QThread): ## Generic worker thread finished = QtCore.pyqtSignal(dict) #< Replace this for data type that will be returned def __init__(self, ret_function, function, *args, **kwargs): ## Init # @param Function ret_function Function to run on end #…