Learn Python The Hard Way ex41中的程序
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
运行如下:
Learn Python The Hard Way ex41中的程序的更多相关文章
- [读书笔记] learn python the hard way书中 有关powershell 的一些小问题
ex46中,创建自己的python, 当你激活环境时 .\.venvs\lpthw\ Scripts\activate 会报一个错误 此时需要以管理员身份运行PowerShell,(当前的PS不用关 ...
- Learn Python the hard way, ex41 来自Percal 25 号星星的哥顿人
我承认,我偷懒了,少打了大量代码(剧情),英文太差,下次可以编个中文的试试 #!/urs/bin/python #coding:utf-8 from sys import exit from rand ...
- 学习Learn Python The Hard Way 前言中的一段话,可与君共勉
在你通过这本书学习编程时,我正在学习弹吉他.我每天至少训练 2 小时,至少花一个小时练习音阶.和声.和琶音,剩下的时间用来学习音乐理论和歌曲演奏以及训练听力等.有时我一天会花 8 个小时来练习,因为我 ...
- [IT学习]Learn Python the Hard Way (Using Python 3)笨办法学Python3版本
黑客余弦先生在知道创宇的知道创宇研发技能表v3.1中提到了入门Python的一本好书<Learn Python the Hard Way(英文版链接)>.其中的代码全部是2.7版本. 如果 ...
- 笨办法学 Python (Learn Python The Hard Way)
最近在看:笨办法学 Python (Learn Python The Hard Way) Contents: 译者前言 前言:笨办法更简单 习题 0: 准备工作 习题 1: 第一个程序 习题 2: 注 ...
- 《Learn python the hard way》Exercise 48: Advanced User Input
这几天有点时间,想学点Python基础,今天看到了<learn python the hard way>的 Ex48,这篇文章主要记录一些工具的安装,以及scan 函数的实现. 首先与Ex ...
- 学 Python (Learn Python The Hard Way)
学 Python (Learn Python The Hard Way) Contents: 译者前言 前言:笨办法更简单 习题 0: 准备工作 习题 1: 第一个程序 习题 2: 注释和井号 习题 ...
- 如何用Python统计《论语》中每个字的出现次数?10行代码搞定--用计算机学国学
编者按: 上学时听过山师王志民先生一场讲座,说每个人不论干什么,都应该学习国学(原谅我学了计算机专业)!王先生讲得很是吸引我这个工科男,可能比我的后来的那些同学听课还要认真些,当然一方面是兴趣.一方面 ...
- [Python][flask][flask-login]关于flask-login中各种API使用实例
本篇博文跟上一篇[Python][flask][flask-wtf]关于flask-wtf中API使用实例教程有莫大的关系. 简介:Flask-Login 为 Flask 提供了用户会话管理.它处理了 ...
随机推荐
- 【Java每日一题】20161209
package Dec2016; public class Ques1209 { public static void main(String[] args){ People g = new Peop ...
- nginx配置多个虚拟主机vhost
在nginx下配置虚拟主机vhost非常方便.主要在nginx的配置文件nginx.conf中添加一个server即可 比如我想配置两个虚拟主机,通过域名linux.com和linux2.com访问, ...
- java代码实现队列的优化
package com.voole.queun; /** * @Decription 队列 * @author TMAC-J * */ public class Queun { /** * 初始化队列 ...
- 【requireJS路径加载】与程序员小卡的交流
这两天正好看到了程序员小卡同学的一篇博客,里面对requireJS路径的解析做了一些说明,里面有点问题待解决,我这里正好知道一点,所以整理成文,不知对小卡同学是否有帮助. http://www.cnb ...
- 如何在mac上安装docker[记录自己在mac上安装docker的经历]
0.引子 最近入手了一台mac笔记本,想在本地安装docker. 1.找安装文档. 文档地址:http://www.widuu.com/chinese_docker/installation/mac. ...
- 天津政府应急系统之GIS一张图(arcgis api for flex)讲解(四)地图导航控件模块
config.xml文件的配置如下: <widget left="10" top="50" config="widgets/Navigation ...
- OpenGL中glVertex、显示列表(glCallList)、顶点数组(Vertex array)、VBO及VAO区别
OpenGL中glVertex.显示列表(glCallList).顶点数组(Vertex array).VBO及VAO区别 1.glVertex 最原始的设置顶点方法,在glBegin和glEnd之间 ...
- android SQLite 批量插入数据慢的解决方案 (针对于不同的android api 版本)
原地址 :http://www.cnblogs.com/wangmars/p/3914090.html SQLite,是一款轻型的数据库,被广泛的运用到很多嵌入式的产品中,因为占用的资源非常少,二其中 ...
- 【代码笔记】iOS-圆角矩形
代码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. se ...
- 安卓开发之ListAdapter(一)
Adapter常用来管理数据,是连接后端数据和前端显示的适配器接口,是数据和UI(view)之间一个重要的纽带.再常见的view(listview.gridview)等地方都需要用到adapter,下 ...