Python operator.methodcaller() Examples

The following are 30 code examples of operator.methodcaller(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module operator , or try the search function .
Example #1
Source File: test_utils.py    From flex with MIT License 6 votes vote down vote up
def test_mapping_with_mappings():
    input_ = {
        'foo': {
            'bar': 'error-a',
            'baz': 'error-b',
        },
        'bar': {
            'baz': 'error-c',
            'foo': 'error-d',
        }
    }
    expected = [
        "'foo':",
        "    - 'bar': 'error-a'",
        "    - 'baz': 'error-b'",
        "'bar':",
        "    - 'baz': 'error-c'",
        "    - 'foo': 'error-d'",
    ]
    actual = list(map(
        operator.methodcaller('replace', "u'", "'"),
        format_errors(input_),
    ))

    assert set(actual) == set(expected) 
Example #2
Source File: test_base.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_difference_incomparable(self, opname):
        a = pd.Index([3, pd.Timestamp('2000'), 1])
        b = pd.Index([2, pd.Timestamp('1999'), 1])
        op = operator.methodcaller(opname, b)

        # sort=None, the default
        result = op(a)
        expected = pd.Index([3, pd.Timestamp('2000'), 2, pd.Timestamp('1999')])
        if opname == 'difference':
            expected = expected[:2]
        tm.assert_index_equal(result, expected)

        # sort=False
        op = operator.methodcaller(opname, b, sort=False)
        result = op(a)
        tm.assert_index_equal(result, expected) 
Example #3
Source File: test_frame.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_set_axis_name_mi(self):
        df = DataFrame(
            np.empty((3, 3)),
            index=MultiIndex.from_tuples([("A", x) for x in list('aBc')]),
            columns=MultiIndex.from_tuples([('C', x) for x in list('xyz')])
        )

        level_names = ['L1', 'L2']
        funcs = ['_set_axis_name', 'rename_axis']
        for func in funcs:
            result = methodcaller(func, level_names)(df)
            assert result.index.names == level_names
            assert result.columns.names == [None, None]

            result = methodcaller(func, level_names, axis=1)(df)
            assert result.columns.names == ["L1", "L2"]
            assert result.index.names == [None, None] 
Example #4
Source File: operation.py    From vergeml with MIT License 6 votes vote down vote up
def __init__(self, apply=None):
        """
        :param apply: a tuple, a comma separated string or None
                      possible values are a combination of:
                        - x: apply the operation to the sample only
                        - y: apply the operation to the label only
                        - train: apply the operation only to the train split
                        - val: apply the operation only to the val split
                        - test: apply the operation only to the test split
                        - all: apply the operation to all
        """
        super().__init__()

        assert isinstance(apply, (list, tuple, str, type(None)))

        if apply == 'all':
            self.apply = set()
        elif isinstance(apply, (list, tuple)):
            self.apply = set(apply)
        elif isinstance(apply, str):
            self.apply = set(map(operator.methodcaller('strip'), apply.split(",")))
        else:
            self.apply = set()
            # CORRECT:
            # self.apply = {'train'} 
Example #5
Source File: json_format.py    From lambda-packs with MIT License 6 votes vote down vote up
def _AnyMessageToJsonObject(self, message):
    """Converts Any message according to Proto3 JSON Specification."""
    if not message.ListFields():
      return {}
    # Must print @type first, use OrderedDict instead of {}
    js = OrderedDict()
    type_url = message.type_url
    js['@type'] = type_url
    sub_message = _CreateMessageFromTypeUrl(type_url)
    sub_message.ParseFromString(message.value)
    message_descriptor = sub_message.DESCRIPTOR
    full_name = message_descriptor.full_name
    if _IsWrapperMessage(message_descriptor):
      js['value'] = self._WrapperMessageToJsonObject(sub_message)
      return js
    if full_name in _WKTJSONMETHODS:
      js['value'] = methodcaller(_WKTJSONMETHODS[full_name][0],
                                 sub_message)(self)
      return js
    return self._RegularMessageToJsonObject(sub_message, js) 
Example #6
Source File: json_format.py    From lambda-packs with MIT License 6 votes vote down vote up
def ConvertMessage(self, value, message):
    """Convert a JSON object into a message.

    Args:
      value: A JSON object.
      message: A WKT or regular protocol message to record the data.

    Raises:
      ParseError: In case of convert problems.
    """
    message_descriptor = message.DESCRIPTOR
    full_name = message_descriptor.full_name
    if _IsWrapperMessage(message_descriptor):
      self._ConvertWrapperMessage(value, message)
    elif full_name in _WKTJSONMETHODS:
      methodcaller(_WKTJSONMETHODS[full_name][1], value, message)(self)
    else:
      self._ConvertFieldValuePair(value, message) 
Example #7
Source File: json_format.py    From lambda-packs with MIT License 6 votes vote down vote up
def _ConvertAnyMessage(self, value, message):
    """Convert a JSON representation into Any message."""
    if isinstance(value, dict) and not value:
      return
    try:
      type_url = value['@type']
    except KeyError:
      raise ParseError('@type is missing when parsing any message.')

    sub_message = _CreateMessageFromTypeUrl(type_url)
    message_descriptor = sub_message.DESCRIPTOR
    full_name = message_descriptor.full_name
    if _IsWrapperMessage(message_descriptor):
      self._ConvertWrapperMessage(value['value'], sub_message)
    elif full_name in _WKTJSONMETHODS:
      methodcaller(
          _WKTJSONMETHODS[full_name][1], value['value'], sub_message)(self)
    else:
      del value['@type']
      self._ConvertFieldValuePair(value, sub_message)
    # Sets Any message
    message.value = sub_message.SerializeToString()
    message.type_url = type_url 
Example #8
Source File: test_operator.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def test_methodcaller(self):
        self.assertRaises(TypeError, operator.methodcaller)
        class A:
            def foo(self, *args, **kwds):
                return args[0] + args[1]
            def bar(self, f=42):
                return f
        a = A()
        f = operator.methodcaller('foo')
        self.assertRaises(IndexError, f, a)
        f = operator.methodcaller('foo', 1, 2)
        self.assertEqual(f(a), 3)
        self.assertRaises(TypeError, f)
        self.assertRaises(TypeError, f, a, 3)
        self.assertRaises(TypeError, f, a, spam=3)
        f = operator.methodcaller('bar')
        self.assertEqual(f(a), 42)
        self.assertRaises(TypeError, f, a, a)
        f = operator.methodcaller('bar', f=5)
        self.assertEqual(f(a), 5) 
Example #9
Source File: json_format.py    From auto-alt-text-lambda-api with MIT License 6 votes vote down vote up
def _AnyMessageToJsonObject(self, message):
    """Converts Any message according to Proto3 JSON Specification."""
    if not message.ListFields():
      return {}
    # Must print @type first, use OrderedDict instead of {}
    js = OrderedDict()
    type_url = message.type_url
    js['@type'] = type_url
    sub_message = _CreateMessageFromTypeUrl(type_url)
    sub_message.ParseFromString(message.value)
    message_descriptor = sub_message.DESCRIPTOR
    full_name = message_descriptor.full_name
    if _IsWrapperMessage(message_descriptor):
      js['value'] = self._WrapperMessageToJsonObject(sub_message)
      return js
    if full_name in _WKTJSONMETHODS:
      js['value'] = methodcaller(_WKTJSONMETHODS[full_name][0],
                                 sub_message)(self)
      return js
    return self._RegularMessageToJsonObject(sub_message, js) 
Example #10
Source File: json_format.py    From auto-alt-text-lambda-api with MIT License 6 votes vote down vote up
def ConvertMessage(self, value, message):
    """Convert a JSON object into a message.

    Args:
      value: A JSON object.
      message: A WKT or regular protocol message to record the data.

    Raises:
      ParseError: In case of convert problems.
    """
    message_descriptor = message.DESCRIPTOR
    full_name = message_descriptor.full_name
    if _IsWrapperMessage(message_descriptor):
      self._ConvertWrapperMessage(value, message)
    elif full_name in _WKTJSONMETHODS:
      methodcaller(_WKTJSONMETHODS[full_name][1], value, message)(self)
    else:
      self._ConvertFieldValuePair(value, message) 
Example #11
Source File: test_resample.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_resample_entirly_nat_window(self, method, unit):
        s = pd.Series([0] * 2 + [np.nan] * 2,
                      index=pd.date_range('2017', periods=4))
        # 0 / 1 by default
        result = methodcaller(method)(s.resample("2d"))
        expected = pd.Series([0.0, unit],
                             index=pd.to_datetime(['2017-01-01',
                                                   '2017-01-03']))
        tm.assert_series_equal(result, expected)

        # min_count=0
        result = methodcaller(method, min_count=0)(s.resample("2d"))
        expected = pd.Series([0.0, unit],
                             index=pd.to_datetime(['2017-01-01',
                                                   '2017-01-03']))
        tm.assert_series_equal(result, expected)

        # min_count=1
        result = methodcaller(method, min_count=1)(s.resample("2d"))
        expected = pd.Series([0.0, np.nan],
                             index=pd.to_datetime(['2017-01-01',
                                                   '2017-01-03']))
        tm.assert_series_equal(result, expected) 
Example #12
Source File: test_frame.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_set_axis_name_mi(self):
        df = DataFrame(
            np.empty((3, 3)),
            index=MultiIndex.from_tuples([("A", x) for x in list('aBc')]),
            columns=MultiIndex.from_tuples([('C', x) for x in list('xyz')])
        )

        level_names = ['L1', 'L2']
        funcs = ['_set_axis_name', 'rename_axis']
        for func in funcs:
            result = methodcaller(func, level_names)(df)
            assert result.index.names == level_names
            assert result.columns.names == [None, None]

            result = methodcaller(func, level_names, axis=1)(df)
            assert result.columns.names == ["L1", "L2"]
            assert result.index.names == [None, None] 
Example #13
Source File: test_operator.py    From BinderFilter with MIT License 6 votes vote down vote up
def test_methodcaller(self):
        self.assertRaises(TypeError, operator.methodcaller)
        class A:
            def foo(self, *args, **kwds):
                return args[0] + args[1]
            def bar(self, f=42):
                return f
        a = A()
        f = operator.methodcaller('foo')
        self.assertRaises(IndexError, f, a)
        f = operator.methodcaller('foo', 1, 2)
        self.assertEqual(f(a), 3)
        f = operator.methodcaller('bar')
        self.assertEqual(f(a), 42)
        self.assertRaises(TypeError, f, a, a)
        f = operator.methodcaller('bar', f=5)
        self.assertEqual(f(a), 5) 
Example #14
Source File: test_operator.py    From oss-ftp with MIT License 6 votes vote down vote up
def test_methodcaller(self):
        self.assertRaises(TypeError, operator.methodcaller)
        class A:
            def foo(self, *args, **kwds):
                return args[0] + args[1]
            def bar(self, f=42):
                return f
        a = A()
        f = operator.methodcaller('foo')
        self.assertRaises(IndexError, f, a)
        f = operator.methodcaller('foo', 1, 2)
        self.assertEqual(f(a), 3)
        f = operator.methodcaller('bar')
        self.assertEqual(f(a), 42)
        self.assertRaises(TypeError, f, a, a)
        f = operator.methodcaller('bar', f=5)
        self.assertEqual(f(a), 5) 
Example #15
Source File: windows.py    From pythonfinder with MIT License 6 votes vote down vote up
def find_all_python_versions(
        self,
        major=None,  # type: Optional[Union[str, int]]
        minor=None,  # type: Optional[int]
        patch=None,  # type: Optional[int]
        pre=None,  # type: Optional[bool]
        dev=None,  # type: Optional[bool]
        arch=None,  # type: Optional[str]
        name=None,  # type: Optional[str]
    ):
        # type (...) -> List[PathEntry]
        version_matcher = operator.methodcaller(
            "matches", major, minor, patch, pre, dev, arch, python_name=name
        )
        pythons = [py for py in self.version_list if version_matcher(py)]
        version_sort = operator.attrgetter("version_sort")
        return [
            c.comes_from for c in sorted(pythons, key=version_sort, reverse=True)
            if c.comes_from
        ] 
Example #16
Source File: windows.py    From pipenv with MIT License 6 votes vote down vote up
def find_all_python_versions(
        self,
        major=None,  # type: Optional[Union[str, int]]
        minor=None,  # type: Optional[int]
        patch=None,  # type: Optional[int]
        pre=None,  # type: Optional[bool]
        dev=None,  # type: Optional[bool]
        arch=None,  # type: Optional[str]
        name=None,  # type: Optional[str]
    ):
        # type (...) -> List[PathEntry]
        version_matcher = operator.methodcaller(
            "matches", major, minor, patch, pre, dev, arch, python_name=name
        )
        pythons = [py for py in self.version_list if version_matcher(py)]
        version_sort = operator.attrgetter("version_sort")
        return [
            c.comes_from for c in sorted(pythons, key=version_sort, reverse=True)
            if c.comes_from
        ] 
Example #17
Source File: test_utils.py    From flex with MIT License 6 votes vote down vote up
def test_mapping_with_iterables():
    input_ = {
        'foo': ['bar', 'baz'],
        'bar': ['baz', 'foo'],
    }
    expected = [
        "'foo':",
        "    0. 'bar'",
        "    1. 'baz'",
        "'bar':",
        "    0. 'baz'",
        "    1. 'foo'",
    ]
    actual = list(map(
        operator.methodcaller('replace', "u'", "'"),
        format_errors(input_),
    ))

    assert set(actual) == set(expected) 
Example #18
Source File: labeled_image.py    From vergeml with MIT License 6 votes vote down vote up
def _get_classes_from_json(self):

        for filename in ("labels.txt", "classes.json"):
            path = os.path.join(self.samples_dir, filename)
            if not os.path.exists(path):
                raise VergeMLError("{} is missing".format(filename))

            with open(path) as f:
                if filename == "labels.txt":
                    items = filter(None, map(methodcaller("strip"), f.read().splitlines()))
                    labels = Labels(items)
                else:
                    self.classes = json.load(f)
        files = {}
        # prefix the sample with input_dir
        for k, v in self.classes['files'].items():

            # on windows and linux, separator is /
            path = k.split("/")
            path.insert(0, self.samples_dir)
            fname = os.path.join(*path)
            files[fname] = v

        self.classes['files'] = files
        self.meta['labels'] = labels 
Example #19
Source File: json_format.py    From auto-alt-text-lambda-api with MIT License 6 votes vote down vote up
def _ConvertAnyMessage(self, value, message):
    """Convert a JSON representation into Any message."""
    if isinstance(value, dict) and not value:
      return
    try:
      type_url = value['@type']
    except KeyError:
      raise ParseError('@type is missing when parsing any message.')

    sub_message = _CreateMessageFromTypeUrl(type_url)
    message_descriptor = sub_message.DESCRIPTOR
    full_name = message_descriptor.full_name
    if _IsWrapperMessage(message_descriptor):
      self._ConvertWrapperMessage(value['value'], sub_message)
    elif full_name in _WKTJSONMETHODS:
      methodcaller(
          _WKTJSONMETHODS[full_name][1], value['value'], sub_message)(self)
    else:
      del value['@type']
      self._ConvertFieldValuePair(value, sub_message)
    # Sets Any message
    message.value = sub_message.SerializeToString()
    message.type_url = type_url 
Example #20
Source File: test_functions.py    From ibis with Apache License 2.0 5 votes vote down vote up
def test_string_strip(con, opname, expected):
    op = operator.methodcaller(opname)
    value = L('   foo   ')
    assert con.execute(op(value)) == expected 
Example #21
Source File: test_functions.py    From ibis with Apache License 2.0 5 votes vote down vote up
def test_cumulative_partitioned_ordered_window(alltypes, func, df):
    t = alltypes
    df = df.sort_values(['string_col', 'timestamp_col']).reset_index(drop=True)
    window = ibis.cumulative_window(
        order_by=t.timestamp_col, group_by=t.string_col
    )
    f = getattr(t.double_col, func)
    expr = t.projection([(t.double_col - f().over(window)).name('double_col')])
    result = expr.execute().double_col
    method = operator.methodcaller('cum{}'.format(func))
    expected = df.groupby(df.string_col).double_col.transform(
        lambda c: c - method(c)
    )
    tm.assert_series_equal(result, expected) 
Example #22
Source File: test_functions.py    From ibis with Apache License 2.0 5 votes vote down vote up
def test_isnull_notnull(con, raw_value, opname, expected):
    lit = L(raw_value)
    op = operator.methodcaller(opname)
    expr = op(lit)
    assert con.execute(expr) == expected 
Example #23
Source File: test_functions.py    From ibis with Apache License 2.0 5 votes vote down vote up
def test_date_extract_field(db, opname, expected):
    op = operator.methodcaller(opname)
    t = db.functional_alltypes
    expr = op(t.timestamp_col.cast('date')).distinct()
    result = expr.execute().astype(int)
    assert set(result) == expected 
Example #24
Source File: kademlia.py    From pyquarkchain with MIT License 5 votes vote down vote up
def buckets_by_distance_to(self, id: int) -> List[KBucket]:
        return sorted(self.buckets, key=operator.methodcaller("distance_to", id)) 
Example #25
Source File: python.py    From pythonfinder with MIT License 5 votes vote down vote up
def find_python_version(
        self,
        major=None,  # type: Optional[Union[str, int]]
        minor=None,  # type: Optional[int]
        patch=None,  # type: Optional[int]
        pre=None,  # type: Optional[bool]
        dev=None,  # type: Optional[bool]
        arch=None,  # type: Optional[str]
        name=None,  # type: Optional[str]
    ):
        # type: (...) -> Optional[PathEntry]
        """Search or self for the specified Python version and return the first match.

        :param major: Major version number.
        :type major: int
        :param int minor: Minor python version to search for, defaults to None
        :param int patch: Patch python version to search for, defaults to None
        :param bool pre: Search for prereleases (default None) - prioritize releases if None
        :param bool dev: Search for devreleases (default None) - prioritize releases if None
        :param str arch: Architecture to include, e.g. '64bit', defaults to None
        :param str name: The name of a python version, e.g. ``anaconda3-5.3.0``
        :returns: A :class:`~pythonfinder.models.PathEntry` instance matching the version requested.
        """

        sub_finder = operator.methodcaller(
            "find_python_version", major, minor, patch, pre, dev, arch, name
        )
        version_sort = operator.attrgetter("as_python.version_sort")
        unnested = [sub_finder(self.roots[path]) for path in self.roots]
        unnested = [
            p
            for p in unnested
            if p is not None and p.is_python and p.as_python is not None
        ]
        paths = sorted(list(unnested), key=version_sort, reverse=True)
        return next(iter(p for p in paths if p is not None), None) 
Example #26
Source File: mixins.py    From pythonfinder with MIT License 5 votes vote down vote up
def find_all_python_versions(
        self,
        major=None,  # type: Optional[Union[str, int]]
        minor=None,  # type: Optional[int]
        patch=None,  # type: Optional[int]
        pre=None,  # type: Optional[bool]
        dev=None,  # type: Optional[bool]
        arch=None,  # type: Optional[str]
        name=None,  # type: Optional[str]
    ):
        # type: (...) -> List[PathEntry]
        """Search for a specific python version on the path. Return all copies

        :param major: Major python version to search for.
        :type major: int
        :param int minor: Minor python version to search for, defaults to None
        :param int patch: Patch python version to search for, defaults to None
        :param bool pre: Search for prereleases (default None) - prioritize releases if None
        :param bool dev: Search for devreleases (default None) - prioritize releases if None
        :param str arch: Architecture to include, e.g. '64bit', defaults to None
        :param str name: The name of a python version, e.g. ``anaconda3-5.3.0``
        :return: A list of :class:`~pythonfinder.models.PathEntry` instances matching the version requested.
        :rtype: List[:class:`~pythonfinder.models.PathEntry`]
        """

        call_method = "find_all_python_versions" if self.is_dir else "find_python_version"
        sub_finder = operator.methodcaller(
            call_method, major, minor, patch, pre, dev, arch, name
        )
        if not self.is_dir:
            return sub_finder(self)
        unnested = [sub_finder(path) for path in expand_paths(self)]
        version_sort = operator.attrgetter("as_python.version_sort")
        unnested = [p for p in unnested if p is not None and p.as_python is not None]
        paths = sorted(unnested, key=version_sort, reverse=True)
        return list(paths) 
Example #27
Source File: path.py    From pythonfinder with MIT License 5 votes vote down vote up
def which(self, executable):
        # type: (str) -> Union[PathEntry, None]
        """
        Search for an executable on the path.

        :param executable: Name of the executable to be located.
        :type executable: str
        :returns: :class:`~pythonfinder.models.PathEntry` object.
        """

        sub_which = operator.methodcaller("which", executable)
        filtered = (sub_which(self.get_path(k)) for k in self.path_order)
        return next(iter(f for f in filtered if f is not None), None) 
Example #28
Source File: path.py    From pythonfinder with MIT License 5 votes vote down vote up
def find_all_python_versions(
        self,
        major=None,  # type: Optional[Union[str, int]]
        minor=None,  # type: Optional[int]
        patch=None,  # type: Optional[int]
        pre=None,  # type: Optional[bool]
        dev=None,  # type: Optional[bool]
        arch=None,  # type: Optional[str]
        name=None,  # type: Optional[str]
    ):
        # type (...) -> List[PathEntry]
        """Search for a specific python version on the path. Return all copies

        :param major: Major python version to search for.
        :type major: int
        :param int minor: Minor python version to search for, defaults to None
        :param int patch: Patch python version to search for, defaults to None
        :param bool pre: Search for prereleases (default None) - prioritize releases if None
        :param bool dev: Search for devreleases (default None) - prioritize releases if None
        :param str arch: Architecture to include, e.g. '64bit', defaults to None
        :param str name: The name of a python version, e.g. ``anaconda3-5.3.0``
        :return: A list of :class:`~pythonfinder.models.PathEntry` instances matching the version requested.
        :rtype: List[:class:`~pythonfinder.models.PathEntry`]
        """

        sub_finder = operator.methodcaller(
            "find_all_python_versions", major, minor, patch, pre, dev, arch, name
        )
        alternate_sub_finder = None
        if major and not (minor or patch or pre or dev or arch or name):
            alternate_sub_finder = operator.methodcaller(
                "find_all_python_versions", None, None, None, None, None, None, major
            )
        if os.name == "nt" and self.windows_finder:
            windows_finder_version = sub_finder(self.windows_finder)
            if windows_finder_version:
                return windows_finder_version
        values = list(self.get_pythons(sub_finder))
        if not values and alternate_sub_finder is not None:
            values = list(self.get_pythons(alternate_sub_finder))
        return values 
Example #29
Source File: join.py    From ibis with Apache License 2.0 5 votes vote down vote up
def _extract_predicate_names(predicates):
    lefts = []
    rights = []
    for predicate in map(operator.methodcaller('op'), predicates):
        if not isinstance(predicate, ops.Equals):
            raise TypeError(
                'Only equality join predicates supported with pandas'
            )
        left_name = predicate.left._name
        right_name = predicate.right._name
        lefts.append(left_name)
        rights.append(right_name)
    return lefts, rights 
Example #30
Source File: test_decimal.py    From ibis with Apache License 2.0 5 votes vote down vote up
def test_decimal_aggregate_function_type(lineitem, func):
    col = lineitem.l_extendedprice
    method = operator.methodcaller(func)
    result = method(col)
    assert isinstance(result, ir.DecimalScalar)
    assert result.type() == col.type()