feat: add some syntax examples
This commit is contained in:
16
examples/00_syntax_prototype/01_simple_types.py
Normal file
16
examples/00_syntax_prototype/01_simple_types.py
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
# type: ignore
|
||||||
|
# ruff: disable[F821]
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
# A simple data-frame with different column of various simple types
|
||||||
|
# Columns can be named and/or typed
|
||||||
|
df: Frame[
|
||||||
|
verified: bool,
|
||||||
|
birth_year: int,
|
||||||
|
height: float,
|
||||||
|
name: str,
|
||||||
|
date: datetime,
|
||||||
|
float, # unnamed
|
||||||
|
unknown: _, # untyped
|
||||||
|
_ # unnamed and untyped
|
||||||
|
]
|
||||||
24
examples/00_syntax_prototype/02_custom_types.midas
Normal file
24
examples/00_syntax_prototype/02_custom_types.midas
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
// Simple custom type derived from floats
|
||||||
|
type Latitude<float>
|
||||||
|
type Longitude<float>
|
||||||
|
|
||||||
|
// Complex custom type, containing two values accessible through properties
|
||||||
|
type GeoLocation<Latitude, Longitude> {
|
||||||
|
lat: Latitude
|
||||||
|
lon: Longitude
|
||||||
|
}
|
||||||
|
|
||||||
|
type LatitudeDiff<float>
|
||||||
|
type LongitudeDiff<float>
|
||||||
|
|
||||||
|
// Simple operation defined on our custom types
|
||||||
|
op <Latitude> - <Latitude> = <LatitudeDiff>
|
||||||
|
op <Longitude> - <Longitude> = <LongitudeDiff>
|
||||||
|
|
||||||
|
// Simple custom type with a constraint
|
||||||
|
type Age<int + (0 <= _ < 150)>
|
||||||
|
|
||||||
|
// Predefined custom constraints that can be referenced in other definitions
|
||||||
|
constraint Positive = _ >= 0
|
||||||
|
constraint StrictlyPositive = _ > 0
|
||||||
|
constraint Even = _ % 2 == 0
|
||||||
34
examples/00_syntax_prototype/02_custom_types.py
Normal file
34
examples/00_syntax_prototype/02_custom_types.py
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
# type: ignore
|
||||||
|
# ruff: disable[F821]
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
# Prototype of custom type import to use valid Python syntax
|
||||||
|
import midas
|
||||||
|
midas.using("02_custom_types.midas")
|
||||||
|
|
||||||
|
# A data-frame using a custom type
|
||||||
|
df: Frame[
|
||||||
|
location: GeoLocation
|
||||||
|
]
|
||||||
|
|
||||||
|
# Properties of a type can be used on a column of that type
|
||||||
|
lat: Column[GeoLocation] = df["location"].lat
|
||||||
|
lon: Column[GeoLocation] = df["location"].lon
|
||||||
|
|
||||||
|
# Unregistered operations between types are not permitted
|
||||||
|
lat + lon # Invalid operation
|
||||||
|
|
||||||
|
# Registered operations are permitted
|
||||||
|
lat1: Latitude = lat[0]
|
||||||
|
lat2: Latitude = lat[1]
|
||||||
|
lat_diff: LatitudeDiff = lat2 - lat1 # Valid operation
|
||||||
|
|
||||||
|
# In addition to the type, a column can have one or more constraints, either defined inline or in a separate file
|
||||||
|
df2: Frame[
|
||||||
|
age: int + (_ >= 0),
|
||||||
|
height: float + (_ >= 0),
|
||||||
|
]
|
||||||
|
df2_bis: Frame[
|
||||||
|
age: int + Positive,
|
||||||
|
height: float + Positive,
|
||||||
|
]
|
||||||
Reference in New Issue
Block a user