Skip to content

Instantly share code, notes, and snippets.

View vilcans's full-sized avatar

Martin Vilcans vilcans

View GitHub Profile
#!/usr/bin/env python
# Python 2 utility for converting an image file to the raw frame buffer format
# used by VZ (and others devices using the MC6847 chip) in graphics mode.
import sys
from array import array
from PIL import Image
#!/usr/bin/env python
# Convert a raw memory dump to a z80 snapshot
import struct
import re
import argparse
compress_re = re.compile(r'(.)\1{1,254}')
"""
TL;DR for http://www.librador.com/2014/07/10/Variable-scope-in-list-comprehension-vs-generator-expression/
In Python 2, a list comprehension "leaks" its variable into the current scope,
while a generator expression does not.
This means that there's a subtle difference between
`list(x for x in y)` and `[x for x in y]`.
Example:
@vilcans
vilcans / dct.py
Created January 20, 2014 23:02
Python code. Transforms an 8x8 matrix using DCT, quantizes the result, then transforms it back.
#!/usr/bin/env python
"""Transforms an 8x8 matrix using DCT,
quantizes the result,
then transforms it back.
"""
from __future__ import division
from math import sqrt, cos, pi, floor
@vilcans
vilcans / gist:3859169
Created October 9, 2012 14:27
Properties in CoffeeScript
# Create a convenient property creator function
Function::property = (properties) ->
for name, descriptor of properties
Object.defineProperty @prototype, name, descriptor
return
# Usage example:
class Circle
constructor: (@radius) ->
@vilcans
vilcans / gist:2852844
Created June 1, 2012 15:12
Funny JavaScript attributes behaviour
>>> z = 42
42
>>> z.foo = 7
7
>>> z.foo
undefined
@vilcans
vilcans / fibonacci.py
Created May 25, 2011 19:53
Comparing recursive and iterative fibonacci functions
# Calculate a number in a Fibonacci series,
# starting at fib(0) == 0 and fib(1) == 1.
# An iterative and a recursive implementation.
# Which one most clearly shows what the code does?
def iterative_fibonacci(x):
a, b = 0, 1
for n in range(x):
a, b = b, a + b
return a

This is the code I use for the tag cloud at www.librador.com:

{% for tag in site.tags %}
  {% assign t = tag.first %}
  {% assign posts = tag.last %}
  <a class="tag tag{{ posts | size }}" href="/tags/#tag-{{ t | to_id }}">{{ t }}</a>
{% endfor %}

It expects there to be a CSS class called tag and several ones called tag1, tag2, tag3 etc, where the number is the number of posts with that tag. Here's my version:

import urllib
print 'javascript:' + urllib.quote("""
(function(){
var e = document.createElement('script');
e.src='/boot.js';
e.type='text/javascript';
document.getElementsByTagName('body')[0].appendChild(e);})()
""".strip().replace('\n', ' '))