Posts tagged ‘for the benefit of google’
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.
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.
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] * (WINDOW- 1), data]) weightings = numpy.repeat(1.0, WINDOW) / WINDOW numpy.convolve(extended_data, weightings)[WINDOW-1:-(WINDOW-1)]
Matplotlib’s pyplot library has the following functions for drawing lines:
The relevant plot in the gallery is here:
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.
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.
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.
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 .
Official api docs.