classicist 1.0.3__tar.gz → 1.0.5__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {classicist-1.0.3/source/classicist.egg-info → classicist-1.0.5}/PKG-INFO +191 -5
- {classicist-1.0.3 → classicist-1.0.5}/README.md +190 -4
- {classicist-1.0.3 → classicist-1.0.5}/source/classicist/__init__.py +39 -3
- {classicist-1.0.3 → classicist-1.0.5}/source/classicist/decorators/__init__.py +5 -0
- classicist-1.0.5/source/classicist/decorators/runtimer/__init__.py +165 -0
- {classicist-1.0.3 → classicist-1.0.5}/source/classicist/exceptions/__init__.py +2 -0
- classicist-1.0.5/source/classicist/types/__init__.py +6 -0
- classicist-1.0.5/source/classicist/types/null/__init__.py +70 -0
- classicist-1.0.5/source/classicist/version.txt +1 -0
- {classicist-1.0.3 → classicist-1.0.5/source/classicist.egg-info}/PKG-INFO +191 -5
- {classicist-1.0.3 → classicist-1.0.5}/source/classicist.egg-info/SOURCES.txt +5 -0
- classicist-1.0.5/tests/test_nulltype.py +265 -0
- classicist-1.0.5/tests/test_runtimer.py +88 -0
- classicist-1.0.3/source/classicist/version.txt +0 -1
- {classicist-1.0.3 → classicist-1.0.5}/LICENSE.md +0 -0
- {classicist-1.0.3 → classicist-1.0.5}/pyproject.toml +0 -0
- {classicist-1.0.3 → classicist-1.0.5}/requirements.development.txt +0 -0
- {classicist-1.0.3 → classicist-1.0.5}/requirements.distribution.txt +0 -0
- {classicist-1.0.3 → classicist-1.0.5}/requirements.txt +0 -0
- {classicist-1.0.3 → classicist-1.0.5}/setup.cfg +0 -0
- {classicist-1.0.3 → classicist-1.0.5}/source/classicist/decorators/aliased/__init__.py +0 -0
- {classicist-1.0.3 → classicist-1.0.5}/source/classicist/decorators/annotation/__init__.py +0 -0
- {classicist-1.0.3 → classicist-1.0.5}/source/classicist/decorators/classproperty/__init__.py +0 -0
- {classicist-1.0.3 → classicist-1.0.5}/source/classicist/decorators/deprecated/__init__.py +0 -0
- {classicist-1.0.3 → classicist-1.0.5}/source/classicist/decorators/hybridmethod/__init__.py +0 -0
- {classicist-1.0.3 → classicist-1.0.5}/source/classicist/decorators/nocache/__init__.py +0 -0
- {classicist-1.0.3 → classicist-1.0.5}/source/classicist/exceptions/decorators/__init__.py +0 -0
- {classicist-1.0.3 → classicist-1.0.5}/source/classicist/exceptions/decorators/aliased/__init__.py +0 -0
- {classicist-1.0.3 → classicist-1.0.5}/source/classicist/exceptions/decorators/annotation/__init__.py +0 -0
- {classicist-1.0.3 → classicist-1.0.5}/source/classicist/exceptions/metaclasses/__init__.py +0 -0
- {classicist-1.0.3 → classicist-1.0.5}/source/classicist/exceptions/metaclasses/shadowproof/__init__.py +0 -0
- {classicist-1.0.3 → classicist-1.0.5}/source/classicist/inspector/__init__.py +0 -0
- {classicist-1.0.3 → classicist-1.0.5}/source/classicist/logging/__init__.py +0 -0
- {classicist-1.0.3 → classicist-1.0.5}/source/classicist/metaclasses/__init__.py +0 -0
- {classicist-1.0.3 → classicist-1.0.5}/source/classicist/metaclasses/aliased/__init__.py +0 -0
- {classicist-1.0.3 → classicist-1.0.5}/source/classicist/metaclasses/shadowproof/__init__.py +0 -0
- {classicist-1.0.3 → classicist-1.0.5}/source/classicist.egg-info/dependency_links.txt +0 -0
- {classicist-1.0.3 → classicist-1.0.5}/source/classicist.egg-info/requires.txt +0 -0
- {classicist-1.0.3 → classicist-1.0.5}/source/classicist.egg-info/top_level.txt +0 -0
- {classicist-1.0.3 → classicist-1.0.5}/source/classicist.egg-info/zip-safe +0 -0
- {classicist-1.0.3 → classicist-1.0.5}/tests/test_aliased.py +0 -0
- {classicist-1.0.3 → classicist-1.0.5}/tests/test_annotation.py +0 -0
- {classicist-1.0.3 → classicist-1.0.5}/tests/test_classproperty.py +0 -0
- {classicist-1.0.3 → classicist-1.0.5}/tests/test_deprecated.py +0 -0
- {classicist-1.0.3 → classicist-1.0.5}/tests/test_hybridmethod.py +0 -0
- {classicist-1.0.3 → classicist-1.0.5}/tests/test_shadowproof.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: classicist
|
|
3
|
-
Version: 1.0.
|
|
3
|
+
Version: 1.0.5
|
|
4
4
|
Summary: Classy class decorators for Python.
|
|
5
5
|
Author: Daniel Sissman
|
|
6
6
|
License-Expression: MIT
|
|
@@ -33,16 +33,20 @@ Dynamic: license-file
|
|
|
33
33
|
|
|
34
34
|
# Classicist: Classy Class Decorators & Extensions
|
|
35
35
|
|
|
36
|
-
The Classicist library provides several useful decorators
|
|
36
|
+
The Classicist library provides several useful decorators, functions, classes, and types
|
|
37
|
+
that offer useful behaviours and functionality, and help to fill some of the gaps in the
|
|
38
|
+
current standard library:
|
|
37
39
|
|
|
38
40
|
* `@hybridmethod` – a decorator that allows methods to be used both as class methods and as instance methods;
|
|
39
|
-
* `@classproperty` – a decorator that
|
|
41
|
+
* `@classproperty` – a decorator that allows class methods to be accessed as class properties;
|
|
40
42
|
* `@annotation` – a decorator that can be used to apply arbitrary annotations to code objects;
|
|
41
|
-
* `@deprecated` – a decorator that can be used to mark functions, classes and methods as being deprecated;
|
|
43
|
+
* `@deprecated` – a decorator that can be used to mark functions, classes and methods as being deprecated, with support for adding optional arbitrary annotations;
|
|
42
44
|
* `@alias` – a decorator that can be used to add aliases to classes, methods defined within classes, module-level functions, and nested functions when overriding the aliasing scope;
|
|
43
45
|
* `@nocache` – a decorator that can be used to mark functions and methods as not being suitable for caching;
|
|
46
|
+
* `@runtimer` – a decorator that can be used to gather call run time information for function and method calls;
|
|
44
47
|
* `shadowproof` – a metaclass that can be used to protect subclasses from class-level attributes
|
|
45
|
-
being overwritten (or shadowed) which can otherwise negatively affect class behaviour in some cases
|
|
48
|
+
being overwritten (or shadowed) which can otherwise negatively affect class behaviour in some cases;
|
|
49
|
+
* `Null` – an alternative to `None`, useful when building custom data model classes and libraries, where supporting "null-safe" style access and navigation of the model's nested hierarchy is preferred.
|
|
46
50
|
|
|
47
51
|
The `classicist` library was previously named `hybridmethod` so if a prior version had
|
|
48
52
|
been installed, please update references to the new library name. Installation of the
|
|
@@ -482,6 +486,48 @@ class Test(object):
|
|
|
482
486
|
pass
|
|
483
487
|
```
|
|
484
488
|
|
|
489
|
+
#### Runtimer: Function & Method Call Timing
|
|
490
|
+
|
|
491
|
+
The `@runtimer` decorator can be used to obtain run times for function and method calls,
|
|
492
|
+
including the start and stop `datetime`, the `timedelta` and the duration in seconds.
|
|
493
|
+
|
|
494
|
+
To collect timing information simply import the `runtimer` decorator from the library,
|
|
495
|
+
and apply it to the function, class method or instance method that you wish to time, and
|
|
496
|
+
after the call has been made, you can obtain the run time information from the function
|
|
497
|
+
or method via the `classicist` library's `runtime` helper method, which provides access
|
|
498
|
+
to an instance of the library's `Runtimer` class which is used to track the run time:
|
|
499
|
+
|
|
500
|
+
```python
|
|
501
|
+
from classicist import runtimer, runtime, Runtimer
|
|
502
|
+
from datetime import datetime
|
|
503
|
+
from time import sleep
|
|
504
|
+
|
|
505
|
+
@runtimer
|
|
506
|
+
def function_to_time(value: int) -> int:
|
|
507
|
+
sleep(0.01)
|
|
508
|
+
return value * 100
|
|
509
|
+
|
|
510
|
+
# Obtain a reference to the function's Runtimer (created by the @runtimer decorator)
|
|
511
|
+
# This reference can be obtained before or after a call to the decorated function
|
|
512
|
+
runtimer: Runtimer = runtime(function_to_time)
|
|
513
|
+
assert isinstance(runtimer, Runtimer)
|
|
514
|
+
|
|
515
|
+
# Obtain the time before the function call for illustrative purposes (not needed in use)
|
|
516
|
+
started: datetime = datetime.now()
|
|
517
|
+
|
|
518
|
+
# Call the method to perform its work, and its runtime will be gathered
|
|
519
|
+
assert function_to_time(2) == 200
|
|
520
|
+
|
|
521
|
+
# Obtain the time after the function call for illustrative purposes (not needed in use)
|
|
522
|
+
stopped: datetime = datetime.now()
|
|
523
|
+
|
|
524
|
+
# Use the gathered runtime information as needed
|
|
525
|
+
assert runtimer.started > started
|
|
526
|
+
assert runtimer.duration >= 0.01
|
|
527
|
+
assert runtimer.timedelta.total_seconds() >= 0.01
|
|
528
|
+
assert runtimer.stopped < stopped
|
|
529
|
+
```
|
|
530
|
+
|
|
485
531
|
#### ShadowProof: Attribute Shadowing Protection Metaclass
|
|
486
532
|
|
|
487
533
|
The `shadowproof` metaclass can be used to protect classes and subclasses from attribute
|
|
@@ -511,6 +557,146 @@ except AttributeShadowingError as exception:
|
|
|
511
557
|
pass
|
|
512
558
|
```
|
|
513
559
|
|
|
560
|
+
#### NullType: Null-Safe Style Access for Data Models and Nested Class Hierarchies
|
|
561
|
+
|
|
562
|
+
The `NullType` class supports the creation of a `Null` singleton instance that offers
|
|
563
|
+
support for safely chaining nested attribute accesses without raising exceptions for
|
|
564
|
+
attributes that have no inherent value.
|
|
565
|
+
|
|
566
|
+
As Python currently lacks a null-aware navigation operator, such as `?.`, unlike many
|
|
567
|
+
other dynamic languages, for safely navigating nested object hierarchies which may
|
|
568
|
+
contain null attributes, the library offers the `NullType` and `Null` singleton as a
|
|
569
|
+
potential option to support this need in the interim. Consistent use of the `Null`
|
|
570
|
+
singleton in place of the standard `None` singleton, in relevant scenarios, such as
|
|
571
|
+
within a custom data model library, can allow for more expressive and clearer code that
|
|
572
|
+
does not require endless checks for intermediary or nested attribute existence.
|
|
573
|
+
|
|
574
|
+
However, there are some caveats to the use of the `NullType` and `Null` singleton as
|
|
575
|
+
these are not built-in features of the language, and Python does not offer support for
|
|
576
|
+
the creation of custom operators nor overriding the `is` operator for identity checking
|
|
577
|
+
which limits some of the use cases in which the `Null` singleton can be used.
|
|
578
|
+
|
|
579
|
+
With knowledge of these caveats and in the right scenarios, the `Null` singleton can
|
|
580
|
+
offer a good way to achieve clearer and more expressive code while navigating nested
|
|
581
|
+
object hierarchies without the clutter of nested attribute existence checks.
|
|
582
|
+
|
|
583
|
+
```python
|
|
584
|
+
from __future__ import annotations
|
|
585
|
+
|
|
586
|
+
from classicist import Null
|
|
587
|
+
|
|
588
|
+
data: dict = {
|
|
589
|
+
"id": 1,
|
|
590
|
+
"name": "A",
|
|
591
|
+
"related": {
|
|
592
|
+
"id": 2,
|
|
593
|
+
"name": "B",
|
|
594
|
+
"related": {
|
|
595
|
+
"id": 3,
|
|
596
|
+
"name": "C",
|
|
597
|
+
},
|
|
598
|
+
},
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
class Model(object):
|
|
602
|
+
"""Sample Model class with some properties that reference nested Model instances."""
|
|
603
|
+
|
|
604
|
+
def __init__(self, data: dict):
|
|
605
|
+
if not isinstance(data, dict):
|
|
606
|
+
raise TypeError("The 'data' argument must reference a valid data dictionary!")
|
|
607
|
+
|
|
608
|
+
if not ("id" in data and isinstance(data["id"], int)):
|
|
609
|
+
raise ValueError("The 'data' must contain an 'id' key with an integer value!")
|
|
610
|
+
|
|
611
|
+
if not ("name" in data and isinstance(data["name"], str)):
|
|
612
|
+
raise ValueError("The 'data' must contain an 'name' key with an string value!")
|
|
613
|
+
|
|
614
|
+
self.data = data
|
|
615
|
+
|
|
616
|
+
@property
|
|
617
|
+
def id(self) -> int:
|
|
618
|
+
return self.data["id"]
|
|
619
|
+
|
|
620
|
+
@property
|
|
621
|
+
def name(self) -> str:
|
|
622
|
+
return self.data["name"]
|
|
623
|
+
|
|
624
|
+
@property
|
|
625
|
+
def related(self) -> Model | Null:
|
|
626
|
+
if data := self.data.get("related"):
|
|
627
|
+
return Model(data=data)
|
|
628
|
+
else:
|
|
629
|
+
return Null
|
|
630
|
+
|
|
631
|
+
@property
|
|
632
|
+
def relates(self) -> Model | Null:
|
|
633
|
+
if data := self.data.get("relates"):
|
|
634
|
+
return Model(data=data)
|
|
635
|
+
else:
|
|
636
|
+
return Null
|
|
637
|
+
|
|
638
|
+
# Create an instance of the sample Model data class
|
|
639
|
+
model = Model(data=data)
|
|
640
|
+
|
|
641
|
+
# Check that the expected data attributes are available
|
|
642
|
+
assert model.id == 1
|
|
643
|
+
assert model.name == "A"
|
|
644
|
+
|
|
645
|
+
# The model.related property references A/1 in the data above, so these properties exist
|
|
646
|
+
assert model.related
|
|
647
|
+
assert model.related.id
|
|
648
|
+
assert model.related.name
|
|
649
|
+
|
|
650
|
+
# Ensure the nested property values are as expected
|
|
651
|
+
assert model.related.id == 2
|
|
652
|
+
assert model.related.name == "B"
|
|
653
|
+
|
|
654
|
+
# Note that model.relates had no corresponding data, so the Model returns `Null` which
|
|
655
|
+
# still allows for nested attribute access, such as to `.id` and `.name` without raising
|
|
656
|
+
# any exceptions; the `Null` singleton also allows for `bool` comparison as shown below:
|
|
657
|
+
assert not model.relates
|
|
658
|
+
assert not model.relates.id
|
|
659
|
+
assert not model.relates.name
|
|
660
|
+
|
|
661
|
+
# There is no limit to the levels of nesting that `NullType` and the `Null` singleton
|
|
662
|
+
# can support, so long as a custom data model or library consistently returns `Null` for
|
|
663
|
+
# cases where the "null-safe" navigation is desired:
|
|
664
|
+
|
|
665
|
+
# model.related.related references 3/C in the data above, so these properties exist
|
|
666
|
+
assert model.related.related.id
|
|
667
|
+
assert model.related.related.name
|
|
668
|
+
|
|
669
|
+
# model.related.relates was not specified in the data above so the Model returns `Null`
|
|
670
|
+
assert not model.related.relates.id
|
|
671
|
+
assert not model.related.relates.name
|
|
672
|
+
|
|
673
|
+
# Ensure the nested property values are as expected
|
|
674
|
+
assert model.related.related.id == 3
|
|
675
|
+
assert model.related.related.name == "C"
|
|
676
|
+
|
|
677
|
+
# These features make it easy to write clearer more expressive code without boilerplate
|
|
678
|
+
# code to check for the availability of nested attributes or entities:
|
|
679
|
+
if isinstance(name := model.related.name, str):
|
|
680
|
+
print("model.related.name => %s" % (name))
|
|
681
|
+
|
|
682
|
+
# No exception is raised here even though model.relates is effectively "null":
|
|
683
|
+
if isinstance(name := model.relates.name, str):
|
|
684
|
+
print("model.relates.name => %s" % (name))
|
|
685
|
+
|
|
686
|
+
# However, there are some caveats as noted with `NullType` and the `Null` singleton as
|
|
687
|
+
# these are a third-party solution so we can only go so far in supplementing null-safe
|
|
688
|
+
# operator behaviour in the language; for example, we cannot perform identity checks to
|
|
689
|
+
# boolean values, True or False, or the actual None singleton value:
|
|
690
|
+
assert not model.relates is True # Notice the `assert not` as `assert` would fail here
|
|
691
|
+
assert not model.relates is False # Notice the `assert not` as `assert` would fail here
|
|
692
|
+
|
|
693
|
+
# Furthermore, we cannot use `None` identity comparison either:
|
|
694
|
+
assert not model.relates is None # Notice the `assert not` as `assert` would fail here
|
|
695
|
+
|
|
696
|
+
# We can however perform an identity check against the `Null` singleton if needed:
|
|
697
|
+
assert model.relates is Null
|
|
698
|
+
```
|
|
699
|
+
|
|
514
700
|
### Unit Tests
|
|
515
701
|
|
|
516
702
|
The Classicist library includes a suite of comprehensive unit tests which ensure that
|
|
@@ -1,15 +1,19 @@
|
|
|
1
1
|
# Classicist: Classy Class Decorators & Extensions
|
|
2
2
|
|
|
3
|
-
The Classicist library provides several useful decorators
|
|
3
|
+
The Classicist library provides several useful decorators, functions, classes, and types
|
|
4
|
+
that offer useful behaviours and functionality, and help to fill some of the gaps in the
|
|
5
|
+
current standard library:
|
|
4
6
|
|
|
5
7
|
* `@hybridmethod` – a decorator that allows methods to be used both as class methods and as instance methods;
|
|
6
|
-
* `@classproperty` – a decorator that
|
|
8
|
+
* `@classproperty` – a decorator that allows class methods to be accessed as class properties;
|
|
7
9
|
* `@annotation` – a decorator that can be used to apply arbitrary annotations to code objects;
|
|
8
|
-
* `@deprecated` – a decorator that can be used to mark functions, classes and methods as being deprecated;
|
|
10
|
+
* `@deprecated` – a decorator that can be used to mark functions, classes and methods as being deprecated, with support for adding optional arbitrary annotations;
|
|
9
11
|
* `@alias` – a decorator that can be used to add aliases to classes, methods defined within classes, module-level functions, and nested functions when overriding the aliasing scope;
|
|
10
12
|
* `@nocache` – a decorator that can be used to mark functions and methods as not being suitable for caching;
|
|
13
|
+
* `@runtimer` – a decorator that can be used to gather call run time information for function and method calls;
|
|
11
14
|
* `shadowproof` – a metaclass that can be used to protect subclasses from class-level attributes
|
|
12
|
-
being overwritten (or shadowed) which can otherwise negatively affect class behaviour in some cases
|
|
15
|
+
being overwritten (or shadowed) which can otherwise negatively affect class behaviour in some cases;
|
|
16
|
+
* `Null` – an alternative to `None`, useful when building custom data model classes and libraries, where supporting "null-safe" style access and navigation of the model's nested hierarchy is preferred.
|
|
13
17
|
|
|
14
18
|
The `classicist` library was previously named `hybridmethod` so if a prior version had
|
|
15
19
|
been installed, please update references to the new library name. Installation of the
|
|
@@ -449,6 +453,48 @@ class Test(object):
|
|
|
449
453
|
pass
|
|
450
454
|
```
|
|
451
455
|
|
|
456
|
+
#### Runtimer: Function & Method Call Timing
|
|
457
|
+
|
|
458
|
+
The `@runtimer` decorator can be used to obtain run times for function and method calls,
|
|
459
|
+
including the start and stop `datetime`, the `timedelta` and the duration in seconds.
|
|
460
|
+
|
|
461
|
+
To collect timing information simply import the `runtimer` decorator from the library,
|
|
462
|
+
and apply it to the function, class method or instance method that you wish to time, and
|
|
463
|
+
after the call has been made, you can obtain the run time information from the function
|
|
464
|
+
or method via the `classicist` library's `runtime` helper method, which provides access
|
|
465
|
+
to an instance of the library's `Runtimer` class which is used to track the run time:
|
|
466
|
+
|
|
467
|
+
```python
|
|
468
|
+
from classicist import runtimer, runtime, Runtimer
|
|
469
|
+
from datetime import datetime
|
|
470
|
+
from time import sleep
|
|
471
|
+
|
|
472
|
+
@runtimer
|
|
473
|
+
def function_to_time(value: int) -> int:
|
|
474
|
+
sleep(0.01)
|
|
475
|
+
return value * 100
|
|
476
|
+
|
|
477
|
+
# Obtain a reference to the function's Runtimer (created by the @runtimer decorator)
|
|
478
|
+
# This reference can be obtained before or after a call to the decorated function
|
|
479
|
+
runtimer: Runtimer = runtime(function_to_time)
|
|
480
|
+
assert isinstance(runtimer, Runtimer)
|
|
481
|
+
|
|
482
|
+
# Obtain the time before the function call for illustrative purposes (not needed in use)
|
|
483
|
+
started: datetime = datetime.now()
|
|
484
|
+
|
|
485
|
+
# Call the method to perform its work, and its runtime will be gathered
|
|
486
|
+
assert function_to_time(2) == 200
|
|
487
|
+
|
|
488
|
+
# Obtain the time after the function call for illustrative purposes (not needed in use)
|
|
489
|
+
stopped: datetime = datetime.now()
|
|
490
|
+
|
|
491
|
+
# Use the gathered runtime information as needed
|
|
492
|
+
assert runtimer.started > started
|
|
493
|
+
assert runtimer.duration >= 0.01
|
|
494
|
+
assert runtimer.timedelta.total_seconds() >= 0.01
|
|
495
|
+
assert runtimer.stopped < stopped
|
|
496
|
+
```
|
|
497
|
+
|
|
452
498
|
#### ShadowProof: Attribute Shadowing Protection Metaclass
|
|
453
499
|
|
|
454
500
|
The `shadowproof` metaclass can be used to protect classes and subclasses from attribute
|
|
@@ -478,6 +524,146 @@ except AttributeShadowingError as exception:
|
|
|
478
524
|
pass
|
|
479
525
|
```
|
|
480
526
|
|
|
527
|
+
#### NullType: Null-Safe Style Access for Data Models and Nested Class Hierarchies
|
|
528
|
+
|
|
529
|
+
The `NullType` class supports the creation of a `Null` singleton instance that offers
|
|
530
|
+
support for safely chaining nested attribute accesses without raising exceptions for
|
|
531
|
+
attributes that have no inherent value.
|
|
532
|
+
|
|
533
|
+
As Python currently lacks a null-aware navigation operator, such as `?.`, unlike many
|
|
534
|
+
other dynamic languages, for safely navigating nested object hierarchies which may
|
|
535
|
+
contain null attributes, the library offers the `NullType` and `Null` singleton as a
|
|
536
|
+
potential option to support this need in the interim. Consistent use of the `Null`
|
|
537
|
+
singleton in place of the standard `None` singleton, in relevant scenarios, such as
|
|
538
|
+
within a custom data model library, can allow for more expressive and clearer code that
|
|
539
|
+
does not require endless checks for intermediary or nested attribute existence.
|
|
540
|
+
|
|
541
|
+
However, there are some caveats to the use of the `NullType` and `Null` singleton as
|
|
542
|
+
these are not built-in features of the language, and Python does not offer support for
|
|
543
|
+
the creation of custom operators nor overriding the `is` operator for identity checking
|
|
544
|
+
which limits some of the use cases in which the `Null` singleton can be used.
|
|
545
|
+
|
|
546
|
+
With knowledge of these caveats and in the right scenarios, the `Null` singleton can
|
|
547
|
+
offer a good way to achieve clearer and more expressive code while navigating nested
|
|
548
|
+
object hierarchies without the clutter of nested attribute existence checks.
|
|
549
|
+
|
|
550
|
+
```python
|
|
551
|
+
from __future__ import annotations
|
|
552
|
+
|
|
553
|
+
from classicist import Null
|
|
554
|
+
|
|
555
|
+
data: dict = {
|
|
556
|
+
"id": 1,
|
|
557
|
+
"name": "A",
|
|
558
|
+
"related": {
|
|
559
|
+
"id": 2,
|
|
560
|
+
"name": "B",
|
|
561
|
+
"related": {
|
|
562
|
+
"id": 3,
|
|
563
|
+
"name": "C",
|
|
564
|
+
},
|
|
565
|
+
},
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
class Model(object):
|
|
569
|
+
"""Sample Model class with some properties that reference nested Model instances."""
|
|
570
|
+
|
|
571
|
+
def __init__(self, data: dict):
|
|
572
|
+
if not isinstance(data, dict):
|
|
573
|
+
raise TypeError("The 'data' argument must reference a valid data dictionary!")
|
|
574
|
+
|
|
575
|
+
if not ("id" in data and isinstance(data["id"], int)):
|
|
576
|
+
raise ValueError("The 'data' must contain an 'id' key with an integer value!")
|
|
577
|
+
|
|
578
|
+
if not ("name" in data and isinstance(data["name"], str)):
|
|
579
|
+
raise ValueError("The 'data' must contain an 'name' key with an string value!")
|
|
580
|
+
|
|
581
|
+
self.data = data
|
|
582
|
+
|
|
583
|
+
@property
|
|
584
|
+
def id(self) -> int:
|
|
585
|
+
return self.data["id"]
|
|
586
|
+
|
|
587
|
+
@property
|
|
588
|
+
def name(self) -> str:
|
|
589
|
+
return self.data["name"]
|
|
590
|
+
|
|
591
|
+
@property
|
|
592
|
+
def related(self) -> Model | Null:
|
|
593
|
+
if data := self.data.get("related"):
|
|
594
|
+
return Model(data=data)
|
|
595
|
+
else:
|
|
596
|
+
return Null
|
|
597
|
+
|
|
598
|
+
@property
|
|
599
|
+
def relates(self) -> Model | Null:
|
|
600
|
+
if data := self.data.get("relates"):
|
|
601
|
+
return Model(data=data)
|
|
602
|
+
else:
|
|
603
|
+
return Null
|
|
604
|
+
|
|
605
|
+
# Create an instance of the sample Model data class
|
|
606
|
+
model = Model(data=data)
|
|
607
|
+
|
|
608
|
+
# Check that the expected data attributes are available
|
|
609
|
+
assert model.id == 1
|
|
610
|
+
assert model.name == "A"
|
|
611
|
+
|
|
612
|
+
# The model.related property references A/1 in the data above, so these properties exist
|
|
613
|
+
assert model.related
|
|
614
|
+
assert model.related.id
|
|
615
|
+
assert model.related.name
|
|
616
|
+
|
|
617
|
+
# Ensure the nested property values are as expected
|
|
618
|
+
assert model.related.id == 2
|
|
619
|
+
assert model.related.name == "B"
|
|
620
|
+
|
|
621
|
+
# Note that model.relates had no corresponding data, so the Model returns `Null` which
|
|
622
|
+
# still allows for nested attribute access, such as to `.id` and `.name` without raising
|
|
623
|
+
# any exceptions; the `Null` singleton also allows for `bool` comparison as shown below:
|
|
624
|
+
assert not model.relates
|
|
625
|
+
assert not model.relates.id
|
|
626
|
+
assert not model.relates.name
|
|
627
|
+
|
|
628
|
+
# There is no limit to the levels of nesting that `NullType` and the `Null` singleton
|
|
629
|
+
# can support, so long as a custom data model or library consistently returns `Null` for
|
|
630
|
+
# cases where the "null-safe" navigation is desired:
|
|
631
|
+
|
|
632
|
+
# model.related.related references 3/C in the data above, so these properties exist
|
|
633
|
+
assert model.related.related.id
|
|
634
|
+
assert model.related.related.name
|
|
635
|
+
|
|
636
|
+
# model.related.relates was not specified in the data above so the Model returns `Null`
|
|
637
|
+
assert not model.related.relates.id
|
|
638
|
+
assert not model.related.relates.name
|
|
639
|
+
|
|
640
|
+
# Ensure the nested property values are as expected
|
|
641
|
+
assert model.related.related.id == 3
|
|
642
|
+
assert model.related.related.name == "C"
|
|
643
|
+
|
|
644
|
+
# These features make it easy to write clearer more expressive code without boilerplate
|
|
645
|
+
# code to check for the availability of nested attributes or entities:
|
|
646
|
+
if isinstance(name := model.related.name, str):
|
|
647
|
+
print("model.related.name => %s" % (name))
|
|
648
|
+
|
|
649
|
+
# No exception is raised here even though model.relates is effectively "null":
|
|
650
|
+
if isinstance(name := model.relates.name, str):
|
|
651
|
+
print("model.relates.name => %s" % (name))
|
|
652
|
+
|
|
653
|
+
# However, there are some caveats as noted with `NullType` and the `Null` singleton as
|
|
654
|
+
# these are a third-party solution so we can only go so far in supplementing null-safe
|
|
655
|
+
# operator behaviour in the language; for example, we cannot perform identity checks to
|
|
656
|
+
# boolean values, True or False, or the actual None singleton value:
|
|
657
|
+
assert not model.relates is True # Notice the `assert not` as `assert` would fail here
|
|
658
|
+
assert not model.relates is False # Notice the `assert not` as `assert` would fail here
|
|
659
|
+
|
|
660
|
+
# Furthermore, we cannot use `None` identity comparison either:
|
|
661
|
+
assert not model.relates is None # Notice the `assert not` as `assert` would fail here
|
|
662
|
+
|
|
663
|
+
# We can however perform an identity check against the `Null` singleton if needed:
|
|
664
|
+
assert model.relates is Null
|
|
665
|
+
```
|
|
666
|
+
|
|
481
667
|
### Unit Tests
|
|
482
668
|
|
|
483
669
|
The Classicist library includes a suite of comprehensive unit tests which ensure that
|
|
@@ -1,20 +1,39 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Decorators
|
|
2
2
|
from classicist.decorators import (
|
|
3
|
+
# @alias decorator
|
|
3
4
|
alias,
|
|
4
|
-
|
|
5
|
+
# @annotation decorator
|
|
5
6
|
annotation,
|
|
6
|
-
|
|
7
|
+
# @classproperty decorator
|
|
7
8
|
classproperty,
|
|
9
|
+
# @deprecated decorator
|
|
8
10
|
deprecated,
|
|
11
|
+
# @hybridmethod decorator
|
|
9
12
|
hybridmethod,
|
|
13
|
+
# @nocache decorator
|
|
10
14
|
nocache,
|
|
15
|
+
# @runtimer decorator
|
|
16
|
+
runtimer,
|
|
11
17
|
)
|
|
12
18
|
|
|
13
19
|
# Decorator Helper Methods
|
|
14
20
|
from classicist.decorators import (
|
|
21
|
+
# @alias decorator helper methods
|
|
15
22
|
is_aliased,
|
|
16
23
|
aliases,
|
|
24
|
+
# @annotation decorator helper methods
|
|
25
|
+
annotate,
|
|
26
|
+
annotations,
|
|
27
|
+
# @deprecated decorator helper methods
|
|
17
28
|
is_deprecated,
|
|
29
|
+
# @runtimer decorator helper methods
|
|
30
|
+
runtime,
|
|
31
|
+
has_runtimer,
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
# Decorator Related Classes
|
|
35
|
+
from classicist.decorators import (
|
|
36
|
+
Runtimer,
|
|
18
37
|
)
|
|
19
38
|
|
|
20
39
|
# Meta Classes
|
|
@@ -25,9 +44,16 @@ from classicist.metaclasses import (
|
|
|
25
44
|
|
|
26
45
|
# Exception Classes
|
|
27
46
|
from classicist.exceptions import (
|
|
47
|
+
AliasError,
|
|
48
|
+
AnnotationError,
|
|
28
49
|
AttributeShadowingError,
|
|
29
50
|
)
|
|
30
51
|
|
|
52
|
+
from classicist.types import (
|
|
53
|
+
NullType,
|
|
54
|
+
Null,
|
|
55
|
+
)
|
|
56
|
+
|
|
31
57
|
__all__ = [
|
|
32
58
|
# Decorators
|
|
33
59
|
"alias",
|
|
@@ -38,13 +64,23 @@ __all__ = [
|
|
|
38
64
|
"deprecated",
|
|
39
65
|
"hybridmethod",
|
|
40
66
|
"nocache",
|
|
67
|
+
"runtimer",
|
|
41
68
|
# Decorator Helper Methods
|
|
42
69
|
"is_aliased",
|
|
43
70
|
"aliases",
|
|
44
71
|
"is_deprecated",
|
|
72
|
+
"runtime",
|
|
73
|
+
"has_runtimer",
|
|
74
|
+
# Decorator Related Classes
|
|
75
|
+
"Runtimer",
|
|
45
76
|
# Meta Classes
|
|
46
77
|
"aliased",
|
|
47
78
|
"shadowproof",
|
|
48
79
|
# Exception Classes
|
|
80
|
+
"AliasError",
|
|
81
|
+
"AnnotationError",
|
|
49
82
|
"AttributeShadowingError",
|
|
83
|
+
# Types
|
|
84
|
+
"NullType",
|
|
85
|
+
"Null",
|
|
50
86
|
]
|
|
@@ -4,6 +4,7 @@ from classicist.decorators.classproperty import classproperty
|
|
|
4
4
|
from classicist.decorators.deprecated import deprecated, is_deprecated
|
|
5
5
|
from classicist.decorators.hybridmethod import hybridmethod
|
|
6
6
|
from classicist.decorators.nocache import nocache
|
|
7
|
+
from classicist.decorators.runtimer import Runtimer, runtimer, runtime, has_runtimer
|
|
7
8
|
|
|
8
9
|
__all__ = [
|
|
9
10
|
"alias",
|
|
@@ -17,4 +18,8 @@ __all__ = [
|
|
|
17
18
|
"is_deprecated",
|
|
18
19
|
"hybridmethod",
|
|
19
20
|
"nocache",
|
|
21
|
+
"Runtimer",
|
|
22
|
+
"runtimer",
|
|
23
|
+
"runtime",
|
|
24
|
+
"has_runtimer",
|
|
20
25
|
]
|