"""
784. 字母大小写全排列
给定一个字符串S，通过将字符串S中的每个字母转变大小写，我们可以获得一个新的字符串。返回所有可能得到的字符串集合。

示例：
输入：S = "a1b2"
输出：["a1b2", "a1B2", "A1b2", "A1B2"]

输入：S = "3z4"
输出：["3z4", "3Z4"]

输入：S = "12345"
输出：["12345"]

提示：

S 的长度不超过12。
S 仅由数字和字母组成。

来源：力扣（LeetCode）
链接：https://leetcode-cn.com/problems/letter-case-permutation
著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
"""
from typing import List

class Solution:
    def letterCasePermutation1(self, S: str) -> List[str]:
        n = len(S)
        res = []

        def dfs(cur, s):
            if cur == n:
                res.append("".join(s))
                return
            if ord('A') <= ord(s[cur]) <= ord('Z'):
                tmp = s[cur]
                s[cur] = chr(ord(s[cur]) + 32)
                dfs(cur + 1, s)
                s[cur] = tmp
            elif ord('a') <= ord(s[cur]) <= ord('z'):
                tmp = s[cur]
                s[cur] = chr(ord(s[cur]) - 32)
                dfs(cur + 1, s)
                s[cur] = tmp

            dfs(cur + 1, s)

        dfs(0, list(S))
        return res

    def letterCasePermutation2(self, S):
        B = sum(letter.isalpha() for letter in S)
        ans = []

        for bits in range(1 << B):
            b = 0
            word = []
            for letter in S:
                if letter.isalpha():
                    if (bits >> b) & 1:
                        word.append(letter.lower())
                    else:
                        word.append(letter.upper())

                    b += 1
                else:
                    word.append(letter)

            ans.append("".join(word))
        return ans

784. 字母大小写全排列.py


import csv
import pandas as pd
from bs4 import BeautifulSoup
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

def get_url(search_term):
    template = "https://www.amazon.com.br/s?k={}"
    search_term = search_term.replace(' ', '+')

    url = template.format(search_term)

    url += '&page{}'
    return url

def extract_record(item):
    atag = item.h2.a
    description  = atag.text.strip()
    url = 'https://www.amazon.com.br' + atag.get('href')

    try:
        price_parent = item.find('span', 'a-price')
        price = price_parent.find('span', 'a-offscreen').text

    except AttributeError:
        return

    try:
        rating = item.i.text
        review_count = item.find('span', 'a-size-base s-underline-text').text
    except AttributeError:
        rating = ''
        review_count = ''

    result = (description, price, rating, review_count, url)

    return result

def main(search_term):
    driver = webdriver.Chrome(ChromeDriverManager().install())

    record = []
    url = get_url(search_term)

    for page in (1, 8):
        driver.get(url.format(page))
        soup = BeautifulSoup(driver.page_source, 'html.parser')
        results = soup.find_all('div', {'data-component-type': 's-search-result'})

        for item in results:
            record = extract_record(item)
            if record:
                records.append(record)

    driver.close()

    with open('results.csv', 'w', newline= '', encoding= 'utf-8' ) as f:
        writer = csv.writer(f)
        writer.writerow(['Description', 'Price', 'Rating', 'ReviewCount', 'Url'])
        writer.writerows(records)

main('data science')

df = pd.read_csv('results.csv', delimiter=';')
df = df[df['ReviewCount'].notna()]
df.to_csv('ds_books_results.csv', sep= ';',index=False,  encoding= 'utf-8',decimal='')  # fix to decimal place


'''
Author: your name
Date: 2021-10-20 10:07:05
LastEditTime: 2021-11-07 14:13:42
LastEditors: Please set LastEditors
Description: In User Settings Edit
FilePath: /mdap-multi-task/src/utils/util_model_eval.py
'''
import os
import torch
import numpy as np
from sklearn.metrics import accuracy_score, precision_recall_fscore_support
from typing import Dict
from args.arg_common import LOG_DIR
from utils.util_log import get_logger
from transformers import BertTokenizer
from args.arg_common import (
    TASKS
)

loggger = get_logger("model_eval",
                     os.path.join(LOG_DIR, "model_eval.log")
                     )

def multitask_eval(multitask_model,
                   tokenizer: BertTokenizer,
                   features_dict: Dict,
                   le_dict: Dict,
                   average_dict: Dict):
    for task_name in TASKS:
        val_len = len(features_dict[task_name]["test"])
        y_pred = []
        y_true = []
        for index in range(0, val_len):

            batch = features_dict[task_name]["test"][index]["text"]
            labels = features_dict[task_name]["test"][index]["label"]
            labels = le_dict[task_name].transform([labels])[0]

            inputs = tokenizer(batch, return_tensors="pt", max_length=512, pad_to_max_length=True)["input_ids"]
            inputs = inputs.to(device=torch.device('cuda' if torch.cuda.is_available() else 'cpu'))

            logits = multitask_model(inputs, task_name=task_name)[0]

            predictions = torch.argmax(
                torch.FloatTensor(torch.softmax(logits, dim=1).detach().cuda().tolist()),
                dim=1,
            )

            y_true.append(labels)
            y_pred.append(predictions.item())

        acc = accuracy_score(y_true, y_pred)
        precision, recall, f1, _ = precision_recall_fscore_support(y_true,
                                                                   y_pred,
                                                                   average=average_dict[task_name]
                                                                   )
        metrics = {
            "acc": acc,
            "precision": precision,
            "recall": recall,
            "f1": f1
        }
        loggger.info("The metric of [{}] task based of semh_model is : [{}]".format(task_name,
                                                                                str(metrics)
                                                                                )
                     )


import time

import requests

from eridu.logger import logger
from eridu.config import ACCESS_KEY, SITE
from eridu.config import POST_IDS_URL, POST_IDS_FILTER, POST_IDS_NUMBER, POST_IDS_SORT, POST_IDS_ORDER
from eridu.config import QUESTIONS_URL, QUESTIONS_FILTER, QUESTIONS_SORT, QUESTIONS_ORDER
from eridu.config import ANSWERS_URL, ANSWERS_FILTER, ANSWERS_SORT, ANSWERS_ORDER

def get_questions(question_ids, url=QUESTIONS_URL, filter=QUESTIONS_FILTER, access_key=ACCESS_KEY, site=SITE, sort=QUESTIONS_SORT, order=QUESTIONS_ORDER):
    url = url.format(';'.join([str(i) for i in question_ids]))

    payload = {
        "pagesize": 100,
        "key": access_key,
        "site": site,
        "sort": sort,
        "order": order,
        "filter": filter,
    }

    logger.info('Getting questions with payload: {}'.format(payload))

    r = requests.get(url, params=payload)

    data = r.json()

    if data.get('backoff') is not None:
        time.sleep(int(data.get('backoff')))

    logger.info('Remaining quota on /questions: {}'.format(data['quota_remaining']))

    return data

def get_answers(answer_ids, url=ANSWERS_URL, filter=ANSWERS_FILTER, access_key=ACCESS_KEY, site=SITE, sort=ANSWERS_SORT, order=ANSWERS_ORDER):
    url = url.format(';'.join([str(i) for i in answer_ids]))

    payload = {
        "pagesize": 100,
        "key": access_key,
        "site": site,
        "sort": sort,
        "order": order,
        "filter": filter,
    }

    logger.info('Getting answers with payload: {}'.format(payload))

    r = requests.get(url, params=payload)

    data = r.json()

    question_ids = [answer['question_id'] for answer in data['items']]

    questions = get_questions(question_ids)

    tags = {question['question_id']: question['tags'] for question in questions['items']}

    for answer in data['items']:
        answer['tags'] = tags[answer['question_id']]

    if data.get('backoff') is not None:
        time.sleep(int(data.get('backoff')))

    logger.info('Remaining quota on /answers: {}'.format(data['quota_remaining']))

    return data

def get_post_ids(page, url=POST_IDS_URL, filter=POST_IDS_FILTER, n_posts=POST_IDS_NUMBER, access_key=ACCESS_KEY, site=SITE, sort=POST_IDS_SORT, order=POST_IDS_ORDER):
    payload = {
        "pagesize": n_posts,
        "page": page,
        "key": access_key,
        "site": site,
        "sort": sort,
        "order": order,
        "filter": filter,
    }
    logger.info('Getting post ids with payload: {}'.format(payload))

    r = requests.get(url, params=payload)

    data = r.json()

    if data.get('backoff') is not None:
        time.sleep(int(data.get('backoff')))

    logger.info('Remaining quota on /posts: {}'.format(data['quota_remaining']))

    return data

def split_post_ids(post_ids):
    logger.info('Splitting post ids into question and answer ids')

    question_ids, answer_ids = [], []

    for item in post_ids:
        if item['post_type'] == "question":
            question_ids.append(item['post_id'])
        elif item['post_type'] == 'answer':
            answer_ids.append(item['post_id'])

    return {
        "question_ids": question_ids,
        "answer_ids": answer_ids
    }

def filter_posts_by_tag(posts, tags):
    tags = set(tags)
    return [post for post in posts if set(post['tags']) & tags]


import copy
import os
import os.path
import shutil
from typing import Dict, List
from firewall import config
from firewall.core.logger import log
from firewall.core.io.io_object import IO_Object
from firewall.core.io.icmptype import IcmpType, icmptype_reader, icmptype_writer
from firewall.core.io.service import Service, service_reader, service_writer
from firewall.core.io.zone import Zone, zone_reader, zone_writer
from firewall.core.io.ipset import IPSet, ipset_reader, ipset_writer
from firewall.core.io.helper import Helper, helper_reader, helper_writer
from firewall.core.io.policy import Policy, policy_reader, policy_writer
from firewall import errors
from firewall.errors import FirewallError

class FirewallConfig:
    def __init__(self, fw):
        self._fw = fw
        self.__init_vars()

    def __repr__(self):
        return '%s(%r, %r, %r, %r, %r, %r, %r, %r, %r, %r, %r, %r, %r, %r, %r)' % \
            (self.__class__,
             self._ipsets, self._icmptypes, self._services, self._zones,
             self._helpers, self.policy_objects,
             self._builtin_ipsets, self._builtin_icmptypes,
             self._builtin_services, self._builtin_zones, self._builtin_helpers,
             self._builtin_policy_objects,
             self._firewalld_conf, self._policies, self._direct)

    def __init_vars(self):
        self._ipsets = { }
        self._icmptypes = { }
        self._services = { }
        self._zones = { }
        self._helpers = { }
        self._policy_objects = { }
        self._builtin_ipsets = { }
        self._builtin_icmptypes = { }
        self._builtin_services = { }
        self._builtin_zones = { }
        self._builtin_helpers = { }
        self._builtin_policy_objects = { }
        self._firewalld_conf = None
        self._policies = None
        self._direct = None

    def cleanup(self):
        for x in list(self._builtin_ipsets.keys()):
            self._builtin_ipsets[x].cleanup()
            del self._builtin_ipsets[x]
        for x in list(self._ipsets.keys()):
            self._ipsets[x].cleanup()
            del self._ipsets[x]

        for x in list(self._builtin_icmptypes.keys()):
            self._builtin_icmptypes[x].cleanup()
            del self._builtin_icmptypes[x]
        for x in list(self._icmptypes.keys()):
            self._icmptypes[x].cleanup()
            del self._icmptypes[x]

        for x in list(self._builtin_services.keys()):
            self._builtin_services[x].cleanup()
            del self._builtin_services[x]
        for x in list(self._services.keys()):
            self._services[x].cleanup()
            del self._services[x]

        for x in list(self._builtin_zones.keys()):
            self._builtin_zones[x].cleanup()
            del self._builtin_zones[x]
        for x in list(self._zones.keys()):
            self._zones[x].cleanup()
            del self._zones[x]

        for x in list(self._builtin_helpers.keys()):
            self._builtin_helpers[x].cleanup()
            del self._builtin_helpers[x]
        for x in list(self._helpers.keys()):
            self._helpers[x].cleanup()
            del self._helpers[x]

        if self._firewalld_conf:
            self._firewalld_conf.cleanup()
            del self._firewalld_conf
            self._firewalld_conf = None

        if self._policies:
            self._policies.cleanup()
            del self._policies
            self._policies = None

        if self._direct:
            self._direct.cleanup()
            del self._direct
            self._direct = None

        self.__init_vars()

    def get_all_io_objects_dict(self):
        """
        Returns a dict of dicts of all permanent config objects.
        """
        conf_dict = {}
        conf_dict["ipsets"] = {ipset: self.get_ipset(ipset) for ipset in self.get_ipsets()}
        conf_dict["helpers"] = {helper: self.get_helper(helper) for helper in self.get_helpers()}
        conf_dict["icmptypes"] = {icmptype: self.get_icmptype(icmptype) for icmptype in self.get_icmptypes()}
        conf_dict["services"] = {service: self.get_service(service) for service in self.get_services()}
        conf_dict["zones"] = {zone: self.get_zone(zone) for zone in self.get_zones()}
        conf_dict["policies"] = {policy: self.get_policy_object(policy) for policy in self.get_policy_objects()}

        conf_dict["conf"] = {}
        conf_dict["conf"]["FirewallBackend"] = self._firewalld_conf.get("FirewallBackend")

        return conf_dict

    def full_check_config(self, extra_io_objects: Dict[str, List[IO_Object]] = {}):
        all_io_objects = self.get_all_io_objects_dict()
        # mix in the extra objects
        for type_key in extra_io_objects:
            for obj in extra_io_objects[type_key]:
                all_io_objects[type_key][obj.name] = obj

        # we need to check in a well defined order because some io_objects will
        # cross-check others
        order = ["ipsets", "helpers", "icmptypes", "services", "zones", "policies"]
        for io_obj_type in order:
            io_objs = all_io_objects[io_obj_type]
            for (name, io_obj) in io_objs.items():
                io_obj.check_config_dict(io_obj.export_config_dict(), all_io_objects)

    # access check

    def lockdown_enabled(self):
        return self._fw.policies.query_lockdown()

    def access_check(self, key, value):
        return self._fw.policies.access_check(key, value)

    # firewalld_conf

    def set_firewalld_conf(self, conf):
        self._firewalld_conf = conf

    def get_firewalld_conf(self):
        return self._firewalld_conf

    def update_firewalld_conf(self):
        if not os.path.exists(config.FIREWALLD_CONF):
            self._firewalld_conf.clear()
        else:
            self._firewalld_conf.read()

    def reset_defaults(self):
        order = [
                    self._zones, self._policy_objects, self._ipsets,
                    self._services, self._helpers, self._icmptypes
                ]
        for io_obj_dict in order:
            dict_copy = copy.copy(io_obj_dict)
            for obj_name in dict_copy:
                obj = dict_copy[obj_name]
                name = os.path.join(obj.path, obj.filename)
                try:
                    shutil.move(name, "%s.old" % name)
                except Exception as msg:
                    log.error("Backup of file '%s' failed: %s", name, msg)
                    os.remove(name)
                del io_obj_dict[obj_name]
        self._firewalld_conf.set_defaults()
        self._firewalld_conf.write()

    # policies

    def set_policies(self, policies):
        self._policies = policies

    def get_policies(self):
        return self._policies

    def update_lockdown_whitelist(self):
        if not os.path.exists(config.LOCKDOWN_WHITELIST):
            self._policies.lockdown_whitelist.cleanup()
        else:
            self._policies.lockdown_whitelist.read()

    # direct

    def set_direct(self, direct):
        self._direct = direct

    def get_direct(self):
        return self._direct

    def update_direct(self):
        if not os.path.exists(config.FIREWALLD_DIRECT):
            self._direct.cleanup()
        else:
            self._direct.read()

    # ipset

    def get_ipsets(self):
        return sorted(set(list(self._ipsets.keys()) + \
                          list(self._builtin_ipsets.keys())))

    def add_ipset(self, obj):
        if obj.builtin:
            self._builtin_ipsets[obj.name] = obj
        else:
            self._ipsets[obj.name] = obj

    def get_ipset(self, name):
        if name in self._ipsets:
            return self._ipsets[name]
        elif name in self._builtin_ipsets:
            return self._builtin_ipsets[name]
        raise FirewallError(errors.INVALID_IPSET, name)

    def load_ipset_defaults(self, obj):
        if obj.name not in self._ipsets:
            raise FirewallError(errors.NO_DEFAULTS, obj.name)
        elif self._ipsets[obj.name] != obj:
            raise FirewallError(errors.NO_DEFAULTS,
                                "self._ipsets[%s] != obj" % obj.name)
        elif obj.name not in self._builtin_ipsets:
            raise FirewallError(errors.NO_DEFAULTS,
                            "'%s' not a built-in ipset" % obj.name)
        self._remove_ipset(obj)
        return self._builtin_ipsets[obj.name]

    def get_ipset_config(self, obj):
        return obj.export_config()

    def set_ipset_config(self, obj, conf):
        x = copy.copy(obj)
        if obj.builtin:
            x.path = config.ETC_FIREWALLD_IPSETS
            x.builtin = False
            if obj.path != x.path:
                x.default = False

        x.import_config(conf, self.get_all_io_objects_dict())
        self.full_check_config({"ipsets": [x]})
        self.add_ipset(x)
        ipset_writer(x)
        return x

    def new_ipset(self, name, conf):
        if name in self._ipsets or name in self._builtin_ipsets:
            raise FirewallError(errors.NAME_CONFLICT,
                                "new_ipset(): '%s'" % name)

        x = IPSet()
        x.check_name(name)
        x.name = name
        x.filename = "%s.xml" % name
        x.path = config.ETC_FIREWALLD_IPSETS
        # It is not possible to add a new one with a name of a buitin
        x.builtin = False
        x.default = True

        x.import_config(conf, self.get_all_io_objects_dict())
        self.full_check_config({"ipsets": [x]})
        self.add_ipset(x)
        ipset_writer(x)
        return x

    def update_ipset_from_path(self, name):
        filename = os.path.basename(name)
        path = os.path.dirname(name)

        if not os.path.exists(name):
            # removed file

            if path == config.ETC_FIREWALLD_IPSETS:
                # removed custom ipset
                for x in self._ipsets.keys():
                    obj = self._ipsets[x]
                    if obj.filename == filename:
                        del self._ipsets[x]
                        if obj.name in self._builtin_ipsets:
                            return ("update", self._builtin_ipsets[obj.name])
                        return ("remove", obj)
            else:
                # removed builtin ipset
                for x in self._builtin_ipsets.keys():
                    obj = self._builtin_ipsets[x]
                    if obj.filename == filename:
                        del self._builtin_ipsets[x]
                        if obj.name not in self._ipsets:
                            # update dbus ipset
                            return ("remove", obj)
                        else:
                            # builtin hidden, no update needed
                            return (None, None)

            # ipset not known to firewalld, yet (timeout, ..)
            return (None, None)

        # new or updated file

        log.debug1("Loading ipset file '%s'", name)
        try:
            obj = ipset_reader(filename, path)
        except Exception as msg:
            log.error("Failed to load ipset file '%s': %s", filename, msg)
            return (None, None)

        # new ipset
        if obj.name not in self._builtin_ipsets and obj.name not in self._ipsets:
            self.add_ipset(obj)
            return ("new", obj)

        # updated ipset
        if path == config.ETC_FIREWALLD_IPSETS:
            # custom ipset update
            if obj.name in self._ipsets:
                obj.default = self._ipsets[obj.name].default
                self._ipsets[obj.name] = obj
            return ("update", obj)
        else:
            if obj.name in self._builtin_ipsets:
                # builtin ipset update
                del self._builtin_ipsets[obj.name]
                self._builtin_ipsets[obj.name] = obj

                if obj.name not in self._ipsets:
                    # update dbus ipset
                    return ("update", obj)
                else:
                    # builtin hidden, no update needed
                    return (None, None)

        # ipset not known to firewalld, yet (timeout, ..)
        return (None, None)

    def _remove_ipset(self, obj):
        if obj.name not in self._ipsets:
            raise FirewallError(errors.INVALID_IPSET, obj.name)
        if obj.path != config.ETC_FIREWALLD_IPSETS:
            raise FirewallError(errors.INVALID_DIRECTORY,
                                "'%s' != '%s'" % (obj.path,
                                                  config.ETC_FIREWALLD_IPSETS))

        name = "%s/%s.xml" % (obj.path, obj.name)
        try:
            shutil.move(name, "%s.old" % name)
        except Exception as msg:
            log.error("Backup of file '%s' failed: %s", name, msg)
            os.remove(name)

        del self._ipsets[obj.name]

    def check_builtin_ipset(self, obj):
        if obj.builtin or not obj.default:
            raise FirewallError(errors.BUILTIN_IPSET,
                                "'%s' is built-in ipset" % obj.name)

    def remove_ipset(self, obj):
        self.check_builtin_ipset(obj)
        self._remove_ipset(obj)

    def rename_ipset(self, obj, name):
        self.check_builtin_ipset(obj)
        new_ipset = self._copy_ipset(obj, name)
        self._remove_ipset(obj)
        return new_ipset

    def _copy_ipset(self, obj, name):
        return self.new_ipset(name, obj.export_config())

    # icmptypes

    def get_icmptypes(self):
        return sorted(set(list(self._icmptypes.keys()) + \
                          list(self._builtin_icmptypes.keys())))

    def add_icmptype(self, obj):
        if obj.builtin:
            self._builtin_icmptypes[obj.name] = obj
        else:
            self._icmptypes[obj.name] = obj

    def get_icmptype(self, name):
        if name in self._icmptypes:
            return self._icmptypes[name]
        elif name in self._builtin_icmptypes:
            return self._builtin_icmptypes[name]
        raise FirewallError(errors.INVALID_ICMPTYPE, name)

    def load_icmptype_defaults(self, obj):
        if obj.name not in self._icmptypes:
            raise FirewallError(errors.NO_DEFAULTS, obj.name)
        elif self._icmptypes[obj.name] != obj:
            raise FirewallError(errors.NO_DEFAULTS,
                                "self._icmptypes[%s] != obj" % obj.name)
        elif obj.name not in self._builtin_icmptypes:
            raise FirewallError(errors.NO_DEFAULTS,
                                "'%s' not a built-in icmptype" % obj.name)
        self._remove_icmptype(obj)
        return self._builtin_icmptypes[obj.name]

    def get_icmptype_config(self, obj):
        return obj.export_config()

    def set_icmptype_config(self, obj, conf):
        x = copy.copy(obj)
        if obj.builtin:
            x.path = config.ETC_FIREWALLD_ICMPTYPES
            x.builtin = False
            if obj.path != x.path:
                x.default = False

        x.import_config(conf, self.get_all_io_objects_dict())
        self.full_check_config({"icmptypes": [x]})
        self.add_icmptype(x)
        icmptype_writer(x)
        return x

    def new_icmptype(self, name, conf):
        if name in self._icmptypes or name in self._builtin_icmptypes:
            raise FirewallError(errors.NAME_CONFLICT,
                                "new_icmptype(): '%s'" % name)

        x = IcmpType()
        x.check_name(name)
        x.name = name
        x.filename = "%s.xml" % name
        x.path = config.ETC_FIREWALLD_ICMPTYPES
        # It is not possible to add a new one with a name of a buitin
        x.builtin = False
        x.default = True

        x.import_config(conf, self.get_all_io_objects_dict())
        self.full_check_config({"icmptypes": [x]})
        self.add_icmptype(x)
        icmptype_writer(x)
        return x

    def update_icmptype_from_path(self, name):
        filename = os.path.basename(name)
        path = os.path.dirname(name)

        if not os.path.exists(name):
            # removed file

            if path == config.ETC_FIREWALLD_ICMPTYPES:
                # removed custom icmptype
                for x in self._icmptypes.keys():
                    obj = self._icmptypes[x]
                    if obj.filename == filename:
                        del self._icmptypes[x]
                        if obj.name in self._builtin_icmptypes:
                            return ("update", self._builtin_icmptypes[obj.name])
                        return ("remove", obj)
            else:
                # removed builtin icmptype
                for x in self._builtin_icmptypes.keys():
                    obj = self._builtin_icmptypes[x]
                    if obj.filename == filename:
                        del self._builtin_icmptypes[x]
                        if obj.name not in self._icmptypes:
                            # update dbus icmptype
                            return ("remove", obj)
                        else:
                            # builtin hidden, no update needed
                            return (None, None)

            # icmptype not known to firewalld, yet (timeout, ..)
            return (None, None)

        # new or updated file

        log.debug1("Loading icmptype file '%s'", name)
        try:
            obj = icmptype_reader(filename, path)
        except Exception as msg:
            log.error("Failed to load icmptype file '%s': %s", filename, msg)
            return (None, None)

        # new icmptype
        if obj.name not in self._builtin_icmptypes and obj.name not in self._icmptypes:
            self.add_icmptype(obj)
            return ("new", obj)

        # updated icmptype
        if path == config.ETC_FIREWALLD_ICMPTYPES:
            # custom icmptype update
            if obj.name in self._icmptypes:
                obj.default = self._icmptypes[obj.name].default
                self._icmptypes[obj.name] = obj
            return ("update", obj)
        else:
            if obj.name in self._builtin_icmptypes:
                # builtin icmptype update
                del self._builtin_icmptypes[obj.name]
                self._builtin_icmptypes[obj.name] = obj

                if obj.name not in self._icmptypes:
                    # update dbus icmptype
                    return ("update", obj)
                else:
                    # builtin hidden, no update needed
                    return (None, None)

        # icmptype not known to firewalld, yet (timeout, ..)
        return (None, None)

    def _remove_icmptype(self, obj):
        if obj.name not in self._icmptypes:
            raise FirewallError(errors.INVALID_ICMPTYPE, obj.name)
        if obj.path != config.ETC_FIREWALLD_ICMPTYPES:
            raise FirewallError(errors.INVALID_DIRECTORY,
                                "'%s' != '%s'" % \
                                (obj.path, config.ETC_FIREWALLD_ICMPTYPES))

        name = "%s/%s.xml" % (obj.path, obj.name)
        try:
            shutil.move(name, "%s.old" % name)
        except Exception as msg:
            log.error("Backup of file '%s' failed: %s", name, msg)
            os.remove(name)

        del self._icmptypes[obj.name]

    def check_builtin_icmptype(self, obj):
        if obj.builtin or not obj.default:
            raise FirewallError(errors.BUILTIN_ICMPTYPE,
                                "'%s' is built-in icmp type" % obj.name)

    def remove_icmptype(self, obj):
        self.check_builtin_icmptype(obj)
        self._remove_icmptype(obj)

    def rename_icmptype(self, obj, name):
        self.check_builtin_icmptype(obj)
        new_icmptype = self._copy_icmptype(obj, name)
        self._remove_icmptype(obj)
        return new_icmptype

    def _copy_icmptype(self, obj, name):
        return self.new_icmptype(name, obj.export_config())

    # services

    def get_services(self):
        return sorted(set(list(self._services.keys()) + \
                          list(self._builtin_services.keys())))

    def add_service(self, obj):
        if obj.builtin:
            self._builtin_services[obj.name] = obj
        else:
            self._services[obj.name] = obj

    def get_service(self, name):
        if name in self._services:
            return self._services[name]
        elif name in self._builtin_services:
            return self._builtin_services[name]
        raise FirewallError(errors.INVALID_SERVICE, "get_service(): '%s'" % name)

    def load_service_defaults(self, obj):
        if obj.name not in self._services:
            raise FirewallError(errors.NO_DEFAULTS, obj.name)
        elif self._services[obj.name] != obj:
            raise FirewallError(errors.NO_DEFAULTS,
                                "self._services[%s] != obj" % obj.name)
        elif obj.name not in self._builtin_services:
            raise FirewallError(errors.NO_DEFAULTS,
                                "'%s' not a built-in service" % obj.name)
        self._remove_service(obj)
        return self._builtin_services[obj.name]

    def get_service_config(self, obj):
        conf_dict = obj.export_config_dict()
        conf_list = []
        for i in range(8): # tuple based dbus API has 8 elements
            if obj.IMPORT_EXPORT_STRUCTURE[i][0] not in conf_dict:
                # old API needs the empty elements as well. Grab it from the
                # object otherwise we don't know the type.
                conf_list.append(copy.deepcopy(getattr(obj, obj.IMPORT_EXPORT_STRUCTURE[i][0])))
            else:
                conf_list.append(conf_dict[obj.IMPORT_EXPORT_STRUCTURE[i][0]])
        return tuple(conf_list)

    def get_service_config_dict(self, obj):
        return obj.export_config_dict()

    def set_service_config(self, obj, conf):
        conf_dict = {}
        for i,value in enumerate(conf):
            conf_dict[obj.IMPORT_EXPORT_STRUCTURE[i][0]] = value

        return self.set_service_config_dict(obj, conf_dict)

    def set_service_config_dict(self, obj, conf):
        x = copy.copy(obj)
        if obj.builtin:
            x.path = config.ETC_FIREWALLD_SERVICES
            x.builtin = False
            if obj.path != x.path:
                x.default = False

        x.import_config_dict(conf, self.get_all_io_objects_dict())
        self.full_check_config({"services": [x]})
        self.add_service(x)
        service_writer(x)
        return x

    def new_service(self, name, conf):
        if name in self._services or name in self._builtin_services:
            raise FirewallError(errors.NAME_CONFLICT,
                                "new_service(): '%s'" % name)

        conf_dict = {}
        for i,value in enumerate(conf):
            conf_dict[Service.IMPORT_EXPORT_STRUCTURE[i][0]] = value

        return self.new_service_dict(name, conf_dict)

    def new_service_dict(self, name, conf):
        if name in self._services or name in self._builtin_services:
            raise FirewallError(errors.NAME_CONFLICT,
                                "new_service(): '%s'" % name)

        x = Service()
        x.check_name(name)
        x.name = name
        x.filename = "%s.xml" % name
        x.path = config.ETC_FIREWALLD_SERVICES
        # It is not possible to add a new one with a name of a buitin
        x.builtin = False
        x.default = True

        x.import_config_dict(conf, self.get_all_io_objects_dict())
        self.full_check_config({"services": [x]})
        self.add_service(x)
        service_writer(x)
        return x

    def update_service_from_path(self, name):
        filename = os.path.basename(name)
        path = os.path.dirname(name)

        if not os.path.exists(name):
            # removed file

            if path == config.ETC_FIREWALLD_SERVICES:
                # removed custom service
                for x in self._services.keys():
                    obj = self._services[x]
                    if obj.filename == filename:
                        del self._services[x]
                        if obj.name in self._builtin_services:
                            return ("update", self._builtin_services[obj.name])
                        return ("remove", obj)
            else:
                # removed builtin service
                for x in self._builtin_services.keys():
                    obj = self._builtin_services[x]
                    if obj.filename == filename:
                        del self._builtin_services[x]
                        if obj.name not in self._services:
                            # update dbus service
                            return ("remove", obj)
                        else:
                            # builtin hidden, no update needed
                            return (None, None)

            # service not known to firewalld, yet (timeout, ..)
            return (None, None)

        # new or updated file

        log.debug1("Loading service file '%s'", name)
        try:
            obj = service_reader(filename, path)
        except Exception as msg:
            log.error("Failed to load service file '%s': %s", filename, msg)
            return (None, None)

        # new service
        if obj.name not in self._builtin_services and obj.name not in self._services:
            self.add_service(obj)
            return ("new", obj)

        # updated service
        if path == config.ETC_FIREWALLD_SERVICES:
            # custom service update
            if obj.name in self._services:
                obj.default = self._services[obj.name].default
                self._services[obj.name] = obj
            return ("update", obj)
        else:
            if obj.name in self._builtin_services:
                # builtin service update
                del self._builtin_services[obj.name]
                self._builtin_services[obj.name] = obj

                if obj.name not in self._services:
                    # update dbus service
                    return ("update", obj)
                else:
                    # builtin hidden, no update needed
                    return (None, None)

        # service not known to firewalld, yet (timeout, ..)
        return (None, None)

    def _remove_service(self, obj):
        if obj.name not in self._services:
            raise FirewallError(errors.INVALID_SERVICE, obj.name)
        if obj.path != config.ETC_FIREWALLD_SERVICES:
            raise FirewallError(errors.INVALID_DIRECTORY,
                                "'%s' != '%s'" % \
                                (obj.path, config.ETC_FIREWALLD_SERVICES))

        name = "%s/%s.xml" % (obj.path, obj.name)
        try:
            shutil.move(name, "%s.old" % name)
        except Exception as msg:
            log.error("Backup of file '%s' failed: %s", name, msg)
            os.remove(name)

        del self._services[obj.name]

    def check_builtin_service(self, obj):
        if obj.builtin or not obj.default:
            raise FirewallError(errors.BUILTIN_SERVICE,
                                "'%s' is built-in service" % obj.name)

    def remove_service(self, obj):
        self.check_builtin_service(obj)
        self._remove_service(obj)

    def rename_service(self, obj, name):
        self.check_builtin_service(obj)
        new_service = self._copy_service(obj, name)
        self._remove_service(obj)
        return new_service

    def _copy_service(self, obj, name):
        return self.new_service_dict(name, obj.export_config_dict())

    # zones

    def get_zones(self):
        return sorted(set(list(self._zones.keys()) + \
                          list(self._builtin_zones.keys())))

    def add_zone(self, obj):
        if obj.builtin:
            self._builtin_zones[obj.name] = obj
        else:
            self._zones[obj.name] = obj

    def forget_zone(self, name):
        if name in self._builtin_zones:
            del self._builtin_zones[name]
        if name in self._zones:
            del self._zones[name]

    def get_zone(self, name):
        if name in self._zones:
            return self._zones[name]
        elif name in self._builtin_zones:
            return self._builtin_zones[name]
        raise FirewallError(errors.INVALID_ZONE, "get_zone(): %s" % name)

    def load_zone_defaults(self, obj):
        if obj.name not in self._zones:
            raise FirewallError(errors.NO_DEFAULTS, obj.name)
        elif self._zones[obj.name] != obj:
            raise FirewallError(errors.NO_DEFAULTS,
                                "self._zones[%s] != obj" % obj.name)
        elif obj.name not in self._builtin_zones:
            raise FirewallError(errors.NO_DEFAULTS,
                                "'%s' not a built-in zone" % obj.name)
        self._remove_zone(obj)
        return self._builtin_zones[obj.name]

    def get_zone_config(self, obj):
        conf_dict = obj.export_config_dict()
        conf_list = []
        for i in range(16): # tuple based dbus API has 16 elements
            if obj.IMPORT_EXPORT_STRUCTURE[i][0] not in conf_dict:
                # old API needs the empty elements as well. Grab it from the
                # object otherwise we don't know the type.
                conf_list.append(copy.deepcopy(getattr(obj, obj.IMPORT_EXPORT_STRUCTURE[i][0])))
            else:
                conf_list.append(conf_dict[obj.IMPORT_EXPORT_STRUCTURE[i][0]])
        return tuple(conf_list)

    def get_zone_config_dict(self, obj):
        return obj.export_config_dict()

    def set_zone_config(self, obj, conf):
        conf_dict = {}
        for i,value in enumerate(conf):
            conf_dict[obj.IMPORT_EXPORT_STRUCTURE[i][0]] = value

        return self.set_zone_config_dict(obj, conf_dict)

    def set_zone_config_dict(self, obj, conf):
        x = copy.copy(obj)
        if obj.builtin:
            x.path = config.ETC_FIREWALLD_ZONES
            x.builtin = False
            if obj.path != x.path:
                x.default = False

        x.import_config_dict(conf, self.get_all_io_objects_dict())
        self.full_check_config({"zones": [x]})
        self.add_zone(x)
        zone_writer(x)
        return x

    def new_zone(self, name, conf):
        if name in self._zones or name in self._builtin_zones:
            raise FirewallError(errors.NAME_CONFLICT, "new_zone(): '%s'" % name)

        conf_dict = {}
        for i,value in enumerate(conf):
            conf_dict[Zone.IMPORT_EXPORT_STRUCTURE[i][0]] = value

        return self.new_zone_dict(name, conf_dict)

    def new_zone_dict(self, name, conf):
        if name in self._zones or name in self._builtin_zones:
            raise FirewallError(errors.NAME_CONFLICT, "new_zone(): '%s'" % name)

        x = Zone()
        x.check_name(name)
        x.name = name
        x.filename = "%s.xml" % name
        x.path = config.ETC_FIREWALLD_ZONES
        # It is not possible to add a new one with a name of a buitin
        x.builtin = False
        x.default = True

        x.import_config_dict(conf, self.get_all_io_objects_dict())
        self.full_check_config({"zones": [x]})
        self.add_zone(x)
        zone_writer(x)
        return x

    def update_zone_from_path(self, name):
        filename = os.path.basename(name)
        path = os.path.dirname(name)

        if not os.path.exists(name):
            # removed file

            if path.startswith(config.ETC_FIREWALLD_ZONES):
                # removed custom zone
                for x in self._zones.keys():
                    obj = self._zones[x]
                    if obj.filename == filename:
                        del self._zones[x]
                        if obj.name in self._builtin_zones:
                            return ("update", self._builtin_zones[obj.name])
                        return ("remove", obj)
            else:
                # removed builtin zone
                for x in self._builtin_zones.keys():
                    obj = self._builtin_zones[x]
                    if obj.filename == filename:
                        del self._builtin_zones[x]
                        if obj.name not in self._zones:
                            # update dbus zone
                            return ("remove", obj)
                        else:
                            # builtin hidden, no update needed
                            return (None, None)

            # zone not known to firewalld, yet (timeout, ..)
            return (None, None)

        # new or updated file

        log.debug1("Loading zone file '%s'", name)
        try:
            obj = zone_reader(filename, path)
        except Exception as msg:
            log.error("Failed to load zone file '%s': %s", filename, msg)
            return (None, None)

        if path.startswith(config.ETC_FIREWALLD_ZONES) and \
           len(path) > len(config.ETC_FIREWALLD_ZONES):
            # custom combined zone part
            obj.name = "%s/%s" % (os.path.basename(path),
                                  os.path.basename(filename)[0:-4])

        # new zone
        if obj.name not in self._builtin_zones and obj.name not in self._zones:
            self.add_zone(obj)
            return ("new", obj)

        # updated zone
        if path.startswith(config.ETC_FIREWALLD_ZONES):
            # custom zone update
            if obj.name in self._zones:
                obj.default = self._zones[obj.name].default
                self._zones[obj.name] = obj
            return ("update", obj)
        else:
            if obj.name in self._builtin_zones:
                # builtin zone update
                del self._builtin_zones[obj.name]
                self._builtin_zones[obj.name] = obj

                if obj.name not in self._zones:
                    # update dbus zone
                    return ("update", obj)
                else:
                    # builtin hidden, no update needed
                    return (None, None)

        # zone not known to firewalld, yet (timeout, ..)
        return (None, None)

    def _remove_zone(self, obj):
        if obj.name not in self._zones:
            raise FirewallError(errors.INVALID_ZONE, obj.name)
        if not obj.path.startswith(config.ETC_FIREWALLD_ZONES):
            raise FirewallError(errors.INVALID_DIRECTORY,
                                "'%s' doesn't start with '%s'" % \
                                (obj.path, config.ETC_FIREWALLD_ZONES))

        name = "%s/%s.xml" % (obj.path, obj.name)
        try:
            shutil.move(name, "%s.old" % name)
        except Exception as msg:
            log.error("Backup of file '%s' failed: %s", name, msg)
            os.remove(name)

        del self._zones[obj.name]

    def check_builtin_zone(self, obj):
        if obj.builtin or not obj.default:
            raise FirewallError(errors.BUILTIN_ZONE,
                                "'%s' is built-in zone" % obj.name)

    def remove_zone(self, obj):
        self.check_builtin_zone(obj)
        self._remove_zone(obj)

    def rename_zone(self, obj, name):
        self.check_builtin_zone(obj)
        obj_conf = obj.export_config_dict()
        self._remove_zone(obj)
        try:
            new_zone = self.new_zone_dict(name, obj_conf)
        except:
            # re-add original if rename failed
            self.new_zone_dict(obj.name, obj_conf)
            raise
        return new_zone

    # policy objects

    def get_policy_objects(self):
        return sorted(set(list(self._policy_objects.keys()) + \
                          list(self._builtin_policy_objects.keys())))

    def add_policy_object(self, obj):
        if obj.builtin:
            self._builtin_policy_objects[obj.name] = obj
        else:
            self._policy_objects[obj.name] = obj

    def get_policy_object(self, name):
        if name in self._policy_objects:
            return self._policy_objects[name]
        elif name in self._builtin_policy_objects:
            return self._builtin_policy_objects[name]
        raise FirewallError(errors.INVALID_POLICY, "get_policy_object(): %s" % name)

    def load_policy_object_defaults(self, obj):
        if obj.name not in self._policy_objects:
            raise FirewallError(errors.NO_DEFAULTS, obj.name)
        elif self._policy_objects[obj.name] != obj:
            raise FirewallError(errors.NO_DEFAULTS,
                                "self._policy_objects[%s] != obj" % obj.name)
        elif obj.name not in self._builtin_policy_objects:
            raise FirewallError(errors.NO_DEFAULTS,
                                "'%s' not a built-in policy" % obj.name)
        self._remove_policy_object(obj)
        return self._builtin_policy_objects[obj.name]

    def get_policy_object_config_dict(self, obj):
        return obj.export_config_dict()

    def set_policy_object_config_dict(self, obj, conf):
        x = copy.copy(obj)
        if obj.builtin:
            x.path = config.ETC_FIREWALLD_POLICIES
            x.builtin = False
            if obj.path != x.path:
                x.default = False

        x.import_config_dict(conf, self.get_all_io_objects_dict())
        self.full_check_config({"policies": [x]})
        self.add_policy_object(x)
        policy_writer(x)
        return x

    def new_policy_object_dict(self, name, conf):
        if name in self._policy_objects or name in self._builtin_policy_objects:
            raise FirewallError(errors.NAME_CONFLICT, "new_policy_object(): '%s'" % name)

        x = Policy()
        x.check_name(name)
        x.name = name
        x.filename = "%s.xml" % name
        x.path = config.ETC_FIREWALLD_POLICIES
        # It is not possible to add a new one with a name of a buitin
        x.builtin = False
        x.default = True

        x.import_config_dict(conf, self.get_all_io_objects_dict())
        self.full_check_config({"policies": [x]})
        self.add_policy_object(x)
        policy_writer(x)
        return x

    def update_policy_object_from_path(self, name):
        filename = os.path.basename(name)
        path = os.path.dirname(name)

        if not os.path.exists(name):
            # removed file

            if path.startswith(config.ETC_FIREWALLD_POLICIES):
                # removed custom policy_object
                for x in self._policy_objects.keys():
                    obj = self._policy_objects[x]
                    if obj.filename == filename:
                        del self._policy_objects[x]
                        if obj.name in self._builtin_policy_objects:
                            return ("update", self._builtin_policy_objects[obj.name])
                        return ("remove", obj)
            else:
                # removed builtin policy_object
                for x in self._builtin_policy_objects.keys():
                    obj = self._builtin_policy_objects[x]
                    if obj.filename == filename:
                        del self._builtin_policy_objects[x]
                        if obj.name not in self._policy_objects:
                            # update dbus policy_object
                            return ("remove", obj)
                        else:
                            # builtin hidden, no update needed
                            return (None, None)

            # policy_object not known to firewalld, yet (timeout, ..)
            return (None, None)

        # new or updated file

        log.debug1("Loading policy file '%s'", name)
        try:
            obj = policy_reader(filename, path)
        except Exception as msg:
            log.error("Failed to load policy file '%s': %s", filename, msg)
            return (None, None)

        if path.startswith(config.ETC_FIREWALLD_POLICIES) and \
           len(path) > len(config.ETC_FIREWALLD_POLICIES):
            # custom combined policy_object part
            obj.name = "%s/%s" % (os.path.basename(path),
                                  os.path.basename(filename)[0:-4])

        # new policy_object
        if obj.name not in self._builtin_policy_objects and obj.name not in self._policy_objects:
            self.add_policy_object(obj)
            return ("new", obj)

        # updated policy_object
        if path.startswith(config.ETC_FIREWALLD_POLICIES):
            # custom policy_object update
            if obj.name in self._policy_objects:
                obj.default = self._policy_objects[obj.name].default
                self._policy_objects[obj.name] = obj
            return ("update", obj)
        else:
            if obj.name in self._builtin_policy_objects:
                # builtin policy_object update
                del self._builtin_policy_objects[obj.name]
                self._builtin_policy_objects[obj.name] = obj

                if obj.name not in self._policy_objects:
                    # update dbus policy_object
                    return ("update", obj)
                else:
                    # builtin hidden, no update needed
                    return (None, None)

        # policy_object not known to firewalld, yet (timeout, ..)
        return (None, None)

    def _remove_policy_object(self, obj):
        if obj.name not in self._policy_objects:
            raise FirewallError(errors.INVALID_POLICY, obj.name)
        if not obj.path.startswith(config.ETC_FIREWALLD_POLICIES):
            raise FirewallError(errors.INVALID_DIRECTORY,
                                "'%s' doesn't start with '%s'" % \
                                (obj.path, config.ETC_FIREWALLD_POLICIES))

        name = "%s/%s.xml" % (obj.path, obj.name)
        try:
            shutil.move(name, "%s.old" % name)
        except Exception as msg:
            log.error("Backup of file '%s' failed: %s", name, msg)
            os.remove(name)

        del self._policy_objects[obj.name]

    def check_builtin_policy_object(self, obj):
        if obj.builtin or not obj.default:
            raise FirewallError(errors.BUILTIN_POLICY,
                                "'%s' is built-in policy" % obj.name)

    def remove_policy_object(self, obj):
        self.check_builtin_policy_object(obj)
        self._remove_policy_object(obj)

    def rename_policy_object(self, obj, name):
        self.check_builtin_policy_object(obj)
        new_policy_object = self._copy_policy_object(obj, name)
        self._remove_policy_object(obj)
        return new_policy_object

    def _copy_policy_object(self, obj, name):
        return self.new_policy_object_dict(name, obj.export_config_dict())

    # helper

    def get_helpers(self):
        return sorted(set(list(self._helpers.keys()) + \
                          list(self._builtin_helpers.keys())))

    def add_helper(self, obj):
        if obj.builtin:
            self._builtin_helpers[obj.name] = obj
        else:
            self._helpers[obj.name] = obj

    def get_helper(self, name):
        if name in self._helpers:
            return self._helpers[name]
        elif name in self._builtin_helpers:
            return self._builtin_helpers[name]
        raise FirewallError(errors.INVALID_HELPER, name)

    def load_helper_defaults(self, obj):
        if obj.name not in self._helpers:
            raise FirewallError(errors.NO_DEFAULTS, obj.name)
        elif self._helpers[obj.name] != obj:
            raise FirewallError(errors.NO_DEFAULTS,
                                "self._helpers[%s] != obj" % obj.name)
        elif obj.name not in self._builtin_helpers:
            raise FirewallError(errors.NO_DEFAULTS,
                            "'%s' not a built-in helper" % obj.name)
        self._remove_helper(obj)
        return self._builtin_helpers[obj.name]

    def get_helper_config(self, obj):
        return obj.export_config()

    def set_helper_config(self, obj, conf):
        x = copy.copy(obj)
        if obj.builtin:
            x.path = config.ETC_FIREWALLD_HELPERS
            x.builtin = False
            if obj.path != x.path:
                x.default = False

        x.import_config(conf, self.get_all_io_objects_dict())
        self.full_check_config({"helpers": [x]})
        self.add_helper(x)
        helper_writer(x)
        return x

    def new_helper(self, name, conf):
        if name in self._helpers or name in self._builtin_helpers:
            raise FirewallError(errors.NAME_CONFLICT,
                                "new_helper(): '%s'" % name)

        x = Helper()
        x.check_name(name)
        x.name = name
        x.filename = "%s.xml" % name
        x.path = config.ETC_FIREWALLD_HELPERS
        # It is not possible to add a new one with a name of a buitin
        x.builtin = False
        x.default = True

        x.import_config(conf, self.get_all_io_objects_dict())
        self.full_check_config({"helpers": [x]})
        self.add_helper(x)
        helper_writer(x)
        return x

    def update_helper_from_path(self, name):
        filename = os.path.basename(name)
        path = os.path.dirname(name)

        if not os.path.exists(name):
            # removed file

            if path == config.ETC_FIREWALLD_HELPERS:
                # removed custom helper
                for x in self._helpers.keys():
                    obj = self._helpers[x]
                    if obj.filename == filename:
                        del self._helpers[x]
                        if obj.name in self._builtin_helpers:
                            return ("update", self._builtin_helpers[obj.name])
                        return ("remove", obj)
            else:
                # removed builtin helper
                for x in self._builtin_helpers.keys():
                    obj = self._builtin_helpers[x]
                    if obj.filename == filename:
                        del self._builtin_helpers[x]
                        if obj.name not in self._helpers:
                            # update dbus helper
                            return ("remove", obj)
                        else:
                            # builtin hidden, no update needed
                            return (None, None)

            # helper not known to firewalld, yet (timeout, ..)
            return (None, None)

        # new or updated file

        log.debug1("Loading helper file '%s'", name)
        try:
            obj = helper_reader(filename, path)
        except Exception as msg:
            log.error("Failed to load helper file '%s': %s", filename, msg)
            return (None, None)

        # new helper
        if obj.name not in self._builtin_helpers and obj.name not in self._helpers:
            self.add_helper(obj)
            return ("new", obj)

        # updated helper
        if path == config.ETC_FIREWALLD_HELPERS:
            # custom helper update
            if obj.name in self._helpers:
                obj.default = self._helpers[obj.name].default
                self._helpers[obj.name] = obj
            return ("update", obj)
        else:
            if obj.name in self._builtin_helpers:
                # builtin helper update
                del self._builtin_helpers[obj.name]
                self._builtin_helpers[obj.name] = obj

                if obj.name not in self._helpers:
                    # update dbus helper
                    return ("update", obj)
                else:
                    # builtin hidden, no update needed
                    return (None, None)

        # helper not known to firewalld, yet (timeout, ..)
        return (None, None)

    def _remove_helper(self, obj):
        if obj.name not in self._helpers:
            raise FirewallError(errors.INVALID_HELPER, obj.name)
        if obj.path != config.ETC_FIREWALLD_HELPERS:
            raise FirewallError(errors.INVALID_DIRECTORY,
                                "'%s' != '%s'" % (obj.path,
                                                  config.ETC_FIREWALLD_HELPERS))

        name = "%s/%s.xml" % (obj.path, obj.name)
        try:
            shutil.move(name, "%s.old" % name)
        except Exception as msg:
            log.error("Backup of file '%s' failed: %s", name, msg)
            os.remove(name)

        del self._helpers[obj.name]

    def check_builtin_helper(self, obj):
        if obj.builtin or not obj.default:
            raise FirewallError(errors.BUILTIN_HELPER,
                                "'%s' is built-in helper" % obj.name)

    def remove_helper(self, obj):
        self.check_builtin_helper(obj)
        self._remove_helper(obj)

    def rename_helper(self, obj, name):
        self.check_builtin_helper(obj)
        new_helper = self._copy_helper(obj, name)
        self._remove_helper(obj)
        return new_helper

    def _copy_helper(self, obj, name):
        return self.new_helper(name, obj.export_config())


#GUItranslator
from tkinter import *
from tkinter import ttk
from googletrans import Translator
translator = Translator()

GUI = Tk()
GUI.geometry('500x300')
GUI.title('boykrap by chiangmai')

FONT = ('Angsana New',15)

L = ttk.Label(GUI,text='กรุรากรอกคำศัพท์ที่ต้องการ',font=FONT)
L.pack()

#Entry
v_vocab = StringVar()
E1 = ttk.Entry(GUI,textvariable = v_vocab,font=FONT,width=40)
E1.pack(pady=20)

#button
def Translate():
    vocab = v_vocab.get()
    meaning = translator.translate(vocab,dest='th')
    print(vocab+' '+meaning.text)
    v_result.set(vocab+' '+meaning.text)

B1 = ttk.Button(GUI,text='Translate',command=Translate)
B1.pack(ipadx=20,ipady=10)

L = ttk.Label(GUI,text='คำแปล',font=FONT)
L.pack(pady=20)

v_result = StringVar()
R1 = ttk.Label(GUI,textvariable=v_result,font=FONT)

GUI.mainloop()


from collections import Counter, defaultdict
from itertools import combinations, chain
import re
import math

with open("9.in") as f:
#with open("test9.in") as f:
    inp = f.read().strip()
    inp = inp.splitlines()

