Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pydantic numpy ndarray type

# Define custom type validatable by Pydantic.
class TypedArray(numpy.ndarray):
    @classmethod
    def __get_validators__(cls):
        yield cls.validate_type

    @classmethod
    def validate_type(cls, val):
        return numpy.array(val, dtype=cls.inner_type)

class ArrayMeta(type):
    def __getitem__(self, t):
        return type('Array', (TypedArray,), {'inner_type': t})

class Array(numpy.ndarray, metaclass=ArrayMeta):
    pass

class Model(BaseModel):
    values: Array[float]

m = Model(values=[1, 2, 3])
print(m)
Comment

PREVIOUS NEXT
Code Example
Python :: convert from R to python 
Python :: Python fibonacci series (Recursion) 
Python :: load py file converted from .ui file 
Python :: python string: built-in function len() 
Python :: Delete all small Latin letters a from the given string. 
Python :: TypeError: Object of type DictProxy is not JSON serializable 
Python :: pandas datafdrame pyplot 
Python :: printing a varible with a varible. python 
Python :: Multiple page PyQt QStackedWidget 
Python :: Spatial Reference arcpy 
Python :: print(s[::-1]) 
Python :: Pipeline_parameters 
Python :: d2h recharge plan list 2022 telugu 
Python :: installing python3.8 in rpi 
Python :: # str and int mixup in python: 
Python :: python with statement variables 
Python :: how to get random images frrom quotefancy python 
Python :: limiting user input to under 100 characters python 
Python :: separate alphanumeric list 
Python :: how to import alpha vantage using api key 
Python :: pandas split coordinate tuple 
Python :: intersect and count in sql 
Python :: flask get summernote text 
Python :: jet 4 access python password 
Python :: duplicate finder python modules 
Python :: installing intelpython3_core using anaconda 
Python :: python difference between multiprocessing pool and threadpool 
Python :: Create a new list from a list when a certain condition is met 
Python :: fibonacci series recursive python 
Python :: EMAIL_BACKEND where to read 
ADD CONTENT
Topic
Content
Source link
Name
8+7 =