drobilla / serd

A lightweight C library for RDF syntax
https://gitlab.com/drobilla/serd
ISC License
86 stars 15 forks source link

Parsing from a string in python #29

Closed loki42 closed 3 years ago

loki42 commented 3 years ago

Is there an example of how to parse from a string in python?

world.load works fine from a file, but if I put the same content in a string and use a StringIO / BytesIO it fails.

loki42 commented 3 years ago

for example. I'd like to get canvasX value out of

a patch:Put ; patch:sequenceNumber "1460"^^xsd:int ; patch:subject </main/control_out_1> ; patch:body [ ingen:canvasX "460.45147705"^^xsd:float ; ingen:canvasY "250.26577759"^^xsd:float ] .

loki42 commented 3 years ago

` def load_str( self, path, syntax=Syntax.TURTLE, reader_flags=ReaderFlags(0), model_flags=ModelFlags.INDEX_SPO, stack_size=4096, ): """Load a model from a string and return it.""" base_uri = Node.uri("/") env = Env(base_uri) model = Model(self, model_flags) inserter = Inserter(model, env) reader = Reader( self, Syntax.TURTLE, reader_flags, inserter.sink(), stack_size )

    st = reader.start_string(path.encode().decode("utf-8"), base_uri)
    if st != Status.SUCCESS:
        raise Exception("Failed to open file {} ({})".format(path, st))

    st = reader.read_document()
    if st != Status.SUCCESS:
        raise Exception("Failed to read file {} ({})".format(path, st))

    st = reader.finish()
    if st != Status.SUCCESS:
        raise Exception(
            "Failed to finish reading file {} ({})".format(path, st)
        )

    return model`

this works. Not sure why the weird encode/decode was needed but it was.

drobilla commented 3 years ago

There's a loads in the latest version. I haven't added support for Python file-like objects yet (and I'm not sure if there will be problems there or how much work it will be, but it would certainly be slower anyway)

drobilla commented 3 years ago

Oh, sorry, just realized the Github copy was out of date. Updated now.

drobilla commented 3 years ago

Examples here in the (temporarily hosted) docs: https://drobilla.net/files/pyserd_docs/

loki42 commented 3 years ago

These docs are excellent. I alas spent the the day switching my parsing code to serd but with the previous version found it required a few wrapper method as ask didn't work but count did etc. I'm sure I'll all be much better with the latest version. My fault for tracking the github version. I'm looking into monitoring ports now so attempting to improve performance for that.

On Sat, 10 Oct 2020, 2:16 am David Robillard, notifications@github.com wrote:

Examples here in the (temporarily hosted) docs: https://drobilla.net/files/pyserd_docs/

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/drobilla/serd/issues/29#issuecomment-706241547, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAISQBEIO2WG73XRXGDJ4FLSJ4SN7ANCNFSM4SIKZSKQ .

drobilla commented 3 years ago

Not sure why the weird encode/decode was needed but it was.

Hm. On my main machine, all the string stuff works (because of the cython: c_string_encoding=utf-8 directive), but on my Debian machine it doesn't, the whole test suite fails with the classic "TypeError: expected bytes, str found".

Not sure what's going on there. Given that cython apparently has this feature, I'd really rather not have to put a bunch of encode/decode calls all over the place...

loki42 commented 3 years ago

Oh is it detecting python 2 instead of 3? I had to specify python 3 in the the waf

On Sat, 10 Oct 2020, 6:48 am David Robillard, notifications@github.com wrote:

Not sure why the weird encode/decode was needed but it was.

Hm. On my main machine, all the string stuff works (because of the cython: c_string_encoding=utf-8 directive), but on my Debian machine it doesn't, the whole test suite fails with the classic "TypeError: expected bytes, str found".

Not sure what's going on there. Given that cython apparently has this feature, I'd really rather not have to put a bunch of encode/decode calls all over the place...

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/drobilla/serd/issues/29#issuecomment-706371639, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAISQBAUPFQJHJASMGDBJOTSJ5SGVANCNFSM4SIKZSKQ .

loki42 commented 3 years ago

Ask and the rest are now working well. I do have a surprising issue but maybe I'm doing something stupid here:

a = """ @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix atom: <http://lv2plug.in/ns/ext/atom#> .
@prefix lv2: <http://lv2plug.in/ns/lv2core#> .
@prefix patch: <http://lv2plug.in/ns/ext/patch#> .
@prefix midi: <http://lv2plug.in/ns/ext/midi#> .
@prefix rsz: <http://lv2plug.in/ns/ext/resize-port#> .
@prefix doap: <http://usefulinc.com/ns/doap#> .
_:b7
a patch:Put ;
patch:sequenceNumber "3"^^xsd:int ;
patch:subject </main/notify> ;
patch:body [
    ingen:canvasX "128.0"^^xsd:float ;
    ingen:canvasY "32.0"^^xsd:float ;
    atom:bufferType atom:Sequence ,
        atom:Sequence ;
    atom:supports patch:Message ;
    <http://lv2plug.in/ns/ext/resize-port#minimumSize> "4096"^^xsd:int ;
    lv2:designation lv2:control ;
    lv2:index "1"^^xsd:int ,
        "1"^^xsd:int ;
    lv2:name "Notify" ;
    lv2:portProperty lv2:connectionOptional ;
    a atom:AtomPort ,
        atom:AtomPort ,
        lv2:OutputPort
    ] ."""

world.loads(a)

loads fine, but if I add the line

@prefix ingen: <http://drobilla.net/ns/ingen#> .

after atom it doesn't load with

error: string:5:46: expected `.', not `'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "serd.pyx", line 895, in serd.World.loads
  File "serd.pyx", line 1503, in serd._ensure_success
serd.SerdError: Failed to read string (Invalid syntax)

Maybe I should start a new issue for that. I'm now running from the gitlab code, sorry for the earlier noise when using the wrong repo.

drobilla commented 3 years ago

Oh is it detecting python 2 instead of 3? I had to specify python 3 in the the waf

My first thought as well, but nope. Oh well, I just updated it to explicitly convert everywhere.

drobilla commented 3 years ago

That was lifetime issues. Probably more where that came from until I figure out how to valgrind this thing...

22d94a671