pyDatalog / pyDatalog / util.py

'''
Created on 28 mars 2013

source : 
    http://anandology.com/blog/using-iterators-and-generators/
    OrderedSet: http://code.activestate.com/recipes/576694/
'''
import sys
import threading

class DatalogError(Exception):
    def __init__(self, value, lineno, function):
        self.value = value
        self.lineno = lineno
        self.function = function
    def __str__(self):
        return "%s\nin line %s of %s" % (self.value, self.lineno, self.function)        


class Counter:
    lock = threading.RLock()
    def __init__(self):
        self.i = 0

    def __iter__(self):
        return self

    def next(self):
        with Counter.lock:
            self.i += 1
            return self.i

###################### support for Python 2 and 3 ####################################
# inspired by the six library

PY2 = sys.version_info[0] == 2

if PY2:
    string_types = basestring,
    
    import __builtin__ as builtins
    xrange = builtins.xrange
else:
    string_types = str,
    xrange = range

if PY2:
    def exec_(_code_, _globs_=None, _locs_=None):
        """Execute code in a namespace."""
        if _globs_ is None:
            frame = sys._getframe(1)
            _globs_ = frame.f_globals
            if _locs_ is None:
                _locs_ = frame.f_locals
            del frame
        elif _locs_ is None:
            _locs_ = _globs_
        exec("""exec _code_ in _globs_, _locs_""")


    exec_("""def reraise(tp, value, tb=None):
    raise tp, value, tb
    """)

else:
    import builtins
    exec_ = getattr(builtins, "exec")

    def reraise(tp, value, tb=None):
        if value.__traceback__ is not tb:
            raise value.with_traceback(tb)
        raise value
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.