本文整理汇总了Python中cyordereddict.OrderedDict.values方法的典型用法代码示例。如果您正苦于以下问题:Python OrderedDict.values方法的具体用法?Python OrderedDict.values怎么用?Python OrderedDict.values使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cyordereddict.OrderedDict
的用法示例。
在下文中一共展示了OrderedDict.values方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_iterators
# 需要导入模块: from cyordereddict import OrderedDict [as 别名]
# 或者: from cyordereddict.OrderedDict import values [as 别名]
def test_iterators(self):
pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
shuffle(pairs)
od = OrderedDict(pairs)
self.assertEqual(list(od), [t[0] for t in pairs])
self.assertEqual(list(od.keys()), [t[0] for t in pairs])
self.assertEqual(list(od.values()), [t[1] for t in pairs])
self.assertEqual(list(od.items()), pairs)
self.assertEqual(list(reversed(od)),
[t[0] for t in reversed(pairs)])
self.assertEqual(list(reversed(od.keys())),
[t[0] for t in reversed(pairs)])
self.assertEqual(list(reversed(od.values())),
[t[1] for t in reversed(pairs)])
self.assertEqual(list(reversed(od.items())), list(reversed(pairs)))
示例2: FormChoice
# 需要导入模块: from cyordereddict import OrderedDict [as 别名]
# 或者: from cyordereddict.OrderedDict import values [as 别名]
class FormChoice(FormDataDef):
def __init__(self, name, *args, **kwargs):
super(FormChoice, self).__init__(name, *args, **kwargs)
self.name = name
self.options = OrderedDict()
@classmethod
def all_from_json_definition(cls, definition, translation_list):
all_choices = {}
for choice_definition in definition:
choice_name = choice_definition.get('name')
choice_key = choice_definition.get('list_name')
if not choice_name or not choice_key:
continue
if choice_key not in all_choices:
all_choices[choice_key] = FormChoice(choice_key)
choices = all_choices[choice_key]
option = choices.options[choice_name] = {}
# apparently choices dont need a label if they have an image
if 'label' in choice_definition:
_label = choice_definition['label']
else:
_label = choice_definition.get('image')
if isinstance(_label, basestring):
_label = [_label]
elif _label is None and len(translation_list) == 1:
_label = [None]
option['labels'] = OrderedDict(zip(translation_list, _label))
option['name'] = choice_name
return all_choices
@property
def translations(self):
for option in self.options.values():
for translation in option['labels'].keys():
yield translation
示例3: startexp
# 需要导入模块: from cyordereddict import OrderedDict [as 别名]
# 或者: from cyordereddict.OrderedDict import values [as 别名]
#.........这里部分代码省略.........
test_tagged_sents[n] = [(word, tag) for word, (_, tag)
in zip(item.sent, sorted(item.tree.pos()))]
logging.info('%d test sentences after length restriction <= %d',
len(test_trees), prm.testcorpus.maxwords)
lexmodel = None
simplelexsmooth = False
test_tagged_sents_mangled = test_tagged_sents
if prm.postagging and prm.postagging.method in (
'treetagger', 'stanford', 'frog'):
if prm.postagging.method == 'treetagger':
# these two tags are never given by tree-tagger,
# so collect words whose tag needs to be overriden
overridetags = ('PTKANT', 'PIDAT')
elif prm.postagging.method == 'stanford':
overridetags = ('PTKANT', )
elif prm.postagging.method == 'frog':
overridetags = ()
taglex = defaultdict(set)
for sent in train_tagged_sents:
for word, tag in sent:
taglex[word].add(tag)
overridetagdict = {tag:
{word for word, tags in taglex.items() if tags == {tag}}
for tag in overridetags}
tagmap = {'$(': '$[', 'PAV': 'PROAV'}
test_tagged_sents_mangled = lexicon.externaltagging(
prm.postagging.method, prm.postagging.model, test_tagged_sents,
overridetagdict, tagmap)
if prm.postagging.retag and not rerun:
logging.info('re-tagging training corpus')
sents_to_tag = OrderedDict(enumerate(train_tagged_sents))
train_tagged_sents = lexicon.externaltagging(prm.postagging.method,
prm.postagging.model, sents_to_tag, overridetagdict,
tagmap).values()
for tree, tagged in zip(trees, train_tagged_sents):
for node in tree.subtrees(
lambda n: len(n) == 1 and isinstance(n[0], int)):
node.label = tagged[node[0]][1]
usetags = True # give these tags to parser
elif prm.postagging and prm.postagging.method == 'unknownword':
if not rerun:
sents, lexmodel = getposmodel(prm.postagging, train_tagged_sents)
simplelexsmooth = prm.postagging.simplelexsmooth
usetags = False # make sure gold POS tags are not given to parser
else:
usetags = True # give gold POS tags to parser
# 0: test sentences as they should be handed to the parser,
# 1: gold trees for evaluation purposes
# 2: gold sents because test sentences may be mangled by unknown word model
# 3: blocks from treebank file to reproduce the relevant part of the
# original treebank verbatim.
testset = OrderedDict((n, (
test_tagged_sents_mangled[n],
test_trees[n],
test_tagged_sents[n],
block))
for n, block in test_blocks.items())
if not test_tagged_sents:
raise ValueError('test corpus (selection) should be non-empty.')
if rerun:
trees, sents = [], []
roots = {t.label for t in trees} | {test_trees[n].label for n in testset}
if len(roots) != 1:
raise ValueError('expected unique ROOT label: %r' % roots)
示例4: PerformancePeriod
# 需要导入模块: from cyordereddict import OrderedDict [as 别名]
# 或者: from cyordereddict.OrderedDict import values [as 别名]
class PerformancePeriod(object):
def __init__(
self,
starting_cash,
period_open=None,
period_close=None,
keep_transactions=True,
keep_orders=False,
serialize_positions=True):
self.period_open = period_open
self.period_close = period_close
self.ending_value = 0.0
self.period_cash_flow = 0.0
self.pnl = 0.0
# sid => position object
self.positions = positiondict()
self.ending_cash = starting_cash
# rollover initializes a number of self's attributes:
self.rollover()
self.keep_transactions = keep_transactions
self.keep_orders = keep_orders
self.initialize_position_calc_arrays()
self.calculate_performance()
# An object to recycle via assigning new values
# when returning portfolio information.
# So as not to avoid creating a new object for each event
self._portfolio_store = zp.Portfolio()
self._account_store = zp.Account()
self._positions_store = zp.Positions()
self.serialize_positions = serialize_positions
self._unpaid_dividends = pd.DataFrame(
columns=zp.DIVIDEND_PAYMENT_FIELDS,
)
self.loc_map = {}
def initialize_position_calc_arrays(self):
# Arrays for quick calculations of positions value.
self._position_amounts = OrderedDict()
self._position_last_sale_prices = OrderedDict()
def set_positions(self, positions):
self.positions = positions
for sid, pos in positions.iteritems():
self._position_amounts[sid] = pos.amount
self._position_last_sale_prices[sid] = pos.last_sale_price
# Invalidate cache.
self._position_values = None # invalidate cache
def rollover(self):
self.starting_value = self.ending_value
self.starting_cash = self.ending_cash
self.period_cash_flow = 0.0
self.pnl = 0.0
self.processed_transactions = defaultdict(list)
self.orders_by_modified = defaultdict(OrderedDict)
self.orders_by_id = OrderedDict()
def set_position_amount(self, sid, amount):
self._position_amounts[sid] = amount
self._position_values = None # invalidate cache
def set_position_last_sale_price(self, sid, last_sale_price):
self._position_last_sale_prices[sid] = last_sale_price
self._position_values = None # invalidate cache
def handle_split(self, split):
if split.sid in self.positions:
# Make the position object handle the split. It returns the
# leftover cash from a fractional share, if there is any.
position = self.positions[split.sid]
leftover_cash = position.handle_split(split)
self._position_amounts[split.sid] = position.amount
self._position_last_sale_prices[split.sid] = \
position.last_sale_price
self._position_values = None # invalidate cache
if leftover_cash > 0:
self.handle_cash_payment(leftover_cash)
def earn_dividends(self, dividend_frame):
"""
Given a frame of dividends whose ex_dates are all the next trading day,
calculate and store the cash and/or stock payments to be paid on each
dividend's pay date.
"""
earned = dividend_frame.apply(self._maybe_earn_dividend, axis=1)\
.dropna(how='all')
if len(earned) > 0:
# Store the earned dividends so that they can be paid on the
# dividends' pay_dates.
self._unpaid_dividends = pd.concat(
#.........这里部分代码省略.........
示例5: FormPack
# 需要导入模块: from cyordereddict import OrderedDict [as 别名]
# 或者: from cyordereddict.OrderedDict import values [as 别名]
class FormPack(object):
# TODO: make a clear signature for __init__
def __init__(self, versions=None, title='Submissions', id_string=None,
default_version_id_key='__version__',
strict_schema=False,
root_node_name='data',
asset_type=None, submissions_xml=None):
if not versions:
versions = []
# accept a single version, but normalize it to an iterable
if isinstance(versions, dict):
versions = [versions]
self.versions = OrderedDict()
# the name of the field in submissions which stores the version ID
self.default_version_id_key = default_version_id_key
self.id_string = id_string
self.root_node_name = root_node_name
self.title = title
self.strict_schema = strict_schema
if len(self.title) > 31: # excel sheet name size limit
self.title = self.title[:28] + '...'
self.asset_type = asset_type
self.load_all_versions(versions)
def __repr__(self):
return '<FormPack %s>' % self._stats()
def version_id_keys(self, _versions=None):
# if no parameter is passed, default to 'all'
if _versions is None:
_versions = self.versions
_id_keys = []
for version in self.versions.values():
_id_key = version.version_id_key
if _id_key not in _id_keys:
_id_keys.append(_id_key)
return _id_keys
@property
def available_translations(self):
translations = set()
for version in self.versions.values():
translations.update(version.translations)
return translations
def lookup(self, prop, default=None):
# can't use a one liner because sometimes self.prop is None
result = getattr(self, prop, default)
if result is None:
return default
return result
def __getitem__(self, index):
try:
if isinstance(index, int):
return tuple(self.versions.values())[index]
else:
return self.versions[index]
except KeyError:
raise KeyError('formpack with version [%s] not found' % str(index))
except IndexError:
raise IndexError('version at index %d is not available' % index)
def _stats(self):
_stats = OrderedDict()
_stats['id_string'] = self.id_string
_stats['versions'] = len(self.versions)
# _stats['submissions'] = self.submissions_count()
_stats['row_count'] = len(self[-1].schema.get('content', {})
.get('survey', []))
# returns stats in the format [ key="value" ]
return '\n\t'.join('%s="%s"' % item for item in _stats.items())
def load_all_versions(self, versions):
for schema in versions:
self.load_version(deepcopy(schema))
def load_version(self, schema):
""" Load one version and attach it to this Formpack
All the metadata parsing is delegated to the FormVersion class,
hence several attributes for FormPack are populated on the fly
while getting versions loaded:
- title : the human readable name of the form. Match the one
from the most recent version.
- id_string : the human readable id of the form. The same for
all versions of the same FormPack.
#.........这里部分代码省略.........
示例6: AttrTree
# 需要导入模块: from cyordereddict import OrderedDict [as 别名]
# 或者: from cyordereddict.OrderedDict import values [as 别名]
class AttrTree(object):
"""
An AttrTree offers convenient, multi-level attribute access for
collections of objects. AttrTree objects may also be combined
together using the update method or merge classmethod. Here is an
example of adding a ViewableElement to an AttrTree and accessing it:
>>> t = AttrTree()
>>> t.Example.Path = 1
>>> t.Example.Path #doctest: +ELLIPSIS
1
"""
_disabled_prefixes = [] # Underscore attributes that should be
_sanitizer = util.sanitize_identifier
@classmethod
def merge(cls, trees):
"""
Merge a collection of AttrTree objects.
"""
first = trees[0]
for tree in trees:
first.update(tree)
return first
def __dir__(self):
"""
The _dir_mode may be set to 'default' or 'user' in which case
only the child nodes added by the user are listed.
"""
dict_keys = self.__dict__.keys()
if self.__dict__['_dir_mode'] == 'user':
return self.__dict__['children']
else:
return dir(type(self)) + list(dict_keys)
def __init__(self, items=None, identifier=None, parent=None, dir_mode='default'):
"""
identifier: A string identifier for the current node (if any)
parent: The parent node (if any)
items: Items as (path, value) pairs to construct
(sub)tree down to given leaf values.
Note that the root node does not have a parent and does not
require an identifier.
"""
self.__dict__['parent'] = parent
self.__dict__['identifier'] = type(self)._sanitizer(identifier, escape=False)
self.__dict__['children'] = []
self.__dict__['_fixed'] = False
self.__dict__['_dir_mode'] = dir_mode # Either 'default' or 'user'
fixed_error = 'No attribute %r in this AttrTree, and none can be added because fixed=True'
self.__dict__['_fixed_error'] = fixed_error
self.__dict__['data'] = OrderedDict()
items = items.items() if isinstance(items, OrderedDict) else items
# Python 3
items = list(items) if items else items
items = [] if not items else items
for path, item in items:
self.set_path(path, item)
@property
def path(self):
"Returns the path up to the root for the current node."
if self.parent:
return '.'.join([self.parent.path, str(self.identifier)])
else:
return self.identifier if self.identifier else self.__class__.__name__
@property
def fixed(self):
"If fixed, no new paths can be created via attribute access"
return self.__dict__['_fixed']
@fixed.setter
def fixed(self, val):
self.__dict__['_fixed'] = val
def update(self, other):
"""
Updated the contents of the current AttrTree with the
contents of a second AttrTree.
"""
if not isinstance(other, AttrTree):
raise Exception('Can only update with another AttrTree type.')
fixed_status = (self.fixed, other.fixed)
(self.fixed, other.fixed) = (False, False)
for identifier, element in other.items():
if identifier not in self.data:
self[identifier] = element
else:
self[identifier].update(element)
(self.fixed, other.fixed) = fixed_status
def set_path(self, path, val):
#.........这里部分代码省略.........
示例7: externaltagging
# 需要导入模块: from cyordereddict import OrderedDict [as 别名]
# 或者: from cyordereddict.OrderedDict import values [as 别名]
def externaltagging(usetagger, model, sents, overridetag, tagmap):
"""Use an external tool to tag a list of sentences."""
logging.info('Start tagging.')
goldtags = [t for sent in sents.values() for _, t in sent]
if usetagger == 'treetagger': # Tree-tagger
if not os.path.exists('tree-tagger/bin/tree-tagger'):
raise ValueError(TREETAGGERHELP)
infile, inname = tempfile.mkstemp(text=True)
with os.fdopen(infile, 'w') as infile:
for tagsent in sents.values():
sent = map(itemgetter(0), tagsent)
infile.write('\n'.join(w.encode('utf-8')
for w in sent) + '\n<S>\n')
filtertags = ''
if not model:
model = 'tree-tagger/lib/german-par-linux-3.2-utf8.bin'
filtertags = '| tree-tagger/cmd/filter-german-tags'
tagger = Popen('tree-tagger/bin/tree-tagger -token -sgml'
' %s %s %s' % (model, inname, filtertags),
stdout=PIPE, shell=True)
tagout = tagger.stdout.read(
).decode('utf-8').split('<S>')[:-1]
os.unlink(inname)
taggedsents = OrderedDict((n, [tagmangle(a, None, overridetag, tagmap)
for a in tags.splitlines() if a.strip()])
for n, tags in zip(sents, tagout))
elif usetagger == 'stanford': # Stanford Tagger
if not os.path.exists('stanford-postagger-full-2012-07-09'):
raise ValueError(STANFORDTAGGERHELP)
infile, inname = tempfile.mkstemp(text=True)
with os.fdopen(infile, 'w') as infile:
for tagsent in sents.values():
sent = map(itemgetter(0), tagsent)
infile.write(' '.join(w.encode('utf-8')
for w in sent) + '\n')
if not model:
model = 'models/german-hgc.tagger'
tagger = Popen(args=(
'/usr/bin/java -mx2G -classpath stanford-postagger.jar'
' edu.stanford.nlp.tagger.maxent.MaxentTagger'
' -tokenize false -encoding utf-8'
' -model %s -textFile %s' % (model, inname)).split(),
cwd='stanford-postagger-full-2012-07-09',
shell=False, stdout=PIPE)
tagout = tagger.stdout.read(
).decode('utf-8').splitlines()
os.unlink(inname)
taggedsents = OrderedDict((n, [tagmangle(a, '_', overridetag, tagmap)
for a in tags.split()]) for n, tags in zip(sents, tagout))
elif usetagger == 'frog': # Dutch 'frog' tagger
tagger = Popen(args=[which('frog')] +
'-n --skip=tacmnp -t /dev/stdin'.split(),
shell=False, stdin=PIPE, stdout=PIPE)
tagout, stderr = tagger.communicate(''.join(
' '.join(w for w in map(itemgetter(0), tagsent)) + '\n'
for tagsent in sents.values()).encode('utf8'))
logging.info(stderr)
# lines consist of: 'idx token lemma POS score'
taggedsents = OrderedDict((n,
[(line.split()[1],
line.split()[3].replace('(', '[').replace(')', ']'))
for line in lines.splitlines()]) for n, lines
in zip(sents, tagout.decode('utf-8').split('\n\n')))
if len(taggedsents) != len(sents):
raise ValueError('mismatch in number of sentences after tagging.')
for n, tags in taggedsents.items():
if len(sents[n]) != len(tags):
raise ValueError('mismatch in number of tokens after tagging.\n'
'before: %r\nafter: %r' % (sents[n], tags))
newtags = [t for sent in taggedsents.values() for _, t in sent]
logging.info('Tag accuracy: %5.2f\ngold - cand: %r\ncand - gold %r',
(100 * accuracy(goldtags, newtags)),
set(goldtags) - set(newtags), set(newtags) - set(goldtags))
return taggedsents
示例8: PositionTracker
# 需要导入模块: from cyordereddict import OrderedDict [as 别名]
# 或者: from cyordereddict.OrderedDict import values [as 别名]
class PositionTracker(object):
def __init__(self):
# sid => position object
self.positions = positiondict()
# Arrays for quick calculations of positions value
self._position_amounts = OrderedDict()
self._position_last_sale_prices = OrderedDict()
self._unpaid_dividends = pd.DataFrame(
columns=zp.DIVIDEND_PAYMENT_FIELDS,
)
self._positions_store = zp.Positions()
def update_last_sale(self, event):
# NOTE, PerformanceTracker already vetted as TRADE type
sid = event.sid
if sid not in self.positions:
return
price = event.price
if not checknull(price):
pos = self.positions[sid]
pos.last_sale_date = event.dt
pos.last_sale_price = price
self._position_last_sale_prices[sid] = price
self._position_values = None # invalidate cache
sid = event.sid
price = event.price
def update_positions(self, positions):
# update positions in batch
self.positions.update(positions)
for sid, pos in iteritems(positions):
self._position_amounts[sid] = pos.amount
self._position_last_sale_prices[sid] = pos.last_sale_price
# Invalidate cache.
self._position_values = None # invalidate cache
def update_position(self, sid, amount=None, last_sale_price=None,
last_sale_date=None, cost_basis=None):
pos = self.positions[sid]
if amount is not None:
pos.amount = amount
self._position_amounts[sid] = amount
self._position_values = None # invalidate cache
if last_sale_price is not None:
pos.last_sale_price = last_sale_price
self._position_last_sale_prices[sid] = last_sale_price
self._position_values = None # invalidate cache
if last_sale_date is not None:
pos.last_sale_date = last_sale_date
if cost_basis is not None:
pos.cost_basis = cost_basis
def execute_transaction(self, txn):
# Update Position
# ----------------
sid = txn.sid
position = self.positions[sid]
position.update(txn)
self._position_amounts[sid] = position.amount
self._position_last_sale_prices[sid] = position.last_sale_price
self._position_values = None # invalidate cache
def handle_commission(self, commission):
# Adjust the cost basis of the stock if we own it
if commission.sid in self.positions:
self.positions[commission.sid].\
adjust_commission_cost_basis(commission)
_position_values = None
@property
def position_values(self):
"""
Invalidate any time self._position_amounts or
self._position_last_sale_prices is changed.
"""
if self._position_values is None:
vals = list(map(mul, self._position_amounts.values(),
self._position_last_sale_prices.values()))
self._position_values = vals
return self._position_values
def calculate_positions_value(self):
if len(self.position_values) == 0:
return np.float64(0)
return sum(self.position_values)
def _longs_count(self):
return sum(map(lambda x: x > 0, self.position_values))
def _long_exposure(self):
return sum(filter(lambda x: x > 0, self.position_values))
def _shorts_count(self):
return sum(map(lambda x: x < 0, self.position_values))
#.........这里部分代码省略.........