Code Snippet

平时在写代码的过程中经常会使用的代码片段记录在这个地方,方便自己的查找~

Python 实现celery动态加载task

# When celery after initialise we register our task into the celery.
@app.on_after_configure.connect
def setup_celery_tasks(sender, **kwargs):
    for task_name, task_option in Settings.jobs_config.get("jobs", {}).items():
        module_path = 'apps.{0}.tasks.{1}'.format(task_option.get("module"), task_name)
        try:
            ip_module = importlib.import_module(module_path)
            ip_module_class = getattr(ip_module, task_option.get("class"))
            ip_module_class.options = task_option.get("options")
            ip_module_class.register(app)
            task_instance = ip_module_class()
            sender.register_task(task_instance)
            log.info("successes load job task on deadpool app at on_after_configure.connect")
        except Exception as e:
            log.exception(e)

Python 实现Yaml文件动态加载

import os
import yaml

class Loader(yaml.SafeLoader):

    def __init__(self, stream):

        self._root = os.path.split(stream.name)[0]

        super(Loader, self).__init__(stream)

    def include(self, node):

        filename = os.path.join(self._root, self.construct_scalar(node))

        with open(filename, 'r') as f:
            return yaml.load(f, Loader)

Loader.add_constructor('!include', Loader.include)

Python 实现两个配置文件合并
class Settings(object):
    """
    This function to protect the custom setting config does not influence the program successfully start up.
    """
    # The default program settings
    default_path = os.path.join(SANDBOX_CONFIG_DIR, "default.yaml")

    # The finally running settings
    version = String(default="v1.0.0-alpha", allow_empty=False)
    variable = Variable()
    advanced = Advanced()

    @classmethod
    def loading_settings(cls):
        """
        To merge the settings of default.yaml & the settings of custom.yaml into the running setting.
        :return:
        """
        def merge_dict(target, source):
            for key, value in source.items():
                if isinstance(value, dict):
                    merge_dict(target.__dict__[key], value)
                else:
                    setattr(target, key, value)

        if os.path.exists(cls.default_path):
            with open(cls.default_path) as default_config:
                cls.default_setting = yaml.load(default_config, Loader=yaml.SafeLoader)
                print(json.dumps(cls.default_setting, indent=4))

        # # TODO: where to place the custom.yaml file
        # if os.path.exists(cls.default_path):
        #     with open(cls.default_path) as default_config:
        #         cls.settings = yaml.load(default_config, Loader=yaml.SafeLoader)

        if cls.default_setting:
            merge_dict(cls, cls.default_setting)

        return cls
Python 实现三个自带验证的数据类型
class Int(Type):
    """ Integer Type Definition class """

    __doc__ = """
    Initialise its with params below:
        :param default::默认值
        :param allow_empty::可取空
        :param v_range::范围 eg: (1, 10)
    """

    def __init__(self, default, allow_empty, v_range=None):
        if v_range:
            (self.min_value, self.max_value) = v_range
            if self.min_value > self.max_value:
                raise PandaStartupError("value range incorrect")
        else:
            (self.min_value, self.max_value) = (None, None)
        super(Int, self).__init__(default, allow_empty)

    def parse(self, value):
        def num_range(ctx, val):
            _fin = val
            if ctx.min_value and val < ctx.min_value:
                _fin = ctx.min_value

            if ctx.max_value and ctx.max_value < val:
                _fin = ctx.max_value
            return _fin

        if isinstance(value, Number):
            if isinstance(value, bool):
                return 1 if value else 0

            return num_range(self, value)

        if isinstance(value, str) and value.isdigit():
            _value = int(value)
            return num_range(self, _value)
        else:
            return self.default

    def check(self, value):
        if isinstance(value, Number):
            if isinstance(value, bool):
                return False

            if self.min_value and value < self.min_value:
                return False

            if self.max_value and self.max_value < value:
                return False
        else:
            return False

        return True


class String(Type):
    """ String Type Definition class """

    __doc__ = """
    Initialise its with params below:
        :param default::默认值
        :param allow_empty::可取空
    """

    def parse(self, value):
        return str(value).strip() if value else self.default

    def check(self, value):
        return isinstance(value, str)


class Boolean(Type):
    """ Boolean Type Definition class """

    __doc__ = """
    Initialise its with params below:
        :param default::默认值
        :param allow_empty::可取空
    """

    def parse(self, value):
        if value in ("true", "True", "yes", "1", "on", "open"):
            return True
        if value in ("false", "False", "no", "0", "off", "close"):
            return False
        return self.default

    def check(self, value):
        return isinstance(value, bool)
Python ABC元类编程
import abc


class Type(metaclass=abc.ABCMeta):
    """ Base Class for Panda-Sandbox Type Definitions """

    __doc__ = """
    Must Implement this class for subtype to create new instance.
    Initialise its with params:
    :param default::默认值
    :param allow_empty::可取空
    @Must implement with below methods:
    :method parse::转化
    :method check::校验
    """

    def __init__(self, default=None, allow_empty=False):
        self.default = default
        self.allow_empty = allow_empty

        self._value = None

    def __set__(self, instance, value):
        if self.check(value):
            self._value = value
        else:
            self._value = self.parse(value)

    def __str__(self):
        if not self._value:
            return str(self.default)
        return str(self._value)

    @abc.abstractmethod
    def parse(self, value):
        """ parse a raw input value to correct type value and locate in the value range """

    @abc.abstractmethod
    def check(self, value):
        """ check the type of a raw value whether we need this type in this instance """
Python 列举目录下指定名称文件

import os
import glob

_current_dir = os.path.abspath(os.path.dirname(__file__))
DATA_ROOT = os.path.normpath(os.path.join(_current_dir, "..", "data"))


if __name__ = "__main__":
    data_sets = glob.glob(os.path.join(DATA_ROOT, 'part_*.csv'))
        for item in data_sets:
            print(item)