sys._getframe

Here are the examples of the python api sys._getframe taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

92 Examples 7

Page 1 Selected Page 2

Example 1

Project: pymo Source File: test_sys.py
Function: test_get_frame
    def test_getframe(self):
        self.assertRaises(TypeError, sys._getframe, 42, 42)
        self.assertRaises(ValueError, sys._getframe, 2000000000)
        self.assertTrue(
            SysModuleTest.test_getframe.im_func.func_code \
            is sys._getframe().f_code
        )

Example 2

Project: imagrium Source File: test_sys.py
Function: test_get_frame
    def test_getframe(self):
        self.assertRaises(TypeError, sys._getframe, 42, 42)
        self.assertRaises(ValueError, sys._getframe, 2000000000)
        self.assert_(
            SysModuleTest.test_getframe.im_func.func_code \
            is sys._getframe().f_code
        )

Example 3

Project: TrustRouter Source File: test_sys.py
Function: test_get_frame
    def test_getframe(self):
        self.assertRaises(TypeError, sys._getframe, 42, 42)
        self.assertRaises(ValueError, sys._getframe, 2000000000)
        self.assertTrue(
            SysModuleTest.test_getframe.__code__ \
            is sys._getframe().f_code
        )

Example 4

Project: cgstudiomap Source File: test_traceback.py
    def test_stack_format(self):
        # Verify _stack functions. Note we have to use _getframe(1) to
        # compare them without this frame appearing in the output
        with captured_output("stderr") as ststderr:
            traceback.print_stack(sys._getframe(1))
        stfile = StringIO()
        traceback.print_stack(sys._getframe(1), file=stfile)
        self.assertEqual(ststderr.getvalue(), stfile.getvalue())

        stfmt = traceback.format_stack(sys._getframe(1))

        self.assertEqual(ststderr.getvalue(), "".join(stfmt))

Example 5

Project: pytest-bdd Source File: steps.py
Function: get_caller_module
def get_caller_module(depth=2):
    """Return the module of the caller."""
    frame = sys._getframe(depth)
    module = inspect.getmodule(frame)
    if module is None:
        return get_caller_module(depth=depth)
    return module

Example 6

Project: quark Source File: quark_threaded_runtime.py
Function: fail
    def fail(self, message):
        self.event_thread.die_now = True
        if False:  # Set to True to enable traceback printing
            print("Traceback (most recent call last, up to fail(...)):")
            traceback.print_stack(sys._getframe(1))
            # Note: sys._getframe(1) works on CPython, Jython, and PyPy. Need to test on IronPython etc.
        sys.stderr.write(message + "\n")
        os._exit(1)

Example 7

Project: client_python Source File: decorator.py
Function: update
    def update(self, func, **kw):
        "Update the signature of func with the data in self"
        func.__name__ = self.name
        func.__doc__ = getattr(self, 'doc', None)
        func.__dict__ = getattr(self, 'dict', {})
        func.__defaults__ = getattr(self, 'defaults', ())
        func.__kwdefaults__ = getattr(self, 'kwonlydefaults', None)
        func.__annotations__ = getattr(self, 'annotations', None)
        try:
            frame = sys._getframe(3)
        except AttributeError:  # for IronPython and similar implementations
            callermodule = '?'
        else:
            callermodule = frame.f_globals.get('__name__', '?')
        func.__module__ = getattr(self, 'module', callermodule)
        func.__dict__.update(kw)

Example 8

Project: python-aspectlib Source File: debug.py
Function: format_stack
def format_stack(skip=0, length=6, _sep=os.path.sep):
    """
    Returns a one-line string with the current callstack.
    """
    return ' < '.join("%s:%s:%s" % (
        '/'.join(f.f_code.co_filename.split(_sep)[-2:]),
        f.f_lineno,
        f.f_code.co_name
    ) for f in islice(frame_iterator(sys._getframe(1 + skip)), length))

Example 9

Project: pgi Source File: _compat.py
Function: exec
    def exec_(_code_, _globs_=None, _locs_=None):
        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_""")

Example 10

Project: cydra Source File: component.py
Function: implements
    @staticmethod
    def implements(*interfaces):
        """Can be used in the class definition of `Component`
        subclasses to declare the extension points that are extended.
        """
        import sys

        frame = sys._getframe(1)
        locals_ = frame.f_locals

        # Some sanity checks
        assert locals_ is not frame.f_globals and '__module__' in locals_, \
               'implements() can only be used in a class definition'

        locals_.setdefault('_implements', []).extend(interfaces)

Example 11

Project: BlenderPanda Source File: baseplatform.py
Function: find_module
def _find_module( exclude = (__name__,)):
    frame = sys._getframe()
    while frame and '__name__' in frame.f_globals:
        if exclude:
            if not frame.f_globals['__name__'] in exclude:
                return frame.f_globals['__name__']
            
        else:
            return frame.f_globals['__name__']
        frame = frame.f_back
    return None

Example 12

Project: maraschino Source File: sqlalchemy.py
def _calling_context(app_path):
    frm = sys._getframe(1)
    while frm.f_back is not None:
        name = frm.f_globals.get('__name__')
        if name and (name == app_path or name.startswith(app_path + '.')):
            funcname = frm.f_code.co_name
            return '%s:%s (%s)' % (
                frm.f_code.co_filename,
                frm.f_lineno,
                funcname
            )
        frm = frm.f_back
    return '<unknown>'

Example 13

Project: qcore Source File: inspection.py
def lazy_stack():
    """Return a generator of records for the stack above the caller's frame.

    Equivalent to inspect.stack() but potentially faster because it does not compute info for all
    stack frames.

    As a further optimization, yields raw frame objects instead of tuples describing the frame. To
    get the full information, call inspect.getframeinfo(frame).

    """
    frame = sys._getframe(1)

    while frame:
        yield frame
        frame = frame.f_back

Example 14

Project: ashiba Source File: dock_layout.py
Function: set_up
    def setup(self, node):
        """ Setup the dock layout validator.

        """
        self._caller = sys._getframe(2)
        self._seen_items = set()
        self._cant_maximize = {}

Example 15

Project: bluepass Source File: support.py
Function: add_result
    def add_result(self, result, params={}, name=None):
        """Add a performance test result."""
        if name is None:
            frame = sys._getframe(1)
            clsname = frame.f_locals.get('self', '').__class__.__name__
            methname = frame.f_code.co_name
            names = split_cap_words(clsname)
            name = '{0}_{1}'.format(''.join(names[1:]), methname[len(self.test_prefix)+1:]).lower()
        if params is not None:
            params = ','.join(['{0}={1}'.format(k, params[k]) for k in params])
        with open(self.results_name, 'a') as fout:
            fout.write('{0:<32s} {1:<16.2f} {2:s}\n'.format(name, result, params))

Example 16

Project: python-compat-runtime Source File: utils.py
Function: get_stack
def get_stack(limit=10):
  # Return a list of strings showing where the current frame was called.
  if not DEBUG:
    return ()
  frame = sys._getframe(1)  # Always skip get_stack() itself.
  lines = []
  while len(lines) < limit and frame is not None:
    f_locals = frame.f_locals
    ndb_debug = f_locals.get('__ndb_debug__')
    if ndb_debug != 'SKIP':
      line = frame_info(frame)
      if ndb_debug is not None:
        line += ' # ' + str(ndb_debug)
      lines.append(line)
    frame = frame.f_back
  return lines

Example 17

Project: flake8-putty Source File: extension.py
def get_reporter_state():
    """Get pep8 reporter state from stack."""
    # Stack
    # 1. get_reporter_state (i.e. this function)
    # 2. putty_ignore_code
    # 3. QueueReport.error or pep8.StandardReport.error for flake8 -j 1
    # 4. pep8.Checker.check_ast or check_physical or check_logical
    #    locals contains `tree` (ast) for check_ast
    frame = sys._getframe(3)
    reporter = frame.f_locals['self']
    line_number = frame.f_locals['line_number']
    offset = frame.f_locals['offset']
    text = frame.f_locals['text']
    check = frame.f_locals['check']
    return reporter, line_number, offset, text, check

Example 18

Project: azure-linux-extensions Source File: _compat.py
Function: exec
    def exec_(code, globs=None, locs=None):
        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""")

Example 19

Project: root_numpy Source File: six.py
Function: exec
    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_""")

Example 20

Project: python-pptx Source File: base.py
Function: alias
def alias(*aliases):
    """
    Decorating a class with @alias('FOO', 'BAR', ..) allows the class to
    be referenced by each of the names provided as arguments.
    """
    def decorator(cls):
        # alias must be set in globals from caller's frame
        caller = sys._getframe(1)
        globals_dict = caller.f_globals
        for alias in aliases:
            globals_dict[alias] = cls
        return cls
    return decorator

Example 21

Project: enigma2 Source File: BugHunting.py
Function: get_frames
def getFrames(deep=2):
	if deep is None or deep == 0:
		deep=1
	frames = []
	for x in range(2,3+deep):
		try:
			frames.append(sys._getframe(x))
		except:
			break
	return frames

Example 22

Project: monasca-analytics Source File: decorators.py
def enclosing_frame(frame=None, level=3):
    """Get an enclosing frame that skips DecoratorTools callback code"""
    frame = frame or sys._getframe(level)
    while frame.f_globals.get('__name__') == __name__:
        frame = frame.f_back
    return frame

Example 23

Project: Concepts Source File: pattern_match.py
    def _bind_to_calling_scope(self):
        '''
        Inject the result of a successful match into the calling scope.
        This only works inside of a decorated function; use dict style
        lookup syntax for use in a context manager.
        NOTE: This uses some not-so-nice abuse of stack frames and the
              ctypes API to make this work and as such it will probably
              not run under anything other than cPython.
        '''
        # Grab the stack frame that the caller's code is running in
        frame = _getframe(2)
        # Dump the matched variables and their values into the frame
        frame.f_locals.update(self.map)
        # Force an update of the frame locals from the locals dict
        pythonapi.PyFrame_LocalsToFast(
            py_object(frame),
            c_int(0)
        )

Example 24

Project: numexpr Source File: necompiler.py
Function: get_arguments
def getArguments(names, local_dict=None, global_dict=None):
    """Get the arguments based on the names."""
    call_frame = sys._getframe(2)
    if local_dict is None:
        local_dict = call_frame.f_locals
    if global_dict is None:
        global_dict = call_frame.f_globals

    arguments = []
    for name in names:
        try:
            a = local_dict[name]
        except KeyError:
            a = global_dict[name]
        arguments.append(numpy.asarray(a))
    return arguments

Example 25

Project: python-control Source File: runtests.py
Function: exec
    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""")

Example 26

Project: eve-central.com Source File: reftree.py
    def ignore_caller(self):
        f = sys._getframe()     # = this function
        cur = f.f_back          # = the function that called us (probably 'walk')
        self.ignore(cur, cur.f_builtins, cur.f_locals, cur.f_globals)
        caller = f.f_back       # = the 'real' caller
        self.ignore(caller, caller.f_builtins, caller.f_locals, caller.f_globals)

Example 27

Project: compliance-checker Source File: util.py
Function: get_attr
    def __getattr__(self, key):
        """ Make attempts to lookup by nonexistent attributes also attempt key lookups. """
        if key in self:
            return self[key]
        import sys
        import dis
        frame = sys._getframe(1)
        if '\x00%c' % dis.opmap['STORE_ATTR'] in frame.f_code.co_code:
            self[key] = DotDict()
            return self[key]

        raise AttributeError(key)

Example 28

Project: pagd Source File: h.py
Function: read_file
def readfile( relpath ):
    """Read file from path ``relpath`` relative to calling template."""
    frame = sys._getframe(1)
    filen = frame.f_globals.get('_ttlfile', None)
    if isfile( filen ) :
        f = join( dirname(filen), relpath )
        if isfile( f ) :
            return open(f).read()
    return ''

Example 29

Project: deep_recommend_system Source File: resource_loader.py
def get_data_files_path():
  """Get the directory where files specified in data attribute are stored.

  Returns:
    The directory where files specified in data attribute of py_test
    and py_binary are stored.
  """
  return _os.path.dirname(_inspect.getfile(_sys._getframe(1)))

Example 30

Project: clastic Source File: tbutils.py
Function: from_frame
    @classmethod
    def from_frame(cls, frame, limit=None):
        ret = []
        if frame is None:
            frame = sys._getframe(1)  # cross-impl yadayada
        if limit is None:
            limit = getattr(sys, 'tracebacklimit', 1000)
        n = 0
        while frame is not None and n < limit:
            item = cls.callpoint_type.from_frame(frame)
            ret.append(item)
            frame = frame.f_back
            n += 1
        ret.reverse()
        return cls(ret)

Example 31

Project: rad2py Source File: qdb.py
Function: start_up
    def startup(self):
        "Notify and wait frontend to set initial params and breakpoints"
        # send some useful info to identify session
        thread = threading.current_thread()
        # get the caller module filename
        frame = sys._getframe()
        fn = self.canonic(frame.f_code.co_filename)
        while frame.f_back and self.canonic(frame.f_code.co_filename) == fn:
            frame = frame.f_back
        args = [__version__, os.getpid(), thread.name, " ".join(sys.argv),
                frame.f_code.co_filename]
        self.pipe.send({'method': 'startup', 'args': args})
        while self.pull_actions() is not None:
            pass

Example 32

Project: stupid-python-tricks Source File: one_line_regex.py
def magic_match(pattern, target):
    """
    Match a regex against a target string.

    Assign the result to a 'match' variable in the *caller's scope*.
    """
    frame = sys._getframe(1)
    result = re.match(pattern, target)
    # This is properly, properly evil. Don't do this:
    frame.f_locals['match'] = result
    return result

Example 33

Project: scalyr-agent-2 Source File: lex.py
Function: get_caller_module_dict
def get_caller_module_dict(levels):
    f = sys._getframe(levels)
    ldict = f.f_globals.copy()
    if f.f_globals != f.f_locals:
        ldict.update(f.f_locals)
    return ldict

Example 34

Project: qdb Source File: test_cmd_manager.py
    def MockTracer(self):
        """
        Construct a mock tracer.
        """
        tracer = MagicMock()
        tracer.address = self.tracer_host, self.tracer_port
        tracer.pause_signal = signal.SIGUSR2
        tracer.retry_attepts = 1
        tracer.local = 0, 0
        tracer.uuid = 'mock'
        tracer.watchlist = {}
        tracer.curframe = sys._getframe()
        tracer.stack = [(sys._getframe(), 1)] * 3
        tracer.skip_fn = lambda _: False
        tracer.cmd_manager = self.cmd_manager
        return tracer

Example 35

Project: gitosis Source File: util.py
Function: make_temp
def maketemp():
    tmp = os.path.join(os.path.dirname(__file__), 'tmp')
    mkdir(tmp)

    caller = sys._getframe(1)
    name = '%s.%s' % (
        sys._getframe(1).f_globals['__name__'],
        caller.f_code.co_name,
        )
    tmp = os.path.join(tmp, name)
    try:
        shutil.rmtree(tmp)
    except OSError, e:
        if e.errno == errno.ENOENT:
            pass
        else:
            raise
    os.mkdir(tmp)
    return tmp

Example 36

Project: 8-bits Source File: tasklets_test.py
Function: test_basic_error
  def testBasicError(self):
    self.ExpectWarnings()
    frames = [sys._getframe()]
    @tasklets.tasklet
    def level3():
      frames.append(sys._getframe())
      raise RuntimeError('hello')
      yield
    @tasklets.tasklet
    def level2():
      frames.append(sys._getframe())
      yield level3()
    @tasklets.tasklet
    def level1():
      frames.append(sys._getframe())
      yield level2()
    @tasklets.tasklet
    def level0():
      frames.append(sys._getframe())
      yield level1()
    fut = level0()
    try:
      fut.check_success()
    except RuntimeError, err:
      _, _, tb = sys.exc_info()
      self.assertEqual(str(err), 'hello')
      tbframes = []
      while tb is not None:
        # It's okay if some _help_tasklet_along frames are present.
        if tb.tb_frame.f_code.co_name != '_help_tasklet_along':
          tbframes.append(tb.tb_frame)
        tb = tb.tb_next
      self.assertEqual(frames, tbframes)
    else:
      self.fail('Expected RuntimeError not raised')

Example 37

