Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add anon getitem Kappa #38

Merged
merged 7 commits into from
Jul 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
junk_data
.pypi-token
.commit_log
__pycache__/
**/__pycache__/
**/.mypy_cache/
Expand Down
4 changes: 2 additions & 2 deletions docs/coverage/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion docs/coverage/coverage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ muutils\json_serialize\json_serialize.py
muutils\json_serialize\serializable_dataclass.py 178 35 80% 68, 84, 140, 146, 152, 159-161, 170-173, 207-222, 232, 236, 239, 242, 248, 258-259, 263, 272-276, 280, 301, 304, 336, 362, 378-379, 402, 429, 435
muutils\json_serialize\util.py 76 41 46% 11-13, 24, 33, 36, 44-50, 59, 67-74, 85-88, 94-104, 116-119, 123-126
muutils\jsonlines.py 31 31 0% 1-73
muutils\kappa.py 14 0 100%
muutils\logger\__init__.py 5 0 100%
muutils\logger\exception_context.py 12 6 50% 24, 27, 30-43
muutils\logger\headerfuncs.py 19 2 89% 12, 54
Expand Down Expand Up @@ -38,9 +39,10 @@ tests\unit\nbutils\test_conversion.py
tests\unit\test_dictmagic.py 34 0 100%
tests\unit\test_group_equiv.py 12 0 100%
tests\unit\test_import_torch.py 4 0 100%
tests\unit\test_kappa.py 39 0 100%
tests\unit\test_mlutils.py 35 3 91% 31, 35, 43
tests\unit\test_statcounter.py 8 0 100%
tests\unit\test_sysinfo.py 4 0 100%
tests\unit\test_tensor_utils.py 36 0 100%
---------------------------------------------------------------------------------------------------------------
TOTAL 1918 643 66%
TOTAL 1971 643 67%
14 changes: 13 additions & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,25 @@ TESTS_DIR := tests/unit
VERSION := $(shell grep -oP '__version__ = "\K.*?(?=")' $(VERSION_INFO_LOCATION))
LAST_VERSION := $(shell cat $(LAST_VERSION_FILE))
PYPOETRY := poetry run python
# note that the commands at the end:
# 1) format the git log
# 2) replace backticks with single quotes, to avoid funny business
# 3) add a final newline, to make tac happy
# 4) reverse the order of the lines, so that the oldest commit is first
# 5) replace newlines with tabs, to prevent the newlines from being lost
COMMIT_LOG_FILE := .commit_log
COMMIT_LOG_SINCE_LAST_VERSION := $(shell (git log $(LAST_VERSION)..HEAD --pretty=format:"- %s (%h)" | tr '`' "'" ; echo) | tac | tr '\n' '\t')
# 1 2 3 4 5

.PHONY: default
default: help

.PHONY: version
version:
@echo "Current version is $(VERSION), last auto-uploaded version is $(LAST_VERSION)"
@echo "Commit log since last version:"
@echo "$(COMMIT_LOG_SINCE_LAST_VERSION)" | tr '\t' '\n' > $(COMMIT_LOG_FILE)
@cat $(COMMIT_LOG_FILE)
@if [ "$(VERSION)" = "$(LAST_VERSION)" ]; then \
echo "Python package $(VERSION) is the same as last published version $(LAST_VERSION), exiting!"; \
exit 1; \
Expand Down Expand Up @@ -116,7 +128,7 @@ publish: check build verify-git version
echo $(VERSION) > $(LAST_VERSION_FILE); \
git add $(LAST_VERSION_FILE); \
git commit -m "Auto update to $(VERSION)"; \
git tag $(VERSION); \
git tag -a $(VERSION) -F $(COMMIT_LOG_FILE); \
git push origin $(VERSION); \
twine upload dist/* --verbose

Expand Down
2 changes: 1 addition & 1 deletion muutils/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.4.3"
__version__ = "0.4.4"
44 changes: 44 additions & 0 deletions muutils/kappa.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""anonymous getitem class

util for constructing a class which has a getitem method which just calls a function

a `lambda` is an anonymous function: kappa is the letter before lambda in the greek alphabet,
hence the name of this class"""

from typing import Callable, Mapping, TypeVar

_kappa_K = TypeVar("_kappa_K")
_kappa_V = TypeVar("_kappa_V")

# get the docstring of this file
_BASE_DOC: str = (
__doc__
+ """

source function docstring:
==============================\n
"""
)


class Kappa(Mapping[_kappa_K, _kappa_V]):
def __init__(self, func_getitem: Callable[[_kappa_K], _kappa_V]) -> None:
self.func_getitem = func_getitem
self.doc = _BASE_DOC + str(
getattr(
func_getitem, "__doc__", "<no docstring provided for source function>"
)
)

def __getitem__(self, x) -> _kappa_V:
return self.func_getitem(x)

def __iter__(self):
raise NotImplementedError(
"This method is not implemented for Kappa, we don't know the valid inputs"
)

def __len__(self):
raise NotImplementedError(
"This method is not implemented for Kappa, no idea how many valid inputs there are"
)
Loading
Loading