def test_reduce(self): from exceptions import LookupError, EnvironmentError le = LookupError(1, 2, "a") assert le.__reduce__() == (LookupError, (1, 2, "a")) le.xyz = (1, 2) assert le.__reduce__() == (LookupError, (1, 2, "a"), {"xyz": (1, 2)}) ee = EnvironmentError(1, 2, "a") assert ee.__reduce__() == (EnvironmentError, (1, 2, "a"))
def test_catch_with_unpack(self): from exceptions import LookupError try: raise LookupError(1, 2) except LookupError, (one, two): assert one == 1 assert two == 2
def check_fields(cond, owner_class): # print(cond) condspl = cond[0].split('__') if condspl[0] not in ['id', *owner_class._fields.keys()]: raise LookupError("Cannot resolve keyword '{}' into field. Choices are: {}". format(condspl[0], ', '.join(owner_class._fields.keys()))) return condspl
def get_asg_capacity(asg_client, asg_name): logger.info('Looking up size of ASG %s', asg_name) output = asg_client.describe_auto_scaling_groups( AutoScalingGroupNames=[asg_name]) asgs = output.get('AutoScalingGroups', []) if len(asgs) != 1: raise LookupError( 'Expected to find one Auto Scaling Group named %s but found %d' % (asg_name, len(asgs))) desired_capacity = asgs[0].get('DesiredCapacity') if desired_capacity is None: raise LookupError('Could not find a desired capacity for ASG %s', asg_name) return desired_capacity
def all_instances_fully_drained(describe_container_instances_response): instances = describe_container_instances_response.get('containerInstances') if not instances: raise LookupError( "The describe_container_instances returned no instances") for instance in instances: if not instance_fully_drained(instance): return False return True
def from_string(cond, owner_class): condspl = Condition.check_fields(cond, owner_class) # if isinstance(cond[1], str): # tmp_value = Condition.quote_replace(cond[1]) # elif isinstance(cond[1], (list, tuple)): # tmp_value = [Condition.quote_replace(str(i)) for i in cond[1]] # else: # tmp_value = cond[1] if len(condspl) == 2: return str(condspl[0]), str(condspl[1]), cond[1] elif len(condspl) == 1: return str(condspl[0]), 'exact', cond[1] else: raise LookupError("unresolved lookup {}".format(cond))
def format_cond(self): if self.cond in self._conditions: like_value = str(self.value).replace('\'', '\'\'') if self.cond == 'exact': return sql.SQL("{}={}").format(sql.Identifier(self.field_name), sql.Literal(str(self.value))).as_string(cursor) elif self.cond == 'in': print(self.value) print(tuple(self.value)) # print([sql.Literal(i) for i in self.value]) tmp_compose = sql.Composed(sql.SQL(', ').join([sql.Literal(i) for i in tuple(self.value)])) print(tmp_compose) return sql.SQL("{} IN ({})").format(sql.Identifier(self.field_name), tmp_compose).as_string(cursor) elif self.cond == 'lt': return sql.SQL("{} < {}").format(sql.Identifier(self.field_name), sql.Literal(str(self.value))).as_string(cursor) elif self.cond == 'gt': return sql.SQL("{} > {}").format(sql.Identifier(self.field_name), sql.Literal(str(self.value))).as_string(cursor) elif self.cond == 'le': return sql.SQL("{} <= {}").format(sql.Identifier(self.field_name), sql.Literal(str(self.value))).as_string(cursor) elif self.cond == 'ge': return sql.SQL("{} >= {}").format(sql.Identifier(self.field_name), sql.Literal(str(self.value))).as_string(cursor) elif self.cond == 'contains': return sql.SQL("{} LIKE '%{}%' ESCAPE '\\'").format(sql.Identifier(self.field_name), sql.SQL(like_value)).as_string(cursor) elif self.cond == 'startswith': return sql.SQL("{} LIKE '{}%' ESCAPE '\\'").format(sql.Identifier(self.field_name), sql.SQL(like_value)).as_string(cursor) elif self.cond == 'endswith': return sql.SQL("{} LIKE '%{}' ESCAPE '\\'").format(sql.Identifier(self.field_name), sql.SQL(like_value)).as_string(cursor) else: raise LookupError("Unsupported lookup '{}' for {} column.".format(self.cond, self.field_name))