Code snippets

A wonderful example of how to use solve loading dependency problem in python. link

def discover_scrapers(token):
    scrapers = []
    for filepath in pathlib.Path(".").glob("*.py"):
        mod = importlib.import_module(filepath.stem)
        # if there's a load_scrapers() function, call that
        if hasattr(mod, "load_scrapers"):
            scrapers.extend(mod.load_scrapers(token))
        # Otherwise instantiate a scraper for each class
        else:
            for klass in mod.__dict__.values():
                try:
                    if (
                        issubclass(klass, Scraper)
                        and klass.__module__ != "disaster_scrapers"
                    ):
                        scrapers.append(klass(token))
                except TypeError:
                    pass
    return scrapers

An example on how to make sure the input variable is in accordance with the expected value.

rate_re = re.compile(r'([\d]+)/([\d]*)([smhd])?')

def _split_rate(rate):
    if isinstance(rate, tuple):
        return rate
    count, multi, period = rate_re.match(rate).groups()
    count = int(count)
    if not period:
        period = 's'
    seconds = _PERIODS[period.lower()]
    if multi:
        seconds = seconds * int(multi)
    return count, seconds