• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Python query.select函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中surf.query.select函数的典型用法代码示例。如果您正苦于以下问题:Python select函数的具体用法?Python select怎么用?Python select使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了select函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: __get_by_subquery

    def __get_by_subquery(self, params):
        context = params.get("context", None)

        inner_query = select("?s")
        inner_params = params.copy()
        if "order" in params:
            # "order" needs to stay in subquery,
            # but doesn't do anything useful in main query
            del params["order"]
        self.__apply_limit_offset_order_get_by_filter(inner_params, inner_query)

        query = select("?s", "?p", "?v", "?c").distinct()
        query.group(('?s', '?p', '?v'), optional_group(('?v', a, '?c')))
        query.where(inner_query)
        if not (context is None):
            query.from_(context)

        # Need ordering in outer query
        if "order" in params:
            if params["order"] == True:
                # Order by subject URI
                query.order_by("?s")
            else:
                # Match another variable, order by it
                query.optional_group(("?s", params["order"], "?order"))
                query.order_by("?order")

        table = self._to_table(self._execute(query))
        subjects = {}
        results = []
        for match in table:
            # Make sure subject and predicate are URIs (they have to be!),
            # this works around bug in Virtuoso -- it sometimes returns
            # URIs as Literals.
            subject = URIRef(match["s"])
            predicate = URIRef(match["p"])
            value = match["v"]

            # Add subject to result list if it's not there
            if not subject in subjects:
                instance_data = {"direct" : {}}
                subjects[subject] = instance_data
                results.append((subject, instance_data))

            # Add predicate to subject's direct predicates if it's not there
            direct_attributes = subjects[subject]["direct"]
            if not predicate in direct_attributes:
                direct_attributes[predicate] = {}

            # Add value to subject->predicate if ...
            predicate_values = direct_attributes[predicate]
            if not value in predicate_values:
                predicate_values[value] = []

            # Add RDF type of the value to subject->predicate->value list
            if "c" in match:
                predicate_values[value].append(match["c"])

        return results
开发者ID:ceberhardt,项目名称:surf,代码行数:59,代码来源:query_reader.py


示例2: get_schema_label

def get_schema_label(uri,prefix,format):
    #store = surf.Store(reader='rdflib',writer='rdflib',rdflib_store='IOMemory')
    store = surf.Store(**{"reader": "librdf", "writer" : "librdf", })
    session = surf.Session(store)
    try:
        store.load_triples(source='file://'+settings.PROJECT_PATH+'/d2rq/vocab/'+prefix+'.'+format)
        print "Loading from file : "+prefix+"."+format
    except:
        store.load_triples(source=uri)
        print "Loading from Internet : "+uri
    #store.enable_logging(True)
    ontology = session.get_class(ns.OWL.Ontology)
    if(ontology.all().first()): #contient un node Ontology
        print "Le schema est une ontologie OWL"
        vocab_type = 'owl'
        titles = ['rdfs_comment','rdfs_label','dc_title','dcterms_title']
        #import pdb; pdb.set_trace() 
        for o in ontology.all():
            for title in titles:
                if(len(o.__getattr__(title))>0):
                    lookup = literal_lang_select(o.__getattr__(title))
                    if(lookup != None):label=lookup
            if not label:
                label = unicode(uri)
    else: # là c'est la vraie methode, 
    #au-dessus c'est juste pour les OWL dont l'URL ≠ de l'URI sujet
    #donc pas forcément obligatoire à garder non plus
        try:
            print "Le schema est un vocabulaire RDFS"
            vocab_type = 'rdfs'
            label = get_label(rdflib.term.URIRef(uri),uri,store)
        except:
            raise
    lookup_args = { 'owl':  (ns.OWL.Class,ns.OWL.ObjectProperty),
                    'rdfs': (ns.RDFS.Class,ns.RDF.Property)}
    vocab_classes = list(store.reader._to_table(store.reader._execute(
                        select("?s").where(("?s", a, lookup_args[vocab_type][0])).filter('(regex(str(?s),"'+uri+'","i"))')# vocab own classes only
                        )))
    vocab_proprietes = list(store.reader._to_table(store.reader._execute(
                        select("?s").where(("?s", a, lookup_args[vocab_type][1])).filter('(regex(str(?s),"'+uri+'","i"))')
                        )))   
    cp = {'classes':[],'proprietes':[]}                        
    for items in ((vocab_classes,'classes'),(vocab_proprietes,'proprietes')):
        for triple in items[0]:
            if(isinstance(triple['s'],rdflib.term.URIRef) and bool(re.compile(uri).match(unicode(triple['s'])))): #ugly patch waiting for query to work
                clabel = get_label(triple['s'],uri,store) #on envoie le subject pour l'identifier
                cp[items[1]].append((unicode(triple['s']),clabel))
                print items[1]+':'+clabel
    store.clear()
    store.close()
    session.close()
    return((label,vocab_type,cp))
开发者ID:quinode,项目名称:django-d2rq,代码行数:52,代码来源:models.py


示例3: _classification_search

def _classification_search(query, start, limit, **kw):
    crdf = CignoRDF()
    rootNode = kw.get('node','').split('|')[-1]
    rootResource = crdf.Collections(rootNode)
    where = crdf.get_where_tree(rootResource)
    query_obj = select("?s").where(*where).distinct()
    resources = crdf.session.default_store.execute_sparql("%s" % query_obj)
    results = []
    for resource in resources['results']['bindings']:
      res = crdf.CignoResources(resource['s']['value'])
      result = {}
      result['title'] = res.rdfs_label.first.format()
      result['uuid'] = res.cigno_uuid.first.format()
      result['detail'] = res.subject.format()
      results.append(result)

    result = {'rows': results, 
              'total': len(results)}

    result['query_info'] = {
        'start': start,
        'limit': limit,
        'q': query
    }
    if start > 0: 
        prev = max(start - limit, 0)
        params = urlencode({'q': query, 'start': prev, 'limit': limit})
        result['prev'] = reverse('geonode.maps.views.metadata_search') + '?' + params

    next = 3
    if next > 0:
        params = urlencode({'q': query, 'start': next - 1, 'limit': limit})
        result['next'] = reverse('geonode.maps.views.metadata_search') + '?' + params
    
    return result
开发者ID:lucacorsato,项目名称:CIGNo,代码行数:35,代码来源:views_rdf.py


示例4: test_same_as_inference_works

    def test_same_as_inference_works(self):
        """ Test owl:sameAs inferencing. """

        store, session = self._get_store_session()
        self._create_persons(session)

        # Let's say Jonathan is the same Person as John
        Person = session.get_class(surf.ns.FOAF["Person"])
        john = session.get_resource("http://John", Person)
        john.load()

        jonathan = session.get_resource("http://Jonathan", Person)
        jonathan.foaf_homepage = 'http://example.com'

        john[surf.ns.OWL['sameAs']] = jonathan
        session.commit()

        store.reader.define = 'input:same-as "yes"'

        query = select("?s").from_(self.CONTEXT)\
                            .where((jonathan.subject, surf.ns.FOAF['name'], '?s'))
        r = store.execute_sparql(unicode(query))

        self.assertEquals(set(entry['s']['value'] for entry in r["results"]["bindings"]),
                          set([john.foaf_name[0]]))
开发者ID:nwebs,项目名称:surf.virtuoso_protocol,代码行数:25,代码来源:tests.py


示例5: __get_by_n_queries

    def __get_by_n_queries(self, params):
        context = params.get("context", None)

        query = select("?s")
        if not (context is None):
            query.from_(context)

        self.__apply_limit_offset_order_get_by_filter(params, query)

        # Load details, for now the simplest approach with N queries.
        # Use _to_table instead of convert to preserve order.
        results = []
        for match in self._to_table(self._execute(query)):
            subject = match["s"]
            instance_data = {}

            result = self._execute(query_S(subject, True, context))
            result = self.convert(result, 'p', 'v', 'c')
            instance_data["direct"] = result

            if not params.get("only_direct"):
                result = self._execute(query_S(subject, False, context))
                result = self.convert(result, 'p', 'v', 'c')
                instance_data["inverse"] = result

            results.append((subject, instance_data))

        return results
开发者ID:ceberhardt,项目名称:surf,代码行数:28,代码来源:query_reader.py


示例6: process

   def process(self, input, output):

      print 'processing ' + input.subject

      # prefix void: <http://rdfs.org/ns/void#>
      #
      # select ?from ?to ?overlap
      # where {
      #    ?from void:subset [ 
      #       a void:Linkset; 
      #       void:target  ?from, 
      #                    ?to; 
      #       void:triples ?overlap;
      #    ] .
      #    filter( ?from != ?to )
      # }
      query = select('?to ?overlap').where((input.subject, ns.VOID['subset'],  '?linkset'),
                                           ('?linkset',    a,                  ns.VOID['Linkset']),
                                           ('?linkset',    ns.VOID['target'],  input.subject),
                                           ('?linkset',    ns.VOID['target'],  '?to'),
                                           ('?linkset',    ns.VOID['triples'], '?overlap')).filter('(?to != <'+input.subject+'>)')
      results = input.session.default_store.execute(query)
      for binding in results:
         target  = binding[0]
         overlap = binding[1]
         #print target + ' ' + overlap
         if overlap >= 50: # "We arbitrarily require at least 50 links." -http://richard.cyganiak.de/2007/10/lod/
            output.rdf_type.append(ns.DATAFAQS['Satisfactory'])

      if ns.DATAFAQS['Satisfactory'] not in output.rdf_type:
         output.rdf_type.append(ns.DATAFAQS['Unsatisfactory'])

      output.save()
开发者ID:anukat2015,项目名称:DataFAQs,代码行数:33,代码来源:void-linkset.py


示例7: test_subquery

 def test_subquery(self):
     """ Try to produce query that contains subquery in WHERE clause. """
     
     expected = canonical(u"""
         SELECT ?s ?p ?o 
         WHERE { 
             ?s ?p ?o. 
             { SELECT ?s WHERE { ?s ?a ?b } LIMIT 3 }
         }
     """)
     
     subquery = select("?s").where(("?s", "?a", "?b")).limit(3)
     
     query = select("?s", "?p", "?o").where(("?s", "?p", "?o"), subquery)
     result = canonical(SparqlTranslator(query).translate())
     
     self.assertEqual(expected, result)
开发者ID:arleincho,项目名称:surf,代码行数:17,代码来源:test_sparql.py


示例8: test_from_none

def test_from_none():
    """
    Check that .from_(None) raises.
    """

    query = select("?s")
    with pytest.raises(ValueError):
        query.from_(None)
开发者ID:cosminbasca,项目名称:surfrdf,代码行数:8,代码来源:test_sparql.py


示例9: process

   def process(self, input, output):

      print 'processing ' + input.subject

      # Fails:
      #
      #PREFIX owl:        <http://www.w3.org/2002/07/owl#>
      #PREFIX dcterms:    <http://purl.org/dc/terms/>
      #PREFIX conversion: <http://purl.org/twc/vocab/conversion/>
      #SELECT count(distinct ?o) as ?count
      #WHERE {
      #  GRAPH <http://logd.tw.rpi.edu/source/nci-nih-gov/dataset/tobacco-law-coverage/version/2010-Aug-25/conversion/enhancement/1/subset/sample> {
      #   ?s ?p ?o .
      #   filter(regex(str(?o),'http://logd.tw.rpi.edu/id/us.*'))
      #  }
      #}

      # Passes:
      #
      #PREFIX owl:        <http://www.w3.org/2002/07/owl#>
      #PREFIX dcterms:    <http://purl.org/dc/terms/>
      #PREFIX conversion: <http://purl.org/twc/vocab/conversion/>
      #SELECT count(distinct ?o) as ?count
      #WHERE {
      #  GRAPH <http://logd.tw.rpi.edu/source/data-gov/dataset/1000/version/2010-Aug-30/conversion/enhancement/1/subset/sample> {
      #   ?s ?p ?o .
      #   filter(regex(str(?o),'http://logd.tw.rpi.edu/id/us.*'))
      #  }
      #}

      ####
      # Query a SPARQL endpoint
      #store = Store(reader = 'sparql_protocol', endpoint = 'http://dbpedia.org/sparql')
      #session = Session(store)
      #session.enable_logging = False
      #result = session.default_store.execute_sparql('select distinct ?type where {[] a ?type} limit 2')
      #if result:
      #   for binding in result['results']['bindings']:
      #      type  = binding['type']['value']
      #      print type
      ####

      store = Store(reader = 'sparql_protocol', endpoint = 'http://logd.tw.rpi.edu/sparql')
      session = Session(store)
      session.enable_logging = False
      query = select("?o").named_group(input.subject,('?s', '?p', '?o')).filter('regex(str(?o),"http://logd.tw.rpi.edu/id/us.*")')
      print unicode(query)
      result = session.default_store.execute(query)
      if result:
         for binding in result['results']['bindings']:
            print binding['o']['value']
            output.rdf_type.append(ns.DATAFAQS['Satisfactory'])

      if ns.DATAFAQS['Satisfactory'] not in output.rdf_type:
         output.rdf_type.append(ns.DATAFAQS['Unsatisfactory'])

      output.save()
开发者ID:anukat2015,项目名称:DataFAQs,代码行数:57,代码来源:references-instance-hub.py


示例10: query_SP

def query_SP(s, p, direct, context):
    """ Construct :class:`surf.query.Query` with `?v` and `?c` as unknowns. """

    s, v = direct and (s, '?v') or ('?v', s)
    query = select('?v', '?c').distinct()
    query.where((s, p, v)).optional_group(('?v', a, '?c'))
    if context:
        query.from_(context)

    return query
开发者ID:ceberhardt,项目名称:surf,代码行数:10,代码来源:query_reader.py


示例11: query_concept

def query_concept(s):
    """
    Construct :class:`surf.query.Query` with `?c` as the unknown.

    :param s: the `subject`
    :return: the query
    :rtype: :class:`surf.query.Query`
    """

    return select('?c').distinct().where((s, a, '?c'))
开发者ID:cosminbasca,项目名称:surfrdf,代码行数:10,代码来源:query_reader.py


示例12: test_simple

    def test_simple(self):
        """ Try to produce a simple "SELECT ... WHERE ..." query.  """

        expected = canonical(u"SELECT ?s ?p ?o WHERE { ?s ?p ?o }")
        query = select("?s", "?p", "?o").where(("?s", "?p", "?o"))
        result = SparqlTranslator(query).translate()

        # Translated query should be unicode object.
        self.assertTrue(isinstance(result, unicode))

        result = canonical(result)
        self.assertEqual(expected, result)
开发者ID:ceberhardt,项目名称:surf,代码行数:12,代码来源:test_sparql.py


示例13: get_label

def get_label(subject,baseuri,store):
    label = None
    ns_titles = [ns.RDFS["comment"],ns.RDFS["label"],ns.DC["title"],ns.DCTERMS["title"]]
    for title in ns_titles:
        query = select("?o").where((subject,title,"?o"))
        found_title = list(store.reader._to_table(store.reader._execute(query)))
        #print 'Nombre de labels '+title+' :'+str(len(found_title))
        if(len(found_title)>0):
            label = literal_lang_select([found_title[x]['o'] for x in range(len(found_title))])
    if(label==None): #on n'a rien trouvé du tout
        label = unicode(subject).replace(baseuri,'')
    return label    
开发者ID:quinode,项目名称:django-d2rq,代码行数:12,代码来源:models.py


示例14: test_union

    def test_union(self):
        """ Try to produce query containing union. """
        
        expected = canonical(u"""
            SELECT ?s
            WHERE {
                { ?s ?v1 ?v2} UNION { ?s ?v3  ?v4 }
            }
        """)

        query = select("?s").union(("?s", "?v1", "?v2"), ("?s", "?v3", "?v4"))
        result = canonical(SparqlTranslator(query).translate())
        
        self.assertEqual(expected, result)
开发者ID:arleincho,项目名称:surf,代码行数:14,代码来源:test_sparql.py


示例15: query_P_S

def query_P_S(c, p, direct, context):
    """ Construct :class:`surf.query.Query` with `?s` and `?c` as unknowns. """

    query = select('?s', '?c').distinct()
    if context:
        query.from_(context)

    for i in range(len(p)):
        s, v = direct and  ('?s', '?v%d' % i) or ('?v%d' % i, '?s')
        if type(p[i]) is URIRef:
            query.where((s, p[i], v))

    query.optional_group(('?s', a, '?c'))

    return query
开发者ID:ceberhardt,项目名称:surf,代码行数:15,代码来源:query_reader.py


示例16: test_str

 def test_str(self):
     """ Try str(query). """
     
     expected = canonical(u"""
         SELECT ?s ?p ?o
         WHERE { 
             ?s ?p ?o 
         }
     """)
     
     query = select("?s", "?p", "?o").where(("?s", "?p", "?o"))
     # test str()
     self.assertEqual(expected, canonical(unicode(str(query))))
     # test unicode()
     self.assertEqual(expected, canonical(unicode(query)))
开发者ID:arleincho,项目名称:surf,代码行数:15,代码来源:test_sparql.py


示例17: test_from

 def test_from(self):
     """ Try to produce query that contains FROM clauses. """
     
     expected = canonical(u"""
         SELECT ?s ?p ?o
         FROM <http://uri1>
         FROM <http://uri2> 
         WHERE { 
             ?s ?p ?o 
         }
     """)
     
     query = select("?s", "?p", "?o").where(("?s", "?p", "?o"))
     query.from_("http://uri1", URIRef("http://uri2"))
     result = canonical(SparqlTranslator(query).translate())
     
     self.assertEqual(expected, result)
开发者ID:arleincho,项目名称:surf,代码行数:17,代码来源:test_sparql.py


示例18: query_s

def query_s(s, direct, context):
    """
    Construct :class:`surf.query.Query` with `?p`, `?v` and `?c` as unknowns.

    :param s: the `subject`
    :param bool direct: whether the predicate is direct or inverse
    :param context: the context
    :return: the query
    :rtype: :class:`surf.query.Query`
    """
    s, v = (s, '?v') if direct else ('?v', s)
    query = select('?p', '?v', '?c').distinct()
    query.where((s, '?p', v)).optional_group(('?v', a, '?c'))
    if context:
        query.from_(context)

    return query
开发者ID:cosminbasca,项目名称:surfrdf,代码行数:17,代码来源:query_reader.py


示例19: test_exceptions

    def test_exceptions(self):
        """ Test that exceptions are raised on invalid queries. """

        store = surf.Store(reader = "sparql_protocol",
                           writer = "sparql_protocol",
                           endpoint = "invalid")

        def try_query():
            store.execute(query)

        query = select("?a")
        self.assertRaises(SparqlReaderException, try_query)

        def try_add_triple():
            store.add_triple("?s", "?p", "?o")

        self.assertRaises(SparqlWriterException, try_add_triple)
开发者ID:arleincho,项目名称:surf,代码行数:17,代码来源:test_sparql_protocol.py


示例20: query_S

def query_S(s, direct, contexts):
    """ Construct :class:`surf.query.Query` with `?p`, `?v` and `?g`, `?c` as
    unknowns. """
    s, v = direct and (s, '?v') or ('?v', s)
    query = select('?p', '?v', '?c', '?g').distinct()
    # Get predicate, objects and optionally rdf:type & named graph of
    # subject rdf:type and object rdf:type
    # TODO fails under Virtuoso as V. doesn't allow ?g to be bound to two
    # optional matches
    query.where((s, '?p', v)).optional_group(('?v', a, '?c'))\
                             .optional_group(named_group('?g', (s, a, v)))\
                             .optional_group(named_group('?g', ('?v', a, '?c')))
    if contexts:
        query.from_(*contexts)
        query.from_named(*contexts)

    return query
开发者ID:arleincho,项目名称:surf,代码行数:17,代码来源:query_reader.py



注:本文中的surf.query.select函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python surface.Surface类代码示例发布时间:2022-05-27
下一篇:
Python six.compat_repr函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap