From 59b2e746a4e6d0e24fed3806ed2ac13e092507cd Mon Sep 17 00:00:00 2001 From: Michael Whittaker Date: Wed, 20 Jan 2021 22:04:18 -0800 Subject: [PATCH] Added examples.py --- .gitignore | 107 ++++++++++++++++++++++++++++++++++++++++++++ examples.py | 31 +++++++++++++ quorums/__init__.py | 0 quorums/quorums.py | 20 ++++----- 4 files changed, 148 insertions(+), 10 deletions(-) create mode 100644 examples.py create mode 100644 quorums/__init__.py diff --git a/.gitignore b/.gitignore index 9090bd6..2c9f974 100644 --- a/.gitignore +++ b/.gitignore @@ -275,3 +275,110 @@ TSWLatexianTemp* # standalone packages *.sta + +# python ####################################################################### +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +.hypothesis/ +.pytest_cache/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +.static_storage/ +.media/ +local_settings.py + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# pyenv +.python-version + +# celery beat schedule file +celerybeat-schedule + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ diff --git a/examples.py b/examples.py new file mode 100644 index 0000000..631c883 --- /dev/null +++ b/examples.py @@ -0,0 +1,31 @@ +from quorums.quorums import * + +a = Node('a') +b = Node('b') +c = Node('c') +d = Node('d') +e = Node('e') +f = Node('f') +g = Node('g') +h = Node('h') +i = Node('i') + + +def main(): + # TODO(mwhittaker): Add more quorums and tidy up. + quorum_systems = { + 'majority': QuorumSystem(reads=majority([a, b, c])), + 'read one, write all': QuorumSystem(reads=choose(1, [a, b, c])), + 'write one, read all': QuorumSystem(writes=choose(1, [a, b, c])), + '3 by 3 grid': QuorumSystem(reads=a*b*c + d*e*f + g*h*i), + } + + for name, qs in quorum_systems.items(): + sigma = qs.strategy(read_fraction=0.5) + print(name) + print(qs) + print(sigma) + print(sigma.load(read_fraction=0.5)) + +if __name__ == '__main__': + main() diff --git a/quorums/__init__.py b/quorums/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/quorums/quorums.py b/quorums/quorums.py index 5a27c50..ad0c44a 100644 --- a/quorums/quorums.py +++ b/quorums/quorums.py @@ -420,16 +420,16 @@ class ExplicitStrategy(Strategy[T]): return np.random.choice(self.writes, p=self.write_weights) -a = Node('a', write_capacity=200, read_capacity=400) -b = Node('b', write_capacity=100, read_capacity=200) -c = Node('c', write_capacity=50, read_capacity=100) - -qs = QuorumSystem(reads = a*b + a*c) -print(list(qs.read_quorums())) -sigma = qs.strategy(read_fraction=0.5) -print(list(qs.write_quorums())) -print(sigma) -print(1 / sigma.load(read_fraction=0.5)) +# a = Node('a', write_capacity=200, read_capacity=400) +# b = Node('b', write_capacity=100, read_capacity=200) +# c = Node('c', write_capacity=50, read_capacity=100) +# +# qs = QuorumSystem(reads = a*b + a*c) +# print(list(qs.read_quorums())) +# sigma = qs.strategy(read_fraction=0.5) +# print(list(qs.write_quorums())) +# print(sigma) +# print(1 / sigma.load(read_fraction=0.5)) # d = Node('d') # e = Node('e')