Skip to content

Instantly share code, notes, and snippets.

View mrts's full-sized avatar

Mart Sõmermaa mrts

  • Tallinn, Estonia (EET, UTC +2 hours)
View GitHub Profile
package util;
import org.apache.log4j.Logger;
import org.springframework.util.StopWatch;
public class DebugLogStopwatch implements AutoCloseable {
Logger logger;
StopWatch stopwatch;
# see http://groups.google.com/group/django-users/browse_thread/thread/259c1679ddaaceb8
# for the problem and http://code.djangoproject.com/ticket/12780 for the admin/options.py patch
# required to get this working
from django.forms.util import ErrorList
class ClipAdmin(admin.ModelAdmin):
def formsets_are_valid(self, formsets, form, form_is_valid, instance,
request):
valid = super(ClipAdmin, self).formsets_are_valid(formsets,
@mrts
mrts / .gitignore
Created January 17, 2011 20:45
Script that uses the Google Calendar API for automated time reports.
*.pyc
*.swp
timetrack_conf.py
@mrts
mrts / descriptors.py
Created January 30, 2011 18:58
Example of chaining descriptors and sharing a cache between them.
"""
Example of chaining descriptors and sharing a cache between them.
"""
class LeafDescriptor(object):
def __get__(self, obj, cls):
return obj.parent._cache
class Bar(object):
baz = LeafDescriptor()
@mrts
mrts / github.py
Created January 30, 2011 18:08
GitHub API with descriptors
# coding: utf-8
"""
To my great disappointment, Dustin Sallings's otherwise excellent py-github
does not work with repositories that have / in branch names, as this breaks
the XML-based parser. Unfortunately, many projects use / for simulating
directory structure. See e.g.
http://github.com/api/v2/xml/repos/show/django/django/branches
for the error.
What follows is a comprehensive interface to the GitHub JSON API that does not
import os
from django.core.management.base import CommandError, LabelCommand
from django.utils.datastructures import SortedDict
from django_commands.utils import parse_apps_and_models
class Command(LabelCommand):
args = '<upload-path> <appname.Model> [appname.Model] ...>'
help = ("Cleans orphaned file field files from <upload-path>.\n"
"'Orphaned' is defined as existing under <upload-path> "
@mrts
mrts / lazycacheddescriptor.py
Created March 9, 2011 19:12
Thread safe descriptor that combines memoizing (caching) and lazy loading
class LazyCachedDescriptor(object):
def __init__(self, fn, *args, **kwargs):
self.cache = None
self.fn = fn
self.args = args
self.kwargs = kwargs
def __get__(self, obj, objtype):
if self.cache is None:
result = [self.fn(*self.args, **self.kwargs)]
@mrts
mrts / gist:4041421
Created November 8, 2012 20:37
copy_if in C++03 and C++11
#include <algorithm>
#include <vector>
#include <iostream>
// for C++03
#include <boost/bind.hpp>
#include <boost/foreach.hpp>
#define foreach BOOST_FOREACH
using namespace std;
#ifndef SCOPEGUARD_H__
#define SCOPEGUARD_H__
#include <utility>
template <class Function>
class ScopeGuard
{
public:
ScopeGuard(Function f) :
@mrts
mrts / CGIHTTPServer.py.diff
Created February 6, 2017 21:15
Patch to Python's bundled CGIHTTPServer.py (from Python 2.7) to run Perl scripts. Useful for serving e.g. AWStats files. Copy `Python27/lib/CGIHTTPServer.py` out, apply the patch and run with `python CGIHTTPServer.py` in `wwwroot`. Then open http://localhost:8000/cgi-bin/awstats.pl?config=example.com in browser.
--- /c/Python27/lib/CGIHTTPServer.py 2015-11-02 16:20:08.000000000 +0200
+++ CGIHTTPServer.py 2017-02-06 23:05:09.734462300 +0200
@@ -103,6 +103,11 @@
head, tail = os.path.splitext(path)
return tail.lower() in (".py", ".pyw")
+ def is_perl(self, path):
+ """Test whether argument path is a Perl script."""
+ head, tail = os.path.splitext(path)
+ return tail.lower() in (".pl")