Here are the examples of the python api six.moves.map taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
178 Examples
4
Example 1
def get_data(self):
sums = np.cuemsum(self.sizes)
idxs = np.arange(self.size())
self.rng.shuffle(idxs)
idxs = np.array(list(map(
lambda x: np.searchsorted(sums, x, 'right'), idxs)))
itrs = [k.get_data() for k in self.df_lists]
assert idxs.max() == len(itrs) - 1, "{}!={}".format(idxs.max(), len(itrs)-1)
for k in idxs:
yield next(itrs[k])
4
Example 2
def __init__(self, input_file, size):
self.seq_length = param.seq_len
self._size = size
self.rng = get_rng(self)
logger.info("Loading corpus...")
# preprocess data
with open(input_file) as f:
data = f.read()
counter = Counter(data)
char_cnt = sorted(counter.items(), key=operator.itemgetter(1), reverse=True)
self.chars = [x[0] for x in char_cnt]
self.vocab_size = len(self.chars)
param.vocab_size = self.vocab_size
self.lut = LookUpTable(self.chars)
self.whole_seq = np.array(list(map(self.lut.get_idx, data)), dtype='int32')
logger.info("Corpus loaded. Vocab size: {}".format(self.vocab_size))
3
Example 3
Project: formencode
License: View license
Source File: test_subclassing_old.py
Function: test_1_warnings
License: View license
Source File: test_subclassing_old.py
Function: test_1_warnings
def test_1_warnings(self): # must run first
with warnings.catch_warnings(record=True) as runtime_warnings:
warnings.simplefilter('default')
DeprecatedNotOneValidator().to_python('2')
for output in runtime_warnings, not_one_warnings:
output = '\n'.join(map(str, output))
msg = '_to_python is deprecated; use _convert_to_python instead'
self.assertTrue(msg in output, output or 'no warnings')
3
Example 4
def _search_all(self, term):
tokens = [t.lower() for t in _tokenizer_regex.findall(term)]
glob_cond = lambda t: ('?' in t) or ('*' in t)
glob_tokens = filter(glob_cond, tokens)
word_tokens = filter(lambda t: not glob_cond(t), tokens)
wids = set()
wids.update(self._lexicon.termToWordIds(word_tokens))
wids.update(itertools.chain(*map(self._lexicon.globToWordIds, glob_tokens)))
wids = self._remove_oov_wids(wids)
# XXX
# We should have OrderedDict-like lazy objects
# and we should have weightedIntersection and weightedUnion
# working lazily in a for of zerodbext.catalog
# This is just a workaround for simpler queries
# XXX
return imap(lambda x: x[0], mass_weightedUnion(self._search_wids(wids)))
3
Example 5
def _except_import_error(node):
"""
Check if the try-except node has an ImportError handler.
Return True if an ImportError handler was infered, False otherwise.
"""
if not isinstance(node, astroid.TryExcept):
return
return any(map(is_import_error, node.handlers))
3
Example 6
def dotproduct(vec1, vec2):
"""Returns the dot product of the two iterables
>>> dotproduct([10, 10], [20, 20])
400
"""
return sum(map(operator.mul, vec1, vec2))
3
Example 7
@cached_property
def thumbs(self):
thumb_ids = filter(None, self.request.GET.get('thumbs', '').split(','))
try:
thumb_ids = map(int, thumb_ids)
except TypeError:
thumbs = Thumb.objects.none()
else:
thumbs = Thumb.objects.filter(pk__in=thumb_ids)
thumb_dict = dict([(t.name, t) for t in thumbs])
ordered_thumbs = [thumb_dict.get(s.name, Thumb(name=s.name)) for s in self.sizes]
return FakeQuerySet(ordered_thumbs, thumbs)
3
Example 8
def test_str(self):
for init in self.inits:
omd = omdict(init)
s = '{%s}' % ', '.join(
map(lambda p: '%s: %s' % (p[0], p[1]), omd.allitems()))
assert s == str(omd)
3
Example 9
def _defaults_match(self, arg):
return any(map(Argument.ignore_default, [self, arg])) \
or (self.default is Argument.any_default and
arg.default is not Argument.no_default) \
or (arg.default is Argument.any_default and
self.default is not Argument.no_default) \
or self.default == arg.default
3
Example 10
def qualdist(qualities, filename, fig_kw):
fig = plt.figure(**fig_kw)
ax = fig.add_subplot(111)
fig.subplots_adjust(top=0.85)
values = map(Counter, qualities)
counts = Counter()
for value in values:
counts += value
x, y = zip(*sorted(counts.items(), key=lambda x: x[0]))
ax.bar([_ - 1 for _ in x], y, color=(0.1,0.6,0.8))
ax.yaxis.grid(b=True, which='major', **{'color':'gray', 'linestyle':':'})
ax.set_axisbelow(True)
x1,x2,y1,y2 = ax.axis()
ax.axis((x1,max(x), 0, y2))
ax.set_title('Quality score cuemulative distribution')
ax.set_xlabel('Phred score')
ax.set_ylabel('Cumulative sum')
add_figure_to_archive(fig, filename, 'quality_score_distribution.png')
return np.median(tuple(itertools.chain(*([n] * m for n, m in counts.items()))))
3
Example 11
def load_tasks(self, command='all'):
def _load_tasks(filename):
filename = os.path.join(self.config['data']['location'], filename)
filename = os.path.expanduser(filename)
with open(filename, 'r') as f:
lines = f.readlines()
return list(map(taskw.utils.decode_task, lines))
return dict(
(db, _load_tasks(DataFile.filename(db)))
for db in Command.files(command)
)
3
Example 12
def compile_clues(self):
# compiling regular expressions
for app in self.apps:
regexps = {}
for key in self.apps[app]:
if key in ['script', 'html', 'url']:
regexps[key + "_re"] = list(six.moves.map(self.compile_clue, self.apps[app][key]))
if key in ['meta', 'headers']:
regexps[key + "_re"] = {}
for entry in self.apps[app][key]:
regexps[key + "_re"][entry] = self.compile_clue(self.apps[app][key][entry])
self.apps[app].update(regexps)
3
Example 13
def tabulate(function, start=0):
"""Return an iterator mapping the function over linear input.
The start argument will be increased by 1 each time the iterator is called
and fed into the function.
>>> t = tabulate(lambda x: x**2, -3)
>>> take(3, t)
[9, 4, 1]
"""
return map(function, count(start))
3
Example 14
def test_paging(self):
page_size = 10
num_pages = 3
results = self.dataset.query(page_size=page_size)
_results = []
for (i, r) in enumerate(results):
# fetch three pages and break
if i / page_size == num_pages:
break
_results.append(r)
self.assertEqual(len(_results), num_pages * page_size)
self.assertEqual(
len(set(map(str, _results))),
num_pages * page_size
)
3
Example 15
def str(self, arg, encoding=None):
if isinstance(arg, six.string_types):
if not isinstance(arg, str):
arg = arg.encode(default_encoding)
return arg
elif arg is None:
return ''
elif isinstance(arg, (list, tuple)):
return ''.join(map(self.str, arg))
elif isinstance(arg, Element):
return str(arg)
else:
arg = six.text_type(arg)
if not isinstance(arg, str): # Python 2
arg = arg.encode(default_encoding)
return arg
3
Example 16
def __init__(self,
url='sqlite:///:memory:',
equities=_default_equities,
**frames):
self._url = url
self._eng = None
if equities is self._default_equities:
equities = make_simple_equity_info(
list(map(ord, 'ABC')),
pd.Timestamp(0),
pd.Timestamp('2015'),
)
frames['equities'] = equities
self._frames = frames
self._eng = None # set in enter and exit
3
Example 17
def iterlists(self):
'''
Example:
omd = omdict([(1,1), (1,11), (1,111), (2,2), (3,3)])
omd.iterlists() -> [1,11,111] -> [2] -> [3]
Returns: An iterator over the list comprised of the lists of values for
each key.
'''
return map(lambda key: self.getlist(key), self)
3
Example 18
@classmethod
def export_color_matrix_filter(cls, e, filter, matrix, svg_filter, attr_in=None, result='color-matrix'):
attr_in = "SourceGraphic" if attr_in is None else attr_in
fe_cxform = e.feColorMatrix()
fe_cxform.set("in", attr_in)
fe_cxform.set("type", "matrix")
fe_cxform.set("values", " ".join(map(str, matrix)))
fe_cxform.set("result", result)
filter.append(fe_cxform)
#print etree.tostring(filter, pretty_print=True)
return result
3
Example 19
def load_single_key(self, key):
# ensure only url/surt is part of key
key = key.split(b' ')[0]
cdx_list = self.redis.zrange(self.key_prefix + key, 0, -1)
# key is not part of list, so prepend to each line
key += b' '
cdx_list = list(map(lambda x: key + x, cdx_list))
return cdx_list
3
Example 20
def __init__(self, seed=None):
"""Initialize a LocalEngine."""
super(LocalEngine, self).__init__(seed=seed)
self.mapper = lambda *args: list(six.moves.map(*args))
self.do_initialize = _do_initialize_tuple
self.do_analyze = _do_analyze_tuple
self.do_insert = _do_insert_tuple
return
3
Example 21
def _join_traj_data(traj_data, top_file):
top = load_topology_cached(top_file)
xyz = np.concatenate(tuple(map(itemgetter(0), traj_data)))
traj = Trajectory(xyz, top)
if all(t.unitcell_lengths is not None for t in traj_data):
unitcell_lengths = np.concatenate(tuple(map(itemgetter(1), traj_data)))
traj.unitcell_lengths = unitcell_lengths
if all(t.box is not None for t in traj_data):
boxes = np.concatenate(tuple(map(itemgetter(-1), traj_data)))
traj.unitcell_vectors = boxes
if all(t.unitcell_angles is not None for t in traj_data):
angles = np.concatenate(tuple(map(itemgetter(2), traj_data)))
traj.unitcell_angles = angles
return traj
3
Example 22
@pytest.fixture
def logger_call_count(logger_methods):
def _logger_call_count():
return sum(map(attrgetter('call_count'), logger_methods))
return _logger_call_count
3
Example 23
@pytest.fixture()
def mock_membership_dict_stream():
def gen_data(some_id):
return {
'type': 'group_membership',
'id': "membership_id_{0}".format(some_id),
'role': 'member',
'user': {'type': 'user', 'id': "user_id_{0}".format(some_id)},
'group': {'type': 'group', 'id': "group_id_{0}".format(some_id)},
}
return map(gen_data, count())
3
Example 24
def to_dict_no_content(fields, row):
"""
Convert a SQLAlchemy row that does not contain a 'content' field to a dict.
If row is None, return None.
Raises AssertionError if there is a field named 'content' in ``fields``.
"""
assert(len(fields) == len(row))
field_names = list(map(_get_name, fields))
assert 'content' not in field_names, "Unexpected content field."
return dict(zip(field_names, row))
3
Example 25
def join(*paths):
r"""
Wrapper around os.path.join that works with Windows drive letters.
>>> join('d:\\foo', '\\bar')
'd:\\bar'
"""
paths_with_drives = map(os.path.splitdrive, paths)
drives, paths = zip(*paths_with_drives)
# the drive we care about is the last one in the list
drive = next(filter(None, reversed(drives)), '')
return os.path.join(drive, os.path.join(*paths))
3
Example 26
def __iter__(self):
return map(itemgetter(0), self.log.conn.execute(
ANCESTORS_QUERY + "SELECT key FROM entries "
"WHERE uuid IN ancestors AND time = ?",
(self.log.h_uuid, self.time,)
))
3
Example 27
def __call__(self, vals):
if not self.colMajor:
shapes = [list(map(lambda f: f(val), self.fns)) for val in vals]
else:
shapes = [list(map(lambda f: f(val), self.fns)) for val in zip(*vals)]
return shapes
3
Example 28
def _deserialize(self, data):
if data is None:
return None
# don't wrap already wrapped data
if isinstance(data, self._doc_class):
return data
if isinstance(data, (list, AttrList)):
data[:] = list(map(self._deserialize, data))
return data
if isinstance(data, AttrDict):
data = data._d_
return self._wrap(data)
3
Example 29
@register.simple_tag
def fields_values(d, k):
"""
>>> data = {'name1': ['value1.1', 'value1.2'], 'name2': ['value2.1', 'value2.2'], }
>>> print(fields_values(data, 'name1'))
value1.1,value1.2
"""
values = d.get(k, [])
return ",".join(map(str, values))
3
Example 30
def quantify(iterable, pred=bool):
"""Return the how many times the predicate is true
>>> quantify([True, False, True])
2
"""
return sum(map(pred, iterable))
3
Example 31
def test_ComposedRule(self):
minute_groups = minutes_for_days(self.cal)
rule1 = Always()
rule2 = Never()
for minute in minute_groups:
composed = rule1 & rule2
should_trigger = composed.should_trigger
self.assertIsInstance(composed, ComposedRule)
self.assertIs(composed.first, rule1)
self.assertIs(composed.second, rule2)
self.assertFalse(any(map(should_trigger, minute)))
3
Example 32
def unique_justseen(iterable, key=None):
"""Yields elements in order, ignoring serial duplicates
>>> list(unique_justseen('AAAABBBCCDAABBB'))
['A', 'B', 'C', 'D', 'A', 'B']
>>> list(unique_justseen('ABBCcAD', str.lower))
['A', 'B', 'C', 'A', 'D']
"""
return map(next, map(operator.itemgetter(1), groupby(iterable, key)))
3
Example 33
def test_paging_with_limit(self):
page_size = 10
num_pages = 3
limit = num_pages * page_size - 1
results = self.dataset.query(limit=limit, page_size=page_size)
_results = []
for (i, r) in enumerate(results):
_results.append(r)
self.assertEqual(len(_results), limit)
self.assertEqual(
len(set(map(str, _results))),
limit
)
3
Example 34
Project: formencode
License: View license
Source File: test_subclassing_old.py
Function: test_1_warnings
License: View license
Source File: test_subclassing_old.py
Function: test_1_warnings
def test_1_warnings(self):
deprecated = (
('_to_python', '_convert_to_python'),
('_from_python', '_convert_from_python'),
('validate_other', '_validate_other'),
('validate_python', '_validate_python'))
output = '\n'.join(map(str, custom_warnings))
for old, new in deprecated:
msg = '%s is deprecated; use %s instead' % (old, new)
self.assertTrue(msg in output, output or 'no warnings')
3
Example 35
def assertQuerysetsEqual(self, qs1, qs2, transform=repr, ordered=True, msg=None):
self.assertIsNotNone(qs1, msg=msg)
self.assertIsNotNone(qs2, msg=msg)
with self.settings(**self.override_settings):
qs1_items = six.moves.map(transform, qs1)
qs2_items = six.moves.map(transform, qs2)
if not ordered:
return self.assertEqual(Counter(qs1_items), Counter(qs2_items), msg=msg)
qs1_values = list(qs1_items)
qs2_values = list(qs2_items)
self.assertEqual(qs1_values, qs2_values, msg=msg)
3
Example 36
Project: formencode
License: View license
Source File: test_subclassing_old.py
Function: test_1_warnings
License: View license
Source File: test_subclassing_old.py
Function: test_1_warnings
def test_1_warnings(self): # must run first
with warnings.catch_warnings(record=True) as runtime_warnings:
warnings.simplefilter('default')
self.validator.to_python('3')
for output in runtime_warnings, all_and_not_one_warnings:
output = '\n'.join(map(str, output))
msg = 'attempt_convert is deprecated; use _attempt_convert instead'
self.assertTrue(msg in output, output or 'no warnings')
3
Example 37
def export_color_transform(self, cxform, svg_filter, result='color-xform'):
fe_cxform = self._e.feColorMatrix()
fe_cxform.set("in", "SourceGraphic")
fe_cxform.set("type", "matrix")
fe_cxform.set("values", " ".join(map(str, cxform.matrix)))
fe_cxform.set("result", "cxform")
fe_composite = self._e.feComposite(operator="in")
fe_composite.set("in2", "SourceGraphic")
fe_composite.set("result", result)
svg_filter.append(fe_cxform)
svg_filter.append(fe_composite)
return result
3
Example 38
def iterlistitems(self):
"""
Example:
omd = omdict([(1,1), (1,11), (1,111), (2,2), (3,3)])
omd.iterlistitems() -> (1,[1,11,111]) -> (2,[2]) -> (3,[3])
Returns: An iterator over the list of key:valuelist items.
"""
return map(lambda key: (key, self.getlist(key)), self)
3
Example 39
def gen_calendars(start, stop, critical_dates):
"""
Generate calendars to use as inputs.
"""
all_dates = pd.date_range(start, stop, tz='utc')
for to_drop in map(list, powerset(critical_dates)):
# Have to yield tuples.
yield (all_dates.drop(to_drop),)
# Also test with the trading calendar.
trading_days = get_calendar("NYSE").all_days
yield (trading_days[trading_days.slice_indexer(start, stop)],)
3
Example 40
def create_merged_cdx_gen(sources, query):
"""
create a generator which loads and merges cdx streams
ensures cdxs are lazy loaded
"""
# Optimize: no need to merge if just one input
if len(sources) == 1:
cdx_iter = sources[0].load_cdx(query)
else:
source_iters = map(lambda src: src.load_cdx(query), sources)
cdx_iter = merge(*(source_iters))
for cdx in cdx_iter:
yield cdx
3
Example 41
def product(*args, **kwds):
# product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
# product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
pools = list(map(tuple, args)) * kwds.get('repeat', 1)
result = [[]]
for pool in pools:
result = [x+[y] for x in result for y in pool]
for prod in result:
yield tuple(prod)
3
Example 42
def iso_date_to_datetime(string):
"""
>>> iso_date_to_datetime('2013-12-26T10:11:12Z')
datetime.datetime(2013, 12, 26, 10, 11, 12)
>>> iso_date_to_datetime('2013-12-26T10:11:12Z')
datetime.datetime(2013, 12, 26, 10, 11, 12)
"""
nums = DATE_TIMESPLIT.split(string)
if nums[-1] == '':
nums = nums[:-1]
the_datetime = datetime.datetime(*map(int, nums))
return the_datetime
3
Example 43
def delete(self, names):
if isinstance(names, six.string_types):
names = self.sanitize(names)
else:
names = list(map(self.sanitize, names))
path = "metrics/%s" % names
payload = {}
if not isinstance(names, string_types):
payload = {'names': names}
path = "metrics"
return self._mexe(path, method="DELETE", query_props=payload)
3
Example 44
def __contains__(cls, member):
if super(ExtendableEnumMeta, cls).__contains__(member):
return True
def in_(subclass):
return member in subclass
return any(map(in_, cls.__subclasses__()))
3
Example 45
def __str__(self):
missing_args = list(map(str, self.missing_args))
return '%s is missing argument%s: %s' % (
self.format_callable(),
's' if len(missing_args) > 1 else '',
', '.join(missing_args),
)
3
Example 46
def getCodec(self, filename, codecs):
codecs = map(re.escape, codecs)
try:
codec = re.search('[^A-Z0-9](?P<codec>' + '|'.join(codecs) + ')[^A-Z0-9]', filename, re.I)
return (codec and codec.group('codec')) or ''
except:
return ''
3
Example 47
def __init__(self, question_file, annotation_file):
with timed_operation('Reading VQA JSON file'):
qobj, aobj = list(map(read_json, [question_file, annotation_file]))
self.task_type = qobj['task_type']
self.questions = qobj['questions']
self._size = len(self.questions)
self.anno = aobj['annotations']
assert len(self.anno) == len(self.questions), \
"{}!={}".format(len(self.anno), len(self.questions))
self._clean()
3
Example 48
def to_dict_with_content(fields, row, decrypt_func):
"""
Convert a SQLAlchemy row that contains a 'content' field to a dict.
``decrypt_func`` will be applied to the ``content`` field of the row.
If row is None, return None.
Raises AssertionError if there is no field named 'content' in ``fields``.
"""
assert(len(fields) == len(row))
field_names = list(map(_get_name, fields))
assert 'content' in field_names, "Missing content field."
result = dict(zip(field_names, row))
result['content'] = decrypt_func(result['content'])
return result
3
Example 49
def find_array_references(ast):
'''returns list of array references in AST'''
if type(ast) is c_ast.ArrayRef:
return [ast]
elif type(ast) is list:
return list(map(find_array_references, ast))
elif ast is None:
return []
else:
return reduce(operator.add, [find_array_references(o[1]) for o in ast.children()], [])
3
Example 50
def conf_warnings(conf):
"""check configuration `conf` and log warnings about non standard settings
if needed
"""
if conf['dry_run']:
logging.warn('note: DRY RUN mode is enabled')
if conf['backends'] != set(DEFAULT_CONFIG['infra']['backends'].split()):
logging.warn('only using backends: %s' % list(conf['backends']))
if conf['stages'] != updater.UPDATE_STAGES:
logging.warn('only doing stages: %s' %
list(map(updater.pp_stage, conf['stages'])))
if conf['force_triggers']:
logging.warn('forcing triggers: %s' % conf['force_triggers'])