本文整理汇总了Python中query.Query.parse方法的典型用法代码示例。如果您正苦于以下问题:Python Query.parse方法的具体用法?Python Query.parse怎么用?Python Query.parse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类query.Query
的用法示例。
在下文中一共展示了Query.parse方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: process_query
# 需要导入模块: from query import Query [as 别名]
# 或者: from query.Query import parse [as 别名]
def process_query(session, toklist, result):
""" Check whether the parse tree is describes a query, and if so, execute the query,
store the query answer in the result dictionary and return True """
q = Query(session)
if not q.parse(toklist, result):
# if Settings.DEBUG:
# print("Unable to parse query, error {0}".format(q.error()))
result["error"] = q.error()
return False
if not q.execute():
# This is a query, but its execution failed for some reason: return the error
# if Settings.DEBUG:
# print("Unable to execute query, error {0}".format(q.error()))
result["error"] = q.error()
return True
# Successful query: return the answer in response
result["response"] = q.answer()
# ...and the query type, as a string ('Person', 'Entity', 'Title' etc.)
result["qtype"] = qt = q.qtype()
result["key"] = q.key()
if qt == "Person":
# For a person query, add an image (if available)
img = get_image_url(q.key(), enclosing_session=session)
if img is not None:
result["image"] = dict(
src=img.src,
width=img.width,
height=img.height,
link=img.link,
origin=img.origin,
name=img.name,
)
return True
示例2: process
# 需要导入模块: from query import Query [as 别名]
# 或者: from query.Query import parse [as 别名]
def process(self, connection):
super(Graph, self).process(connection)
query = self.recv()
query = Query.parse(query)
cnx = self.open()
root = cnx.root()
try:
result = list(process(query, root))
except Exception:
transaction.abort()
cnx.close()
self.send({
'type': 'exception',
'data': traceback.format_exc()
})
else:
transaction.commit()
self.send({'type': 'result', 'data': result})
cnx.close()
示例3: find
# 需要导入模块: from query import Query [as 别名]
# 或者: from query.Query import parse [as 别名]
def find(self, query, lang=None):
lang = self._convert_lang(lang)
search = str(query)
if query.filename and not query.pointer:
search = query.filename
params = urllib.parse.urlencode({"q": search})
if not query.pointer and query.filename == query.name:
soup = util.connect("subscene.com", "/subtitles/title?" + params)
sub_links_page = self._find_movie_by_name(query, soup)
else:
sub_links_page = "/subtitles/release?" + params
if not sub_links_page:
return []
soup = util.connect("subscene.com", sub_links_page)
sub_links = []
for sub in soup.find_all("a"):
if lang not in sub.get("href"):
continue
spans = sub.find_all("span")
if len(spans) <= 1 or not spans[0].contents:
continue
link_name = spans[1].contents[0].strip()
link_query = Query.parse(link_name)
if str(link_query.pointer) != str(query.pointer):
continue
if SequenceMatcher(None, query.name, link_query.name).ratio() < 0.8:
continue
sub_links.append({
"filename": link_name + ".srt",
"url": sub.get("href"),
"score": SequenceMatcher(None, query.filename, link_name.lower()).ratio()
})
sub_links = sorted(sub_links, key=lambda v: v["score"], reverse=True)
ret = []
i = 0
for item in sub_links:
soup = util.connect("subscene.com", item["url"])
dl_button = soup.find(id="downloadButton")
dl_link = dl_button.get("href")
rating = 0
rating_title = soup.find("span", class_="rating-bar")
if rating_title:
rating = self._extract_rating(rating_title["title"])
score = (rating / 10) * 0.15 + 0.6 * item["score"]
result = SubtitleResult("http://subscene.com" + dl_link, score)
result.target_name = item["filename"]
result.zipped = True
ret.append(result)
i += 1
if i == 10:
break
return ret
示例4: print
# 需要导入模块: from query import Query [as 别名]
# 或者: from query.Query import parse [as 别名]
default_lang = locale.getdefaultlocale()[0][:2]
parser = argparse.ArgumentParser()
parser.add_argument("file", help="Name or file to search")
parser.add_argument("-c", "--count", type=int, default=1, help="Maximum number of files to download")
parser.add_argument("-l", "--lang", default=default_lang, help="Language of the subtitles")
args = parser.parse_args()
name = args.file
root = os.getcwd()
if os.path.isfile(name):
root = os.path.dirname(name)
query = Query.parse(os.path.basename(args.file))
if not query:
print("Could not parse the query")
sys.exit(1)
print("Searching for matches...", end="", flush=True)
dl = Downloader([
SubClub(),
SubScene()
])
results = dl.get(query, args.count, args.lang)
print(" found %i" % len(results))
for file in results: