巴甫洛夫数据库连接器

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不会影响程序速度。 它不是线程安全的。在

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java JavaFX触控事件未触发Ubuntu 20.04触控笔记本电脑   java如何在AWT中关闭窗口?   java Dagger 2:注入具有构造函数参数的成员   创建对象的Java调用类   对象我想在A.java中添加两个数字,并在B.java中打印结果(如何?)   java如何使用AWS SDK for Android从数字海洋空间下载图像?   java Facebook sdk 4.0.1无法使用Android studio获取某些字段   4分钟后web应用程序(Angular 8和Rest API)中的java自动会话超时   在Eclipse for Java EE developers edition中禁用HTML警告   java按字母顺序排列字符串我错过了什么明显的东西吗?   java在Jshell中println和printf有什么不同