Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_by_signature(user_id, app_id):
        '''
        Get the collection.
        '''
        try:
            return TabCollect.get(
                (TabCollect.user_id == user_id) &
                (TabCollect.post_id == app_id)
            )
        except:
            return None

Response: Get the collection by signature.


Instruction:
Write a Python 3 script to
get the cound of views.

Response:
def count_of_user(user_id):
        '''
        Get the cound of views.
        '''
        return TabCollect.select(
            TabCollect, TabPost.uid.alias('post_uid'),
            TabPost.title.alias('post_title'),
            TabPost.view_count.alias('post_view_count')
        ).where(
            TabCollect.user_id == user_id
        ).join(
            TabPost, on=(TabCollect.post_id == TabPost.uid)
        ).count()


Instruction:
How would you explain what the following Python 3 function does
def add_or_update(user_id, app_id):
        '''
        Add the collection or update.
        '''

        rec = MCollect.get_by_signature(user_id, app_id)

        if rec:
            entry = TabCollect.update(
                timestamp=int(time.time())
            ).where(TabCollect.uid == rec.uid)
            entry.execute()
        else:
            TabCollect.create(
                uid=tools.get_uuid(),
                user_id=user_id,
                post_id=app_id,
                timestamp=int(time.time()),
            )

Response: Add the collection or update.


Instruction:
How would you implement a function in Python 3 that
decorates a function to signify its deprecation ifest

Response:
def deprecated(deprecated_in=None, removed_in=None, current_version=None, details=""):
    """Decorate a function to signify its deprecation

    This function wraps a method that will soon be removed and does two things:
        * The docstring of the method will be modified to include a notice
          about deprecation, e.g., "Deprecated since 0.9.11. Use foo instead."
        * Raises a :class:`~deprecation.DeprecatedWarning`
          via the :mod:`warnings` module, which is a subclass of the built-in
          :class:`DeprecationWarning`. Note that built-in
          :class:`DeprecationWarning`\\s are ignored by default, so for users
          to be informed of said warnings they will need to enable them--see
          the :mod:`warnings` module documentation for more details.

    :param deprecated_in: The version at which the decorated method is
                          considered deprecated. This will usually be the
                          next version to be released when the decorator is
                          added. The default is **None**, which effectively
                          means immediate deprecation. If this is not
                          specified, then the `removed_in` and
                          `current_version` arguments are ignored.
    :param removed_in: The version when the decorated method will be removed.
                       The default is **None**, specifying that the function
                       is not currently planned to be removed.
                       Note: This cannot be set to a value if
                       `deprecated_in=None`.
    :param current_version: The source of version information for the
                            currently running code. This will usually be
                            a `__version__` attribute on your library.
                            The default is `None`.
                            When `current_version=None` the automation to
                            determine if the wrapped function is actually
                            in a period of deprecation or time for removal
                            does not work, causing a
                            :class:`~deprecation.DeprecatedWarning`
                            to be raised in all cases.
    :param details: Extra details to be added to the method docstring and
                    warning. For example, the details may point users to
                    a replacement method, such as "Use the foo_bar
                    method instead". By default there are no details.
    """
    # You can't just jump to removal. It's weird, unfair, and also makes
    # building up the docstring weird.
    if deprecated_in is None and removed_in is not None:
        raise TypeError("Cannot set removed_in to a value "
                        "without also setting deprecated_in")

    # Only warn when it's appropriate. There may be cases when it makes sense
    # to add this decorator before a formal deprecation period begins.
    # In CPython, PendingDeprecatedWarning gets used in that period,
    # so perhaps mimick that at some point.
    is_deprecated = False
    is_unsupported = False

    # StrictVersion won't take a None or a "", so make whatever goes to it
    # is at least *something*.
    if current_version:
        current_version = version.StrictVersion(current_version)

        if removed_in and current_version >= version.StrictVersion(removed_in):
            is_unsupported = True
        elif deprecated_in and current_version >= version.StrictVersion(deprecated_in):
            is_deprecated = True
    else:
        # If we can't actually calculate that we're in a period of
        # deprecation...well, they used the decorator, so it's deprecated.
        # This will cover the case of someone just using
        # @deprecated("1.0") without the other advantages.
        is_deprecated = True

    should_warn = any([is_deprecated, is_unsupported])

    def _function_wrapper(function):
        if should_warn:
            # Everything *should* have a docstring, but just in case...
            existing_docstring = function.__doc__ or ""

            # The various parts of this decorator being optional makes for
            # a number of ways the deprecation notice could go. The following
            # makes for a nicely constructed sentence with or without any
            # of the parts.
            parts = {
                "deprecated_in":
                    " in %s" % deprecated_in if deprecated_in else "",
                "removed_in":
                    ", to be removed in %s" % removed_in if removed_in else "",
                "period":
                    "." if deprecated_in or removed_in or details else "",
                "details":
                    " %s" % details if details else ""}

            deprecation_note = ("*Deprecated{deprecated_in}{removed_in}"
                                "{period}{details}*".format(**parts))

            function.__doc__ = "\n\n".join([existing_docstring,
                                            deprecation_note])

        @functools.wraps(function)
        def _inner(*args, **kwargs):
            if should_warn:
                if is_unsupported:
                    cls = UnsupportedWarning
                else:
                    cls = DeprecatedWarning

                the_warning = cls(function.__name__, deprecated_in,
                                  removed_in, details)
                warnings.warn(the_warning)

            return function(*args, **kwargs)

        return _inner

    return _function_wrapper


Instruction:
Make a summary of the following Python 3 code
def fail_if_not_removed(method):

    def _inner(*args, **kwargs):
        with warnings.catch_warnings(record=True) as caught_warnings:
            warnings.simplefilter("always")
            rval = method(*args, **kwargs)

        for warning in caught_warnings:
            if warning.category == UnsupportedWarning:
                raise AssertionError(
                    ("%s uses a function that should be removed: %s" %
                     (method, str(warning.message))))
        return rval

    return _inner

Response: Decorator that ensures that the deprecated code is removed.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def gen_xlsx_table_info():
    '''
    向表中插入数据
    '''

    XLSX_FILE = './database/esheet/20180811.xlsx'
    if os.path.exists(XLSX_FILE):
        pass
    else:
        return

    RAW_LIST = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
                'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']

    FILTER_COLUMNS = RAW_LIST + ["A" + x for x in RAW_LIST] + \
                     ["B" + x for x in RAW_LIST] + \
                     ["C" + x for x in RAW_LIST] + \
                     ["D" + x for x in RAW_LIST]

    tvalue = []

    file_d = open_workbook(XLSX_FILE)
    # 获得第一个页签对象

    x = 0
    for sheet_ranges in load_workbook(filename=XLSX_FILE):

        select_sheet = file_d.sheets()[x]

        # 获取总共的行数
        rows_num = select_sheet.nrows + 1

        for row_num in range(6, rows_num):
            tvalue = []
            for xr in FILTER_COLUMNS:

                row1_val = sheet_ranges[xr + '1'].value
                row4_val = sheet_ranges[xr + '{0}'.format(row_num)].value

                if row1_val:
                    if row4_val == None:
                        row4_val = ''
                    tvalue.append(row4_val)

            insert_tab(tvalue)
        x = x + 1
    print("成功插入 " + str(rows_num - 6) + " 行数据")

Response: Generate an XLSX table info file.


Instruction:
How would you implement a function in Python 3 that
gets all the arguments from the post request. Only get the first argument by default.

Response:
def get_post_data(self):
        '''
        Get all the arguments from post request. Only get the first argument by default.
        '''
        post_data = {}
        for key in self.request.arguments:
            post_data[key] = self.get_arguments(key)[0]
        return post_data


Instruction:
How would you code a function in Python 3 to
check the user role for docs.

Response:
def check_post_role(self):
        '''
        check the user role for docs.
        '''
        priv_dic = {'ADD': False, 'EDIT': False, 'DELETE': False, 'ADMIN': False}
        if self.userinfo:
            if self.userinfo.role[1] > '0':
                priv_dic['ADD'] = True
            if self.userinfo.role[1] >= '1':
                priv_dic['EDIT'] = True
            if self.userinfo.role[1] >= '3':
                priv_dic['DELETE'] = True
            if self.userinfo.role[1] >= '2':
                priv_dic['ADMIN'] = True
        return priv_dic


Instruction:
Implement a Python 3 function for
overriding the function to control the UI language.

Response:
def get_user_locale(self):
        '''
        Override the function, to control the UI language.
        '''
        locale_id = self.get_cookie('ulocale')
        if locale_id:
            return tornado.locale.get(locale_id)
        else:
            return tornado.locale.get('en_US')


Instruction:
Write a Python 3 script for
overriding the function to control the UI language.

Response:
def get_browser_locale(self):
        '''
        Override the function, to control the UI language.
        '''
        locale_id = self.get_cookie('blocale')
        if locale_id:
            return tornado.locale.get(locale_id)
        else:
            return tornado.locale.get('en_US')


Instruction:
Make a summary of the following Python 3 code
def wrap_tmpl(self, tmpl):
        '''
        return the warpped template path.
        :param tmpl:
        '''
        return 'admin/' + tmpl.format(sig='p') if self.is_p else tmpl.format(sig='')

Response: Wrap a template path.


Instruction:
Write a Python 3 function that can
get the entity id by the path.

Response:
def get_id_by_impath(path):
        '''
        The the entity id by the path.
        '''
        logger.info('Get Entiry, Path: {0}'.format(path))

        entity_list = TabEntity.select().where(TabEntity.path == path)
        out_val = None
        if entity_list.count() == 1:
            out_val = entity_list.get()
        elif entity_list.count() > 1:
            for rec in entity_list:
                MEntity.delete(rec.uid)
            out_val = None
        else:
            pass
        return out_val


Instruction:
Write a Python 3 function that can
create entity in the database

Response:
def create_entity(uid='', path='', desc='', kind='1'):
        '''
        create entity record in the database.
        '''

        if path:
            pass
        else:
            return False

        if uid:
            pass
        else:
            uid = get_uuid()
        try:
            TabEntity.create(
                uid=uid,
                path=path,
                desc=desc,
                time_create=time.time(),
                kind=kind
            )
            return True
        except:
            return False


Instruction:
Here you have a function in Python 3, explain what it does
def generate_html_files(*args):
    '''
    Generate the templates for adding, editing, viewing.
    :return: None
    '''
    _ = args

    for tag_key, tag_list in SWITCH_DICS.items():
        if tag_key.startswith('dic_'):
            __write_view_tmpl(tag_list)

    __write_filter_tmpl(TPL_LIST)
    __write_list_tmpl(TPL_LISTINFO)

Response: Generate the templates for adding editing viewing.


Instruction:
Can you create a Python 3 function that
adds or update the category.

Response:
def add_or_update(self, app_id):
        '''
        Add or update the category.
        '''
        logger.info('Collect info: user-{0}, uid-{1}'.format(self.userinfo.uid, app_id))
        MCollect.add_or_update(self.userinfo.uid, app_id)
        out_dic = {'success': True}
        return json.dump(out_dic, self)


Instruction:
Can you implement a function in Python 3 that
shows the list of the user collections.

Response:
def show_list(self, the_list, cur_p=''):
        '''
        List of the user collections.
        '''

        current_page_num = int(cur_p) if cur_p else 1
        current_page_num = 1 if current_page_num < 1 else current_page_num

        num_of_cat = MCollect.count_of_user(self.userinfo.uid)
        page_num = int(num_of_cat / CMS_CFG['list_num']) + 1

        kwd = {'current_page': current_page_num}

        self.render('misc/collect/list.html',
                    recs_collect=MCollect.query_pager_by_all(self.userinfo.uid,
                                                             current_page_num).objects(),
                    pager=tools.gen_pager_purecss('/collect/{0}'.format(the_list),
                                                  page_num,
                                                  current_page_num),
                    userinfo=self.userinfo,

                    cfg=CMS_CFG,
                    kwd=kwd)


Instruction:
Write a Python 3 script to
create a page with the given slug.

Response:
def create_page(slug, post_data):
        '''
        The page would be created with slug.
        '''
        logger.info('Call create Page')
        if MWiki.get_by_uid(slug):
            return False

        title = post_data['title'].strip()
        if len(title) < 2:
            return False
        return MWiki.__create_rec(slug, '2', post_data=post_data)


Instruction:
Can you write a function in Python 3 where it
queries the wiki of dated.

Response:
def query_dated(num=10, kind='1'):
        '''
        List the wiki of dated.
        '''
        return TabWiki.select().where(
            TabWiki.kind == kind
        ).order_by(
            TabWiki.time_update.desc()
        ).limit(num)


Instruction:
How would you code a function in Python 3 to
query the most viewed wiki.

Response:
def query_most(num=8, kind='1'):
        '''
        List the most viewed wiki.
        '''
        return TabWiki.select().where(
            TabWiki.kind == kind
        ).order_by(
            TabWiki.view_count.desc()
        ).limit(num)


Instruction:
Explain what the following Python 3 code does
def update_view_count(citiao):
        '''
        view count of the wiki, plus 1. By wiki
        '''
        entry = TabWiki.update(
            view_count=TabWiki.view_count + 1
        ).where(
            TabWiki.title == citiao
        )
        entry.execute()

Response: Update the view count of the wiki plus 1. By wiki


Instruction:
Here you have a function in Python 3, explain what it does
def update_view_count_by_uid(uid):
        '''
        update the count of wiki, by uid.
        '''
        entry = TabWiki.update(
            view_count=TabWiki.view_count + 1
        ).where(
            TabWiki.uid == uid
        )
        entry.execute()

Response: update the count of wiki by uid.


Instruction:
Given the following Python 3 function, write the documentation
def get_by_wiki(citiao):
        '''
        Get the wiki record by title.
        '''
        q_res = TabWiki.select().where(TabWiki.title == citiao)
        the_count = q_res.count()
        if the_count == 0 or the_count > 1:
            return None
        else:
            MWiki.update_view_count(citiao)
            return q_res.get()

Response: Get the wiki record by title.


Instruction:
Can you create a Python 3 function that
views count plus one.

Response:
def view_count_plus(slug):
        '''
        View count plus one.
        '''
        entry = TabWiki.update(
            view_count=TabWiki.view_count + 1,
        ).where(TabWiki.uid == slug)
        entry.execute()


Instruction:
Can you implement a function in Python 3 that
queries pager by kind

Response:
def query_pager_by_kind(kind, current_page_num=1):
        '''
        Query pager
        '''
        return TabWiki.select().where(TabWiki.kind == kind).order_by(TabWiki.time_create.desc()).paginate(current_page_num, CMS_CFG['list_num'])


Instruction:
Can you implement a function in Python 3 that
gets the count of certain kind.

Response:
def count_of_certain_kind(kind):
        '''
        Get the count of certain kind.
        '''

        recs = TabWiki.select().where(TabWiki.kind == kind)

        return recs.count()


Instruction:
Can you implement a function in Python 3 that
gets the ID of a tag by its name.

Response:
def get_id_by_name(tag_name, kind='z'):
        '''
        Get ID by tag_name of the label.
        '''
        recs = TabTag.select().where(
            (TabTag.name == tag_name) & (TabTag.kind == kind)
        )
        logger.info('tag count of {0}: {1} '.format(tag_name, recs.count()))
        # the_id = ''
        if recs.count() == 1:
            the_id = recs.get().uid
        elif recs.count() > 1:
            rec0 = None
            for rec in recs:
                # Only keep one.
                if rec0:
                    TabPost2Tag.delete().where(TabPost2Tag.tag_id == rec.uid).execute()
                    TabTag.delete().where(TabTag.uid == rec.uid).execute()
                else:
                    rec0 = rec

            the_id = rec0.uid
        else:
            the_id = MLabel.create_tag(tag_name)
        return the_id


Instruction:
Write a Python 3 script for
getting label by slug.

Response:
def get_by_slug(tag_slug):
        '''
        Get label by slug.
        '''
        label_recs = TabTag.select().where(TabTag.slug == tag_slug)
        return label_recs.get() if label_recs else False


Instruction:
Can you generate the documentation for the following Python 3 function
def create_tag(tag_name, kind='z'):
        '''
        Create tag record by tag_name
        '''
        cur_recs = TabTag.select().where(
            (TabTag.name == tag_name) &
            (TabTag.kind == kind)
        )
        if cur_recs.count():
            uid = cur_recs.get().uid

            # TabTag.delete().where(
            #     (TabTag.name == tag_name) &
            #     (TabTag.kind == kind)
            # ).execute()
        else:
            uid = tools.get_uu4d_v2()  # Label with the ID of v2.
            while TabTag.select().where(TabTag.uid == uid).count() > 0:
                uid = tools.get_uu4d_v2()

            TabTag.create(
                uid=uid,
                slug=uid,
                name=tag_name,
                order=1,
                count=0,
                kind='z',
                tmpl=9,
                pid='zzzz',
            )
        return uid

Response: Create tag record by tag_name and kind.


Instruction:
Can you write a function in Python 3 where it
gets records by post id.

Response:
def get_by_uid(post_id):
        '''
        Get records by post id.
        '''
        return TabPost2Tag.select(
            TabPost2Tag,
            TabTag.name.alias('tag_name'),
            TabTag.uid.alias('tag_uid')
        ).join(
            TabTag, on=(TabPost2Tag.tag_id == TabTag.uid)
        ).where(
            (TabPost2Tag.post_id == post_id) & (TabTag.kind == 'z')
        )


Instruction:
Write a Python 3 script to
return the number of certian slug.

Response:
def total_number(slug, kind='1'):
        '''
        Return the number of certian slug.
        '''
        return TabPost.select().join(
            TabPost2Tag,
            on=(TabPost.uid == TabPost2Tag.post_id)
        ).where(
            (TabPost2Tag.tag_id == slug) & (TabPost.kind == kind)
        ).count()


Instruction:
Make a summary of the following Python 3 code
def query_pager_by_slug(slug, kind='1', current_page_num=1):
        '''
        Query pager
        '''
        return TabPost.select().join(
            TabPost2Tag, on=(TabPost.uid == TabPost2Tag.post_id)
        ).where(
            (TabPost2Tag.tag_id == slug) &
            (TabPost.kind == kind)
        ).paginate(current_page_num, CMS_CFG['list_num'])

Response: Query pager by slug.


Instruction:
Make a summary of the following Python 3 code
def add_or_update(self, app_id, value):
        '''
        Adding or updating the evalution.
        :param app_id:  the ID of the post.
        :param value: the evaluation
        :return:  in JSON format.
        '''
        MEvaluation.add_or_update(self.userinfo.uid, app_id, value)

        out_dic = {
            'eval0': MEvaluation.app_evaluation_count(app_id, 0),
            'eval1': MEvaluation.app_evaluation_count(app_id, 1)
        }

        return json.dump(out_dic, self)

Response: Adds or updates the evalution.


Instruction:
Create a Python 3 function to
create entity2user record in the database.

Response:
def create_entity2user(enti_uid, user_id):
        '''
        create entity2user record in the database.
        '''
        record = TabEntity2User.select().where(
            (TabEntity2User.entity_id == enti_uid) & (TabEntity2User.user_id == user_id)
        )

        if record.count() > 0:
            record = record.get()
            MEntity2User.count_increate(record.uid, record.count)
        else:
            TabEntity2User.create(
                uid=tools.get_uuid(),
                entity_id=enti_uid,
                user_id=user_id,
                count=1,
                timestamp=time.time()
            )


Instruction:
Create a Python 3 function to
check the HTML file for the specific page.

Response:
def check_html(html_file, begin):
    '''
    Checking the HTML
    '''
    sig = False
    for html_line in open(html_file).readlines():

        # uu = x.find('{% extends')
        uuu = pack_str(html_line).find('%extends')
        # print(pack_str(x))
        if uuu > 0:
            f_tmpl = html_line.strip().split()[-2].strip('"')
            curpath, curfile = os.path.split(html_file)
            ff_tmpl = os.path.abspath(os.path.join(curpath, f_tmpl))
            if os.path.isfile(ff_tmpl):
                # print(os.path.abspath(ff_tmpl))
                pass
            else:
                print('=' *10 + 'ERROR' + '=' *10)
                print('The file:')
                print(' ' * 4 + html_file)
                print('needs:')
                print(' ' * 4 +ff_tmpl)
                print('Error, tmpl not find.')
                # continue
                sys.exit(1)
            sig = True
        if sig:
            pass
        else:
            continue
        vvv = pack_str(html_line).find('%block')
        if vvv > 0:
            test_fig = False
            for the_line in open(ff_tmpl).readlines():
                if the_line.find(pack_str(html_line)) > 0:
                    test_fig = True

            # fff = sim_filename(ff_tmpl)
            # sss = sim_filename(html_file)

            fff = ff_tmpl[begin:]
            sss = html_file[begin:]

            tmplsig = [fff, sss]

            if tmplsig in RELS_UNIQ_ARR:
                pass
            else:
                RELS_UNIQ_ARR.append(tmplsig)
                DOT_OBJ.edge(fff, sss)
            if test_fig:
                # G.add_edge(ff_tmpl[begin], html_file[begin])
                pass
            else:
                pass


Instruction:
Can you generate a brief explanation for the following Python 3 code
def do_for_dir(inws, begin):
    '''
    do something in the directory.
    '''
    inws = os.path.abspath(inws)
    for wroot, wdirs, wfiles in os.walk(inws):
        for wfile in wfiles:
            if wfile.endswith('.html'):
                if 'autogen' in wroot:
                    continue
                check_html(os.path.abspath(os.path.join(wroot, wfile)), begin)

Response: do something in the directory inws


Instruction:
Implement a Python 3 function for
doing check it. srws is a list of srws to check it.

Response:
def run_checkit(srws=None):
    '''
    do check it.
    '''
    begin = len(os.path.abspath('templates')) + 1
    inws = os.path.abspath(os.getcwd())
    if srws:
        do_for_dir(srws[0], begin)
    else:
        do_for_dir(os.path.join(inws, 'templates'), begin)

    DOT_OBJ.render('xxtmpl', view=True)


Instruction:
Given the following Python 3 function, write the documentation
def query_all():
        '''
        Query all the records from TabPost2Tag.
        '''
        recs = TabPost2Tag.select(
            TabPost2Tag,
            TabTag.kind.alias('tag_kind'),
        ).join(
            TabTag,
            on=(TabPost2Tag.tag_id == TabTag.uid)
        )
        return recs

Response: Query all the records from TabPost2Tag.


Instruction:
Can you implement a function in Python 3 that
removes the relation between post and tag.

Response:
def remove_relation(post_id, tag_id):
        '''
        Delete the record of post 2 tag.
        '''
        entry = TabPost2Tag.delete().where(
            (TabPost2Tag.post_id == post_id) &
            (TabPost2Tag.tag_id == tag_id)
        )
        entry.execute()
        MCategory.update_count(tag_id)


Instruction:
Here you have a function in Python 3, explain what it does
def remove_tag(tag_id):
        '''
        Delete the records of certain tag.
        '''
        entry = TabPost2Tag.delete().where(
            TabPost2Tag.tag_id == tag_id
        )
        entry.execute()

Response: Remove the records of certain tag.


Instruction:
Implement a Python 3 function for
querying records by post.

Response:
def query_by_post(postid):
        '''
        Query records by post.
        '''
        return TabPost2Tag.select().where(
            TabPost2Tag.post_id == postid
        ).order_by(TabPost2Tag.order)


Instruction:
Implement a function in Python 3 to
get the record by post and catalog.

Response:
def __get_by_info(post_id, catalog_id):
        '''
        Geo the record by post and catalog.
        '''
        recs = TabPost2Tag.select().where(
            (TabPost2Tag.post_id == post_id) &
            (TabPost2Tag.tag_id == catalog_id)
        )

        if recs.count() == 1:
            return recs.get()
        elif recs.count() > 1:
            # return the first one, and delete others.
            out_rec = None
            for rec in recs:
                if out_rec:
                    entry = TabPost2Tag.delete().where(
                        TabPost2Tag.uid == rec.uid
                    )
                    entry.execute()
                else:
                    out_rec = rec
            return out_rec
        return None


Instruction:
Implement a Python 3 function for
returning the count of post2tag.

Response:
def query_count():
        '''
        The count of post2tag.
        '''
        recs = TabPost2Tag.select(
            TabPost2Tag.tag_id,
            peewee.fn.COUNT(TabPost2Tag.tag_id).alias('num')
        ).group_by(
            TabPost2Tag.tag_id
        )
        return recs


Instruction:
Implement a Python 3 function for
updating the field of post2tag.

Response:
def update_field(uid, post_id=None, tag_id=None, par_id=None):
        '''
        Update the field of post2tag.
        '''
        if post_id:
            entry = TabPost2Tag.update(
                post_id=post_id
            ).where(TabPost2Tag.uid == uid)
            entry.execute()

        if tag_id:
            entry2 = TabPost2Tag.update(
                par_id=tag_id[:2] + '00',
                tag_id=tag_id,
            ).where(TabPost2Tag.uid == uid)
            entry2.execute()
        if par_id:
            entry2 = TabPost2Tag.update(
                par_id=par_id
            ).where(TabPost2Tag.uid == uid)
            entry2.execute()


Instruction:
Can you write a function in Python 3 where it
adds a record of post 2 tag.

Response:
def add_record(post_id, catalog_id, order=0):
        '''
        Create the record of post 2 tag, and update the count in g_tag.
        '''

        rec = MPost2Catalog.__get_by_info(post_id, catalog_id)
        if rec:
            entry = TabPost2Tag.update(
                order=order,
                # For migration. the value should be added when created.
                par_id=rec.tag_id[:2] + '00',
            ).where(TabPost2Tag.uid == rec.uid)
            entry.execute()
        else:
            TabPost2Tag.create(
                uid=tools.get_uuid(),
                par_id=catalog_id[:2] + '00',
                post_id=post_id,
                tag_id=catalog_id,
                order=order,
            )

        MCategory.update_count(catalog_id)


Instruction:
Can you tell what is the following Python 3 function doing
def count_of_certain_category(cat_id, tag=''):
        '''
        Get the count of certain category.
        '''

        if cat_id.endswith('00'):
            # The first level category, using the code bellow.
            cat_con = TabPost2Tag.par_id == cat_id
        else:
            cat_con = TabPost2Tag.tag_id == cat_id

        if tag:
            condition = {
                'def_tag_arr': [tag]
            }
            recs = TabPost2Tag.select().join(
                TabPost,
                on=((TabPost2Tag.post_id == TabPost.uid) & (TabPost.valid == 1))
            ).where(
                cat_con & TabPost.extinfo.contains(condition)
            )
        else:
            recs = TabPost2Tag.select().where(
                cat_con
            )

        return recs.count()

Response: Get the count of certain category.


Instruction:
Can you generate the documentation for the following Python 3 function
def query_pager_by_slug(slug, current_page_num=1, tag='', order=False):
        '''
        Query pager via category slug.
        '''
        cat_rec = MCategory.get_by_slug(slug)
        if cat_rec:
            cat_id = cat_rec.uid
        else:
            return None

        # The flowing code is valid.
        if cat_id.endswith('00'):
            # The first level category, using the code bellow.
            cat_con = TabPost2Tag.par_id == cat_id
        else:
            cat_con = TabPost2Tag.tag_id == cat_id

        if tag:
            condition = {
                'def_tag_arr': [tag]
            }
            recs = TabPost.select().join(
                TabPost2Tag,
                on=((TabPost.uid == TabPost2Tag.post_id) & (TabPost.valid == 1))
            ).where(
                cat_con & TabPost.extinfo.contains(condition)
            ).order_by(
                TabPost.time_update.desc()
            ).paginate(current_page_num, CMS_CFG['list_num'])
        elif order:
            recs = TabPost.select().join(
                TabPost2Tag,
                on=((TabPost.uid == TabPost2Tag.post_id) & (TabPost.valid == 1))
            ).where(
                cat_con
            ).order_by(
                TabPost.order.asc()
            )
        else:
            recs = TabPost.select().join(
                TabPost2Tag,
                on=((TabPost.uid == TabPost2Tag.post_id) & (TabPost.valid == 1))
            ).where(
                cat_con
            ).order_by(
                TabPost.time_update.desc()
            ).paginate(current_page_num, CMS_CFG['list_num'])

        return recs

Response: Query pager via category slug.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def query_by_entity_uid(idd, kind=''):
        '''
        Query post2tag by certain post.
        '''

        if kind == '':
            return TabPost2Tag.select(
                TabPost2Tag,
                TabTag.slug.alias('tag_slug'),
                TabTag.name.alias('tag_name')
            ).join(
                TabTag, on=(TabPost2Tag.tag_id == TabTag.uid)
            ).where(
                (TabPost2Tag.post_id == idd) &
                (TabTag.kind != 'z')
            ).order_by(
                TabPost2Tag.order
            )
        return TabPost2Tag.select(
            TabPost2Tag,
            TabTag.slug.alias('tag_slug'),
            TabTag.name.alias('tag_name')
        ).join(TabTag, on=(TabPost2Tag.tag_id == TabTag.uid)).where(
            (TabTag.kind == kind) &
            (TabPost2Tag.post_id == idd)
        ).order_by(
            TabPost2Tag.order
        )

Response: Query post2tag by certain post.


Instruction:
Here you have a function in Python 3, explain what it does
def get_first_category(app_uid):
        '''
        Get the first, as the uniqe category of post.
        '''
        recs = MPost2Catalog.query_by_entity_uid(app_uid).objects()
        if recs.count() > 0:
            return recs.get()

        return None

Response: Get the first uniqe category of post.


Instruction:
Write a Python 3 script to
render the info page for the current user.

Response:
def render(self, *args, **kwargs):
        '''
        fun(uid_with_str)
        fun(uid_with_str, slug = val1, glyph = val2)
        '''

        uid_with_str = args[0]

        slug = kwargs.get('slug', False)

        with_title = kwargs.get('with_title', False)

        glyph = kwargs.get('glyph', '')

        kwd = {
            'glyph': glyph
        }
        curinfo = MCategory.get_by_uid(uid_with_str)

        sub_cats = MCategory.query_sub_cat(uid_with_str)

        if slug:
            tmpl = 'modules/info/catalog_slug.html'

        else:
            tmpl = 'modules/info/catalog_of.html'
        return self.render_string(tmpl,
                                  pcatinfo=curinfo,
                                  sub_cats=sub_cats,
                                  recs=sub_cats,
                                  with_title=with_title,
                                  kwd=kwd)


Instruction:
Make a summary of the following Python 3 code
def render(self, *args, **kwargs):
        '''
        fun(user_name, kind)
        fun(user_name, kind, num)
        fun(user_name, kind, num, with_tag = val1, glyph = val2)
        fun(user_name = vala, kind = valb, num = valc, with_tag = val1, glyph = val2)
        '''
        user_name = kwargs.get('user_name', args[0])
        kind = kwargs.get('kind', args[1])
        num = kwargs.get('num', args[2] if len(args) > 2 else 6)
        with_tag = kwargs.get('with_tag', False)
        glyph = kwargs.get('glyph', '')

        all_cats = MUsage.query_most(user_name, kind, num).objects()
        kwd = {
            'with_tag': with_tag,
            'router': router_post[kind],
            'glyph': glyph
        }
        return self.render_string('modules/info/list_user_equation.html',
                                  recs=all_cats,
                                  kwd=kwd)

Response: Render the most recent user - related entries.


Instruction:
Here you have a function in Python 3, explain what it does
def render_it(self, *args, **kwargs):
        '''
        Render without userinfo.
        fun(kind, num)
        fun(kind, num, with_tag = val1)
        fun(kind, num, with_tag = val1, glyph = val2)
        '''
        kind = kwargs.get('kind', args[0])
        num = kwargs.get('num', args[1] if len(args) > 1 else 6)
        with_tag = kwargs.get('with_tag', False)
        glyph = kwargs.get('glyph', '')

        all_cats = MPost.query_most(kind=kind, num=num).objects()
        kwd = {
            'with_tag': with_tag,
            'router': router_post[kind],
            'glyph': glyph
        }
        return self.render_string('modules/info/list_equation.html',
                                  recs=all_cats,
                                  kwd=kwd)

Response:
Render without userinfo.
        fun(kind, num)
        fun(kind, num, with_tag = val1)
        fun(kind, num, with_tag = val1, glyph = val2)


