python开发的软件sparrow-sparrow · PyPI

Usage

Normally, you will only need to import the base sparrow module

>>> import sparrow

Most of the database backends will not work out of the box.

Since the RDFLib backend is written in python and packaged on pypi,

it is always available, and installed with Sparrow.

Let’s create an in memory rdflib database

>>> db = sparrow.database('rdflib', 'memory')

>>> db

Let’s add some triples to the database, we will use turtle syntax for this.

We’ll make some example statements where we will state that john is a person,

and that his firstname is “John”.

>>> data = """@prefix ex: .

... ex:john a ex:Person; ex:name "John" ."""

Now we can add this to the database. We will need to tell the database in

which context to store the data. The data itself can be either a file or http

based URI, a string of data, or a file-like object.

So, let’s add this to the persons context.

>>> db.add_turtle(StringIO(data), 'persons')

We can now ask the database, which contexts it has:

>>> db.contexts()

[u'persons']

You can store data in as many different contexts as you like, or put everything

in a single context.

Lets do a simple SPARQL query on the database

>>> result = db.select('SELECT ?x {?x "John".}')

We can get the results as a list of dictionaries. This follows the SPARQL

JSON result format.

>>> result

[{u'x': {'type': u'uri', 'value': u'http://example.org#john'}}]

Besides querying, we can also get the data back from the database in any

of the supported formats. We specify which format we want, and which context

to use.

>>> db.get_ntriples('persons').read()

' ...'

If the database backend supports it, you can ask how many triples are in a

context.

>>> db.count('persons')

2

If you want to remove triples, you will need to supply data describing which

triples to remove.

>>> data = ' a .'

>>> db.remove_turtle(data, 'persons')

>>> db.count('persons')

1

You can also remove all triples in a context

>>> db.clear('persons')

>>> db.count('persons')

0

Since the "persons’ context is now empty, it is also removed.

>>> db.contexts()

[]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值