import random
from urllib import urlopen
import sys WORD_URL = "http://learncodethehardway.org/words.txt"
WORDS = [] PHRASES = {
"class %%%(%%%):":
"Make a class named %%% that is-a %%%.",
"class %%%(object):\n\tdef __init__(self, ***)":
"class %%% has-a __init__ that takes self and *** parameters.",
"class %%%(object):\n\tdef ***(self, @@@)":
"class %%% has-a function named *** that takes self and @@@ parameters.",
"*** = %%%()":
"Set *** to an instance of class %%%.",
"***.***(@@@)":
"From *** get the *** function, and call it with parameters self, @@@.",
"***.*** = '***'":
"From *** get the *** attribute and set it to '***'."
} #do they want to drill phrases first
PHRASE_FIRST = False
if len(sys.argv) == 2 and sys.argv[1] == "english":
PHRASE_FIRST = True # load up the words from the website
for word in urlopen(WORD_URL).readlines():
WORDS.append(word.strip()) def convert(snippet, phrase):
class_names = [w.capitalize() for w in
random.sample(WORDS, snippet.count("%%%"))]
other_names = random.sample(WORDS, snippet.count("***"))
results = []
param_names = [] for i in range(0, snippet.count("@@@")):
param_count = random.randint(1,3)
param_names.append(', '.join(random.sample(WORDS, param_count))) for sentence in snippet, phrase:
result = sentence[:] #fake class names
for word in class_names:
result = result.replace("%%%",word,1) #fake other_names
for word in other_names:
result = result.replace("***", word, 1) #fake parameter lists
for word in param_names:
result = result.replace("@@@", word, 1) results.append(result) return results # keep going until they hit CTRL-D
try:
while True:
snippets = PHRASES.keys()
random.shuffle(snippets) for snippet in snippets:
phrase = PHRASES[snippet]
question, answer = convert(snippet, phrase)
if PHRASE_FIRST:
question, answer = answer, question print question raw_input("> ")
print "ANSWER: %s\n\n" % answer
except EOFError:
print "\nBye"

这个程序的作用是在网一个网址获取一些单词,用这些单词命名程序的代码块和其中的元素。不管你在>后输入什么,程序都会将该代码块的含义及作用打印出来。

当你按Ctrl+Z的时候,程序退出并打印Bye

运行如下:

最新文章

  1. angularjs 依赖注入--自己学着实现
  2. php实现设计模式之 状态模式
  3. Windows Azure 虚拟机的IP地址操作
  4. 困扰多日的C#调用Haskell问题竟然是Windows的一个坑
  5. POJ 3259 Wormholes(SPFA)
  6. 【转】Ofbiz学习经验谈
  7. Ambiguous mapping found. Cannot map 'xxxxController' bean method
  8. 【JAVA - 基础】之Annotation注解浅析
  9. cf590B Chip 'n Dale Rescue Rangers
  10. Hdu1401-Solitaire(双向bfs)
  11. 基于spark的plsa实现
  12. typedef使用
  13. SQLSERVER误删Windows登录用户
  14. python之路: 基础篇
  15. 12.TCP的成块数据流
  16. python xml sendEmail
  17. DaemonSet 典型应用场景 - 每天5分钟玩转 Docker 容器技术(129)
  18. Entity Framework——并发策略
  19. shell grep
  20. php如何判断数组是一维还是多维

热门文章

  1. php实现文件上传与下载(中)
  2. IOS 类似微博,#话题#,@人,[表情] 网址 正则匹配
  3. SQL Server SA 密码丢失无法连接数据库怎么办?
  4. MongoDB 语法使用小结
  5. BZOJ 1806 IOI2007 Miners 矿工配餐 动态规划
  6. MVC 过滤器 ActionFilterAttribute
  7. 基于ASP.NET MVC的热插拔模块式开发框架(OrchardNoCMS)介绍(二)
  8. JavaScript用typeof判断变量是数组还是对象,都返回object
  9. del命令实现全盘删除指定文件
  10. vue.js权威指南 PDF
  11. 深度拾遗(06) - 1X1卷积/global average pooling
  12. nginx报错 [error] open() “/usr/local/var/run/openresty.pid” failed (2: No such file or directory)
  13. Safari 浏览器模拟iPhone和其他浏览器
  14. 关于VS2017 添加 EF的MVC控制器报错的解决方法
  15. js跳转页面(转)
  16. redis内部分享ppt
  17. 使用http://start.spring.io/ 生成工程
  18. 如何避免HBase写入过快引起的各种问题
  19. 劣质代码评析——《写给大家看的C语言书(第2版)》附录B之21点程序(六)
  20. Locust HTTP client