2010-09-15 50 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

相关问题