mirror of
https://github.com/erg-lang/erg.git
synced 2025-08-04 18:58:30 +00:00
clean: use black
and ruff
This commit is contained in:
parent
eba80469a6
commit
cc21271998
14 changed files with 93 additions and 66 deletions
|
@ -1,12 +1,13 @@
|
|||
from _erg_control import then__
|
||||
from _erg_range import Range
|
||||
from _erg_nat import NatMut
|
||||
from _erg_int import IntMut
|
||||
from _erg_contains_operator import contains_operator
|
||||
from _erg_result import is_ok
|
||||
from _erg_control import then__
|
||||
from _erg_int import IntMut
|
||||
from _erg_nat import NatMut
|
||||
from _erg_range import Range
|
||||
from _erg_result import Error
|
||||
from _erg_result import is_ok
|
||||
from _erg_type import UnionType
|
||||
|
||||
|
||||
class Array(list):
|
||||
@staticmethod
|
||||
def try_new(arr): # -> Result[Array]
|
||||
|
@ -15,7 +16,7 @@ class Array(list):
|
|||
else:
|
||||
return Error("not a list")
|
||||
|
||||
def generic_try_new(arr, cls = None): # -> Result[Array]
|
||||
def generic_try_new(arr, cls=None): # -> Result[Array]
|
||||
if cls is None:
|
||||
return Array.try_new(arr)
|
||||
else:
|
||||
|
@ -82,7 +83,7 @@ class Array(list):
|
|||
if isinstance(t, list):
|
||||
if len(t) < len(self):
|
||||
return False
|
||||
for (inner_t, elem) in zip(t, self):
|
||||
for inner_t, elem in zip(t, self):
|
||||
if not contains_operator(inner_t, elem):
|
||||
return False
|
||||
return True
|
||||
|
@ -109,6 +110,7 @@ class Array(list):
|
|||
|
||||
def prod(self, start=1):
|
||||
from functools import reduce
|
||||
|
||||
return reduce(lambda x, y: x * y, self, start)
|
||||
|
||||
def reversed(self):
|
||||
|
@ -129,12 +131,15 @@ class Array(list):
|
|||
|
||||
def repeat(self, n):
|
||||
from copy import deepcopy
|
||||
|
||||
new = []
|
||||
for _ in range(n):
|
||||
new.extend(deepcopy(self))
|
||||
return Array(new)
|
||||
|
||||
|
||||
class UnsizedArray:
|
||||
elem: object
|
||||
|
||||
def __init__(self, elem):
|
||||
self.elem = elem
|
||||
|
|
|
@ -3,9 +3,10 @@ from _erg_nat import NatMut
|
|||
from _erg_result import Error
|
||||
from _erg_type import MutType
|
||||
|
||||
|
||||
class Bool(Nat):
|
||||
def try_new(b: bool): # -> Result[Nat]
|
||||
if b == True or b == False:
|
||||
if isinstance(b, bool):
|
||||
return Bool(b)
|
||||
else:
|
||||
return Error("Bool can't be other than True or False")
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
from _erg_result import Error
|
||||
from _erg_range import Range
|
||||
|
||||
|
||||
class Bytes(bytes):
|
||||
def try_new(b): # -> Result[Nat]
|
||||
if isinstance(b, bytes):
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
from _erg_result import is_ok
|
||||
from _erg_range import Range
|
||||
from _erg_type import is_type
|
||||
from _erg_type import _isinstance
|
||||
from _erg_type import UnionType
|
||||
|
||||
from collections import namedtuple
|
||||
|
||||
from _erg_range import Range
|
||||
from _erg_result import is_ok
|
||||
from _erg_type import UnionType
|
||||
from _erg_type import _isinstance
|
||||
from _erg_type import is_type
|
||||
|
||||
# (elem in y) == contains_operator(y, elem)
|
||||
def contains_operator(y, elem) -> bool:
|
||||
if hasattr(elem, "type_check"):
|
||||
|
@ -56,11 +56,12 @@ def contains_operator(y, elem) -> bool:
|
|||
value = next(iter(y.values()))
|
||||
value_check = all([contains_operator(value, el) for el in elem.values()])
|
||||
return key_check and value_check
|
||||
type_check = True # TODO:
|
||||
type_check = True # TODO:
|
||||
len_check = True # It can be True even if either elem or y has the larger number of elems
|
||||
return type_check and len_check
|
||||
elif _isinstance(elem, list):
|
||||
from _erg_array import Array
|
||||
|
||||
return contains_operator(y, Array(elem))
|
||||
elif callable(elem):
|
||||
# TODO:
|
||||
|
|
|
@ -24,11 +24,13 @@ def with__(obj, body):
|
|||
def discard__(obj):
|
||||
pass
|
||||
|
||||
|
||||
def assert__(test, msg=None):
|
||||
assert test, msg
|
||||
|
||||
|
||||
def then__(x, f):
|
||||
if x == None or x == NotImplemented:
|
||||
if x is None or x is NotImplemented:
|
||||
return x
|
||||
else:
|
||||
return f(x)
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
from _erg_float import Float
|
||||
from _erg_int import Int
|
||||
from _erg_nat import Nat
|
||||
from _erg_float import Float
|
||||
from _erg_str import Str
|
||||
|
||||
|
||||
|
|
|
@ -5,13 +5,16 @@ class Dict(dict):
|
|||
return Dict(dic)
|
||||
else:
|
||||
return Error("not a dict")
|
||||
|
||||
def concat(self, other):
|
||||
return Dict({**self, **other})
|
||||
|
||||
def diff(self, other):
|
||||
return Dict({k: v for k, v in self.items() if k not in other})
|
||||
|
||||
# other: Iterable
|
||||
def update(self, other, conflict_resolver=None):
|
||||
if conflict_resolver == None:
|
||||
if conflict_resolver is None:
|
||||
super().update(other)
|
||||
elif isinstance(other, dict):
|
||||
self.merge(other, conflict_resolver)
|
||||
|
@ -21,16 +24,21 @@ class Dict(dict):
|
|||
self[k] = conflict_resolver(self[k], v)
|
||||
else:
|
||||
self[k] = v
|
||||
|
||||
# other: Dict
|
||||
def merge(self, other, conflict_resolver=None):
|
||||
self.update(other, conflict_resolver)
|
||||
|
||||
def insert(self, key, value):
|
||||
self[key] = value
|
||||
|
||||
def remove(self, key):
|
||||
res = self.get(key)
|
||||
if res != None:
|
||||
if res is not None:
|
||||
del self[key]
|
||||
return res
|
||||
|
||||
def as_record(self):
|
||||
from collections import namedtuple
|
||||
return namedtuple('Record', self.keys())(**self)
|
||||
|
||||
return namedtuple("Record", self.keys())(**self)
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
from _erg_result import Error
|
||||
from _erg_control import then__
|
||||
from _erg_result import Error
|
||||
from _erg_type import MutType
|
||||
|
||||
|
||||
class Float(float):
|
||||
EPSILON = 2.220446049250313e-16
|
||||
|
||||
|
@ -50,6 +51,7 @@ class Float(float):
|
|||
def nearly_eq(self, other, epsilon=EPSILON):
|
||||
return abs(self - other) < epsilon
|
||||
|
||||
|
||||
class FloatMut(MutType): # inherits Float
|
||||
value: Float
|
||||
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
from _erg_result import Error
|
||||
from _erg_control import then__
|
||||
from _erg_result import Error
|
||||
from _erg_type import MutType
|
||||
|
||||
|
||||
class Int(int):
|
||||
def try_new(i): # -> Result[Nat]
|
||||
if isinstance(i, int):
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
from _erg_result import Error
|
||||
from _erg_int import Int
|
||||
from _erg_int import IntMut # don't unify with the above line
|
||||
from _erg_control import then__
|
||||
from _erg_int import IntMut # don't unify with the above line
|
||||
from _erg_int import Int
|
||||
from _erg_result import Error
|
||||
from _erg_type import MutType
|
||||
|
||||
|
||||
class Nat(Int):
|
||||
def __init__(self, i):
|
||||
if int(i) < 0:
|
||||
|
|
|
@ -95,13 +95,13 @@ class RangeIterator:
|
|||
self.rng = rng
|
||||
self.needle = self.rng.start
|
||||
if issubclass(Nat, type(self.rng.start)):
|
||||
if not (self.needle in self.rng):
|
||||
if self.needle not in self.rng:
|
||||
self.needle += 1
|
||||
elif issubclass(Str, type(self.rng.start)):
|
||||
if not (self.needle in self.rng):
|
||||
if self.needle not in self.rng:
|
||||
self.needle = chr(ord(self.needle) + 1)
|
||||
else:
|
||||
if not (self.needle in self.rng):
|
||||
if self.needle not in self.rng:
|
||||
self.needle = self.needle.succ()
|
||||
|
||||
def __iter__(self):
|
||||
|
|
|
@ -1,27 +1,22 @@
|
|||
# HACK: import MutType to suppress segfault in CPython 3.10 (cause unknown)
|
||||
from _erg_type import MutType as _MutType
|
||||
from _erg_range import (
|
||||
Range,
|
||||
LeftOpenRange,
|
||||
RightOpenRange,
|
||||
OpenRange,
|
||||
ClosedRange,
|
||||
RangeIterator,
|
||||
)
|
||||
from _erg_result import Error, is_ok
|
||||
from _erg_float import Float, FloatMut
|
||||
from _erg_int import Int, IntMut
|
||||
from _erg_nat import Nat, NatMut
|
||||
from _erg_array import Array, UnsizedArray
|
||||
from _erg_bool import Bool
|
||||
from _erg_bytes import Bytes
|
||||
from _erg_str import Str, StrMut
|
||||
from _erg_array import Array, UnsizedArray
|
||||
from _erg_dict import Dict
|
||||
from _erg_set import Set
|
||||
from _erg_contains_operator import contains_operator
|
||||
from _erg_dict import Dict
|
||||
from _erg_float import Float, FloatMut
|
||||
from _erg_int import Int, IntMut
|
||||
from _erg_mutate_operator import mutate_operator
|
||||
from _erg_nat import Nat, NatMut
|
||||
from _erg_range import (ClosedRange, LeftOpenRange, OpenRange, Range,
|
||||
RangeIterator, RightOpenRange)
|
||||
from _erg_result import Error, is_ok
|
||||
from _erg_set import Set
|
||||
from _erg_str import Str, StrMut
|
||||
from _erg_type import MutType as _MutType
|
||||
|
||||
Record = tuple
|
||||
|
||||
|
||||
class Never:
|
||||
pass
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
from _erg_result import Error
|
||||
from _erg_int import Int
|
||||
from _erg_control import then__
|
||||
from _erg_int import Int
|
||||
from _erg_result import Error
|
||||
from _erg_type import MutType
|
||||
|
||||
|
||||
class Str(str):
|
||||
def __instancecheck__(cls, obj):
|
||||
return isinstance(obj, str)
|
||||
|
@ -39,6 +40,7 @@ class Str(str):
|
|||
|
||||
def __getitem__(self, index_or_slice):
|
||||
from _erg_range import Range
|
||||
|
||||
if isinstance(index_or_slice, slice):
|
||||
return Str(str.__getitem__(self, index_or_slice))
|
||||
elif isinstance(index_or_slice, Range):
|
||||
|
|
|
@ -2,40 +2,47 @@ try:
|
|||
from typing import Union
|
||||
except ImportError:
|
||||
import warnings
|
||||
|
||||
warnings.warn("`typing.Union` is not available. Please use Python 3.8+.")
|
||||
|
||||
class Union:
|
||||
pass
|
||||
|
||||
|
||||
class UnionType:
|
||||
__origin__ = Union
|
||||
__args__: list # list[type]
|
||||
def __init__(self, *args):
|
||||
self.__args__ = args
|
||||
def __str__(self):
|
||||
s = "UnionType["
|
||||
for i, arg in enumerate(self.__args__):
|
||||
if i > 0:
|
||||
s += ", "
|
||||
s += str(arg)
|
||||
s += "]"
|
||||
return s
|
||||
def __repr__(self):
|
||||
return self.__str__()
|
||||
__origin__ = Union
|
||||
__args__: list # list[type]
|
||||
|
||||
def __init__(self, *args):
|
||||
self.__args__ = args
|
||||
|
||||
def __str__(self):
|
||||
s = "UnionType[" + ", ".join(str(arg) for arg in self.__args__) + "]"
|
||||
return s
|
||||
|
||||
def __repr__(self):
|
||||
return self.__str__()
|
||||
|
||||
|
||||
class FakeGenericAlias:
|
||||
__origin__: type
|
||||
__args__: list # list[type]
|
||||
def __init__(self, origin, *args):
|
||||
self.__origin__ = origin
|
||||
self.__args__ = args
|
||||
__origin__: type
|
||||
__args__: list # list[type]
|
||||
|
||||
def __init__(self, origin, *args):
|
||||
self.__origin__ = origin
|
||||
self.__args__ = args
|
||||
|
||||
|
||||
try:
|
||||
from types import GenericAlias
|
||||
except ImportError:
|
||||
GenericAlias = FakeGenericAlias
|
||||
|
||||
|
||||
def is_type(x) -> bool:
|
||||
return isinstance(x, (type, FakeGenericAlias, GenericAlias, UnionType))
|
||||
|
||||
|
||||
# The behavior of `builtins.isinstance` depends on the Python version.
|
||||
def _isinstance(obj, classinfo) -> bool:
|
||||
if isinstance(classinfo, (FakeGenericAlias, GenericAlias, UnionType)):
|
||||
|
@ -49,5 +56,6 @@ def _isinstance(obj, classinfo) -> bool:
|
|||
except:
|
||||
return False
|
||||
|
||||
|
||||
class MutType:
|
||||
value: object
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue