-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
functools: Add functools.total_ordering.
This commit is the result of copying the total_ordering code and tests over from CPython v3.7.17. One test is disabled because it expects builtin objects to have attributes (__lt__, __gt__, etc.). Another test for compatibility with pickle is also disabled because pickle compatibility is currently broken. Bumped package version to 0.0.8. The functools code in CPython has the following credits: Written by Nick Coghlan <ncoghlan at gmail.com>, Raymond Hettinger <python at rcn.com>, and Łukasz Langa <lukasz at langa.pl>. Copyright (C) 2006-2013 Python Software Foundation. See C source code for _functools credits/copyright This work was donated by W Winfried Kretzschmar. Signed-off-by: W Winfried Kretzschmar <[email protected]>
- Loading branch information
Showing
7 changed files
with
400 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
FROM micropython/unix:v1.22 | ||
|
||
RUN micropython -m mip install unittest | ||
RUN micropython -m mip install unittest-discover | ||
|
||
|
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,24 @@ | ||
# functools | ||
|
||
# Testing | ||
|
||
## Install docker | ||
|
||
See https://docs.docker.com/engine/install/ | ||
|
||
## Build test environment | ||
|
||
``` | ||
docker build -t micropython-unittest . | ||
``` | ||
|
||
## Run tests | ||
|
||
docker run -v .:/code -ti --rm micropython-unittest micropython -m unittest | ||
|
||
|
||
# License | ||
|
||
Some files are distributed under the Python Software Foundation license. | ||
These files reference the Python Software Foundation license at the top of the file. | ||
|
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,3 @@ | ||
from .total_ordering import total_ordering | ||
|
||
from .functools import * |
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,133 @@ | ||
"""total_ordering | ||
This code was extracted from the CPython v3.7.17 implementation of Lib/functools.py | ||
Written by Nick Coghlan <ncoghlan at gmail.com>, | ||
Raymond Hettinger <python at rcn.com>, | ||
and Łukasz Langa <lukasz at langa.pl>. | ||
Copyright (C) 2006-2013 Python Software Foundation. | ||
See C source code for _functools credits/copyright | ||
This code is distributed under the Python Software License. | ||
""" | ||
|
||
|
||
################################################################################ | ||
### total_ordering class decorator | ||
################################################################################ | ||
|
||
# The total ordering functions all invoke the root magic method directly | ||
# rather than using the corresponding operator. This avoids possible | ||
# infinite recursion that could occur when the operator dispatch logic | ||
# detects a NotImplemented result and then calls a reflected method. | ||
|
||
def _gt_from_lt(self, other, NotImplemented=NotImplemented): | ||
'Return a > b. Computed by @total_ordering from (not a < b) and (a != b).' | ||
op_result = self.__lt__(other) | ||
if op_result is NotImplemented: | ||
return op_result | ||
return not op_result and self != other | ||
|
||
def _le_from_lt(self, other, NotImplemented=NotImplemented): | ||
'Return a <= b. Computed by @total_ordering from (a < b) or (a == b).' | ||
op_result = self.__lt__(other) | ||
return op_result or self == other | ||
|
||
def _ge_from_lt(self, other, NotImplemented=NotImplemented): | ||
'Return a >= b. Computed by @total_ordering from (not a < b).' | ||
op_result = self.__lt__(other) | ||
if op_result is NotImplemented: | ||
return op_result | ||
return not op_result | ||
|
||
def _ge_from_le(self, other, NotImplemented=NotImplemented): | ||
'Return a >= b. Computed by @total_ordering from (not a <= b) or (a == b).' | ||
op_result = self.__le__(other) | ||
if op_result is NotImplemented: | ||
return op_result | ||
return not op_result or self == other | ||
|
||
def _lt_from_le(self, other, NotImplemented=NotImplemented): | ||
'Return a < b. Computed by @total_ordering from (a <= b) and (a != b).' | ||
op_result = self.__le__(other) | ||
if op_result is NotImplemented: | ||
return op_result | ||
return op_result and self != other | ||
|
||
def _gt_from_le(self, other, NotImplemented=NotImplemented): | ||
'Return a > b. Computed by @total_ordering from (not a <= b).' | ||
op_result = self.__le__(other) | ||
if op_result is NotImplemented: | ||
return op_result | ||
return not op_result | ||
|
||
def _lt_from_gt(self, other, NotImplemented=NotImplemented): | ||
'Return a < b. Computed by @total_ordering from (not a > b) and (a != b).' | ||
op_result = self.__gt__(other) | ||
if op_result is NotImplemented: | ||
return op_result | ||
return not op_result and self != other | ||
|
||
def _ge_from_gt(self, other, NotImplemented=NotImplemented): | ||
'Return a >= b. Computed by @total_ordering from (a > b) or (a == b).' | ||
op_result = self.__gt__(other) | ||
return op_result or self == other | ||
|
||
def _le_from_gt(self, other, NotImplemented=NotImplemented): | ||
'Return a <= b. Computed by @total_ordering from (not a > b).' | ||
op_result = self.__gt__(other) | ||
if op_result is NotImplemented: | ||
return op_result | ||
return not op_result | ||
|
||
def _le_from_ge(self, other, NotImplemented=NotImplemented): | ||
'Return a <= b. Computed by @total_ordering from (not a >= b) or (a == b).' | ||
op_result = self.__ge__(other) | ||
if op_result is NotImplemented: | ||
return op_result | ||
return not op_result or self == other | ||
|
||
def _gt_from_ge(self, other, NotImplemented=NotImplemented): | ||
'Return a > b. Computed by @total_ordering from (a >= b) and (a != b).' | ||
op_result = self.__ge__(other) | ||
if op_result is NotImplemented: | ||
return op_result | ||
return op_result and self != other | ||
|
||
def _lt_from_ge(self, other, NotImplemented=NotImplemented): | ||
'Return a < b. Computed by @total_ordering from (not a >= b).' | ||
op_result = self.__ge__(other) | ||
if op_result is NotImplemented: | ||
return op_result | ||
return not op_result | ||
|
||
_convert = { | ||
'__lt__': [('__gt__', _gt_from_lt), | ||
('__le__', _le_from_lt), | ||
('__ge__', _ge_from_lt)], | ||
'__le__': [('__ge__', _ge_from_le), | ||
('__lt__', _lt_from_le), | ||
('__gt__', _gt_from_le)], | ||
'__gt__': [('__lt__', _lt_from_gt), | ||
('__ge__', _ge_from_gt), | ||
('__le__', _le_from_gt)], | ||
'__ge__': [('__le__', _le_from_ge), | ||
('__gt__', _gt_from_ge), | ||
('__lt__', _lt_from_ge)] | ||
} | ||
|
||
def total_ordering(cls): | ||
"""Class decorator that fills in missing ordering methods""" | ||
# Find user-defined comparisons (not those inherited from object). | ||
roots = {op for op in _convert if getattr(cls, op, None) is not getattr(object, op, None)} | ||
if not roots: | ||
raise ValueError('must define at least one ordering operation: < > <= >=') | ||
root = max(roots) # prefer __lt__ to __le__ to __gt__ to __ge__ | ||
for opname, opfunc in _convert[root]: | ||
if opname not in roots: | ||
# function objects have no attributes in micropython | ||
# opfunc.__name__ = opname | ||
setattr(cls, opname, opfunc) | ||
return cls | ||
|
||
|
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,3 +1,3 @@ | ||
metadata(version="0.0.7") | ||
metadata(version="0.0.8") | ||
|
||
module("functools.py") |
Oops, something went wrong.