-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
quickstart documentation update (#16)
- Loading branch information
1 parent
55b63a3
commit 29b7ec7
Showing
4 changed files
with
85 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,53 @@ | ||
# EmptyCollection | ||
The concept of the `EmptyCollection` shares similarities with the [optional type](https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html) found in various programming languages. It serves as a mechanism to handle the absence of values or empty results. | ||
|
||
Similar to optional types in other languages, the `EmptyCollection` provides a consistent interface and allows for operations and attribute access without the need for explicit checks for empty or null values. It acts as a container that represents the absence of a value or result. | ||
|
||
By utilizing the `EmptyCollection`, developers can write cleaner and more concise code by treating empty results as a valid state without the need for verbose conditional statements. This promotes a more functional programming style, allowing for seamless chaining and composition of operations even in scenarios where the result might be empty. | ||
|
||
Just as optional types in different programming languages offer methods or functions to check for presence (_isPresent()_) and provide fallback values (_orElse()_), the `EmptyCollection` provides a simple fallback functionality to handle cases where the collection is empty as it always evaluates to `False`. | ||
|
||
This demonstrates how you can implement a straightforward fallback mechanism using the or operator when retrieving the final data. | ||
```python | ||
from trackteroid import ( | ||
Asset, | ||
Query | ||
) | ||
|
||
asset_collection = Query(Asset).by_name("DOESNT_EXIST").get_all() | ||
print(asset_collection) | ||
# output: EmptyCollection[Asset] | ||
|
||
print(not asset_collection) | ||
# output: True | ||
|
||
print(asset_collection or "Oh vey... no results found.") | ||
# output: Oh vey... no results found. | ||
``` | ||
|
||
This code example showcases how to gracefully handle scenarios where the intermediate steps of querying, filtering, and retrieving data may result in an empty collection. By utilizing the or operator and providing an empty list as a fallback, we ensure that the final result is either the desired data or an empty list, mitigating the risk of errors or unexpected behavior. | ||
|
||
```python | ||
from trackteroid import ( | ||
Asset, | ||
Query | ||
) | ||
|
||
print( | ||
# The Query result could already be empty. | ||
# This is more likely when using criteria to filter results when querying. | ||
Query(Asset).get_first( | ||
projections=[ | ||
"versions.is_published", | ||
"versions.user.username" | ||
] | ||
). | ||
# An asset could have no versions or at least no versions that have been marked as `is_published`. | ||
versions.filter( | ||
lambda avc: avc.is_published[0] | ||
). | ||
user.username | ||
# Finally, we retrieve the username of the user associated with the filtered versions. | ||
# If at any point we encounter no results, we can gracefully handle it by providing an empty list as a fallback. | ||
or [] | ||
) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +0,0 @@ | ||
# EntityCollection | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters