1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
|
.. include:: shields.inc
.. raw:: latex
\part{Introduction}
.. only:: html
| |SHIELD:svg:pyTooling-github| |SHIELD:svg:pyTooling-src-license| |SHIELD:svg:pyTooling-ghp-doc| |SHIELD:svg:pyTooling-doc-license|
| |SHIELD:svg:pyTooling-pypi-tag| |SHIELD:svg:pyTooling-pypi-status| |SHIELD:svg:pyTooling-pypi-python|
| |SHIELD:svg:pyTooling-gha-test| |SHIELD:svg:pyTooling-lib-status| |SHIELD:svg:pyTooling-codacy-quality| |SHIELD:svg:pyTooling-codacy-coverage| |SHIELD:svg:pyTooling-codecov-coverage|
.. Disabled shields: |SHIELD:svg:pyTooling-gitter| |SHIELD:svg:pyTooling-lib-dep| |SHIELD:svg:pyTooling-lib-rank|
.. only:: latex
|SHIELD:png:pyTooling-github| |SHIELD:png:pyTooling-src-license| |SHIELD:png:pyTooling-ghp-doc| |SHIELD:png:pyTooling-doc-license|
|SHIELD:png:pyTooling-pypi-tag| |SHIELD:png:pyTooling-pypi-status| |SHIELD:png:pyTooling-pypi-python|
|SHIELD:png:pyTooling-gha-test| |SHIELD:png:pyTooling-lib-status| |SHIELD:png:pyTooling-codacy-quality| |SHIELD:png:pyTooling-codacy-coverage| |SHIELD:png:pyTooling-codecov-coverage|
.. Disabled shields: |SHIELD:svg:pyTooling-gitter| |SHIELD:png:pyTooling-lib-dep| |SHIELD:png:pyTooling-lib-rank|
--------------------------------------------------------------------------------
pyTooling Documentation
#######################
**pyTooling** is a powerful collection of arbitrary and useful (abstract) data models, lacking classes, decorators, a
new performance boosting meta-class and enhanced exceptions. It also provides lots of helper functions e.g. to ease the
handling of package descriptions or to unify multiple existing APIs into a single API.
It's useful ‒ if not even essential ‒ for **any** Python-based project independent if it's a library, framework, CLI
tool or just a "script".
In addition, pyTooling provides a collection of `CI job templates for GitHub Actions <https://github.com/pyTooling/Actions>`__.
This drastically simplifies GHA-based CI pipelines for Python projects.
Package Details
***************
The following descriptions and code examples give peak onto pyTooling's highlights. But be ensured, there is more to
explore, which can't be highlighted on the main landing page.
Attributes
==========
.. grid:: 2
.. grid-item::
:columns: 6
The :ref:`pyTooling.Attributes <ATTR>` module offers the base implementation of
`.NET-like attributes <https://learn.microsoft.com/en-us/dotnet/csharp/advanced-topics/reflection-and-attributes/>`__
realized with :term:`Python decorators <decorator>`. The annotated and declarative data is stored as instances of
:ref:`Attribute <ATTR/Predefined/Attribute>` classes in an additional ``__pyattr__`` field per class, method or
function.
The annotation syntax (decorator syntax) allows users to attach any structured data to classes, methods or
functions. In many cases, a user will derive a custom attribute from :ref:`Attribute <ATTR/Predefined/Attribute>`
and override the ``__init__`` method, so user-defined parameters can be accepted when the attribute is constructed.
Later, classes, methods or functions can be searched for by querying the attribute class for attribute instance
usage locations (see example to the right). Another option for class and method attributes is declaring a classes
using pyTooling's :ref:`META/ExtendedType` meta-class. Here the class itself offers helper methods for discovering
annotated methods.
A :ref:`SimpleAttribute <ATTR/Predefined/SimpleAttribute>` class is offered accepting any positional and keyword
parameters. In a more advanced use case, users are encouraged to derive their own attribute class hierarchy from
:ref:`Attribute <ATTR/Predefined/Attribute>`.
.. grid-item::
:columns: 6
.. tab-set::
.. tab-item:: Function Attributes
.. code-block:: Python
from pyTooling.Attributes import Attribute
class Command(Attribute):
def __init__(self, cmd: str, help: str = "") -> None:
pass
class Flag(Attribute):
def __init__(self, param: str, short: str = None, long: str = None, help: str = "") -> None:
pass
@Command(cmd="version", help="Print version information.")
@Flag(param="verbose", short="-v", long="--verbose", help="Default handler.")
def Handler(self, args) -> None:
pass
for function in Command.GetFunctions():
pass
.. tab-item:: Method Attributes
.. code-block:: Python
from pyTooling.Attributes import Attribute
from pyTooling.MetaClasses import ExtendedType
class TestCase(Attribute):
def __init__(self, name: str) -> None:
pass
class Program(metaclass=ExtendedType):
@TestCase(name="Handler routine")
def Handler(self, args) -> None:
pass
prog = Program()
for method, attributes in prog.GetMethodsWithAttributes(predicate=TestCase):
pass
.. tab-item:: Class Attributes
.. code-block:: Python
from pyTooling.Attributes import Attribute
from pyTooling.MetaClasses import ExtendedType
class TestSuite(Attribute):
def __init__(self, name: str) -> None:
pass
@TestSuite(name="Command line interface tests")
class Program(metaclass=ExtendedType):
def Handler(self, args) -> None:
pass
prog = Program()
for testsuite in TestSuite.GetClasses():
pass
ArgParse
--------
.. grid:: 2
.. grid-item::
:columns: 6
Defining commands, arguments or flags for a command line argument parser like :mod:`argparse` is done imperatively.
This means code executed in-order defines how the parser will accept inputs. Then more user-defined code is needed
to dispatch the collected and type-converted arguments to handler routines. See an example to the right as
"Traditional argparse".
In contrast, :ref:`pyTooling.Attributes.ArgParse <ATTR/ArgParse>` allows the definition of :ref:`commands <ATTR/ArgParse/Commands>`,
:ref:`arguments <ATTR/ArgParse/Arguments>` or :ref:`flags <ATTR/ArgParse/Flags>` as declarative code attached to
handler routines using pyTooling's attributes. This allow a cleaner and more readable coding style. Also
maintainability is improved, as arguments are defined using clear attribute names attached to the matching handler
routine. Thus parser and handler code is not separated.
If the command line interface uses many commands, handlers and their arguments can be spread across
:ref:`mixin classes <ATTR/ArgParse/MixIn>`. Later, the whole CLI is assembled by using multiple inheritance. In
case handlers use shared argument sets, arguments can be :ref:`grouped <ATTR/ArgParse/Grouping>` and shared by
defining grouping attributes.
.. grid-item::
:columns: 6
.. tab-set::
.. tab-item:: Traditional ArgParse
.. code-block:: Python
class Program:
def __init__(self) -> None:
mainParser = argparse.ArgumentParser()
mainParser.set_defaults(func=self.HandleDefault)
mainParser.add_argument("-v", "--verbose")
subParsers = mainParser.add_subparsers()
newUserParser = subParsers.add_parser("new-user", help="Add a new user.")
newUserParser.add_argument(dest="username", metaName="username", help="Name of the new user.")
newUserParser.add_argument("--quota", dest="quota", help="Max usable disk space.")
newUserParser.set_defaults(func=self.NewUserHandler)
deleteUserParser = subParsers.add_parser("delete-user", help="Delete a user.")
deleteUserParser.add_argument(dest="username", metaName="username", help="Name of the user.")
deleteUserParser.add_argument("-f", "--force", dest="force", help="Ignore internal checks.")
deleteUserParser.set_defaults(func=self.DeleteUserHandler)
listUserParser = subParsers.add_parser("list-user", help="List all users.")
listUserParser.set_defaults(func=self.ListUserHandler)
def HandleDefault(self, args) -> None:
pass
def NewUserHandler(self, args) -> None:
pass
def DeleteUserHandler(self, args) -> None:
pass
def ListUserHandler(self, args) -> None:
pass
.. tab-item:: pyTooling.Attributes.ArgParse
:selected:
.. code-block:: Python
class Program:
@DefaultHandler()
@FlagArgument(short="-v", long="--verbose", dest="verbose", help="Show verbose messages.")
def HandleDefault(self, args) -> None:
pass
@CommandHandler("new-user", help="Add a new user.")
@StringArgument(dest="username", metaName="username", help="Name of the new user.")
@LongValuedFlag("--quota", dest="quota", help="Max usable disk space.")
def NewUserHandler(self, args) -> None:
pass
@CommandHandler("delete-user", help="Delete a user.")
@StringArgument(dest="username", metaName="username", help="Name of the user.")
@FlagArgument(short="-f", long="--force", dest="force", help="Ignore internal checks.")
def DeleteUserHandler(self, args) -> None:
pass
@CommandHandler("list-user", help="List all users.")
def ListUserHandler(self, args) -> None:
pass
CLI Abstraction
===============
.. grid:: 2
.. grid-item::
:columns: 6
:ref:`pyTooling.CLIAbstraction <CLIABS>` offers an abstraction layer for command line programs, so they can be
used easily in Python. There is no need for manually assembling parameter lists or considering the order of
parameters. All parameters like ``-v`` or ``--value=42`` are described using nested classes on a
:ref:`Program <CLIABS/Program>` class. Each nested class derived from predefined argument classes knows about the
correct formatting pattern, character escaping, and if needed about necessary type conversions.
Such an instance of a program can be converted to an argument list suitable for :class:`subprocess.Popen`.
In stead of deriving from :ref:`Program <CLIABS/Program>`, abstracted command line tools can derive from
:ref:`Executable <CLIABS/Executable>` which offers embedded :class:`~subprocess.Popen` behavior.
.. grid-item::
:columns: 6
.. code-block:: Python
class Git(Executable):
def __new__(cls, *args: Tuple[Any, ...], **kwargs: Dict[str, Any]) -> self:
cls._executableNames = {
"Darwin": "git",
"FreeBSD": "git",
"Linux": "git",
"Windows": "git.exe"
}
return super().__new__(cls)
@CLIArgument()
class FlagHelp(ShortFlag, name="h"): ...
@CLIArgument()
class FlagVersion(LongFlag, name="version"): ...
@CLIArgument()
class CommandHelp(CommandArgument, name="help"): ...
@CLIArgument()
class CommandCommit(CommandArgument, name="commit"): ...
@CLIArgument()
class ValueCommitMessage(ShortTupleFlag, name="m"): ...
tool = Git()
tool[tool.FlagVersion] = True
tool.StartProcess()
Common Helper Functions
=======================
.. grid:: 2
.. grid-item::
:columns: 6
This is a set of useful :ref:`helper functions <COMMON/HelperFunctions>`:
* :ref:`COMMON/Helper/firstElement`, :ref:`COMMON/Helper/lastElement` get the first/last element from an indexable.
* :ref:`COMMON/Helper/firstItem`, :ref:`COMMON/Helper/lastItem` get the first/last item from an iterable.
* :ref:`COMMON/Helper/firstKey`, :ref:`COMMON/Helper/firstValue`, :ref:`COMMON/Helper/firstPair` get the first
key/value/pair from an ordered dictionary.
* :ref:`COMMON/Helper/getsizeof` calculates the "real" size of a data structure.
* :ref:`COMMON/Helper/isnestedclass` checks if a class is nested inside another class.
* :ref:`COMMON/Helper/mergedicts` merges multiple dictionaries into a new dictionary.
* :ref:`COMMON/Helper/zipdicts` iterate multiple dictionaries simultaneously.
.. grid-item::
:columns: 6
.. tab-set::
.. tab-item:: firstItem
.. code-block:: Python
def myFunction(condition: bool) -> Iterable:
myList = [3, 21, 5, 7]
if condition:
return myList[0:2]
else
return myList[1:3]
beginOfSequence = myFunction(True)
first = firstItem(beginOfSequence)
# 3
.. tab-item:: mergedicts
.. code-block:: Python
from pyTooling.Common import mergedicts
dictA = {"a": 11, "b": 12}
dictB = {"x": 21, "y": 22}
for key, value in mergedicts(dictA, dictB):
pass
# ("a", 11)
# ("b", 12)
# ("x", 21)
# ("y", 22)
.. tab-item:: zipdicts
:selected:
.. code-block:: Python
from pyTooling.Common import zipdicts
dictA = {"a": 11, "b": 12, "c": 13}
dictB = {"a": 21, "b": 22, "c": 23}
for key, valueA, valueB in zipdicts(dictA, dictB):
pass
# ("a", 11, 21)
# ("a", 12, 22)
# ("a", 13, 23)
Common Classes
==============
.. grid:: 2
.. grid-item::
:columns: 6
* :ref:`Call-by-reference parameters <COMMON/CallByRef>`: Python doesn't provide *call-by-reference parameters* for
simple types. |br|
This behavior can be emulated with classes provided by the :mod:`pyTooling.CallByRef` module.
* :ref:`Unified license names <LICENSING>`: Setuptools, PyPI, and others have a varying understanding of license names. |br|
The :mod:`pyTooling.Licensing` module provides :ref:`unified license names <LICENSING>` as well as license name
mappings or translations.
* :ref:`Unified platform and environment description <COMMON/Platform>`: Python has many ways in figuring out the
current platform using APIs from ``sys``, ``platform``, ``os``, …. Unfortunately, none of the provided standard
APIs offers a comprehensive answer. pyTooling provides a :ref:`CurrentPlatform <COMMON/CurrentPlatform>`
singleton summarizing multiple platform APIs into a single class instance.
* :ref:`Representations of version numbers <VERSIONING>`: While Python itself has a good versioning schema, there
are no classes provided to abstract a version numbers. pyTooling provides such representations following
semantic versioning (SemVer) and calendar versioning (CalVer) schemes. The implementation can parse many common
formats and allows user defined formatting. In addition, versions can be compared with various operators
including PIPs ``~=`` operator.
* :ref:`Measuring execution times <COMMON/Stopwatch>` can be achieved by using a stopwatch implementation
providing start, pause, resume, split and stop features. Internally, Python's *high resolution clock* is used.
The stopwatch also provides a context manager, so it can be used in a ``with``-statement.
.. grid-item::
:columns: 6
.. tab-set::
.. tab-item:: Licenses
.. code-block:: Python
pass
.. tab-item:: Platform
.. code-block:: Python
from pytest import mark
from unittest import TestCase
from pyTooling.Common import CurrentPlatform
class MyTests(TestCase):
@mark.skipif(not CurrentPlatform.IsNativeWindows, reason="Skipped, if platform isn't native Windows.")
def test_OnlyNativeWindows(self) -> None:
pass
@mark.skipif(not CurrentPlatform.IsMinGW64OnWindows, reason="Skipped, if platform isn't MinGW64.")
def test_OnlyMinGW64(self) -> None:
pass
@mark.skipif(CurrentPlatform.IsPyPy, reason="getsizeof: not supported on PyPy")
def test_ObjectSize(self) -> None:
pass
.. tab-item:: Version Classes
:selected:
.. code-block:: Python
from pyTooling.Versioning import SemanticVersion, PythonVersion, CalendarVersion
version = SemanticVersion("v2.5.4")
version.Major
version.Minor
version.Patch
if version >= "2.5":
print(f"{version:%p%M.%m.%u}")
# Python versioning from sys.version_info
from pyTooling.Versioning import PythonVersion, CalendarVersion
pythonVersion = PythonVersion.FromSysVersionInfo()
# Calendar versioning
from pyTooling.Versioning import CalendarVersion
osvvmVersion = CalendarVersion.Parse("2024.07")
.. tab-item:: Stopwatch
.. code-block:: Python
from pyTooling.Stopwatch import Stopwatch
sw = Stopwatch("my name", preferPause=True)
sw.Start()
# do something
sw.Pause()
with sw:
# do something
sw.Resume()
# do something
sw.Stop()
print(f"Start: {sw.StartTime}")
print(f"Stop: {sw.StopTime}")
print(f"Duration: {sw.Duration}")
print(f"Activity: {sw.Activity}")
print(f"Inactivity: {sw.Inactivity}")
print("Splits:")
for duration, activity in sw:
print(f" {'running for' if activity else 'paused for '} {duration}")
Configuration
=============
.. grid:: 2
.. grid-item::
:columns: 6
Various file formats suitable for configuration information share the same features supporting: key-value pairs
(dictionaries), sequences (lists), and simple types like string, integer and float. pyTooling provides an
:ref:`abstract configuration file data model <CONFIG>` supporting these features. Moreover, concrete
:ref:`configuration file format reader <CONFIG/FileFormat>` implementations are provided as well.
* :ref:`JSON configuration reader <CONFIG/FileFormat/JSON>` for the JSON file format.
* :ref:`TOML configuration reader <CONFIG/FileFormat/TOML>` |rarr| To be implemented.
* :ref:`YAML configuration reader <CONFIG/FileFormat/YAML>` for the YAML file format.
.. grid-item::
:columns: 6
.. tab-set::
.. tab-item:: JSON
.. code-block:: Python
from pathlib import Path
from pyTooling.Configuration.JSON import Configuration
configFile = Path("config.json")
config = Configuration(configFile)
# Accessing root-level scalar value
configFileFormatVersion = config["version"]
# Accessing value in a sequence
firstItemInList = config["list"][0]
# Accessing first value in dictionary
firstItemInDict = config["dict"]["key_1"]
# Iterate simple list
simpleList = config["list"]
for item in simpleList:
pass
.. tab-item:: TOML
.. todo:: Needs example code
.. code-block:: Python
pass
.. tab-item:: YAML
.. code-block:: Python
from pathlib import Path
from pyTooling.Configuration.YAML import Configuration
configFile = Path("config.yml")
config = Configuration(configFile)
# Accessing root-level scalar value
configFileFormatVersion = config["version"]
# Accessing value in a sequence
firstItemInList = config["list"][0]
# Accessing first value in dictionary
firstItemInDict = config["dict"]["key_1"]
# Iterate simple list
simpleList = config["list"]
for item in simpleList:
pass
.. tab-item:: XML
.. todo:: Needs example code
.. code-block:: Python
pass
Data Structures
===============
.. grid:: 2
.. grid-item::
:columns: 6
pyTooling also provides :ref:`fast and powerful data structures <STRUCT>` offering object-oriented APIs:
* :ref:`Graph data structure <STRUCT/Graph>` |br|
|rarr| A directed graph implementation using a :class:`~pyTooling.Graph.Vertex` and an
:class:`~pyTooling.Graph.Edge` class.
* :ref:`Tree data structure <STRUCT/Tree>` |br|
|rarr| A fast and simple implementation using a single :class:`~pyTooling.Tree.Node` class.
* :ref:`Doubly Linked List <STRUCT/LinkedList>` |br|
|rarr| An object-oriented implementation using a :class:`~pyTooling.List.Node` and a
:class:`~pyTooling.List.LinkedList` class.
* :ref:`Path data structure <STRUCT/Path>` |br|
|rarr| To be documented.
* :ref:`Finite State Machine data structure <STRUCT/StateMachine>` |br|
|rarr| A data model for state machines using a :class:`~pyTooling.StateMachine.State` and a
:class:`~pyTooling.StateMachine.Transition` class.
.. #* :ref:`Scope data structure <STRUCT/Scope>` |br|
|rarr| A fast and simple implementation using a single :class:`~pyTooling.Tree.Node` class.
.. grid-item::
:columns: 6
.. tab-set::
.. tab-item:: Graph
.. code-block:: Python
from pyTooling.Graph import Graph, Vertex
graph = Graph(name="myGraph")
# Create new vertices and an edge between them
vertex1 = Vertex(vertexID=1, graph=graph)
vertex2 = Vertex(vertexID=2, value="2", graph=graph)
edge12 = vertex1.EdgeToVertex(vertex2, edgeValue="1 -> 2", weight=15)
# Create an edge to a new vertex
edge2x = vertex2.EdgeToNewVertex(vertexID=3)
vertex3 = edge2x.Destination
# Create a link between two vertices
link31 = vertex3.LinkToVertex(vertex1)
.. tab-item:: Statemachine
.. todo:: Needs example code
.. code-block:: Python
pass
.. tab-item:: Tree
.. code-block:: Python
from pyTooling.Tree import Node
# Create a new tree by creating a root node (no parent reference)
root = Node(value="OSVVM Regression Tests")
# Construct the tree top-down
lib = Node(value="Utility Library", parent=root)
# Another standalone node with unique ID (actually an independent tree)
common = Node(nodeID=5, value="Common")
# Construct bottom-up
axi = Node(value="AXI")
axiCommon = Node(value="AXI4 Common")
axi.AddChild(axiCommon)
# Group nodes and handover children at node creation time
vcList = [common, axi]
vcs = Node(value="Verification Components", parent=root, children=vcList)
# Add multiple nodes at once
axiProtocols = (
Node(value="AXI4-Stream"),
Node(value="AXI4-Lite"),
Node(value="AXI4")
)
axi.AddChildren(axiProtocols)
# Create another standalone node and attach it later to a tree.
uart = Node(value="UART")
uart.Parent = vcs
.. tab-item:: Doubly Linked List
.. code-block:: Python
from pyTooling.List import Node
# Create a new doubly linked list from an iterable
node = Node(2)
nodes = (Node(1), node, Node(3))
linkedList = LinkedList(nodes)
# Add node before first element
linkedList.InsertBeforeFirst(Node(0))
# Add node after last element
linkedList.InsertAfterLast(Node(4))
# Get length
linkedList.Count # alternatively: len(linkedList)
# Delete node
node.Remove()
.. grid:: 3
.. grid-item:: Graph
:columns: 4
.. mermaid::
:caption: A directed graph with backward-edges denoted by dotted vertex relations.
%%{init: { "flowchart": { "nodeSpacing": 15, "rankSpacing": 30, "curve": "linear", "useMaxWidth": false } } }%%
graph LR
A(A); B(B); C(C); D(D); E(E); F(F) ; G(G); H(H); I(I)
A --> B --> E
G --> F
A --> C --> G --> H --> D
D -.-> A
D & F -.-> B
I ---> E --> F --> D
classDef node fill:#eee,stroke:#777,font-size:smaller;
classDef node fill:#eee,stroke:#777,font-size:smaller;
classDef node fill:#eee,stroke:#777,font-size:smaller;
.. grid-item:: Statemachine
:columns: 3
.. mermaid::
:caption: A statemachine graph.
%%{init: { "flowchart": { "nodeSpacing": 15, "rankSpacing": 30, "curve": "linear", "useMaxWidth": false } } }%%
graph TD
A(Idle); B(Check); C(Prepare); D(Read); E(Finished); F(Write) ; G(Retry); H(WriteWait); I(ReadWait)
A:::mark1 --> B --> C --> F
F --> H --> E:::cur
B --> G --> B
G -.-> A --> C
D -.-> A
C ---> D --> I --> E -.-> A
classDef node fill:#eee,stroke:#777,font-size:smaller;
classDef cur fill:#9e9,stroke:#6e6;
classDef mark1 fill:#69f,stroke:#37f,color:#eee;
.. grid-item:: Tree
:columns: 5
.. mermaid::
:caption: Root of the current node are marked in blue.
%%{init: { "flowchart": { "nodeSpacing": 15, "rankSpacing": 30, "curve": "linear", "useMaxWidth": false } } }%%
graph TD
R(Root)
A(...)
BL(Node); B(GrandParent); BR(Node)
CL(Uncle); C(Parent); CR(Aunt)
DL(Sibling); D(Node); DR(Sibling)
ELN1(Niece); ELN2(Nephew)
EL(Child); E(Child); ER(Child);
ERN1(Niece);ERN2(Nephew)
F1(GrandChild); F2(GrandChild)
R:::mark1 --> A
A --> BL & B & BR
B --> CL & C & CR
C --> DL & D & DR
DL --> ELN1 & ELN2
D:::cur --> EL & E & ER
DR --> ERN1 & ERN2
E --> F1 & F2
classDef node fill:#eee,stroke:#777,font-size:smaller;
classDef cur fill:#9e9,stroke:#6e6;
classDef mark1 fill:#69f,stroke:#37f,color:#eee;
Decorators
==========
.. grid:: 2
.. grid-item::
:columns: 6
* :ref:`META/Abstract`
* :ref:`DECO/AbstractMethod`: Methods marked with :pycode:`@abstractmethod` are abstract and need to be overwritten in
a derived class. |br|
An *abstract method* might be called from the overwriting method.
* :ref:`DECO/MustOverride`: Methods marked with :pycode:`@mustoverride` are abstract and need to be overridden in a
derived class. |br|
It's not allowed to call a *mustoverride method*.
* :ref:`DECO/DataAccess`
* :ref:`DECO/readonly`: Methods marked with :pycode:`@readonly` get transformed into a read-only property.
* ⚠BROKEN⚠: Methods with :ref:`DECO/classproperty` decorator transform methods to class-properties.
* :ref:`DECO/Documentation`
* :ref:`DECO/export`: Register a given function or class as publicly accessible in a module. |br|
Functions and classes exposed like this are also used by Sphinx extensions to (auto-)document public module members.
* :ref:`DECO/InheritDocString`: The decorator copies the doc-string from a given base-class to the annotated method.
* :ref:`DECO/Performance`
* :ref:`DECO/slotted`: Classes marked with :pycode:`@slotted` get transformed into classes using ``__slots__``. |br|
This is achieve by exchanging the meta-class to :class:`~pyTooling.MetaClasses.ExtendedType`.
* :ref:`DECO/mixin`: Classes marked with :pycode:`@mixin` do not store their fields in ``__slots__``. |br|
When such a :term:`mixin-class` is inherited by a class using slots, the fields of the mixin become slots.
* :ref:`DECO/singleton`: Classes marked with :pycode:`@singleton` get transformed into singleton classes. |br|
This is achieve by exchanging the meta-class to :class:`~pyTooling.MetaClasses.ExtendedType`.
* :ref:`DECO/Misc`
* :ref:`DECO/notimplemented`: This decorator replaces a callable (function or method) with a callable raising a
:exc:`NotImplementedError`. The original code becomes unreachable.
.. grid-item::
:columns: 6
.. todo:: Needs example code
.. code-block:: Python
pass
Exceptions
==========
* :exc:`~pyTooling.Exceptions.EnvironmentException` |br|
... is raised when an expected environment variable is missing.
* :exc:`~pyTooling.Exceptions.PlatformNotSupportedException` |br|
... is raise if the platform is not supported.
* :exc:`~pyTooling.Exceptions.NotConfiguredException` |br|
... is raise if the requested setting is not configured.
Meta-Classes
============
pyTooling provides an :ref:`enhanced meta-class <META>` called :class:`~pyTooling.MetaClasses.ExtendedType` to replace
the default meta-class :class:`type`. It combines features like using slots, abstract methods and creating singletons by
applying a single meta-class. In comparison, Python's approach in to provide multiple specific meta-classes (see
:mod:`abc`) that can't be combined e.g. to a singleton using slots.
:ref:`ExtendedType <META/ExtendedType>` allows to implement :ref:`slotted types <META/Slotted>`,
:ref:`mixins <META/Mixin>`, :ref:`abstract and override methods <META/Abstract>` and :ref:`singletons <META/Singleton>`,
and combinations thereof. Exception messages in case of errors have been improved too.
Slotted types significantly reduce the memory footprint by 4x and decrease the class field access time by 10..25%. While
setting up slotted types needed a lot of manual coding, this is now fully automated by this meta-class. It assumes,
annotated fields are going to be slots. Moreover, it also takes care deferred slots in multiple-inheritance scenarios by
marking secondary base-classes as mixins. This defers slot creation until a mixin is inherited.
.. grid:: 2
.. grid-item::
:columns: 6
:pycode:`class MyClass(metaclass=ExtendedType):`
A class definition using the :class:`~pyTooling.MetaClasses.ExtendedType` meta-class. I can now implement
:ref:`abstract methods <META/Abstract>` using the decorators :ref:`DECO/AbstractMethod` or :ref:`DECO/MustOverride`.
:pycode:`class MyClass(metaclass=ExtendedType, singleton=True):`
A class defined with enabled :ref:`singleton <META/Singleton>` behavior allows only a single instance of that class to
exist. If another instance is going to be created, a previously cached instance of that class will be returned.
:pycode:`class MyClass(metaclass=ExtendedType, slots=True):`
A class defined with enabled :ref:`slots <META/Slotted>` behavior stores instance fields in slots. The meta-class,
translates all type-annotated fields in the class definition to slots. Slots allow a more efficient field storage and
access compared to dynamically stored and accessed fields hosted in ``__dict__``. This improves the memory footprint
as well as the field access performance of all class instances. This behavior is automatically inherited to all
derived classes.
:pycode:`class MyClass(metaclass=ExtendedType, slots=True, mixin=True):`
A class defined with enabled :ref:`mixin <META/Mixin>` behavior collects type-annotated instance fields so they can be
added to slots in an inherited class. Thus, slots are not created for mixin-classes but deferred in the inheritance
hierarchy.
:pycode:`class MyClass(SlottedObject):`
A class definition deriving from :class:`~pyTooling.MetaClasses.SlottedObject` will bring the slotted type behavior to
that class and all its derived classes.
.. grid-item::
:columns: 6
.. tab-set::
.. tab-item:: Singleton
.. code-block:: Python
class Application(metaclass=ExtendedType, singleton=True):
_x: int
def __init__(self) -> None:
print("Instance of 'App1WithoutParameters' was created")
self._x = 10
instance1 = Application()
instance2 = Application()
assert instance1 is instance2
.. tab-item:: Slotted Class
.. code-block:: Python
class Data(metaclass=ExtendedType, slots=True):
_x: int
_y: int = 12
def __init__(self, x: int) -> None:
self._x = x
data = Data(11)
.. tab-item:: MixIn Class
.. todo:: Needs example code
.. code-block:: Python
def
Packaging
=========
.. grid:: 2
.. grid-item::
:columns: 6
A set of helper functions to describe a Python package for setuptools.
* Helper Functions:
* :func:`pyTooling.Packaging.loadReadmeFile` |br|
Load a ``README.md`` file from disk and provide the content as long description for setuptools.
* :func:`pyTooling.Packaging.loadRequirementsFile` |br|
Load a ``requirements.txt`` file from disk and provide the content for setuptools.
* :func:`pyTooling.Packaging.extractVersionInformation` |br|
Extract version information from Python source files and provide the data to setuptools.
* Package Descriptions
* :func:`pyTooling.Packaging.DescribePythonPackage` |br|
tbd
* :func:`pyTooling.Packaging.DescribePythonPackageHostedOnGitHub` |br|
tbd
.. grid-item::
:columns: 6
.. tab-set::
.. tab-item:: DescribePythonPackage
.. code-block:: Python
from setuptools import setup
from pathlib import Path
from pyTooling.Packaging import DescribePythonPackage
pass
.. tab-item:: DescribePythonPackageHostedOnGitHub
:selected:
.. code-block:: Python
from setuptools import setup
from pathlib import Path
from pyTooling.Packaging import DescribePythonPackageHostedOnGitHub
gitHubNamespace = "Paebbels"
packageName = "pyVersioning"
packageDirectory = packageName.replace(".", "/")
packageInformationFile = Path(f"{packageDirectory}/__init__.py")
setup(
**DescribePythonPackageHostedOnGitHub(
packageName=packageName,
description="Write version information collected from (CI) environment for any programming language as source file.",
gitHubNamespace=gitHubNamespace,
sourceFileWithVersion=packageInformationFile,
consoleScripts={
"pyVersioning": "pyVersioning.CLI:main",
}
)
)
Terminal
========
.. grid:: 2
.. grid-item::
:columns: 6
The :ref:`pyTooling.TerminalUI <TERM>` package offers a set of helpers to implement a text user interface (TUI) in
a terminal. It's designed on the idea that command line programs emit one line of text per message. Each message
can be categorized as normal text, warnings, errors, and many more.
Therefore, this package offers a :ref:`LineTerminal <TERM/LineTerminal>` implementation, derived from a basic
:ref:`Terminal <TERM/Terminal>` class. Of cause, it also includes colored outputs based on `colorama`.
.. todo:: Terminal helpers.
.. grid-item::
:columns: 6
.. todo:: Needs example code
.. _CONTRIBUTORS:
Contributors
************
* `Patrick Lehmann <https://GitHub.com/Paebbels>`__ (Maintainer)
* `Sven Köhler <https://GitHub.com/skoehler>`__
* `Unai Martinez-Corral <https://GitHub.com/umarcor/>`__
* `and more... <https://GitHub.com/pyTooling/pyTooling/graphs/contributors>`__
.. _LICENSE:
License
*******
.. only:: html
This Python package (source code) is licensed under `Apache License 2.0 <Code-License.html>`__. |br|
The accompanying documentation is licensed under `Creative Commons - Attribution 4.0 (CC-BY 4.0) <Doc-License.html>`__.
.. only:: latex
This Python package (source code) is licensed under **Apache License 2.0**. |br|
The accompanying documentation is licensed under **Creative Commons - Attribution 4.0 (CC-BY 4.0)**.
.. toctree::
:caption: Overview
:hidden:
News
Installation
Dependency
Tutorials/index
.. raw:: latex
\part{Main Documentation}
.. toctree::
:caption: Attributes
:hidden:
Attributes/index
Attributes/ArgParse
.. toctree::
:caption: CLI Abstraction
:hidden:
CLIAbstraction/index
CLIAbstraction/Program
CLIAbstraction/Executable
CLIAbstraction/Arguments
.. toctree::
:caption: Common
:hidden:
Common/index
Common/CallByRef
Common/Licensing
Common/Filesystem
Common/Platform
Common/Stopwatch
Common/Versioning
.. toctree::
:caption: Configuration
:hidden:
Configuration/index
Configuration/FileFormats
.. toctree::
:caption: Data Structures
:hidden:
DataStructures/index
DataStructures/LinkedList
DataStructures/Cartesian
DataStructures/Graph
DataStructures/Path/index
DataStructures/StateMachine
DataStructures/Tree
.. toctree::
:caption: Decorators
:hidden:
Decorators
.. toctree::
:caption: Exceptions and Warnings
:hidden:
Exceptions
Warning/index
.. toctree::
:caption: Meta Classes
:hidden:
MetaClasses
.. toctree::
:caption: Packaging
:hidden:
Packaging
.. toctree::
:caption: Terminal
:hidden:
Terminal/index
.. raw:: latex
\part{References and Reports}
.. toctree::
:caption: References and Reports
:hidden:
Python Class Reference <pyTooling/pyTooling>
unittests/index
coverage/index
CodeCoverage
Doc. Coverage Report <DocCoverage>
Static Type Check Report ➚ <typing/index>
.. raw:: latex
\part{Appendix}
.. toctree::
:caption: Appendix
:hidden:
License
Doc-License
Glossary
genindex
Python Module Index <modindex>
TODO
|