def test_select_first_regex(self): data = [ 'foo bar', 'boo far', 'foolproof', ] assert (data > select_first(r'^b.*r$')) == 'boo far'
def test_select_first_regex(self): data = [ 'foo bar', 'boo far', 'foolproof', ] assert (data > select_first(r'^b.*r$')) == 'boo far'
def validate_each(*rules): """ Like :func:`validate`, but checks each item in `iterable` separately against `rules`. Also the first encountered error is returned without checking the rest of the items. """ return lambda val: ( (val > foreach(validate(*rules)) | select_first(X != OK) or OK) if isinstance(val, Iterable) and not isinstance(val, basestring) else (False, '"{0}" is not iterable'.format(val)))
def __init__(self, *args, **kwargs): super(SmartAdmin, self).__init__(*args, **kwargs) if self.list_display == admin.ModelAdmin.list_display: self.list_display = ('__str__', ) + (self._get_fields( lambda field: type(field) not in self.list_display_exclude)) self.date_hierarchy = (self.date_hierarchy or self.all_fields > maybe | select_first(type | X._in_([DateTimeField, DateField])) | X.name) self.list_filter = self.list_filter or self._get_list_filter() self.search_fields = self.search_fields or self._get_search_fields() self.raw_id_fields = self.raw_id_fields or self._get_fields( self.should_be_raw_id_field) self.filter_horizontal = self.filter_horizontal or self._get_fields( type | (X == ManyToManyField))
def validate(*rules): """ Creates a validation function that will check if it's input satisfies `rules`. `rules` should be an (arbitrarily nested) sequence of functions that take one argument and return a tuple of: ``(valid: bool, error: string or None)`` where `valid` says if the argument satisfies the rule and `error` says why not if it doesn't. The rules are checked sequentially and when an error is encountered, it is returned immediately from the function, without checking the rest of the rules. The returned value is the one returned from the rule, i.e. ``(False, "error message")`` If no error is encountered the function returns ``(True, None)``. (Note that the validation function itself is a rule.) """ rules = rules > flatten | tuple return lambda val: rules > foreach(X(val)) | select_first(X != OK) or OK
def test_select_first_empty(self): assert select_first(X)([]) is None
def test_select_first_none(self): result = select_first(X == 2)([0, 1, 0, 1]) assert result is None
def test_select_first(self): result = select_first(X % 2 == 0)([3, 4, 5, 6]) assert result == 4
def test_select_first_empty(self): assert select_first(X)([]) is None
def test_select_first_none(self): result = select_first(X == 2)([0, 1, 0, 1]) assert result is None
def test_select_first(self): result = select_first(X % 2 == 0)([3, 4, 5, 6]) assert result == 4