def test_subclass2(self): A = make_dataclass("A", ('x', 'y')) class B(A): __fields__ = ('z', ) class C(B): pass self.assertEqual(type(A), type(B)) self.assertEqual(type(C), type(B)) self.assertEqual(C.__dictoffset__, 0) self.assertEqual(C.__weakrefoffset__, 0) c = C(1, 2, 3) self.assertEqual(gc.is_tracked(c), False) self.assertEqual(repr(c), "C(x=1, y=2, z=3)") self.assertEqual(c.x, 1) self.assertEqual(c.y, 2) self.assertEqual(c.z, 3) self.assertEqual(asdict(c), {'x': 1, 'y': 2, 'z': 3}) self.assertEqual(sys.getsizeof(c), pyobject_size + 3 * ref_size) with self.assertRaises(TypeError): weakref.ref(c) with self.assertRaises(AttributeError): c.__dict__ c = None
def test_subclass4_tp(self): class A(dataobject): x:int y:int class B(A): z:int class N: def norm_1(self): return abs(self.x) + abs(self.y) + abs(self.z) class C(B, N): pass self.assertEqual(type(A), type(B)) self.assertEqual(type(C), type(B)) self.assertEqual(C.__dictoffset__, 0) self.assertEqual(C.__weakrefoffset__, 0) c = C(1,2,3) self.assertEqual(repr(c), "C(x=1, y=2, z=3)") self.assertEqual(c.x, 1) self.assertEqual(c.y, 2) self.assertEqual(c.z, 3) self.assertEqual(c.norm_1(), 6) self.assertEqual(asdict(c), {'x':1, 'y':2, 'z':3}) self.assertEqual(C.__annotations__, {'x':int, 'y':int, 'z':int}) # self.assertEqual(sys.getsizeof(c), 40) with self.assertRaises(TypeError): weakref.ref(c) with self.assertRaises(AttributeError): c.__dict__
def list_profiles(self) -> [Profile]: """ Lista todos os perfis Implementa: 5. listar todas as informações de todos os perfis """ return [asdict(p) for p in self.profiles]
def test_subclass3_tp(self): class A(dataobject): x:int y:int class B: def norm_1(self): return abs(self.x) + abs(self.y) class C(A, B): pass self.assertEqual(type(C), type(A)) self.assertEqual(C.__dictoffset__, 0) self.assertEqual(C.__weakrefoffset__, 0) c = C(1,2) self.assertEqual(repr(c), "C(x=1, y=2)") self.assertEqual(c.x, 1) self.assertEqual(c.y, 2) self.assertEqual(c.norm_1(), 3) self.assertEqual(asdict(c), {'x':1, 'y':2}) self.assertEqual(C.__annotations__, {'x':int, 'y':int}) with self.assertRaises(TypeError): weakref.ref(c) with self.assertRaises(AttributeError): c.__dict__
def filter_by(self, field: str, value: str) -> [Profile]: """ Lista os perfis que tem o valor 'value' no campo 'field' Implementa: 1. listar todas as pessoas formadas em um determinado curso; """ filter_fn = lambda profile: getattr(profile, field) == value query = filter(filter_fn, self.profiles) return [asdict(profile) for profile in query]
def get_profile(self, email: str, as_dict: bool = True) -> Profile: """Retorna o objeto do perfil dado como entrada a chave 'email' Implementa: 6. dado o email de um perfil, retornar suas informações. """ filter_fn = lambda profile: profile.email == email filtered = filter(filter_fn, self.profiles) profile = list(filtered)[0] return asdict(profile) if as_dict else profile
def test_defaults(self): A = make_dataclass("A", ('x', 'y', 'z'), defaults=(100, 200, 300)) a1 = A() self.assertEqual(repr(a1), "A(x=100, y=200, z=300)") self.assertEqual(a1.x, 100) self.assertEqual(a1.y, 200) self.assertEqual(a1.z, 300) self.assertEqual(asdict(a1), {'x': 100, 'y': 200, 'z': 300}) a2 = A(1, z=400) self.assertEqual(repr(a2), "A(x=1, y=200, z=400)") self.assertEqual(a2.x, 1) self.assertEqual(a2.y, 200) self.assertEqual(a2.z, 400) self.assertEqual(asdict(a2), {'x': 1, 'y': 200, 'z': 400}) a3 = A(1, 2, z=400) self.assertEqual(repr(a3), "A(x=1, y=2, z=400)") self.assertEqual(a3.x, 1) self.assertEqual(a3.y, 2) self.assertEqual(a3.z, 400) self.assertEqual(asdict(a3), {'x': 1, 'y': 2, 'z': 400})
def test_datatype_dict(self): A = make_dataclass("A", ('x', 'y'), use_dict=True, use_weakref=True) a = A(1, 2) self.assertEqual(gc.is_tracked(a), False) self.assertEqual(repr(a), "A(x=1, y=2)") self.assertEqual(a.x, 1) self.assertEqual(a.y, 2) self.assertEqual(asdict(a), {'x': 1, 'y': 2}) weakref.ref(a) self.assertEqual(a.__dict__, {}) a.z = 3 self.assertEqual(a.z, a.__dict__['z']) a = None
def test_datatype(self): A = make_dataclass("A", ('x', 'y')) a = A(1, 2) self.assertEqual(repr(a), "A(x=1, y=2)") self.assertEqual(a.x, 1) self.assertEqual(a.y, 2) self.assertEqual(asdict(a), {'x': 1, 'y': 2}) # self.assertEqual(sys.getsizeof(a), 32) with self.assertRaises(TypeError): weakref.ref(a) with self.assertRaises(AttributeError): a.__dict__ with self.assertRaises(AttributeError): a.z = 3 with self.assertRaises(AttributeError): a.z a = None
def test_datatype_dict_tp(self): class A(dataobject): __fields__ = 'x', 'y', '__dict__', '__weakref__' x:int y:int a = A(1,2) self.assertEqual(repr(a), "A(x=1, y=2)") self.assertEqual(a.x, 1) self.assertEqual(a.y, 2) self.assertEqual(asdict(a), {'x':1, 'y':2}) self.assertEqual(A.__annotations__, {'x':int, 'y':int}) # self.assertEqual(sys.getsizeof(a), 48) self.assertNotEqual(A.__dictoffset__, 0) self.assertNotEqual(A.__weakrefoffset__, 0) weakref.ref(a) self.assertEqual(a.__dict__, {}) a.z = 3 self.assertEqual(a.z, a.__dict__['z'])
def test_subclass(self): A = make_dataclass("A", ('x', 'y')) class B(A): pass self.assertEqual(type(A), type(B)) self.assertEqual(B.__dictoffset__, 0) self.assertEqual(B.__weakrefoffset__, 0) b = B(1, 2) self.assertEqual(gc.is_tracked(b), False) self.assertEqual(repr(b), "B(x=1, y=2)") self.assertEqual(b.x, 1) self.assertEqual(b.y, 2) self.assertEqual(asdict(b), {'x': 1, 'y': 2}) self.assertEqual(sys.getsizeof(b), pyobject_size + 2 * ref_size) self.assertEqual(A.__basicsize__, B.__basicsize__) with self.assertRaises(TypeError): weakref.ref(b) with self.assertRaises(AttributeError): b.__dict__
def test_datatype2_tp(self): class A(dataobject): x:int y:int a = A(1,2) self.assertEqual(repr(a), "A(x=1, y=2)") self.assertEqual(a.x, 1) self.assertEqual(a.y, 2) self.assertEqual(asdict(a), {'x':1, 'y':2}) self.assertEqual(A.__annotations__, {'x':int, 'y':int}) self.assertEqual(A.__fields__, ('x', 'y')) # self.assertEqual(sys.getsizeof(a), 32) with self.assertRaises(TypeError): weakref.ref(a) with self.assertRaises(AttributeError): a.__dict__ with self.assertRaises(AttributeError): a.z = 3 with self.assertRaises(AttributeError): a.z a = None
def test_subclass_tp(self): class A(dataobject): x:int y:int class B(A): pass self.assertEqual(type(A), type(B)) self.assertEqual(B.__dictoffset__, 0) self.assertEqual(B.__weakrefoffset__, 0) b = B(1,2) self.assertEqual(repr(b), "B(x=1, y=2)") self.assertEqual(b.x, 1) self.assertEqual(b.y, 2) self.assertEqual(asdict(b), {'x':1, 'y':2}) self.assertEqual(B.__annotations__, {'x':int, 'y':int}) # self.assertEqual(sys.getsizeof(a), 32) self.assertEqual(A.__basicsize__, B.__basicsize__) with self.assertRaises(TypeError): weakref.ref(b) with self.assertRaises(AttributeError): b.__dict__
and k.id = t.class_id order by discipline, course_number""") # Build/update mongo documents for the individual courses, collecting all their designations and # reasons. num_courses = 0 for row in curric_cursor.fetchall(): courses_cursor.execute(f"""select course_id, discipline, catalog_number, title, designation, attributes, course_status from courses where institution = 'QNS01' and discipline = '{row.discipline}' and catalog_number ~ '^{row.course_number}.?$' """) course_ids = [r.course_id for r in courses_cursor.fetchall()] designation = asdict(designation_t._make([row.name, row.type, row.reason, row.is_primary])) course = asdict(course_t._make([row.discipline, row.course_number, course_ids, designation])) if args.debug: print(course) mongo_courses.insert_one(course) num_courses += 1 if args.verbose: print(f'{num_courses:,} rows inserted into gened_courses.courses')