Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
sciyoshi committed Apr 24, 2024
1 parent 53831de commit 0488ed0
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 462 deletions.
53 changes: 21 additions & 32 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,32 @@ name: CI

on:
push:
branches:
- main
pull_request:

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.10", "3.11", "3.12"]
steps:
- uses: actions/checkout@v2

- uses: actions/setup-python@v2
with:
python-version: 3.11

- name: cache poetry install
uses: actions/cache@v3
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
path: ~/.local
key: poetry-0
python-version: ${{ matrix.python-version }}
- run: pip install uv
- run: uv venv
- run: uv pip install --requirement pyproject.toml --all-extras
- run: .venv/bin/pytest

- uses: snok/install-poetry@v1
with:
virtualenvs-create: true
virtualenvs-in-project: true

- name: cache deps
id: cache-deps
uses: actions/cache@v3
with:
path: .venv
key: pydeps-${{ hashFiles('**/poetry.lock') }}

- run: poetry install --no-interaction --no-root
if: steps.cache-deps.outputs.cache-hit != 'true'

- run: poetry install --no-interaction

- run: poetry run pytest tests.py
- run: poetry run black mudder.py tests.py
- run: poetry run flake8 mudder.py
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
- run: pip install uv
- run: uv venv
- run: uv pip install --requirement pyproject.toml --all-extras
- run: .venv/bin/ruff format --check .
- run: .venv/bin/ruff check .
- run: .venv/bin/mypy .
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ From the original readme:
> Generate lexicographically-spaced strings between two strings from
> pre-defined alphabets.
[1]: https://github.com/fasiha/mudderjs
This technique is also known as _fractional indexing_.

[1]: https://github.com/fasiha/mudderjs

## Example

Expand Down
35 changes: 20 additions & 15 deletions mudder.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import math
from decimal import Decimal, ROUND_HALF_UP
from decimal import ROUND_HALF_UP, Decimal
from functools import partial
from itertools import chain, cycle
from operator import add
from typing import Dict, Iterable, List, Optional, Reversible, Tuple, Union

__all__ = [
"SymbolTable",
"decimal",
"alphabet",
"base36",
"base62",
"alphabet",
"decimal",
]


Expand All @@ -30,17 +30,16 @@ def is_prefix_code(strings: Iterable[str]) -> bool:
class SymbolTable:
def __init__(
self, symbols: Iterable[str], symbol_map: Optional[Dict[str, int]] = None
):
) -> None:
symbols = list(symbols)
if not symbol_map:
symbol_map = dict((c, i) for i, c in enumerate(symbols))

symbol_values = set(symbol_map.values())
for i in range(len(symbols)):
if i not in symbol_values:
raise ValueError(
f"{len(symbols)} symbols given but {i} not found in symbol table"
)
msg = f"{len(symbols)} symbols given but {i} not found in symbol table"
raise ValueError(msg)

self.num2sym = symbols
self.sym2num = symbol_map
Expand All @@ -64,10 +63,11 @@ def digits_to_string(self, digits: Iterable[int]) -> str:
def string_to_digits(self, string: Iterable[str]) -> List[int]:
if isinstance(string, str):
if not self.is_prefix_code:
raise ValueError(
msg = (
"Parsing without prefix code is unsupported. "
"Pass in array of stringy symbols?"
)
raise ValueError(msg)
string = (c for c in string if c in self.sym2num)

return [self.sym2num[c] for c in string]
Expand Down Expand Up @@ -146,15 +146,16 @@ def long_div(
return result, remainder


def long_sub_same_len( # noqa: C901
def long_sub_same_len(
a: List[int],
b: List[int],
base: int,
remainder: Optional[Tuple[int, int]] = None,
denominator=0,
denominator: int = 0,
) -> Tuple[List[int], int]:
if len(a) != len(b):
raise ValueError("a and b should have same length")
msg = "a and b should have same length"
raise ValueError(msg)

a = a.copy() # pre-emptively copy
if remainder:
Expand All @@ -169,7 +170,8 @@ def long_sub_same_len( # noqa: C901
ret[i] = a[i] - b[i]
continue
if i == 0:
raise ValueError("Cannot go negative")
msg = "Cannot go negative"
raise ValueError(msg)
do_break = False
# look for a digit to the left to borrow from
for j in reversed(range(i)):
Expand All @@ -189,7 +191,8 @@ def long_sub_same_len( # noqa: C901
break
if do_break:
continue
raise ValueError("Failed to find digit to borrow from")
msg = "Failed to find digit to borrow from"
raise ValueError(msg)
if remainder:
# result, remainder
return ret[:-1], ret[-1]
Expand All @@ -200,7 +203,8 @@ def long_add_same_len(
a: List[int], b: List[int], base: int, remainder: int, denominator: int
) -> Tuple[List[int], bool, int, int]:
if len(a) != len(b):
raise ValueError("a and b should have same length")
msg = "a and b should have same length"
raise ValueError(msg)

carry = remainder >= denominator
res = b.copy()
Expand Down Expand Up @@ -230,7 +234,8 @@ def long_linspace(
elif len(b) < len(a):
b = right_pad(b, len(a))
if a == b:
raise ValueError("Start and end strings are lexicographically inseperable")
msg = "Start and end strings are lexicographically inseparable"
raise ValueError(msg)
a_div, a_div_rem = long_div(a, m, base)
b_div, b_div_rem = long_div(b, m, base)

Expand Down
6 changes: 0 additions & 6 deletions mypy.ini

This file was deleted.

Loading

0 comments on commit 0488ed0

Please sign in to comment.