In [1]:
from datomic import *

In [2]:
# Connect to the database
db = DB('localhost', 8080, 'free', 'tvdata')

In [3]:
# Review the database information
db.info()


Out[3]:
{'basis-t': 1010, 'db/alias': 'free/tvdata'}

In [4]:
# Start a new transaction
tx = db.tx()

In [5]:
# Add a person to our database
person = tx.add("person/", {'name': "Wanda Sykes"})

In [6]:
# Demonstrate that our person has a temp id
# In Datomic, all temp ids are negative
print(person)


{'db/id': -1}

In [7]:
# Commit our transaction
tx.execute()


Out[7]:
{'db-after': {'basis-t': 1014, 'db/alias': 'free/tvdata'},
 'db-before': {'basis-t': 1010, 'db/alias': 'free/tvdata'},
 'tempids': {-9223350046622220289: 17592186045431},
 'tx-data': [{'a': 50,
   'added': True,
   'e': 13194139534326,
   'tx': 13194139534326,
   'v': datetime.datetime(2014, 2, 11, 0, 21, 33, 683000, tzinfo=<UTC>)},
  {'a': 66,
   'added': True,
   'e': 17592186045431,
   'tx': 13194139534326,
   'v': 'Wanda Sykes'}]}

In [8]:
# Retrieve our person, post commit
db.e(person)


<<< fetched entity {'db/id': 17592186045431} in 23.784ms
Out[8]:
{'db/id': 17592186045431, 'person/name': 'Wanda Sykes'}

In [9]:
# List all the people in our database
db.q('[:find ?p :where [_ :person/name ?p]]')


Out[9]:
[['Susie Essman'],
 ['Jeff Garlin'],
 ['Wanda Sykes'],
 ['Richard Lewis'],
 ['Bob Einstein'],
 ['Larry David'],
 ['Cheryl Hines'],
 ['J.B. Smoove']]

In [10]:
# Get the name of our newly added person
db.e(person).get('person/name')


<<< fetched entity {'db/id': 17592186045431} in 9.18ms
Out[10]:
'Wanda Sykes'

In [11]:
# Retract the person with the name 'Wanda Sykes' from our database
db.retract(person, 'person/name', 'Wanda Sykes')


<<< retracted {'db/id': 17592186045431},person/name,Wanda Sykes in 22.201ms
Out[11]:
{'db-after': {'basis-t': 1016, 'db/alias': 'free/tvdata'},
 'db-before': {'basis-t': 1014, 'db/alias': 'free/tvdata'},
 'tempids': {},
 'tx-data': [{'a': 50,
   'added': True,
   'e': 13194139534328,
   'tx': 13194139534328,
   'v': datetime.datetime(2014, 2, 11, 0, 22, 21, 27000, tzinfo=<UTC>)},
  {'a': 66,
   'added': False,
   'e': 17592186045431,
   'tx': 13194139534328,
   'v': 'Wanda Sykes'}]}

In [12]:
# Get the list of all people in our database, post retraction
people = db.q('[:find ?p :where [_ :person/name ?p]]')

In [13]:
# "people" is just a list of lists of people, get its length
len(people)


Out[13]:
7

In [14]:
# pull the names out into a new list "name_list"
name_list = [name[0] for name in people]

In [15]:
name_list


Out[15]:
['Susie Essman',
 'Jeff Garlin',
 'Richard Lewis',
 'Bob Einstein',
 'Larry David',
 'Cheryl Hines',
 'J.B. Smoove']

In [16]:
# filter out names that contain the letter "e"
filter(lambda x: x.find('e') > -1, name_list)


Out[16]:
['Susie Essman',
 'Jeff Garlin',
 'Richard Lewis',
 'Bob Einstein',
 'Cheryl Hines',
 'J.B. Smoove']

In [ ]: