relationalai.dsl.Rows
class Rows
Represents a set of rows of data.
Rows
objects are created using the as_rows()
function and can be used to pass data from Python to the model without having to define a Type
or use a dynamic rule or query context.
Example
Section titled “Example”Use as_rows()
to convert a list or dictionary of data to a Rows
object:
import relationalai as raifrom relationalai.std import as_rows
# =====# SETUP# =====
model = rai.Model("MyModel")Person = model.Type("Company")
# =======# EXAMPLE# =======
# Define a Rows object from a list of dictionaries.data_row = as_rows([ {"id": 1, "name": "Alice", "age": 30}, {"id": 2, "name": "Bob", "age": 45},])
# Use data_row to define Person objects in the model.with model.rule(): Person.add(id=data_row.id).set(name=data_row.name, age=data_row.age)
# Query the data.with model.query() as select: person = Person() response = select(person.id, person.name, person.age)
print(response.results)# id name age# 0 1 Alice 30# 1 2 Bob 45
Methods
Section titled “Methods”Name | Description | Return Type |
---|---|---|
.__getitem__() | Gets an Expression object representing a column in the Rows object using subscript notation. | Expression |
.__getattr__() | Gets an Expression object representing a column in the Rows object using dot notation. | Expression |
.__getattr__()
Section titled “.__getattr__()”Rows.__getattr__(name: str) -> Expression
Gets an Expression
object representing a column in the Rows
object by its key using dot notation.
Only works if the Rows
object was created with as_rows()
from a list of dictionaries.
Parameters
Section titled “Parameters”Name | Type | Description |
---|---|---|
name | str | The string key of the column to get from the Rows object. |
Returns
Section titled “Returns”An Expression
object.
Example
Section titled “Example”Rows.__getattr__()
allows you to access columns in a Rows
object by their string key using dot notation:
import relationalai as raifrom relationalai.std import as_rows
# =====# SETUP# =====
model = rai.Model("MyModel")Person = model.Type("Person")
# =======# EXAMPLE# =======
# Define Person objects from a list of dictionaries.with model.rule(): row = as_rows([ {"id": 1, "name": "Alice", "age": 30}, {"id": 2, "name": "Bob", "age": 45}, ]) Person.add(id=row.id).set(name=row.name, age=row.age) # Access by key name
.__getitem__()
Section titled “.__getitem__()”Rows.__getitem__(index: int|str) -> Expression
Gets an Expression
object representing a column in the Rows
object using subscript notation.
If Rows
was created from a list of tuples, then index
must be an integer index.
If Rows
was created from a list of dictionaries, then index
must be a string key.
Parameters
Section titled “Parameters”Name | Type | Description |
---|---|---|
index | int or str | The integer index or string key of the column to get from the Rows object. |
Returns
Section titled “Returns”An Expression
object.
Example
Section titled “Example”Rows.__getitem__()
allows you to access columns in a Rows
object using subscript notation:
import relationalai as raifrom relationalai.std import as_rows
# =====# SETUP# =====
model = rai.Model("MyModel")Company = model.Type("Company")Person = model.Type("Person")
# =======# EXAMPLE# =======
# Define Company objects from a list of tuples.with model.rule(): row = as_rows([(1, "RelationalAI"), (2, "Snowflake")]) Company.add(id=row[0]).set(name=row[1])
# Define Person objects from a list of dictionaries.with model.rule(): row = as_rows([ {"id": 1, "name": "Alice", "age": 30}, {"id": 2, "name": "Bob", "age": 45}, ]) Person.add(id=row["id"]).set(name=row["name"], age=row["age"])