Commits

Lynn Rees committed e6c24d5

- new utilities

Comments (0)

Files changed (4)

 # -*- coding: utf-8 -*-
 '''stuf descriptor utilities'''
 
-from functools import update_wrapper
+from functools import update_wrapper, partial
 
 from stuf.deep import selfname, setter
 
         return self if this is None else self._set(this)
 
 
+class lazypartial(lazy):
+
+    '''
+    Lazily assign attributes on an instance upon first use.
+    '''
+
+    def _set(self, this):
+        return setter(this, self.name, partial(*self.method(this)))
+
+
 class lazy_class(_lazyinit):
 
     '''
 from stuf.six import items, map
 
 
-def deferfunc(func):
-    '''Defer running `func`.'''
-    yield func()
+def breakcount(func, length):
+    '''
+    Run an iterator until it reaches its original length.
 
-
-def deferiter(iterator):
-    '''Defer running `iterator`.'''
-    yield next(iterator)
+    :param iterable: an iterable to exhaust
+    '''
+    while length:
+        yield func()
+        length -= 1
 
 
 def count(iterable, enumerate=enumerate, next=next, S=StopIteration):
                 return 0
 
 
+def deferfunc(func):
+    '''Defer running `func`.'''
+    yield func()
+
+
+def deferiter(iterator):
+    '''Defer running `iterator`.'''
+    yield next(iterator)
+
+
 def exhaust(iterable, exception=StopIteration, _n=next):
     '''
     Call next on an iterator until it's exhausted.
         pass
 
 
-def breakcount(func, length):
-    '''
-    Run an iterator until it reaches its original length.
-
-    :param iterable: an iterable to exhaust
-    '''
-    while length:
-        yield func()
-        length -= 1
-
-
 def exhaustmap(mapping, call, filter=None, exception=StopIteration, _n=next):
     '''
     Call `next` on an iterator until it's exhausted.
 # -*- coding: utf-8 -*-
-# pylint: disable-msg=f0401,w0613,w0631,w0622,w0122
 '''utilities for writing code that runs on Python 2 and 3'''
 
 import sys
 import types
 import operator
 try:
+    from __builtin__ import intern
     from future_builtins import filter, map, zip
+    from cPickle import loads as ld, dumps, HIGHEST_PROTOCOL
 except ImportError:
+    from sys import intern  # @UnusedImport
     from builtins import filter, map, zip  # @UnusedImport
+    from pickle import loads as ld, dumps, HIGHEST_PROTOCOL  # @UnusedImport
 try:
     import unittest2 as unittest
 except ImportError:
         from dummy_thread import get_ident
     except ImportError:
         from _thread import get_ident  # @UnusedImport
-try:
-    from __builtin__ import intern
-except ImportError:
-    from sys import intern  # @UnusedImport
-try:
-    from cPickle import loads as ld, dumps, HIGHEST_PROTOCOL
-except ImportError:
-    from pickle import loads as ld, dumps, HIGHEST_PROTOCOL  # @UnusedImport
-
-__author__ = 'Benjamin Peterson <benjamin@python.org>'
-__version__ = '1.1.0'
 
 # True if we are running on Python 3.
 PY3 = sys.version_info[0] == 3
     PY3, HIGHEST_PROTOCOL, items, isstring, function_code, ld, dumps, u, b,
     intern)
 
+# check for None
+isnone = lambda x, y: x if y is None else y
+
 
 def memoize(f, i=intern, z=items, r=repr, uw=update_wrapper):
     '''
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.