mirror of
https://github.com/theroyallab/tabbyAPI.git
synced 2026-04-19 22:08:59 +00:00
Tree: Use unwrap and coalesce for optional handling
Python doesn't have proper handling of optionals. The only way to handle them is checking via an if statement if the value is None or by using the "or" keyword to unwrap optionals. Previously, I used the "or" method to unwrap, but this caused issues due to falsy values falling back to the default. This is especially the case with booleans were "False" changed to "True". Instead, add two new functions: unwrap and coalesce. Both function to properly implement a functional way of "None" coalescing. Signed-off-by: kingbri <bdashore3@proton.me>
This commit is contained in:
11
utils.py
11
utils.py
@@ -30,3 +30,14 @@ def get_generator_error(message: str):
|
||||
|
||||
def get_sse_packet(json_data: str):
|
||||
return f"data: {json_data}\n\n"
|
||||
|
||||
# Unwrap function for Optionals
|
||||
def unwrap(wrapped, default = None):
|
||||
if wrapped is None:
|
||||
return default
|
||||
else:
|
||||
return wrapped
|
||||
|
||||
# Coalesce function for multiple unwraps
|
||||
def coalesce(*args):
|
||||
return next((arg for arg in args if arg is not None), None)
|
||||
|
||||
Reference in New Issue
Block a user