inp = [list(map(int, list(x))) for x in inp]

dirs = [(0, 1), (0, -1), (1, 0), (-1, 0)]

low = []

def inBounds(li, vi):
    return (li in range(len(inp))) and (vi in range(len(inp[0])))

def getNeighbors(li, vi):
    return [inp[li + dx][vi + dy] for dx, dy in dirs if inBounds(li + dx, vi + dy)]

for li, line in enumerate(inp):
    for vi, val in enumerate(line):
        if all(val < x for x in getNeighbors(li, vi)):
            low.append((li, vi))

scores = [inp[li][vi] + 1 for li, vi in low]
print(sum(scores))

# part 2

def countBasin(li, vi, seen):
    if (li, vi) in seen or not inBounds(li, vi) or inp[li][vi] == 9:
        return 0
    seen.add((li, vi))

    return 1 + sum(countBasin(li + dx, vi + dy, seen) for dx, dy in dirs)

basins = [countBasin(li, vi, set()) for li, vi in low]
result = sorted(basins)
print(math.prod(result[-3:]))


from sys import stdin

class COMmod:
    def __init__(self, n, mod) -> None:
        self.fact = [1] * (n + 1)
        self.rfact = [1] * (n + 1)
        self.mod = mod
        r = 1
        for i in range(1, n + 1):
            self.fact[i] = r = r * i % mod
        self.rfact[n] = r = pow(self.fact[n], mod - 2, mod)
        for i in range(n, 0, -1):
            self.rfact[i - 1] = r = r * i % mod

    def perm(self, n, k):
        return self.fact[n] * self.rfact[n - k] % self.mod

    def comb(self, n, k):
        return self.fact[n] * self.rfact[k] * self.rfact[n - k] % self.mod

