</head> <body> <div id='images-1' style="width: 1230px;"> <a href='image1.html'>Name: Image 1 <br/><img src='image1.jpg' /></a> <a href='image2.html'>Name: Image 2 <br/><img src='image2.jpg' /></a> <a href='image3.html'>Name: Image 3 <br/><img src='image3.jpg' /></a> </div> <div id='images-2' class='small'> <a href='image4.html'>Name: Image 4 <br/><img src='image4.jpg' /></a> <a href='image5.html'>Name: Image 5 <br/><img src='image5.jpg' /></a> </div> </body> </html> ''' response = HtmlResponse(url='http://www.example.com', body=body, encoding='utf8') print('“选中所有的img=>”', response.css('img')) print('“选中所有base和title”', response.css('base, title')) print('“E1 E2:选中E1后代元素中的E2元素”:', response.css('div img')) print('“E1>E2:选中E1子元素中的E2元素”:', response.css('body>div')) print('“[ATTR]:选中包含ATTR属性的元素”', response.css('[style]')) print('“[ATTR=VALUE]:选中包含ATTR属性且值为VALUE的元素”', response.css('[id=images-1]')) print(mycolor.show('“E:nth-child(n):选中E元素,且该元素必须是其父元素的第n个子元素”'), response.css('div>a:nth-child(1)'), '\n', response.css('div:nth-child(2)>a:nth-child(1)')) print(mycolor.show('“E:first-child:选中E元素,该元素必须是其父元素的第一个子元素”'), response.css('div:first-child>a:last-child')) print('“E::text:选中E元素的文本节点”', response.css('a::text'))
e = Enginner_Old('enginner0') print(s.name, e.name) s.set_workedyears(2) e.set_workedyears(5) print(s.workedyears, e.workedyears) print('-------------改造开始-----------------') class Employee: def __init__(self, name): self.name = name def set_workedyears(self, year): self.workedyears = year class Salesman(Employee): pass class Enginner(Employee): pass s1 = Salesman('salesman1') e1 = Enginner('enginner1') s1.set_workedyears(2) e1.set_workedyears(5) print(show(s1.name), show(e1.name)) print(s1.workedyears, e1.workedyears)
class DemoClass: def __init__(self): self.balance = 0 def withdraw_old(self, amount): if amount > self.balance: return -1 else: self.balance -= amount return 0 d = DemoClass() r = d.withdraw_old(10) print(show(r)) print('程序种发现错误的地方,并不一定知道如何处理错误,需要让调用者知道错误' + ' 清楚的将 普通程序 和 错误处理 分开,让程序更容易理解,代码可读性是我们虔诚追求的目标🏆') print('-------------改造开始-----------------') class DemoClass_AfterRF: def __init__(self): self.balance = 0 def withdraw(self, amount): if amount > self.balance: raise Exception('Balance Exception!') self.balance -= amount
def test_afterRF(range): print(show('do things to ', 'red'), range.start, range.end)
winter_rate = 0.01 summer_rate = 0.02 mydate = parser.parse('2018-02-27').date() mydate += datetime.timedelta(days=random.randint(-300, 300)) print(mydate) myquantity = 10000 SUMMER_START = parser.parse('2018-06-01').date() SUMMER_END = parser.parse('2018-09-01').date() if mydate < SUMMER_START or mydate > SUMMER_END: charge = myquantity * winter_rate * 1.1 else: charge = myquantity * summer_rate print(show(charge)) print('- · - - · - - · - - · - ') def not_summer(mydate): if mydate < SUMMER_START or mydate > SUMMER_END: return True else: return False def winter_charge(quantity): return quantity * winter_rate * 1.1 def summer_charge(quantity):
html1 = open('example1.html').read() html2 = open('example2.html').read() response1 = HtmlResponse(url='http://example1.com', body=html1, encoding='utf8') response2 = HtmlResponse(url='http://example2.com', body=html2, encoding='utf8') le = LinkExtractor() links = le.extract_links(response1) for link in links: print(link) print(mycolor.show('allow 参数 --------')) pattern = '/intro/.+\.html$' le = LinkExtractor(allow=pattern) links = le.extract_links(response1) for link in links: print(link) print(mycolor.show('deny 参数 --------')) print('“示例 提取页面example1.html中所有站外链接(即排除站内链接)”') pattern1 = pattern1 = '^' + urlparse(response1.url).geturl() print(pattern1) le = LinkExtractor(deny=pattern1) links = le.extract_links(response1) for link in links: print(link)
if type == Old_Employee.engineer: return monthly_salary elif type == Old_Employee.salesman: return monthly_salary + commission elif type == Old_Employee.manager: return monthly_salary + bonus else: raise ValueError('what happened?') e = Old_Employee(2) print(e.type, e.pay_amount(e.type)) print('假设工程师可以晋升为经理,对象类型是可变,我们不能' + '使用继承方式处理类型码') print(show('开始改造'), '- > -' * 5) class Employee: engineer = 0 salesman = 1 manager = 2 monthly_salary = 10000 commission = 5000 bonus = 20000 def __init__(self, code): self.type = EmployeeType().new_type(code) def pay_amount(self):
else: return 1 def discounted_price_old(base_price, discount_level): if discount_level == 2: return base_price * 0.1 else: return base_price * 0.05 quantity = 1000 item_price = 9.8 base_price = quantity * item_price discounted_level = get_discount_level(quantity) final_price = discounted_price_old(base_price, discounted_level) print('final_price=', show(final_price)) print('-------------改造开始-----------------') def discounted_price(base_price): discount_level = get_discount_level(quantity) if discount_level == 2: return base_price * 0.1 else: return base_price * 0.05 final_price1 = discounted_price(base_price) print(show(final_price1, 'red'))
def with_range(days_temp_range): print(show('do things to :', 'red'), days_temp_range.low, days_temp_range.high)
from mycolor import show row = [] row.append('Livepool') row.append(15) print(row[0], row[1]) print(show('数组是以某种顺序容纳一组相似的对象'), '有时候很难记第一个元素是人名,对象可以用字段名称和\ 函数传达这样的信息,无须死机它') class Performance: def __init__(self, name, wins): self.name = name self.wins = wins r = Performance('Livepool', 15) print(r.name, r.wins)
from mycolor import show _values = ['test0', 'test1'] def get_value_for_period_old(period_number): try: return _values[period_number] except ValueError: print("Oops! That was wrong period_number!") return 0 get_value_for_period_old(1) print('异常 只应该呗用于异常、罕见的行为,'+show('不应该成为条件检查的替代品')) print('-------------改造开始-----------------') def get_value_for_period(period_number): if period_number >= len(_values): return 0 return _values[period_number] print(get_value_for_period(1)) print(get_value_for_period(2))
from scrapy.selector import Selector from scrapy.http import HtmlResponse import mycolor text_content = '<a href="#">click here to go to the \ <strong>Next Page</strong></a>' sel = Selector(text=text_content) print(sel) print(sel.xpath('/html/body/a/strong/text()')) print(mycolor.show('same as 使用string函数:'), sel.xpath('string(/html/body/a/strong)').extract()) print('如果想得到a 中的整个字符串Click here to go to the Next Page') print(sel.xpath('/html/body/a//text()')) print(sel.xpath('string(/html/body/a)').extract())
from dateutil import parser import datetime from mycolor import show class MyDate(datetime.datetime): def next_day(self): newstart = self + datetime.timedelta(days=1) return newstart d = MyDate(2018, 2, 24, 14, 37) print('new way:', show(d.next_day(), 'red'))
import math from mycolor import show def special_calculate(x): if x > 0: return math.sqrt(x) * 10 else: raise ValueError('Error here!') t0 = special_calculate(4) # t1 = special_calculate(-4) print('某段代码对程序状态作出某种假设' + show(' 用断言明确表现这种假设')) print('- · - - · 开始改造 - - · - - · - ') def special_calculate_with_assert(x): assert x > 0 return math.sqrt(x) * 10 t2 = special_calculate_with_assert(4) # t2 = special_calculate_with_assert(-4) print(t0, t2)
from mycolor import show class Order: def __init__(self, customer): self.customer = customer d = Order('xiaoming') print('old:', show(d.customer)) class Order_afterRF: def __init__(self, customer): self.customer = customer class Customer: def __init__(self, name): self.name = name d1 = Order_afterRF(Customer('xiaoming1')) print(show(d1.customer.name, 'red'))
def get_total_price(self): return self.get_unit_price() * self.quantity def get_unit_price(self): return self.employee.rate if self.is_labor else self.unit_price class Employee: def __init__(self, rate): self.rate = rate j = JobItem_old(9.8, 10000, True, Employee(0.99)) t = j.get_total_price() print(show(t)) print('-------------改造开始-----------------') class JobItem: def __init__(self, unit_price, quantity): self.unit_price = unit_price self.quantity = quantity self.is_labor = False # self.employee = employee def get_total_price(self): return self.get_unit_price() * self.quantity def get_unit_price(self): return self.unit_price
from mycolor import show def potential_energy(mass, height): return mass * 9.81 * height old = potential_energy(10, 100) print('magic number way:', show(old)) print(show('开始改造'),'- > -'*5) GRAVITATIONAL_CONSTANT = 9.81 def potential_energy_afterRF(mass, height): return mass * GRAVITATIONAL_CONSTANT * height new = potential_energy_afterRF(10, 100) print('symbolic constant way:', show(new, 'red'))
class BillingPlan: def basic(self): return 'By failing to prepare, you are preparing to fail.' customer = None billing_plan = BillingPlan() print('-·--·--·--·-正文部分-·-·-·-·-·-·-·') if customer == None: plan = billing_plan.basic() else: plan = customer.get_plan() print(show(plan)) print('- · - - · 开始改造 - - · - - · - ') class NullCustomer(Customer): def get_plan(self): return BillingPlan().basic() print(customer, customer == None) customer1 = NullCustomer() if customer == None else Customer() plan = customer1.get_plan() print(show(plan, 'red'))
from mycolor import show class Person: def __init__(self, name, dpmt): self.name = name self.department = dpmt def __str__(self): return 'name:' + self.name class Department: def __init__(self, chargecode, manager): self.chargecode = chargecode self.manager = manager john = Person('john1', Department('01', Person('bob', None))) should_way_manager = john.department.manager print(show(should_way_manager))
def get_invoiceable_credit_limit(): print(show(' I am batter than getinvcdlimt'))
return getbase_speed() elif type == AFRICAN: return getbase_speed() - get_loadfactor() * coconuts elif type == NORWEGIAN: return 0 if is_nailed else getbase_speed(voltage) else: raise Exception('error!') def getbase_speed(p=1): return p * 10 def get_loadfactor(): return 100 t = get_speed_old(2) print(show(t)) print('- · - - · - - · - - · - ') class CountryFactory(object): @staticmethod def create(type): if type == EROPEAN: return Eropean() elif type == African: return AFRICAN() elif type == NORWEGIAN: return Norwegian() class Country:
from dateutil import parser import datetime from mycolor import show yourinput_date = '2018-02-24 14:19' dt = parser.parse(yourinput_date) newstart = dt + datetime.timedelta(days=1) print('old way:', show(newstart)) print('如果这种事情发生太多次;重复代码是万恶之源,重复代码应该被抽出来放在一个函数,\ 这个函数原本应该在提供服务的类中实现,只不过无法修改\ 不要忘记:这只是权宜之计') def next_day(input_date): dt = parser.parse(input_date) newstart = dt + datetime.timedelta(days=1) return newstart print('new way:', show(next_day(yourinput_date), 'red'))
senirotiy = 1 months_disabled = 13 is_parttime = True def disability_amount(): if senirotiy < 2: return 0 if months_disabled > 12: return 0 if is_parttime: return 0 print(show(disability_amount())) print('有一系列条件测试,都得到相同结果;' + '你可以将测试合并为一个条件表达式, 将这个条件表达式提炼为独立函数') print('- · - - · - - · - - · - ') def isnot_eligable_for_disability(): if senirotiy < 2 or months_disabled > 12 \ or is_parttime: return True def disability_amount_afterRF(): if isnot_eligable_for_disability(): return 0
from mycolor import show class Order: def __init__(self, customer): self.customer = customer class Customer: def __init__(self, name): self.name = name d0 = Order(Customer('xiaoming1')) print(show(d0.customer.name)) c0 = Customer('c') c1 = Customer('c') print( c0 == c1) class Customer_afterRF: instance = None def __init__(self, name): self.name = name @staticmethod def get_instnace(name): if Customer_afterRF.instance is None: Customer_afterRF.instance = Customer_afterRF(name) return Customer_afterRF.instance