Skip to content

Instantly share code, notes, and snippets.

View privatwolke's full-sized avatar

Stephan Klein privatwolke

View GitHub Profile
@privatwolke
privatwolke / gather_dict.py
Created September 20, 2017 13:27
Python: Gather a dictionary of asyncio Task instances while preserving keys
async def gather_dict(tasks: dict):
async def mark(key, coro):
return key, await coro
return {
key: result
for key, result in await gather(
*(mark(key, coro) for key, coro in tasks.items())
)
}
@privatwolke
privatwolke / port_number.py
Created September 6, 2016 21:02
Create port number based on application name
from binascii import crc32
def port_number(app_name, min_port=49152, max_port=65535):
"""
Returns a port number based on the given app_name which falls in the range
[min_port, max_port].
"""
checksum = crc32(app_name) & 0xffffffff
scale = max_port - min_port
return int((checksum / float(0xffffffff)) * scale + min_port)