Instruction:
Can you implement a function in Python 3 that
uploads a given component to pypi

Response:
def upload(dist_dir):
    """Upload a given component to pypi
    The pypi username and password must either be specified in a ~/.pypirc file
    or in environment variables PYPI_USER and PYPI_PASS
    """

    if 'PYPI_USER' in os.environ and 'PYPI_PASS' in os.environ:
        pypi_user = os.environ['PYPI_USER']
        pypi_pass = os.environ['PYPI_PASS']
    else:
        pypi_user = None
        pypi_pass = None
        print("No PYPI user information in environment")

    packages = glob.glob(dist_dir)

    # Invoke upload this way since subprocess call of twine cli has cross platform issues
    twine_upload(packages, 'pypi', False, None, pypi_user, pypi_pass, None, None, '~/.pypirc', False, None, None, None)


Instruction:
Explain what the following Python 3 code does
def simple_cmd():
    parser = argparse.ArgumentParser(
        prog="Simple command-line function toolkit.",
        description="""Input function name and args and kwargs.
        python xxx.py main -a 1 2 3 -k a=1,b=2,c=3""",
    )
    parser.add_argument("-f", "--func_name", default="main")
    parser.add_argument("-a", "--args", dest="args", nargs="*")
    parser.add_argument("-k", "--kwargs", dest="kwargs")
    parser.add_argument(
        "-i",
        "-s",
        "--info",
        "--show",
        "--status",
        dest="show",
        action="store_true",
        help="show the args, kwargs and function's source code.",
    )
    params = parser.parse_args()
    func_name = params.func_name
    func = globals().get(func_name)
    if not (callable(func)):
        Config.utils_logger.warning("invalid func_name: %s" % func_name)
        return
    args = params.args or []
    kwargs = params.kwargs or {}
    if kwargs:
        items = [re.split("[:=]", i) for i in re.split("[,;]+", kwargs)]
        kwargs = dict(items)
    if params.show:
        from inspect import getsource

        Config.utils_logger.info("args: %s; kwargs: %s" % (args, kwargs))
        Config.utils_logger.info(getsource(func))
        return
    func(*args, **kwargs)

Response: Simple command - line toolkit.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def print_mem(unit="MB"):
    try:
        import psutil

        B = float(psutil.Process(os.getpid()).memory_info().vms)
        KB = B / 1024
        MB = KB / 1024
        GB = MB / 1024
        result = vars()[unit]
        print_info("memory usage: %.2f(%s)" % (result, unit))
        return result
    except ImportError:
        print_info("pip install psutil first.")

Response: Show the proc - mem - cost with psutil use this only for lazinesssss.


Instruction:
Implement a function in Python 3 to
translate curl - string into dict of request.

Response:
def curlparse(string, encoding="utf-8"):
    """Translate curl-string into dict of request.
        :param string: standard curl-string, like `r'''curl ...'''`.
        :param encoding: encoding for post-data encoding.

    Basic Usage::

      >>> from torequests.utils import curlparse
      >>> curl_string = '''curl 'https://p.3.cn?skuIds=1&nonsense=1&nonce=0' -H 'Pragma: no-cache' -H 'DNT: 1' -H 'Accept-Encoding: gzip, deflate' -H 'Accept-Language: zh-CN,zh;q=0.9' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8' -H 'Cache-Control: no-cache' -H 'Referer: https://p.3.cn?skuIds=1&nonsense=1&nonce=0' -H 'Cookie: ASPSESSIONIDSQRRSADB=MLHDPOPCAMBDGPFGBEEJKLAF' -H 'Connection: keep-alive' --compressed'''
      >>> request_args = curlparse(curl_string)
      >>> request_args
      {'url': 'https://p.3.cn?skuIds=1&nonsense=1&nonce=0', 'headers': {'Pragma': 'no-cache', 'Dnt': '1', 'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'zh-CN,zh;q=0.9', 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8', 'Cache-Control': 'no-cache', 'Referer': 'https://p.3.cn?skuIds=1&nonsense=1&nonce=0', 'Cookie': 'ASPSESSIONIDSQRRSADB=MLHDPOPCAMBDGPFGBEEJKLAF', 'Connection': 'keep-alive'}, 'method': 'get'}
      >>> import requests
      >>> requests.request(**request_args)
      <Response [200]>
    """
    assert "\n" not in string, 'curl-string should not contain \\n, try r"...".'
    if string.startswith("http"):
        return {"url": string, "method": "get"}
    args, unknown = _Curl.parser.parse_known_args(shlex.split(string.strip()))
    requests_args = {}
    headers = {}
    requests_args["url"] = args.url
    for header in args.header:
        key, value = header.split(":", 1)
        headers[key.title()] = value.strip()
    if args.user_agent:
        headers["User-Agent"] = args.user_agent
    if headers:
        requests_args["headers"] = headers
    if args.user:
        requests_args["auth"] = tuple(u for u in args.user.split(":", 1) + [""])[:2]
    # if args.proxy:
    # pass
    data = args.data or args.data_binary or args.form
    if data:
        if data.startswith("$"):
            data = data[1:]
        args.method = "post"
        if "application/x-www-form-urlencoded" in headers.get("Content-Type", ""):
            data = dict(
                [
                    (i.split("=")[0], unquote_plus(i.split("=")[1]))
                    for i in data.split("&")
                ]
            )
            requests_args["data"] = data
        else:
            data = data.encode(encoding)
            requests_args["data"] = data
    requests_args["method"] = args.method.lower()
    if args.connect_timeout:
        requests_args["timeout"] = args.connect_timeout
    return requests_args


Instruction:
Can you tell what is the following Python 3 function doing
def slice_into_pieces(seq, n):
    length = len(seq)
    if length % n == 0:
        size = length // n
    else:
        size = length // n + 1
    for it in slice_by_size(seq, size):
        yield it

Response: Slice a sequence into n pieces return a generation of n pieces


Instruction:
Write a Python 3 function that can
slice a sequence into chunks with size.

Response:
def slice_by_size(seq, size):
    """Slice a sequence into chunks, return as a generation of chunks with `size`."""
    filling = null
    for it in zip(*(itertools_chain(seq, [filling] * size),) * size):
        if filling in it:
            it = tuple(i for i in it if i is not filling)
        if it:
            yield it


Instruction:
Create a Python 3 function to
translate timestamp into human - readable format.

Response:
def ttime(timestamp=None, tzone=None, fail="", fmt="%Y-%m-%d %H:%M:%S"):
    """Translate timestamp into human-readable: %Y-%m-%d %H:%M:%S.

    :param timestamp: the timestamp float, or `time.time()` by default.
    :param tzone: time compensation, int(-time.timezone / 3600) by default,
                (can be set with Config.TIMEZONE).
    :param fail: while raising an exception, return it.
    :param fmt: %Y-%m-%d %H:%M:%S, %z not work.
    :rtype: str

    >>> ttime()
    2018-03-15 01:24:35
    >>> ttime(1486572818.421858323)
    2017-02-09 00:53:38
    """
    tzone = Config.TIMEZONE if tzone is None else tzone
    fix_tz = tzone * 3600
    if timestamp is None:
        timestamp = time.time()
    else:
        timestamp = float(timestamp)
        if 1e12 <= timestamp < 1e13:
            # Compatible timestamp with 13-digit milliseconds
            timestamp = timestamp / 1000
    try:
        timestamp = time.time() if timestamp is None else timestamp
        return time.strftime(fmt, time.gmtime(timestamp + fix_tz))
    except:
        import traceback

        traceback.print_exc()
        return fail


Instruction:
Can you generate the documentation for the following Python 3 function
def ptime(timestr=None, tzone=None, fail=0, fmt="%Y-%m-%d %H:%M:%S"):
    tzone = Config.TIMEZONE if tzone is None else tzone
    fix_tz = -(tzone * 3600 + time.timezone)
    #: str(timestr) for datetime.datetime object
    timestr = str(timestr or ttime())
    try:
        return int(time.mktime(time.strptime(timestr, fmt)) + fix_tz)
    except:
        return fail

Response: Translate a string like 2018 - 03 - 15 01 - 27 - 56 into a timestamp.


Instruction:
Implement a function in Python 3 to
split seconds into a list of time units divisors and units.

Response:
def split_seconds(seconds):
    """Split seconds into [day, hour, minute, second, ms]

        `divisor: 1, 24, 60, 60, 1000`

        `units: day, hour, minute, second, ms`

    >>> split_seconds(6666666)
    [77, 3, 51, 6, 0]
    """
    ms = seconds * 1000
    divisors = (1, 24, 60, 60, 1000)
    quotient, result = ms, []
    for divisor in divisors[::-1]:
        quotient, remainder = divmod(quotient, divisor)
        result.append(quotient) if divisor == 1 else result.append(remainder)
    return result[::-1]


Instruction:
Can you create a Python 3 function that
translates seconds into human - readable form.

Response:
def timeago(seconds=0, accuracy=4, format=0, lang="en"):
    """Translate seconds into human-readable.

        :param seconds: seconds (float/int).
        :param accuracy: 4 by default (units[:accuracy]), determine the length of elements.
        :param format: index of [led, literal, dict].
        :param lang: en or cn.
        :param units: day, hour, minute, second, ms.

    >>> timeago(93245732.0032424, 5)
    '1079 days, 05:35:32,003'
    >>> timeago(93245732.0032424, 4, 1)
    '1079 days 5 hours 35 minutes 32 seconds'
    >>> timeago(-389, 4, 1)
    '-6 minutes 29 seconds 0 ms'
    """
    assert format in [0, 1, 2], ValueError("format arg should be one of 0, 1, 2")
    negative = "-" if seconds < 0 else ""
    seconds = abs(seconds)
    if lang == "en":
        units = ("day", "hour", "minute", "second", "ms")
    elif lang == "cn":
        units = (u"天", u"小时", u"分钟", u"秒", u"毫秒")
    times = split_seconds(seconds)
    if format == 2:
        return dict(zip(units, times))

    day, hour, minute, second, ms = times

    if format == 0:
        day_str = (
            "%d %s%s, " % (day, units[0], "s" if day > 1 and lang == "en" else "")
            if day
            else ""
        )
        mid_str = ":".join(("%02d" % i for i in (hour, minute, second)))
        if accuracy > 4:
            mid_str += ",%03d" % ms
        return negative + day_str + mid_str
    elif format == 1:
        # find longest valid fields index (non-zero in front)
        valid_index = 0
        for x, i in enumerate(times):
            if i > 0:
                valid_index = x
                break
        else:
            valid_index = x
        result_str = [
            "%d %s%s"
            % (num, unit, "s" if lang == "en" and num > 1 and unit != "ms" else "")
            for num, unit in zip(times, units)
        ][valid_index:][:accuracy]
        result_str = " ".join(result_str)
        return negative + result_str


Instruction:
Write a Python 3 script to
reverse conversion for parse_qs

Response:
def unparse_qs(qs, sort=False, reverse=False):
    """Reverse conversion for parse_qs"""
    result = []
    items = qs.items()
    if sort:
        items = sorted(items, key=lambda x: x[0], reverse=reverse)
    for keys, values in items:
        query_name = quote(keys)
        for value in values:
            result.append(query_name + "=" + quote(value))
    return "&".join(result)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def unparse_qsl(qsl, sort=False, reverse=False):
    result = []
    items = qsl
    if sort:
        items = sorted(items, key=lambda x: x[0], reverse=reverse)
    for keys, values in items:
        query_name = quote(keys)
        result.append(query_name + "=" + quote(values))
    return "&".join(result)

Response: Reverse conversion for parse_qsl


Instruction:
Given the following Python 3 function, write the documentation
def kill_after(seconds, timeout=2):
    pid = os.getpid()
    kill = os.kill
    run_after_async(seconds, kill, pid, signal.SIGTERM)
    run_after_async(seconds + timeout, kill, pid, 9)

Response: Kill self after seconds


Instruction:
Can you tell what is the following Python 3 function doing
def try_import(module_name, names=None, default=ImportErrorModule, warn=True):
    try:
        module = importlib.import_module(module_name)
    except ImportError:
        if warn:
            if warn is True:
                Config.utils_logger.warning(
                    "Module `%s` not found. Install it to remove this warning"
                    % module_name
                )
            else:
                warn(module_name, names, default)
        module = (
            ImportErrorModule(module_name) if default is ImportErrorModule else default
        )
    if not names:
        return module
    if not isinstance(names, (tuple, set, list)):
        names = [names]
    result = []
    for name in names:
        if hasattr(module, name):
            result.append(module.__getattribute__(name))
        else:
            result.append(
                ImportErrorModule("%s.%s" % (module_name, name))
                if default is ImportErrorModule
                else default
            )
    return result[0] if len(result) == 1 else result

Response: Try import module_name except ImportError and return default.


Instruction:
How would you explain what the following Python 3 function does
def ensure_request(request):
    if isinstance(request, dict):
        result = request
    elif isinstance(request, (unicode, str)):
        request = request.strip()
        if request.startswith("http"):
            result = {"method": "get", "url": request}
        elif request.startswith("curl "):
            result = curlparse(request)
    else:
        raise ValueError("request should be dict or str.")
    result["method"] = result.setdefault("method", "get").lower()
    return result

Response: Used for requests. request with ** ensure_request ( request )


Instruction:
Implement a function in Python 3 to
set the dict key as key. title ; keys should be str. Always be used to headers.

Response:
def ensure_dict_key_title(dict_obj):
    """Set the dict key as key.title(); keys should be str.
    Always be used to headers.

        >>> from torequests.utils import ensure_dict_key_title
        >>> ensure_dict_key_title({'hello-world':1, 'HELLOWORLD':2})
        {'Hello-World': 1, 'Helloworld': 2}
    """
    if not all((isinstance(i, unicode) for i in dict_obj.keys())):
        return dict_obj
    return {key.title(): value for key, value in dict_obj.items()}


Instruction:
Write a Python 3 function for
giving a seq of number return the median of the number.

Response:
def guess_interval(nums, accuracy=0):
    """Given a seq of number, return the median, only calculate interval >= accuracy.

    ::

        from torequests.utils import guess_interval
        import random

        seq = [random.randint(1, 100) for i in range(20)]
        print(guess_interval(seq, 5))
        # sorted_seq: [2, 10, 12, 19, 19, 29, 30, 32, 38, 40, 41, 54, 62, 69, 75, 79, 82, 88, 97, 99]
        # diffs: [8, 7, 10, 6, 13, 8, 7, 6, 6, 9]
        # median: 8
    """
    if not nums:
        return 0
    nums = sorted([int(i) for i in nums])
    if len(nums) == 1:
        return nums[0]
    diffs = [nums[i + 1] - nums[i] for i in range(len(nums) - 1)]
    diffs = [item for item in diffs if item >= accuracy]
    sorted_diff = sorted(diffs)
    result = sorted_diff[len(diffs) // 2]
    return result


Instruction:
How would you implement a function in Python 3 that
runs a function in background will not block main thread s exit.

Response:
def bg(func):
    """Run a function in background, will not block main thread's exit.(thread.daemon=True)
    ::

        from torequests.utils import bg, print_info
        import time

        def test1(n):
            time.sleep(n)
            print_info(n, 'done')

        @bg
        def test2(n):
            time.sleep(n)
            print_info(n, 'done')

        test3 = bg(test1)

        test2(1)
        test3(1)
        print_info('not be blocked')
        time.sleep(2)

        # [2018-06-12 23:46:19](L81): not be blocked
        # [2018-06-12 23:46:20](L81): 1 done
        # [2018-06-12 23:46:20](L81): 1 done
    """

    @wraps(func)
    def wrapper(*args, **kwargs):
        t = Thread(target=func, args=args, kwargs=kwargs)
        t.daemon = True
        t.start()
        return t

    return wrapper


Instruction:
Can you generate a brief explanation for the following Python 3 code
def countdown(
    seconds=None,
    block=True,
    interval=1,
    daemon=True,
    tick_callback=None,
    finish_callback=None,
):
    """Run a countdown function to wait something, similar to threading.Timer,
     but will show the detail tick by tick_callback.
     ::

        from torequests.utils import countdown

        countdown(3)
        # 3 2 1
        # countdown finished [3 seconds]: 2018-06-13 00:12:55 => 2018-06-13 00:12:58.
        countdown('2018-06-13 00:13:29')
        # 10 9 8 7 6 5 4 3 2 1
        # countdown finished [10 seconds]: 2018-06-13 00:13:18 => 2018-06-13 00:13:28.
"""

    def default_tick_callback(s, seconds, *args):
        flush_print(s, sep="", end=" ")

    def default_finish_callback(seconds, start_time):
        flush_print()

    def cd(seconds, interval):
        for s in range(seconds, 0, -interval):
            tick_callback(s, seconds, interval)
            time.sleep(interval)
        if callable(finish_callback):
            finish_callback(seconds, start_time)

    start_time = time.time()
    tick_callback = tick_callback or default_tick_callback
    finish_callback = (
        default_finish_callback if finish_callback is None else finish_callback
    )

    if unicode(seconds).isdigit():
        seconds = int(seconds)
    elif isinstance(seconds, (unicode, str)):
        seconds = int(ptime(seconds) - time.time())
    t = Thread(target=cd, args=(seconds, interval))
    t.daemon = daemon
    t.start()
    if block:
        t.join()

Response: Run a countdown function to wait something.


Instruction:
Make a summary of the following Python 3 code
def flush_print(*args, **kwargs):
    # PY2 raise SyntaxError for : def flush_print(*args, sep='', end=''):
    sep, end, flush = (
        kwargs.pop("sep", " "),
        kwargs.pop("end", "\n"),
        kwargs.pop("flush", 1),
    )
    string = sep.join((unicode(i) for i in args))
    sys.stdout.write("%s%s" % (string, end))
    if flush:
        sys.stdout.flush()

Response: Print a sequence of strings to stdout.


Instruction:
Implement a Python 3 function for
using tPool to request for curl string.

Response:
def curlrequests(curl_string, **kwargs):
    """Use tPool to request for curl string.
    If kwargs contains the req which hasattr request method, like req=requests.

    :param curl_string: standard curl string.
    :type curl_string: str
    :param kwargs: valid kwargs for tPool.
    :type curl_string: dict

    Basic Usage::

        from torequests.utils import curlrequests

        r = curlrequests('''curl 'http://p.3.cn/' -H 'Connection: keep-alive' -H 'Cache-Control: max-age=0' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36' -H 'DNT: 1' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8' -H 'Accept-Encoding: gzip, deflate' -H 'Accept-Language: zh-CN,zh;q=0.9,en;q=0.8' -H 'If-None-Match: "55dd9090-264"' -H 'If-Modified-Since: Wed, 26 Aug 2015 10:10:24 GMT' --compressed''', retry=1)
        print(r.text)
    """
    req = kwargs.pop('req', tPool())
    kwargs.update(curlparse(curl_string))
    return req.request(**kwargs)


Instruction:
Implement a Python 3 function for
registering one object which can be matched by regex.

Response:
def register(self, patterns, obj=None, instances=None, **reg_kwargs):
        """Register one object which can be matched/searched by regex.

        :param patterns: a list/tuple/set of regex-pattern.
        :param obj: return it while search/match success.
        :param instances: instance list will search/match the patterns.
        :param reg_kwargs: kwargs for re.compile.
        """
        assert obj, "bool(obj) should be True."
        patterns = patterns if isinstance(patterns, (list, tuple, set)) else [patterns]
        instances = instances or []
        instances = (
            instances if isinstance(instances, (list, tuple, set)) else [instances]
        )
        for pattern in patterns:
            pattern_compiled = re.compile(pattern, **reg_kwargs)
            self.container.append((pattern_compiled, obj, instances))
            if self.ensure_mapping:
                # check all instances to avoid one-to-many instances.
                self._check_instances()
            else:
                # no need to check all instances.
                for instance in instances:
                    assert self.search(instance) == [obj] or self.match(instance) == [
                        obj
                    ], (
                        "instance %s should fit at least one pattern %s"
                        % (instance, pattern)
                    )


Instruction:
Write a Python 3 script for
returning match or search result.

Response:
def find(self, string, default=None):
        """Return match or search result.

        :rtype: list"""
        return self.match(string) or self.search(string) or default


Instruction:
Make a summary of the following Python 3 code
def search(self, string, default=None):
        default = default if default else []
        result = [item[1] for item in self.container if item[0].search(string)]
        if self.ensure_mapping:
            assert len(result) < 2, "%s matches more than one pattern: %s" % (
                string,
                result,
            )
        return result if result else default

Response: Use re. search to find the result of search


Instruction:
Write a Python 3 script for
giving suggestion from all instances.

Response:
def fuzzy(self, key, limit=5):
        """Give suggestion from all instances."""
        instances = [i[2] for i in self.container if i[2]]
        if not instances:
            return
        instances = sum(instances, [])
        from fuzzywuzzy import process

        maybe = process.extract(key, instances, limit=limit)
        return maybe


Instruction:
Given the following Python 3 function, write the documentation
def show_all(self, as_string=True):
        result = []
        for item in self.container:
            pattern = str(item[0])[10:] if PY3 else item[0].pattern
            instances = item[2] or []
            value = (
                '%s "%s"' % (item[1].__name__, (item[1].__doc__ or ""))
                if callable(item[1])
                else str(item[1])
            )
            value = "%s %s" % (type(item[1]), value)
            result.append(" => ".join((pattern, ",".join(instances), value)))
        return "\n".join(result) if as_string else result

Response: returns a string with all the available items


Instruction:
Write a Python 3 script for
calling self. log_func and return expect_string.

Response:
def x(self):
        """Call self.log_func(self) and return expect_string."""
        self._log_after_del = False
        passed_string = self.string
        if self.log_func:
            self.log_func(self)
        else:
            print_info(
                "Timer [%(passed)s]: %(name)s, start at %(start)s."
                % (
                    dict(
                        name=self.name, start=ttime(self.start_at), passed=passed_string
                    )
                )
            )
        return passed_string


Instruction:
How would you explain what the following Python 3 function does
def tick(self):
        string = self.passed
        if self.rounding:
            string = round(string)
        if self.readable:
            string = self.readable(string)
        return string

Response: Return the time cost string as expect.


Instruction:
Make a summary of the following Python 3 code
def default_callback(self, text):
        text = text.replace("\r\n", "\n")
        text = "%s\n" % text
        flush_print(text, sep="", end="")
        return text

Response: Default clean the \\ n in text.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def watch(self, limit=None, timeout=None):
        start_time = time.time()
        count = 0
        while not timeout or time.time() - start_time < timeout:
            new = self.read()
            if new != self.temp:
                count += 1
                self.callback(new)
                if count == limit:
                    break
            self.temp = new
            time.sleep(self.interval)

Response: Block method to watch the clipboard changing.


Instruction:
Can you generate the documentation for the following Python 3 function
def watch_async(self, limit=None, timeout=None):
        return self.watch(limit=limit, timeout=timeout)

Response: Non - block method to watch the clipboard changing.


Instruction:
How would you code a function in Python 3 to
find one entry in the registry.

Response:
def find_one(cls, pattern, string, flags=0):
        """JS-like match object. Use index number to get groups, if not match or no group, will return ''.

        Basic Usage::

            >>> from torequests.utils import find_one
            >>> string = "abcd"
            >>> find_one("a.*", string)
            <torequests.utils.RegMatch object at 0x0705F1D0>
            >>> find_one("a.*", string)[0]
            'abcd'
            >>> find_one("a.*", string)[1]
            ''
            >>> find_one("a(.)", string)[0]
            'ab'
            >>> find_one("a(.)", string)[1]
            'b'
            >>> find_one("a(.)", string)[2] or "default"
            'default'
            >>> import re
            >>> item = find_one("a(B)(C)", string, flags=re.I | re.S)
            >>> item
            <torequests.utils.RegMatch object at 0x0705F1D0>
            >>> item[0]
            'abc'
            >>> item[1]
            'b'
            >>> item[2]
            'c'
            >>> item[3]
            ''
            >>> # import re
            >>> # re.findone = find_one
            >>> register_re_findone()
            >>> re.findone('a(b)', 'abcd')[1] or 'default'
            'b'

        """
        item = re.search(pattern, string, flags=flags)
        return cls(item)


Instruction:
Can you tell what is the following Python 3 function doing
def create_issue(title, body, repo, token):
    '''create a Github issue, given a title, body, repo, and token.

       Parameters
       ==========
       title: the issue title
       body: the issue body
       repo: the full name of the repo
       token: the user's personal Github token

    '''
    owner, name = repo.split('/')
    url = 'https://api.github.com/repos/%s/%s/issues' % (owner, name)

    data = {'title': title, 'body': body }

    headers = { "Authorization": "token %s" % token,
                "Accept": "application/vnd.github.symmetra-preview+json" }

    response = requests.post(url, data=json.dumps(data), headers=headers)

    if response.status_code in [201, 202]:
        url = response.json()['html_url']
        bot.info(url)
        return url

    elif response.status_code == 404:
        bot.error('Cannot create issue. Does your token have scope repo?')
        sys.exit(1)

    else:
        bot.error('Cannot create issue %s' %title)
        bot.error(response.content)
        sys.exit(1)

Response: create a Github issue given a title body repo and token


Instruction:
Explain what the following Python 3 code does
async def get_target(config, url):

    previous = config.cache.get(
        'target', url, schema_version=SCHEMA_VERSION) if config.cache else None

    headers = previous.caching if previous else None

    request = await utils.retry_get(config, url, headers=headers)
    if not request or not request.success:
        return previous

    if request.cached:
        return previous

    current = Target(request)

    if config.cache:
        config.cache.set('target', url, current)

    return current

Response: Given a URL get the webmention endpoint


Instruction:
Can you create a Python 3 function that
sends a webmention to this target from the specified entry.

Response:
async def send(self, config, entry):
        """ Send a webmention to this target from the specified entry """
        if self.endpoint:
            LOGGER.debug("%s -> %s", entry.url, self.url)
            try:
                await self.endpoint.send(config, entry.url, self.url)
            except Exception as err:  # pylint:disable=broad-except
                LOGGER.warning("Ping %s: got %s: %s",
                               self.url, err.__class__.__name__, err)


Instruction:
How would you implement a function in Python 3 that
returns a new Helper object based on the given name and quiet.

Response:
def get_helper(name=None, quiet=True, **kwargs):
    '''
       get the correct helper depending on the environment variable
       HELPME_CLIENT

       quiet: if True, suppress most output about the client (e.g. speak)

    '''
    # Second priority, from environment
    from helpme.defaults import HELPME_CLIENT

    # First priority, from command line
    if name is not None:
        HELPME_CLIENT = name

    # If no obvious credential provided, we can use HELPME_CLIENT
    if   HELPME_CLIENT == 'github': from .github import Helper;
    elif HELPME_CLIENT == 'uservoice': from .uservoice import Helper
    elif HELPME_CLIENT == 'discourse': from .discourse import Helper
    else: from .github import Helper

    Helper.name = HELPME_CLIENT
    Helper.quiet = quiet

    # Initialize the database
    return Helper()


Instruction:
Write a Python 3 script to
mock the BaBLEInterface class with some controllers inside.

Response:
def mock_bable(monkeypatch):
    """ Mock the BaBLEInterface class with some controllers inside. """
    mocked_bable = MockBaBLE()
    mocked_bable.set_controllers([
        Controller(0, '11:22:33:44:55:66', '#0'),
        Controller(1, '22:33:44:55:66:11', '#1', settings={'powered': True, 'low_energy': True}),
        Controller(2, '33:44:55:66:11:22', '#2', settings={'powered': True})
    ])

    monkeypatch.setattr(bable_interface, 'BaBLEInterface', lambda: mocked_bable)

    return mocked_bable


Instruction:
Implement a function in Python 3 to
execute the actual Stratum program.

Response:
def handle(self):
        """
        Executes the actual Stratum program.
        """
        self.output = PyStratumStyle(self.input, self.output)

        command = self.get_application().find('constants')
        ret = command.execute(self.input, self.output)
        if ret:
            return ret

        command = self.get_application().find('loader')
        ret = command.execute(self.input, self.output)
        if ret:
            return ret

        command = self.get_application().find('wrapper')
        ret = command.execute(self.input, self.output)

        self.output.writeln('')

        return ret


Instruction:
Implement a Python 3 function for
loading a python module from its absolute filesystem path

Response:
def load_from_file(module_path):
    """
    Load a python module from its absolute filesystem path

    Borrowed from django-cms
    """
    from imp import load_module, PY_SOURCE

    imported = None
    if module_path:
        with open(module_path, 'r') as openfile:
            imported = load_module('mod', openfile, module_path, ('imported', 'r', PY_SOURCE))
    return imported


Instruction:
How would you explain what the following Python 3 function does
def _reset_django(settings):
    if settings._wrapped != empty:
        clear_url_caches()
        from django.apps import apps
        apps.clear_cache()
        settings._wrapped = empty
        clear_url_caches()

Response: Hackish way to reset the django instance settings and AppConfig


Instruction:
Write a Python 3 function for
ensuring that the arguments in args_list are unicode and ensure that they are unique.

Response:
def ensure_unicoded_and_unique(args_list, application):
    """
    Iterate over args_list, make it unicode if needed and ensure that there
    are no duplicates.
    Returns list of unicoded arguments in the same order.
    """
    unicoded_args = []
    for argument in args_list:
        argument = (six.u(argument)
                    if not isinstance(argument, six.text_type) else argument)
        if argument not in unicoded_args or argument == application:
            unicoded_args.append(argument)
    return unicoded_args


Instruction:
Can you generate a brief explanation for the following Python 3 code
def rmtree(path):
    if islink(path):
        unlink(path)
    elif os.path.isdir(path):
        for sub in os.listdir(path):
            sub = os.path.join(path, sub)
            if os.path.isfile(sub):
                if not os.access(sub, os.W_OK):
                    # Is the error an access error?
                    os.chmod(sub, stat.S_IWUSR)
                os.unlink(sub)
            else:
                rmtree(sub)
        os.rmdir(path)
    else:
        raise RuntimeError("rmtree called on non-link/folder")

Response: This function removes the contents of the specified folder.


Instruction:
Implement a Python 3 function for
returning True if the path is a link.

Response:
def islink(path):
    """os.path.islink() but return True for junction points on Windows.
    """
    if platform.system() == "Windows":
        if sys.version_info[:2] < (3, 5):
            try:
                # pylint: disable=undefined-variable
                attrs = ctypes.windll.kernel32.GetFileAttributesW(unicode(path))  # noqa
                assert attrs != -1
                return bool(attrs & FILE_ATTRIBUTE_REPARSE_POINT)
            except (AttributeError, AssertionError):
                return False
        try:
            st = os.lstat(path)  # pylint: disable=invalid-name
        except (OSError, AttributeError):
            return False
        # pylint: disable=no-member
        return stat.S_ISLNK(st.st_mode) or st.st_file_attributes & stat.FILE_ATTRIBUTE_REPARSE_POINT
    return os.path.islink(path)


Instruction:
Make a summary of the following Python 3 code
def write_two_phases(filename, data, io):
        write_flag = True
        if os.path.exists(filename):
            with open(filename, 'r') as file:
                old_data = file.read()
                if data == old_data:
                    write_flag = False

        if write_flag:
            tmp_filename = filename + '.tmp'
            with open(tmp_filename, 'w+') as file:
                file.write(data)
            os.replace(tmp_filename, filename)
            io.text('Wrote: <fso>{0}</fso>'.format(filename))
        else:
            io.text('File <fso>{0}</fso> is up to date'.format(filename))

Response: Writes a file in two phases to the filesystem.


Instruction:
Create a Python 3 function to
get an entry from the cache and return it

Response:
async def get_entry(config, url):
    """ Given an entry URL, return the entry

    Arguments:

    config -- the configuration
    url -- the URL of the entry

    Returns: 3-tuple of (current, previous, updated) """

    previous = config.cache.get(
        'entry', url,
        schema_version=SCHEMA_VERSION) if config.cache else None

    headers = previous.caching if previous else None

    request = await utils.retry_get(config, url, headers=headers)
    if not request or not request.success:
        LOGGER.error("Could not get entry %s: %d", url,
                     request.status if request else -1)
        return None, previous, False

    # cache hit
    if request.cached:
        return previous, previous, False

    current = Entry(request)

    # Content updated
    if config.cache:
        config.cache.set('entry', url, current)

    return current, previous, (not previous
                               or previous.digest != current.digest
                               or previous.status != current.status)


Instruction:
Can you tell what is the following Python 3 function doing
def _check_rel(attrs, rel_whitelist, rel_blacklist):

        rels = attrs.get('rel', [None])

        if rel_blacklist:
            # Never return True for a link whose rel appears in the blacklist
            for rel in rels:
                if rel in rel_blacklist:
                    return False

        if rel_whitelist:
            # If there is a whitelist for rels, only return true for a rel that
            # appears in it
            for rel in rels:
                if rel in rel_whitelist:
                    return True
            # If there is a whitelist and we don't match, then reject
            return False

        return True

Response: Check a link s relations against the whitelist or blacklist.


Instruction:
Can you write a function in Python 3 where it
checks that a link is not on the same domain as the source URL.

Response:
def _domain_differs(self, href):
        """ Check that a link is not on the same domain as the source URL """
        target = utils.get_domain(href)
        if not target:
            return False

        origin = utils.get_domain(self.url)
        return target != origin


Instruction:
Write a Python 3 function for
giving an Entry object return all of the outgoing links.

Response:
def get_targets(self, config):
        """ Given an Entry object, return all of the outgoing links. """

        return {urllib.parse.urljoin(self.url, attrs['href'])
                for attrs in self._targets
                if self._check_rel(attrs, config.rel_whitelist, config.rel_blacklist)
                and self._domain_differs(attrs['href'])}


Instruction:
Can you tell what is the following Python 3 function doing
def run_command(self, config_file, sources):
        config = configparser.ConfigParser()
        config.read(config_file)

        rdbms = config.get('database', 'rdbms').lower()

        loader = self.create_routine_loader(rdbms)
        status = loader.main(config_file, sources)

        return status

Response: Run the command line.


Instruction:
Explain what the following Python 3 code does
def create_routine_loader(self, rdbms):
        # Note: We load modules and classes dynamically such that on the end user's system only the required modules
        #       and other dependencies for the targeted RDBMS must be installed (and required modules and other
        #       dependencies for the other RDBMSs are not required).

        if rdbms == 'mysql':
            module = locate('pystratum_mysql.MySqlRoutineLoader')
            return module.MySqlRoutineLoader(self.output)

        if rdbms == 'mssql':
            module = locate('pystratum_mssql.MsSqlRoutineLoader')
            return module.MsSqlRoutineLoader(self.output)

        if rdbms == 'pgsql':
            module = locate('pystratum_pgsql.PgSqlRoutineLoader')
            return module.PgSqlRoutineLoader(self.output)

        raise Exception("Unknown RDBMS '{0!s}'.".format(rdbms))

Response: Returns a Routine Loader object for loading stored routines into a RDBMS instance.


Instruction:
Given the following Python 3 function, write the documentation
async def create_vm(self, *, preset_name, image, flavor, security_groups=None,
                        userdata=None, key_name=None, availability_zone=None,
                        subnet=None):
        """
        Dummy create_vm func.
        """
        info = {
            'id': next(self._id_it),
            'name': preset_name,
            'ip': ['127.0.0.1'],
            'created': 0,
            'state': VmState.RUNNING,
            'flavor': flavor,
            'image': image,
            'metadata': {'test-meta': 'abctest'},
            'timed_shutdown_at': 1522753481,
            'tags': ['a-tag', 'b-tag', 'c-tag']
        }
        logging.debug('Prepare vm: %s', info)
        vm = Vm(self, **info)
        self._vms[vm.id] = vm
        logging.debug('Create: %s', vm)
        return None

Response: Create a new VM.


Instruction:
Given the following Python 3 function, write the documentation
async def list_vms(self, preset_name):
        return list(vm for vm in self._vms.values() if vm.name == preset_name)

Response: List all VMs in the specified preset.


Instruction:
Can you generate the documentation for the following Python 3 function
def suggest_filename(file_path, exists=None):
    import os.path
    import re
    if not isinstance(exists, bool):
        exists = os.path.exists(file_path)

    if exists:
        file_path, file_extension = os.path.splitext(file_path)
        # print(file_path)
        m = re.search(r"_\d+$", file_path)
        if m is None:
            # cislo = 2
            new_cislo_str = "_2"
        else:
            cislostr = (m.group())
            cislo = int(cislostr[1:]) + 1
            # it is normal number
            file_path = file_path[:-len(cislostr)]
            new_cislo_str = "_" + str(cislo)

        file_path = file_path + new_cislo_str + file_extension  # .zfill(2)
        # trorcha rekurze
        file_path = suggest_filename(file_path)

    return file_path

Response: Try if exist path and append number to its end.


Instruction:
Write a Python 3 script to
read object from file.

Response:
def obj_from_file(filename='annotation.yaml', filetype='auto'):
    ''' Read object from file '''

    if filetype == 'auto':
        _, ext = os.path.splitext(filename)
        filetype = ext[1:]

    if filetype in ('yaml', 'yml'):
        from ruamel.yaml import YAML
        yaml = YAML(typ="unsafe")
        with open(filename, encoding="utf-8") as f:
            obj = yaml.load(f)
        if obj is None:
            obj = {}
        # import yaml
        # with open(filename, encoding="utf-8") as f:
        #     intext = f.read()
        #     obj = yaml.load(intext)
    elif filetype in ('pickle', 'pkl', 'pklz', 'picklezip'):
        fcontent = read_pkl_and_pklz(filename)
        # import pickle
        if sys.version_info[0] < 3:
            import cPickle as pickle
        else:
            import _pickle as pickle
        # import sPickle as pickle
        if sys.version_info.major == 2:
            obj = pickle.loads(fcontent)
        else:
            obj = pickle.loads(fcontent, encoding="latin1")
    else:
        logger.error('Unknown filetype ' + filetype)
    return obj


Instruction:
Can you create a Python 3 function that
writes an object to a file.

Response:
def obj_to_file(obj, filename, filetype='auto', ndarray_to_list=False, squeeze=True):
    '''Writes annotation in file.

    :param filetype:
        auto
        yaml
        pkl, pickle
        pklz, picklezip
    :param ndarray_to_list: convert ndarrays in obj to lists
    :param squeeze: squeeze ndarray

    '''
    # import json
    # with open(filename, mode='w') as f:
    #    json.dump(annotation,f)
    if ndarray_to_list:
        obj = ndarray_to_list_in_structure(obj, squeeze=squeeze)

    # write to yaml
    d = os.path.dirname(os.path.abspath(filename))
    if not os.path.exists(d):
        os.makedirs(d)

    if filetype == 'auto':
        _, ext = os.path.splitext(filename)
        filetype = ext[1:]

    if filetype in ('yaml', 'yml'):
        # import yaml
        from ruamel.yaml import YAML
        yaml = YAML(typ="unsafe")
        with open(filename, 'wt', encoding="utf-8") as f:
            yaml.dump(obj, f)
        # if sys.version_info.major == 2:
        #     with open(filename, 'wb') as f:
        #         yaml.dump(obj, f, encoding="utf-8")
        # else:
        #     with open(filename, "w", encoding="utf-8") as f:
        #         yaml.dump(obj, f)
    elif filetype in ('pickle', 'pkl'):
        f = open(filename, 'wb')
        logger.info("filename " + filename)
        # if sys.version_info[0] < 3: import cPickle as pickle
        # else: import _pickle as pickle
        import pickle
        pickle.dump(obj, f, -1)
        f.close
    elif filetype in ('streamingpicklezip', 'spklz'):
        # this is not working :-(
        import gzip
        import sPickle as pickle
        f = gzip.open(filename, 'wb', compresslevel=1)
        # f = open(filename, 'wb')
        pickle.s_dump(obj, f)
        f.close
    elif filetype in ('picklezip', 'pklz'):
        import gzip
        if sys.version_info[0] < 3: import cPickle as pickle
        else: import _pickle as pickle
        f = gzip.open(filename, 'wb', compresslevel=1)
        # f = open(filename, 'wb')
        pickle.dump(obj, f)
        f.close
    elif filetype in('mat'):

        import scipy.io as sio
        sio.savemat(filename, obj)
    else:
        logger.error('Unknown filetype ' + filetype)


Instruction:
Can you generate the documentation for the following Python 3 function
def resize_to_shape(data, shape, zoom=None, mode='nearest', order=0):
    # @TODO remove old code in except part

    try:
        # rint 'pred vyjimkou'
        # aise Exception ('test without skimage')
        # rint 'za vyjimkou'
        import skimage
        import skimage.transform
# Now we need reshape  seeds and segmentation to original size

        segm_orig_scale = skimage.transform.resize(
            data, shape, order=0,
            preserve_range=True,
            mode="constant",
        )

        segmentation = segm_orig_scale
        logger.debug('resize to orig with skimage')
    except:
        import scipy
        import scipy.ndimage
        dtype = data.dtype
        if zoom is None:
            zoom = shape / np.asarray(data.shape).astype(np.double)

        segm_orig_scale = scipy.ndimage.zoom(
            data,
            1.0 / zoom,
            mode=mode,
            order=order
        ).astype(dtype)
        logger.debug('resize to orig with scipy.ndimage')

# @TODO odstranit hack pro oříznutí na stejnou velikost
# v podstatě je to vyřešeno, ale nechalo by se to dělat elegantněji v zoom
# tam je bohužel patrně bug
        # rint 'd3d ', self.data3d.shape
        # rint 's orig scale shape ', segm_orig_scale.shape
        shp = [
            np.min([segm_orig_scale.shape[0], shape[0]]),
            np.min([segm_orig_scale.shape[1], shape[1]]),
            np.min([segm_orig_scale.shape[2], shape[2]]),
        ]
        # elf.data3d = self.data3d[0:shp[0], 0:shp[1], 0:shp[2]]
        # mport ipdb; ipdb.set_trace() # BREAKPOINT

        segmentation = np.zeros(shape, dtype=dtype)
        segmentation[
            0:shp[0],
            0:shp[1],
            0:shp[2]] = segm_orig_scale[0:shp[0], 0:shp[1], 0:shp[2]]

        del segm_orig_scale
    return segmentation

Response: Function resize input data to specific shape.


Instruction:
Create a Python 3 function for
functioning can resize data3d or segmentation to specified voxelsize mm

Response:
def resize_to_mm(data3d, voxelsize_mm, new_voxelsize_mm, mode='nearest'):
    """
    Function can resize data3d or segmentation to specifed voxelsize_mm
    :new_voxelsize_mm: requested voxelsize. List of 3 numbers, also
        can be a string 'orig', 'orgi*2' and 'orgi*4'.

    :voxelsize_mm: size of voxel
    :mode: default is 'nearest'
    """
    import scipy
    import scipy.ndimage

    if np.all(list(new_voxelsize_mm) == 'orig'):
        new_voxelsize_mm = np.array(voxelsize_mm)
    elif np.all(list(new_voxelsize_mm) == 'orig*2'):
        new_voxelsize_mm = np.array(voxelsize_mm) * 2
    elif np.all(list(new_voxelsize_mm) == 'orig*4'):
        new_voxelsize_mm = np.array(voxelsize_mm) * 4
        # vx_size = np.array(metadata['voxelsize_mm']) * 4

    zoom = voxelsize_mm / (1.0 * np.array(new_voxelsize_mm))
    data3d_res = scipy.ndimage.zoom(
        data3d,
        zoom,
        mode=mode,
        order=1
    ).astype(data3d.dtype)
    return data3d_res


Instruction:
Explain what the following Python 3 code does
def suits_with_dtype(mn, mx, dtype):
    type_info = np.iinfo(dtype)
    if mx <= type_info.max and mn >= type_info.min:
        return True
    else:
        return False

Response: Check whether the given range of values can be stored into defined data type.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def use_economic_dtype(data3d, slope=1, inter=0, dtype=None):

    if dtype is None:
        dtype = data3d.dtype
        if issubclass(dtype.type, np.integer):
            mn = data3d.min() * slope + inter
            mx = data3d.max() * slope + inter
            if suits_with_dtype(mn, mx, dtype=np.uint8):
                dtype = np.uint8
            elif suits_with_dtype(mn, mx, dtype=np.int8):
                dtype = np.int8
            elif suits_with_dtype(mn, mx, dtype=np.uint16):
                dtype = np.uint16
            elif suits_with_dtype(mn, mx, dtype=np.int16):
                dtype = np.int16
            elif suits_with_dtype(mn, mx, dtype=np.uint32):
                dtype = np.uint32
            elif suits_with_dtype(mn, mx, dtype=np.int32):
                dtype = np.int32

    # new_data3d = ((np.float(slope) * data3d) + np.float(inter)).astype(dtype)
    if slope == 1 and inter == 0:
        # this can prevent out of memmory
        new_data3d = data3d.astype(dtype)
    else:
        new_data3d = ((slope * data3d) + inter).astype(dtype)
    return new_data3d

Response: Use more economic integer - like dtype if it is possible.


Instruction:
Given the following Python 3 function, write the documentation
def get_sitk_image_from_ndarray(data3d):

    import SimpleITK as sitk
    rescale_intercept = None
    if sitk.Version.MajorVersion() > 0:
        if data3d.dtype == np.int8:
            rescale_intercept = -2**7
            data3d = (data3d - rescale_intercept).astype(np.uint8)
        elif data3d.dtype == np.int16:
            # simpleitk is not able to store this. It uses only 11 bites
            # rescale_intercept = -2**15
            rescale_intercept = -2**10
            data3d = (data3d - rescale_intercept).astype(np.uint16)
        elif data3d.dtype == np.int32:
            rescale_intercept = -2**31
            data3d = (data3d - rescale_intercept).astype(np.uint16)

    dim = sitk.GetImageFromArray(data3d)
    if sitk.Version.MajorVersion() > 0:
        if rescale_intercept is not None:
            # rescale slope (0028|1053), rescale intercept (0028|1052)
            dim.SetMetaData("0028|1052", str(rescale_intercept))
            dim.SetMetaData("0028|1053", "1")

    return dim

Response: This function returns the SimpleItk Image object from the data array.


Instruction:
Given the following Python 3 function, write the documentation
def get_slope_and_intercept_from_pdcm(dcmdata):

    if hasattr(dcmdata, "RescaleSlope") and hasattr(dcmdata, "RescaleIntercept"):
        rescale_slope = dcmdata.RescaleSlope
        rescale_intercept = dcmdata.RescaleIntercept
        slope, inter = get_slope_and_intercept_from_strings(rescale_slope, rescale_intercept)
    else:
        slope = 1
        inter = 0

    return slope, inter

Response: Get slope and intercept from pydicom file object.


Instruction:
Create a Python 3 function to
split an integer into evenly distributed list of size n

Response:
def split_evenly(n, chunks):
    """Split an integer into evenly distributed list

    >>> split_evenly(7, 3)
    [3, 2, 2]

    >>> split_evenly(12, 3)
    [4, 4, 4]

    >>> split_evenly(35, 10)
    [4, 4, 4, 4, 4, 3, 3, 3, 3, 3]

    >>> split_evenly(1, 2)
    Traceback (most recent call last):
        ...
    ChunkingError: Number of chunks is greater than number

    """
    if n < chunks:
        raise ChunkingError("Number of chunks is greater than number")
    if n % chunks == 0:
        # Either we can evenly split or only 1 chunk left
        return [n / chunks] * chunks
    # otherwise the current chunk should be a bit larger
    max_size = n / chunks + 1
    return [max_size] + split_evenly(n - max_size, chunks - 1)


Instruction:
Can you generate the documentation for the following Python 3 function
def check_env(self, envar, value):
        '''ensure that variable envar is set to some value,
           otherwise exit on error.

           Parameters
           ==========
           envar: the environment variable name
           value: the setting that shouldn't be None
        '''
        if value is None:
            bot.error('You must export %s to use Discourse' % envar)
            print('https://vsoch.github.io/helpme/helper-discourse')
            sys.exit(1)

Response: ensure that variable envar is set to some value


Instruction:
How would you code a function in Python 3 to
generate a keypair on behalf of the user

Response:
def _generate_keys(self):
        '''the discourse API requires the interactions to be signed, so we
           generate a keypair on behalf of the user
        '''
        from helpme.defaults import HELPME_CLIENT_SECRETS
        keypair_dir = os.path.join(os.path.dirname(HELPME_CLIENT_SECRETS),
                                   'discourse')

        # Have we generated a keypair file before?
        self.keypair_file = os.path.join(keypair_dir, 'private.pem')

        # We likely won't have generated it on first use!
        if not hasattr(self, 'key'):
            self.key = generate_keypair(self.keypair_file)

        # If we generated the keypair file, we will have already loaded the key
        if not hasattr(self, 'public_key'):
            self.public_key = load_keypair(self.keypair_file)


Instruction:
Make a summary of the following Python 3 code
def _submit(self):
        '''submit the question to the board. When we get here we should have
           (under self.data)

                {'record_environment': [('DISPLAY', ':0')],
                 'user_prompt_board': 'http://127.0.0.1',
                 'user_prompt_issue': 'I want to know why dinosaurs are so great!',
                 'user_prompt_title': 'Why are dinosaurs so great?'}

           self.token should be propogated with the personal access token
        '''
        body = self.data['user_prompt_issue']
        title = self.data['user_prompt_title']
        board = self.data['user_prompt_board']
        username = self.data['user_prompt_username']
        category = self.data['user_prompt_category']

        # Step 1: Token
        if self.token == None:
            self.token = self.request_token(board)
            self._get_and_update_setting('HELPME_DISCOURSE_TOKEN', self.token)

        # Step 1: Environment

        envars = self.data.get('record_environment')
        body = body + envars_to_markdown(envars)

        # Step 2: Asciinema

        asciinema = self.data.get('record_asciinema')
        if asciinema not in [None, '']:
            url = upload_asciinema(asciinema)

            # If the upload is successful, add a link to it.

            if url is not None:
                body += "\n[View Asciinema Recording](%s)" % url

        # Add other metadata about client

        body += "\n\ngenerated by [HelpMe](https://vsoch.github.io/helpme/)"
        body += "\nHelpMe Discourse Id: %s" %(self.run_id)

        # Submit the issue

        post = self.create_post(title, body, board, category, username)
        return post

Response: Submit the question to the board.


Instruction:
Can you write a function in Python 3 where it
retrieves an object which id_field matches id_val.

Response:
def get(self, cls, id_field, id_val):
        """
        Retrieve an object which `id_field` matches `id_val`. If it exists in
        the cache, it will be fetched from Redis. If not, it will be fetched
        via the `fetch` method and cached in Redis (unless the cache flag got
        invalidated in the meantime).
        """
        cache_key, flag_key = self.get_keys(cls, id_field, id_val)

        result = self.get_cached_or_set_flag(keys=(cache_key, flag_key))

        # in Lua, arrays cannot hold nil values, so e.g. if [1, nil] is returned,
        # we'll only get [1] here. That's why we need to append None ourselves.
        if len(result) == 1:
            result.append(None)

        previous_flag, cached_data = result

        # if cached data was found, deserialize and return it
        if cached_data is not None:
            deserialized = self.deserialize(cls, cached_data)

            # verify that the cached object matches our expectations
            # if not, return from the persistant storage instead.
            if self.verify(cls, id_field, id_val, deserialized):
                return deserialized
            else:
                # invalidate the cache if it didn't pass verification
                self.invalidate(cls, id_field, id_val)

        obj = self.fetch(cls, id_field, id_val)

        # If the flag wasn't previously set, then we set it and we're responsible
        # for putting the item in the cache. Do this unless the cache got
        # invalidated and the flag was removed.
        if not previous_flag:
            obj_serialized = self.serialize(obj)
            self.cache(keys=(cache_key, flag_key), args=(obj_serialized,))

        return obj


Instruction:
How would you implement a function in Python 3 that
verifies that the object we retrieved from cache matches the requested ID.

Response:
def verify(self, cls, id_field, id_val, obj_from_cache):
        """
        Verify that the object we retrieved from cache matches the requested
        `id_field`/`id_val`.
        """
        return getattr(obj_from_cache, id_field) == id_val


Instruction:
Write a Python 3 function for
invalidating the cache for a given Mongo object.

Response:
def invalidate(self, cls, id_field, id_val):
        """
        Invalidate the cache for a given Mongo object by deleting the cached
        data and the cache flag.
        """
        cache_key, flag_key = self.get_keys(cls, id_field, id_val)

        pipeline = self.redis.pipeline()
        pipeline.delete(cache_key)
        pipeline.delete(flag_key)
        pipeline.execute()


Instruction:
Create a Python 3 function to
update the state of the NukiLock object.

Response:
def update(self, aggressive=False):
        """
        Update the state of the NukiLock
        :param aggressive: Whether to aggressively poll the Bridge. If set to
        True, this will actively query the Lock, thus using more battery.
        :type aggressive: bool
        """
        if aggressive:
            data = self._bridge.lock_state(self.nuki_id)
            if not data['success']:
                logger.warning(
                'Failed to update the state of lock {}'.format(self.nuki_id)
            )
            self._json.update({k: v for k, v in data.items() if k != 'success'})
        else:
            data = [l for l in self._bridge.locks if l.nuki_id == self.nuki_id]
            assert data, (
                   'Failed to update data for lock. '
                   'Nuki ID {} volatized.'.format(self.nuki_id))
            self._json.update(data[0]._json)


Instruction:
Can you implement a function in Python 3 that
returns a tuple containing the item properties.

Response:
def get_dict_properties(item, fields, mixed_case_fields=[], formatters={}):
    """Return a tuple containing the item properties.
    :param item: a single dict resource
    :param fields: tuple of strings with the desired field names
    :param mixed_case_fields: tuple of field names to preserve case
    :param formatters: dictionary mapping field names to callables
       to format the values
    """
    row = []

    for field in fields:
        if field in mixed_case_fields:
            field_name = field.replace(' ', '_')
        else:
            field_name = field.lower().replace(' ', '_')
        data = item[field_name] if field_name in item else ''
        if field in formatters:
            row.append(formatters[field](data))
        else:
            row.append(data)
    return tuple(row)


Instruction:
Here you have a function in Python 3, explain what it does
def log_method(log, level=logging.DEBUG):

    def decorator(func):
        func_name = func.__name__

        @six.wraps(func)
        def wrapper(self, *args, **kwargs):
            if log.isEnabledFor(level):
                pretty_args = []
                if args:
                    pretty_args.extend(str(a) for a in args)
                if kwargs:
                    pretty_args.extend(
                        "%s=%s" % (k, v) for k, v in six.iteritems(kwargs))
                log.log(level, "%s(%s)", func_name, ", ".join(pretty_args))
            return func(self, *args, **kwargs)

        return wrapper

    return decorator

Response: A decorator that logs a method and its arguments when entered.


Instruction:
Explain what the following Python 3 code does
def check_local() -> None:
    to_check = ['./replay', './replay/toDo', './replay/archive']

    for i in to_check:
        if not os.path.exists(i):
            os.makedirs(i)

Response: Checks that the required directories exist.


Instruction:
Explain what the following Python 3 code does
def create_file(project: str, environment: str, feature: str, state: str) -> None:
    check_local()
    save_path = './replay/toDo/'
    filename = '{0}.txt'.format(str(uuid.uuid1()))
    complete_name = os.path.join(save_path, filename)

    with open(complete_name, 'w') as filename:
        filename.write('rc update-ld-api -p {0} -e {1} -f {2} -s {3}'.format(
            project,
            environment,
            feature,
            state
        ))

Response: Create file to replay.


Instruction:
Write a Python 3 function for
executing all commands in replay and move them to the replay archive directory.

Response:
def execute_replay() -> None:
    """
    Execute all commands.

    For every command that is found in replay/toDo, execute each of them
    and move the file to the replay/archive directory.
    """
    files = glob.glob('./replay/toDo/*')
    sorted_files = sorted(files, key=os.path.getctime)

    if not sorted_files:  # list is not empty
        LOG.debug('Found %s, beginning execution.', sorted_files)
        for command_file in sorted_files:
            with open(command_file, 'r') as command:
                cmd = command.read()
                LOG.debug('executing command: %s', cmd)
                resp = run([cmd, '-v', 'DEBUG'], shell=True, check=True)
                LOG.debug(resp)
                LOG.debug('moving %s to archive', command.name)
                move_command = 'mv {0} ./replay/archive/'.format(command.name)
                run(move_command, shell=True, check=True)
        LOG.info('LaunchDarkly is now up to date.')
    else:
        LOG.warning('No files found, nothing to replay.')


Instruction:
Implement a Python 3 function for
asking the user for a prompt and only return when one of the requested options is provided.

Response:
def choice_prompt(prompt, choices=None, choice=None):
    '''Ask the user for a prompt, and only return when one of the requested
       options is provided.

       Parameters
       ==========
       prompt: the prompt to ask the user
       choices: a list of choices that are valid, defaults to [Y/N/y/n]

    '''
    if not choices:
        choices = ["y", "n", "Y", "N"]

    print(prompt)
    get_input = getattr(__builtins__, 'raw_input', input)
    pretty_choices = '/'.join(choices)
    message = 'Please enter your choice [%s] : ' %(pretty_choices)
    while choice not in choices:
        choice = get_input(message).strip()

        # If the option isn't valid, this is shown next
        message = "Please enter a valid option in [%s]" %(pretty_choices)
    return choice


Instruction:
Write a Python 3 script to
ask the user for a text entry that matches a regular expression.

Response:
def regexp_prompt(prompt, regexp='.', answer=''):
    '''Ask the user for a text entry that matches a regular expression

       Parameters
       ==========
       prompt: the prompt to ask the user
       regexp: the regular expression to match. defaults to anything.

    '''
    get_input = getattr(__builtins__, 'raw_input', input)
    while not re.search(regexp, answer):
        answer = get_input(prompt + ': ').strip()
        # If the option isn't valid, this is shown next
        message = "Your entry must match the regular expression %s" %regexp

    return answer


Instruction:
Explain what the following Python 3 code does
def which(software, strip_newline=True):
    '''get_install will return the path to where an executable is installed.
    '''
    if software is None:
        software = "singularity"
    cmd = ['which', software ]
    try:
        result = run_command(cmd)
        if strip_newline is True:
            result['message'] = result['message'].strip('\n')
        return result

    except: # FileNotFoundError
        return None

Response: get_install will return the path to where an executable is installed.


Instruction:
Given the following Python 3 function, write the documentation
def guess_encoding(request):
    ctype = request.headers.get('content-type')
    if not ctype:
        # we don't have a content-type, somehow, so...
        LOGGER.warning("%s: no content-type; headers are %s",
                       request.url, request.headers)
        return 'utf-8'

    # explicit declaration
    match = re.search(r'charset=([^ ;]*)(;| |$)', ctype)
    if match:
        return match[1]

    # html default
    if ctype.startswith('text/html'):
        return 'iso-8859-1'

    # everything else's default
    return 'utf-8'

Response: Try to guess the encoding of a request without going through the slow chardet process


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _make_headers(config, kwargs):

    headers = kwargs.get('headers')
    headers = headers.copy() if headers is not None else {}
    headers['User-Agent'] = config.args.user_agent

    kwargs = kwargs.copy()
    kwargs['headers'] = headers
    return kwargs

Response: Replace the kwargs with one where the headers include our user - agent


Instruction:
Here you have a function in Python 3, explain what it does
async def retry_get(config, url, *args, **kwargs):
    return await _retry_do(config.session.get, url, *args,
                           **_make_headers(config, kwargs))

Response: Aiohttp wrapper for GET


Instruction:
Can you generate the documentation for the following Python 3 function
async def retry_post(config, url, *args, **kwargs):
    return await _retry_do(config.session.post, url, *args,
                           **_make_headers(config, kwargs))

Response: Aiohttp wrapper for POST


Instruction:
Implement a function in Python 3 to
run the entire helper procedure

Response:
def run(self, positionals=None):
        '''run the entire helper procedure, including:

             - start: initialize the helper, collection preferences
             - record: record any relevant features for the environment / session
             - interact: interact with the user for additional informatoin
             - submit: submit the completed request

             Each of the above functions for a helper can determine global
             collection preferences from the system helpme.cfg in the module
             root. After performing global actions, each function then calls
             a class specific function of the same name (e.g., start calls
             _start) that is implemented by the helper class to do custom
             operations for the helper.
        '''
        # Step 0: Each run session is given a fun name
        self.run_id = RobotNamer().generate()

        # Step 1: get config steps
        steps = self.config._sections[self.name]

        # Step 2: Start the helper (announce and run start, which is init code)
        self.start(positionals)

        # Step 3: Iterate through flow, check each step for known record/prompt,
        #         and collect outputs appropriately

        for step, content in steps.items():
            self.collect(step, content)

        # Step 4: When data collected, pass data structures to submit
        self.submit()


Instruction:
Can you tell what is the following Python 3 function doing
def start(self, positionals=None):
        '''start the helper flow. We check helper system configurations to
           determine components that should be collected for the submission.
           This is where the client can also pass on any extra (positional)
           arguments in a list from the user.
        '''
        bot.info('[helpme|%s]' %(self.name))
        self.speak()
        self._start(positionals)

Response: start the helper flow


Instruction:
How would you code a function in Python 3 to
collect the required metadata from the user

Response:
def collect(self, step, content):
        '''given a name of a configuration key and the provided content, collect
           the required metadata from the user.

           Parameters
           ==========
           step: the key in the configuration. Can be one of:
                   user_message_<name>
                   runtime_arg_<name>
                   record_asciinema
                   record_environment
                   user_prompt_<name>
           content: the default value or boolean to indicate doing the step.
        '''

        # Option 1: The step is just a message to print to the user
        if step.startswith('user_message'):
            print(content)

        # Option 2: The step is to collect a user prompt (if not at runtime)
        elif step.startswith('user_prompt'):
            self.collect_argument(step, content)

        # Option 3: The step is to record an asciinema!
        elif step == 'record_asciinema':
            self.record_asciinema()

        # Option 4: Record the user environment
        elif step == "record_environment":
            self.record_environment()

        bot.debug(self.data)


Instruction:
Write a Python 3 script for
collecting the runtime argument if the key is not found in the configuration

Response:
def collect_argument(self, step, message):
        '''given a key in the configuration, collect the runtime argument if
           provided. Otherwise, prompt the user for the value.

           Parameters
           ==========
           step: the name of the step, should be 'runtime_arg_<name>'
           message: the content of the step, the message to show the user if the
                    argument <name> is not found under args.

        '''
        if step not in self.data:
            self.data[step] = regexp_prompt(message)


Instruction:
Can you generate the documentation for the following Python 3 function
def record_environment(self):
        '''collect a limited set of environment variables based on the list
           under record_envirionment in the configuration file.
        '''

        # whitelist is a newline separated list under record_environment

        envars = self._get_setting(name='whitelist',
                                   section='record_environment',
                                   user=False)

        if envars is not None:

            # User uppercase

            envars = [x.upper() for x in envars.split('\n')]

            # Make transparent for the user

            bot.custom(prefix="Environment ",
                       message='|'.join(envars),
                       color="CYAN")

            # Iterate through and collect based on name

            keep = [(k,v) for k,v in os.environ.items() if k.upper() in envars]

            # Ask the user for permission

            if confirm_prompt('Is this list ok to share?'):
                self.data['record_environment'] = keep

Response:
collect a limited set of environment variables based on the list
           under record_envirionment in the configuration file.


Instruction:
Implement a function in Python 3 to
record an asciinema from the user session saving the file to a temporary file and showing the user so if he needs to do it again.

Response:
def record_asciinema(self):
        '''record an asciinema from the user session, saving the file to
           a temporary file and showing the user so if he/she needs to do it
           again, the file can be provided. The flow of events below makes
           the following checks:

           1. The user confirms it is ok to record
           2. The record_asciinema setting is present and True in the config
           3. An asciinema file path has not been provided by the user

        '''

        # If the user already provided a file, we don't need to ask again

        if 'record_asciinema' not in self.data:

            if confirm_prompt("Would you like to send a terminal recording?"):

                try:
                    record = self.config.getboolean(self.name, 'record_asciinema')
                    filename = record_asciinema()
                    self.data['record_asciinema'] = filename

                    message = '''If you need to run helpme again you can give
                    the path to this file with  --asciinema %s''' % filename

                    bot.custom(prefix="Asciinema ", message=message, color="CYAN")

                except NoOptionError:

                    bot.warning('Cannot record asciinema, skipping.')


Instruction:
Explain what the following Python 3 code does
def speak(self):
        '''
           a function for the helper to announce him or herself, depending
           on the level specified. If you want your client to have additional
           announced things here, then implement the class `_speak` for your
           client.

        '''
        if self.quiet is False:
            bot.info('[helper|%s]' %(self.name))
            self._speak()

Response: speak the user s log for this object


Instruction:
How would you implement a function in Python 3 that
logs the number of constants generated.

Response:
def __log_number_of_constants(self):
        """
        Logs the number of constants generated.
        """
        n_id = len(self._labels)
        n_widths = len(self._constants) - n_id

        self._io.writeln('')
        self._io.text('Number of constants based on column widths: {0}'.format(n_widths))
        self._io.text('Number of constants based on database IDs : {0}'.format(n_id))


Instruction:
Implement a Python 3 function for
reading the parameters from the configuration file.

Response:
def _read_configuration_file(self, config_filename):
        """
        Reads parameters from the configuration file.

        :param str config_filename: The name of the configuration file.
        """
        config = configparser.ConfigParser()
        config.read(config_filename)

        self._constants_filename = config.get('constants', 'columns')
        self._prefix = config.get('constants', 'prefix')
        self._class_name = config.get('constants', 'class')


Instruction:
Implement a Python 3 function for
writing the constant class to the file.

Response:
def __write_constant_class(self):
        """
        Inserts new and replaces old (if any) constant declaration statements in the class that acts like a namespace
        for constants.
        """
        helper = ConstantClass(self._class_name, self._io)

        content = helper.source_with_constants(self._constants)

        Util.write_two_phases(helper.file_name(), content, self._io)


Instruction:
Write a Python 3 script to
check that the environment variable is set and not empty.

Response:
def _check_env_var(envvar: str) -> bool:
    """Check Environment Variable to verify that it is set and not empty.

    :param envvar: Environment Variable to Check.

    :returns: True if Environment Variable is set and not empty.

    :raises: KeyError if Environment Variable is not set or is empty.

    .. versionadded:: 0.0.12
    """
    if os.getenv(envvar) is None:
        raise KeyError(
            "Required ENVVAR: {0} is not set".format(envvar))
    if not os.getenv(envvar):  # test if env var is empty
        raise KeyError(
            "Required ENVVAR: {0} is empty".format(envvar))
    return True


Instruction:
Can you create a Python 3 function that
validates State Argument Validate State

Response:
def valid_state(state: str) -> bool:
    """Validate State Argument

    Checks that either 'on' or 'off' was entered as an argument to the
    CLI and make it lower case.

    :param state: state to validate.

    :returns: True if state is valid.

    .. versionchanged:: 0.0.12
        This moethod was renamed from validateState to valid_state to conform
        to PEP-8. Also removed "magic" text for state and instead reference the
        _VALID_STATES constant.
    """
    lower_case_state = state.lower()

    if lower_case_state in _VALID_STATES:
        return True
    return False


Instruction:
Write a Python 3 function that can
validate that required env vars exist.

Response:
def valid_env_vars() -> bool:
    """Validate that required env vars exist.

    :returns: True if required env vars exist.

    .. versionadded:: 0.0.12
    """
    for envvar in _REQUIRED_ENV_VARS:
        try:
            _check_env_var(envvar)
        except KeyError as ex:
            LOG.error(ex)
            sys.exit(1)
    return True


Instruction:
Given the following Python 3 function, write the documentation
def configure_panel(self):
        webroot = os.path.dirname(__file__)

        self.template_path = os.path.join(webroot, 'templates')
        aiohttp_jinja2.setup(
            self, loader=jinja2.FileSystemLoader(self.template_path),
            filters={'sorted': sorted, 'int': int}
        )

        self['static_root_url'] = '/static'
        self.router.add_view('/', Panel)
        self.router.add_static(
            '/static/', path=os.path.join(webroot, 'static'), name='static'
        )

Response: Configure templates and routing for the current locale.


Instruction:
Explain what the following Python 3 code does
async def start(self):
        logging.info('Starting server, listening on %s.', self.port)
        runner = web.AppRunner(self)
        await runner.setup()
        site = web.TCPSite(runner, '', self.port)
        await site.start()

Response: Initialize and start the server.


Instruction:
Can you generate a brief explanation for the following Python 3 code
async def get(self):
        shepherd = self.request.app.vmshepherd
        data = {'presets': {}, 'config': shepherd.config}
        presets = await shepherd.preset_manager.list_presets()
        runtime = shepherd.runtime_manager
        for name in presets:
            preset = shepherd.preset_manager.get_preset(name)
            data['presets'][name] = {
                'preset': preset,
                'vms': preset.vms,
                'runtime': await runtime.get_preset_data(name),
                'vmshepherd_id': shepherd.instance_id,
                'now': time.time()
            }
        return data

Response: Get all available presets and data


Instruction:
Implement a function in Python 3 to
serialize a dictionary into a string like object.

Response:
def serialize(dictionary):
    """
    Turn dictionary into argument like string.

    """

    data = []
    for key, value in dictionary.items():
        data.append('{0}="{1}"'.format(key, value))
    return ', '.join(data)


Instruction:
Can you tell what is the following Python 3 function doing
def getAmountOfHostsConnected(self, lanInterfaceId=1, timeout=1):
        namespace = Lan.getServiceType("getAmountOfHostsConnected") + str(lanInterfaceId)
        uri = self.getControlURL(namespace)

        results = self.execute(uri, namespace, "GetHostNumberOfEntries", timeout=timeout)

        return int(results["NewHostNumberOfEntries"])

Response: Execute NewHostNumberOfEntries action to get the amount of hosts connected to the LAN interface.


Instruction:
Create a Python 3 function for
executing GetGenericHostEntry action to get detailed information s of a connected host.

Response:
def getHostDetailsByIndex(self, index, lanInterfaceId=1, timeout=1):
        """Execute GetGenericHostEntry action to get detailed information's of a connected host.

        :param index: the index of the host
        :param int lanInterfaceId: the id of the LAN interface
        :param float timeout: the timeout to wait for the action to be executed
        :return: the detailed information's of a connected host.
        :rtype: HostDetails

        .. seealso:: :meth:`~simpletr64.actions.Lan.getAmountOfHostsConnected`
        """
        namespace = Lan.getServiceType("getHostDetailsByIndex") + str(lanInterfaceId)
        uri = self.getControlURL(namespace)

        results = self.execute(uri, namespace, "GetGenericHostEntry", timeout=timeout, NewIndex=index)

        return HostDetails(results)


Instruction:
Can you tell what is the following Python 3 function doing
def getHostDetailsByMACAddress(self, macAddress, lanInterfaceId=1, timeout=1):
        namespace = Lan.getServiceType("getHostDetailsByMACAddress") + str(lanInterfaceId)
        uri = self.getControlURL(namespace)

        results = self.execute(uri, namespace, "GetSpecificHostEntry", timeout=timeout, NewMACAddress=macAddress)

        return HostDetails(results, macAddress=macAddress)

Response: Execute GetSpecificHostEntry action to get the details for a host specified by its MAC address.


Instruction:
How would you implement a function in Python 3 that
executes GetInfo action to get information about the Ethernet interface.

Response:
def getEthernetInfo(self, lanInterfaceId=1, timeout=1):
        """Execute GetInfo action to get information's about the Ethernet interface.

        :param int lanInterfaceId: the id of the LAN interface
        :param float timeout: the timeout to wait for the action to be executed
        :return: information's about the Ethernet interface.
        :rtype: EthernetInfo
        """
        namespace = Lan.getServiceType("getEthernetInfo") + str(lanInterfaceId)
        uri = self.getControlURL(namespace)

        results = self.execute(uri, namespace, "GetInfo", timeout=timeout)

        return EthernetInfo(results)


Instruction:
Write a Python 3 script to
execute GetStatistics action to get statistics of the Ethernet interface.

Response:
def getEthernetStatistic(self, lanInterfaceId=1, timeout=1):
        """Execute GetStatistics action to get statistics of the Ethernet interface.

        :param int lanInterfaceId: the id of the LAN interface
        :param float timeout: the timeout to wait for the action to be executed
        :return: statisticss of the Ethernet interface.
        :rtype: EthernetStatistic
        """
        namespace = Lan.getServiceType("getEthernetStatistic") + str(lanInterfaceId)
        uri = self.getControlURL(namespace)

        results = self.execute(uri, namespace, "GetStatistics", timeout=timeout)

        return EthernetStatistic(results)


Instruction:
Can you generate the documentation for the following Python 3 function
def setEnable(self, status, lanInterfaceId=1, timeout=1):
        namespace = Lan.getServiceType("setEnable") + str(lanInterfaceId)
        uri = self.getControlURL(namespace)

        if status:
            setStatus = 1
        else:
            setStatus = 0

        self.execute(uri, namespace, "SetEnable", timeout=timeout, NewEnable=setStatus)

Response: Set enable status for a LAN interface


Instruction:
Create a Python 3 function to
submit a uservoice ticket.

Response:
def _submit(self):
        '''submit a uservoice ticket. When we get here we should have:

           {'user_prompt_issue': 'I want to do the thing.',
            'record_asciinema': '/tmp/helpme.93o__nt5.json',
            'record_environment': ((1,1),(2,2)...(N,N))}

           Required Client Variables
           self.api_key
           self.api_secret
           self.subdomain
           self.email

        '''

        # Step 0: Authenticate with uservoice API
        self.authenticate()

        title = "HelpMe UserVoice Ticket: %s" %(self.run_id)
        body = self.data['user_prompt_issue']

        # Step 1: Environment

        envars = self.data.get('record_environment')
        if envars not in [None, '', []]:
            body += '\n\nEnvironment:\n'
            for envar in envars:
                body += ' - %s: %s\n' %(envar[0], envar[1])

        # Step 2: Asciinema

        asciinema = self.data.get('record_asciinema')
        if asciinema not in [None, '']:
            url = upload_asciinema(asciinema)

            # If the upload is successful, add a link to it.

            if url is not None:
                body += "\n\nAsciinema Recording: %s" %url

        # Add other metadata about client

        body += "\ngenerated by HelpMe: https://vsoch.github.io/helpme/"

        # Submit the ticket!

        self.post_ticket(title, body)


Instruction:
Explain what the following Python 3 code does
def authenticate(self):
        '''authenticate with uservoice by creating a client.'''

        if not hasattr(self, 'client'):
            self.client = uservoice.Client(self.subdomain,
                                           self.api_key,
                                           self.api_secret)

Response: authenticate with uservoice by creating a client.


Instruction:
Make a summary of the following Python 3 code
def post_ticket(self, title, body):
        '''post_ticket will post a ticket to the uservoice helpdesk

           Parameters
           ==========
           title: the title (subject) of the issue
           body: the message to send

        '''

        # Populate the ticket
        ticket = {'subject': title,
                  'message': body }

        response = self.client.post("/api/v1/tickets.json", {
                                    'email': self.email,
                                    'ticket': ticket })['ticket']
        bot.info(response['url'])

Response: post a ticket to the uservoice helpdesk


Instruction:
Can you generate a brief explanation for the following Python 3 code
def read(datapath, qt_app=None, dataplus_format=True, gui=False, start=0, stop=None, step=1, convert_to_gray=True,
         series_number=None, dicom_expected=None, **kwargs):
    """Simple read function. Internally calls DataReader.Get3DData()"""
    dr = DataReader()
    return dr.Get3DData(datapath=datapath, qt_app=qt_app, dataplus_format=dataplus_format, gui=gui, start=start,
                        stop=stop, step=step, convert_to_gray=convert_to_gray, series_number=series_number,
                        use_economic_dtype=True, dicom_expected=dicom_expected  , **kwargs)

Response: Simple 3D read function. Internally calls DataReader. Get3DData


Instruction:
Can you implement a function in Python 3 that
functions which returns metadata dict.

Response:
def _metadata(image, datapath):
    """Function which returns metadata dict.

    :param image: image to get spacing from
    :param datapath: path to data
    :return: {'series_number': '', 'datadir': '', 'voxelsize_mm': ''}
    """
    metadata = {'series_number': 0, 'datadir': datapath}
    spacing = image.GetSpacing()
    metadata['voxelsize_mm'] = [
        spacing[2],
        spacing[0],
        spacing[1],
    ]
    return metadata


Instruction:
Write a Python 3 function for
getting 3D data from a given directory.

Response:
def Get3DData(self, datapath, qt_app=None, dataplus_format=True, gui=False, start=0, stop=None, step=1,
                  convert_to_gray=True, series_number=None, use_economic_dtype=True, dicom_expected=None, **kwargs):
        """Returns 3D data and its metadata.

        # NOTE(:param qt_app:) If it is set to None (as default) all dialogs for series selection are performed in
        terminal. If qt_app is set to QtGui.QApplication() dialogs are in Qt.

        :param datapath: directory with input data
        :param qt_app: Dialog destination. If None (default) -> terminal, if 'QtGui.QApplication()' -> Qt
        :param dataplus_format: New data format. Metadata and data are returned in one structure.
        :param gui: True if 'QtGui.QApplication()' instead of terminal should be used
        :param int start: used for DicomReader, defines where 3D data reading should start
        :param int stop: used for DicomReader, defines where 3D data reading should stop
        :param int step: used for DicomReader, defines step for 3D data reading
        :param bool convert_to_gray: if True -> RGB is converted to gray
        :param int series_number: used in DicomReader, essential in metadata
        :param use_economic_dtype: if True, casts 3D data array to less space consuming dtype
        :param dicom_expected: set true if it is known that data is in dicom format. Set False to suppress
        dicom warnings.
        :return: tuple (data3d, metadata)
        """
        self.orig_datapath = datapath
        datapath = os.path.expanduser(datapath)

        if series_number is not None and type(series_number) != int:
            series_number = int(series_number)

        if not os.path.exists(datapath):
            logger.error("Path '" + datapath + "' does not exist")
            return
        if qt_app is None and gui is True:
            from PyQt4.QtGui import QApplication
            qt_app = QApplication(sys.argv)

        if type(datapath) is not str:
            datapath = str(datapath)
        datapath = os.path.normpath(datapath)

        self.start = start
        self.stop = stop
        self.step = step
        self.convert_to_gray = convert_to_gray
        self.series_number = series_number
        self.kwargs = kwargs
        self.qt_app = qt_app
        self.gui = gui

        if os.path.isfile(datapath):
            logger.debug('file read recognized')
            data3d, metadata = self.__ReadFromFile(datapath)

        elif os.path.exists(datapath):
            logger.debug('directory read recognized')
            data3d, metadata = self.__ReadFromDirectory(datapath=datapath, dicom_expected=dicom_expected)
            # datapath, start, stop, step, gui=gui, **kwargs)
        else:
            logger.error('Data path {} not found'.format(datapath))

        if convert_to_gray:
            if len(data3d.shape) > 3:
                # TODO: implement better rgb2gray
                data3d = data3d[:, :, :, 0]
        if use_economic_dtype:
            data3d = self.__use_economic_dtype(data3d)

        if dataplus_format:
            logger.debug('dataplus format')
            # metadata = {'voxelsize_mm': [1, 1, 1]}
            datap = metadata
            datap['data3d'] = data3d
            logger.debug('datap keys () : ' + str(datap.keys()))
            return datap
        else:
            return data3d, metadata


Instruction:
Make a summary of the following Python 3 code
def __ReadFromFile(self, datapath):
        def _create_meta(_datapath):
            """Just simply returns some dict. This functions exists in order to keep DRY"""
            meta = {
                'series_number': 0,
                'datadir': _datapath
            }
            return meta

        path, ext = os.path.splitext(datapath)
        ext = ext[1:]
        if ext in ('pklz', 'pkl'):
            logger.debug('pklz format detected')
            from . import misc
            data = misc.obj_from_file(datapath, filetype='pkl')
            data3d = data.pop('data3d')
            # metadata must have series_number
            metadata = _create_meta(datapath)
            metadata.update(data)

        elif ext in ['hdf5']:
            from . import hdf5_io
            datap = hdf5_io.load_dict_from_hdf5(datapath)
            # datap = self.read_hdf5(datapath)
            data3d = datap.pop('data3d')

            # back compatibility
            if 'metadata' in datap.keys():
                datap = datap['metadata']
            # metadata must have series_number
            metadata = _create_meta(datapath)
            metadata.update(datap)

        elif ext in ['idx']:
            from . import idxformat
            idxreader = idxformat.IDXReader()
            data3d, metadata = idxreader.read(datapath)
        elif ext in ['dcm', 'DCM', 'dicom']:
            data3d, metadata = self._read_with_sitk(datapath)
            metadata = self._fix_sitk_bug(datapath, metadata)
        elif ext in ["bz2"]:
            new_datapath = tgz.untar(datapath)
            data3d, metadata = self.__ReadFromDirectory(new_datapath)
        else:
            logger.debug('file format "' + str(ext) + '"')
            # reading raw file
            data3d, metadata = self._read_with_sitk(datapath)
        return data3d, metadata

Response: Reads a single file and returns 3D data and its metadata.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _read_with_sitk(datapath):
        try:
            import SimpleITK as Sitk
        except ImportError as e:
            logger.error("Unable to import SimpleITK. On Windows try version 1.0.1")
        image = Sitk.ReadImage(datapath)
        data3d = dcmtools.get_pixel_array_from_sitk(image)
        # data3d, original_dtype = dcmreaddata.get_pixel_array_from_dcmobj(image)
        metadata = _metadata(image, datapath)
        return data3d, metadata

Response: Reads file using SimpleITK. Returns array of pixels image and metadata.


Instruction:
Can you tell what is the following Python 3 function doing
def _fix_sitk_bug(path, metadata):
        ds = dicom.read_file(path)
        try:
            metadata["voxelsize_mm"][0] = ds.SpacingBetweenSlices
        except Exception as e:
            logger.warning("Read dicom 'SpacingBetweenSlices' failed: ", e)
        return metadata

Response: This function is a fix for simple ITK for Z axis in 3D images.


Instruction:
Make a summary of the following Python 3 code
def get_pid(pid=None):
    '''get_pid will return a pid of interest. First we use given variable,
    then environmental variable PID, and then PID of running process
    '''
    if pid == None:
        if os.environ.get("PID",None) != None:
            pid = int(os.environ.get("PID"))
        # Then use current running script as process
        else:
            pid = os.getpid()
    print("pid is %s" %pid)
    return pid

Response: get_pid will return a pid of interest. First we use given variable and environmental variable PID then current running script as process


Instruction:
Write a Python 3 script to
load the stored routine into the instance of MySQL.

Response:
def load_stored_routine(self):
        """
        Loads the stored routine into the instance of MySQL.

        Returns the metadata of the stored routine if the stored routine is loaded successfully. Otherwise returns
        False.

        :rtype: dict[str,str]|bool
        """
        try:
            self._routine_name = os.path.splitext(os.path.basename(self._source_filename))[0]

            if os.path.exists(self._source_filename):
                if os.path.isfile(self._source_filename):
                    self._m_time = int(os.path.getmtime(self._source_filename))
                else:
                    raise LoaderException("Unable to get mtime of file '{}'".format(self._source_filename))
            else:
                raise LoaderException("Source file '{}' does not exist".format(self._source_filename))

            if self._pystratum_old_metadata:
                self._pystratum_metadata = self._pystratum_old_metadata

            load = self._must_reload()
            if load:
                self.__read_source_file()

                self.__get_placeholders()

                self._get_designation_type()

                self._get_name()

                self.__substitute_replace_pairs()

                self._load_routine_file()

                if self._designation_type == 'bulk_insert':
                    self._get_bulk_insert_table_columns_info()

                self._get_routine_parameters_info()

                self.__get_doc_block_parts_wrapper()

                self.__save_shadow_copy()

                self._update_metadata()

            return self._pystratum_metadata

        except Exception as exception:
            self._log_exception(exception)
            return False


Instruction:
How would you explain what the following Python 3 function does
def __read_source_file(self):
        with open(self._source_filename, 'r', encoding=self._routine_file_encoding) as file:
            self._routine_source_code = file.read()

        self._routine_source_code_lines = self._routine_source_code.split("\n")

Response: Reads the source file and sets the attributes self. _routine_source_code and self. _routine_source_lines.


Instruction:
Implement a function in Python 3 to
save a copy of the stored routine source with pure SQL.

Response:
def __save_shadow_copy(self):
        """
        Saves a copy of the stored routine source with pure SQL (if shadow directory is set).
        """
        if not self.shadow_directory:
            return

        destination_filename = os.path.join(self.shadow_directory, self._routine_name) + '.sql'

        if os.path.realpath(destination_filename) == os.path.realpath(self._source_filename):
            raise LoaderException("Shadow copy will override routine source '{}'".format(self._source_filename))

        # Remove the (read only) shadow file if it exists.
        if os.path.exists(destination_filename):
            os.remove(destination_filename)

        # Write the shadow file.
        with open(destination_filename, 'wt', encoding=self._routine_file_encoding) as handle:
            handle.write(self._routine_source_code)

        # Make the file read only.
        mode = os.stat(self._source_filename)[stat.ST_MODE]
        os.chmod(destination_filename, mode & ~stat.S_IWUSR & ~stat.S_IWGRP & ~stat.S_IWOTH)


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

        routine_source = []
        i = 0
        for line in self._routine_source_code_lines:
            self._replace['__LINE__'] = "'%d'" % (i + 1)
            for search, replace in self._replace.items():
                tmp = re.findall(search, line, re.IGNORECASE)
                if tmp:
                    line = line.replace(tmp[0], replace)
            routine_source.append(line)
            i += 1

        self._routine_source_code = "\n".join(routine_source)

Response: Substitutes all replace pairs in the stored routine source code.


Instruction:
Make a summary of the following Python 3 code
def _log_exception(self, exception):
        self._io.error(str(exception).strip().split(os.linesep))

Response:
Logs an exception.

        :param Exception exception: The exception.

        :rtype: None