Search...
ctrl/
Light
Dark
System
Sign in

Datatypes​

gel-python automatically converts Gel types to the corresponding Python types and vice versa.

The table below shows the correspondence between Gel and Python types.

Gel Type

Python Type

Set

gel.Set

array<anytype>

gel.Array

anytuple

gel.Tuple or gel.NamedTuple

anyenum

gel.EnumValue

Object

gel.Object

bool

bool

bytes

bytes

str

str

cal::local_date

datetime.date

cal::local_time

offset-naive datetime.time

cal::local_datetime

offset-naive datetime.datetime

cal::relative_duration

gel.RelativeDuration

cal::date_duration

gel.DateDuration

datetime

offset-aware datetime.datetime

duration

datetime.timedelta

float32, float64

float

int16, int32, int64, bigint

int

decimal

Decimal

json

str

uuid

uuid.UUID

Inexact single-precision float values may have a different representation when decoded into a Python float. This is inherent to the implementation of limited-precision floating point types. If you need the decimal representation to match, cast the expression to float64 or decimal in your query.

class

Set

This is list since version 1.0.

class

Object

An immutable representation of an object instance returned from a query.

gel.Object instances are dataclass-compatible since version 1.0, for example, dataclasses.is_dataclass() will return True, and dataclasses.asdict() will work on gel.Object instances.

gel.Object.__hash__ is just object.__hash__ in version 1.0. Similarly, == is equivalent to the is operator comparing gel.Object instances, and <, <=, >, >= are not allowed on gel.Object instances.

The value of an object property or a link can be accessed through a corresponding attribute:

Copy
>>> 
import gel
Copy
>>> 
client = gel.create_client()
Copy
>>> 
... 
... 
... 
r = client.query_single('''
    SELECT schema::ObjectType {name}
    FILTER .name = 'std::Object'
    LIMIT 1''')
Copy
>>> 
r
Object{name := 'std::Object'}
Copy
>>> 
r.name
'std::Object'

interface

obj[linkname]

Return a gel.Link or a gel.LinkSet instance representing the instance(s) of link linkname associated with obj.

Example:

Copy
>>> 
import gel
Copy
>>> 
client = gel.create_client()
Copy
>>> 
... 
... 
... 
... 
r = client.query_single('''
    SELECT schema::Property {name, annotations: {name, @value}}
    FILTER .name = 'listen_port'
           AND .source.name = 'cfg::Config'
    LIMIT 1''')
Copy
>>> 
r
Object {
    name: 'listen_port',
    annotations: {
        Object {
            name: 'cfg::system',
            @value: 'true'
        }
    }
}
Copy
>>> 
r['annotations']
LinkSet(name='annotations')
Copy
>>> 
l = list(r['annotations])[0]
Copy
>>> 
l.value
'true'

class

Tuple

This is tuple since version 1.0.

class

NamedTuple

An immutable value representing an Gel named tuple value.

gel.NamedTuple is a subclass of tuple and is duck-type compatible with collections.namedtuple since version 1.0.

Instances of gel.NamedTuple generally behave similarly to namedtuple:

Copy
>>> 
import gel
Copy
>>> 
client = gel.create_client()
Copy
>>> 
r = client.query_single('''SELECT (a := 1, b := 'a', c := [3])''')
Copy
>>> 
r
(a := 1, b := 'a', c := [3])
Copy
>>> 
r.b
'a'
Copy
>>> 
r[0]
1
Copy
>>> 
r == (1, 'a', [3])
True
Copy
>>> 
r._fields
('a', 'b', 'c')

class

Array

This is list since version 1.0.

class

RelativeDuration

An immutable value representing an Gel cal::relative_duration value.

Copy
>>> 
import gel
Copy
>>> 
client = gel.create_client()
Copy
>>> 
r = client.query_single('''SELECT <cal::relative_duration>"1 year 2 days 3 seconds"''')
Copy
>>> 
r
<gel.RelativeDuration "P1Y2DT3S">
Copy
>>> 
r.months
12
Copy
>>> 
r.days
2
Copy
>>> 
r.microseconds
3000000

class

DateDuration

An immutable value representing an Gel cal::date_duration value.

Copy
>>> 
import gel
Copy
>>> 
client = gel.create_client()
Copy
>>> 
r = client.query_single('''SELECT <cal::date_duration>"1 year 2 days"''')
Copy
>>> 
r
<gel.DateDuration "P1Y2D">
Copy
>>> 
r.months
12
Copy
>>> 
r.days
2

class

EnumValue

An immutable value representing an Gel enum value.

Since version 1.0, gel.EnumValue is a subclass of enum.Enum. Actual enum values are instances of ad-hoc enum classes created by the codecs to represent the actual members defined in your Gel schema.

Copy
>>> 
import gel
Copy
>>> 
client = gel.create_client()
Copy
>>> 
r = client.query_single("""SELECT <Color>'red'""")
Copy
>>> 
r
<gel.EnumValue 'red'>
Copy
>>> 
str(r)
'red'
Copy
>>> 
r.value  # added in 1.0
'red'
Copy
>>> 
r.name  # added in 1.0, simply str.upper() of r.value
'RED'