From 3cf3011160eda0475e1fea9ee80d9a8c02360cb3 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Wed, 13 May 2026 14:36:51 +0200 Subject: [PATCH] feat: add some syntax examples --- .../00_syntax_prototype/01_simple_types.py | 16 +++++++++ .../00_syntax_prototype/02_custom_types.midas | 24 +++++++++++++ .../00_syntax_prototype/02_custom_types.py | 34 +++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 examples/00_syntax_prototype/01_simple_types.py create mode 100644 examples/00_syntax_prototype/02_custom_types.midas create mode 100644 examples/00_syntax_prototype/02_custom_types.py diff --git a/examples/00_syntax_prototype/01_simple_types.py b/examples/00_syntax_prototype/01_simple_types.py new file mode 100644 index 0000000..cfb10aa --- /dev/null +++ b/examples/00_syntax_prototype/01_simple_types.py @@ -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 +] diff --git a/examples/00_syntax_prototype/02_custom_types.midas b/examples/00_syntax_prototype/02_custom_types.midas new file mode 100644 index 0000000..ba8b758 --- /dev/null +++ b/examples/00_syntax_prototype/02_custom_types.midas @@ -0,0 +1,24 @@ +// Simple custom type derived from floats +type Latitude +type Longitude + +// Complex custom type, containing two values accessible through properties +type GeoLocation { + lat: Latitude + lon: Longitude +} + +type LatitudeDiff +type LongitudeDiff + +// Simple operation defined on our custom types +op - = +op - = + +// Simple custom type with a constraint +type Age + +// Predefined custom constraints that can be referenced in other definitions +constraint Positive = _ >= 0 +constraint StrictlyPositive = _ > 0 +constraint Even = _ % 2 == 0 \ No newline at end of file diff --git a/examples/00_syntax_prototype/02_custom_types.py b/examples/00_syntax_prototype/02_custom_types.py new file mode 100644 index 0000000..0297058 --- /dev/null +++ b/examples/00_syntax_prototype/02_custom_types.py @@ -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, +]