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 64e4b12
Show file tree
Hide file tree
Showing 11 changed files with 294 additions and 469 deletions.
3 changes: 0 additions & 3 deletions .flake8

This file was deleted.

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 .
25 changes: 25 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Release

on:
release:
types:
- created

jobs:
build:
runs-on: ubuntu-latest
permissions:
# IMPORTANT: this permission is mandatory for trusted publishing
id-token: write
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.12"
- run: pip install uv
- run: uv venv
- run: uv pip install --requirement pyproject.toml
- run: uv pip install setuptools setuptools-scm wheel build
- run: .venv/bin/python -m build --no-isolation
- name: Publish package distributions to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
164 changes: 160 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,160 @@
.mypy_cache
**/__pycache__
*.egg-info
dist
# 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/
share/python-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/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# 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/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
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 → mudder/__init__.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
Empty file added mudder/py.typed
Empty file.
6 changes: 0 additions & 6 deletions mypy.ini

This file was deleted.

Loading

0 comments on commit 64e4b12

Please sign in to comment.