【发布时间】:2011-03-25 18:15:31
【问题描述】:
我是 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
在此代码中,如何将“database.db”替换为类外的变量,以获得更好的配置。 ?
最好的问候,
【问题讨论】:
标签: python variables configuration