Skip to content

Commit

Permalink
add python 2 support
Browse files Browse the repository at this point in the history
  • Loading branch information
linnik committed Oct 20, 2020
1 parent 72dda35 commit 6b9d0d7
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 54 deletions.
10 changes: 9 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
language: python

python:
- 2.7
- 3.4
- 3.5
- 3.6
- 3.7-dev
- 3.7
- 3.8
- 3.9-dev
- nightly
- pypy
- pypy3
install:
- pip install .
script:
Expand Down
3 changes: 1 addition & 2 deletions roundrobin/basic_rr.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from itertools import cycle
from typing import Any, Callable


def basic(dataset: [Any]) -> Callable[[], Any]:
def basic(dataset):
iterator = cycle(dataset)

def get_next():
Expand Down
4 changes: 4 additions & 0 deletions roundrobin/basic_rr.pyi
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]: ...
19 changes: 6 additions & 13 deletions roundrobin/smooth_rr.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
from typing import Callable, Optional, Tuple


def smooth(dataset: [Tuple[str, int]]) -> Callable[[], Optional[str]]:
def smooth(dataset):
dataset_length = len(dataset)
dataset_extra_weights = [ItemWeight(*x) for x in dataset]

def get_next() -> Optional[str]:
def get_next():
if dataset_length == 0:
return None
if dataset_length == 1:
Expand All @@ -20,19 +17,15 @@ def get_next() -> Optional[str]:
extra.effective_weight += 1
if not result or result.current_weight < extra.current_weight:
result = extra
if result:
result.current_weight -= total_weight
return result.key
return None
if not result: # this should be unreachable, but check anyway
raise RuntimeError
result.current_weight -= total_weight
return result.key

return get_next


class ItemWeight:
key: str
weight: int
current_weight: int
effective_weight: int

__slots__ = ('key', 'weight', 'current_weight', 'effective_weight')

Expand Down
12 changes: 12 additions & 0 deletions roundrobin/smooth_rr.pyi
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
43 changes: 26 additions & 17 deletions roundrobin/weighted_rr.py
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
5 changes: 5 additions & 0 deletions roundrobin/weighted_rr.pyi
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: ...
49 changes: 28 additions & 21 deletions setup.py
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()

0 comments on commit 6b9d0d7

Please sign in to comment.