Skip to content

Instantly share code, notes, and snippets.

@melpomene
melpomene / toy_blockchain.py
Last active November 13, 2021 07:28
Toy blockchain implementation demonstrating Proof of work
# This Python file uses the following encoding: utf-8
from copy import deepcopy
from hashlib import sha256
class Blockchain:
def __init__(self, difficulty = 1):
genesis_block = GenesisBlock()
self.chain_tail = genesis_block
self.geneis_block = genesis_block
@melpomene
melpomene / binarysearchtree.py
Created April 16, 2014 21:10
Binary search tree in python
class Node():
def __init__(self, value):
self.value = value
self.left = None
self.right = None
def append(self, value):
if value < self.value:
@melpomene
melpomene / compare_duck_type_speed.py
Created January 28, 2014 11:04
Compare isinstance, type and ducktyping to see which one is quickest.
import time
""""
Compare isinstance, type and ducktyping to see which one is quickest.
Typical output
=============
IsInstance 0.00567038607597
ducktyping 0.0232819545269
type 0.004327688694
@melpomene
melpomene / check_vpn.py
Created September 22, 2013 14:31
VPN verification and reconnection script for Ubuntu with network-manager.
import os, requests, time
from datetime import datetime
import socket
URL = "https://showmemyip.example.com" # EDIT THIS. NEEDS TO BE A URL THAT RETURNS YOUR PUBLIC IP.
CORRECT_REVERSE_URL = "ipredator.se" # EDIT THIS TO BE THE REVERSE-URL OF YOUR VPN PROVIDER
VPN_CONNECTION_NAME = "Ipredator" # EDIT THIS TO THE NAME OF THE VPN CONNECTION IN NETWORK-MANAGER.
"""
Run this script in cron to verify that you are connected to your desired VPN service
which is automatically reconnected if the connection is lost
all_questions = None
for database in databases:
if all_questions is None:
all_questions = load_questions(database=database,verified=True)
build_something(questions)
@melpomene
melpomene / download.py
Last active April 25, 2021 01:58
Script to download all your images from dailyview.com (gamla bilddagboken.se)
#!/usr/bin/env python
# encoding: utf-8
"""
Python script to download all your dailyview.com images
Run with 'python download.py'
Depends on requests and Beautiful soup
"""
@melpomene
melpomene / lasttuesdaysocietypodcast.py
Created July 13, 2013 14:05
Python script to download all the Last Tuesday Society podcasts in one swoop
#!/usr/bin/env python
# encoding: utf-8
"""
Python script to download all the Last Tuesday Society podcasts in one swoop
Find them here: http://www.thelasttuesdaysociety.org/podcasts.html
Run with 'python lasttuesdaysocietypodcast.py'
Depends on requests and Beautiful soup
"""
(1000603665) {"1000046259": 1.0, "1000045077": 1.0}
(1001390102) {"1000913520": 1.0, "1000042346": 1.0}
(1004797975) {"1000046435": 1.0, "1003829903": 1.0}
.... some 200k rows more ...
(1004536000)-[:SIMILAR]->(1000840000)
(1004536000)-[:SIMILAR]->(1005442000)
@melpomene
melpomene / toXRFF.py
Last active December 16, 2015 11:18
Converts the card into XRFF format, allowing WEKA to read UTF-8 data.
#!/usr/bin/env python
# encoding: utf-8
import codecs
from collections import Counter
class Card:
def __init__(self, data):
self.idnr= data[0].strip("\n")
self.category = data[1].strip("\n")
self.star = data[2].strip("\n")
self.name = data[3].strip("\n")
@melpomene
melpomene / tostringkernelarff.py
Last active December 16, 2015 09:49
This script transforms the tab seperated files to ARFF to allow us to use String Kernel from SVM. There are some issues with WEKA and UTF-8 yet to be resolved.
#!/usr/bin/env python
# encoding: utf-8
import codecs
from collections import Counter
class Card:
def __init__(self, data):
self.idnr= data[0].strip("\n")
self.category = data[1].strip("\n")
self.star = data[2].strip("\n")
self.name = data[3].strip("\n")