def test(): assert get_equivalence_classes([1, 2, 3, 1j, 2j, 3j, 1+1j, 2+2j, 3+3j], abs) == { 1: set((1, 1j,)), 2: set((2, 2j,)), 3: set((3, 3j,)), abs(1 + 1j): set((1 + 1j,)), abs(2 + 2j): set((2 + 2j,)), abs(3 + 3j): set((3 + 3j,)), } assert get_equivalence_classes({1: 2, 3: 4, 'meow': 2}) == \ {2: set((1, 'meow')), 4: set((3,))}
def test(): assert get_equivalence_classes([1, 2, 3, 1j, 2j, 3j, 1+1j, 2+2j, 3+3j], abs) == { 1: {1, 1j}, 2: {2, 2j}, 3: {3, 3j}, abs(1 + 1j): {1 + 1j}, abs(2 + 2j): {2 + 2j}, abs(3 + 3j): {3 + 3j}, } assert get_equivalence_classes({1: 2, 3: 4, 'meow': 2}) == \ {2: {1, 'meow'}, 4: {3}}
def test(): assert get_equivalence_classes( [1, 2, 3, 1j, 2j, 3j, 1 + 1j, 2 + 2j, 3 + 3j], abs) == { 1: {1, 1j}, 2: {2, 2j}, 3: {3, 3j}, abs(1 + 1j): {1 + 1j}, abs(2 + 2j): {2 + 2j}, abs(3 + 3j): {3 + 3j}, }
def test(): assert get_equivalence_classes([1, 2, 3, 1j, 2j, 3j, 1+1j, 2+2j, 3+3j], abs) == { 1: {1, 1j}, 2: {2, 2j}, 3: {3, 3j}, abs(1 + 1j): {1 + 1j}, abs(2 + 2j): {2 + 2j}, abs(3 + 3j): {3 + 3j}, }
def test(): assert get_equivalence_classes( [1, 2, 3, 1j, 2j, 3j, 1 + 1j, 2 + 2j, 3 + 3j], abs) == { 1: set(( 1, 1j, )), 2: set(( 2, 2j, )), 3: set(( 3, 3j, )), abs(1 + 1j): set((1 + 1j, )), abs(2 + 2j): set((2 + 2j, )), abs(3 + 3j): set((3 + 3j, )), } assert get_equivalence_classes({1: 2, 3: 4, 'meow': 2}) == \ {2: set((1, 'meow')), 4: set((3,))}
def test_ordered_dict_output(): # Insertion order: assert get_equivalence_classes( nifty_collections.OrderedDict(((1, 2), (3, 4), ('meow', 2))), use_ordered_dict=True) == \ nifty_collections.OrderedDict([(2, set((1, 'meow'))), (4, set((3,)))]) assert get_equivalence_classes( nifty_collections.OrderedDict((('meow', 2), (1, 2), (3, 4))), use_ordered_dict=True) == \ nifty_collections.OrderedDict([(2, set((1, 'meow'))), (4, set((3,)))]) assert get_equivalence_classes( nifty_collections.OrderedDict(((3, 4), (1, 2), ('meow', 2))), use_ordered_dict=True) == \ nifty_collections.OrderedDict([(4, set((3,))), (2, set((1, 'meow',)))]) assert get_equivalence_classes( nifty_collections.OrderedDict(((1, 2), (3, 4), ('meow', 2))), container=tuple, use_ordered_dict=True) == \ nifty_collections.OrderedDict([(2, (1, 'meow')), (4, (3,))]) assert get_equivalence_classes( nifty_collections.OrderedDict((('meow', 2), (1, 2), (3, 4))), container=tuple, use_ordered_dict=True) == \ nifty_collections.OrderedDict([(2, ('meow', 1)), (4, (3,))]) # Sorting: assert get_equivalence_classes({1: 2, 3: 4, 'meow': 2}, sort_ordered_dict=True) == \ nifty_collections.OrderedDict([(2, set((1, 'meow'))), (4, set((3,)))]) assert get_equivalence_classes({1: 2, 3: 4, 'meow': 2}, sort_ordered_dict=lambda x: -x) == \ nifty_collections.OrderedDict([(4, set((3,))), (2, set((1, 'meow')))])
def test_iterable_input(): assert get_equivalence_classes(range(1, 5), str) == \ {'1': set((1,)), '2': set((2,)), '3': set((3,)), '4': set((4,)),} assert get_equivalence_classes([1, 2+3j, 4, 5-6j], 'imag') \ == {0: set((1, 4)), 3: set((2+3j,)), -6: set((5-6j,))}
def get_packages_and_modules_filenames(root, recursive=False): ''' Find the filenames of all of the packages and modules inside the package. `root` may be a module, package, or a path. todo: module? really? todo: needs testing ''' from python_toolbox import logic_tools if isinstance(root, types.ModuleType): root_module = root root_path = pathlib.Path(root_module).parent elif isinstance(root, (str, pathlib.PurePath)): root_path = pathlib.Path(root).absolute() # Not making `root_module`, it might not be imported. ###################################################### result = [] for entry in os.listdir(root_path): full_path = root_path / entry if is_module(full_path): result.append(entry) continue elif is_package(full_path): result.append(entry) if recursive: inner_results = get_packages_and_modules_filenames( full_path, recursive=True) result += [entry / thing for thing in inner_results] ### Filtering out duplicate filenames for the same module: ################ # # filename_to_module_name = {filename: filename.stem for filename in result} module_name_to_filenames = \ logic_tools.get_equivalence_classes(filename_to_module_name) for module_name, filenames in module_name_to_filenames.items(): if len(filenames) <= 1: # Does this save us from the case of packages? continue filenames_by_priority = sorted( filenames, key=lambda filename: _extensions_by_priority.index(filename.suffix ), ) redundant_filenames = filenames_by_priority[1:] for redundant_filename in redundant_filenames: result.remove(redundant_filename) # # ### Done filtering duplicate filenames for the same module. ############### return [root_path / entry for entry in result]
def test_iterable_input(): assert get_equivalence_classes(range(1, 5), str) == \ {'1': {1}, '2': {2}, '3': {3}, '4': {4},} assert get_equivalence_classes([1, 2+3j, 4, 5-6j], 'imag') \ == {0: {1, 4}, 3: {2+3j}, -6: {5-6j}}
def get_packages_and_modules_filenames(root, recursive=False): ''' Find the filenames of all of the packages and modules inside the package. `root` may be a module, package, or a path. todo: module? really? todo: needs testing ''' from python_toolbox import logic_tools if isinstance(root, types.ModuleType): root_module = root root_path = pathlib.Path(root_module).parent elif isinstance(root, (str, pathlib.PurePath)): root_path = pathlib.Path(root).absolute() # Not making `root_module`, it might not be imported. ###################################################### result = [] for entry in os.listdir(root_path): full_path = root_path / entry if is_module(full_path): result.append(entry) continue elif is_package(full_path): result.append(entry) if recursive: inner_results = get_packages_and_modules_filenames( full_path, recursive=True ) result += [entry / thing for thing in inner_results] ### Filtering out duplicate filenames for the same module: ################ # # filename_to_module_name = { filename: filename.stem for filename in result } module_name_to_filenames = \ logic_tools.get_equivalence_classes(filename_to_module_name) for module_name, filenames in module_name_to_filenames.items(): if len(filenames) <= 1: # Does this save us from the case of packages? continue filenames_by_priority = sorted( filenames, key=lambda filename: _extensions_by_priority.index(filename.suffix), ) redundant_filenames = filenames_by_priority[1:] for redundant_filename in redundant_filenames: result.remove(redundant_filename) # # ### Done filtering duplicate filenames for the same module. ############### return [root_path / entry for entry in result]