Skip to content

Instantly share code, notes, and snippets.

@rubik
rubik / infos.py
Created January 5, 2011 16:06
Python archives infos
import os
import time
import tarfile
import zipfile
def bytes2str(bytes):
if bytes < 0: return "0 B"
if bytes < 1024: return "%.2f B" % (bytes)
elif bytes < 1048576: return "%.2f Kb" % (bytes / 1024)
elif bytes < 1073741824: return "%.2f Mb" % (bytes / 1048576)
from __future__ import division
import sys
class Person(object):
attrs = ('name', 'surname', 'birth', 'birth_place', 'gender', 'marital_status', 'address', 'job')
def __init__(self, name, surname, birth, birth_place, gender, marital_status, address, job):
kw = locals()
@rubik
rubik / werkzeug-pyg_themes.diff
Created March 27, 2011 09:26
Diff between Werkzeug and Pyg Sphinx themes
--- a/werkzeug.css_t
+++ b/pyg.css_t
@@ -7,8 +7,8 @@
*/
{% set page_width = '980px' %}
-{% set sidebar_width = '220px' %}
-{% set font_family = "Georgia, serif" %}
+{% set sidebar_width = '230px' %}
+{% set font_family = "'Ubuntu', serif" %}
@rubik
rubik / badmo.py
Created April 20, 2011 10:38
Bad modules detector (modules imported but not used)
import re
import os
import ast
import glob
import collections
class ImportVisitor(ast.NodeVisitor):
def __init__(self):
self.imports = []
self.modules = collections.defaultdict(list)
@rubik
rubik / vc.py
Created July 3, 2011 10:28
vc.py
'''
vc.py is a slight modification of virtualenvcontext, and it offers the possibility
to use the context manager without virtualenvwrapper.
This is useful while testing things, because it allows you to works with ad hoc
virtualenvs.
'''
## Modified from virtualenvcontext (C) 2011 Ralph Bean
## http://pypi.python.org/pypi/virtualenvcontext/
@rubik
rubik / frac_math.py
Created December 7, 2011 16:22
Floor and ceil function without computing division
def floor(x, y):
return (x - x % y) / float(y)
def ceil(x, y):
return floor(x - 1, y) + 1
def integer_part(x, y):
if x * y >= 0:
return floor(x, y)
return ceil(x, y)
@rubik
rubik / a_cfrac.py
Created December 10, 2011 10:32
Creating a simple continued fraction from a square root
import math
def from_root(n):
'''
Construct a continued fraction from a square root. The argument
`n` should be an integer representing the radicand of the root:
>>> from_root(2)
(1, [2])
@rubik
rubik / find_roots.py
Created February 29, 2012 16:07
Polynomial parsing and division (by Ruffini's method)
import poly
import operator
import fractions
import functools
import itertools
def is_root(p, k):
'''
True if k is a root of the polynomial p, False otherwise.
@rubik
rubik / kivy_multistroke_profiling.txt
Created April 8, 2012 18:13
Kivy multistroke recognition profiles (with cProfile)
1964 function calls in 0.024 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.024 0.024 <string>:1(<module>)
2 0.000 0.000 0.000 0.000 clock.py:121(_hash)
2 0.000 0.000 0.000 0.000 clock.py:135(__init__)
1 0.000 0.000 0.000 0.000 clock.py:159(get_callback)
1 0.000 0.000 0.000 0.000 clock.py:178(release)
@rubik
rubik / split_in_columns.py
Created April 25, 2012 08:57
Split a number in batches
'''
Split a long number in batches.
>>> k = 178684461669052552311410692812805706249615844217278044703496837914086683543763273909969771627106004287604844670397177991379601L
>>> print pretty(k)
17868446 16690525 52311410 69281280 57062496 15844217 27804470 34968379
14086683 54376327 39099697 71627106 00428760 48446703 97177991 379601
>>> print pretty(k, 3, 9)