更新基础模型
This commit is contained in:
parent
ff0dffb36f
commit
9912567004
87
db/model.py
87
db/model.py
@ -1,5 +1,3 @@
|
|||||||
from typing import Union, Any
|
|
||||||
|
|
||||||
from pydantic import BaseModel, Field
|
from pydantic import BaseModel, Field
|
||||||
from bson.objectid import ObjectId
|
from bson.objectid import ObjectId
|
||||||
|
|
||||||
@ -11,79 +9,36 @@ class MdbObjectId(ObjectId):
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def validate(cls, v):
|
def validate(cls, v):
|
||||||
if not isinstance(v, ObjectId):
|
try:
|
||||||
raise TypeError('ObjectId required')
|
res = ObjectId(v)
|
||||||
return v
|
except:
|
||||||
|
raise TypeError('不能装换为 ObjectId')
|
||||||
|
else:
|
||||||
|
return res
|
||||||
|
|
||||||
|
|
||||||
class GBaseModel(BaseModel):
|
class GBaseModel(BaseModel):
|
||||||
"""
|
id: MdbObjectId = Field(..., title="平台", alias='_id')
|
||||||
字段名与保护变量命名冲突,将 _ 前缀变为后缀,读出再还原
|
platform: str = Field(None, title="平台", alias='_platform')
|
||||||
"""
|
channel_name: str = Field(None, title="channel", alias='_channel_name')
|
||||||
id_: MdbObjectId = Field(..., title="平台")
|
owner_name: str = Field(None, title="owner", alias='_owner_name')
|
||||||
platform_: str = Field(None, title="平台")
|
channel_uid: str = Field(None, title="channel_uid", alias='_channel_uid')
|
||||||
channel_name_: str = Field(None, title="channel")
|
device_id: str = Field(None, title='device_id', alias='_device_id')
|
||||||
owner_name_: str = Field(None, title="owner")
|
district_server_id: int = Field(None, title="区服id", alias='_district_server_id')
|
||||||
channel_uid_: str = Field(None, title="channel_uid")
|
game_role_id: str = Field(None, title="角色id", alias='_game_role_id')
|
||||||
device_id_: str = Field(None, title='device_id')
|
event_time: int = Field(..., title="事件时间", alias='_event_time')
|
||||||
district_server_id_: int = Field(None, title="区服id")
|
|
||||||
game_role_id_: str = Field(None, title="角色id")
|
|
||||||
event_time_: int = Field(..., title="事件时间")
|
|
||||||
role_create_time: int = Field(None, title="角色创建时间")
|
role_create_time: int = Field(None, title="角色创建时间")
|
||||||
role_level: int = Field(None, title="角色等级")
|
role_level: int = Field(None, title="角色等级")
|
||||||
role_vip: int = Field(None, title="角色vip等级")
|
role_vip: int = Field(None, title="角色vip等级")
|
||||||
|
role_stage: int = Field(None, title="关卡")
|
||||||
def __init__(self, **data: Any):
|
|
||||||
if isinstance(data.get('_id'), str) and len(data['_id']) == 24:
|
|
||||||
data['_id'] = ObjectId(data['_id'])
|
|
||||||
|
|
||||||
new_data = {}
|
|
||||||
for k, v in data.items(): # type:str,Any
|
|
||||||
if k.startswith('_'):
|
|
||||||
new_k = k[1:] + k[0]
|
|
||||||
new_data[new_k] = v
|
|
||||||
else:
|
|
||||||
new_data[k] = v
|
|
||||||
|
|
||||||
super().__init__(**new_data)
|
|
||||||
|
|
||||||
def dict(
|
|
||||||
self,
|
|
||||||
*,
|
|
||||||
include: Union['AbstractSetIntStr', 'MappingIntStrAny'] = None,
|
|
||||||
exclude: Union['AbstractSetIntStr', 'MappingIntStrAny'] = None,
|
|
||||||
by_alias: bool = False,
|
|
||||||
skip_defaults: bool = None,
|
|
||||||
exclude_unset: bool = False,
|
|
||||||
exclude_defaults: bool = False,
|
|
||||||
exclude_none: bool = False,
|
|
||||||
) -> 'DictStrAny':
|
|
||||||
data = super().dict()
|
|
||||||
|
|
||||||
new_data = {}
|
|
||||||
for k, v in data.items(): # type:str,Any
|
|
||||||
if k.endswith('_'):
|
|
||||||
new_k = k[-1] + k[:-1]
|
|
||||||
new_data[new_k] = v
|
|
||||||
else:
|
|
||||||
new_data[k] = v
|
|
||||||
return new_data
|
|
||||||
|
|
||||||
class Config:
|
|
||||||
arbitrary_types_allowed = True
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_fields(cls):
|
def get_fields(cls):
|
||||||
fields = []
|
return [v.alias for v in cls.__fields__.values()]
|
||||||
for k in cls.__fields__:
|
|
||||||
if k.endswith('_'):
|
|
||||||
fields.append(k[-1] + k[:-1])
|
|
||||||
else:
|
|
||||||
fields.append(k)
|
|
||||||
return fields
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
obj = GBaseModel(_id="5fd0f4812de17aeba6c1a374", role_level='2', aaa=123, _platform=13566)
|
obj = GBaseModel(_id="5fd0f4812de17aeba6c1a373", role_level='2', aaa=123, _platform=13566, _event_time=123456789)
|
||||||
print(obj.dict())
|
print(GBaseModel.get_fields())
|
||||||
print(obj.role_level)
|
print(obj.dict(by_alias=True))
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
from loguru import logger
|
from loguru import logger
|
||||||
|
from .field_type import *
|
||||||
|
|
||||||
logger.add('/data/log/data_cleaning/log.log', format="{time} {level} {name}:{line} {message}", level="INFO",
|
logger.add('/data/log/data_cleaning/log.log', format="{time} {level} {name}:{line} {message}", level="INFO",
|
||||||
rotation="100 MB", retention='7 days',
|
rotation="100 MB", retention='7 days',
|
||||||
|
4
utils/field_type.py
Normal file
4
utils/field_type.py
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
from typing import TypeVar
|
||||||
|
|
||||||
|
IntStr = TypeVar('IntStr', int, str)
|
||||||
|
IntFloat = TypeVar('IntFloat', int, float)
|
Loading…
Reference in New Issue
Block a user