本文整理汇总了Python中web.re_compile函数的典型用法代码示例。如果您正苦于以下问题:Python re_compile函数的具体用法?Python re_compile怎么用?Python re_compile使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了re_compile函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: split_key
def split_key(bib_key):
"""
>>> split_key('1234567890')
('isbn_10', '1234567890')
>>> split_key('ISBN:1234567890')
('isbn_10', '1234567890')
>>> split_key('ISBN1234567890')
('isbn_10', '1234567890')
>>> split_key('ISBN1234567890123')
('isbn_13', '1234567890123')
>>> split_key('LCCNsa 64009056')
('lccn', 'sa 64009056')
>>> split_key('badkey')
(None, None)
"""
bib_key = bib_key.strip()
if not bib_key:
return None, None
valid_keys = ['isbn', 'lccn', 'oclc', 'ocaid', 'olid']
key, value = None, None
# split with : when possible
if ':' in bib_key:
key, value = bib_key.split(':', 1)
key = key.lower()
else:
# try prefix match
for k in valid_keys:
if bib_key.lower().startswith(k):
key = k
value = bib_key[len(k):]
continue
# treat plain number as ISBN
if key is None and bib_key[0].isdigit():
key = 'isbn'
value = bib_key
# treat OLxxxM as OLID
re_olid = web.re_compile('OL\d+M(@\d+)?')
if key is None and re_olid.match(bib_key.upper()):
key = 'olid'
value = bib_key.upper()
# decide isbn_10 or isbn_13 based on length.
if key == 'isbn':
if len(value.replace('-', '')) == 13:
key = 'isbn_13'
else:
key = 'isbn_10'
if key == 'oclc':
key = 'oclc_numbers'
if key == 'olid':
key = 'key'
value = '/books/' + value.upper()
return key, value
示例2: request
def request(path, method, data):
"""Fakes the web request.
Useful when infobase is not run as a separate process.
"""
web.ctx.infobase_localmode = True
web.ctx.infobase_input = data or {}
web.ctx.infobase_method = method
def get_class(classname):
if '.' in classname:
modname, classname = classname.rsplit('.', 1)
mod = __import__(modname, None, None, ['x'])
fvars = mod.__dict__
else:
fvars = globals()
return fvars[classname]
try:
# hack to make cache work for local infobase connections
cache.loadhook()
for pattern, classname in web.group(app.mapping, 2):
m = web.re_compile('^' + pattern + '$').match(path)
if m:
args = m.groups()
cls = get_class(classname)
tocall = getattr(cls(), method)
return tocall(*args)
raise web.notfound()
finally:
# hack to make cache work for local infobase connections
cache.unloadhook()
示例3: find_page
def find_page():
path = web.ctx.path
encoding = web.ctx.get("encoding")
# I don't about this mode.
if encoding not in encodings:
raise web.HTTPError("406 Not Acceptable", {})
# encoding can be specified as part of path, strip the encoding part of path.
if encoding:
path = web.rstrips(path, "." + encoding)
for p in get_sorted_paths():
m = web.re_compile("^" + p + "$").match(path)
if m:
cls = pages[p].get(encoding) or pages[p].get(None)
args = m.groups()
# FeatureFlags support.
# A handler can be enabled only if a feature is active.
if hasattr(cls, "is_enabled") and bool(cls().is_enabled()) is False:
continue
return cls, args
return None, None
示例4: fuzzy_find
def fuzzy_find(value, options, stopwords=[]):
"""Try find the option nearest to the value.
>>> fuzzy_find("O'Reilly", ["O'Reilly Inc", "Addison-Wesley"])
"O'Reilly Inc"
"""
if not options:
return value
rx = web.re_compile("[-_\.&, ]+")
# build word frequency
d = defaultdict(list)
for option in options:
for t in rx.split(option):
d[t].append(option)
# find score for each option
score = defaultdict(lambda: 0)
for t in rx.split(value):
if t.lower() in stopwords:
continue
for option in d[t]:
score[option] += 1
# take the option with maximum score
return max(options, key=score.__getitem__)
示例5: parse_toc_row
def parse_toc_row(line):
"""Parse one row of table of contents.
>>> def f(text):
... d = parse_toc_row(text)
... return (d['level'], d['label'], d['title'], d['pagenum'])
...
>>> f("* chapter 1 | Welcome to the real world! | 2")
(1, 'chapter 1', 'Welcome to the real world!', '2')
>>> f("Welcome to the real world!")
(0, '', 'Welcome to the real world!', '')
>>> f("** | Welcome to the real world! | 2")
(2, '', 'Welcome to the real world!', '2')
>>> f("|Preface | 1")
(0, '', 'Preface', '1')
>>> f("1.1 | Apple")
(0, '1.1', 'Apple', '')
"""
RE_LEVEL = web.re_compile("(\**)(.*)")
level, text = RE_LEVEL.match(line.strip()).groups()
if "|" in text:
tokens = text.split("|", 2)
label, title, page = pad(tokens, 3, '')
else:
title = text
label = page = ""
return web.storage(level=len(level), label=label.strip(), title=title.strip(), pagenum=page.strip())
示例6: filter_index
def filter_index(self, index, name, value):
operations = {
"~": lambda i, value: isinstance(i.value, basestring) and i.value.startswith(web.rstrips(value, "*")),
"<": lambda i, value: i.value < value,
">": lambda i, value: i.value > value,
"!": lambda i, value: i.value != value,
"=": lambda i, value: i.value == value,
}
pattern = ".*([%s])$" % "".join(operations)
rx = web.re_compile(pattern)
m = rx.match(name)
if m:
op = m.group(1)
name = name[:-1]
else:
op = "="
f = operations[op]
if isinstance(value, list): # Match any of the elements in value if it's a list
for i in index:
if i.name == name and any(f(i, v) for v in value):
yield i.key
else: # Otherwise just match directly
for i in index:
if i.name == name and f(i, value):
yield i.key
示例7: parse_error
def parse_error(path):
html = open(path).read(10000)
soup = BeautifulSoup(html)
h1 = web.htmlunquote(soup.body.h1.string or "")
h2 = web.htmlunquote(soup.body.h2.string or "")
message = h1.split('at')[0].strip() + ': ' + (h2 and h2.splitlines()[0])
code, url = [web.htmlunquote(td.string) for td in soup.body.table.findAll('td')]
# strip common prefixes
code = web.re_compile(".*/(?:staging|production)/(openlibrary|infogami|web)").sub(r'\1', code)
m = web.re_compile('(\d\d)(\d\d)(\d\d)(\d{6})').match(web.numify(os.path.basename(path)))
hh, mm, ss, microsec = m.groups()
return web.storage(url=url, message=message, code=code, time="%s:%s:%s" % (hh, mm, ss))
示例8: split_key
def split_key(bib_key):
"""
>>> split_key('1234567890')
('isbn_', '1234567890')
>>> split_key('ISBN:1234567890')
('isbn_', '1234567890')
>>> split_key('ISBN1234567890')
('isbn_', '1234567890')
>>> split_key('ISBN1234567890123')
('isbn_', '1234567890123')
>>> split_key('LCCNsa 64009056')
('lccn', 'sa 64009056')
>>> split_key('badkey')
(None, None)
"""
bib_key = bib_key.strip()
if not bib_key:
return None, None
valid_keys = ['isbn', 'lccn', 'oclc', 'ocaid', 'olid']
key, value = None, None
# split with : when possible
if ':' in bib_key:
key, value = bib_key.split(':', 1)
key = key.lower()
else:
# try prefix match
for k in valid_keys:
if bib_key.lower().startswith(k):
key = k
value = bib_key[len(k):]
continue
# treat plain number as ISBN
if key is None and bib_key[0].isdigit():
key = 'isbn'
value = bib_key
# treat OLxxxM as OLID
re_olid = web.re_compile('OL\d+M(@\d+)?')
if key is None and re_olid.match(bib_key.upper()):
key = 'olid'
value = bib_key.upper()
if key == 'isbn':
# 'isbn_' is a special indexed field that gets both isbn_10 and isbn_13 in the normalized form.
key = 'isbn_'
value = value.replace("-", "") # normalize isbn by stripping hyphens
if key == 'oclc':
key = 'oclc_numbers'
if key == 'olid':
key = 'key'
value = '/books/' + value.upper()
return key, value
示例9: get_real_path
def get_real_path():
pat = '^(' + '|'.join(p[0] for p in patterns) + ')(?:/.*)?'
rx = web.re_compile(pat)
m = rx.match(web.ctx.path)
if m:
path = m.group(1)
return m.group(1)
else:
return web.ctx.path
示例10: delegate
def delegate(self):
if web.ctx.path == "/admin":
return self.handle(admin_index)
for t in admin_tasks:
m = web.re_compile('^' + t.path + '$').match(web.ctx.path)
if m:
return self.handle(t.cls, m.groups())
raise web.notfound()
示例11: GET
def GET(self, key):
page = web.ctx.site.get(key)
if web.re_compile('/(authors|books|works)/OL.*').match(key):
if page is None:
raise web.seeother(key)
else:
raise web.seeother(page.url(suffix="/edit"))
else:
return core.edit.GET(self, key)
示例12: parse_key
def parse_key(self, key):
"""Returns prefix and path from the key.
"""
m = web.re_compile(r'/subjects/(place:|time:|person:|)(.+)').match(key)
if m:
prefix = "/subjects/" + m.group(1)
path = m.group(2)
return prefix, path
else:
return None, None
示例13: escape
def escape(self, query):
r"""Escape special characters in the query string
>>> solr = Solr("")
>>> solr.escape("a[b]c")
'a\\[b\\]c'
"""
chars = r'+-!(){}[]^"~*?:\\'
pattern = "([%s])" % re.escape(chars)
return web.re_compile(pattern).sub(r'\\\1', query)
示例14: add
def add(self, doc):
#@@ UGLY!
doc = common.parse_query(doc)
doc = client.Site(None, None)._process_dict(doc)
key = doc['key']
self.docs[key] = client.create_thing(self, key, doc)
olid = key.split("/")[-1]
if web.re_compile(r'OL\d+[A-Z]').match(olid):
self.olids[olid] = key
示例15: parse_lang_header
def parse_lang_header():
"""Parses HTTP_ACCEPT_LANGUAGE header."""
accept_language = web.ctx.get('env', {}).get('HTTP_ACCEPT_LANGUAGE', '')
re_accept_language = web.re_compile(', *')
tokens = re_accept_language.split(accept_language)
# take just the language part. ignore other details.
# for example `en-gb;q=0.8` will be treated just as `en`.
langs = [t[:2] for t in tokens]
return langs and langs[0]