Instruction:
Create a Python 3 function to
render no user logged in render no user logged in

Response:
def render_it(self, kind, num, with_tag=False, glyph=''):
        '''
        render, no user logged in
        '''
        all_cats = MPost.query_recent(num, kind=kind)
        kwd = {
            'with_tag': with_tag,
            'router': router_post[kind],
            'glyph': glyph
        }
        return self.render_string('modules/info/list_equation.html',
                                  recs=all_cats,
                                  kwd=kwd)


Instruction:
How would you explain what the following Python 3 function does
def render_user(self, *args, **kwargs):
        '''
        render, with userinfo
        fun(kind, num)
        fun(kind, num, with_tag = val1)
        fun(kind, num, with_tag = val1, user_id = val2)
        fun(kind, num, with_tag = val1, user_id = val2, glyph = val3)
        '''

        kind = kwargs.get('kind', args[0])
        num = kwargs.get('num', args[1] if len(args) > 1 else 6)
        with_tag = kwargs.get('with_tag', False)
        user_id = kwargs.get('user_id', '')
        glyph = kwargs.get('glyph', '')

        logger.info(
            'Infor user recent, username: {user_name}, kind: {kind}, num: {num}'.format(
                user_name=user_id, kind=kind, num=num
            )
        )

        all_cats = MUsage.query_recent(user_id, kind, num).objects()
        kwd = {
            'with_tag': with_tag,
            'router': router_post[kind],
            'glyph': glyph
        }
        return self.render_string('modules/info/list_user_equation.html',
                                  recs=all_cats,
                                  kwd=kwd)

Response:
render, with userinfo
        fun(kind, num)
        fun(kind, num, with_tag = val1)
        fun(kind, num, with_tag = val1, user_id = val2)
        fun(kind, num, with_tag = val1, user_id = val2, glyph = val3)


Instruction:
Explain what the following Python 3 code does
def to_modify(self, uid):
        '''
        Try to edit the link.
        '''
        if self.userinfo.role[1] >= '3':
            pass
        else:
            return False

        self.render('misc/link/link_edit.html',
                    kwd={},
                    postinfo=MLink.get_by_uid(uid),
                    userinfo=self.userinfo)

Response: Try to edit the link.


Instruction:
Write a Python 3 script to
create link by user.

Response:
def user_add_link(self):
        '''
        Create link by user.
        '''
        if self.check_post_role()['ADD']:
            pass
        else:
            return False
        post_data = self.get_post_data()

        post_data['user_name'] = self.get_current_user()

        cur_uid = tools.get_uudd(2)
        while MLink.get_by_uid(cur_uid):
            cur_uid = tools.get_uudd(2)

        MLink.create_link(cur_uid, post_data)

        self.redirect('/link/list')


Instruction:
Given the following Python 3 function, write the documentation
def delete_by_id(self, del_id):
        '''
        Delete a link by id.
        '''
        if self.check_post_role()['DELETE']:
            pass
        else:
            return False
        if self.is_p:
            if MLink.delete(del_id):
                output = {'del_link': 1}
            else:
                output = {'del_link': 0}
            return json.dump(output, self)
        else:
            is_deleted = MLink.delete(del_id)
            if is_deleted:
                self.redirect('/link/list')

Response: Delete a link by id.


Instruction:
Explain what the following Python 3 code does
def get_rating(postid, userid):
        '''
        Get the rating of certain post and user.
        '''
        try:
            recs = TabRating.select().where(
                (TabRating.post_id == postid) & (TabRating.user_id == userid)
            )
        except:
            return False
        if recs.count() > 0:
            return recs.get().rating
        else:
            return False

Response: Get the rating of certain post and user.


Instruction:
Write a Python 3 script for
updating the rating of certain post and user.

Response:
def update(postid, userid, rating):
        '''
        Update the rating of certain post and user.
        The record will be created if no record exists.
        '''
        rating_recs = TabRating.select().where(
            (TabRating.post_id == postid) & (TabRating.user_id == userid)
        )
        if rating_recs.count() > 0:
            MRating.__update_rating(rating_recs.get().uid, rating)
        else:
            MRating.__insert_data(postid, userid, rating)


Instruction:
Explain what the following Python 3 code does
def __update_rating(uid, rating):
        '''
        Update rating.
        '''
        entry = TabRating.update(
            rating=rating
        ).where(TabRating.uid == uid)
        entry.execute()

Response: Update the rating table with the given uid.


Instruction:
Write a Python 3 function that can
insert data into the record.

Response:
def __insert_data(postid, userid, rating):
        '''
        Inert new record.
        '''
        uid = tools.get_uuid()
        TabRating.create(
            uid=uid,
            post_id=postid,
            user_id=userid,
            rating=rating,
            timestamp=tools.timestamp(),
        )
        return uid


Instruction:
Can you tell what is the following Python 3 function doing
def update_category(uid, postdata, kwargs):
    '''
    Update the category of the post.
    '''
    catid = kwargs['catid'] if ('catid' in kwargs and MCategory.get_by_uid(kwargs['catid'])) else None

    post_data = postdata

    current_infos = MPost2Catalog.query_by_entity_uid(uid, kind='').objects()

    new_category_arr = []
    # Used to update post2category, to keep order.
    def_cate_arr = ['gcat{0}'.format(x) for x in range(10)]

    # for old page.
    def_cate_arr.append('def_cat_uid')

    # Used to update post extinfo.
    cat_dic = {}
    for key in def_cate_arr:
        if key not in post_data:
            continue
        if post_data[key] == '' or post_data[key] == '0':
            continue
        # 有可能选重复了。保留前面的
        if post_data[key] in new_category_arr:
            continue

        new_category_arr.append(post_data[key] + ' ' * (4 - len(post_data[key])))
        cat_dic[key] = post_data[key] + ' ' * (4 - len(post_data[key]))

    if catid:
        def_cat_id = catid
    elif new_category_arr:
        def_cat_id = new_category_arr[0]
    else:
        def_cat_id = None

    if def_cat_id:
        cat_dic['def_cat_uid'] = def_cat_id
        cat_dic['def_cat_pid'] = MCategory.get_by_uid(def_cat_id).pid

    print('=' * 40)
    print(uid)
    print(cat_dic)
    MPost.update_jsonb(uid, cat_dic)

    for index, catid in enumerate(new_category_arr):
        MPost2Catalog.add_record(uid, catid, index)

    # Delete the old category if not in post requests.
    for cur_info in current_infos:
        if cur_info.tag_id not in new_category_arr:
            MPost2Catalog.remove_relation(uid, cur_info.tag_id)

Response: Update the category of the post.


Instruction:
How would you explain what the following Python 3 function does
def add_relation(app_f, app_t, weight=1):
        '''
        Adding relation between two posts.
        '''
        recs = TabRel.select().where(
            (TabRel.post_f_id == app_f) & (TabRel.post_t_id == app_t)
        )
        if recs.count() > 1:
            for record in recs:
                MRelation.delete(record.uid)

        if recs.count() == 0:
            uid = tools.get_uuid()
            entry = TabRel.create(
                uid=uid,
                post_f_id=app_f,
                post_t_id=app_t,
                count=1,
            )
            return entry.uid
        elif recs.count() == 1:
            MRelation.update_relation(app_f, app_t, weight)
        else:
            return False

Response: Adds relation between two posts.


Instruction:
Can you tell what is the following Python 3 function doing
def get_app_relations(app_id, num=20, kind='1'):
        '''
        The the related infors.
        '''
        info_tag = MInfor2Catalog.get_first_category(app_id)
        if info_tag:
            return TabPost2Tag.select(
                TabPost2Tag,
                TabPost.title.alias('post_title'),
                TabPost.valid.alias('post_valid')
            ).join(
                TabPost, on=(TabPost2Tag.post_id == TabPost.uid)
            ).where(
                (TabPost2Tag.tag_id == info_tag.tag_id) &
                (TabPost.kind == kind)
            ).order_by(
                peewee.fn.Random()
            ).limit(num)
        return TabPost2Tag.select(
            TabPost2Tag,
            TabPost.title.alias('post_title'),
            TabPost.valid.alias('post_valid')
        ).join(
            TabPost, on=(TabPost2Tag.post_id == TabPost.uid)
        ).where(
            TabPost.kind == kind
        ).order_by(peewee.fn.Random()).limit(num)

Response: Get the related infors.


Instruction:
Can you generate the documentation for the following Python 3 function
def get(self, *args, **kwargs):
        '''
        /label/s/view
        '''
        url_arr = self.parse_url(args[0])

        if len(url_arr) == 2:
            if url_arr[0] == 'remove':
                self.remove_redis_keyword(url_arr[1])
            else:
                self.list(url_arr[0], url_arr[1])
        elif len(url_arr) == 3:
            self.list(url_arr[0], url_arr[1], url_arr[2])
        else:
            return False

Response: Get a specific resource from the cache.


Instruction:
Create a Python 3 function for
removing the keyword for redis.

Response:
def remove_redis_keyword(self, keyword):
        '''
        Remove the keyword for redis.
        '''
        redisvr.srem(CMS_CFG['redis_kw'] + self.userinfo.user_name, keyword)
        return json.dump({}, self)


Instruction:
Implement a function in Python 3 to
view the label list.

Response:
def list(self, kind, tag_slug, cur_p=''):
        '''
        根据 cat_handler.py 中的 def view_cat_new(self, cat_slug, cur_p = '')
        '''
        # 下面用来使用关键字过滤信息，如果网站信息量不是很大不要开启
        # Todo:
        # if self.get_current_user():
        #     redisvr.sadd(config.redis_kw + self.userinfo.user_name, tag_slug)

        if cur_p == '':
            current_page_number = 1
        else:
            current_page_number = int(cur_p)

        current_page_number = 1 if current_page_number < 1 else current_page_number

        pager_num = int(MPost2Label.total_number(tag_slug, kind) / CMS_CFG['list_num'])
        tag_info = MLabel.get_by_slug(tag_slug)
        if tag_info:
            tag_name = tag_info.name
        else:
            tag_name = 'Label search results'
        kwd = {'tag_name': tag_name,
               'tag_slug': tag_slug,
               'title': tag_name,
               'current_page': current_page_number,
               'router': router_post[kind],
               'kind': kind
               }

        the_list_file = './templates/list/label_{kind}.html'.format(kind=kind)

        if os.path.exists(the_list_file):
            tmpl = 'list/label_{kind}.html'.format(kind=kind)

        else:
            tmpl = 'list/label.html'

        self.render(tmpl,
                    infos=MPost2Label.query_pager_by_slug(
                        tag_slug,
                        kind=kind,
                        current_page_num=current_page_number
                    ),
                    kwd=kwd,
                    userinfo=self.userinfo,
                    pager=self.gen_pager(kind, tag_slug, pager_num, current_page_number),
                    cfg=CMS_CFG)


