Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
https://tradingstrategy.ai

Mikko Ohtamaa miohtama

🏠
https://tradingstrategy.ai
View GitHub Profile
@miohtama
miohtama / dramatiq-task-queue.py
Last active Nov 6, 2021
Throttling background task queue using Dramatiq - wait until more workers are freed
View dramatiq-task-queue.py
import time
from typing import Dict, Optional
from dramatiq import Message, Broker
from dramatiq.results import ResultMissing
class TaskQueue:
"""A task queue using Dramatiq background task framework.
@miohtama
miohtama / bsh-notes.bash
Created Nov 4, 2021
Binance Smart Chain notes
View bsh-notes.bash
./geth_linux \
--config ./config.toml \
--datadir ./data/bsc \
--cache 8000 \
--rpc.allow-unprotected-txs \
--txlookuplimit 0 \
--http.port 9545 \
--http.addr 127.0.0.1 \
--http.vhosts=* \
@miohtama
miohtama / gist:34a83d870a14aa7e580d
Last active Oct 31, 2021
Safe evaluation of math expressions in Python, using byte code verifier and eval()
View gist:34a83d870a14aa7e580d
""""
The orignal author: Alexer / #python.fi
"""
import opcode
import dis
import sys
import multiprocessing
@miohtama
miohtama / gist:5216738
Created Mar 21, 2013
Enabling SSLCACertificateFile and SSLVerifyClient for one page only in Apache 2.2+
View gist:5216738
# Apache configuration for running local browser against a locally running xxxx for manual smartcard testing
# Listen 4433
<VirtualHost 127.0.0.1:4433>
# Real men use mod_proxy
DocumentRoot "/nowhere"
ServerName local-apache
ServerAdmin you@example.com
@miohtama
miohtama / transactionawaretask.py
Last active Oct 22, 2021
Transaction-aware Celery tasks for Pyramid - only run the task after the transaction has committed
View transactionawaretask.py
"""Transaction-aware Celery task handling.
Core originally written for Warehouse project https://raw.githubusercontent.com/pypa/warehouse/master/warehouse/celery.py
"""
from celery import Task
from pyramid import scripting
from pyramid.interfaces import IRequest
from pyramid.request import Request
@miohtama
miohtama / webserver.py
Created Sep 17, 2015
Using Waitress web server to run functional py.test test suite for a WSGI app in Python
View webserver.py
"""py.test fixtures for spinning up a WSGI server for functional test run."""
import threading
import time
from pyramid.router import Router
from waitress import serve
from urllib.parse import urlparse
import pytest
View erc_20_scanner.py
"""A stateful event scanner for Ethereum-based blockchains using web3.py.
With the stateful mechanism, you can do one batch scan or incremental scans,
where events are added where the scanner left last time.
Copyright 2021 Mikko Ohtamaa, https://twitter.com/moo9000, licensed under MIT
"""
import datetime
import time
@miohtama
miohtama / gist:5389146
Created Apr 15, 2013
Decoding emails in Python e.g. for GMail and imapclient lib
View gist:5389146
import email
def get_decoded_email_body(message_body):
""" Decode email body.
Detect character set if the header is not set.
We try to get text/plain, but if there is not one then fallback to text/html.
:param message_body: Raw 7-bit message body input e.g. from imaplib. Double encoded in quoted-printable and latin-1
View erdpy-failed-transaction.py
from erdpy.accounts import Account, Address
from erdpy.proxy import ElrondProxy
from erdpy.transactions import BunchOfTransactions
from erdpy.transactions import Transaction
from erdpy.wallet import signing
proxy = ElrondProxy("https://devnet-gateway.elrond.com")
sender = Account(pem_file="test-wallet.pem")
sender.sync_nonce(proxy)
@miohtama
miohtama / fast-web3-py-event-decoder.py
Created Aug 19, 2021
Fast event fetcher and decoder for web3.py
View fast-web3-py-event-decoder.py
"""Below is an example for faster JSON-RPC event fetcher.
It skips a lot of steps, like converting raw binary values to corresponding numbers (float, ints),
looking up ABI labels and building `AttributedDict` object.
The resulting Event dictitionary is generated faster, but harder to use and you need to know what you are doing.
"""
def _fetch_events_for_all_contracts(