Skip to content

Instantly share code, notes, and snippets.

View dvf's full-sized avatar

Daniel van Flymen dvf

View GitHub Profile
@dvf
dvf / blockchain.py
Created September 23, 2017 19:03
Step 2.1: Creating a Transaction
class Blockchain(object):
...
def new_transaction(self, sender, recipient, amount):
"""
Creates a new transaction to go into the next mined Block
:param sender: <str> Address of the Sender
:param recipient: <str> Address of the Recipient
:param amount: <int> Amount
@dvf
dvf / blockchain.py
Last active June 29, 2019 19:54
Step 8: The Mining Endpoint
import hashlib
import json
from time import time
from uuid import uuid4
from flask import Flask, jsonify, request
...
@dvf
dvf / blockchain.py
Created September 23, 2017 20:31
Step 9: Consensus (Retrieve Chain)
@app.route('/chain', methods=['GET'])
def full_chain():
response = {
'chain': blockchain.chain,
'length': len(blockchain.chain),
}
return jsonify(response), 200
@dvf
dvf / blockchain.py
Last active March 31, 2021 18:16
Step 10: Registering Nodes
...
from urllib.parse import urlparse
...
class Blockchain(object):
def __init__(self):
...
self.nodes = set()
...
@dvf
dvf / blockchain.py
Last active May 22, 2020 14:11
Step 11: Implementing Consensus Algorithm
...
import requests
class Blockchain(object)
...
def valid_chain(self, chain):
"""
Determine if a given blockchain is valid
@dvf
dvf / blockchain.py
Last active May 22, 2020 14:10
Step 12: New Endpoints for Consensus
@app.route('/nodes/register', methods=['POST'])
def register_nodes():
values = request.get_json()
nodes = values.get('nodes')
if nodes is None:
return "Error: Please supply a valid list of nodes", 400
for node in nodes:
blockchain.register_node(node)
@dvf
dvf / blockchain.py
Last active February 22, 2018 21:10
Creating a simple Blockchain in Python
class Blockchain(object):
def __init__(self):
self.current_transactions = []
self.chain = []
def new_block(self):
# Creates a new Block in the Blockchain
pass
def new_transaction(self):
@dvf
dvf / python-setup.md
Last active December 6, 2022 13:20
Running Python locally without driving yourself insane

This is my highly opinionated way of developing with Python locally. Use it, don't use it. But you probably know that it's a PITA to manage different projects with different dependencies targeting different Python versions, and there are different ways of installing Python too:

  • Using the interpreters preinstalled in the OS 😵
  • Using brew (or apt etc.) 😅
  • Using the binaries from www.python.org 😫
  • Using pyenv 😎
@dvf
dvf / MD5.js
Created May 19, 2019 20:20
MD5 Algorithm for Google Sheets
function MD5 (input) {
var digest = Utilities.computeDigest(Utilities.DigestAlgorithm.MD5, input);
var output = "";
for (i = 0; i < digest.length; i++) {
var h = digest[i];
if (h < 0) { h += 256; }
if (h.toString(16).length == 1) { output += '0'; }
output += h.toString(16);
}
return output;
@dvf
dvf / asyncio.md
Last active February 19, 2026 08:57
How on earth does Asyncio work

What are coroutines?

Coroutines are functions that can be paused. We define them using the async syntax:

async def say_something(what):
    print(what)

But coroutines can't be run directly: