本文整理汇总了Python中sunpy.extern.six.iteritems函数的典型用法代码示例。如果您正苦于以下问题:Python iteritems函数的具体用法?Python iteritems怎么用?Python iteritems使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了iteritems函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: merge
def merge(items, key=(lambda x: x)):
"""
Given sorted lists of iterables, return new iterable that returns
elements of all iterables sorted with respect to key.
"""
state = {}
for item in map(iter, items):
try:
first = next(item)
except StopIteration:
continue
else:
state[item] = (first, key(first))
while state:
for item, (value, tk) in six.iteritems(state):
# Value is biggest.
if all(tk >= k for it, (v, k) in six.iteritems(state)
if it is not item):
yield value
break
try:
n = next(item)
state[item] = (n, key(n))
except StopIteration:
del state[item]
示例2: super
def super(self, *args, **kwargs):
""" Like __call__, only that when you give it super(cls, obj) items,
it will skip the multimethod for cls and use the one for its parent
class. The normal __call__ does not consider this for performance
reasons. """
objs = self.get(*args, **kwargs)
types = tuple(
[
x.__thisclass__.__mro__[1] if isinstance(x, super) else type(x)
for x in objs
]
)
nargs = [
x.__self__ if isinstance(x, super) else x
for x in args
]
for k, elem in six.iteritems(kwargs):
if isinstance(elem, super):
kwargs[k] = elem.__self__
# This code is duplicate for performance reasons.
cached = self.cache.get(types, None)
if cached is not None:
return cached(*nargs, **kwargs)
for signature, fun in reversed(self.methods):
if all(issubclass(ty, sig) for ty, sig in zip(types, signature)):
self.cache[types] = fun
return fun(*nargs, **kwargs)
raise TypeError
示例3: _freeze
def _freeze(obj):
""" Create hashable representation of result dict. """
if isinstance(obj, dict):
return tuple((k, _freeze(v)) for k, v in iteritems(obj))
if isinstance(obj, list):
return tuple(_freeze(elem) for elem in obj)
return obj
示例4: _URL_followsPattern
def _URL_followsPattern(self, url):
"""Check whether the url provided follows the pattern"""
pattern = self.pattern
for k, v in six.iteritems(TIME_CONVERSIONS):
pattern = pattern.replace(k, v)
matches = re.match(pattern, url)
if matches:
return matches.end() == matches.endpos == len(self.now)
return False
示例5: _create
def _create(wlk, root, session):
query = session.query(DatabaseEntry)
for key, value in six.iteritems(root.attrs):
typ = key[0]
if typ == 'tag':
criterion = TableTag.name.in_([value])
# `key[1]` is here the `inverted` attribute of the tag. That means
# that if it is True, the given tag must not be included in the
# resulting entries.
if key[1]:
query = query.filter(~DatabaseEntry.tags.any(criterion))
else:
query = query.filter(DatabaseEntry.tags.any(criterion))
elif typ == 'fitsheaderentry':
key, val, inverted = value
key_criterion = TableFitsHeaderEntry.key == key
value_criterion = TableFitsHeaderEntry.value == val
if inverted:
query = query.filter(not_(and_(
DatabaseEntry.fits_header_entries.any(key_criterion),
DatabaseEntry.fits_header_entries.any(value_criterion))))
else:
query = query.filter(and_(
DatabaseEntry.fits_header_entries.any(key_criterion),
DatabaseEntry.fits_header_entries.any(value_criterion)))
elif typ == 'download time':
start, end, inverted = value
if inverted:
query = query.filter(
~DatabaseEntry.download_time.between(start, end))
else:
query = query.filter(
DatabaseEntry.download_time.between(start, end))
elif typ == 'path':
path, inverted = value
if inverted:
# pylint: disable=E711
query = query.filter(or_(
DatabaseEntry.path != path, DatabaseEntry.path == None))
else:
query = query.filter(DatabaseEntry.path == path)
elif typ == 'wave':
wavemin, wavemax, waveunit = value
query = query.filter(and_(
DatabaseEntry.wavemin >= wavemin,
DatabaseEntry.wavemax <= wavemax))
elif typ == 'time':
start, end, near = value
query = query.filter(and_(
DatabaseEntry.observation_time_start < end,
DatabaseEntry.observation_time_end > start))
else:
if typ.lower() not in SUPPORTED_SIMPLE_VSO_ATTRS.union(SUPPORTED_NONVSO_ATTRS):
raise NotImplementedError("The attribute {0!r} is not yet supported to query a database.".format(typ))
query = query.filter_by(**{typ: value})
return query.all()
示例6: _extractDateURL
def _extractDateURL(self, url):
"""Extracts the date from a particular url following the pattern"""
# remove the user and passwd from files if there:
url = url.replace("anonymous:[email protected]@", "")
# url_to_list substitutes '.' and '_' for '/' to then create
# a list of all the blocks in times - assuming they are all
# separated with either '.', '_' or '/'
url_to_list = lambda txt: re.sub(r'\.|_', '/', txt).split('/')
pattern_list = url_to_list(self.pattern)
url_list = url_to_list(url)
time_order = ['%Y', '%y', '%b', '%B', '%m', '%d', '%j',
'%H', '%I', '%M', '%S', '%e', '%f']
final_date = []
final_pattern = []
# Find in directory and filename
for pattern_elem, url_elem in zip(pattern_list, url_list):
time_formats = [x for x in time_order if x in pattern_elem]
if len(time_formats) > 0:
# Find whether there's text that should not be here
toremove = re.split('%.', pattern_elem)
if len(toremove) > 0:
for bit in toremove:
if bit != '':
url_elem = url_elem.replace(bit, '', 1)
pattern_elem = pattern_elem.replace(bit, '', 1)
final_date.append(url_elem)
final_pattern.append(pattern_elem)
for time_bit in time_formats:
time_order.remove(time_bit)
# Find and remove repeated elements eg: %Y in ['%Y', '%Y%m%d']
# Make all as single strings
date_together = ''.join(final_date)
pattern_together = ''.join(final_pattern)
re_together = pattern_together
for k, v in six.iteritems(TIME_CONVERSIONS):
re_together = re_together.replace(k, v)
# Lists to contain the unique elements of the date and the pattern
final_date = list()
final_pattern = list()
re_together = re_together.replace('[A-Z]', '\\[A-Z]')
for p, r in zip(pattern_together.split('%')[1:], re_together.split('\\')[1:]):
if p == 'e':
continue
regexp = r'\{}'.format(r) if not r.startswith('[') else r
pattern = '%{}'.format(p)
date_part = re.search(regexp, date_together)
date_together = date_together[:date_part.start()] + \
date_together[date_part.end():]
if pattern not in final_pattern:
final_pattern.append('%{}'.format(p))
final_date.append(date_part.group())
return datetime.datetime.strptime(' '.join(final_date),
' '.join(final_pattern))
示例7: _apply
def _apply(wlk, root, api, queryblock):
""" Implementation detail. """
for k, v in iteritems(root.attrs):
lst = k[-1]
rest = k[:-1]
block = queryblock
for elem in rest:
block = block[elem]
block[lst] = v
示例8: make_getdatarequest
def make_getdatarequest(self, response, methods=None, info=None):
""" Make datarequest with methods from response. """
if methods is None:
methods = self.method_order + ['URL']
return self.create_getdatarequest(
dict((k, [x.fileid for x in v])
for k, v in iteritems(self.by_provider(response))),
methods, info
)
示例9: to_be_removed
def to_be_removed(self):
"""Returns the key with the lowest times of access and its
corresponding value as a tuple.
"""
min_ = float('inf')
lfu_key = None
for k, v in six.iteritems(self.usage_counter):
if v < min_:
min_ = v
lfu_key = k
return lfu_key, self.get(lfu_key)
示例10: make
def make(self, atype, **kwargs):
""" Create new SOAP object with attributes specified in kwargs.
To assign subattributes, use foo__bar=1 to assign
['foo']['bar'] = 1. """
obj = self.api.factory.create(atype)
for k, v in iteritems(kwargs):
split = k.split('__')
tip = split[-1]
rest = split[:-1]
item = obj
for elem in rest:
item = item[elem]
if isinstance(v, dict):
# Do not throw away type information for dicts.
for k, v in iteritems(v):
item[tip][k] = v
else:
item[tip] = v
return obj
示例11: create_getdatarequest
def create_getdatarequest(self, maps, methods, info=None):
""" Create datarequest from maps mapping data provider to
fileids and methods, """
if info is None:
info = {}
return self.make(
'VSOGetDataRequest',
request__method__methodtype=methods,
request__info=info,
request__datacontainer__datarequestitem=[
self.make('DataRequestItem', provider=k, fileiditem__fileid=[v])
for k, v in iteritems(maps)
]
)
示例12: find_time
def find_time(string, format):
""" Return iterator of occurrences of date formatted with format
in string. Currently supported format codes: """
re_format = format
for key, value in six.iteritems(REGEX):
re_format = re_format.replace(key, value)
matches = re.finditer(re_format, string)
for match in matches:
try:
matchstr = string[slice(*match.span())]
dt = datetime.strptime(matchstr, format)
except ValueError:
continue
else:
yield dt
示例13: _extractDateURL
def _extractDateURL(self, url):
"""Extracts the date from a particular url following the pattern"""
# url_to_list substitutes '.' and '_' for '/' to then create
# a list of all the blocks in times - assuming they are all
# separated with either '.', '_' or '/'
url_to_list = lambda txt: re.sub(r'\.|_', '/', txt).split('/')
pattern_list = url_to_list(self.pattern)
url_list = url_to_list(url)
time_order = ['%Y', '%y', '%b', '%B', '%m', '%d', '%j',
'%H', '%I', '%M', '%S']
final_date = []
final_pattern = []
# Find in directory and filename
for pattern_elem, url_elem in zip(pattern_list, url_list):
time_formats = [x for x in time_order if x in pattern_elem]
if len(time_formats) > 0:
final_date.append(url_elem)
final_pattern.append(pattern_elem)
for time_bit in time_formats:
time_order.remove(time_bit)
# Find and remove repeated elements eg: %Y in ['%Y', '%Y%m%d']
# Make all as single strings
date_together = ''.join(final_date)
pattern_together = ''.join(final_pattern)
re_together = pattern_together
for k, v in six.iteritems(TIME_CONVERSIONS):
re_together = re_together.replace(k, v)
# Create new empty lists
final_date = list()
final_pattern = list()
for p,r in zip(pattern_together.split('%')[1:], re_together.split('\\')[1:]):
regexp = '\\{}'.format(r)
pattern = '%{}'.format(p)
date_part = re.match(regexp, date_together)
date_together = date_together[:date_part.start()] + \
date_together[date_part.end():]
if pattern not in final_pattern:
final_pattern.append('%{}'.format(p))
final_date.append(date_part.group())
return datetime.datetime.strptime(' '.join(final_date),
' '.join(final_pattern))
示例14: _close
def _close(self, callback, args, server):
""" Called after download is done. Activated queued downloads, call callback.
"""
callback(*args)
self.connections[server] -= 1
self.conns -= 1
if self.q[server]:
self._attempt_download(*self.q[server].pop())
else:
for k, v in iteritems(self.q): # pylint: disable=W0612
while v:
if self._attempt_download(*v[0]):
v.popleft()
if self.conns == self.max_total:
return
else:
break
示例15: print_all
def print_all(key=None):
"""
Provides a table of the complete list of constants.
Parameters
----------
key : Python string or unicode
Key in dictionary `constants`
Returns
-------
table : `astropy.table.Table`
"""
data_rows = []
for key, this_constant in iteritems(constants):
data_rows.append([key, this_constant.name, this_constant.value, this_constant.uncertainty,
str(this_constant.unit), this_constant.reference])
t = Table(rows=data_rows, names=('key', 'name', 'value', 'uncertainty', 'unit', 'Reference'))
return t