Github user HyukjinKwon commented on a diff in the pull request:
https://github.com/apache/spark/pull/19439#discussion_r148711768
--- Diff: python/pyspark/ml/image.py ---
@@ -0,0 +1,192 @@
+#
+# 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.
+#
+
+"""
+.. attribute:: ImageSchema
+
+ A singleton-like attribute of :class:`_ImageSchema` in this module.
--- End diff --
> Is this a standard way to define singletons in Python?
Up to my knowledge, there are many workarounds to resemble the singleton
but I am pretty sure there is no standard way AFAIK.
We have similar examples for this pattern within Spark:
https://github.com/apache/spark/blob/39e2bad6a866d27c3ca594d15e574a1da3ee84cc/python/pyspark/accumulators.py#L104
https://github.com/apache/spark/blob/39e2bad6a866d27c3ca594d15e574a1da3ee84cc/python/pyspark/storagelevel.py#L52-L58
There is an example for another approach within Spark actually:
https://github.com/apache/spark/blob/17af727e38c3faaeab5b91a8cdab5f2181cf3fc4/python/pyspark/sql/types.py#L96-L106
I am okay to use this approach as well. However, basically they should the
similar things in our case. This way also requires to create at least single
instance. The point of this approach I believe is to return the same instance
for `__init__` but I think our case should disallow `__init__` itself.
> What happens when this package or module gets reloaded?
I think reloading won't affect the functionalities here. It creates
`_ImageSchema` class again, creates an instance of `_ImageSchema` and
monkey-paches `__init__` to disallow creating instances.
```python
>>> from pyspark.ml import image
>>> print image.ImageSchema._imageSchema
None
>>> print image.ImageSchema.imageSchema
StructType(List(StructField(image,StructType(List(StructField(origin,StringType,true),StructField(height,IntegerType,false),StructField(width,IntegerType,false),StructField(nChannels,IntegerType,false),StructField(mode,IntegerType,false),StructField(data,BinaryType,false))),true)))
>>> print image.ImageSchema._imageSchema
StructType(List(StructField(image,StructType(List(StructField(origin,StringType,true),StructField(height,IntegerType,false),StructField(width,IntegerType,false),StructField(nChannels,IntegerType,false),StructField(mode,IntegerType,false),StructField(data,BinaryType,false))),true)))
>>> reload(image)
<module 'pyspark.ml.image' from '/.../spark/python/pyspark/ml/image.pyc'>
>>> print image.ImageSchema._imageSchema
None
>>> print image.ImageSchema.imageSchema
StructType(List(StructField(image,StructType(List(StructField(origin,StringType,true),StructField(height,IntegerType,false),StructField(width,IntegerType,false),StructField(nChannels,IntegerType,false),StructField(mode,IntegerType,false),StructField(data,BinaryType,false))),true)))
>>> print image.ImageSchema._imageSchema
StructType(List(StructField(image,StructType(List(StructField(origin,StringType,true),StructField(height,IntegerType,false),StructField(width,IntegerType,false),StructField(nChannels,IntegerType,false),StructField(mode,IntegerType,false),StructField(data,BinaryType,false))),true)))
```
I think we are fine if we happen to have multiple instances for
`ImageSchema` actually because it only has cached attributes in each instance.
> Numpy uses a somewhat different approach:
https://github.com/numpy/numpy/blob/d75b86c0c49f7eb3ec60564c2e23b3ff237082a2/numpy/_globals.py
I think Numpy case tries to keep the same object comparison clean, `a is
b`. In our case, I guess it does not require actually. (and partly this is a
reason why I used the term "singleton-like").
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]