readline = stdin.readline
w, h = map(int, readline().split())
mod = 10**9 + 7
commod = COMmod(w + h - 2, mod)
print(commod.comb(w + h - 2, h - 1))


from data.coco import COCO_ROOT
import json
import torch
import numpy as np
import os
os.environ["CUDA_VISIBLE_DEVICES"] = '1'

## 修改这里的绝对地址 ##
## CHANGE PATH HERE ##
detection_results_path = r'eval\detection_results2021-09-05_05-15.pth'
save_path = r'.\detections_test-dev2017_ssdoptreal_results.json'
##       END        ##
anno_pth =COCO_ROOT + r'coco2017\Annotations\image_info_test-dev2017.json'
# anno_pth = r'D:\COCO\annotations_train2017\annotations\instances_val2017.json'
with open(anno_pth, 'r') as f:
    anno = json.load(f)
img_name2id = dict([(o['file_name'].rstrip('.jpg'), o['id']) for o in anno['images']])
cat_name2id = dict([(o['name'], o['id']) for o in anno['categories']])

dets = torch.load(detection_results_path)
formatted = []
for det in dets:
    img_name = det['img_name']
    cat_name = det['cat_name']
    xmin, ymin, xmax, ymax = det['bbox']
    score = det['score']
    if isinstance(score, str):
        score = float(score)
        xmin, ymin, xmax, ymax = float(xmin), float(ymin), float(xmax), float(ymax)
    x = round(xmin, 1)
    y = round(ymin, 1)
    width = round(xmax - xmin, 1)
    height = round(ymax - ymin, 1)
    formatted.append({
        'image_id': img_name2id[img_name],
        'category_id': cat_name2id[cat_name],
        'bbox': [x, y, width, height],
        'score': round(score, 3)
    })
del dets
# keep top-k bboxes, k=100
k = 100
id2bboxes = dict()
for i, d in enumerate(formatted):
    if d['image_id'] not in id2bboxes.keys():
        id2bboxes[d['image_id']] = []
    id2bboxes[d['image_id']].append(i)
for img_id, ids in id2bboxes.items():
    id2bboxes[img_id] = np.array(ids)[np.array([formatted[i]['score'] for i in ids]).argsort()[::-1]][:k]

formatted = np.array(formatted)[np.concatenate(list(id2bboxes.values()))].tolist()

with open(save_path, 'w') as f:
    json.dump(formatted, f, separators=(',', ':'))


""" Treasure module base class for v4 Munchkin"""
from random import randint

f = 0
card = None

"""
may shift functions and change dict function object to ref in another class
"""
def immune():
    """immune to curses"""
    print("I love my Tinfoil Hat")

def once():
    """Send item to 'used' list"""
    print("This item has been depleted")

def escape():
    """roll to escape combat, may need: return once()"""
    print("This item has been depleted")

def deflect():
    """1-3 deflect curse, 4-6 run, 6 go up level"""
    print("The curse rebounds")

class Treasure(object):
    """special methods"""

    used = [] # non-accessible list of used items (not to be used again during game play)

    """All treasure items, immutable tuple with dict, name, description, bonus, sell, special, type"""

    treasure_cards = [({"name":"Electric Radioactive Acid Potion", "des": "Use during any combat. +5 to either side.",
                       "bonus": 5, "sell": 200, 'type': once}),
                      ({"name":"Flaming Poison Potion", "des": "Use during combat", "bonus": 3, "sell": 100,
                        'type': once}),
                      ({'name':"Instant Wall", 'des': 'Automatic escape for 1 or 2 players', 'sell': 300,
                        'type': once}),
                      ({"name":"Flask Of Glue", "des": "Use during combat, must re-roll escape even if auto last time",
                       "bonus": 0, "sell": 100, "special": escape}),

                      ({"name":"The Occasionally Reliable Amulet", 'des':'If equipped, chance to deflect curse',
                        'sell': 600, 'special': deflect}),
                      ({"name":"Tinfoil Hat", "des": "Immune to curses", "bonus": 0, "sell": 800, "special": immune}),
                      ]

    @staticmethod
    def select():
        """get tupel from list and display contents"""
        global f
        global card
        f = randint(0, 5)
        card = Treasure.treasure_cards[f]
        # return card #return to child caller for sorting

        print(Treasure.treasure_cards[f])
        print(card.get('name'))
        print(card.get('des'))
        #print(card.get('bonus'))
        #print(card.get('sell'))
        try:
            x = Treasure.treasure_cards[f].get('special')
            x() # not work with class/static methods
        except TypeError:
            print("This item has no special")

            try:
                y = Treasure.treasure_cards[f].get('type')
                y()
                Treasure.used.append(card) # appends card to used list
            except TypeError:
                print("This item has no type")

Treasure.select()
print(f"\nCards in used pile:\n {Treasure.used}") # shows what card is in used pile, function object expected.


'''
cette fonction permet de calculer l'indice corporel de masse d'une personne
'''

print("Ce programme vous permet de calculer votre indice corporel de masse")
def calcul_imc():
    poids = float(input("Entrez votre poids kg svp: "))
    taille = float(input("Entrez votre taille en cm svp: "))

    taille2 = taille/100

    imc = round(poids/taille2**2, 1)

    print(f"Votre IMC est: {imc}")
    if imc < 18.5:
        print("Insuffisance pondérale")
    elif 18.5 <= imc < 25:
        print("Corpulence normale")
    elif 25 <= imc < 30:
        print("Surpoids")
    else:
        print("Non prise en charge")
calcul_imc()


class Solution:
    def equalPairs(self, grid: List[List[int]]) -> int:
        rows = collections.defaultdict(int)

        for row in grid:
            temp = ''
            for r in row:
                temp += str(r) + ','
            rows[temp] += 1

        ans = 0
        for col in range(len(grid[0])):
            temp = ''
            for r in range(len(grid)):
                temp += str(grid[r][col]) + ','
            ans += rows[temp]

        return ans

Python3/2352. Equal Row and Column Pairs.py


import fixturesUtils
import imageUtils

import mayaUtils
import testUtils

from maya import cmds

import os

