Skip to content

Instantly share code, notes, and snippets.

View stuaxo's full-sized avatar
💭
 

Stuart Axon stuaxo

💭
 
View GitHub Profile
@stuaxo
stuaxo / zeromq_gtk.py
Created June 15, 2013 18:25
Send drawing commands to several gtk windows using zeromq.
#!/usr/bin/python
'''
Drawing the same images to many windows using cairo
and zero mq.
Requirements:
pyzmq, gtk3
@stuaxo
stuaxo / best_fit.py
Last active December 20, 2015 20:49
Find the largest rectangle of {aspect ratio} that can fit into the given dimensions.
def get_aspect(width, height):
""" :return aspect ratio """
return float(width) / float(height)
def get_ideal_size(width, height, ideal_aspect):
"""
:return the size of the largest rectangle with an aspect
ratio of 'ideal_aspect' that will fit into width x height.
"""
aspect = get_aspect(width, height)
@stuaxo
stuaxo / wb_to_dict.py
Last active August 26, 2022 01:20
Output an excel workbook as a python dict.Information about the formatting/colour of cells is lost - which is OK if the sheet is sensible. Individual rows are output horizontally.Types are converted to python types.This is not comprehensive + just enough for the sheet I happen to be working with today.
import datetime
import xlrd
from pprint import PrettyPrinter
def trim_nones(li):
""" remove trailing Nones from a list """
while li and li[-1] is None:
li.pop()
@stuaxo
stuaxo / outline_for_spreadsheet_logging.py
Created October 11, 2013 13:10
Outline for a logger using with statements, aimed at spreadsheets.
from contextlib import contextmanager
import Queue
import threading
import collections
class Cursor(object):
def __init__(self, worksheet):
self.worksheet = worksheet
self.col = 0
{
"metadata": {
"name": "tmp"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
@stuaxo
stuaxo / horizontal_checkboxes.py
Created February 27, 2014 17:05
attempt to make django checkboxes horizontal
class SomeModelForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(MessageModelForm, self).__init__(*args, **kwargs)
for f in self.fields.values():
if isinstance(f.widget, widgets.CheckboxInput):
f.widget.attrs.update({'class': "checkbox inline"})
# Superceded by this: https://gist.github.com/stuaxo/9308767
import Queue
import sys
import time
import select
import tty
import termios
from threading import Thread
"""
Interuptible cmd.Cmd.
This is useful if you need a long running thread like a GUI event loop.
The trick is to run the cmd.Cmd shell in the main thread, and your main
loop in another background thread.
@stuaxo
stuaxo / nodebox_color_test.py
Last active August 29, 2015 13:58
Shoebot/Nodebox color test
import random
random.seed(1)
size(550,450)
background(1,1,1)
colors=ximport("colors")
clr3 = colors.named_color("#DD7100")
clr4 = colors.named_color("yellowy")
@stuaxo
stuaxo / ipython_cairo.ipynb
Last active August 29, 2015 14:02
Display Hooks for pycairo, cairocffi Surfaces and Contexts.\n
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.