This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# get the fingerprint of a public key | |
ssh-keygen -l -E md5 -f /PATH/TO/YOUR_PUBLIC_KEY.pub |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# get the fingerprint of a public key | |
ssh-keygen -l -E md5 -f /PATH/TO/YOUR_PUBLIC_KEY.pub |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for field in fields_iter: | |
is_related_object = False | |
# This slightly odd construct is so that we can access any | |
# data-descriptor object (DeferredAttribute) without triggering its | |
# __get__ method. | |
if (field.attname not in kwargs and | |
(isinstance(self.__class__.__dict__.get(field.attname), DeferredAttribute) | |
or field.column is None)): | |
# This field will be populated on request. | |
continue |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NameDescriptor: | |
""" | |
NameDescriptor is a data descriptor because it implements __get__ and one of either __set__ or __del__ | |
""" | |
def __init__(self, name='Any Name'): | |
print(f"\n__init__ called on NameDescriptor.") | |
self.name: str = name | |
def __get__(self, instance, klass) -> str: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Student: | |
def __init__(self): | |
print(f"\n__init__ called on Student.") | |
def __getattribute__(self, key): | |
print(f"\n__getattribute__ invoked on Student with key {key}") | |
v = super(Student, self).__getattribute__(key) | |
if hasattr(v, '__get__'): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
student = Student() | |
print(f"\nFirst access of name attribute on student: {student.name}") | |
student.__setattr__("name", "Yu Chen") | |
print(f"\n{student} instance symbol table: {student.__dict__}") | |
print(f"\nSecond access of name attribute on student: {student.name}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print(f"\nFirst access of grade attribute on student: {student.grade}") | |
student.__setattr__("grade", 12) | |
print(f"\n{student} instance symbol table: {student.__dict__}") | |
print(f"\nSecond access of grade attribute on student: {student.grade}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ModelBase(type): | |
a_variable_from_metaclass = 3 | |
_instances = dict() # keep track of the instances created per class | |
@classmethod | |
def __prepare__(metacls, name, bases, **kwargs): # called first | |
print(f"\nInside MetaClass __prepare__, name: {name}, bases: {bases}, metacls: {metacls}, kwargs: {kwargs}") | |
namespace = super().__prepare__(name, bases, **kwargs) | |
print(f"Returned namespace: {namespace}") | |
return namespace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Model(metaclass=ModelBase): | |
def __init__(self): | |
print(f"Inside __init__ of Model with: {self}") | |
super(Model, self).__init__() # object's __init__ | |
class Student(Model): | |
def __init__(self): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if not abstract: | |
new_class.add_to_class( | |
'DoesNotExist', | |
subclass_exception( | |
'DoesNotExist', | |
tuple( | |
x.DoesNotExist for x in parents if hasattr(x, '_meta') and not x._meta.abstract | |
) or (ObjectDoesNotExist,), | |
module, | |
attached_to=new_class)) |