diff --git a/.gitignore b/.gitignore index 211b061..4e550cf 100644 --- a/.gitignore +++ b/.gitignore @@ -132,4 +132,8 @@ dmypy.json .github/templates/* # History -.history \ No newline at end of file +.history + +# Project files +sketch* +ukp_project_template/sketch* \ No newline at end of file diff --git a/README.md b/README.md index 66bcff3..441b6c2 100644 --- a/README.md +++ b/README.md @@ -48,15 +48,24 @@ pip install -r requirements-dev.txt # Only needed for development ### Using the classes -This is how you can use classes inside `ukp_project_template`: +To import classes/methods of `ukp_project_template` from inside the package itself you can use relative imports: ```py -from ukp_project_template import BaseClass -from ukp_project_template import base_function +from .base import BaseClass # Notice how I omit the package name -BaseClass().base_method() -base_function() +BaseClass().something() ``` + +To import classes/methods from outside the package (e.g. when you want to use the package in some other project) you can instead refer to the package name: + +```py +from ukp_project_template import BaseClass # Notice how I omit the file name +from ukp_project_template.subpackage import SubPackageClass # Here it's necessary because it's a subpackage + +BaseClass().something() +SubPackageClass().something() +``` + ### Using scripts This is how you can use `ukp_project_template` from command line: diff --git a/tests/test_base.py b/tests/test_base.py index 30265ba..6eac760 100644 --- a/tests/test_base.py +++ b/tests/test_base.py @@ -1,5 +1,6 @@ # Tests are defined here from ukp_project_template import BaseClass +from ukp_project_template.subpackage import SubPackageClass def test_template(): assert True @@ -10,4 +11,12 @@ def test_base_class(): assert str(bc1) == "test1" assert repr(bc1) == "test1" - assert bc1 != bc2 \ No newline at end of file + assert bc1 != bc2 + assert bc1.something() == "something" + +def test_subpackage(): + spc = SubPackageClass(name="test") + + assert str(spc) == "SubPackage - test" + assert repr(spc) == "SubPackage - test" + assert spc.something() == "SubPackage - something" \ No newline at end of file diff --git a/ukp_project_template/__init__.py b/ukp_project_template/__init__.py index 19ff72d..4003b3e 100644 --- a/ukp_project_template/__init__.py +++ b/ukp_project_template/__init__.py @@ -1,5 +1,8 @@ from .base import BaseClass + + __all__ = [ + "subpackage", "BaseClass" ] \ No newline at end of file diff --git a/ukp_project_template/__main__.py b/ukp_project_template/__main__.py index 6c08406..c6bc87c 100644 --- a/ukp_project_template/__main__.py +++ b/ukp_project_template/__main__.py @@ -1,6 +1,6 @@ """Entry point for ukp_project_template.""" -from ukp_project_template.cli import main # pragma: no cover +from .cli import main # pragma: no cover if __name__ == "__main__": # pragma: no cover main() diff --git a/ukp_project_template/base.py b/ukp_project_template/base.py index 33440b0..7ecc6ac 100644 --- a/ukp_project_template/base.py +++ b/ukp_project_template/base.py @@ -54,3 +54,9 @@ def __eq__(self, other): True if the entities are equal, False otherwise. """ return self.name == other.name + + def something(self): + """ + Does something. + """ + return "something" diff --git a/ukp_project_template/cli.py b/ukp_project_template/cli.py index 8a75aef..325b7c9 100644 --- a/ukp_project_template/cli.py +++ b/ukp_project_template/cli.py @@ -7,7 +7,8 @@ - Start a web application - Import things from your .base module """ - +from .base import BaseClass +from .subpackage import SubPackageClass def main(): # pragma: no cover """ @@ -25,4 +26,8 @@ def main(): # pragma: no cover * List all available tasks * Run an application (Flask, FastAPI, Django, etc.) """ - print("This will do something") + bc = BaseClass("test") + print(f"This will do something: {bc.something()}") + + spc = SubPackageClass("test") + print(f"This will do something else: {spc.something()}") diff --git a/ukp_project_template/subpackage/__init__.py b/ukp_project_template/subpackage/__init__.py new file mode 100644 index 0000000..bf2baf1 --- /dev/null +++ b/ukp_project_template/subpackage/__init__.py @@ -0,0 +1,5 @@ +from .subpackage import SubPackageClass + +__all__ = [ + "SubPackageClass" +] \ No newline at end of file diff --git a/ukp_project_template/subpackage/subpackage.py b/ukp_project_template/subpackage/subpackage.py new file mode 100644 index 0000000..c676ed5 --- /dev/null +++ b/ukp_project_template/subpackage/subpackage.py @@ -0,0 +1,15 @@ +class SubPackageClass: + def __init__(self, name): + self.name = name + + def __str__(self): + return f"SubPackage - {self.name}" + + def __repr__(self): + return f"SubPackage - {self.name}" + + def __eq__(self, other): + return self.name == other.name + + def something(self): + return "SubPackage - something" \ No newline at end of file