sob.thesaurus
This module provides functionality for creating a data model from a set of example structures.
Synonyms
This class is a set-like object containing deserialized data, implied to represent variations of one type of entity, and is used to infer a model for that entity.
Source code in src/sob/thesaurus.py
499 500 501 502 503 504 505 506 |
|
add
This method adds a synonymous item to the set. If the item is a file-like (input/output) object, that object is first read, deserialized, and unmarshalled.
Parameters:
-
item
(sob.abc.Readable | sob.abc.MarshallableTypes
) –A file-like or a JSON-serializable python object.
Source code in src/sob/thesaurus.py
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 |
|
union
union(
other: collections.abc.Iterable[
sob.abc.Readable | sob.abc.MarshallableTypes
],
) -> sob.thesaurus.Synonyms
This method returns an instance of Synonyms
which incorporates
all (non-redundant) items from both self
and other
.
Source code in src/sob/thesaurus.py
582 583 584 585 586 587 588 589 590 591 |
|
get_models
get_models(
pointer: str,
module: str = "__main__",
name: typing.Callable[
[str], str
] = sob.thesaurus.get_class_name_from_pointer,
) -> collections.abc.Iterable[type]
Retrieve a sequence of class definitions representing a data model capable of describing these synonyms.
Parameters:
- pointer (str): A JSON pointer for the top-level model class, used to infer class names.
- module (str): The name of the module in which model classes will be defined. This defaults to "main".
- name (str) = sob.thesaurus.get_class_name_from_pointer:
A function which accepts one
str
argument—a synonym key concatenated with "#" and JSON pointer (for example: "key#/body/items/0") and which returns astr
which will be the resulting class name (for example: "KeyBodyItemsItem").
Source code in src/sob/thesaurus.py
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 |
|
Thesaurus
Thesaurus(
_items: (
collections.abc.Mapping[
str,
collections.abc.Iterable[
sob.abc.Readable | sob.abc.MarshallableTypes
]
| sob.thesaurus.Synonyms,
]
| collections.abc.Iterable[
tuple[
str,
collections.abc.Iterable[
sob.abc.Readable
| sob.abc.MarshallableTypes
]
| sob.thesaurus.Synonyms,
]
]
| sob.thesaurus.Thesaurus
| None
) = None,
**kwargs: collections.abc.Iterable[
sob.abc.Readable | sob.abc.MarshallableTypes
]
)
An instance of sob.Thesaurus
is a dictionary-like object wherein
each value is an instance of sob.Synonyms
.
For example, if you have an API with several GET endpoints, the endpoint
paths relative to the API base URL would make ideal keys for your
sob.Thesaurus
instance. After adding a representative sample of responses
from each endpoint to the corresponding sob.Synonyms
instance in your
sob.Thesaurus
instance, your thesaurus will be able to generate a
python module with an sob
based data model for all of your endpoints,
including polymorphism where encountered.
The keys of an sob.Thesaurus
dictionary are meaningful in that they
contribute to the naming of classes (which are formatted to comply with
PEP-8, and to avoid collision with builtins, language keywords, etc.).
For background: The sob
library was designed for authoring a data model
representing schemas defined by an OpenAPI specification. Although
OpenAPI specifications are increasingly ubiquitous, there are scenarios
where you might need to interact with an API which does not have an
OpenAPI specification, or for which the OpenAPI specification is simply
not available to you. In these cases, you can generate an sob
model to validate your API responses using sob.Thesaurus
.
Parameters:
-
_items
(collections.abc.Mapping[str, collections.abc.Iterable[sob.abc.Readable | sob.abc.MarshallableTypes] | sob.thesaurus.Synonyms] | collections.abc.Iterable[tuple[str, collections.abc.Iterable[sob.abc.Readable | sob.abc.MarshallableTypes] | sob.thesaurus.Synonyms]] | sob.thesaurus.Thesaurus | None
, default:None
) –A mapping of keys to values, where each value is an iterable of items which are synonymous with the key. This can either be an iterable of key/value pair tuples, or a dictionary-like object.
Source code in src/sob/thesaurus.py
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 |
|
__setitem__
__setitem__(
key: str,
value: (
sob.thesaurus.Synonyms
| collections.abc.Iterable[
sob.abc.Readable | sob.abc.MarshallableTypes
]
),
) -> None
This method adds/overwrites the synonyms for the specified key
.
If the value
is not an instance of sob.Synonyms
, a new instance
of sob.Synonyms
is created and JSON data items from value
are
added to it.
Parameters:
-
key
(str
) –A string to utilize when attributing a unique name to the class representing these synonyms.
-
value
(sob.thesaurus.Synonyms | collections.abc.Iterable[sob.abc.Readable | sob.abc.MarshallableTypes]
) –An iterable of JSON data which should be considered synonymous.
Source code in src/sob/thesaurus.py
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 |
|
__delitem__
__delitem__(key: str) -> None
This method deletes the synonyms assigned the specified key
.
Parameters:
-
key
(str
) –
Source code in src/sob/thesaurus.py
1087 1088 1089 1090 1091 1092 1093 1094 |
|
pop
pop(
key: str,
default: (
sob.thesaurus.Synonyms | sob._types.Undefined
) = sob._types.UNDEFINED,
) -> sob.thesaurus.Synonyms
This method removes and returns the synonyms assigned to the specified
key
.
Parameters:
-
key
(str
) – -
default
(sob.thesaurus.Synonyms | sob._types.Undefined
, default:sob._types.UNDEFINED
) –A value to return if the specified
key
does not exist. If no default is provided, aKeyError
will be raised if the key is not found.
Source code in src/sob/thesaurus.py
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 |
|
popitem
This method removes and returns a tuple of the most recently added
key/synonyms pair (by default), or the first added key/synonyms pair
if last
is set to False
.
Source code in src/sob/thesaurus.py
1114 1115 1116 1117 1118 1119 1120 |
|
clear
clear() -> None
This method clears the thesaurus, removing all synonyms.
Source code in src/sob/thesaurus.py
1122 1123 1124 1125 1126 |
|
update
update(
**kwargs: (
sob.thesaurus.Synonyms
| collections.abc.Iterable[
sob.abc.Readable | sob.abc.MarshallableTypes
]
),
) -> None
This method updates the thesaurus with one or more specified synonyms.
Parameters:
-
kwargs
(sob.thesaurus.Synonyms | collections.abc.Iterable[sob.abc.Readable | sob.abc.MarshallableTypes]
, default:{}
) –A mapping of keys to values, where each value is an iterable of items which are synonymous with the key, or is an instance of
sob.Synonyms
.
Source code in src/sob/thesaurus.py
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 |
|
setdefault
setdefault(
key: str,
default: collections.abc.Iterable[
sob.abc.Readable | sob.abc.MarshallableTypes
],
) -> sob.thesaurus.Synonyms
This method assigns default
synonyms to the specified key
if
no synonyms have previously been assigned to the key, and returns
either the existing or newly assigned synonyms.
Source code in src/sob/thesaurus.py
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 |
|
get_module_source
get_module_source(
name: typing.Callable[
[str], str
] = sob.thesaurus.get_class_name_from_pointer,
) -> str
This method generates and returns the source code for a module defining data models applicable to the data contained in this thesaurus.
Parameters:
- name (str) = sob.thesaurus.get_class_name_from_pointer:
A function which accepts one
str
argument—a synonym key concatenated with "#" and JSON pointer (for example: "key#/body/items/0") and which returns astr
which will be the resulting class name (for example: "KeyBodyItemsItem").
Source code in src/sob/thesaurus.py
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 |
|
get_module
get_module(
name: typing.Callable[
[str], str
] = sob.thesaurus.get_class_name_from_pointer,
) -> types.ModuleType
This method generates and returns a module defining data models
applicable to the data contained in this thesaurus. This module is not
suitable for writing out for static use--use Thesaurus.save_module
to generate and write a model suitable for static use.
Parameters:
- name (str) = sob.thesaurus.get_class_name_from_pointer:
A function which accepts one
str
argument—a synonym key concatenated with "#" and JSON pointer (for example: "key#/body/items/0") and which returns astr
which will be the resulting class name (for example: "KeyBodyItemsItem").
Source code in src/sob/thesaurus.py
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 |
|
save_module
save_module(
path: str | pathlib.Path,
name: typing.Callable[
[str], str
] = sob.thesaurus.get_class_name_from_pointer,
) -> None
This method generates and saves the source code for a module defining data models applicable to the data contained in this thesaurus.
Parameters:
- path (str): The file path where the data will be written.
- name (str) = sob.thesaurus.get_class_name_from_pointer:
A function which accepts one
str
argument—a synonym key concatenated with "#" and JSON pointer (for example: "key#/body/items/0") and which returns astr
which will be the resulting class name (for example: "KeyBodyItemsItem").
Source code in src/sob/thesaurus.py
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 |
|
get_class_name_from_pointer
get_class_name_from_pointer(pointer: str) -> str
This function creates a class name based on the sob.Thesaurus
key of the
sob.Synonyms
instance to which an element belongs,
combined with the JSON pointer of the applicable element. This function
can be substituted for another, when generating a module from a thesaurus,
by passing a function to the name
parameter of
sob.Thesaurus.get_module_source
, sob.Thesaurus.get_module
, or
sob.Thesaurus.save_module
.
Parameters:
- pointer (str): The synonyms key + JSON pointer of the element for which the class is being generated.
Source code in src/sob/thesaurus.py
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 |
|
get_class_meta_attribute_assignment_source
get_class_meta_attribute_assignment_source(
class_name_: str,
attribute_name: str,
metadata: sob.abc.Meta,
) -> str
This function generates source code for setting a metadata attribute on a class.
Parameters:
- class_name (str): The name of the class to which we want to assign a metadata attribute.
- attribute_name (str): The name of the attribute we want to assign.
- metadata (sob.abc.Meta): The metadata from which to take the assigned value.
Source code in src/sob/thesaurus.py
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 |
|