Instruction:
Here you have a function in Python 3, explain what it does
def gen_pager(self, kind, cat_slug, page_num, current):
        '''
        cat_slug 分类
        page_num 页面总数
        current 当前页面
        '''
        if page_num == 1:
            return ''

        pager_shouye = '''
   << 首页

'''.format(
            'hidden' if current <= 1 else '', kind, cat_slug
        )

        pager_pre = '''
< 前页

'''.format(
            'hidden' if current <= 1 else '', kind, cat_slug, current - 1
        )
        pager_mid = ''
        for ind in range(0, page_num):
            tmp_mid = '''
{3}

'''.format(
                'active' if ind + 1 == current else '', kind, cat_slug, ind + 1
            )
            pager_mid += tmp_mid
        pager_next = '''
后页 >

'''.format(
            'hidden' if current >= page_num else '', kind, cat_slug, current + 1
        )
        pager_last = '''
末页>>

'''.format(
            'hidden' if current >= page_num else '', kind, cat_slug, page_num
        )
        pager = pager_shouye + pager_pre + pager_mid + pager_next + pager_last
        return pager

Response: Generate a pager for the page.


Instruction:
Write a Python 3 script to
build the directory for Whoosh database and locale.

Response:
def build_directory():
    '''
    Build the directory for Whoosh database, and locale.
    '''
    if os.path.exists('locale'):
        pass
    else:
        os.mkdir('locale')
    if os.path.exists(WHOOSH_DB_DIR):
        pass
    else:
        os.makedirs(WHOOSH_DB_DIR)


Instruction:
Make a summary of the following Python 3 code
def run_check_kind(_):
    '''
    Running the script.
    '''
    for kindv in router_post:
        for rec_cat in MCategory.query_all(kind=kindv):
            catid = rec_cat.uid
            catinfo = MCategory.get_by_uid(catid)
            for rec_post2tag in MPost2Catalog.query_by_catid(catid):
                postinfo = MPost.get_by_uid(rec_post2tag.post_id)
                if postinfo.kind == catinfo.kind:
                    pass
                else:
                    print(postinfo.uid)

Response: Check if the kind of post is the same as the category.


Instruction:
How would you explain what the following Python 3 function does
def run_create_admin(*args):
    '''
    creating the default administrator.
    '''
    post_data = {
        'user_name': 'giser',
        'user_email': 'giser@osgeo.cn',
        'user_pass': '131322',
        'role': '3300',
    }
    if MUser.get_by_name(post_data['user_name']):
        print('User {user_name} already exists.'.format(user_name='giser'))
    else:
        MUser.create_user(post_data)

Response: Create the default administrator.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def update_post(self, postid):
        '''
        The rating of Post should be updaed if the count is greater than 10

        '''
        voted_recs = MRating.query_by_post(postid)
        if voted_recs.count() > 10:
            rating = MRating.query_average_rating(postid)
        else:
            rating = 5

        logger.info('Get post rating: {rating}'.format(rating=rating))
        # MPost.__update_rating(postid, rating)
        MPost.update_misc(postid, rating=rating)

Response: Update the post with the rating of the given postid.


Instruction:
Can you generate the documentation for the following Python 3 function
def update_rating(self, postid):
        '''
        only the used who logged in would voting.
        '''
        post_data = self.get_post_data()
        rating = float(post_data['rating'])
        postinfo = MPost.get_by_uid(postid)
        if postinfo and self.userinfo:
            MRating.update(postinfo.uid, self.userinfo.uid, rating=rating)
            self.update_post(postid)
        else:
            return False

Response: update the rating of the user


Instruction:
Can you tell what is the following Python 3 function doing
def __get_diff_recent():
    '''
    Generate the difference of posts. recently.
    '''
    diff_str = ''

    for key in router_post:
        recent_posts = MPost.query_recent_edited(tools.timestamp() - TIME_LIMIT, kind=key)
        for recent_post in recent_posts:
            hist_rec = MPostHist.get_last(recent_post.uid)
            if hist_rec:
                raw_title = hist_rec.title
                new_title = recent_post.title

                infobox = diff_table(raw_title, new_title)

                diff_str = diff_str + '''

TITLE: {0}

                '''.format(recent_post.title) + infobox

                infobox = diff_table(hist_rec.cnt_md, recent_post.cnt_md)

                diff_str = diff_str + '
CONTENT:{0}
'.format(
                    recent_post.title
                ) + infobox + '
'
            else:
                continue
    return diff_str

Response: Generate the difference of posts. recently.


Instruction:
Can you create a Python 3 function that
gets Review for wikis.

Response:
def __get_wiki_review(email_cnt, idx):
    '''
    Review for wikis.
    '''
    recent_posts = MWiki.query_recent_edited(tools.timestamp() - TIME_LIMIT, kind='2')
    for recent_post in recent_posts:
        hist_rec = MWikiHist.get_last(recent_post.uid)
        if hist_rec:
            foo_str = '''

{0}	{1}	Edit	{2}
                    {3}

                    '''.format(idx, recent_post.user_name, recent_post.title,
                               os.path.join(SITE_CFG['site_url'], 'page', recent_post.uid))
            email_cnt = email_cnt + foo_str
        else:
            foo_str = '''

{0}	{1}	New 	{2}
                    {3}

                    '''.format(idx, recent_post.user_name, recent_post.title,
                               os.path.join(SITE_CFG['site_url'], 'page', recent_post.uid))
            email_cnt = email_cnt + foo_str
        idx = idx + 1
    email_cnt = email_cnt + '
'
    return email_cnt, idx


Instruction:
Write a Python 3 script for
getting Review for posts.

Response:
def __get_post_review(email_cnt, idx):
    '''
    Review for posts.
    '''
    for key in router_post:
        recent_posts = MPost.query_recent_edited(tools.timestamp() - TIME_LIMIT, kind=key)
        for recent_post in recent_posts:
            hist_rec = MPostHist.get_last(recent_post.uid)
            if hist_rec:
                foo_str = '''

{0}	{1}	Edit	{2}
                    {3}

                    '''.format(idx, recent_post.user_name, recent_post.title,
                               os.path.join(SITE_CFG['site_url'], router_post[key],
                                            recent_post.uid))
                email_cnt = email_cnt + foo_str
            else:
                foo_str = '''

{0}	{1}	New 	{2}
                    {3}

                    '''.format(idx, recent_post.user_name, recent_post.title,
                               os.path.join(SITE_CFG['site_url'], router_post[key],
                                            recent_post.uid))
                email_cnt = email_cnt + foo_str
            idx = idx + 1

    return email_cnt, idx


Instruction:
Can you write a function in Python 3 where it
runs the Review command.

Response:
def run_review(*args):
    '''
    Get the difference of recents modification, and send the Email.
    For: wiki, page, and post.
    '''
    email_cnt = '''<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <style type="text/css">
        table.diff {font-family:Courier; border:medium;}
        .diff_header {background-color:#e0e0e0}
        td.diff_header {text-align:right}
        .diff_next {background-color:#c0c0c0}
        .diff_add {background-color:#aaffaa}
        .diff_chg {background-color:#ffff77}
        .diff_sub {background-color:#ffaaaa}
    </style></head><body>'''

    idx = 1

    email_cnt = email_cnt + '<table border=1>'

    email_cnt, idx = __get_post_review(email_cnt, idx)  # post
    email_cnt, idx = __get_page_review(email_cnt, idx)  # page.
    email_cnt, idx = __get_wiki_review(email_cnt, idx)  # wiki

    ###########################################################

    diff_str = __get_diff_recent()

    if len(diff_str) < 20000:
        email_cnt = email_cnt + diff_str
    email_cnt = email_cnt + '''</body></html>'''

    if idx > 1:
        send_mail(post_emails, "{0}|{1}|{2}".format(SMTP_CFG['name'], '文档更新情况', DATE_STR), email_cnt)


Instruction:
Implement a Python 3 function for
querying the count of certain category.

Response:
def query_field_count(limit_num, kind='1'):
        '''
        Query the posts count of certain category.
        '''
        return TabTag.select().where(
            TabTag.kind == kind
        ).order_by(
            TabTag.count.desc()
        ).limit(limit_num)


Instruction:
Create a Python 3 function to
get the category record by its slug

Response:
def get_by_slug(slug):
        '''
        return the category record .
        '''
        rec = TabTag.select().where(TabTag.slug == slug)
        if rec.count() > 0:
            return rec.get()
        return None


Instruction:
How would you implement a function in Python 3 that
updates the count of certain category.

Response:
def update_count(cat_id):
        '''
        Update the count of certain category.
        '''
        # Todo: the record not valid should not be counted.
        entry2 = TabTag.update(
            count=TabPost2Tag.select().where(
                TabPost2Tag.tag_id == cat_id
            ).count()
        ).where(TabTag.uid == cat_id)
        entry2.execute()


Instruction:
Write a Python 3 script to
add or update the data by the given ID of post.

Response:
def add_or_update(uid, post_data):
        '''
        Add or update the data by the given ID of post.
        '''
        catinfo = MCategory.get_by_uid(uid)
        if catinfo:
            MCategory.update(uid, post_data)
        else:
            TabTag.create(
                uid=uid,
                name=post_data['name'],
                slug=post_data['slug'],
                order=post_data['order'],
                kind=post_data['kind'] if 'kind' in post_data else '1',
                pid=post_data['pid'],
            )
        return uid


Instruction:
Can you generate the documentation for the following Python 3 function
def recent(self, with_catalog=True, with_date=True):
        '''
        List posts that recent edited.
        '''
        kwd = {
            'pager': '',
            'title': 'Recent posts.',
            'with_catalog': with_catalog,
            'with_date': with_date,
        }
        self.render('list/post_list.html',
                    kwd=kwd,
                    view=MPost.query_recent(num=20),
                    postrecs=MPost.query_recent(num=2),
                    format_date=tools.format_date,
                    userinfo=self.userinfo,
                    cfg=CMS_CFG, )

Response: List posts that recent edited.


Instruction:
Write a Python 3 script for
listing the posts to be modified.

Response:
def errcat(self):
        '''
        List the posts to be modified.
        '''
        post_recs = MPost.query_random(limit=1000)
        outrecs = []
        errrecs = []
        idx = 0
        for postinfo in post_recs:
            if idx > 16:
                break
            cat = MPost2Catalog.get_first_category(postinfo.uid)
            if cat:
                if 'def_cat_uid' in postinfo.extinfo:
                    if postinfo.extinfo['def_cat_uid'] == cat.tag_id:
                        pass
                    else:
                        errrecs.append(postinfo)
                        idx += 1
                else:
                    errrecs.append(postinfo)
                    idx += 1
            else:
                outrecs.append(postinfo)
                idx += 1
        self.render('list/errcat.html',
                    kwd={},
                    norecs=outrecs,
                    errrecs=errrecs,
                    userinfo=self.userinfo)


Instruction:
How would you implement a function in Python 3 that
lists the post of dated.

