Apply Attributes
This page provides details on how to apply attributes in PyKX.
Tip: For the best experience, we recommend reading about PyKX attributes first.
In PyKX, you can apply attributes to various data structures, including Vector/List types, Tables, and KeyedTables. To apply the attributes, call the sorted, unique, grouped, and parted methods on these objects.
Sorted
The sorted attribute ensures that all items in the Vector / Table column are sorted in ascending
order. This attribute will be removed if you append to the list with an item that is not in sorted
order.
Example of applying the sorted attribute to a Vector by calling the sorted method on the Vector:
>>> a = kx.q.til(10)
>>> a
pykx.LongVector(pykx.q('0 1 2 3 4 5 6 7 8 9'))
>>> a.sorted()
pykx.LongVector(pykx.q('`s#0 1 2 3 4 5 6 7 8 9'))
Unique
The unique attribute ensures that all items in the Vector / Table column are unique (there are
no duplicated values). This attribute will be removed if you append to the list with an item that
is not unique.
Example of applying the unique attribute to the first column of the table:
>>> a = kx.Table(data = {
... 'a': kx.q.til(5),
... 'b': ['a', 'b', 'c', 'd', 'e']
... })
>>> kx.q.meta(a)
pykx.KeyedTable(pykx.q('
c| t f a
-| -----
a| j
b| s
'))
>>> a = a.unique()
>>> kx.q.meta(a)
pykx.KeyedTable(pykx.q('
c| t f a
-| -----
a| j u
b| s
'))
Grouped
The grouped attribute ensures that all items in the Vector / Table column are stored in a
different format to help reduce memory usage. It creates a backing dictionary to store the value and
indexes that each value has within the list.
Unlike other attributes, the grouped attribute will be kept on all insert operations to the list. For instance, this is how a grouped list would be stored:
// The list
`g#`a`b`c`a`b`b`c
// The backing dictionary
a| 0 3
b| 1 4 5
c| 2 6
Example of applying the grouped attribute to a specified column of a table:
>>> a = kx.Table(data = {
... 'a': kx.q.til(5),
... 'b': ['a', 'a', 'b', 'b', 'b']
... })
>>> kx.q.meta(a)
pykx.KeyedTable(pykx.q('
c| t f a
-| -----
a| j
b| s
'))
>>> a = a.grouped('b')
>>> kx.q.meta(a)
pykx.KeyedTable(pykx.q('
c| t f a
-| -----
a| j
b| s g
'))
Parted
The parted attribute is similar to the grouped attribute with the additional requirement that each unique value must be adjacent to its other copies, where the grouped attribute allows them to be dispersed throughout the Vector / Table.
When possible, the parted attribute results in a larger performance gain than using the grouped attribute. This attribute will be removed if you append to the list with an item that is not in the parted
order.
// Can be parted
`p#`a`a`a`e`e`b`b`c`c`c`d
// Has to be grouped as the `d symbols are not all contiguous within the vector
`g#`a`a`d`e`e`b`b`c`c`c`d
Example of applying the parted attribute to multiple columns on a table:
>>> a = kx.Table(data = {
... 'a': kx.q.til(5),
... 'b': ['a', 'a', 'b', 'b', 'b']
... })
>>> kx.q.meta(a)
pykx.KeyedTable(pykx.q('
c| t f a
-| -----
a| j
b| s
'))
>>> a = a.parted(['a', 'b'])
>>> kx.q.meta(a)
pykx.KeyedTable(pykx.q('
c| t f a
-| -----
a| j p
b| s p
'))
Performance
When attributes are set on PyKX objects, various functions can use these attributes to speed up their
execution, by using different algorithms. For example, searching through a list without an attribute
requires checking every single value. However, setting the sorted attribute allows a search algorithm
to use a binary search instead and then only a fraction of the values actually needs to be checked.
Examples of functions that can use attributes to speed up execution:
- Where clauses in
selectandexectemplates run faster withwhere =,where inandwhere within. - Searching with
bin,distinct,Findandin. - Sorting with
iascoridesc.
Note
Setting attributes consumes resources and is likely to improve performance on large lists.