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 '***'."
}
PHRASE_FIRST = False
if len(sys.argv) == 2 and sys.argv[1] == "english":
PHRASE_FIRST = True
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
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"
root@he-desktop:~/mystuff# python oop_test.py
class Deer(object): def __init__(self, connection) > ANSWER: class Deer has-a __init__ that takes self and connection parameters. class Cause(Agreement): > ANSWER: Make a class named Cause that is-a Agreement. animal.driving(arch) > ANSWER: From animal get the driving function, and call it with parameters self, arch. cat = Aftermath() > ANSWER: Set cat to an instance of class Aftermath. cork.card = 'attempt' >
class mystuff(object):
def __int__(self):
self.tangerine = "Hello"
def apple(self):
print "apple"
thing = mystuff() thing.apple() print thing.tangerine
# 字典 mystuff['apple'] # 模块 mystuff.apple() print mystuff.tangerine # 类 thing = mystuff() thing.apple() print thing.tangerine
class Song(object):
def __init__(self, lyrics):
self.lyrics = lyrics
def sing_me_a_song(self):
for line in self.lyrics:
print line
happy_bday = Song(["Happy birthday to you",
"I don't want to get sued",
"So I'll stop right there"])
bulls_on_parade = Song(["they relly around the family",
"With pockets full of shells"])
happy_bday.sing_me_a_song()
bulls_on_parade.sing_me_a_song()
Happy birthday to you I don't want to get sued So I'll stop right there they relly around the family With pockets full of shells
## Animal is-a object (yes, sort of confusing) look at the extra credit
class Animal(object):
pass
## ?? is-a
class Dog(Animal):
def __init__(self, name):
## ?? has-a
self.name = name
## ?? is-a
class Cat(Animal):
def __init__(self, name):
## ?? has-a
self.name = name
## ?? is-a
class Person(object):
def __init__(self, name):
## ?? has-a
self.name = name
## Person has-a pet of some kind
self.pet = None
## ?? has-a
class Employee(Person):
def __init__(self, name, salary):
## ?? hmm what is this strange magic?
super(Employee, self).__init__(name)
## ?? has-a
self.salary = salary
## ?? is-a
class Fish(object):
pass
## ?? is-a
class Salmon(Fish):
pass
## ?? is-a
class Halibut(Fish):
pass
## rover is-a Dog
rover = Dog("Rover")
## ?? is-a
satan = Cat("Satan")
## ?? is-a
mary = Person("Mary")
## ?? is-a
mary.pet = satan
## ?? is-a
frank = Employee("Frank", 120000)
## ?? is-a
frank.pet = rover
## ?? is-a
flipper = Fish()
## ?? is-a
crouse = Salmon()
## ?? is-a
harry = Halibut()
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有