Skip to content

Commit 2bddecf

Browse files
committed
class study
1 parent a9a8d46 commit 2bddecf

File tree

6 files changed

+72
-12
lines changed

6 files changed

+72
-12
lines changed

spy/spy/spiders/example.json

Whitespace-only changes.

spy/spy/spiders/example.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# -*- coding: utf-8 -*-
22

33
from spy.items import SpyItem
4-
from scrapy import Spider
4+
from scrapy import Spider, Request
55
from scrapy.utils.response import get_base_url
66
from urllib.parse import urljoin
77

@@ -11,6 +11,10 @@ class ExampleSpider(Spider):
1111
allowed_domains = ['tencent.com']
1212
start_urls = ['http://hr.tencent.com/position.php']
1313

14+
def start_requests(self):
15+
for url in self.start_urls:
16+
yield Request(url=url, callback=self.parse)
17+
1418
def parse(self, response):
1519
base_url = get_base_url(response)
1620
sites_even = response.css('tr.even')

studyLib/oop/chain.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
3+
import os
4+
35
__author__ = 'Mr.Huo'
46

57

@@ -9,7 +11,8 @@ def __init__(self, path=''):
911

1012
def __getattr__(self, path):
1113
# print(path)
12-
return Chain('%s/%s' % (self._path, path))
14+
self._path = '%s/%s' % (self._path, path)
15+
return self
1316

1417
def __str__(self):
1518
return self._path
@@ -18,12 +21,13 @@ def __str__(self):
1821

1922
# 任何类,只需要定义一个__call__()方法,就可以直接对实例进行调用
2023
def __call__(self, item):
21-
return Chain('%s:%s' % (self._path, item))
24+
self._path = '%s:%s' % (self._path, item)
25+
return self
2226

2327

2428
def main():
25-
print(Chain('home').status.user.timeline.list)
26-
print(Chain('home').status.user('Huo').timeline.list)
29+
print(id(Chain('home').status.user.timeline.list),Chain('home').status.user.timeline.list)
30+
print(id(Chain('home').status.user('Huo').timeline.list),Chain('home').status.user('Huo').timeline.list)
2731
c = Chain('ZhiXuan')
2832
print(c.status.user.timeline.list)
2933
print(c('HUO'))
@@ -32,4 +36,4 @@ def main():
3236

3337

3438
if __name__ == '__main__':
35-
main()
39+
main()

studyLib/oop/get_set_attr.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
5+
__author__ = 'Mr.Huo'
6+
7+
8+
class Dog():
9+
def __init__(self, name="sun"):
10+
self.name = name
11+
12+
def __getattr__(self, item):
13+
print("call on:%s from __getattr__" % (item))
14+
if hasattr(self, item):
15+
return getattr(self, item)
16+
else:
17+
print("error")
18+
return None
19+
20+
def __setattr__(self, key, value):
21+
print("call on:%s from __setattr__" % (key))
22+
return object.__setattr__(self, key, value)
23+
pass
24+
25+
#def __getattribute__(self, item):
26+
# print("call on:%s from __getattribute__" % (item))
27+
# return object.__getattribute__(self, item)
28+
29+
30+
def main():
31+
dog = Dog("dog")
32+
print('-' * 30)
33+
print(dog.name)
34+
#dog.weight = 100
35+
print(dog.weight)
36+
pass
37+
38+
39+
if __name__ == '__main__':
40+
main()

studyLib/oop/screen.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
# @property @XXX.setter 将属性的读取方法变为属性
77

88
class Screen():
9+
__slots__ = ('_width', '_height')
10+
11+
def __init__(self, width=None, height=None):
12+
self._width = width
13+
self._height = height
14+
915
@property
1016
def width(self):
1117
return self._width

studyLib/oop/specil_name.py renamed to studyLib/oop/special_name.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,32 @@ class Fibonacci(object):
77
def __init__(self, name='Fibonacci'):
88
self.name = name
99
self.a, self.b = 0, 1
10+
self.list = []
1011
print('Init Class %s' % self.name)
1112

1213
# __str__用于print()
1314
def __str__(self):
14-
'''用于print()'''
15+
"""用于print()"""
1516
return self.name
1617

1718
# __repr__()返回程序开发者看到的字符串,也就是说,__repr__()是为调试服务的
1819
__repr__ = __str__
1920

2021
# __iter__()返回迭代对象, 同__next__()做迭代使用
2122
def __iter__(self):
22-
print("[*]when self.a=%d,self.b=%d,__iter__ is called\n " % (self.a, self.b))
23+
print("[*]when self.a=%d,self.b=%d,__iter__ is called!" % (self.a, self.b))
2324
return self
2425

25-
def __next__(self):
26+
def __next__(self, stop=1000):
2627
self.a, self.b = self.b, self.a + self.b
27-
if self.a > 100:
28+
if self.a > stop:
2829
raise StopIteration()
30+
self.list.append(self.a)
2931
return self.a
3032

3133
# __getitem__()取下标操作
3234
def __getitem__(self, item):
33-
pass
35+
return self.list[item]
3436

3537
# __getattr__()方法,动态返回一个属性
3638
def __getattr__(self, item):
@@ -51,7 +53,11 @@ def main():
5153
print(x)
5254
print(fi.test1)
5355
print(fi.test2(1, 2, name='Huo'))
54-
#fi.test3
56+
print(fi[::])
57+
print(fi.list)
58+
for x in fi:
59+
print(x)
60+
print(fi.list)
5561

5662

5763
if __name__ == '__main__':

0 commit comments

Comments
 (0)