I regret nothing

Click here to load reader

download I regret nothing

of 39

  • date post

    19-Jun-2015
  • Category

    Technology

  • view

    1.836
  • download

    1

Embed Size (px)

description

You can't judge me. A lightning talk.

Transcript of I regret nothing

  • 1. Priorities onmodel.save()device = Device.objects.get(sn=1234567890)device.barcode = 46262device.save()

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. SNMPplugin Puppet SSH pluginpluginManual data Device entry 4. Loss of information12:0112:0212:37SNMP Puppet SNMP Device Device Devicehas has hasa CPU1a Xeona CPU1E5645 @2.4 GHz 5. device.save(priority=plugin.priority) 12:01P= 12:02 P= 12:37P=10 2010SNMP Puppet SNMP Device Device Attribute hashas change a CPU1 a XeonignoredE5645 @ Device2.4 GHz still hasa XeonE5645 @2.4 GHz 6. Manual data entry by a humanuserP=100 0 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. Multiple inheritance 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. 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_staffif EDITOR_TRACKABLE_MODEL is Userelse 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_staffif EDITOR_TRACKABLE_MODEL is Userelse user__is_staff: True}) 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_staffif EDITOR_TRACKABLE_MODEL is Userelse 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_staffif EDITOR_TRACKABLE_MODEL is Userelse user__is_staff: True}) 12. Monkey patchingTraceback (most recent call last):...TypeError: save() got an unexpected keywordargument priority 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. Nulldata[INFRA2][MANAGERS][MANAGER][0][POWERLEVEL] 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. >>> 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. locals() 18. def messages(request):template=messages/list.htmluser = request.usermessage_list = Message.objects.filter(owner=user)return render_to_response(template,{message_list: message_list, user: user},context_instance=RequestContext(request)) 19. def messages(request):template=messages/list.htmluser = request.usermessage_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.htmluser = request.usermessage_list = Message.objects.filter(owner=user)return locals() 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. 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. Class contextclass Gender(Choices):_ = Choices.Choicemale = _("male")female = _("female")not_specified = _("not specified")class User(db.Model):gender = ChoiceField(_("gender"), choices=Gender,default=Gender.not_specified) 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. Operatorsclass Gender(Choices):male = _("male")