Priorities on          model.save()device = Device.objects.get(sn=‘1234567890’)          device.barcode = ‘46262’         ...
from django.db import models as dbclass Device(db.Model):    name = db.CharField(verbose_name=_("name"), max_length=255)  ...
SNMP            plugin   Puppet             SSH   plugin            pluginManual data       Device            … entry
Loss of information12:01          12:02          12:37SNMP           Puppet         SNMP  • Device        • Device      • ...
device.save(priority=plugin.priority) 12:01        P=   12:02     P=   12:37      P=              10             20       ...
Manual data entry by a human            user          P=          100           0
from django.db import models as dbclass Device(db.Model):    name = db.CharField(verbose_name=_("name"), max_length=255)  ...
Multiple inheritance
class Article(Localized, Titled, Slugged,        Categorized, Taggable, AbstractArticle,        TimeTrackable, EditorTrack...
class EditorTrackable(db.Model):    created_by = db.ForeignKey(        EDITOR_TRACKABLE_MODEL,        verbose_name=_("crea...
class EditorTrackable(db.Model):    created_by = db.ForeignKey(        EDITOR_TRACKABLE_MODEL,        verbose_name=_("crea...
Monkey patchingTraceback (most recent call last):  ...TypeError: save() got an unexpected keywordargument priority
from django.db import modelsmodels.Model._lck_save = models.Model.savemodels.Model.save = (lambda self,    force_insert=Fa...
Nulldata[INFRA2][MANAGERS][MANAGER][0][POWERLEVEL]
# bad solution 1if INFRA2 in d:  if MANAGERS in d[INFRA2’]:    if MANAGER in d[INFRA2][MANAGERS]:      if len(d[INFRA2][MA...
>>> from null import Null>>> Null.any_attributeNull>>> Null[any_key’]Null>>> Null[123]Null>>> Null.any_method()Null>>> boo...
locals()
def messages(request):    template=messages/list.html    user = request.user    message_list = Message.objects.filter(    ...
def messages(request):    template=messages/list.html    user = request.user    message_list = Message.objects.filter(    ...
Class contextGENDER_MALE = 0GENDER_FEMALE = 1GENDER_NOT_SPECIFIED = 2GENDER_CHOICES = (    (GENDER_MALE, _(male)),    (GEN...
Class contextclass Gender(Choices):    male = Choice(_("male"))    female = Choice(_("female"))    not_specified = Choice(...
Class contextclass Gender(Choices):    _ = Choices.Choice    male = _("male")    female = _("female")    not_specified = _...
Operatorsclass User(db.Model):    gender = ChoiceField(_("gender"), choices=Gender,            default=Gender.not_specifie...
Operatorsclass Gender(Choices):    male = _("male") << {hello: Hi, boy.’}    female = _("female") << {hello: Hello, girl.’...
I can do this all day
Conditional fields/methodsclass ArticlePage(DisplayCounter, HasShowContent):    article = db.ForeignKey(Article,          ...
Injecting context by               execfile# settings.pyfrom lck.django import current_dir_supportexecfile(current_dir_sup...
I regret nothing
I regret nothing
I regret nothing
I regret nothing
I regret nothing
I regret nothing
I regret nothing
I regret nothing
I regret nothing
I regret nothing
I regret nothing
I regret nothing
Upcoming SlideShare
Loading in …5
×

I regret nothing

2,376 views

Published on

You can't judge me. A lightning talk.

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,376
On SlideShare
0
From Embeds
0
Number of Embeds
288
Actions
Shares
0
Downloads
10
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

I regret nothing

  1. 1. Priorities on model.save()device = Device.objects.get(sn=‘1234567890’) device.barcode = ‘46262’ device.save()
  2. 2. from django.db import models as dbclass Device(db.Model): name = db.CharField(verbose_name=_("name"), max_length=255) parent = db.ForeignKey(self, verbose_name=_("parent device"), on_delete=db.SET_NULL, null=True, blank=True, default=None, related_name="child_set") model = db.ForeignKey(DeviceModel, verbose_name=_("model"), null=True, blank=True, default=None, related_name="device_set", on_delete=db.SET_NULL) sn = db.CharField(verbose_name=_("serial number"), max_length=255, unique=True, null=True, blank=True, default=None) barcode = db.CharField(verbose_name=_("barcode"), max_length=255, unique=True, null=True, blank=True, default=None) remarks = db.TextField(verbose_name=_("remarks"), help_text=_("Additional information."), blank=True, default="") boot_firmware = db.CharField(verbose_name=_("boot firmware"), null=True, blank=True, max_length=255) hard_firmware = db.CharField(verbose_name=_("hardware firmware"), null=True, blank=True, max_length=255)# ...
  3. 3. SNMP plugin Puppet SSH plugin pluginManual data Device … entry
  4. 4. Loss of information12:01 12:02 12:37SNMP Puppet SNMP • Device • Device • Device has has has a CPU1 a Xeon a CPU1 E5645 @ 2.4 GHz
  5. 5. device.save(priority=plugin.priority) 12:01 P= 12:02 P= 12:37 P= 10 20 10SNMP Puppet SNMP • Device • Device • Attribute has has change a CPU1 a Xeon ignored E5645 @ • Device 2.4 GHz still has a Xeon E5645 @ 2.4 GHz
  6. 6. Manual data entry by a human user P= 100 0
  7. 7. from django.db import models as dbclass Device(db.Model): name = db.CharField(verbose_name=_("name"), max_length=255) parent = db.ForeignKey(self, verbose_name=_("parent device"), on_delete=db.SET_NULL, null=True, blank=True, default=None, related_name="child_set") model = db.ForeignKey(DeviceModel, verbose_name=_("model"), null=True, blank=True, default=None, related_name="device_set", on_delete=db.SET_NULL) sn = db.CharField(verbose_name=_("serial number"), max_length=255, unique=True, null=True, blank=True, default=None) barcode = db.CharField(verbose_name=_("barcode"), max_length=255, unique=True, null=True, blank=True, default=None) remarks = db.TextField(verbose_name=_("remarks"), help_text=_("Additional information."), blank=True, default="") boot_firmware = db.CharField(verbose_name=_("boot firmware"), null=True, blank=True, max_length=255) hard_firmware = db.CharField(verbose_name=_("hardware firmware"), null=True, blank=True, max_length=255)# ...
  8. 8. Multiple inheritance
  9. 9. class Article(Localized, Titled, Slugged, Categorized, Taggable, AbstractArticle, TimeTrackable, EditorTrackable, Publishable, Commentable,DisplayCounter, HasShowContent): class Meta: verbose_name = _("article") verbose_name_plural = _("articles")
  10. 10. class EditorTrackable(db.Model): created_by = db.ForeignKey( EDITOR_TRACKABLE_MODEL, verbose_name=_("created by"), null=True, blank=True, default=None, related_name=+, on_delete=db.SET_NULL, limit_choices_to={is_staff’ if EDITOR_TRACKABLE_MODEL is User else user__is_staff: True}) modified_by = db.ForeignKey( EDITOR_TRACKABLE_MODEL, verbose_name=_("modified by"), null=True, blank=True, default=None, related_name=+, on_delete=db.SET_NULL, limit_choices_to={is_staff’ if EDITOR_TRACKABLE_MODEL is User else user__is_staff: True})
  11. 11. class EditorTrackable(db.Model): created_by = db.ForeignKey( EDITOR_TRACKABLE_MODEL, verbose_name=_("created by"), null=True, blank=True, default=None, related_name=+, on_delete=db.SET_NULL, limit_choices_to={is_staff’ if EDITOR_TRACKABLE_MODEL is User else user__is_staff: True}) modified_by = db.ForeignKey( EDITOR_TRACKABLE_MODEL, verbose_name=_("modified by"), null=True, blank=True, default=None, related_name=+, on_delete=db.SET_NULL, limit_choices_to={is_staff’ if EDITOR_TRACKABLE_MODEL is User else user__is_staff: True})
  12. 12. Monkey patchingTraceback (most recent call last): ...TypeError: save() got an unexpected keywordargument priority
  13. 13. from django.db import modelsmodels.Model._lck_save = models.Model.savemodels.Model.save = (lambda self, force_insert=False, force_update=False, using=None, *args, **kwargs: self._lck_save(force_insert, force_update, using))
  14. 14. Nulldata[INFRA2][MANAGERS][MANAGER][0][POWERLEVEL]
  15. 15. # bad solution 1if INFRA2 in d: if MANAGERS in d[INFRA2’]: if MANAGER in d[INFRA2][MANAGERS]: if len(d[INFRA2][MANAGERS]): if POWERLEVEL in d[INFRA2][MANAGERS] [MANAGER][0]: return d[INFRA2][MANAGERS][MANAGER] [0][POWERLEVEL’]return None# bad solution 2data.get(INFRA2, {}).get(MANAGERS, {}).get(MANAGER, {}).get(0, []).get(POWERLEVEL’)# bad solution 3try: return data[INFRA2][MANAGERS][MANAGER][0] [POWERLEVEL]except (KeyError, IndexError): return None
  16. 16. >>> from null import Null>>> Null.any_attributeNull>>> Null[any_key’]Null>>> Null[123]Null>>> Null.any_method()Null>>> bool(Null)False# solution 4>>> from null import nullify>>> data = nullify(data)>>> data[INFRA2][MANAGERS][MANAGER][0] [POWERLEVEL’]Null
  17. 17. locals()
  18. 18. def messages(request): template=messages/list.html user = request.user message_list = Message.objects.filter( owner=user) return render_to_response( template, {message_list: message_list, user: user}, context_instance=RequestContext(request))
  19. 19. def messages(request): template=messages/list.html user = request.user message_list = Message.objects.filter( owner=user) return render_to_response( template, {message_list: message_list, user: user}, context_instance=RequestContext(request))@viewdef messages(request): template = messages/list.html’ user = request.user message_list = Message.objects.filter( owner=user) return locals()
  20. 20. Class contextGENDER_MALE = 0GENDER_FEMALE = 1GENDER_NOT_SPECIFIED = 2GENDER_CHOICES = ( (GENDER_MALE, _(male)), (GENDER_FEMALE, _(female)), (GENDER_NOT_SPECIFIED, _(not specified)),)class User(db.Model): gender = db.IntegerField(_("gender"), choices=GENDER_CHOICES)
  21. 21. Class contextclass Gender(Choices): male = Choice(_("male")) female = Choice(_("female")) not_specified = Choice(_("not specified"))class User(db.Model): gender = ChoiceField(_("gender"), choices=Gender, default=Gender.not_specified)
  22. 22. Class contextclass Gender(Choices): _ = Choices.Choice male = _("male") female = _("female") not_specified = _("not specified")class User(db.Model): gender = ChoiceField(_("gender"), choices=Gender, default=Gender.not_specified)
  23. 23. Operatorsclass User(db.Model): gender = ChoiceField(_("gender"), choices=Gender, default=Gender.not_specified) def greet(self): if self.gender == Gender.male: return "Hi, boy.” elif self.gender == Gender.female: return "Hello, girl.” else: return "Hey there, user!"
  24. 24. Operatorsclass Gender(Choices): male = _("male") << {hello: Hi, boy.’} female = _("female") << {hello: Hello, girl.’} not_specified = _("not specified") << { hello: Hey there, user!’}class User(models.Model): gender = ChoiceField(choices=Gender, default=Gender.not_specified) def greet(self): return self.gender.hello
  25. 25. I can do this all day
  26. 26. Conditional fields/methodsclass ArticlePage(DisplayCounter, HasShowContent): article = db.ForeignKey(Article, verbose_name=_("article")) page_number = db.PositiveIntegerField( verbose_name=_("page number"), default=None, null=True, blank=True) #... if settings.CUSTOMER_PAID_FOR_PRINTING_SUPPORT: def print(self): ...
  27. 27. Injecting context by execfile# settings.pyfrom lck.django import current_dir_supportexecfile(current_dir_support)# ...STATIC_ROOT = CURRENT_DIR + static

×