Skip to content

Instantly share code, notes, and snippets.

View ychennay's full-sized avatar

Yu Chen ychennay

View GitHub Profile
@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 / 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 / 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 / properties.py
Created March 1, 2020 07:50
simple OOP and properties demo
class InvalidDataError(Exception):
pass
class Person:
def __init__(self, name: str, age: int):
self.name = name
self.age = age
@ychennay
ychennay / models_base_2.py
Created February 24, 2020 16:46
Part II of __new__
class ModelBase(type):
"""Metaclass for all models."""
def __new__(cls, name, bases, attrs, **kwargs):
# ... Part I code
# Pass all attrs without a (Django-specific) contribute_to_class()
# method to type.__new__() so that they're properly initialized
# (i.e. __set_name__()).
contributable_attrs = {}
@ychennay
ychennay / student_meta.py
Created February 24, 2020 16:38
Student Model with Django Meta class
class Student(models.Model):
class Meta:
abstract = True
verbose_name_plural = "students"
# rest of the original fields
@ychennay
ychennay / model.py
Created February 23, 2020 20:07
Django model class
class Model(metaclass=ModelBase):
def __init__(self, *args, **kwargs):
"""
initializes an instance of this data model
"""
@classmethod
def from_db(cls, db, field_names, values):
@ychennay
ychennay / tracing_model_base.py
Created February 22, 2020 22:39
Tracing Django Model base
class ModelBase(type):
@classmethod
def __prepare__(metacls, name, bases, **kwargs):
print(f"Calling ModelBase's __prepare__ method with name={name} and bases={bases}")
namespace = super().__prepare__(name, bases, **kwargs)
print(f"Returned namespace from ModelBase's __prepare__ method is {namespace}")
return namespace
# ... rest of metaclass definition
@ychennay
ychennay / deferred_attribute.py
Created February 22, 2020 19:59
DeferredAttribute in Django
class DeferredAttribute:
"""
A wrapper for a deferred-loading field. When the value is read from this
object the first time, the query is executed.
"""
def __init__(self, field):
self.field = field
def __get__(self, instance, cls=None):
"""