def get_accessible_data(request: object, requested_ids: list) -> (list, list): """ Use request object to check if user has read access to a list of data (entries_id). Output is a list with accessible data and a second list with inaccessible data. :param request: :param requested_ids: :return: accessible_ids, error_ids """ if isinstance(requested_ids, int): requested_ids = [requested_ids] elif isinstance(requested_ids, str): requested_ids = [int(requested_ids)] # first get datasets without embargo / open for for everyone accessible_data = list( Entries.objects.values_list('id', flat=True).filter(pk__in=requested_ids, embargo=False)) # check if the user wanted more and is authenticated. If yes check if user has access and get the rest if len(requested_ids) > len( accessible_data) and request.user.is_authenticated: accessible_embargo_datasets = list( set(requested_ids) & set(request.session['datasets'])) # intersect sets accessible_data.extend(accessible_embargo_datasets) # check if there is still data not accessible and create error for these error_list = list(set(requested_ids) - set(accessible_data)) return {'open': accessible_data, 'blocked': error_list}
def regul_param(self, value): """ Set the value of the regularizing parameter. """ self._regularizing_parameter = value for name in ['hessian', 'gradient', 'value']: if hasattr(self, name): method = getattr(self, name) iscached = (isinstance(method, CachedMethodPermanent) or isinstance(method, CachedMethod)) if iscached: method.hard_reset()
def __mul__(self, other): """ Multiply the objective function by a scallar to set the `regul_param` attribute. """ if not isinstance(other, int) and not isinstance(other, float): raise TypeError('Can only multiply a Objective by a float or int') # Make a shallow copy of self to return. If returned self, doing # 'a = 10*b' a and b would reference the same object. obj = self.copy() obj.regul_param = obj.regul_param*other return obj
def convert_to_names(objs): """ Returns __name__ of each object unless object is already a string. In which case the string is returned as is. """ res = [] for obj in objs: if isinstance(obj, (str, bytes)): res.append(obj) else: res.append(obj.__name__) return res
def _unpack_components(self, args): """ Find all the MultiObjective elements in components and unpack them into a single list. This is needed so that ``D = A + B + C`` can be indexed as ``D[0] == A, D[1] == B, D[2] == C``. Otherwise, ``D[1]`` would be a ``MultiObjetive == B + C``. """ components = [] for comp in args: if isinstance(comp, MultiObjective): components.extend([c*comp.regul_param for c in comp]) else: components.append(comp) return components