25th January 2019
I sometimes write code to learn things not at all related to code or technical concepts. It can be an implementation of a concept in math or even a
Resting Metabolic Rate calculator. I used this technique quite a lot back when I was in school.
Below I’m providing an example I found while looking through old hard drives during the holidays. It’s Python implementation of the basics of complex numbers. The implementation itself is useless as these features are already built into the Python standard library itself but at the time things like this helped me grasp concepts quickly.
import math
'''
Implementation of Polar and Cartesian coordinates and conversion between them.
These features exists within the Python standard library, this was meant
for educational purposes.
'''
class ComplexNumber:
def __init__(self, real, imaginary, polar = False):
if polar:
distance = real
angle = imaginary
self.real = distance * math.cos(math.radians(angle))
self.imaginary = distance * math.sin(math.radians(angle))
else:
self.real = real
self.imaginary = imaginary
@property
def distance(self):
return math.sqrt((self.real ** 2) + (self.imaginary ** 2))
@property
def angle(self):
multiplier = 1
if (self.real < 0):
# Quadrant 2 or 3
multiplier = 180
elif (self.real >= 0 and self.imaginary < 0):
# Quadrant 4
multiplier = 360
return math.degrees(math.atan(self.imaginary / self.real)) * multiplier
19th December 2018
Click the link or type runes.rocks into your web browser and you will be directed to a random article about a rune inscription or runestone.
I love small ideas and experiments. They are cheap they scale from a few hours to a few moths. An example I’m often refer to is Kyrksok.se a site we build a few years ago in less then two days.
However runes.rocks and the Swedish version runor.rocks sets a new record. It took me five minutes to buy the domain names and everything up thanks to existing tools.
I bought domain names and pointed the redirect URL to Magnus Manske’s Random article tool and waited for the name servers to update. Done.
Landerydsstenen CC-BY Bengt A Lundberg / Riksantikvarieämbetet
26th September 2018
There are multiple “On this Day” APIs built on top of Wikipedia but I created yet another one. This API can be used to retrieve birth, deaths, and events for any given day of the year.
The reason for building this was that I wanted something stable with support for things like Cross-Origin-Requests and SSL. The need wasn’t actually mine. A friend of mine sees the potential of “On this Day” information as a possible delighter in everyday contexts and I offered to help.
The data is (because it’s harvested from Wikipedia) licensed under Creative Commons Attribution-ShareAlike 3.0 Unported The API is actually just a collection of pre-generated static JSON files cached and exposed using Cloudflare. So feel free to query without limits.
API Sandbox and Documentation
9th September 2018
While working on an evaluation of Generous Interfaces in the GLAM sector at the Swedish National Heritage Board we extracted colors in images to make them searchable by their palettes. For usability reasons we wanted to limit the extracted colors to a comprehensible palette.
Colorsnap is a Python package for snapping/rounding colors to other colors/palettes. Although I could find plenty of resources and examples on how to do this, I could not find a accessible package.
So now you can do:
pip install colorsnap
Let’s take the hex color #0000ba
and round it to its nearest color available as a named color in the CSS 3 specification.
from colorsnap.palettes import CSS_3
from colorsnap import snap_color
snap_color(CSS_3, '#0000ba')
# returns the following tuple ('#0000ba', 'mediumblue')
visualized (input to the left):
Colorsnap comes with palettes for CSS 2, 2.1, 3, and 4 but you can also use your own:
from colorsnap import snap_color
palette = {
'black': '#000000',
'gray': '#808080',
'white': '#ffffff',
}
snap_color(palette, '#0000ba')
In addition to using named colors you can also round colors to basic hex values:
from colorsnap import snap_color
palette = ['#4286f4', '#414449']
snap_color(palette, '#0000ba')
It’s available over at PyPI and Github.