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
Last active June 24, 2019 11:36
Step 7: Transaction Endpoint
import hashlib
import json
from textwrap import dedent
from time import time
from uuid import uuid4
from flask import Flask, jsonify, request
...
@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
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 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 January 28, 2021 15:29
Blockchain: Step 3
import hashlib
import json
from time import time
class Blockchain(object):
def __init__(self):
self.current_transactions = []
self.chain = []
@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 November 10, 2022 20:12
Step 6: Setting up Flask
import hashlib
import json
from textwrap import dedent
from time import time
from uuid import uuid4
from flask import Flask
class Blockchain(object):
@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 / walk_folders.py
Last active January 25, 2023 07:46
Example of recursively walking a folder using Django Storages API
from pathlib import Path
from django.core.files.storage import default_storage
def walk_folder(storage, base="/", error_handler=None):
"""
Recursively walks a folder, using Django's File Storage.
:param storage: <Storage>
:param base: <str> The base folder
@dvf
dvf / asyncio.md
Last active March 7, 2023 09:32
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: