async def __anew__(cls, *args, **kwargs): assert inloop() assert isinstance(cls, type) logger.debug( "Running __anew__ for object type {}, args = {}, kwargs = {}". format( cls.__qualname__, await cls.list_str(args), await cls.dict_str(kwargs), )) if __debug__: from .user_obj import UserObj if args and not issubclass(cls, UserObj): warnings.warn( "Should only be using varargs for UserObjs, not type {}". format(cls.__qualname__), stacklevel=2) new = super().__new__(cls) assert isinstance(new, MathObj) #see below # not isinstance(cls, MathObj) incase things like SeededOperator want to modify results logger.debug("Awaiting __ainit__ for object type {}".format( cls.__qualname__)) await new.__ainit__(*args, **kwargs) # super(MathObj, new).__init__() # Dont think this is needed - it's covered in __init__ return new
async def has_asyncattr(obj, attr): assert inloop() async_name = MathObj._get_async_name(attr) if not isinstance(obj, (MathObj, type(Undefined))): logger.warning('Type {} is not a MathObj or Undefined'.format( type(obj))) return async_name != None and hasattr(obj, async_name)
async def _get_oper(self, other: Any = Undefined) -> 'SeededOperator': assert inloop() from pymath2.builtins.functions.operator import opers if other is Undefined: return await opers[stack()[1][3]].__acall__(self) return await opers[stack()[1][3]].__acall__(self, await self.scrub(other))
async def __arepr__(self) -> str: assert inloop() value = await self._avalue value_attr = await self.get_asyncattr(self._avalue) assert not isinstance(value_attr, MathObj) value_repr = repr(value_attr) return '{}({})'.format(self.__class__.__name__, value_repr)
async def _args_str(self): warnloop(logger) assert inloop() ret = [] async with finish() as f: for x in (f.future(arg) for arg in self.args): ret.append(arg) return str([x.result() for x in ret])
async def _aname(self): warnloop(logger) assert inloop() if self._name is Undefined: return await self.unseeded_base_object._aname else: assert self._name is not Undefined return self.name
def deriv(self, du: 'Variable') -> ('ValuedObj', Undefined): assert not inloop() if __debug__: from .variable import Variable assert isinstance( du, Variable ), 'Can only take derivative with regards to Variable, not {}'.format( type(du)) return complete(self._aderiv(du))
async def __arepr__(self) -> str: assert inloop() if await self._ahasvalue: value_str = await self._avalue assert not isinstance(value_str, MathObj), 'value cannot be a MathObj.' # we will be calling str, so we need this. value_str = str(value_str) else: value_str = '' return '{}({})'.format(type(self).__qualname__, value_str)
async def __ainit__(self, value: NamedValuedObj, **kwargs) -> None: assert inloop() assert 'name' not in kwargs # it'll be generated. assert isinstance(value, ValuedObj) # or at least has ._aname assert isinstance(value, NamedObj) #or at least has ._avalue await super().__ainit__(name='d{}'.format(await value._aname), value=value, **kwargs)
async def __astr__(self) -> str: assert inloop() async with FinishSet() as f: value = f.future(self._avalue) hasvalue = f.future(self._ahasvalue) if not hasvalue.result(): return self.generic_str('unvalued') str_attr = await self.get_asyncattr(value.result(), '__str__') assert not isinstance(str_attr, MathObj) return str(str_attr)
def d(self, du: 'Variable') -> 'UnseededFunction': assert not inloop() if __debug__: from .variable import Variable assert isinstance( du, Variable ), 'Can only take derivative with regards to Variable, not {}'.format( type(du)) from .derivative import Derivative return complete(Derivative._a_get_deriv(self, du))
async def __aeq__(self, other: Any) -> bool: assert inloop() other = self.scrub(other) if not hasattr(other, 'value'): return False async with FinishSet() as f: myv = f.future(self._avalue) otv = f.future(other._avalue) if myv.result() == otv.result() and myv.result() is not Undefined: return True return super().__eq__(other)
async def __ainit__(self, value: Any = Undefined, **kwargs) -> None: assert inloop() if type(value) not in self._valid_types: raise TypeError( 'Cannot have type {} as a value for {}. Valid Types: {}'. format( type(value).__qualname__, type(self).__qualname__, self._valid_types, )) await super().__ainit__(value=value, **kwargs)
async def _ahasattr(self, attr_or_obj, attr=None): assert inloop() try: if attr == None: geta = getattr(self, attr_or_obj) else: geta = getattr(attr_or_obj, attr) if iscoroutine(geta): geta = await geta return geta except AttributeError: return False
async def __ainit__(self, *args, **kwargs): assert inloop() super().__ainit__(**kwargs) argsl = [ensure_future(self.scrub(arg)) for arg in args] args = [] for arg in argl: args.append(await arg) list.__init__(self, args) #baaad for attr_name in self._attrs_list_for_this_len: attr = getattr(self, attr_name) if not attr.hasname: attr.name = '{}_for_{}'.format(attr_name, id(self))
async def __ainit__(self, unseeded_base_object: 'UnseededFunction', args: tuple = Undefined, **kwargs) -> None: warnloop(logger) assert inloop() if __debug__: from .unseeded_function import UnseededFunction assert isinstance(unseeded_base_object, UnseededFunction), '{}, type {}'.format(unseeded_base_object, type(unseeded_base_object)) async with finish() as f: f.future(super().__ainit__(**kwargs)) f.future(self.__asetattr__('unseeded_base_object', unseeded_base_object)) f.future(self.__asetattr__('args', args)) assert hasattr(self, 'unseeded_base_object') assert hasattr(self, 'args')
async def __ainit__(self, *args, **kwargs): assert inloop() if __debug__: from .user_obj import UserObj if args and not isinstance(self, UserObj): warnings.warn( "Should only be using varargs for UserObjs, not type {}". format(type(self).__qualname__), stacklevel=2) logger.debug("Ran super().__init__ for object type {}".format( type(self).__qualname__), stack_info=True) super().__init__(*args, **kwargs)
async def get_asyncattr(obj, attr: str = '__repr__', call=True): assert inloop() async_name = MathObj._get_async_name(attr) if async_name != None and hasattr(obj, async_name): if not call: return getattr(obj, async_name) attr = await getattr(obj, async_name)() else: attr = getattr(obj, attr) if hasattr(attr, '__call__'): return attr() return attr quit('dont go here') return attr
def get_vars(self): warnloop(logger) #idk if this is necessary assert inloop() #idk if this is necessary ret = [] for arg in self.args: if isinstance(arg, SeededFunction): logger.debug('Append get_vars from SeededFunction at 0x{:0x}'.format(id(arg))) # cause cant call str cause might be in loop ret += arg.get_vars() elif isinstance(arg, Variable): logger.debug('Append get_vars from Variable at 0x{:0x}'.format(id(arg))) # cause cant call str cause might be in loop ret.append(arg) return ret
async def _a_get_deriv(sv, ov): assert inloop() # if not isinstance if not isinstance(ov, Variable): raise TypeError( 'Can only take deriv with regards to a Variable, not {}'. format(type(ov))) await ov.__asetattr__('_old_value_before_deriv', await ov._avalue) await ov._avalue_deleter() ret = await sv._aderiv(ov) await ov._avalue_setter(ov._old_value_before_deriv) await ov.__adelattr__('_old_value_before_deriv') return ret
async def __arsub__(self, other: Any) -> 'SeededOperator': assert inloop() return await self._get_oper(other)
def __rsub__(self, other: Any) -> 'SeededOperator': assert not inloop() return complete(self.__arsub__(other))
def __rrshift__(self, other: Any) -> 'SeededOperator': assert not inloop() return await (self.__arrshift__(other))
async def __ainvert__(self) -> 'SeededOperator': assert inloop() return await self._get_oper()
def __invert__(self) -> 'SeededOperator': assert not inloop() return complete(self.__ainvert__())
async def __afloat__(self) -> float: assert inloop() return float(await self._avalue)
def __float__(self) -> float: assert not inloop() return complete(self.__afloat__())
async def __aint__(self) -> int: assert inloop() return int(await self._avalue)
def __int__(self) -> int: assert not inloop() return complete(self.__aint__())
async def __abool__(self) -> bool: assert inloop() return bool(await self._avalue)