我是Python的新手,我正在用它做一些测试。在
我需要知道的是处理配置变量的最佳方法是什么。在
例如,对于此代码:
import twitter
import random
import sqlite3
import time
import bitly_api #https://github.com/bitly/bitly-api-python
class TwitterC:
def logtodatabase(self, tweet, timestamp):
# Will log to the database
database = sqlite3.connect('database.db') # Create a database file
cursor = database.cursor() # Create a cursor
cursor.execute("CREATE TABLE IF NOT EXISTS twitter(id_tweet INTEGER AUTO_INCREMENT PRIMARY KEY, tweet TEXT, timestamp TEXT);") # Make a table
# Assign the values for the insert into
msg_ins = tweet
timestamp_ins = timestamp
values = [msg_ins, timestamp_ins]
# Insert data into the table
cursor.execute("INSERT INTO twitter(tweet, timestamp) VALUES(?, ?)", values)
database.commit() # Save our changes
database.close() # Close the connection to the database
在这个代码中,我如何替换'数据库.db'在类外使用变量,以获得更好的配置?在
谨致问候
或者,如果要从命令行传入配置,可以使用argparse。在
然后在创建twitter类时,可以传入所需的配置选项。在
可以创建包含配置变量的python脚本: 配置.py公司名称:
您的文件:
^{pr2}$您可以使用Python标准库中的ConfigParser。在
相关问题 更多 >
编程相关推荐