Added examples.py

This commit is contained in:
Michael Whittaker 2021-01-20 22:04:18 -08:00
parent 1b84ec3134
commit 59b2e746a4
4 changed files with 148 additions and 10 deletions

107
.gitignore vendored
View file

@ -275,3 +275,110 @@ TSWLatexianTemp*
# standalone packages # standalone packages
*.sta *.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/

31
examples.py Normal file
View file

@ -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()

0
quorums/__init__.py Normal file
View file

View file

@ -420,16 +420,16 @@ class ExplicitStrategy(Strategy[T]):
return np.random.choice(self.writes, p=self.write_weights) return np.random.choice(self.writes, p=self.write_weights)
a = Node('a', write_capacity=200, read_capacity=400) # a = Node('a', write_capacity=200, read_capacity=400)
b = Node('b', write_capacity=100, read_capacity=200) # b = Node('b', write_capacity=100, read_capacity=200)
c = Node('c', write_capacity=50, read_capacity=100) # c = Node('c', write_capacity=50, read_capacity=100)
#
qs = QuorumSystem(reads = a*b + a*c) # qs = QuorumSystem(reads = a*b + a*c)
print(list(qs.read_quorums())) # print(list(qs.read_quorums()))
sigma = qs.strategy(read_fraction=0.5) # sigma = qs.strategy(read_fraction=0.5)
print(list(qs.write_quorums())) # print(list(qs.write_quorums()))
print(sigma) # print(sigma)
print(1 / sigma.load(read_fraction=0.5)) # print(1 / sigma.load(read_fraction=0.5))
# d = Node('d') # d = Node('d')
# e = Node('e') # e = Node('e')