有没有更好的/更加python化的方式来做到这一点?(Is there a better/more pythonified way to do this?)

编程入门 行业动态 更新时间:2024-10-28 21:20:06
有没有更好的/更加python化的方式来做到这一点?(Is there a better/more pythonified way to do this?)

我一直在自己的新工作中自学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()

I've been teaching myself Python at my new job, and really enjoying the language. I've written a short class to do some basic data manipulation, and I'm pretty confident about it.

But old habits from my structured/modular programming days are hard to break, and I know there must be a better way to write this. So, I was wondering if anyone would like to take a look at the following, and suggest some possible improvements, or put me on to a resource that could help me discover those for myself.

A quick note: The RandomItems root class was written by someone else, and I'm still wrapping my head around the itertools library. Also, this isn't the entire module - just the class I'm working on, and it's prerequisites.

What do you think?

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()

最满意答案

这是我的五美分:

构造函数应该被称为__init__ 。 你可以通过使用random.sample来取消一些代码,它会执行你的next()和sublist() ,但是它是预先打包的。 覆盖__iter__ (在你的类中定义方法),你可以摆脱RandomIter 。 您可以在文档中阅读关于它的更多信息(请注意Py3K,有些内容可能与较低版本无关)。 你可以使用yield ,因为你可能会或可能不知道创建一个generator,因此浪费很少或没有记忆。 random_name可以使用str.join代替。 请注意,如果不能保证是字符串,则可能需要转换这些值。 这可以通过[str(x) for x in iterable]或内置map 。

Here are my five cents:

Constructor should be called __init__. You could abolish some code by using random.sample, it does what your next() and sublist() does but it's prepackaged. Override __iter__ (define the method in your class) and you can get rid of RandomIter. You can read more about at it in the docs (note Py3K, some stuff may not be relevant for lower version). You could use yield for this which as you may or may not know creates a generator, thus wasting little to no memory. random_name could use str.join instead. Note that you may need to convert the values if they are not guaranteed to be strings. This can be done through [str(x) for x in iterable] or in-built map.

更多推荐

本文发布于:2023-07-15 22:03:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1119368.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:这一点   方式   python   pythonified

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!