Skip to main content

通用、便捷、准确的字符串时间解析工具

Project description

gggifcheck

通用条目字段检查(General General General Item Field Check) ,是基于Python编写通用检查工具,适应于各种场景的使用,只需要略微进行适配。

使用方法

安装

pip install gggifcheck

使用CheckField

from gggifcheck.fields import StringCheckField

# 方式1
s = StringCheckField(key='s', value='aa', min_length=1, max_length=2,
                     contains=['a'], excludes=['b'])
print(s.value)
# 方式2
s = StringCheckField(min_length=1, max_length=2, contains=['a'], excludes=['b'])
s.input('s', 'aa')
print(s.value)

使用CheckItem

from gggifcheck.fields import StringCheckField
from gggifcheck.items import CheckItem


class TestCheckItem(CheckItem):
    a = StringCheckField(min_length=1, max_length=2, contains=['a'],
                         excludes=['b'])
    b = StringCheckField(min_length=1, max_length=2, contains=['a'],
                         excludes=['c'])


item = TestCheckItem()
item['a'] = 'aa'
item['b'] = 'ab'
print(dict(item))

使用案例

结合scrapy

# 对scrapy Item进行改写
import scrapy
from gggifcheck import fields
from gggifcheck.items import CheckItem


class ScrapyCheckItem(scrapy.Item, CheckItem):

    def __init__(self, *args, **kwargs):
        self._values = {}
        self.check_fields = {}
        if args or kwargs:
            for k, v in dict(*args, **kwargs).items():
                self[k] = v

    def __getitem__(self, key):
        if key in self.fields and key not in self._values:
            value = None
            for field1, field2 in self.relate_process_default:
                value = self[field2]
                break
            self[key] = value
        return self._values[key]

    def __setitem__(self, key, value):
        if key in self.fields:
            field = self.fields[key]
            build_check_field = field.get('build_check_field')
            if build_check_field:
                check_field = build_check_field.build(key=key, value=value)
                self.check_fields[key] = check_field
                self._values[key] = check_field.value
            else:
                self._values[key] = value
        else:
            raise KeyError(
                f"{self.__class__.__name__} does not support field: {key}")

    def __setattr__(self, name, value):
        if name.startswith('_') or name in ['check_fields']:
            self.__dict__[name] = value
        else:
            raise AttributeError(
                f"Use item[{name!r}] = {value!r} to set field value")

    def keys(self):
        self._process_and_check()
        _ = [self[field] for field in self.fields]  # 进行所有字段检查
        return self._values.keys()

    def get_base_value(self, key):
        if key in self.check_fields:
            return self.check_fields[key].base_value
        elif key in self.fields:
            field = self.fields[key]
            build_check_field = field.get('build_check_field')
            if build_check_field:
                return build_check_field.build_default(
                    key=key, value=None).base_value
            return self.fields[key] or None
        else:
            raise KeyError(
                f"{self.__class__.__name__} does not support field: {key}")


# 示例
class PostItem(ScrapyCheckItem):
    id = scrapy.Field(
        check_field=fields.MD5CheckField(
            nullable=False))
    channel = scrapy.Field(
        check_field=fields.IntegerCheckField(nullable=False, min_value=1,
                                             max_value=6))


item = PostItem()
item['id'] = '81dc9bdb52d04dc20036dbd8313ed055'
item['channel'] = 1
print(dict(item))

关于作者

  1. 邮箱:1194542196@qq.com
  2. 微信:hu1194542196
  3. 目前还需要很多需要改进的地方,可以私信作者,你们的提供越多,本库才能更完善。

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gggifcheck-0.0.2.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gggifcheck-0.0.2-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

Details for the file gggifcheck-0.0.2.tar.gz.

File metadata

  • Download URL: gggifcheck-0.0.2.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.5

File hashes

Hashes for gggifcheck-0.0.2.tar.gz
Algorithm Hash digest
SHA256 644c68448e4f1c203f092096347aa39ca7c963e89637201376887d86033fd92b
MD5 6b780d0cb180a0d7f5f031e5f3229691
BLAKE2b-256 41833fbb4ba4ba84a637f6428b842e9d1ac69c280afe9c5deea09583be948c10

See more details on using hashes here.

File details

Details for the file gggifcheck-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: gggifcheck-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.5

File hashes

Hashes for gggifcheck-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 33dabca0598343edf934e9f7376bddeb491f9cf2d23bf160969a2a85fc874a8c
MD5 7133f56f9d27b6a9b73baf4beadc8e22
BLAKE2b-256 08edaa0c361c95a7effb77b540f438d64697c774dbcc0fe8735bb3bc1f747190

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page