github.com/apache/beam/sdks/v2@v2.48.2/python/apache_beam/internal/module_test.py (about) 1 # 2 # Licensed to the Apache Software Foundation (ASF) under one or more 3 # contributor license agreements. See the NOTICE file distributed with 4 # this work for additional information regarding copyright ownership. 5 # The ASF licenses this file to You under the Apache License, Version 2.0 6 # (the "License"); you may not use this file except in compliance with 7 # the License. You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 # 17 18 """Module used to define functions and classes used by the coder unit tests.""" 19 20 # pytype: skip-file 21 22 import re 23 import sys 24 from typing import Type 25 26 27 class TopClass(object): 28 class NestedClass(object): 29 def __init__(self, datum): 30 self.datum = 'X:%s' % datum 31 32 class MiddleClass(object): 33 class NestedClass(object): 34 def __init__(self, datum): 35 self.datum = 'Y:%s' % datum 36 37 38 def get_lambda_with_globals(): 39 return lambda s: re.findall(r'\w+', s) 40 41 42 def get_lambda_with_closure(message): 43 return lambda: 'closure: %s' % message 44 45 46 class Xyz(object): 47 """A class to be pickled.""" 48 def foo(self, s): 49 return re.findall(r'\w+', s) 50 51 52 def create_class(datum): 53 """Creates an unnamable class to be pickled.""" 54 class Z(object): 55 def get(self): 56 return 'Z:%s' % datum 57 58 return Z() 59 60 61 XYZ_OBJECT = Xyz() 62 63 64 class RecursiveClass(object): 65 """A class that contains a reference to itself.""" 66 67 SELF_TYPE = None # type: Type[RecursiveClass] 68 69 def __init__(self, datum): 70 self.datum = 'RecursiveClass:%s' % datum 71 72 73 RecursiveClass.SELF_TYPE = RecursiveClass 74 75 # pylint: disable=exec-used 76 if sys.version_info >= (3, 7): 77 # create dataclass to be pickled 78 exec( 79 ''' 80 from dataclasses import dataclass 81 @dataclass 82 class DataClass: 83 datum: str''')