Tag Archives: entities

Epiphany Queries

Python Epiphany collection classes can be queried in a way that is reminiscent of SQL WHERE clauses, except with the full expressive power of Python. Let’s create a deities collection class and a corresponding deity entity class to set up a demonstration.


class deities(entities):
    pass

class deity(entity):
    def __init__(self, name, gender):
        self.name = name
        self.gender = gender

Here, deities inherits from entities making it a collection class. deity objects will obviously be appended to this collection class. Each deity object will store the deities name and gender.

Let’s start by using our classes to build up a pantheon of the major Greek gods and goddesses.


pantheon = deities()
pantheon  +=  deity('Aphrodite',   'female')
pantheon  +=  deity('Apollo',      'male')
pantheon  +=  deity('Ares',        'male')
pantheon  +=  deity('Artemis',     'female')
pantheon  +=  deity('Athena',      'female')
pantheon  +=  deity('Demeter',     'female')
pantheon  +=  deity('Hades',       'male')
pantheon  +=  deity('Hephaestus',  'male')
pantheon  +=  deity('Hera',        'female')
pantheon  +=  deity('Hermes',      'male')
pantheon  +=  deity('Hestia',      'female')
pantheon  +=  deity('Poseidon',    'male')
pantheon  +=  deity('Zeus',        'male')

Now, using the pantheon collection object, lets create gods and goddesses collections by querying the pantheon collection.


gods = pantheon.where(lambda x: x.gender == 'male')
goddesses = pantheon.where(lambda x: x.gender == 'female')

assert isinstance(gods, entities)
assert isinstance(goddesses, entities)

assert gods.count == 7
assert goddesses.count == 6

The first line uses the where method to collect all the deities that are male. The second line does the same for female deities. A lambda object is used as the query, though any Python callable, such as a function, could be used. Lambdas have the advantage of being terse.

Later we can assert that the where method returns a generic entities class . Ideally, where would have created and returned a deities class in this example. However, that feature hasn’t been implemented yet.

Finally, we make use of the queries to observe that there are 7 male deities and 6 female deities in our original pantheon collection.

In the above queries, only one property, gender, was tested for equality. However, we can make the test as expressive as any Python expression can be. Let’s say we want to see how many male deities are in our pantheon collection whose name starts with the letter ‘A’.


gods = pantheon.where(lambda x: x.name[0] == 'A' and x.gender == 'male')
assert gods.count == 2
assert gods[0].name == 'Apollo'
assert gods[1].name == 'Ares'

Here we see it’s no problem to use Python’s string indexing as well as the and operator to create a slightly more complex query. Of course, very complicated queries with complex nested Boolean expressions can be used as well. We can see that two deities are returned: the first being ‘Apollo’ and the second being ‘Ares’.

Appending to and retrieving objects from commonpy entities

This tutorial will get you started subclassing the ‘entities’ class in commonpy to make array-like classes. Checkout version 1.1 to follow along.

First, let’s get some subclasses defined.


from entities import *
class gods(entities):
    pass

class god(entity):
    def __init__(self, name):
        self.name = name

    def __str__(self):
        return self.name

Here, our array-like class is gods since it inherits from entities. The second class, god inherits from entity. Instances of this class will will be elements of the gods. It doesn’t have to inherit from entity but it helps and one operations is impossible unless it does (we’ll see which one below).

Let’s create some gods and append them to the gods collection.


norse_pantheon = gods()
assert norse_pantheon.count == 0

thor = god('Thor')
odin = god('Odin')

norse_pantheon.append(thor)
norse_pantheon.append(odin)
assert norse_pantheon.count == 2

After instantiating our norse_pantheon collection, we can assert that its count property is 0 since we haven’t added any elements yet. Next we create thor and odin objects and append them to the norse_pantheon. Now the count property is 2.

The append method is only one way to add objects to our collection. Let’s clear the collection and try some other ways.


norse_pantheon.clear()
assert norse_pantheon.count == 0
assert norse_pantheon.isempty

norse_pantheon += thor
norse_pantheon += odin
assert norse_pantheon.count == 2

The clear method method empties the collection. Now we can assert that the count proprety is 0 and norse_pantheon.isempty is True.

Next we append thor and oden to the collection again, this time using the += operator.

The append method and the += operator do the same thing here. I prefer += since it reduces the number of characters on the line. Once you get used to it, it’s easier to read.

There is one more method to append object to entities collection which is very useful in some circumstances.


norse_pantheon.clear()
norse_pantheon += thor + odin
assert norse_pantheon.count == 2

Here, thor and odin are added together to make an entities collection which is then append to the norse_pantheon collection. Convenient, but for this to work, thor and odin had to inherit from the entity class.

So far, we’ve added to the collection. Now, let’s explore ways to get the objects out of the collection. Here are three ways to do the same thing.


assert norse_pantheon[0] is thor
assert norse_pantheon[1] is odin

assert norse_pantheon['Thor'] is thor
assert norse_pantheon['Odin'] is odin

assert norse_pantheon.first is thor
assert norse_pantheon.second is odin

The first uses the collection indexer to to get the object out by it’s index number. The first object added to a collection will be indexed at 0, the second will be index at 1 and so on.

The second way is to use the name property as the index. Since we instantiated the god objects so their name properties would be ‘Thor’ and ‘Odin’ (see the constructor), we can obtain them by their name value.

The third way is to use the first and second properties. (There are also third, fourth and fifth properties. At the moment, there is no onehundredandseventyseventh property but I am looking in to a way to do that)

We can also iterate over the collection.


for g in norse_pantheon:
    print(g.name)

Since we added the __str__ method to the god class, we can do this and it will do the same as the above.


print(norse_pantheon)

Appending to and retrieving objects from Epiphany Py entities

This tutorial will get you started subclassing the ‘entities’ class in Epiphany Py to make array-like classes. Checkout version 1.1 to follow along.

First, let’s get some subclasses defined.


from entities import *
class gods(entities):
    pass

class god(entity):
    def __init__(self, name):
        self.name = name

    def __str__(self):
        return self.name

Here, our array-like class is gods since it inherits from entities. The second class, god inherits from entity. Instances of this class will will be elements of the gods. It doesn’t have to inherit from entity but it helps and one operations is impossible unless it does (we’ll see which one below).

Let’s create some gods and append them to the gods collection.


norse_pantheon = gods()
assert norse_pantheon.count == 0

thor = god('Thor')
odin = god('Odin')

norse_pantheon.append(thor)
norse_pantheon.append(odin)
assert norse_pantheon.count == 2

After instantiating our norse_pantheon collection, we can assert that its count property is 0 since we haven’t added any elements yet. Next we create thor and odin objects and append them to the norse_pantheon. Now the count property is 2.

The append method is only one way to add objects to our collection. Let’s clear the collection and try some other ways.


norse_pantheon.clear()
assert norse_pantheon.count == 0
assert norse_pantheon.isempty

norse_pantheon += thor
norse_pantheon += odin
assert norse_pantheon.count == 2

The clear method method empties the collection. Now we can assert that the count proprety is 0 and norse_pantheon.isempty is True.

Next we append thor and oden to the collection again, this time using the += operator.

The append method and the += operator do the same thing here. I prefer += since it reduces the number of characters on the line. Once you get used to it, it’s easier to read.

There is one more method to append object to entities collection which is very useful in some circumstances.


norse_pantheon.clear()
norse_pantheon += thor + odin
assert norse_pantheon.count == 2

Here, thor and odin are added together to make an entities collection which is then append to the norse_pantheon collection. Convenient, but for this to work, thor and odin had to inherit from the entity class.

So far, we’ve added to the collection. Now, let’s explore ways to get the objects out of the collection. Here are three ways to do the same thing.


assert norse_pantheon[0] is thor
assert norse_pantheon[1] is odin

assert norse_pantheon['Thor'] is thor
assert norse_pantheon['Odin'] is odin

assert norse_pantheon.first is thor
assert norse_pantheon.second is odin

The first uses the collection indexer to to get the object out by it’s index number. The first object added to a collection will be indexed at 0, the second will be index at 1 and so on.

The second way is to use the name property as the index. Since we instantiated the god objects so their name properties would be ‘Thor’ and ‘Odin’ (see the constructor), we can obtain them by their name value.

The third way is to use the first and second properties. (There are also third, fourth and fifth properties. At the moment, there is no onehundredandseventyseventh property but I am looking in to a way to do that)

We can also iterate over the collection.


for g in norse_pantheon:
    print(g.name)

Since we added the __str__ method to the god class, we can do this and it will do the same as the above.


print(norse_pantheon)