Response:
def refresh(self):
        '''
        List the post of dated.
        '''
        kwd = {
            'pager': '',
            'title': '',
        }
        self.render('list/post_list.html',
                    kwd=kwd,
                    userinfo=self.userinfo,
                    view=MPost.query_dated(10),
                    postrecs=MPost.query_dated(10),
                    format_date=tools.format_date,
                    cfg=CMS_CFG)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def build_dir():
    '''
    Build the directory used for templates.
    '''
    tag_arr = ['add', 'edit', 'view', 'list', 'infolist']
    path_arr = [os.path.join(CRUD_PATH, x) for x in tag_arr]
    for wpath in path_arr:
        if os.path.exists(wpath):
            continue
        os.makedirs(wpath)

Response: Build the directory used for templates.


Instruction:
Create a Python 3 function to
get reply list of certain post.

Response:
def query_by_post(postid):
        '''
        Get reply list of certain post.
        '''
        return TabReply.select().where(
            TabReply.post_id == postid
        ).order_by(TabReply.timestamp.desc())


Instruction:
Can you generate a brief explanation for the following Python 3 code
def __write_filter_dic(wk_sheet, column):
    '''
    return filter dic for certain column
    '''
    row1_val = wk_sheet['{0}1'.format(column)].value
    row2_val = wk_sheet['{0}2'.format(column)].value
    row3_val = wk_sheet['{0}3'.format(column)].value
    row4_val = wk_sheet['{0}4'.format(column)].value

    if row1_val and row1_val.strip() != '':
        row2_val = row2_val.strip()

        slug_name = row1_val.strip()
        c_name = row2_val.strip()

        tags1 = [x.strip() for x in row3_val.split(',')]
        tags_dic = {}

        #  if only one tag,
        if len(tags1) == 1:
            xx_1 = row2_val.split(':')  # 'text'  # HTML text input control.

            if xx_1[0].lower() in INPUT_ARR:
                xx_1[0] = xx_1[0].lower()
            else:
                xx_1[0] = 'text'

            if len(xx_1) == 2:
                ctr_type, unit = xx_1
            else:
                ctr_type = xx_1[0]
                unit = ''
            tags_dic[1] = unit

        else:
            ctr_type = 'select'  # HTML selectiom control.
            for index, tag_val in enumerate(tags1):
                # the index of tags_dic starts from 1.
                tags_dic[index + 1] = tag_val.strip()

        outkey = 'html_{0}'.format(slug_name)
        outval = {
            'en': slug_name,
            'zh': c_name,
            'dic': tags_dic,
            'type': ctr_type,
            'display': row4_val,
        }
        return (outkey, outval)
    else:
        return (None, None)

Response: write filter dic for certain column


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_last(postid):
        '''
        Get the last wiki in history.
        '''
        recs = TabWikiHist.select().where(
            TabWikiHist.wiki_id == postid
        ).order_by(TabWikiHist.time_update.desc())

        return None if recs.count() == 0 else recs.get()

Response: Get the last wiki in history.


Instruction:
Create a Python 3 function for
checking if user is prived from a given rule.

Response:
def is_prived(usr_rule, def_rule):
    '''
    Compare between two role string.
    '''
    for iii in range(4):
        if def_rule[iii] == '0':
            continue
        if usr_rule[iii] >= def_rule[iii]:
            return True

    return False


Instruction:
Can you implement a function in Python 3 that
drops the tables in the base xml file.

Response:
def run_drop_tables(_):
    '''
    Running the script.
    '''
    print('--')

    drop_the_table(TabPost)
    drop_the_table(TabTag)
    drop_the_table(TabMember)
    drop_the_table(TabWiki)
    drop_the_table(TabLink)
    drop_the_table(TabEntity)
    drop_the_table(TabPostHist)
    drop_the_table(TabWikiHist)
    drop_the_table(TabCollect)
    drop_the_table(TabPost2Tag)
    drop_the_table(TabRel)
    drop_the_table(TabEvaluation)
    drop_the_table(TabUsage)
    drop_the_table(TabReply)
    drop_the_table(TabUser2Reply)
    drop_the_table(TabRating)


Instruction:
How would you code a function in Python 3 to
detect the name and version of a node in the system.

Response:
def detect(agent, fill_none=False):
    """
    fill_none: if name/version is not detected respective key is still added to the result with value None
    """
    result = dict(platform=dict(name=None, version=None))
    _suggested_detectors = []

    for info_type in detectorshub:
        detectors = _suggested_detectors or detectorshub[info_type]
        for detector in detectors:
            try:
                detector.detect(agent, result)
            except Exception as _err:
                pass

    if fill_none:
        for outer_key in ('os', 'browser'):
            outer_value = result.setdefault(outer_key, dict())
            for inner_key in ('name', 'version'):
                outer_value.setdefault(inner_key, None)

    return result


Instruction:
Here you have a function in Python 3, explain what it does
def getVersion(self, agent, word):
        version_markers = self.version_markers if \
            isinstance(self.version_markers[0], (list, tuple)) else [self.version_markers]
        version_part = agent.split(word, 1)[-1]
        for start, end in version_markers:
            if version_part.startswith(start) and end in version_part:
                version = version_part[1:]
                if end:  # end could be empty string
                    version = version.split(end)[0]
                if not self.allow_space_in_version:
                    version = version.split()[0]
                return version

Response: returns the version string for the given word


Instruction:
Write a Python 3 script for
copying the files and runs memory tests if needed.

Response:
def compile_files(args, mem_test=False):
    ''' Copiles the files and runs memory tests
        if needed.
        PARAM args: list of files passed as CMD args
                    to be compiled.
        PARAM mem_test: Weither to perform memory test ?
    '''
    for filename in args:
        if not os.path.isfile(filename):
            print('The file doesn\'t exits')
            return
        build_and_run_file(filename)
        print("")


Instruction:
Implement a function in Python 3 to
build and runs the file specified by filename according to the extension specified by the extension parameter returns the resulting string.

Response:
def build_and_run_file(filename):
    ''' Builds and runs the filename specified
        according to the extension
        PARAM filename: name of file to build and run
    '''
    (directory, name, extension) = get_file_tuple(filename)
    if extension == 'c':
        print(" = = = = = = ", YELLOW, "GCC: Compiling " + filename + " file", \
              RESET, " = = = = = =\n")
        compiler = Compiler(filename)
        out = compiler.compile()
        if out != 0:
            print('Error while compiling. Code:', out, 'Please retry.')
            return out
        print("")

        out = compiler.run()
        return out

    elif extension == 'cpp':
        print(" = = = = = = ", YELLOW, "GPP: Compiling " + filename + " file", \
              RESET, " = = = = = =\n")
        compiler = Compiler(filename)
        out = compiler.compile()
        if out != 0:
            print('Error while compiling. Code:', out, 'Please retry.')
            return out
        print("")

        out = compiler.run()
        return out

    elif extension == 'py':
        print(" = = = = = = ", YELLOW, "PYTHON: Executing " + filename + " file", \
              RESET, " = = = = = =\n")
        compiler = Compiler(filename)
        out = compiler.run()
        return out
    elif extension == 'java':
        command = EXECUTABLE_JAVAC + ' ' + filename
        perform_system_command(command)
        command_run = EXECUTABLE_JAVA + ' ' + name
        test_file = directory + "/" + name + ".input"
        if os.path.exists(test_file):
            command_run += " < " + test_file
        return perform_system_command(command_run)
    else:
        print("Language yet not supported")
        return -1


Instruction:
Write a Python 3 script for
checking if all of the required programs are installed.

Response:
def check_exec_installed(exec_list):
    """ Check the required programs are
        installed.
        PARAM exec_list: list of programs to check
        RETURN: True if all installed else False
    """
    all_installed = True
    for exe in exec_list:
        if not is_tool(exe):
            print("Executable: " + exe + " is not installed")
            all_installed = False
    return all_installed


Instruction:
Explain what the following Python 3 code does
def parse_known_args():
    parser = ArgumentParser()
    parser.add_argument("-l", "--loop", type=int, help="Loop every X seconds")
    parser.add_argument('-V', '--version',
                        action='store_true',
                        dest='version',
                        help='Print the version number and exit')
    parser.add_argument("-u", "--update",
                        action='store_true',
                        dest="update",
                        help="Update the software from online repo")
    parser.add_argument("-p", "--problem",
                        action='store_true',
                        dest="problem",
                        help="Report a problem")
    parser.add_argument("-m", "--memory",
                        action='store_true',
                        dest="memory",
                        help="Run memory tests")
    args, otherthings = parser.parse_known_args()
    return args, otherthings, parser

Response: Parse command line arguments


Instruction:
How would you explain what the following Python 3 function does
def multi_process(func, data, num_process=None, verbose=True, **args):
    '''Function to use multiprocessing to process pandas Dataframe.

    This function applies a function on each row of the input DataFrame by
    multiprocessing.

    Args:
        func (function): The function to apply on each row of the input
            Dataframe. The func must accept pandas.Series as the first
            positional argument and return a pandas.Series.
        data (pandas.DataFrame): A DataFrame to be processed.
        num_process (int, optional): The number of processes to run in
            parallel. Defaults to be the number of CPUs of the computer.
        verbose (bool, optional): Set to False to disable verbose output.
        args (dict): Keyword arguments to pass as keywords arguments to `func`
    return:
        A dataframe containing the results
    '''
    # Check arguments value
    assert isinstance(data, pd.DataFrame), \
        'Input data must be a pandas.DataFrame instance'
    if num_process is None:
        num_process = multiprocessing.cpu_count()
    # Establish communication queues
    tasks = multiprocessing.JoinableQueue()
    results = multiprocessing.Queue()
    error_queue = multiprocessing.Queue()
    start_time = time.time()
    # Enqueue tasks
    num_task = len(data)
    for i in range(num_task):
        tasks.put(data.iloc[i, :])
    # Add a poison pill for each consumer
    for i in range(num_process):
        tasks.put(None)

    logger.info('Create {} processes'.format(num_process))
    consumers = [Consumer(func, tasks, results, error_queue, **args)
                 for i in range(num_process)]
    for w in consumers:
        w.start()
    # Add a task tracking process
    task_tracker = TaskTracker(tasks, verbose)
    task_tracker.start()
    # Wait for all input data to be processed
    tasks.join()
    # If there is any error in any process, output the error messages
    num_error = error_queue.qsize()
    if num_error > 0:
        for i in range(num_error):
            logger.error(error_queue.get())
        raise RuntimeError('Multi process jobs failed')
    else:
        # Collect results
        result_table = []
        while num_task:
            result_table.append(results.get())
            num_task -= 1
        df_results = pd.DataFrame(result_table)
        logger.info("Jobs finished in {0:.2f}s".format(
            time.time()-start_time))
        return df_results

Response: Function to use multiprocessing to process pandas Dataframe by using multiprocessing to process pandas Dataframe by multiprocessing.


Instruction:
Implement a function in Python 3 to
run the job in a loop.

