Package: barentsz
Barentsz⛵ Explore and discover modules, classes, functions, attributes.pip install barentsz❄ OverviewDiscover all packages in a path;Discover all modules in a path;Discover all/some classes in a path or module;Discover all/some functions in a path, module or class;Discover all/some attributes in a path or module.List of all features>>>importbarentsz>>>forfeaturein(fforfindir(barentsz)ifnotf.startswith('_')):...print(feature)discoverdiscover_attributesdiscover_classesdiscover_functionsdiscover_module_namesdiscover_modulesdiscover_packagesdiscover_pathshere❄ Features in detailThe sections below contain all features that are offered by this lib. For the API details,
please see theHelp documentationsubsections.DiscoverImport>>>frombarentszimportdiscoverUsage Example>>>discover('./test_resources/examples_for_readme')[<class'examples_for_readme.module_a.ClassA'>, <class 'examples_for_readme.module_b.ClassB'>]Help documentation>>>help(discover)Helponfunctiondiscoverinmodulebarentsz._discover:<BLANKLINE>discover(source:Any=None,*,what:Any=typing.List[type],**kwargs:dict)->listConveniencefunctionfordiscoveringtypesinsomesource.Ifnotsourceisgiven,thedirectoryisusedinwhichthecallingmoduleislocated.<BLANKLINE>Args:source:thesourceinwhichissearchedorthedirectoryofthecallerifNone.what:thetypethatistobediscovered.**kwargs:anykeywordargumentthatispassedon.<BLANKLINE>Returns:alistofdiscoveries.<BLANKLINE>Discover ClassesImport>>>frombarentszimportdiscover_classesUsage Example>>>discover_classes('./test_resources/examples_for_readme')[<class'examples_for_readme.module_a.ClassA'>, <class 'examples_for_readme.module_b.ClassB'>]Help documentation>>>help(discover_classes)Helponfunctiondiscover_classesinmodulebarentsz._discover:<BLANKLINE>discover_classes(source:Union[pathlib.Path,str,module,Iterable[module]],signature:type=typing.Any,include_privates:bool=False,in_private_modules:bool=False,raise_on_fail:bool=False,exclude:Union[type,Callable[[type],bool],Iterable[Union[type,Callable[[type],bool]]]]=None)->List[type]Discoveranyclasseswithinthegivensourceandaccordingtothegivenconstraints.<BLANKLINE>Args:source:thesourceinwhichissearchedforanyclasses.signature:onlyclassesthatinheritfromsignaturearereturned.include_privates:ifTrue,privateclassesareincludedaswell.in_private_modules:ifTrue,privatemodulesareexploredaswell.raise_on_fail:ifTrue,raisesanImportErroruponthefirstimportfailure.exclude:oneormoretypesorpredicatesthataretobeexcludedfromtheresult.<BLANKLINE>Returns:alistofalldiscoveredclasses(types).<BLANKLINE>Discover FunctionsImport>>>frombarentszimportdiscover_functionsUsage Example>>>functions=discover_functions('./test_resources/examples_for_readme')>>>[f.__name__forfinfunctions]['function_a','function_b']Help documentation>>>help(discover_functions)Helponfunctiondiscover_functionsinmodulebarentsz._discover:<BLANKLINE>discover_functions(source:Union[pathlib.Path,str,module,Iterable[module],type],signature:Type[Callable]=typing.Callable,include_privates:bool=False,in_private_modules:bool=False,raise_on_fail:bool=False)->List[type]Discoveranyfunctionswithinthegivensourceandaccordingtothegivenconstraints.<BLANKLINE>Args:source:thesourceinwhichissearchedforanyfunctions.signature:onlyfunctionsthathavethissignature(parametersandreturntype)areincluded.include_privates:ifTrue,privatefunctionsareincludedaswell.in_private_modules:ifTrue,privatemodulesareexploredaswell.raise_on_fail:ifTrue,raisesanImportErroruponthefirstimportfailure.<BLANKLINE>Returns:alistofalldiscoveredfunctions.<BLANKLINE>Discover AttributesImport>>>frombarentszimportdiscover_attributesUsage Example>>>attributes=discover_attributes('./test_resources/examples_for_readme')>>>[a.nameforainattributes]['attr_a','attr_b']Help documentation>>>help(discover_attributes)Helponfunctiondiscover_attributesinmodulebarentsz._discover:<BLANKLINE>discover_attributes(source:Union[pathlib.Path,str,module,Iterable[module]],signature:type=typing.Any,include_privates:bool=False,in_private_modules:bool=False,raise_on_fail:bool=False)->List[barentsz._attribute.Attribute]Discoveranyattributeswithinthegivensourceandaccordingtothegivenconstraints.<BLANKLINE>Args:source:thesourceinwhichissearchedforanyattributes.signature:onlyattributesthataresubtypesofthissignatureareincluded.include_privates:ifTrue,privateattributesareincludedaswell.in_private_modules:ifTrue,privatemodulesareexploredaswell.raise_on_fail:ifTrue,raisesanImportErroruponthefirstimportfailure.<BLANKLINE>Returns:alistofalldiscoveredattributes.<BLANKLINE>Discover ModulesImport>>>frombarentszimportdiscover_modulesUsage Example>>>modules=discover_modules('./test_resources/examples_for_readme')>>>[m.__name__forminmodules]['examples_for_readme.module_a','examples_for_readme.module_b']Help documentation>>>help(discover_modules)Helponfunctiondiscover_modulesinmodulebarentsz._discover:<BLANKLINE>discover_modules(directory:Union[pathlib.Path,str],include_privates:bool=False,raise_on_fail:bool=False)->List[module]Returnalistofmoduleswithinthegivendirectory.Thedirectorymustbeapackageandonlymodulesarereturnedthatareinpackages.Args:directory:thedirectoryinwhichissearchedformodules.include_privates:ifTrue,privates(undersanddunders)arealsoincluded.raise_on_fail:ifTrue,anImportErrorisraiseduponfailingtoimportanymodule.<BLANKLINE>Returns:alistofmoduleobjects.<BLANKLINE>Discover PackagesImport>>>frombarentszimportdiscover_packagesUsage Example>>>discover_packages('./test_resources/examples_for_readme')['examples_for_readme']Help documentation>>>help(discover_packages)Helponfunctiondiscover_packagesinmodulebarentsz._discover:<BLANKLINE>discover_packages(directory:Union[pathlib.Path,str])->List[str]Returnalistofpackageswithinthegivendirectory.Thedirectorymustbeapackage.Args:directory:thedirectoryinwhichissearchedforpackages.<BLANKLINE>Returns:alistofpackages.<BLANKLINE>Current Directory (here)Import>>>frombarentszimporthereUsage Example>>>str(here())'.'Help documentation>>>help(here)Helponfunctionhereinmodulebarentsz._here:<BLANKLINE>here(frames_back:int=0)->pathlib.PathGetthecurrentdirectoryfromwhichthisfunctioniscalled.Args:frames_back:thenumberofextraframestolookback.<BLANKLINE>Returns:thedirectoryasaPathinstance.<BLANKLINE>Discover PathsImport>>>frombarentszimportdiscover_pathsUsage Example>>>paths=discover_paths('./test_resources/examples_for_readme','**/*.py')>>>[str(p.as_posix())forpinpaths]['test_resources/examples_for_readme/__init__.py','test_resources/examples_for_readme/module_a.py','test_resources/examples_for_readme/module_b.py']Help documentation>>>help(discover_paths)Helponfunctiondiscover_pathsinmodulebarentsz._discover:<BLANKLINE>discover_paths(directory:Union[pathlib.Path,str],pattern:str)->List[pathlib.Path]ReturnalistofPathswithinthegivendirectorythatmatchthegivenpattern.<BLANKLINE>Args:directory:thedirectoryinwhichissearchedforpaths.pattern:apattern(example:'**/*.py').<BLANKLINE>Returns:alistofPathobjects.<BLANKLINE>❄ (Not So) Frequently Asked QuestionsWhen is Barentsz particularly useful?When e.g. adding a class to some package and you want it to be picked up
in your application, without having to add an import or registration
somewhere.Does Barentsz require my classes to be compromised (e.g. with inheritance or a decorator or something)?No, never.What must I do for Barentsz to discover my class (or function, attribute, etc.)?Nothing special. Just make sure that the path that is explored is a Python package.Why do the "Help documentation" sections contain this "<BLANKLINE>"?That's because this documentation is underdoctest.
It helps to ensure that the documentation is always up to date.What's with the funny name?Well... since this library is all about exploring and discovering and because
I really enjoyed the cold north, I thought it to be a fitting name.What is the answer to the Ultimate Question of Life, the Universe, and Everything?Haven't got a clue, what are you asking me for anyway? I suggest you build an AI
to deduce the answer (using barentsz of course).❄ Changelist1.2.1 [2020-09-26]Fix for a bug when discovering using a relative path.1.2.0 [2020-09-20]Changedexcludeparameter to also allow predicates.1.1.0 [2020-08-05]Added theherefunction that returns the directory of the caller of that function.Added thediscoveryfunction that can conveniently find types using the current dir and a given class.Addedexcludetodiscover_classesto allow for excluding one or more types from discovery.Fix for double discovered classes.1.0.0 [2020-07-28]First release. 🎉