当前位置: 首页>>代码示例>>Python>>正文


Python toolz.valmap函数代码示例

本文整理汇总了Python中toolz.valmap函数的典型用法代码示例。如果您正苦于以下问题:Python valmap函数的具体用法?Python valmap怎么用?Python valmap使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了valmap函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: setup

    def setup(self):
        keys = self.keys

        while not keys.issubset(self.scheduler.tasks):
            yield gen.sleep(0.05)

        tasks = [self.scheduler.tasks[k] for k in keys]

        self.keys = None

        self.scheduler.add_plugin(self)  # subtle race condition here
        self.all_keys, errors = dependent_keys(tasks, complete=self.complete)
        if not self.complete:
            self.keys = self.all_keys.copy()
        else:
            self.keys, _ = dependent_keys(tasks, complete=False)
        self.all_keys.update(keys)
        self.keys |= errors & self.all_keys

        if not self.keys:
            self.stop(exception=None, key=None)

        # Group keys by func name
        self.keys = valmap(set, groupby(self.func, self.keys))
        self.all_keys = valmap(set, groupby(self.func, self.all_keys))
        for k in self.all_keys:
            if k not in self.keys:
                self.keys[k] = set()

        for k in errors:
            self.transition(k, None, 'erred', exception=True)
        logger.debug("Set up Progress keys")
开发者ID:tomMoral,项目名称:distributed,代码行数:32,代码来源:progress.py

示例2: function

 def function(scheduler, p):
     result = {'all': valmap(len, p.all_keys),
               'remaining': valmap(len, p.keys),
               'status': p.status}
     if p.status == 'error':
         result.update(p.extra)
     return result
开发者ID:tomMoral,项目名称:distributed,代码行数:7,代码来源:progressbar.py

示例3: _scatter

    def _scatter(self, data, workers=None, broadcast=False):
        """ Scatter data to local data dictionary

        Rather than send data out to the cluster we keep data local.  However
        we do report to the scheduler that the local worker has the scattered
        data.  This allows other workers to come by and steal this data if
        desired.

        Keywords like ``broadcast=`` do not work, however operations like
        ``.replicate`` work fine after calling scatter, which can fill in for
        this functionality.
        """
        with log_errors():
            if not (workers is None and broadcast is False):
                raise NotImplementedError("Scatter from worker doesn't support workers or broadcast keywords")

            if isinstance(data, dict) and not all(isinstance(k, (bytes, str))
                                                   for k in data):
                d = yield self._scatter(keymap(tokey, data), workers, broadcast)
                raise gen.Return({k: d[tokey(k)] for k in data})

            if isinstance(data, (list, tuple, set, frozenset)):
                keys = []
                for x in data:
                    try:
                        keys.append(tokenize(x))
                    except:
                        keys.append(str(uuid.uuid1()))
                data2 = dict(zip(keys, data))
            elif isinstance(data, dict):
                keys = set(data)
                data2 = data
            else:
                raise TypeError("Don't know how to scatter %s" % type(data))

            nbytes = valmap(sizeof, data2)

            # self.worker.data.update(data2)  # thread safety matters
            self.worker.loop.add_callback(self.worker.data.update, data2)

            yield self.scheduler.update_data(
                    who_has={key: [self.worker.address] for key in data2},
                    nbytes=valmap(sizeof, data2),
                    client=self.id)

            if isinstance(data, dict):
                out = {k: Future(k, self) for k in data}
            elif isinstance(data, (tuple, list, set, frozenset)):
                out = type(data)([Future(k, self) for k in keys])
            else:
                raise TypeError(
                        "Input to scatter must be a list or dict")

            for key in keys:
                self.futures[key]['status'] = 'finished'
                self.futures[key]['event'].set()

            raise gen.Return(out)
开发者ID:simonkamronn,项目名称:distributed,代码行数:58,代码来源:worker_executor.py

示例4: setup

    def setup(self, keys, complete):
        errors = Progress.setup(self, keys, complete)

        # Group keys by func name
        self.keys = valmap(set, groupby(self.func, self.keys))
        self.all_keys = valmap(set, groupby(self.func, self.all_keys))
        for k in self.all_keys:
            if k not in self.keys:
                self.keys[k] = set()

        logger.debug("Set up Progress keys")
        return errors
开发者ID:aterrel,项目名称:distributed,代码行数:12,代码来源:diagnostics.py

示例5: expect_kinds

def expect_kinds(**named):
    """
    Preprocessing decorator that verifies inputs have expected dtype kinds.

    Usage
    -----
    >>> from numpy import int64, int32, float32
    >>> @expect_kinds(x='i')
    ... def foo(x):
    ...    return x
    ...
    >>> foo(int64(2))
    2
    >>> foo(int32(2))
    2
    >>> foo(float32(2))
    Traceback (most recent call last):
       ...n
    TypeError: foo() expected a numpy object of kind 'i' for argument 'x', but got 'f' instead.  # noqa
    """
    for name, kind in iteritems(named):
        if not isinstance(kind, (str, tuple)):
            raise TypeError(
                "expect_dtype_kinds() expected a string or tuple of strings"
                " for argument {name!r}, but got {kind} instead.".format(
                    name=name, kind=dtype,
                )
            )

    @preprocess(kinds=call(lambda x: x if isinstance(x, tuple) else (x,)))
    def _expect_kind(kinds):
        """
        Factory for kind-checking functions that work the @preprocess
        decorator.
        """
        def error_message(func, argname, value):
            # If the bad value has a dtype, but it's wrong, show the dtype
            # kind.  Otherwise just show the value.
            try:
                value_to_show = value.dtype.kind
            except AttributeError:
                value_to_show = value
            return (
                "{funcname}() expected a numpy object of kind {kinds} "
                "for argument {argname!r}, but got {value!r} instead."
            ).format(
                funcname=_qualified_name(func),
                kinds=' or '.join(map(repr, kinds)),
                argname=argname,
                value=value_to_show,
            )

        def _actual_preprocessor(func, argname, argvalue):
            if getattrs(argvalue, ('dtype', 'kind'), object()) not in kinds:
                raise TypeError(error_message(func, argname, argvalue))
            return argvalue

        return _actual_preprocessor

    return preprocess(**valmap(_expect_kind, named))
开发者ID:280185386,项目名称:zipline,代码行数:60,代码来源:input_validation.py

示例6: merge_ownership_periods

def merge_ownership_periods(mappings):
    """
    Given a dict of mappings where the values are lists of
    OwnershipPeriod objects, returns a dict with the same structure with
    new OwnershipPeriod objects adjusted so that the periods have no
    gaps.

    Orders the periods chronologically, and pushes forward the end date
    of each period to match the start date of the following period. The
    end date of the last period pushed forward to the max Timestamp.
    """
    return valmap(
        lambda v: tuple(
            OwnershipPeriod(
                a.start,
                b.start,
                a.sid,
                a.value,
            ) for a, b in sliding_window(
                2,
                concatv(
                    sorted(v),
                    # concat with a fake ownership object to make the last
                    # end date be max timestamp
                    [OwnershipPeriod(
                        pd.Timestamp.max.tz_localize('utc'),
                        None,
                        None,
                        None,
                    )],
                ),
            )
        ),
        mappings,
    )
开发者ID:chrisvasquez,项目名称:zipline,代码行数:35,代码来源:assets.py

示例7: __init__

    def __init__(self, constructors, name, *args, **kwargs):
        args = tuple(map(self._unwrap_name, args))
        kwargs = valmap(self._unwrap_name, kwargs)
        already_bound = {}
        for n, arg in enumerate(args):
            if arg in already_bound:
                raise TypeError(
                    'argument %r at position %d is already bound to the'
                    ' positional argument at index %d' % (
                        arg,
                        n,
                        already_bound[arg],
                    ),
                )
            already_bound[arg] = n

        for k, arg in kwargs.items():
            if arg in already_bound:
                loc = already_bound[arg]
                raise TypeError(
                    'argument %r at keyword %s is already bound to the %s' % (
                        arg,
                        k,
                        ('positional argument at index %d' % loc)
                        if isinstance(loc, int) else
                        ('keyword argument %r' % loc),
                    ),
                )

        super().__init__(constructors, name, *args, **kwargs)
        del constructors[name]
        self._constructors = constructors
开发者ID:llllllllll,项目名称:adt,代码行数:32,代码来源:case.py