Response:
def run(self):
        '''Define the job of each process to run.
        '''
        while True:
            next_task = self._task_queue.get()
            # If there is any error, only consume data but not run the job
            if self._error_queue.qsize() > 0:
                self._task_queue.task_done()
                continue
            if next_task is None:
                # Poison pill means shutdown
                self._task_queue.task_done()
                break
            try:
                answer = self._func(next_task, **self._args)
                self._task_queue.task_done()
                self._result_queue.put(answer)
            except Exception as e:
                self._task_queue.task_done()
                self._error_queue.put((os.getpid(), e))
                logger.error(e)
                continue


Instruction:
Can you generate the documentation for the following Python 3 function
def run(self):
        '''Define the job of each process to run.
        '''
        if self.verbose:
            pbar = tqdm(total=100)
        while True:
            task_remain = self._task_queue.qsize()
            task_finished = int((float(self.total_task - task_remain) /
                                 float(self.total_task)) * 100)
            if task_finished % 20 == 0 and task_finished != self.current_state:
                self.current_state = task_finished
                logger.info('{0}% done'.format(task_finished))
                if self.verbose and task_finished > 0:
                    pbar.update(20)
            if task_remain == 0:
                break
        logger.debug('All task data cleared')

Response: Define the job of each process to run.


Instruction:
Given the following Python 3 function, write the documentation
def func(data_row, wait):
    ''' A sample function
    It takes 'wait' seconds to calculate the sum of each row
    '''
    time.sleep(wait)
    data_row['sum'] = data_row['col_1'] + data_row['col_2']
    return data_row

Response: A sample function that calculates the sum of each row


Instruction:
Create a Python 3 function to
compute the mean error of the simulated and observed data.

Response:
def me(simulated_array, observed_array, replace_nan=None, replace_inf=None,
       remove_neg=False, remove_zero=False):
    """Compute the mean error of the simulated and observed data.

    .. image:: /pictures/ME.png

    **Range:** -inf < MAE < inf, data units, closer to zero is better, indicates bias.

    **Notes:** The mean error (ME) measures the difference between the simulated data and the
    observed data. For the mean error, a smaller number indicates a better fit to the original
    data. Note that if the error is in the form of random noise, the mean error will be very small,
    which can skew the accuracy of this metric. ME is cumulative and will be small even if there
    are large positive and negative errors that balance.

    Parameters
    ----------
    simulated_array: one dimensional ndarray
        An array of simulated data from the time series.

    observed_array: one dimensional ndarray
        An array of observed data from the time series.

    replace_nan: float, optional
        If given, indicates which value to replace NaN values with in the two arrays. If None, when
        a NaN value is found at the i-th position in the observed OR simulated array, the i-th value
        of the observed and simulated array are removed before the computation.

    replace_inf: float, optional
        If given, indicates which value to replace Inf values with in the two arrays. If None, when
        an inf value is found at the i-th position in the observed OR simulated array, the i-th
        value of the observed and simulated array are removed before the computation.

    remove_neg: boolean, optional
        If True, when a negative value is found at the i-th position in the observed OR simulated
        array, the i-th value of the observed AND simulated array are removed before the
        computation.

    remove_zero: boolean, optional
        If true, when a zero value is found at the i-th position in the observed OR simulated
        array, the i-th value of the observed AND simulated array are removed before the
        computation.

    Returns
    -------
    float
        The mean error value.

    Examples
    --------
    Note that in this example the random noise cancels, leaving a very small ME.

    >>> import HydroErr as he
    >>> import numpy as np

    >>> # Seed for reproducibility
    >>> np.random.seed(54839)

    >>> x = np.arange(100) / 20
    >>> sim = np.sin(x) + 2
    >>> obs = sim * (((np.random.rand(100) - 0.5) / 10) + 1)
    >>> he.me(sim, obs)
    -0.006832220968967168

    References
    ----------
    - Fisher, R.A., 1920. A Mathematical Examination of the Methods of Determining the Accuracy of
      an Observation by the Mean Error, and by the Mean Square Error. Monthly Notices of the Royal
      Astronomical Society 80 758 - 770.
    """
    # Treating missing values
    simulated_array, observed_array = treat_values(simulated_array, observed_array,
                                                   replace_nan=replace_nan,
                                                   replace_inf=replace_inf,
                                                   remove_neg=remove_neg,
                                                   remove_zero=remove_zero)
    return np.mean(simulated_array - observed_array)


Instruction:
Here you have a function in Python 3, explain what it does
def mae(simulated_array, observed_array, replace_nan=None, replace_inf=None,
        remove_neg=False, remove_zero=False):
    """Compute the mean absolute error of the simulated and observed data.

    .. image:: /pictures/MAE.png

    **Range:** 0 ≤ MAE < inf, data units, smaller is better.

    **Notes:** The ME measures the absolute difference between the simulated data and the observed
    data. For the mean abolute error, a smaller number indicates a better fit to the original data.
    Also note that random errors do not cancel. Also referred to as an L1-norm.

    Parameters
    ----------
    simulated_array: one dimensional ndarray
        An array of simulated data from the time series.

    observed_array: one dimensional ndarray
        An array of observed data from the time series.

    replace_nan: float, optional
        If given, indicates which value to replace NaN values with in the two arrays. If None, when
        a NaN value is found at the i-th position in the observed OR simulated array, the i-th value
        of the observed and simulated array are removed before the computation.

    replace_inf: float, optional
        If given, indicates which value to replace Inf values with in the two arrays. If None, when
        an inf value is found at the i-th position in the observed OR simulated array, the i-th
        value of the observed and simulated array are removed before the computation.

    remove_neg: boolean, optional
        If True, when a negative value is found at the i-th position in the observed OR simulated
        array, the i-th value of the observed AND simulated array are removed before the
        computation.

    remove_zero: boolean, optional
        If true, when a zero value is found at the i-th position in the observed OR simulated
        array, the i-th value of the observed AND simulated array are removed before the
        computation.

    Returns
    -------
    float
        The mean absolute error value.

    References
    ----------
    - Willmott, Cort J., and Kenji Matsuura. “Advantages of the Mean Absolute Error (MAE) over the
      Root Mean Square Error (RMSE) in Assessing Average Model Performance.” Climate Research 30,
      no. 1 (2005): 79–82.
    - Willmott, Cort J., and Kenji Matsuura. “On the Use of Dimensioned Measures of Error to
      Evaluate the Performance of Spatial Interpolators.” International Journal of Geographical
      Information Science 20, no. 1 (2006): 89–102.

    Examples
    --------

    >>> import HydroErr as he
    >>> import numpy as np

    >>> sim = np.array([5, 7, 9, 2, 4.5, 6.7])
    >>> obs = np.array([4.7, 6, 10, 2.5, 4, 6.8])
    >>> he.mae(sim, obs)
    0.5666666666666665
    """

    # Checking and cleaning the data
    simulated_array, observed_array = treat_values(
        simulated_array,
        observed_array,
        replace_nan=replace_nan,
        replace_inf=replace_inf,
        remove_neg=remove_neg,
        remove_zero=remove_zero
    )

    return np.mean(np.absolute(simulated_array - observed_array))

Response: Compute the mean absolute error of the simulated and observed data.


Instruction:
Given the following Python 3 function, write the documentation
def mle(simulated_array, observed_array, replace_nan=None, replace_inf=None,
        remove_neg=False, remove_zero=False):
    """
    Compute the mean log error of the simulated and observed data.

    .. image:: /pictures/MLE.png

    **Range:** -inf < MLE < inf, data units, closer to zero is better.

    **Notes** Same as the mean erro (ME) only use log ratios as the error term. Limits the impact of outliers, more
    evenly weights high and low data values.

    Parameters
    ----------
    simulated_array: one dimensional ndarray
        An array of simulated data from the time series.

    observed_array: one dimensional ndarray
        An array of observed data from the time series.

    replace_nan: float, optional
        If given, indicates which value to replace NaN values with in the two arrays. If None, when
        a NaN value is found at the i-th position in the observed OR simulated array, the i-th value
        of the observed and simulated array are removed before the computation.

    replace_inf: float, optional
        If given, indicates which value to replace Inf values with in the two arrays. If None, when
        an inf value is found at the i-th position in the observed OR simulated array, the i-th
        value of the observed and simulated array are removed before the computation.

    remove_neg: boolean, optional
        If True, when a negative value is found at the i-th position in the observed OR simulated
        array, the i-th value of the observed AND simulated array are removed before the
        computation.

    remove_zero: boolean, optional
        If true, when a zero value is found at the i-th position in the observed OR simulated
        array, the i-th value of the observed AND simulated array are removed before the
        computation.

    Returns
    -------
    float
        The mean log error value.

    Examples
    --------

    Note that the value is very small because it is in log space.

    >>> import HydroErr as he
    >>> import numpy as np

    >>> sim = np.array([5, 7, 9, 2, 4.5, 6.7])
    >>> obs = np.array([4.7, 6, 10, 2.5, 4, 6.8])
    >>> he.mle(sim, obs)
    0.002961767058151136

    References
    ----------
    - Törnqvist, Leo, Pentti Vartia, and Yrjö O. Vartia. “How Should Relative Changes Be Measured?”
      The American Statistician 39, no. 1 (1985): 43–46.
    """

    # Checking and cleaning the data

    simulated_array, observed_array = treat_values(
        simulated_array,
        observed_array,
        replace_nan=replace_nan,
        replace_inf=replace_inf,
        remove_neg=remove_neg,
        remove_zero=remove_zero
    )

    sim_log = np.log1p(simulated_array)
    obs_log = np.log1p(observed_array)
    return np.mean(sim_log - obs_log)

Response: Compute the mean log error of the simulated and observed data.


Instruction:
Here you have a function in Python 3, explain what it does
def mde(simulated_array, observed_array, replace_nan=None, replace_inf=None,
        remove_neg=False, remove_zero=False):
    """
    Compute the median error (MdE) between the simulated and observed data.

    .. image:: /pictures/MdE.png

    **Range** -inf < MdE < inf, closer to zero is better.

    **Notes** This metric indicates bias. It is similar to the mean error (ME), only it takes the
    median rather than the mean. Median measures reduces the impact of outliers.

    Parameters
    ----------
    simulated_array: one dimensional ndarray
        An array of simulated data from the time series.

    observed_array: one dimensional ndarray
        An array of observed data from the time series.

    replace_nan: float, optional
        If given, indicates which value to replace NaN values with in the two arrays. If None, when
        a NaN value is found at the i-th position in the observed OR simulated array, the i-th value
        of the observed and simulated array are removed before the computation.

    replace_inf: float, optional
        If given, indicates which value to replace Inf values with in the two arrays. If None, when
        an inf value is found at the i-th position in the observed OR simulated array, the i-th
        value of the observed and simulated array are removed before the computation.

    remove_neg: boolean, optional
        If True, when a negative value is found at the i-th position in the observed OR simulated
        array, the i-th value of the observed AND simulated array are removed before the
        computation.

    remove_zero: boolean, optional
        If true, when a zero value is found at the i-th position in the observed OR simulated
        array, the i-th value of the observed AND simulated array are removed before the
        computation.

    Examples
    --------

    Note that the last outlier residual in the time series is negated using the median.

    >>> import HydroErr as he
    >>> import numpy as np

    >>> sim = np.array([5, 7, 9, 2, 4.5, 6.7])
    >>> obs = np.array([4.7, 6, 10, 2.5, 4, 100])
    >>> he.mde(sim, obs)
    -0.10000000000000009

    Returns
    -------
    float
        The median error value.

    """

    # Checking and cleaning the data

    simulated_array, observed_array = treat_values(
        simulated_array,
        observed_array,
        replace_nan=replace_nan,
        replace_inf=replace_inf,
        remove_neg=remove_neg,
        remove_zero=remove_zero
    )

    return np.median(simulated_array - observed_array)

