Skip to content

Commit

Permalink
upd: models.py added (pydantic) and PEP 518
Browse files Browse the repository at this point in the history
  • Loading branch information
mpuccini committed Nov 29, 2022
1 parent 6b40158 commit d9609f6
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 44 deletions.
5 changes: 4 additions & 1 deletion iemap/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import time
from functools import wraps
from requests_toolbelt import MultipartEncoder
from requests.exceptions import ConnectionError
import requests
from os.path import dirname, isfile
from os.path import dirname
from os import getcwd
import mimetypes
import json
from bson import ObjectId
from iemap.helpers import get_file_size, endpoints
from iemap.models import newProject



Expand Down
153 changes: 153 additions & 0 deletions iemap/models.py
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]
38 changes: 38 additions & 0 deletions pyproject.toml
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"
3 changes: 0 additions & 3 deletions requirements.txt

This file was deleted.

2 changes: 0 additions & 2 deletions setup.cfg

This file was deleted.

38 changes: 0 additions & 38 deletions setup.py

This file was deleted.

0 comments on commit d9609f6

Please sign in to comment.