本文整理汇总了Python中utils.group函数的典型用法代码示例。如果您正苦于以下问题:Python group函数的具体用法?Python group怎么用?Python group使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了group函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test
def test():
import sys
verbose = '-v' in sys.argv
def assertEqual(a, b):
if a == b:
if verbose:
sys.stderr.write('.')
sys.stderr.flush()
else:
assert a == b, "\nexpected: %s\ngot: %s" % (repr(b), repr(a))
from utils import storage, group
t = Template
tests = [
lambda: t('1')(), '1\n',
lambda: t('$def with ()\n1')(), '1\n',
lambda: t('$def with (a)\n$a')(1), '1\n',
lambda: t('$def with (a=0)\n$a')(1), '1\n',
lambda: t('$def with (a=0)\n$a')(a=1), '1\n',
lambda: t('$if 1: 1')(), '1\n',
lambda: t('$if 1:\n 1')(), '1\n',
lambda: t('$if 0: 0\n$elif 1: 1')(), '1\n',
lambda: t('$if 0: 0\n$elif None: 0\n$else: 1')(), '1\n',
lambda: t('$if (0 < 1) and (1 < 2): 1')(), '1\n',
lambda: t('$for x in [1, 2, 3]: $x')(), '1\n2\n3\n',
lambda: t('$for x in []: 0\n$else: 1')(), '1\n',
lambda: t('$def with (a)\n$while a and a.pop(): 1')([1, 2, 3]), '1\n1\n1\n',
lambda: t('$while 0: 0\n$else: 1')(), '1\n',
lambda: t('$ a = 1\n$a')(), '1\n',
lambda: t('$# 0')(), '',
lambda: t('$def with (d)\n$for k, v in d.iteritems(): $k')({1: 1}), '1\n',
lambda: t('$def with (a)\n$(a)')(1), '1\n',
lambda: t('$def with (a)\n$a')(1), '1\n',
lambda: t('$def with (a)\n$a.b')(storage(b=1)), '1\n',
lambda: t('$def with (a)\n$a[0]')([1]), '1\n',
lambda: t('${0 or 1}')(), '1\n',
lambda: t('$ a = [1]\n$a[0]')(), '1\n',
lambda: t('$ a = {1: 1}\n$a.keys()[0]')(), '1\n',
lambda: t('$ a = []\n$if not a: 1')(), '1\n',
lambda: t('$ a = {}\n$if not a: 1')(), '1\n',
lambda: t('$ a = -1\n$a')(), '-1\n',
lambda: t('$ a = "1"\n$a')(), '1\n',
lambda: t('$if 1 is 1: 1')(), '1\n',
lambda: t('$if not 0: 1')(), '1\n',
lambda: t('$if 1:\n $if 1: 1')(), '1\n',
lambda: t('$ a = 1\n$a')(), '1\n',
lambda: t('$ a = 1.\n$a')(), '1.0\n',
lambda: t('$({1: 1}.keys()[0])')(), '1\n',
lambda: t('$for x in [1, 2, 3]:\n\t$x') (), ' 1\n 2\n 3\n',
lambda: t('$def with (a)\n$:a')(1), '1\n',
]
for func, value in group(tests, 2):
assertEqual(func(), value)
j = Template("$var foo: bar")()
assertEqual(str(j), '')
assertEqual(j.foo, 'bar\n')
if verbose: sys.stderr.write('\n')
示例2: find_elements
def find_elements(diff_array):
diff_array.flags.writeable = True
diff_array[22:92,160:320] = zeros((70,160))
contours,hierarchy = cv2.findContours(diff_array, 1, 2)
bird = None
raw_pipes = []
for cnt in contours:
area = cv2.contourArea(cnt)
rect = cv2.boundingRect(cnt)
if 1400 < area < 1700 and 90 < rect[0] < 170:
bird = rect
elif area > 1500:
raw_pipes.append(rect)
pipes = []
for pipe in group(sorted(raw_pipes, key=lambda x:x[0]), 2):
x = min(pipe[0][0], pipe[1][0])
y_values = [pipe[0][1], pipe[1][1]]
min_y_index, min_y_value = min(enumerate(y_values), key=operator.itemgetter(1))
y = min_y_value + [pipe[0][3], pipe[1][3]][min_y_index]
size_x = min(pipe[0][2], pipe[1][2])
size_y = y_values[min_y_index-1] - y_values[min_y_index] - y + min_y_value
p = [x, y, size_x, size_y]
pipes.append(p)
return bird, pipes
示例3: bin_points
def bin_points(x_pnts, y_pnts, min_bucket, split_bucket):
# adaptive min_bucket and split_bucket
adaptive_min_bucket = isinstance(min_bucket, str) and \
min_bucket == 'adaptive'
adaptive_split_bucket = isinstance(split_bucket, str) and \
split_bucket == 'adaptive'
if adaptive_min_bucket or adaptive_split_bucket:
contrast_range = x_pnts[[0, -1]]
# or if we want to trim edges:
# drop_elements = int(np.ceil(len(x_pnts) * 0.08))
# contrast_range = x_pnts[drop_elements:-drop_elements][[0, -1]]
contrast_range = np.diff(contrast_range)[0]
if adaptive_min_bucket:
min_bucket = contrast_range / 20.
if adaptive_split_bucket:
split_bucket = contrast_range / 10.
# look for buckets
x_buckets = group(np.diff(x_pnts) <= min_bucket)
n_pnts_in_bucket = (np.diff(x_buckets, axis=-1) + 1).ravel()
good_buckets = n_pnts_in_bucket >= (3 - 1) # -1 because of diff
if x_buckets.shape[0] > 0 and np.any(good_buckets):
x_buckets = x_buckets[good_buckets, :]
# turn buckets to slices, get mean and sem
x_buckets[:, 1] += 2 # +1 because of python slicing
# another +1 because of diff
slices = [slice(l[0], l[1]) for l in x_buckets]
# test each slice for contrast range and split if needed
add_slices = list()
ii = 0
while ii < len(slices):
slc = slices[ii]
pnts = x_pnts[slc]
l, h = pnts[[0, -1]]
rng = h - l
start = slc.start
if rng > split_bucket:
slices.pop(ii)
n_full_splits = int(np.floor(rng / split_bucket))
last_l = l
last_ind = 0
for splt in range(n_full_splits):
this_high = last_l + split_bucket
this_ind = np.where(pnts >= this_high)[0][0]
add_slices.append(slice(start + last_ind, start +
this_ind + 1))
last_l = this_high
last_ind = this_ind + 1
# last one - to the end
if start + last_ind < slc.stop:
add_slices.append(slice(start + last_ind, slc.stop))
else:
ii += 1
slices.extend(add_slices)
return slices
示例4: _match
def _match(self, mapping, value):
for pat, what in utils.group(mapping, 2):
rx = utils.re_compile("^" + pat + "$")
result = rx.match(value)
if result:
return what, [x and urllib.unquote(x) for x in result.groups()]
return None, None
示例5: _match
def _match(self, mapping, value):
for pat, what in utils.group(mapping, 2):
if isinstance(what, basestring):
what, result = utils.re_subm("^" + pat + "$", what, web.ctx.path)
else:
result = utils.re_compile("^" + pat + "$").match(web.ctx.path)
if result: # it's a match
return what, [x and urllib.unquote(x) for x in result.groups()]
return None, None
示例6: _match
def _match(self, mapping, value):
for pat, what in utils.group(mapping, 2):
if isinstance(what, basestring):
what, result = utils.re_subm('^' + pat + '$', what, value)
else:
result = utils.re_compile('^' + pat + '$').match(value)
if result: # it's a match
return what, [x for x in result.groups()]
return None, None
示例7: set_lang
async def set_lang(self, chat_id, command, args):
lang = args.strip() if args else None
if lang:
await self.save_user_settings(chat_id, {'lang': LANG.get(lang)})
await bot.sendMessage(chat_id, 'language saved', reply_markup=ReplyKeyboardRemove())
else:
markup = ReplyKeyboardMarkup(
keyboard=group(['/lang' + x for x in LANG.keys()], 2),
one_time_keyboard=True
)
await bot.sendMessage(chat_id, 'set language', reply_markup=markup)
示例8: set_cc
async def set_cc(self, chat_id, command, args):
cc = args.strip() if args else None
if cc:
await self.save_user_settings(chat_id, {'cc': CC.get(cc)})
await bot.sendMessage(chat_id, 'region saved', reply_markup=ReplyKeyboardRemove())
else:
markup = ReplyKeyboardMarkup(
keyboard=group(['/cc' + x for x in CC.keys()], 3),
one_time_keyboard=True
)
await bot.sendMessage(chat_id, 'set region', reply_markup=markup)
示例9: handle
def handle(self):
for dir, what in utils.group(self.mapping, 2):
if web.ctx.path.startswith(dir + '/'):
# change paths to make path relative to dir
web.ctx.home += dir
web.ctx.homepath += dir
web.ctx.path = web.ctx.path[len(dir):]
web.ctx.fullpath = web.ctx.fullpath[len(dir):]
return self._delegate(what, self.fvars)
raise NotFound
示例10: __call__
def __call__(self, hits):
words = getattr(self._cl.query, 'sphinx', self._cl.query)
docs = []
for match in hits.matches:
docs.extend([utils._unicode(match['@hit'][f]) for f in self._on_fields])
all_excerpts = self._cl.BuildExcerpts(docs, self._index, words, self._opts)
print all_excerpts
for match, excerpts in zip(hits.matches, utils.group(all_excerpts, len(self._on_fields))):
for f, excerpt in zip(self._on_fields, excerpts):
match['@hit'][f + self._suffix] = excerpt or match['@hit'][f]
示例11: get_shuffle_tokens
def get_shuffle_tokens(self):
#shuffle_keys = []
#for s in string.lowercase:
# for s2 in string.lowercase:
# shuffle_keys.append(s+s2)
#for s in string.uppercase:
# for s2 in string.lowercase+string.uppercase:
# shuffle_keys.append(s+s2)
#
#return dict([(('shuffle', shard), Token()) for shard in shuffle_keys])
# return dict([(('shuffle', shard), Token()) for shard in string.lowercase+string.uppercase])
return dict([(('shuffle', tuple(g)), Token()) for g in
group(list(self.shuffle_keys), ShuffleKeysPerShard)])
示例12: handle
def handle(mapping, fvars=None):
"""
Call the appropriate function based on the url to function mapping in `mapping`.
If no module for the function is specified, look up the function in `fvars`. If
`fvars` is empty, using the caller's context.
`mapping` should be a tuple of paired regular expressions with function name
substitutions. `handle` will import modules as necessary.
"""
for url, ofno in utils.group(mapping, 2):
if isinstance(ofno, tuple):
ofn, fna = ofno[0], list(ofno[1:])
else:
ofn, fna = ofno, []
fn, result = utils.re_subm('^' + url + '$', ofn, web.ctx.path)
if result: # it's a match
if fn.split(' ', 1)[0] == "redirect":
url = fn.split(' ', 1)[1]
if web.ctx.method == "GET":
x = web.ctx.env.get('QUERY_STRING', '')
if x:
url += '?' + x
return http.redirect(url)
elif '.' in fn:
x = fn.split('.')
mod, cls = '.'.join(x[:-1]), x[-1]
mod = __import__(mod, globals(), locals(), [""])
cls = getattr(mod, cls)
else:
cls = fn
mod = fvars
if isinstance(mod, types.ModuleType):
mod = vars(mod)
try:
cls = mod[cls]
except KeyError:
return web.notfound()
meth = web.ctx.method
if meth == "HEAD":
if not hasattr(cls, meth):
meth = "GET"
if not hasattr(cls, meth):
return nomethod(cls)
tocall = getattr(cls(), meth)
args = list(result.groups())
for d in re.findall(r'\\(\d+)', ofn):
args.pop(int(d) - 1)
return tocall(*([x and urllib.unquote(x) for x in args] + fna))
return web.notfound()
示例13: _match
def _match(self, mapping, value):
for pat, what in utils.group(mapping, 2):
if isinstance(what, application):
if value.startswith(pat):
f = lambda: self._delegate_sub_application(pat, what)
return f, None
else:
continue
elif isinstance(what, basestring):
what, result = utils.re_subm('^' + pat + '$', what, value)
else:
result = utils.re_compile('^' + pat + '$').match(value)
if result: # it's a match
return what, [x and urllib.unquote(x) for x in result.groups()]
return None, None
示例14: init_mapping
def init_mapping(self, mapping):
self.mapping = list(utils.group(mapping, 2))
示例15: parse
def parse(self, node):
"""
parse a layer element
"""
from utils import group
from itertools import product, imap
from struct import unpack
import array
self.set_properties(node)
data = None
next_gid = None
data_node = node.find('data')
encoding = data_node.get("encoding", None)
if encoding == "base64":
from base64 import decodestring
data = decodestring(data_node.text.strip())
elif encoding == "csv":
next_gid = imap(int, "".join(
line.strip() for line in data_node.text.strip()
).split(","))
elif encoding:
msg = "TMX encoding type: {0} is not supported."
raise Exception, msg.format(encoding)
compression = data_node.get("compression", None)
if compression == "gzip":
from StringIO import StringIO
import gzip
fh = gzip.GzipFile(fileobj=StringIO(data))
data = fh.read()
fh.close()
elif compression == "zlib":
import zlib
data = zlib.decompress(data)
elif compression:
msg = "TMX compression type: {0} is not supported."
raise Exception, msg.format(str(attr["compression"]))
# if data is None, then it was not decoded or decompressed, so
# we assume here that it is going to be a bunch of tile elements
# TODO: this will probably raise an exception if there are no tiles
if encoding == next_gid is None:
def get_children(parent):
for child in parent.findall('tile'):
yield int(child.get('gid'))
next_gid = get_children(data_node)
elif data:
# data is a list of gids. cast as 32-bit ints to format properly
# create iterator to efficiently parse data
next_gid = imap(lambda i: unpack("<L", "".join(i))[0], group(data, 4))
# using bytes here limits the layer to 256 unique tiles
# may be a limitation for very detailed maps, but most maps are not
# so detailed.
[self.data.append(array.array("H")) for i in xrange(self.height)]
for (y, x) in product(xrange(self.height), xrange(self.width)):
self.data[y].append(self.parent.register_gid(*decode_gid(next(next_gid))))