Git Product home page Git Product logo

Comments (4)

vtermanis avatar vtermanis commented on May 27, 2024

This might do the trick (to remove single line comments):

re.compile(r"""
    # leading whitespace
    \s*
    # bits of query
    (?P<query_parts>(
        # IRI
        <.*?>|
        # quoted strings (using single or double quotes, in triplicate)
        (?P<quotes>("{3}|'{3}))(.|\n)*?(?P=quotes)|
        # quoted strings (using single or double quotes, individual)
        (?P<lquotes>["']).*?(?P=lquotes)|
        # everything except starts for the above two, whitespace and comment
        [^<"'\#\s]+|
        # whitespace (except newline) if not followed by comment, newline (or end of query)
        [^\S\n]+(?![^\S\n]*(\#|\n|$))|
        # escaped hashes
        (?<=\\)\#
    # bits of query can have zero or more elements
    )*)
    # comment and preceeding whitespace (up to next newline, if present)
    (\s*\#[^\n]*)?""", re.X)

(It might not be necessary to check whether the whole match starts & ends with newline - I'm not sure.)

Updated: Added triple quote & escaped hash detection, fixed multi-line string pruning

from sparqlwrapper.

joernhees avatar joernhees commented on May 27, 2024

hmm, while this seems to solve the comment problem, in #32 @gromgull already mentioned that it could be beneficial to use the rdflib sparql parser to do this properly... i'd actually vote for that as it reduces code-duplication for partial parsing with weird side effects like this...

from sparqlwrapper.

vtermanis avatar vtermanis commented on May 27, 2024

The only downside might be (although maybe I'm wrong?) that it's considerably less efficient to ask rdflib to parse it first (assuming regex uses C extension). Wouldn't it be better to rely on the user to indicate whether they're trying to perform a query or update request? After all, they must know what they're doing in order to do something with (or the lack of) response data from the request anyway. I do realise this would break backwards compatibility so presumably is out of the question.
(Whilst e.g. SQL libraries have a single query() function, they still assume the user to know what they're doing with what comes back.)

from sparqlwrapper.

vtermanis avatar vtermanis commented on May 27, 2024

One last thought: How about if query() allows one to specify whether it's a query or an update request but make the parameter optional (and, if not specified, check query for type as before).

from sparqlwrapper.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.