本文整理汇总了Python中utils.group函数的典型用法代码示例。如果您正苦于以下问题:Python group函数的具体用法?Python group怎么用?Python group使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了group函数的18个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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')
开发者ID:jcodagnone,项目名称:inmuebles,代码行数:60,代码来源:template.py
示例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
开发者ID:JoaoFelipe,项目名称:Flappybot,代码行数:27,代码来源:code.py
示例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
开发者ID:mmagnuski,项目名称:exp_CircStim,代码行数:60,代码来源:viz.py
示例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
开发者ID:acgourley,项目名称:watchdog,代码行数:7,代码来源:application.py
示例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
开发者ID:Letractively,项目名称:pytof,代码行数:9,代码来源:application.py
示例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
开发者ID:wangfeng3769,项目名称:remotebox,代码行数:10,代码来源:application.py
示例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)
开发者ID:AyumuKasuga,项目名称:steambot,代码行数:11,代码来源:bot.py
示例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)
开发者ID:AyumuKasuga,项目名称:steambot,代码行数:11,代码来源:bot.py
示例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
开发者ID:christopherbdnk,项目名称:watchdog,代码行数:11,代码来源:application.py
示例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]
开发者ID:abhijo89,项目名称:fSphinx,代码行数:12,代码来源:hits.py
示例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)])
开发者ID:josephreisinger,项目名称:Parallel-Textual-Extraction,代码行数:13,代码来源:__init__.py
示例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()
开发者ID:1ngmar,项目名称:ianki,代码行数:51,代码来源:request.py
示例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
开发者ID:christopherbdnk,项目名称:watchdog,代码行数:16,代码来源:application.py
示例14: init_mapping
def init_mapping(self, mapping):
self.mapping = list(utils.group(mapping, 2))
开发者ID:alustig,项目名称:OSPi,代码行数:2,代码来源:application.py
示例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))))
开发者ID:Waveharp,项目名称:Fighting-Time,代码行数:71,代码来源:pytmx.py
示例16: add_mapping
def add_mapping(self, map):
self.mapping.extend(utils.group(map, 2))
开发者ID:vincent19376,项目名称:Grunge,代码行数:2,代码来源:application.py
示例17: mtime_beat
def mtime_beat():
'''每次任务只跑一年的'''
y_list = []
y = get_year() + 1 # 要抓取的年份
debug('Fetch Year: {} starting...'.format(y))
instance = fetch(y, 1)
page = get_movie_pages(instance)
if page is None:
warn('Movie"page has not fetched')
# 执行间隔自适应
if scheduler.get_interval < TASK_BEAT * 7:
scheduler.change_interval(incr=True)
return
ids = get_movie_ids(instance)
if ids is None:
# 间隔自适应也不能太大
warn('Movie has not fetched')
if scheduler.get_interval < TASK_BEAT * 7:
scheduler.change_interval(incr=True)
return
# 当任务继续能执行的时候,回到默认的间隔
if scheduler.get_interval > TASK_BEAT:
debug('Interval back to default')
scheduler.change_interval(TASK_BEAT)
y_list.extend(ids)
if not y_list:
# 本年没有电影
debug('Year: {} has not movie'.format(y))
YearFinished(year=y).save()
sleep2()
return mtime_beat()
if page > 1:
p = 2
while p <= page:
instance = fetch(y, p)
debug('Fetch Year:{} Page:{}'.format(y, p))
ids = get_movie_ids(instance)
if ids is None:
# 间隔自适应也不能太大
if scheduler.get_interval < TASK_BEAT * 7:
scheduler.change_interval(incr=True)
# 出现需要验证码 手动输入或者等待一段时间后重试,直到能正常使用
sleep2(VERIFY_INTERVAL)
continue
ids = []
y_list.extend(ids)
p += 1
sleep2()
obj = IdFinished.objects(year=y).first()
if obj is not None:
has_finished = obj.ids
else:
has_finished = []
to_process = get_unfinished(has_finished, y_list)
# 给相应队列添加任务
for payload in group(to_process, TASK_BEAT_NUM):
for task in ['Fullcredits', 'Movie', 'Comment', 'Character',
'MicroComment', 'Scenes', 'Awards', 'Plot',
'Details']:
debug('Push payload: {} to {} Queue'.format(payload, task))
try:
Message(year=y, task=task, payload=payload).save()
# Hack一下
#Message.objects.get_or_create(year=y, task=task, payload=payload)
except NotUniqueError:
debug('Duplicate insert: [{}], payload: {}'.format(task, payload))
# 当前年份数据已经入MQ
YearFinished(year=y).save()
debug('Year: {} done'.format(y))
开发者ID:StevenLOL,项目名称:Mtime,代码行数:69,代码来源:beat.py
示例18: iceil
end = args["end_val"]
delta = args["delta"] * 1.0
if start == end:
return 1
if delta > 0 and start > end:
return 0
elif delta < 0 and end > start:
return 0
elif start <= end:
return iceil((end + 1 - start) / abs(delta))
else:
return iceil((start + 1 - end) / abs(delta))
TESTS_GROUPS = group(
flatten([cls().build_tests() for cls in Gbs3TestScript.__subclasses__()]),
128
)
TEST_DIR = os.path.dirname(os.path.dirname(__file__))
THIS_TEST_DIR = os.path.dirname(__file__)
class AutoTestCaseGbs3(unittest.TestCase, AutoTestCase):
def setUp(self):
copy_file(
THIS_TEST_DIR + "/Biblioteca.gbs",
TEST_DIR + "/examples/Biblioteca.gbs"
)
def gobstones_parameters(self):
return "--language gobstones"
开发者ID:gobstones,项目名称:pygobstones-lang,代码行数:32,代码来源:autotestsgbs3_test.py
注:本文中的utils.group函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论