root / rdfalchemy / trunk / rdfalchemy / samples / example.py

Revision 99, 2.4 kB (checked in by phil, 11 months ago)

fixed a bug in ZODB create_engine

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