巴甫洛夫数据库连接器
pvlv-database的Python项目详细描述
巴甫洛夫数据库
加快聊天机器人开发的数据库连接器。 它使用mongo数据库,可以在本地托管一个实例,也可以使用免费的mondo Atlas服务。在
包的配置
在项目根目录下创建文件夹'/configs', 在文件夹中添加一个名为数据库.cgf在
强制性参数:
- MONGO_CONNECTION_STRING:到远程数据库的连接字符串, 必须手动创建,请参阅mongo文档
- 数据库名称:数据库的名称
- USERS_TABLE_NAME:保存USERS obj的表的名称
- 帮会名称:保存公会对象的表的名称
[connection]
MONGO_CONNECTION_STRING = mongodb+srv://<username>:<pwsd>@<connection>.mongodb.net/test?retryWrites=true
[database]
DATABASE_NAME = test
USERS_TABLE_NAME = users
GUILDS_TABLE_NAME = guilds
[debug]
DEBUG = True
[cache]
DB_SAVE_INTERVAL = 600 # time in seconds
MAX_CACHE_ITEMS = 100 # max items saved per type, this will use ram
[values]
# Max data log retention, after the time specified the data for the stats will be deleted
# Don't keep an high retention on high granularitis cause il will slow down a lot
MAX_RETENTION_HOUR = 72 # user logs by hour, after 72 hours the data with that eta will be deleted
MAX_RETENTION_DAY = 90
MAX_RETENTION_MONTH = 120
# Time spent calculation config
# This mean that in 11 seconds you ca write a message 30 chars long.
# Others values are calculated with proprortion (there is an anti spam meccanism)
SAMPLE_STRING_LEN = 30
TIME_SAMPLE_VALUE = 11
# XP gain calculation config
# This mean that you can gain 12 in a normal message (logaritmic gain)
# 15 at max (anti spam meccanism).
# If you send 1 char message you'll gain at least 1 xp
XP_SAMPLE_VALUE = 12
XP_MAX_VALUE = 15
XP_NEXT_LEVEL = 300 # each new level have 300 xp more than his predecessor.
Same as for XP
BITS_SAMPLE_VALUE = 5
BITS_MAX_VALUE = 2
基本用途
这是一个使用示例。 不在乎效率!在
您不必调用一次并传递db对象,因为它是内部缓存的 因此它不会生成对远程数据库的新调用。在
^{pr2}$用法,更新程序
轻松更新用户统计所有在一个。在
- 经验值
- 花费的时间
- 比特
- 用户名
- 聊天室名称
frompvlv_databaseimportDatabasefrompvlv_databaseimportBaseStatsUpdaterguild_id=123456789user_id=123456789db=Database(guild_id,user_id)bsu=BaseStatsUpdater(db,timestamp)bsu.text(message_text)bsu.username(username)bsu.guild_name(guild_name)bsu.update_text()
效率
假设您必须在代码的不同模块中多次询问db。 自己测试这个代码。在
fromtimeimporttimefrompvlv_databaseimportDatabaseguild_id=123456789user_id=123456789t1=time()db=Database(guild_id,user_id)# first call 800/2000 ms (on MongoDB Atlas cloud)t2=time()t=(t2-t1)*1000print('Execution time {} ms'.format(int(t)))# other calls in different modulest1=time()db_1=Database(guild_id,user_id)# 1 msdb_2=Database(guild_id,user_id)# 1 mst2=time()t=(t2-t1)*1000print('Execution time {} ms'.format(int(t)))
救世主在另一条线上。所以il不会影响程序速度。 它不是线程安全的。在
- 项目
标签: