Skip to content

Commit

Permalink
identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddoret committed Dec 8, 2024
1 parent 8e962bd commit b9c8b41
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 46 deletions.
77 changes: 41 additions & 36 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/punctilious/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import _util
from _util import get_yaml_from_package
import _identifiers
from _identifiers import ensure_identifier, ensure_slug, FlexibleSlug, FlexibleUUID, Identifier, Slug, SlugsDictionary
import _presentation
from _presentation import Representation, ensure_representations, ensure_representation, latex_math, Tag, \
TagsPreferences, \
Expand Down
Binary file modified src/punctilious/__pycache__/__init__.cpython-312.pyc
Binary file not shown.
Binary file modified src/punctilious/__pycache__/_foundations.cpython-312.pyc
Binary file not shown.
Binary file modified src/punctilious/__pycache__/_identifiers.cpython-312.pyc
Binary file not shown.
2 changes: 1 addition & 1 deletion src/punctilious/_foundations.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def __init__(self, c, a=None):
:param c: A connector.
:param args: A (possibly empty) collection of arguments.
"""
pass
super().__init__()

def __new__(cls, c, a=None):
c = ensure_connector(c)
Expand Down
26 changes: 17 additions & 9 deletions src/punctilious/_identifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def __eq__(self, other):
return hash(self) == hash(other)

def __hash__(self):
return hash((self.__class__, str(self),))
return hash((self.__class__, super().__str__(),))

def __init__(self, slug: str):
super().__init__()
Expand All @@ -23,10 +23,10 @@ def __new__(cls, slug: str):
return super().__new__(cls, slug)

def __repr__(self):
return f'"{str(super())}" slug'
return f'"{str(super().__str__())}" slug'

def __str__(self):
return str(super())
return str(super().__str__())


class SlugsDictionary(dict):
Expand Down Expand Up @@ -88,11 +88,7 @@ def __init__(self, i: FlexibleUUID, p: FlexibleSlug, s: FlexibleSlug):
:param p: A package slug.
:param s: An object slug.
"""
i = ensure_uuid(i)
p = ensure_slug(p)
s = ensure_slug(s)
phi = (i, p, s,)
super().__init__(phi)
super().__init__()

def __new__(cls, i: FlexibleUUID, p: FlexibleSlug, s: FlexibleSlug):
i = ensure_uuid(i)
Expand All @@ -106,7 +102,7 @@ def __repr__(self):
:return:
"""
return f'{self[0]}.{self[2]} ({self[1]}) identifier'
return f'"{self[1]}.{self[2]}" ({self[0]}) identifier'

def __str__(self):
"""A friendly representation of the identifier.
Expand All @@ -126,3 +122,15 @@ def package_uuid(self) -> uuid.UUID:
@property
def slug(self) -> Slug:
return self[2]


FlexibleIdentifier = typing.Union[Identifier]


def ensure_identifier(o: Identifier | str) -> Identifier:
"""Assure `o` is of type Identifier, or implicitly convert `o` to Identifier, or raise an error if this fails.
"""
if isinstance(o, Identifier):
return o
else:
raise ValueError(f'Invalid identifier {o}')
Binary file not shown.
36 changes: 36 additions & 0 deletions tests/test_identifiers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import pytest
import punctilious as pu
import uuid
from test_shared_library import create_atomic_connector, create_function


class TestSlug:
def test_slug(self):
"""Test of representation with multiple string-constant renderers.
"""

s1 = pu.Slug("foo")
s2 = pu.Slug("bar")
s3 = pu.Slug("foo")
assert s1 != s2
assert s1 == s3

def test_identifier(self):
uuid1 = uuid.uuid4()
uuid2 = uuid.uuid4()
foo = pu.Slug('foo')
bar = pu.Slug('bar')
taz = pu.Slug('taz')
i1 = pu.Identifier(uuid1, foo, bar)
i2 = pu.Identifier(uuid2, foo, bar)
i3 = pu.Identifier(uuid1, foo, bar)
i4 = pu.Identifier(uuid1, foo, taz)
print(hash(i1))
print(hash(i4))
assert i1.package_slug == pu.Slug('foo')
assert i1.package_uuid == uuid1
assert i1.slug == pu.Slug('bar')
assert i4.slug == pu.Slug('taz')
assert i1 != i2
assert i1 == i3
assert i1 != i4

0 comments on commit b9c8b41

Please sign in to comment.