Instruction:
Here you have a function in Python 3, explain what it does
def _remote_methodcall(id, method_name, *args, **kwargs):
    obj = distob.engine[id]
    nargs = []
    for a in args:
        if isinstance(a, Id):
            nargs.append(distob.engine[a])
        elif (isinstance(a, collections.Sequence) and
                not isinstance(a, string_types)):
            nargs.append(
                    [distob.engine[b] if isinstance(b, Id) else b for b in a])
        else: nargs.append(a)
    for k, a in kwargs.items():
        if isinstance(a, Id):
            kwargs[k] = distob.engine[a]
        elif (isinstance(a, collections.Sequence) and
                not isinstance(a, string_types)):
            kwargs[k] = [
                    distob.engine[b] if isinstance(b, Id) else b for b in a]
    result = getattr(obj, method_name)(*nargs, **kwargs)
    if (isinstance(result, collections.Sequence) and
            not isinstance(result, string_types)):
        # We will return any sub-sequences by value, not recurse deeper
        results = []
        for subresult in result:
            if type(subresult) in distob.engine.proxy_types:
                results.append(Ref(subresult))
            else:
                results.append(subresult)
        return results
    elif type(result) in distob.engine.proxy_types:
        return Ref(result)
    else:
        return result

Response: Internal method to call the remote method on the object with the given id.


Instruction:
Implement a function in Python 3 to
call a method of obj locally or remotely as appropriate.

Response:
def methodcall(obj, method_name, *args, **kwargs):
    """Call a method of `obj`, either locally or remotely as appropriate.
    obj may be an ordinary object, or a Remote object (or Ref or object Id)
    If there are multiple remote arguments, they must be on the same engine.

    kwargs:
      prefer_local (bool, optional): Whether to return cached local results if
        available, in preference to returning Remote objects. Default is True.
      block (bool, optional): Whether remote calls should be synchronous.
        If False, returned results may be AsyncResults and should be converted
        by the caller using convert_result() before use. Default is True.
    """
    this_engine = distob.engine.eid
    args = [obj] + list(args)
    prefer_local = kwargs.pop('prefer_local', None)
    if prefer_local is None:
        if isinstance(obj, Remote):
            prefer_local = obj.prefer_local
        else:
            prefer_local = True
    block = kwargs.pop('block', True)
    execloc, args, kwargs = _process_args(args, kwargs, prefer_local)
    if execloc is this_engine:
        r = getattr(args[0], method_name)(*args[1:], **kwargs)
    else:
        if False and prefer_local:
            # result cache disabled until issue mattja/distob#1 is fixed
            try:
                kwtuple = tuple((k, kwargs[k]) for k in sorted(kwargs.keys()))
                key = (args[0], method_name, args, kwtuple)
                r = _call_cache[key]
            except TypeError as te:
                if te.args[0][:10] == 'unhashable':
                    #print("unhashable. won't be able to cache")
                    r = _uncached_methodcall(execloc, args[0], method_name,
                                             *args[1:], **kwargs)
                else:
                    raise
            except KeyError:
                r = _uncached_methodcall(execloc, args[0], method_name,
                                         *args[1:], **kwargs)
                if block:
                    _call_cache[key] = r.r
        else:
            r = _uncached_methodcall(execloc, args[0], method_name,
                                     *args[1:], **kwargs)
    if block:
        return convert_result(r)
    else:
        return r


Instruction:
Write a Python 3 script to
scan an object and return a tuple of method and attribute info.

Response:
def _scan_instance(obj, include_underscore, exclude):
    """(Executed on remote or local engine) Examines an object and returns info
    about any instance-specific methods or attributes.
    (For example, any attributes that were set by __init__() )

    By default, methods or attributes starting with an underscore are ignored.

    Args:
      obj (object): the object to scan. must be on this local engine.
      include_underscore (bool or sequence of str): Should methods or
        attributes that start with an underscore be proxied anyway? If a
        sequence of names is provided then methods or attributes starting with
        an underscore will only be proxied if their names are in the sequence.
      exclude (sequence of str): names of any methods or attributes that should
        not be reported.
    """
    from sys import getsizeof
    always_exclude = ('__new__', '__init__', '__getattribute__', '__class__',
                      '__reduce__', '__reduce_ex__')
    method_info = []
    attributes_info = []
    if hasattr(obj, '__dict__'):
        for name in obj.__dict__:
            if (name not in exclude and
                name not in always_exclude and
                (name[0] != '_' or
                 include_underscore is True or
                 name in include_underscore)):
                f = obj.__dict__[name]
                if hasattr(f, '__doc__'):
                    doc = f.__doc__
                else:
                    doc = None
                if callable(f) and not isinstance(f, type):
                    method_info.append((name, doc))
                else:
                    attributes_info.append((name, doc))
    return (method_info, attributes_info, getsizeof(obj))


Instruction:
Can you create a Python 3 function that
classes decorator. Modifies `Remote` subclasses to add proxy methods and attributes that mimic those defined in class `base`. Example: @proxy_methods(Tree) class RemoteTree(Remote, Tree) The decorator registers the new proxy class and specifies which methods and attributes of class `base` should be proxied via a remote call to a real object, and which methods/attributes should not be proxied but instead called directly on the instance of the proxy class. By default all methods and attributes of the class `base` will be proxied except those starting with an underscore. The MRO of the decorated class is respected: Any methods and attributes defined in the decorated class (or in other bases of the decorated class that do not come after `base` in its MRO) will override those added by this decorator, so that `base` is treated like a base class. Args: base (type): The class whose instances should be remotely controlled. include_underscore (bool or sequence of str): Should methods or attributes that start with an underscore be proxied anyway? If a sequence of names is provided then methods or attributes starting with an underscore will only be proxied if their names are in the sequence. exclude (sequence of str): Names of any methods or attributes that should not be proxied. supers (bool): Proxy methods and attributes defined in superclasses of ``base``, in addition to those defined directly in class ``base``

Response:
def proxy_methods(base, include_underscore=None, exclude=None, supers=True):
    """class decorator. Modifies `Remote` subclasses to add proxy methods and
    attributes that mimic those defined in class `base`.

    Example:

      @proxy_methods(Tree)
      class RemoteTree(Remote, Tree)

    The decorator registers the new proxy class and specifies which methods
    and attributes of class `base` should be proxied via a remote call to
    a real object, and which methods/attributes should not be proxied but
    instead called directly on the instance of the proxy class.

    By default all methods and attributes of the class `base` will be
    proxied except those starting with an underscore.

    The MRO of the decorated class is respected:
    Any methods and attributes defined in the decorated class
    (or in other bases of the decorated class that do not come after `base`
     in its MRO) will override those added by this decorator,
    so that `base` is treated like a base class.

    Args:
      base (type): The class whose instances should be remotely controlled.
      include_underscore (bool or sequence of str): Should methods or
        attributes that start with an underscore be proxied anyway? If a
        sequence of names is provided then methods or attributes starting with
        an underscore will only be proxied if their names are in the sequence.
      exclude (sequence of str): Names of any methods or attributes that
        should not be proxied.
      supers (bool): Proxy methods and attributes defined in superclasses
        of ``base``, in addition to those defined directly in class ``base``
    """
    always_exclude = ('__new__', '__init__', '__getattribute__', '__class__',
                      '__reduce__', '__reduce_ex__')
    if isinstance(include_underscore, str):
        include_underscore = (include_underscore,)
    if isinstance(exclude, str):
        exclude = (exclude,)
    if not include_underscore:
        include_underscore = ()
    if not exclude:
        exclude = ()
    def rebuild_class(cls):
        # Identify any bases of cls that do not come after `base` in the list:
        bases_other = list(cls.__bases__)
        if bases_other[-1] is object:
            bases_other.pop()
        if base in bases_other:
            bases_other = bases_other[:bases_other.index(base)]
        if not issubclass(cls.__bases__[0], Remote):
            raise DistobTypeError('First base class must be subclass of Remote')
        if not issubclass(base, object):
            raise DistobTypeError('Only new-style classes currently supported')
        dct = cls.__dict__.copy()
        if cls.__doc__ is None or '\n' not in cls.__doc__:
            base_doc = base.__doc__
            if base_doc is None:
                base_doc = ''
            dct['__doc__'] = """Local object representing a remote %s
                  It can be used just like a %s object, but behind the scenes
                  all requests are passed to a real %s object on a remote host.

                  """ % ((base.__name__,)*3) + base_doc
        newcls = type(cls.__name__, cls.__bases__, dct)
        newcls._include_underscore = include_underscore
        newcls._exclude = exclude
        if supers:
            proxied_classes = base.__mro__[:-1]
        else:
            proxied_classes = (base,)
        for c in proxied_classes:
            for name in c.__dict__:
                #respect MRO: proxy an attribute only if it is not overridden
                if (name not in newcls.__dict__ and
                        all(name not in b.__dict__
                            for c in bases_other for b in c.mro()[:-1]) and
                        name not in newcls._exclude and
                        name not in always_exclude and
                        (name[0] != '_' or
                         newcls._include_underscore is True or
                         name in newcls._include_underscore)):
                    f = c.__dict__[name]
                    if hasattr(f, '__doc__'):
                        doc = f.__doc__
                    else:
                        doc = None
                    if callable(f) and not isinstance(f, type):
                        setattr(newcls, name, _make_proxy_method(name, doc))
                    else:
                        setattr(newcls, name, _make_proxy_property(name, doc))
        newcls.__module__ = '__main__' # cause dill to pickle it whole
        import __main__
        __main__.__dict__[newcls.__name__] = newcls # for dill..
        ObjectHub.register_proxy_type(base, newcls)
        return newcls
    return rebuild_class


Instruction:
Can you implement a function in Python 3 that
distributes an object or list to remote engines. Returns an async result or lists of nested objects where each of which is a Ref

Response:
def _async_scatter(obj, destination=None):
    """Distribute an obj or list to remote engines.
    Return an async result or (possibly nested) lists of async results,
    each of which is a Ref
    """
    #TODO Instead of special cases for strings and Remote, should have a
    #     list of types that should not be proxied, inc. strings and Remote.
    if (isinstance(obj, Remote) or
            isinstance(obj, numbers.Number) or
            obj is None):
        return obj
    if (isinstance(obj, collections.Sequence) and
            not isinstance(obj, string_types)):
        ars = []
        if destination is not None:
            assert(len(destination) == len(obj))
            for i in range(len(obj)):
                ars.append(_async_scatter(obj[i], destination[i]))
        else:
            for i in range(len(obj)):
                ars.append(_async_scatter(obj[i], destination=None))
        return ars
    else:
        if distob.engine is None:
            setup_engines()
        client = distob.engine._client
        dv = distob.engine._dv
        def remote_put(obj):
            return Ref(obj)
        if destination is not None:
            assert(isinstance(destination, numbers.Integral))
            dv.targets = destination
        else:
            dv.targets = _async_scatter.next_engine
            _async_scatter.next_engine = (
                    _async_scatter.next_engine + 1) % len(client)
        ar_ref = dv.apply_async(remote_put, obj)
        dv.targets = client.ids
        return ar_ref


Instruction:
Make a summary of the following Python 3 code
def _ars_to_proxies(ars):
    if (isinstance(ars, Remote) or
            isinstance(ars, numbers.Number) or
            ars is None):
        return ars
    elif isinstance(ars, collections.Sequence):
        res = []
        for i in range(len(ars)):
            res.append(_ars_to_proxies(ars[i]))
        return res
    elif isinstance(ars, ipyparallel.AsyncResult):
        ref = ars.r
        ObClass = ref.type
        if ObClass in distob.engine.proxy_types:
            RemoteClass = distob.engine.proxy_types[ObClass]
        else:
            RemoteClass = type(
                    'Remote' + ObClass.__name__, (Remote, ObClass), dict())
            RemoteClass = proxy_methods(ObClass)(RemoteClass)
        proxy_obj = RemoteClass(ref)
        return proxy_obj
    else:
        raise DistobTypeError('Unpacking ars: unexpected type %s' % type(ars))

Response: wait for AsyncResults and return Remote objects


Instruction:
Can you tell what is the following Python 3 function doing
def _scatter_ndarray(ar, axis=-1, destination=None, blocksize=None):
    from .arrays import DistArray, RemoteArray
    shape = ar.shape
    ndim = len(shape)
    if axis is None:
        return _directed_scatter([ar], destination=[destination],
                                 blocksize=blocksize)[0]
    if axis < -ndim or axis > ndim - 1:
        raise DistobValueError('axis out of range')
    if axis < 0:
        axis = ndim + axis
    n = shape[axis]
    if n == 1:
        return _directed_scatter([ar], destination=[destination])[0]
    if isinstance(destination, collections.Sequence):
        ne = len(destination) # number of engines to scatter array to
    else:
        if distob.engine is None:
            setup_engines()
        ne = distob.engine.nengines # by default scatter across all engines
    if blocksize is None:
        blocksize = ((n - 1) // ne) + 1
    if blocksize > n:
        blocksize = n
    if isinstance(ar, DistArray):
        if axis == ar._distaxis:
            return ar
        else:
            raise DistobError('Currently can only scatter one axis of array')
    # Currently, if requested to scatter an array that is already Remote and
    # large, first get whole array locally, then scatter. Not really optimal.
    if isinstance(ar, RemoteArray) and n > blocksize:
        ar = ar._ob
    s = slice(None)
    subarrays = []
    low = 0
    for i in range(0, n // blocksize):
        high = low + blocksize
        index = (s,)*axis + (slice(low, high),) + (s,)*(ndim - axis - 1)
        subarrays.append(ar[index])
        low += blocksize
    if n % blocksize != 0:
        high = low + (n % blocksize)
        index = (s,)*axis + (slice(low, high),) + (s,)*(ndim - axis - 1)
        subarrays.append(ar[index])
    subarrays = _directed_scatter(subarrays, destination=destination)
    return DistArray(subarrays, axis)

Response: Turn a numpy array into a DistArray or RemoteArray with optional destination and blocksize.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def scatter(obj, axis=-1, blocksize=None):
    if hasattr(obj, '__distob_scatter__'):
        return obj.__distob_scatter__(axis, None, blocksize)
    if distob._have_numpy and (isinstance(obj, np.ndarray) or
                        hasattr(type(obj), '__array_interface__')):
        return _scatter_ndarray(obj, axis, blocksize)
    elif isinstance(obj, Remote):
        return obj
    ars = _async_scatter(obj)
    proxy_obj = _ars_to_proxies(ars)
    return proxy_obj

Response: Distribute a python object or list of objects along a given axis.


Instruction:
How would you explain what the following Python 3 function does
def gather(obj):
    if hasattr(obj, '__distob_gather__'):
        return obj.__distob_gather__()
    elif (isinstance(obj, collections.Sequence) and
            not isinstance(obj, string_types)):
        return [gather(subobj) for subobj in obj]
    else:
        return obj

Response: Retrieve objects that have been distributed making them local again


Instruction:
Can you generate a brief explanation for the following Python 3 code
def vectorize(f):
    def vf(obj, *args, **kwargs):
        # user classes can customize how to vectorize a function:
        if hasattr(obj, '__distob_vectorize__'):
            return obj.__distob_vectorize__(f)(obj, *args, **kwargs)
        if isinstance(obj, Remote):
            return call(f, obj, *args, **kwargs)
        elif distob._have_numpy and (isinstance(obj, np.ndarray) or
                 hasattr(type(obj), '__array_interface__')):
            distarray = scatter(obj, axis=-1)
            return vf(distarray, *args, **kwargs)
        elif isinstance(obj, collections.Sequence):
            inputs = scatter(obj)
            dv = distob.engine._client[:]
            kwargs = kwargs.copy()
            kwargs['block'] = False
            results = []
            for obj in inputs:
                results.append(call(f, obj, *args, **kwargs))
            for i in range(len(results)):
                results[i] = convert_result(results[i])
            return results
    if hasattr(f, '__name__'):
        vf.__name__ = 'v' + f.__name__
        f_str = f.__name__ + '()'
    else:
        f_str = 'callable'
    doc = u"""Apply %s in parallel to a list or array\n
           Args:
             obj (Sequence of objects or an array)
             other args are the same as for %s
           """ % (f_str, f_str)
    if hasattr(f, '__doc__') and f.__doc__ is not None:
        doc = doc.rstrip() + (' detailed below:\n----------\n' + f.__doc__)
    vf.__doc__ = doc
    return vf

Response: Upgrade normal function f to act in parallel on distibuted lists or arrays of N + 1 dimensions.


Instruction:
How would you explain what the following Python 3 function does
def apply(f, obj, *args, **kwargs):
    return vectorize(f)(obj, *args, **kwargs)

Response: Apply a function in parallel to each element of the input


Instruction:
How would you implement a function in Python 3 that
calls a method on each element of a sequence in parallel.

Response:
def call_all(sequence, method_name, *args, **kwargs):
    """Call a method on each element of a sequence, in parallel.
    Returns:
      list of results
    """
    kwargs = kwargs.copy()
    kwargs['block'] = False
    results = []
    for obj in sequence:
        results.append(methodcall(obj, method_name, *args, **kwargs))
    for i in range(len(results)):
        results[i] = convert_result(results[i])
    return results


Instruction:
Can you tell what is the following Python 3 function doing
def register_proxy_type(cls, real_type, proxy_type):
        if distob.engine is None:
            cls._initial_proxy_types[real_type] = proxy_type
        elif isinstance(distob.engine, ObjectHub):
            distob.engine._runtime_reg_proxy_type(real_type, proxy_type)
        else:
            # TODO: remove next line after issue #58 in dill is fixed.
            distob.engine._singleeng_reg_proxy_type(real_type, proxy_type)
            pass

Response: Configure engines so that remote methods returning values of typereal_type will instead return by proxy as type proxy_type


Instruction:
Can you tell what is the following Python 3 function doing
def _fetch(self):
        if not self.is_local and not self._obcache_current:
            #print('fetching data from %s' % self._ref.id)
            def _remote_fetch(id):
                return distob.engine[id]
            self._obcache = self._dv.apply_sync(_remote_fetch, self._id)
            self._obcache_current = True
            self.__engine_affinity__ = (distob.engine.eid,
                                        self.__engine_affinity__[1])

Response: forces update of a local cached copy of the real object


Instruction:
How would you explain what the following Python 3 function does
def extract_json(fileobj, keywords, comment_tags, options):
    data=fileobj.read()
    json_extractor=JsonExtractor(data)
    strings_data=json_extractor.get_lines_data()

    for item in strings_data:
        messages = [item['content']]
        if item.get('funcname') == 'ngettext':
            messages.append(item['alt_content'])
        yield item['line_number'],item.get('funcname','gettext'),tuple(messages),[]

Response: Extract a list of strings from a JSON file.


Instruction:
Can you tell what is the following Python 3 function doing
def get_lines_data(self):

        encoding = 'utf-8'

        for token in tokenize(self.data.decode(encoding)):
            if token.type == 'operator':
                if token.value == '{':
                    self.start_object()
                elif token.value ==':':
                    self.with_separator(token)
                elif token.value == '}':
                    self.end_object()
                elif token.value == ',':
                    self.end_pair()

            elif token.type=='string':
                if self.state=='key':
                    self.current_key=unquote_string(token.value)
                    if self.current_key==JSON_GETTEXT_KEYWORD:
                        self.gettext_mode=True

                #==value not actually used, but if only key was met (like in list) it still will be used. The important part, that key wont be parsed as value, not reversal
                if self.gettext_mode:
                    if self.current_key==JSON_GETTEXT_KEY_CONTENT:
                        self.token_to_add=token
                    elif self.current_key==JSON_GETTEXT_KEY_ALT_CONTENT:
                        self.token_params['alt_token']=token
                    elif self.current_key==JSON_GETTEXT_KEY_FUNCNAME:
                        self.token_params['funcname']=token.value
                else:
                    self.token_to_add=token

        return self.results

Response: Returns a list of line numbers for the current line number.


Instruction:
Explain what the following Python 3 code does
def is_git_repo(path):
    '''Returns True if path is a git repository.'''

    if path.startswith('git@') or path.startswith('https://'):
        return True

    if os.path.exists(unipath(path, '.git')):
        return True

    return False

Response: Returns True if path is a git repository.


Instruction:
Implement a function in Python 3 to
return True if path is in the home directory of the environment.

Response:
def is_home_environment(path):
    '''Returns True if path is in CPENV_HOME'''

    home = unipath(os.environ.get('CPENV_HOME', '~/.cpenv'))
    path = unipath(path)

    return path.startswith(home)


Instruction:
Here you have a function in Python 3, explain what it does
def is_redirecting(path):
    '''Returns True if path contains a .cpenv file'''

    candidate = unipath(path, '.cpenv')
    return os.path.exists(candidate) and os.path.isfile(candidate)

Response: Returns True if path contains a. cpenv file


Instruction:
Given the following Python 3 function, write the documentation
def redirect_to_env_paths(path):
    '''Get environment path from redirect file'''

    with open(path, 'r') as f:
        redirected = f.read()

    return shlex.split(redirected)

Response: Get environment paths from redirect file


Instruction:
Can you generate a brief explanation for the following Python 3 code
def expandpath(path):
    '''Returns an absolute expanded path'''

    return os.path.abspath(os.path.expandvars(os.path.expanduser(path)))

Response: Returns an absolute expanded path


Instruction:
Write a Python 3 function for
liking os. path. join but also expands and normalizes path parts.

Response:
def unipath(*paths):
    '''Like os.path.join but also expands and normalizes path parts.'''

    return os.path.normpath(expandpath(os.path.join(*paths)))


Instruction:
Can you generate a brief explanation for the following Python 3 code
def binpath(*paths):
    '''Like os.path.join but acts relative to this packages bin path.'''

    package_root = os.path.dirname(__file__)
    return os.path.normpath(os.path.join(package_root, 'bin', *paths))

Response: Like os. path. join but acts relative to this packages bin path.


Instruction:
Can you generate the documentation for the following Python 3 function
def ensure_path_exists(path, *args):
    '''Like os.makedirs but keeps quiet if path already exists'''
    if os.path.exists(path):
        return

    os.makedirs(path, *args)

Response: Like os. makedirs but keeps quiet if path already exists


Instruction:
How would you implement a function in Python 3 that
walks down a directory tree. Same as os. walk but allows for a depth limit.

Response:
def walk_dn(start_dir, depth=10):
    '''
    Walk down a directory tree. Same as os.walk but allows for a depth limit
    via depth argument
    '''

    start_depth = len(os.path.split(start_dir))
    end_depth = start_depth + depth

    for root, subdirs, files in os.walk(start_dir):
        yield root, subdirs, files

        if len(os.path.split(root)) >= end_depth:
            break


Instruction:
Create a Python 3 function for
walking up a directory tree and yield a list of all the files and subdirs.

Response:
def walk_up(start_dir, depth=20):
    '''
    Walk up a directory tree
    '''
    root = start_dir

    for i in xrange(depth):
        contents = os.listdir(root)
        subdirs, files = [], []
        for f in contents:
            if os.path.isdir(os.path.join(root, f)):
                subdirs.append(f)
            else:
                files.append(f)

        yield root, subdirs, files

        parent = os.path.dirname(root)
        if parent and not parent == root:
            root = parent
        else:
            break


Instruction:
How would you code a function in Python 3 to
add a sequence value to env dict d

Response:
def _join_seq(d, k, v):
    '''Add a sequence value to env dict'''

    if k not in d:
        d[k] = list(v)

    elif isinstance(d[k], list):
        for item in v:
            if item not in d[k]:
                d[k].insert(0, item)

    elif isinstance(d[k], string_types):
        v.append(d[k])
        d[k] = v


Instruction:
Write a Python 3 function that can
join a bunch of dicts

Response:
def join_dicts(*dicts):
    '''Join a bunch of dicts'''

    out_dict = {}

    for d in dicts:
        for k, v in d.iteritems():

            if not type(v) in JOINERS:
                raise KeyError('Invalid type in dict: {}'.format(type(v)))

            JOINERS[type(v)](out_dict, k, v)

    return out_dict


Instruction:
Can you tell what is the following Python 3 function doing
def env_to_dict(env, pathsep=os.pathsep):
    '''
    Convert a dict containing environment variables into a standard dict.
    Variables containing multiple values will be split into a list based on
    the argument passed to pathsep.

    :param env: Environment dict like os.environ.data
    :param pathsep: Path separator used to split variables
    '''

    out_dict = {}

    for k, v in env.iteritems():
        if pathsep in v:
            out_dict[k] = v.split(pathsep)
        else:
            out_dict[k] = v

    return out_dict

Response: Convert a dict containing environment variables into a standard dict.


Instruction:
Write a Python 3 function that can
convert a python dict to an environment dict containing valid environment variable values.

Response:
def dict_to_env(d, pathsep=os.pathsep):
    '''
    Convert a python dict to a dict containing valid environment variable
    values.

    :param d: Dict to convert to an env dict
    :param pathsep: Path separator used to join lists(default os.pathsep)
    '''

    out_env = {}

    for k, v in d.iteritems():
        if isinstance(v, list):
            out_env[k] = pathsep.join(v)
        elif isinstance(v, string_types):
            out_env[k] = v
        else:
            raise TypeError('{} not a valid env var type'.format(type(v)))

    return out_env


Instruction:
Here you have a function in Python 3, explain what it does
def expand_envvars(env):
    '''
    Expand all environment variables in an environment dict

    :param env: Environment dict
    '''

    out_env = {}

    for k, v in env.iteritems():
        out_env[k] = Template(v).safe_substitute(env)

    # Expand twice to make sure we expand everything we possibly can
    for k, v in out_env.items():
        out_env[k] = Template(v).safe_substitute(out_env)

    return out_env

Response: Expand all environment variables in an environment dict


Instruction:
Can you write a function in Python 3 where it
returns an unused random filepath.

Response:
def get_store_env_tmp():
    '''Returns an unused random filepath.'''

    tempdir = tempfile.gettempdir()
    temp_name = 'envstore{0:0>3d}'
    temp_path = unipath(tempdir, temp_name.format(random.getrandbits(9)))
    if not os.path.exists(temp_path):
        return temp_path
    else:
        return get_store_env_tmp()


Instruction:
Can you generate a brief explanation for the following Python 3 code
def store_env(path=None):
    '''Encode current environment as yaml and store in path or a temporary
    file. Return the path to the stored environment.
    '''

    path = path or get_store_env_tmp()

    env_dict = yaml.safe_dump(os.environ.data, default_flow_style=False)

    with open(path, 'w') as f:
        f.write(env_dict)

    return path

Response:
Encode current environment as yaml and store in path or a temporary
    file. Return the path to the stored environment.


Instruction:
Explain what the following Python 3 code does
def restore_env(env_dict):
    '''Set environment variables in the current python process from a dict
    containing envvars and values.'''

    if hasattr(sys, 'real_prefix'):
        sys.prefix = sys.real_prefix
        del(sys.real_prefix)

    replace_osenviron(expand_envvars(dict_to_env(env_dict)))

Response:
Set environment variables in the current python process from a dict
    containing envvars and values.


Instruction:
How would you code a function in Python 3 to
restore the current environment from an environment stored in a yaml yaml file.

Response:
def restore_env_from_file(env_file):
    '''Restore the current environment from an environment stored in a yaml
    yaml file.

    :param env_file: Path to environment yaml file.
    '''

    with open(env_file, 'r') as f:
        env_dict = yaml.load(f.read())

    restore_env(env_dict)


Instruction:
Write a Python 3 script to
set environment variables in the current python process from a dict containing envvars and values.

Response:
def set_env(*env_dicts):
    '''Set environment variables in the current python process from a dict
    containing envvars and values.'''

    old_env_dict = env_to_dict(os.environ.data)
    new_env_dict = join_dicts(old_env_dict, *env_dicts)
    new_env = dict_to_env(new_env_dict)
    replace_osenviron(expand_envvars(new_env))


Instruction:
Write a Python 3 function that can
restore the current environment from an environment stored in a yaml file.

Response:
def set_env_from_file(env_file):
    '''Restore the current environment from an environment stored in a yaml
    yaml file.

    :param env_file: Path to environment yaml file.
    '''

    with open(env_file, 'r') as f:
        env_dict = yaml.load(f.read())

    if 'environment' in env_dict:
        env_dict = env_dict['environment']

    set_env(env_dict)


Instruction:
Write a Python 3 script to
return the URL to the upstream data source for the given URI based on configuration

Response:
def upstream_url(self, uri):
        "Returns the URL to the upstream data source for the given URI based on configuration"
        return self.application.options.upstream + self.request.uri


Instruction:
How would you code a function in Python 3 to
return ( headers body ) from the cache or raise KeyError

Response:
def cache_get(self):
        "Returns (headers, body) from the cache or raise KeyError"
        key = self.cache_key()
        (headers, body, expires_ts) = self.application._cache[key]
        if expires_ts < time.now():
            # asset has expired, delete it
            del self.application._cache[key]
            raise KeyError(key)

        return (headers, body)


Instruction:
Create a Python 3 function to
return request object for calling the upstream

Response:
def make_upstream_request(self):
        "Return request object for calling the upstream"
        url = self.upstream_url(self.request.uri)
        return tornado.httpclient.HTTPRequest(url,
            method=self.request.method,
            headers=self.request.headers,
            body=self.request.body if self.request.body else None)


Instruction:
Write a Python 3 script for
returning the time to live in seconds. 0 means no caching.

Response:
def ttl(self, response):
        """Returns time to live in seconds. 0 means no caching.

        Criteria:
        - response code 200
        - read-only method (GET, HEAD, OPTIONS)
        Plus http headers:
        - cache-control: option1, option2, ...
          where options are:
          private | public
          no-cache
          no-store
          max-age: seconds
          s-maxage: seconds
          must-revalidate
          proxy-revalidate
        - expires: Thu, 01 Dec 1983 20:00:00 GMT
        - pragma: no-cache (=cache-control: no-cache)

        See http://www.mobify.com/blog/beginners-guide-to-http-cache-headers/

        TODO: tests

        """
        if response.code != 200: return 0
        if not self.request.method in ['GET', 'HEAD', 'OPTIONS']: return 0

        try:
            pragma = self.request.headers['pragma']
            if pragma == 'no-cache':
                return 0
        except KeyError:
            pass

        try:
            cache_control = self.request.headers['cache-control']

            # no caching options
            for option in ['private', 'no-cache', 'no-store', 'must-revalidate', 'proxy-revalidate']:
                if cache_control.find(option): return 0

            # further parsing to get a ttl
            options = parse_cache_control(cache_control)
            try:
                return int(options['s-maxage'])
            except KeyError:
                pass
            try:
                return int(options['max-age'])
            except KeyError:
                pass

            if 's-maxage' in options:
                max_age = options['s-maxage']
                if max_age < ttl: ttl = max_age
            if 'max-age' in options:
                max_age = options['max-age']
                if max_age < ttl: ttl = max_age
            return ttl
        except KeyError:
            pass

        try:
            expires = self.request.headers['expires']
            return time.mktime(time.strptime(expires, '%a, %d %b %Y %H:%M:%S')) - time.time()
        except KeyError:
            pass


Instruction:
Explain what the following Python 3 code does
def hist2d(x, y, bins=10, labels=None, aspect="auto", plot=True, fig=None, ax=None, interpolation='none', cbar=True, **kwargs):
    h_range   = kwargs.pop('range', None)
    h_normed  = kwargs.pop('normed', None)
    h_weights = kwargs.pop('weights', None)

    h, xe, ye = _np.histogram2d(x, y, bins=bins, range=h_range, normed=h_normed, weights=h_weights)
    extent    = [xe[0], xe[-1], ye[0], ye[-1]]
    # fig     = plt.figure()
    if plot:
        if ax is None:
            if fig is None:
                fig = _figure('hist2d')
            ax = fig.gca()
            ax.clear()

        img = ax.imshow(h.transpose(), extent=extent, interpolation=interpolation, aspect=aspect, **kwargs)
        if cbar:
            _colorbar(ax=ax, im=img)

        if labels is not None:
            _addlabel(labels[0], labels[1], labels[2])

        # _showfig(fig, aspect)
    return h, extent

Response: Create a 2 - D histogram of data x y.


Instruction:
Can you implement a function in Python 3 that
calculates the frequency of the plasma density in units of mass.

Response:
def w_p(self):
        """
        Plasma frequency :math:`\\omega_p` for given plasma density
        """
        return _np.sqrt(self.n_p * _np.power(_spc.e, 2) / (_spc.m_e * _spc.epsilon_0))


Instruction:
Given the following Python 3 function, write the documentation
def k_ion(self, E):
        return self.n_p * _np.power(_spc.e, 2) / (2*_sltr.GeV2joule(E) * _spc.epsilon_0)

Response: Geometric focusing force due to ion column for given plasma density


Instruction:
Can you write a function in Python 3 where it
guarantees the existence of a basic MANIFEST. in.

Response:
def manifest():
    """Guarantee the existence of a basic MANIFEST.in.

    manifest doc: http://docs.python.org/distutils/sourcedist.html#manifest

    `options.paved.dist.manifest.include`: set of files (or globs) to include with the `include` directive.

    `options.paved.dist.manifest.recursive_include`: set of files (or globs) to include with the `recursive-include` directive.

    `options.paved.dist.manifest.prune`: set of files (or globs) to exclude with the `prune` directive.

    `options.paved.dist.manifest.include_sphinx_docroot`: True -> sphinx docroot is added as `graft`

    `options.paved.dist.manifest.include_sphinx_docroot`: True -> sphinx builddir is added as `prune`
    """
    prune = options.paved.dist.manifest.prune
    graft = set()

    if options.paved.dist.manifest.include_sphinx_docroot:
        docroot = options.get('docroot', 'docs')
        graft.update([docroot])

        if options.paved.dist.manifest.exclude_sphinx_builddir:
            builddir = docroot + '/' + options.get("builddir", ".build")
            prune.update([builddir])

    with open(options.paved.cwd / 'MANIFEST.in', 'w') as fo:
        for item in graft:
            fo.write('graft %s\n' % item)
        for item in options.paved.dist.manifest.include:
            fo.write('include %s\n' % item)
        for item in options.paved.dist.manifest.recursive_include:
            fo.write('recursive-include %s\n' % item)
        for item in prune:
            fo.write('prune %s\n' % item)


Instruction:
Explain what the following Python 3 code does
def create_threadpool_executed_func(original_func):
    def wrapped_func(*args, **kwargs):
        try:
            result = original_func(*args, **kwargs)
            return True, result
        except:
            return False, sys.exc_info()

    def new_func(*args, **kwargs):
        status, result = gevent.get_hub().threadpool.apply(wrapped_func, args, kwargs)
        if status:
            return result
        else:
            six.reraise(*result)
    new_func.__name__ = original_func.__name__
    new_func.__doc__ = "(gevent-friendly)" + (" " + original_func.__doc__ if original_func.__doc__ is not None else "")
    return new_func

Response: Creates a function that defers function calls execute inside gevent s threadpool.


Instruction:
Can you generate the documentation for the following Python 3 function
def format_pathname(
        pathname,
        max_length):
    """
    Format a pathname

    :param str pathname: Pathname to format
    :param int max_length: Maximum length of result pathname (> 3)
    :return: Formatted pathname
    :rtype: str
    :raises ValueError: If *max_length* is not larger than 3

    This function formats a pathname so it is not longer than *max_length*
    characters. The resulting pathname is returned. It does so by replacing
    characters at the start of the *pathname* with three dots, if necessary.
    The idea is that the end of the *pathname* is the most important part
    to be able to identify the file.
    """
    if max_length <= 3:
        raise ValueError("max length must be larger than 3")

    if len(pathname) > max_length:
        pathname = "...{}".format(pathname[-(max_length-3):])

    return pathname

Response: Formats a pathname so that it is not longer than max_length characters.


Instruction:
Here you have a function in Python 3, explain what it does
def format_time_point(
        time_point_string):

    """
    :param str time_point_string: String representation of a time point
        to format
    :return: Formatted time point
    :rtype: str
    :raises ValueError: If *time_point_string* is not formatted by
        dateutil.parser.parse

    See :py:meth:`datetime.datetime.isoformat` function for supported formats.
    """
    time_point = dateutil.parser.parse(time_point_string)

    if not is_aware(time_point):
        time_point = make_aware(time_point)

    time_point = local_time_point(time_point)

    return time_point.strftime("%Y-%m-%dT%H:%M:%S")

Response: Formats a time point in a cluster node s time series.


Instruction:
Here you have a function in Python 3, explain what it does
def format_uuid(
        uuid,
        max_length=10):
    """
    Format a UUID string

    :param str uuid: UUID to format
    :param int max_length: Maximum length of result string (> 3)
    :return: Formatted UUID
    :rtype: str
    :raises ValueError: If *max_length* is not larger than 3

    This function formats a UUID so it is not longer than *max_length*
    characters. The resulting string is returned. It does so by replacing
    characters at the end of the *uuid* with three dots, if necessary.
    The idea is that the start of the *uuid* is the most important part
    to be able to identify the related entity.

    The default *max_length* is 10, which will result in a string
    containing the first 7 characters of the *uuid* passed in. Most of
    the time, such a string is still unique within a collection of UUIDs.
    """
    if max_length <= 3:
        raise ValueError("max length must be larger than 3")

    if len(uuid) > max_length:
        uuid = "{}...".format(uuid[0:max_length-3])

    return uuid

Response: Formats a UUID string for a given locale.


Instruction:
Write a Python 3 function for
finding the reduced chi square difference of observe and expect with a given error and ddof degrees of freedom.

Response:
def chisquare(observe, expect, error, ddof, verbose=True):
    """
    Finds the reduced chi square difference of *observe* and *expect* with a given *error* and *ddof* degrees of freedom.

    *verbose* flag determines if the reduced chi square is printed to the terminal.
    """
    chisq = 0
    error   = error.flatten()
    observe = observe.flatten()
    expect  = expect.flatten()
    for i, el in enumerate(observe):
        chisq = chisq + _np.power((el - expect[i]) / error[i], 2)

    red_chisq = chisq / (len(observe) - ddof)
    if verbose:
        # print 'Chi-Squared is {}.'.format(chisq)
        print('Reduced Chi-Squared is {}.'.format(red_chisq))

    return red_chisq


Instruction:
Write a Python 3 function that can
return the mantissa and exponent of a number x.

Response:
def frexp10(x):
    """
    Finds the mantissa and exponent of a number :math:`x` such that :math:`x = m 10^e`.

    Parameters
    ----------

    x : float
        Number :math:`x` such that :math:`x = m 10^e`.

    Returns
    -------

    mantissa : float
        Number :math:`m` such that :math:`x = m 10^e`.
    exponent : float
        Number :math:`e` such that :math:`x = m 10^e`.
    """
    expon = _np.int(_np.floor(_np.log10(_np.abs(x))))
    mant = x/_np.power(10, expon)
    return (mant, expon)


Instruction:
Can you tell what is the following Python 3 function doing
def get_upcoming_events_count(days=14, featured=False):
    from happenings.models import Event
    start_period = today - datetime.timedelta(days=2)
    end_period = today + datetime.timedelta(days=days)
    if featured:
        return Event.objects.filter(
            featured=True,
            start_date__gte=start_period,
            start_date__lte=end_period
        ).count()
    return Event.objects.filter(start_date__gte=start_period, start_date__lte=end_period).count()

Response: Returns the number of upcoming events for a given number of days


Instruction:
Can you implement a function in Python 3 that
gets upcoming events for a given number of days.

Response:
def get_upcoming_events(num, days, featured=False):
    """
    Get upcoming events.
    Allows slicing to a given number,
    picking the number of days to hold them after they've started
    and whether they should be featured or not.
    Usage:
    {% get_upcoming_events 5 14 featured as events %}
    Would return no more than 5 Featured events,
    holding them for 14 days past their start date.

    """
    from happenings.models import Event
    start_date = today - datetime.timedelta(days=days)
    events = Event.objects.filter(start_date__gt=start_date).order_by('start_date')
    if featured:
        events = events.filter(featured=True)
    events = events[:num]
    return events


Instruction:
Here you have a function in Python 3, explain what it does
def get_events_by_date_range(days_out, days_hold, max_num=5, featured=False):
    from happenings.models import Event
    range_start = today - datetime.timedelta(days=days_hold)
    range_end = today + datetime.timedelta(days=days_out)

    events = Event.objects.filter(
        start_date__gte=range_start,
        start_date__lte=range_end
    ).order_by('start_date')
    if featured:
        events = events.filter(featured=True)
    events = events[:max_num]
    return events

Response: Get upcoming events for a given number of days out


Instruction:
Create a Python 3 function to
return a dict of next and previous on updates

Response:
def paginate_update(update):
    """
    attempts to get next and previous on updates
    """
    from happenings.models import Update
    time = update.pub_time
    event = update.event
    try:
        next = Update.objects.filter(
            event=event,
            pub_time__gt=time
        ).order_by('pub_time').only('title')[0]
    except:
        next = None
    try:
        previous = Update.objects.filter(
            event=event,
            pub_time__lt=time
        ).order_by('-pub_time').only('title')[0]
    except:
        previous = None
    return {'next': next, 'previous': previous, 'event': event}


Instruction:
Can you generate the documentation for the following Python 3 function
def notify_client(
        notifier_uri,
        client_id,
        status_code,
        message=None):
    """
    Notify the client of the result of handling a request

    The payload contains two elements:

    - client_id
    - result

    The *client_id* is the id of the client to notify. It is assumed
    that the notifier service is able to identify the client by this id
    and that it can pass the *result* to it.

    The *result* always contains a *status_code* element. In case the
    message passed in is not None, it will also contain a *message*
    element.

    In case the notifier service does not exist or returns an error,
    an error message will be logged to *stderr*.
    """
    payload = {
        "client_id": client_id,
        "result": {
            "response": {
                "status_code": status_code
            }
        }
    }

    if message is not None:
        payload["result"]["response"]["message"] = message

    response = requests.post(notifier_uri, json=payload)

    if response.status_code != 201:
        sys.stderr.write("failed to notify client: {}\n".format(payload))
        sys.stderr.flush()

Response: Notify the client of the result of handling a request.


Instruction:
Make a summary of the following Python 3 code
def consume_message_with_notify(
        notifier_uri_getter):
    """
    Decorator for methods handling requests from RabbitMQ

    This decorator builds on the :py:func:`consume_message` decorator. It extents
    it by logic for notifying a client of the result of handling the
    request.

    The *notifier_uri_getter* argument must be a callable which accepts
    *self* and returns the uri of the notifier service.
    """

    def consume_message_with_notify_decorator(
            method):

        @consume_message
        def wrapper(
                self,
                data):

            notifier_uri = notifier_uri_getter(self)
            client_id = data["client_id"]

            # Forward the call to the method and notify the client of the
            # result
            try:
                method(self, data)
                notify_client(notifier_uri, client_id, 200)
            except Exception as exception:
                notify_client(notifier_uri, client_id, 400, str(exception))
                raise

        return wrapper

    return consume_message_with_notify_decorator

Response: This is a simple decorator for methods handling requests from RabbitMQ and notifies a client of the result of the message.


Instruction:
Can you generate the documentation for the following Python 3 function
def get(self, key):
        try:
            value = self._cache[key]
            self._order.push(key)
            self._hits += 1
            return value
        except KeyError, e:
            self._misses += 1
            raise

Response: Get a key from the cache.


Instruction:
Can you tell what is the following Python 3 function doing
def put(self, key, value):
        self._cache[key] = value
        self._order.push(key)
        self._size += 1

Response: Add a new entry to the cache.


Instruction:
Can you write a function in Python 3 where it
deletes the entry with the given key.

Response:
def delete(self, key):
        """
        >>> c = LRUCache()
        >>> c.put(1, 'one')
        >>> c.get(1)
        'one'
        >>> c.delete(1)
        >>> c.get(1)
        Traceback (most recent call last):
            ...
        KeyError: 1
        >>> c.delete(1)
        Traceback (most recent call last):
            ...
        KeyError: 1
        """
        del self._cache[key]
        self._order.delete(key)
        self._size -= 1


Instruction:
How would you explain what the following Python 3 function does
def put(self, key, value):
        mem = sys.getsizeof(value)
        if self._mem + mem > self._maxmem:
            self.delete(self.last())
        LRUCache.put(self, key, (value, mem))
        self._mem += mem

Response: Put a key - value pair into the cache.


Instruction:
Implement a function in Python 3 to
delete an entry from the cache.

Response:
def delete(self, key):
        """
        >>> c = MemSizeLRUCache()
        >>> c.put(1, 1)
        >>> c.mem()
        24
        >>> c.delete(1)
        >>> c.mem()
        0
        """
        (_value, mem) = LRUCache.get(self, key)
        self._mem -= mem
        LRUCache.delete(self, key)


Instruction:
How would you explain what the following Python 3 function does
def put(self, key, value):
        # check if we're maxed out first
        if self.size() == self._maxsize:
            # need to kick something out...
            self.delete(self.last())
        LRUCache.put(self, key, value)

Response: Put a key - value pair into the cache.


Instruction:
How would you code a function in Python 3 to
retrieve the value of a setting whose name is indicated by name_hyphen.

Response:
def setting(self, name_hyphen):
        """
        Retrieves the setting value whose name is indicated by name_hyphen.

        Values starting with $ are assumed to reference environment variables,
        and the value stored in environment variables is retrieved. It's an
        error if thes corresponding environment variable it not set.
        """
        if name_hyphen in self._instance_settings:
            value = self._instance_settings[name_hyphen][1]
        else:
            msg = "No setting named '%s'" % name_hyphen
            raise UserFeedback(msg)

        if hasattr(value, 'startswith') and value.startswith("$"):
            env_var = value.lstrip("$")
            if env_var in os.environ:
                return os.getenv(env_var)
            else:
                msg = "'%s' is not defined in your environment" % env_var
                raise UserFeedback(msg)

        elif hasattr(value, 'startswith') and value.startswith("\$"):
            return value.replace("\$", "$")

        else:
            return value


Instruction:
How would you implement a function in Python 3 that
returns a dict of all setting values.

Response:
def setting_values(self, skip=None):
        """
        Returns dict of all setting values (removes the helpstrings).
        """
        if not skip:
            skip = []

        return dict(
                (k, v[1])
                for k, v in six.iteritems(self._instance_settings)
                if not k in skip)


Instruction:
Write a Python 3 script to
update the internal settings of the cashew instance.

Response:
def _update_settings(self, new_settings, enforce_helpstring=True):
        """
        This method does the work of updating settings. Can be passed with
        enforce_helpstring = False which you may want if allowing end users to
        add arbitrary metadata via the settings system.

        Preferable to use update_settings (without leading _) in code to do the
        right thing and always have docstrings.
        """
        for raw_setting_name, value in six.iteritems(new_settings):
            setting_name = raw_setting_name.replace("_", "-")

            setting_already_exists = setting_name in self._instance_settings
            value_is_list_len_2 = isinstance(value, list) and len(value) == 2
            treat_as_tuple = not setting_already_exists and value_is_list_len_2

            if isinstance(value, tuple) or treat_as_tuple:
                self._instance_settings[setting_name] = value

            else:
                if setting_name not in self._instance_settings:
                    if enforce_helpstring:
                        msg = "You must specify param '%s' as a tuple of (helpstring, value)"
                        raise InternalCashewException(msg % setting_name)

                    else:
                        # Create entry with blank helpstring.
                        self._instance_settings[setting_name] = ('', value,)

                else:
                    # Save inherited helpstring, replace default value.
                    orig = self._instance_settings[setting_name]
                    self._instance_settings[setting_name] = (orig[0], value,)


Instruction:
Can you create a Python 3 function that
returns a combined dictionary of setting values and attribute values.

Response:
def settings_and_attributes(self):
        """Return a combined dictionary of setting values and attribute values."""
        attrs = self.setting_values()
        attrs.update(self.__dict__)
        skip = ["_instance_settings", "aliases"]
        for a in skip:
            del attrs[a]
        return attrs


Instruction:
Write a Python 3 function for
detecting if we get a class or a name convert a name to a class.

Response:
def get_reference_to_class(cls, class_or_class_name):
        """
        Detect if we get a class or a name, convert a name to a class.
        """
        if isinstance(class_or_class_name, type):
            return class_or_class_name

        elif isinstance(class_or_class_name, string_types):
            if ":" in class_or_class_name:
                mod_name, class_name = class_or_class_name.split(":")

                if not mod_name in sys.modules:
                    __import__(mod_name)

                mod = sys.modules[mod_name]
                return mod.__dict__[class_name]

            else:
                return cls.load_class_from_locals(class_or_class_name)

        else:
            msg = "Unexpected Type '%s'" % type(class_or_class_name)
            raise InternalCashewException(msg)


Instruction:
Can you implement a function in Python 3 that
checks that the class has a docstring and returns it if successful.

Response:
def check_docstring(cls):
        """
        Asserts that the class has a docstring, returning it if successful.
        """
        docstring = inspect.getdoc(cls)
        if not docstring:
            breadcrumbs = " -> ".join(t.__name__ for t in inspect.getmro(cls)[:-1][::-1])
            msg = "docstring required for plugin '%s' (%s, defined in %s)"
            args = (cls.__name__, breadcrumbs, cls.__module__)
            raise InternalCashewException(msg % args)

        max_line_length = cls._class_settings.get('max-docstring-length')
        if max_line_length:
            for i, line in enumerate(docstring.splitlines()):
                if len(line) > max_line_length:
                    msg = "docstring line %s of %s is %s chars too long"
                    args = (i, cls.__name__, len(line) - max_line_length)
                    raise Exception(msg % args)

        return docstring


Instruction:
Here you have a function in Python 3, explain what it does
def resourcePath(self, relative_path):
        from os import path
        import sys

        try:
            # PyInstaller creates a temp folder and stores path in _MEIPASS
            base_path = sys._MEIPASS
        except Exception:
            base_path = path.dirname(path.abspath(__file__))
        return path.join(base_path, relative_path)

Response: Get absolute path to resource


Instruction:
How would you explain what the following Python 3 function does
def addLogbook(self, physDef= "LCLS", mccDef="MCC", initialInstance=False):
        '''Add new block of logbook selection windows. Only 5 allowed.'''
        if self.logMenuCount < 5:
            self.logMenus.append(LogSelectMenu(self.logui.multiLogLayout, initialInstance))
            self.logMenus[-1].addLogbooks(self.logTypeList[1], self.physics_programs, physDef)
            self.logMenus[-1].addLogbooks(self.logTypeList[0], self.mcc_programs, mccDef)
            self.logMenus[-1].show()
            self.logMenuCount += 1
            if initialInstance:
                # Initial logbook menu can add additional menus, all others can only remove themselves.
                QObject.connect(self.logMenus[-1].logButton, SIGNAL("clicked()"), self.addLogbook)
            else:
                from functools import partial
                QObject.connect(self.logMenus[-1].logButton, SIGNAL("clicked()"), partial(self.removeLogbook, self.logMenus[-1]))

Response: Add new block of logbook selection windows. Only 5 allowed.


Instruction:
Write a Python 3 function that can
remove logbook menu set.

Response:
def removeLogbook(self, menu=None):
        '''Remove logbook menu set.'''
        if self.logMenuCount > 1 and menu is not None:
            menu.removeMenu()
            self.logMenus.remove(menu)
            self.logMenuCount -= 1


Instruction:
Implement a Python 3 function for
returning selected log books by type.

Response:
def selectedLogs(self):
        '''Return selected log books by type.'''
        mcclogs = []
        physlogs = []
        for i in range(len(self.logMenus)):
            logType = self.logMenus[i].selectedType()
            log = self.logMenus[i].selectedProgram()
            if logType == "MCC":
                if log not in mcclogs:
                    mcclogs.append(log)
            elif logType == "Physics":
                if log not in physlogs:
                    physlogs.append(log)
        return mcclogs, physlogs


Instruction:
Can you tell what is the following Python 3 function doing
def acceptedUser(self, logType):
        '''Verify enetered user name is on accepted MCC logbook list.'''
        from urllib2 import urlopen, URLError, HTTPError
        import json

        isApproved = False

        userName = str(self.logui.userName.text())
        if userName == "":
            return False  # Must have a user name to submit entry

        if logType == "MCC":
            networkFault = False
            data = []
            log_url = "https://mccelog.slac.stanford.edu/elog/dev/mgibbs/dev_json_user_list.php/?username=" + userName
            try:
                data = urlopen(log_url, None, 5).read()
                data = json.loads(data)
            except URLError as error:
                print("URLError: " + str(error.reason))
                networkFault = True
            except HTTPError as error:
                print("HTTPError: " + str(error.reason))
                networkFault = True

            # If network fails, ask user to verify
            if networkFault:
                msgBox = QMessageBox()
                msgBox.setText("Cannot connect to MCC Log Server!")
                msgBox.setInformativeText("Use entered User name anyway?")
                msgBox.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
                msgBox.setDefaultButton(QMessageBox.Ok)
                if msgBox.exec_() == QMessageBox.Ok:
                    isApproved = True

            if data != [] and (data is not None):
                isApproved = True
        else:
            isApproved = True
        return isApproved

Response: Verify enetered user name is on accepted MCC logbook list.


Instruction:
Given the following Python 3 function, write the documentation
def xmlSetup(self, logType, logList):

        from xml.etree.ElementTree import Element, SubElement, ElementTree
        from datetime import datetime

        curr_time = datetime.now()
        if logType == "MCC":
            # Set up xml tags
            log_entry = Element('log_entry')
            title     = SubElement(log_entry, 'title')
            program   = SubElement(log_entry, 'program')
            timestamp = SubElement(log_entry, 'timestamp')
            priority  = SubElement(log_entry, 'priority')
            os_user   = SubElement(log_entry, 'os_user')
            hostname  = SubElement(log_entry, 'hostname')
            text      = SubElement(log_entry, 'text')
            log_user  = SubElement(log_entry, 'log_user')

            # Check for multiple logbooks and parse into seperate tags
            logbook = []
            for i in range(len(logList)):
                logbook.append(SubElement(log_entry, 'logbook'))
                logbook[i].text = logList[i].lower()

            # Take care of dummy, unchanging tags first
            log_entry.attrib['type'] = "LOGENTRY"
            program.text = "152"
            priority.text = "NORMAL"
            os_user.text = "nobody"
            hostname.text = "mccelog"
            text.attrib['type'] = "text/plain"

            # Handle attachment if image exists
            if not self.imagePixmap.isNull():
                attachment = SubElement(log_entry, 'attachment')
                attachment.attrib['name'] = "Figure 1"
                attachment.attrib['type'] = "image/" + self.imageType
                attachment.text = curr_time.strftime("%Y%m%d_%H%M%S_") + str(curr_time.microsecond) + "." + self.imageType

            # Set timestamp format
            timestamp.text = curr_time.strftime("%Y/%m/%d %H:%M:%S")

            fileName = "/tmp/" + curr_time.strftime("%Y%m%d_%H%M%S_") + str(curr_time.microsecond) + ".xml"

        else:  # If using Physics logbook
            timeString = curr_time.strftime("%Y-%m-%dT%H:%M:%S")

            # Set up xml tags
            log_entry = Element(None)
            severity  = SubElement(log_entry, 'severity')
            location  = SubElement(log_entry, 'location')
            keywords  = SubElement(log_entry, 'keywords')
            time      = SubElement(log_entry, 'time')
            isodate   = SubElement(log_entry, 'isodate')
            log_user  = SubElement(log_entry, 'author')
            category  = SubElement(log_entry, 'category')
            title     = SubElement(log_entry, 'title')
            metainfo  = SubElement(log_entry, 'metainfo')

            # Handle attachment if image exists
            if not self.imagePixmap.isNull():
                imageFile = SubElement(log_entry, 'link')
                imageFile.text = timeString + "-00." + self.imageType
                thumbnail = SubElement(log_entry, 'file')
                thumbnail.text = timeString + "-00.png"

            text      = SubElement(log_entry, 'text')  # Logbook expects Text tag to come last (for some strange reason)

            # Take care of dummy, unchanging tags first
            log_entry.attrib['type'] = "LOGENTRY"
            category.text = "USERLOG"
            location.text = "not set"
            severity.text = "NONE"
            keywords.text = "none"

            time.text = curr_time.strftime("%H:%M:%S")
            isodate.text = curr_time.strftime("%Y-%m-%d")

            metainfo.text = timeString + "-00.xml"
            fileName = "/tmp/" + metainfo.text

        # Fill in user inputs
        log_user.text = str(self.logui.userName.text())

        title.text = str(self.logui.titleEntry.text())
        if title.text == "":
            QMessageBox().warning(self, "No Title entered", "Please enter a title for the entry...")
            return None

        text.text = str(self.logui.textEntry.toPlainText())
        # If text field is truly empty, ElementTree leaves off tag entirely which causes logbook parser to fail
        if text.text == "":
            text.text = " "

        # Create xml file
        xmlFile = open(fileName, "w")
        if logType == "MCC":
            ElementTree(log_entry).write(xmlFile)
        else:
            xmlString = self.prettify(log_entry)
            xmlFile.write(xmlString)
        xmlFile.write("\n")  # Close with newline so cron job parses correctly
        xmlFile.close()

        return fileName.rstrip(".xml")

Response: Create xml file with fields from logbook form.


Instruction:
Write a Python 3 function for
parsing xml elements for pretty printing

Response:
def prettify(self, elem):
        """Parse xml elements for pretty printing"""

        from xml.etree import ElementTree
        from re import sub

        rawString = ElementTree.tostring(elem, 'utf-8')
        parsedString = sub(r'(?=<[^/].*>)', '\n', rawString)  # Adds newline after each closing tag

        return parsedString[1:]


Instruction:
Implement a function in Python 3 to
convert supplied QPixmap object to image file.

Response:
def prepareImages(self, fileName, logType):
        """Convert supplied QPixmap object to image file."""
        import subprocess

        if self.imageType == "png":
            self.imagePixmap.save(fileName + ".png", "PNG", -1)
            if logType == "Physics":
                makePostScript = "convert " + fileName + ".png " + fileName + ".ps"
                process = subprocess.Popen(makePostScript, shell=True)
                process.wait()
                thumbnailPixmap = self.imagePixmap.scaled(500, 450, Qt.KeepAspectRatio)
                thumbnailPixmap.save(fileName + ".png", "PNG", -1)
        else:
            renameImage = "cp " + self.image + " " + fileName + ".gif"
            process = subprocess.Popen(renameImage, shell=True)
            process.wait()
            if logType == "Physics":
                thumbnailPixmap = self.imagePixmap.scaled(500, 450, Qt.KeepAspectRatio)
                thumbnailPixmap.save(fileName + ".png", "PNG", -1)


Instruction:
How would you explain what the following Python 3 function does
def submitEntry(self):

        # logType = self.logui.logType.currentText()
        mcclogs, physlogs = self.selectedLogs()
        success = True

        if mcclogs != []:
            if not self.acceptedUser("MCC"):
                QMessageBox().warning(self, "Invalid User", "Please enter a valid user name!")
                return

            fileName = self.xmlSetup("MCC", mcclogs)
            if fileName is None:
                return

            if not self.imagePixmap.isNull():
                self.prepareImages(fileName, "MCC")
            success = self.sendToLogbook(fileName, "MCC")

        if physlogs != []:
            for i in range(len(physlogs)):
                fileName = self.xmlSetup("Physics", physlogs[i])
                if fileName is None:
                    return

                if not self.imagePixmap.isNull():
                    self.prepareImages(fileName, "Physics")
                success_phys = self.sendToLogbook(fileName, "Physics", physlogs[i])
                success = success and success_phys

        self.done(success)

Response: Process user inputs and subit logbook entry when user clicks Submit button


Instruction:
Create a Python 3 function for
processing log information and push to selected logbooks.

Response:
def sendToLogbook(self, fileName, logType, location=None):
        '''Process log information and push to selected logbooks.'''
        import subprocess

        success = True
        if logType == "MCC":
            fileString = ""
            if not self.imagePixmap.isNull():
                fileString = fileName + "." + self.imageType

            logcmd = "xml2elog " + fileName + ".xml " + fileString
            process = subprocess.Popen(logcmd, shell=True)
            process.wait()
            if process.returncode != 0:
                success = False
        else:
            from shutil import copy

            path = "/u1/" + location.lower() + "/physics/logbook/data/"  # Prod path
            # path = "/home/softegr/alverson/log_test/"  # Dev path
            try:
                if not self.imagePixmap.isNull():
                    copy(fileName + ".png", path)
                    if self.imageType == "png":
                        copy(fileName + ".ps", path)
                    else:
                        copy(fileName + "." + self.imageType, path)

                # Copy .xml file last to ensure images will be picked up by cron job
                # print("Copying file " + fileName + " to path " + path)
                copy(fileName + ".xml", path)
            except IOError as error:
                print(error)
                success = False

        return success


Instruction:
How would you implement a function in Python 3 that
clears all form fields except author.

Response:
def clearForm(self):
        """Clear all form fields (except author)."""

        self.logui.titleEntry.clear()
        self.logui.textEntry.clear()

        # Remove all log selection menus except the first
        while self.logMenuCount > 1:
            self.removeLogbook(self.logMenus[-1])


Instruction:
Make a summary of the following Python 3 code
def setupUI(self):
        '''Create graphical objects for menus.'''

        labelSizePolicy = QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
        labelSizePolicy.setHorizontalStretch(0)
        labelSizePolicy.setVerticalStretch(0)
        menuSizePolicy = QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
        menuSizePolicy.setHorizontalStretch(0)
        menuSizePolicy.setVerticalStretch(0)

        logTypeLayout = QHBoxLayout()
        logTypeLayout.setSpacing(0)

        typeLabel = QLabel("Log Type:")
        typeLabel.setMinimumSize(QSize(65, 0))
        typeLabel.setMaximumSize(QSize(65, 16777215))
        typeLabel.setSizePolicy(labelSizePolicy)
        logTypeLayout.addWidget(typeLabel)
        self.logType = QComboBox(self)
        self.logType.setMinimumSize(QSize(100, 0))
        self.logType.setMaximumSize(QSize(150, 16777215))
        menuSizePolicy.setHeightForWidth(self.logType.sizePolicy().hasHeightForWidth())
        self.logType.setSizePolicy(menuSizePolicy)
        logTypeLayout.addWidget(self.logType)
        logTypeLayout.setStretch(1, 6)

        programLayout = QHBoxLayout()
        programLayout.setSpacing(0)

        programLabel = QLabel("Program:")
        programLabel.setMinimumSize(QSize(60, 0))
        programLabel.setMaximumSize(QSize(60, 16777215))
        programLabel.setSizePolicy(labelSizePolicy)
        programLayout.addWidget(programLabel)
        self.programName = QComboBox(self)
        self.programName.setMinimumSize(QSize(100, 0))
        self.programName.setMaximumSize(QSize(150, 16777215))
        menuSizePolicy.setHeightForWidth(self.programName.sizePolicy().hasHeightForWidth())
        self.programName.setSizePolicy(menuSizePolicy)
        programLayout.addWidget(self.programName)
        programLayout.setStretch(1, 6)

        # Initial instance allows adding additional menus, all following menus can only remove themselves.
        if self.initialInstance:
            self.logButton = QPushButton("+", self)
            self.logButton.setToolTip("Add logbook")
        else:
            self.logButton = QPushButton("-")
            self.logButton.setToolTip("Remove logbook")

        self.logButton.setMinimumSize(QSize(16, 16))  # 24x24
        self.logButton.setMaximumSize(QSize(16, 16))  # 24x24
        self.logButton.setObjectName("roundButton")
        # self.logButton.setAutoFillBackground(True)
        # region = QRegion(QRect(self.logButton.x()+15, self.logButton.y()+14, 20, 20), QRegion.Ellipse)
        # self.logButton.setMask(region)

        self.logButton.setStyleSheet("QPushButton {border-radius: 8px;}")

        self._logSelectLayout = QHBoxLayout()
        self._logSelectLayout.setSpacing(6)
        self._logSelectLayout.addLayout(logTypeLayout)
        self._logSelectLayout.addLayout(programLayout)
        self._logSelectLayout.addWidget(self.logButton)
        self._logSelectLayout.setStretch(0, 6)
        self._logSelectLayout.setStretch(1, 6)

Response: Create graphical objects for menus.


Instruction:
Can you create a Python 3 function that
displays menus and connect even signals.

Response:
def show(self):
        '''Display menus and connect even signals.'''
        self.parent.addLayout(self._logSelectLayout)
        self.menuCount += 1
        self._connectSlots()


Instruction:
Make a summary of the following Python 3 code
def addLogbooks(self, type=None, logs=[], default=""):
        '''Add or change list of logbooks.'''
        if type is not None and len(logs) != 0:
            if type in self.logList:
                for logbook in logs:
                    if logbook not in self.logList.get(type)[0]:
                        # print("Adding log " + " to " + type + " log type.")
                        self.logList.get(type)[0].append(logbook)
            else:
                # print("Adding log type: " + type)
                self.logList[type] = []
                self.logList[type].append(logs)

            # If default given, auto-select upon menu creation
            if len(self.logList[type]) > 1 and default != "":
                self.logList.get(type)[1] == default
            else:
                self.logList.get(type).append(default)

            self.logType.clear()
            self.logType.addItems(list(self.logList.keys()))
            self.changeLogType()

Response: Add or change list of logbooks.


Instruction:
Explain what the following Python 3 code does
def removeLogbooks(self, type=None, logs=[]):
        '''Remove unwanted logbooks from list.'''
        if type is not None and type in self.logList:
            if len(logs) == 0 or logs == "All":
                del self.logList[type]
            else:
                for logbook in logs:
                    if logbook in self.logList[type]:
                        self.logList[type].remove(logbook)

            self.changeLogType()

Response: Remove unwanted logbooks from list.


Instruction:
Write a Python 3 function that can
populate log program list to correspond with log type selection.

Response:
def changeLogType(self):
        '''Populate log program list to correspond with log type selection.'''
        logType = self.selectedType()
        programs = self.logList.get(logType)[0]
        default = self.logList.get(logType)[1]
        if logType in self.logList:
            self.programName.clear()
            self.programName.addItems(programs)
            self.programName.setCurrentIndex(programs.index(default))


Instruction:
Explain what the following Python 3 code does
def addMenu(self):
        '''Add menus to parent gui.'''
        self.parent.multiLogLayout.addLayout(self.logSelectLayout)
        self.getPrograms(logType, programName)

Response: Add menus to parent gui.


Instruction:
Can you tell what is the following Python 3 function doing
def removeLayout(self, layout):
        '''Iteratively remove graphical objects from layout.'''
        for cnt in reversed(range(layout.count())):
            item = layout.takeAt(cnt)
            widget = item.widget()
            if widget is not None:
                widget.deleteLater()
            else:
                '''If sublayout encountered, iterate recursively.'''
                self.removeLayout(item.layout())

Response: Remove graphical objects from layout.


Instruction:
Can you generate the documentation for the following Python 3 function
def addlabel(ax=None, toplabel=None, xlabel=None, ylabel=None, zlabel=None, clabel=None, cb=None, windowlabel=None, fig=None, axes=None):

    if (axes is None) and (ax is not None):
        axes = ax

    if (windowlabel is not None) and (fig is not None):
        fig.canvas.set_window_title(windowlabel)

    if fig is None:
        fig = _plt.gcf()

    if fig is not None and axes is None:
        axes = fig.get_axes()
        if axes == []:
            logger.error('No axes found!')

    if axes is not None:
        if toplabel is not None:
            axes.set_title(toplabel)
        if xlabel is not None:
            axes.set_xlabel(xlabel)
        if ylabel is not None:
            axes.set_ylabel(ylabel)
        if zlabel is not None:
            axes.set_zlabel(zlabel)

    if (clabel is not None) or (cb is not None):
        if (clabel is not None) and (cb is not None):
            cb.set_label(clabel)
        else:
            if clabel is None:
                logger.error('Missing colorbar label')
            else:
                logger.error('Missing colorbar instance')

Response: Adds labels to a plot.


Instruction:
Create a Python 3 function for
determining the URL corresponding to Python object

Response:
def linkcode_resolve(domain, info):
    """
    Determine the URL corresponding to Python object
    """
    if domain != 'py':
        return None

    modname = info['module']
    fullname = info['fullname']

    submod = sys.modules.get(modname)
    if submod is None:
        return None

    obj = submod
    for part in fullname.split('.'):
        try:
            obj = getattr(obj, part)
        except:
            return None

    try:
        fn = inspect.getsourcefile(obj)
    except:
        fn = None
    if not fn:
        return None

    try:
        source, lineno = inspect.getsourcelines(obj)
    except:
        lineno = None

    if lineno:
        linespec = "#L%d-L%d" % (lineno, lineno + len(source) - 1)
    else:
        linespec = ""

    fn = relpath(fn, start=dirname(scisalt.__file__))

    if 'dev' in scisalt.__version__:
        return "http://github.com/joelfrederico/SciSalt/blob/master/scisalt/%s%s" % (
            fn, linespec)
    else:
        return "http://github.com/joelfrederico/SciSalt/blob/v%s/scisalt/%s%s" % (
            scisalt.__version__, fn, linespec)


Instruction:
Make a summary of the following Python 3 code
def syncdb(args):
    cmd = args and 'syncdb %s' % ' '.join(options.args) or 'syncdb --noinput'
    call_manage(cmd)
    for fixture in options.paved.django.syncdb.fixtures:
        call_manage("loaddata %s" % fixture)

Response: Update the database with model schema. Shorthand for paver manage syncdb.


Instruction:
Explain what the following Python 3 code does
def start(info):
    cmd = options.paved.django.runserver

    if cmd == 'runserver_plus':
        try:
            import django_extensions
        except ImportError:
            info("Could not import django_extensions. Using default runserver.")
            cmd = 'runserver'

    port = options.paved.django.runserver_port
    if port:
        cmd = '%s %s' % (cmd, port)

    call_manage(cmd)

Response: Start the dev server.


Instruction:
Given the following Python 3 function, write the documentation
def schema(args):
    try:
        import south
        cmd = args and 'schemamigration %s' % ' '.join(options.args) or 'schemamigration'
        call_manage(cmd)
    except ImportError:
        error('Could not import south.')

Response: Run South s schemamigration command.


Instruction:
Given the following Python 3 function, write the documentation
def validate(cls, definition):
        '''
        This static method validates a BioMapMapper definition.
        It returns None on success and throws an exception otherwise.
        '''
        schema_path = os.path.join(os.path.dirname(__file__),
                                   '../../schema/mapper_definition_schema.json')
        with open(schema_path, 'r') as jsonfp:
            schema = json.load(jsonfp)
        # Validation of JSON schema
        jsonschema.validate(definition, schema)
        # Validation of JSON properties relations
        assert definition['main_key'] in definition['supported_keys'], \
               '\'main_key\' must be contained in \'supported_keys\''
        assert set(definition.get('list_valued_keys', [])) <= set(definition['supported_keys']), \
               '\'list_valued_keys\' must be a subset of \'supported_keys\''
        assert set(definition.get('disjoint', [])) <= set(definition.get('list_valued_keys', [])), \
               '\'disjoint\' must be a subset of \'list_valued_keys\''
        assert set(definition.get('key_synonyms', {}).values()) <= set(definition['supported_keys']), \
               '\'The values of the \'key_synonyms\' mapping must be in \'supported_keys\''

Response: This static method validates a BioMapMapper definition. It returns None on failure and throws an exception if the definition is invalid.


Instruction:
Write a Python 3 script for
returning all data entries for a particular key. Default is the main key.

Response:
def get_all(self, key=None):
        '''
        Returns all data entries for a particular key. Default is the main key.

        Args:

            key (str): key whose values to return (default: main key)

        Returns:

            List of all data entries for the key
        '''
        key = self.definition.main_key if key is None else key
        key = self.definition.key_synonyms.get(key, key)
        entries = self._get_all(key)
        if key in self.definition.scalar_nonunique_keys:
            return set(entries)
        return entries


Instruction:
Can you create a Python 3 function that
gets all the requests in the database.

Response:
def get_requests(self, params={}):
        """
        List requests

        http://dev.wheniwork.com/#listing-requests
        """
        if "status" in params:
            params['status'] = ','.join(map(str, params['status']))

        requests = []
        users = {}
        messages = {}
        params['page'] = 0
        while True:
            param_list = [(k, params[k]) for k in sorted(params)]
            url = "/2/requests/?%s" % urlencode(param_list)

            data = self._get_resource(url)
            for entry in data["users"]:
                user = Users.user_from_json(entry)
                users[user.user_id] = user
            for entry in data["requests"]:
                request = self.request_from_json(entry)
                requests.append(request)
            for entry in data["messages"]:
                message = Messages.message_from_json(entry)
                if message.request_id not in messages:
                    messages[message.request_id] = []
                messages[message.request_id].append(message)

            if not data['more']:
                break

            params['page'] += 1

        for request in requests:
            request.user = users.get(request.user_id, None)
            request.messages = messages.get(request.request_id, [])

        return requests


Instruction:
Write a Python 3 function for
making a guess of the hidden object to a set of digits.

Response:
def guess(self, *args):
        """
        Make a guess, comparing the hidden object to a set of provided digits. The digits should
        be passed as a set of arguments, e.g:

        * for a normal game: 0, 1, 2, 3
        * for a hex game: 0xA, 0xB, 5, 4
        * alternate for hex game: 'A', 'b', 5, 4

        :param args: An iterable of digits (int or str)
        :return: A dictionary object detailing the analysis and results of the guess
        """

        if self.game is None:
            raise ValueError("The Game is unexpectedly undefined!")

        response_object = {
            "bulls": None,
            "cows": None,
            "analysis": None,
            "status": None
        }

        if self.game.status == self.GAME_WON:
            response_object["status"] = \
                self._start_again_message("You already won!")
        elif self.game.status == self.GAME_LOST:
            response_object["status"] = \
                self._start_again_message("You already lost!")
        elif self.game.guesses_remaining < 1:
            response_object["status"] = \
                self._start_again_message("You've made too many guesses")
        else:
            guess_made = DigitWord(*args, wordtype=self.game.mode.digit_type)
            comparison = self.game.answer.compare(guess_made)

            self.game.guesses_made += 1
            response_object["bulls"] = 0
            response_object["cows"] = 0
            response_object["analysis"] = []

            for comparison_object in comparison:
                if comparison_object.match:
                    response_object["bulls"] += 1
                elif comparison_object.in_word:
                    response_object["cows"] += 1
                response_object["analysis"].append(comparison_object.get_object())

            if response_object["bulls"] == self.game.mode.digits:
                self.game.status = self.GAME_WON
                self.game.guesses_made = self.game.mode.guesses_allowed
                response_object["status"] = self._start_again_message(
                    "Congratulations, you win!"
                )
            elif self.game.guesses_remaining < 1:
                self.game.status = self.GAME_LOST
                response_object["status"] = self._start_again_message(
                    "Sorry, you lost!"
                )

        return response_object


Instruction:
Write a Python 3 function that can
load a game from a serialized JSON representation.

Response:
def load(self, game_json=None, mode=None):
        """
        Load a game from a serialized JSON representation. The game expects a well defined
        structure as follows (Note JSON string format):

        '{
            "guesses_made": int,
            "key": "str:a 4 word",
            "status": "str: one of playing, won, lost",
            "mode": {
                "digits": int,
                "digit_type": DigitWord.DIGIT | DigitWord.HEXDIGIT,
                "mode": GameMode(),
                "priority": int,
                "help_text": str,
                "instruction_text": str,
                "guesses_allowed": int
            },
            "ttl": int,
            "answer": [int|str0, int|str1, ..., int|strN]
        }'

        * "mode" will be cast to a GameMode object
        * "answer" will be cast to a DigitWord object

        :param game_json: The source JSON - MUST be a string
        :param mode: A mode (str or GameMode) for the game being loaded
        :return: A game object
        """

        if game_json is None:    # New game_json
            if mode is not None:
                if isinstance(mode, str):
                    _game_object = GameObject(mode=self._match_mode(mode=mode))
                elif isinstance(mode, GameMode):
                    _game_object = GameObject(mode=mode)
                else:
                    raise TypeError("Game mode must be a GameMode or string")
            else:
                _game_object = GameObject(mode=self._game_modes[0])
            _game_object.status = self.GAME_PLAYING
        else:
            if not isinstance(game_json, str):
                raise TypeError("Game must be passed as a serialized JSON string.")

            game_dict = json.loads(game_json)

            if not 'mode' in game_dict:
                raise ValueError("Mode is not provided in JSON; game_json cannot be loaded!")

            _mode = GameMode(**game_dict["mode"])
            _game_object = GameObject(mode=_mode, source_game=game_dict)

        self.game = copy.deepcopy(_game_object)


Instruction:
Write a Python 3 script to
load the game modes for the current locale.

Response:
def load_modes(self, input_modes=None):
        """
        Loads modes (GameMode objects) to be supported by the game object. Four default
        modes are provided (normal, easy, hard, and hex) but others could be provided
        either by calling load_modes directly or passing a list of GameMode objects to
        the instantiation call.

        :param input_modes: A list of GameMode objects; nb: even if only one new GameMode
        object is provided, it MUST be passed as a list - for example, passing GameMode gm1
        would require passing [gm1] NOT gm1.

        :return: A list of GameMode objects (both defaults and any added).
        """

        # Set default game modes
        _modes = [
            GameMode(
                mode="normal", priority=2, digits=4, digit_type=DigitWord.DIGIT, guesses_allowed=10
            ),
            GameMode(
                mode="easy", priority=1, digits=3, digit_type=DigitWord.DIGIT, guesses_allowed=6
            ),
            GameMode(
                mode="hard", priority=3, digits=6, digit_type=DigitWord.DIGIT, guesses_allowed=6
            ),
            GameMode(
                mode="hex", priority=4, digits=4, digit_type=DigitWord.HEXDIGIT, guesses_allowed=10
            )
        ]

        if input_modes is not None:
            if not isinstance(input_modes, list):
                raise TypeError("Expected list of input_modes")

            for mode in input_modes:
                if not isinstance(mode, GameMode):
                    raise TypeError("Expected list to contain only GameMode objects")
                _modes.append(mode)

        self._game_modes = copy.deepcopy(_modes)


Instruction:
Can you write a function in Python 3 where it
returns list of strings split by input delimeter

Response:
def line(self, line):
        """Returns list of strings split by input delimeter

        Argument:
        line - Input line to cut
        """
        # Remove empty strings in case of multiple instances of delimiter
        return [x for x in re.split(self.delimiter, line.rstrip()) if x != '']


Instruction:
How would you explain what the following Python 3 function does
def get_message(self, message_id):
        url = "/2/messages/%s" % message_id

        return self.message_from_json(self._get_resource(url)["message"])

Response: Get a specific message by its ID


Instruction:
Make a summary of the following Python 3 code
def get_messages(self, params={}):
        param_list = [(k, params[k]) for k in sorted(params)]
        url = "/2/messages/?%s" % urlencode(param_list)

        data = self._get_resource(url)
        messages = []
        for entry in data["messages"]:
            messages.append(self.message_from_json(entry))

        return messages

Response: Get all the messages from wheniwork.


Instruction:
Here you have a function in Python 3, explain what it does
def create_message(self, params={}):
        url = "/2/messages/"
        body = params

        data = self._post_resource(url, body)
        return self.message_from_json(data["message"])

Response: Creates a new message in the given params


Instruction:
Explain what the following Python 3 code does
def update_message(self, message):
        url = "/2/messages/%s" % message.message_id

        data = self._put_resource(url, message.json_data())
        return self.message_from_json(data)

Response: Modify an existing message.


Instruction:
Can you tell what is the following Python 3 function doing
def delete_messages(self, messages):
        url = "/2/messages/?%s" % urlencode([('ids', ",".join(messages))])

        data = self._delete_resource(url)
        return data

Response:
Delete existing messages.

        http://dev.wheniwork.com/#delete-existing-message


Instruction:
Can you write a function in Python 3 where it
returns site data. http://dev.wheniwork.com/#get-existing-site

Response:
def get_site(self, site_id):
        """
        Returns site data.

        http://dev.wheniwork.com/#get-existing-site
        """
        url = "/2/sites/%s" % site_id

        return self.site_from_json(self._get_resource(url)["site"])


Instruction:
Make a summary of the following Python 3 code
def get_sites(self):
        url = "/2/sites"

        data = self._get_resource(url)
        sites = []
        for entry in data['sites']:
            sites.append(self.site_from_json(entry))

        return sites

Response: Returns a list of sites.


Instruction:
Create a Python 3 function for
creating a site with the given parameters.

Response:
def create_site(self, params={}):
        """
        Creates a site

        http://dev.wheniwork.com/#create-update-site
        """
        url = "/2/sites/"
        body = params

        data = self._post_resource(url, body)
        return self.site_from_json(data["site"])


Instruction:
Write a Python 3 script to
return a link to a view that moves the passed in object up in rank.

Response:
def admin_link_move_up(obj, link_text='up'):
    """Returns a link to a view that moves the passed in object up in rank.

    :param obj:
        Object to move
    :param link_text:
        Text to display in the link.  Defaults to "up"
    :returns:
        HTML link code to view for moving the object
    """
    if obj.rank == 1:
        return ''

    content_type = ContentType.objects.get_for_model(obj)
    link = reverse('awl-rankedmodel-move', args=(content_type.id, obj.id,
        obj.rank - 1))

    return '%s' % (link, link_text)


Instruction:
Write a Python 3 script for
returning a link to a view that moves the passed in object down in rank.

Response:
def admin_link_move_down(obj, link_text='down'):
    """Returns a link to a view that moves the passed in object down in rank.

    :param obj:
        Object to move
    :param link_text:
        Text to display in the link.  Defaults to "down"
    :returns:
        HTML link code to view for moving the object
    """
    if obj.rank == obj.grouped_filter().count():
        return ''

    content_type = ContentType.objects.get_for_model(obj)
    link = reverse('awl-rankedmodel-move', args=(content_type.id, obj.id,
        obj.rank + 1))

    return '%s' % (link, link_text)


Instruction:
Implement a function in Python 3 to
show a figure with a typical orientation so that x and y axes are set up as expected.

Response:
def showfig(fig, aspect="auto"):
    """
    Shows a figure with a typical orientation so that x and y axes are set up as expected.
    """

    ax = fig.gca()

    # Swap y axis if needed
    alim = list(ax.axis())
    if alim[3] < alim[2]:
        temp    = alim[2]
        alim[2] = alim[3]
        alim[3] = temp
        ax.axis(alim)

    ax.set_aspect(aspect)
    fig.show()


Instruction:
How would you explain what the following Python 3 function does
def _setup_index(index):
    index = int(index)
    if index > 0:
        index -= 1
    elif index == 0:
        # Zero indicies should not be allowed by default.
        raise ValueError
    return index

Response: Sets up the index for the base base.


Instruction:
Explain what the following Python 3 code does
def cut(self, line):
        result = []
        line = self.line(line)

        for i, field in enumerate(self.positions):
            try:
                index = _setup_index(field)
                try:
                    result += line[index]
                except IndexError:
                    result.append(self.invalid_pos)
            except ValueError:
                result.append(str(field))
            except TypeError:
                result.extend(self._cut_range(line, int(field[0]), i))

        return ''.join(result)

Response:
Returns selected positions from cut input source in desired
        arrangement.


Instruction:
Write a Python 3 function that can
process the list of positions and ranges to account for ranges

Response:
def _setup_positions(self, positions):
        """Processes positions to account for ranges

        Arguments:
            positions -     list of positions and/or ranges to process
        """
        updated_positions = []

        for i, position in enumerate(positions):
            ranger = re.search(r'(?P<start>-?\d*):(?P<end>\d*)', position)

            if ranger:
                if i > 0:
                    updated_positions.append(self.separator)
                start = group_val(ranger.group('start'))
                end = group_val(ranger.group('end'))

                if start and end:
                    updated_positions.extend(self._extendrange(start, end + 1))
                # Since the number of positions on a line is unknown,
                # send input to cause exception that can be caught and call
                # _cut_range helper function
                elif ranger.group('start'):
                    updated_positions.append([start])
                else:
                    updated_positions.extend(self._extendrange(1, end + 1))
            else:
                updated_positions.append(positions[i])
                try:
                    if int(position) and int(positions[i+1]):
                        updated_positions.append(self.separator)
                except (ValueError, IndexError):
                    pass

        return updated_positions