Project: spladder Source File: rproc.py
def rproc(ProcName, P1, Mem=None, options=None, runtime=None, callfile=None, resubmission=False):
    # [jobinfo]=rproc(ProcName, P1, Mem, options, time)
    #
    # time in minutes
    # mem in mb

    global SCHED_JOB_ID_SPLIT, SCHED_GET_JOB_NUMBER, SCHED_SUBMIT_CMD
    global SCHED_MIN_OPTIONS

    _set_scheduler()

    environment = '' # TODO

    if callfile is None:
        ### check if ProcName is defined in calling function
        callframe = sys._getframe(1)
        if not ProcName in callframe.f_locals:
            if not ProcName in callframe.f_globals:
                print >> sys.stderr, 'ERROR: Could find no definition for %s in local or global context of calling function. Use kword callfile to specify file where %s is defined. Use the relative path to the location of the calling function!' % (ProcName, ProcName)
                return 
            else:
                callfile = (callframe.f_globals[ProcName].__module__, inspect.getfile(callframe.f_globals[ProcName]))
        else:
            callfile = (callframe.f_locals[ProcName].__module__, inspect.getfile(callframe.f_locals[ProcName]))

    ### detect path of this script
    this_frame = sys._getframe(0)
    rproc_path = os.path.abspath(inspect.getfile(this_frame))

    if runtime is None:
        runtime = 24

    if Mem is None:
        Mem = 300

    if Mem < 100:
        print >> sys.stderr, 'WARNING: You specified to allocate less than 100Mb memory for your job. This might not be enough to start. Re-setting to 100Mb'
        Mem = 100

    if options is None:
        options = dict()

    ### get module list of caller to re-create environment
    if not 'imports' in options:
        options['imports'] = dict()
    if not resubmission:
        callframe = sys._getframe(1)
        #options['package'] = os.path.dirname(os.path.abspath(callframe.f_globals['__file__']))
        for l in callframe.f_globals:
            if (len(l) < 2 or l[:2] != '__'):
                if isinstance(callframe.f_globals[l], types.ModuleType):
                    if not l in options['imports']:
                        if imp.is_builtin(callframe.f_globals[l].__name__) != 0:
                            options['imports'][l] = (callframe.f_globals[l].__name__, 'builtin') 
                        else:
                            options['imports'][l] = (callframe.f_globals[l].__name__, callframe.f_globals[l].__file__) 

    if not callfile[0] in options['imports']:
        options['imports'][callfile[0]] = callfile

    home_str = os.environ['HOME'] 

    use_reservation = False
    ### TODO this is only relevant for SGE
    if 'ncpus' in options and options['ncpus'] > 1:
        use_reservation = 1 ;

    if not 'verbosity' in options:
        options['verbosity'] = True
    if not 'maxjobs' in options:
        options['maxjobs'] = 5000
    if not 'waitonfull' in options:
        options['waitonfull'] = True
    if not 'immediately' in options:
        options['immediately'] = False
    if not 'immediately_bg' in options:
        options['immediately_bg'] = False
    if not 'submit_now' in options:
        options['submit_now'] = True
    if not 'nicetohave' in options:
        options['nicetohave'] = False
    if not 'ncpus' in options:
        options['ncpus'] = 1
    if not 'start_dir' in options:
        dirctry = os.getcwd()
    else:
        dirctry = options['start_dir'] 
    if not 'resubmit' in options:
        options['resubmit'] = False
        options['time_req_resubmit'] = []
        options['mem_req_resubmit'] = []
    if not 'data_size' in options:
        options['data_size'] = [] 
    if not 'hard_time_limit' in options:
        options['hard_time_limit'] = 1000000

    jobinfo = rproc_empty()

    jobinfo.ProcName = ProcName
    jobinfo.P1 = P1
    jobinfo.Mem = Mem
    jobinfo.options = options
    jobinfo.time = runtime
    jobinfo.created = True
    jobinfo.resubmit = options['resubmit']
    jobinfo.mem_req_resubmit = options['mem_req_resubmit']
    jobinfo.time_req_resubmit = options['time_req_resubmit']
    jobinfo.data_size = options['data_size']
    jobinfo.hard_time_limit = options['hard_time_limit']

    if not os.path.exists(os.path.join(os.environ['HOME'], 'tmp', '.sge')):
        username = os.environ['USER']
        base_dir = os.path.join(home_str, '.sge.', 'tmp', username)
        if not os.path.exists(base_dir):
            os.makedirs(base_dir)

        tmp_dir = os.path.join(home_str, '.sge', 'tmp', username, 'tmp')
        if not os.path.exists(tmp_dir):
            os.makedirs(tmp_dir)

        sge_dir = os.path.join(home_str, '.sge', 'tmp', username, 'sge')
        if not os.path.exists(sge_dir):
            os.makedirs(sge_dir)

        if not os.path.exists(os.path.join(os.environ['HOME'], 'tmp')):
            os.makedirs(os.path.join(os.environ['HOME'], 'tmp'))

        if not os.path.exists(os.path.join(os.environ['HOME'], 'tmp', '.sge')):
            ### TODO this does not exist anywhere: sge_tmp_dir
            os.symlink(sge_tmp_dir, os.path.join(os.environ['HOME'], 'tmp', '.sge'))

    assert(os.path.exists(os.path.join(os.environ['HOME'],'tmp', '.sge')))

    if not os.path.exists(os.path.join(dirctry, '.sge')):
        username = os.environ['USER']
        ### TODO make this configurable
        sge_base_dir = dirctry.replace(os.path.join('cluster', 'home', username), os.path.join(home_str, '.sge', 'tmp', username))
        if not os.path.exists(sge_base_dir):
            os.makedirs(sge_base_dir)

        sge_dir = os.path.join(sge_base_dir, 'sge')
        os.makedirs(sge_dir)
        
        os.symlink(sge_dir, os.path.join(dirctry, '.sge'))

    ### assembly option string
    if use_reservation:
        option_str = ' -R y'
    else:
        option_str = ''
     
    #option_str += ' -l h_vmem=%iM -l s_vmem=%iM -soft -l h_cpu=%1.0f -hard ' % (Mem, Mem,  max(60, runtime*60))
    # TORQUE
    #option_str += '-l nodes=1:ppn=%i -l mem=%imb,vmem=%imb,pmem=%imb -l walltime=%1.0f' % (options['ncpus'], Mem, Mem,  Mem, max(60, runtime*60))
    #option_str += '-n %i -M %i -R "rusage[mem=%i]" -W %i' % (options['ncpus'], Mem, math.ceil(Mem / float(options['ncpus'])), max(60, runtime*60))
    option_str += SCHED_MIN_OPTIONS.substitute(cores=str(options['ncpus']), mem=str(Mem), coremem=str(math.ceil(Mem / float(options['ncpus']))), time=str(max(60, runtime)))

    if environment == 'galaxy':
        option_str += ' -l parent=0.0 '

    if 'hold' in options:
        if options['hold']: 
            option_str += ' -h u'
    if 'queue' in options:
        option_str += ' -q "%s" ' % options['queue']
    if 'nicetohave' in options and options['nicetohave']:
        option_str += ' -l nicetohave=1'

    if 'priority' in options:
        option_str += ' -p %i' % options['priority']

    if 'express' in options and options['express']:
        option_str += ' -l express'

    if 'hostname' in options:
        option_str += '%s -l hostname=%s' % (option_str, options['hostname'])

    ### TODO make this configurable
    # use same pthon that the one it was called with
    bin_str = sys.executable

    ### request cplex license
    if 'cplex' in options and options['cplex']:
        option_str += ' -l cplex=1'

    ### request several cpus
    #if 'ncpus' in options and options['ncpus'] > 1:
    #    option_str += ' -pe "*" %i ' % options['ncpus']

    if 'identifier' in options:
      identifier = options['identifier']
    else:
      identifier = 'RP' ;

    cc = random.randint(0, 100000)
    prefix = '%s%i-%1.10f' % (identifier, cc, time.time())
    rproc_dir = '%s/tmp/.sge' % os.environ['HOME']
    mat_fname = os.path.join(rproc_dir, '%s.pickle' % prefix) 
    data_fname = os.path.join(rproc_dir, '%s_data.pickle' % prefix) 
    result_fname = os.path.join(rproc_dir, '%s_result.pickle' % prefix)
    m_fname = os.path.join(rproc_dir, '%s.sh' % prefix)
    while os.path.exists(mat_fname) or os.path.exists(result_fname) or os.path.exists(m_fname):
        cc = random.randint(0, 100000)
        prefix = '%s%i-%1.10f' % (identifier, cc, time.time())
        mat_fname = os.path.join(rproc_dir, '%s.pickle' % prefix) 
        data_fname = os.path.join(rproc_dir, '%s_data.pickle' % prefix) 
        result_fname = os.path.join(rproc_dir, '%s_result.pickle' % prefix)
        m_fname = os.path.join(rproc_dir, '%s.sh' % prefix)

    if 'log_fname' in options:
        log_fname = options['log_fname']
    else:
        log_fname = os.path.join(dirctry, '.sge', '%s_%s.rproc' % (prefix, time.strftime('%d-%b-%Y_%H_%M')))
    qsublog_fname = '%s.qsubout' % log_fname

    jobinfo.prefix = prefix 
    jobinfo.mat_fname = mat_fname 
    jobinfo.data_fname = data_fname
    jobinfo.result_fname = result_fname 
    jobinfo.m_fname = m_fname 
    jobinfo.log_fname = log_fname 
    jobinfo.qsublog_fname = qsublog_fname 
    jobinfo.callfile = callfile

    ### save the call information
    cPickle.dump((ProcName, dirctry, options, callfile), open(mat_fname, 'wb'), -1)
    cPickle.dump(P1, open(data_fname, 'wb'), -1)

    evalstring = '%s %s %s %s' % (bin_str, rproc_path, mat_fname, data_fname)
    evalstring = 'cd %s; %s; exit' % (dirctry, evalstring)
    fd = open(m_fname, 'w')
    print >> fd, '%s' % evalstring
    fd.close()

    if 'envstr' in options:
        envstr = options['envstr']
        if len(envstr) > 0:
          envstr += ';' 
    else:
        envstr = '' 

    if options['immediately']:
        callstr = '%s bash %s >> %s' % (envstr, m_fname, log_fname)
    elif options['immediately_bg']:
        callstr = '%s bash %s >> %s &' % (envstr, m_fname, log_fname)
    else:
      #str = 'echo \'%s hostname; bash %s >> %s\' | qsub -o %s -j y -r y %s -N %s >> %s 2>&1' % (envstr, m_fname, log_fname, qsublog_fname, option_str, prefix, log_fname)
      # TORQUE
      #str = 'echo \'%s hostname; bash %s >> %s\' | qsub -o %s -j oe -r y %s -N %s >> %s 2>&1' % (envstr, m_fname, log_fname, qsublog_fname, option_str, prefix, log_fname)
      # LSF
      #str = 'echo \'%s hostname; bash %s >> %s\' | bsub -o %s -e %s %s -J %s >> %s 2>&1' % (envstr, m_fname, log_fname, qsublog_fname, qsublog_fname, option_str, prefix, log_fname)
      callstr = SCHED_SUBMIT_CMD.substitute(env=envstr, script=m_fname, log=log_fname, qsub_log=qsublog_fname, options=option_str, name=prefix)

      #print >> sys.stderr, callstr

    ### too verbose
    #if options['submit_now'] and options['verbosity']:
    #    print callstr

    # wait until we are allowed to submit again, i.e. #jobs < maxjobs
    if not options['immediately'] and not options['immediately_bg'] and options['waitonfull']:
        while True:
            try:
                #num_queued = int(subprocess.check_output('qstat -u' + os.environ['USER'] + '2> /dev/null | grep ' + os.environ['USER'] + '| wc -l | tr -d " "', shell=True).strip())
                #num_queued = int(subprocess.check_output('bjobs -u' + os.environ['USER'] + '2> /dev/null | grep ' + os.environ['USER'] + '| wc -l | tr -d " "', shell=True).strip())
                num_queued = int(subprocess.check_output(SCHED_GET_JOB_NUMBER.substitute(user=os.environ['USER']), shell=True).strip())
            except:
                print >> sys.stderr, 'WARNING: could not determine how many jobs are scheduled'
                break
            
            # keep 50 spare jobs if multiple rprocs are scheduling...
            if (num_queued < options['maxjobs']):
                break
            else:
                if options['verbosity']:
                    print >> sys.stdout, 'queue full, sleeping 60 seconds (%i/%i)' %(num_queued, options['maxjobs'])
                time.sleep(60)

    if options['submit_now']:
        if options['immediately'] and options['verbosity']:
            print >> sys.stdout, 'immediatedly starting job on local machine'
        if options['immediately_bg'] and options['verbosity']:
            print >> sys.stdout, 'immediatedly starting job on local machine in background'

        if options['immediately_bg']:
            while True:
                str_ = subprocess.check_output('uptime').strip()
                float(str_[re.search('average:', str_).start()+8:].split(',')[0])
                hit = re.search('average:', str_)
                while hit is None:
                    hit = re.search('average:', str_)
                idx = hit.start() 
                cpu_load = float(str_[idx+8:].split(',')[0])
                if cpu_load > 13:
                    if options['verbosity']:
                        print 'load too high: %1.2f' % cpu_load
                    time.sleep(10)
                else:
                    break
            time.sleep(2)
      
        p1 = subprocess.Popen(['echo', callstr], stdout=subprocess.PIPE)
        p2 = subprocess.Popen(['bash'], stdin=p1.stdout, stdout=subprocess.PIPE)
        p2.communicate()
        ret = p2.returncode
        if ret != 0:
            print >> sys.stderr, 'submission failed:\n\tsubmission string: %s\n\treturn code: %i' % (callstr, ret)
        jobinfo.submission_time = time.time()
      
        ### grab job ID from submission log file
        if not options['immediately'] and not options['immediately_bg']:
            fd = open(log_fname, 'r')
            jobinfo.jobid = -1
            if fd:
                s = fd.read().strip()

                items = eval(SCHED_JOB_ID_SPLIT.substitute(var='s'))
                try:
                    jobinfo.jobid = int(items[0])
                except:
                    print >> sys.stderr, callstr
                    print >> sys.stderr, 'ERROR: submission failed: %s' % s
                    sys.exit(1)
                fd.close()
                rproc_register('submit', jobinfo)
            else:
                print  >> sys.stderr, '%s does not exist' % log_fname
        else:
            jobinfo.jobid = 0 
    else:
        jobinfo.jobid = 0 

    return jobinfo

Example 38

Project: exabgp Source File: objgraph.py
def _show_graph(objs, edge_func, swap_source_target,
                max_depth=3, extra_ignore=(), filter=None, too_many=10,
                highlight=None, filename=None, extra_info=None,
                refcounts=False, shortnames=True, output=None,
                cull_func=None):
    if not isinstance(objs, (list, tuple)):
        objs = [objs]
    if filename and output:
        raise ValueError('Cannot specify both output and filename.')
    elif output:
        f = output
    elif filename and filename.endswith('.dot'):
        f = codecs.open(filename, 'w', encoding='utf-8')
        dot_filename = filename
    else:
        fd, dot_filename = tempfile.mkstemp(prefix='objgraph-',
                                            suffix='.dot', text=True)
        f = os.fdopen(fd, "w")
        if getattr(f, 'encoding', None):
            # Python 3 will wrap the file in the user's preferred encoding
            # Re-wrap it for utf-8
            import io
            f = io.TextIOWrapper(f.detach(), 'utf-8')
    f.write('digraph ObjectGraph {\n'
            '  node[shape=box, style=filled, fillcolor=white];\n')
    queue = []
    depth = {}
    ignore = set(extra_ignore)
    ignore.add(id(objs))
    ignore.add(id(extra_ignore))
    ignore.add(id(queue))
    ignore.add(id(depth))
    ignore.add(id(ignore))
    ignore.add(id(sys._getframe()))   # this function
    ignore.add(id(sys._getframe().f_locals))
    ignore.add(id(sys._getframe(1)))  # show_refs/show_backrefs
    ignore.add(id(sys._getframe(1).f_locals))
    for obj in objs:
        f.write('  %s[fontcolor=red];\n' % (_obj_node_id(obj)))
        depth[id(obj)] = 0
        queue.append(obj)
        del obj
    gc.collect()
    nodes = 0
    while queue:
        nodes += 1
        # The names "source" and "target" are reversed here because
        # originally there was just show_backrefs() and we were
        # traversing the reference graph backwards.
        target = queue.pop(0)
        tdepth = depth[id(target)]
        f.write('  %s[label="%s"];\n' % (_obj_node_id(target),
                                         _obj_label(target, extra_info,
                                                    refcounts, shortnames)))
        h, s, v = _gradient((0, 0, 1), (0, 0, .3), tdepth, max_depth)
        if inspect.ismodule(target):
            h = .3
            s = 1
        if highlight and highlight(target):
            h = .6
            s = .6
            v = 0.5 + v * 0.5
        f.write('  %s[fillcolor="%g,%g,%g"];\n'
                % (_obj_node_id(target), h, s, v))
        if v < 0.5:
            f.write('  %s[fontcolor=white];\n' % (_obj_node_id(target)))
        if hasattr(getattr(target, '__class__', None), '__del__'):
            f.write('  %s->%s_has_a_del[color=red,style=dotted,'
                    'len=0.25,weight=10];\n' % (_obj_node_id(target),
                                                _obj_node_id(target)))
            f.write('  %s_has_a_del[label="__del__",shape=doublecircle,'
                    'height=0.25,color=red,fillcolor="0,.5,1",fontsize=6];\n'
                    % (_obj_node_id(target)))
        if tdepth >= max_depth:
            continue
        if cull_func is not None and cull_func(target):
            continue
        neighbours = edge_func(target)
        ignore.add(id(neighbours))
        n = 0
        skipped = 0
        for source in neighbours:
            if id(source) in ignore:
                continue
            if filter and not filter(source):
                continue
            if n >= too_many:
                skipped += 1
                continue
            if swap_source_target:
                srcnode, tgtnode = target, source
            else:
                srcnode, tgtnode = source, target
            elabel = _edge_label(srcnode, tgtnode, shortnames)
            f.write('  %s -> %s%s;\n' % (_obj_node_id(srcnode),
                                         _obj_node_id(tgtnode), elabel))
            if id(source) not in depth:
                depth[id(source)] = tdepth + 1
                queue.append(source)
            n += 1
            del source
        del neighbours
        if skipped > 0:
            h, s, v = _gradient((0, 1, 1), (0, 1, .3), tdepth + 1, max_depth)
            if swap_source_target:
                label = "%d more references" % skipped
                edge = "%s->too_many_%s" % (_obj_node_id(target),
                                            _obj_node_id(target))
            else:
                label = "%d more backreferences" % skipped
                edge = "too_many_%s->%s" % (_obj_node_id(target),
                                            _obj_node_id(target))
            f.write('  %s[color=red,style=dotted,len=0.25,weight=10];\n'
                    % edge)
            f.write('  too_many_%s[label="%s",shape=box,height=0.25,'
                    'color=red,fillcolor="%g,%g,%g",fontsize=6];\n'
                    % (_obj_node_id(target), label, h, s, v))
            f.write('  too_many_%s[fontcolor=white];\n'
                    % (_obj_node_id(target)))
    f.write("}\n")
    if output:
        return
    # The file should only be closed if this function was in charge of opening
    # the file.
    f.close()
    print("Graph written to %s (%d nodes)" % (dot_filename, nodes))
    _present_graph(dot_filename, filename)

Example 39

Project: rst2pdf Source File: tenjin.py
Function: create_helpers_module
def _create_helpers_module():

    def to_str(val):
        """Convert value into string. Return '' if val is None.
           ex.
             >>> to_str(None)
             ''
             >>> to_str("foo")
             'foo'
             >>> to_str(u"\u65e5\u672c\u8a9e")
             u'\u65e5\u672c\u8a9e'
             >>> to_str(123)
             '123'
        """
        if val is None:
            return ''
        if isinstance(val, str):
            return val
        return str(val, 'utf-8')

    def generate_tostrfunc(encoding):
        """Generate 'to_str' function which encodes unicode to str.
           ex.
              import tenjin
              from tenjin.helpers import escape
              to_str = tenjin.generate_tostrfunc('utf-8')
              engine = tenjin.Engine()
              context = { 'items': [u'AAA', u'BBB', u'CCC'] }
              output = engine.render('example.pyhtml')
              print output
        """
        def to_str(val):
            if val is None:
                return ''
            if isinstance(val, str):
                return val
            return str(val, 'utf-8')
        return to_str

    def echo(string):
        """add string value into _buf. this is equivarent to '#{string}'."""
        frame = sys._getframe(1)
        context = frame.f_locals
        context['_buf'].append(string)

    def start_capture(varname=None):
        """
        start capturing with name.

        ex. list.rbhtml
          <html><body>
          <?py start_capture('itemlist') ?>
            <ul>
              <?py for item in list: ?>
              <li>${item}</li>
              <?py #end ?>
            </ul>
          <?py stop_capture() ?>
          </body></html>

        ex. layout.rbhtml
          <html xml:lang="en" lang="en">
           <head>
            <title>Capture Example</title>
           </head>
           <body>
            <!-- content -->
          #{itemlist}
            <!-- /content -->
           </body>
          </html>
        """
        frame = sys._getframe(1)
        context = frame.f_locals
        context['_buf_tmp'] = context['_buf']
        context['_capture_varname'] = varname
        context['_buf'] = []

    def stop_capture(store_to_context=True):
        """
        stop capturing and return the result of capturing.
        if store_to_context is True then the result is stored into _context[varname].
        """
        frame = sys._getframe(1)
        context = frame.f_locals
        result = ''.join(context['_buf'])
        context['_buf'] = context.pop('_buf_tmp')
        varname = context.pop('_capture_varname')
        if varname:
            context[varname] = result
            if store_to_context:
                context['_context'][varname] = result
        return result

    def captured_as(name):
        """
        helper method for layout template.
        if captured string is found then append it to _buf and return True,
        else return False.
        """
        frame = sys._getframe(1)
        context = frame.f_locals
        if name in context:
            _buf = context['_buf']
            _buf.append(context[name])
            return True
        return False

    def _p(arg):
        """ex. '/show/'+_p("item['id']") => "/show/#{item['id']}" """
        return '<`#%s#`>' % arg    # decoded into #{...} by preprocessor

    def _P(arg):
        """ex. '<b>%s</b>' % _P("item['id']") => "<b>${item['id']}</b>" """
        return '<`$%s$`>' % arg    # decoded into ${...} by preprocessor

    def _decode_params(s):
        """decode <`#...#`> and <`$...$`> into #{...} and ${...}"""
        from urllib.parse import unquote
        dct = { 'lt':'<', 'gt':'>', 'amp':'&', 'quot':'"', '#039':"'", }
        def unescape(s):
            #return s.replace('<', '<').replace('>', '>').replace('&quot;', '"').replace('&#039;', "'").replace('&amp;',  '&')
            return re.sub(r'&(lt|gt|quot|amp|#039);',  lambda m: dct[m.group(1)],  s)
        s = re.sub(r'%3C%60%23(.*?)%23%60%3E', lambda m: '#{%s}' % unquote(m.group(1)), s)
        s = re.sub(r'%3C%60%24(.*?)%24%60%3E', lambda m: '${%s}' % unquote(m.group(1)), s)
        s = re.sub(r'<`#(.*?)#`>',   lambda m: '#{%s}' % unescape(m.group(1)), s)
        s = re.sub(r'<`\$(.*?)\$`>', lambda m: '${%s}' % unescape(m.group(1)), s)
        s = re.sub(r'<`#(.*?)#`>', r'#{\1}', s)
        s = re.sub(r'<`\$(.*?)\$`>', r'${\1}', s)
        return s

    mod = _create_module('tenjin.helpers')
    mod.to_str             = to_str
    mod.generate_tostrfunc = generate_tostrfunc
    mod.echo               = echo
    mod.start_capture      = start_capture
    mod.stop_capture       = stop_capture
    mod.captured_as        = captured_as
    mod._p                 = _p
    mod._P                 = _P
    mod._decode_params     = _decode_params
    mod.__all__ = ['escape', 'to_str', 'echo', 'generate_tostrfunc',
                   'start_capture', 'stop_capture', 'captured_as',
                   '_p', '_P', '_decode_params',
                   ]
    return mod

Example 40

Project: objgraph Source File: objgraph.py
def _show_graph(objs, edge_func, swap_source_target,
                max_depth=3, extra_ignore=(), filter=None, too_many=10,
                highlight=None, filename=None, extra_info=None,
                refcounts=False, shortnames=True, output=None,
                cull_func=None):
    if not _isinstance(objs, (list, tuple)):
        objs = [objs]
    if filename and output:
        raise ValueError('Cannot specify both output and filename.')
    elif output:
        f = output
    elif filename and filename.endswith('.dot'):
        f = codecs.open(filename, 'w', encoding='utf-8')
        dot_filename = filename
    else:
        fd, dot_filename = tempfile.mkstemp(prefix='objgraph-',
                                            suffix='.dot', text=True)
        f = os.fdopen(fd, "w")
        if getattr(f, 'encoding', None):
            # Python 3 will wrap the file in the user's preferred encoding
            # Re-wrap it for utf-8
            import io
            f = io.TextIOWrapper(f.detach(), 'utf-8')
    f.write('digraph ObjectGraph {\n'
            '  node[shape=box, style=filled, fillcolor=white];\n')
    queue = []
    depth = {}
    ignore = set(extra_ignore)
    ignore.add(id(objs))
    ignore.add(id(extra_ignore))
    ignore.add(id(queue))
    ignore.add(id(depth))
    ignore.add(id(ignore))
    ignore.add(id(sys._getframe()))   # this function
    ignore.add(id(sys._getframe().f_locals))
    ignore.add(id(sys._getframe(1)))  # show_refs/show_backrefs
    ignore.add(id(sys._getframe(1).f_locals))
    for obj in objs:
        f.write('  %s[fontcolor=red];\n' % (_obj_node_id(obj)))
        depth[id(obj)] = 0
        queue.append(obj)
        del obj
    gc.collect()
    nodes = 0
    while queue:
        nodes += 1
        # The names "source" and "target" are reversed here because
        # originally there was just show_backrefs() and we were
        # traversing the reference graph backwards.
        target = queue.pop(0)
        tdepth = depth[id(target)]
        f.write('  %s[label="%s"];\n' % (_obj_node_id(target),
                                         _obj_label(target, extra_info,
                                                    refcounts, shortnames)))
        h, s, v = _gradient((0, 0, 1), (0, 0, .3), tdepth, max_depth)
        if inspect.ismodule(target):
            h = .3
            s = 1
        if highlight and highlight(target):
            h = .6
            s = .6
            v = 0.5 + v * 0.5
        f.write('  %s[fillcolor="%g,%g,%g"];\n'
                % (_obj_node_id(target), h, s, v))
        if v < 0.5:
            f.write('  %s[fontcolor=white];\n' % (_obj_node_id(target)))
        if hasattr(getattr(target, '__class__', None), '__del__'):
            f.write('  %s->%s_has_a_del[color=red,style=dotted,'
                    'len=0.25,weight=10];\n' % (_obj_node_id(target),
                                                _obj_node_id(target)))
            f.write('  %s_has_a_del[label="__del__",shape=doublecircle,'
                    'height=0.25,color=red,fillcolor="0,.5,1",fontsize=6];\n'
                    % (_obj_node_id(target)))
        if tdepth >= max_depth:
            continue
        if cull_func is not None and cull_func(target):
            continue
        neighbours = edge_func(target)
        ignore.add(id(neighbours))
        n = 0
        skipped = 0
        for source in neighbours:
            if id(source) in ignore:
                continue
            if filter and not filter(source):
                continue
            if n >= too_many:
                skipped += 1
                continue
            if swap_source_target:
                srcnode, tgtnode = target, source
            else:
                srcnode, tgtnode = source, target
            elabel = _edge_label(srcnode, tgtnode, shortnames)
            f.write('  %s -> %s%s;\n' % (_obj_node_id(srcnode),
                                         _obj_node_id(tgtnode), elabel))
            if id(source) not in depth:
                depth[id(source)] = tdepth + 1
                queue.append(source)
            n += 1
            del source
        del neighbours
        if skipped > 0:
            h, s, v = _gradient((0, 1, 1), (0, 1, .3), tdepth + 1, max_depth)
            if swap_source_target:
                label = "%d more references" % skipped
                edge = "%s->too_many_%s" % (_obj_node_id(target),
                                            _obj_node_id(target))
            else:
                label = "%d more backreferences" % skipped
                edge = "too_many_%s->%s" % (_obj_node_id(target),
                                            _obj_node_id(target))
            f.write('  %s[color=red,style=dotted,len=0.25,weight=10];\n'
                    % edge)
            f.write('  too_many_%s[label="%s",shape=box,height=0.25,'
                    'color=red,fillcolor="%g,%g,%g",fontsize=6];\n'
                    % (_obj_node_id(target), label, h, s, v))
            f.write('  too_many_%s[fontcolor=white];\n'
                    % (_obj_node_id(target)))
    f.write("}\n")
    if output:
        return
    # The file should only be closed if this function was in charge of opening
    # the file.
    f.close()
    print("Graph written to %s (%d nodes)" % (dot_filename, nodes))
    _present_graph(dot_filename, filename)

Example 41

Project: PyIB Source File: tenjin.py
def _create_helpers_module():

    def to_str(val):
        """Convert value into string. Return '' if val is None.
           ex.
             >>> to_str(None)
             ''
             >>> to_str("foo")
             'foo'
             >>> to_str(u"\u65e5\u672c\u8a9e")
             u'\u65e5\u672c\u8a9e'
             >>> to_str(123)
             '123'
        """
        if val is None:              return ''
        if isinstance(val, str):     return val
        if isinstance(val, unicode): return val
        return str(val)

    def generate_tostrfunc(encoding):
        """Generate 'to_str' function which encodes unicode to str.
           ex.
              import tenjin
              from tenjin.helpers import escape
              to_str = tenjin.generate_tostrfunc('utf-8')
              engine = tenjin.Engine()
              context = { 'items': [u'AAA', u'BBB', u'CCC'] }
              output = engine.render('example.pyhtml')
              print output
        """
        def to_str(val):
            if val is None:               return ''
            if isinstance(val, str):      return val
            if isinstance(val, unicode):  return val.encode(encoding)
            return str(val)
        return to_str

    def echo(string):
        """add string value into _buf. this is equivarent to '#{string}'."""
        frame = sys._getframe(1)
        context = frame.f_locals
        context['_buf'].append(string)

    def start_capture(varname=None):
        """
        start capturing with name.

        ex. list.rbhtml
          <html><body>
          <?py start_capture('itemlist') ?>
            <ul>
              <?py for item in list: ?>
              <li>${item}</li>
              <?py #end ?>
            </ul>
          <?py stop_capture() ?>
          </body></html>

        ex. layout.rbhtml
          <html xml:lang="en" lang="en">
           <head>
            <title>Capture Example</title>
           </head>
           <body>
            <!-- content -->
          #{itemlist}
            <!-- /content -->
           </body>
          </html>
        """
        frame = sys._getframe(1)
        context = frame.f_locals
        context['_buf_tmp'] = context['_buf']
        context['_capture_varname'] = varname
        context['_buf'] = []

    def stop_capture(store_to_context=True):
        """
        stop capturing and return the result of capturing.
        if store_to_context is True then the result is stored into _context[varname].
        """
        frame = sys._getframe(1)
        context = frame.f_locals
        result = ''.join(context['_buf'])
        context['_buf'] = context.pop('_buf_tmp')
        varname = context.pop('_capture_varname')
        if varname:
            context[varname] = result
            if store_to_context:
                context['_context'][varname] = result
        return result

    def captured_as(name):
        """
        helper method for layout template.
        if captured string is found then append it to _buf and return True,
        else return False.
        """
        frame = sys._getframe(1)
        context = frame.f_locals
        if context.has_key(name):
            _buf = context['_buf']
            _buf.append(context[name])
            return True
        return False

    def _p(arg):
        """ex. '/show/'+_p("item['id']") => "/show/#{item['id']}" """
        return '<`#%s#`>' % arg    # decoded into #{...} by preprocessor

    def _P(arg):
        """ex. '<b>%s</b>' % _P("item['id']") => "<b>${item['id']}</b>" """
        return '<`$%s$`>' % arg    # decoded into ${...} by preprocessor

    def _decode_params(s):
        """decode <`#...#`> and <`$...$`> into #{...} and ${...}"""
        from urllib import unquote
        dct = { 'lt':'<', 'gt':'>', 'amp':'&', 'quot':'"', '#039':"'", }
        def unescape(s):
            #return s.replace('<', '<').replace('>', '>').replace('&quot;', '"').replace('&#039;', "'").replace('&amp;',  '&')
            return re.sub(r'&(lt|gt|quot|amp|#039);',  lambda m: dct[m.group(1)],  s)
        s = re.sub(r'%3C%60%23(.*?)%23%60%3E', lambda m: '#{%s}' % unquote(m.group(1)), s)
        s = re.sub(r'%3C%60%24(.*?)%24%60%3E', lambda m: '${%s}' % unquote(m.group(1)), s)
        s = re.sub(r'<`#(.*?)#`>',   lambda m: '#{%s}' % unescape(m.group(1)), s)
        s = re.sub(r'<`\$(.*?)\$`>', lambda m: '${%s}' % unescape(m.group(1)), s)
        s = re.sub(r'<`#(.*?)#`>', r'#{\1}', s)
        s = re.sub(r'<`\$(.*?)\$`>', r'${\1}', s)
        return s

    mod = _create_module('tenjin.helpers')
    mod.to_str             = to_str
    mod.generate_tostrfunc = generate_tostrfunc
    mod.echo               = echo
    mod.start_capture      = start_capture
    mod.stop_capture       = stop_capture
    mod.captured_as        = captured_as
    mod._p                 = _p
    mod._P                 = _P
    mod._decode_params     = _decode_params
    mod.__all__ = ['escape', 'to_str', 'echo', 'generate_tostrfunc',
                   'start_capture', 'stop_capture', 'captured_as',
                   '_p', '_P', '_decode_params',
                   ]
    return mod

Example 42

Project: pyqtgraph Source File: debug.py
Function: findrefpath
def findRefPath(startObj, endObj, maxLen=8, restart=True, seen={}, path=None, ignore=None):
    """Determine all paths of object references from startObj to endObj"""
    refs = []
    if path is None:
        path = [endObj]
    if ignore is None:
        ignore = {}
    ignore[id(sys._getframe())] = None
    ignore[id(path)] = None
    ignore[id(seen)] = None
    prefix = " "*(8-maxLen)
    #print prefix + str(map(type, path))
    prefix += " "
    if restart:
        #gc.collect()
        seen.clear()
    gc.collect()
    newRefs = [r for r in gc.get_referrers(endObj) if id(r) not in ignore]
    ignore[id(newRefs)] = None
    #fo = allFrameObjs()
    #newRefs = []
    #for r in gc.get_referrers(endObj):
        #try:
            #if r not in fo:
                #newRefs.append(r)
        #except:
            #newRefs.append(r)            
        
    for r in newRefs:
        #print prefix+"->"+str(type(r))
        if type(r).__name__ in ['frame', 'function', 'listiterator']:
            #print prefix+"  FRAME"
            continue
        try:
            if any([r is x for x in  path]):
                #print prefix+"  LOOP", objChainString([r]+path)
                continue
        except:
            print(r)
            print(path)
            raise
        if r is startObj:
            refs.append([r])
            print(refPathString([startObj]+path))
            continue
        if maxLen == 0:
            #print prefix+"  END:", objChainString([r]+path)
            continue
        ## See if we have already searched this node.
        ## If not, recurse.
        tree = None
        try:
            cache = seen[id(r)]
            if cache[0] >= maxLen:
                tree = cache[1]
                for p in tree:
                    print(refPathString(p+path))
        except KeyError:
            pass
        
        ignore[id(tree)] = None
        if tree is None:
            tree = findRefPath(startObj, r, maxLen-1, restart=False, path=[r]+path, ignore=ignore)
            seen[id(r)] = [maxLen, tree]
        ## integrate any returned results
        if len(tree) == 0:
            #print prefix+"  EMPTY TREE"
            continue
        else:
            for p in tree:
                refs.append(p+[r])
        #seen[id(r)] = [maxLen, refs]
    return refs

Example 43

Project: magicsuper Source File: __init__.py
Function: super
def super(typ=_SENTINEL, type_or_obj=_SENTINEL, framedepth=1):
    '''Like builtin super(), but capable of magic.

    This acts just like the builtin super() function, but if called
    without any arguments it attempts to infer them at runtime.
    '''
    #  Infer the correct call if used without arguments.
    if typ is _SENTINEL:
        # We'll need to do some frame hacking.
        f = sys._getframe(framedepth)    

        try:
            # Get the function's first positional argument.
            type_or_obj = f.f_locals[f.f_code.co_varnames[0]]
        except (IndexError,KeyError,):
            raise RuntimeError('super() used in a function with no args')
        
        try:
            # Get the MRO so we can crawl it.
            mro = type_or_obj.__mro__
        except AttributeError:
            try:
                mro = type_or_obj.__class__.__mro__
            except AttributeError:
                raise RuntimeError('super() used with a non-newstyle class')
        
        #   A ``for...else`` block?  Yes!  It's odd, but useful.
        #   If unfamiliar with for...else, see: 
        #
        #       http://psung.blogspot.com/2007/12/for-else-in-python.html
        for typ in mro:
            #  Find the class that owns the currently-executing method.
            for meth in typ.__dict__.itervalues():
                # Drill down through any wrappers to the underlying func.
                # This handles e.g. classmethod() and staticmethod().
                try:
                    while not isinstance(meth,FunctionType):
                        try:
                            meth = meth.__func__
                        except AttributeError:
                            meth = meth.__get__(type_or_obj)
                except (AttributeError, TypeError):
                    continue
                if meth.func_code is f.f_code:
                    break   # Aha!  Found you.
            else:
                continue    #  Not found! Move onto the next class in MRO.
            break    #  Found! Break out of the search loop.
        else:
            raise RuntimeError('super() called outside a method')
    
    #  Dispatch to builtin super().
    if type_or_obj is not _SENTINEL:
        return _builtin_super(typ,type_or_obj)
    return _builtin_super(typ)

Example 44

Project: pyqtgraph Source File: debug.py
Function: searchrefs
def searchRefs(obj, *args):
    """Pseudo-interactive function for tracing references backward.
    **Arguments:**
    
        obj:   The initial object from which to start searching
        args:  A set of string or int arguments.
               each integer selects one of obj's referrers to be the new 'obj'
               each string indicates an action to take on the current 'obj':
                  t:  print the types of obj's referrers
                  l:  print the lengths of obj's referrers (if they have __len__)
                  i:  print the IDs of obj's referrers
                  o:  print obj
                  ro: return obj
                  rr: return list of obj's referrers
    
    Examples::
    
       searchRefs(obj, 't')                    ## Print types of all objects referring to obj
       searchRefs(obj, 't', 0, 't')            ##   ..then select the first referrer and print the types of its referrers
       searchRefs(obj, 't', 0, 't', 'l')       ##   ..also print lengths of the last set of referrers
       searchRefs(obj, 0, 1, 'ro')             ## Select index 0 from obj's referrer, then select index 1 from the next set of referrers, then return that object
       
    """
    ignore = {id(sys._getframe()): None}
    gc.collect()
    refs = gc.get_referrers(obj)
    ignore[id(refs)] = None
    refs = [r for r in refs if id(r) not in ignore]
    for a in args:
        
        #fo = allFrameObjs()
        #refs = [r for r in refs if r not in fo]
        
        if type(a) is int:
            obj = refs[a]
            gc.collect()
            refs = gc.get_referrers(obj)
            ignore[id(refs)] = None
            refs = [r for r in refs if id(r) not in ignore]
        elif a == 't':
            print(list(map(typeStr, refs)))
        elif a == 'i':
            print(list(map(id, refs)))
        elif a == 'l':
            def slen(o):
                if hasattr(o, '__len__'):
                    return len(o)
                else:
                    return None
            print(list(map(slen, refs)))
        elif a == 'o':
            print(obj)
        elif a == 'ro':
            return obj
        elif a == 'rr':
            return refs

Example 45

Project: PokemonGo-Bot-Desktop Source File: warnings.py
Function: warn
def warn(message, category=None, stacklevel=1):
    """Issue a warning, or maybe ignore it or raise an exception."""
    # Check if message is already a Warning object
    if isinstance(message, Warning):
        category = message.__class__
    # Check category argument
    if category is None:
        category = UserWarning
    assert issubclass(category, Warning)
    # Get context information
    try:
        caller = sys._getframe(stacklevel)
    except ValueError:
        globals = sys.__dict__
        lineno = 1
    else:
        globals = caller.f_globals
        lineno = caller.f_lineno
    if '__name__' in globals:
        module = globals['__name__']
    else:
        module = "<string>"
    filename = globals.get('__file__')
    if filename:
        fnl = filename.lower()
        if fnl.endswith((".pyc", ".pyo")):
            filename = filename[:-1]
    else:
        if module == "__main__":
            try:
                filename = sys.argv[0]
            except AttributeError:
                # embedded interpreters don't have sys.argv, see bug #839151
                filename = '__main__'
        if not filename:
            filename = module
    registry = globals.setdefault("__warningregistry__", {})
    warn_explicit(message, category, filename, lineno, module, registry,
                  globals)

Example 46

Project: pyblosxom Source File: tools.py
Function: get_logger
def get_logger(log_file=None):
    """Creates and returns a log channel.

    If no log_file is given the system-wide logfile as defined in
    config.py is used. If a log_file is given that's where the created
    logger logs to.

    :param log_file: the file to log to.  defaults to None which
                     causes Pyblosxom to check for the ``log_file``
                     config.py property and if that's blank, then the
                     log_file is stderr

    :returns: a log channel (logger instance) which you can call
              ``error``, ``warning``, ``debug``, ``info``, ... on.
    """
    custom_log_file = False
    if log_file is None:
        log_file = _config.get('log_file', 'stderr')
        f = sys._getframe(1)
        filename = f.f_code.co_filename
        module = f.f_globals["__name__"]
        # by default use the root logger
        log_name = ""
        for path in _config.get('plugin_dirs', []):
            if filename.startswith(path):
                # if it's a plugin, use the module name as the log
                # channels name
                log_name = module
                break
        # default to log level WARNING if it's not defined in
        # config.py
        log_level = _config.get('log_level', 'warning')
    else:
        # handle custom log_file
        custom_log_file = True
        # figure out a name for the log channel
        log_name = os.path.splitext(os.path.basename(log_file))[0]
        # assume log_level debug (show everything)
        log_level = "debug"

    global _loghandler_registry

    # get the logger for this channel
    logger = logging.getLogger(log_name)
    # don't propagate messages up the logger hierarchy
    logger.propagate = 0

    # setup the handler if it doesn't already exist.  only add one
    # handler per log channel.
    key = "%s|%s" % (log_file, log_name)
    if not key in _loghandler_registry:

        # create the handler
        if log_file == "stderr":
            hdlr = logging.StreamHandler(sys.stderr)
        else:
            if log_file == "NONE": # user disabled logging
                if os.name == 'nt': # windoze
                    log_file = "NUL"
                else: # assume *nix
                    log_file = "/dev/null"
            try:
                hdlr = logging.FileHandler(log_file)
            except IOError:
                # couldn't open logfile, fallback to stderr
                hdlr = logging.StreamHandler(sys.stderr)

        # create and set the formatter
        if log_name:
            fmtr_s = '%(asctime)s [%(levelname)s] %(name)s: %(message)s'
        else: # root logger
            fmtr_s = '%(asctime)s [%(levelname)s]: %(message)s'

        hdlr.setFormatter(logging.Formatter(fmtr_s))

        logger.addHandler(hdlr)
        int_level = getattr(logging, log_level.upper())
        logger.setLevel(int_level)

        if not custom_log_file:
            # only log messages from plugins listed in log_filter.
            # add 'root' to the log_filter list to still allow
            # application level messages.
            log_filter = _config.get('log_filter', None)
            if log_filter:
                lfilter = LogFilter(log_filter)
                logger.addFilter(lfilter)

        # remember that we've seen this handler
        _loghandler_registry[key] = True

    return logger

Example 47

Project: pytest Source File: source.py
Function: compile
    def compile(self, filename=None, mode='exec',
                flag=generators.compiler_flag,
                dont_inherit=0, _genframe=None):
        """ return compiled code object. if filename is None
            invent an artificial filename which displays
            the source/line position of the caller frame.
        """
        if not filename or py.path.local(filename).check(file=0):
            if _genframe is None:
                _genframe = sys._getframe(1) # the caller
            fn,lineno = _genframe.f_code.co_filename, _genframe.f_lineno
            base = "<%d-codegen " % self._compilecounter
            self.__class__._compilecounter += 1
            if not filename:
                filename = base + '%s:%d>' % (fn, lineno)
            else:
                filename = base + '%r %s:%d>' % (filename, fn, lineno)
        source = "\n".join(self.lines) + '\n'
        try:
            co = cpy_compile(source, filename, mode, flag)
        except SyntaxError:
            ex = sys.exc_info()[1]
            # re-represent syntax errors from parsing python strings
            msglines = self.lines[:ex.lineno]
            if ex.offset:
                msglines.append(" "*ex.offset + '^')
            msglines.append("(code was compiled probably from here: %s)" % filename)
            newex = SyntaxError('\n'.join(msglines))
            newex.offset = ex.offset
            newex.lineno = ex.lineno
            newex.text = ex.text
            raise newex
        else:
            if flag & _AST_FLAG:
                return co
            lines = [(x + "\n") for x in self.lines]
            if sys.version_info[0] >= 3:
                # XXX py3's inspect.getsourcefile() checks for a module
                # and a pep302 __loader__ ... we don't have a module
                # at code compile-time so we need to fake it here
                m = ModuleType("_pycodecompile_pseudo_module")
                py.std.inspect.modulesbyfile[filename] = None
                py.std.sys.modules[None] = m
                m.__loader__ = 1
            py.std.linecache.cache[filename] = (1, None, lines, filename)
            return co

Example 48

Project: antioch Source File: code.py
def is_frame_access_allowed():
    """
    Used by get/setattr to delegate access.
    
    Returns True if the call to __getattribute__ or __setattr__ originated
    in either the exchange, test, or bootstrap modules.
    
    Previously this used inspect, which made it super slow. The only potential
    issue with using _getframe() is that it's not guaranteed to be there in
    non CPython implementations.
    """
    f = sys._getframe(1)
    c1 = f.f_back.f_code
    c2 = f.f_back.f_back.f_code
    try:
        from antioch.core import interface
        model_source_path = os.path.abspath(interface.__file__)
        if(model_source_path.endswith('pyc')):
            model_source_path = model_source_path[:-1]
        if(c2.co_filename == model_source_path):
            #print '%r =(1)= %r' % (c2.co_filename, model_source_path)
            return True
        
        from antioch.core import exchange
        exchange_source_path = os.path.abspath(exchange.__file__)
        if(exchange_source_path.endswith('pyc')):
            exchange_source_path = exchange_source_path[:-1]
        if(c2.co_filename == exchange_source_path):
            #print '%r =(2)= %r' % (c2.co_filename, exchange_source_path)
            return True
        
        from antioch import test
        test_source_path = os.path.abspath(os.path.dirname(test.__file__))
        if(c2.co_filename.startswith(test_source_path)):
            #print '%r 1startswith %r' % (c2.co_filename, test_source_path)
            return True
        
        from antioch.core import bootstrap
        bootstrap_source_path = os.path.abspath(os.path.dirname(bootstrap.__file__))
        if(c2.co_filename.startswith(bootstrap_source_path)):
            #print '%r 2startswith %r' % (c2.co_filename, bootstrap_source_path)
            return True
        
        return False
    finally:
        del c2
        del c1
        del f

Example 49

Project: pydata-cookbook Source File: __init__.py
    def __init__(self, content, name=None, namespace=None, stacklevel=None,
                 get_template=None, default_inherit=None, line_offset=0,
                 delimeters=None):
        self.content = content

        # set delimeters
        if delimeters is None:
            delimeters = (self.default_namespace['start_braces'],
                          self.default_namespace['end_braces'])
        else:
            assert len(delimeters) == 2 and all([isinstance(delimeter, basestring)
                                                 for delimeter in delimeters])
            self.default_namespace = self.__class__.default_namespace.copy()
            self.default_namespace['start_braces'] = delimeters[0]
            self.default_namespace['end_braces'] = delimeters[1]
        self.delimeters = delimeters
        
        self._unicode = is_unicode(content)
        if name is None and stacklevel is not None:
            try:
                caller = sys._getframe(stacklevel)
            except ValueError:
                pass
            else:
                globals = caller.f_globals
                lineno = caller.f_lineno
                if '__file__' in globals:
                    name = globals['__file__']
                    if name.endswith('.pyc') or name.endswith('.pyo'):
                        name = name[:-1]
                elif '__name__' in globals:
                    name = globals['__name__']
                else:
                    name = '<string>'
                if lineno:
                    name += ':%s' % lineno
        self.name = name
        self._parsed = parse(content, name=name, line_offset=line_offset, delimeters=self.delimeters)
        if namespace is None:
            namespace = {}
        self.namespace = namespace
        self.get_template = get_template
        if default_inherit is not None:
            self.default_inherit = default_inherit

Example 50

Project: python-future Source File: newsuper.py
Function: newsuper
def newsuper(typ=_SENTINEL, type_or_obj=_SENTINEL, framedepth=1):
    '''Like builtin super(), but capable of magic.

    This acts just like the builtin super() function, but if called
    without any arguments it attempts to infer them at runtime.
    '''
    #  Infer the correct call if used without arguments.
    if typ is _SENTINEL:
        # We'll need to do some frame hacking.
        f = sys._getframe(framedepth)    

        try:
            # Get the function's first positional argument.
            type_or_obj = f.f_locals[f.f_code.co_varnames[0]]
        except (IndexError, KeyError,):
            raise RuntimeError('super() used in a function with no args')
        
        try:
            # Get the MRO so we can crawl it.
            mro = type_or_obj.__mro__
        except (AttributeError, RuntimeError):  # see issue #160
            try:
                mro = type_or_obj.__class__.__mro__
            except AttributeError:
                raise RuntimeError('super() used with a non-newstyle class')
        
        #   A ``for...else`` block?  Yes!  It's odd, but useful.
        #   If unfamiliar with for...else, see: 
        #
        #       http://psung.blogspot.com/2007/12/for-else-in-python.html
        for typ in mro:
            #  Find the class that owns the currently-executing method.
            for meth in typ.__dict__.values():
                # Drill down through any wrappers to the underlying func.
                # This handles e.g. classmethod() and staticmethod().
                try:
                    while not isinstance(meth,FunctionType):
                        if isinstance(meth, property):
                            # Calling __get__ on the property will invoke
                            # user code which might throw exceptions or have
                            # side effects
                            meth = meth.fget
                        else:
                            try:
                                meth = meth.__func__
                            except AttributeError:
                                meth = meth.__get__(type_or_obj)
                except (AttributeError, TypeError):
                    continue
                if meth.func_code is f.f_code:
                    break   # Aha!  Found you.
            else:
                continue    #  Not found! Move onto the next class in MRO.
            break    #  Found! Break out of the search loop.
        else:
            raise RuntimeError('super() called outside a method')
    
    #  Dispatch to builtin super().
    if type_or_obj is not _SENTINEL:
        return _builtin_super(typ, type_or_obj)
    return _builtin_super(typ)
See More Examples - Go to Next Page
Page 1 Selected Page 2