-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add context managers for backend determination (#244)
- Loading branch information
1 parent
a1c98d6
commit ff57432
Showing
7 changed files
with
393 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,8 @@ uarray | |
get_state | ||
set_state | ||
reset_state | ||
determine_backend | ||
determine_backend_multi | ||
|
||
|
||
|
||
|
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
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
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import uarray as ua | ||
|
||
|
||
class _TypedBackend: | ||
__ua_domain__ = "ua_examples" | ||
|
||
def __init__(self, *my_types): | ||
self.my_types = my_types | ||
|
||
def __ua_convert__(self, dispatchables, coerce): | ||
if not all(type(d.value) in self.my_types for d in dispatchables): | ||
return NotImplemented | ||
return tuple(d.value for d in dispatchables) | ||
|
||
def __ua_function__(self, func, args, kwargs): | ||
return self.my_types[0]() | ||
|
||
|
||
class TypeA: | ||
@classmethod | ||
def __repr__(cls): | ||
return cls.__name__ | ||
|
||
|
||
class TypeB(TypeA): | ||
pass | ||
|
||
|
||
class TypeC(TypeA): | ||
pass | ||
|
||
|
||
BackendA = _TypedBackend(TypeA) | ||
BackendB = _TypedBackend(TypeB) | ||
BackendC = _TypedBackend(TypeC) | ||
BackendAB = _TypedBackend(TypeA, TypeB) | ||
BackendBC = _TypedBackend(TypeB, TypeC) | ||
|
||
creation_multimethod = ua.generate_multimethod( | ||
lambda: (), lambda a, kw, d: (a, kw), "ua_examples" | ||
) | ||
call_multimethod = ua.generate_multimethod( | ||
lambda *a: tuple(ua.Dispatchable(x, "mark") for x in a), | ||
lambda a, kw, d: (a, kw), | ||
"ua_examples", | ||
) |
Oops, something went wrong.