class testVP2RenderDelegateTextureLoading(imageUtils.ImageDiffingTestCase):
    """
    Test texture loading in sync and async mode.
    """

    @classmethod
    def setUpClass(cls):
        input_path = fixturesUtils.setUpClass(
            __file__, initializeStandalone=False, loadPlugin=False
        )

        cls._baseline_dir = os.path.join(
            input_path, "VP2RenderDelegateTextureLoadingTest", "baseline"
        )

        cls._test_dir = os.path.abspath(".")

        cls._optVarName = "mayaUsd_DisableAsyncTextureLoading"
        # Save optionVar preference
        cls._hasDisabledAsync = cmds.optionVar(exists=cls._optVarName)
        if cls._hasDisabledAsync:
            cls._prevDisableAsync = cmds.optionVar(q=cls._optVarName)

        mayaUtils.loadPlugin("mayaUsdPlugin")
        # Resume running idle tasks
        cmds.flushIdleQueue(resume=True)

    @classmethod
    def tearDownClass(cls):
        # Restore user optionVar
        if cls._hasDisabledAsync:
            cmds.optionVar(iv=(cls._optVarName, cls._prevDisableAsync))
        else:
            cmds.optionVar(remove=cls._optVarName)

    def assertSnapshotClose(self, imageName):
        baseline_image = os.path.join(self._baseline_dir, imageName)
        snapshot_image = os.path.join(self._test_dir, imageName)
        imageUtils.snapshot(snapshot_image, width=768, height=768)
        return self.assertImagesClose(baseline_image, snapshot_image)

    def testTextureLoadingSync(self):
        cmds.file(force=True, new=True)

        # Make sure the sync mode is ON (disable async loading)
        cmds.optionVar(iv=(self._optVarName, 1))

        cmds.xform("persp", t=(2, 2, 5.8))
        cmds.xform("persp", ro=[0, 0, 0], ws=True)

        panel = mayaUtils.activeModelPanel()
        cmds.modelEditor(panel, edit=True, lights=False, displayLights="default", displayTextures=True)

        testFile = testUtils.getTestScene("multipleMaterialsAssignment",
                                          "MultipleMaterialsAssignment.usda")

        # Default purpose mode is "proxy"
        shapeNode, _ = mayaUtils.createProxyFromFile(testFile)
        cmds.select(cl=True)
        self.assertSnapshotClose("TextureLoading_Proxy_Sync.png")

        # Switch purpose to "render"
        cmds.setAttr("{}.drawProxyPurpose".format(shapeNode), 0)
        cmds.setAttr("{}.drawRenderPurpose".format(shapeNode), 1)

        cmds.select(cl=True)
        self.assertSnapshotClose("TextureLoading_Render_Sync.png")

    def testTextureLoadingAsync(self):
        cmds.file(force=True, new=True)
        # Make sure the async mode is ON (enable async loading)
        cmds.optionVar(iv=(self._optVarName, 0))

        cmds.xform("persp", t=(2, 2, 5.8))
        cmds.xform("persp", ro=[0, 0, 0], ws=True)

        panel = mayaUtils.activeModelPanel()
        cmds.modelEditor(panel, edit=True, lights=False, displayLights="default", displayTextures=True)

        testFile = testUtils.getTestScene("multipleMaterialsAssignment",
                                          "MultipleMaterialsAssignment.usda")

        # Default purpose mode is "proxy"
        shapeNode, _ = mayaUtils.createProxyFromFile(testFile)
        cmds.select(cl=True)

        # Force all idle tasks to finish
        cmds.flushIdleQueue()
        self.assertSnapshotClose("TextureLoading_Proxy_Async.png")

        # Switch purpose to "render"
        cmds.setAttr("{}.drawProxyPurpose".format(shapeNode), 0)
        cmds.setAttr("{}.drawRenderPurpose".format(shapeNode), 1)

        # Force all idle tasks to finish
        cmds.flushIdleQueue()
        self.assertSnapshotClose("TextureLoading_Render_Async.png")

if __name__ == '__main__':
    fixturesUtils.runTests(globals())


#!/usr/bin/python3
""" class base """
import json
from os import path

class Base:
    """ class """
    __nb_objects = 0

    def __init__(self, id=None):
        """ class initializer"""
        if id is not None:
            self.id = id
        else:
            Base.__nb_objects += 1
            self.id = Base.__nb_objects

    @staticmethod
    def to_json_string(list_dictionaries):
        """json metohd"""
        if list_dictionaries is None:
            list_dictionaries = []
        return json.dumps(list_dictionaries)

    @classmethod
    def save_to_file(cls, list_objs):
        """writes the JSON string representation of list_objs to a file"""
        file = cls.__name__ + ".json"
        empty = []
        if list_objs:
            for i in list_objs:
                empty.append(cls.to_dictionary(i))
        with open(file, "w") as w:
            w.write(cls.to_json_string(empty))

    @classmethod
    def create(cls, **dictionary):
        """returns an instance with all attributes already set"""
        if cls.__name__ is "Rectangle":
            from models.rectangle import Rectangle
            obj = Rectangle(1, 1)
        elif cls.__name__ is "Square":
            from models.square import Square
            obj = Square(1)
        obj.update(**dictionary)
        return obj

    @classmethod
    def load_from_file(cls):
        """returns a list of instances"""
        file = cls.__name__ + ".json"
        if not path.isfile(file):
            return []
        with open(file, "r", encoding="utf-8") as f:
            return [cls.create(**d) for d in cls.from_json_string(f.read())]

    @staticmethod
    def from_json_string(json_string):
        """returns the list of the JSON string representation json_string"""
        if json_string is None or len(json_string) == 0:
            return []
        return json.loads(json_string)


"""Implement Nexus (nxos) Specific Configurations for Evpn objects.
"""

# Table of contents:
#     class Evpn:
#         class InterfaceAttributes:
#             def build_config/build_unconfig:
#             class EthernetSegmentAttributes:
#                 def build_config/build_unconfig:
#                 class BgpAttributes:
#                     def build_config/build_unconfig:
#         class DeviceAttributes:
#             def build_config/build_unconfig:

from abc import ABC
import warnings

from genie.conf.base.attributes import UnsupportedAttributeWarning, AttributesHelper
from genie.conf.base.cli import CliConfigBuilder
import genie.conf.base.interface
from genie.conf.base.config import CliConfig

class Evpn(ABC):

    class VniAttributes(ABC):

        def build_config(self, apply=True, attributes=None, unconfig=False):
            assert not apply
            attributes = AttributesHelper(self, attributes)
            configurations = CliConfigBuilder(unconfig=unconfig)

            # nxos: evpn / vni 4096 l2 (config-evpn-evi)
            with configurations.submode_context(attributes.format('vni {vni.vni_id} l2', force=True)):
                if unconfig and attributes.iswildcard:
                    configurations.submode_unconfig()

                ns, attributes2 = attributes.namespace('ethernet_segment')
                if ns is not None:
                    configurations.append_block(ns.build_config(apply=False, attributes=attributes2, unconfig=unconfig))

            return str(configurations)

        def build_unconfig(self, apply=True, attributes=None):
            return self.build_config(apply=apply, attributes=attributes, unconfig=True)

        class EthernetSegmentAttributes(ABC):

            def build_config(self, apply=True, attributes=None, unconfig=False):
                assert not apply
                attributes = AttributesHelper(self, attributes)
                configurations = CliConfigBuilder(unconfig=unconfig)

                ns, attributes2 = attributes.namespace('bgp')
                if ns is not None:
                    configurations.append_block(ns.build_config(apply=False, attributes=attributes2, unconfig=unconfig))

                return str(configurations)

            def build_unconfig(self, apply=True, attributes=None):
                return self.build_config(apply=apply, attributes=attributes, unconfig=True)

            class BgpAttributes(ABC):

                def build_config(self, apply=True, attributes=None, unconfig=False):
                    assert not apply
                    attributes = AttributesHelper(self, attributes)
                    configurations = CliConfigBuilder(unconfig=unconfig)

                    if attributes.value('enabled', force=True):

                        # nxos: evpn / vni 4096 l2 / bgp
                        if attributes.value('enabled'):
                            configurations.append_line('bgp')

                        # nxos: evpn / vni 4096 l2 / rd auto
                        # nxos: evpn / vni 4096 l2 / rd 100:200000
                        # nxos: evpn / vni 4096 l2 / rd 100000:200
                        # nxos: evpn / vni 4096 l2 / rd 1.2.3.4:1
                        configurations.append_line(attributes.format('rd {rd}'))

                        # nxos: evpn / vni 4096 l2 / route-target both auto
                        # nxos: evpn / vni 4096 l2 / route-target both 100:200000
                        # nxos: evpn / vni 4096 l2 / route-target both 100000:200
                        # nxos: evpn / vni 4096 l2 / route-target both 1.2.3.4:1
                        both_route_targets = set(self.export_route_targets) & set(self.import_route_targets)

                        # nxos: evpn / vni 4096 l2 / route-target export auto  # XXXJST how does this match none in IOS-XR?
                        # nxos: evpn / vni 4096 l2 / route-target export 100:200000
                        # nxos: evpn / vni 4096 l2 / route-target export 100000:200
                        # nxos: evpn / vni 4096 l2 / route-target export 1.2.3.4:1
                        for v, attributes2 in attributes.sequence_values('export_route_targets'):
                            if v == 'auto':
                                cfg = 'route-target {} {}'.format(
                                    'both' if v in both_route_targets else 'export',
                                    v)
                            else:
                                cfg = 'route-target {} {}'.format(
                                    'both' if v in both_route_targets else 'export',
                                    v.route_target)
                                if v.stitching:
                                    warnings.warn(
                                        'export bgp route-target stitching',
                                        UnsupportedAttributeWarning)
                            configurations.append_line(cfg)

                        # nxos: evpn / vni 4096 l2 / route-target import auto
                        # nxos: evpn / vni 4096 l2 / route-target import 100:200000
                        # nxos: evpn / vni 4096 l2 / route-target import 100000:200
                        # nxos: evpn / vni 4096 l2 / route-target import 1.2.3.4:1
                        for v, attributes2 in attributes.sequence_values('import_route_targets'):
                            if v == 'auto':
                                cfg = 'route-target import {}'.format(v)
                            else:
                                cfg = 'route-target import {}'.format(v.route_target)
                                if v.stitching:
                                    warnings.warn(
                                        'import bgp route-target stitching',
                                        UnsupportedAttributeWarning)
                            configurations.append_line(cfg)

                    return str(configurations)

                def build_unconfig(self, apply=True, attributes=None):
                    return self.build_config(apply=apply, attributes=attributes, unconfig=True)

    class DeviceAttributes(ABC):

        def build_config(self, apply=True, attributes=None, unconfig=False):
            attributes = AttributesHelper(self, attributes)
            configurations = CliConfigBuilder(unconfig=unconfig)

            # nxos: evpn esi multihoming

            # nxos: evpn (config-evpn)
            with configurations.submode_context('evpn'):
                if unconfig and attributes.iswildcard:
                    configurations.submode_unconfig()

                # nxos: evpn / vni 4096 l2 (config-evpn-evi)
                for sub, attributes2 in attributes.mapping_values('vni_attr', keys=self.vnis, sort=True):
                    configurations.append_block(sub.build_config(apply=False, attributes=attributes2, unconfig=unconfig))

            if apply:
                if configurations:
                    self.device.configure(configurations, fail_invalid=True)
            else:
                return CliConfig(device=self.device, unconfig=unconfig,
                                 cli_config=configurations, fail_invalid=True)

        def build_unconfig(self, apply=True, attributes=None):
            return self.build_config(apply=apply, attributes=attributes, unconfig=True)


import argparse
import os
import subprocess

