• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
I regret nothing
 

I regret nothing

on

  • 1,808 views

You can't judge me. A lightning talk.

You can't judge me. A lightning talk.

Statistics

Views

Total Views
1,808
Views on SlideShare
1,537
Embed Views
271

Actions

Likes
2
Downloads
9
Comments
0

4 Embeds 271

http://lanyrd.com 242
http://eventifier.co 26
http://webcache.googleusercontent.com 2
https://si0.twimg.com 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    I regret nothing I regret nothing Presentation Transcript

    • Priorities on model.save()device = Device.objects.get(sn=‘1234567890’) device.barcode = ‘46262’ device.save()
    • 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)# ...
    • SNMP plugin Puppet SSH plugin pluginManual data Device … entry
    • 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
    • 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
    • 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) 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)# ...
    • Multiple inheritance
    • class Article(Localized, Titled, Slugged, Categorized, Taggable, AbstractArticle, TimeTrackable, EditorTrackable, Publishable, Commentable,DisplayCounter, HasShowContent): class Meta: verbose_name = _("article") verbose_name_plural = _("articles")
    • 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})
    • 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})
    • 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=False, force_update=False, using=None, *args, **kwargs: self._lck_save(force_insert, force_update, using))
    • 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][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
    • >>> 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
    • locals()
    • 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))
    • 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()
    • 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)
    • 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)
    • 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)
    • 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!"
    • 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
    • I can do this all day
    • 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): ...
    • Injecting context by execfile# settings.pyfrom lck.django import current_dir_supportexecfile(current_dir_support)# ...STATIC_ROOT = CURRENT_DIR + static