| 1 | from rdfalchemy import rdfSubject, rdfSingle, rdfMultiple |
|---|
| 2 | from rdflib import ConjunctiveGraph, Namespace, Literal |
|---|
| 3 | |
|---|
| 4 | import logging |
|---|
| 5 | log = logging.getLogger() |
|---|
| 6 | log.addHandler(logging.StreamHandler()) |
|---|
| 7 | log.setLevel(logging.DEBUG) |
|---|
| 8 | |
|---|
| 9 | OV = Namespace('http://owl.openvest.org/2005/10/Portfolio#') |
|---|
| 10 | VCARD = Namespace("http://www.w3.org/2006/vcard/ns#") |
|---|
| 11 | |
|---|
| 12 | rdfSubject.db = ConjunctiveGraph() |
|---|
| 13 | rdfSubject.db.load('./data/example.n3', format='n3') |
|---|
| 14 | |
|---|
| 15 | class Company(rdfSubject): |
|---|
| 16 | rdf_type = OV.Company |
|---|
| 17 | symbol = rdfSingle(OV.symbol,'symbol') |
|---|
| 18 | cik = rdfSingle(OV.secCik,'cik') |
|---|
| 19 | companyName = rdfSingle(OV.companyName) |
|---|
| 20 | address = rdfSingle(VCARD.adr) |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | # Above here would typically go in a model.py file and be imported |
|---|
| 24 | ########################################################################## |
|---|
| 25 | # Below here goes in the file with business logic agnostic of persistance |
|---|
| 26 | |
|---|
| 27 | c = Company.get_by(symbol='IBM') |
|---|
| 28 | ## this will enable us to see that the reads are cached |
|---|
| 29 | import logging |
|---|
| 30 | log=logging.getLogger('rdfAlchemy') |
|---|
| 31 | ## comment out to quite debug messages |
|---|
| 32 | log.setLevel(logging.DEBUG) |
|---|
| 33 | |
|---|
| 34 | ## list Companies |
|---|
| 35 | for c in Company.ClassInstances(): |
|---|
| 36 | print "%s has an SEC symbol of %s" % (c.companyName, c.cik) |
|---|
| 37 | print '' |
|---|
| 38 | |
|---|
| 39 | c = Company.get_by(symbol = 'IBM') |
|---|
| 40 | |
|---|
| 41 | ## Add a descriptor on the fly |
|---|
| 42 | Company.stockDescription = rdfSingle(OV.stockDescription,'stockDescription') |
|---|
| 43 | |
|---|
| 44 | print "%s: %s"%(c.companyName,c.stockDescription) |
|---|
| 45 | print " same as" |
|---|
| 46 | print "%s: %s"%(c[OV.companyName],c[OV.stockDescription]) |
|---|
| 47 | |
|---|
| 48 | print "## CHECK to see if multiple reads cause database reads" |
|---|
| 49 | print " you should see no non-blank lines between here\n" |
|---|
| 50 | s = "%s: %s"%(c.companyName,c.stockDescription) |
|---|
| 51 | s = "%s: %s"%(c.companyName,c.stockDescription) |
|---|
| 52 | print "\n and here" |
|---|
| 53 | |
|---|
| 54 | c = Company.get_by(symbol = 'IBM') |
|---|
| 55 | print " and exactly the same number of non-blank lines between here\n" |
|---|
| 56 | s = "%s: %s"%(c.companyName,c.stockDescription) |
|---|
| 57 | print "\n and here" |
|---|
| 58 | |
|---|
| 59 | c = Company.get_by(symbol = 'IBM') |
|---|
| 60 | print " and here\n" |
|---|
| 61 | s = "%s: %s"%(c.companyName,c.stockDescription) |
|---|
| 62 | s = "%s: %s"%(c.companyName,c.stockDescription) |
|---|
| 63 | s = "%s: %s"%(c.companyName,c.stockDescription) |
|---|
| 64 | print "\n and here" |
|---|
| 65 | |
|---|
| 66 | ## add another descriptor on the fly |
|---|
| 67 | Company.industry = rdfSingle(OV.yindustry,'industry') |
|---|
| 68 | |
|---|
| 69 | ## add an attribute (from the database) |
|---|
| 70 | c = Company.get_by(symbol = 'JAVA') |
|---|
| 71 | c.industry = 'Computer stuff' |
|---|
| 72 | |
|---|
| 73 | ## delete an attribute (from the database) |
|---|
| 74 | c = Company.get_by(symbol = 'IBM') |
|---|
| 75 | del c.industry |
|---|
| 76 | |
|---|
| 77 | # write out the new n3 file to see the changes |
|---|
| 78 | c.db.serialize('example-out.n3',format='n3') |
|---|