Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

405 (Method not allowed) for SELECT QUERY with # street address in comment #232

Open
WolfgangFahl opened this issue Jan 7, 2024 · 2 comments

Comments

@WolfgangFahl
Copy link

WolfgangFahl commented Jan 7, 2024

see WolfgangFahl/pyLoDStorage#119

When sending the query

# Count all Q44613:monastery items
# with the given street address(P6375) https://www.wikidata.org/wiki/Property:P6375 
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX schema: <http://schema.org/>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>

SELECT ?count (COUNT(?count) AS ?frequency) WHERE {{
SELECT ?item ?itemLabel (COUNT (?value) AS ?count)
WHERE
{
  # instance of monastery
  ?item wdt:P31 wd:Q44613.
  ?item rdfs:label ?itemLabel.
  FILTER (LANG(?itemLabel) = "en").
  # street address
  ?item wdt:P6375 ?value.
} GROUP BY ?item ?itemLabel

}}
GROUP BY ?count
ORDER BY DESC (?frequency)

try it!

with SPARQLWrapper 1.8.5 and 2.0.0 the query is interpreted as an ADD query since the _parseQueryType and def _cleanComments functions don't handle this case appropriately.

@WolfgangFahl
Copy link
Author

WolfgangFahl commented Jan 7, 2024

the comment pattern will not catch a comment in line #1

So i asked ChatGPT

comments_pattern = re.compile(r"(^|\n)\s#.*?\n") does not find comments in line 1 how to fix?*
ChatGPT

The regular expression comments_pattern = re.compile(r"(^|\n)\s*#.*?\n") is designed to match comments that are either at the beginning of the string or after a newline character, followed by optional whitespace and then the # symbol. However, it expects the comment to end with a newline (\n). If the comment is on the first line and the string doesn't start with a newline or if the file doesn't end with a newline, it might not match as intended.

To fix the regular expression so it can also find comments on the first line, you can modify the regular expression pattern to consider the start of the string (^) and the start of the line after any newline (\n) as potential starts for the comment. Additionally, you can adjust the end of the pattern to allow for the end of the string ($) as a valid end for a comment, not just a newline. This ensures that it matches comments that might be at the very end of the text without a trailing newline.

Here's how you can modify it:

import re

# Modified pattern to match comments at the start of the string or after a newline
# and consider end of string as a valid end for a comment
comments_pattern = re.compile(r"(?m)^\s*#.*")

# Test strings
test_string1 = "# This is a comment at the start of the string"
test_string2 = "Some code\n   # This is an indented comment"

# Finding and printing matches
print(comments_pattern.findall(test_string1))
print(comments_pattern.findall(test_string2))

Explanation of the pattern r"(?m)^\s*#.*":

(?m): This is a flag for multiline mode, which changes the meaning of ^ and $ to match at the start and end of each line, not just the start/end of the whole string.
^: Asserts the position at the start of the line.
\s*: Matches zero or more whitespace characters (spaces, tabs, etc.).
#.*: Matches the # character followed by any characters (except for line terminators), effectively matching the entire comment.

With this regular expression, comments at the start of the string, the start of any line, or at the end of the string will be matched appropriately.

@WolfgangFahl
Copy link
Author

my workaround is:

def fix_comments(self, query_string: str) -> str:
        """
        make sure broken SPARQLWrapper will find comments
        """
        return "#\n"+query_string

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant