sys._getframe.f_globals

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

55 Examples 7

Example 51

Project: multiversion
License: View license
Source File: multiversion.py
def version_import(name, globals=None, locals=None, fromlist=None, level=-1):
    """An import hook that performs the versioned import.  It can't work
    on the level of the regular import hooks as it's actually renaming
    imports.
    """
    if globals is None:
        globals = sys._getframe(1).f_globals
    if locals is None:
        locals = {}
    if fromlist is None:
        fromlist = []
    key = get_cache_key(name, globals)
    actual_name = name

    if key is not None:
        actual_name = rewrite_import_name(actual_name, key)
        if version_not_loaded(key):
            load_version(key)

        if not fromlist:
            fromlist = ['__name__']
    rv = actual_import(actual_name, globals, locals, fromlist, level)
    proxy = sys.modules.get(name)
    if proxy is None:
        rv.__multiversion_proxy__ = proxy = ModuleProxy(name)
        def cleanup_proxy(ref):
            try:
                sys.modules.pop(name, None)
            except (TypeError, AttributeError):
                pass
        sys.modules[name] = weakref.proxy(proxy, cleanup_proxy)
    return rv

Example 52

Project: iot-utilities
License: View license
Source File: enum.py
Function: create
    def _create_(cls, class_name, names=None, *, module=None, qualname=None, type=None, start=1):
        """Convenience method to create a new Enum class.

        `names` can be:

        * A string containing member names, separated either with spaces or
          commas.  Values are incremented by 1 from `start`.
        * An iterable of member names.  Values are incremented by 1 from `start`.
        * An iterable of (member name, value) pairs.
        * A mapping of member name -> value pairs.

        """
        metacls = cls.__class__
        bases = (cls, ) if type is None else (type, cls)
        classdict = metacls.__prepare__(class_name, bases)

        # special processing needed for names?
        if isinstance(names, str):
            names = names.replace(',', ' ').split()
        if isinstance(names, (tuple, list)) and isinstance(names[0], str):
            names = [(e, i) for (i, e) in enumerate(names, start)]

        # Here, names is either an iterable of (name, value) or a mapping.
        for item in names:
            if isinstance(item, str):
                member_name, member_value = item, names[item]
            else:
                member_name, member_value = item
            classdict[member_name] = member_value
        enum_class = metacls.__new__(metacls, class_name, bases, classdict)

        # TODO: replace the frame hack if a blessed way to know the calling
        # module is ever developed
        if module is None:
            try:
                module = sys._getframe(2).f_globals['__name__']
            except (AttributeError, ValueError) as exc:
                pass
        if module is None:
            _make_class_unpicklable(enum_class)
        else:
            enum_class.__module__ = module
        if qualname is not None:
            enum_class.__qualname__ = qualname

        return enum_class

Example 53

Project: rez
License: View license
Source File: enum.py
Function: create
    def _create_(cls, class_name, names=None, module=None, type=None):
        """Convenience method to create a new Enum class.

        `names` can be:

        * A string containing member names, separated either with spaces or
          commas.  Values are auto-numbered from 1.
        * An iterable of member names.  Values are auto-numbered from 1.
        * An iterable of (member name, value) pairs.
        * A mapping of member name -> value.

        """
        metacls = cls.__class__
        if type is None:
            bases = (cls, )
        else:
            bases = (type, cls)
        classdict = metacls.__prepare__(class_name, bases)
        __order__ = []

        # special processing needed for names?
        if isinstance(names, basestring):
            names = names.replace(',', ' ').split()
        if isinstance(names, (tuple, list)) and isinstance(names[0], basestring):
            names = [(e, i+1) for (i, e) in enumerate(names)]

        # Here, names is either an iterable of (name, value) or a mapping.
        for item in names:
            if isinstance(item, basestring):
                member_name, member_value = item, names[item]
            else:
                member_name, member_value = item
            classdict[member_name] = member_value
            __order__.append(member_name)
        # only set __order__ in classdict if name/value was not from a mapping
        if not isinstance(item, basestring):
            classdict['__order__'] = ' '.join(__order__)
        enum_class = metacls.__new__(metacls, class_name, bases, classdict)

        # TODO: replace the frame hack if a blessed way to know the calling
        # module is ever developed
        if module is None:
            try:
                module = _sys._getframe(2).f_globals['__name__']
            except (AttributeError, ValueError):
                pass
        if module is None:
            _make_class_unpicklable(enum_class)
        else:
            enum_class.__module__ = module

        return enum_class

Example 54

Project: should-dsl
License: View license
Source File: dsl.py
    def _destroy_function_matchers(self):
        self._outer_frame = sys._getframe(2).f_globals
        self._remove_matchers_from_namespace()
        self._put_original_identifiers_back()

Example 55

Project: should-dsl
License: View license
Source File: dsl.py
    def _create_function_matchers(self):
        self._outer_frame = sys._getframe(2).f_globals
        self._save_clashed_identifiers()
        self._put_matchers_on_namespace()
See More Examples - Go to Next Page
Page 1 Page 2 Selected