def waitget(self,key, maxwaittime = <NUM_LIT> ):

wtimes = [<NUM_LIT>] * <NUM_LIT:3> + [<NUM_LIT:0.5>] * <NUM_LIT:2> + [<NUM_LIT:1>]<EOL>tries = <NUM_LIT:0><EOL>waited = <NUM_LIT:0><EOL>while <NUM_LIT:1>:<EOL><INDENT>try:<EOL><INDENT>val = self[key]<EOL>return val<EOL><DEDENT>except KeyError:<EOL><INDENT>pass<EOL><DEDENT>if waited > maxwaittime:<EOL><INDENT>raise KeyError(key)<EOL><DEDENT>time.sleep(wtimes[tries])<EOL>waited+=wtimes[tries]<EOL>if tries < len(wtimes) -<NUM_LIT:1>:<EOL><INDENT>tries+=<NUM_LIT:1><EOL><DEDENT><DEDENT>

Wait (poll) for a key to get a value

        Will wait for `maxwaittime` seconds before raising a KeyError.
        The call exits normally if the `key` field in db gets a value
        within the timeout period.

        Use this for synchronizing different processes or for ensuring
        that an unfortunately timed "db['key'] = newvalue" operation
        in another process (which causes all 'get' operation to cause a
        KeyError for the duration of pickling) won't screw up your program
        logic.


return PickleShareLink(self, folder)<EOL>

Get a convenient link for accessing items


if isinstance( object, str ):<EOL><INDENT>return add_article( object )<EOL><DEDENT>return add_article( object.__class__.__name__ )<EOL>

Returns a string containing the class name of an object with the
    correct indefinite article ('a' or 'an') preceding it (e.g., 'an Image',
    'a PlotValue').


if name[:<NUM_LIT:1>].lower() in '<STR_LIT>':<EOL><INDENT>return '<STR_LIT>' + name<EOL><DEDENT>return '<STR_LIT>' + name<EOL>

Returns a string containing the correct indefinite article ('a' or 'an')
    prefixed to the specified string.


the_type = type(obj)<EOL>if (not py3compat.PY3) and the_type is InstanceType:<EOL><INDENT>the_type = obj.__class__<EOL><DEDENT>msg = '<STR_LIT>' % (obj, the_type)<EOL>return msg<EOL>

Return a string representation of a value and its type for readable
    error messages.


if isinstance(name, str):<EOL><INDENT>return [name]<EOL><DEDENT>elif name is None:<EOL><INDENT>return ['<STR_LIT>']<EOL><DEDENT>elif isinstance(name, (list, tuple)):<EOL><INDENT>for n in name:<EOL><INDENT>assert isinstance(n, str), "<STR_LIT>"<EOL><DEDENT>return name<EOL><DEDENT>

Convert the name argument to a list of names.

    Examples
    --------

    >>> parse_notifier_name('a')
    ['a']
    >>> parse_notifier_name(['a','b'])
    ['a', 'b']
    >>> parse_notifier_name(None)
    ['anytrait']


results = []<EOL>for key in dir(object):<EOL><INDENT>try:<EOL><INDENT>value = getattr(object, key)<EOL><DEDENT>except AttributeError:<EOL><INDENT>pass<EOL><DEDENT>else:<EOL><INDENT>if not predicate or predicate(value):<EOL><INDENT>results.append((key, value))<EOL><DEDENT><DEDENT><DEDENT>results.sort()<EOL>return results<EOL>

A safe version of inspect.getmembers that handles missing attributes.

    This is useful when there are descriptor based attributes that for
    some reason raise AttributeError even though they exist.  This happens
    in zope.inteface with the __provides__ attribute.


def __init__(self, default_value=NoDefaultSpecified, **metadata):

if default_value is not NoDefaultSpecified:<EOL><INDENT>self.default_value = default_value<EOL><DEDENT>if len(metadata) > <NUM_LIT:0>:<EOL><INDENT>if len(self.metadata) > <NUM_LIT:0>:<EOL><INDENT>self._metadata = self.metadata.copy()<EOL>self._metadata.update(metadata)<EOL><DEDENT>else:<EOL><INDENT>self._metadata = metadata<EOL><DEDENT><DEDENT>else:<EOL><INDENT>self._metadata = self.metadata<EOL><DEDENT>self.init()<EOL>


return self.default_value<EOL>

Create a new instance of the default value.


This is called by :meth:`HasTraits.__new__` to finish init'ing.

        Some stages of initialization must be delayed until the parent
        :class:`HasTraits` instance has been created.  This method is
        called in :meth:`HasTraits.__new__` after the instance has been
        created.

        This method trigger the creation and validation of default values
        and also things like the resolution of str given class names in
        :class:`Type` and :class`Instance`.

        Parameters
        ----------
        obj : :class:`HasTraits` instance
            The parent :class:`HasTraits` instance that has just been
            created.


<EOL>mro = type(obj).mro()<EOL>meth_name = '<STR_LIT>' % self.name<EOL>for cls in mro[:mro.index(self.this_class)+<NUM_LIT:1>]:<EOL><INDENT>if meth_name in cls.__dict__:<EOL><INDENT>break<EOL><DEDENT><DEDENT>else:<EOL><INDENT>dv = self.get_default_value()<EOL>newdv = self._validate(obj, dv)<EOL>obj._trait_values[self.name] = newdv<EOL>return<EOL><DEDENT>obj._trait_dyn_inits[self.name] = cls.__dict__[meth_name]<EOL>

Set the default value on a per instance basis.

        This method is called by :meth:`instance_init` to create and
        validate the default value.  The creation and validation of
        default values must be delayed until the parent :class:`HasTraits`
        class has been instantiated.


if obj is None:<EOL><INDENT>return self<EOL><DEDENT>else:<EOL><INDENT>try:<EOL><INDENT>value = obj._trait_values[self.name]<EOL><DEDENT>except KeyError:<EOL><INDENT>if self.name in obj._trait_dyn_inits:<EOL><INDENT>value = obj._trait_dyn_inits[self.name](obj)<EOL>value = self._validate(obj, value)<EOL>obj._trait_values[self.name] = value<EOL>return value<EOL><DEDENT>else:<EOL><INDENT>raise TraitError('<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT><DEDENT>except Exception:<EOL><INDENT>raise TraitError('<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>return value<EOL><DEDENT><DEDENT>

Get the value of the trait by self.name for the instance.

        Default values are instantiated when :meth:`HasTraits.__new__`
        is called.  Thus by the time this method gets called either the
        default value or a user defined value (they called :meth:`__set__`)
        is in the :class:`HasTraits` instance.


def __new__(mcls, name, bases, classdict):

<EOL>for k,v in classdict.items():<EOL><INDENT>if isinstance(v, TraitType):<EOL><INDENT>v.name = k<EOL><DEDENT>elif inspect.isclass(v):<EOL><INDENT>if issubclass(v, TraitType):<EOL><INDENT>vinst = v()<EOL>vinst.name = k<EOL>classdict[k] = vinst<EOL><DEDENT><DEDENT><DEDENT>return super(MetaHasTraits, mcls).__new__(mcls, name, bases, classdict)<EOL>

Create the HasTraits class.

        This instantiates all TraitTypes in the class dict and sets their
        :attr:`name` attribute.


def __init__(cls, name, bases, classdict):

for k, v in classdict.items():<EOL><INDENT>if isinstance(v, TraitType):<EOL><INDENT>v.this_class = cls<EOL><DEDENT><DEDENT>super(MetaHasTraits, cls).__init__(name, bases, classdict)<EOL>

Finish initializing the HasTraits class.

        This sets the :attr:`this_class` attribute of each TraitType in the
        class dict to the newly created class ``cls``.


def on_trait_change(self, handler, name=None, remove=False):

if remove:<EOL><INDENT>names = parse_notifier_name(name)<EOL>for n in names:<EOL><INDENT>self._remove_notifiers(handler, n)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>names = parse_notifier_name(name)<EOL>for n in names:<EOL><INDENT>self._add_notifiers(handler, n)<EOL><DEDENT><DEDENT>

Setup a handler to be called when a trait changes.

        This is used to setup dynamic notifications of trait changes.

        Static handlers can be created by creating methods on a HasTraits
        subclass with the naming convention '_[traitname]_changed'.  Thus,
        to create static handler for the trait 'a', create the method
        _a_changed(self, name, old, new) (fewer arguments can be used, see
        below).

        Parameters
        ----------
        handler : callable
            A callable that is called when a trait changes.  Its
            signature can be handler(), handler(name), handler(name, new)
            or handler(name, old, new).
        name : list, str, None
            If None, the handler will apply to all traits.  If a list
            of str, handler will apply to all names in the list.  If a
            str, the handler will apply just to that name.
        remove : bool
            If False (the default), then install the handler.  If True
            then unintall it.


return list(cls.class_traits(**metadata).keys())<EOL>

Get a list of all the names of this classes traits.

        This method is just like the :meth:`trait_names` method, but is unbound.


traits = dict([memb for memb in getmembers(cls) ifisinstance(memb[<NUM_LIT:1>], TraitType)])<EOL>if len(metadata) == <NUM_LIT:0>:<EOL><INDENT>return traits<EOL><DEDENT>for meta_name, meta_eval in list(metadata.items()):<EOL><INDENT>if type(meta_eval) is not FunctionType:<EOL><INDENT>metadata[meta_name] = _SimpleTest(meta_eval)<EOL><DEDENT><DEDENT>result = {}<EOL>for name, trait in list(traits.items()):<EOL><INDENT>for meta_name, meta_eval in list(metadata.items()):<EOL><INDENT>if not meta_eval(trait.get_metadata(meta_name)):<EOL><INDENT>break<EOL><DEDENT><DEDENT>else:<EOL><INDENT>result[name] = trait<EOL><DEDENT><DEDENT>return result<EOL>

Get a list of all the traits of this class.

        This method is just like the :meth:`traits` method, but is unbound.

        The TraitTypes returned don't know anything about the values
        that the various HasTrait's instances are holding.

        This follows the same algorithm as traits does and does not allow
        for any simple way of specifying merely that a metadata name
        exists, but has any value.  This is because get_metadata returns
        None if a metadata key doesn't exist.


return list(self.traits(**metadata).keys())<EOL>

Get a list of all the names of this classes traits.


traits = dict([memb for memb in getmembers(self.__class__) ifisinstance(memb[<NUM_LIT:1>], TraitType)])<EOL>if len(metadata) == <NUM_LIT:0>:<EOL><INDENT>return traits<EOL><DEDENT>for meta_name, meta_eval in list(metadata.items()):<EOL><INDENT>if type(meta_eval) is not FunctionType:<EOL><INDENT>metadata[meta_name] = _SimpleTest(meta_eval)<EOL><DEDENT><DEDENT>result = {}<EOL>for name, trait in list(traits.items()):<EOL><INDENT>for meta_name, meta_eval in list(metadata.items()):<EOL><INDENT>if not meta_eval(trait.get_metadata(meta_name)):<EOL><INDENT>break<EOL><DEDENT><DEDENT>else:<EOL><INDENT>result[name] = trait<EOL><DEDENT><DEDENT>return result<EOL>

Get a list of all the traits of this class.

        The TraitTypes returned don't know anything about the values
        that the various HasTrait's instances are holding.

        This follows the same algorithm as traits does and does not allow
        for any simple way of specifying merely that a metadata name
        exists, but has any value.  This is because get_metadata returns
        None if a metadata key doesn't exist.


def trait_metadata(self, traitname, key):

try:<EOL><INDENT>trait = getattr(self.__class__, traitname)<EOL><DEDENT>except AttributeError:<EOL><INDENT>raise TraitError("<STR_LIT>" %<EOL>(self.__class__.__name__, traitname))<EOL><DEDENT>else:<EOL><INDENT>return trait.get_metadata(key)<EOL><DEDENT>


def __init__ (self, default_value=None, klass=None, allow_none=True, **metadata ):

if default_value is None:<EOL><INDENT>if klass is None:<EOL><INDENT>klass = object<EOL><DEDENT><DEDENT>elif klass is None:<EOL><INDENT>klass = default_value<EOL><DEDENT>if not (inspect.isclass(klass) or isinstance(klass, str)):<EOL><INDENT>raise TraitError("<STR_LIT>")<EOL><DEDENT>self.klass       = klass<EOL>self._allow_none = allow_none<EOL>super(Type, self).__init__(default_value, **metadata)<EOL>

Construct a Type trait

        A Type trait specifies that its values must be subclasses of
        a particular class.

        If only ``default_value`` is given, it is used for the ``klass`` as
        well.

        Parameters
        ----------
        default_value : class, str or None
            The default value must be a subclass of klass.  If an str,
            the str must be a fully specified class name, like 'foo.bar.Bah'.
            The string is resolved into real class, when the parent
            :class:`HasTraits` class is instantiated.
        klass : class, str, None
            Values of this trait must be a subclass of klass.  The klass
            may be specified in a string like: 'foo.bar.MyClass'.
            The string is resolved into real class, when the parent
            :class:`HasTraits` class is instantiated.
        allow_none : boolean
            Indicates whether None is allowed as an assignable value. Even if
            ``False``, the default value may be ``None``.


try:<EOL><INDENT>if issubclass(value, self.klass):<EOL><INDENT>return value<EOL><DEDENT><DEDENT>except:<EOL><INDENT>if (value is None) and (self._allow_none):<EOL><INDENT>return value<EOL><DEDENT><DEDENT>self.error(obj, value)<EOL>

Validates that the value is a valid object instance.


if isinstance(self.klass, str):<EOL><INDENT>klass = self.klass<EOL><DEDENT>else:<EOL><INDENT>klass = self.klass.__name__<EOL><DEDENT>result = '<STR_LIT>' + klass<EOL>if self._allow_none:<EOL><INDENT>return result + '<STR_LIT>'<EOL><DEDENT>return result<EOL>


def __init__(self, klass=None, args=None, kw=None,<EOL>allow_none=True, **metadata ):

self._allow_none = allow_none<EOL>if (klass is None) or (not (inspect.isclass(klass) or isinstance(klass, str))):<EOL><INDENT>raise TraitError('<STR_LIT>'<EOL>'<STR_LIT>' % klass)<EOL><DEDENT>self.klass = klass<EOL>if args is None and kw is None:<EOL><INDENT>default_value = None<EOL><DEDENT>else:<EOL><INDENT>if args is None:<EOL><INDENT>args = ()<EOL><DEDENT>elif kw is None:<EOL><INDENT>kw = {}<EOL><DEDENT>if not isinstance(kw, dict):<EOL><INDENT>raise TraitError("<STR_LIT>")<EOL><DEDENT>if not isinstance(args, tuple):<EOL><INDENT>raise TraitError("<STR_LIT>")<EOL><DEDENT>default_value = DefaultValueGenerator(*args, **kw)<EOL><DEDENT>super(Instance, self).__init__(default_value, **metadata)<EOL>

Construct an Instance trait.

        This trait allows values that are instances of a particular
        class or its sublclasses.  Our implementation is quite different
        from that of enthough.traits as we don't allow instances to be used
        for klass and we handle the ``args`` and ``kw`` arguments differently.

        Parameters
        ----------
        klass : class, str
            The class that forms the basis for the trait.  Class names
            can also be specified as strings, like 'foo.bar.Bar'.
        args : tuple
            Positional arguments for generating the default value.
        kw : dict
            Keyword arguments for generating the default value.
        allow_none : bool
            Indicates whether None is allowed as a value.

        Default Value
        -------------
        If both ``args`` and ``kw`` are None, then the default value is None.
        If ``args`` is a tuple and ``kw`` is a dict, then the default is
        created as ``klass(*args, **kw)``.  If either ``args`` or ``kw`` is
        not (but not both), None is replace by ``()`` or ``{}``.


dv  = self.default_value<EOL>if isinstance(dv, DefaultValueGenerator):<EOL><INDENT>return dv.generate(self.klass)<EOL><DEDENT>else:<EOL><INDENT>return dv<EOL><DEDENT>

Instantiate a default value instance.

        This is called when the containing HasTraits classes'
        :meth:`__new__` method is called to ensure that a unique instance
        is created for each HasTraits instance.


result = '<STR_LIT>' + repr(self.values)<EOL>if self._allow_none:<EOL><INDENT>return result + '<STR_LIT>'<EOL><DEDENT>return result<EOL>


def __init__(self, trait=None, default_value=None, allow_none=True,<EOL>**metadata):

istrait = lambda t: isinstance(t, type) and issubclass(t, TraitType)<EOL>if default_value is None and not istrait(trait):<EOL><INDENT>default_value = trait<EOL>trait = None<EOL><DEDENT>if default_value is None:<EOL><INDENT>args = ()<EOL><DEDENT>elif isinstance(default_value, self._valid_defaults):<EOL><INDENT>args = (default_value,)<EOL><DEDENT>else:<EOL><INDENT>raise TypeError('<STR_LIT>' %(self.__class__.__name__, default_value))<EOL><DEDENT>if istrait(trait):<EOL><INDENT>self._trait = trait()<EOL>self._trait.name = '<STR_LIT>'<EOL><DEDENT>elif trait is not None:<EOL><INDENT>raise TypeError("<STR_LIT>"%repr_type(trait))<EOL><DEDENT>super(Container,self).__init__(klass=self.klass, args=args,<EOL>allow_none=allow_none, **metadata)<EOL>

Create a container trait type from a list, set, or tuple.

        The default value is created by doing ``List(default_value)``,
        which creates a copy of the ``default_value``.

        ``trait`` can be specified, which restricts the type of elements
        in the container to that TraitType.

        If only one arg is given and it is not a Trait, it is taken as
        ``default_value``:

        ``c = List([1,2,3])``

        Parameters
        ----------

        trait : TraitType [ optional ]
            the type for restricting the contents of the Container.  If unspecified,
            types are not checked.

        default_value : SequenceType [ optional ]
            The default value for the Trait.  Must be list/tuple/set, and
            will be cast to the container type.

        allow_none : Bool [ default True ]
            Whether to allow the value to be None

        **metadata : any
            further keys for extensions to the Trait (e.g. config)


def __init__(self, trait=None, default_value=None, minlen=<NUM_LIT:0>, maxlen=sys.maxsize,<EOL>allow_none=True, **metadata):

self._minlen = minlen<EOL>self._maxlen = maxlen<EOL>super(List, self).__init__(trait=trait, default_value=default_value,<EOL>allow_none=allow_none, **metadata)<EOL>

Create a List trait type from a list, set, or tuple.

        The default value is created by doing ``List(default_value)``,
        which creates a copy of the ``default_value``.

        ``trait`` can be specified, which restricts the type of elements
        in the container to that TraitType.

        If only one arg is given and it is not a Trait, it is taken as
        ``default_value``:

        ``c = List([1,2,3])``

        Parameters
        ----------

        trait : TraitType [ optional ]
            the type for restricting the contents of the Container.  If unspecified,
            types are not checked.

        default_value : SequenceType [ optional ]
            The default value for the Trait.  Must be list/tuple/set, and
            will be cast to the container type.

        minlen : Int [ default 0 ]
            The minimum length of the input list

        maxlen : Int [ default sys.maxint ]
            The maximum length of the input list

        allow_none : Bool [ default True ]
            Whether to allow the value to be None

        **metadata : any
            further keys for extensions to the Trait (e.g. config)


def __init__(self, *traits, **metadata):

default_value = metadata.pop('<STR_LIT>', None)<EOL>allow_none = metadata.pop('<STR_LIT>', True)<EOL>istrait = lambda t: isinstance(t, type) and issubclass(t, TraitType)<EOL>if len(traits) == <NUM_LIT:1> and default_value is None and not istrait(traits[<NUM_LIT:0>]):<EOL><INDENT>default_value = traits[<NUM_LIT:0>]<EOL>traits = ()<EOL><DEDENT>if default_value is None:<EOL><INDENT>args = ()<EOL><DEDENT>elif isinstance(default_value, self._valid_defaults):<EOL><INDENT>args = (default_value,)<EOL><DEDENT>else:<EOL><INDENT>raise TypeError('<STR_LIT>' %(self.__class__.__name__, default_value))<EOL><DEDENT>self._traits = []<EOL>for trait in traits:<EOL><INDENT>t = trait()<EOL>t.name = '<STR_LIT>'<EOL>self._traits.append(t)<EOL><DEDENT>if self._traits and default_value is None:<EOL><INDENT>args = None<EOL><DEDENT>super(Container,self).__init__(klass=self.klass, args=args,<EOL>allow_none=allow_none, **metadata)<EOL>

Tuple(*traits, default_value=None, allow_none=True, **medatata)

        Create a tuple from a list, set, or tuple.

        Create a fixed-type tuple with Traits:

        ``t = Tuple(Int, Str, CStr)``

        would be length 3, with Int,Str,CStr for each element.

        If only one arg is given and it is not a Trait, it is taken as
        default_value:

        ``t = Tuple((1,2,3))``

        Otherwise, ``default_value`` *must* be specified by keyword.

        Parameters
        ----------

        *traits : TraitTypes [ optional ]
            the tsype for restricting the contents of the Tuple.  If unspecified,
            types are not checked. If specified, then each positional argument
            corresponds to an element of the tuple.  Tuples defined with traits
            are of fixed length.

        default_value : SequenceType [ optional ]
            The default value for the Tuple.  Must be list/tuple/set, and
            will be cast to a tuple. If `traits` are specified, the
            `default_value` must conform to the shape and type they specify.

        allow_none : Bool [ default True ]
            Whether to allow the value to be None

        **metadata : any
            further keys for extensions to the Trait (e.g. config)


def __init__(self, default_value=None, allow_none=True, **metadata):

if default_value is None:<EOL><INDENT>args = ((),)<EOL><DEDENT>elif isinstance(default_value, dict):<EOL><INDENT>args = (default_value,)<EOL><DEDENT>elif isinstance(default_value, SequenceTypes):<EOL><INDENT>args = (default_value,)<EOL><DEDENT>else:<EOL><INDENT>raise TypeError('<STR_LIT>' % default_value)<EOL><DEDENT>super(Dict,self).__init__(klass=dict, args=args,<EOL>allow_none=allow_none, **metadata)<EOL>

Create a dict trait type from a dict.

        The default value is created by doing ``dict(default_value)``,
        which creates a copy of the ``default_value``.


for name,value in color_templates:<EOL><INDENT>setattr(in_class,name,in_class._base % value)<EOL><DEDENT>

Build a set of color attributes in a class.

    Helper function for building the *TermColors classes.


if name is None:<EOL><INDENT>name = self.name<EOL><DEDENT>return ColorScheme(name, self.colors.dict())<EOL>

Return a full copy of the object, optionally renaming it.


<EOL>self.active_scheme_name = '<STR_LIT>'<EOL>self.active_colors = None<EOL>if scheme_list:<EOL><INDENT>if default_scheme == '<STR_LIT>':<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>for scheme in scheme_list:<EOL><INDENT>self.add_scheme(scheme)<EOL><DEDENT>self.set_active_scheme(default_scheme)<EOL><DEDENT>

Create a table of color schemes.

        The table can be created empty and manually filled or it can be
        created with a list of valid color schemes AND the specification for
        the default active scheme.


return ColorSchemeTable(list(self.values()),self.active_scheme_name)<EOL>


if not isinstance(new_scheme,ColorScheme):<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>self[new_scheme.name] = new_scheme<EOL>


scheme_names = list(self.keys())<EOL>if case_sensitive:<EOL><INDENT>valid_schemes = scheme_names<EOL>scheme_test = scheme<EOL><DEDENT>else:<EOL><INDENT>valid_schemes = [s.lower() for s in scheme_names]<EOL>scheme_test = scheme.lower()<EOL><DEDENT>try:<EOL><INDENT>scheme_idx = valid_schemes.index(scheme_test)<EOL><DEDENT>except ValueError:<EOL><INDENT>raise ValueError('<STR_LIT>' + scheme +'<STR_LIT>'+str(scheme_names).replace("<STR_LIT>",'<STR_LIT>'))<EOL><DEDENT>else:<EOL><INDENT>active = scheme_names[scheme_idx]<EOL>self.active_scheme_name = active<EOL>self.active_colors = self[active].colors<EOL>self['<STR_LIT>'] = self[active]<EOL><DEDENT>

Set the currently active scheme.

        Names are by default compared in a case-insensitive way, but this can
        be changed by setting the parameter case_sensitive to true.


for op1,op2 in ex_op:<EOL><INDENT>if op1 in dict and op2 in dict:<EOL><INDENT>raise ValueError('<STR_LIT>''<STR_LIT>'+op1+'<STR_LIT>'+op2+'<STR_LIT>')<EOL><DEDENT><DEDENT>

Check for presence of mutually exclusive keys in a dict.

    Call: mutex_opts(dict,[[op1a,op1b],[op2a,op2b]...]


return <NUM_LIT:1>-(<NUM_LIT:0> in [x in checklist for x in candidates])<EOL>

Check whether a list of items ALL appear in a given list of options.

    Returns a single 1 or 0 value.


return [x in checklist for x in candidates]<EOL>

Check whether a list of items appear in a given list of options.

    Returns a list of 1 and 0, one for each candidate given.


Set multiple attributes for an object, similar to Pascal's with.

    Example:
    with_obj(jim,
             born = 1960,
             haircolour = 'Brown',
             eyecolour = 'Green')

    Credit: Greg Ewing, in
    http://mail.python.org/pipermail/python-list/2001-May/040703.html.

    NOTE: up until IPython 0.7.2, this was called simply 'with', but 'with'
    has become a keyword for Python 2.5, so we had to rename it.


out_list = []<EOL>idx = <NUM_LIT:0><EOL>for object in object_list:<EOL><INDENT>try:<EOL><INDENT>handler = getattr(object, method)<EOL><DEDENT>except AttributeError:<EOL><INDENT>out_list.append(None)<EOL><DEDENT>else:<EOL><INDENT>if argseq:<EOL><INDENT>args = [lst[idx] for lst in argseq]<EOL>out_list.append(handler(args,**kw))<EOL><DEDENT>else:<EOL><INDENT>out_list.append(handler(**kw))<EOL><DEDENT><DEDENT>idx += <NUM_LIT:1><EOL><DEDENT>return out_list<EOL>

map_method(method,object_list,*args,**kw) -> list

    Return a list of the results of applying the methods to the items of the
    argument sequence(s).  If more than one sequence is given, the method is
    called with an argument list consisting of the corresponding item of each
    sequence. All sequences must be of the same length.

    Keyword arguments are passed verbatim to all objects called.

    This is Python code, so it's not nearly as fast as the builtin map().


if fns == None:<EOL><INDENT>warn("<STR_LIT>" % (mod_name,))<EOL><DEDENT>else:<EOL><INDENT>warn("<STR_LIT>" % (fns,mod_name))<EOL><DEDENT>


try:<EOL><INDENT>val = dct[key]<EOL><DEDENT>except KeyError:<EOL><INDENT>if default is NotGiven:<EOL><INDENT>raise<EOL><DEDENT>else:<EOL><INDENT>return default<EOL><DEDENT><DEDENT>else:<EOL><INDENT>del dct[key]<EOL>return val<EOL><DEDENT>

Return dct[key] and delete dct[key].

    If default is given, return it if dct[key] doesn't exist, otherwise raise
    KeyError.


with AvoidUNCPath() as path:<EOL><INDENT>if path is not None:<EOL><INDENT>cmd = '<STR_LIT>' % (path, cmd)<EOL><DEDENT>with Win32ShellCommandController(cmd) as scc:<EOL><INDENT>scc.run()<EOL><DEDENT><DEDENT>

Win32 version of os.system() that works with network shares.

    Note that this implementation returns None, as meant for use in IPython.

    Parameters
    ----------
    cmd : str
      A command to be executed in the system shell.

    Returns
    -------
    None : we explicitly do NOT return the subprocess status code, as this
    utility is meant to be used extensively in IPython, where any return value
    would trigger :func:`sys.displayhook` calls.


def __init__(self, cmd, mergeout = True):

self.cmd = cmd<EOL>self.mergeout = mergeout<EOL>

Initializes the shell command controller.

        The cmd is the program to execute, and mergeout is
        whether to blend stdout and stderr into one output
        in stdout. Merging them together in this fashion more
        reliably keeps stdout and stderr in the correct order
        especially for interactive shell usage.


def run(self, stdout_func = None, stdin_func = None, stderr_func = None):

if stdout_func == None and stdin_func == None and stderr_func == None:<EOL><INDENT>return self._run_stdio()<EOL><DEDENT>if stderr_func != None and self.mergeout:<EOL><INDENT>raise RuntimeError("<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>stdin_thread = None<EOL>threads = []<EOL>if stdin_func:<EOL><INDENT>stdin_thread = threading.Thread(target=self._stdin_thread,<EOL>args=(self.hstdin, self.piProcInfo.hProcess,<EOL>stdin_func, stdout_func))<EOL><DEDENT>threads.append(threading.Thread(target=self._stdout_thread,<EOL>args=(self.hstdout, stdout_func)))<EOL>if not self.mergeout:<EOL><INDENT>if stderr_func == None:<EOL><INDENT>stderr_func = stdout_func<EOL><DEDENT>threads.append(threading.Thread(target=self._stdout_thread,<EOL>args=(self.hstderr, stderr_func)))<EOL><DEDENT>if ResumeThread(self.piProcInfo.hThread) == <NUM_LIT>:<EOL><INDENT>raise ctypes.WinError()<EOL><DEDENT>if stdin_thread is not None:<EOL><INDENT>stdin_thread.start()<EOL><DEDENT>for thread in threads:<EOL><INDENT>thread.start()<EOL><DEDENT>if WaitForSingleObject(self.piProcInfo.hProcess, INFINITE) ==WAIT_FAILED:<EOL><INDENT>raise ctypes.WinError()<EOL><DEDENT>for thread in threads:<EOL><INDENT>thread.join()<EOL><DEDENT>if stdin_thread is not None:<EOL><INDENT>stdin_thread.join()<EOL><DEDENT>

Runs the process, using the provided functions for I/O.

        The function stdin_func should return strings whenever a
        character or characters become available.
        The functions stdout_func and stderr_func are called whenever
        something is printed to stdout or stderr, respectively.
        These functions are called from different threads (but not
        concurrently, because of the GIL).


<EOL>handle = msvcrt.get_osfhandle(sys.stdin.fileno())<EOL>result = WaitForSingleObject(handle, <NUM_LIT:100>)<EOL>if result == WAIT_FAILED:<EOL><INDENT>raise ctypes.WinError()<EOL><DEDENT>elif result == WAIT_TIMEOUT:<EOL><INDENT>print("<STR_LIT:.>", end='<STR_LIT>')<EOL>return None<EOL><DEDENT>else:<EOL><INDENT>data = ctypes.create_string_buffer(<NUM_LIT>)<EOL>bytesRead = DWORD(<NUM_LIT:0>)<EOL>print('<STR_LIT:?>', end='<STR_LIT>')<EOL>if not ReadFile(handle, data, <NUM_LIT>,<EOL>ctypes.byref(bytesRead), None):<EOL><INDENT>raise ctypes.WinError()<EOL><DEDENT>FlushConsoleInputBuffer(handle)<EOL>data = data.value<EOL>data = data.replace('<STR_LIT:\r\n>', '<STR_LIT:\n>')<EOL>data = data.replace('<STR_LIT:\r>', '<STR_LIT:\n>')<EOL>print(repr(data) + "<STR_LIT:U+0020>", end='<STR_LIT>')<EOL>return data<EOL><DEDENT>

Use the raw Win32 handle of sys.stdin to do non-blocking reads


<EOL>try:<EOL><INDENT>data = sys.stdin.read(<NUM_LIT:1>)<EOL>data = data.replace('<STR_LIT:\r>', '<STR_LIT:\n>')<EOL>return data<EOL><DEDENT>except WindowsError as we:<EOL><INDENT>if we.winerror == ERROR_NO_DATA:<EOL><INDENT>return None<EOL><DEDENT>else:<EOL><INDENT>raise we<EOL><DEDENT><DEDENT>


print(s, end='<STR_LIT>', file=sys.stdout)<EOL>sys.stdout.flush()<EOL>


print(s, end='<STR_LIT>', file=sys.stderr)<EOL>sys.stderr.flush()<EOL>


<EOL>if self.mergeout:<EOL><INDENT>return self.run(stdout_func = self._stdout_raw,<EOL>stdin_func = self._stdin_raw_block)<EOL><DEDENT>else:<EOL><INDENT>return self.run(stdout_func = self._stdout_raw,<EOL>stdin_func = self._stdin_raw_block,<EOL>stderr_func = self._stderr_raw)<EOL><DEDENT>

Runs the process using the system standard I/O.

        IMPORTANT: stdin needs to be asynchronous, so the Python
                   sys.stdin object is not used. Instead,
                   msvcrt.kbhit/getwch are used asynchronously.


unique = []<EOL>unique_dict = {}<EOL>for nn in elems:<EOL><INDENT>if nn not in unique_dict:<EOL><INDENT>unique.append(nn)<EOL>unique_dict[nn] = None<EOL><DEDENT><DEDENT>return unique<EOL>

uniq_stable(elems) -> list

    Return from an iterable, a list of all the unique elements in the input,
    but maintaining the order in which they first appear.

    A naive solution to this problem which just makes a dictionary with the
    elements as keys fails to respect the stability condition, since
    dictionaries are unsorted by nature.

    Note: All elements in the input must be valid dictionary keys for this
    routine to work, as it internally uses a dictionary for efficiency
    reasons.


def sort_compare(lst1, lst2, inplace=<NUM_LIT:1>):

if not inplace:<EOL><INDENT>lst1 = lst1[:]<EOL>lst2 = lst2[:]<EOL><DEDENT>lst1.sort(); lst2.sort()<EOL>return lst1 == lst2<EOL>

Sort and compare two lists.

    By default it does it in place, thus modifying the lists. Use inplace = 0
    to avoid that (at the cost of temporary copy creation).


dic = {}<EOL>for k,v in lst: dic[k] = v<EOL>return dic<EOL>

Takes a list of (key,value) pairs and turns it into a dict.


dic = {}<EOL>for elem in lst:<EOL><INDENT>if type(elem) in (types.ListType,types.TupleType):<EOL><INDENT>size = len(elem)<EOL>if  size == <NUM_LIT:0>:<EOL><INDENT>pass<EOL><DEDENT>elif size == <NUM_LIT:1>:<EOL><INDENT>dic[elem] = default<EOL><DEDENT>else:<EOL><INDENT>k,v = elem[<NUM_LIT:0>], elem[<NUM_LIT:1>:]<EOL>if len(v) == <NUM_LIT:1>: v = v[<NUM_LIT:0>]<EOL>dic[k] = v<EOL><DEDENT><DEDENT>else:<EOL><INDENT>dic[elem] = default<EOL><DEDENT><DEDENT>return dic<EOL>

Takes a list and turns it into a dict.
    Much slower than list2dict, but more versatile. This version can take
    lists with sublists of arbitrary length (including sclars).


return [x for subseq in seq for x in subseq]<EOL>

Flatten a list of lists (NOT recursive, only works for 2d lists).


def get_slice(seq, start=<NUM_LIT:0>, stop=None, step=<NUM_LIT:1>):

if stop == None:<EOL><INDENT>stop = len(seq)<EOL><DEDENT>item = lambda i: seq[i]<EOL>return map(item,xrange(start,stop,step))<EOL>

Get a slice of a sequence with variable step. Specify start,stop,step.


chunk = lambda i: seq[i:i+size]<EOL>return map(chunk,xrange(<NUM_LIT:0>,len(seq),size))<EOL>

Chop a sequence into chunks of the given size.


try:<EOL><INDENT>from win32api import SearchPath<EOL><DEDENT>except ImportError:<EOL><INDENT>raise ImportError('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>PATH = os.environ['<STR_LIT>']<EOL>extensions = ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>']<EOL>path = None<EOL>for ext in extensions:<EOL><INDENT>try:<EOL><INDENT>path = SearchPath(PATH, cmd + ext)[<NUM_LIT:0>]<EOL><DEDENT>except:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>if path is None:<EOL><INDENT>raise OSError("<STR_LIT>" % cmd)<EOL><DEDENT>else:<EOL><INDENT>return path<EOL><DEDENT><DEDENT>

Find the full path to a .bat or .exe using the win32api module.


enc = DEFAULT_ENCODING<EOL>for line in read_no_interrupt(p.stdout).splitlines():<EOL><INDENT>line = line.decode(enc, '<STR_LIT:replace>')<EOL>print(line, file=sys.stdout)<EOL><DEDENT>for line in read_no_interrupt(p.stderr).splitlines():<EOL><INDENT>line = line.decode(enc, '<STR_LIT:replace>')<EOL>print(line, file=sys.stderr)<EOL><DEDENT>return p.wait()<EOL>


<EOL>with AvoidUNCPath() as path:<EOL><INDENT>if path is not None:<EOL><INDENT>cmd = '<STR_LIT>' % (path, cmd)<EOL><DEDENT>return process_handler(cmd, _system_body)<EOL><DEDENT>


with AvoidUNCPath() as path:<EOL><INDENT>if path is not None:<EOL><INDENT>cmd = '<STR_LIT>' % (path, cmd)<EOL><DEDENT>out = process_handler(cmd, lambda p: p.communicate()[<NUM_LIT:0>], STDOUT)<EOL><DEDENT>if out is None:<EOL><INDENT>out = b'<STR_LIT>'<EOL><DEDENT>return py3compat.bytes_to_str(out)<EOL>

Return standard output of executing cmd in a shell.

    Accepts the same arguments as os.system().

    Parameters
    ----------
    cmd : str
      A command to be executed in the system shell.

    Returns
    -------
    stdout : str


if name is None:<EOL><INDENT>return None<EOL><DEDENT>try:<EOL><INDENT>file, filename, _ = imp.find_module(name, path)<EOL><DEDENT>except ImportError:<EOL><INDENT>return None<EOL><DEDENT>if file is None:<EOL><INDENT>return filename<EOL><DEDENT>else:<EOL><INDENT>file.close()<EOL><DEDENT>if os.path.splitext(filename)[<NUM_LIT:1>] in ["<STR_LIT>", "<STR_LIT>"]:<EOL><INDENT>return filename<EOL><DEDENT>else:<EOL><INDENT>return None<EOL><DEDENT>

imp.find_module variant that only return path of module.

    The `imp.find_module` returns a filehandle that we are not interested in.
    Also we ignore any bytecode files that `imp.find_module` finds.

    Parameters
    ----------
    name : str
        name of module to locate
    path : list of str
        list of paths to search for `name`. If path=None then search sys.path

    Returns
    -------
    filename : str
        Return full path of module or None if module is missing or does not have
        .py or .pyw extension


fbase =  os.path.join(dirname, "<STR_LIT>")<EOL>for ext in ["<STR_LIT>", "<STR_LIT>"]:<EOL><INDENT>fname = fbase + ext<EOL>if os.path.isfile(fname):<EOL><INDENT>return fname<EOL><DEDENT><DEDENT>

Get __init__ file path for module directory

    Parameters
    ----------
    dirname : str
        Find the __init__ file in directory `dirname`

    Returns
    -------
    init_path : str
        Path to __init__ file


parts = module_name.split("<STR_LIT:.>")<EOL>basepath = find_module(parts[<NUM_LIT:0>])<EOL>for submodname in parts[<NUM_LIT:1>:]:<EOL><INDENT>basepath = find_module(submodname, [basepath])<EOL><DEDENT>if basepath and os.path.isdir(basepath):<EOL><INDENT>basepath = get_init(basepath)<EOL><DEDENT>return basepath<EOL>

Find module `module_name` on sys.path

    Return the path to module `module_name`. If `module_name` refers to
    a module directory then return path to __init__ file. Return full
    path of module or None if module is missing or does not have .py or .pyw
    extension. We are not interested in running bytecode.

    Parameters
    ----------
    module_name : str

    Returns
    -------
    modulepath : str
        Path to module `module_name`.


def pr(s):<EOL><INDENT>print(s)<EOL><DEDENT>junk = ['<STR_LIT>','<STR_LIT>','<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>']<EOL>def ignorable(p):<EOL><INDENT>for pat in junk:<EOL><INDENT>if p.startswith(pat) or p.fnmatch(pat):<EOL><INDENT>return True<EOL><DEDENT><DEDENT>return False<EOL><DEDENT>modded = []<EOL>files = [path(srcdir).relpathto(p) for p in path(srcdir).walkfiles()]<EOL>rep = tgtdir / '<STR_LIT>'<EOL>try:<EOL><INDENT>rpt = pickle.load(rep.open())<EOL><DEDENT>except:<EOL><INDENT>rpt = {}<EOL><DEDENT>for f in files:<EOL><INDENT>if ignorable(f):<EOL><INDENT>continue<EOL><DEDENT>src = srcdir / f<EOL>tgt = tgtdir / f<EOL>if not tgt.isfile():<EOL><INDENT>pr("<STR_LIT>" % str(tgt))<EOL>tgt.write_text(src.text())<EOL>rpt[str(tgt)] = hashlib.md5(tgt.text()).hexdigest()<EOL><DEDENT>else:<EOL><INDENT>cont = tgt.text()<EOL>sum = rpt.get(str(tgt), None)<EOL>if sum and hashlib.md5(cont).hexdigest() == sum:<EOL><INDENT>pr("<STR_LIT>" % tgt)<EOL>tgt.write_text(src.text())<EOL>rpt[str(tgt)] = hashlib.md5(tgt.text()).hexdigest()<EOL><DEDENT>else:<EOL><INDENT>pr('<STR_LIT>' % tgt)<EOL>real = showdiff(tgt,src)<EOL>pr('<STR_LIT>') <EOL>if not real:<EOL><INDENT>pr("<STR_LIT>")<EOL>rpt[str(tgt)] = hashlib.md5(tgt.text()).hexdigest()<EOL><DEDENT>else:<EOL><INDENT>modded.append(tgt)<EOL><DEDENT><DEDENT><DEDENT><DEDENT>pickle.dump(rpt, rep.open('<STR_LIT:w>'))<EOL>if modded:<EOL><INDENT>print("<STR_LIT>")<EOL>for m in modded:<EOL><INDENT>print(m)<EOL><DEDENT><DEDENT>

Copy over all files in srcdir to tgtdir w/ native line endings

    Creates .upgrade_report in tgtdir that stores md5sums of all files
    to notice changed files b/w upgrades.


object.__setattr__(self, '<STR_LIT>', True)<EOL>dict.__init__(self, *args, **kw)<EOL>

Initialize with a dictionary, another Struct, or data.

        Parameters
        ----------
        args : dict, Struct
            Initialize with one dict or Struct
        kw : dict
            Initialize with key, value pairs.

        Examples
        --------

        >>> s = Struct(a=10,b=30)
        >>> s.a
        10
        >>> s.b
        30
        >>> s2 = Struct(s,c=30)
        >>> sorted(s2.keys())
        ['a', 'b', 'c']


if not self._allownew and key not in self:<EOL><INDENT>raise KeyError(<EOL>"<STR_LIT>" % key)<EOL><DEDENT>dict.__setitem__(self, key, value)<EOL>

Set an item with check for allownew.

        Examples
        --------

        >>> s = Struct()
        >>> s['a'] = 10
        >>> s.allow_new_attr(False)
        >>> s['a'] = 10
        >>> s['a']
        10
        >>> try:
        ...     s['b'] = 20
        ... except KeyError:
        ...     print 'this is not allowed'
        ...
        this is not allowed


<EOL>if isinstance(key, str):<EOL><INDENT>if key in self.__dict__ or hasattr(Struct, key):<EOL><INDENT>raise AttributeError(<EOL>'<STR_LIT>' % key<EOL>)<EOL><DEDENT><DEDENT>try:<EOL><INDENT>self.__setitem__(key, value)<EOL><DEDENT>except KeyError as e:<EOL><INDENT>raise AttributeError(e)<EOL><DEDENT>

Set an attr with protection of class members.

        This calls :meth:`self.__setitem__` but convert :exc:`KeyError` to
        :exc:`AttributeError`.

        Examples
        --------

        >>> s = Struct()
        >>> s.a = 10
        >>> s.a
        10
        >>> try:
        ...     s.get = 10
        ... except AttributeError:
        ...     print "you can't set a class member"
        ...
        you can't set a class member


try:<EOL><INDENT>result = self[key]<EOL><DEDENT>except KeyError:<EOL><INDENT>raise AttributeError(key)<EOL><DEDENT>else:<EOL><INDENT>return result<EOL><DEDENT>

Get an attr by calling :meth:`dict.__getitem__`.

        Like :meth:`__setattr__`, this method converts :exc:`KeyError` to
        :exc:`AttributeError`.

        Examples
        --------

        >>> s = Struct(a=10)
        >>> s.a
        10
        >>> type(s.get)
        <... 'builtin_function_or_method'>
        >>> try:
        ...     s.b
        ... except AttributeError:
        ...     print "I don't have that key"
        ...
        I don't have that key


self.merge(other)<EOL>return self<EOL>


sout = self.copy()<EOL>sout.merge(other)<EOL>return sout<EOL>


sout = self.copy()<EOL>sout -= other<EOL>return sout<EOL>


for k in list(other.keys()):<EOL><INDENT>if k in self:<EOL><INDENT>del self[k]<EOL><DEDENT><DEDENT>return self<EOL>

Inplace remove keys from self that are in other.

        Examples
        --------

        >>> s1 = Struct(a=10,b=30)
        >>> s2 = Struct(a=40)
        >>> s1 -= s2
        >>> s1
        {'b': 30}


outdict = {}<EOL>for k,lst in list(data.items()):<EOL><INDENT>if isinstance(lst, str):<EOL><INDENT>lst = lst.split()<EOL><DEDENT>for entry in lst:<EOL><INDENT>outdict[entry] = k<EOL><DEDENT><DEDENT>return outdict<EOL>

Helper function for merge.

        Takes a dictionary whose values are lists and returns a dict with
        the elements of each list as keys and the original keys as values.


return Struct(dict.copy(self))<EOL>

Return a copy as a Struct.

        Examples
        --------

        >>> s = Struct(a=10,b=30)
        >>> s2 = s.copy()
        >>> type(s2) is Struct
        True


return key in self<EOL>

hasattr function available as a method.

        Implemented like has_key.

        Examples
        --------

        >>> s = Struct(a=10)
        >>> s.hasattr('a')
        True
        >>> s.hasattr('b')
        False
        >>> s.hasattr('get')
        False


object.__setattr__(self, '<STR_LIT>', allow)<EOL>

Set whether new attributes can be created in this Struct.

        This can be used to catch typos by verifying that the attribute user
        tries to change already exists in this Struct.


def merge(self, __loc_data__=None, __conflict_solve=None, **kw):

data_dict = dict(__loc_data__,**kw)<EOL>preserve = lambda old,new: old<EOL>update   = lambda old,new: new<EOL>add      = lambda old,new: old + new<EOL>add_flip = lambda old,new: new + old  <EOL>add_s    = lambda old,new: old + '<STR_LIT:U+0020>' + new<EOL>conflict_solve = list2dict2(list(self.keys()), default = preserve)<EOL>if __conflict_solve:<EOL><INDENT>inv_conflict_solve_user = __conflict_solve.copy()<EOL>for name, func in [('<STR_LIT>',preserve), ('<STR_LIT>',update),<EOL>('<STR_LIT>',add), ('<STR_LIT>',add_flip),<EOL>('<STR_LIT>',add_s)]:<EOL><INDENT>if name in list(inv_conflict_solve_user.keys()):<EOL><INDENT>inv_conflict_solve_user[func] = inv_conflict_solve_user[name]<EOL>del inv_conflict_solve_user[name]<EOL><DEDENT><DEDENT>conflict_solve.update(self.__dict_invert(inv_conflict_solve_user))<EOL><DEDENT>for key in data_dict:<EOL><INDENT>if key not in self:<EOL><INDENT>self[key] = data_dict[key]<EOL><DEDENT>else:<EOL><INDENT>self[key] = conflict_solve[key](self[key],data_dict[key])<EOL><DEDENT><DEDENT>

Merge two Structs with customizable conflict resolution.

        This is similar to :meth:`update`, but much more flexible. First, a
        dict is made from data+key=value pairs. When merging this dict with
        the Struct S, the optional dictionary 'conflict' is used to decide
        what to do.

        If conflict is not given, the default behavior is to preserve any keys
        with their current value (the opposite of the :meth:`update` method's
        behavior).

        Parameters
        ----------
        __loc_data : dict, Struct
            The data to merge into self
        __conflict_solve : dict
            The conflict policy dict.  The keys are binary functions used to
            resolve the conflict and the values are lists of strings naming
            the keys the conflict resolution function applies to.  Instead of
            a list of strings a space separated string can be used, like
            'a b c'.
        kw : dict
            Additional key, value pairs to merge in

        Notes
        -----

        The `__conflict_solve` dict is a dictionary of binary functions which will be used to
        solve key conflicts.  Here is an example::

            __conflict_solve = dict(
                func1=['a','b','c'],
                func2=['d','e']
            )

        In this case, the function :func:`func1` will be used to resolve
        keys 'a', 'b' and 'c' and the function :func:`func2` will be used for
        keys 'd' and 'e'.  This could also be written as::

            __conflict_solve = dict(func1='a b c',func2='d e')

        These functions will be called for each key they apply to with the
        form::

            func1(self['a'], other['a'])

        The return value is used as the final merged value.

        As a convenience, merge() provides five (the most commonly needed)
        pre-defined policies: preserve, update, add, add_flip and add_s. The
        easiest explanation is their implementation::

            preserve = lambda old,new: old
            update   = lambda old,new: new
            add      = lambda old,new: old + new
            add_flip = lambda old,new: new + old  # note change of order!
            add_s    = lambda old,new: old + ' ' + new  # only for str!

        You can use those four words (as strings) as keys instead
        of defining them as functions, and the merge method will substitute
        the appropriate functions for you.

        For more complicated conflict resolution policies, you still need to
        construct your own functions.

        Examples
        --------

        This show the default policy:

        >>> s = Struct(a=10,b=30)
        >>> s2 = Struct(a=20,c=40)
        >>> s.merge(s2)
        >>> sorted(s.items())
        [('a', 10), ('b', 30), ('c', 40)]

        Now, show how to specify a conflict dict:

        >>> s = Struct(a=10,b=30)
        >>> s2 = Struct(a=20,b=40)
        >>> conflict = {'update':'a','add':'b'}
        >>> s.merge(s2,conflict)
        >>> sorted(s.items())
        [('a', 20), ('b', 70)]


if not istr:<EOL><INDENT>return istr<EOL><DEDENT>if (istr[<NUM_LIT:0>]=="<STR_LIT:'>" and istr[-<NUM_LIT:1>]=="<STR_LIT:'>") or(istr[<NUM_LIT:0>]=='<STR_LIT:">' and istr[-<NUM_LIT:1>]=='<STR_LIT:">'):<EOL><INDENT>return istr[<NUM_LIT:1>:-<NUM_LIT:1>]<EOL><DEDENT>else:<EOL><INDENT>return istr<EOL><DEDENT>

Remove a single pair of quotes from the endpoints of a string.


return strng.replace('<STR_LIT:">','<STR_LIT>').replace("<STR_LIT:'>","<STR_LIT>")<EOL>

Return the input string with single and double quotes escaped out


if isinstance(words, basestring):<EOL><INDENT>return [word.strip() for word in words.split(sep,maxsplit)<EOL>if word and not word.isspace() ]<EOL><DEDENT>if flat:<EOL><INDENT>return flatten(map(qw,words,[<NUM_LIT:1>]*len(words)))<EOL><DEDENT>return map(qw,words)<EOL>

Similar to Perl's qw() operator, but with some more options.

    qw(words,flat=0,sep=' ',maxsplit=-1) -> words.split(sep,maxsplit)

    words can also be a list itself, and with flat=1, the output will be
    recursively flattened.

    Examples:

    >>> qw('1 2')
    ['1', '2']

    >>> qw(['a b','1 2',['m n','p q']])
    [['a', 'b'], ['1', '2'], [['m', 'n'], ['p', 'q']]]

    >>> qw(['a b','1 2',['m n','p q']],flat=1)
    ['a', 'b', '1', '2', 'm', 'n', 'p', 'q']


return qw(words,<NUM_LIT:1>,sep,maxsplit)<EOL>

Calls qw(words) in flat mode. It's just a convenient shorthand.


if isinstance(indata, basestring):<EOL><INDENT>return [qw(indata)]<EOL><DEDENT>else:<EOL><INDENT>return qw(indata)<EOL><DEDENT>

qw_lol('a b') -> [['a','b']],
    otherwise it's just a call to qw().

    We need this to make sure the modules_some keys *always* end up as a
    list of lists.


<EOL>out=[]<EOL>if case:<EOL><INDENT>for term in list:<EOL><INDENT>if term.find(pat)>-<NUM_LIT:1>: out.append(term)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>lpat=pat.lower()<EOL>for term in list:<EOL><INDENT>if term.lower().find(lpat)>-<NUM_LIT:1>: out.append(term)<EOL><DEDENT><DEDENT>if len(out): return out<EOL>else: return None<EOL>

Simple minded grep-like function.
    grep(pat,list) returns occurrences of pat in list, None on failure.

    It only does simple string matching, with no support for regexps. Use the
    option case=0 for case-insensitive matching.


return grep(pat,dir(__main__)+dir(__main__.__builtins__),*opts)<EOL>

Return grep() on dir()+dir(__builtins__).

    A very common use of grep() when working interactively.


return dgrep(pat,<NUM_LIT:0>)<EOL>


return grep(pat,list,case=<NUM_LIT:0>)<EOL>


def indent(instr,nspaces=<NUM_LIT:4>, ntabs=<NUM_LIT:0>, flatten=False):

if instr is None:<EOL><INDENT>return<EOL><DEDENT>ind = '<STR_LIT:\t>'*ntabs+'<STR_LIT:U+0020>'*nspaces<EOL>if flatten:<EOL><INDENT>pat = re.compile(r'<STR_LIT>', re.MULTILINE)<EOL><DEDENT>else:<EOL><INDENT>pat = re.compile(r'<STR_LIT>', re.MULTILINE)<EOL><DEDENT>outstr = re.sub(pat, ind, instr)<EOL>if outstr.endswith(os.linesep+ind):<EOL><INDENT>return outstr[:-len(ind)]<EOL><DEDENT>else:<EOL><INDENT>return outstr<EOL><DEDENT>

Indent a string a given number of spaces or tabstops.

    indent(str,nspaces=4,ntabs=0) -> indent str by ntabs+nspaces.

    Parameters
    ----------

    instr : basestring
        The string to be indented.
    nspaces : int (default: 4)
        The number of spaces to be indented.
    ntabs : int (default: 0)
        The number of tabs to be indented.
    flatten : bool (default: False)
        Whether to scrub existing indentation.  If True, all lines will be
        aligned to the same indentation.  If False, existing indentation will
        be strictly increased.

    Returns
    -------

    str|unicode : string indented by ntabs and nspaces.


backup_suffixes = {'<STR_LIT>':'<STR_LIT>','<STR_LIT>':'<STR_LIT>','<STR_LIT>':'<STR_LIT>','<STR_LIT>':'<STR_LIT>'}<EOL>bak_filename = filename + backup_suffixes[os.name]<EOL>original = open(filename).read()<EOL>shutil.copy2(filename,bak_filename)<EOL>try:<EOL><INDENT>new = open(filename,'<STR_LIT:wb>')<EOL>new.write(os.linesep.join(original.splitlines()))<EOL>new.write(os.linesep) <EOL>new.close()<EOL><DEDENT>except:<EOL><INDENT>os.rename(bak_filename,filename)<EOL><DEDENT>if not backup:<EOL><INDENT>try:<EOL><INDENT>os.remove(bak_filename)<EOL><DEDENT>except:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>

Convert (in-place) a file to line-ends native to the current OS.

    If the optional backup argument is given as false, no backup of the
    original file is left.


if isinstance(arg,basestring): return [arg]<EOL>else: return arg<EOL>

Always return a list of strings, given a string or list of strings
    as input.

    :Examples:

        In [7]: list_strings('A single string')
        Out[7]: ['A single string']

        In [8]: list_strings(['A single string in a list'])
        Out[8]: ['A single string in a list']

        In [9]: list_strings(['A','list','of','strings'])
        Out[9]: ['A', 'list', 'of', 'strings']


if not txt:<EOL><INDENT>return (mark*width)[:width]<EOL><DEDENT>nmark = (width-len(txt)-<NUM_LIT:2>)//len(mark)//<NUM_LIT:2><EOL>if nmark < <NUM_LIT:0>: nmark =<NUM_LIT:0><EOL>marks = mark*nmark<EOL>return '<STR_LIT>' % (marks,txt,marks)<EOL>


ini_spaces = ini_spaces_re.match(strng)<EOL>if ini_spaces:<EOL><INDENT>return ini_spaces.end()<EOL><DEDENT>else:<EOL><INDENT>return <NUM_LIT:0><EOL><DEDENT>

Return the number of initial spaces in a string


<EOL>par_re = re.compile(r'<STR_LIT>',re.MULTILINE)<EOL>strng = par_re.sub('<STR_LIT>',strng)<EOL>return strng<EOL>

Format a string for screen printing.

    This removes some latex-type format codes.


if text.startswith('<STR_LIT:\n>'):<EOL><INDENT>return textwrap.dedent(text)<EOL><DEDENT>splits = text.split('<STR_LIT:\n>',<NUM_LIT:1>)<EOL>if len(splits) == <NUM_LIT:1>:<EOL><INDENT>return textwrap.dedent(text)<EOL><DEDENT>first, rest = splits<EOL>rest = textwrap.dedent(rest)<EOL>return '<STR_LIT:\n>'.join([first, rest])<EOL>

Equivalent of textwrap.dedent that ignores unindented first line.

    This means it will still dedent strings like:
    '''foo
    is a bar
    '''

    For use in wrap_paragraphs.


paragraph_re = re.compile(r'<STR_LIT>', re.MULTILINE)<EOL>text = dedent(text).strip()<EOL>paragraphs = paragraph_re.split(text)[::<NUM_LIT:2>] <EOL>out_ps = []<EOL>indent_re = re.compile(r'<STR_LIT>', re.MULTILINE)<EOL>for p in paragraphs:<EOL><INDENT>if indent_re.search(p) is None:<EOL><INDENT>p = textwrap.fill(p, ncols)<EOL><DEDENT>out_ps.append(p)<EOL><DEDENT>return out_ps<EOL>

Wrap multiple paragraphs to fit a specified width.

    This is equivalent to textwrap.wrap, but with support for multiple
    paragraphs, as separated by empty lines.

    Returns
    -------

    list of complete paragraphs, wrapped to fill `ncols` columns.


substr = '<STR_LIT>'<EOL>if len(data) > <NUM_LIT:1> and len(data[<NUM_LIT:0>]) > <NUM_LIT:0>:<EOL><INDENT>for i in range(len(data[<NUM_LIT:0>])):<EOL><INDENT>for j in range(len(data[<NUM_LIT:0>])-i+<NUM_LIT:1>):<EOL><INDENT>if j > len(substr) and all(data[<NUM_LIT:0>][i:i+j] in x for x in data):<EOL><INDENT>substr = data[<NUM_LIT:0>][i:i+j]<EOL><DEDENT><DEDENT><DEDENT><DEDENT>elif len(data) == <NUM_LIT:1>:<EOL><INDENT>substr = data[<NUM_LIT:0>]<EOL><DEDENT>return substr<EOL>

Return the longest common substring in a list of strings.

    Credit: http://stackoverflow.com/questions/2892931/longest-common-substring-from-more-than-two-strings-python


lines = text.splitlines()<EOL>matches = set()<EOL>for line in lines:<EOL><INDENT>prefix = re.match(r'<STR_LIT>', line)<EOL>if prefix:<EOL><INDENT>matches.add(prefix.group(<NUM_LIT:1>))<EOL><DEDENT>else:<EOL><INDENT>break<EOL><DEDENT><DEDENT>else:<EOL><INDENT>prefix = long_substr(list(matches))<EOL>if prefix:<EOL><INDENT>strip = len(prefix)<EOL>text = '<STR_LIT:\n>'.join([ ln[strip:] for ln in lines])<EOL><DEDENT><DEDENT>return text<EOL>

Strip leading email quotation characters ('>').

    Removes any combination of leading '>' interspersed with whitespace that
    appears *identically* in all lines of the input text.

    Parameters
    ----------
    text : str

    Examples
    --------

    Simple uses::

        In [2]: strip_email_quotes('> > text')
        Out[2]: 'text'

        In [3]: strip_email_quotes('> > text\\n> > more')
        Out[3]: 'text\\nmore'

    Note how only the common prefix that appears in all lines is stripped::

        In [4]: strip_email_quotes('> > text\\n> > more\\n> more...')
        Out[4]: '> text\\n> more\\nmore...'

    So if any line has no quote marks ('>') , then none are stripped from any
    of them ::

        In [5]: strip_email_quotes('> > text\\n> > more\\nlast different')
        Out[5]: '> > text\\n> > more\\nlast different'


for i in xrange(<NUM_LIT:0>, len(l), n):<EOL><INDENT>yield l[i:i+n]<EOL><DEDENT>


def _find_optimal(rlist , separator_size=<NUM_LIT:2> , displaywidth=<NUM_LIT>):

for nrow in range(<NUM_LIT:1>, len(rlist)+<NUM_LIT:1>) :<EOL><INDENT>chk = map(max,_chunks(rlist, nrow))<EOL>sumlength = sum(chk)<EOL>ncols = len(chk)<EOL>if sumlength+separator_size*(ncols-<NUM_LIT:1>) <= displaywidth :<EOL><INDENT>break;<EOL><DEDENT><DEDENT>return {'<STR_LIT>' : ncols,<EOL>'<STR_LIT>':(displaywidth - sumlength)/(ncols-<NUM_LIT:1>) if (ncols -<NUM_LIT:1>) else <NUM_LIT:0>,<EOL>'<STR_LIT>' : nrow,<EOL>'<STR_LIT>' : chk<EOL>}<EOL>

Calculate optimal info to columnize a list of string


def _get_or_default(mylist, i, default=None):

if i >= len(mylist):<EOL><INDENT>return default<EOL><DEDENT>else :<EOL><INDENT>return mylist[i]<EOL><DEDENT>

return list item number, or default if don't exist


def columnize(items, separator='<STR_LIT:U+0020>', displaywidth=<NUM_LIT>):

if not items :<EOL><INDENT>return '<STR_LIT:\n>'<EOL><DEDENT>matrix, info = compute_item_matrix(items, separator_size=len(separator), displaywidth=displaywidth)<EOL>fmatrix = [filter(None, x) for x in matrix]<EOL>sjoin = lambda x : separator.join([ y.ljust(w, '<STR_LIT:U+0020>') for y, w in zip(x, info['<STR_LIT>'])])<EOL>return '<STR_LIT:\n>'.join(map(sjoin, fmatrix))+'<STR_LIT:\n>'<EOL>

Transform a list of strings into a single string with columns.

    Parameters
    ----------
    items : sequence of strings
        The strings to process.

    separator : str, optional [default is two spaces]
        The string that separates columns.

    displaywidth : int, optional [default is 80]
        Width of the display in number of characters.

    Returns
    -------
    The formatted string.


def grep(self, pattern, prune = False, field = None):

def match_target(s):<EOL><INDENT>if field is None:<EOL><INDENT>return s<EOL><DEDENT>parts = s.split()<EOL>try:<EOL><INDENT>tgt = parts[field]<EOL>return tgt<EOL><DEDENT>except IndexError:<EOL><INDENT>return "<STR_LIT>"<EOL><DEDENT><DEDENT>if isinstance(pattern, basestring):<EOL><INDENT>pred = lambda x : re.search(pattern, x, re.IGNORECASE)<EOL><DEDENT>else:<EOL><INDENT>pred = pattern<EOL><DEDENT>if not prune:<EOL><INDENT>return SList([el for el in self if pred(match_target(el))])<EOL><DEDENT>else:<EOL><INDENT>return SList([el for el in self if not pred(match_target(el))])<EOL><DEDENT>

Return all strings matching 'pattern' (a regex or callable)

        This is case-insensitive. If prune is true, return all items
        NOT matching the pattern.

        If field is specified, the match must occur in the specified
        whitespace-separated field.

        Examples::

            a.grep( lambda x: x.startswith('C') )
            a.grep('Cha.*log', prune=1)
            a.grep('chm', field=-1)


if len(fields) == <NUM_LIT:0>:<EOL><INDENT>return [el.split() for el in self]<EOL><DEDENT>res = SList()<EOL>for el in [f.split() for f in self]:<EOL><INDENT>lineparts = []<EOL>for fd in fields:<EOL><INDENT>try:<EOL><INDENT>lineparts.append(el[fd])<EOL><DEDENT>except IndexError:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>if lineparts:<EOL><INDENT>res.append("<STR_LIT:U+0020>".join(lineparts))<EOL><DEDENT><DEDENT>return res<EOL>

Collect whitespace-separated fields from string list

        Allows quick awk-like usage of string lists.

        Example data (in var a, created by 'a = !ls -l')::
            -rwxrwxrwx  1 ville None      18 Dec 14  2006 ChangeLog
            drwxrwxrwx+ 6 ville None       0 Oct 24 18:05 IPython

        a.fields(0) is ['-rwxrwxrwx', 'drwxrwxrwx+']
        a.fields(1,0) is ['1 -rwxrwxrwx', '6 drwxrwxrwx+']
        (note the joining by space).
        a.fields(-1) is ['ChangeLog', 'IPython']

        IndexErrors are ignored.

        Without args, fields() just split()'s the strings.


def sort(self,field= None,  nums = False):

<EOL>if field is not None:<EOL><INDENT>dsu = [[SList([line]).fields(field),  line] for line in self]<EOL><DEDENT>else:<EOL><INDENT>dsu = [[line,  line] for line in self]<EOL><DEDENT>if nums:<EOL><INDENT>for i in range(len(dsu)):<EOL><INDENT>numstr = "<STR_LIT>".join([ch for ch in dsu[i][<NUM_LIT:0>] if ch.isdigit()])<EOL>try:<EOL><INDENT>n = int(numstr)<EOL><DEDENT>except ValueError:<EOL><INDENT>n = <NUM_LIT:0>;<EOL><DEDENT>dsu[i][<NUM_LIT:0>] = n<EOL><DEDENT><DEDENT>dsu.sort()<EOL>return SList([t[<NUM_LIT:1>] for t in dsu])<EOL>

sort by specified fields (see fields())

        Example::
            a.sort(1, nums = True)

        Sorts a by second field, in numerical order (so that 21 > 3)


def f(*a,**k):<EOL><INDENT>dhook_s = sys.displayhook<EOL>sys.displayhook = sys.__displayhook__<EOL>try:<EOL><INDENT>out = func(*a,**k)<EOL><DEDENT>finally:<EOL><INDENT>sys.displayhook = dhook_s<EOL><DEDENT>return out<EOL><DEDENT>f.__doc__ = func.__doc__<EOL>return f<EOL>

Wrap a function call in a sys.displayhook controller.

    Returns a wrapper around func which calls func, with all its arguments and
    keywords unmodified, using the default sys.displayhook.  Since IPython
    modifies sys.displayhook, it breaks the behavior of certain systems that
    rely on the default behavior, notably doctest.


<EOL>if _sysinfo.commit:<EOL><INDENT>return "<STR_LIT>", _sysinfo.commit<EOL><DEDENT>proc = subprocess.Popen('<STR_LIT>',<EOL>stdout=subprocess.PIPE,<EOL>stderr=subprocess.PIPE,<EOL>cwd=pkg_path, shell=True)<EOL>repo_commit, _ = proc.communicate()<EOL>if repo_commit:<EOL><INDENT>return '<STR_LIT>', repo_commit.strip()<EOL><DEDENT>return '<STR_LIT>', '<STR_LIT>'<EOL>

Get short form of commit hash given directory `pkg_path`

    We get the commit hash from (in order of preference):

    * IPython.utils._sysinfo.commit
    * git output, if we are in a git repository

    If these fail, we return a not-found placeholder tuple

    Parameters
    ----------
    pkg_path : str
       directory containing package
       only used for getting commit from active repo

    Returns
    -------
    hash_from : str
       Where we got the hash from - description
    hash_str : str
       short form of hash


Return dict describing the context of this package

    Parameters
    ----------
    pkg_path : str
       path containing __init__.py for package

    Returns
    -------
    context : dict
       with named parameters of interest


return os.sysconf("<STR_LIT>")<EOL>

Return the number of active CPUs on a Unix system.


p = subprocess.Popen(['<STR_LIT>','<STR_LIT>','<STR_LIT>'],stdout=subprocess.PIPE)<EOL>return p.stdout.read()<EOL>

Return the number of active CPUs on a Darwin system.


return os.environ.get("<STR_LIT>")<EOL>

Return the number of active CPUs on a Windows system.


<EOL>ncpufuncs = {'<STR_LIT>':_num_cpus_unix,<EOL>'<STR_LIT>':_num_cpus_darwin,<EOL>'<STR_LIT>':_num_cpus_windows,<EOL>'<STR_LIT>':_num_cpus_windows,<EOL>}<EOL>ncpufunc = ncpufuncs.get(platform.system(),<EOL>_num_cpus_unix)<EOL>try:<EOL><INDENT>ncpus = max(<NUM_LIT:1>,int(ncpufunc()))<EOL><DEDENT>except:<EOL><INDENT>ncpus = <NUM_LIT:1><EOL><DEDENT>return ncpus<EOL>

Return the effective number of CPUs in the system as an integer.

    This cross-platform function makes an attempt at finding the total number of
    available CPUs in the system, as returned by various underlying system and
    python calls.

    If it can't find a sensible answer, it returns 1 (though an error *may* make
    it return a large positive number that's actually incorrect).


callerNS = sys._getframe(<NUM_LIT:2>).f_locals<EOL>return dict((k,callerNS[k]) for k in names)<EOL>

Extract a set of variables by name from another frame.

    Similar to extractVars(), but with a specified depth of 1, so that names
    are exctracted exactly from above the caller.

    This is simply a convenience function so that the very common case (for us)
    of skipping exactly 1 frame doesn't have to construct a special dict for
    keyword passing.


cf = sys._getframe(<NUM_LIT:1>)<EOL>print('<STR_LIT>' % (cf.f_code.co_name,pre_msg,expr,<EOL>eval(expr,cf.f_globals,cf.f_locals)))<EOL>

Print the value of an expression from the caller's frame.

    Takes an expression, evaluates it in the caller's frame and prints both
    the given expression and the resulting value (as well as a debug mark
    indicating the name of the calling function.  The input must be of a form
    suitable for eval().

    An optional message can be passed, which will be prepended to the printed
    expr->value pair.


f = sys._getframe(depth + <NUM_LIT:1>)<EOL>global_ns = f.f_globals<EOL>module = sys.modules[global_ns['<STR_LIT>']]<EOL>return (module, f.f_locals)<EOL>

Returns (module, locals) of the funciton `depth` frames away from the caller


<EOL>words = set(dir(obj))<EOL>if hasattr(obj, '<STR_LIT>'):<EOL><INDENT>words |= set(get_class_members(obj.__class__))<EOL><DEDENT>for attr in ('<STR_LIT>', '<STR_LIT>'):<EOL><INDENT>if hasattr(obj, attr):<EOL><INDENT>try:<EOL><INDENT>func = getattr(obj, attr)<EOL>if callable(func):<EOL><INDENT>words |= set(func())<EOL><DEDENT><DEDENT>except:<EOL><INDENT>pass<EOL><DEDENT><DEDENT><DEDENT>words = [w for w in words if isinstance(w, basestring)]<EOL>return sorted(words)<EOL>

dir2(obj) -> list of strings

    Extended version of the Python builtin dir(), which does a few extra
    checks, and supports common objects with unusual internals that confuse
    dir(), such as Traits and PyCrust.

    This version is guaranteed to return only a list of true strings, whereas
    dir() returns anything that objects inject into themselves, even if they
    are later not really valid for attribute access (many extension libraries
    have such bugs).


exits = []<EOL>vars = []<EOL>exc = (None, None, None)<EOL>try:<EOL><INDENT>for mgr in managers:<EOL><INDENT>exit = mgr.__exit__<EOL>enter = mgr.__enter__<EOL>vars.append(enter())<EOL>exits.append(exit)<EOL><DEDENT>yield vars<EOL><DEDENT>except:<EOL><INDENT>exc = sys.exc_info()<EOL><DEDENT>finally:<EOL><INDENT>while exits:<EOL><INDENT>exit = exits.pop()<EOL>try:<EOL><INDENT>if exit(*exc):<EOL><INDENT>exc = (None, None, None)<EOL><DEDENT><DEDENT>except:<EOL><INDENT>exc = sys.exc_info()<EOL><DEDENT><DEDENT>if exc != (None, None, None):<EOL><INDENT>raise exc[<NUM_LIT:0>](exc[<NUM_LIT:1>]).with_traceback(exc[<NUM_LIT:2>])<EOL><DEDENT><DEDENT>

Combine multiple context managers into a single nested context manager.

   This function has been deprecated in favour of the multiple manager form
   of the with statement.

   The one advantage of this function over the multiple manager form of the
   with statement is that argument unpacking allows it to be
   used with a variable number of context managers as follows:

      with nested(*managers):
          do_something()


reps = int(reps)<EOL>assert reps >=<NUM_LIT:1>, '<STR_LIT>'<EOL>if reps==<NUM_LIT:1>:<EOL><INDENT>start = clock()<EOL>out = func(*args,**kw)<EOL>tot_time = clock()-start<EOL><DEDENT>else:<EOL><INDENT>rng = xrange(reps-<NUM_LIT:1>) <EOL>start = clock()<EOL>for dummy in rng: func(*args,**kw)<EOL>out = func(*args,**kw)  <EOL>tot_time = clock()-start<EOL><DEDENT>av_time = tot_time / reps<EOL>return tot_time,av_time,out<EOL>

timings_out(reps,func,*args,**kw) -> (t_total,t_per_call,output)

    Execute a function reps times, return a tuple with the elapsed total
    CPU time in seconds, the time per call and the function's output.

    Under Unix, the return value is the sum of user+system time consumed by
    the process, computed via the resource module.  This prevents problems
    related to the wraparound effect which the time.clock() function has.

    Under Windows the return value is in wall clock seconds. See the
    documentation for the time module for more details.


return timings_out(reps,func,*args,**kw)[<NUM_LIT:0>:<NUM_LIT:2>]<EOL>

timings(reps,func,*args,**kw) -> (t_total,t_per_call)

    Execute a function reps times, return a tuple with the elapsed total CPU
    time in seconds and the time per call. These are just the first two values
    in timings_out().


return timings_out(<NUM_LIT:1>,func,*args,**kw)[<NUM_LIT:0>]<EOL>

timing(func,*args,**kw) -> t_total

    Execute a function once, return the elapsed total CPU time in
    seconds. This is just the first value in timings_out().


import errno<EOL>try:<EOL><INDENT>return p.read()<EOL><DEDENT>except IOError as err:<EOL><INDENT>if err.errno != errno.EINTR:<EOL><INDENT>raise<EOL><DEDENT><DEDENT>

Read from a pipe ignoring EINTR errors.

    This is necessary because when reading from pipes with GUI event loops
    running in the background, often interrupts are raised that stop the
    command from completing.


def process_handler(cmd, callback, stderr=subprocess.PIPE):

sys.stdout.flush()<EOL>sys.stderr.flush()<EOL>close_fds = sys.platform != '<STR_LIT:win32>'<EOL>p = subprocess.Popen(cmd, shell=True,<EOL>stdin=subprocess.PIPE,<EOL>stdout=subprocess.PIPE,<EOL>stderr=stderr,<EOL>close_fds=close_fds)<EOL>try:<EOL><INDENT>out = callback(p)<EOL><DEDENT>except KeyboardInterrupt:<EOL><INDENT>print('<STR_LIT>')<EOL>sys.stdout.flush()<EOL>sys.stderr.flush()<EOL>out = None<EOL><DEDENT>finally:<EOL><INDENT>if p.returncode is None:<EOL><INDENT>try:<EOL><INDENT>p.terminate()<EOL>p.poll()<EOL><DEDENT>except OSError:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>if p.returncode is None:<EOL><INDENT>try:<EOL><INDENT>p.kill()<EOL><DEDENT>except OSError:<EOL><INDENT>pass<EOL><DEDENT><DEDENT><DEDENT>return out<EOL>

Open a command in a shell subprocess and execute a callback.

    This function provides common scaffolding for creating subprocess.Popen()
    calls.  It creates a Popen object and then calls the callback with it.

    Parameters
    ----------
    cmd : str
      A string to be executed with the underlying system shell (by calling
    :func:`Popen` with ``shell=True``.

    callback : callable
      A one-argument function that will be called with the Popen object.

    stderr : file descriptor number, optional
      By default this is set to ``subprocess.PIPE``, but you can also pass the
      value ``subprocess.STDOUT`` to force the subprocess' stderr to go into
      the same file descriptor as its stdout.  This is useful to read stdout
      and stderr combined in the order they are generated.

    Returns
    -------
    The return value of the provided callback is returned.


out = process_handler(cmd, lambda p: p.communicate()[<NUM_LIT:0>], subprocess.STDOUT)<EOL>if out is None:<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT>return py3compat.bytes_to_str(out)<EOL>

Return standard output of executing cmd in a shell.

    Accepts the same arguments as os.system().

    Parameters
    ----------
    cmd : str
      A command to be executed in the system shell.

    Returns
    -------
    stdout : str


out_err = process_handler(cmd, lambda p: p.communicate())<EOL>if out_err is None:<EOL><INDENT>return '<STR_LIT>', '<STR_LIT>'<EOL><DEDENT>out, err = out_err<EOL>return py3compat.bytes_to_str(out), py3compat.bytes_to_str(err)<EOL>

Return (standard output, standard error) of executing cmd in a shell.

    Accepts the same arguments as os.system().

    Parameters
    ----------
    cmd : str
      A command to be executed in the system shell.

    Returns
    -------
    stdout : str
    stderr : str


def arg_split(s, posix=False, strict=True):

<EOL>is_unicode = False<EOL>if (not py3compat.PY3) and isinstance(s, str):<EOL><INDENT>is_unicode = True<EOL>s = s.encode('<STR_LIT:utf-8>')<EOL><DEDENT>lex = shlex.shlex(s, posix=posix)<EOL>lex.whitespace_split = True<EOL>lex.commenters='<STR_LIT>' <EOL>tokens = []<EOL>while True:<EOL><INDENT>try:<EOL><INDENT>tokens.append(next(lex))<EOL><DEDENT>except StopIteration:<EOL><INDENT>break<EOL><DEDENT>except ValueError:<EOL><INDENT>if strict:<EOL><INDENT>raise<EOL><DEDENT>tokens.append(lex.token)<EOL>break<EOL><DEDENT><DEDENT>if is_unicode:<EOL><INDENT>tokens = [x.decode('<STR_LIT:utf-8>') for x in tokens]<EOL><DEDENT>return tokens<EOL>

Split a command line's arguments in a shell-like manner.

    This is a modified version of the standard library's shlex.split()
    function, but with a default of posix=False for splitting, so that quotes
    in inputs are respected.

    if strict=False, then any errors shlex.split would raise will result in the
    unparsed remainder being the last element of the list, rather than raising.
    This is because we sometimes use arg_split to parse things other than
    command-line args.


def add_s(self, s, obj, priority= <NUM_LIT:0> ):

chain = self.strs.get(s, CommandChainDispatcher())<EOL>chain.add(obj,priority)<EOL>self.strs[s] = chain<EOL>


def add_re(self, regex, obj, priority= <NUM_LIT:0> ):

chain = self.regexs.get(regex, CommandChainDispatcher())<EOL>chain.add(obj,priority)<EOL>self.regexs[regex] = chain<EOL>


if key in self.strs:<EOL><INDENT>yield self.strs[key]<EOL><DEDENT>for r, obj in self.regexs.items():<EOL><INDENT>if re.match(r, key):<EOL><INDENT>yield obj<EOL><DEDENT>else:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>

Get a seq of Commandchain objects that match key


for val in self.dispatch(key):<EOL><INDENT>for el in val:<EOL><INDENT>yield el[<NUM_LIT:1>] <EOL><DEDENT><DEDENT>return<EOL>

Yield all 'value' targets, without priority


it = iter(filelike)<EOL>try:<EOL><INDENT>first = next(it)<EOL>if not cookie_comment_re.match(first):<EOL><INDENT>yield first<EOL><DEDENT>second = next(it)<EOL>if not cookie_comment_re.match(second):<EOL><INDENT>yield second<EOL><DEDENT><DEDENT>except StopIteration:<EOL><INDENT>return<EOL><DEDENT>for line in it:<EOL><INDENT>yield line<EOL><DEDENT>

Generator to pull lines from a text-mode file, skipping the encoding
    cookie if it is found in the first two lines.


with open(filename) as f:   <EOL><INDENT>if skip_encoding_cookie:<EOL><INDENT>return "<STR_LIT>".join(strip_encoding_cookie(f))<EOL><DEDENT>else:<EOL><INDENT>return f.read()<EOL><DEDENT><DEDENT>

Read a Python file, using the encoding declared inside the file.

    Parameters
    ----------
    filename : str
      The path to the file to read.
    skip_encoding_cookie : bool
      If True (the default), and the encoding declaration is found in the first
      two lines, that line will be excluded from the output - compiling a
      unicode string with an encoding declaration is a SyntaxError in Python 2.

    Returns
    -------
    A unicode string containing the contents of the file.


def read_py_url(url, errors='<STR_LIT:replace>', skip_encoding_cookie=True):

response = urllib.request.urlopen(url)<EOL>buffer = io.BytesIO(response.read())<EOL>encoding, lines = detect_encoding(buffer.readline)<EOL>buffer.seek(<NUM_LIT:0>)<EOL>text = TextIOWrapper(buffer, encoding, errors=errors, line_buffering=True)<EOL>text.mode = '<STR_LIT:r>'<EOL>if skip_encoding_cookie:<EOL><INDENT>return "<STR_LIT>".join(strip_encoding_cookie(text))<EOL><DEDENT>else:<EOL><INDENT>return text.read()<EOL><DEDENT>

Read a Python file from a URL, using the encoding declared inside the file.

    Parameters
    ----------
    url : str
      The URL from which to fetch the file.
    errors : str
      How to handle decoding errors in the file. Options are the same as for
      bytes.decode(), but here 'replace' is the default.
    skip_encoding_cookie : bool
      If True (the default), and the encoding declaration is found in the first
      two lines, that line will be excluded from the output - compiling a
      unicode string with an encoding declaration is a SyntaxError in Python 2.

    Returns
    -------
    A unicode string containing the contents of the file.


if cmd == '<STR_LIT>':<EOL><INDENT>return os.path.abspath(sys.executable)<EOL><DEDENT>try:<EOL><INDENT>path = _find_cmd(cmd).rstrip()<EOL><DEDENT>except OSError:<EOL><INDENT>raise FindCmdError('<STR_LIT>' % cmd)<EOL><DEDENT>if path == '<STR_LIT>':<EOL><INDENT>raise FindCmdError('<STR_LIT>' % cmd)<EOL><DEDENT>return os.path.abspath(path)<EOL>

Find absolute path to executable cmd in a cross platform manner.

    This function tries to determine the full path to a command line program
    using `which` on Unix/Linux/OS X and `win32api` on Windows.  Most of the
    time it will use the version that is first on the users `PATH`.  If
    cmd is `python` return `sys.executable`.

    Warning, don't use this to find IPython command line programs as there
    is a risk you will find the wrong one.  Instead find those using the
    following code and looking for the application itself::

        from IPython.utils.path import get_ipython_module_path
        from IPython.utils.process import pycmd2argv
        argv = pycmd2argv(get_ipython_module_path('IPython.frontend.terminal.ipapp'))

    Parameters
    ----------
    cmd : str
        The command line program to look for.


ext = os.path.splitext(cmd)[<NUM_LIT:1>]<EOL>if ext in ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>return [cmd]<EOL><DEDENT>else:<EOL><INDENT>return [sys.executable, cmd]<EOL><DEDENT>

r"""Take the path of a python command and return a list (argv-style).

    This only works on Python based command line programs and will find the
    location of the ``python`` executable using ``sys.executable`` to make
    sure the right version is used.

    For a given path ``cmd``, this returns [cmd] if cmd's extension is .exe,
    .com or .bat, and [, cmd] otherwise.

    Parameters
    ----------
    cmd : string
      The path of the command.

    Returns
    -------
    argv-style list.


cwd = os.getcwdu().replace('<STR_LIT:\\>','<STR_LIT:/>')<EOL>drivepart = '<STR_LIT>'<EOL>tail = cwd<EOL>if sys.platform == '<STR_LIT:win32>':<EOL><INDENT>if len(cwd) < <NUM_LIT:4>:<EOL><INDENT>return cwd<EOL><DEDENT>drivepart,tail = os.path.splitdrive(cwd)<EOL><DEDENT>parts = tail.split('<STR_LIT:/>')<EOL>if len(parts) > <NUM_LIT:2>:<EOL><INDENT>tail = '<STR_LIT:/>'.join(parts[-<NUM_LIT:2>:])<EOL><DEDENT>return (drivepart + (<EOL>cwd == '<STR_LIT:/>' and '<STR_LIT:/>' or tail))<EOL>

Return abbreviated version of cwd, e.g. d:mydir


typenamelist = [tname for tname in dir(types) if tname.endswith("<STR_LIT>")]<EOL>typestr2type, type2typestr = {}, {}<EOL>for tname in typenamelist:<EOL><INDENT>name = tname[:-<NUM_LIT:4>].lower()          <EOL>obj = getattr(types, tname)<EOL>typestr2type[name] = obj<EOL>if name not in dont_include_in_type2typestr:<EOL><INDENT>type2typestr[obj] = name<EOL><DEDENT><DEDENT>return typestr2type, type2typestr<EOL>

Return dictionaries mapping lower case typename (e.g. 'tuple') to type
    objects from the types package, and vice versa.


if typestr_or_type == "<STR_LIT:all>":<EOL><INDENT>return True<EOL><DEDENT>if type(typestr_or_type) == types.TypeType:<EOL><INDENT>test_type = typestr_or_type<EOL><DEDENT>else:<EOL><INDENT>test_type = typestr2type.get(typestr_or_type, False)<EOL><DEDENT>if test_type:<EOL><INDENT>return isinstance(obj, test_type)<EOL><DEDENT>return False<EOL>

is_type(obj, typestr_or_type) verifies if obj is of a certain type. It
    can take strings or actual python types for the second argument, i.e.
    'tuple'<->TupleType. 'all' matches all types.

    TODO: Should be extended for choosing more than one type.


return show_all or str.startswith("<STR_LIT>") or not str.startswith("<STR_LIT:_>")<EOL>

Return true for strings starting with single _ if show_all is true.


ns = {}<EOL>for key in dir2(obj):<EOL><INDENT>try:<EOL><INDENT>ns[key] = getattr(obj, key)<EOL><DEDENT>except AttributeError:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>return ns<EOL>

Produce a dictionary of an object's attributes. Builds on dir2 by
    checking that a getattr() call actually succeeds.


def filter_ns(ns, name_pattern="<STR_LIT:*>", type_pattern="<STR_LIT:all>", ignore_case=True,<EOL>show_all=True):

pattern = name_pattern.replace("<STR_LIT:*>","<STR_LIT>").replace("<STR_LIT:?>","<STR_LIT:.>")<EOL>if ignore_case:<EOL><INDENT>reg = re.compile(pattern+"<STR_LIT:$>", re.I)<EOL><DEDENT>else:<EOL><INDENT>reg = re.compile(pattern+"<STR_LIT:$>")<EOL><DEDENT>return dict((key,obj) for key, obj in ns.iteritems() if reg.match(key)and show_hidden(key, show_all)and is_type(obj, type_pattern) )<EOL>

Filter a namespace dictionary by name pattern and item type.


def list_namespace(namespace, type_pattern, filter, ignore_case=False, show_all=False):

pattern_list=filter.split("<STR_LIT:.>")<EOL>if len(pattern_list) == <NUM_LIT:1>:<EOL><INDENT>return filter_ns(namespace, name_pattern=pattern_list[<NUM_LIT:0>],<EOL>type_pattern=type_pattern,<EOL>ignore_case=ignore_case, show_all=show_all)<EOL><DEDENT>else:<EOL><INDENT>filtered = filter_ns(namespace, name_pattern=pattern_list[<NUM_LIT:0>],<EOL>type_pattern="<STR_LIT:all>",<EOL>ignore_case=ignore_case, show_all=show_all)<EOL>results = {}<EOL>for name, obj in filtered.iteritems():<EOL><INDENT>ns = list_namespace(dict_dir(obj), type_pattern,<EOL>"<STR_LIT:.>".join(pattern_list[<NUM_LIT:1>:]),<EOL>ignore_case=ignore_case, show_all=show_all)<EOL>for inner_name, inner_obj in ns.iteritems():<EOL><INDENT>results["<STR_LIT>"%(name,inner_name)] = inner_obj<EOL><DEDENT><DEDENT>return results<EOL><DEDENT>

Return dictionary of all objects in a namespace dictionary that match
    type_pattern and filter.


if not hasattr(stream, '<STR_LIT>') or not stream.encoding:<EOL><INDENT>return default<EOL><DEDENT>else:<EOL><INDENT>return stream.encoding<EOL><DEDENT>

Return the given stream's encoding or a default.

    There are cases where sys.std* might not actually be a stream, so
    check for the encoding attribute prior to returning it, and return
    a default if it doesn't exist or evaluates as False. `default'
    is None if not provided.


enc = get_stream_enc(sys.stdin)<EOL>if not enc or enc=='<STR_LIT:ascii>':<EOL><INDENT>try:<EOL><INDENT>enc = locale.getpreferredencoding()<EOL><DEDENT>except Exception:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>return enc or sys.getdefaultencoding()<EOL>

Return IPython's guess for the default encoding for bytes as text.

    Asks for stdin.encoding first, to match the calling Terminal, but that
    is often None for subprocesses.  Fall back on locale.getpreferredencoding()
    which should be a sensible platform default (that respects LANG environment),
    and finally to sys.getdefaultencoding() which is the most conservative option,
    and usually ASCII.


for k in dikt.iterkeys():<EOL><INDENT>if isinstance(k, basestring):<EOL><INDENT>ik=fk=None<EOL>try:<EOL><INDENT>ik = int(k)<EOL><DEDENT>except ValueError:<EOL><INDENT>try:<EOL><INDENT>fk = float(k)<EOL><DEDENT>except ValueError:<EOL><INDENT>continue<EOL><DEDENT><DEDENT>if ik is not None:<EOL><INDENT>nk = ik<EOL><DEDENT>else:<EOL><INDENT>nk = fk<EOL><DEDENT>if nk in dikt:<EOL><INDENT>raise KeyError("<STR_LIT>"%nk)<EOL><DEDENT>dikt[nk] = dikt.pop(k)<EOL><DEDENT><DEDENT>return dikt<EOL>

Rekey a dict that has been forced to use str keys where there should be
    ints by json.


if isinstance(obj, dict):<EOL><INDENT>obj = dict(obj) <EOL>for k,v in obj.iteritems():<EOL><INDENT>obj[k] = extract_dates(v)<EOL><DEDENT><DEDENT>elif isinstance(obj, (list, tuple)):<EOL><INDENT>obj = [ extract_dates(o) for o in obj ]<EOL><DEDENT>elif isinstance(obj, basestring):<EOL><INDENT>if ISO8601_PAT.match(obj):<EOL><INDENT>obj = datetime.strptime(obj, ISO8601)<EOL><DEDENT><DEDENT>return obj<EOL>

extract ISO8601 dates from unpacked JSON


if isinstance(obj, dict):<EOL><INDENT>obj = dict(obj) <EOL>for k,v in obj.iteritems():<EOL><INDENT>obj[k] = squash_dates(v)<EOL><DEDENT><DEDENT>elif isinstance(obj, (list, tuple)):<EOL><INDENT>obj = [ squash_dates(o) for o in obj ]<EOL><DEDENT>elif isinstance(obj, datetime):<EOL><INDENT>obj = obj.strftime(ISO8601)<EOL><DEDENT>return obj<EOL>

squash datetime objects into ISO8601 strings


if isinstance(obj, datetime):<EOL><INDENT>return obj.strftime(ISO8601)<EOL><DEDENT>else:<EOL><INDENT>raise TypeError("<STR_LIT>"%obj)<EOL><DEDENT>

default function for packing datetime objects in JSON.


encoded = format_dict.copy()<EOL>pngdata = format_dict.get('<STR_LIT>')<EOL>if isinstance(pngdata, bytes) and pngdata[:<NUM_LIT:8>] == PNG:<EOL><INDENT>encoded['<STR_LIT>'] = encodestring(pngdata).decode('<STR_LIT:ascii>')<EOL><DEDENT>jpegdata = format_dict.get('<STR_LIT>')<EOL>if isinstance(jpegdata, bytes) and jpegdata[:<NUM_LIT:2>] == JPEG:<EOL><INDENT>encoded['<STR_LIT>'] = encodestring(jpegdata).decode('<STR_LIT:ascii>')<EOL><DEDENT>return encoded<EOL>

b64-encodes images in a displaypub format dict

    Perhaps this should be handled in json_clean itself?

    Parameters
    ----------

    format_dict : dict
        A dictionary of display data keyed by mime-type

    Returns
    -------

    format_dict : dict
        A copy of the same dictionary,
        but binary image data ('image/png' or 'image/jpeg')
        is base64-encoded.


<EOL>atomic_ok = (unicode, int, types.NoneType)<EOL>container_to_list = (tuple, set, types.GeneratorType)<EOL>if isinstance(obj, float):<EOL><INDENT>if math.isnan(obj) or math.isinf(obj):<EOL><INDENT>return repr(obj)<EOL><DEDENT>return obj<EOL><DEDENT>if isinstance(obj, atomic_ok):<EOL><INDENT>return obj<EOL><DEDENT>if isinstance(obj, bytes):<EOL><INDENT>return obj.decode(DEFAULT_ENCODING, '<STR_LIT:replace>')<EOL><DEDENT>if isinstance(obj, container_to_list) or (<EOL>hasattr(obj, '<STR_LIT>') and hasattr(obj, next_attr_name)):<EOL><INDENT>obj = list(obj)<EOL><DEDENT>if isinstance(obj, list):<EOL><INDENT>return [json_clean(x) for x in obj]<EOL><DEDENT>if isinstance(obj, dict):<EOL><INDENT>nkeys = len(obj)<EOL>nkeys_collapsed = len(set(map(str, obj)))<EOL>if nkeys != nkeys_collapsed:<EOL><INDENT>raise ValueError('<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT>out = {}<EOL>for k,v in obj.iteritems():<EOL><INDENT>out[str(k)] = json_clean(v)<EOL><DEDENT>return out<EOL><DEDENT>return repr(obj)<EOL>

Clean an object to ensure it's safe to encode in JSON.

    Atomic, immutable objects are returned unmodified.  Sets and tuples are
    converted to lists, lists are copied and dicts are also copied.

    Note: dicts whose keys could cause collisions upon encoding (such as a dict
    with both the number 1 and the string '1' as keys) will cause a ValueError
    to be raised.

    Parameters
    ----------
    obj : any python object

    Returns
    -------
    out : object

      A version of the input which will not cause an encoding error when
      encoded as JSON.  Note that this function does not *encode* its inputs,
      it simply sanitizes it so that there will be no encoding errors later.

    Examples
    --------
    >>> json_clean(4)
    4
    >>> json_clean(range(10))
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    >>> sorted(json_clean(dict(x=1, y=2)).items())
    [('x', 1), ('y', 2)]
    >>> sorted(json_clean(dict(x=1, y=2, z=[1,2,3])).items())
    [('x', 1), ('y', 2), ('z', [1, 2, 3])]
    >>> json_clean(True)
    True


usage_msg = """<STR_LIT>"""<EOL>parser = optparse.OptionParser(usage=usage_msg)<EOL>newopt = parser.add_option<EOL>newopt('<STR_LIT>','<STR_LIT>',metavar='<STR_LIT>',dest='<STR_LIT>',action='<STR_LIT:store>',<EOL>choices=['<STR_LIT>','<STR_LIT>','<STR_LIT>'],default=_scheme_default,<EOL>help="<STR_LIT>")<EOL>opts,args = parser.parse_args(argv)<EOL>if len(args) > <NUM_LIT:1>:<EOL><INDENT>parser.error("<STR_LIT>")<EOL><DEDENT>if len(args) == <NUM_LIT:0>:<EOL><INDENT>fname = '<STR_LIT:->' <EOL><DEDENT>else:<EOL><INDENT>fname = args[<NUM_LIT:0>]<EOL><DEDENT>if fname == '<STR_LIT:->':<EOL><INDENT>stream = sys.stdin<EOL><DEDENT>else:<EOL><INDENT>try:<EOL><INDENT>stream = open(fname)<EOL><DEDENT>except IOError as msg:<EOL><INDENT>print(msg, file=sys.stderr)<EOL>sys.exit(<NUM_LIT:1>)<EOL><DEDENT><DEDENT>parser = Parser()<EOL>try:<EOL><INDENT>try:<EOL><INDENT>parser.format(stream.read(),scheme=opts.scheme_name)<EOL><DEDENT>except IOError as msg:<EOL><INDENT>if msg.args != (<NUM_LIT:32>,'<STR_LIT>'):<EOL><INDENT>raise<EOL><DEDENT><DEDENT><DEDENT>finally:<EOL><INDENT>if stream is not sys.stdin:<EOL><INDENT>stream.close()<EOL><DEDENT><DEDENT>

Run as a command-line script: colorize a python file or stdin using ANSI
    color escapes and print to stdout.

    Inputs:

      - argv(None): a list of strings like sys.argv[1:] giving the command-line
        arguments. If None, use sys.argv[1:].


def __init__(self, color_table=None,out = sys.stdout):

self.color_table = color_table and color_table or ANSICodeColors<EOL>self.out = out<EOL>

Create a parser with a specified color table and output channel.

        Call format() to process code.


def format2(self, raw, out = None, scheme = '<STR_LIT>'):

string_output = <NUM_LIT:0><EOL>if out == '<STR_LIT:str>' or self.out == '<STR_LIT:str>' orisinstance(self.out,io.StringIO):<EOL><INDENT>out_old = self.out<EOL>self.out = io.StringIO()<EOL>string_output = <NUM_LIT:1><EOL><DEDENT>elif out is not None:<EOL><INDENT>self.out = out<EOL><DEDENT>if scheme == '<STR_LIT>':<EOL><INDENT>error = False<EOL>self.out.write(raw)<EOL>if string_output:<EOL><INDENT>return raw,error<EOL><DEDENT>else:<EOL><INDENT>return None,error<EOL><DEDENT><DEDENT>colors = self.color_table[scheme].colors<EOL>self.colors = colors <EOL>self.raw = raw.expandtabs().rstrip()<EOL>self.lines = [<NUM_LIT:0>, <NUM_LIT:0>]<EOL>pos = <NUM_LIT:0><EOL>raw_find = self.raw.find<EOL>lines_append = self.lines.append<EOL>while <NUM_LIT:1>:<EOL><INDENT>pos = raw_find('<STR_LIT:\n>', pos) + <NUM_LIT:1><EOL>if not pos: break<EOL>lines_append(pos)<EOL><DEDENT>lines_append(len(self.raw))<EOL>self.pos = <NUM_LIT:0><EOL>text = io.StringIO(self.raw)<EOL>error = False<EOL>try:<EOL><INDENT>for atoken in generate_tokens(text.readline):<EOL><INDENT>self(*atoken)<EOL><DEDENT><DEDENT>except tokenize.TokenError as ex:<EOL><INDENT>msg = ex.args[<NUM_LIT:0>]<EOL>line = ex.args[<NUM_LIT:1>][<NUM_LIT:0>]<EOL>self.out.write("<STR_LIT>" %<EOL>(colors[token.ERRORTOKEN],<EOL>msg, self.raw[self.lines[line]:],<EOL>colors.normal)<EOL>)<EOL>error = True<EOL><DEDENT>self.out.write(colors.normal+'<STR_LIT:\n>')<EOL>if string_output:<EOL><INDENT>output = self.out.getvalue()<EOL>self.out = out_old<EOL>return (output, error)<EOL><DEDENT>return (None, error)<EOL>

Parse and send the colored source.

        If out and scheme are not specified, the defaults (given to
        constructor) are used.

        out should be a file-type object. Optionally, out can be given as the
        string 'str' and the parser will automatically return the output in a
        string.


def __call__(self, toktype, toktext, xxx_todo_changeme, xxx_todo_changeme1, line):

(srow,scol) = xxx_todo_changeme<EOL>(erow,ecol) = xxx_todo_changeme1<EOL>colors = self.colors<EOL>owrite = self.out.write<EOL>linesep = os.linesep<EOL>oldpos = self.pos<EOL>newpos = self.lines[srow] + scol<EOL>self.pos = newpos + len(toktext)<EOL>if newpos > oldpos:<EOL><INDENT>owrite(self.raw[oldpos:newpos])<EOL><DEDENT>if toktype in [token.INDENT, token.DEDENT]:<EOL><INDENT>self.pos = newpos<EOL>return<EOL><DEDENT>if token.LPAR <= toktype and toktype <= token.OP:<EOL><INDENT>toktype = token.OP<EOL><DEDENT>elif toktype == token.NAME and keyword.iskeyword(toktext):<EOL><INDENT>toktype = _KEYWORD<EOL><DEDENT>color = colors.get(toktype, colors[_TEXT])<EOL>if linesep in toktext:<EOL><INDENT>toktext = toktext.replace(linesep, '<STR_LIT>' %<EOL>(colors.normal,linesep,color))<EOL><DEDENT>owrite('<STR_LIT>' % (color,toktext,colors.normal))<EOL>

Token handler, with syntax highlighting.


def wrapper(*args,**kw):<EOL><INDENT>wrapper.called = False<EOL>out = func(*args,**kw)<EOL>wrapper.called = True<EOL>return out<EOL><DEDENT>wrapper.called = False<EOL>wrapper.__doc__ = func.__doc__<EOL>return wrapper<EOL>

Wrap a function to detect and flag when it gets called.

    This is a decorator which takes a function and wraps it in a function with
    a 'called' attribute. wrapper.called is initialized to False.

    The wrapper.called attribute is set to False right before each call to the
    wrapped function, so if the call fails it remains False.  After the call
    completes, wrapper.called is set to True and the output is returned.

    Testing for truth in wrapper.called allows you to determine if a call to
    func() was attempted and succeeded.


fobj = open(filename,'<STR_LIT:r>');<EOL>source = fobj.read();<EOL>fobj.close()<EOL>return source<EOL>

Read a file and close it.  Returns the file source.


fobj = open(filename,'<STR_LIT:r>');<EOL>lines = fobj.readlines();<EOL>fobj.close()<EOL>return lines<EOL>

Read a file and close it.  Returns the file source using readlines().


def raw_input_multi(header='<STR_LIT>', ps1='<STR_LIT>', ps2='<STR_LIT>',terminate_str = '<STR_LIT:.>'):

try:<EOL><INDENT>if header:<EOL><INDENT>header += '<STR_LIT:\n>'<EOL><DEDENT>lines = [raw_input(header + ps1)]<EOL><DEDENT>except EOFError:<EOL><INDENT>return []<EOL><DEDENT>terminate = [terminate_str]<EOL>try:<EOL><INDENT>while lines[-<NUM_LIT:1>:] != terminate:<EOL><INDENT>new_line = raw_input(ps1)<EOL>while new_line.endswith('<STR_LIT:\\>'):<EOL><INDENT>new_line = new_line[:-<NUM_LIT:1>] + raw_input(ps2)<EOL><DEDENT>lines.append(new_line)<EOL><DEDENT>return lines[:-<NUM_LIT:1>]  <EOL><DEDENT>except EOFError:<EOL><INDENT>print()<EOL>return lines<EOL><DEDENT>

Take multiple lines of input.

    A list with each line of input as a separate element is returned when a
    termination string is entered (defaults to a single '.'). Input can also
    terminate via EOF (^D in Unix, ^Z-RET in Windows).

    Lines of input which end in \\ are joined into single entries (and a
    secondary continuation prompt is issued as long as the user terminates
    lines with \\). This allows entering very long strings which are still
    meant to be treated as single entities.


def raw_input_ext(prompt='<STR_LIT>',  ps2='<STR_LIT>'):

line = raw_input(prompt)<EOL>while line.endswith('<STR_LIT:\\>'):<EOL><INDENT>line = line[:-<NUM_LIT:1>] + raw_input(ps2)<EOL><DEDENT>return line<EOL>

Similar to raw_input(), but accepts extended lines if input ends with \\.


answers = {'<STR_LIT:y>':True,'<STR_LIT:n>':False,'<STR_LIT:yes>':True,'<STR_LIT>':False}<EOL>ans = None<EOL>while ans not in answers.keys():<EOL><INDENT>try:<EOL><INDENT>ans = raw_input(prompt+'<STR_LIT:U+0020>').lower()<EOL>if not ans:  <EOL><INDENT>ans = default<EOL><DEDENT><DEDENT>except KeyboardInterrupt:<EOL><INDENT>pass<EOL><DEDENT>except EOFError:<EOL><INDENT>if default in answers.keys():<EOL><INDENT>ans = default<EOL>print()<EOL><DEDENT>else:<EOL><INDENT>raise<EOL><DEDENT><DEDENT><DEDENT>return answers[ans]<EOL>

Asks a question and returns a boolean (y/n) answer.

    If default is given (one of 'y','n'), it is used if the user input is
    empty. Otherwise the question is repeated until an answer is given.

    An EOF is treated as the default answer.  If there is no default, an
    exception is raised to prevent infinite loops.

    Valid answers are: y/yes/n/no (match is not case sensitive).


fname = tempfile.mkstemp(ext)[<NUM_LIT:1>]<EOL>f = open(fname,'<STR_LIT:w>')<EOL>f.write(src)<EOL>f.flush()<EOL>return fname, f<EOL>

Make a temporary python file, return filename and filehandle.

    Parameters
    ----------
    src : string or list of strings (no need for ending newlines if list)
      Source code to be written to the file.

    ext : optional, string
      Extension for the generated file.

    Returns
    -------
    (filename, open filehandle)
      It is the caller's responsibility to close the open file and unlink it.


print(*args, sep=kw.get('<STR_LIT>', '<STR_LIT:U+0020>'), end=kw.get('<STR_LIT:end>', '<STR_LIT:\n>'),<EOL>file=sys.__stdout__)<EOL>sys.__stdout__.flush()<EOL>

Raw print to sys.__stdout__, otherwise identical interface to print().


print(*args, sep=kw.get('<STR_LIT>', '<STR_LIT:U+0020>'), end=kw.get('<STR_LIT:end>', '<STR_LIT:\n>'),<EOL>file=sys.__stderr__)<EOL>sys.__stderr__.flush()<EOL>

Raw print to sys.__stderr__, otherwise identical interface to print().


def __init__(self, file_or_name, mode="<STR_LIT:w>", channel='<STR_LIT>'):

if channel not in ['<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>raise ValueError('<STR_LIT>' % channel)<EOL><DEDENT>if hasattr(file_or_name, '<STR_LIT>') and hasattr(file_or_name, '<STR_LIT>'):<EOL><INDENT>self.file = file_or_name<EOL><DEDENT>else:<EOL><INDENT>self.file = open(file_or_name, mode)<EOL><DEDENT>self.channel = channel<EOL>self.ostream = getattr(sys, channel)<EOL>setattr(sys, channel, self)<EOL>self._closed = False<EOL>

Construct a new Tee object.

        Parameters
        ----------
        file_or_name : filename or open filehandle (writable)
          File that will be duplicated

        mode : optional, valid mode for open().
          If a filename was give, open with this mode.

        channel : str, one of ['stdout', 'stderr']


self.flush()<EOL>setattr(sys, self.channel, self.ostream)<EOL>self.file.close()<EOL>self._closed = True<EOL>


self.file.write(data)<EOL>self.ostream.write(data)<EOL>self.ostream.flush()<EOL>


self.file.flush()<EOL>self.ostream.flush()<EOL>


kw.setdefault('<STR_LIT>','<STR_LIT:U+0020>')<EOL>kw.setdefault('<STR_LIT>','<STR_LIT>')<EOL>kw.setdefault('<STR_LIT:start>',<NUM_LIT:0>)<EOL>kw.setdefault('<STR_LIT>',len(lst))<EOL>start = kw['<STR_LIT:start>']; del kw['<STR_LIT:start>']<EOL>stop = kw['<STR_LIT>']; del kw['<STR_LIT>']<EOL>if self.depth == <NUM_LIT:0> and '<STR_LIT>' in kw.keys():<EOL><INDENT>print(kw['<STR_LIT>'])<EOL><DEDENT>for idx in range(start,stop):<EOL><INDENT>elem = lst[idx]<EOL>newpos = pos + str(idx)<EOL>if type(elem)==type([]):<EOL><INDENT>self.depth += <NUM_LIT:1><EOL>self.__call__(elem, newpos+"<STR_LIT:U+002C>", **kw)<EOL>self.depth -= <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>print(kw['<STR_LIT>']*self.depth + newpos + kw["<STR_LIT>"] + repr(elem))<EOL><DEDENT><DEDENT>

Prints the nested list numbering levels.


sys.stdout.write(self.stdout)<EOL>sys.stderr.write(self.stderr)<EOL>sys.stdout.flush()<EOL>sys.stderr.flush()<EOL>

write my output to sys.stdout/err as appropriate


def embed_kernel(module=None, local_ns=None, **kwargs):

(caller_module, caller_locals) = extract_module_locals(<NUM_LIT:1>)<EOL>if module is None:<EOL><INDENT>module = caller_module<EOL><DEDENT>if local_ns is None:<EOL><INDENT>local_ns = caller_locals<EOL><DEDENT>from .zmq.ipkernel import embed_kernel as real_embed_kernel<EOL>real_embed_kernel(module=module, local_ns=local_ns, **kwargs)<EOL>

Embed and start an IPython kernel in a given scope.

    Parameters
    ----------
    module : ModuleType, optional
        The module to load into IPython globals (default: caller)
    local_ns : dict, optional
        The namespace to load into IPython user namespace (default: caller)

    kwargs : various, optional
        Further keyword args are relayed to the KernelApp constructor,
        allowing configuration of the Kernel.  Will only have an effect
        on the first embed_kernel call for a given process.


cell = NotebookNode()<EOL>cell.cell_type = u'<STR_LIT:code>'<EOL>if code is not None:<EOL><INDENT>cell.code = unicode(code)<EOL><DEDENT>if prompt_number is not None:<EOL><INDENT>cell.prompt_number = int(prompt_number)<EOL><DEDENT>return cell<EOL>

Create a new code cell with input and output


cell = NotebookNode()<EOL>if text is not None:<EOL><INDENT>cell.text = unicode(text)<EOL><DEDENT>cell.cell_type = u'<STR_LIT:text>'<EOL>return cell<EOL>


nb = NotebookNode()<EOL>if cells is not None:<EOL><INDENT>nb.cells = cells<EOL><DEDENT>else:<EOL><INDENT>nb.cells = []<EOL><DEDENT>return nb<EOL>

Create a notebook by name, id and a list of worksheets.


raise NotImplementedError("<STR_LIT>")<EOL>


return self.reads(fp.read(), **kwargs)<EOL>


return fp.write(self.writes(nb,**kwargs))<EOL>


return from_dict(d)<EOL>

Convert from a raw JSON dict to a nested NotebookNode structure.