Skip to content

Instantly share code, notes, and snippets.

View petri's full-sized avatar

Petri Savolainen petri

View GitHub Profile
@petri
petri / predicate.py
Last active September 27, 2020 18:48
a first come, first served queing predicate for asyncio Condition
class my_turn:
"""a first come, first served coordinator predicate for
asyncio.Condition.wait_for(predicate); could also do away
the 'caller' by just hardcoding it to the current task
"""
queue = []
def __init__(self, caller):
type(self).queue.insert(0, caller)
@petri
petri / bouncer.py
Last active September 28, 2020 14:28
generic helper awaitable to synchronize awaiters chronologically on a first-come first-served basis
class bouncer(Awaitable):
"serve awaiters on a first-come first-served basis"
queue = []
def __init__(self):
type(self).queue.append(self.caller)
@property
def caller(self):
@petri
petri / test.py
Created November 27, 2020 20:56
test example
def test_cmdline_logging_info(caplog, capsys):
retv = tnefparse.cmdline.tnefparse(('-l', 'INFO', '-rb', 'tests/examples/rtf.tnef'))
assert not retv
stdout, _ = capsys.readouterr()
assert len(stdout) == 593
assert caplog.record_tuples == [
('tnefparse', logging.INFO, 'Skipping checksum for performance'),
]
def tnefparse(argv=None):
@petri
petri / get_products.py
Created August 31, 2022 20:12
get product metadata from web page in Python
import extruct
import requests
from w3lib.html import get_base_url
from urllib.parse import urlparse
url = "https://www.kalevala.fi/collections/korvakorut/products/paratiisi-tappikorvakorut-hopea"
@petri
petri / temp.py
Created August 13, 2024 18:49
get raspberry PI temperature straight from /dev/vcio
import os
import struct
import fcntl
from decimal import Decimal
DEVICE_FILE_NAME = "/dev/vcio"
IOCTL_MBOX_PROPERTY = 0xC0046400 # _IOWR(MAJOR_NUM, 0, char *) adjusted for alignment
GET_GENCMD_RESULT = 0x00030080
MAX_STRING = 1024