Example #1
0
def test_ordered_dict_output():
    assert dict_tools.reverse_with_set_values({1: 2, 3: 4, 'meow': 2},
                                              sort=True) == \
    nifty_collections.OrderedDict([(2, {1, 'meow'}), (4, {3})])

    assert dict_tools.reverse_with_set_values({1: 2, 3: 4, 'meow': 2},
                                              sort=lambda x: -x) == \
    nifty_collections.OrderedDict([(4, {3}), (2, {1, 'meow'})])
def test_ordered_dict_output():
    assert dict_tools.reverse_with_set_values({1: 2, 3: 4, "meow": 2}, sort=True) == nifty_collections.OrderedDict(
        [(2, {1, "meow"}), (4, {3})]
    )

    assert dict_tools.reverse_with_set_values(
        {1: 2, 3: 4, "meow": 2}, sort=lambda x: -x
    ) == nifty_collections.OrderedDict([(4, {3}), (2, {1, "meow"})])
def test_iterable_input():
    assert dict_tools.reverse_with_set_values((range(1, 5), str)) == {"1": {1}, "2": {2}, "3": {3}, "4": {4}}

    assert dict_tools.reverse_with_set_values(([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
    '''

    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 = dict(
        (filename, filename.stem) for filename in result)
    module_name_to_filenames = \
        dict_tools.reverse_with_set_values(filename_to_module_name)

    for module_name, filenames in module_name_to_filenames.iteritems():
        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 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
    """

    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 = dict_tools.reverse_with_set_values(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():
    '''Test the basic workings of `reverse_with_set_values`.'''
    assert dict_tools.reverse_with_set_values({1: 2, 3: 4, 'meow': 2}) == \
                                                       {2: {1, 'meow'}, 4: {3}}
def test_iterable_input():
    assert dict_tools.reverse_with_set_values((range(1, 5), str)) == \
                                 {'1': {1}, '2': {2}, '3': {3}, '4': {4},}
    
    assert dict_tools.reverse_with_set_values(([1, 2+3j, 4, 5-6j], 'imag')) \
                                          == {0: {1, 4}, 3: {2+3j}, -6: {5-6j}}
Example #8
0
def test():
    '''Test the basic workings of `reverse_with_set_values`.'''
    assert dict_tools.reverse_with_set_values({1: 2, 3: 4, 'meow': 2}) == \
                                                       {2: {1, 'meow'}, 4: {3}}
Example #9
0
def test_iterable_input():
    assert dict_tools.reverse_with_set_values((range(1, 5), str)) == \
                                 {'1': {1}, '2': {2}, '3': {3}, '4': {4},}

    assert dict_tools.reverse_with_set_values(([1, 2+3j, 4, 5-6j], 'imag')) \
                                          == {0: {1, 4}, 3: {2+3j}, -6: {5-6j}}
Example #10
0
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
    '''
        
    if isinstance(root, types.ModuleType):
        root_module = root
        root_path = os.path.dirname(root_module.__file__)
    elif isinstance(root, str):
        root_path = os.path.abspath(root)
        # Not making `root_module`, it might not be imported.
    
    ######################################################
    
    result = []
        
    for entry in os.listdir(root_path):
        
        full_path = os.path.join(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 += [os.path.join(entry, thing) for thing in
                           inner_results]
    
    ### Filtering out duplicate filenames for the same module: ################
    #                                                                         #
                
    filename_to_module_name = {
        filename: os.path.splitext(filename)[0] for filename in result
    }
    module_name_to_filenames = \
        dict_tools.reverse_with_set_values(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(os.path.splitext(filename)[1]),
        )
        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 [os.path.join(os.path.dirname(full_path), entry) for entry in
            result]
def test():
    """Test the basic workings of `reverse_with_set_values`."""
    assert dict_tools.reverse_with_set_values({1: 2, 3: 4, "meow": 2}) == {2: {1, "meow"}, 4: {3}}