-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
upd: models.py added (pydantic) and PEP 518
- Loading branch information
Showing
6 changed files
with
195 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
from __future__ import annotations | ||
from datetime import datetime | ||
from bson.objectid import ObjectId as BsonObjectId | ||
from typing import Annotated, List, Union, Optional | ||
import json | ||
from pydantic import BaseModel, Field, validator | ||
from re import findall | ||
|
||
|
||
class ObjectIdStr(str): | ||
@classmethod | ||
def __get_validators__(cls): | ||
yield cls.validate | ||
|
||
@classmethod | ||
def validate(cls, v): | ||
try: | ||
return BsonObjectId(v) | ||
except Exception: | ||
raise ValueError(f"{v} is not a valid ObjectId") | ||
return str(v) | ||
|
||
|
||
class PydanticObjectId(BsonObjectId): | ||
@classmethod | ||
def __get_validators__(cls): | ||
yield cls.validate | ||
|
||
@classmethod | ||
def validate(cls, v): | ||
if not isinstance(v, BsonObjectId): | ||
raise TypeError("ObjectId required") | ||
return str(v) | ||
|
||
|
||
class Provenance(BaseModel): | ||
email: Optional[str] # email retrieved from JWT | ||
affiliation: Optional[str] # affiliation retrieved from JWT | ||
createdAt: Annotated[ | ||
datetime, Field(default_factory=lambda: datetime.now().utcnow()) | ||
] | ||
updatedAt: Annotated[ | ||
datetime, Field(default_factory=lambda: datetime.now().utcnow()) | ||
] | ||
|
||
|
||
class Project(BaseModel): | ||
name: str | ||
label: str | ||
description: Optional[str] | ||
|
||
|
||
class Parameter(BaseModel): | ||
name: str | ||
value: Union[float, str] | ||
|
||
|
||
class Agent(BaseModel): | ||
name: str | ||
version: str | ||
|
||
|
||
class ChemicalCompositionItem(BaseModel): | ||
element: str | ||
percentage: str | ||
|
||
|
||
class Lattice(BaseModel): | ||
a: str | ||
b: str | ||
c: str | ||
alpha: str | ||
beta: str | ||
gamma: str | ||
|
||
|
||
class InputMaterial(BaseModel): | ||
lattice: Optional[Lattice] | ||
sites: List[List[float]] | ||
species: List[str] | ||
cell: List[List[float]] | ||
|
||
|
||
class OutputMaterial(BaseModel): | ||
lattice: Optional[Lattice] | ||
sites: List[List[float]] | ||
species: List[str] | ||
cell: List[List[float]] | ||
|
||
|
||
class Material(BaseModel): | ||
formula: str | ||
elements: Optional[List[str]] # List[Union[str, str]] | ||
input: Optional[InputMaterial] | ||
output: Optional[OutputMaterial] | ||
|
||
@validator("elements", always=True) | ||
def composite_name(cls, v, values, **kwargs): | ||
elements = [ | ||
x | ||
for x in findall("[A-Z][a-z]?|[0-9]+", values["formula"]) | ||
if not x.isnumeric() | ||
] | ||
return elements | ||
|
||
|
||
class PropertyFile(BaseModel): | ||
fullpath: str | ||
|
||
|
||
class Property(BaseModel): | ||
name: str | ||
value: Union[float, str] | ||
file: Optional[PropertyFile] | ||
|
||
|
||
class Process(BaseModel): | ||
isExperiment: bool | ||
method: str | ||
agent: Optional[Agent] | ||
|
||
|
||
class FileProject(BaseModel): | ||
hash: Optional[str] | ||
name: str | ||
extention: str | ||
size: Optional[str] | ||
createdAt: Annotated[ | ||
datetime, Field(default_factory=lambda: datetime.now().utcnow()) | ||
] | ||
updatedAt: Annotated[ | ||
datetime, Field(default_factory=lambda: datetime.now().utcnow()) | ||
] | ||
|
||
class Config: | ||
use_enum_values = True | ||
|
||
|
||
def validate_datetime(cls, values): | ||
""" | ||
Reusable validator for pydantic models | ||
""" | ||
return values or datetime.now().utcnow() | ||
|
||
|
||
class newProject(BaseModel): | ||
identifier: Optional[str] | ||
provenance: Optional[Provenance] | ||
project: Project | ||
process: Process | ||
material: Material | ||
parameters: List[Parameter] | ||
properties: List[Property] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
[build-system] | ||
requires = ["setuptools>=61.0"] | ||
build-backend = "setuptools.build_meta" | ||
|
||
[project] | ||
name = "iemap" | ||
version = "0.3.1" | ||
authors = [ | ||
{ name="Sergio Ferlito", email="sergio.ferlito@enea.it" }, | ||
{ name="marco.puccini", email="marco.puccini@enea.it" } | ||
] | ||
maintainers = [ | ||
{ name = "Marco Puccini", email = "marco.puccini@enea.it" } | ||
] | ||
description = "Tools to use IEMAP Rest API" | ||
readme = "README.md" | ||
requires-python = ">=3.10" | ||
classifiers = [ | ||
"Development Status :: 3 - Alpha", | ||
"Intended Audience :: Science/Research", | ||
"Operating System :: OS Independent", | ||
"Topic :: Scientific/Engineering", | ||
"Topic :: Software Development :: Libraries :: Python Modules", | ||
"License :: OSI Approved :: GNU General Public License (GPL)", | ||
"Programming Language :: Python :: 3.10", | ||
"Topic :: Scientific/Engineering :: Physics", | ||
"Topic :: Utilities" | ||
] | ||
dependencies = [ | ||
"requests==2.28.1", | ||
"requests_toolbelt==0.9.1", | ||
"bson==0.5.10", | ||
"pydantic==1.10.2" | ||
] | ||
|
||
[project.urls] | ||
"Homepage" = "https://github.com/ai4mat/iemap-module" | ||
"Bug Tracker" = "https://github.com/ai4mat/iemap-module/issues" |
This file was deleted.
Oops, something went wrong.