mdmodels 0.1.0__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.
Files changed (37) hide show
  1. mdmodels-0.1.0/PKG-INFO +75 -0
  2. mdmodels-0.1.0/README.md +49 -0
  3. mdmodels-0.1.0/mdmodels/__init__.py +35 -0
  4. mdmodels-0.1.0/mdmodels/adder_method.py +130 -0
  5. mdmodels-0.1.0/mdmodels/create.py +498 -0
  6. mdmodels-0.1.0/mdmodels/datamodel.py +301 -0
  7. mdmodels-0.1.0/mdmodels/git_utils.py +50 -0
  8. mdmodels-0.1.0/mdmodels/graph/__init__.py +45 -0
  9. mdmodels-0.1.0/mdmodels/graph/basenode.py +196 -0
  10. mdmodels-0.1.0/mdmodels/graph/create.py +151 -0
  11. mdmodels-0.1.0/mdmodels/graph/relation.py +176 -0
  12. mdmodels-0.1.0/mdmodels/library.py +226 -0
  13. mdmodels-0.1.0/mdmodels/llm/__init__.py +34 -0
  14. mdmodels-0.1.0/mdmodels/llm/embed.py +81 -0
  15. mdmodels-0.1.0/mdmodels/llm/fetcher.py +141 -0
  16. mdmodels-0.1.0/mdmodels/llm/handler.py +137 -0
  17. mdmodels-0.1.0/mdmodels/llm/prompts.py +157 -0
  18. mdmodels-0.1.0/mdmodels/llm/response.py +87 -0
  19. mdmodels-0.1.0/mdmodels/llm/templates/__init__.py +23 -0
  20. mdmodels-0.1.0/mdmodels/llm/templates/dataset_query.py +231 -0
  21. mdmodels-0.1.0/mdmodels/meta.py +96 -0
  22. mdmodels-0.1.0/mdmodels/path.py +306 -0
  23. mdmodels-0.1.0/mdmodels/reference.py +131 -0
  24. mdmodels-0.1.0/mdmodels/sql/__init__.py +27 -0
  25. mdmodels-0.1.0/mdmodels/sql/base.py +43 -0
  26. mdmodels-0.1.0/mdmodels/sql/connector.py +215 -0
  27. mdmodels-0.1.0/mdmodels/sql/create.py +425 -0
  28. mdmodels-0.1.0/mdmodels/sql/insert.py +240 -0
  29. mdmodels-0.1.0/mdmodels/sql/linked_type.py +123 -0
  30. mdmodels-0.1.0/mdmodels/sql/utils.py +197 -0
  31. mdmodels-0.1.0/mdmodels/units/__init__.py +21 -0
  32. mdmodels-0.1.0/mdmodels/units/annotation.py +41 -0
  33. mdmodels-0.1.0/mdmodels/units/converter.py +210 -0
  34. mdmodels-0.1.0/mdmodels/units/mappings.py +59 -0
  35. mdmodels-0.1.0/mdmodels/units/unit_definition.py +168 -0
  36. mdmodels-0.1.0/mdmodels/utils.py +77 -0
  37. mdmodels-0.1.0/pyproject.toml +46 -0