Response: Compute the median error between the simulated and observed data.


Instruction:
Can you tell what is the following Python 3 function doing
def mdae(simulated_array, observed_array, replace_nan=None, replace_inf=None,
         remove_neg=False, remove_zero=False):
    """
    Compute the median absolute error (MdAE) between the simulated and observed data.

    .. image:: /pictures/MdAE.png

    **Range** 0 ≤ MdAE < inf, closer to zero is better.

    **Notes** Random errors (noise) do not cancel. It is the same as the mean absolute error (MAE), only it takes the
    median rather than the mean. Median measures reduces the impact of outliers.

    Parameters
    ----------
    simulated_array: one dimensional ndarray
        An array of simulated data from the time series.

    observed_array: one dimensional ndarray
        An array of observed data from the time series.

    replace_nan: float, optional
        If given, indicates which value to replace NaN values with in the two arrays. If None, when
        a NaN value is found at the i-th position in the observed OR simulated array, the i-th value
        of the observed and simulated array are removed before the computation.

    replace_inf: float, optional
        If given, indicates which value to replace Inf values with in the two arrays. If None, when
        an inf value is found at the i-th position in the observed OR simulated array, the i-th
        value of the observed and simulated array are removed before the computation.

    remove_neg: boolean, optional
        If True, when a negative value is found at the i-th position in the observed OR simulated
        array, the i-th value of the observed AND simulated array are removed before the
        computation.

    remove_zero: boolean, optional
        If true, when a zero value is found at the i-th position in the observed OR simulated
        array, the i-th value of the observed AND simulated array are removed before the
        computation.

    Examples
    --------

    Note that the last outlier residual in the time series is negated using the median.

    >>> import HydroErr as he
    >>> import numpy as np

    >>> sim = np.array([5, 7, 9, 2, 4.5, 6.7])
    >>> obs = np.array([4.7, 6, 10, 2.5, 4, 100])
    >>> he.mdae(sim, obs)
    0.75

    Returns
    -------
    float
        The median absolute error value.

    """

    # Checking and cleaning the data

    simulated_array, observed_array = treat_values(
        simulated_array,
        observed_array,
        replace_nan=replace_nan,
        replace_inf=replace_inf,
        remove_neg=remove_neg,
        remove_zero=remove_zero
    )

    return np.median(np.abs(simulated_array - observed_array))

Response: Compute the median absolute error between the simulated and observed data.


Instruction:
Given the following Python 3 function, write the documentation
def ed(simulated_array, observed_array, replace_nan=None, replace_inf=None,
       remove_neg=False, remove_zero=False):
    """
    Compute the Euclidean distance between predicted and observed values in vector space.

    .. image:: /pictures/ED.png

    **Range** 0 ≤ ED < inf, smaller is better.
    **Notes** Also sometimes referred to as the L2-norm.

    Parameters
    ----------
    simulated_array: one dimensional ndarray
        An array of simulated data from the time series.

    observed_array: one dimensional ndarray
        An array of observed data from the time series.

    replace_nan: float, optional
        If given, indicates which value to replace NaN values with in the two arrays. If None, when
        a NaN value is found at the i-th position in the observed OR simulated array, the i-th value
        of the observed and simulated array are removed before the computation.

    replace_inf: float, optional
        If given, indicates which value to replace Inf values with in the two arrays. If None, when
        an inf value is found at the i-th position in the observed OR simulated array, the i-th
        value of the observed and simulated array are removed before the computation.

    remove_neg: boolean, optional
        If True, when a negative value is found at the i-th position in the observed OR simulated
        array, the i-th value of the observed AND simulated array are removed before the
        computation.

    remove_zero: boolean, optional
        If true, when a zero value is found at the i-th position in the observed OR simulated
        array, the i-th value of the observed AND simulated array are removed before the
        computation.

    Examples
    --------

    >>> import HydroErr as he
    >>> import numpy as np

    >>> sim = np.array([5, 7, 9, 2, 4.5, 6.7])
    >>> obs = np.array([4.7, 6, 10, 2.5, 4, 7])
    >>> he.ed(sim, obs)
    1.63707055437449

    Returns
    -------
    float
        The euclidean distance error value.

    References
    ----------
    - Kennard, M. J., Mackay, S. J., Pusey, B. J., Olden, J. D., & Marsh, N. (2010). Quantifying
      uncertainty in estimation of hydrologic metrics for ecohydrological studies. River Research
      and Applications, 26(2), 137-156.
    """

    # Checking and cleaning the data

    simulated_array, observed_array = treat_values(
        simulated_array,
        observed_array,
        replace_nan=replace_nan,
        replace_inf=replace_inf,
        remove_neg=remove_neg,
        remove_zero=remove_zero
    )

    return np.linalg.norm(observed_array - simulated_array)

Response: Compute the Euclidean distance between predicted and observed values in vector space.


Instruction:
Here you have a function in Python 3, explain what it does
def ned(simulated_array, observed_array, replace_nan=None, replace_inf=None,
        remove_neg=False, remove_zero=False):
    """
    Compute the normalized Euclidian distance between the simulated and observed data in vector
    space.

    .. image:: /pictures/NED.png

    **Range** 0 ≤ NED < inf, smaller is better.

    **Notes** Also sometimes referred to as the squared L2-norm.

    Parameters
    ----------
    simulated_array: one dimensional ndarray
        An array of simulated data from the time series.

    observed_array: one dimensional ndarray
        An array of observed data from the time series.

    replace_nan: float, optional
        If given, indicates which value to replace NaN values with in the two arrays. If None, when
        a NaN value is found at the i-th position in the observed OR simulated array, the i-th value
        of the observed and simulated array are removed before the computation.

    replace_inf: float, optional
        If given, indicates which value to replace Inf values with in the two arrays. If None, when
        an inf value is found at the i-th position in the observed OR simulated array, the i-th
        value of the observed and simulated array are removed before the computation.

    remove_neg: boolean, optional
        If True, when a negative value is found at the i-th position in the observed OR simulated
        array, the i-th value of the observed AND simulated array are removed before the
        computation.

    remove_zero: boolean, optional
        If true, when a zero value is found at the i-th position in the observed OR simulated
        array, the i-th value of the observed AND simulated array are removed before the
        computation.

    Returns
    -------
    float
        The normalized euclidean distance value.

    Examples
    --------
    >>> import HydroErr as he
    >>> import numpy as np

    >>> sim = np.array([5, 7, 9, 2, 4.5, 6.7])
    >>> obs = np.array([4.7, 6, 10, 2.5, 4, 7])
    >>> he.ned(sim, obs)
    0.2872053604165771

    References
    ----------
    - Kennard, M. J., Mackay, S. J., Pusey, B. J., Olden, J. D., & Marsh, N. (2010). Quantifying
      uncertainty in estimation of hydrologic metrics for ecohydrological studies. River Research
      and Applications, 26(2), 137-156.
    """

    # Checking and cleaning the data

    simulated_array, observed_array = treat_values(
        simulated_array,
        observed_array,
        replace_nan=replace_nan,
        replace_inf=replace_inf,
        remove_neg=remove_neg,
        remove_zero=remove_zero
    )

    a = observed_array / np.mean(observed_array)
    b = simulated_array / np.mean(simulated_array)
    return np.linalg.norm(a - b)

Response: Compute the normalized euclidean distance between the simulated and observed data.


Instruction:
Can you implement a function in Python 3 that
computes the range normalized root square error between the simulated and observed data sets.

Response:
def nrmse_range(simulated_array, observed_array, replace_nan=None, replace_inf=None,
                remove_neg=False, remove_zero=False):
    """Compute the range normalized root mean square error between the simulated and observed data.

    .. image:: /pictures/NRMSE_Range.png

    **Range:** 0 ≤ NRMSE < inf.

    **Notes:** This metric is the RMSE normalized by the range of the observed time series (x).
    Normalizing allows comparison between data sets with different scales. The NRMSErange is the
    most sensitive to outliers of the three normalized rmse metrics.

    Parameters
    ----------
    simulated_array: one dimensional ndarray
        An array of simulated data from the time series.

    observed_array: one dimensional ndarray
        An array of observed data from the time series.

    replace_nan: float, optional
        If given, indicates which value to replace NaN values with in the two arrays. If None, when
        a NaN value is found at the i-th position in the observed OR simulated array, the i-th value
        of the observed and simulated array are removed before the computation.

    replace_inf: float, optional
        If given, indicates which value to replace Inf values with in the two arrays. If None, when
        an inf value is found at the i-th position in the observed OR simulated array, the i-th
        value of the observed and simulated array are removed before the computation.

    remove_neg: boolean, optional
        If True, when a negative value is found at the i-th position in the observed OR simulated
        array, the i-th value of the observed AND simulated array are removed before the
        computation.

    remove_zero: boolean, optional
        If true, when a zero value is found at the i-th position in the observed OR simulated
        array, the i-th value of the observed AND simulated array are removed before the
        computation.

    Returns
    -------
    float
        The range normalized root mean square error value.

    Examples
    --------

    >>> import HydroErr as he
    >>> import numpy as np

    >>> sim = np.array([5, 7, 9, 2, 4.5, 6.7])
    >>> obs = np.array([4.7, 6, 10, 2.5, 4, 7])
    >>> he.nrmse_range(sim, obs)
    0.0891108340256152

    References
    ----------
    - Pontius, R.G., Thontteh, O., Chen, H., 2008. Components of information for multiple
      resolution comparison between maps that share a real variable. Environmental and Ecological
      Statistics 15(2) 111-142.
    """

    # Checking and cleaning the data

    simulated_array, observed_array = treat_values(
        simulated_array,
        observed_array,
        replace_nan=replace_nan,
        replace_inf=replace_inf,
        remove_neg=remove_neg,
        remove_zero=remove_zero
    )

    rmse_value = np.sqrt(np.mean((simulated_array - observed_array) ** 2))
    obs_max = np.max(observed_array)
    obs_min = np.min(observed_array)
    return rmse_value / (obs_max - obs_min)