Skip to content

Commit

Permalink
feat: adding a subpackage example, fixing imports
Browse files Browse the repository at this point in the history
  • Loading branch information
akatief committed Jun 5, 2024
1 parent 4d54bde commit 9842203
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 10 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,8 @@ dmypy.json
.github/templates/*

# History
.history
.history

# Project files
sketch*
ukp_project_template/sketch*
19 changes: 14 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
11 changes: 10 additions & 1 deletion tests/test_base.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -10,4 +11,12 @@ def test_base_class():

assert str(bc1) == "test1"
assert repr(bc1) == "test1"
assert bc1 != bc2
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"
3 changes: 3 additions & 0 deletions ukp_project_template/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from .base import BaseClass



__all__ = [
"subpackage",
"BaseClass"
]
2 changes: 1 addition & 1 deletion ukp_project_template/__main__.py
Original file line number Diff line number Diff line change
@@ -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()
6 changes: 6 additions & 0 deletions ukp_project_template/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
9 changes: 7 additions & 2 deletions ukp_project_template/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
Expand All @@ -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()}")
5 changes: 5 additions & 0 deletions ukp_project_template/subpackage/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from .subpackage import SubPackageClass

__all__ = [
"SubPackageClass"
]
15 changes: 15 additions & 0 deletions ukp_project_template/subpackage/subpackage.py
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 9842203

Please sign in to comment.