2010-09-15 71 views
3

我一直在自己的新工作中自學Python,並且非常喜歡這門語言。我寫了一個短小的課來做一些基本的數據操作,我對此非常有信心。是否有更好/更python化的方式來做到這一點?

但是我的結構化/模塊化編程日的舊習慣很難打破,而且我知道必須有更好的方式來編寫它。所以,我想知道是否有人想看看下面的內容,並提出一些可能的改進建議,或者將我提供給可以幫助我發現自己的資源的資源。

快速提示:RandomItems根類是由其他人編寫的,而且我還在圍繞itertools庫進行包裝。此外,這不是整個模塊 - 只是我正在學習的課程,而且它是先決條件。

您認爲如何?

import itertools 
import urllib2 
import random 
import string 

class RandomItems(object): 
    """This is the root class for the randomizer subclasses. These 
     are used to generate arbitrary content for each of the fields 
     in a csv file data row. The purpose is to automatically generate 
     content that can be used as functional testing fixture data. 
    """ 
    def __iter__(self): 
     while True: 
      yield self.next() 

    def slice(self, times): 
     return itertools.islice(self, times) 

class RandomWords(RandomItems): 
    """Obtain a list of random real words from the internet, place them 
     in an iterable list object, and provide a method for retrieving 
     a subset of length 1-n, of random words from the root list. 
    """ 
    def __init__(self): 
     urls = [ 
      "http://dictionary-thesaurus.com/wordlists/Nouns%285,449%29.txt", 
      "http://dictionary-thesaurus.com/wordlists/Verbs%284,874%29.txt", 
      "http://dictionary-thesaurus.com/wordlists/Adjectives%2850%29.txt", 
      "http://dictionary-thesaurus.com/wordlists/Adjectives%28929%29.txt", 
      "http://dictionary-thesaurus.com/wordlists/DescriptiveActionWords%2835%29.txt", 
      "http://dictionary-thesaurus.com/wordlists/WordsThatDescribe%2886%29.txt", 
      "http://dictionary-thesaurus.com/wordlists/DescriptiveWords%2886%29.txt", 
      "http://dictionary-thesaurus.com/wordlists/WordsFunToUse%28100%29.txt", 
      "http://dictionary-thesaurus.com/wordlists/Materials%2847%29.txt", 
      "http://dictionary-thesaurus.com/wordlists/NewsSubjects%28197%29.txt", 
      "http://dictionary-thesaurus.com/wordlists/Skills%28341%29.txt", 
      "http://dictionary-thesaurus.com/wordlists/TechnicalManualWords%281495%29.txt", 
      "http://dictionary-thesaurus.com/wordlists/GRE_WordList%281264%29.txt" 
     ] 
     self._words = [] 
     for url in urls: 
      urlresp = urllib2.urlopen(urllib2.Request(url)) 
      self._words.extend([word for word in urlresp.read().split("\r\n")]) 
     self._words = list(set(self._words)) # Removes duplicates 
     self._words.sort() # sorts the list 

    def next(self): 
     """Return a single random word from the list 
     """ 
     return random.choice(self._words) 

    def get(self): 
     """Return the entire list, if needed. 
     """ 
     return self._words 

    def wordcount(self): 
     """Return the total number of words in the list 
     """ 
     return len(self._words) 

    def sublist(self,size=3): 
     """Return a random segment of _size_ length. The default is 3 words. 
     """ 
     segment = [] 
     for i in range(size): 
      segment.append(self.next()) 
     #printable = " ".join(segment)   
     return segment 

    def random_name(self): 
     """Return a string-formatted list of 3 random words. 
     """ 
     words = self.sublist() 
     return "%s %s %s" % (words[0], words[1], words[2]) 

def main(): 
    """Just to see it work... 
    """ 
    wl = RandomWords() 
    print wl.wordcount() 
    print wl.next() 
    print wl.sublist() 
    print 'Three Word Name = %s' % wl.random_name() 
    #print wl.get() 

if __name__ == "__main__": 
    main() 

回答

5

第一個反應:我將卸載硬編碼的URL到傳遞給該類的構造函數參數中,並可能從配置中讀取;這將允許在不需要重新部署的情況下進行更輕鬆的更改。

這樣做的缺點是該類的消費者必須知道這些URL的存儲位置......所以您可以創建一個伴隨類,其唯一的工作就是知道這些URL是什麼(即在配置中,甚至是在硬盤上編碼)以及如何獲得它們。您可以允許您的課程使用者提供網址,或者如果未提供這些網址,課程可能會爲網址提供配套課程。

+0

對不起,我傾向於迴歸SOLID,不管語言如何。我想不出任何會導致你的班級變得更加Pythonic的特定於Python的東西。 – Randolpho 2010-09-15 14:16:18

+1

不過,這是一個很好的建議:) – Skurmedel 2010-09-15 14:25:04

+1

將硬編碼的URL移出到一個單獨的類是我應該記得從我的舊Java課程。感謝您指出了這一點! – 2010-09-15 15:05:13

相關問題