Skip to content

Instantly share code, notes, and snippets.

@rickardlindberg
rickardlindberg / .vimrc
Created November 16, 2024 13:15
Auto formatting in Vim
autocmd BufWritePre *.py call Format("black --quiet -")
autocmd BufWritePre *.json call Format("jq")
autocmd BufWritePre *.py call Format("sed 's/ *$//'")
function! Format(command)
let view = winsaveview()
keepjumps silent exec "%!" . a:command
if v:shell_error > 0
silent undo
endif
import itertools
class World:
def __init__(self, interactions):
self.objects = []
self.interactions = interactions
def add(self, item):
self.objects.append(item)
@rickardlindberg
rickardlindberg / test.py
Last active June 18, 2023 17:41
Python Mock does not detect changed signature
from unittest.mock import Mock
import unittest.mock
import doctest
class Calculator:
"""
>>> mock_display = Mock(Display)
>>> calculator = Calculator(mock_display)
>>> calculator.add(3)
@rickardlindberg
rickardlindberg / pygame_cairo.py
Created May 20, 2023 06:42
Example how to display images drawn with Cairo in Pygame.
import cairo
import pygame
import sys
SIZE = (400, 400)
def draw_frame(cairo_context, position):
cairo_context.set_source_rgb(1, 0, 0)
cairo_context.rectangle(position, position, 10, 10)
cairo_context.fill()
@rickardlindberg
rickardlindberg / gist:857329
Created March 6, 2011 14:25
string calculator kata in python
#!/usr/bin/python
import unittest
class StringCalculatorSpec(unittest.TestCase):
def test_returs_0_for_empty_string(self):
when_calculating("").the_result_is(0)
def test_returns_single_numbers(self):
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
class MyApp: public wxApp
{
public:
virtual bool OnInit();
@rickardlindberg
rickardlindberg / Bowling.hs
Created June 30, 2013 08:06
Bowling kata in Haskell.
import Test.Hspec
score :: [Int] -> Int
score rolls = score' 10 rolls
where
score' 0 rolls = 0
score' framesLeft rolls =
let (frameScore, restRolls) = popFrame rolls
in frameScore + score' (framesLeft - 1) restRolls
@rickardlindberg
rickardlindberg / Bowling.hs
Last active December 18, 2015 07:59
Bowling kata with kajgo 10 Jun 2013.
main = do
print $ score (replicate 20 0) == 0
print $ score (replicate 20 1) == 20
print $ score ([4,6,1,0] ++ replicate 16 0) == 10 + 1 + 1
print $ score ([10,6,1] ++ replicate 16 0) == 10 + 6 + 1 + 6 + 1
print $ score (replicate 18 0 ++ [3,7,1]) == 11
print $ score (replicate 18 0 ++ [10,5,2]) == 10 + 5 + 2
print $ score (replicate 18 0 ++ [10,10,10]) == 30
print $ score (replicate 12 10) == 300
@rickardlindberg
rickardlindberg / gist:1395519
Created November 26, 2011 11:57
My first complete implementation of game of life
import Control.Concurrent
import System.Console.ANSI
-- Logic
data Cell = Alive | Dead
deriving (Eq, Show)
type Neighbours = [Cell]
@rickardlindberg
rickardlindberg / calc.py
Created July 23, 2010 11:50
TDD Calculator
# Response to http://misko.hevery.com/2009/11/17/how-to-get-started-with-tdd/
import unittest
class CalculatorTest(unittest.TestCase):
def setUp(self):
class MockView(object):