Skip to content

Instantly share code, notes, and snippets.

@ir4y
Created July 22, 2013 05:03
Show Gist options
  • Save ir4y/6051382 to your computer and use it in GitHub Desktop.
Save ir4y/6051382 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
from hashlib import sha1
def dirty_fabric(checkfield, *fields):
class _dirty_field(object):
def __init__(self):
self._ckeckfield = checkfield
self._fields = fields
@property
def _is_exist_field(self):
k = self.__dict__.keys()
if self._ckeckfield not in k:
return False
for field in self._fields:
if field not in k:
return False
return True
@property
def _is_valid_type(self):
return True
@property
def _is_valid_field(self):
return self._is_exist_field and self._is_valid_type
def _get_checksum(self):
checksum = sha1()
for field in self._fields:
checksum.update(self.__dict__.get(field))
return checksum.hexdigest()
def is_dirty(self):
if not self._is_valid_field:
return None
hexdig = self._get_checksum()
if self.__dict__.get(self._ckeckfield) == hexdig:
return None
return hexdig
return _dirty_field
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment