Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View travishen's full-sized avatar
🎹
Play till the end.

ssivart travishen

🎹
Play till the end.
View GitHub Profile
def quantity(storage_name):
def getter(instance):
return instance.__dict__[storage_name]
def setter(instance, value):
if value > 0:
instance.__dict__[stoage_name] = value
raise ValueError()
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
class CartItem:
def __init__(self, weight):
self.weight = weight
def get_weight(self):
return self.__weight
def set_weight(self, val):
if val > 0:
self.__weight = val
class Record:
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
def __eq__(self, other):
if isinstance(other, Record):
return self.__dict__ = other.__dict__
return NotImplement
class DatabaseMissingError(RuntimeError):
class FrozenJSON:
"""A read-only facade for nevigating a JSON-like object"""
def __init__(self, mapping):
self.__data = dict(mapping)
def __getattr__(self, name):
if hasattr(self.__data, name):
return getattr(self.__data, name)
else:
@asyncio.coroutine
def init(loop, address, port):
"""Yield a server for event loop to drive"""
app = aiohttp.web.Application(loop=loop)
# you should consider make `home` a corutine to prevent blocking
app.router.add_route('GET', '/', home)
handler = app.make_handler()
# create_server brings up the server, using handler as the protocol handler and binding it to address and port
server = yield from loop.create_server(handler, address, port)
retrun server.sockets[0].getsockname()
def main(addr='127.0.0.1', port=2323):
port = int(port)
loop = asyncio.get_event_loop()
server_coro = asyncio.start_server(handle_quries, address, port, loop=loop)
# Drive server_coro to bring up the server
server = loop.run_until_complete(server_coro)
host = server.sockets[0].getsockname()
print(f'Serving on {host}. Hit CTRL-C to stop.')
try:
@travishen
travishen / spinner_async.py
Last active May 5, 2022 11:59
Spinner asyncio
import sys
import asyncio
@asyncio.coroutine
def spin():
write, flush = sys.stdout.write, sys.stdout.flush
for char in itertools.cycle('|/-\\'):
msg = char + ' loading ...'
write(msg)
flush()
@travishen
travishen / spinner_thread.py
Created May 5, 2022 11:40
Spinner threading
import sys
import threading
class Signal:
stop = False
def spin(signal):
write, flush = sys.stdout.write, sys.stdout.flush # create alias
for char in itertools.cycle('|/-\\'):
msg = char + ' loading ...'