#!/user/bin/env python
#-*-coding:utf-8 -*-
#Author: qinjiaxi
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
PHRASES_FIRST = False
if len(sys.argv) == 2 and sys.argv[1] == 'english':
PHRASES_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('###'))]#从words序列中抽取带'###'的字符串,数量是片段里面含有'###'的数量并把第一个字母变大写,后面变小些(也就是随机抽取类)
other_names = random.sample(WORDS, snippet.count("***"))#从words序列中抽取带'***'的字符串,数量是片段里面含有'***'的数量
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)))#从words列表中随机抽取1-3个参数,加入到参数名列表中并用逗号隔开
for sentence in snippet, phrase:
result = sentence[:]
#fake class_names
for word in class_names:
result = result.replace("###", word, 1)#把第一个'###'替换成word
#fake other_names
for word in sentence:
result = result.replace("***", word, 1)#把第一个'***'替换成word
#fake parameter lists
for word in param_names:
result = result.replace('@@@', word, 1)#把第一个'@@@'替换成word
results.append(result)
return results try:
while True:
snippets = PHRASES.keys()#获取字典中的key并以列表方式返回
random.shuffle(snippets)#将列表中的元素随机打乱 for snippet in snippets:
phrase = PHRASES[snippet]#获取字典中的值
question, answer = convert(snippet, phrase)#调用convert函数
if PHRASES_FIRST:
question, answer = answer, question
print(question)
raw_input('>')
print("ANSWER: %s\n\n" % answer)
except EOFError:
print('\nbye')

  

笨办法学习python-ex41源码加自己注释的更多相关文章

  1. 笨办法学习python之hashmap

    #!/user/bin/env python #-*-coding:utf-8 -*- #Author: qinjiaxi #初始化aMap列表,把列表num_buckets添加到aMap中,num_ ...

  2. python多线程爬取-今日头条的街拍数据(附源码加思路注释)

    这里用的是json+re+requests+beautifulsoup+多线程 1 import json import re from multiprocessing.pool import Poo ...

  3. “笨方法”学习Python笔记(1)-Windows下的准备

    Python入门书籍 来自于开源中国微信公众号推荐的一篇文章 全民Python时代,豆瓣高级工程师告诉你 Python 怎么学 问:请问你目前最好的入门书是那本?有没有和PHP或者其他语言对比讲Pyt ...

  4. Hadoop学习笔记(9) ——源码初窥

    Hadoop学习笔记(9) ——源码初窥 之前我们把Hadoop算是入了门,下载的源码,写了HelloWorld,简要分析了其编程要点,然后也编了个较复杂的示例.接下来其实就有两条路可走了,一条是继续 ...

  5. 深入学习 esp8266 wifimanager源码解析(打造专属自己的web配网)

    QQ技术互动交流群:ESP8266&32 物联网开发 群号622368884,不喜勿喷 单片机菜鸟博哥CSDN 1.前言 废话少说,本篇博文的目的就是深入学习 WifiManager 这个gi ...

  6. 《python解释器源码剖析》第0章--python的架构与编译python

    本系列是以陈儒先生的<python源码剖析>为学习素材,所记录的学习内容.不同的是陈儒先生的<python源码剖析>所剖析的是python2.5,本系列对应的是python3. ...

  7. Mybatis3源码加注释后后编译问题

    参考:https://mp.weixin.qq.com/s/v0ihaPsuyGufdc_ImEqX8A给mybatis3源码加注释并编译源代码 编译命令: mvn clean mvn install ...

  8. 【转】python:让源码更安全之将py编译成so

    python:让源码更安全之将py编译成so 应用场景 Python是一种面向对象的解释型计算机程序设计语言,具有丰富和强大的库,使用其开发产品快速高效. python的解释特性是将py编译为独有的二 ...

  9. Spring源码加载过程图解(一)

    最近看了一下Spring源码加载的简装版本,为了更好的理解,所以在绘图的基础上,进行了一些总结.(图画是为了理解和便于记忆Spring架构) Spring的核心是IOC(控制反转)和AOP(面向切面编 ...

随机推荐

  1. SeleniumHQ

    下载地址:http://www.seleniumhq.org/download/

  2. STM32 i2c通讯失败复位方法

    最近在调研STM32 F10X,准备把公司AVR的MCU项目迁移到STM32上.在调研STM32 i2c这一部分时,在与i2c slave硬件连接断开后,这时再去读/写 i2c slave需要STM3 ...

  3. Centos史上新版最详细步骤-Linux无脑命令式oracle11g静默安装

    1. 关闭selinux 1.1 sed -i "s/SELINUX=enforcing/SELINUX=disabled/" /etc/selinux/config 1.2 或者 ...

  4. QMessage自动定时关闭

    QMessageBox *box = new QMessageBox(QMessageBox::Information,tr("test"),tr("testtest&q ...

  5. Elasticsearch系列---实现分布式锁

    概要 Elasticsearch在文档更新时默认使用的是乐观锁方案,而Elasticsearch利用文档的一些create限制条件,也能达到悲观锁的效果,我们一起来看一看. 乐观锁与悲观锁 乐观锁 E ...

  6. 学习web前端的roadmap

  7. 3年前的一个小项目经验,分享给菜鸟兄弟们(公文收发小软件:小技能 SmallDatetime)...

    为什么80%的码农都做不了架构师?>>>   这个系统中的数据库有100多M,里面当然有很多表,我的每个表里,有几个字段,都是一样的例如 CreateUserID.CreateDat ...

  8. 动态规划经典算法--最长公共子序列 LCS

    转移方程 代码: //法一: #include <bits/stdc++.h> using namespace std; //---------------https://lunatic. ...

  9. STL学习心得

    STL的知识翻来复去,也就那么回事,但是真的想要熟练使用,要下一番功夫.无论是算法,还是STL容器,直白的说就是套路,然而对于一道题,告诉你是STL容器的题,让你套容器也绝非易事. 怎样使用容器,对于 ...

  10. B站弹幕系统架构——GOIM解读

    架构图 说明: 1.logic启动http服务器, 接受http请求,用于将数据推送到kafka以及获取在线用户信息,websocket身份校验 2.comet组件起动webdocket/tcp服务, ...