banesullivan / scooby

🐶 🕵️ Great Dane turned Python environment detective
MIT License
47 stars 12 forks source link

Add soft-dependency example to docs #32

Closed prisae closed 4 years ago

prisae commented 5 years ago

Something we should add to the docs once they exist: There is an easy, neat solution to make scooby a soft dependency:

# Make scooby a soft dependency:
try:
    from scooby import Report as ScoobyReport
except ImportError:
    class ScoobyReport:
        def __init__(self, additional, core, optional, ncol, text_width, sort):
            print('\n  *ERROR*: `Report` requires `scooby`.'
                  '\n           Install it via `pip install scooby`.\n')

class Report(ScoobyReport):
    def __init__(self, additional=None, ncol=3, text_width=80, sort=False):
        """Initiate a scooby.Report instance."""

        # Mandatory packages.
        core = ['yourpackage', 'your_core_packages', 'e.g.', 'numpy', 'scooby']

        # Optional packages.
        optional = ['your_optional_packages', 'e.g.', 'matplotlib']

        super().__init__(additional=additional, core=core, optional=optional,
                         ncol=ncol, text_width=text_width, sort=sort)

If a user has scooby installed, all works as expected. If scooby is not installed, it will just print the following message:

>>> import your_package
>>> your_package.Report()

  *ERROR*: `Report` requires `scooby`
           Install it via `pip install scooby`.
prisae commented 4 years ago

It looks like the main docs of scooby will remain the only docs for a while. Shall I add it at the end of the readme for now?

banesullivan commented 4 years ago

I think so! I think it'll be a while before either of us have the time to make full-fledged docs and they likely wouldn't be too much more than the current README, so I say go ahead in the README