示例8: test_retrieve_specific_type

    def test_retrieve_specific_type(self, type_, lookup_name, failure_type):
        equities = make_simple_equity_info(
            range(5), start_date=pd.Timestamp("2014-01-01"), end_date=pd.Timestamp("2015-01-01")
        )
        max_equity = equities.index.max()
        futures = make_commodity_future_info(first_sid=max_equity + 1, root_symbols=["CL"], years=[2014])
        equity_sids = [0, 1]
        future_sids = [max_equity + 1, max_equity + 2, max_equity + 3]
        if type_ == Equity:
            success_sids = equity_sids
            fail_sids = future_sids
        else:
            fail_sids = equity_sids
            success_sids = future_sids

        with tmp_asset_finder(equities=equities, futures=futures) as finder:
            # Run twice to exercise caching.
            lookup = getattr(finder, lookup_name)
            for _ in range(2):
                results = lookup(success_sids)
                self.assertIsInstance(results, dict)
                self.assertEqual(set(results.keys()), set(success_sids))
                self.assertEqual(valmap(int, results), dict(zip(success_sids, success_sids)))
                self.assertEqual({type_}, {type(asset) for asset in itervalues(results)})
                with self.assertRaises(failure_type):
                    lookup(fail_sids)
                with self.assertRaises(failure_type):
                    # Should fail if **any** of the assets are bad.
                    lookup([success_sids[0], fail_sids[0]])
开发者ID:testmana2,项目名称:zipline,代码行数:29,代码来源:test_assets.py

示例9: assemble_ast

def assemble_ast(tag:str, idsclasses: Mapping[str, str], attrs: Mapping[str, str], body: list):
  """
  Small helper function for the template_2_ast function that assembles the appropriate ast element
  given the tag name, a dictionary of ids/classes from the tag name, further attrs, and a list of children or the body.
  For most components, there won't be any children.
  
  :param tag:
  :param idsclasses:
  :param attrs:
  :param body:
  :return:
  """
  iscomponent = re.match(r'^[A-Z]', tag)
  attrs['id'] = (attrs.get('id', '') + ' ' + idsclasses.get('id', '')).strip()
  attrs['class'] = (attrs.get('class', '') + ' ' + idsclasses.get('class', '')).strip()
  # remove the empty attributes to avoid clutter and save bytes.
  attrs = dict(t.valfilter(lambda x: not (isinstance(x, str) and x.strip() == ''), attrs))
  # special handling for the "style" attribute, since that can be a dictionary
  attrs = t.valmap(lambda val:' '.join('{}: {};'.format(k,v) for k,v in val.items())
                   if isinstance(val, dict) else val,
                   attrs)
  
  if iscomponent:
    return {'name': tag, 'props': attrs, 'children': body}
  else:
    return {'tag': tag, 'attrs': attrs, 'body': body}
开发者ID:vshesh,项目名称:glue,代码行数:26,代码来源:util.py

示例10: symbol_ownership_map

    def symbol_ownership_map(self):
        rows = sa.select(self.equity_symbol_mappings.c).execute().fetchall()

        mappings = {}
        for row in rows:
            mappings.setdefault((row.company_symbol, row.share_class_symbol), []).append(
                SymbolOwnership(
                    pd.Timestamp(row.start_date, unit="ns", tz="utc"),
                    pd.Timestamp(row.end_date, unit="ns", tz="utc"),
                    row.sid,
                    row.symbol,
                )
            )

        return valmap(
            lambda v: tuple(
                SymbolOwnership(a.start, b.start, a.sid, a.symbol)
                for a, b in sliding_window(
                    2,
                    concatv(
                        sorted(v),
                        # concat with a fake ownership object to make the last
                        # end date be max timestamp
                        [SymbolOwnership(pd.Timestamp.max.tz_localize("utc"), None, None, None)],
                    ),
                )
            ),
            mappings,
            factory=lambda: mappings,
        )
开发者ID:RoyHsiao,项目名称:zipline,代码行数:30,代码来源:assets.py

示例11: get_has_what

 def get_has_what(self, stream, keys=None):
     if keys:
         keys = [coerce_to_address(key) for key in keys]
     if keys is not None:
         return {k: list(self.has_what[k]) for k in keys}
     else:
         return valmap(list, self.has_what)
开发者ID:LiuXiaozeeee,项目名称:distributed,代码行数:7,代码来源:center.py

示例12: expect_dtypes

