Skip to content

Instantly share code, notes, and snippets.

View ychennay's full-sized avatar

Yu Chen ychennay

View GitHub Profile
@ychennay
ychennay / commands.sh
Created March 12, 2020 17:51
Useful commands
# get the fingerprint of a public key
ssh-keygen -l -E md5 -f /PATH/TO/YOUR_PUBLIC_KEY.pub
@ychennay
ychennay / commands.sh
Created March 12, 2020 17:51
Useful commands
# get the fingerprint of a public key
ssh-keygen -l -E md5 -f /PATH/TO/YOUR_PUBLIC_KEY.pub
@ychennay
ychennay / deferred_loading.py
Created March 17, 2020 06:09
Example of deferred loading during initiation of Django's Model class
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
@ychennay
ychennay / data_descriptors.py
Created March 17, 2020 21:11
Example of data and non-data descriptors
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:
@ychennay
ychennay / student.py
Created March 17, 2020 21:15
Example of a Python class using descriptors as properties
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__'):
@ychennay
ychennay / data_descriptor_behavior.py
Created March 17, 2020 21:18
Data Descriptor Behavior
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}")
@ychennay
ychennay / non_data_descriptor_behavior.py
Created March 17, 2020 22:42
Example of non-data descriptor behavior
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}")
@ychennay
ychennay / metaclass_example.py
Last active December 23, 2022 05:46
Example of metaclass
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
@ychennay
ychennay / student_example_class.py
Last active March 18, 2020 04:13
Student and Model class
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):
@ychennay
ychennay / dynamic_exceptions.py
Created March 18, 2020 03:51
Examples of dynamic exceptions
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))