@@ -0,0 +1,75 @@
1
+ Metadata-Version: 2.1
2
+ Name: mdmodels
3
+ Version: 0.1.0
4
+ Summary: Python package for the MDModels Rust crate
5
+ Author: Jan Range
6
+ Author-email: range.jan@web.de
7
+ Requires-Python: >=3.10,<4.0
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: Programming Language :: Python :: 3.10
10
+ Classifier: Programming Language :: Python :: 3.11
11
+ Classifier: Programming Language :: Python :: 3.12
12
+ Classifier: Programming Language :: Python :: 3.13
13
+ Requires-Dist: astropy (>=6,<7)
14
+ Requires-Dist: bigtree (>=0.21.1,<0.22.0)
15
+ Requires-Dist: dotted-dict (>=1.1.3,<2.0.0)
16
+ Requires-Dist: httpx (>=0.28,<0.29)
17
+ Requires-Dist: mdmodels-core (>=0.1.7,<0.2.0)
18
+ Requires-Dist: nest-asyncio (>=1.6.0,<2.0.0)
19
+ Requires-Dist: pandas (>=2.2.3,<3.0.0)
20
+ Requires-Dist: pydantic-xml (>=2.12.1,<3.0.0)
21
+ Requires-Dist: python-forge (>=18.6.0,<19.0.0)
22
+ Requires-Dist: python-jsonpath (>=1.2.0,<2.0.0)
23
+ Requires-Dist: rich (>=13.9.4,<14.0.0)
24
+ Requires-Dist: validators (>=0.34.0,<0.35.0)
25
+ Description-Content-Type: text/markdown
26
+
27
+ # pyMDModels
28
+
29
+ This is the Python package for the MDModels Rust crate and hosts a set of tools to work with metadata models defined in markdown.
30
+
31
+ **Whats in the bag?**
32
+
33
+ - **Core** - The core functionality to work with MDModels
34
+ - **PyDantic** - Generate Pydantic models (on steroids) from MDModels
35
+ - **LLM tools** - Use LLMs to extract, transform and explore data
36
+ - **SQL tools** - Create SQL databases and query them with SQLAlchemy
37
+ - **Graph tools** - Create graph databases and query them with SPARQL
38
+
39
+ One note before you start: This package is work in progress and the API will likely change in the future. Issues and contributions are very welcome!
40
+
41
+ ## Installation
42
+
43
+ To install the package, you can use the following command:
44
+
45
+ ```bash
46
+ pip install mdmodels
47
+
48
+ # LLM tools
49
+ pip install mdmodels[chat]
50
+
51
+ # Graph tools
52
+ pip install mdmodels[graph]
53
+
54
+ # SQL tools
55
+ pip install mdmodels[sql]
56
+
57
+ # All tools
58
+ pip install mdmodels[all]
59
+ ```
60
+
61
+ ## Examples
62
+
63
+ To get you started, have a look at the [examples](./examples) folder, featuring notebooks that showcase the usage of the package. This is what is available right now:
64
+
65
+ - [Core](./examples/basic)
66
+ - [Basic](./examples/basic) - Basic usage of the core functionality
67
+ - [LLM tools](./examples/llm)
68
+ - [Question Answering](./examples/llm/answering) - Use LLMs to answer questions about the data
69
+ - [Metadata Extraction](./examples/llm/extraction) - Use LLMs to extract metadata from text and to databases
70
+ - [Metadata Mapping](./examples/llm/mapping) - Use LLMs to map metadata from one format to another
71
+ - [Similarity Search](./examples/llm/embedding) - Use LLMs to find similar items in a database
72
+ - [SQL tools](./examples/sql)
73
+ - [Basic](./examples/sql/basic) - Create a SQL database and interact with it
74
+ - [Graph tools](./examples/graph)
75
+ - [Basic](./examples/graph/basic) - Create a graph database and interact with it
@@ -0,0 +1,49 @@
1
+ # pyMDModels
2
+
3
+ This is the Python package for the MDModels Rust crate and hosts a set of tools to work with metadata models defined in markdown.
4
+
5
+ **Whats in the bag?**
6
+
7
+ - **Core** - The core functionality to work with MDModels
8
+ - **PyDantic** - Generate Pydantic models (on steroids) from MDModels
9
+ - **LLM tools** - Use LLMs to extract, transform and explore data
10
+ - **SQL tools** - Create SQL databases and query them with SQLAlchemy
11
+ - **Graph tools** - Create graph databases and query them with SPARQL
12
+
13
+ One note before you start: This package is work in progress and the API will likely change in the future. Issues and contributions are very welcome!
14
+
15
+ ## Installation
16
+
17
+ To install the package, you can use the following command:
18
+
19
+ ```bash
20
+ pip install mdmodels
21
+
22
+ # LLM tools
23
+ pip install mdmodels[chat]
24
+
25
+ # Graph tools
26
+ pip install mdmodels[graph]
27
+
28
+ # SQL tools
29
+ pip install mdmodels[sql]
30
+
31
+ # All tools
32
+ pip install mdmodels[all]
33
+ ```
34
+
35
+ ## Examples
36
+
37
+ To get you started, have a look at the [examples](./examples) folder, featuring notebooks that showcase the usage of the package. This is what is available right now:
38
+
39
+ - [Core](./examples/basic)
40
+ - [Basic](./examples/basic) - Basic usage of the core functionality
41
+ - [LLM tools](./examples/llm)
42
+ - [Question Answering](./examples/llm/answering) - Use LLMs to answer questions about the data
43
+ - [Metadata Extraction](./examples/llm/extraction) - Use LLMs to extract metadata from text and to databases
44
+ - [Metadata Mapping](./examples/llm/mapping) - Use LLMs to map metadata from one format to another
45
+ - [Similarity Search](./examples/llm/embedding) - Use LLMs to find similar items in a database
46
+ - [SQL tools](./examples/sql)
47
+ - [Basic](./examples/sql/basic) - Create a SQL database and interact with it
48
+ - [Graph tools](./examples/graph)
49
+ - [Basic](./examples/graph/basic) - Create a graph database and interact with it
@@ -0,0 +1,35 @@
1
+ # -----------------------------------------------------------------------------
2
+ # Copyright (c) 2024 Jan Range
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ # of this software and associated documentation files (the "Software"), to deal
6
+ # in the Software without restriction, including without limitation the rights
7
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ # copies of the Software, and to permit persons to whom the Software is
9
+ # furnished to do so, subject to the following conditions:
10
+ # #
11
+ # The above copyright notice and this permission notice shall be included in
12
+ # all copies or substantial portions of the Software.
13
+ # #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ # THE SOFTWARE.
21
+ # -----------------------------------------------------------------------------
22
+ import nest_asyncio
23
+ from mdmodels_core import Templates # noqa
24
+
25
+ from .datamodel import DataModel
26
+
27
+
28
+ def patch_nest_asyncio():
29
+ nest_asyncio.apply()
30
+
31
+
32
+ __all__ = [
33
+ "DataModel",
34
+ "Templates",
35
+ ]
@@ -0,0 +1,130 @@
1
+ # -----------------------------------------------------------------------------
2
+ # Copyright (c) 2024 Jan Range
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ # of this software and associated documentation files (the "Software"), to deal
6
+ # in the Software without restriction, including without limitation the rights
7
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ # copies of the Software, and to permit persons to whom the Software is
9
+ # furnished to do so, subject to the following conditions:
10
+ # #
11
+ # The above copyright notice and this permission notice shall be included in
12
+ # all copies or substantial portions of the Software.
13
+ # #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ # THE SOFTWARE.
21
+ # -----------------------------------------------------------------------------
22
+ import builtins
23
+ from typing import Union, get_origin, get_args, Annotated
24
+ import warnings
25
+
26
+ import forge
27
+
28
+ from forge import FParameter, sign, FSignature
29
+
30
+ from mdmodels import DataModel
31
+ from mdmodels.units.unit_definition import UnitDefinition, BaseUnit
32
+ from mdmodels.utils import extract_dtype
33
+
34
+
35
+ def apply_adder_methods(cls: type[DataModel]):
36
+ """
37
+ Apply adder methods to the given DataModel class.
38
+
39
+ Args:
40
+ cls (type[DataModel]): The DataModel class to which adder methods will be applied.
41
+ """
42
+ for name, field in cls.model_fields.items():
43
+ if get_origin(field.annotation) is not list:
44
+ continue
45
+
46
+ method_name = f"add_to_{name}"
47
+ underlying_type = get_args(field.annotation)
48
+
49
+ if len(underlying_type) != 1 or get_origin(underlying_type[0]) is Union:
50
+ warnings.warn(
51
+ f"Only one type is supported for adder methods. {cls.__name__}.{name} has multiple types. Skipping.",
52
+ )
53
+ continue
54
+ else:
55
+ underlying_type = underlying_type[0]
56
+
57
+ if underlying_type in [UnitDefinition, BaseUnit] or is_builtin_type(
58
+ underlying_type
59
+ ):
60
+ continue
61
+
62
+ add_method = _create_add_method(underlying_type, name)
63
+ setattr(cls, method_name, add_method)
64
+
65
+
66
+ def is_builtin_type(obj):
67
+ """
68
+ Check if the given object is a built-in type.
69
+
70
+ Args:
71
+ obj: The object to check.
72
+
73
+ Returns:
74
+ bool: True if the object is a built-in type, False otherwise.
75
+ """
76
+ return obj.__name__ in dir(builtins)
77
+
78
+
79
+ def _create_add_method(
80
+ coll_cls: type[DataModel],
81
+ field: str,
82
+ ):
83
+ """
84
+ Create an adder method for the given collection class and field.
85
+
86
+ Args:
87
+ coll_cls (type[DataModel]): The collection class for which the adder method is created.
88
+ field (str): The field to which the adder method will be applied.
89
+
90
+ Returns:
91
+ function: The created adder method.
92
+ """
93
+
94
+ @sign(*_create_signature(coll_cls))
95
+ def add_method(self, **kwargs):
96
+ coll = getattr(self, field)
97
+ coll.append(coll_cls(**kwargs))
98
+ return coll[-1]
99
+
100
+ return add_method
101
+
102
+
103
+ def _create_signature(coll_cls: type[DataModel]):
104
+ """
105
+ Create a signature for the adder method.
106
+
107
+ Args:
108
+ coll_cls (type[DataModel]): The collection class for which the signature is created.
109
+
110
+ Returns:
111
+ FSignature: The created signature.
112
+ """
113
+ if get_origin(coll_cls) is Annotated:
114
+ coll_cls = extract_dtype(coll_cls)
115
+
116
+ annotations = coll_cls.__annotations__
117
+ parameters = [
118
+ FParameter(
119
+ kind=FParameter.KEYWORD_ONLY,
120
+ name=name,
121
+ type=annotations[name],
122
+ default=None if get_origin(annotations[name]) is not list else [],
123
+ )
124
+ for name in annotations
125
+ if name != "return"
126
+ ]
127
+
128
+ return FSignature(
129
+ [forge.self] + parameters,
130
+ )