Back to home page

OSCL-LXR

 
 

    


0001 #
0002 # Licensed to the Apache Software Foundation (ASF) under one or more
0003 # contributor license agreements.  See the NOTICE file distributed with
0004 # this work for additional information regarding copyright ownership.
0005 # The ASF licenses this file to You under the Apache License, Version 2.0
0006 # (the "License"); you may not use this file except in compliance with
0007 # the License.  You may obtain a copy of the License at
0008 #
0009 # http://www.apache.org/licenses/LICENSE-2.0
0010 #
0011 # Unless required by applicable law or agreed to in writing, software
0012 # distributed under the License is distributed on an "AS IS" BASIS,
0013 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014 # See the License for the specific language governing permissions and
0015 # limitations under the License.
0016 #
0017 
0018 """
0019 Module defining global singleton classes.
0020 
0021 This module raises a RuntimeError if an attempt to reload it is made. In that
0022 way the identities of the classes defined here are fixed and will remain so
0023 even if pyspark itself is reloaded. In particular, a function like the following
0024 will still work correctly after pyspark is reloaded:
0025 
0026     def foo(arg=pyspark._NoValue):
0027         if arg is pyspark._NoValue:
0028             ...
0029 
0030 See gh-7844 for a discussion of the reload problem that motivated this module.
0031 
0032 Note that this approach is taken after from NumPy.
0033 """
0034 
0035 __ALL__ = ['_NoValue']
0036 
0037 
0038 # Disallow reloading this module so as to preserve the identities of the
0039 # classes defined here.
0040 if '_is_loaded' in globals():
0041     raise RuntimeError('Reloading pyspark._globals is not allowed')
0042 _is_loaded = True
0043 
0044 
0045 class _NoValueType(object):
0046     """Special keyword value.
0047 
0048     The instance of this class may be used as the default value assigned to a
0049     deprecated keyword in order to check if it has been given a user defined
0050     value.
0051 
0052     This class was copied from NumPy.
0053     """
0054     __instance = None
0055 
0056     def __new__(cls):
0057         # ensure that only one instance exists
0058         if not cls.__instance:
0059             cls.__instance = super(_NoValueType, cls).__new__(cls)
0060         return cls.__instance
0061 
0062     # needed for python 2 to preserve identity through a pickle
0063     def __reduce__(self):
0064         return (self.__class__, ())
0065 
0066     def __repr__(self):
0067         return "<no value>"
0068 
0069 
0070 _NoValue = _NoValueType()