default_format = "png"
default_qscale_jpg = "4"

def extract_frames(main_data, resolution, format, q, quiet,
                   transcoded, codec, crf, keyint):

    if transcoded:
        desc = [resolution, 'scenes']
        desc += [codec] if codec else []
        desc += ["crf"+crf] if crf else []
        desc += ["keyint"+keyint] if keyint else []
        in_path = os.path.join(main_data,*desc)
    else:
        if codec:
            raise ValueError("--codec specified, but not --transcoded");
        if crf:
            raise ValueError("--crf specified, but not --transcoded");
        if keyint:
            raise ValueError("--keyint specified, but not --transcoded");
        in_path = os.path.join(main_data,'orig','scenes')

    desc = [resolution,'frames']
    desc += [codec] if codec else []
    desc += ["crf"+crf] if crf else []
    desc += ["keyint"+keyint] if keyint else []
    if not format:
        format = default_format
    else:
        desc += [format]

    if not q:
        if format == "jpg":
            q = default_qscale_jpg
    else:
        desc += ["q" + q]

    out_path = os.path.join(main_data,*desc)

    res_args = []
    if resolution == '4K':
        pass
    else:
        if resolution == '1080p':
            res_str = '1920:1080'
        elif resolution == '720p':
            res_str = '1280:720'
        elif resolution == '540p':
            res_str = '960:540'
        else:
            raise ValueError("Unknown resolution")
        res_args += ["-vf", "scale=%s" % res_str, "-sws_flags", "bilinear"]

    codec_args = []
    if format == "png":
        if q:
            codec_args += ["-compression_level", q]
    elif format == "jpg":
        codec_args += ["-q:v", q]
    else:
        raise ValueError("Unknown format")

    if quiet:
        cmdout = subprocess.DEVNULL
    else:
        cmdout = None

    for subset_name, subset_dir in [('training', 'train'), ('validation', 'val')]:
        if not os.path.exists(os.path.join(in_path,subset_dir)):
            raise ValueError("No "+subset_name+" data found in "+in_path+", " +
                             "did you run split_scenes.py?")

        for in_file in os.listdir(os.path.join(in_path,subset_dir)):
            if in_file.endswith('.mp4'):
                scene = in_file.split('_')[1].split('.')[0]
                cur_out_path = os.path.join(out_path,subset_dir,scene)
                if not os.path.isdir(cur_out_path):
                    os.makedirs(cur_out_path)
                cur_in_path = os.path.join(in_path,subset_dir,in_file)
                cmd = ["ffmpeg", "-n", "-i", cur_in_path]
                cmd += res_args
                cmd += codec_args
                cmd += [os.path.join(cur_out_path, "%05d."+format)]
                print("Running:", " ".join(cmd))
                subprocess.run(cmd, stdout=cmdout, stderr=cmdout)

if __name__=='__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--main_data', type=str, required=True,
                        help="Path to root data directory")
    parser.add_argument('--resolution', type=str, required=True,
                        choices=['4K', '1080p', '720p', '540p'])
    parser.add_argument('--format', type=str,
                        choices=['png', 'jpg'])
    parser.add_argument('-q', type=str,
                        help="quality to use for compression [2-31] for jpg and [0-9] for png")
    parser.add_argument('--transcoded', action='store_true',
                        help="Use transcoded videos instead of original split video")
    parser.add_argument('--quiet', action='store_true',
                        help="Suppress ffmpeg output")
    parser.add_argument('--codec', type=str, default=None,
                        choices=['h264', 'hevc'],
                        help="codec of transcoded video to use")
    parser.add_argument('--crf', type=str, default=None,
                        help="crf value of transcoded video to use")
    parser.add_argument('--keyint', type=str, default=None,
                        help="keyframe interval of transcoded video to use")
    args = parser.parse_args()
    extract_frames(**vars(args))


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.3.3
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # s_reg_truncated_lfm [<img src="https://www.arpm.co/lab/icons/icon_permalink.png" width=30 height=30 style="display: inline;">](https://www.arpm.co/lab/redirect.php?code=s_reg_truncated_lfm&codeLang=Python)
# For details, see [here](https://www.arpm.co/lab/redirect.php?permalink=eb-trunc-time).

# +
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
from mpl_toolkits.mplot3d import Axes3D

from arpym.statistics.meancov_sp import meancov_sp
from arpym.estimation.fit_lfm_ols import fit_lfm_ols
from arpym.estimation.cov_2_corr import cov_2_corr
from arpym.tools.histogram_sp import histogram_sp
from arpym.tools.logo import add_logo
# -

# ## [Input parameters](https://www.arpm.co/lab/redirect.php?permalink=s_reg_truncated_lfm-parameters)

spot = np.array([0, 1, 9])  # targets and factors to spot
n_long = 61  # long index
n_short = np.array([366, 244])  # short indices

# ## [Step 0](https://www.arpm.co/lab/redirect.php?permalink=s_reg_truncated_lfm-implementation-step00): Load data

# +
path = '~/databases/global-databases/equities/db_stocks_SP500/'
data = pd.read_csv(path + 'db_stocks_sp.csv', index_col=0, header=[0, 1],
                   parse_dates=True)
idx_sector = pd.read_csv(path + 'db_sector_idx.csv', index_col=0,
                         parse_dates=True)
idx_sector = idx_sector.drop("RealEstate", axis=1)  # delete RealEstate

dates = np.intersect1d(data.index, idx_sector.index)
data = data.loc[dates]
idx_sector = idx_sector.loc[dates]

t_ = len(data.index) - 1
n_ = len(data.columns)
k_ = len(idx_sector.columns)
# -

# ## [Step 1](https://www.arpm.co/lab/redirect.php?permalink=s_reg_truncated_lfm-implementation-step01): Compute linear returns of X and Z

v_stock = data.values  # stock values
x = (v_stock[1:, :] - v_stock[:-1, :]) / v_stock[:-1, :]  # linear return of the stock values
v_sector = idx_sector.values  # sector indices
z = (v_sector[1:, :] - v_sector[:-1, :]) / v_sector[:-1, :]  # linear return of the sector indices

# ## [Step 2](https://www.arpm.co/lab/redirect.php?permalink=s_reg_truncated_lfm-implementation-step02): Compute OLSFP estimates and residuals

alpha, beta, s2, eps = fit_lfm_ols(x, z) #  compute OLSFP estimates and residuals

# ## [Step 3](https://www.arpm.co/lab/redirect.php?permalink=s_reg_truncated_lfm-implementation-step03): Compute the joint covariance and correlation

# +
# compute covariance
[mu_epsz, sig2_epsz] = meancov_sp(np.hstack((eps, z)))  # compute covariance between ε and Z
sig2_eps = sig2_epsz[:n_, :n_]  # variance of ε
sig2_z = sig2_epsz[n_:, n_:]  # variance of Z

# compute correlation
c2_epsz, _ = cov_2_corr(sig2_epsz)  #  compute correlation between ε and Z
c_epsz = c2_epsz[:n_, n_:]
c2_eps = np.tril(c2_epsz[:n_, :n_], -1)  # correlation among residuals
# -

# ## [Step 4](https://www.arpm.co/lab/redirect.php?permalink=s_reg_truncated_lfm-implementation-step04): Compute standard deviations of two portfolios

# +
w_1 = np.ones(n_) / n_  # equal weight portfolio
w_2 = np.zeros(n_)  # long/short weight portfolio
w_2[n_long] = 0.69158715  # long weight portfolio
w_2[n_short] = np.array([-0.67752045, -0.01406671])  # short weight portfolio

_, sig2_x = meancov_sp(x)  # compute historical covariance of Xt
sig2_x_trunc = beta @ sig2_z @ beta.T + np.diag(np.diag(sig2_eps))  # truncated target covariance of Xt

std_1 = np.sqrt(w_1.T @ sig2_x @ w_1)  # standard deviation of the equal weight portfolio from sig2_x
std_trunc_1 = np.sqrt(w_1.T @ sig2_x_trunc @ w_1)  # standard deviation of the euqal weight portfolio from sig2_x_trunc

std_2 = np.sqrt(w_2.T @ sig2_x @ w_2)  # standard deviation of the long/short weight portfolio from sig2_x
std_trunc_2 = np.sqrt(w_2.T @ sig2_x_trunc @ w_2)  # standard deviation of the long/short weight portfolio from sig2_x_trunc
# -

# ## Plots

# +
# (untruncated) correlations among residuals
corr_eps = c2_eps[np.nonzero(c2_eps)]  # reshape the correlations
n, xout = histogram_sp(corr_eps)

mydpi = 72.0
fig = plt.figure(figsize=(1280.0/mydpi,720.0/mydpi),dpi=mydpi)
ax0 = plt.axes([0.595, 0.83, 0.92, 0.45])
ax0.plot(corr_eps.mean(),0,'ro')
plt.xlim(-0.6, 1.6)
plt.ylim(0, 7)
h = ax0.bar(xout, n, width=xout[1]-xout[0], facecolor=[.7, .7, .7], edgecolor='k')
plt.text(0.24, 6.2, r'$\mathbb{C}$' + r'$r$' + r'$\{\.ε_m\, \.ε_n\}$',
         fontsize=20)
plt.xlabel(r'Correlation values', fontsize=17)
plt.ylabel(r'Frequencies', fontsize=17)
ax0.yaxis.set_major_locator(MaxNLocator(integer=True))
plt.title('Cross correlations in regression LFM')

c2_x, _ = cov_2_corr(sig2_x)
c2_x = np.tril(c2_x[:n_, :n_], -1)
corr_x = c2_x[np.nonzero(c2_x)]  # reshape the correlations
n, xout = histogram_sp(corr_x)

ax1 = plt.axes([0.595, 0.3, 0.92, 0.45])
plt.xlim(-0.6, 1.6)
plt.ylim(0, 4)
ax1.plot(corr_x.mean(),0,'ro')
ax1.axes.get_xaxis().set_ticks([])
h1 = ax1.bar(xout, n, width=xout[1]-xout[0], facecolor=[.7, .7, .7], edgecolor='k')
plt.ylabel(r'Frequencies', fontsize=17)
plt.gca().invert_yaxis()
plt.text(0.6, 3.8, r'$\mathbb{C}$' + r'$r$' + r'$\{X_{m,t}, X_{n,t}\}$',
         fontsize=20)
ax1.yaxis.set_major_locator(MaxNLocator(integer=True))

add_logo(fig, location=4)
# -