Posts tagged ‘for the benefit of google’

Code exceptions to syslog in addition to standard out

I’m not sure where the limits of where syslog should be used are, but for some use cases writing to syslog is very useful. In particular, syslog is used as “the place to go if there is any problems with hardware, or the low level running of my system”, and having such a common dumping ground is quite useful.

The following function call will start writing exceptions from a python script to syslog in addition to standard error (or wherever you were logging to before)

# Code log exceptions to syslog in addition to standard out

import sys
import syslog
import traceback

def syslog_exceptions():
    hook = sys.excepthook
    def new_hook(type, value, traceback):
        hook(type, value, traceback)
        output = traceback.format_exception(type, value, traceback)
        syslog.syslog(output)
    new_hook.previous_hook = hook
    sys.excepthook = new_hook

P.S There was a patch about a year ago to add this to the python standard library… looks like nothing happened alas.

May 21, 2011 at 12:44 am Leave a comment

Wifi tethering out of the box on gingerbread without root

I spent quite a while being annoyed at needing root for wireless tethering on my android phone until I discovered that google had quietly hidden this in their setting menu under tethering and portable hot spots section of their wireless and network settings. But once I found this everything seemed to work fine. I am however not sure whether this option will be blocked on slightly more evil mobile services than mine.

May 8, 2011 at 8:28 pm Leave a comment

Python numpy moving average for data

The following examples produces a moving average of the preceding WINDOW values. We truncate the first (WINDOW -1) values since we can’t find the average before them. (The default behaviour for convolution is to assume that values before the start of our sequence are 0). (More formally, we construct the sequence y for the sequence x where y_i = (x_i + x_(i+1) + …. x_(i+n)) / n)

WINDOW = 10
data = [1,2,3,4,5,5,5,5,5,5,5,5,5,5,5]
weightings = numpy.repeat(1.0, WINDOW) / WINDOW
numpy.convolve(data, weightings)[WINDOW-1:-(WINDOW-1)]

This makes use of numpy’s convolution function. This is a general purpose moving average operation.

Changing weightings makes some values more important; offsetting appropriately allows you to view average as around point rather than before point.

Rather than truncating values we can fix the initial values in place, as illustrated in this example:

WINDOW = 10
data = [1,2,3,4,5,5,5,5,5,5,5,5,5,5,5]
extended_data = numpy.hstack([[data[0]] * (WINDOW- 1), data])
weightings = numpy.repeat(1.0, WINDOW) / WINDOW
numpy.convolve(extended_data, weightings)[WINDOW-1:-(WINDOW-1)]

February 24, 2011 at 11:58 pm 5 comments

Setting the minimum values for axes in matplotlib

pylab.axis(xmin=0, ymin=0)

February 24, 2011 at 11:28 pm Leave a comment

Drawing lines with matplotlib

Matplotlib’s pyplot library has the following functions for drawing lines:

axvline – A full vertical line (does not start or stop)
vlines – A vertical line segment (or multiple)
axhline – A full horizontal line
hlines – …

The relevant plot in the gallery is here:

http://matplotlib.sourceforge.net/examples/pylab_examples/axhspan_demo.html

March 25, 2010 at 3:55 pm Leave a comment

Postgres sql basic querying based on datetime

For the benefit of google (and me when I’m using google)

In postgres and probably most other SQLs the following

SELECT * FROM table where creation_date > timestamp 'now' - interval '2 hours';

finds all records that were created in the last 2 hours.

See also:
http://www.postgresql.org/docs/8.0/static/datatype-datetime.html

March 11, 2010 at 9:29 pm Leave a comment

Python switching off buffering to stdout

For the benefit of google.

To switch off buffering of stdout in python one can use the following code:

def switch_off_buffering():
        sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
switch_off_buffering()

Note that some buffering occurs by default.

Alternatively one can use the -u option to python.

See also:
http://stackoverflow.com/questions/107705/python-output-buffering

March 11, 2010 at 9:16 pm Leave a comment

Javascript: keyCode versus charCode

Javascript key press events have both a keyCode and a charCode property. These are mutually exclusive – whenever one is non-zero the other is zero.

Which one is set depends on the type of event: keydown and keyup events give one events with keyCode set (they produce corresponding character) whilst keypress events give one events with charCode set.

July 26, 2009 at 11:35 pm Leave a comment

Reading files in factor

Working code for the impatient

The following factor code reads and counts the number of words in the file “/etc/fstab”

USING: io.encodings.ascii io io.files prettyprint splitting sequences ;
"/etc/fstab" ascii file-contents " \n\t" split length .

See also

Official api docs.

April 10, 2009 at 7:08 pm Leave a comment


May 2024
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031