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
def subclass_exception(name, bases, module, attached_to): | |
""" | |
Create exception subclass. Used by ModelBase below. | |
The exception is created in a way that allows it to be pickled, assuming | |
that the returned exception class will be added as an attribute to the | |
'attached_to' class. | |
""" | |
return type(name, bases, { | |
'__module__': module, |
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
# stolen from https://stackoverflow.com/questions/16655097/django-abstract-models-versus-regular-inheritance | |
class PermissionsMixin(models.Model): | |
""" | |
A mixin class that adds the fields and methods necessary to support | |
Django's Group and Permission model using the ModelBackend. | |
""" | |
is_superuser = models.BooleanField(_('superuser status'), default=False, | |
help_text=_('Designates that this user has all permissions without ' | |
'explicitly assigning them.')) | |
groups = models.ManyToManyField(Group, verbose_name=_('groups'), |
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 abstract: | |
# Abstract base models can't be instantiated and don't appear in | |
# the list of models for an app. We do the final setup for them a | |
# little differently from normal models. | |
attr_meta.abstract = False | |
new_class.Meta = attr_meta | |
return new_class | |
new_class._prepare() | |
new_class._meta.apps.register_model(new_class._meta.app_label, new_class) |
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
# Inherit private fields (like GenericForeignKey) from the parent | |
# class | |
for field in base._meta.private_fields: | |
if field.name in field_names: | |
if not base._meta.abstract: | |
raise FieldError( | |
'Local field %r in class %r clashes with field of ' | |
'the same name from base class %r.' % ( | |
field.name, | |
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
class Student(models.Model): | |
... | |
class SpecificStudent(Student): | |
class Meta: | |
proxy = True | |
def perform_student_specific_action(self): | |
# ... | |
pass |
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
# Basic setup for proxy models. | |
if is_proxy: | |
base = None | |
for parent in [kls for kls in parents if hasattr(kls, '_meta')]: | |
if parent._meta.abstract: | |
if parent._meta.fields: | |
raise TypeError( | |
"Abstract base class containing model fields not " | |
"permitted for proxy model '%s'." % 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
# Add fields from abstract base class if it wasn't overridden. | |
for field in parent_fields: | |
if (field.name not in field_names and | |
field.name not in new_class.__dict__ and | |
field.name not in inherited_attributes): | |
new_field = copy.deepcopy(field) | |
new_class.add_to_class(field.name, new_field) | |
# Replace parent links defined on this base by the new | |
# field. It will be appropriately resolved if required. | |
if field.one_to_one: |
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
package com.yuchen; | |
public class Student { | |
public String name; | |
public float gpa; | |
public String getName() { | |
return 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
class Person: | |
def __init__(self, name): | |
self.__secret_attribute = 42 | |
self.name = name | |
if __name__ == "__main__": | |
person = Person("Yu") | |
# print(person.__secret_attribute) # AttributeError: 'Person' object has no attribute '__secret_attribute' |
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
f = NULL; | |
if (descr != NULL) { | |
Py_INCREF(descr); // increase the reference count of the descriptor | |
f = descr->ob_type->tp_descr_get; // access the descriptor's getter method from the descriptor's PyTypeObject ob_type | |
if (f != NULL && PyDescr_IsData(descr)) { | |
res = f(descr, obj, (PyObject *)obj->ob_type); // actual call to the descriptor's __get__ | |
if (res == NULL && suppress && | |
PyErr_ExceptionMatches(PyExc_AttributeError)) { | |
PyErr_Clear(); | |
} |