Browse Source

Construct supports the filter function.

tags/v1.6.0
Lu 4 years ago
parent
commit
9f68d43c32
4 changed files with 59 additions and 2 deletions
  1. +1
    -0
      mindspore/_extends/parse/resources.py
  2. +8
    -0
      mindspore/_extends/parse/standard_method.py
  3. +3
    -2
      mindspore/_extends/parse/trope.py
  4. +47
    -0
      tests/ut/python/ops/test_filter.py

+ 1
- 0
mindspore/_extends/parse/resources.py View File

@@ -111,6 +111,7 @@ convert_object_map = {
T.len: M.ms_len,
T.bool_: M.bool_,
T.map: C.Map(),
T.filter: M.filter_,
T.partial: F.partial,
T.zip: C.zip_operation,
T.enumerate: M.enumerate_,


+ 8
- 0
mindspore/_extends/parse/standard_method.py View File

@@ -1793,3 +1793,11 @@ def list_append(self_, item):
def to_array(x):
"""Implementation of `to_array`."""
return x.__ms_to_array__()

def filter_(fun, iter_):
"""Support the use of built-in function filter."""
result = []
for elem in iter_:
if fun(elem):
result.append(elem)
return result

+ 3
- 2
mindspore/_extends/parse/trope.py View File

@@ -27,7 +27,8 @@ from operator import ( # noqa

# support system function call
from builtins import ( # noqa
bool, getattr, setattr, len, iter, next, pow, range, map, zip, print, enumerate, isinstance
bool, getattr, setattr, len, iter, next, pow, range, map, zip,
print, enumerate, isinstance, filter
)

# support functools
@@ -45,7 +46,7 @@ __all__ = ['add', 'sub', 'mul', 'truediv', 'floordiv', 'mod', 'eq', 'ne', 'lt',
'matmul', 'getitem', 'setitem',
'bool', 'getattr', 'setattr', 'len', 'iter', 'next', 'pow', 'range', 'map', 'zip',
'partial', 'print', 'enumerate', 'isinstance',
'exp', 'log', 'sin', 'cos', 'tan']
'exp', 'log', 'sin', 'cos', 'tan', 'filter']


def MakeTuple(*elts): # pragma: no cover


+ 47
- 0
tests/ut/python/ops/test_filter.py View File

@@ -0,0 +1,47 @@
# Copyright 2021 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
""" test_filter """

from mindspore.nn import Cell
from mindspore import context

context.set_context(mode=context.GRAPH_MODE, save_graphs=True)


def is_odd(x):
""" Judge whether the parameter is odd """

if x % 2:
return True
return False


class NetWork(Cell):
""" NetWork definition """

def __init__(self):
super(NetWork, self).__init__()
self.func = is_odd

def construct(self, list_):
set_func = filter
ret = set_func(self.func, list_)
return ret


list1 = [1, 2, 3]
net1 = NetWork()
result = net1(list1)
assert result == (1, 3)

Loading…
Cancel
Save