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 提供了用户会话管理.它处理了 ...
随机推荐
- [WCF编程]6.绑定行为
一.绑定行为概述 为了支持服务端的其它本地特性,WCF定义了行为的概念.行为就是服务的本地特性,不会影响服务的通信模式.客户端并不知道服务端行为,所以行为不会出现在服务的绑定和发布的元数据中.说下WC ...
- Redis 详解 (一) StackExchange.Redis Client
这期我们来看StackExchange.Redis,这是redis 的.net客户端之一.Redis是一个开源的内存数据存储,可以用来做数据库,缓存或者消息代理服务.目前有不少人在使用ServiceS ...
- PHP资源网站收藏
PHP之道 http://laravel-china.github.io/php-the-right-way/#use_the_current_stable_version http://www.ph ...
- java web学习总结(十六) -------------------数据库连接池
一.应用程序直接获取数据库连接的缺点 用户每次请求都需要向数据库获得链接,而数据库创建连接通常需要消耗相对较大的资源,创建时间也较长.假设网站一天10万访问量,数据库服务器就需要创建10万次连接,极大 ...
- JavaScript中数组去除重复
方式一:常规模式 1.构建一个新的临时数组存放结果 2.for循环中每次从原数组中取出一个元素,用这个元素循环与临时数组对比 3.若临时数组中没有该元素,则存到临时数组中 //方式一: Array.p ...
- c++对象池使用
// // ObjectPool.h // DragonBall // // Created by user on 13-8-22. // // #include <iostream> # ...
- 国内好用的maven仓库,添加到本地nexus中
速度快的maven仓库真的难找,国外网站很多被封了.前天在网上找,有人说阿里有个公开的,速度很快.确实很快! 我添加到本地nexus中,结果不能更新索引,我是这么添加的,如下: 这个地址在浏览器中能够 ...
- elk查询语法
查询指定IP段,如123.123.123.* geo.ip=123.123.123.*
- pull解析器: 反序列化与序列化
pull解析器:反序列化 读取xml文件来获取一个对象的数据 import java.io.FileInputStream; import java.io.IOException; import ja ...
- AndroidStudio配置gradle,让App自动签名
最近开发关于微信一系列功能,发现分享.支付必须要打包签名才能测试,太耽误事了,耗时耗力...在网上扒拉扒拉资料,发现有很多前辈都处理过类似问题,非常感谢大家的分享,参考链接:http://blog.c ...