Tag: Python

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

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

  • 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, […]

  • 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, […]

  • 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 # […]