本文整理汇总了Python中six.moves.izip函数的典型用法代码示例。如果您正苦于以下问题:Python izip函数的具体用法?Python izip怎么用?Python izip使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了izip函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: safe_str_cmp
def safe_str_cmp(a, b):
"""This function compares strings in somewhat constant time. This
requires that the length of at least one string is known in advance.
Returns `True` if the two strings are equal, or `False` if they are not.
.. versionadded:: 0.7
"""
if isinstance(a, text_type):
a = a.encode('utf-8')
if isinstance(b, text_type):
b = b.encode('utf-8')
if _builtin_safe_str_cmp is not None:
return _builtin_safe_str_cmp(a, b)
if len(a) != len(b):
return False
rv = 0
if PY2:
for x, y in izip(a, b):
rv |= ord(x) ^ ord(y)
else:
for x, y in izip(a, b):
rv |= x ^ y
return rv == 0
示例2: todok
def todok(self):
from .dok import dok_matrix
dok = dok_matrix((self.shape), dtype=self.dtype)
dok.update(izip(izip(self.row, self.col), self.data))
return dok
示例3: __getitem__
def __getitem__(self, key):
try:
if isinstance(key, int) and (key >= 0):
if key in self.cache:
return self.cache[key]
elif key < self.stop:
self.stop = 0
self.iterator = iter(self.f())
delta = key - self.stop
result = next(islice(self.iterator, delta, delta + 1))
self.cache[key] = result
self.stop = key + 1
return result
elif isinstance(key, slice):
if key.start is None and key.stop is None:
# Whole sequence is asked
return list(self.f())
start = key.start or 0
step = key.step or 1
indexes = count(start, step)
index_upd = start
while (key.stop is None or index_upd < key.stop) and index_upd in self.cache:
index_upd += step
if index_upd < self.stop and (key.stop is None or index_upd < key.stop):
self.iterator = iter(self.f())
result = list(islice(self.iterator, start, key.stop, step))
for i, value in izip(indexes, result):
self.cache[i] = value
self.stop = i + 1 if key.stop is None else key.stop
return result
else:
result = [self.cache[i] for i in six.moves.xrange(start, index_upd, step)]
if key.stop is None:
result_upd = list(islice(self.iterator, index_upd - self.stop, None, step))
elif index_upd < key.stop:
result_upd = list(islice(self.iterator, index_upd - self.stop, key.stop - self.stop, step))
else:
result_upd = []
for i, value in izip(indexes, result_upd):
self.cache[i] = value
self.stop = key.stop
return result + result_upd
else:
raise KeyError("Key must be non-negative integer or slice, not {}"
.format(key))
except StopIteration:
self.iterator = self.f()
self.stop = 0
raise
示例4: lazy_load_trees
def lazy_load_trees(skeleton_ids, node_properties):
""" Return a lazy collection of pairs of (long, DiGraph)
representing (skeleton_id, tree).
The node_properties is a list of strings, each being a name of a column
in the django model of the Treenode table that is not the treenode id, parent_id
or skeleton_id. """
values_list = ('id', 'parent_id', 'skeleton_id')
props = tuple(set(node_properties) - set(values_list))
values_list += props
ts = Treenode.objects.filter(skeleton__in=skeleton_ids) \
.order_by('skeleton') \
.values_list(*values_list)
skid = None
tree = None
for t in ts:
if t[2] != skid:
if tree:
yield (skid, tree)
# Prepare for the next one
skid = t[2]
tree = DiGraph()
fields = {k: v for k,v in izip(props, islice(t, 3, 3 + len(props)))}
tree.add_node(t[0], fields)
if t[1]:
# From child to parent
tree.add_edge(t[0], t[1])
if tree:
yield (skid, tree)
示例5: refresh
def refresh(self, items, consistent=False):
"""
Overwrite model data with freshest from database
Parameters
----------
items : list or :class:`~flywheel.models.Model`
Models to sync
consistent : bool, optional
If True, force a consistent read from the db. (default False)
"""
if isinstance(items, Model):
items = [items]
if not items:
return
tables = defaultdict(list)
for item in items:
tables[item.meta_.ddb_tablename(self.namespace)].append(item)
for tablename, items in six.iteritems(tables):
keys = [item.pk_dict_ for item in items]
results = self.dynamo.batch_get(tablename, keys,
consistent=consistent)
for item, data in izip(items, results):
with item.loading_(self):
for key, val in data.items():
item.set_ddb_val_(key, val)
示例6: get_sample
def get_sample(self, fit, factor=4, num=1):
vals = numpy.array(fit.model.thawedpars)
scales = self.scale.get_scales(fit)
samples = [numpy.random.uniform(val - factor * abs(scale),
val + factor * abs(scale),
int(num)) for val, scale in izip(vals, scales)]
return numpy.asarray(samples).T
示例7: get_scales
def get_scales(self, fit, myscales=None):
scales = []
thawedpars = [par for par in fit.model.pars if not par.frozen]
if None == myscales:
oldestmethod = fit.estmethod
covar = Covariance()
covar.config['sigma'] = self.sigma
fit.estmethod = Covariance()
try:
r = fit.est_errors()
finally:
fit.estmethod = oldestmethod
for par, val, lo, hi in izip(thawedpars, r.parvals, r.parmins, r.parmaxes):
scale = None
if lo is not None and hi is not None:
scale = numpy.abs(lo)
else:
warning("Covariance failed for '%s', trying Confidence..." %
par.fullname)
conf = Confidence()
conf.config['sigma'] = self.sigma
fit.estmethod = conf
try:
t = fit.est_errors(parlist=(par,))
if t.parmins[0] is not None and t.parmaxes[0] is not None:
scale = numpy.abs(t.parmins[0])
else:
if t.parmins[0] is None and t.parmaxes[0] is not None:
scale = numpy.abs(t.parmaxes[0])
else:
warning('1 sigma bounds for parameter ' +
par.fullname +
' could not be found, using soft limit minimum')
if 0.0 == numpy.abs(par.min):
scale = 1.0e-16
else:
scale = numpy.abs(par.min)
finally:
fit.estmethod = oldestmethod
scales.append(scale)
else:
if not numpy.iterable(myscales):
raise TypeError(
"scales option must be iterable of length %d " % len(thawedpars))
scales = list(map(abs, myscales))
scales = numpy.asarray(scales).transpose()
return scales
示例8: return_docs
def return_docs(self, return_doc_cb):
"""Return the changed documents and their last change generation
repeatedly invoking the callback return_doc_cb.
The final step of a sync exchange.
:param: return_doc_cb(doc, gen, trans_id): is a callback
used to return the documents with their last change generation
to the target replica.
:return: None
"""
changes_to_return = self.changes_to_return
# return docs, including conflicts
changed_doc_ids = [doc_id for doc_id, _, _ in changes_to_return]
self._trace('before get_docs')
docs = self._db.get_docs(
changed_doc_ids, check_for_conflicts=False, include_deleted=True)
docs_by_gen = izip(
docs, (gen for _, gen, _ in changes_to_return),
(trans_id for _, _, trans_id in changes_to_return))
_outgoing_trace = [] # for tests
for doc, gen, trans_id in docs_by_gen:
return_doc_cb(doc, gen, trans_id)
_outgoing_trace.append((doc.doc_id, doc.rev))
# for tests
self._db._last_exchange_log['return'] = {
'docs': _outgoing_trace,
'last_gen': self.new_gen}
示例9: unpack
def unpack(self, buff):
"""
Unpack the given binary buffer into the fields. The result
is a dictionary mapping field names to values.
"""
args = struct.unpack_from(self._fmt, buff[:self._size])
return dict(izip(self._names, args))
示例10: eval_model_to_fit
def eval_model_to_fit(self, modelfuncs):
total_model = []
for func, data in izip(modelfuncs, self.datasets):
total_model.append(data.eval_model_to_fit(func))
return numpy.concatenate(total_model)
示例11: azip
def azip(*iterables, **kwargs):
"""Move `axis` (default -1) to the front of ndarrays in `iterables`."""
from six.moves import map as imap, zip as izip
return izip(*(
imap(kwargs.get('func', unmask),
np.rollaxis(i, kwargs.get('axis', -1), kwargs.get('start', 0)))
if isinstance(i, np.ndarray) else i for i in iterables))
示例12: _parse_set_weight_values
def _parse_set_weight_values(argvish):
new_cmd_format, opts, args = validate_args(argvish)
# We'll either parse the all-in-one-string format or the
# --options format,
# but not both. If both are specified, raise an error.
try:
devs = []
if not new_cmd_format:
if len(args) % 2 != 0:
print(Commands.set_weight.__doc__.strip())
exit(EXIT_ERROR)
devs_and_weights = izip(islice(argvish, 0, len(argvish), 2),
islice(argvish, 1, len(argvish), 2))
for devstr, weightstr in devs_and_weights:
devs.extend(builder.search_devs(
parse_search_value(devstr)) or [])
weight = float(weightstr)
_set_weight_values(devs, weight)
else:
if len(args) != 1:
print(Commands.set_weight.__doc__.strip())
exit(EXIT_ERROR)
devs.extend(builder.search_devs(
parse_search_values_from_opts(opts)) or [])
weight = float(args[0])
_set_weight_values(devs, weight)
except ValueError as e:
print(e)
exit(EXIT_ERROR)
示例13: set_arrays
def set_arrays(filename, args, fields=None, ascii=True, clobber=False):
if os.path.isfile(filename) and not clobber:
raise IOErr("filefound", filename)
if not numpy.iterable(args) or len(args) == 0:
raise IOErr('noarrayswrite')
if not numpy.iterable(args[0]):
raise IOErr('noarrayswrite')
size = len(args[0])
for arg in args:
if not numpy.iterable(arg):
raise IOErr('noarrayswrite')
elif len(arg) != size:
raise IOErr('arraysnoteq')
if ascii and '[' not in filename and ']' not in filename:
filename += "[opt kernel=text/simple]"
tbl = pycrates.TABLECrate()
if fields is None:
fields = ['col%i' % (ii + 1) for ii in range(len(args))]
if len(args) != len(fields):
raise IOErr('toomanycols', str(len(fields)), str(len(args)))
for val, name in izip(args, fields):
_set_column(tbl, name, val)
pycrates.write_file(tbl, filename, clobber=True)
close_crate_dataset(tbl.get_dataset())
示例14: multiupdate_metadata
def multiupdate_metadata(self, keys, metadatas):
""" Update the metadata for a collection of keys.
Where supported by an implementation, this should perform the whole
collection of sets as a single transaction.
Like zip() if keys and metadatas have different lengths, then any excess
values in the longer list should be silently ignored.
Parameters
----------
keys : iterable of strings
The keys for the resources in the key-value store. Each key is a
unique identifier for a resource within the key-value store.
metadatas : iterable of dicts
An iterator that provides the metadata dictionaries for the
corresponding keys.
Events
------
StoreSetEvent :
On successful completion of a transaction, a StoreSetEvent should be
emitted with the key & metadata for each key that was set.
"""
with self.transaction('Updating metadata for '+', '.join('"%s"' % key for key in keys)):
for key, metadata in izip(keys, metadatas):
self.update_metadata(key, metadata)
示例15: par_at_boundary
def par_at_boundary( low, val, high, tol ):
for par_min, par_val, par_max in izip( low, val, high ):
if sao_fcmp( par_val, par_min, tol ) == 0:
return True
if sao_fcmp( par_val, par_max, tol ) == 0:
return True
return False