def expect_dtypes(**named):
    """
    Preprocessing decorator that verifies inputs have expected numpy dtypes.

    Usage
    -----
    >>> from numpy import dtype, arange, int8, float64
    >>> @expect_dtypes(x=dtype(int8))
    ... def foo(x, y):
    ...    return x, y
    ...
    >>> foo(arange(3, dtype=int8), 'foo')
    (array([0, 1, 2], dtype=int8), 'foo')
    >>> foo(arange(3, dtype=float64), 'foo')  # doctest: +NORMALIZE_WHITESPACE
    ...                                       # doctest: +ELLIPSIS
    Traceback (most recent call last):
       ...
    TypeError: ...foo() expected a value with dtype 'int8' for argument 'x',
    but got 'float64' instead.
    """
    for name, type_ in iteritems(named):
        if not isinstance(type_, (dtype, tuple)):
            raise TypeError(
                "expect_dtypes() expected a numpy dtype or tuple of dtypes"
                " for argument {name!r}, but got {dtype} instead.".format(
                    name=name, dtype=dtype,
                )
            )

    @preprocess(dtypes=call(lambda x: x if isinstance(x, tuple) else (x,)))
    def _expect_dtype(dtypes):
        """
        Factory for dtype-checking functions that work with the @preprocess
        decorator.
        """
        def error_message(func, argname, value):
            # If the bad value has a dtype, but it's wrong, show the dtype
            # name.  Otherwise just show the value.
            try:
                value_to_show = value.dtype.name
            except AttributeError:
                value_to_show = value
            return (
                "{funcname}() expected a value with dtype {dtype_str} "
                "for argument {argname!r}, but got {value!r} instead."
            ).format(
                funcname=_qualified_name(func),
                dtype_str=' or '.join(repr(d.name) for d in dtypes),
                argname=argname,
                value=value_to_show,
            )

        def _actual_preprocessor(func, argname, argvalue):
            if getattr(argvalue, 'dtype', object()) not in dtypes:
                raise TypeError(error_message(func, argname, argvalue))
            return argvalue

        return _actual_preprocessor

    return preprocess(**valmap(_expect_dtype, named))
开发者ID:4ever911,项目名称:zipline,代码行数:60,代码来源:input_validation.py

示例13: expect_element

def expect_element(*_pos, **named):
    """
    Preprocessing decorator that verifies inputs are elements of some
    expected collection.

    Usage
    -----
    >>> @expect_element(x=('a', 'b'))
    ... def foo(x):
    ...    return x.upper()
    ...
    >>> foo('a')
    'A'
    >>> foo('b')
    'B'
    >>> foo('c')
    Traceback (most recent call last):
       ...
    ValueError: foo() expected a value in ('a', 'b') for argument 'x', but got 'c' instead.  # noqa

    Notes
    -----
    This uses the `in` operator (__contains__) to make the containment check.
    This allows us to use any custom container as long as the object supports
    the container protocol.
    """
    if _pos:
        raise TypeError("expect_element() only takes keyword arguments.")

    return preprocess(**valmap(_expect_element, named))
开发者ID:louiekang,项目名称:zipline,代码行数:30,代码来源:input_validation.py

示例14: check_subset

 def check_subset(*ss_args, **ss_kwargs):
     """
     【装饰器】
     检查输入参数是否是某一集合的子集;检查失败raise CheckError
     :param ss_args: 参数集合tuple
     :param ss_kwargs: 参数集合dict
     :return: 
     """
     # 检查是否有不合规的tuple参数
     for ss in ss_args:
         if not isinstance(ss, (list, set, type(None))):
             raise TypeError(
                 "check_subset() expected a list or set or None of values"
                 ", but got {subset_} or tuple instead.".format(
                     subset_=str(type(ss)),
                 )
             )
     # 检查是否有不合规的dict参数
     for name, ss in six.iteritems(ss_kwargs):
         if not isinstance(ss, (list, set, type(None))):
             raise TypeError(
                 "check_subset() expected a list or set of values for "
                 "argument '{name_}', but got {subset_} or tuple instead.".format(
                     name_=name, subset_=str(type(ss)),
                 )
             )
     # 将subset_check函数作用在函数参数上
     return arg_process(*map(subset_check, list(ss_args)), **valmap(subset_check, ss_kwargs))
开发者ID:3774257,项目名称:abu,代码行数:28,代码来源:ABuChecker.py

示例15: md

def md(template, *args, **kwargs):
    """Wraps string.format with naive markdown escaping"""
    def escape(s):
        for char in ('*', '#', '_', '~', '`', '>'):
            s = s.replace(char, '\\' + char)
        return s
    return template.format(*map(escape, args), **toolz.valmap(escape, kwargs))
开发者ID:pcmoritz,项目名称:arrow,代码行数:7,代码来源:crossbow.py


注:本文中的toolz.valmap函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。