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 import fields, items
class TestCheckItem(items.CheckItem):
a = fields.StringCheckField(min_length=1,
max_length=2, contains=['a'], excludes=['b'])
b = fields.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._base_values = {}
self._checked = False
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:
self._base_values[key] = value
field = self.fields[key]
check_field = field.get('check_field')
if isinstance(check_field, fields.CheckField):
fe = check_field.from_instance()
fe.input(key, value)
self._values[key] = fe.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('_'):
self.__dict__[name] = value
else:
raise AttributeError(
f"Use item[{name!r}] = {value!r} to set field value")
def keys(self):
if not self._checked: # 默认只检查一次,无论是否检查异常
self._checked = True
self._process_and_check()
_ = [self[field] for field in self.fields]
return self._values.keys()
def checked(self, _checked):
self._checked = _checked
# 示例
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))
关于作者
- 邮箱:1194542196@qq.com
- 微信:hu1194542196
- 目前还需要很多需要改进的地方,可以私信作者,你们的提供越多,本库才能更完善。
Metadata
Release files for gggifcheck 0.0.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| gggifcheck-0.0.5.tar.gz | 8.0 kB | Without provenance | Details for gggifcheck-0.0.5.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| gggifcheck-0.0.5-py3-none-any.whl | Python 3 | none | any | Details for gggifcheck-0.0.5-py3-none-any.whl |
Total release size: 17.1 kB
Release files / gggifcheck-0.0.5.tar.gz
| Download URL | gggifcheck-0.0.5.tar.gz |
|---|---|
| Size | 8.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5bb0568d8c034c048c31864ef6b2a1ebbbbbceb9aae5e94a4dce01be24b60c9d
|
|
BLAKE2b-256 checksum How to use checksums |
3a3ceebe9bbb49447102fdad33a32d9cfec0c5f534fbc56979fb91084e127b11
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.5
|
Release files / gggifcheck-0.0.5-py3-none-any.whl
| Download URL | gggifcheck-0.0.5-py3-none-any.whl |
|---|---|
| Size | 9.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f4c3dc2b09375ebadcd88d2ce59b5d0e608c28c27318ced78c1043ce23c41d79
|
|
BLAKE2b-256 checksum How to use checksums |
2c59f8ca38cf7e94af4a4995048da34a7f5330793acdd08351a0951f115b9a80
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.5
|
Security for gggifcheck 0.0.5
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.