dianfu commented on a change in pull request #11220: [FLINK-16249][python][ml] 
Add interfaces for Params, ParamInfo and WithParams
URL: https://github.com/apache/flink/pull/11220#discussion_r384916133
 
 

 ##########
 File path: flink-python/pyflink/ml/param/base.py
 ##########
 @@ -0,0 +1,356 @@
+################################################################################
+#  Licensed to the Apache Software Foundation (ASF) under one
+#  or more contributor license agreements.  See the NOTICE file
+#  distributed with this work for additional information
+#  regarding copyright ownership.  The ASF licenses this file
+#  to you 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.
+################################################################################
+import array
+import jsonpickle
+from typing import TypeVar, Generic
+
+V = TypeVar('V')
+
+
+class WithParams(Generic[V]):
+    """
+    Parameters are widely used in machine learning realm. This class defines a 
common
+    interface to interact with classes with parameters.
+    """
+
+    def get_params(self) -> 'Params':
+        """
+        Returns the all the parameters.
+
+        :return: all the parameters.
+        """
+        pass
+
+    def set(self, info: 'ParamInfo', value: V) -> 'WithParams':
+        """
+        Set the value of a specific parameter.
+
+        :param info: the info of the specific param to set.
+        :param value: the value to be set to the specific param.
+        :return: the WithParams itself.
+        """
+        self.get_params().set(info, value)
+        return self
+
+    def get(self, info: 'ParamInfo') -> V:
+        """
+        Returns the value of the specific param.
+
+        :param info: the info of the specific param, usually with default 
value.
+        :return: the value of the specific param, or default value defined in 
the
+        ParamInfo if the inner Params doesn't contains this param.
+        """
+        return self.get_params().get(info)
+
+    def _set(self, **kwargs):
+        """
+        Sets user-supplied params.
+        """
+        for param, value in kwargs.items():
+            p = getattr(self, param)
+            if value is not None:
+                try:
+                    value = p.type_converter(value)
+                except TypeError as e:
+                    raise TypeError('Invalid param value given for param "%s". 
%s' % (p.name, e))
+            self.get_params().set(p, value)
+        return self
+
+
+class Params(Generic[V]):
+    """
+    The map-like container class for parameter. This class is provided to unify
+    the interaction with parameters.
+    """
+
+    def __init__(self):
+        self._paramMap = {}
 
 Review comment:
   _paramMap -> _param_map?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to