我该如何重构我的Python类?

0 投票
3 回答
565 浏览
提问于 2025-04-16 14:44

我刚开始学习Python。这是我写的第一个类:

import config   # Ficheiro de configuracao
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

    def shortUrl(self, url):
        bit = bitly_api.Connection(config.bitly_username, config.bitly_key) # Instanciar a API
        return bit.shorten(url) # Encurtar o URL

    def updateTwitterStatus(self, update): 
        short   = self.shortUrl(update["url"]) # Vou encurtar o URL
        update  = update["msg"] + short['url']
        # Will post to twitter and print the posted text
        api     = twitter.Api(consumer_key=config.consumer_key, 
                                   consumer_secret=config.consumer_secret, 
                                   access_token_key=config.access_token_key, 
                                   access_token_secret=config.access_token_secret)
        status  = api.PostUpdate(update) # Fazer o update
        msg     = status.text # Vou gravar o texto enviado para a variavel 'msg'
        # Vou gravar p a Base de Dados
        self.logToDatabase(msg, time.time())
        print msg # So p mostrar o texto enviado. Comentar esta linha de futuro.

x = TwitterC()
x.updateTwitterStatus({"url": "http://xxxx.com/?cat=49", "msg": "Searching for some ....? "})

我想问一下,我应该怎么改进这段我觉得很丑的代码呢?

举个例子。当我尝试复制一个Twitter更新时,我遇到了这个错误:

Traceback (most recent call last):
  File "C:\Users\anlopes\workspace\redes_sociais\src\twitterC.py", line 42, in <module>
    x.updateTwitterStatus({"url": "http://xxx.com/?cat=49", "msg": "Searching for some ...? "})
  File "C:\Users\anlopes\workspace\redes_sociais\src\twitterC.py", line 35, in updateTwitterStatus
    status  = api.PostUpdate(update) # Fazer o update
  File "C:\home_python\python_virtualenv\lib\site-packages\twitter.py", line 2549, in PostUpdate
    self._CheckForTwitterError(data)
  File "C:\home_python\python_virtualenv\lib\site-packages\twitter.py", line 3484, in _CheckForTwitterError
    raise TwitterError(data['error'])
twitter.TwitterError: Status is a duplicate.

那我该怎么在Python中捕捉到这个错误呢?

需要一些提示。

最好的祝福,

3 个回答

1

首先要做的就是把这段代码从类里拿出来。它根本不需要放在类里面。应该把它做成一个模块,里面放一些独立的函数。

编辑以添加更多解释 在Python中,大部分代码自然会被分组到模块里。类主要是在你需要创建多个独立实例,每个实例都有自己的数据时才有用。但在这里并不是这种情况——你只是把类当成一个相关代码的占位符。模块就是用来做这个的。

举个例子,如果你想建模一个单独的推文,它知道自己的内容,并且能把自己保存到数据库里,那确实是使用面向对象编程的好例子。但“与Twitter相关的东西”并不是一个类,而是一个模块。

3

从输出信息来看,你的代码出现了一个叫做twitter.TwitterError的错误。你是这样捕捉这个错误的:

try:
    # yadda yadda
except twitter.TwitterError:
    # exception code
else:
    # happy flow code, optionally.
finally:
    # must-run code, optionally

现在,当你在写你的第一个类,并且不知道如何在某种语言中捕捉错误时,你不应该去尝试获取Twitter的更新并把它们保存到数据库里。你应该先打印“你好,世界!”。去找个教程学学吧 :D。

2

一种可能的方法是写一个函数,用来连接和断开数据库的连接,在连接期间可以做一些事情。这个函数大概长这样:

class DBFactory(object):
    def DBConnection(self, Func, args):
        database      = sqlite3.connect('database.db') # Create a database file
        cursor        = database.cursor() # Create a cursor

        Func(cursor, args)

        database.commit() # Save our changes
        database.close() # Close the connection to the database

现在,Funcargs这两个参数实际上是用来和数据库进行交互的。比如可以这样做:

def CreateTable(cursor, args):
    cursor.execute("CREATE TABLE IF NOT EXISTS {0};".format(args)) # Make a table

如果你想创建一个表格,只需要这样调用:

f = DBFactory()
f.DBConnection(CreateTable, "twitter(id_tweet INTEGER AUTO_INCREMENT PRIMARY KEY, tweet TEXT, timestamp TEXT)"

你可以用类似的方法进行其他数据库操作,比如插入或删除数据。每次都调用DBConnection这个方法。这样可以让你的类结构更清晰,至少在我看来是这样的。

请注意,我上面的代码没有实际测试过,可能会有一些小错误,但我希望你能理解这个思路。希望这对你有帮助!

再见
Woltan

撰写回答