-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
91 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from typing import Any, Callable | ||
|
||
|
||
def basic(dataset: [Any]) -> Callable[[], Any]: ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from typing import Any, Callable, Tuple | ||
|
||
|
||
def smooth(dataset: [Tuple[Any, int]]) -> Callable[[], Any]: | ||
def get_next() -> Any: ... | ||
|
||
|
||
class ItemWeight: | ||
key: Any | ||
weight: int | ||
current_weight: int | ||
effective_weight: int |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,40 @@ | ||
import math | ||
from typing import Callable, Optional, Tuple | ||
try: | ||
from math import gcd | ||
except ImportError: | ||
from fractions import gcd | ||
|
||
|
||
def weighted(dataset: [Tuple[str, int]]) -> Callable[[], Optional[str]]: | ||
current_index = -1 | ||
current_weight = 0 | ||
# python2 workaround for python3 nonlocal keyword | ||
class Store: | ||
__slots__ = ('index', 'weight') | ||
|
||
def __init__(self, index, weight): | ||
self.index = index | ||
self.weight = weight | ||
|
||
|
||
def weighted(dataset): | ||
current = Store(index=-1, weight=0) | ||
|
||
dataset_length = len(dataset) | ||
dataset_max_weight = 0 | ||
dataset_gcd_weight = 0 | ||
|
||
for _, weight in dataset: | ||
if dataset_max_weight < weight: | ||
dataset_max_weight = weight | ||
dataset_gcd_weight = math.gcd(dataset_gcd_weight, weight) | ||
dataset_gcd_weight = gcd(dataset_gcd_weight, weight) | ||
|
||
def get_next() -> Optional[str]: | ||
nonlocal current_index | ||
nonlocal current_weight | ||
def get_next(): | ||
while True: | ||
current_index = (current_index + 1) % dataset_length | ||
if current_index == 0: | ||
current_weight = current_weight - dataset_gcd_weight | ||
if current_weight <= 0: | ||
current_weight = dataset_max_weight | ||
if current_weight == 0: | ||
current.index = (current.index + 1) % dataset_length | ||
if current.index == 0: | ||
current.weight = current.weight - dataset_gcd_weight | ||
if current.weight <= 0: | ||
current.weight = dataset_max_weight | ||
if current.weight == 0: | ||
return None | ||
if dataset[current_index][1] >= current_weight: | ||
return dataset[current_index][0] | ||
if dataset[current.index][1] >= current.weight: | ||
return dataset[current.index][0] | ||
|
||
return get_next |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from typing import Any, Callable, Tuple | ||
|
||
|
||
def weighted(dataset: [Tuple[Any, int]]) -> Callable[[], Any]: | ||
def get_next() -> Any: ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,31 @@ | ||
import pathlib | ||
|
||
import os | ||
import setuptools | ||
|
||
HERE = pathlib.Path(__file__).parent | ||
README = (HERE / "README.md").read_text() | ||
README_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), "README.md") | ||
|
||
|
||
def setup(): | ||
readme_content = '' | ||
with open(README_PATH, "r") as fp: | ||
readme_content = fp.read() | ||
setuptools.setup( | ||
name="roundrobin", | ||
version="0.0.2", | ||
description="Collection of roundrobin utilities", | ||
long_description=readme_content, | ||
long_description_content_type="text/markdown", | ||
url="https://github.com/linnik/roundrobin", | ||
author="Vyacheslav Linnik", | ||
author_email="[email protected]", | ||
license="MIT", | ||
classifiers=[ | ||
"License :: OSI Approved :: MIT License", | ||
"Programming Language :: Python :: 2", | ||
"Programming Language :: Python :: 3", | ||
], | ||
packages=setuptools.find_packages(), | ||
) | ||
|
||
|
||
setuptools.setup( | ||
name="roundrobin", | ||
version="0.0.1", | ||
description="Collection of roundrobin utilities", | ||
long_description=README, | ||
long_description_content_type="text/markdown", | ||
url="https://github.com/linnik/roundrobin", | ||
author="Vyacheslav Linnik", | ||
author_email="[email protected]", | ||
license="MIT", | ||
classifiers=[ | ||
"License :: OSI Approved :: MIT License", | ||
"Programming Language :: Python :: 3", | ||
"Programming Language :: Python :: 3.5", | ||
], | ||
packages=setuptools.find_packages(), | ||
) | ||
if __name__ == "__main__": | ||
setup() |