Skip to content

Instantly share code, notes, and snippets.

@petrblahos
petrblahos / sym_in_py.py
Created October 12, 2012 17:52
Symbolická matematika v Pythonu
class Expression(object):
def __add__(self, rt):
return Plus(self, rt)
def __sub__(self, rt):
return Minus(self, rt)
def __mul__(self, rt):
return Multiply(self, rt)
def __div__(self, rt):
return Divide(self, rt)
@petrblahos
petrblahos / sqlabaan.py
Created November 12, 2012 10:28
SQLAlchemy dialect for renamed columns
import copy
import sqlalchemy.engine.base
import sqlalchemy.dialects.mssql.base
import sqlalchemy.dialects.mssql.pyodbc
class SQLABaanRowProxy(sqlalchemy.engine.base.RowProxy):
def __init__(self, parent, row, processors, keymap):
"""
For all keys starting with "t_" adds also a "t$..." key.
@petrblahos
petrblahos / tiny1.py
Created December 6, 2012 19:23
tiny wsgi with webob
from webob import Response
class TinyApp(object):
def __call__(self, environ, start_response):
resp = Response("""<html><body>AHOJ</body></html>""")
return resp(environ, start_response)
if __name__ == '__main__':
from wsgiref.simple_server import make_server
@petrblahos
petrblahos / tiny2.py
Created December 6, 2012 19:31
Slightly less tiny webob app
import time
from webob import Request, Response
from webob import exc
def slow_generator():
yield "<html><body><h1>Starting</h1>\n"
for i in range(10):
yield("%s: Hello!!! Hello!!! Hello!!! Hello!!! Hello!!! Hello!!!<br>\n" % (i+1))
time.sleep(0.4)
@petrblahos
petrblahos / gist:4541213
Created January 15, 2013 19:21
pysktest1/tests/__init__.py
import sys
import unittest
class _WritelnDecorator(object):
"""Used to decorate file-like objects with a handy 'writeln' method"""
def __init__(self,stream):
self.stream = stream
def __getattr__(self, attr):
if attr in ('stream', '__getstate__'):
raise AttributeError(attr)
@petrblahos
petrblahos / gist:5050586
Created February 27, 2013 18:59
Small example of bottle.py usage.
import json
import bottle
@bottle.get("/text")
def text():
return "Returning text"
@bottle.get("/data/get")
def get_data():
#!/bin/sh
sleep 2400
state=`grep "sd[ab] " /proc/diskstats`
same_counter=0
echo "starting" >> /haltit
while [ ${same_counter} -le 60 ] ; do
#!/bin/sh
case "$1" in
start|startup)
echo "starting sleep_on_dist" >> /etc/zyxel/storage/sysvol/.debian/root/ssstart
/etc/zyxel/storage/sysvol/.debian/root/sleep_on_disk.sh &
echo "started?" >> /etc/zyxel/storage/sysvol/.debian/root/ssstart
;;
stop|shutdown)
;;
*)
import csv
import sqlite3
import sys
db_con = sqlite3.connect(":memory:")
db_con.text_factory = str
def safe_column_name(name):
"""
Modifies the column name so that it only contains alphanumeric
@petrblahos
petrblahos / gtk_leds1.py
Created March 19, 2014 18:58
Small Gtk app that toggles images according to the button state.
from gi.repository import Gtk
class LedControl(Gtk.Window):
"""
A simple app that changes images according to the button status.
"""
def __init__(self):
Gtk.Window.__init__(self, title="LED Control")
self.set_border_width(6)