Monday, 25 March 2024

Python: Check if a string matches at least one glob condition

import fnmatch

def matches_one_glob_condition(item: str, conditions: list[str]) -> bool:
    """
    Check if the given item matches at least one of the specified glob patterns.

    Args:
    item (str): The item to check.
    conditions (list[str]): A list of glob patterns to match against the item.

    Returns:
    bool: True if the item matches at least one condition, False otherwise.
    """
    for condition in conditions:
        if fnmatch.fnmatch(item, condition):
            return True
    return False
  
print(matches_one_glob_condition('beir/scifacts/test', ['beir/*/test']))

No comments:

Post a Comment

Parse Wikipedia dump

""" This module processes Wikipedia dump files by extracting individual articles and parsing them into a structured format, ...