Skip to content
This repository has been archived by the owner on Jul 16, 2023. It is now read-only.

Commit

Permalink
add safe looping
Browse files Browse the repository at this point in the history
  • Loading branch information
rjdbcm committed Nov 27, 2021
1 parent 0453478 commit fd77cc0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Aspidites/api/convert.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,9 @@ def cvt_for_loop_decl(s, loc, t):
m = r.group(1) + ".items()"
else:
m = t[4]
s = str(t[3] + "".join(t[:3]) + " in " + m + lit_colon).encode("UTF-8")
s = str(t[3] + "".join(t[:3]) + " in __maybe(__safeLoop, " + m + end + lit_colon).encode("UTF-8")
else:
s = str(t[1] + t[0] + " in " + t[2] + lit_colon).encode("UTF-8")
s = str(t[1] + t[0] + " in __maybe(__safeLoop, " + t[2] + end + lit_colon).encode("UTF-8")
return s.decode("UTF-8")


Expand Down
16 changes: 15 additions & 1 deletion Aspidites/api/math.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Undefined:
"""A monad for a failed programmatic unit; like NoneType but hashable.
Falsy singleton acts as an absorbing element for division."""

__slots__ = ("__weakref__", "__instance__", "func", "args", "kwargs")
__slots__ = ("__weakref__", "__instance__", "_consumed", "func", "args", "kwargs")

def __hash__(self):
# noinspection PyUnresolvedReferences
Expand Down Expand Up @@ -73,6 +73,16 @@ class Undefined:
# Undefined has 0 elements
return 0

def __iter__(self):
return Undefined(self)

def __next__(self):
if not self._consumed:
self._consumed = True
return Undefined(self)
else:
raise StopIteration

def __repr__(self):
if hasattr(self.func, "__name__"):
r = (
Expand All @@ -91,6 +101,7 @@ class Undefined:
self.func = func
self.args = args
self.kwargs = kwargs
self._consumed = False


def SafeSlice(x: Any, start=None, stop=None, step=None):
Expand All @@ -100,6 +111,9 @@ def SafeSlice(x: Any, start=None, stop=None, step=None):
return x[start:stop:step]


def SafeLoop(x: Any):
return (i for i in x)

# noinspection PyPep8Naming,PyProtectedMember,PyUnresolvedReferences
def SafeFactorial(a: Any) -> Any:
if a < 0 or isnan(a) or isinf(a) or isinstance(a, (float, complex)):
Expand Down
2 changes: 1 addition & 1 deletion Aspidites/api/templates.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ from Aspidites._vendor.pyrsistent import pset as __pset,pmap as __pmap,pvector a
from Aspidites.woma import *
from Aspidites._vendor import take,drop,takelast,droplast,consume,nth,first_true,iterate,padnone,ncycles,repeatfunc,grouper,group_by,roundrobin,partition,splitat,splitby,powerset,pairwise,iter_suppress,flatten,accumulate,reduce,filterfalse,zip_longest,chain,combinations,cycle,dropwhile,islice,repeat,starmap,takewhile,tee,call,apply,flip,curry,curried,zipwith,foldl,foldr,unfold,Capture,Strict,OneOf,AllOf,NoneOf,Not,Each,EachItem,Some,Between,Length,Contains,Regex,Check,InstanceOf,SubclassOf,Arguments,Returns,Transformed,At,Object,match as __match,_
from Aspidites.api.monads import Maybe as __maybe,Surely as __surely
from Aspidites.api.math import Undefined as __undefined,SafeSlice as __safeSlice,SafeDiv as __safeDiv,SafeMul as __safeMul,SafeAdd as __safeAdd,SafeSub as __safeSub,SafeExp as __safeExp,SafeMod as __safeMod,SafeFloorDiv as __safeFloorDiv,SafeUnaryAdd as __safeUnaryAdd,SafeUnarySub as __safeUnarySub,SafeFactorial as __safeFactorial
from Aspidites.api.math import Undefined as __undefined,SafeSlice as __safeSlice,SafeLoop as __safeLoop,SafeDiv as __safeDiv,SafeMul as __safeMul,SafeAdd as __safeAdd,SafeSub as __safeSub,SafeExp as __safeExp,SafeMod as __safeMod,SafeFloorDiv as __safeFloorDiv,SafeUnaryAdd as __safeUnaryAdd,SafeUnarySub as __safeUnarySub,SafeFactorial as __safeFactorial
from Aspidites._vendor.contracts import contract as __contract,new_contract as __new_contract,check
from Aspidites._vendor.RestrictedPython.Guards import safe_builtins as __safe_builtins
from Aspidites._vendor.RestrictedPython.compile import compile_restricted as compile
Expand Down

0 comments on commit fd77cc0

Please sign in to comment.